// File: /workspace/github_cpp_makefile_star2_O2/cphinton42[P]hinton-language/src/scope.cpp
// Function: create_scope_metadata(Scoping_Context*, Function_AST*, unsigned long, Hashed_Scope*, unsigned long, AST*)
internal
void create_scope_metadata(Scoping_Context *ctx, Function_AST *func, u64 type, Hashed_Scope *scope, u64 scope_index, AST *ast)
{
    if(!ast)
    {
        return;
    }
    
    switch(ast->type)
    {
        case AST_Type::decl_ast: {
            Decl_AST *decl_ast = static_cast<Decl_AST*>(ast);
            create_scope_metadata(ctx, func, IDENT_DECL, scope, scope_index, &decl_ast->ident);
            create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, decl_ast->decl_type);
            create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, decl_ast->expr);
        } break;
        case AST_Type::block_ast: {
            Block_AST *block_ast = static_cast<Block_AST*>(ast);
            
            Hashed_Scope *block_scope = pool_alloc(Hashed_Scope, ctx->ast_pool);
            init_hashed_scope(block_scope, scope, scope_index, 8);
            
            for(u64 i = 0; i < block_ast->statements.count; ++i)
            {
                create_scope_metadata(ctx, func, IDENT_REFERENCE, block_scope, i, block_ast->statements[i]);
            }
        } break;
        case AST_Type::while_ast: {
            While_AST *while_ast = static_cast<While_AST*>(ast);
            create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, while_ast->guard);
            create_scope_metadata(ctx, func, IDENT_DECL, scope, scope_index, while_ast->body);
        } break;
        case AST_Type::for_ast: {
            For_AST *for_ast = static_cast<For_AST*>(ast);
            
            Hashed_Scope *for_scope = pool_alloc(Hashed_Scope, ctx->ast_pool);
            init_hashed_scope(for_scope, scope, scope_index, 4);
            
            create_scope_metadata(ctx, func, IDENT_LOOP_VAR, for_scope, 0, for_ast->induction_var);
            
            if(for_ast->flags & FOR_FLAG_OVER_ARRAY)
            {
                create_scope_metadata(ctx, func, IDENT_LOOP_VAR, for_scope, 0, for_ast->index_var);
                create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, for_ast->array_expr);
            }
            else
            {
                create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, for_ast->low_expr);
                create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, for_ast->high_expr);
            }
            create_scope_metadata(ctx, func, IDENT_REFERENCE, for_scope, 1, for_ast->body);
        } break;
        case AST_Type::if_ast: {
            If_AST *if_ast = static_cast<If_AST*>(ast);
            create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, if_ast->guard);
            create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, if_ast->then_block);
            create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, if_ast->else_block);
        } break;
        case AST_Type::assign_ast: {
            Assign_AST *assign_ast = static_cast<Assign_AST*>(ast);
            create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, assign_ast->lhs);
            create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, assign_ast->rhs);
        } break;
        case AST_Type::return_ast: {
            Return_AST *return_ast = static_cast<Return_AST*>(ast);
            return_ast->function = func;
            create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, return_ast->expr);
        } break;
        case AST_Type::ident_ast: {
            Ident_AST *ident_ast = static_cast<Ident_AST*>(ast);
            
            String str = ident_ast->ident;
            Atom atom = atomize_string(ctx->atom_table, str);
            
            ident_ast->atom = atom;
            ident_ast->tagged_expr_ptr = type; // need to know this
            ident_ast->scope_index = scope_index;
            ident_ast->scope = scope;
            
            if(type != IDENT_REFERENCE)
            {
                Scope_Entry entry;
                entry.key = atom;
                entry.index = scope_index;
                entry.definition = ident_ast;
                bool success = scope_insert(scope, entry);
                if(!success)
                {
                    // TODO: better error reporting
                    Expr_AST *prev = scope_find(scope, atom, scope_index);
                    assert(prev);
                    print_err("Error: %d:%d: Redeclared identifier '%.*s'\nPrevious declaration at %d:%d\n", ident_ast->line_number, ident_ast->line_offset, str.count, str.data, prev->line_number, prev->line_offset);
                    ctx->success = false;
                }
            }
        } break;
        case AST_Type::function_type_ast: {
            Function_Type_AST *function_type_ast = static_cast<Function_Type_AST*>(ast);
            for(u64 i = 0; i < function_type_ast->parameter_types.count; ++i)
            {
                create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, function_type_ast->parameter_types[i]);
            }
            for(u64 i = 0; i < function_type_ast->return_types.count; ++i)
            {
                create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, function_type_ast->return_types[i]);
            }
        } break;
        case AST_Type::function_ast: {
            Function_AST *function_ast = static_cast<Function_AST*>(ast);
            
            Hashed_Scope *func_scope = pool_alloc(Hashed_Scope, ctx->ast_pool);
            init_hashed_scope(func_scope, scope, scope_index, 8);
            
            create_scope_metadata(ctx, function_ast, IDENT_REFERENCE, func_scope, 0, function_ast->prototype);
            
            for(u64 i = 0; i < function_ast->param_names.count; ++i)
            {
                create_scope_metadata(ctx, function_ast, IDENT_PARAM, func_scope, 0, function_ast->param_names[i]);
            }
            for(u64 i = 0; i < function_ast->default_values.count; ++i)
            {
                create_scope_metadata(ctx, function_ast, IDENT_REFERENCE, func_scope, 1, function_ast->default_values[i]);
            }
            create_scope_metadata(ctx, function_ast, IDENT_REFERENCE, func_scope, 2, function_ast->block);
        } break;
        case AST_Type::function_call_ast: {
            Function_Call_AST *function_call_ast = static_cast<Function_Call_AST*>(ast);
            
            create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, function_call_ast->function);
            
            for(u64 i = 0; i < function_call_ast->args.count; ++i)
            {
                create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, function_call_ast->args[i]);
            }
        } break;
        case AST_Type::access_ast: {
            Access_AST *access_ast = static_cast<Access_AST*>(ast);
            
            String str = access_ast->ident;
            Atom atom = atomize_string(ctx->atom_table, str);
            access_ast->atom = atom;
            access_ast->expr = nullptr;
            create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, access_ast->lhs);
        } break;
        case AST_Type::binary_operator_ast: {
            Binary_Operator_AST *binop_ast = static_cast<Binary_Operator_AST*>(ast);
            create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, binop_ast->lhs);
            create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, binop_ast->rhs);
        } break;
        case AST_Type::enum_ast: {
            Enum_AST *enum_ast = static_cast<Enum_AST*>(ast);
            
            Hashed_Scope *values_scope = pool_alloc(Hashed_Scope, ctx->ast_pool);
            init_hashed_scope(values_scope, scope, scope_index, 8);
            
            for(u64 i = 0; i < enum_ast->values.count; ++i)
            {
                // TODO: is IDENT_DECL best for this?
                create_scope_metadata(ctx, func, IDENT_REFERENCE, values_scope, 0, enum_ast->values[i]);
            }
            enum_ast->scope = values_scope;
        } break;
        case AST_Type::struct_ast: {
            Struct_AST *struct_ast = static_cast<Struct_AST*>(ast);
            
            Hashed_Scope *constants_scope = pool_alloc(Hashed_Scope, ctx->ast_pool);
            init_hashed_scope(constants_scope, scope, scope_index, 8);
            Hashed_Scope *fields_scope = pool_alloc(Hashed_Scope, ctx->ast_pool);
            init_hashed_scope(fields_scope, constants_scope, 0, 8);
            
            for(u64 i = 0; i < struct_ast->constants.count; ++i)
            {
                create_scope_metadata(ctx, func, IDENT_DECL, constants_scope, 0, struct_ast->constants[i]);
            }
            for(u64 i = 0; i < struct_ast->fields.count; ++i)
            {
                create_scope_metadata(ctx, func, IDENT_FIELD, fields_scope, 0, struct_ast->fields[i]);
            }
            struct_ast->constant_scope = constants_scope;
            struct_ast->field_scope = fields_scope;
        } break;
        case AST_Type::unary_ast: {
            Unary_Operator_AST *unop_ast = static_cast<Unary_Operator_AST*>(ast);
            create_scope_metadata(ctx, func, IDENT_REFERENCE, scope, scope_index, unop_ast->operand);
        } break;
        case AST_Type::number_ast:
        case AST_Type::primitive_ast:
        case AST_Type::string_ast:
        case AST_Type::bool_ast: {
            // Do nothing
        } break;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %r14
movq %r8, 0x20(%rsp)
movq %rcx, %r12
movq %rdx, %r15
movq %rsi, 0x28(%rsp)
movq %rdi, %rbx
pushq $0x1
popq %rbp
pushq $0x4
popq %rdx
testq %r14, %r14
je 0x89b6
movzwl (%r14), %eax
cmpq $0x10, %rax
ja 0x89b6
leaq 0x601a(%rip), %rcx      # 0xe458
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x10(%r14), %r9
movq %rbx, %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
pushq $0x2
popq %rdx
movq %r12, %rcx
movq 0x20(%rsp), %r13
movq %r13, %r8
callq 0x83f6
movq 0x50(%r14), %r9
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq %r13, %r8
callq 0x83f6
pushq $0x4
popq %rdx
pushq $0x58
jmp 0x8679
movq 0x10(%r14), %r9
movq %rbx, %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq 0x20(%rsp), %r13
movq %r13, %r8
callq 0x83f6
movq 0x18(%r14), %r9
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq %r13, %r8
jmp 0x84d9
movq 0x18(%r14), %r9
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq 0x20(%rsp), %r8
callq 0x83f6
pushq $0x4
popq %rdx
jmp 0x850f
movq 0x28(%rsp), %rax
movq %rax, 0x10(%r14)
pushq $0x18
jmp 0x8679
movq 0x28(%r14), %rsi
movq 0x30(%r14), %rdx
movq (%rbx), %rdi
callq 0x8354
pushq $0x4
popq %rdx
movq %rax, 0x28(%r14)
andq $0x0, 0x30(%r14)
pushq $0x20
jmp 0x8679
movq 0x8(%rbx), %rdi
pushq $0x30
popq %rsi
callq 0x7fa2
movq %rax, %r13
movq %r12, (%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%r13)
leaq 0x10(%r13), %rdi
pushq $0x8
popq %rsi
callq 0xcbac
movq 0x20(%r14), %r9
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x83f6
xorl %r15d, %r15d
pushq $0x5
popq %r12
cmpq 0x28(%r14), %r15
jae 0x8583
movq 0x30(%r14), %rax
movq (%rax,%r15,8), %r9
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x83f6
incq %r15
jmp 0x855c
xorl %r15d, %r15d
cmpq 0x38(%r14), %r15
jae 0x85ad
movq 0x40(%r14), %rax
movq (%rax,%r15,8), %r9
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r13, %rcx
movq %rbp, %r8
callq 0x83f6
incq %r15
jmp 0x8586
pushq $0x48
popq %rax
movq %r14, 0x28(%rsp)
movq %rbp, %r15
pushq $0x2
popq %rcx
movq %rcx, 0x20(%rsp)
jmp 0x86c7
movq 0x8(%rbx), %rdi
pushq $0x30
popq %rsi
movq %rdx, %r15
callq 0x7fa2
movq %rax, %r13
movq %r12, (%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%r13)
leaq 0x10(%r13), %rdi
movq %r15, %rsi
callq 0xcbac
movq 0x10(%r14), %r9
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r15, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x83f6
testb $0x8, 0x2(%r14)
movq 0x18(%r14), %r9
movq %rbx, %rdi
jne 0x868c
movq 0x28(%rsp), %r15
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq 0x20(%rsp), %r8
jmp 0x869d
pushq $0x28
jmp 0x8679
movq 0x10(%r14), %r9
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq 0x20(%rsp), %r8
callq 0x83f6
pushq $0x4
popq %rdx
pushq $0x18
popq %rax
pushq $0x2
popq %r15
jmp 0x867d
movq 0x28(%r14), %r9
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq 0x20(%rsp), %r8
callq 0x83f6
pushq $0x4
popq %rdx
pushq $0x30
popq %rax
movq %rbp, %r15
movq %r12, %r13
movq (%r14,%rax), %r14
movq %r13, %r12
jmp 0x8420
movq 0x28(%rsp), %r15
movq %r15, %rsi
pushq $0x4
popq %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x83f6
movq 0x20(%r14), %r9
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq 0x20(%rsp), %r8
callq 0x83f6
pushq $0x28
popq %rax
movq %rbp, %r15
movq %rbp, 0x20(%rsp)
pushq $0x4
popq %rdx
jmp 0x8680
movq 0x8(%rbx), %rdi
pushq $0x30
popq %r13
movq %r13, %rsi
callq 0x7fa2
movq %rax, %r15
movq %r12, (%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%r15)
leaq 0x10(%r15), %rdi
pushq $0x8
popq %rbp
movq %rbp, %rsi
callq 0xcbac
movq 0x8(%rbx), %rdi
movq %r13, %rsi
callq 0x7fa2
movq %rax, %r12
movq %r15, (%rax)
andq $0x0, 0x8(%rax)
leaq 0x10(%rax), %rdi
movq %rbp, %rsi
callq 0xcbac
xorl %ebp, %ebp
pushq $0x2
popq %r13
cmpq 0x20(%r14), %rbp
jae 0x874c
movq 0x28(%r14), %rax
movq (%rax,%rbp,8), %r9
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r13, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x83f6
incq %rbp
jmp 0x8723
xorl %ebp, %ebp
pushq $0x3
popq %r13
cmpq 0x30(%r14), %rbp
jae 0x877b
movq 0x38(%r14), %rax
movq (%rax,%rbp,8), %r9
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r13, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x83f6
incq %rbp
jmp 0x8752
movq %r15, 0x40(%r14)
movq %r12, 0x48(%r14)
jmp 0x89b6
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rbp
movq (%rbx), %rdi
movq %rsi, 0x28(%rsp)
movq %rbp, %rdx
callq 0x8354
movq %rax, %r13
movq %rax, 0x20(%r14)
movq %r15, 0x28(%r14)
movq 0x20(%rsp), %rax
movq %rax, 0x30(%r14)
movq %r12, 0x38(%r14)
cmpq $0x1, %r15
je 0x89b6
movq %r13, 0x30(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x38(%rsp)
movq %r14, 0x40(%rsp)
leaq 0x10(%r12), %rdi
movq %r14, 0x10(%rsp)
movups 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0xcc62
testb %al, %al
jne 0x89b6
pushq $0x1
popq %rcx
movq %r12, %rdi
movq %r13, %rsi
movq 0x20(%rsp), %rdx
callq 0x82b5
testq %rax, %rax
je 0x89c5
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
movl 0x8(%rax), %r9d
movl 0xc(%rax), %eax
movl %eax, (%rsp)
leaq 0x78ac(%rip), %rdi      # 0x100d7
movq 0x28(%rsp), %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x3ec6
movb $0x0, 0x10(%rbx)
jmp 0x89b6
xorl %r13d, %r13d
pushq $0x1
popq %r15
movq 0x20(%rsp), %rbp
cmpq 0x20(%r14), %r13
jae 0x8878
movq 0x28(%r14), %rax
movq (%rax,%r13,8), %r9
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %rbp, %r8
callq 0x83f6
incq %r13
jmp 0x884f
xorl %r13d, %r13d
pushq $0x1
popq %r15
cmpq 0x30(%r14), %r13
jae 0x89b6
movq 0x38(%r14), %rax
movq (%rax,%r13,8), %r9
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
movq 0x20(%rsp), %r8
callq 0x83f6
incq %r13
jmp 0x887f
movq 0x8(%rbx), %rdi
pushq $0x30
popq %rsi
callq 0x7fa2
movq %rax, %r15
movq %r12, (%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%r15)
leaq 0x10(%r15), %rdi
pushq $0x8
popq %rsi
callq 0xcbac
xorl %r12d, %r12d
pushq $0x1
popq %r13
cmpq 0x10(%r14), %r12
jae 0x89b6
movq 0x18(%r14), %rax
movq (%rax,%r12,8), %r9
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r13, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x83f6
incq %r12
jmp 0x88dc
movq 0x20(%r14), %r9
pushq $0x1
popq %r15
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
movq 0x20(%rsp), %r8
callq 0x83f6
xorl %r13d, %r13d
cmpq 0x28(%r14), %r13
jae 0x89b6
movq 0x30(%r14), %rax
movq (%rax,%r13,8), %r9
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
movq 0x20(%rsp), %r8
callq 0x83f6
incq %r13
jmp 0x892c
movq 0x8(%rbx), %rdi
pushq $0x30
popq %rsi
callq 0x7fa2
movq %rax, %r15
movq %r12, (%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%r15)
leaq 0x10(%r15), %rdi
pushq $0x8
popq %rsi
callq 0xcbac
xorl %r13d, %r13d
pushq $0x1
popq %r12
cmpq 0x20(%r14), %r13
jae 0x89b2
movq 0x28(%r14), %rax
movq (%rax,%r13,8), %r9
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r12, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x83f6
incq %r13
jmp 0x8989
movq %r15, 0x30(%r14)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x76a7(%rip), %rdi      # 0x10073
leaq 0x6981(%rip), %rsi      # 0xf354
leaq 0x769e(%rip), %rcx      # 0x10078
movl $0xbe, %edx
callq 0x3080