// File: baziotis[P]minijava-cpp/typecheck.cpp
// Function: MainTypeCheckVisitor::visit(Method*, char const*)
void MainTypeCheckVisitor::visit(Method *method, const char *class_name) {
    LOG_SCOPE;
    debug_print("MainTypeCheck::Method %s\n", method->id);
    this->curr_method = method;

    __expr_context.method = method;

    bool is_main_method = method->id == main_method_string;

    cgen_start_method(method, class_name, is_main_method);

    if (is_main_method && type_transitively_extends_Main(this, class_name))
    {
        typecheck_error(method->loc, "In type: `", class_name,
                        "` you can't name a method `main` because "
                        "the type transitively extends `Main` class.");
    }

    for (int i = 0; i < __expr_context.nesting_level; ++i) {
      for (int j = 0; j < __expr_context.if_bufs[i].len; ++j) {
        __expr_context.if_bufs[i][j].assigned = false;
      }
      for (int j = 0; j < __expr_context.else_bufs[i].len; ++j) {
        __expr_context.else_bufs[i][j].assigned = false;
      }
    }
    __expr_context.nesting_level = 0;


    for (Statement *stmt : method->stmts) {
        stmt->accept(this);
    }
    // An undefined return expression may actually end up here.
    // Check parse.cpp
    // A null expression may also come here because of the `main()` method.
    if (!is_main_method && !method->ret_expr->is_undefined()) {
        assert(method->ret_expr);
        Type *ret_type = method->ret_expr->accept(this);
        if (ret_type->kind != TY::UNDEFINED) {
          assert(ret_type);
          assert(method->ret_type);
          llvalue_t ret_val = __expr_context.llval;
          if (!compatible_types(method->ret_type, ret_type)) {
              location_t loc_here = method->ret_expr->loc;
              typecheck_error(loc_here, "The type: `", ret_type->name(),
                              "` of the return expression does not match the ",
                              "return type: `", method->ret_type->name(),
                              "` of method: `", method->id, "`");
          } else {
              llvm_ret(ret_type, ret_val);
          }
        }
    }
    this->curr_method = NULL;

    cgen_end_method(is_main_method);

    // Free arena for objects of function lifetime
    deallocate(MEM::FUNC);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0x19(%rbp), %rdi
callq 0x3da0
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0xd63f(%rip), %rdi      # 0x1bc98
xorl %eax, %eax
callq 0x3e60
jmp 0xe662
movq -0xc8(%rbp), %rax
movq -0x10(%rbp), %rcx
movq %rcx, 0x78(%rax)
movq -0x10(%rbp), %rcx
leaq 0x1634c(%rip), %rax     # 0x249c8
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
leaq 0x163ca(%rip), %rcx     # 0x24a58
movq (%rcx), %rcx
subq %rcx, %rax
sete -0x2d(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movzbl -0x2d(%rbp), %edx
andl $0x1, %edx
callq 0x7350
jmp 0xe6ae
testb $0x1, -0x2d(%rbp)
je 0xe71a
movq -0xc8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xeb40
movb %al, -0xc9(%rbp)
jmp 0xe6cc
movb -0xc9(%rbp), %al
testb $0x1, %al
jne 0xe6d8
jmp 0xe71a
movq -0x10(%rbp), %rax
movl 0x50(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rdx
movl -0x34(%rbp), %edi
leaq 0xd5c2(%rip), %rsi      # 0x1bcb2
leaq 0xd5c6(%rip), %rcx      # 0x1bcbd
callq 0xda80
jmp 0xe6fe
jmp 0xe71a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x19(%rbp), %rdi
callq 0x3dd0
jmp 0xeb35
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
leaq 0x1629d(%rip), %rcx     # 0x249c8
cmpl 0x30(%rcx), %eax
jge 0xe868
movl $0x0, -0x3c(%rbp)
movslq -0x3c(%rbp), %rax
movq %rax, -0xe0(%rbp)
movslq -0x38(%rbp), %rsi
leaq 0x16277(%rip), %rdi     # 0x249c8
addq $0x40, %rdi
callq 0x16410
movq %rax, -0xd8(%rbp)
jmp 0xe763
movq -0xe0(%rbp), %rax
movq -0xd8(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0xe7c6
movslq -0x38(%rbp), %rsi
leaq 0x16246(%rip), %rdi     # 0x249c8
addq $0x40, %rdi
callq 0x16410
movq %rax, -0xe8(%rbp)
jmp 0xe794
movq -0xe8(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
callq 0x16430
movq %rax, -0xf0(%rbp)
jmp 0xe7ad
movq -0xf0(%rbp), %rax
movb $0x0, 0x20(%rax)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xe73b
movl $0x0, -0x40(%rbp)
movslq -0x40(%rbp), %rax
movq %rax, -0x100(%rbp)
movslq -0x38(%rbp), %rsi
leaq 0x161e5(%rip), %rdi     # 0x249c8
addq $0x58, %rdi
callq 0x16410
movq %rax, -0xf8(%rbp)
jmp 0xe7f5
movq -0x100(%rbp), %rax
movq -0xf8(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0xe858
movslq -0x38(%rbp), %rsi
leaq 0x161b4(%rip), %rdi     # 0x249c8
addq $0x58, %rdi
callq 0x16410
movq %rax, -0x108(%rbp)
jmp 0xe826
movq -0x108(%rbp), %rdi
movslq -0x40(%rbp), %rsi
callq 0x16430
movq %rax, -0x110(%rbp)
jmp 0xe83f
movq -0x110(%rbp), %rax
movb $0x0, 0x20(%rax)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0xe7cd
jmp 0xe85a
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xe721
leaq 0x16159(%rip), %rax     # 0x249c8
movl $0x0, 0x30(%rax)
movq -0x10(%rbp), %rax
addq $0x58, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x16450
movq %rax, -0x118(%rbp)
jmp 0xe894
movq -0x118(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
callq 0x16470
movq %rax, -0x120(%rbp)
jmp 0xe8b1
movq -0x120(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
je 0xe8f7
movq -0xc8(%rbp), %rsi
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
jmp 0xe8e7
jmp 0xe8e9
movq -0x50(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x50(%rbp)
jmp 0xe8bc
testb $0x1, -0x2d(%rbp)
jne 0xeafa
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0xb580
movb %al, -0x121(%rbp)
jmp 0xe916
movb -0x121(%rbp), %al
testb $0x1, %al
jne 0xeafa
jmp 0xe926
movq -0x10(%rbp), %rax
cmpq $0x0, 0x70(%rax)
je 0xe933
jmp 0xe952
leaq 0xd3d8(%rip), %rdi      # 0x1bd12
leaq 0xce1d(%rip), %rsi      # 0x1b75e
movl $0x33e, %edx            # imm = 0x33E
leaq 0xd3d6(%rip), %rcx      # 0x1bd23
callq 0x20d0
movq -0xc8(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x16490
movq %rax, -0x130(%rbp)
jmp 0xe96f
movq -0x130(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0xeaf8
cmpq $0x0, -0x68(%rbp)
je 0xe991
jmp 0xe9b0
leaq 0xd3cc(%rip), %rdi      # 0x1bd64
leaq 0xcdbf(%rip), %rsi      # 0x1b75e
movl $0x341, %edx            # imm = 0x341
leaq 0xd378(%rip), %rcx      # 0x1bd23
callq 0x20d0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0xe9bd
jmp 0xe9dc
leaq 0xd398(%rip), %rdi      # 0x1bd5c
leaq 0xcd93(%rip), %rsi      # 0x1b75e
movl $0x342, %edx            # imm = 0x342
leaq 0xd34c(%rip), %rcx      # 0x1bd23
callq 0x20d0
leaq 0x15fe5(%rip), %rax     # 0x249c8
movups 0x8(%rax), %xmm0
movups 0x18(%rax), %xmm1
movaps %xmm1, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x68(%rbp), %rsi
callq 0xebe0
movb %al, -0x131(%rbp)
jmp 0xea0f
movb -0x131(%rbp), %al
testb $0x1, %al
jne 0xeab8
jmp 0xea1f
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movl (%rax), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x98(%rbp)
movq -0x68(%rbp), %rdi
callq 0xc180
movq %rax, -0x140(%rbp)
jmp 0xea4d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xc180
movq %rax, -0x148(%rbp)
jmp 0xea63
movq -0x148(%rbp), %r9
movq -0x140(%rbp), %rdx
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movl -0x98(%rbp), %edi
leaq 0xc2a8(%rip), %rsi      # 0x1ad2d
movq %rsp, %rax
movq %rsi, 0x10(%rax)
movq %rcx, 0x8(%rax)
leaq 0xd320(%rip), %rcx      # 0x1bdb7
movq %rcx, (%rax)
leaq 0xd2cc(%rip), %rsi      # 0x1bd6d
leaq 0xd2d1(%rip), %rcx      # 0x1bd79
leaq 0xd2f9(%rip), %r8       # 0x1bda8
callq 0xec80
jmp 0xeab6
jmp 0xeaf6
movq -0x68(%rbp), %rdi
movaps -0x90(%rbp), %xmm0
movaps -0x80(%rbp), %xmm1
movaps %xmm1, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movaps -0xc0(%rbp), %xmm0
movaps -0xb0(%rbp), %xmm1
movq %rsp, %rax
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x7740
jmp 0xeaf4
jmp 0xeaf6
jmp 0xeaf8
jmp 0xeafa
movq -0xc8(%rbp), %rax
movq $0x0, 0x78(%rax)
movzbl -0x2d(%rbp), %edi
andl $0x1, %edi
callq 0x75f0
jmp 0xeb17
movl $0x4, %edi
callq 0x25d0
jmp 0xeb23
leaq -0x19(%rbp), %rdi
callq 0x3dd0
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x21c0
nop