// File: cphinton42[P]hinton-language/src/check.cpp
// Function: typecheck_function_type(Context*, unsigned int, Expr_AST*, Function_Type_AST*)
bool typecheck_function_type(Context *ctx, u32 stage, Expr_AST *type, Function_Type_AST *function_type_ast)
{
    u32 new_stage = 0;
    bool add_job = false;
    
    switch(stage)
    {
        case 0: {
            function_type_ast->flags |= EXPR_FLAG_NOT_LVALUE;
            add_job = true;
        } // fall through
        case 1: {
            new_stage = 1;
            Status match = types_match(ctx, type, &type_t_ast);
            if(match != Status::good)
            {
                break;
            }
            set_resolved_type(function_type_ast, &type_t_ast);
            add_job = true;
        } // fall through
        case 2: {
            new_stage = 2;
            auto parameter_types = function_type_ast->parameter_types;
            for(u64 i = 0; i < parameter_types.count; ++i)
            {
                auto job = make_typecheck_job(&type_t_ast, parameter_types[i]);
                do_typecheck_job(ctx, job);
            }
            auto return_types = function_type_ast->return_types;
            for(u64 i = 0; i < return_types.count; ++i)
            {
                auto job = make_typecheck_job(&type_t_ast, return_types[i]);
                do_typecheck_job(ctx, job);
            }
            new_stage = 3;
        } // fall through
        // Note: typecheck_function has special handling for the prototype
        // If this changes, that should also change
    }
    
    if(new_stage < 3)
    {
        auto job = make_typecheck_job(type, function_type_ast, new_stage);
        array_add(ctx->waiting_jobs, job);
    }
    return new_stage != stage;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movb $0x0, -0x25(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x104(%rbp)
testl %eax, %eax
je 0x6031
jmp 0x6014
movl -0x104(%rbp), %eax
subl $0x1, %eax
je 0x6044
jmp 0x6021
movl -0x104(%rbp), %eax
subl $0x2, %eax
je 0x6086
jmp 0x61f3
movq -0x20(%rbp), %rax
movzwl 0x2(%rax), %ecx
orl $0x20, %ecx
movw %cx, 0x2(%rax)
movb $0x1, -0x25(%rbp)
movl $0x1, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq 0x19e96(%rip), %rdx     # 0x1fef0
movl $0x1, %ecx
callq 0x9240
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x6072
jmp 0x61f3
movq -0x20(%rbp), %rdi
leaq 0x19e73(%rip), %rsi     # 0x1fef0
callq 0x3be0
movb $0x1, -0x25(%rbp)
movl $0x2, -0x24(%rbp)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rcx
movq %rcx, -0x40(%rbp)
movq 0x28(%rax), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x6121
movq -0x48(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x17050
movq (%rax), %rdx
leaq -0x60(%rbp), %rdi
leaq 0x19e22(%rip), %rsi     # 0x1fef0
xorl %ecx, %ecx
callq 0x4a80
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x78(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x4b40
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x60a9
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rcx
movq %rcx, -0x88(%rbp)
movq 0x38(%rax), %rax
movq %rax, -0x80(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x88(%rbp), %rax
jae 0x61ec
movq -0x90(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x17050
movq (%rax), %rdx
leaq -0xa8(%rbp), %rdi
leaq 0x19d75(%rip), %rsi     # 0x1fef0
xorl %ecx, %ecx
callq 0x4a80
movq -0x8(%rbp), %rdi
movq -0xa8(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0xc0(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x4b40
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x6143
movl $0x3, -0x24(%rbp)
cmpl $0x3, -0x24(%rbp)
jae 0x6295
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
leaq -0xd8(%rbp), %rdi
callq 0x4a80
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0xd8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq 0x19a33(%rip), %rax     # 0x1fc80
movq %rax, -0x100(%rbp)
movq 0x19a2d(%rip), %rax     # 0x1fc88
movq %rax, -0xf8(%rbp)
movq -0x100(%rbp), %rsi
movq -0xf8(%rbp), %rdx
leaq -0xf0(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x17c30
movl -0x24(%rbp), %eax
cmpl -0xc(%rbp), %eax
setne %al
andb $0x1, %al
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopl (%rax)