// File: cphinton42[P]hinton-language/src/check.cpp
// Function: reduce_type(Context*, Expr_AST*, Expr_AST**)
Status reduce_type(Context *ctx, Expr_AST *type, Expr_AST **type_out)
{
    assert(type_resolved(type));
    // assert type->resolved_type matches type_t_ast ?
    
    switch(type->type)
    {
        case AST_Type::ident_ast: {
            Ident_AST *ident_ast = static_cast<Ident_AST*>(type);
            
            u64 ident_type = ident_get_type(ident_ast);
            Expr_AST *expr = ident_get_expr(ident_ast);
            // TODO: Is this type of constancy sufficient
            bool constant = (ident_ast->flags & EXPR_FLAG_CONSTANT);
            if(ident_type == IDENT_REFERENCE || (ident_type == IDENT_DECL && constant))
            {
                if(expr)
                {
                    return reduce_type(ctx, expr, type_out);
                }
                else
                {
                    return Status::yield;
                }
            }
            else
            {
                // TODO: later, handle type parameters, etc
                report_error("Cannot reduce identifier", type);
                ctx->success = false;
                return Status::bad;
            }
        } break;
        case AST_Type::access_ast: {
            Access_AST *access_ast = static_cast<Access_AST*>(type);
            if(access_ast->expr)
            {
                return reduce_type(ctx, access_ast->expr, type_out);
            }
            else
            {
                return Status::yield;
            }
        } break;
        
        case AST_Type::unary_ast: {
            Unary_Operator_AST *unop_ast = static_cast<Unary_Operator_AST*>(type);
            assert(unop_ast->op == Unary_Operator::ref);
        } // fall through
        case AST_Type::function_type_ast:
        case AST_Type::struct_ast:
        case AST_Type::enum_ast:
        case AST_Type::primitive_ast: {
            *type_out = type;
            return Status::good;
        } break;
        
        case AST_Type::function_ast:
        case AST_Type::function_call_ast:
        case AST_Type::binary_operator_ast:
        case AST_Type::number_ast:
        case AST_Type::string_ast:
        case AST_Type::bool_ast: {
            // assert(false) // Should this be allowed?
            report_error("Expected type", type);
            ctx->success = false;
            return Status::bad;
        } break;
        
        case_non_exprs: {
            assert(false);
        } break;
    }
    
    assert(false);
    return Status::bad;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x18(%rbp), %rdi
callq 0x5600
testb $0x1, %al
jne 0x9063
jmp 0x9065
jmp 0x9084
leaq 0x10f23(%rip), %rdi     # 0x19f8f
leaq 0x10e79(%rip), %rsi     # 0x19eec
movl $0x64d, %edx            # imm = 0x64D
leaq 0x10f24(%rip), %rcx     # 0x19fa3
callq 0x3080
movq -0x18(%rbp), %rax
movzwl (%rax), %eax
movq %rax, -0x58(%rbp)
subq $0x13, %rax
ja 0x920f
movq -0x58(%rbp), %rax
leaq 0x100d0(%rip), %rcx     # 0x19174
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x17010
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
callq 0x17030
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movzwl 0x2(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, -0x39(%rbp)
cmpq $0x1, -0x30(%rbp)
je 0x90f9
cmpq $0x2, -0x30(%rbp)
jne 0x9125
testb $0x1, -0x39(%rbp)
je 0x9125
cmpq $0x0, -0x38(%rbp)
je 0x9119
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x9040
movl %eax, -0x4(%rbp)
jmp 0x922e
movl $0x1, -0x4(%rbp)
jmp 0x922e
movq -0x18(%rbp), %rsi
leaq 0x10eaa(%rip), %rdi     # 0x19fda
callq 0x4950
movq -0x10(%rbp), %rax
movb $0x0, 0x10(%rax)
movl $0x2, -0x4(%rbp)
jmp 0x922e
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x9179
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x9040
movl %eax, -0x4(%rbp)
jmp 0x922e
movl $0x1, -0x4(%rbp)
jmp 0x922e
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq $0x3, 0x20(%rax)
jne 0x919a
jmp 0x91b9
leaq 0x10e52(%rip), %rdi     # 0x19ff3
leaq 0x10d44(%rip), %rsi     # 0x19eec
movl $0x67a, %edx            # imm = 0x67A
leaq 0x10def(%rip), %rcx     # 0x19fa3
callq 0x3080
jmp 0x91bb
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x922e
movq -0x18(%rbp), %rsi
leaq 0x10e3d(%rip), %rdi     # 0x1a017
callq 0x4950
movq -0x10(%rbp), %rax
movb $0x0, 0x10(%rax)
movl $0x2, -0x4(%rbp)
jmp 0x922e
leaq 0x10c2b(%rip), %rdi     # 0x19e22
leaq 0x10cee(%rip), %rsi     # 0x19eec
movl $0x691, %edx            # imm = 0x691
leaq 0x10d99(%rip), %rcx     # 0x19fa3
callq 0x3080
leaq 0x10c0c(%rip), %rdi     # 0x19e22
leaq 0x10ccf(%rip), %rsi     # 0x19eec
movl $0x695, %edx            # imm = 0x695
leaq 0x10d7a(%rip), %rcx     # 0x19fa3
callq 0x3080
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)