authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-04 13:58:08-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-04 13:58:08-07:00
logedfbf85ecdf29ee976534403369323def7905c62
tree04e9be8dfd761336da9544c2fbf52db27ede20d5
parent230ce72f168d5bcd0feb2f8f0aed42ea6ad27b09

Sema: implement error sets


3 files changed, 24 insertions(+), 23 deletions(-)

BRANCH_TODO-22
......@@ -118,31 +118,9 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {
118118 return mod.failWithOwnedErrorMsg(scope, msg);
119119 }
120120
121
122 const error_set = try arena.create(Module.ErrorSet);
123 error_set.* = .{
124 .owner_decl = astgen.decl,
125 .node_offset = astgen.decl.nodeIndexToRelative(node),
126 .names_ptr = fields.ptr,
127 .names_len = @intCast(u32, fields.len),
128 };
129 const error_set_ty = try Type.Tag.error_set.create(arena, error_set);
130 const error_set_val = try Value.Tag.ty.create(arena, error_set_ty);
131 const new_decl = try mod.createAnonymousDecl(scope, &new_decl_arena, .{
132 .ty = Type.initTag(.type),
133 .val = error_set_val,
134 });
135 const decl_index = try mod.declareDeclDependency(astgen.decl, new_decl);
136 const result = try gz.addDecl(.decl_val, decl_index, node);
137 return rvalue(gz, scope, rl, result, node);
138
139
140
141121 // when implementing this be sure to add test coverage for the asm return type
142122 // not resolving into a type (the node_offset_asm_ret_ty field of LazySrcLoc)
143123
144
145
146124pub fn analyzeNamespace(
147125 mod: *Module,
148126 namespace: *Scope.Namespace,
src/Module.zig+1
......@@ -478,6 +478,7 @@ pub const ErrorSet = struct {
478478 names_len: u32,
479479 /// The string bytes are stored in the owner Decl arena.
480480 /// They are in the same order they appear in the AST.
481 /// The length is given by `names_len`.
481482 names_ptr: [*]const []const u8,
482483
483484 pub fn srcLoc(self: ErrorSet) SrcLoc {
src/Sema.zig+23-1
......@@ -908,11 +908,33 @@ fn zirErrorSetDecl(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Inner
908908 const tracy = trace(@src());
909909 defer tracy.end();
910910
911 const gpa = sema.gpa;
911912 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
912913 const src = inst_data.src();
913914 const extra = sema.code.extraData(Zir.Inst.ErrorSetDecl, inst_data.payload_index);
915 const fields = sema.code.extra[extra.end..][0..extra.data.fields_len];
916
917 var new_decl_arena = std.heap.ArenaAllocator.init(gpa);
914918
915 return sema.mod.fail(&block.base, sema.src, "TODO implement zirErrorSetDecl", .{});
919 const error_set = try new_decl_arena.allocator.create(Module.ErrorSet);
920 const error_set_ty = try Type.Tag.error_set.create(&new_decl_arena.allocator, error_set);
921 const error_set_val = try Value.Tag.ty.create(&new_decl_arena.allocator, error_set_ty);
922 const new_decl = try sema.mod.createAnonymousDecl(&block.base, .{
923 .ty = Type.initTag(.type),
924 .val = error_set_val,
925 });
926 const names = try new_decl_arena.allocator.alloc([]const u8, fields.len);
927 for (fields) |str_index, i| {
928 names[i] = try new_decl_arena.allocator.dupe(u8, sema.code.nullTerminatedString(str_index));
929 }
930 error_set.* = .{
931 .owner_decl = new_decl,
932 .node_offset = inst_data.src_node,
933 .names_ptr = names.ptr,
934 .names_len = @intCast(u32, names.len),
935 };
936 try new_decl.finalizeNewArena(&new_decl_arena);
937 return sema.analyzeDeclVal(block, src, new_decl);
916938}
917939
918940fn zirRetPtr(