diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-01-11 16:20:32 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-01-11 16:25:21 -0700 |
| commit | 29c9d5896c0f3349b37eff4cbce280fffa7e2924 (patch) | |
| tree | 82aebe8095bc7dcafc6cca525b14ab82c5382278 /src/Module.zig | |
| parent | 5cc2e500e68a60cb99b48755af39856ff60198b6 (diff) | |
| parent | bace1181b2f6d63e51b6e511aa8318481e2eafee (diff) | |
| download | zig-29c9d5896c0f3349b37eff4cbce280fffa7e2924.tar.gz zig-29c9d5896c0f3349b37eff4cbce280fffa7e2924.zip | |
Merge branch 'Stage2 begin implementing container types'
Diffstat (limited to 'src/Module.zig')
| -rw-r--r-- | src/Module.zig | 54 |
1 files changed, 48 insertions, 6 deletions
diff --git a/src/Module.zig b/src/Module.zig index 9707e79e2a..8575c59f4c 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -460,7 +460,7 @@ pub const Scope = struct { }; } - /// Asserts the scope has a parent which is a ZIRModule, Contaienr or File and + /// Asserts the scope has a parent which is a ZIRModule, Container or File and /// returns the sub_file_path field. pub fn subFilePath(base: *Scope) []const u8 { switch (base.tag) { @@ -501,12 +501,12 @@ pub const Scope = struct { } } - pub fn getOwnerPkg(base: *Scope) *Package { + pub fn getFileScope(base: *Scope) *Scope.File { var cur = base; while (true) { cur = switch (cur.tag) { - .container => return @fieldParentPtr(Container, "base", cur).file_scope.pkg, - .file => return @fieldParentPtr(File, "base", cur).pkg, + .container => return @fieldParentPtr(Container, "base", cur).file_scope, + .file => return @fieldParentPtr(File, "base", cur), .zir_module => unreachable, // TODO are zir modules allowed to import packages? .gen_zir => @fieldParentPtr(GenZIR, "base", cur).parent, .local_val => @fieldParentPtr(LocalVal, "base", cur).parent, @@ -582,7 +582,7 @@ pub const Scope = struct { file_scope: *Scope.File, /// Direct children of the file. - decls: std.AutoArrayHashMapUnmanaged(*Decl, void), + decls: std.AutoArrayHashMapUnmanaged(*Decl, void) = .{}, ty: Type, pub fn deinit(self: *Container, gpa: *Allocator) void { @@ -2464,6 +2464,48 @@ pub fn createAnonymousDecl( return new_decl; } +pub fn createContainerDecl( + self: *Module, + scope: *Scope, + base_token: std.zig.ast.TokenIndex, + decl_arena: *std.heap.ArenaAllocator, + typed_value: TypedValue, +) !*Decl { + const scope_decl = scope.decl().?; + const name = try self.getAnonTypeName(scope, base_token); + defer self.gpa.free(name); + const name_hash = scope.namespace().fullyQualifiedNameHash(name); + const src_hash: std.zig.SrcHash = undefined; + const new_decl = try self.createNewDecl(scope, name, scope_decl.src_index, name_hash, src_hash); + const decl_arena_state = try decl_arena.allocator.create(std.heap.ArenaAllocator.State); + + decl_arena_state.* = decl_arena.state; + new_decl.typed_value = .{ + .most_recent = .{ + .typed_value = typed_value, + .arena = decl_arena_state, + }, + }; + new_decl.analysis = .complete; + new_decl.generation = self.generation; + + return new_decl; +} + +fn getAnonTypeName(self: *Module, scope: *Scope, base_token: std.zig.ast.TokenIndex) ![]u8 { + // TODO add namespaces, generic function signatrues + const tree = scope.tree(); + const base_name = switch (tree.token_ids[base_token]) { + .Keyword_struct => "struct", + .Keyword_enum => "enum", + .Keyword_union => "union", + .Keyword_opaque => "opaque", + else => unreachable, + }; + const loc = tree.tokenLocationLoc(0, tree.token_locs[base_token]); + return std.fmt.allocPrint(self.gpa, "{}:{}:{}", .{ base_name, loc.line, loc.column }); +} + fn getNextAnonNameIndex(self: *Module) usize { return @atomicRmw(usize, &self.next_anon_name_index, .Add, 1, .Monotonic); } @@ -2645,7 +2687,7 @@ pub fn analyzeSlice(self: *Module, scope: *Scope, src: usize, array_ptr: *Inst, } pub fn analyzeImport(self: *Module, scope: *Scope, src: usize, target_string: []const u8) !*Scope.File { - const cur_pkg = scope.getOwnerPkg(); + const cur_pkg = scope.getFileScope().pkg; const cur_pkg_dir_path = cur_pkg.root_src_directory.path orelse "."; const found_pkg = cur_pkg.table.get(target_string); |
