// File: 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
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0x2704
jmp 0x2723
leaq 0xc15e(%rip), %rdi      # 0xe869
leaq 0xbf2f(%rip), %rsi      # 0xe641
movl $0xaa, %edx
leaq 0xc16f(%rip), %rcx      # 0xe88d
callq 0x10c0
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x40(%rbp)
subq $0xb, %rax
ja 0x2aa6
movq -0x40(%rbp), %rax
leaq 0xb8ea(%rip), %rcx      # 0xe02c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x26e0
movq -0x38(%rbp), %rdi
movq %rax, %rsi
movq -0x10(%rbp), %rax
movl 0x10(%rax), %edx
callq 0x2410
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x2aa6
movq -0x10(%rbp), %rax
cmpq $0xff, 0x10(%rax)
ja 0x27a8
movq -0x10(%rbp), %rax
movl $0x2, 0x4(%rax)
jmp 0x27d1
movq -0x10(%rbp), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, 0x10(%rax)
ja 0x27c4
movq -0x10(%rbp), %rax
movl $0x3, 0x4(%rax)
jmp 0x27cf
movq -0x10(%rbp), %rax
movl $0x4, 0x4(%rax)
jmp 0x27d1
jmp 0x2aa6
movq -0x10(%rbp), %rax
movl $0x5, 0x4(%rax)
jmp 0x2aa6
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, -0x20(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0x21e0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x2aa6
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x26e0
movq -0x38(%rbp), %rdi
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x26e0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x4(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x4(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x28(%rbp), %esi
callq 0x2480
testb $0x1, %al
jne 0x288b
jmp 0x28b6
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movl -0x28(%rbp), %edx
callq 0x2410
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x2932
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x24(%rbp), %esi
callq 0x2480
testb $0x1, %al
jne 0x28cc
jmp 0x28f7
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movl -0x24(%rbp), %edx
callq 0x2410
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x2930
movq -0x38(%rbp), %rdi
addq $0x8, %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rsi
movl -0x24(%rbp), %ecx
movl -0x28(%rbp), %r9d
leaq 0xbfb6(%rip), %rdx      # 0xe8cf
leaq 0xbfb7(%rip), %r8       # 0xe8d7
leaq 0xbfb8(%rip), %rax      # 0xe8df
movq %rax, (%rsp)
callq 0xa3c0
jmp 0x2932
jmp 0x2aa6
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x26e0
movq -0x38(%rbp), %rdi
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x26e0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x4(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x4(%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x30(%rbp), %esi
callq 0x2480
testb $0x1, %al
jne 0x29a1
jmp 0x29c2
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movl -0x30(%rbp), %edx
callq 0x2410
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x2a34
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x2c(%rbp), %esi
callq 0x2480
testb $0x1, %al
jne 0x29d8
jmp 0x29f9
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movl -0x2c(%rbp), %edx
callq 0x2580
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0x2a32
movq -0x38(%rbp), %rdi
addq $0x8, %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rsi
movl -0x2c(%rbp), %ecx
movl -0x30(%rbp), %r9d
leaq 0xbeb4(%rip), %rdx      # 0xe8cf
leaq 0xbeb5(%rip), %r8       # 0xe8d7
leaq 0xbeb6(%rip), %rax      # 0xe8df
movq %rax, (%rsp)
callq 0xa3c0
jmp 0x2a34
movq -0x10(%rbp), %rax
movl $0x5, 0x4(%rax)
jmp 0x2aa6
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x26e0
movq -0x38(%rbp), %rdi
movq %rax, %rsi
movl $0x5, %edx
callq 0x2580
movq -0x38(%rbp), %rdi
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x26e0
movq -0x38(%rbp), %rdi
movq %rax, %rsi
movl $0x5, %edx
callq 0x2580
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movl $0x5, 0x4(%rax)
movq -0x10(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq