// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movq %rcx, %r15
movq %rdx, %rbp
movq %rsi, 0x20(%rsp)
movq %rdi, %rbx
leaq 0x68e4(%rip), %r13      # 0x10458
testq %r14, %r14
je 0xa18c
movzwl (%r14), %eax
cmpq $0x10, %rax
ja 0xa18c
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
leaq 0x10(%r14), %r9
movl $0x2, %edx
movq %rbx, %rdi
movq 0x20(%rsp), %r13
movq %r13, %rsi
movq %r15, %rcx
movq %r12, %r8
callq 0x9b4b
movq 0x50(%r14), %r9
movl $0x1, %ebp
movl $0x1, %edx
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x6889(%rip), %r13      # 0x10458
movq %r15, %rcx
movq %r12, %r8
callq 0x9b4b
movq 0x58(%r14), %r14
jmp 0x9b74
movq 0x10(%r14), %r9
movl $0x1, %ebp
movl $0x1, %edx
movq %rbx, %rdi
movq 0x20(%rsp), %r13
movq %r13, %rsi
movq %r15, %rcx
movq %r12, %r8
callq 0x9b4b
movq 0x18(%r14), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x683e(%rip), %r13      # 0x10458
jmp 0x9c32
movq 0x18(%r14), %r9
movl $0x1, %ebp
movl $0x1, %edx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rcx
movq %r12, %r8
callq 0x9b4b
movq 0x20(%r14), %r14
jmp 0x9b74
movq 0x20(%rsp), %rax
movq %rax, 0x10(%r14)
movq 0x18(%r14), %r14
movl $0x1, %ebp
jmp 0x9b74
movq 0x28(%r14), %rsi
movq 0x30(%r14), %rdx
movq (%rbx), %rdi
callq 0x9a5a
movq %rax, 0x28(%r14)
movq $0x0, 0x30(%r14)
movq 0x20(%r14), %r14
movl $0x1, %ebp
jmp 0x9b74
movq 0x28(%r14), %r14
movl $0x1, %ebp
jmp 0x9b74
movq 0x10(%r14), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rcx
movq %r12, %r8
callq 0x9b4b
movq 0x18(%r14), %r14
movl $0x2, %ebp
jmp 0x9b74
movq 0x28(%r14), %r9
movl $0x1, %ebp
movl $0x1, %edx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rcx
movq %r12, %r8
callq 0x9b4b
movq 0x30(%r14), %r14
jmp 0x9b74
movq 0x8(%rbx), %rdi
movl $0x30, %esi
callq 0x95e6
movq %rax, %r13
movq %r15, (%rax)
movq %r12, 0x8(%rax)
leaq 0x10(%rax), %rdi
movl $0x8, %esi
callq 0xebb1
movq 0x20(%r14), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x9b4b
cmpq $0x0, 0x28(%r14)
je 0x9d5a
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 0x9b4b
incq %r15
cmpq 0x28(%r14), %r15
jb 0x9d33
cmpq $0x0, 0x38(%r14)
je 0x9d8e
xorl %r15d, %r15d
movq 0x40(%r14), %rax
movq (%rax,%r15,8), %r9
movl $0x1, %edx
movl $0x1, %r8d
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rcx
callq 0x9b4b
incq %r15
cmpq 0x38(%r14), %r15
jb 0x9d64
movq %r14, 0x20(%rsp)
movq 0x48(%r14), %r14
movl $0x2, %r12d
movl $0x1, %ebp
movq %r13, %r15
jmp 0x9b6d
movq 0x8(%rbx), %rdi
movl $0x30, %esi
callq 0x95e6
movq %rax, %r13
movq %r15, (%rax)
movq %r12, 0x8(%rax)
leaq 0x10(%rax), %rdi
movl $0x4, %esi
callq 0xebb1
movq 0x10(%r14), %r9
movl $0x4, %edx
movq %rbx, %rdi
movq 0x20(%rsp), %rbp
movq %rbp, %rsi
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x9b4b
testb $0x8, 0x2(%r14)
movq 0x18(%r14), %r9
jne 0x9e0d
movl $0x1, %edx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rcx
movq %r12, %r8
jmp 0x9e1e
movl $0x4, %edx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x9b4b
movq %rbp, %rsi
movq 0x20(%r14), %r9
movl $0x1, %ebp
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rcx
movq %r12, %r8
callq 0x9b4b
movq 0x28(%r14), %r14
movq %r13, %r15
movl $0x1, %r12d
jmp 0x9b6d
movq 0x8(%rbx), %rdi
movl $0x30, %esi
callq 0x95e6
movq %rax, %rbp
movq %r15, (%rax)
movq %r12, 0x8(%rax)
leaq 0x10(%rax), %rdi
movl $0x8, %esi
callq 0xebb1
movq 0x8(%rbx), %rdi
movl $0x30, %esi
callq 0x95e6
movq %rax, %r15
movq %rbp, (%rax)
movq $0x0, 0x8(%rax)
leaq 0x10(%rax), %rdi
movl $0x8, %esi
callq 0xebb1
cmpq $0x0, 0x20(%r14)
movq 0x20(%rsp), %r13
je 0x9eda
xorl %r12d, %r12d
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %r9
movl $0x2, %edx
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rcx
xorl %r8d, %r8d
callq 0x9b4b
incq %r12
cmpq 0x20(%r14), %r12
jb 0x9eb3
cmpq $0x0, 0x30(%r14)
je 0x9f0b
xorl %r12d, %r12d
movq 0x38(%r14), %rax
movq (%rax,%r12,8), %r9
movl $0x3, %edx
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x9b4b
incq %r12
cmpq 0x30(%r14), %r12
jb 0x9ee4
movq %rbp, 0x40(%r14)
movq %r15, 0x48(%r14)
jmp 0xa18c
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq (%rbx), %rdi
movq %rsi, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
callq 0x9a5a
movq %rax, 0x20(%r14)
movq %rbp, 0x28(%r14)
movq %r12, 0x30(%r14)
movq %r15, 0x38(%r14)
cmpq $0x1, %rbp
je 0xa18c
movq %rax, %r13
movq %rax, 0x30(%rsp)
movq %r12, 0x38(%rsp)
movq %r14, 0x40(%rsp)
leaq 0x10(%r15), %rdi
shrq $0x1e, %rax
xorq %r13, %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 0xec71
testb %al, %al
jne 0xa18c
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
movl $0x1, %ecx
callq 0x9988
testq %rax, %rax
je 0xa19b
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
movl 0x8(%rax), %r9d
movl 0xc(%rax), %eax
movl %eax, (%rsp)
leaq 0x81a3(%rip), %rdi      # 0x1218c
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %r8
xorl %eax, %eax
callq 0x4261
movb $0x0, 0x10(%rbx)
jmp 0xa18c
cmpq $0x0, 0x20(%r14)
movq 0x20(%rsp), %rbp
je 0xa039
xorl %r13d, %r13d
movq 0x28(%r14), %rax
movq (%rax,%r13,8), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rcx
movq %r12, %r8
callq 0x9b4b
incq %r13
cmpq 0x20(%r14), %r13
jb 0xa012
cmpq $0x0, 0x30(%r14)
je 0xa18c
xorl %r13d, %r13d
movq 0x38(%r14), %rax
movq (%rax,%r13,8), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rcx
movq %r12, %r8
callq 0x9b4b
incq %r13
cmpq 0x30(%r14), %r13
jb 0xa047
jmp 0xa18c
movq 0x20(%r14), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rcx
movq %r12, %r8
callq 0x9b4b
cmpq $0x0, 0x28(%r14)
je 0xa18c
xorl %r13d, %r13d
movq 0x30(%r14), %rax
movq (%rax,%r13,8), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rcx
movq %r12, %r8
callq 0x9b4b
incq %r13
cmpq 0x28(%r14), %r13
jb 0xa09f
jmp 0xa18c
movq 0x8(%rbx), %rdi
movl $0x30, %esi
callq 0x95e6
movq %rax, %r13
movq %r15, (%rax)
movq %r12, 0x8(%rax)
leaq 0x10(%rax), %rdi
movl $0x8, %esi
callq 0xebb1
cmpq $0x0, 0x10(%r14)
je 0xa18c
xorl %r15d, %r15d
movq 0x18(%r14), %rax
movq (%rax,%r15,8), %r9
movl $0x1, %edx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rcx
movq %r15, %r8
callq 0x9b4b
incq %r15
cmpq 0x10(%r14), %r15
jb 0xa101
jmp 0xa18c
movq 0x8(%rbx), %rdi
movl $0x30, %esi
callq 0x95e6
movq %rax, %r13
movq %r15, (%rax)
movq %r12, 0x8(%rax)
leaq 0x10(%rax), %rdi
movl $0x8, %esi
callq 0xebb1
cmpq $0x0, 0x20(%r14)
movq 0x20(%rsp), %r12
je 0xa188
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 0x9b4b
incq %r15
cmpq 0x20(%r14), %r15
jb 0xa161
movq %r13, 0x30(%r14)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7f86(%rip), %rdi      # 0x12128
leaq 0x71e3(%rip), %rsi      # 0x1138c
leaq 0x7f7d(%rip), %rcx      # 0x1212d
movl $0xbe, %edx
callq 0x3080