// File: /workspace/github_cpp_makefile_star2_O3/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 0x20f0
movq %rsi, %rbx
movl (%rsi), %eax
cmpq $0xb, %rax
ja 0x20de
movq %rdi, %r15
movl $0x5, %r14d
leaq 0x715e(%rip), %rcx      # 0x902c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x10(%rbx), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x1e94
movq %r14, 0x10(%rbx)
movq 0x18(%rbx), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x1e94
movq %r14, 0x18(%rbx)
movq 0x10(%rbx), %rdi
movl 0x4(%rdi), %r12d
movl 0x4(%r14), %r14d
movl %r14d, %esi
callq 0x1d02
testb %al, %al
je 0x1f9b
leaq 0x10(%rbx), %r13
jmp 0x1fb6
movq 0x10(%rbx), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x1e94
movq %r12, 0x10(%rbx)
movq 0x18(%rbx), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x1e94
movq %r12, 0x18(%rbx)
movq 0x10(%rbx), %rdi
movl 0x4(%rdi), %eax
movl %eax, -0x2c(%rbp)
movl 0x4(%r12), %r12d
movl %r12d, %esi
callq 0x1d02
movq 0x10(%rbx), %r13
testb %al, %al
je 0x1feb
movq (%r15), %rdi
movl $0x1, %esi
callq 0x6f46
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 0x20da
leaq 0x18(%rbx), %r13
movq (%r13), %rdi
movl %r12d, %esi
callq 0x1d02
testb %al, %al
je 0x210f
movl %r12d, %r14d
movq (%r13), %r12
movq (%r15), %rdi
movl $0x1, %esi
callq 0x6f46
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 0x20da
movq %r13, %rdi
movl -0x2c(%rbp), %r13d
movl %r13d, %esi
callq 0x1d02
testb %al, %al
je 0x2142
movq 0x18(%rbx), %rsi
movq %r15, %rdi
movl %r13d, %edx
jmp 0x2067
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
movq 0x18(%rbx), %rcx
movq %r15, %rdi
callq 0x1b7c
movl %eax, %r14d
jmp 0x20da
movq 0x10(%rbx), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x1e94
movq %r15, %rdi
movq %r12, %rsi
movl $0x5, %edx
callq 0x1d7e
movq %rax, 0x10(%rbx)
movq 0x18(%rbx), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x1e94
movq %r15, %rdi
movq %r12, %rsi
movl $0x5, %edx
callq 0x1d7e
movq %rax, 0x18(%rbx)
jmp 0x20da
movq 0x10(%rbx), %rax
movl $0x2, %r14d
cmpq $0x100, %rax            # imm = 0x100
jb 0x20da
shrq $0x20, %rax
cmpq $0x1, %rax
movl $0x4, %r14d
sbbl $0x0, %r14d
jmp 0x20da
movq 0x18(%rbx), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x1e94
movl 0x10(%rbx), %r12d
movq (%r15), %rdi
movl $0x1, %esi
callq 0x6f46
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 0x7536(%rip), %rdi      # 0x962d
leaq 0x732b(%rip), %rsi      # 0x9429
leaq 0x754c(%rip), %rcx      # 0x9651
movl $0xaa, %edx
callq 0x1090
addq $0x8, %r15
movq (%r13), %rax
movq 0x8(%rax), %rsi
leaq 0x7581(%rip), %rax      # 0x96a3
movq %rax, (%rsp)
leaq 0x7566(%rip), %rdx      # 0x9693
leaq 0x7567(%rip), %r8       # 0x969b
movq %r15, %rdi
movl %r12d, %ecx
movl %r14d, %r9d
callq 0x6fce
addq $0x8, %r15
movq 0x18(%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0x754e(%rip), %rax      # 0x96a3
movq %rax, (%rsp)
leaq 0x7533(%rip), %rdx      # 0x9693
leaq 0x7534(%rip), %r8       # 0x969b
movq %r15, %rdi
movl %r13d, %ecx
movl %r12d, %r9d
callq 0x6fce
nop