authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-01 21:08:12-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-01 21:08:12-05:00
logf6cbb73c7402fc100bbfb26c1a35c9f23b3f36ff
tree6d62eb0f6b04292b98ef2bd92b40699a6c32da0a
parentc6ace9720c8666f893f236c00ea9b370b1b2bb70

rewrite scope implementation

* now there are not extra unused hash tables * each variable declaration opens a new scope inside a function

7 files changed, 420 insertions(+), 266 deletions(-)

src/all_types.hpp+74-23
......@@ -31,6 +31,7 @@ struct ConstExprValue;
3131struct IrInstruction;
3232struct IrInstructionCast;
3333struct IrBasicBlock;
34struct ScopeDecls;
3435
3536struct IrExecutable {
3637 ZigList<IrBasicBlock *> basic_block_list;
......@@ -251,8 +252,8 @@ struct AstNodeFnDef {
251252
252253 // populated by semantic analyzer
253254 TypeTableEntry *implicit_return_type;
254 // the first child block context
255 Scope *scope;
255 Scope *containing_scope;
256 Scope *child_scope;
256257};
257258
258259struct AstNodeFnDecl {
......@@ -638,7 +639,7 @@ struct AstNodeStructDecl {
638639 ZigList<AstNode *> decls;
639640
640641 // populated by semantic analyzer
641 Scope *scope;
642 ScopeDecls *decls_scope;
642643 TypeTableEntry *type_entry;
643644 TypeTableEntry *generic_fn_type;
644645 bool skip;
......@@ -884,7 +885,7 @@ struct TypeTableEntryStruct {
884885 uint64_t size_bytes;
885886 bool is_invalid; // true if any fields are invalid
886887 bool is_slice;
887 Scope *scope;
888 ScopeDecls *decls_scope;
888889
889890 // set this flag temporarily to detect infinite loops
890891 bool embedded_in_current;
......@@ -910,7 +911,7 @@ struct TypeTableEntryEnum {
910911 TypeTableEntry *tag_type;
911912 TypeTableEntry *union_type;
912913
913 Scope *scope;
914 ScopeDecls *decls_scope;
914915
915916 // set this flag temporarily to detect infinite loops
916917 bool embedded_in_current;
......@@ -926,7 +927,7 @@ struct TypeTableEntryUnion {
926927 TypeStructField *fields;
927928 uint64_t size_bytes;
928929 bool is_invalid; // true if any fields are invalid
929 Scope *scope;
930 ScopeDecls *decls_scope;
930931
931932 // set this flag temporarily to detect infinite loops
932933 bool embedded_in_current;
......@@ -1044,7 +1045,7 @@ struct ImportTableEntry {
10441045 ZigLLVMDIFile *di_file;
10451046 Buf *source_code;
10461047 ZigList<size_t> *line_offsets;
1047 Scope *scope;
1048 ScopeDecls *decls_scope;
10481049 AstNode *c_import_node;
10491050 bool any_imports_failed;
10501051
......@@ -1070,8 +1071,6 @@ struct FnTableEntry {
10701071 AstNode *proto_node;
10711072 AstNode *fn_def_node;
10721073 ImportTableEntry *import_entry;
1073 // Required to be a pre-order traversal of the AST. (parents must come before children)
1074 ZigList<Scope *> all_block_contexts;
10751074 Buf symbol_name;
10761075 TypeTableEntry *type_entry; // function type
10771076 bool internal_linkage;
......@@ -1310,7 +1309,8 @@ struct VariableTableEntry {
13101309 ZigLLVMDILocalVariable *di_loc_var;
13111310 size_t src_arg_index;
13121311 size_t gen_arg_index;
1313 Scope *scope;
1312 Scope *parent_scope;
1313 Scope *child_scope;
13141314 LLVMValueRef param_value_ref;
13151315 bool force_depends_on_compile_var;
13161316 ImportTableEntry *import;
......@@ -1334,26 +1334,77 @@ struct LabelTableEntry {
13341334struct Scope {
13351335 AstNode *node;
13361336
1337 // any variables that are introduced by this scope
1337 // if the scope has a parent, this is it. Every scope has a parent except
1338 // ScopeIdGlobal
1339 Scope *parent;
1340
1341 ZigLLVMDIScope *di_scope;
1342
1343 bool safety_off;
1344 AstNode *safety_set_node;
1345};
1346
1347// This scope comes from global declarations or from
1348// declarations in a container declaration
1349// NodeTypeRoot, NodeTypeContainerDecl
1350struct ScopeDecls {
1351 Scope base;
1352
1353 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> decl_table;
1354};
1355
1356// This scope comes from a container declaration such as a struct,
1357// enum, or union.
1358struct ScopeContainer {
1359 Scope base;
1360
13381361 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> decl_table;
1339 HashMap<Buf *, VariableTableEntry *, buf_hash, buf_eql_buf> var_table;
1362};
1363
1364// This scope comes from a block expression in user code.
1365// NodeTypeBlock
1366struct ScopeBlock {
1367 Scope base;
1368
13401369 HashMap<Buf *, LabelTableEntry *, buf_hash, buf_eql_buf> label_table;
1370};
13411371
1342 // if the block is inside a function, this is the function it is in:
1343 FnTableEntry *fn_entry;
1372// This scope is created from every defer expression.
1373// NodeTypeDefer
1374struct ScopeDefer {
1375 Scope base;
1376};
13441377
1345 // if the block has a parent, this is it
1346 Scope *parent;
1378// This scope is created for every variable declaration inside an IrExecutable
1379// NodeTypeVariableDeclaration, NodeTypeParamDecl
1380struct ScopeVarDecl {
1381 Scope base;
13471382
1348 // if break or continue is valid in this context, this is the loop node that
1349 // it would pertain to
1350 AstNode *parent_loop_node;
1383 // The variable that creates this scope
1384 VariableTableEntry *var;
1385};
13511386
1352 ZigLLVMDIScope *di_scope;
1353 Buf *c_import_buf;
1387// This scope is created for a @cImport
1388// NodeTypeFnCallExpr
1389struct ScopeCImport {
1390 Scope base;
13541391
1355 bool safety_off;
1356 AstNode *safety_set_node;
1392 Buf c_import_buf;
1393};
1394
1395// This scope is created for a loop such as for or while in order to
1396// make break and continue statements work.
1397// NodeTypeForExpr or NodeTypeWhileExpr
1398struct ScopeLoop {
1399 Scope base;
1400};
1401
1402// This scope is created for a function definition.
1403// NodeTypeFnDef
1404struct ScopeFnBody {
1405 Scope base;
1406
1407 FnTableEntry *fn_entry;
13571408};
13581409
13591410enum AtomicOrder {
src/analyze.cpp+172-146
......@@ -115,26 +115,24 @@ TypeTableEntry *new_type_table_entry(TypeTableEntryId id) {
115115 return entry;
116116}
117117
118static Scope **get_container_block_context_ptr(TypeTableEntry *type_entry) {
118static ScopeDecls **get_container_scope_ptr(TypeTableEntry *type_entry) {
119119 if (type_entry->id == TypeTableEntryIdStruct) {
120 return &type_entry->data.structure.scope;
120 return &type_entry->data.structure.decls_scope;
121121 } else if (type_entry->id == TypeTableEntryIdEnum) {
122 return &type_entry->data.enumeration.scope;
122 return &type_entry->data.enumeration.decls_scope;
123123 } else if (type_entry->id == TypeTableEntryIdUnion) {
124 return &type_entry->data.unionation.scope;
124 return &type_entry->data.unionation.decls_scope;
125125 }
126126 zig_unreachable();
127127}
128128
129Scope *get_container_block_context(TypeTableEntry *type_entry) {
130 return *get_container_block_context_ptr(type_entry);
129ScopeDecls *get_container_scope(TypeTableEntry *type_entry) {
130 return *get_container_scope_ptr(type_entry);
131131}
132132
133static TypeTableEntry *new_container_type_entry(TypeTableEntryId id, AstNode *source_node,
134 Scope *parent_context)
135{
133static TypeTableEntry *new_container_type_entry(TypeTableEntryId id, AstNode *source_node, Scope *parent_scope) {
136134 TypeTableEntry *entry = new_type_table_entry(id);
137 *get_container_block_context_ptr(entry) = new_scope(source_node, parent_context);
135 *get_container_scope_ptr(entry) = create_decls_scope(source_node, parent_scope);
138136 return entry;
139137}
140138
......@@ -766,11 +764,11 @@ static TypeTableEntryId container_to_type(ContainerKind kind) {
766764 zig_unreachable();
767765}
768766
769TypeTableEntry *get_partial_container_type(CodeGen *g, ImportTableEntry *import, Scope *context,
767TypeTableEntry *get_partial_container_type(CodeGen *g, ImportTableEntry *import, Scope *scope,
770768 ContainerKind kind, AstNode *decl_node, const char *name)
771769{
772770 TypeTableEntryId type_id = container_to_type(kind);
773 TypeTableEntry *entry = new_container_type_entry(type_id, decl_node, context);
771 TypeTableEntry *entry = new_container_type_entry(type_id, decl_node, scope);
774772
775773 switch (kind) {
776774 case ContainerKindStruct:
......@@ -844,10 +842,10 @@ static IrInstruction *analyze_const_value(CodeGen *g, Scope *scope, AstNode *nod
844842 return result;
845843}
846844
847static TypeTableEntry *analyze_type_expr(CodeGen *g, ImportTableEntry *import, Scope *context,
845static TypeTableEntry *analyze_type_expr(CodeGen *g, ImportTableEntry *import, Scope *scope,
848846 AstNode *node)
849847{
850 IrInstruction *result = analyze_const_value(g, context, node, g->builtin_types.entry_type);
848 IrInstruction *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type);
851849 if (result->type_entry->id == TypeTableEntryIdInvalid)
852850 return g->builtin_types.entry_invalid;
853851
......@@ -861,7 +859,7 @@ static bool fn_wants_full_static_eval(FnTableEntry *fn_table_entry) {
861859}
862860
863861// fn_table_entry is populated if and only if there is a function definition for this prototype
864static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *import, Scope *context,
862static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *import, Scope *scope,
865863 TypeTableEntry *expected_type, AstNode *node, bool is_naked, bool is_cold, FnTableEntry *fn_table_entry)
866864{
867865 assert(node->type == NodeTypeFnProto);
......@@ -888,7 +886,7 @@ static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *impor
888886 if (fn_proto->skip) {
889887 type_entry = g->builtin_types.entry_invalid;
890888 } else {
891 type_entry = analyze_type_expr(g, import, context, child->data.param_decl.type);
889 type_entry = analyze_type_expr(g, import, scope, child->data.param_decl.type);
892890 }
893891
894892 switch (type_entry->id) {
......@@ -951,7 +949,7 @@ static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *impor
951949 if (fn_proto->skip) {
952950 fn_type_id.return_type = g->builtin_types.entry_invalid;
953951 } else {
954 fn_type_id.return_type = analyze_type_expr(g, import, context, fn_proto->return_type);
952 fn_type_id.return_type = analyze_type_expr(g, import, scope, fn_proto->return_type);
955953 }
956954 switch (fn_type_id.return_type->id) {
957955 case TypeTableEntryIdInvalid:
......@@ -1022,7 +1020,7 @@ static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *impor
10221020}
10231021
10241022static void resolve_function_proto(CodeGen *g, AstNode *node, FnTableEntry *fn_table_entry,
1025 ImportTableEntry *import, Scope *containing_context)
1023 ImportTableEntry *import, Scope *containing_scope)
10261024{
10271025 assert(node->type == NodeTypeFnProto);
10281026 AstNodeFnProto *fn_proto = &node->data.fn_proto;
......@@ -1037,7 +1035,7 @@ static void resolve_function_proto(CodeGen *g, AstNode *node, FnTableEntry *fn_t
10371035
10381036
10391037
1040 TypeTableEntry *fn_type = analyze_fn_proto_type(g, import, containing_context, nullptr, node,
1038 TypeTableEntry *fn_type = analyze_fn_proto_type(g, import, containing_scope, nullptr, node,
10411039 fn_proto->is_nakedcc, fn_proto->is_coldcc, fn_table_entry);
10421040
10431041 fn_table_entry->type_entry = fn_type;
......@@ -1060,8 +1058,8 @@ static void resolve_function_proto(CodeGen *g, AstNode *node, FnTableEntry *fn_t
10601058 }
10611059
10621060 if (fn_table_entry->fn_def_node) {
1063 Scope *context = new_scope(fn_table_entry->fn_def_node, containing_context);
1064 fn_table_entry->fn_def_node->data.fn_def.scope = context;
1061 fn_table_entry->fn_def_node->data.fn_def.containing_scope = create_fndef_scope(
1062 fn_table_entry->fn_def_node, containing_scope, fn_table_entry);
10651063 }
10661064
10671065 if (!fn_wants_full_static_eval(fn_table_entry)) {
......@@ -1095,23 +1093,6 @@ static void resolve_function_proto(CodeGen *g, AstNode *node, FnTableEntry *fn_t
10951093 ZigLLVMAddFunctionAttr(fn_table_entry->fn_value, "no-frame-pointer-elim", "true");
10961094 ZigLLVMAddFunctionAttr(fn_table_entry->fn_value, "no-frame-pointer-elim-non-leaf", nullptr);
10971095 }
1098
1099 if (fn_table_entry->fn_def_node) {
1100 // Add debug info.
1101 unsigned line_number = node->line + 1;
1102 unsigned scope_line = line_number;
1103 bool is_definition = fn_table_entry->fn_def_node != nullptr;
1104 unsigned flags = 0;
1105 bool is_optimized = g->is_release_build;
1106 ZigLLVMDISubprogram *subprogram = ZigLLVMCreateFunction(g->dbuilder,
1107 containing_context->di_scope, buf_ptr(&fn_table_entry->symbol_name), "",
1108 import->di_file, line_number,
1109 fn_type->di_type, fn_table_entry->internal_linkage,
1110 is_definition, scope_line, flags, is_optimized, nullptr);
1111
1112 fn_table_entry->fn_def_node->data.fn_def.scope->di_scope = ZigLLVMSubprogramToScope(subprogram);
1113 ZigLLVMFnSetSubprogram(fn_table_entry->fn_value, subprogram);
1114 }
11151096 }
11161097}
11171098
......@@ -1151,7 +1132,7 @@ static void resolve_enum_type(CodeGen *g, ImportTableEntry *import, TypeTableEnt
11511132 uint64_t biggest_align_in_bits = 0;
11521133 uint64_t biggest_union_member_size_in_bits = 0;
11531134
1154 Scope *context = enum_type->data.enumeration.scope;
1135 Scope *scope = &enum_type->data.enumeration.decls_scope->base;
11551136
11561137 // set temporary flag
11571138 enum_type->data.enumeration.embedded_in_current = true;
......@@ -1161,7 +1142,7 @@ static void resolve_enum_type(CodeGen *g, ImportTableEntry *import, TypeTableEnt
11611142 AstNode *field_node = decl_node->data.struct_decl.fields.at(i);
11621143 TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[i];
11631144 type_enum_field->name = field_node->data.struct_field.name;
1164 TypeTableEntry *field_type = analyze_type_expr(g, import, context,
1145 TypeTableEntry *field_type = analyze_type_expr(g, import, scope,
11651146 field_node->data.struct_field.type);
11661147 type_enum_field->type_entry = field_type;
11671148 type_enum_field->value = i;
......@@ -1337,14 +1318,14 @@ static void resolve_struct_type(CodeGen *g, ImportTableEntry *import, TypeTableE
13371318 // this field should be set to true only during the recursive calls to resolve_struct_type
13381319 struct_type->data.structure.embedded_in_current = true;
13391320
1340 Scope *context = struct_type->data.structure.scope;
1321 Scope *scope = &struct_type->data.structure.decls_scope->base;
13411322
13421323 size_t gen_field_index = 0;
13431324 for (size_t i = 0; i < field_count; i += 1) {
13441325 AstNode *field_node = decl_node->data.struct_decl.fields.at(i);
13451326 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
13461327 type_struct_field->name = field_node->data.struct_field.name;
1347 TypeTableEntry *field_type = analyze_type_expr(g, import, context,
1328 TypeTableEntry *field_type = analyze_type_expr(g, import, scope,
13481329 field_node->data.struct_field.type);
13491330 type_struct_field->type_entry = field_type;
13501331 type_struct_field->src_index = i;
......@@ -1463,7 +1444,7 @@ static bool get_is_generic_fn(AstNode *proto_node) {
14631444}
14641445
14651446static void preview_fn_proto_instance(CodeGen *g, ImportTableEntry *import, AstNode *proto_node,
1466 Scope *containing_context)
1447 Scope *containing_scope)
14671448{
14681449 assert(proto_node->type == NodeTypeFnProto);
14691450
......@@ -1515,7 +1496,7 @@ static void preview_fn_proto_instance(CodeGen *g, ImportTableEntry *import, AstN
15151496
15161497
15171498 } else {
1518 resolve_function_proto(g, proto_node, fn_table_entry, import, containing_context);
1499 resolve_function_proto(g, proto_node, fn_table_entry, import, containing_scope);
15191500
15201501 if (!fn_wants_full_static_eval(fn_table_entry)) {
15211502 g->fn_protos.append(fn_table_entry);
......@@ -1546,7 +1527,7 @@ static void preview_fn_proto_instance(CodeGen *g, ImportTableEntry *import, AstN
15461527 }
15471528}
15481529
1549static void add_top_level_decl(CodeGen *g, ImportTableEntry *import, Scope *scope,
1530static void add_top_level_decl(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope,
15501531 AstNode *node, Buf *name)
15511532{
15521533 assert(import);
......@@ -1562,19 +1543,17 @@ static void add_top_level_decl(CodeGen *g, ImportTableEntry *import, Scope *scop
15621543 g->resolve_queue.append(node);
15631544 }
15641545
1565 node->scope = scope;
1546 node->scope = &decls_scope->base;
15661547
1567 auto entry = scope->decl_table.maybe_get(name);
1548 auto entry = decls_scope->decl_table.put_unique(name, node);
15681549 if (entry) {
15691550 AstNode *other_decl_node = entry->value;
15701551 ErrorMsg *msg = add_node_error(g, node, buf_sprintf("redefinition of '%s'", buf_ptr(name)));
15711552 add_error_note(g, msg, other_decl_node, buf_sprintf("previous definition is here"));
1572 } else {
1573 scope->decl_table.put(name, node);
15741553 }
15751554}
15761555
1577static void scan_struct_decl(CodeGen *g, ImportTableEntry *import, Scope *context, AstNode *node) {
1556static void scan_struct_decl(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope, AstNode *node) {
15781557 assert(node->type == NodeTypeContainerDecl);
15791558
15801559 if (node->data.struct_decl.type_entry) {
......@@ -1583,7 +1562,7 @@ static void scan_struct_decl(CodeGen *g, ImportTableEntry *import, Scope *contex
15831562 }
15841563
15851564 Buf *name = node->data.struct_decl.name;
1586 TypeTableEntry *container_type = get_partial_container_type(g, import, context,
1565 TypeTableEntry *container_type = get_partial_container_type(g, import, &decls_scope->base,
15871566 node->data.struct_decl.kind, node, buf_ptr(name));
15881567 node->data.struct_decl.type_entry = container_type;
15891568
......@@ -1591,8 +1570,8 @@ static void scan_struct_decl(CodeGen *g, ImportTableEntry *import, Scope *contex
15911570 for (size_t i = 0; i < node->data.struct_decl.decls.length; i += 1) {
15921571 AstNode *child_node = node->data.struct_decl.decls.at(i);
15931572 get_as_top_level_decl(child_node)->parent_decl = node;
1594 Scope *child_context = get_container_block_context(container_type);
1595 scan_decls(g, import, child_context, child_node);
1573 ScopeDecls *child_scope = get_container_scope(container_type);
1574 scan_decls(g, import, child_scope, child_node);
15961575 }
15971576}
15981577
......@@ -1644,37 +1623,37 @@ static void preview_error_value_decl(CodeGen *g, AstNode *node) {
16441623 node->data.error_value_decl.top_level_decl.resolution = TldResolutionOk;
16451624}
16461625
1647void scan_decls(CodeGen *g, ImportTableEntry *import, Scope *context, AstNode *node) {
1626void scan_decls(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope, AstNode *node) {
16481627 switch (node->type) {
16491628 case NodeTypeRoot:
16501629 for (size_t i = 0; i < import->root->data.root.top_level_decls.length; i += 1) {
16511630 AstNode *child = import->root->data.root.top_level_decls.at(i);
1652 scan_decls(g, import, context, child);
1631 scan_decls(g, import, decls_scope, child);
16531632 }
16541633 break;
16551634 case NodeTypeContainerDecl:
16561635 {
16571636 Buf *name = node->data.struct_decl.name;
1658 add_top_level_decl(g, import, context, node, name);
1637 add_top_level_decl(g, import, decls_scope, node, name);
16591638 if (node->data.struct_decl.generic_params.length == 0) {
1660 scan_struct_decl(g, import, context, node);
1639 scan_struct_decl(g, import, decls_scope, node);
16611640 }
16621641 }
16631642 break;
16641643 case NodeTypeFnDef:
16651644 node->data.fn_def.fn_proto->data.fn_proto.fn_def_node = node;
1666 scan_decls(g, import, context, node->data.fn_def.fn_proto);
1645 scan_decls(g, import, decls_scope, node->data.fn_def.fn_proto);
16671646 break;
16681647 case NodeTypeVariableDeclaration:
16691648 {
16701649 Buf *name = node->data.variable_declaration.symbol;
1671 add_top_level_decl(g, import, context, node, name);
1650 add_top_level_decl(g, import, decls_scope, node, name);
16721651 break;
16731652 }
16741653 case NodeTypeTypeDecl:
16751654 {
16761655 Buf *name = node->data.type_decl.symbol;
1677 add_top_level_decl(g, import, context, node, name);
1656 add_top_level_decl(g, import, decls_scope, node, name);
16781657 break;
16791658 }
16801659 case NodeTypeFnProto:
......@@ -1688,14 +1667,14 @@ void scan_decls(CodeGen *g, ImportTableEntry *import, Scope *context, AstNode *n
16881667 }
16891668 count_inline_and_var_args(node);
16901669
1691 add_top_level_decl(g, import, context, node, fn_name);
1670 add_top_level_decl(g, import, decls_scope, node, fn_name);
16921671 break;
16931672 }
16941673 case NodeTypeUse:
16951674 {
16961675 TopLevelDecl *tld = get_as_top_level_decl(node);
16971676 tld->import = import;
1698 node->scope = context;
1677 node->scope = &decls_scope->base;
16991678 g->use_queue.append(node);
17001679 tld->import->use_decls.append(node);
17011680 break;
......@@ -1816,70 +1795,65 @@ TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEnt
18161795
18171796// Set name to nullptr to make the variable anonymous (not visible to programmer).
18181797// TODO merge with definition of add_local_var in ir.cpp
1819static VariableTableEntry *add_local_var_shadowable(CodeGen *g, AstNode *source_node, ImportTableEntry *import,
1820 Scope *context, Buf *name, TypeTableEntry *type_entry, bool is_const, AstNode *val_node,
1821 bool shadowable)
1798static VariableTableEntry *add_local_var(CodeGen *g, AstNode *source_node, ImportTableEntry *import,
1799 Scope *parent_scope, Buf *name, TypeTableEntry *type_entry, bool is_const, AstNode *val_node)
18221800{
18231801 VariableTableEntry *variable_entry = allocate<VariableTableEntry>(1);
18241802 variable_entry->type = type_entry;
1825 variable_entry->scope = context;
1803 variable_entry->parent_scope = parent_scope;
18261804 variable_entry->import = import;
1827 variable_entry->shadowable = shadowable;
1805 variable_entry->shadowable = false;
18281806 variable_entry->mem_slot_index = SIZE_MAX;
18291807
1830 if (name) {
1831 buf_init_from_buf(&variable_entry->name, name);
1808 assert(name);
18321809
1833 if (type_entry->id != TypeTableEntryIdInvalid) {
1834 VariableTableEntry *existing_var = find_variable(g, context, name);
1835 if (existing_var && !existing_var->shadowable) {
1836 ErrorMsg *msg = add_node_error(g, source_node,
1837 buf_sprintf("redeclaration of variable '%s'", buf_ptr(name)));
1838 add_error_note(g, msg, existing_var->decl_node, buf_sprintf("previous declaration is here"));
1810 buf_init_from_buf(&variable_entry->name, name);
1811
1812 if (type_entry->id != TypeTableEntryIdInvalid) {
1813 VariableTableEntry *existing_var = find_variable(g, parent_scope, name);
1814 if (existing_var && !existing_var->shadowable) {
1815 ErrorMsg *msg = add_node_error(g, source_node,
1816 buf_sprintf("redeclaration of variable '%s'", buf_ptr(name)));
1817 add_error_note(g, msg, existing_var->decl_node, buf_sprintf("previous declaration is here"));
1818 variable_entry->type = g->builtin_types.entry_invalid;
1819 } else {
1820 auto primitive_table_entry = g->primitive_type_table.maybe_get(name);
1821 if (primitive_table_entry) {
1822 TypeTableEntry *type = primitive_table_entry->value;
1823 add_node_error(g, source_node,
1824 buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));
18391825 variable_entry->type = g->builtin_types.entry_invalid;
18401826 } else {
1841 auto primitive_table_entry = g->primitive_type_table.maybe_get(name);
1842 if (primitive_table_entry) {
1843 TypeTableEntry *type = primitive_table_entry->value;
1844 add_node_error(g, source_node,
1845 buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));
1827 AstNode *decl_node = find_decl(parent_scope, name);
1828 if (decl_node && decl_node->type != NodeTypeVariableDeclaration) {
1829 ErrorMsg *msg = add_node_error(g, source_node,
1830 buf_sprintf("redefinition of '%s'", buf_ptr(name)));
1831 add_error_note(g, msg, decl_node, buf_sprintf("previous definition is here"));
18461832 variable_entry->type = g->builtin_types.entry_invalid;
1847 } else {
1848 AstNode *decl_node = find_decl(context, name);
1849 if (decl_node && decl_node->type != NodeTypeVariableDeclaration) {
1850 ErrorMsg *msg = add_node_error(g, source_node,
1851 buf_sprintf("redefinition of '%s'", buf_ptr(name)));
1852 add_error_note(g, msg, decl_node, buf_sprintf("previous definition is here"));
1853 variable_entry->type = g->builtin_types.entry_invalid;
1854 }
18551833 }
18561834 }
18571835 }
1836 }
18581837
1859 context->var_table.put(&variable_entry->name, variable_entry);
1838 Scope *child_scope;
1839 if (source_node->type == NodeTypeParamDecl) {
1840 child_scope = create_var_scope(source_node, parent_scope, variable_entry);
18601841 } else {
1861 // TODO replace _anon with @anon and make sure all tests still pass
1862 buf_init_from_str(&variable_entry->name, "_anon");
1863 }
1864 if (context->fn_entry) {
1865 context->fn_entry->variable_list.append(variable_entry);
1842 // it's already in the decls table
1843 child_scope = parent_scope;
18661844 }
18671845
1846
18681847 variable_entry->src_is_const = is_const;
18691848 variable_entry->gen_is_const = is_const;
18701849 variable_entry->decl_node = source_node;
18711850 variable_entry->val_node = val_node;
1851 variable_entry->child_scope = child_scope;
18721852
18731853
18741854 return variable_entry;
18751855}
18761856
1877static VariableTableEntry *add_local_var(CodeGen *g, AstNode *source_node, ImportTableEntry *import,
1878 Scope *context, Buf *name, TypeTableEntry *type_entry, bool is_const, AstNode *val_node)
1879{
1880 return add_local_var_shadowable(g, source_node, import, context, name, type_entry, is_const, val_node, false);
1881}
1882
18831857static void resolve_var_decl(CodeGen *g, ImportTableEntry *import, AstNode *node) {
18841858 assert(node->type == NodeTypeVariableDeclaration);
18851859
......@@ -1889,8 +1863,6 @@ static void resolve_var_decl(CodeGen *g, ImportTableEntry *import, AstNode *node
18891863 bool is_export = (var_decl->top_level_decl.visib_mod == VisibModExport);
18901864 bool is_extern = var_decl->is_extern;
18911865
1892 assert(!scope->fn_entry);
1893
18941866 TypeTableEntry *explicit_type = nullptr;
18951867 if (var_decl->type) {
18961868 TypeTableEntry *proposed_type = analyze_type_expr(g, import, scope, var_decl->type);
......@@ -1980,7 +1952,7 @@ void resolve_top_level_decl(CodeGen *g, AstNode *node, bool pointer_only) {
19801952 if (node->data.type_decl.override_type) {
19811953 entry = node->data.type_decl.override_type;
19821954 } else {
1983 TypeTableEntry *child_type = analyze_type_expr(g, import, import->scope, type_node);
1955 TypeTableEntry *child_type = analyze_type_expr(g, import, &import->decls_scope->base, type_node);
19841956 if (child_type->id == TypeTableEntryIdInvalid) {
19851957 entry = child_type;
19861958 } else {
......@@ -2170,52 +2142,105 @@ bool types_match_const_cast_only(TypeTableEntry *expected_type, TypeTableEntry *
21702142 return false;
21712143}
21722144
2173Scope *new_scope(AstNode *node, Scope *parent) {
2174 Scope *context = allocate<Scope>(1);
2175 context->node = node;
2176 context->parent = parent;
2177 context->decl_table.init(1);
2178 context->var_table.init(1);
2179 context->label_table.init(1);
2145ScopeDecls *create_decls_scope(AstNode *node, Scope *parent) {
2146 assert(node->type == NodeTypeRoot || node->type == NodeTypeContainerDecl);
2147 ScopeDecls *scope = allocate<ScopeDecls>(1);
2148 scope->base.node = node;
2149 scope->base.parent = parent;
2150 scope->decl_table.init(4);
2151 return scope;
2152}
2153
2154Scope *create_block_scope(AstNode *node, Scope *parent) {
2155 assert(node->type == NodeTypeBlock);
2156 ScopeBlock *scope = allocate<ScopeBlock>(1);
2157 scope->base.node = node;
2158 scope->base.parent = parent;
2159 scope->label_table.init(1);
2160 return &scope->base;
2161}
21802162
2181 if (parent) {
2182 context->parent_loop_node = parent->parent_loop_node;
2183 context->c_import_buf = parent->c_import_buf;
2184 }
2163Scope *create_defer_scope(AstNode *node, Scope *parent) {
2164 assert(node->type == NodeTypeDefer);
2165 ScopeDefer *scope = allocate<ScopeDefer>(1);
2166 scope->base.node = node;
2167 scope->base.parent = parent;
2168 return &scope->base;
2169}
21852170
2186 if (node && node->type == NodeTypeFnDef) {
2187 AstNode *fn_proto_node = node->data.fn_def.fn_proto;
2188 context->fn_entry = fn_proto_node->data.fn_proto.fn_table_entry;
2189 } else if (parent) {
2190 context->fn_entry = parent->fn_entry;
2191 }
2171Scope *create_var_scope(AstNode *node, Scope *parent, VariableTableEntry *var) {
2172 assert(node->type == NodeTypeVariableDeclaration || node->type == NodeTypeParamDecl);
2173 ScopeVarDecl *scope = allocate<ScopeVarDecl>(1);
2174 scope->base.node = node;
2175 scope->base.parent = parent;
2176 scope->var = var;
2177 return &scope->base;
2178}
21922179
2193 if (context->fn_entry) {
2194 context->fn_entry->all_block_contexts.append(context);
2195 }
2180Scope *create_cimport_scope(AstNode *node, Scope *parent) {
2181 assert(node->type == NodeTypeFnCallExpr);
2182 ScopeCImport *scope = allocate<ScopeCImport>(1);
2183 scope->base.node = node;
2184 scope->base.parent = parent;
2185 buf_resize(&scope->c_import_buf, 0);
2186 return &scope->base;
2187}
2188
2189Scope *create_loop_scope(AstNode *node, Scope *parent) {
2190 assert(node->type == NodeTypeWhileExpr || node->type == NodeTypeForExpr);
2191 ScopeLoop *scope = allocate<ScopeLoop>(1);
2192 scope->base.node = node;
2193 scope->base.parent = parent;
2194 return &scope->base;
2195}
21962196
2197 return context;
2197Scope *create_fndef_scope(AstNode *node, Scope *parent, FnTableEntry *fn_entry) {
2198 assert(node->type == NodeTypeFnDef);
2199 ScopeFnBody *scope = allocate<ScopeFnBody>(1);
2200 scope->base.node = node;
2201 scope->base.parent = parent;
2202 scope->fn_entry = fn_entry;
2203 return &scope->base;
21982204}
21992205
2200AstNode *find_decl(Scope *context, Buf *name) {
2201 while (context) {
2202 auto entry = context->decl_table.maybe_get(name);
2203 if (entry) {
2204 return entry->value;
2206AstNode *find_decl(Scope *scope, Buf *name) {
2207 while (scope) {
2208 if (scope->node->type == NodeTypeRoot ||
2209 scope->node->type == NodeTypeContainerDecl)
2210 {
2211 ScopeDecls *decls_scope = (ScopeDecls *)scope;
2212 auto entry = decls_scope->decl_table.maybe_get(name);
2213 if (entry)
2214 return entry->value;
22052215 }
2206 context = context->parent;
2216 scope = scope->parent;
22072217 }
22082218 return nullptr;
22092219}
22102220
2211VariableTableEntry *find_variable(CodeGen *g, Scope *orig_context, Buf *name) {
2212 Scope *context = orig_context;
2213 while (context) {
2214 auto entry = context->var_table.maybe_get(name);
2215 if (entry) {
2216 return entry->value;
2221VariableTableEntry *find_variable(CodeGen *g, Scope *scope, Buf *name) {
2222 while (scope) {
2223 if (scope->node->type == NodeTypeVariableDeclaration ||
2224 scope->node->type == NodeTypeParamDecl)
2225 {
2226 ScopeVarDecl *var_scope = (ScopeVarDecl *)scope;
2227 if (buf_eql_buf(name, &var_scope->var->name))
2228 return var_scope->var;
2229 } else if (scope->node->type == NodeTypeRoot ||
2230 scope->node->type == NodeTypeContainerDecl)
2231 {
2232 ScopeDecls *decls_scope = (ScopeDecls *)scope;
2233 auto entry = decls_scope->decl_table.maybe_get(name);
2234 if (entry) {
2235 AstNode *decl_node = entry->value;
2236 if (decl_node->type == NodeTypeVariableDeclaration) {
2237 VariableTableEntry *var = decl_node->data.variable_declaration.variable;
2238 if (var)
2239 return var;
2240 }
2241 }
22172242 }
2218 context = context->parent;
2243 scope = scope->parent;
22192244 }
22202245
22212246 return nullptr;
......@@ -2355,7 +2380,7 @@ static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {
23552380 }
23562381 fn_table_entry->anal_state = FnAnalStateProbing;
23572382
2358 Scope *context = node->data.fn_def.scope;
2383 Scope *child_scope = node->data.fn_def.containing_scope;
23592384
23602385 TypeTableEntry *fn_type = fn_table_entry->type_entry;
23612386 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
......@@ -2381,16 +2406,20 @@ static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {
23812406 add_node_error(g, param_decl_node, buf_sprintf("missing parameter name"));
23822407 }
23832408
2384 VariableTableEntry *var = add_local_var(g, param_decl_node, import, context, param_decl->name,
2409 VariableTableEntry *var = add_local_var(g, param_decl_node, import, child_scope, param_decl->name,
23852410 type, true, nullptr);
23862411 var->src_arg_index = i;
23872412 param_decl_node->data.param_decl.variable = var;
2413 child_scope = var->child_scope;
2414 fn_table_entry->variable_list.append(var);
23882415
23892416 if (fn_type->data.fn.gen_param_info) {
23902417 var->gen_arg_index = fn_type->data.fn.gen_param_info[i].gen_index;
23912418 }
23922419 }
23932420
2421 node->data.fn_def.child_scope = child_scope;
2422
23942423 TypeTableEntry *expected_type = fn_type->data.fn.fn_type_id.return_type;
23952424
23962425 if (fn_type->data.fn.fn_type_id.is_extern && handle_is_ptr(expected_type)) {
......@@ -2456,7 +2485,7 @@ static void add_symbols_from_import(CodeGen *g, AstNode *src_use_node, AstNode *
24562485 continue;
24572486 }
24582487 if (target_tld->visib_mod != VisibModPrivate) {
2459 auto existing_entry = tld->import->scope->decl_table.maybe_get(target_tld->name);
2488 auto existing_entry = tld->import->decls_scope->decl_table.put_unique(target_tld->name, decl_node);
24602489 if (existing_entry) {
24612490 AstNode *existing_decl = existing_entry->value;
24622491 if (existing_decl != decl_node) {
......@@ -2466,8 +2495,6 @@ static void add_symbols_from_import(CodeGen *g, AstNode *src_use_node, AstNode *
24662495 add_error_note(g, msg, existing_decl, buf_sprintf("previous definition here"));
24672496 add_error_note(g, msg, decl_node, buf_sprintf("imported definition here"));
24682497 }
2469 } else {
2470 tld->import->scope->decl_table.put(target_tld->name, decl_node);
24712498 }
24722499 }
24732500 }
......@@ -2494,7 +2521,7 @@ void preview_use_decl(CodeGen *g, AstNode *node) {
24942521 assert(node->type == NodeTypeUse);
24952522 TopLevelDecl *tld = get_as_top_level_decl(node);
24962523
2497 IrInstruction *result = analyze_const_value(g, tld->import->scope, node->data.use.expr,
2524 IrInstruction *result = analyze_const_value(g, &tld->import->decls_scope->base, node->data.use.expr,
24982525 g->builtin_types.entry_namespace);
24992526 if (result->type_entry->id == TypeTableEntryIdInvalid)
25002527 tld->import->any_imports_failed = true;
......@@ -2553,8 +2580,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package,
25532580 g->import_table.put(abs_full_path, import_entry);
25542581 g->import_queue.append(import_entry);
25552582
2556 import_entry->scope = new_scope(import_entry->root, nullptr);
2557 import_entry->scope->di_scope = ZigLLVMFileToScope(import_entry->di_file);
2583 import_entry->decls_scope = create_decls_scope(import_entry->root, nullptr);
25582584
25592585
25602586 assert(import_entry->root->type == NodeTypeRoot);
......@@ -2581,7 +2607,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package,
25812607void semantic_analyze(CodeGen *g) {
25822608 for (; g->import_queue_index < g->import_queue.length; g->import_queue_index += 1) {
25832609 ImportTableEntry *import = g->import_queue.at(g->import_queue_index);
2584 scan_decls(g, import, import->scope, import->root);
2610 scan_decls(g, import, import->decls_scope, import->root);
25852611 }
25862612
25872613 for (; g->use_queue_index < g->use_queue.length; g->use_queue_index += 1) {
src/analyze.hpp+10-3
......@@ -15,7 +15,6 @@ ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg);
1515ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, AstNode *node, Buf *msg);
1616TypeTableEntry *new_type_table_entry(TypeTableEntryId id);
1717TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool is_const);
18Scope *new_scope(AstNode *node, Scope *parent);
1918bool is_node_void_expr(AstNode *node);
2019uint64_t type_size(CodeGen *g, TypeTableEntry *type_entry);
2120TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, size_t size_in_bits);
......@@ -59,11 +58,19 @@ TypeTableEntry *container_ref_type(TypeTableEntry *type_entry);
5958bool type_is_complete(TypeTableEntry *type_entry);
6059void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry);
6160TypeStructField *find_struct_type_field(TypeTableEntry *type_entry, Buf *name);
62Scope *get_container_block_context(TypeTableEntry *type_entry);
61ScopeDecls *get_container_scope(TypeTableEntry *type_entry);
6362TypeEnumField *find_enum_type_field(TypeTableEntry *enum_type, Buf *name);
6463bool is_container_ref(TypeTableEntry *type_entry);
65void scan_decls(CodeGen *g, ImportTableEntry *import, Scope *context, AstNode *node);
64void scan_decls(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope, AstNode *node);
6665void preview_use_decl(CodeGen *g, AstNode *node);
6766void resolve_use_decl(CodeGen *g, AstNode *node);
6867
68ScopeDecls *create_decls_scope(AstNode *node, Scope *parent);
69Scope *create_block_scope(AstNode *node, Scope *parent);
70Scope *create_defer_scope(AstNode *node, Scope *parent);
71Scope *create_var_scope(AstNode *node, Scope *parent, VariableTableEntry *var);
72Scope *create_cimport_scope(AstNode *node, Scope *parent);
73Scope *create_loop_scope(AstNode *node, Scope *parent);
74Scope *create_fndef_scope(AstNode *node, Scope *parent, FnTableEntry *fn_entry);
75
6976#endif
src/codegen.cpp+44-26
......@@ -227,9 +227,47 @@ void codegen_set_rdynamic(CodeGen *g, bool rdynamic) {
227227static void render_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val);
228228static void render_const_val_global(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val);
229229
230static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
231 if (scope->di_scope)
232 return scope->di_scope;
233
234 if (scope->node->type == NodeTypeFnDef) {
235 assert(scope->parent);
236 ScopeFnBody *fn_scope = (ScopeFnBody *)scope;
237 FnTableEntry *fn_table_entry = fn_scope->fn_entry;
238 unsigned line_number = fn_table_entry->proto_node->line + 1;
239 unsigned scope_line = line_number;
240 bool is_definition = fn_table_entry->fn_def_node != nullptr;
241 unsigned flags = 0;
242 bool is_optimized = g->is_release_build;
243 ZigLLVMDISubprogram *subprogram = ZigLLVMCreateFunction(g->dbuilder,
244 get_di_scope(g, scope->parent), buf_ptr(&fn_table_entry->symbol_name), "",
245 scope->node->owner->di_file, line_number,
246 fn_table_entry->type_entry->di_type, fn_table_entry->internal_linkage,
247 is_definition, scope_line, flags, is_optimized, nullptr);
248
249 scope->di_scope = ZigLLVMSubprogramToScope(subprogram);
250 ZigLLVMFnSetSubprogram(fn_table_entry->fn_value, subprogram);
251 } else if (scope->node->type == NodeTypeRoot ||
252 scope->node->type == NodeTypeContainerDecl)
253 {
254 scope->di_scope = ZigLLVMFileToScope(scope->node->owner->di_file);
255 } else {
256 assert(scope->parent);
257 ZigLLVMDILexicalBlock *di_block = ZigLLVMCreateLexicalBlock(g->dbuilder,
258 get_di_scope(g, scope->parent),
259 scope->node->owner->di_file,
260 scope->node->line + 1,
261 scope->node->column + 1);
262 scope->di_scope = ZigLLVMLexicalBlockToScope(di_block);
263 }
264
265 return scope->di_scope;
266}
267
230268static void set_debug_source_node(CodeGen *g, AstNode *node) {
231269 assert(node->scope);
232 ZigLLVMSetCurrentDebugLocation(g->builder, node->line + 1, node->column + 1, node->scope->di_scope);
270 ZigLLVMSetCurrentDebugLocation(g->builder, node->line + 1, node->column + 1, get_di_scope(g, node->scope));
233271}
234272
235273static void clear_debug_source_node(CodeGen *g) {
......@@ -558,10 +596,9 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, AstNode *source_node,
558596}
559597
560598static void gen_var_debug_decl(CodeGen *g, VariableTableEntry *var) {
561 Scope *scope = var->scope;
562599 AstNode *source_node = var->decl_node;
563600 ZigLLVMDILocation *debug_loc = ZigLLVMGetDebugLoc(source_node->line + 1, source_node->column + 1,
564 scope->di_scope);
601 get_di_scope(g, var->parent_scope));
565602 ZigLLVMInsertDeclareAtEnd(g->dbuilder, var->value_ref, var->di_loc_var, debug_loc,
566603 LLVMGetInsertBlock(g->builder));
567604}
......@@ -2133,8 +2170,7 @@ static void gen_global_var(CodeGen *g, VariableTableEntry *var, LLVMValueRef ini
21332170 assert(var->import);
21342171 assert(type_entry);
21352172 bool is_local_to_unit = true;
2136 ZigLLVMCreateGlobalVariable(g->dbuilder,
2137 var->scope->di_scope, buf_ptr(&var->name),
2173 ZigLLVMCreateGlobalVariable(g->dbuilder, get_di_scope(g, var->parent_scope), buf_ptr(&var->name),
21382174 buf_ptr(&var->name), var->import->di_file, var->decl_node->line + 1,
21392175 type_entry->di_type, is_local_to_unit, init_val);
21402176}
......@@ -2328,24 +2364,6 @@ static void do_code_gen(CodeGen *g) {
23282364 AstNodeFnProto *fn_proto = &proto_node->data.fn_proto;
23292365
23302366 build_all_basic_blocks(g, fn_table_entry);
2331
2332
2333 // Set up debug info for blocks
2334 for (size_t bc_i = 0; bc_i < fn_table_entry->all_block_contexts.length; bc_i += 1) {
2335 Scope *scope = fn_table_entry->all_block_contexts.at(bc_i);
2336
2337 if (!scope->di_scope) {
2338 ZigLLVMDILexicalBlock *di_block = ZigLLVMCreateLexicalBlock(g->dbuilder,
2339 scope->parent->di_scope,
2340 import->di_file,
2341 scope->node->line + 1,
2342 scope->node->column + 1);
2343 scope->di_scope = ZigLLVMLexicalBlockToScope(di_block);
2344 }
2345
2346
2347 }
2348
23492367 clear_debug_source_node(g);
23502368
23512369 // allocate temporary stack data
......@@ -2383,7 +2401,7 @@ static void do_code_gen(CodeGen *g) {
23832401 if (var->is_inline)
23842402 continue;
23852403
2386 if (var->scope->node->type == NodeTypeFnDef) {
2404 if (var->parent_scope->node->type == NodeTypeFnDef) {
23872405 assert(var->gen_arg_index != SIZE_MAX);
23882406 TypeTableEntry *gen_type;
23892407 if (handle_is_ptr(var->type)) {
......@@ -2395,7 +2413,7 @@ static void do_code_gen(CodeGen *g) {
23952413 unsigned align_bytes = ZigLLVMGetPrefTypeAlignment(g->target_data_ref, var->type->type_ref);
23962414 LLVMSetAlignment(var->value_ref, align_bytes);
23972415 }
2398 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, var->scope->di_scope,
2416 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
23992417 buf_ptr(&var->name), import->di_file, var->decl_node->line + 1,
24002418 gen_type->di_type, !g->strip_debug_symbols, 0, var->gen_arg_index + 1);
24012419
......@@ -2406,7 +2424,7 @@ static void do_code_gen(CodeGen *g) {
24062424 unsigned align_bytes = ZigLLVMGetPrefTypeAlignment(g->target_data_ref, var->type->type_ref);
24072425 LLVMSetAlignment(var->value_ref, align_bytes);
24082426
2409 var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, var->scope->di_scope,
2427 var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
24102428 buf_ptr(&var->name), import->di_file, var->decl_node->line + 1,
24112429 var->type->di_type, !g->strip_debug_symbols, 0);
24122430 }
src/hash_map.hpp+9
......@@ -61,6 +61,15 @@ public:
6161 }
6262 }
6363
64 Entry *put_unique(const K &key, const V &value) {
65 // TODO make this more efficient
66 Entry *entry = internal_get(key);
67 if (entry)
68 return entry;
69 put(key, value);
70 return nullptr;
71 }
72
6473 const V &get(const K &key) const {
6574 Entry *entry = internal_get(key);
6675 if (!entry)
src/ir.cpp+108-65
......@@ -1211,28 +1211,39 @@ static IrInstruction *ir_build_ref_from(IrBuilder *irb, IrInstruction *old_instr
12111211 return new_instruction;
12121212}
12131213
1214static void ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_block, Scope *outer_block,
1214static void ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope,
12151215 bool gen_error_defers, bool gen_maybe_defers)
12161216{
1217 while (inner_block != outer_block) {
1218 if (inner_block->node->type == NodeTypeDefer &&
1219 ((inner_block->node->data.defer.kind == ReturnKindUnconditional) ||
1220 (gen_error_defers && inner_block->node->data.defer.kind == ReturnKindError) ||
1221 (gen_maybe_defers && inner_block->node->data.defer.kind == ReturnKindMaybe)))
1217 while (inner_scope != outer_scope) {
1218 if (inner_scope->node->type == NodeTypeDefer &&
1219 ((inner_scope->node->data.defer.kind == ReturnKindUnconditional) ||
1220 (gen_error_defers && inner_scope->node->data.defer.kind == ReturnKindError) ||
1221 (gen_maybe_defers && inner_scope->node->data.defer.kind == ReturnKindMaybe)))
12221222 {
1223 AstNode *defer_expr_node = inner_block->node->data.defer.expr;
1223 AstNode *defer_expr_node = inner_scope->node->data.defer.expr;
12241224 ir_gen_node(irb, defer_expr_node, defer_expr_node->scope);
12251225 }
1226 inner_block = inner_block->parent;
1226 inner_scope = inner_scope->parent;
12271227 }
12281228}
12291229
1230static FnTableEntry *scope_fn_entry(Scope *scope) {
1231 while (scope) {
1232 if (scope->node->type == NodeTypeFnDef) {
1233 ScopeFnBody *fn_scope = (ScopeFnBody *)scope;
1234 return fn_scope->fn_entry;
1235 }
1236 scope = scope->parent;
1237 }
1238 return nullptr;
1239}
1240
12301241static IrInstruction *ir_gen_return(IrBuilder *irb, AstNode *node) {
12311242 assert(node->type == NodeTypeReturnExpr);
12321243
12331244 Scope *scope = node->scope;
12341245
1235 if (!scope->fn_entry) {
1246 if (!scope_fn_entry(scope)) {
12361247 add_node_error(irb->codegen, node, buf_sprintf("return expression outside function definition"));
12371248 return irb->codegen->invalid_instruction;
12381249 }
......@@ -1243,7 +1254,7 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, AstNode *node) {
12431254 {
12441255 IrInstruction *return_value;
12451256 if (expr_node) {
1246 return_value = ir_gen_node(irb, expr_node, scope);
1257 return_value = ir_gen_node(irb, expr_node, node->scope);
12471258 } else {
12481259 return_value = ir_build_const_void(irb, node);
12491260 }
......@@ -1264,11 +1275,11 @@ static void ir_set_cursor_at_end(IrBuilder *irb, IrBasicBlock *basic_block) {
12641275 irb->current_basic_block = basic_block;
12651276}
12661277
1267static VariableTableEntry *add_local_var(CodeGen *codegen, AstNode *node, Scope *scope,
1278static VariableTableEntry *add_local_var(CodeGen *codegen, AstNode *node, Scope *parent_scope,
12681279 Buf *name, bool src_is_const, bool gen_is_const, bool is_shadowable, bool is_inline)
12691280{
12701281 VariableTableEntry *variable_entry = allocate<VariableTableEntry>(1);
1271 variable_entry->scope = scope;
1282 variable_entry->parent_scope = parent_scope;
12721283 variable_entry->import = node->owner;
12731284 variable_entry->shadowable = is_shadowable;
12741285 variable_entry->mem_slot_index = SIZE_MAX;
......@@ -1277,7 +1288,7 @@ static VariableTableEntry *add_local_var(CodeGen *codegen, AstNode *node, Scope
12771288 if (name) {
12781289 buf_init_from_buf(&variable_entry->name, name);
12791290
1280 VariableTableEntry *existing_var = find_variable(codegen, scope, name);
1291 VariableTableEntry *existing_var = find_variable(codegen, parent_scope, name);
12811292 if (existing_var && !existing_var->shadowable) {
12821293 ErrorMsg *msg = add_node_error(codegen, node,
12831294 buf_sprintf("redeclaration of variable '%s'", buf_ptr(name)));
......@@ -1291,7 +1302,7 @@ static VariableTableEntry *add_local_var(CodeGen *codegen, AstNode *node, Scope
12911302 buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));
12921303 variable_entry->type = codegen->builtin_types.entry_invalid;
12931304 } else {
1294 AstNode *decl_node = find_decl(scope, name);
1305 AstNode *decl_node = find_decl(parent_scope, name);
12951306 if (decl_node && decl_node->type != NodeTypeVariableDeclaration) {
12961307 ErrorMsg *msg = add_node_error(codegen, node,
12971308 buf_sprintf("redefinition of '%s'", buf_ptr(name)));
......@@ -1301,28 +1312,32 @@ static VariableTableEntry *add_local_var(CodeGen *codegen, AstNode *node, Scope
13011312 }
13021313 }
13031314
1304 scope->var_table.put(&variable_entry->name, variable_entry);
13051315 } else {
13061316 assert(is_shadowable);
1307 // TODO replace _anon with @anon and make sure all tests still pass
1317 // TODO make this name not actually be in scope. user should be able to make a variable called "_anon"
1318 // might already be solved, let's just make sure it has test coverage
1319 // maybe we put a prefix on this so the debug info doesn't clobber user debug info for same named variables
13081320 buf_init_from_str(&variable_entry->name, "_anon");
13091321 }
13101322
13111323 variable_entry->src_is_const = src_is_const;
13121324 variable_entry->gen_is_const = gen_is_const;
13131325 variable_entry->decl_node = node;
1326 variable_entry->child_scope = create_var_scope(node, parent_scope, variable_entry);
13141327
13151328 return variable_entry;
13161329}
13171330
13181331// Set name to nullptr to make the variable anonymous (not visible to programmer).
1319static VariableTableEntry *ir_add_local_var(IrBuilder *irb, AstNode *node, Scope *scope, Buf *name,
1332// After you call this function var->child_scope has the variable in scope
1333static VariableTableEntry *ir_create_var(IrBuilder *irb, AstNode *node, Scope *scope, Buf *name,
13201334 bool src_is_const, bool gen_is_const, bool is_shadowable, bool is_inline)
13211335{
13221336 VariableTableEntry *var = add_local_var(irb->codegen, node, scope, name,
13231337 src_is_const, gen_is_const, is_shadowable, is_inline);
13241338 if (is_inline || gen_is_const)
13251339 var->mem_slot_index = exec_next_mem_slot(irb->exec);
1340 assert(var->child_scope);
13261341 return var;
13271342}
13281343
......@@ -1330,7 +1345,7 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, AstNode *block_node) {
13301345 assert(block_node->type == NodeTypeBlock);
13311346
13321347 Scope *parent_scope = block_node->scope;
1333 Scope *outer_block_scope = new_scope(block_node, parent_scope);
1348 Scope *outer_block_scope = create_block_scope(block_node, parent_scope);
13341349 Scope *child_scope = outer_block_scope;
13351350
13361351 IrInstruction *return_value = nullptr;
......@@ -1341,6 +1356,9 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, AstNode *block_node) {
13411356 // defer starts a new block context
13421357 child_scope = statement_node->data.defer.child_block;
13431358 assert(child_scope);
1359 } else if (return_value->id == IrInstructionIdDeclVar) {
1360 IrInstructionDeclVar *decl_var_instruction = (IrInstructionDeclVar *)return_value;
1361 child_scope = decl_var_instruction->var->child_scope;
13441362 }
13451363 }
13461364
......@@ -1760,7 +1778,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, AstNode *node) {
17601778 if (arg0_value == irb->codegen->invalid_instruction)
17611779 return arg0_value;
17621780
1763 if (node->scope->fn_entry) {
1781 if (scope_fn_entry(node->scope)) {
17641782 add_node_error(irb->codegen, node, buf_sprintf("import valid only at top level scope"));
17651783 return irb->codegen->invalid_instruction;
17661784 }
......@@ -1999,8 +2017,10 @@ static IrInstruction *ir_gen_var_decl(IrBuilder *irb, AstNode *node) {
19992017 bool is_const = variable_declaration->is_const;
20002018 bool is_extern = variable_declaration->is_extern;
20012019 bool is_inline = ir_should_inline(irb) || variable_declaration->is_inline;
2002 VariableTableEntry *var = ir_add_local_var(irb, node, node->scope,
2020 VariableTableEntry *var = ir_create_var(irb, node, node->scope,
20032021 variable_declaration->symbol, is_const, is_const, is_shadowable, is_inline);
2022 // we detect IrInstructionIdDeclVar in gen_block to make sure the next node
2023 // is inside var->child_scope
20042024
20052025 if (!is_extern && !variable_declaration->expr) {
20062026 var->type = irb->codegen->builtin_types.entry_invalid;
......@@ -2079,14 +2099,15 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, AstNode *node) {
20792099 }
20802100 bool is_inline = ir_should_inline(irb) || node->data.for_expr.is_inline;
20812101
2082 Scope *child_scope = new_scope(node, parent_scope);
2083 child_scope->parent_loop_node = node;
2102 Scope *child_scope = create_loop_scope(node, parent_scope);
20842103 elem_node->scope = child_scope;
20852104
20862105 // TODO make it an error to write to element variable or i variable.
20872106 Buf *elem_var_name = elem_node->data.symbol_expr.symbol;
2088 node->data.for_expr.elem_var = ir_add_local_var(irb, elem_node, child_scope, elem_var_name,
2107 node->data.for_expr.elem_var = ir_create_var(irb, elem_node, child_scope, elem_var_name,
20892108 true, false, false, is_inline);
2109 child_scope = node->data.for_expr.elem_var->child_scope;
2110
20902111 IrInstruction *undefined_value = ir_build_const_undefined(irb, elem_node);
20912112 ir_build_var_decl(irb, elem_node, node->data.for_expr.elem_var, elem_var_type, undefined_value);
20922113 IrInstruction *elem_var_ptr = ir_build_var_ptr(irb, node, node->data.for_expr.elem_var);
......@@ -2096,13 +2117,15 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, AstNode *node) {
20962117 index_var_source_node = index_node;
20972118 Buf *index_var_name = index_node->data.symbol_expr.symbol;
20982119 index_node->scope = child_scope;
2099 node->data.for_expr.index_var = ir_add_local_var(irb, index_node, child_scope, index_var_name,
2120 node->data.for_expr.index_var = ir_create_var(irb, index_node, child_scope, index_var_name,
21002121 true, false, false, is_inline);
21012122 } else {
21022123 index_var_source_node = node;
2103 node->data.for_expr.index_var = ir_add_local_var(irb, node, child_scope, nullptr,
2124 node->data.for_expr.index_var = ir_create_var(irb, node, child_scope, nullptr,
21042125 true, false, true, is_inline);
21052126 }
2127 child_scope = node->data.for_expr.index_var->child_scope;
2128
21062129 IrInstruction *usize = ir_build_const_type(irb, node, irb->codegen->builtin_types.entry_usize);
21072130 IrInstruction *zero = ir_build_const_usize(irb, node, 0);
21082131 IrInstruction *one = ir_build_const_usize(irb, node, 1);
......@@ -2159,10 +2182,11 @@ static IrInstruction *ir_gen_this_literal(IrBuilder *irb, AstNode *node) {
21592182 if (!scope->parent)
21602183 return ir_build_const_import(irb, node, node->owner);
21612184
2162 if (scope->fn_entry && (!scope->parent->fn_entry ||
2163 (scope->parent->parent && !scope->parent->parent->fn_entry)))
2185 FnTableEntry *fn_entry = scope_fn_entry(scope);
2186 if (fn_entry && scope->parent && scope->parent->parent &&
2187 !scope_fn_entry(scope->parent->parent))
21642188 {
2165 return ir_build_const_fn(irb, node, scope->fn_entry);
2189 return ir_build_const_fn(irb, node, fn_entry);
21662190 }
21672191
21682192 if (scope->node->type == NodeTypeContainerDecl) {
......@@ -2308,15 +2332,15 @@ static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, AstNode *node) {
23082332 if (var_type == irb->codegen->invalid_instruction)
23092333 return irb->codegen->invalid_instruction;
23102334 }
2311 Scope *child_scope = new_scope(node, node->scope);
23122335 bool is_shadowable = false;
23132336 bool is_const = var_decl->is_const;
2314 VariableTableEntry *var = ir_add_local_var(irb, node, child_scope,
2337 VariableTableEntry *var = ir_create_var(irb, node, node->scope,
23152338 var_decl->symbol, is_const, is_const, is_shadowable, is_inline);
2339
23162340 IrInstruction *var_ptr_value = ir_build_unwrap_maybe(irb, node, expr_value, false);
23172341 IrInstruction *var_value = var_is_ptr ? var_ptr_value : ir_build_load_ptr(irb, node, var_ptr_value);
23182342 ir_build_var_decl(irb, node, var, var_type, var_value);
2319 IrInstruction *then_expr_result = ir_gen_node(irb, then_node, child_scope);
2343 IrInstruction *then_expr_result = ir_gen_node(irb, then_node, var->child_scope);
23202344 if (then_expr_result == irb->codegen->invalid_instruction)
23212345 return then_expr_result;
23222346 IrBasicBlock *after_then_block = irb->current_basic_block;
......@@ -2360,11 +2384,11 @@ static bool ir_gen_switch_prong_expr(IrBuilder *irb, AstNode *switch_node, AstNo
23602384 Buf *var_name = var_symbol_node->data.symbol_expr.symbol;
23612385 bool var_is_ptr = prong_node->data.switch_prong.var_is_ptr;
23622386
2363 child_scope = new_scope(switch_node, switch_node->scope);
23642387 bool is_shadowable = false;
23652388 bool is_const = true;
2366 VariableTableEntry *var = ir_add_local_var(irb, var_symbol_node, child_scope,
2389 VariableTableEntry *var = ir_create_var(irb, var_symbol_node, switch_node->scope,
23672390 var_name, is_const, is_const, is_shadowable, is_inline);
2391 child_scope = var->child_scope;
23682392 IrInstruction *var_value;
23692393 if (prong_value) {
23702394 IrInstruction *var_ptr_value = ir_build_switch_var(irb, var_symbol_node, target_value_ptr, prong_value);
......@@ -2542,14 +2566,25 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, AstNode *node) {
25422566 return ir_build_phi(irb, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
25432567}
25442568
2545static LabelTableEntry *find_label(IrExecutable *exec, Scope *orig_context, Buf *name) {
2546 Scope *context = orig_context;
2547 while (context) {
2548 auto entry = context->label_table.maybe_get(name);
2549 if (entry) {
2550 return entry->value;
2569static LabelTableEntry *find_label(IrExecutable *exec, Scope *scope, Buf *name) {
2570 while (scope) {
2571 if (scope->node->type == NodeTypeBlock) {
2572 ScopeBlock *block_scope = (ScopeBlock *)scope;
2573 auto entry = block_scope->label_table.maybe_get(name);
2574 if (entry)
2575 return entry->value;
25512576 }
2552 context = context->parent;
2577 scope = scope->parent;
2578 }
2579
2580 return nullptr;
2581}
2582
2583static ScopeBlock *find_block_scope(IrExecutable *exec, Scope *scope) {
2584 while (scope) {
2585 if (scope->node->type == NodeTypeBlock)
2586 return (ScopeBlock *)scope;
2587 scope = scope->parent;
25532588 }
25542589 return nullptr;
25552590}
......@@ -2571,7 +2606,8 @@ static IrInstruction *ir_gen_label(IrBuilder *irb, AstNode *node) {
25712606 add_error_note(irb->codegen, msg, existing_label->decl_node, buf_sprintf("other label here"));
25722607 return irb->codegen->invalid_instruction;
25732608 } else {
2574 node->scope->label_table.put(label_name, label);
2609 ScopeBlock *scope_block = find_block_scope(irb->exec, node->scope);
2610 scope_block->label_table.put(label_name, label);
25752611 }
25762612
25772613 bool is_inline = ir_should_inline(irb);
......@@ -2778,9 +2814,9 @@ IrInstruction *ir_gen_fn(CodeGen *codegn, FnTableEntry *fn_entry) {
27782814 assert(fn_def_node->type == NodeTypeFnDef);
27792815
27802816 AstNode *body_node = fn_def_node->data.fn_def.body;
2781 Scope *scope = fn_def_node->data.fn_def.scope;
2817 Scope *child_scope = fn_def_node->data.fn_def.child_scope;
27822818
2783 return ir_gen(codegn, body_node, scope, ir_executable);
2819 return ir_gen(codegn, body_node, child_scope, ir_executable);
27842820}
27852821
27862822static IrInstruction *ir_eval_fn(IrAnalyze *ira, IrInstruction *source_instruction,
......@@ -3023,8 +3059,10 @@ static IrInstruction *ir_resolve_cast(IrAnalyze *ira, IrInstruction *source_inst
30233059 } else {
30243060 IrInstruction *result = ir_build_cast(&ira->new_irb, source_instr->source_node, wanted_type, value, cast_op);
30253061 result->type_entry = wanted_type;
3026 if (need_alloca && source_instr->source_node->scope->fn_entry) {
3027 source_instr->source_node->scope->fn_entry->alloca_list.append(result);
3062 if (need_alloca) {
3063 FnTableEntry *fn_entry = scope_fn_entry(source_instr->source_node->scope);
3064 if (fn_entry)
3065 fn_entry->alloca_list.append(result);
30283066 }
30293067 return result;
30303068 }
......@@ -3550,7 +3588,8 @@ static TypeTableEntry *ir_analyze_ref(IrAnalyze *ira, IrInstruction *source_inst
35503588 ir_link_new_instruction(value, source_instruction);
35513589 return ptr_type;
35523590 } else {
3553 FnTableEntry *fn_entry = source_instruction->source_node->scope->fn_entry;
3591 FnTableEntry *fn_entry = scope_fn_entry(source_instruction->source_node->scope);
3592 assert(fn_entry);
35543593 IrInstruction *new_instruction = ir_build_ref_from(&ira->new_irb, source_instruction, value);
35553594 fn_entry->alloca_list.append(new_instruction);
35563595 return ptr_type;
......@@ -4098,8 +4137,9 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
40984137 ir_build_var_decl_from(&ira->new_irb, &decl_var_instruction->base, var, var_type, casted_init_value);
40994138
41004139 Scope *scope = decl_var_instruction->base.source_node->scope;
4101 if (scope->fn_entry)
4102 scope->fn_entry->variable_list.append(var);
4140 FnTableEntry *fn_entry = scope_fn_entry(scope);
4141 if (fn_entry)
4142 fn_entry->variable_list.append(var);
41034143
41044144 return ira->codegen->builtin_types.entry_void;
41054145}
......@@ -4200,8 +4240,11 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
42004240 IrInstruction *new_call_instruction = ir_build_call_from(&ira->new_irb, &call_instruction->base,
42014241 fn_entry, fn_ref, call_param_count, casted_args);
42024242
4203 if (type_has_bits(return_type) && handle_is_ptr(return_type))
4204 call_instruction->base.source_node->scope->fn_entry->alloca_list.append(new_call_instruction);
4243 if (type_has_bits(return_type) && handle_is_ptr(return_type)) {
4244 FnTableEntry *fn_entry = scope_fn_entry(call_instruction->base.source_node->scope);
4245 assert(fn_entry);
4246 fn_entry->alloca_list.append(new_call_instruction);
4247 }
42054248
42064249 return ir_finish_anal(ira, return_type);
42074250}
......@@ -4732,7 +4775,8 @@ static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruc
47324775 return var->type;
47334776
47344777 ConstExprValue *mem_slot = nullptr;
4735 if (var->scope->fn_entry) {
4778 FnTableEntry *fn_entry = scope_fn_entry(var->parent_scope);
4779 if (fn_entry) {
47364780 // TODO once the analyze code is fully ported over to IR we won't need this SIZE_MAX thing.
47374781 if (var->mem_slot_index != SIZE_MAX)
47384782 mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];
......@@ -4879,9 +4923,8 @@ static TypeTableEntry *ir_analyze_container_member_access_inner(IrAnalyze *ira,
48794923 IrInstruction *container_ptr, TypeTableEntry *container_type)
48804924{
48814925 if (!is_slice(bare_struct_type)) {
4882 Scope *container_block_context = get_container_block_context(bare_struct_type);
4883 assert(container_block_context);
4884 auto entry = container_block_context->decl_table.maybe_get(field_name);
4926 ScopeDecls *container_scope = get_container_scope(bare_struct_type);
4927 auto entry = container_scope->decl_table.maybe_get(field_name);
48854928 AstNode *fn_decl_node = entry ? entry->value : nullptr;
48864929 if (fn_decl_node && fn_decl_node->type == NodeTypeFnProto) {
48874930 resolve_top_level_decl(ira->codegen, fn_decl_node, false);
......@@ -5023,8 +5066,8 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
50235066 } else if (child_type->id == TypeTableEntryIdEnum) {
50245067 zig_panic("TODO enum type field");
50255068 } else if (child_type->id == TypeTableEntryIdStruct) {
5026 Scope *container_block_context = get_container_block_context(child_type);
5027 auto entry = container_block_context->decl_table.maybe_get(field_name);
5069 ScopeDecls *container_scope = get_container_scope(child_type);
5070 auto entry = container_scope->decl_table.maybe_get(field_name);
50285071 AstNode *decl_node = entry ? entry->value : nullptr;
50295072 if (decl_node) {
50305073 bool depends_on_compile_var = container_ptr->static_value.depends_on_compile_var;
......@@ -5055,7 +5098,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
50555098 ImportTableEntry *namespace_import = namespace_val->data.x_import;
50565099
50575100 bool depends_on_compile_var = container_ptr->static_value.depends_on_compile_var;
5058 AstNode *decl_node = find_decl(namespace_import->scope, field_name);
5101 AstNode *decl_node = find_decl(&namespace_import->decls_scope->base, field_name);
50595102 if (!decl_node) {
50605103 // we must now resolve all the use decls
50615104 for (size_t i = 0; i < namespace_import->use_decls.length; i += 1) {
......@@ -5066,7 +5109,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
50665109 }
50675110 resolve_use_decl(ira->codegen, use_decl_node);
50685111 }
5069 decl_node = find_decl(namespace_import->scope, field_name);
5112 decl_node = find_decl(&namespace_import->decls_scope->base, field_name);
50705113 }
50715114 if (decl_node) {
50725115 TopLevelDecl *tld = get_as_top_level_decl(decl_node);
......@@ -5327,15 +5370,15 @@ static TypeTableEntry *ir_analyze_instruction_set_debug_safety(IrAnalyze *ira,
53275370 if (target_type->id == TypeTableEntryIdBlock) {
53285371 target_context = target_val->data.x_block;
53295372 } else if (target_type->id == TypeTableEntryIdFn) {
5330 target_context = target_val->data.x_fn->fn_def_node->data.fn_def.scope;
5373 target_context = target_val->data.x_fn->fn_def_node->data.fn_def.child_scope;
53315374 } else if (target_type->id == TypeTableEntryIdMetaType) {
53325375 TypeTableEntry *type_arg = target_val->data.x_type;
53335376 if (type_arg->id == TypeTableEntryIdStruct) {
5334 target_context = type_arg->data.structure.scope;
5377 target_context = &type_arg->data.structure.decls_scope->base;
53355378 } else if (type_arg->id == TypeTableEntryIdEnum) {
5336 target_context = type_arg->data.enumeration.scope;
5379 target_context = &type_arg->data.enumeration.decls_scope->base;
53375380 } else if (type_arg->id == TypeTableEntryIdUnion) {
5338 target_context = type_arg->data.unionation.scope;
5381 target_context = &type_arg->data.unionation.decls_scope->base;
53395382 } else {
53405383 add_node_error(ira->codegen, target_instruction->source_node,
53415384 buf_sprintf("expected scope reference, found type '%s'", buf_ptr(&type_arg->name)));
......@@ -5966,7 +6009,7 @@ static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructi
59666009 ImportTableEntry *target_import = add_source_file(ira->codegen, target_package,
59676010 abs_full_path, search_dir, import_target_path, import_code);
59686011
5969 scan_decls(ira->codegen, target_import, target_import->scope, target_import->root);
6012 scan_decls(ira->codegen, target_import, target_import->decls_scope, target_import->root);
59706013
59716014 ConstExprValue *out_val = ir_build_const_from(ira, &import_instruction->base, depends_on_compile_var);
59726015 out_val->data.x_import = target_import;
......@@ -6021,7 +6064,7 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru
60216064
60226065 IrInstructionStructInitField *new_fields = allocate<IrInstructionStructInitField>(actual_field_count);
60236066
6024 FnTableEntry *fn_entry = instruction->source_node->scope->fn_entry;
6067 FnTableEntry *fn_entry = scope_fn_entry(instruction->source_node->scope);
60256068 bool outside_fn = (fn_entry == nullptr);
60266069
60276070 ConstExprValue const_val = {};
......@@ -6124,7 +6167,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
61246167 const_val.data.x_array.elements = allocate<ConstExprValue>(elem_count);
61256168 const_val.data.x_array.size = elem_count;
61266169
6127 FnTableEntry *fn_entry = instruction->base.source_node->scope->fn_entry;
6170 FnTableEntry *fn_entry = scope_fn_entry(instruction->base.source_node->scope);
61286171 bool outside_fn = (fn_entry == nullptr);
61296172
61306173 IrInstruction **new_items = allocate<IrInstruction *>(elem_count);
src/parseh.cpp+3-3
......@@ -817,7 +817,7 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl)
817817 const EnumDecl *enum_def = enum_decl->getDefinition();
818818 if (!enum_def) {
819819 TypeTableEntry *enum_type = get_partial_container_type(c->codegen, c->import,
820 c->import->scope,
820 &c->import->decls_scope->base,
821821 ContainerKindEnum, c->source_node, buf_ptr(full_type_name));
822822 c->enum_type_table.put(bare_name, enum_type);
823823 c->decl_table.put(enum_decl, enum_type);
......@@ -842,7 +842,7 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl)
842842
843843 if (pure_enum) {
844844 TypeTableEntry *enum_type = get_partial_container_type(c->codegen, c->import,
845 c->import->scope,
845 &c->import->decls_scope->base,
846846 ContainerKindEnum, c->source_node, buf_ptr(full_type_name));
847847 c->enum_type_table.put(bare_name, enum_type);
848848 c->decl_table.put(enum_decl, enum_type);
......@@ -1002,7 +1002,7 @@ static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_
10021002
10031003
10041004 TypeTableEntry *struct_type = get_partial_container_type(c->codegen, c->import,
1005 c->import->scope, ContainerKindStruct, c->source_node, buf_ptr(full_type_name));
1005 &c->import->decls_scope->base, ContainerKindStruct, c->source_node, buf_ptr(full_type_name));
10061006
10071007 c->struct_type_table.put(bare_name, struct_type);
10081008 c->decl_table.put(record_decl, struct_type);