authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-18 16:56:50-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-18 16:56:50-05:00
loge50ced44a2cf6268c19df901ad56b367d8d802fe
treeb4bd67b3a0a8bdbc853dba760c0d66a406e11d4a
parent2e6aa6d813cf3fd4180b8c9ffc671b4bcee54586

IR: all structs anonymous


11 files changed, 168 insertions(+), 197 deletions(-)

CMakeLists.txt+1-1
......@@ -38,7 +38,6 @@ include_directories(
3838)
3939
4040set(ZIG_SOURCES
41 "${CMAKE_SOURCE_DIR}/src/ir.cpp"
4241 "${CMAKE_SOURCE_DIR}/src/analyze.cpp"
4342 "${CMAKE_SOURCE_DIR}/src/ast_render.cpp"
4443 "${CMAKE_SOURCE_DIR}/src/bignum.cpp"
......@@ -48,6 +47,7 @@ set(ZIG_SOURCES
4847 "${CMAKE_SOURCE_DIR}/src/errmsg.cpp"
4948 "${CMAKE_SOURCE_DIR}/src/error.cpp"
5049 "${CMAKE_SOURCE_DIR}/src/eval.cpp"
50 "${CMAKE_SOURCE_DIR}/src/ir.cpp"
5151 "${CMAKE_SOURCE_DIR}/src/ir_print.cpp"
5252 "${CMAKE_SOURCE_DIR}/src/link.cpp"
5353 "${CMAKE_SOURCE_DIR}/src/main.cpp"
doc/langref.md+6-5
......@@ -7,7 +7,7 @@ Root = many(TopLevelItem) "EOF"
77
88TopLevelItem = ErrorValueDecl | Block | TopLevelDecl
99
10TopLevelDecl = option(VisibleMod) (FnDef | ExternDecl | ContainerDecl | GlobalVarDecl | TypeDecl | UseDecl)
10TopLevelDecl = option(VisibleMod) (FnDef | ExternDecl | GlobalVarDecl | TypeDecl | UseDecl)
1111
1212TypeDecl = "type" Symbol "=" TypeExpr ";"
1313
......@@ -17,9 +17,7 @@ GlobalVarDecl = VariableDeclaration ";"
1717
1818VariableDeclaration = option("inline") ("var" | "const") Symbol option(":" TypeExpr) "=" Expression
1919
20ContainerDecl = ("struct" | "enum" | "union") Symbol option(ParamDeclList) "{" many(StructMember) "}"
21
22StructMember = (StructField | FnDef | GlobalVarDecl | ContainerDecl)
20StructMember = (StructField | FnDef | GlobalVarDecl)
2321
2422StructField = Symbol option(":" Expression) ",")
2523
......@@ -143,7 +141,7 @@ StructLiteralField = "." Symbol "=" Expression
143141
144142PrefixOp = "!" | "-" | "~" | "*" | ("&" option("const")) | "?" | "%" | "%%" | "??" | "-%"
145143
146PrimaryExpression = Number | String | CharLiteral | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression | Symbol | ("@" Symbol FnCallExpression) | ArrayType | (option("extern") FnProto) | AsmExpression | ("error" "." Symbol)
144PrimaryExpression = Number | String | CharLiteral | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression | Symbol | ("@" Symbol FnCallExpression) | ArrayType | (option("extern") FnProto) | AsmExpression | ("error" "." Symbol) | ContainerDecl
147145
148146ArrayType = "[" option(Expression) "]" option("const") TypeExpr
149147
......@@ -152,6 +150,9 @@ GotoExpression = option("inline") "goto" Symbol
152150GroupedExpression = "(" Expression ")"
153151
154152KeywordLiteral = "true" | "false" | "null" | "break" | "continue" | "undefined" | "zeroes" | "error" | "type" | "this"
153
154ContainerDecl = ("struct" | "enum" | "union") "{" many(StructMember) "}"
155
155156```
156157
157158## Operator Precedence
src/all_types.hpp+3-7
......@@ -44,6 +44,7 @@ struct IrGotoItem {
4444
4545struct IrExecutable {
4646 ZigList<IrBasicBlock *> basic_block_list;
47 Buf *name;
4748 size_t mem_slot_count;
4849 size_t next_debug_id;
4950 size_t *backward_branch_count;
......@@ -195,7 +196,6 @@ struct Tld {
195196 // set this flag temporarily to detect infinite loops
196197 bool dep_loop_flag;
197198 TldResolution resolution;
198 Tld *parent_tld;
199199};
200200
201201struct TldVar {
......@@ -594,12 +594,8 @@ enum ContainerKind {
594594 ContainerKindUnion,
595595};
596596
597struct AstNodeStructDecl {
598 VisibMod visib_mod;
599 Buf *name;
597struct AstNodeContainerDecl {
600598 ContainerKind kind;
601 ZigList<AstNode *> generic_params;
602 bool generic_params_is_var_args; // always an error but it can happen from parsing
603599 ZigList<AstNode *> fields;
604600 ZigList<AstNode *> decls;
605601};
......@@ -722,7 +718,7 @@ struct AstNode {
722718 AstNodeGoto goto_expr;
723719 AstNodeAsmExpr asm_expr;
724720 AstNodeFieldAccessExpr field_access_expr;
725 AstNodeStructDecl struct_decl;
721 AstNodeContainerDecl container_decl;
726722 AstNodeStructField struct_field;
727723 AstNodeStringLiteral string_literal;
728724 AstNodeCharLiteral char_literal;
src/analyze.cpp+61-86
......@@ -831,10 +831,7 @@ static TypeTableEntryId container_to_type(ContainerKind kind) {
831831 zig_unreachable();
832832}
833833
834TypeTableEntry *get_partial_container_type(CodeGen *g, ImportTableEntry *import, Scope *scope,
835 ContainerKind kind, AstNode *decl_node, const char *name)
836{
837
834TypeTableEntry *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind, AstNode *decl_node, const char *name) {
838835 TypeTableEntryId type_id = container_to_type(kind);
839836 TypeTableEntry *entry = new_container_type_entry(type_id, decl_node, scope);
840837
......@@ -852,6 +849,7 @@ TypeTableEntry *get_partial_container_type(CodeGen *g, ImportTableEntry *import,
852849
853850 unsigned line = decl_node->line;
854851
852 ImportTableEntry *import = get_scope_import(scope);
855853 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), name);
856854 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
857855 ZigLLVMTag_DW_structure_type(), name,
......@@ -862,15 +860,6 @@ TypeTableEntry *get_partial_container_type(CodeGen *g, ImportTableEntry *import,
862860 return entry;
863861}
864862
865static TypeTableEntry *get_partial_container_type_tld(CodeGen *g, Scope *scope, TldContainer *tld_container) {
866 ImportTableEntry *import = tld_container->base.import;
867 AstNode *container_node = tld_container->base.source_node;
868 assert(container_node->type == NodeTypeContainerDecl);
869 ContainerKind kind = container_node->data.struct_decl.kind;
870 return get_partial_container_type(g, import, scope, kind, container_node, buf_ptr(tld_container->base.name));
871}
872
873
874863TypeTableEntry *get_underlying_type(TypeTableEntry *type_entry) {
875864 if (type_entry->id == TypeTableEntryIdTypeDecl) {
876865 return type_entry->data.type_decl.canonical_type;
......@@ -879,15 +868,15 @@ TypeTableEntry *get_underlying_type(TypeTableEntry *type_entry) {
879868 }
880869}
881870
882static IrInstruction *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, TypeTableEntry *type_entry) {
871static IrInstruction *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, TypeTableEntry *type_entry, Buf *type_name) {
883872 size_t backward_branch_count = 0;
884873 return ir_eval_const_value(g, scope, node, type_entry,
885874 &backward_branch_count, default_backward_branch_quota,
886 nullptr, nullptr, node);
875 nullptr, nullptr, node, type_name);
887876}
888877
889878TypeTableEntry *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
890 IrInstruction *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type);
879 IrInstruction *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type, nullptr);
891880 if (result->type_entry->id == TypeTableEntryIdInvalid)
892881 return g->builtin_types.entry_invalid;
893882
......@@ -1069,7 +1058,7 @@ static void resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type) {
10691058 assert(decl_node->type == NodeTypeContainerDecl);
10701059 assert(enum_type->di_type);
10711060
1072 uint32_t field_count = decl_node->data.struct_decl.fields.length;
1061 uint32_t field_count = decl_node->data.container_decl.fields.length;
10731062
10741063 enum_type->data.enumeration.src_field_count = field_count;
10751064 enum_type->data.enumeration.fields = allocate<TypeEnumField>(field_count);
......@@ -1091,7 +1080,7 @@ static void resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type) {
10911080
10921081 size_t gen_field_index = 0;
10931082 for (uint32_t i = 0; i < field_count; i += 1) {
1094 AstNode *field_node = decl_node->data.struct_decl.fields.at(i);
1083 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
10951084 TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[i];
10961085 type_enum_field->name = field_node->data.struct_field.name;
10971086 TypeTableEntry *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
......@@ -1201,7 +1190,7 @@ static void resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type) {
12011190 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, enum_type->type_ref);
12021191 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
12031192 ZigLLVMFileToScope(import->di_file),
1204 buf_ptr(decl_node->data.struct_decl.name),
1193 buf_ptr(&enum_type->name),
12051194 import->di_file, decl_node->line + 1,
12061195 debug_size_in_bits,
12071196 debug_align_in_bits,
......@@ -1217,7 +1206,7 @@ static void resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type) {
12171206 uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, tag_type_entry->type_ref);
12181207 uint64_t tag_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, tag_type_entry->type_ref);
12191208 ZigLLVMDIType *tag_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
1220 ZigLLVMFileToScope(import->di_file), buf_ptr(decl_node->data.struct_decl.name),
1209 ZigLLVMFileToScope(import->di_file), buf_ptr(&enum_type->name),
12211210 import->di_file, decl_node->line + 1,
12221211 tag_debug_size_in_bits,
12231212 tag_debug_align_in_bits,
......@@ -1257,7 +1246,7 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) {
12571246 assert(decl_node->type == NodeTypeContainerDecl);
12581247 assert(struct_type->di_type);
12591248
1260 size_t field_count = decl_node->data.struct_decl.fields.length;
1249 size_t field_count = decl_node->data.container_decl.fields.length;
12611250
12621251 struct_type->data.structure.src_field_count = field_count;
12631252 struct_type->data.structure.fields = allocate<TypeStructField>(field_count);
......@@ -1274,7 +1263,7 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) {
12741263
12751264 size_t gen_field_index = 0;
12761265 for (size_t i = 0; i < field_count; i += 1) {
1277 AstNode *field_node = decl_node->data.struct_decl.fields.at(i);
1266 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
12781267 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
12791268 type_struct_field->name = field_node->data.struct_field.name;
12801269 TypeTableEntry *field_type = analyze_type_expr(g, scope,
......@@ -1316,7 +1305,7 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) {
13161305 ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(gen_field_count);
13171306
13181307 for (size_t i = 0; i < field_count; i += 1) {
1319 AstNode *field_node = decl_node->data.struct_decl.fields.at(i);
1308 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
13201309 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
13211310 gen_field_index = type_struct_field->gen_index;
13221311 if (gen_field_index == SIZE_MAX) {
......@@ -1348,7 +1337,7 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) {
13481337 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, struct_type->type_ref);
13491338 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
13501339 ZigLLVMFileToScope(import->di_file),
1351 buf_ptr(decl_node->data.struct_decl.name),
1340 buf_ptr(&struct_type->name),
13521341 import->di_file, decl_node->line + 1,
13531342 debug_size_in_bits,
13541343 debug_align_in_bits,
......@@ -1364,14 +1353,28 @@ static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
13641353 zig_panic("TODO");
13651354}
13661355
1367static void get_fully_qualified_decl_name(Buf *buf, Tld *tld, uint8_t sep) {
1368 if (tld->parent_tld) {
1369 get_fully_qualified_decl_name(buf, tld->parent_tld, sep);
1370 buf_append_char(buf, sep);
1371 buf_append_buf(buf, tld->name);
1372 } else {
1373 buf_init_from_buf(buf, tld->name);
1356static void get_fully_qualified_decl_name_internal(Buf *buf, Scope *scope, uint8_t sep) {
1357 if (!scope)
1358 return;
1359
1360 if (scope->id == ScopeIdDecls) {
1361 get_fully_qualified_decl_name_internal(buf, scope->parent, sep);
1362
1363 ScopeDecls *scope_decls = (ScopeDecls *)scope;
1364 if (scope_decls->container_type) {
1365 buf_append_buf(buf, &scope_decls->container_type->name);
1366 buf_append_char(buf, sep);
1367 }
1368 return;
13741369 }
1370
1371 get_fully_qualified_decl_name_internal(buf, scope->parent, sep);
1372}
1373
1374static void get_fully_qualified_decl_name(Buf *buf, Tld *tld, uint8_t sep) {
1375 buf_resize(buf, 0);
1376 get_fully_qualified_decl_name_internal(buf, tld->parent_scope, sep);
1377 buf_append_buf(buf, tld->name);
13751378}
13761379
13771380FnTableEntry *create_fn_raw(FnInline inline_value, bool internal_linkage) {
......@@ -1401,6 +1404,17 @@ FnTableEntry *create_fn(AstNode *proto_node) {
14011404 return fn_entry;
14021405}
14031406
1407static bool scope_is_root_decls(Scope *scope) {
1408 while (scope) {
1409 if (scope->id == ScopeIdDecls) {
1410 ScopeDecls *scope_decls = (ScopeDecls *)scope;
1411 return (scope_decls->container_type == nullptr);
1412 }
1413 scope = scope->parent;
1414 }
1415 zig_unreachable();
1416}
1417
14041418static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
14051419 ImportTableEntry *import = tld_fn->base.import;
14061420 AstNode *proto_node = tld_fn->base.source_node;
......@@ -1448,9 +1462,8 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
14481462 if (fn_def_node)
14491463 g->fn_defs.append(fn_table_entry);
14501464
1451 Tld *parent_tld = tld_fn->base.parent_tld;
1452 bool is_main_fn = !parent_tld && (import == g->root_import) &&
1453 buf_eql_str(&fn_table_entry->symbol_name, "main");
1465 bool is_main_fn = scope_is_root_decls(tld_fn->base.parent_scope) &&
1466 (import == g->root_import) && buf_eql_str(&fn_table_entry->symbol_name, "main");
14541467 if (is_main_fn)
14551468 g->main_fn = fn_table_entry;
14561469
......@@ -1480,23 +1493,6 @@ static void add_top_level_decl(CodeGen *g, ScopeDecls *decls_scope, Tld *tld) {
14801493 }
14811494}
14821495
1483static void scan_struct_decl(CodeGen *g, ScopeDecls *decls_scope, TldContainer *tld_container) {
1484 assert(!tld_container->type_entry);
1485
1486 AstNode *container_node = tld_container->base.source_node;
1487 assert(container_node->type == NodeTypeContainerDecl);
1488
1489 TypeTableEntry *container_type = get_partial_container_type_tld(g, &decls_scope->base, tld_container);
1490 tld_container->type_entry = container_type;
1491
1492 // handle the member function definitions independently
1493 for (size_t i = 0; i < container_node->data.struct_decl.decls.length; i += 1) {
1494 AstNode *child_node = container_node->data.struct_decl.decls.at(i);
1495 ScopeDecls *child_scope = get_container_scope(container_type);
1496 scan_decls(g, tld_container->base.import, child_scope, child_node, &tld_container->base);
1497 }
1498}
1499
15001496static void preview_error_value_decl(CodeGen *g, AstNode *node) {
15011497 assert(node->type == NodeTypeErrorValueDecl);
15021498
......@@ -1525,7 +1521,7 @@ static void preview_error_value_decl(CodeGen *g, AstNode *node) {
15251521}
15261522
15271523void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source_node,
1528 Scope *parent_scope, Tld *parent_tld)
1524 Scope *parent_scope)
15291525{
15301526 tld->id = id;
15311527 tld->name = name;
......@@ -1533,40 +1529,25 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source
15331529 tld->source_node = source_node;
15341530 tld->import = source_node->owner;
15351531 tld->parent_scope = parent_scope;
1536 tld->parent_tld = parent_tld;
15371532}
15381533
1539void scan_decls(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope, AstNode *node, Tld *parent_tld) {
1534void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
15401535 switch (node->type) {
15411536 case NodeTypeRoot:
1542 for (size_t i = 0; i < import->root->data.root.top_level_decls.length; i += 1) {
1543 AstNode *child = import->root->data.root.top_level_decls.at(i);
1544 scan_decls(g, import, decls_scope, child, parent_tld);
1545 }
1546 break;
1547 case NodeTypeContainerDecl:
1548 {
1549 Buf *name = node->data.struct_decl.name;
1550 VisibMod visib_mod = node->data.struct_decl.visib_mod;
1551 TldContainer *tld_container = allocate<TldContainer>(1);
1552 init_tld(&tld_container->base, TldIdContainer, name, visib_mod, node, &decls_scope->base, parent_tld);
1553 add_top_level_decl(g, decls_scope, &tld_container->base);
1554 if (node->data.struct_decl.generic_params.length == 0) {
1555 scan_struct_decl(g, decls_scope, tld_container);
1556 } else {
1557 zig_panic("TODO all structs anonymous?");
1558 }
1537 for (size_t i = 0; i < node->data.root.top_level_decls.length; i += 1) {
1538 AstNode *child = node->data.root.top_level_decls.at(i);
1539 scan_decls(g, decls_scope, child);
15591540 }
15601541 break;
15611542 case NodeTypeFnDef:
1562 scan_decls(g, import, decls_scope, node->data.fn_def.fn_proto, parent_tld);
1543 scan_decls(g, decls_scope, node->data.fn_def.fn_proto);
15631544 break;
15641545 case NodeTypeVariableDeclaration:
15651546 {
15661547 Buf *name = node->data.variable_declaration.symbol;
15671548 VisibMod visib_mod = node->data.variable_declaration.visib_mod;
15681549 TldVar *tld_var = allocate<TldVar>(1);
1569 init_tld(&tld_var->base, TldIdVar, name, visib_mod, node, &decls_scope->base, parent_tld);
1550 init_tld(&tld_var->base, TldIdVar, name, visib_mod, node, &decls_scope->base);
15701551 add_top_level_decl(g, decls_scope, &tld_var->base);
15711552 break;
15721553 }
......@@ -1575,7 +1556,7 @@ void scan_decls(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope, A
15751556 Buf *name = node->data.type_decl.symbol;
15761557 VisibMod visib_mod = node->data.type_decl.visib_mod;
15771558 TldTypeDef *tld_typedef = allocate<TldTypeDef>(1);
1578 init_tld(&tld_typedef->base, TldIdTypeDef, name, visib_mod, node, &decls_scope->base, parent_tld);
1559 init_tld(&tld_typedef->base, TldIdTypeDef, name, visib_mod, node, &decls_scope->base);
15791560 add_top_level_decl(g, decls_scope, &tld_typedef->base);
15801561 break;
15811562 }
......@@ -1590,13 +1571,14 @@ void scan_decls(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope, A
15901571
15911572 VisibMod visib_mod = node->data.fn_proto.visib_mod;
15921573 TldFn *tld_fn = allocate<TldFn>(1);
1593 init_tld(&tld_fn->base, TldIdFn, fn_name, visib_mod, node, &decls_scope->base, parent_tld);
1574 init_tld(&tld_fn->base, TldIdFn, fn_name, visib_mod, node, &decls_scope->base);
15941575 add_top_level_decl(g, decls_scope, &tld_fn->base);
15951576 break;
15961577 }
15971578 case NodeTypeUse:
15981579 {
15991580 g->use_queue.append(node);
1581 ImportTableEntry *import = get_scope_import(&decls_scope->base);
16001582 import->use_decls.append(node);
16011583 break;
16021584 }
......@@ -1604,6 +1586,7 @@ void scan_decls(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope, A
16041586 // error value declarations do not depend on other top level decls
16051587 preview_error_value_decl(g, node);
16061588 break;
1589 case NodeTypeContainerDecl:
16071590 case NodeTypeParamDecl:
16081591 case NodeTypeFnDecl:
16091592 case NodeTypeReturnExpr:
......@@ -1787,7 +1770,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
17871770 if (explicit_type && explicit_type->id == TypeTableEntryIdInvalid) {
17881771 implicit_type = explicit_type;
17891772 } else if (var_decl->expr) {
1790 init_value = analyze_const_value(g, tld_var->base.parent_scope, var_decl->expr, explicit_type);
1773 init_value = analyze_const_value(g, tld_var->base.parent_scope, var_decl->expr, explicit_type, var_decl->symbol);
17911774 assert(init_value);
17921775 implicit_type = init_value->type_entry;
17931776
......@@ -1837,13 +1820,6 @@ static void resolve_decl_typedef(CodeGen *g, TldTypeDef *tld_typedef) {
18371820void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only) {
18381821 if (tld->resolution != TldResolutionUnresolved)
18391822 return;
1840 if (pointer_only && tld->id == TldIdContainer) {
1841 g->resolve_queue.append(tld);
1842 return;
1843 }
1844
1845 ImportTableEntry *import = tld->import;
1846 assert(import);
18471823
18481824 if (tld->dep_loop_flag) {
18491825 add_node_error(g, tld->source_node, buf_sprintf("'%s' depends on itself", buf_ptr(tld->name)));
......@@ -2222,7 +2198,6 @@ static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {
22222198 FnTypeParamInfo *param_info = &fn_type_id->param_info[i];
22232199 AstNode *param_decl_node = get_param_decl_node(fn_table_entry, i);
22242200 AstNodeParamDecl *param_decl = &param_decl_node->data.param_decl;
2225
22262201
22272202 TypeTableEntry *param_type = param_info->type;
22282203 bool is_noalias = param_info->is_noalias;
......@@ -2350,7 +2325,7 @@ void preview_use_decl(CodeGen *g, AstNode *node) {
23502325 assert(node->type == NodeTypeUse);
23512326
23522327 IrInstruction *result = analyze_const_value(g, &node->owner->decls_scope->base,
2353 node->data.use.expr, g->builtin_types.entry_namespace);
2328 node->data.use.expr, g->builtin_types.entry_namespace, nullptr);
23542329
23552330 if (result->type_entry->id == TypeTableEntryIdInvalid)
23562331 node->owner->any_imports_failed = true;
......@@ -2438,7 +2413,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package,
24382413void semantic_analyze(CodeGen *g) {
24392414 for (; g->import_queue_index < g->import_queue.length; g->import_queue_index += 1) {
24402415 ImportTableEntry *import = g->import_queue.at(g->import_queue_index);
2441 scan_decls(g, import, import->decls_scope, import->root, nullptr);
2416 scan_decls(g, import->decls_scope, import->root);
24422417 }
24432418
24442419 for (; g->use_queue_index < g->use_queue.length; g->use_queue_index += 1) {
src/analyze.hpp+3-5
......@@ -26,8 +26,7 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id);
2626TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type);
2727TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t array_size);
2828TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_const);
29TypeTableEntry *get_partial_container_type(CodeGen *g, ImportTableEntry *import, Scope *scope,
30 ContainerKind kind, AstNode *decl_node, const char *name);
29TypeTableEntry *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind, AstNode *decl_node, const char *name);
3130TypeTableEntry *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x);
3231TypeTableEntry *get_error_type(CodeGen *g, TypeTableEntry *child_type);
3332TypeTableEntry *get_bound_fn_type(CodeGen *g, FnTableEntry *fn_entry);
......@@ -60,13 +59,12 @@ TypeStructField *find_struct_type_field(TypeTableEntry *type_entry, Buf *name);
6059ScopeDecls *get_container_scope(TypeTableEntry *type_entry);
6160TypeEnumField *find_enum_type_field(TypeTableEntry *enum_type, Buf *name);
6261bool is_container_ref(TypeTableEntry *type_entry);
63void scan_decls(CodeGen *g, ImportTableEntry *import, ScopeDecls *decls_scope, AstNode *node, Tld *parent_tld);
62void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node);
6463void preview_use_decl(CodeGen *g, AstNode *node);
6564void resolve_use_decl(CodeGen *g, AstNode *node);
6665FnTableEntry *scope_fn_entry(Scope *scope);
6766ImportTableEntry *get_scope_import(Scope *scope);
68void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source_node,
69 Scope *parent_scope, Tld *parent_tld);
67void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source_node, Scope *parent_scope);
7068VariableTableEntry *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name,
7169 TypeTableEntry *type_entry, bool is_const, ConstExprValue *init_value);
7270TypeTableEntry *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node);
src/ast_render.cpp+4-6
......@@ -585,13 +585,11 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
585585 break;
586586 case NodeTypeContainerDecl:
587587 {
588 const char *struct_name = buf_ptr(node->data.struct_decl.name);
589 const char *pub_str = visib_mod_string(node->data.struct_decl.visib_mod);
590 const char *container_str = container_string(node->data.struct_decl.kind);
591 fprintf(ar->f, "%s%s %s {\n", pub_str, container_str, struct_name);
588 const char *container_str = container_string(node->data.container_decl.kind);
589 fprintf(ar->f, "%s {\n", container_str);
592590 ar->indent += ar->indent_size;
593 for (size_t field_i = 0; field_i < node->data.struct_decl.fields.length; field_i += 1) {
594 AstNode *field_node = node->data.struct_decl.fields.at(field_i);
591 for (size_t field_i = 0; field_i < node->data.container_decl.fields.length; field_i += 1) {
592 AstNode *field_node = node->data.container_decl.fields.at(field_i);
595593 assert(field_node->type == NodeTypeStructField);
596594 print_indent(ar);
597595 print_symbol(ar, field_node->data.struct_field.name);
src/ir.cpp+59-31
......@@ -2306,6 +2306,8 @@ static IrInstruction *ir_gen_decl_ref(IrBuilder *irb, AstNode *source_node, Tld
23062306 return irb->codegen->invalid_instruction;
23072307
23082308 switch (tld->id) {
2309 case TldIdContainer:
2310 zig_unreachable();
23092311 case TldIdVar:
23102312 {
23112313 TldVar *tld_var = (TldVar *)tld;
......@@ -2327,16 +2329,6 @@ static IrInstruction *ir_gen_decl_ref(IrBuilder *irb, AstNode *source_node, Tld
23272329 else
23282330 return ref_instruction;
23292331 }
2330 case TldIdContainer:
2331 {
2332 TldContainer *tld_container = (TldContainer *)tld;
2333
2334 IrInstruction *ref_instruction = ir_build_const_type(irb, scope, source_node, tld_container->type_entry);
2335 if (lval != LValPurposeNone)
2336 return ir_build_ref(irb, scope, source_node, ref_instruction);
2337 else
2338 return ref_instruction;
2339 }
23402332 case TldIdTypeDef:
23412333 {
23422334 TldTypeDef *tld_typedef = (TldTypeDef *)tld;
......@@ -3861,6 +3853,52 @@ static IrInstruction *ir_gen_err_ok_or(IrBuilder *irb, Scope *parent_scope, AstN
38613853 return ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values);
38623854}
38633855
3856static const char *container_string(ContainerKind kind) {
3857 switch (kind) {
3858 case ContainerKindEnum: return "enum";
3859 case ContainerKindStruct: return "struct";
3860 case ContainerKindUnion: return "union";
3861 }
3862 zig_unreachable();
3863}
3864
3865static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
3866 assert(node->type == NodeTypeContainerDecl);
3867
3868 ContainerKind kind = node->data.container_decl.kind;
3869 Buf *name;
3870 if (irb->exec->name) {
3871 name = irb->exec->name;
3872 } else {
3873 FnTableEntry *fn_entry = exec_fn_entry(irb->exec);
3874 if (fn_entry) {
3875 zig_panic("TODO name the container inside the function");
3876 } else {
3877 name = buf_sprintf("(anonymous %s at %s:%zu:%zu)", container_string(kind),
3878 buf_ptr(node->owner->path), node->line + 1, node->column + 1);
3879 }
3880 }
3881
3882
3883 VisibMod visib_mod = VisibModPub;
3884 TldContainer *tld_container = allocate<TldContainer>(1);
3885 init_tld(&tld_container->base, TldIdContainer, name, visib_mod, node, parent_scope);
3886
3887 TypeTableEntry *container_type = get_partial_container_type(irb->codegen, parent_scope, kind, node, buf_ptr(name));
3888 ScopeDecls *child_scope = get_container_scope(container_type);
3889
3890 tld_container->type_entry = container_type;
3891 tld_container->decls_scope = child_scope;
3892
3893 for (size_t i = 0; i < node->data.container_decl.decls.length; i += 1) {
3894 AstNode *child_node = node->data.container_decl.decls.at(i);
3895 scan_decls(irb->codegen, child_scope, child_node);
3896 }
3897 irb->codegen->resolve_queue.append(&tld_container->base);
3898
3899 return ir_build_const_type(irb, parent_scope, node, container_type);
3900}
3901
38643902static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scope,
38653903 LValPurpose lval)
38663904{
......@@ -3872,6 +3910,7 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
38723910 case NodeTypeUse:
38733911 case NodeTypeSwitchProng:
38743912 case NodeTypeSwitchRange:
3913 case NodeTypeStructField:
38753914 zig_unreachable();
38763915 case NodeTypeBlock:
38773916 return ir_lval_wrap(irb, scope, ir_gen_block(irb, scope, node), lval);
......@@ -3941,11 +3980,11 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
39413980 return ir_lval_wrap(irb, scope, ir_gen_slice(irb, scope, node), lval);
39423981 case NodeTypeUnwrapErrorExpr:
39433982 return ir_lval_wrap(irb, scope, ir_gen_err_ok_or(irb, scope, node), lval);
3983 case NodeTypeContainerDecl:
3984 return ir_lval_wrap(irb, scope, ir_gen_container_decl(irb, scope, node), lval);
39443985 case NodeTypeFnProto:
39453986 case NodeTypeFnDef:
39463987 case NodeTypeFnDecl:
3947 case NodeTypeContainerDecl:
3948 case NodeTypeStructField:
39493988 case NodeTypeErrorValueDecl:
39503989 case NodeTypeTypeDecl:
39513990 zig_panic("TODO more IR gen for node types");
......@@ -4558,9 +4597,10 @@ static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, Un
45584597
45594598IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
45604599 TypeTableEntry *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,
4561 FnTableEntry *fn_entry, Buf *c_import_buf, AstNode *source_node)
4600 FnTableEntry *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name)
45624601{
45634602 IrExecutable ir_executable = {0};
4603 ir_executable.name = exec_name;
45644604 ir_executable.is_inline = true;
45654605 ir_executable.fn_entry = fn_entry;
45664606 ir_executable.c_import_buf = c_import_buf;
......@@ -4577,6 +4617,7 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node
45774617 fprintf(stderr, "}\n");
45784618 }
45794619 IrExecutable analyzed_executable = {0};
4620 analyzed_executable.name = exec_name;
45804621 analyzed_executable.is_inline = true;
45814622 analyzed_executable.fn_entry = fn_entry;
45824623 analyzed_executable.c_import_buf = c_import_buf;
......@@ -6017,7 +6058,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
60176058 AstNode *body_node = fn_entry->fn_def_node->data.fn_def.body;
60186059 IrInstruction *result = ir_eval_const_value(ira->codegen, exec_scope, body_node, return_type,
60196060 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, fn_entry,
6020 nullptr, call_instruction->base.source_node);
6061 nullptr, call_instruction->base.source_node, nullptr);
60216062 if (result->type_entry->id == TypeTableEntryIdInvalid)
60226063 return ira->codegen->builtin_types.entry_invalid;
60236064
......@@ -6772,6 +6813,8 @@ static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source
67726813 return ira->codegen->builtin_types.entry_invalid;
67736814
67746815 switch (tld->id) {
6816 case TldIdContainer:
6817 zig_unreachable();
67756818 case TldIdVar:
67766819 {
67776820 TldVar *tld_var = (TldVar *)tld;
......@@ -6794,21 +6837,6 @@ static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source
67946837 return ir_analyze_const_ptr(ira, source_instruction, const_val, fn_entry->type_entry,
67956838 depends_on_compile_var, ConstPtrSpecialNone, ptr_is_const);
67966839 }
6797 case TldIdContainer:
6798 {
6799 TldContainer *tld_container = (TldContainer *)tld;
6800 assert(tld_container->type_entry);
6801
6802 // TODO instead of allocating this every time, put it in the tld value and we can reference
6803 // the same one every time
6804 ConstExprValue *const_val = allocate<ConstExprValue>(1);
6805 const_val->special = ConstValSpecialStatic;
6806 const_val->data.x_type = tld_container->type_entry;
6807
6808 bool ptr_is_const = true;
6809 return ir_analyze_const_ptr(ira, source_instruction, const_val, ira->codegen->builtin_types.entry_type,
6810 depends_on_compile_var, ConstPtrSpecialNone, ptr_is_const);
6811 }
68126840 case TldIdTypeDef:
68136841 {
68146842 TldTypeDef *tld_typedef = (TldTypeDef *)tld;
......@@ -7928,7 +7956,7 @@ static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructi
79287956 ImportTableEntry *target_import = add_source_file(ira->codegen, target_package,
79297957 abs_full_path, search_dir, import_target_path, import_code);
79307958
7931 scan_decls(ira->codegen, target_import, target_import->decls_scope, target_import->root, nullptr);
7959 scan_decls(ira->codegen, target_import->decls_scope, target_import->root);
79327960
79337961 ConstExprValue *out_val = ir_build_const_from(ira, &import_instruction->base, depends_on_compile_var);
79347962 out_val->data.x_import = target_import;
......@@ -8303,7 +8331,7 @@ static TypeTableEntry *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruc
83038331 TypeTableEntry *void_type = ira->codegen->builtin_types.entry_void;
83048332 IrInstruction *result = ir_eval_const_value(ira->codegen, &cimport_scope->base, block_node, void_type,
83058333 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr,
8306 &cimport_scope->buf, block_node);
8334 &cimport_scope->buf, block_node, nullptr);
83078335 if (result->type_entry->id == TypeTableEntryIdInvalid)
83088336 return ira->codegen->builtin_types.entry_invalid;
83098337
src/ir.hpp+1-1
......@@ -15,7 +15,7 @@ IrInstruction *ir_gen_fn(CodeGen *g, FnTableEntry *fn_entry);
1515
1616IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
1717 TypeTableEntry *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,
18 FnTableEntry *fn_entry, Buf *c_import_buf, AstNode *source_node);
18 FnTableEntry *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name);
1919
2020TypeTableEntry *ir_analyze(CodeGen *g, IrExecutable *old_executable, IrExecutable *new_executable,
2121 TypeTableEntry *expected_type, AstNode *expected_type_source_node);
src/parseh.cpp+8-14
......@@ -120,7 +120,7 @@ static const char *decl_name(const Decl *decl) {
120120}
121121
122122static void parseh_init_tld(Context *c, Tld *tld, TldId id, Buf *name) {
123 init_tld(tld, id, name, c->visib_mod, c->source_node, &c->import->decls_scope->base, nullptr);
123 init_tld(tld, id, name, c->visib_mod, c->source_node, &c->import->decls_scope->base);
124124 tld->resolution = TldResolutionOk;
125125}
126126
......@@ -184,7 +184,7 @@ static Tld *create_global_num_lit_ap(Context *c, const Decl *source_decl, Buf *n
184184}
185185
186186
187static void add_const_type(Context *c, Buf *name, TypeTableEntry *type_entry) {
187static Tld *add_const_type(Context *c, Buf *name, TypeTableEntry *type_entry) {
188188 ConstExprValue *var_value = allocate<ConstExprValue>(1);
189189 var_value->special = ConstValSpecialStatic;
190190 var_value->data.x_type = type_entry;
......@@ -192,15 +192,11 @@ static void add_const_type(Context *c, Buf *name, TypeTableEntry *type_entry) {
192192 add_global(c, &tld_var->base);
193193
194194 c->global_type_table.put(name, type_entry);
195 return &tld_var->base;
195196}
196197
197198static Tld *add_container_tld(Context *c, TypeTableEntry *type_entry) {
198 TldContainer *tld_container = allocate<TldContainer>(1);
199 parseh_init_tld(c, &tld_container->base, TldIdContainer, &type_entry->name);
200 tld_container->type_entry = type_entry;
201
202 add_global(c, &tld_container->base);
203 return &tld_container->base;
199 return add_const_type(c, &type_entry->name, type_entry);
204200}
205201
206202static Tld *add_typedef_tld(Context *c, TypeTableEntry *type_decl) {
......@@ -690,8 +686,7 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl)
690686
691687 const EnumDecl *enum_def = enum_decl->getDefinition();
692688 if (!enum_def) {
693 TypeTableEntry *enum_type = get_partial_container_type(c->codegen, c->import,
694 &c->import->decls_scope->base,
689 TypeTableEntry *enum_type = get_partial_container_type(c->codegen, &c->import->decls_scope->base,
695690 ContainerKindEnum, c->source_node, buf_ptr(full_type_name));
696691 c->enum_type_table.put(bare_name, enum_type);
697692 c->decl_table.put(enum_decl, enum_type);
......@@ -715,8 +710,7 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl)
715710 TypeTableEntry *tag_type_entry = resolve_qual_type(c, enum_decl->getIntegerType(), enum_decl);
716711
717712 if (pure_enum) {
718 TypeTableEntry *enum_type = get_partial_container_type(c->codegen, c->import,
719 &c->import->decls_scope->base,
713 TypeTableEntry *enum_type = get_partial_container_type(c->codegen, &c->import->decls_scope->base,
720714 ContainerKindEnum, c->source_node, buf_ptr(full_type_name));
721715 c->enum_type_table.put(bare_name, enum_type);
722716 c->decl_table.put(enum_decl, enum_type);
......@@ -855,8 +849,8 @@ static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_
855849 Buf *full_type_name = buf_sprintf("struct_%s", buf_ptr(bare_name));
856850
857851
858 TypeTableEntry *struct_type = get_partial_container_type(c->codegen, c->import,
859 &c->import->decls_scope->base, ContainerKindStruct, c->source_node, buf_ptr(full_type_name));
852 TypeTableEntry *struct_type = get_partial_container_type(c->codegen, &c->import->decls_scope->base,
853 ContainerKindStruct, c->source_node, buf_ptr(full_type_name));
860854
861855 c->struct_type_table.put(bare_name, struct_type);
862856 c->decl_table.put(record_decl, struct_type);
src/parser.cpp+20-39
......@@ -216,6 +216,7 @@ static AstNode *ast_parse_prefix_op_expr(ParseContext *pc, size_t *token_index,
216216static AstNode *ast_parse_fn_proto(ParseContext *pc, size_t *token_index, bool mandatory, VisibMod visib_mod);
217217static AstNode *ast_parse_return_expr(ParseContext *pc, size_t *token_index);
218218static AstNode *ast_parse_grouped_expr(ParseContext *pc, size_t *token_index, bool mandatory);
219static AstNode *ast_parse_container_decl(ParseContext *pc, size_t *token_index, bool mandatory);
219220
220221static void ast_expect_token(ParseContext *pc, Token *token, TokenId token_id) {
221222 if (token->id == token_id) {
......@@ -618,7 +619,7 @@ static AstNode *ast_parse_goto_expr(ParseContext *pc, size_t *token_index, bool
618619 return node;
619620}
620621/*
621PrimaryExpression = "Number" | "String" | "CharLiteral" | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression | "Symbol" | ("@" "Symbol" FnCallExpression) | ArrayType | FnProto | AsmExpression | ("error" "." "Symbol")
622PrimaryExpression = Number | String | CharLiteral | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression | Symbol | ("@" Symbol FnCallExpression) | ArrayType | (option("extern") FnProto) | AsmExpression | ("error" "." Symbol) | ContainerDecl
622623KeywordLiteral = "true" | "false" | "null" | "break" | "continue" | "undefined" | "zeroes" | "error" | "type" | "this"
623624*/
624625static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
......@@ -737,6 +738,10 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bo
737738 return asm_expr;
738739 }
739740
741 AstNode *container_decl = ast_parse_container_decl(pc, token_index, false);
742 if (container_decl)
743 return container_decl;
744
740745 if (!mandatory)
741746 return nullptr;
742747
......@@ -2219,43 +2224,31 @@ static AstNode *ast_parse_use(ParseContext *pc, size_t *token_index, VisibMod vi
22192224}
22202225
22212226/*
2222ContainerDecl = ("struct" | "enum" | "union") Symbol option(ParamDeclList) "{" many(StructMember) "}"
2223StructMember = (StructField | FnDef | GlobalVarDecl | ContainerDecl)
2227ContainerDecl = ("struct" | "enum" | "union") "{" many(StructMember) "}"
2228StructMember = (StructField | FnDef | GlobalVarDecl)
22242229StructField = Symbol option(":" Expression) ",")
22252230*/
2226static AstNode *ast_parse_container_decl(ParseContext *pc, size_t *token_index, VisibMod visib_mod) {
2231static AstNode *ast_parse_container_decl(ParseContext *pc, size_t *token_index, bool mandatory) {
22272232 Token *first_token = &pc->tokens->at(*token_index);
22282233
22292234 ContainerKind kind;
2230
22312235 if (first_token->id == TokenIdKeywordStruct) {
22322236 kind = ContainerKindStruct;
22332237 } else if (first_token->id == TokenIdKeywordEnum) {
22342238 kind = ContainerKindEnum;
22352239 } else if (first_token->id == TokenIdKeywordUnion) {
22362240 kind = ContainerKindUnion;
2241 } else if (mandatory) {
2242 ast_invalid_token_error(pc, first_token);
22372243 } else {
22382244 return nullptr;
22392245 }
22402246 *token_index += 1;
22412247
2242 Token *struct_name = ast_eat_token(pc, token_index, TokenIdSymbol);
2243
22442248 AstNode *node = ast_create_node(pc, NodeTypeContainerDecl, first_token);
2245 node->data.struct_decl.kind = kind;
2246 node->data.struct_decl.name = token_buf(struct_name);
2247 node->data.struct_decl.visib_mod = visib_mod;
2248
2249 Token *paren_or_brace = &pc->tokens->at(*token_index);
2250 if (paren_or_brace->id == TokenIdLParen) {
2251 ast_parse_param_decl_list(pc, token_index, &node->data.struct_decl.generic_params,
2252 &node->data.struct_decl.generic_params_is_var_args);
2253 ast_eat_token(pc, token_index, TokenIdLBrace);
2254 } else if (paren_or_brace->id == TokenIdLBrace) {
2255 *token_index += 1;
2256 } else {
2257 ast_invalid_token_error(pc, paren_or_brace);
2258 }
2249 node->data.container_decl.kind = kind;
2250
2251 ast_eat_token(pc, token_index, TokenIdLBrace);
22592252
22602253 for (;;) {
22612254 Token *visib_tok = &pc->tokens->at(*token_index);
......@@ -2272,20 +2265,14 @@ static AstNode *ast_parse_container_decl(ParseContext *pc, size_t *token_index,
22722265
22732266 AstNode *fn_def_node = ast_parse_fn_def(pc, token_index, false, visib_mod);
22742267 if (fn_def_node) {
2275 node->data.struct_decl.decls.append(fn_def_node);
2268 node->data.container_decl.decls.append(fn_def_node);
22762269 continue;
22772270 }
22782271
22792272 AstNode *var_decl_node = ast_parse_variable_declaration_expr(pc, token_index, false, visib_mod);
22802273 if (var_decl_node) {
22812274 ast_eat_token(pc, token_index, TokenIdSemicolon);
2282 node->data.struct_decl.decls.append(var_decl_node);
2283 continue;
2284 }
2285
2286 AstNode *container_decl_node = ast_parse_container_decl(pc, token_index, visib_mod);
2287 if (container_decl_node) {
2288 node->data.struct_decl.decls.append(container_decl_node);
2275 node->data.container_decl.decls.append(var_decl_node);
22892276 continue;
22902277 }
22912278
......@@ -2311,7 +2298,7 @@ static AstNode *ast_parse_container_decl(ParseContext *pc, size_t *token_index,
23112298 ast_eat_token(pc, token_index, TokenIdComma);
23122299 }
23132300
2314 node->data.struct_decl.fields.append(field_node);
2301 node->data.container_decl.fields.append(field_node);
23152302 } else {
23162303 ast_invalid_token_error(pc, token);
23172304 }
......@@ -2368,7 +2355,7 @@ static AstNode *ast_parse_type_decl(ParseContext *pc, size_t *token_index, Visib
23682355
23692356/*
23702357TopLevelItem = ErrorValueDecl | Block | TopLevelDecl
2371TopLevelDecl = option(VisibleMod) (FnDef | ExternDecl | ContainerDecl | GlobalVarDecl | TypeDecl | UseDecl)
2358TopLevelDecl = option(VisibleMod) (FnDef | ExternDecl | GlobalVarDecl | TypeDecl | UseDecl)
23722359*/
23732360static void ast_parse_top_level_decls(ParseContext *pc, size_t *token_index, ZigList<AstNode *> *top_level_decls) {
23742361 for (;;) {
......@@ -2402,12 +2389,6 @@ static void ast_parse_top_level_decls(ParseContext *pc, size_t *token_index, Zig
24022389 continue;
24032390 }
24042391
2405 AstNode *struct_node = ast_parse_container_decl(pc, token_index, visib_mod);
2406 if (struct_node) {
2407 top_level_decls->append(struct_node);
2408 continue;
2409 }
2410
24112392 AstNode *var_decl_node = ast_parse_variable_declaration_expr(pc, token_index, false, visib_mod);
24122393 if (var_decl_node) {
24132394 ast_eat_token(pc, token_index, TokenIdSemicolon);
......@@ -2630,8 +2611,8 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
26302611 }
26312612 break;
26322613 case NodeTypeContainerDecl:
2633 visit_node_list(&node->data.struct_decl.fields, visit, context);
2634 visit_node_list(&node->data.struct_decl.decls, visit, context);
2614 visit_node_list(&node->data.container_decl.fields, visit, context);
2615 visit_node_list(&node->data.container_decl.decls, visit, context);
26352616 break;
26362617 case NodeTypeStructField:
26372618 visit_field(&node->data.struct_field.type, visit, context);
test/self_hosted2.zig+2-2
......@@ -87,9 +87,9 @@ var goto_counter: i32 = 0;
8787
8888
8989
90struct FooA {
90const FooA = struct {
9191 fn add(a: i32, b: i32) -> i32 { a + b }
92}
92};
9393const foo_a = FooA {};
9494
9595fn testStructStatic() {