// File: /workspace/github_cpp_makefile_star2_O3/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 %r8, 0x20(%rsp)
movq %rsi, 0x28(%rsp)
testq %r9, %r9
je 0xa1ec
movq %r9, %r14
movq %rcx, %r12
movq %rdx, %rbp
movq %rdi, %rbx
movzwl (%r14), %eax
cmpq $0x10, %rax
ja 0xa1ec
movl $0x28, %r15d
leaq 0x68bd(%rip), %rcx      # 0x10458
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x10(%r14), %r9
movl $0x2, %edx
movq %rbx, %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
movq %r12, %rcx
movq 0x20(%rsp), %r13
movq %r13, %r8
callq 0x9b53
movq 0x50(%r14), %r9
movl $0x1, %ebp
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rcx
movq %r13, %r8
callq 0x9b53
movl $0x58, %r15d
jmp 0x9e2b
movq 0x10(%r14), %r9
movl $0x1, %ebp
movl $0x1, %edx
movq %rbx, %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
movq %r12, %rcx
movq 0x20(%rsp), %r13
movq %r13, %r8
callq 0x9b53
movq 0x18(%r14), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rcx
movq %r13, %r8
jmp 0x9c50
movq 0x18(%r14), %r9
movl $0x1, %ebp
movl $0x1, %edx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r12, %rcx
movq 0x20(%rsp), %r8
callq 0x9b53
movl $0x20, %r15d
jmp 0x9e2b
movq 0x28(%r14), %r9
movl $0x1, %ebp
movl $0x1, %edx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r12, %rcx
movq 0x20(%rsp), %r8
callq 0x9b53
movl $0x30, %r15d
jmp 0x9e2b
movq 0x8(%rbx), %rdi
movl $0x30, %esi
callq 0x962b
movq %rax, %r13
movq %r12, (%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%r13)
leaq 0x10(%r13), %rdi
movl $0x8, %esi
callq 0xe7a6
movq 0x20(%r14), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x9b53
cmpq $0x0, 0x28(%r14)
je 0x9d04
xorl %r15d, %r15d
movq 0x30(%r14), %rax
movq (%rax,%r15,8), %r9
movl $0x5, %edx
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x9b53
incq %r15
cmpq 0x28(%r14), %r15
jb 0x9cdd
movl $0x2, %eax
movq %rax, 0x20(%rsp)
movl $0x48, %r15d
cmpq $0x0, 0x38(%r14)
je 0x9e86
xorl %r12d, %r12d
movq 0x40(%r14), %rax
movq (%rax,%r12,8), %r9
movl $0x1, %ebp
movl $0x1, %edx
movl $0x1, %r8d
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rcx
callq 0x9b53
incq %r12
cmpq 0x38(%r14), %r12
jb 0x9d22
movq %r14, 0x28(%rsp)
jmp 0x9e2e
movq 0x28(%rsp), %rax
movq %rax, 0x10(%r14)
movl $0x18, %r15d
jmp 0x9e26
movq 0x8(%rbx), %rdi
movl $0x30, %esi
callq 0x962b
movq %rax, %r13
movq %r12, (%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%r13)
leaq 0x10(%r13), %rdi
movl $0x4, %esi
callq 0xe7a6
movq 0x10(%r14), %r9
movl $0x4, %edx
movq %rbx, %rdi
movq 0x28(%rsp), %rbp
movq %rbp, %rsi
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x9b53
testb $0x8, 0x2(%r14)
movq 0x18(%r14), %r9
jne 0x9e43
movl $0x1, %edx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rcx
movq 0x20(%rsp), %r8
jmp 0x9e54
movq 0x10(%r14), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r12, %rcx
movq 0x20(%rsp), %r8
callq 0x9b53
movl $0x2, %ebp
movl $0x18, %r15d
jmp 0x9e2b
movq 0x28(%r14), %rsi
movq 0x30(%r14), %rdx
movq (%rbx), %rdi
callq 0x9a86
movq %rax, 0x28(%r14)
movq $0x0, 0x30(%r14)
movl $0x20, %r15d
movl $0x1, %ebp
movq %r12, %r13
movq (%r14,%r15), %r14
movq %r13, %r12
testq %r14, %r14
jne 0x9b80
jmp 0xa1ec
movl $0x4, %edx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x9b53
movq %rbp, %rsi
movq 0x20(%r14), %r9
movl $0x1, %ebp
movl $0x1, %edx
movq %rbx, %rdi
movq %r12, %rcx
movq 0x20(%rsp), %r8
callq 0x9b53
movl $0x1, %eax
movq %rax, 0x20(%rsp)
jmp 0x9e2e
movq %r14, 0x28(%rsp)
movl $0x1, %ebp
jmp 0x9e2e
movq 0x8(%rbx), %rdi
movl $0x30, %esi
callq 0x962b
movq %rax, %r13
movq %r12, (%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%r13)
leaq 0x10(%r13), %rdi
movl $0x8, %esi
callq 0xe7a6
cmpq $0x0, 0x20(%r14)
movq 0x28(%rsp), %r12
je 0x9ef3
xorl %r15d, %r15d
movq 0x28(%r14), %rax
movq (%rax,%r15,8), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x9b53
incq %r15
cmpq 0x20(%r14), %r15
jb 0x9ecc
movq %r13, 0x30(%r14)
jmp 0xa1ec
movq 0x8(%rbx), %rdi
movl $0x30, %esi
callq 0x962b
movq %rax, %r13
movq %r12, (%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%r13)
leaq 0x10(%r13), %rdi
movl $0x8, %esi
callq 0xe7a6
movq 0x8(%rbx), %rdi
movl $0x30, %esi
callq 0x962b
movq %rax, %r12
movq %r13, (%rax)
movq $0x0, 0x8(%rax)
leaq 0x10(%rax), %rdi
movl $0x8, %esi
callq 0xe7a6
cmpq $0x0, 0x20(%r14)
movq 0x28(%rsp), %rbp
je 0x9f87
xorl %r15d, %r15d
movq 0x28(%r14), %rax
movq (%rax,%r15,8), %r9
movl $0x2, %edx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x9b53
incq %r15
cmpq 0x20(%r14), %r15
jb 0x9f60
cmpq $0x0, 0x30(%r14)
je 0x9fb8
xorl %r15d, %r15d
movq 0x38(%r14), %rax
movq (%rax,%r15,8), %r9
movl $0x3, %edx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x9b53
incq %r15
cmpq 0x30(%r14), %r15
jb 0x9f91
movq %r13, 0x40(%r14)
movq %r12, 0x48(%r14)
jmp 0xa1ec
movq 0x8(%rbx), %rdi
movl $0x30, %esi
callq 0x962b
movq %rax, %r13
movq %r12, (%rax)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%r13)
leaq 0x10(%r13), %rdi
movl $0x8, %esi
callq 0xe7a6
cmpq $0x0, 0x10(%r14)
je 0xa1ec
xorl %r12d, %r12d
movq 0x18(%r14), %rax
movq (%rax,%r12,8), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r13, %rcx
movq %r12, %r8
callq 0x9b53
incq %r12
cmpq 0x10(%r14), %r12
jb 0x9ffe
jmp 0xa1ec
movq 0x20(%r14), %rsi
movq 0x28(%r14), %r13
movq (%rbx), %rdi
movq %rsi, 0x28(%rsp)
movq %r13, %rdx
callq 0x9a86
movq %rax, %r15
movq %rax, 0x20(%r14)
movq %rbp, 0x28(%r14)
movq 0x20(%rsp), %rax
movq %rax, 0x30(%r14)
movq %r12, 0x38(%r14)
cmpq $0x1, %rbp
je 0xa1ec
movq %r15, 0x30(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x38(%rsp)
movq %r14, 0x40(%rsp)
leaq 0x10(%r12), %rdi
movq %r15, %rax
shrq $0x1e, %rax
xorq %r15, %rax
movabsq $-0x40a7b892e31b1a47, %rcx # imm = 0xBF58476D1CE4E5B9
imulq %rax, %rcx
movq %rcx, %rax
shrq $0x1b, %rax
xorq %rcx, %rax
movabsq $-0x6b2fb644ecceee15, %rcx # imm = 0x94D049BB133111EB
imulq %rax, %rcx
movq %rcx, %rsi
shrq $0x1f, %rsi
xorq %rcx, %rsi
movq %r14, 0x10(%rsp)
movups 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
callq 0xe866
testb %al, %al
jne 0xa1ec
movq %r12, %rdi
movq %r15, %rsi
movq 0x20(%rsp), %rdx
movl $0x1, %ecx
callq 0x99c5
testq %rax, %rax
je 0xa1fb
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
movl 0x8(%rax), %r9d
movl 0xc(%rax), %eax
movl %eax, (%rsp)
leaq 0x7ffc(%rip), %rdi      # 0x12107
movq 0x28(%rsp), %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x416b
movb $0x0, 0x10(%rbx)
jmp 0xa1ec
movq 0x20(%r14), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r12, %rcx
movq 0x20(%rsp), %r8
callq 0x9b53
cmpq $0x0, 0x28(%r14)
je 0xa1ec
xorl %r15d, %r15d
movq 0x30(%r14), %rax
movq (%rax,%r15,8), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r12, %rcx
movq 0x20(%rsp), %r8
callq 0x9b53
incq %r15
cmpq 0x28(%r14), %r15
jb 0xa14f
jmp 0xa1ec
cmpq $0x0, 0x20(%r14)
movq 0x28(%rsp), %r13
movq 0x20(%rsp), %rbp
je 0xa1b7
xorl %r15d, %r15d
movq 0x28(%r14), %rax
movq (%rax,%r15,8), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rcx
movq %rbp, %r8
callq 0x9b53
incq %r15
cmpq 0x20(%r14), %r15
jb 0xa190
cmpq $0x0, 0x30(%r14)
je 0xa1ec
xorl %r15d, %r15d
movq 0x38(%r14), %rax
movq (%rax,%r15,8), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r12, %rcx
movq 0x20(%rsp), %r8
callq 0x9b53
incq %r15
cmpq 0x30(%r14), %r15
jb 0xa1c1
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7ea1(%rip), %rdi      # 0x120a3
leaq 0x717b(%rip), %rsi      # 0x11384
leaq 0x7e98(%rip), %rcx      # 0x120a8
movl $0xbe, %edx
callq 0x3080