diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-05-10 22:50:00 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-05-10 22:50:00 -0700 |
| commit | dae22a0a1f13cc963e96cd704941eed29b8dde27 (patch) | |
| tree | d353e68f224f4c8d9447ea2560ab1cd79189fbf5 /src/Module.zig | |
| parent | b9a099e83cf11629554c68c7606836065133f56b (diff) | |
| download | zig-dae22a0a1f13cc963e96cd704941eed29b8dde27.tar.gz zig-dae22a0a1f13cc963e96cd704941eed29b8dde27.zip | |
stage2: struct, union, enum, opaque, error sets get better names
This commit takes advantage of the new "NameStrategy" that is exposed
in the ZIR in order to name Decls after their parent when asked to. This
makes the next failing test case pass.
Diffstat (limited to 'src/Module.zig')
| -rw-r--r-- | src/Module.zig | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/src/Module.zig b/src/Module.zig index 434e74ca2f..7860f9141c 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -3759,17 +3759,19 @@ pub fn constIntBig(mod: *Module, arena: *Allocator, src: LazySrcLoc, ty: Type, b } } -pub fn createAnonymousDecl(mod: *Module, scope: *Scope, typed_value: TypedValue) !*Decl { +/// Takes ownership of `name` even if it returns an error. +pub fn createAnonymousDeclNamed( + mod: *Module, + scope: *Scope, + typed_value: TypedValue, + name: [:0]u8, +) !*Decl { + errdefer mod.gpa.free(name); + const scope_decl = scope.ownerDecl().?; const namespace = scope_decl.namespace; try namespace.anon_decls.ensureUnusedCapacity(mod.gpa, 1); - const name_index = mod.getNextAnonNameIndex(); - const name = try std.fmt.allocPrintZ(mod.gpa, "{s}__anon_{d}", .{ - scope_decl.name, name_index, - }); - errdefer mod.gpa.free(name); - const new_decl = try mod.allocateNewDecl(namespace, scope_decl.src_node); new_decl.name = name; @@ -3793,7 +3795,16 @@ pub fn createAnonymousDecl(mod: *Module, scope: *Scope, typed_value: TypedValue) return new_decl; } -fn getNextAnonNameIndex(mod: *Module) usize { +pub fn createAnonymousDecl(mod: *Module, scope: *Scope, typed_value: TypedValue) !*Decl { + const scope_decl = scope.ownerDecl().?; + const name_index = mod.getNextAnonNameIndex(); + const name = try std.fmt.allocPrintZ(mod.gpa, "{s}__anon_{d}", .{ + scope_decl.name, name_index, + }); + return mod.createAnonymousDeclNamed(scope, typed_value, name); +} + +pub fn getNextAnonNameIndex(mod: *Module) usize { return @atomicRmw(usize, &mod.next_anon_name_index, .Add, 1, .Monotonic); } |
