// File: /workspace/github_cpp_makefile_star2_O1/tsoding[P]wcc/src/./wcc_type_checker.cpp
// Function: Type_Checker::check_types_of_expression(Expression*)
Expression *Type_Checker::check_types_of_expression(Expression *expression)
{
    assert(expression->type == Type::Unchecked);

    switch (expression->kind) {
    case Expression_Kind::Type_Cast: {
        expression->type_cast.expression =
            cast_expression_to(
                check_types_of_expression(expression->type_cast.expression),
                expression->type_cast.type);
        expression->type = expression->type_cast.type;
    } break;

    case Expression_Kind::Number_Literal: {
        if (expression->number_literal.unwrap <= 0xFF) {
            expression->type = Type::U8;
        } else if (expression->number_literal.unwrap <= 0xFF'FF'FF'FF) {
            expression->type = Type::U32;
        } else {
            expression->type = Type::U64;
        }
    } break;

    case Expression_Kind::Bool_Literal: {
        expression->type = Type::Bool;
    } break;

    case Expression_Kind::Variable: {
        expression->type = type_of_name(expression->offset, expression->variable.name);
    } break;

    case Expression_Kind::Multiply:
    case Expression_Kind::Rem:
    case Expression_Kind::Minus:
    case Expression_Kind::Plus: {
        expression->binary_op.lhs = check_types_of_expression(expression->binary_op.lhs);
        expression->binary_op.rhs = check_types_of_expression(expression->binary_op.rhs);
        auto lhs_type = expression->binary_op.lhs->type;
        auto rhs_type = expression->binary_op.rhs->type;

        if (is_expression_implicitly_convertible_to(expression->binary_op.lhs, rhs_type)) {
            expression->binary_op.lhs = cast_expression_to(expression->binary_op.lhs, rhs_type);
            expression->type = rhs_type;
        } else if (is_expression_implicitly_convertible_to(expression->binary_op.rhs, lhs_type)) {
            expression->binary_op.rhs = cast_expression_to(expression->binary_op.rhs, lhs_type);
            expression->type = lhs_type;
        } else {
            reporter.fail(expression->binary_op.rhs->offset, "Types `", lhs_type, "` and `", rhs_type, "` are not comparable with each other");
        }
    } break;

    case Expression_Kind::Equals:
    case Expression_Kind::Less_Equals:
    case Expression_Kind::Greater: {
        expression->binary_op.lhs = check_types_of_expression(expression->binary_op.lhs);
        expression->binary_op.rhs = check_types_of_expression(expression->binary_op.rhs);
        auto lhs_type = expression->binary_op.lhs->type;
        auto rhs_type = expression->binary_op.rhs->type;

        if (is_expression_implicitly_convertible_to(expression->binary_op.lhs, rhs_type)) {
            expression->binary_op.lhs = cast_expression_to(expression->binary_op.lhs, rhs_type);
        } else if (is_expression_implicitly_convertible_to(expression->binary_op.lhs, lhs_type)) {
            expression->binary_op.rhs = try_implicitly_convert_expression_to(expression->binary_op.rhs, lhs_type);
        } else {
            reporter.fail(expression->binary_op.rhs->offset, "Types `", lhs_type, "` and `", rhs_type, "` are not comparable with each other");
        }
        expression->type = Type::Bool;
    } break;

    case Expression_Kind::And: {
        expression->binary_op.lhs = try_implicitly_convert_expression_to(
            check_types_of_expression(expression->binary_op.lhs),
            Type::Bool);
        expression->binary_op.rhs = try_implicitly_convert_expression_to(
            check_types_of_expression(expression->binary_op.rhs),
            Type::Bool);
        expression->type = Type::Bool;
    } break;
    }

    return expression;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
cmpl $0x0, 0x4(%rsi)
jne 0x20ee
movq %rsi, %rbx
movl (%rsi), %eax
cmpq $0xb, %rax
ja 0x20dc
movq %rdi, %r15
movl $0x5, %r14d
leaq 0x8160(%rip), %rcx      # 0xa02c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x10(%rbx), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x1e92
movq %r14, 0x10(%rbx)
movq 0x18(%rbx), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x1e92
movq %r14, 0x18(%rbx)
movq 0x10(%rbx), %rdi
movl 0x4(%rdi), %r12d
movl 0x4(%r14), %r14d
movl %r14d, %esi
callq 0x1d00
testb %al, %al
je 0x1f99
leaq 0x10(%rbx), %r13
jmp 0x1fb4
movq 0x10(%rbx), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x1e92
movq %r12, 0x10(%rbx)
movq 0x18(%rbx), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x1e92
movq %r12, 0x18(%rbx)
movq 0x10(%rbx), %rdi
movl 0x4(%rdi), %eax
movl %eax, -0x2c(%rbp)
movl 0x4(%r12), %r12d
movl %r12d, %esi
callq 0x1d00
movq 0x10(%rbx), %r13
testb %al, %al
je 0x1fe9
movq (%r15), %rdi
movl $0x1, %esi
callq 0x71e2
movl $0x4, (%rax)
movl %r12d, 0x4(%rax)
movq 0x8(%r13), %rcx
movq %rcx, 0x8(%rax)
movl %r12d, 0x10(%rax)
movq %r13, 0x18(%rax)
movq %rax, 0x10(%rbx)
jmp 0x20d8
leaq 0x18(%rbx), %r13
movq (%r13), %rdi
movl %r12d, %esi
callq 0x1d00
testb %al, %al
je 0x210d
movl %r12d, %r14d
movq (%r13), %r12
movq (%r15), %rdi
movl $0x1, %esi
callq 0x71e2
movl $0x4, (%rax)
movl %r14d, 0x4(%rax)
movq 0x8(%r12), %rcx
movq %rcx, 0x8(%rax)
movl %r14d, 0x10(%rax)
movq %r12, 0x18(%rax)
movq %rax, (%r13)
jmp 0x20d8
movq %r13, %rdi
movl -0x2c(%rbp), %r13d
movl %r13d, %esi
callq 0x1d00
testb %al, %al
je 0x2140
movq 0x18(%rbx), %rsi
movq %r15, %rdi
movl %r13d, %edx
jmp 0x2065
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
movq 0x18(%rbx), %rcx
movq %r15, %rdi
callq 0x1b64
movl %eax, %r14d
jmp 0x20d8
movq 0x10(%rbx), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x1e92
movq %r15, %rdi
movq %r12, %rsi
movl $0x5, %edx
callq 0x1d7c
movq %rax, 0x10(%rbx)
movq 0x18(%rbx), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x1e92
movq %r15, %rdi
movq %r12, %rsi
movl $0x5, %edx
callq 0x1d7c
movq %rax, 0x18(%rbx)
jmp 0x20d8
movq 0x10(%rbx), %rax
movl $0x2, %r14d
cmpq $0x100, %rax            # imm = 0x100
jb 0x20d8
shrq $0x20, %rax
cmpq $0x1, %rax
movl $0x4, %r14d
sbbl $0x0, %r14d
jmp 0x20d8
movq 0x18(%rbx), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x1e92
movl 0x10(%rbx), %r12d
movq (%r15), %rdi
movl $0x1, %esi
callq 0x71e2
movl $0x4, (%rax)
movl %r12d, 0x4(%rax)
movq 0x8(%r14), %rcx
movq %rcx, 0x8(%rax)
movl %r12d, 0x10(%rax)
movq %r14, 0x18(%rax)
movq %rax, 0x18(%rbx)
movl 0x10(%rbx), %r14d
movl %r14d, 0x4(%rbx)
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8518(%rip), %rdi      # 0xa60d
leaq 0x830d(%rip), %rsi      # 0xa409
leaq 0x852e(%rip), %rcx      # 0xa631
movl $0xaa, %edx
callq 0x1090
addq $0x8, %r15
movq (%r13), %rax
movq 0x8(%rax), %rsi
leaq 0x8563(%rip), %rax      # 0xa683
movq %rax, (%rsp)
leaq 0x8548(%rip), %rdx      # 0xa673
leaq 0x8549(%rip), %r8       # 0xa67b
movq %r15, %rdi
movl %r12d, %ecx
movl %r14d, %r9d
callq 0x726a
addq $0x8, %r15
movq 0x18(%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0x8530(%rip), %rax      # 0xa683
movq %rax, (%rsp)
leaq 0x8515(%rip), %rdx      # 0xa673
leaq 0x8516(%rip), %r8       # 0xa67b
movq %r15, %rdi
movl %r13d, %ecx
movl %r12d, %r9d
callq 0x726a
nop