// File: baziotis[P]minijava-cpp/typecheck.cpp
// Function: DeclarationVisitor::visit(MethodDeclaration*)
Method *DeclarationVisitor::visit(MethodDeclaration *method_decl) {
    // Note: It's responsibility of the one who calls this visit
    // to have assured that a method with the same id does not exist.
    LOG_SCOPE;
    assert(!method_decl->is_undefined());
    print_indentation();
    debug_log(method_decl->loc, "MethodDeclaration: ", method_decl->id, "\n");
    Method *method = new Method(method_decl);
    assert(method);
    method->ret_type = this->typespec_to_type(method_decl->typespec, method_decl->ret->loc);
    for (LocalDeclaration *par : method_decl->params) {
        // We accept the param anyway. But in the case that there is
        // another param with the same name, we still insert this
        // (so that the rest of type-checking can sort of continue) but
        // we have to generate a (dummy) id for it.
        Param *param = par->accept(this);
        if (method->locals.find(par->id)) {
            typecheck_error(par->loc, "Parameter `", par->id, "` is already defined",
                            " in method `", method_decl->id, "`");
            param->id = gen_id();
        }
        method->locals.insert(param->id, param);
    }
    for (LocalDeclaration *var : method_decl->vars) {
        if (method->locals.find(var->id)) {
            typecheck_error(var->loc, "Variable `", var->id, "` is already defined",
                            " in method `", method_decl->id, "`");
        } else {
            Var *v = var->accept(this);
            method->locals.insert(v->id, v);
        }
    }
    return method;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xb8(%rbp)
leaq -0x11(%rbp), %rdi
callq 0x3da0
movq -0x10(%rbp), %rdi
callq 0x3af0
movb %al, -0xa9(%rbp)
jmp 0xded8
movb -0xa9(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xdee6
jmp 0xdee8
jmp 0xdf18
leaq 0xdc81(%rip), %rdi      # 0x1bb70
leaq 0xd868(%rip), %rsi      # 0x1b75e
movl $0x2ad, %edx            # imm = 0x2AD
leaq 0xdc8b(%rip), %rcx      # 0x1bb8d
callq 0x20d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0xe2bc
callq 0x3e00
jmp 0xdf1f
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
movl -0x28(%rbp), %edi
leaq 0xdc8a(%rip), %rsi      # 0x1bbc4
leaq 0xc803(%rip), %rcx      # 0x1a744
callq 0xda30
jmp 0xdf48
movl $0x78, %edi
callq 0x15c60
movq %rax, -0xc0(%rbp)
jmp 0xdf5b
movq -0xc0(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x14d80
jmp 0xdf6d
movq -0xc0(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xdf81
jmp 0xdfc2
leaq 0xdb49(%rip), %rdi      # 0x1bad1
leaq 0xd7cf(%rip), %rsi      # 0x1b75e
movl $0x2b1, %edx            # imm = 0x2B1
leaq 0xdbf2(%rip), %rcx      # 0x1bb8d
callq 0x20d0
movq -0xc0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x78, %esi
callq 0x2120
jmp 0xe2bc
movq -0xb8(%rbp), %rdi
movq -0x10(%rbp), %rax
movups 0x8(%rax), %xmm0
movaps %xmm0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movl (%rax), %eax
movl %eax, -0x44(%rbp)
movl -0x40(%rbp), %esi
movq -0x38(%rbp), %rdx
movl -0x44(%rbp), %ecx
callq 0xcea0
movq %rax, -0xc8(%rbp)
jmp 0xdffa
movq -0xc8(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x160d0
movq %rax, -0xd0(%rbp)
jmp 0xe027
movq -0xd0(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
callq 0x160f0
movq %rax, -0xd8(%rbp)
jmp 0xe044
movq -0xd8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0xe148
movq -0xb8(%rbp), %rsi
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0x16140
movq %rax, -0xe0(%rbp)
jmp 0xe081
movq -0xe0(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rdi
addq $0x18, %rdi
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x16110
movq %rax, -0xe8(%rbp)
jmp 0xe0aa
movq -0xe8(%rbp), %rax
cmpq $0x0, %rax
je 0xe11f
movq -0x68(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x74(%rbp)
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %r9
movl -0x74(%rbp), %edi
leaq 0xcc53(%rip), %rcx      # 0x1ad2d
movq %rsp, %rax
movq %rcx, (%rax)
leaq 0xdaf1(%rip), %rsi      # 0x1bbd8
leaq 0xdaf6(%rip), %rcx      # 0x1bbe4
leaq 0xdb04(%rip), %r8       # 0x1bbf9
callq 0xdb90
jmp 0xe0fc
movq -0xb8(%rbp), %rdi
callq 0xde50
movq %rax, -0xf0(%rbp)
jmp 0xe111
movq -0xf0(%rbp), %rcx
movq -0x70(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rdi
addq $0x18, %rdi
movq -0x70(%rbp), %rdx
movq (%rdx), %rsi
callq 0x16170
jmp 0xe135
jmp 0xe137
movq -0x58(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x58(%rbp)
jmp 0xe04f
movq -0x10(%rbp), %rax
addq $0x38, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
callq 0x160d0
movq %rax, -0xf8(%rbp)
jmp 0xe166
movq -0xf8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
callq 0x160f0
movq %rax, -0x100(%rbp)
jmp 0xe186
movq -0x100(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x90(%rbp), %rax
je 0xe298
movq -0x88(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x30(%rbp), %rdi
addq $0x18, %rdi
movq -0x98(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x16110
movq %rax, -0x108(%rbp)
jmp 0xe1da
movq -0x108(%rbp), %rax
cmpq $0x0, %rax
je 0xe23a
movq -0x98(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x9c(%rbp)
movq -0x98(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %r9
movl -0x9c(%rbp), %edi
leaq 0xcb17(%rip), %rcx      # 0x1ad2d
movq %rsp, %rax
movq %rcx, (%rax)
leaq 0xd9e3(%rip), %rsi      # 0x1bc06
leaq 0xd9ba(%rip), %rcx      # 0x1bbe4
leaq 0xd9c8(%rip), %r8       # 0x1bbf9
callq 0xdb90
jmp 0xe238
jmp 0xe27f
movq -0xb8(%rbp), %rsi
movq -0x98(%rbp), %rdi
callq 0x16140
movq %rax, -0x110(%rbp)
jmp 0xe256
movq -0x110(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x30(%rbp), %rdi
addq $0x18, %rdi
movq -0xa8(%rbp), %rdx
movq (%rdx), %rsi
callq 0x16170
jmp 0xe27d
jmp 0xe27f
jmp 0xe281
movq -0x88(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x88(%rbp)
jmp 0xe194
movq -0x30(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0x11(%rbp), %rdi
callq 0x3dd0
movq -0x118(%rbp), %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
leaq -0x11(%rbp), %rdi
callq 0x3dd0
movq -0x20(%rbp), %rdi
callq 0x21c0
nop