// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %rax
cmpl $0x0, 0x4(%rsi)
jne 0x1f1b
movq %rsi, %rbx
movl (%rsi), %eax
cmpq $0xb, %rax
ja 0x1f09
movq %rdi, %r14
pushq $0x5
popq %r15
leaq 0x62c5(%rip), %rcx      # 0x802c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x10(%rbx), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x1d32
movq %r15, 0x10(%rbx)
movq 0x18(%rbx), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x1d32
movq %r15, 0x18(%rbx)
movq 0x10(%rbx), %rdi
movl 0x4(%rdi), %r12d
movl 0x4(%r15), %r15d
movl %r15d, %esi
callq 0x1bc8
testb %al, %al
je 0x1e0a
leaq 0x10(%rbx), %r13
jmp 0x1e25
movq 0x10(%rbx), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x1d32
movq %r12, 0x10(%rbx)
movq 0x18(%rbx), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x1d32
movq %r13, 0x18(%rbx)
movq 0x10(%rbx), %rdi
movl 0x4(%rdi), %r12d
movl 0x4(%r13), %r13d
movl %r13d, %esi
callq 0x1bc8
movq 0x10(%rbx), %rsi
testb %al, %al
je 0x1e3d
movq %r14, %rdi
movl %r13d, %edx
callq 0x1b94
movq %rax, 0x10(%rbx)
jmp 0x1f05
leaq 0x18(%rbx), %r13
movq (%r13), %rdi
movl %r12d, %esi
callq 0x1bc8
testb %al, %al
je 0x1f3a
movl %r12d, %r15d
movq (%r13), %rsi
movq %r14, %rdi
movl %r15d, %edx
callq 0x1b94
movq %rax, (%r13)
jmp 0x1f05
movq %rsi, %rdi
movl %r12d, %esi
callq 0x1bc8
testb %al, %al
je 0x1f6d
movq 0x18(%rbx), %rsi
movq %r14, %rdi
movl %r12d, %edx
jmp 0x1eb1
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
movq 0x18(%rbx), %rcx
movq %r14, %rdi
callq 0x1a40
movl %eax, %r15d
jmp 0x1f05
movq 0x10(%rbx), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x1d32
movq %r14, %rdi
movq %r12, %rsi
movl %r15d, %edx
callq 0x1c36
movq %rax, 0x10(%rbx)
movq 0x18(%rbx), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x1d32
movq %r14, %rdi
movq %r12, %rsi
movl %r15d, %edx
callq 0x1c36
movq %rax, 0x18(%rbx)
jmp 0x1f05
movq 0x10(%rbx), %rax
cmpq $0x100, %rax            # imm = 0x100
jae 0x1ef5
pushq $0x2
popq %r15
jmp 0x1f05
movq 0x18(%rbx), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x1d32
movl 0x10(%rbx), %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1b94
movq %rax, 0x18(%rbx)
movl 0x10(%rbx), %r15d
jmp 0x1f05
shrq $0x20, %rax
cmpq $0x1, %rax
pushq $0x4
popq %r15
sbbl $0x0, %r15d
movl %r15d, 0x4(%rbx)
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x66ff(%rip), %rdi      # 0x8621
leaq 0x64f4(%rip), %rsi      # 0x841d
leaq 0x6715(%rip), %rcx      # 0x8645
movl $0xaa, %edx
callq 0x1090
addq $0x8, %r14
movq (%r13), %rax
movq 0x8(%rax), %rsi
leaq 0x674a(%rip), %rax      # 0x8697
movq %rax, (%rsp)
leaq 0x672f(%rip), %rdx      # 0x8687
leaq 0x6730(%rip), %r8       # 0x868f
movq %r14, %rdi
movl %r12d, %ecx
movl %r15d, %r9d
callq 0x5d62
addq $0x8, %r14
movq 0x18(%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0x6717(%rip), %rax      # 0x8697
movq %rax, (%rsp)
leaq 0x66fc(%rip), %rdx      # 0x8687
leaq 0x66fd(%rip), %r8       # 0x868f
movq %r14, %rdi
movl %r12d, %ecx
movl %r13d, %r9d
callq 0x5d62