authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-28 10:11:32-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-28 10:11:32-05:00
log5424b4320def194b205dcfe8e937035d2d80ae09
tree033c9534190356feefa34ac7649f729202beac89
parentd093f51f16ab9fe4f119a47c80c59d99a90a590f
signature Commit is signed but in an unrecognized format.

remove namespace type; files are empty structs

closes #1047

18 files changed, 329 insertions(+), 411 deletions(-)

doc/langref.html.in+14-11
......@@ -6785,7 +6785,6 @@ pub const TypeId = enum {
67856785 Enum,
67866786 Union,
67876787 Fn,
6788 Namespace,
67896788 Block,
67906789 BoundFn,
67916790 ArgTuple,
......@@ -6820,7 +6819,6 @@ pub const TypeInfo = union(TypeId) {
68206819 Enum: Enum,
68216820 Union: Union,
68226821 Fn: Fn,
6823 Namespace: void,
68246822 BoundFn: Fn,
68256823 ArgTuple: void,
68266824 Opaque: void,
......@@ -8167,17 +8165,18 @@ coding style.
81678165 </p>
81688166 <ul>
81698167 <li>
8170 If {#syntax#}x{#endsyntax#} is a {#syntax#}struct{#endsyntax#} (or an alias of a {#syntax#}struct{#endsyntax#}),
8171 then {#syntax#}x{#endsyntax#} should be {#syntax#}TitleCase{#endsyntax#}.
8168 If {#syntax#}x{#endsyntax#} is a {#syntax#}type{#endsyntax#}
8169 then {#syntax#}x{#endsyntax#} should be {#syntax#}TitleCase{#endsyntax#}, unless it
8170 is a {#syntax#}struct{#endsyntax#} with 0 fields and is never meant to be instantiated,
8171 in which case it is considered to be a "namespace" and uses {#syntax#}snake_case{#end_syntax#}.
81728172 </li>
81738173 <li>
8174 If {#syntax#}x{#endsyntax#} otherwise identifies a type, {#syntax#}x{#endsyntax#} should have {#syntax#}snake_case{#endsyntax#}.
8174 If {#syntax#}x{#endsyntax#} is callable, and {#syntax#}x{#endsyntax#}'s return type is
8175 {#syntax#}type{#endsyntax#}, then {#syntax#}x{#endsyntax#} should be {#syntax#}TitleCase{#endsyntax#}.
81758176 </li>
81768177 <li>
8177 If {#syntax#}x{#endsyntax#} is callable, and {#syntax#}x{#endsyntax#}'s return type is {#syntax#}type{#endsyntax#}, then {#syntax#}x{#endsyntax#} should be {#syntax#}TitleCase{#endsyntax#}.
8178 </li>
8179 <li>
8180 If {#syntax#}x{#endsyntax#} is otherwise callable, then {#syntax#}x{#endsyntax#} should be {#syntax#}camelCase{#endsyntax#}.
8178 If {#syntax#}x{#endsyntax#} is otherwise callable, then {#syntax#}x{#endsyntax#} should
8179 be {#syntax#}camelCase{#endsyntax#}.
81818180 </li>
81828181 <li>
81838182 Otherwise, {#syntax#}x{#endsyntax#} should be {#syntax#}snake_case{#endsyntax#}.
......@@ -8203,7 +8202,9 @@ const const_name = 42;
82038202const primitive_type_alias = f32;
82048203const string_alias = []u8;
82058204
8206const StructName = struct {};
8205const StructName = struct {
8206 field: i32,
8207};
82078208const StructAlias = StructName;
82088209
82098210fn functionName(param_name: TypeName) void {
......@@ -8231,7 +8232,9 @@ const xml_document =
82318232 \\<document>
82328233 \\</document>
82338234;
8234const XmlParser = struct {};
8235const XmlParser = struct {
8236 field: i32,
8237};
82358238
82368239// The initials BE (Big Endian) are just another word in Zig identifier names.
82378240fn readU32Be() u32 {}
src-self-hosted/type.zig-12
......@@ -39,7 +39,6 @@ pub const Type = struct {
3939 Id.ErrorSet => @fieldParentPtr(ErrorSet, "base", base).destroy(comp),
4040 Id.Enum => @fieldParentPtr(Enum, "base", base).destroy(comp),
4141 Id.Union => @fieldParentPtr(Union, "base", base).destroy(comp),
42 Id.Namespace => @fieldParentPtr(Namespace, "base", base).destroy(comp),
4342 Id.BoundFn => @fieldParentPtr(BoundFn, "base", base).destroy(comp),
4443 Id.ArgTuple => @fieldParentPtr(ArgTuple, "base", base).destroy(comp),
4544 Id.Opaque => @fieldParentPtr(Opaque, "base", base).destroy(comp),
......@@ -73,7 +72,6 @@ pub const Type = struct {
7372 Id.ErrorSet => return @fieldParentPtr(ErrorSet, "base", base).getLlvmType(allocator, llvm_context),
7473 Id.Enum => return @fieldParentPtr(Enum, "base", base).getLlvmType(allocator, llvm_context),
7574 Id.Union => return @fieldParentPtr(Union, "base", base).getLlvmType(allocator, llvm_context),
76 Id.Namespace => unreachable,
7775 Id.BoundFn => return @fieldParentPtr(BoundFn, "base", base).getLlvmType(allocator, llvm_context),
7876 Id.ArgTuple => unreachable,
7977 Id.Opaque => return @fieldParentPtr(Opaque, "base", base).getLlvmType(allocator, llvm_context),
......@@ -89,7 +87,6 @@ pub const Type = struct {
8987 Id.ComptimeInt,
9088 Id.Undefined,
9189 Id.Null,
92 Id.Namespace,
9390 Id.BoundFn,
9491 Id.ArgTuple,
9592 Id.Opaque,
......@@ -123,7 +120,6 @@ pub const Type = struct {
123120 Id.ComptimeInt,
124121 Id.Undefined,
125122 Id.Null,
126 Id.Namespace,
127123 Id.BoundFn,
128124 Id.ArgTuple,
129125 Id.Opaque,
......@@ -1020,14 +1016,6 @@ pub const Type = struct {
10201016 }
10211017 };
10221018
1023 pub const Namespace = struct {
1024 base: Type,
1025
1026 pub fn destroy(self: *Namespace, comp: *Compilation) void {
1027 comp.gpa().destroy(self);
1028 }
1029 };
1030
10311019 pub const BoundFn = struct {
10321020 base: Type,
10331021
src/all_types.hpp+40-44
......@@ -21,7 +21,6 @@
2121#include "libc_installation.hpp"
2222
2323struct AstNode;
24struct ImportTableEntry;
2524struct ZigFn;
2625struct Scope;
2726struct ScopeBlock;
......@@ -317,7 +316,6 @@ struct ConstExprValue {
317316 ConstUnionValue x_union;
318317 ConstArrayValue x_array;
319318 ConstPtrValue x_ptr;
320 ImportTableEntry *x_import;
321319 ConstArgTuple x_arg_tuple;
322320
323321 // populated if special == ConstValSpecialRuntime
......@@ -369,7 +367,7 @@ struct Tld {
369367 VisibMod visib_mod;
370368 AstNode *source_node;
371369
372 ImportTableEntry *import;
370 ZigType *import;
373371 Scope *parent_scope;
374372 // set this flag temporarily to detect infinite loops
375373 bool dep_loop_flag;
......@@ -937,7 +935,7 @@ struct AstNode {
937935 enum NodeType type;
938936 size_t line;
939937 size_t column;
940 ImportTableEntry *owner;
938 ZigType *owner;
941939 union {
942940 AstNodeFnDef fn_def;
943941 AstNodeFnProto fn_proto;
......@@ -1075,12 +1073,32 @@ enum ResolveStatus {
10751073 ResolveStatusSizeKnown,
10761074};
10771075
1076struct ZigPackage {
1077 Buf root_src_dir;
1078 Buf root_src_path; // relative to root_src_dir
1079
1080 // reminder: hash tables must be initialized before use
1081 HashMap<Buf *, ZigPackage *, buf_hash, buf_eql_buf> package_table;
1082};
1083
1084// Stuff that only applies to a struct which is the implicit root struct of a file
1085struct RootStruct {
1086 ZigPackage *package;
1087 Buf *path; // relative to root_package->root_src_dir
1088 ZigList<size_t> *line_offsets;
1089 Buf *source_code;
1090 AstNode *c_import_node;
1091 ZigLLVMDIFile *di_file;
1092 bool scanned;
1093};
1094
10781095struct ZigTypeStruct {
10791096 AstNode *decl_node;
10801097 TypeStructField *fields;
10811098 ScopeDecls *decls_scope;
10821099 uint64_t size_bytes;
10831100 HashMap<Buf *, TypeStructField *, buf_hash, buf_eql_buf> fields_by_name;
1101 RootStruct *root_struct;
10841102
10851103 uint32_t src_field_count;
10861104 uint32_t gen_field_count;
......@@ -1232,7 +1250,6 @@ enum ZigTypeId {
12321250 ZigTypeIdEnum,
12331251 ZigTypeIdUnion,
12341252 ZigTypeIdFn,
1235 ZigTypeIdNamespace,
12361253 ZigTypeIdBoundFn,
12371254 ZigTypeIdArgTuple,
12381255 ZigTypeIdOpaque,
......@@ -1285,29 +1302,6 @@ struct ZigType {
12851302 bool gen_h_loop_flag;
12861303};
12871304
1288struct PackageTableEntry {
1289 Buf root_src_dir;
1290 Buf root_src_path; // relative to root_src_dir
1291
1292 // reminder: hash tables must be initialized before use
1293 HashMap<Buf *, PackageTableEntry *, buf_hash, buf_eql_buf> package_table;
1294};
1295
1296struct ImportTableEntry {
1297 AstNode *root;
1298 Buf *path; // relative to root_package->root_src_dir
1299 PackageTableEntry *package;
1300 ZigLLVMDIFile *di_file;
1301 Buf *source_code;
1302 ZigList<size_t> *line_offsets;
1303 ScopeDecls *decls_scope;
1304 AstNode *c_import_node;
1305 bool any_imports_failed;
1306 bool scanned;
1307
1308 ZigList<AstNode *> use_decls;
1309};
1310
13111305enum FnAnalState {
13121306 FnAnalStateReady,
13131307 FnAnalStateProbing,
......@@ -1670,7 +1664,7 @@ struct CodeGen {
16701664 LLVMValueRef return_err_fn;
16711665
16721666 // reminder: hash tables must be initialized before use
1673 HashMap<Buf *, ImportTableEntry *, buf_hash, buf_eql_buf> import_table;
1667 HashMap<Buf *, ZigType *, buf_hash, buf_eql_buf> import_table;
16741668 HashMap<Buf *, BuiltinFnEntry *, buf_hash, buf_eql_buf> builtin_fn_table;
16751669 HashMap<Buf *, ZigType *, buf_hash, buf_eql_buf> primitive_type_table;
16761670 HashMap<TypeId, ZigType *, type_id_hash, type_id_eql> type_table;
......@@ -1684,7 +1678,7 @@ struct CodeGen {
16841678 HashMap<Buf *, ConstExprValue *, buf_hash, buf_eql_buf> string_literals_table;
16851679 HashMap<const ZigType *, ConstExprValue *, type_ptr_hash, type_ptr_eql> type_info_cache;
16861680
1687 ZigList<ImportTableEntry *> import_queue;
1681 ZigList<ZigType *> import_queue;
16881682 size_t import_queue_index;
16891683 ZigList<Tld *> resolve_queue;
16901684 size_t resolve_queue_index;
......@@ -1699,14 +1693,14 @@ struct CodeGen {
16991693 ZigList<ErrorTableEntry *> errors_by_index;
17001694 size_t largest_err_name_len;
17011695
1702 PackageTableEntry *std_package;
1703 PackageTableEntry *panic_package;
1704 PackageTableEntry *test_runner_package;
1705 PackageTableEntry *compile_var_package;
1706 ImportTableEntry *compile_var_import;
1707 ImportTableEntry *root_import;
1708 ImportTableEntry *bootstrap_import;
1709 ImportTableEntry *test_runner_import;
1696 ZigPackage *std_package;
1697 ZigPackage *panic_package;
1698 ZigPackage *test_runner_package;
1699 ZigPackage *compile_var_package;
1700 ZigType *compile_var_import;
1701 ZigType *root_import;
1702 ZigType *bootstrap_import;
1703 ZigType *test_runner_import;
17101704
17111705 struct {
17121706 ZigType *entry_bool;
......@@ -1731,7 +1725,6 @@ struct CodeGen {
17311725 ZigType *entry_unreachable;
17321726 ZigType *entry_type;
17331727 ZigType *entry_invalid;
1734 ZigType *entry_namespace;
17351728 ZigType *entry_block;
17361729 ZigType *entry_num_lit_int;
17371730 ZigType *entry_num_lit_float;
......@@ -1851,7 +1844,7 @@ struct CodeGen {
18511844 Buf *root_out_name;
18521845 Buf *test_filter;
18531846 Buf *test_name_prefix;
1854 PackageTableEntry *root_package;
1847 ZigPackage *root_package;
18551848 Buf *zig_lib_dir;
18561849 Buf *zig_std_dir;
18571850
......@@ -1945,13 +1938,16 @@ struct ScopeDecls {
19451938 Scope base;
19461939
19471940 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> decl_table;
1948 bool safety_off;
1941 ZigList<AstNode *> use_decls;
19491942 AstNode *safety_set_node;
1950 bool fast_math_on;
19511943 AstNode *fast_math_set_node;
1952 ImportTableEntry *import;
1944 ZigType *import;
19531945 // If this is a scope from a container, this is the type entry, otherwise null
19541946 ZigType *container_type;
1947
1948 bool safety_off;
1949 bool fast_math_on;
1950 bool any_imports_failed;
19551951};
19561952
19571953// This scope comes from a block expression in user code.
......@@ -3507,7 +3503,7 @@ struct FnWalkTypes {
35073503};
35083504
35093505struct FnWalkVars {
3510 ImportTableEntry *import;
3506 ZigType *import;
35113507 LLVMValueRef llvm_fn;
35123508 ZigFn *fn;
35133509 ZigVar *var;
src/analyze.cpp+133-145
......@@ -28,10 +28,14 @@ static Error ATTRIBUTE_MUST_USE resolve_enum_zero_bits(CodeGen *g, ZigType *enum
2828static Error ATTRIBUTE_MUST_USE resolve_union_zero_bits(CodeGen *g, ZigType *union_type);
2929static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry);
3030
31static ErrorMsg *add_error_note_token(CodeGen *g, ErrorMsg *parent_msg, ImportTableEntry *owner, Token *token,
32 Buf *msg)
33{
34 if (owner->c_import_node != nullptr) {
31static bool is_top_level_struct(ZigType *import) {
32 return import->id == ZigTypeIdStruct && import->data.structure.root_struct != nullptr;
33}
34
35static ErrorMsg *add_error_note_token(CodeGen *g, ErrorMsg *parent_msg, ZigType *owner, Token *token, Buf *msg) {
36 assert(is_top_level_struct(owner));
37 RootStruct *root_struct = owner->data.structure.root_struct;
38 if (root_struct->c_import_node != nullptr) {
3539 // if this happens, then translate_c generated code that
3640 // failed semantic analysis, which isn't supposed to happen
3741
......@@ -46,18 +50,20 @@ static ErrorMsg *add_error_note_token(CodeGen *g, ErrorMsg *parent_msg, ImportTa
4650 return note;
4751 }
4852
49 ErrorMsg *err = err_msg_create_with_line(owner->path, token->start_line, token->start_column,
50 owner->source_code, owner->line_offsets, msg);
53 ErrorMsg *err = err_msg_create_with_line(root_struct->path, token->start_line, token->start_column,
54 root_struct->source_code, root_struct->line_offsets, msg);
5155
5256 err_msg_add_note(parent_msg, err);
5357 return err;
5458}
5559
56ErrorMsg *add_token_error(CodeGen *g, ImportTableEntry *owner, Token *token, Buf *msg) {
57 if (owner->c_import_node != nullptr) {
60ErrorMsg *add_token_error(CodeGen *g, ZigType *owner, Token *token, Buf *msg) {
61 assert(is_top_level_struct(owner));
62 RootStruct *root_struct = owner->data.structure.root_struct;
63 if (root_struct->c_import_node != nullptr) {
5864 // if this happens, then translate_c generated code that
5965 // failed semantic analysis, which isn't supposed to happen
60 ErrorMsg *err = add_node_error(g, owner->c_import_node,
66 ErrorMsg *err = add_node_error(g, root_struct->c_import_node,
6167 buf_sprintf("compiler bug: @cImport generated invalid zig code"));
6268
6369 add_error_note_token(g, err, owner, token, msg);
......@@ -65,8 +71,8 @@ ErrorMsg *add_token_error(CodeGen *g, ImportTableEntry *owner, Token *token, Buf
6571 g->errors.append(err);
6672 return err;
6773 }
68 ErrorMsg *err = err_msg_create_with_line(owner->path, token->start_line, token->start_column,
69 owner->source_code, owner->line_offsets, msg);
74 ErrorMsg *err = err_msg_create_with_line(root_struct->path, token->start_line, token->start_column,
75 root_struct->source_code, root_struct->line_offsets, msg);
7076
7177 g->errors.append(err);
7278 return err;
......@@ -114,7 +120,7 @@ void init_scope(CodeGen *g, Scope *dest, ScopeId id, AstNode *source_node, Scope
114120 dest->parent = parent;
115121}
116122
117ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type, ImportTableEntry *import) {
123ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type, ZigType *import) {
118124 assert(node == nullptr || node->type == NodeTypeContainerDecl || node->type == NodeTypeFnCallExpr);
119125 ScopeDecls *scope = allocate<ScopeDecls>(1);
120126 init_scope(g, &scope->base, ScopeIdDecls, node, parent);
......@@ -207,11 +213,11 @@ Scope *create_coro_prelude_scope(CodeGen *g, AstNode *node, Scope *parent) {
207213 return &scope->base;
208214}
209215
210ImportTableEntry *get_scope_import(Scope *scope) {
216ZigType *get_scope_import(Scope *scope) {
211217 while (scope) {
212218 if (scope->id == ScopeIdDecls) {
213219 ScopeDecls *decls_scope = (ScopeDecls *)scope;
214 assert(decls_scope->import);
220 assert(is_top_level_struct(decls_scope->import));
215221 return decls_scope->import;
216222 }
217223 scope = scope->parent;
......@@ -261,7 +267,6 @@ AstNode *type_decl_node(ZigType *type_entry) {
261267 case ZigTypeIdErrorUnion:
262268 case ZigTypeIdErrorSet:
263269 case ZigTypeIdFn:
264 case ZigTypeIdNamespace:
265270 case ZigTypeIdBoundFn:
266271 case ZigTypeIdArgTuple:
267272 case ZigTypeIdPromise:
......@@ -323,7 +328,6 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
323328 case ZigTypeIdErrorUnion:
324329 case ZigTypeIdErrorSet:
325330 case ZigTypeIdFn:
326 case ZigTypeIdNamespace:
327331 case ZigTypeIdBoundFn:
328332 case ZigTypeIdArgTuple:
329333 case ZigTypeIdPromise:
......@@ -1010,14 +1014,14 @@ ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const c
10101014
10111015 buf_init_from_str(&entry->name, name);
10121016
1013 ImportTableEntry *import = scope ? get_scope_import(scope) : nullptr;
1017 ZigType *import = scope ? get_scope_import(scope) : nullptr;
10141018 unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0;
10151019
10161020 entry->type_ref = LLVMInt8Type();
10171021 entry->di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,
10181022 ZigLLVMTag_DW_structure_type(), buf_ptr(&entry->name),
1019 import ? ZigLLVMFileToScope(import->di_file) : nullptr,
1020 import ? import->di_file : nullptr,
1023 import ? ZigLLVMFileToScope(import->data.structure.root_struct->di_file) : nullptr,
1024 import ? import->data.structure.root_struct->di_file : nullptr,
10211025 line);
10221026 entry->zero_bits = false;
10231027
......@@ -1288,6 +1292,25 @@ static ZigTypeId container_to_type(ContainerKind kind) {
12881292 zig_unreachable();
12891293}
12901294
1295// This is like get_partial_container_type except it's for the implicit root struct of files.
1296ZigType *get_root_container_type(CodeGen *g, const char *name, RootStruct *root_struct) {
1297 ZigType *entry = new_type_table_entry(ZigTypeIdStruct);
1298 entry->data.structure.decls_scope = create_decls_scope(g, nullptr, nullptr, nullptr, entry);
1299 entry->data.structure.root_struct = root_struct;
1300 entry->data.structure.layout = ContainerLayoutAuto;
1301 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), name);
1302
1303 size_t line = 0; // root therefore first line
1304 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
1305
1306 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
1307 dwarf_kind, name,
1308 ZigLLVMFileToScope(root_struct->di_file), root_struct->di_file, (unsigned)(line + 1));
1309
1310 buf_init_from_str(&entry->name, name);
1311 return entry;
1312}
1313
12911314ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind,
12921315 AstNode *decl_node, const char *name, ContainerLayout layout)
12931316{
......@@ -1312,11 +1335,12 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind
13121335 size_t line = decl_node ? decl_node->line : 0;
13131336 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
13141337
1315 ImportTableEntry *import = get_scope_import(scope);
1338 ZigType *import = get_scope_import(scope);
13161339 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), name);
13171340 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
13181341 dwarf_kind, name,
1319 ZigLLVMFileToScope(import->di_file), import->di_file, (unsigned)(line + 1));
1342 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
1343 import->data.structure.root_struct->di_file, (unsigned)(line + 1));
13201344
13211345 buf_init_from_str(&entry->name, name);
13221346
......@@ -1459,7 +1483,6 @@ static Error emit_error_unless_type_allowed_in_packed_struct(CodeGen *g, ZigType
14591483 case ZigTypeIdNull:
14601484 case ZigTypeIdErrorUnion:
14611485 case ZigTypeIdErrorSet:
1462 case ZigTypeIdNamespace:
14631486 case ZigTypeIdBoundFn:
14641487 case ZigTypeIdArgTuple:
14651488 case ZigTypeIdOpaque:
......@@ -1547,7 +1570,6 @@ bool type_allowed_in_extern(CodeGen *g, ZigType *type_entry) {
15471570 case ZigTypeIdNull:
15481571 case ZigTypeIdErrorUnion:
15491572 case ZigTypeIdErrorSet:
1550 case ZigTypeIdNamespace:
15511573 case ZigTypeIdBoundFn:
15521574 case ZigTypeIdArgTuple:
15531575 case ZigTypeIdPromise:
......@@ -1706,7 +1728,6 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
17061728 return g->builtin_types.entry_invalid;
17071729 case ZigTypeIdComptimeFloat:
17081730 case ZigTypeIdComptimeInt:
1709 case ZigTypeIdNamespace:
17101731 case ZigTypeIdBoundFn:
17111732 case ZigTypeIdMetaType:
17121733 case ZigTypeIdVoid:
......@@ -1801,7 +1822,6 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
18011822
18021823 case ZigTypeIdComptimeFloat:
18031824 case ZigTypeIdComptimeInt:
1804 case ZigTypeIdNamespace:
18051825 case ZigTypeIdBoundFn:
18061826 case ZigTypeIdMetaType:
18071827 case ZigTypeIdUnreachable:
......@@ -1895,7 +1915,7 @@ static Error resolve_enum_type(CodeGen *g, ZigType *enum_type) {
18951915 ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);
18961916
18971917 Scope *scope = &enum_type->data.enumeration.decls_scope->base;
1898 ImportTableEntry *import = get_scope_import(scope);
1918 ZigType *import = get_scope_import(scope);
18991919
19001920 // set temporary flag
19011921 enum_type->data.enumeration.embedded_in_current = true;
......@@ -1924,9 +1944,9 @@ static Error resolve_enum_type(CodeGen *g, ZigType *enum_type) {
19241944 ZigLLVMDIType **di_root_members = nullptr;
19251945 size_t debug_member_count = 0;
19261946 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
1927 ZigLLVMFileToScope(import->di_file),
1947 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
19281948 buf_ptr(&enum_type->name),
1929 import->di_file, (unsigned)(decl_node->line + 1),
1949 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
19301950 debug_size_in_bits,
19311951 debug_align_in_bits,
19321952 0, nullptr, di_root_members, (int)debug_member_count, 0, nullptr, "");
......@@ -1942,8 +1962,8 @@ static Error resolve_enum_type(CodeGen *g, ZigType *enum_type) {
19421962 uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, tag_int_type->type_ref);
19431963 uint64_t tag_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, tag_int_type->type_ref);
19441964 ZigLLVMDIType *tag_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
1945 ZigLLVMFileToScope(import->di_file), buf_ptr(&enum_type->name),
1946 import->di_file, (unsigned)(decl_node->line + 1),
1965 ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&enum_type->name),
1966 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
19471967 tag_debug_size_in_bits,
19481968 tag_debug_align_in_bits,
19491969 di_enumerators, field_count,
......@@ -2159,15 +2179,15 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
21592179 if (struct_type->zero_bits) {
21602180 struct_type->type_ref = LLVMVoidType();
21612181
2162 ImportTableEntry *import = get_scope_import(scope);
2182 ZigType *import = get_scope_import(scope);
21632183 uint64_t debug_size_in_bits = 0;
21642184 uint64_t debug_align_in_bits = 0;
21652185 ZigLLVMDIType **di_element_types = nullptr;
21662186 size_t debug_field_count = 0;
21672187 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
2168 ZigLLVMFileToScope(import->di_file),
2188 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
21692189 buf_ptr(&struct_type->name),
2170 import->di_file, (unsigned)(decl_node->line + 1),
2190 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
21712191 debug_size_in_bits,
21722192 debug_align_in_bits,
21732193 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");
......@@ -2191,7 +2211,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
21912211
21922212 ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(debug_field_count);
21932213
2194 ImportTableEntry *import = get_scope_import(scope);
2214 ZigType *import = get_scope_import(scope);
21952215 size_t debug_field_index = 0;
21962216 for (size_t i = 0; i < field_count; i += 1) {
21972217 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
......@@ -2234,7 +2254,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
22342254 }
22352255 di_element_types[debug_field_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
22362256 ZigLLVMTypeToScope(struct_type->di_type), buf_ptr(type_struct_field->name),
2237 import->di_file, (unsigned)(field_node->line + 1),
2257 import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1),
22382258 debug_size_in_bits,
22392259 debug_align_in_bits,
22402260 debug_offset_in_bits,
......@@ -2247,9 +2267,9 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
22472267 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, struct_type->type_ref);
22482268 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, struct_type->type_ref);
22492269 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
2250 ZigLLVMFileToScope(import->di_file),
2270 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
22512271 buf_ptr(&struct_type->name),
2252 import->di_file, (unsigned)(decl_node->line + 1),
2272 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
22532273 debug_size_in_bits,
22542274 debug_align_in_bits,
22552275 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");
......@@ -2298,7 +2318,7 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
22982318 uint64_t biggest_size_in_bits = 0;
22992319
23002320 Scope *scope = &union_type->data.unionation.decls_scope->base;
2301 ImportTableEntry *import = get_scope_import(scope);
2321 ZigType *import = get_scope_import(scope);
23022322
23032323 // set temporary flag
23042324 union_type->data.unionation.embedded_in_current = true;
......@@ -2325,7 +2345,7 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
23252345
23262346 union_inner_di_types[union_field->gen_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
23272347 ZigLLVMTypeToScope(union_type->di_type), buf_ptr(union_field->enum_field->name),
2328 import->di_file, (unsigned)(field_node->line + 1),
2348 import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1),
23292349 store_size_in_bits,
23302350 abi_align_in_bits,
23312351 0,
......@@ -2358,9 +2378,9 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
23582378 ZigLLVMDIType **di_root_members = nullptr;
23592379 size_t debug_member_count = 0;
23602380 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
2361 ZigLLVMFileToScope(import->di_file),
2381 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
23622382 buf_ptr(&union_type->name),
2363 import->di_file, (unsigned)(decl_node->line + 1),
2383 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
23642384 debug_size_in_bits,
23652385 debug_align_in_bits,
23662386 0, di_root_members, (int)debug_member_count, 0, "");
......@@ -2396,8 +2416,8 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
23962416
23972417 // create debug type for union
23982418 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
2399 ZigLLVMFileToScope(import->di_file), buf_ptr(&union_type->name),
2400 import->di_file, (unsigned)(decl_node->line + 1),
2419 ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&union_type->name),
2420 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
24012421 biggest_size_in_bits, biggest_align_in_bits, 0, union_inner_di_types,
24022422 gen_field_count, 0, "");
24032423
......@@ -2452,7 +2472,7 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
24522472 // create debug type for union
24532473 ZigLLVMDIType *union_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
24542474 ZigLLVMTypeToScope(union_type->di_type), "AnonUnion",
2455 import->di_file, (unsigned)(decl_node->line + 1),
2475 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
24562476 biggest_size_in_bits, biggest_align_in_bits, 0, union_inner_di_types,
24572477 gen_field_count, 0, "");
24582478
......@@ -2463,7 +2483,7 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
24632483
24642484 ZigLLVMDIType *union_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder,
24652485 ZigLLVMTypeToScope(union_type->di_type), "payload",
2466 import->di_file, (unsigned)(decl_node->line + 1),
2486 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
24672487 biggest_size_in_bits,
24682488 biggest_align_in_bits,
24692489 union_offset_in_bits,
......@@ -2474,7 +2494,7 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
24742494
24752495 ZigLLVMDIType *tag_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder,
24762496 ZigLLVMTypeToScope(union_type->di_type), "tag",
2477 import->di_file, (unsigned)(decl_node->line + 1),
2497 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
24782498 tag_debug_size_in_bits,
24792499 tag_debug_align_in_bits,
24802500 tag_offset_in_bits,
......@@ -2487,9 +2507,9 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
24872507 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, union_type->type_ref);
24882508 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, union_type->type_ref);
24892509 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
2490 ZigLLVMFileToScope(import->di_file),
2510 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
24912511 buf_ptr(&union_type->name),
2492 import->di_file, (unsigned)(decl_node->line + 1),
2512 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
24932513 debug_size_in_bits,
24942514 debug_align_in_bits,
24952515 0, nullptr, di_root_members, 2, 0, nullptr, "");
......@@ -3185,15 +3205,15 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
31853205 }
31863206
31873207 if (create_enum_type) {
3188 ImportTableEntry *import = get_scope_import(scope);
3208 ZigType *import = get_scope_import(scope);
31893209 uint64_t tag_debug_size_in_bits = tag_type->zero_bits ? 0 :
31903210 8*LLVMStoreSizeOfType(g->target_data_ref, tag_type->type_ref);
31913211 uint64_t tag_debug_align_in_bits = tag_type->zero_bits ? 0 :
31923212 8*LLVMABIAlignmentOfType(g->target_data_ref, tag_type->type_ref);
31933213 // TODO get a more accurate debug scope
31943214 ZigLLVMDIType *tag_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
3195 ZigLLVMFileToScope(import->di_file), buf_ptr(&tag_type->name),
3196 import->di_file, (unsigned)(decl_node->line + 1),
3215 ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&tag_type->name),
3216 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
31973217 tag_debug_size_in_bits, tag_debug_align_in_bits, di_enumerators, field_count,
31983218 tag_type->di_type, "");
31993219 tag_type->di_type = tag_di_type;
......@@ -3265,7 +3285,8 @@ static bool scope_is_root_decls(Scope *scope) {
32653285 while (scope) {
32663286 if (scope->id == ScopeIdDecls) {
32673287 ScopeDecls *scope_decls = (ScopeDecls *)scope;
3268 return (scope_decls->container_type == nullptr);
3288 return scope_decls->container_type == nullptr ||
3289 is_top_level_struct(scope_decls->container_type);
32693290 }
32703291 scope = scope->parent;
32713292 }
......@@ -3326,7 +3347,7 @@ void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, Buf *symbol_name, GlobalLi
33263347}
33273348
33283349static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
3329 ImportTableEntry *import = tld_fn->base.import;
3350 ZigType *import = tld_fn->base.import;
33303351 AstNode *source_node = tld_fn->base.source_node;
33313352 if (source_node->type == NodeTypeFnProto) {
33323353 AstNodeFnProto *fn_proto = &source_node->data.fn_proto;
......@@ -3382,11 +3403,11 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
33823403 }
33833404
33843405 if (scope_is_root_decls(tld_fn->base.parent_scope) &&
3385 (import == g->root_import || import->package == g->panic_package))
3406 (import == g->root_import || import->data.structure.root_struct->package == g->panic_package))
33863407 {
33873408 if (g->have_pub_main && buf_eql_str(&fn_table_entry->symbol_name, "main")) {
33883409 g->main_fn = fn_table_entry;
3389 } else if ((import->package == g->panic_package || g->have_pub_panic) &&
3410 } else if ((import->data.structure.root_struct->package == g->panic_package || g->have_pub_panic) &&
33903411 buf_eql_str(&fn_table_entry->symbol_name, "panic"))
33913412 {
33923413 g->panic_fn = fn_table_entry;
......@@ -3473,8 +3494,8 @@ static void preview_test_decl(CodeGen *g, AstNode *node, ScopeDecls *decls_scope
34733494 if (!g->is_test_build)
34743495 return;
34753496
3476 ImportTableEntry *import = get_scope_import(&decls_scope->base);
3477 if (import->package != g->root_package)
3497 ZigType *import = get_scope_import(&decls_scope->base);
3498 if (import->data.structure.root_struct->package != g->root_package)
34783499 return;
34793500
34803501 Buf *decl_name_buf = node->data.test_decl.name;
......@@ -3511,8 +3532,8 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source
35113532}
35123533
35133534void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value) {
3514 Tld *tld = g->compile_var_import->decls_scope->decl_table.get(name);
3515 resolve_top_level_decl(g, tld, false, tld->source_node);
3535 Tld *tld = get_container_scope(g->compile_var_import)->decl_table.get(name);
3536 resolve_top_level_decl(g, tld, tld->source_node);
35163537 assert(tld->id == TldIdVar);
35173538 TldVar *tld_var = (TldVar *)tld;
35183539 tld_var->var->const_value = value;
......@@ -3561,8 +3582,8 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
35613582 case NodeTypeUse:
35623583 {
35633584 g->use_queue.append(node);
3564 ImportTableEntry *import = get_scope_import(&decls_scope->base);
3565 import->use_decls.append(node);
3585 ZigType *import = get_scope_import(&decls_scope->base);
3586 get_container_scope(import)->use_decls.append(node);
35663587 break;
35673588 }
35683589 case NodeTypeTestDecl:
......@@ -3654,7 +3675,6 @@ ZigType *validate_var_type(CodeGen *g, AstNode *source_node, ZigType *type_entry
36543675 return g->builtin_types.entry_invalid;
36553676 case ZigTypeIdComptimeFloat:
36563677 case ZigTypeIdComptimeInt:
3657 case ZigTypeIdNamespace:
36583678 case ZigTypeIdMetaType:
36593679 case ZigTypeIdVoid:
36603680 case ZigTypeIdBool:
......@@ -3847,16 +3867,10 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
38473867 g->global_vars.append(tld_var);
38483868}
38493869
3850void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only, AstNode *source_node) {
3870void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) {
38513871 if (tld->resolution != TldResolutionUnresolved)
38523872 return;
38533873
3854 if (tld->dep_loop_flag) {
3855 add_node_error(g, tld->source_node, buf_sprintf("'%s' depends on itself", buf_ptr(tld->name)));
3856 tld->resolution = TldResolutionInvalid;
3857 return;
3858 }
3859
38603874 tld->dep_loop_flag = true;
38613875 g->tld_ref_source_node_stack.append(source_node);
38623876
......@@ -3894,9 +3908,9 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only, AstNode *so
38943908
38953909Tld *find_decl(CodeGen *g, Scope *scope, Buf *name) {
38963910 // we must resolve all the use decls
3897 ImportTableEntry *import = get_scope_import(scope);
3898 for (size_t i = 0; i < import->use_decls.length; i += 1) {
3899 AstNode *use_decl_node = import->use_decls.at(i);
3911 ZigType *import = get_scope_import(scope);
3912 for (size_t i = 0; i < get_container_scope(import)->use_decls.length; i += 1) {
3913 AstNode *use_decl_node = get_container_scope(import)->use_decls.at(i);
39003914 if (use_decl_node->data.use.resolution == TldResolutionUnresolved) {
39013915 preview_use_decl(g, use_decl_node);
39023916 resolve_use_decl(g, use_decl_node);
......@@ -4039,7 +4053,6 @@ static bool is_container(ZigType *type_entry) {
40394053 case ZigTypeIdErrorUnion:
40404054 case ZigTypeIdErrorSet:
40414055 case ZigTypeIdFn:
4042 case ZigTypeIdNamespace:
40434056 case ZigTypeIdBoundFn:
40444057 case ZigTypeIdArgTuple:
40454058 case ZigTypeIdOpaque:
......@@ -4098,7 +4111,6 @@ void resolve_container_type(CodeGen *g, ZigType *type_entry) {
40984111 case ZigTypeIdErrorUnion:
40994112 case ZigTypeIdErrorSet:
41004113 case ZigTypeIdFn:
4101 case ZigTypeIdNamespace:
41024114 case ZigTypeIdBoundFn:
41034115 case ZigTypeIdInvalid:
41044116 case ZigTypeIdArgTuple:
......@@ -4344,7 +4356,7 @@ static void add_symbols_from_import(CodeGen *g, AstNode *src_use_node, AstNode *
43444356
43454357 ConstExprValue *use_target_value = src_use_node->data.use.value;
43464358 if (type_is_invalid(use_target_value->type)) {
4347 dst_use_node->owner->any_imports_failed = true;
4359 get_container_scope(dst_use_node->owner)->any_imports_failed = true;
43484360 return;
43494361 }
43504362
......@@ -4352,14 +4364,15 @@ static void add_symbols_from_import(CodeGen *g, AstNode *src_use_node, AstNode *
43524364
43534365 assert(use_target_value->special != ConstValSpecialRuntime);
43544366
4355 ImportTableEntry *target_import = use_target_value->data.x_import;
4367 ZigType *target_import = use_target_value->data.x_type;
43564368 assert(target_import);
4369 assert(target_import->id == ZigTypeIdStruct);
43574370
4358 if (target_import->any_imports_failed) {
4359 dst_use_node->owner->any_imports_failed = true;
4371 if (get_container_scope(target_import)->any_imports_failed) {
4372 get_container_scope(dst_use_node->owner)->any_imports_failed = true;
43604373 }
43614374
4362 auto it = target_import->decls_scope->decl_table.entry_iterator();
4375 auto it = get_container_scope(target_import)->decl_table.entry_iterator();
43634376 for (;;) {
43644377 auto *entry = it.next();
43654378 if (!entry)
......@@ -4374,7 +4387,7 @@ static void add_symbols_from_import(CodeGen *g, AstNode *src_use_node, AstNode *
43744387
43754388 Buf *target_tld_name = entry->key;
43764389
4377 auto existing_entry = dst_use_node->owner->decls_scope->decl_table.put_unique(target_tld_name, target_tld);
4390 auto existing_entry = get_container_scope(dst_use_node->owner)->decl_table.put_unique(target_tld_name, target_tld);
43784391 if (existing_entry) {
43794392 Tld *existing_decl = existing_entry->value;
43804393 if (existing_decl != target_tld) {
......@@ -4387,8 +4400,8 @@ static void add_symbols_from_import(CodeGen *g, AstNode *src_use_node, AstNode *
43874400 }
43884401 }
43894402
4390 for (size_t i = 0; i < target_import->use_decls.length; i += 1) {
4391 AstNode *use_decl_node = target_import->use_decls.at(i);
4403 for (size_t i = 0; i < get_container_scope(target_import)->use_decls.length; i += 1) {
4404 AstNode *use_decl_node = get_container_scope(target_import)->use_decls.at(i);
43924405 if (use_decl_node->data.use.visib_mod != VisibModPrivate)
43934406 add_symbols_from_import(g, use_decl_node, dst_use_node);
43944407 }
......@@ -4415,16 +4428,16 @@ void preview_use_decl(CodeGen *g, AstNode *node) {
44154428 }
44164429
44174430 node->data.use.resolution = TldResolutionResolving;
4418 ConstExprValue *result = analyze_const_value(g, &node->owner->decls_scope->base,
4419 node->data.use.expr, g->builtin_types.entry_namespace, nullptr);
4431 ConstExprValue *result = analyze_const_value(g, &get_container_scope(node->owner)->base,
4432 node->data.use.expr, g->builtin_types.entry_type, nullptr);
44204433
44214434 if (type_is_invalid(result->type))
4422 node->owner->any_imports_failed = true;
4435 get_container_scope(node->owner)->any_imports_failed = true;
44234436
44244437 node->data.use.value = result;
44254438}
44264439
4427ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *resolved_path, Buf *source_code) {
4440ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Buf *source_code) {
44284441 if (g->verbose_tokenize) {
44294442 fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(resolved_path));
44304443 fprintf(stderr, "----------------\n");
......@@ -4452,32 +4465,34 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *r
44524465 fprintf(stderr, "------\n");
44534466 }
44544467
4455 ImportTableEntry *import_entry = allocate<ImportTableEntry>(1);
4456 import_entry->package = package;
4457 import_entry->source_code = source_code;
4458 import_entry->line_offsets = tokenization.line_offsets;
4459 import_entry->path = resolved_path;
4460
4461 import_entry->root = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color);
4462 assert(import_entry->root);
4463 if (g->verbose_ast) {
4464 ast_print(stderr, import_entry->root, 0);
4465 }
4466
44674468 Buf *src_dirname = buf_alloc();
44684469 Buf *src_basename = buf_alloc();
44694470 os_path_split(resolved_path, src_dirname, src_basename);
44704471
4471 import_entry->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
4472 Buf noextname = BUF_INIT;
4473 os_path_extname(src_basename, &noextname, nullptr);
4474 RootStruct *root_struct = allocate<RootStruct>(1);
4475 root_struct->package = package;
4476 root_struct->source_code = source_code;
4477 root_struct->line_offsets = tokenization.line_offsets;
4478 root_struct->path = resolved_path;
4479 root_struct->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
4480 ZigType *import_entry = get_root_container_type(g, buf_ptr(&noextname), root_struct);
4481
4482 AstNode *root_node = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color);
4483 assert(root_node != nullptr);
4484 assert(root_node->type == NodeTypeContainerDecl);
4485 import_entry->data.structure.decl_node = root_node;
4486 import_entry->data.structure.decls_scope->base.source_node = root_node;
4487 if (g->verbose_ast) {
4488 ast_print(stderr, root_node, 0);
4489 }
4490
44724491 g->import_table.put(resolved_path, import_entry);
44734492 g->import_queue.append(import_entry);
44744493
4475 import_entry->decls_scope = create_decls_scope(g, import_entry->root, nullptr, nullptr, import_entry);
4476
4477
4478 assert(import_entry->root->type == NodeTypeContainerDecl);
4479 for (size_t decl_i = 0; decl_i < import_entry->root->data.container_decl.decls.length; decl_i += 1) {
4480 AstNode *top_level_decl = import_entry->root->data.container_decl.decls.at(decl_i);
4494 for (size_t decl_i = 0; decl_i < root_node->data.container_decl.decls.length; decl_i += 1) {
4495 AstNode *top_level_decl = root_node->data.container_decl.decls.at(decl_i);
44814496
44824497 if (top_level_decl->type == NodeTypeFnDef) {
44834498 AstNode *proto_node = top_level_decl->data.fn_def.fn_proto;
......@@ -4502,16 +4517,16 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *r
45024517 return import_entry;
45034518}
45044519
4505void scan_import(CodeGen *g, ImportTableEntry *import) {
4506 if (!import->scanned) {
4507 import->scanned = true;
4508 scan_decls(g, import->decls_scope, import->root);
4520void scan_import(CodeGen *g, ZigType *import) {
4521 if (!import->data.structure.root_struct->scanned) {
4522 import->data.structure.root_struct->scanned = true;
4523 scan_decls(g, import->data.structure.decls_scope, import->data.structure.decl_node);
45094524 }
45104525}
45114526
45124527void semantic_analyze(CodeGen *g) {
45134528 for (; g->import_queue_index < g->import_queue.length; g->import_queue_index += 1) {
4514 ImportTableEntry *import = g->import_queue.at(g->import_queue_index);
4529 ZigType *import = g->import_queue.at(g->import_queue_index);
45154530 scan_import(g, import);
45164531 }
45174532
......@@ -4530,9 +4545,8 @@ void semantic_analyze(CodeGen *g) {
45304545 {
45314546 for (; g->resolve_queue_index < g->resolve_queue.length; g->resolve_queue_index += 1) {
45324547 Tld *tld = g->resolve_queue.at(g->resolve_queue_index);
4533 bool pointer_only = false;
45344548 AstNode *source_node = nullptr;
4535 resolve_top_level_decl(g, tld, pointer_only, source_node);
4549 resolve_top_level_decl(g, tld, source_node);
45364550 }
45374551
45384552 for (; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) {
......@@ -4613,7 +4627,6 @@ bool handle_is_ptr(ZigType *type_entry) {
46134627 case ZigTypeIdComptimeInt:
46144628 case ZigTypeIdUndefined:
46154629 case ZigTypeIdNull:
4616 case ZigTypeIdNamespace:
46174630 case ZigTypeIdBoundFn:
46184631 case ZigTypeIdArgTuple:
46194632 case ZigTypeIdOpaque:
......@@ -4880,8 +4893,6 @@ static uint32_t hash_const_val(ConstExprValue *const_val) {
48804893 return 3415065496;
48814894 case ZigTypeIdErrorSet:
48824895 return hash_const_val_error_set(const_val);
4883 case ZigTypeIdNamespace:
4884 return hash_ptr(const_val->data.x_import);
48854896 case ZigTypeIdVector:
48864897 // TODO better hashing algorithm
48874898 return 3647867726;
......@@ -4943,7 +4954,6 @@ static bool can_mutate_comptime_var_state(ConstExprValue *value) {
49434954 case ZigTypeIdComptimeInt:
49444955 case ZigTypeIdUndefined:
49454956 case ZigTypeIdNull:
4946 case ZigTypeIdNamespace:
49474957 case ZigTypeIdBoundFn:
49484958 case ZigTypeIdFn:
49494959 case ZigTypeIdOpaque:
......@@ -5013,7 +5023,6 @@ static bool return_type_is_cacheable(ZigType *return_type) {
50135023 case ZigTypeIdComptimeInt:
50145024 case ZigTypeIdUndefined:
50155025 case ZigTypeIdNull:
5016 case ZigTypeIdNamespace:
50175026 case ZigTypeIdBoundFn:
50185027 case ZigTypeIdFn:
50195028 case ZigTypeIdOpaque:
......@@ -5143,7 +5152,6 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
51435152 case ZigTypeIdComptimeFloat:
51445153 case ZigTypeIdComptimeInt:
51455154 case ZigTypeIdMetaType:
5146 case ZigTypeIdNamespace:
51475155 case ZigTypeIdBoundFn:
51485156 case ZigTypeIdArgTuple:
51495157 case ZigTypeIdOptional:
......@@ -5209,7 +5217,6 @@ ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry) {
52095217 case ZigTypeIdUndefined:
52105218 case ZigTypeIdNull:
52115219 case ZigTypeIdMetaType:
5212 case ZigTypeIdNamespace:
52135220 case ZigTypeIdBoundFn:
52145221 case ZigTypeIdArgTuple:
52155222 return ReqCompTimeYes;
......@@ -5797,8 +5804,6 @@ bool const_values_equal(CodeGen *g, ConstExprValue *a, ConstExprValue *b) {
57975804 }
57985805 case ZigTypeIdErrorUnion:
57995806 zig_panic("TODO");
5800 case ZigTypeIdNamespace:
5801 return a->data.x_import == b->data.x_import;
58025807 case ZigTypeIdArgTuple:
58035808 return a->data.x_arg_tuple.start_index == b->data.x_arg_tuple.start_index &&
58045809 a->data.x_arg_tuple.end_index == b->data.x_arg_tuple.end_index;
......@@ -6072,16 +6077,6 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
60726077 }
60736078 return;
60746079 }
6075 case ZigTypeIdNamespace:
6076 {
6077 ImportTableEntry *import = const_val->data.x_import;
6078 if (import->c_import_node) {
6079 buf_appendf(buf, "(namespace from C import)");
6080 } else {
6081 buf_appendf(buf, "(namespace: %s)", buf_ptr(import->path));
6082 }
6083 return;
6084 }
60856080 case ZigTypeIdBoundFn:
60866081 {
60876082 ZigFn *fn_entry = const_val->data.x_bound_fn.fn;
......@@ -6203,7 +6198,6 @@ uint32_t type_id_hash(TypeId x) {
62036198 case ZigTypeIdEnum:
62046199 case ZigTypeIdUnion:
62056200 case ZigTypeIdFn:
6206 case ZigTypeIdNamespace:
62076201 case ZigTypeIdBoundFn:
62086202 case ZigTypeIdArgTuple:
62096203 case ZigTypeIdPromise:
......@@ -6252,7 +6246,6 @@ bool type_id_eql(TypeId a, TypeId b) {
62526246 case ZigTypeIdEnum:
62536247 case ZigTypeIdUnion:
62546248 case ZigTypeIdFn:
6255 case ZigTypeIdNamespace:
62566249 case ZigTypeIdBoundFn:
62576250 case ZigTypeIdArgTuple:
62586251 case ZigTypeIdOpaque:
......@@ -6419,7 +6412,6 @@ static const ZigTypeId all_type_ids[] = {
64196412 ZigTypeIdEnum,
64206413 ZigTypeIdUnion,
64216414 ZigTypeIdFn,
6422 ZigTypeIdNamespace,
64236415 ZigTypeIdBoundFn,
64246416 ZigTypeIdArgTuple,
64256417 ZigTypeIdOpaque,
......@@ -6480,18 +6472,16 @@ size_t type_id_index(ZigType *entry) {
64806472 return 17;
64816473 case ZigTypeIdFn:
64826474 return 18;
6483 case ZigTypeIdNamespace:
6484 return 19;
64856475 case ZigTypeIdBoundFn:
6486 return 20;
6476 return 19;
64876477 case ZigTypeIdArgTuple:
6488 return 21;
6478 return 20;
64896479 case ZigTypeIdOpaque:
6490 return 22;
6480 return 21;
64916481 case ZigTypeIdPromise:
6492 return 23;
6482 return 22;
64936483 case ZigTypeIdVector:
6494 return 24;
6484 return 23;
64956485 }
64966486 zig_unreachable();
64976487}
......@@ -6538,8 +6528,6 @@ const char *type_id_name(ZigTypeId id) {
65386528 return "Union";
65396529 case ZigTypeIdFn:
65406530 return "Fn";
6541 case ZigTypeIdNamespace:
6542 return "Namespace";
65436531 case ZigTypeIdBoundFn:
65446532 return "BoundFn";
65456533 case ZigTypeIdArgTuple:
......@@ -6619,8 +6607,8 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b) {
66196607}
66206608
66216609ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name) {
6622 Tld *tld = codegen->compile_var_import->decls_scope->decl_table.get(buf_create_from_str(name));
6623 resolve_top_level_decl(codegen, tld, false, nullptr);
6610 Tld *tld = get_container_scope(codegen->compile_var_import)->decl_table.get(buf_create_from_str(name));
6611 resolve_top_level_decl(codegen, tld, nullptr);
66246612 assert(tld->id == TldIdVar);
66256613 TldVar *tld_var = (TldVar *)tld;
66266614 ConstExprValue *var_value = tld_var->var->const_value;
src/analyze.hpp+8-7
......@@ -12,7 +12,7 @@
1212
1313void semantic_analyze(CodeGen *g);
1414ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg);
15ErrorMsg *add_token_error(CodeGen *g, ImportTableEntry *owner, Token *token, Buf *msg);
15ErrorMsg *add_token_error(CodeGen *g, ZigType *owner, Token *token, Buf *msg);
1616ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, AstNode *node, Buf *msg);
1717ZigType *new_type_table_entry(ZigTypeId id);
1818ZigType *get_pointer_to_type(CodeGen *g, ZigType *child_type, bool is_const);
......@@ -30,6 +30,7 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size);
3030ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type);
3131ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind,
3232 AstNode *decl_node, const char *name, ContainerLayout layout);
33ZigType *get_root_container_type(CodeGen *g, const char *name, RootStruct *root_struct);
3334ZigType *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x);
3435ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payload_type);
3536ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry);
......@@ -46,12 +47,12 @@ bool type_allowed_in_extern(CodeGen *g, ZigType *type_entry);
4647bool ptr_allows_addr_zero(ZigType *ptr_type);
4748bool type_is_nonnull_ptr(ZigType *type);
4849
49ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *abs_full_path, Buf *source_code);
50ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *abs_full_path, Buf *source_code);
5051
5152
5253ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **crossed_fndef_scope);
5354Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);
54void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only, AstNode *source_node);
55void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node);
5556bool type_is_codegen_pointer(ZigType *type);
5657
5758ZigType *get_src_ptr_type(ZigType *type);
......@@ -77,11 +78,11 @@ bool is_array_ref(ZigType *type_entry);
7778bool is_container_ref(ZigType *type_entry);
7879bool is_valid_vector_elem_type(ZigType *elem_type);
7980void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node);
80void scan_import(CodeGen *g, ImportTableEntry *import);
81void scan_import(CodeGen *g, ZigType *import);
8182void preview_use_decl(CodeGen *g, AstNode *node);
8283void resolve_use_decl(CodeGen *g, AstNode *node);
8384ZigFn *scope_fn_entry(Scope *scope);
84ImportTableEntry *get_scope_import(Scope *scope);
85ZigType *get_scope_import(Scope *scope);
8586void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source_node, Scope *parent_scope);
8687ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name,
8788 bool is_const, ConstExprValue *init_value, Tld *src_tld, ZigType *var_type);
......@@ -109,7 +110,7 @@ ScopeCImport *create_cimport_scope(CodeGen *g, AstNode *node, Scope *parent);
109110ScopeLoop *create_loop_scope(CodeGen *g, AstNode *node, Scope *parent);
110111ScopeSuspend *create_suspend_scope(CodeGen *g, AstNode *node, Scope *parent);
111112ScopeFnDef *create_fndef_scope(CodeGen *g, AstNode *node, Scope *parent, ZigFn *fn_entry);
112ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type, ImportTableEntry *import);
113ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type, ZigType *import);
113114Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent);
114115Scope *create_coro_prelude_scope(CodeGen *g, AstNode *node, Scope *parent);
115116Scope *create_runtime_scope(CodeGen *g, AstNode *node, Scope *parent, IrInstruction *is_comptime);
......@@ -186,7 +187,7 @@ LinkLib *add_link_lib(CodeGen *codegen, Buf *lib);
186187
187188uint32_t get_abi_alignment(CodeGen *g, ZigType *type_entry);
188189ZigType *get_align_amt_type(CodeGen *g);
189PackageTableEntry *new_anonymous_package(void);
190ZigPackage *new_anonymous_package(void);
190191
191192Buf *const_value_to_buffer(ConstExprValue *const_val);
192193void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, Buf *symbol_name, GlobalLinkageId linkage, bool ccc);
src/codegen.cpp+33-41
......@@ -48,15 +48,15 @@ static void init_darwin_native(CodeGen *g) {
4848 }
4949}
5050
51static PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_path) {
52 PackageTableEntry *entry = allocate<PackageTableEntry>(1);
51static ZigPackage *new_package(const char *root_src_dir, const char *root_src_path) {
52 ZigPackage *entry = allocate<ZigPackage>(1);
5353 entry->package_table.init(4);
5454 buf_init_from_str(&entry->root_src_dir, root_src_dir);
5555 buf_init_from_str(&entry->root_src_path, root_src_path);
5656 return entry;
5757}
5858
59PackageTableEntry *new_anonymous_package(void) {
59ZigPackage *new_anonymous_package(void) {
6060 return new_package("", "");
6161}
6262
......@@ -621,7 +621,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
621621 if (scope->di_scope)
622622 return scope->di_scope;
623623
624 ImportTableEntry *import = get_scope_import(scope);
624 ZigType *import = get_scope_import(scope);
625625 switch (scope->id) {
626626 case ScopeIdCImport:
627627 zig_unreachable();
......@@ -644,7 +644,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
644644 assert(fn_di_scope != nullptr);
645645 ZigLLVMDISubprogram *subprogram = ZigLLVMCreateFunction(g->dbuilder,
646646 fn_di_scope, buf_ptr(&fn_table_entry->symbol_name), "",
647 import->di_file, line_number,
647 import->data.structure.root_struct->di_file, line_number,
648648 fn_table_entry->type_entry->data.fn.raw_di_type, is_internal_linkage,
649649 is_definition, scope_line, flags, is_optimized, nullptr);
650650
......@@ -658,7 +658,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
658658 assert(decls_scope->container_type);
659659 scope->di_scope = ZigLLVMTypeToScope(decls_scope->container_type->di_type);
660660 } else {
661 scope->di_scope = ZigLLVMFileToScope(import->di_file);
661 scope->di_scope = ZigLLVMFileToScope(import->data.structure.root_struct->di_file);
662662 }
663663 return scope->di_scope;
664664 case ScopeIdBlock:
......@@ -668,7 +668,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
668668 assert(scope->parent);
669669 ZigLLVMDILexicalBlock *di_block = ZigLLVMCreateLexicalBlock(g->dbuilder,
670670 get_di_scope(g, scope->parent),
671 import->di_file,
671 import->data.structure.root_struct->di_file,
672672 (unsigned)scope->source_node->line + 1,
673673 (unsigned)scope->source_node->column + 1);
674674 scope->di_scope = ZigLLVMLexicalBlockToScope(di_block);
......@@ -2196,7 +2196,7 @@ var_ok:
21962196 if (dest_ty != nullptr && var->decl_node) {
21972197 // arg index + 1 because the 0 index is return value
21982198 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
2199 buf_ptr(&var->name), fn_walk->data.vars.import->di_file,
2199 buf_ptr(&var->name), fn_walk->data.vars.import->data.structure.root_struct->di_file,
22002200 (unsigned)(var->decl_node->line + 1),
22012201 dest_ty->di_type, !g->strip_debug_symbols, 0, di_arg_index + 1);
22022202 }
......@@ -5800,7 +5800,6 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
58005800 case ZigTypeIdNull:
58015801 case ZigTypeIdErrorUnion:
58025802 case ZigTypeIdErrorSet:
5803 case ZigTypeIdNamespace:
58045803 case ZigTypeIdBoundFn:
58055804 case ZigTypeIdArgTuple:
58065805 case ZigTypeIdVoid:
......@@ -6400,7 +6399,6 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
64006399 case ZigTypeIdComptimeInt:
64016400 case ZigTypeIdUndefined:
64026401 case ZigTypeIdNull:
6403 case ZigTypeIdNamespace:
64046402 case ZigTypeIdBoundFn:
64056403 case ZigTypeIdArgTuple:
64066404 case ZigTypeIdOpaque:
......@@ -6506,12 +6504,12 @@ static void gen_global_var(CodeGen *g, ZigVar *var, LLVMValueRef init_val,
65066504 assert(var->gen_is_const);
65076505 assert(type_entry);
65086506
6509 ImportTableEntry *import = get_scope_import(var->parent_scope);
6507 ZigType *import = get_scope_import(var->parent_scope);
65106508 assert(import);
65116509
65126510 bool is_local_to_unit = true;
65136511 ZigLLVMCreateGlobalVariable(g->dbuilder, get_di_scope(g, var->parent_scope), buf_ptr(&var->name),
6514 buf_ptr(&var->name), import->di_file,
6512 buf_ptr(&var->name), import->data.structure.root_struct->di_file,
65156513 (unsigned)(var->decl_node->line + 1),
65166514 type_entry->di_type, is_local_to_unit);
65176515
......@@ -6767,7 +6765,7 @@ static void do_code_gen(CodeGen *g) {
67676765 *slot = build_alloca(g, slot_type, "", alignment_bytes);
67686766 }
67696767
6770 ImportTableEntry *import = get_scope_import(&fn_table_entry->fndef_scope->base);
6768 ZigType *import = get_scope_import(&fn_table_entry->fndef_scope->base);
67716769
67726770 unsigned gen_i_init = want_first_arg_sret(g, fn_type_id) ? 1 : 0;
67736771
......@@ -6799,7 +6797,7 @@ static void do_code_gen(CodeGen *g) {
67996797 var->value_ref = build_alloca(g, var->var_type, buf_ptr(&var->name), var->align_bytes);
68006798
68016799 var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
6802 buf_ptr(&var->name), import->di_file, (unsigned)(var->decl_node->line + 1),
6800 buf_ptr(&var->name), import->data.structure.root_struct->di_file, (unsigned)(var->decl_node->line + 1),
68036801 var->var_type->di_type, !g->strip_debug_symbols, 0);
68046802
68056803 } else if (is_c_abi) {
......@@ -6823,7 +6821,7 @@ static void do_code_gen(CodeGen *g) {
68236821 }
68246822 if (var->decl_node) {
68256823 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
6826 buf_ptr(&var->name), import->di_file,
6824 buf_ptr(&var->name), import->data.structure.root_struct->di_file,
68276825 (unsigned)(var->decl_node->line + 1),
68286826 gen_type->di_type, !g->strip_debug_symbols, 0, (unsigned)(var->gen_arg_index + 1));
68296827 }
......@@ -6971,12 +6969,6 @@ static void define_builtin_types(CodeGen *g) {
69716969 entry->zero_bits = true;
69726970 g->builtin_types.entry_invalid = entry;
69736971 }
6974 {
6975 ZigType *entry = new_type_table_entry(ZigTypeIdNamespace);
6976 buf_init_from_str(&entry->name, "(namespace)");
6977 entry->zero_bits = true;
6978 g->builtin_types.entry_namespace = entry;
6979 }
69806972 {
69816973 ZigType *entry = new_type_table_entry(ZigTypeIdComptimeFloat);
69826974 buf_init_from_str(&entry->name, "comptime_float");
......@@ -7470,7 +7462,6 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
74707462 " Enum: Enum,\n"
74717463 " Union: Union,\n"
74727464 " Fn: Fn,\n"
7473 " Namespace: void,\n"
74747465 " BoundFn: Fn,\n"
74757466 " ArgTuple: void,\n"
74767467 " Opaque: void,\n"
......@@ -7948,17 +7939,21 @@ void codegen_translate_c(CodeGen *g, Buf *full_path) {
79487939 Buf *src_dirname = buf_alloc();
79497940 os_path_split(full_path, src_dirname, src_basename);
79507941
7951 ImportTableEntry *import = allocate<ImportTableEntry>(1);
7952 import->source_code = nullptr;
7953 import->path = full_path;
7942 Buf noextname = BUF_INIT;
7943 os_path_extname(src_basename, &noextname, nullptr);
7944
7945 RootStruct *root_struct = allocate<RootStruct>(1);
7946 root_struct->source_code = nullptr;
7947 root_struct->path = full_path;
7948 root_struct->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
7949
7950 ZigType *import = get_root_container_type(g, buf_ptr(&noextname), root_struct);
79547951 g->root_import = import;
7955 import->decls_scope = create_decls_scope(g, nullptr, nullptr, nullptr, import);
79567952
79577953 detect_libc(g);
79587954
79597955 init(g);
79607956
7961 import->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
79627957
79637958 ZigList<ErrorMsg *> errors = {0};
79647959 Error err = parse_h_file(import, &errors, buf_ptr(full_path), g, nullptr);
......@@ -7977,7 +7972,7 @@ void codegen_translate_c(CodeGen *g, Buf *full_path) {
79777972 }
79787973}
79797974
7980static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package, const char *basename) {
7975static ZigType *add_special_code(CodeGen *g, ZigPackage *package, const char *basename) {
79817976 Buf *code_basename = buf_create_from_str(basename);
79827977 Buf path_to_code_src = BUF_INIT;
79837978 os_path_join(g->zig_std_special_dir, code_basename, &path_to_code_src);
......@@ -7994,17 +7989,17 @@ static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package
79947989 return add_source_file(g, package, resolved_path, import_code);
79957990}
79967991
7997static PackageTableEntry *create_bootstrap_pkg(CodeGen *g, PackageTableEntry *pkg_with_main) {
7998 PackageTableEntry *package = codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "bootstrap.zig");
7992static ZigPackage *create_bootstrap_pkg(CodeGen *g, ZigPackage *pkg_with_main) {
7993 ZigPackage *package = codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "bootstrap.zig");
79997994 package->package_table.put(buf_create_from_str("@root"), pkg_with_main);
80007995 return package;
80017996}
80027997
8003static PackageTableEntry *create_test_runner_pkg(CodeGen *g) {
7998static ZigPackage *create_test_runner_pkg(CodeGen *g) {
80047999 return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "test_runner.zig");
80058000}
80068001
8007static PackageTableEntry *create_panic_pkg(CodeGen *g) {
8002static ZigPackage *create_panic_pkg(CodeGen *g) {
80088003 return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "panic.zig");
80098004}
80108005
......@@ -8096,7 +8091,7 @@ static void gen_root_source(CodeGen *g) {
80968091
80978092 {
80988093 // Zig has lazy top level definitions. Here we semantically analyze the panic function.
8099 ImportTableEntry *import_with_panic;
8094 ZigType *import_with_panic;
81008095 if (g->have_pub_panic) {
81018096 import_with_panic = g->root_import;
81028097 } else {
......@@ -8104,9 +8099,9 @@ static void gen_root_source(CodeGen *g) {
81048099 import_with_panic = add_special_code(g, g->panic_package, "panic.zig");
81058100 }
81068101 scan_import(g, import_with_panic);
8107 Tld *panic_tld = find_decl(g, &import_with_panic->decls_scope->base, buf_create_from_str("panic"));
8102 Tld *panic_tld = find_decl(g, &get_container_scope(import_with_panic)->base, buf_create_from_str("panic"));
81088103 assert(panic_tld != nullptr);
8109 resolve_top_level_decl(g, panic_tld, false, nullptr);
8104 resolve_top_level_decl(g, panic_tld, nullptr);
81108105 }
81118106
81128107
......@@ -8341,7 +8336,6 @@ static void prepend_c_type_to_decl_list(CodeGen *g, GenH *gen_h, ZigType *type_e
83418336 case ZigTypeIdComptimeInt:
83428337 case ZigTypeIdUndefined:
83438338 case ZigTypeIdNull:
8344 case ZigTypeIdNamespace:
83458339 case ZigTypeIdBoundFn:
83468340 case ZigTypeIdArgTuple:
83478341 case ZigTypeIdErrorUnion:
......@@ -8524,7 +8518,6 @@ static void get_c_type(CodeGen *g, GenH *gen_h, ZigType *type_entry, Buf *out_bu
85248518 case ZigTypeIdInvalid:
85258519 case ZigTypeIdMetaType:
85268520 case ZigTypeIdBoundFn:
8527 case ZigTypeIdNamespace:
85288521 case ZigTypeIdComptimeFloat:
85298522 case ZigTypeIdComptimeInt:
85308523 case ZigTypeIdUndefined:
......@@ -8676,7 +8669,6 @@ static void gen_h_file(CodeGen *g) {
86768669 case ZigTypeIdNull:
86778670 case ZigTypeIdErrorUnion:
86788671 case ZigTypeIdErrorSet:
8679 case ZigTypeIdNamespace:
86808672 case ZigTypeIdBoundFn:
86818673 case ZigTypeIdArgTuple:
86828674 case ZigTypeIdOptional:
......@@ -8775,7 +8767,7 @@ void codegen_add_time_event(CodeGen *g, const char *name) {
87758767 g->timing_events.append({os_get_time(), name});
87768768}
87778769
8778static void add_cache_pkg(CodeGen *g, CacheHash *ch, PackageTableEntry *pkg) {
8770static void add_cache_pkg(CodeGen *g, CacheHash *ch, ZigPackage *pkg) {
87798771 if (buf_len(&pkg->root_src_path) == 0)
87808772 return;
87818773
......@@ -9029,9 +9021,9 @@ void codegen_build_and_link(CodeGen *g) {
90299021 codegen_add_time_event(g, "Done");
90309022}
90319023
9032PackageTableEntry *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path) {
9024ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path) {
90339025 init(g);
9034 PackageTableEntry *pkg = new_package(root_src_dir, root_src_path);
9026 ZigPackage *pkg = new_package(root_src_dir, root_src_path);
90359027 if (g->std_package != nullptr) {
90369028 assert(g->compile_var_package != nullptr);
90379029 pkg->package_table.put(buf_create_from_str("std"), g->std_package);
src/codegen.hpp+1-1
......@@ -48,7 +48,7 @@ void codegen_print_timing_report(CodeGen *g, FILE *f);
4848void codegen_link(CodeGen *g);
4949void codegen_build_and_link(CodeGen *g);
5050
51PackageTableEntry *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path);
51ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path);
5252void codegen_add_assembly(CodeGen *g, Buf *path);
5353void codegen_add_object(CodeGen *g, Buf *object_path);
5454
src/ir.cpp+34-84
......@@ -258,7 +258,6 @@ static bool types_have_same_zig_comptime_repr(ZigType *a, ZigType *b) {
258258 case ZigTypeIdPointer:
259259 case ZigTypeIdUndefined:
260260 case ZigTypeIdNull:
261 case ZigTypeIdNamespace:
262261 case ZigTypeIdBoundFn:
263262 case ZigTypeIdErrorSet:
264263 case ZigTypeIdOpaque:
......@@ -1123,11 +1122,11 @@ static IrInstruction *ir_create_const_fn(IrBuilder *irb, Scope *scope, AstNode *
11231122 return &const_instruction->base;
11241123}
11251124
1126static IrInstruction *ir_build_const_import(IrBuilder *irb, Scope *scope, AstNode *source_node, ImportTableEntry *import) {
1125static IrInstruction *ir_build_const_import(IrBuilder *irb, Scope *scope, AstNode *source_node, ZigType *import) {
11271126 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
1128 const_instruction->base.value.type = irb->codegen->builtin_types.entry_namespace;
1127 const_instruction->base.value.type = irb->codegen->builtin_types.entry_type;
11291128 const_instruction->base.value.special = ConstValSpecialStatic;
1130 const_instruction->base.value.data.x_import = import;
1129 const_instruction->base.value.data.x_type = import;
11311130 return &const_instruction->base;
11321131}
11331132
......@@ -3824,7 +3823,7 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,
38243823 if (tld)
38253824 return ir_build_decl_ref(irb, scope, node, tld, lval);
38263825
3827 if (node->owner->any_imports_failed) {
3826 if (get_container_scope(node->owner)->any_imports_failed) {
38283827 // skip the error message since we had a failing import in this file
38293828 // if an import breaks we don't need redundant undeclared identifier errors
38303829 return irb->codegen->invalid_instruction;
......@@ -6620,9 +6619,12 @@ static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char
66206619 buf_appendf(name, ")");
66216620 return name;
66226621 } else {
6623 //Note: C-imports do not have valid location information
6622 // Note: C-imports do not have valid location information
6623 // TODO this will get fixed by https://github.com/ziglang/zig/issues/2015
66246624 return buf_sprintf("(anonymous %s at %s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize ")", kind_name,
6625 (source_node->owner->path != nullptr) ? buf_ptr(source_node->owner->path) : "(null)", source_node->line + 1, source_node->column + 1);
6625 (source_node->owner->data.structure.root_struct->path != nullptr) ?
6626 buf_ptr(source_node->owner->data.structure.root_struct->path) :
6627 "(null)", source_node->line + 1, source_node->column + 1);
66266628 }
66276629}
66286630
......@@ -12065,7 +12067,6 @@ static IrInstruction *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *
1206512067 case ZigTypeIdErrorSet:
1206612068 case ZigTypeIdFn:
1206712069 case ZigTypeIdOpaque:
12068 case ZigTypeIdNamespace:
1206912070 case ZigTypeIdBoundFn:
1207012071 case ZigTypeIdArgTuple:
1207112072 case ZigTypeIdPromise:
......@@ -13407,7 +13408,6 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio
1340713408 case ZigTypeIdOptional:
1340813409 case ZigTypeIdErrorUnion:
1340913410 case ZigTypeIdErrorSet:
13410 case ZigTypeIdNamespace:
1341113411 case ZigTypeIdBoundFn:
1341213412 case ZigTypeIdArgTuple:
1341313413 case ZigTypeIdOpaque:
......@@ -13432,7 +13432,6 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio
1343213432 case ZigTypeIdErrorSet:
1343313433 case ZigTypeIdVector:
1343413434 zig_panic("TODO export const value of type %s", buf_ptr(&target->value.type->name));
13435 case ZigTypeIdNamespace:
1343613435 case ZigTypeIdBoundFn:
1343713436 case ZigTypeIdArgTuple:
1343813437 case ZigTypeIdOpaque:
......@@ -14616,7 +14615,6 @@ static IrInstruction *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op_
1461614615 case ZigTypeIdEnum:
1461714616 case ZigTypeIdUnion:
1461814617 case ZigTypeIdFn:
14619 case ZigTypeIdNamespace:
1462014618 case ZigTypeIdBoundFn:
1462114619 case ZigTypeIdArgTuple:
1462214620 case ZigTypeIdPromise:
......@@ -15368,7 +15366,7 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
1536815366 auto entry = container_scope->decl_table.maybe_get(field_name);
1536915367 Tld *tld = entry ? entry->value : nullptr;
1537015368 if (tld && tld->id == TldIdFn) {
15371 resolve_top_level_decl(ira->codegen, tld, false, source_instr->source_node);
15369 resolve_top_level_decl(ira->codegen, tld, source_instr->source_node);
1537215370 if (tld->resolution == TldResolutionInvalid)
1537315371 return ira->codegen->invalid_instruction;
1537415372 TldFn *tld_fn = (TldFn *)tld;
......@@ -15557,8 +15555,7 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name,
1555715555
1555815556
1555915557static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {
15560 bool pointer_only = false;
15561 resolve_top_level_decl(ira->codegen, tld, pointer_only, source_instruction->source_node);
15558 resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node);
1556215559 if (tld->resolution == TldResolutionInvalid)
1556315560 return ira->codegen->invalid_instruction;
1556415561
......@@ -15971,37 +15968,6 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1597115968 buf_sprintf("type '%s' does not support field access", buf_ptr(&child_type->name)));
1597215969 return ira->codegen->invalid_instruction;
1597315970 }
15974 } else if (container_type->id == ZigTypeIdNamespace) {
15975 assert(container_ptr->value.type->id == ZigTypeIdPointer);
15976 ConstExprValue *container_ptr_val = ir_resolve_const(ira, container_ptr, UndefBad);
15977 if (!container_ptr_val)
15978 return ira->codegen->invalid_instruction;
15979
15980 ConstExprValue *namespace_val = const_ptr_pointee(ira, ira->codegen, container_ptr_val,
15981 field_ptr_instruction->base.source_node);
15982 if (namespace_val == nullptr)
15983 return ira->codegen->invalid_instruction;
15984 assert(namespace_val->special == ConstValSpecialStatic);
15985
15986 ImportTableEntry *namespace_import = namespace_val->data.x_import;
15987
15988 Tld *tld = find_decl(ira->codegen, &namespace_import->decls_scope->base, field_name);
15989 if (tld) {
15990 if (tld->visib_mod == VisibModPrivate &&
15991 tld->import != source_node->owner)
15992 {
15993 ErrorMsg *msg = ir_add_error_node(ira, source_node,
15994 buf_sprintf("'%s' is private", buf_ptr(field_name)));
15995 add_error_note(ira->codegen, msg, tld->source_node, buf_sprintf("declared here"));
15996 return ira->codegen->invalid_instruction;
15997 }
15998 return ir_analyze_decl_ref(ira, &field_ptr_instruction->base, tld);
15999 } else {
16000 const char *import_name = namespace_import->path ? buf_ptr(namespace_import->path) : "(C import)";
16001 ir_add_error_node(ira, source_node,
16002 buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name), import_name));
16003 return ira->codegen->invalid_instruction;
16004 }
1600515971 } else {
1600615972 ir_add_error_node(ira, field_ptr_instruction->base.source_node,
1600715973 buf_sprintf("type '%s' does not support field access", buf_ptr(&container_type->name)));
......@@ -16281,7 +16247,6 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1628116247 case ZigTypeIdEnum:
1628216248 case ZigTypeIdUnion:
1628316249 case ZigTypeIdFn:
16284 case ZigTypeIdNamespace:
1628516250 case ZigTypeIdBoundFn:
1628616251 case ZigTypeIdPromise:
1628716252 case ZigTypeIdVector:
......@@ -16402,7 +16367,6 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira,
1640216367 case ZigTypeIdEnum:
1640316368 case ZigTypeIdUnion:
1640416369 case ZigTypeIdFn:
16405 case ZigTypeIdNamespace:
1640616370 case ZigTypeIdBoundFn:
1640716371 case ZigTypeIdPromise:
1640816372 case ZigTypeIdVector:
......@@ -16452,7 +16416,6 @@ static IrInstruction *ir_analyze_instruction_size_of(IrAnalyze *ira,
1645216416 case ZigTypeIdComptimeInt:
1645316417 case ZigTypeIdBoundFn:
1645416418 case ZigTypeIdMetaType:
16455 case ZigTypeIdNamespace:
1645616419 case ZigTypeIdArgTuple:
1645716420 case ZigTypeIdOpaque:
1645816421 ir_add_error_node(ira, size_of_instruction->base.source_node,
......@@ -16860,7 +16823,6 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1686016823 case ZigTypeIdPointer:
1686116824 case ZigTypeIdPromise:
1686216825 case ZigTypeIdFn:
16863 case ZigTypeIdNamespace:
1686416826 case ZigTypeIdErrorSet: {
1686516827 if (pointee_val) {
1686616828 IrInstruction *result = ir_const(ira, &switch_target_instruction->base, nullptr);
......@@ -17009,25 +16971,25 @@ static IrInstruction *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructio
1700916971 return ira->codegen->invalid_instruction;
1701016972
1701116973 AstNode *source_node = import_instruction->base.source_node;
17012 ImportTableEntry *import = source_node->owner;
16974 ZigType *import = source_node->owner;
1701316975
1701416976 Buf *import_target_path;
1701516977 Buf *search_dir;
17016 assert(import->package);
17017 PackageTableEntry *target_package;
17018 auto package_entry = import->package->package_table.maybe_get(import_target_str);
16978 assert(import->data.structure.root_struct->package);
16979 ZigPackage *target_package;
16980 auto package_entry = import->data.structure.root_struct->package->package_table.maybe_get(import_target_str);
1701916981 if (package_entry) {
1702016982 target_package = package_entry->value;
1702116983 import_target_path = &target_package->root_src_path;
1702216984 search_dir = &target_package->root_src_dir;
1702316985 } else {
1702416986 // try it as a filename
17025 target_package = import->package;
16987 target_package = import->data.structure.root_struct->package;
1702616988 import_target_path = import_target_str;
1702716989
1702816990 // search relative to importing file
1702916991 search_dir = buf_alloc();
17030 os_path_dirname(import->path, search_dir);
16992 os_path_dirname(import->data.structure.root_struct->path, search_dir);
1703116993 }
1703216994
1703316995 Buf full_path = BUF_INIT;
......@@ -17041,10 +17003,7 @@ static IrInstruction *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructio
1704117003
1704217004 auto import_entry = ira->codegen->import_table.maybe_get(resolved_path);
1704317005 if (import_entry) {
17044 IrInstruction *result = ir_const(ira, &import_instruction->base,
17045 ira->codegen->builtin_types.entry_namespace);
17046 result->value.data.x_import = import_entry->value;
17047 return result;
17006 return ir_const_type(ira, &import_instruction->base, import_entry->value);
1704817007 }
1704917008
1705017009 if ((err = file_fetch(ira->codegen, resolved_path, import_code))) {
......@@ -17059,13 +17018,11 @@ static IrInstruction *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructio
1705917018 }
1706017019 }
1706117020
17062 ImportTableEntry *target_import = add_source_file(ira->codegen, target_package, resolved_path, import_code);
17021 ZigType *target_import = add_source_file(ira->codegen, target_package, resolved_path, import_code);
1706317022
1706417023 scan_import(ira->codegen, target_import);
1706517024
17066 IrInstruction *result = ir_const(ira, &import_instruction->base, ira->codegen->builtin_types.entry_namespace);
17067 result->value.data.x_import = target_import;
17068 return result;
17025 return ir_const_type(ira, &import_instruction->base, target_import);
1706917026}
1707017027
1707117028static IrInstruction *ir_analyze_instruction_ref(IrAnalyze *ira, IrInstructionRef *ref_instruction) {
......@@ -17741,7 +17698,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Sco
1774117698 while ((curr_entry = decl_it.next()) != nullptr) {
1774217699 // If the definition is unresolved, force it to be resolved again.
1774317700 if (curr_entry->value->resolution == TldResolutionUnresolved) {
17744 resolve_top_level_decl(ira->codegen, curr_entry->value, false, curr_entry->value->source_node);
17701 resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node);
1774517702 if (curr_entry->value->resolution != TldResolutionOk) {
1774617703 return ErrorSemanticAnalyzeFail;
1774717704 }
......@@ -18056,7 +18013,6 @@ static Error ir_make_type_info_value(IrAnalyze *ira, AstNode *source_node, ZigTy
1805618013 case ZigTypeIdComptimeInt:
1805718014 case ZigTypeIdUndefined:
1805818015 case ZigTypeIdNull:
18059 case ZigTypeIdNamespace:
1806018016 case ZigTypeIdArgTuple:
1806118017 case ZigTypeIdOpaque:
1806218018 *out = nullptr;
......@@ -18702,14 +18658,14 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
1870218658 if (type_is_invalid(cimport_result->type))
1870318659 return ira->codegen->invalid_instruction;
1870418660
18705 ImportTableEntry *child_import = allocate<ImportTableEntry>(1);
18706 child_import->decls_scope = create_decls_scope(ira->codegen, node, nullptr, nullptr, child_import);
18707 child_import->c_import_node = node;
18708 child_import->package = new_anonymous_package();
18709 child_import->package->package_table.put(buf_create_from_str("builtin"), ira->codegen->compile_var_package);
18710 child_import->package->package_table.put(buf_create_from_str("std"), ira->codegen->std_package);
18711 child_import->di_file = ZigLLVMCreateFile(ira->codegen->dbuilder,
18661 RootStruct *root_struct = allocate<RootStruct>(1);
18662 root_struct->package = new_anonymous_package();
18663 root_struct->package->package_table.put(buf_create_from_str("builtin"), ira->codegen->compile_var_package);
18664 root_struct->package->package_table.put(buf_create_from_str("std"), ira->codegen->std_package);
18665 root_struct->c_import_node = node;
18666 root_struct->di_file = ZigLLVMCreateFile(ira->codegen->dbuilder,
1871218667 buf_ptr(buf_create_from_str("cimport.h")), buf_ptr(buf_create_from_str(".")));
18668 ZigType *child_import = get_root_container_type(ira->codegen, "cimport", root_struct);
1871318669
1871418670 ZigList<ErrorMsg *> errors = {0};
1871518671
......@@ -18738,14 +18694,12 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
1873818694 if (ira->codegen->verbose_cimport) {
1873918695 fprintf(stderr, "\nC imports:\n");
1874018696 fprintf(stderr, "-----------\n");
18741 ast_render(ira->codegen, stderr, child_import->root, 4);
18697 ast_render(ira->codegen, stderr, child_import->data.structure.decl_node, 4);
1874218698 }
1874318699
18744 scan_decls(ira->codegen, child_import->decls_scope, child_import->root);
18700 scan_decls(ira->codegen, get_container_scope(child_import), child_import->data.structure.decl_node);
1874518701
18746 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_namespace);
18747 result->value.data.x_import = child_import;
18748 return result;
18702 return ir_const_type(ira, &instruction->base, child_import);
1874918703}
1875018704
1875118705static IrInstruction *ir_analyze_instruction_c_include(IrAnalyze *ira, IrInstructionCInclude *instruction) {
......@@ -18819,10 +18773,10 @@ static IrInstruction *ir_analyze_instruction_embed_file(IrAnalyze *ira, IrInstru
1881918773 if (!rel_file_path)
1882018774 return ira->codegen->invalid_instruction;
1882118775
18822 ImportTableEntry *import = get_scope_import(instruction->base.scope);
18776 ZigType *import = get_scope_import(instruction->base.scope);
1882318777 // figure out absolute path to resource
1882418778 Buf source_dir_path = BUF_INIT;
18825 os_path_dirname(import->path, &source_dir_path);
18779 os_path_dirname(import->data.structure.root_struct->path, &source_dir_path);
1882618780
1882718781 Buf *resolve_paths[] = {
1882818782 &source_dir_path,
......@@ -20179,7 +20133,6 @@ static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruct
2017920133 case ZigTypeIdComptimeInt:
2018020134 case ZigTypeIdUndefined:
2018120135 case ZigTypeIdNull:
20182 case ZigTypeIdNamespace:
2018320136 case ZigTypeIdBoundFn:
2018420137 case ZigTypeIdArgTuple:
2018520138 case ZigTypeIdVoid:
......@@ -21025,7 +20978,6 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
2102520978 case ZigTypeIdOpaque:
2102620979 case ZigTypeIdBoundFn:
2102720980 case ZigTypeIdArgTuple:
21028 case ZigTypeIdNamespace:
2102920981 case ZigTypeIdUnreachable:
2103020982 case ZigTypeIdComptimeFloat:
2103120983 case ZigTypeIdComptimeInt:
......@@ -21185,7 +21137,6 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
2118521137 case ZigTypeIdOpaque:
2118621138 case ZigTypeIdBoundFn:
2118721139 case ZigTypeIdArgTuple:
21188 case ZigTypeIdNamespace:
2118921140 case ZigTypeIdUnreachable:
2119021141 case ZigTypeIdComptimeFloat:
2119121142 case ZigTypeIdComptimeInt:
......@@ -21343,7 +21294,6 @@ static bool type_can_bit_cast(ZigType *t) {
2134321294 case ZigTypeIdOpaque:
2134421295 case ZigTypeIdBoundFn:
2134521296 case ZigTypeIdArgTuple:
21346 case ZigTypeIdNamespace:
2134721297 case ZigTypeIdUnreachable:
2134821298 case ZigTypeIdComptimeFloat:
2134921299 case ZigTypeIdComptimeInt:
......@@ -21516,7 +21466,7 @@ static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,
2151621466 Tld *tld = instruction->tld;
2151721467 LVal lval = instruction->lval;
2151821468
21519 resolve_top_level_decl(ira->codegen, tld, lval == LValPtr, instruction->base.source_node);
21469 resolve_top_level_decl(ira->codegen, tld, instruction->base.source_node);
2152021470 if (tld->resolution == TldResolutionInvalid)
2152121471 return ira->codegen->invalid_instruction;
2152221472
src/main.cpp+5-5
......@@ -215,7 +215,7 @@ struct CliPkg {
215215 CliPkg *parent;
216216};
217217
218static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) {
218static void add_package(CodeGen *g, CliPkg *cli_pkg, ZigPackage *pkg) {
219219 for (size_t i = 0; i < cli_pkg->children.length; i += 1) {
220220 CliPkg *child_cli_pkg = cli_pkg->children.at(i);
221221
......@@ -223,10 +223,10 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) {
223223 Buf *basename = buf_alloc();
224224 os_path_split(buf_create_from_str(child_cli_pkg->path), dirname, basename);
225225
226 PackageTableEntry *child_pkg = codegen_create_package(g, buf_ptr(dirname), buf_ptr(basename));
226 ZigPackage *child_pkg = codegen_create_package(g, buf_ptr(dirname), buf_ptr(basename));
227227 auto entry = pkg->package_table.put_unique(buf_create_from_str(child_cli_pkg->name), child_pkg);
228228 if (entry) {
229 PackageTableEntry *existing_pkg = entry->value;
229 ZigPackage *existing_pkg = entry->value;
230230 Buf *full_path = buf_alloc();
231231 os_path_join(&existing_pkg->root_src_dir, &existing_pkg->root_src_path, full_path);
232232 fprintf(stderr, "Unable to add package '%s'->'%s': already exists as '%s'\n",
......@@ -543,7 +543,7 @@ int main(int argc, char **argv) {
543543 return EXIT_FAILURE;
544544 }
545545
546 PackageTableEntry *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname),
546 ZigPackage *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname),
547547 buf_ptr(&build_file_basename));
548548 g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg);
549549 g->enable_cache = get_cache_opt(enable_cache, true);
......@@ -1145,7 +1145,7 @@ int main(int argc, char **argv) {
11451145 }
11461146 } else if (cmd == CmdTranslateC) {
11471147 codegen_translate_c(g, in_file_buf);
1148 ast_render(g, stdout, g->root_import->root, 4);
1148 ast_render(g, stdout, g->root_import->data.structure.decl_node, 4);
11491149 if (timing_info)
11501150 codegen_print_timing_report(g, stdout);
11511151 return EXIT_SUCCESS;
src/parser.cpp+10-8
......@@ -18,7 +18,7 @@ struct ParseContext {
1818 Buf *buf;
1919 size_t current_token;
2020 ZigList<Token> *tokens;
21 ImportTableEntry *owner;
21 ZigType *owner;
2222 ErrColor err_color;
2323};
2424
......@@ -130,8 +130,10 @@ static void ast_error(ParseContext *pc, Token *token, const char *format, ...) {
130130 va_end(ap);
131131
132132
133 ErrorMsg *err = err_msg_create_with_line(pc->owner->path, token->start_line, token->start_column,
134 pc->owner->source_code, pc->owner->line_offsets, msg);
133 ErrorMsg *err = err_msg_create_with_line(pc->owner->data.structure.root_struct->path,
134 token->start_line, token->start_column,
135 pc->owner->data.structure.root_struct->source_code,
136 pc->owner->data.structure.root_struct->line_offsets, msg);
135137 err->line_start = token->start_line;
136138 err->column_start = token->start_column;
137139
......@@ -148,8 +150,10 @@ static void ast_asm_error(ParseContext *pc, AstNode *node, size_t offset, const
148150 Buf *msg = buf_vprintf(format, ap);
149151 va_end(ap);
150152
151 ErrorMsg *err = err_msg_create_with_line(pc->owner->path, node->line, node->column,
152 pc->owner->source_code, pc->owner->line_offsets, msg);
153 ErrorMsg *err = err_msg_create_with_line(pc->owner->data.structure.root_struct->path,
154 node->line, node->column,
155 pc->owner->data.structure.root_struct->source_code,
156 pc->owner->data.structure.root_struct->line_offsets, msg);
153157
154158 print_err_msg(err, pc->err_color);
155159 exit(EXIT_FAILURE);
......@@ -570,9 +574,7 @@ static void ast_parse_asm_template(ParseContext *pc, AstNode *node) {
570574 }
571575}
572576
573AstNode *ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner,
574 ErrColor err_color)
575{
577AstNode *ast_parse(Buf *buf, ZigList<Token> *tokens, ZigType *owner, ErrColor err_color) {
576578 ParseContext pc = {};
577579 pc.err_color = err_color;
578580 pc.owner = owner;
src/parser.hpp+1-1
......@@ -16,7 +16,7 @@ ATTRIBUTE_PRINTF(2, 3)
1616void ast_token_error(Token *token, const char *format, ...);
1717
1818
19AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner, ErrColor err_color);
19AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ZigType *owner, ErrColor err_color);
2020
2121void ast_print(AstNode *node, int indent);
2222
src/translate_c.cpp+5-4
......@@ -76,7 +76,7 @@ struct TransScopeWhile {
7676};
7777
7878struct Context {
79 ImportTableEntry *import;
79 ZigType *import;
8080 ZigList<ErrorMsg *> *errors;
8181 VisibMod visib_mod;
8282 bool want_export;
......@@ -4732,7 +4732,7 @@ static void process_preprocessor_entities(Context *c, ZigClangASTUnit *zunit) {
47324732 }
47334733}
47344734
4735Error parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,
4735Error parse_h_buf(ZigType *import, ZigList<ErrorMsg *> *errors, Buf *source,
47364736 CodeGen *codegen, AstNode *source_node)
47374737{
47384738 Error err;
......@@ -4748,7 +4748,7 @@ Error parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *so
47484748 return err;
47494749}
47504750
4751Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const char *target_file,
4751Error parse_h_file(ZigType *import, ZigList<ErrorMsg *> *errors, const char *target_file,
47524752 CodeGen *codegen, AstNode *source_node)
47534753{
47544754 Context context = {0};
......@@ -4958,7 +4958,8 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const
49584958 render_macros(c);
49594959 render_aliases(c);
49604960
4961 import->root = c->root;
4961 import->data.structure.decl_node = c->root;
4962 import->data.structure.decls_scope->base.source_node = c->root;
49624963
49634964 return ErrorNone;
49644965}
src/translate_c.hpp+2-2
......@@ -11,10 +11,10 @@
1111
1212#include "all_types.hpp"
1313
14Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const char *target_file,
14Error parse_h_file(ZigType *import, ZigList<ErrorMsg *> *errors, const char *target_file,
1515 CodeGen *codegen, AstNode *source_node);
1616
17Error parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,
17Error parse_h_buf(ZigType *import, ZigList<ErrorMsg *> *errors, Buf *source,
1818 CodeGen *codegen, AstNode *source_node);
1919
2020#endif
std/hash_map.zig-2
......@@ -486,7 +486,6 @@ pub fn autoHash(key: var, comptime rng: *std.rand.Random, comptime HashInt: type
486486 builtin.TypeId.ErrorSet => return autoHash(@errorToInt(key), rng),
487487 builtin.TypeId.Promise, builtin.TypeId.Fn => return autoHash(@ptrToInt(key), rng),
488488
489 builtin.TypeId.Namespace,
490489 builtin.TypeId.BoundFn,
491490 builtin.TypeId.ComptimeFloat,
492491 builtin.TypeId.ComptimeInt,
......@@ -532,7 +531,6 @@ pub fn autoEql(a: var, b: @typeOf(a)) bool {
532531 builtin.TypeId.Float,
533532 builtin.TypeId.ComptimeFloat,
534533 builtin.TypeId.ComptimeInt,
535 builtin.TypeId.Namespace,
536534 builtin.TypeId.Promise,
537535 builtin.TypeId.Enum,
538536 builtin.TypeId.BoundFn,
std/testing.zig-1
......@@ -44,7 +44,6 @@ pub fn expectEqual(expected: var, actual: @typeOf(expected)) void {
4444 TypeId.ComptimeFloat,
4545 TypeId.ComptimeInt,
4646 TypeId.Enum,
47 TypeId.Namespace,
4847 TypeId.Fn,
4948 TypeId.Promise,
5049 TypeId.Vector,
test/compile_errors.zig+41-41
......@@ -2,6 +2,47 @@ const tests = @import("tests.zig");
22const builtin = @import("builtin");
33
44pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add(
6 "@typeInfo causing depend on itself compile error",
7 \\const start = struct {
8 \\ fn crash() bug() {
9 \\ return bug;
10 \\ }
11 \\};
12 \\fn bug() void {
13 \\ _ = @typeInfo(start).Struct;
14 \\}
15 \\export fn entry() void {
16 \\ var boom = start.crash();
17 \\}
18 ,
19 ".tmp_source.zig:2:5: error: 'crash' depends on itself",
20 );
21
22 cases.add(
23 "enum field value references enum",
24 \\pub const Foo = extern enum {
25 \\ A = Foo.B,
26 \\ C = D,
27 \\};
28 \\export fn entry() void {
29 \\ var s: Foo = Foo.E;
30 \\}
31 ,
32 ".tmp_source.zig:1:17: error: 'Foo' depends on itself",
33 );
34
35 cases.add(
36 "top level decl dependency loop",
37 \\const a : @typeOf(b) = 0;
38 \\const b : @typeOf(a) = 0;
39 \\export fn entry() void {
40 \\ const c = a + b;
41 \\}
42 ,
43 ".tmp_source.zig:1:1: error: 'a' depends on itself",
44 );
45
546 cases.addTest(
647 "not an enum type",
748 \\export fn entry() void {
......@@ -917,23 +958,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
917958 ".tmp_source.zig:1:1: error: non-extern function has no body",
918959 );
919960
920 cases.add(
921 "@typeInfo causing depend on itself compile error",
922 \\const start = struct {
923 \\ fn crash() bug() {
924 \\ return bug;
925 \\ }
926 \\};
927 \\fn bug() void {
928 \\ _ = @typeInfo(start).Struct;
929 \\}
930 \\export fn entry() void {
931 \\ var boom = start.crash();
932 \\}
933 ,
934 ".tmp_source.zig:2:5: error: 'crash' depends on itself",
935 );
936
937961 cases.add(
938962 "@handle() called outside of function definition",
939963 \\var handle_undef: promise = undefined;
......@@ -1182,19 +1206,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
11821206 break :x tc;
11831207 });
11841208
1185 cases.add(
1186 "enum field value references enum",
1187 \\pub const Foo = extern enum {
1188 \\ A = Foo.B,
1189 \\ C = D,
1190 \\};
1191 \\export fn entry() void {
1192 \\ var s: Foo = Foo.E;
1193 \\}
1194 ,
1195 ".tmp_source.zig:1:17: error: 'Foo' depends on itself",
1196 );
1197
11981209 cases.add(
11991210 "@floatToInt comptime safety",
12001211 \\comptime {
......@@ -2622,17 +2633,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
26222633 ".tmp_source.zig:1:8: error: invalid builtin function: 'bogus'",
26232634 );
26242635
2625 cases.add(
2626 "top level decl dependency loop",
2627 \\const a : @typeOf(b) = 0;
2628 \\const b : @typeOf(a) = 0;
2629 \\export fn entry() void {
2630 \\ const c = a + b;
2631 \\}
2632 ,
2633 ".tmp_source.zig:1:1: error: 'a' depends on itself",
2634 );
2635
26362636 cases.add(
26372637 "noalias on non pointer param",
26382638 \\fn f(noalias x: i32) void {}
test/stage1/behavior/misc.zig+1-1
......@@ -469,7 +469,7 @@ test "@typeId" {
469469 expect(@typeId(AUnionEnum) == Tid.Union);
470470 expect(@typeId(AUnion) == Tid.Union);
471471 expect(@typeId(fn () void) == Tid.Fn);
472 expect(@typeId(@typeOf(builtin)) == Tid.Namespace);
472 expect(@typeId(@typeOf(builtin)) == Tid.Type);
473473 // TODO bound fn
474474 // TODO arg tuple
475475 // TODO opaque
test/stage1/behavior/type_info.zig+1-1
......@@ -186,7 +186,7 @@ fn testUnion() void {
186186 expect(TypeId(typeinfo_info) == TypeId.Union);
187187 expect(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto);
188188 expect(typeinfo_info.Union.tag_type.? == TypeId);
189 expect(typeinfo_info.Union.fields.len == 25);
189 expect(typeinfo_info.Union.fields.len == 24);
190190 expect(typeinfo_info.Union.fields[4].enum_field != null);
191191 expect(typeinfo_info.Union.fields[4].enum_field.?.value == 4);
192192 expect(typeinfo_info.Union.fields[4].field_type == @typeOf(@typeInfo(u8).Int));