From f173d078c780c9946742c4ce686ccd0dccdb7e98 Mon Sep 17 00:00:00 2001 From: Vexu Date: Sun, 15 Nov 2020 13:03:48 +0200 Subject: stage2: outline container types --- src/Module.zig | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index 81dc72e601..3e6be31be1 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -428,7 +428,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) { @@ -1515,6 +1515,7 @@ pub fn analyzeContainer(self: *Module, container_scope: *Scope.Container) !void // an incremental update. This code handles both cases. const tree = try self.getAstTree(container_scope); const decls = tree.root_node.decls(); + // const decls = container_scope.root_node.decls(); try self.comp.work_queue.ensureUnusedCapacity(decls.len); try container_scope.decls.ensureCapacity(self.gpa, decls.len); @@ -2272,6 +2273,33 @@ pub fn createAnonymousDecl( return new_decl; } +fn createContainerDecl(self: *Module, scope: *Scope, container_node: *std.zig.ast.Node.ContainerDecl) !*Decl { + const name = try self.getAnonTypeName(scope, container_node.kind_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.generation = self.generation; + + return new_decl; +} + +fn getAnonTypeName(self: *Module, scope: *Scope, base_token: std.zig.ast.TokenIndex) ![]u8 { + const container = scope.getContainer(); + const tree = self.getAstTree(container); + const base_name = switch (tree.token_ids[base_token]) { + .Keyword_struct => "struct", + .Keyword_enum => "enum", + .Keyword_union => "union", + 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); } -- cgit v1.2.3