| author | |
| committer | |
| log | c1e19f4c0a62047c26d5baabe25887e533cc739f |
| tree | 921e818474ece6846d958fabf1a99c0ab3cc3df8 |
| parent | f173d078c780c9946742c4ce686ccd0dccdb7e98 |
| signature |
8 files changed, 228 insertions(+), 41 deletions(-)
src/Module.zig+23-9| ... | @@ -469,12 +469,12 @@ pub const Scope = struct { | ... | @@ -469,12 +469,12 @@ pub const Scope = struct { |
| 469 | } | 469 | } |
| 470 | } | 470 | } |
| 471 | 471 | ||
| 472 | pub fn getOwnerPkg(base: *Scope) *Package { | 472 | pub fn getFileScope(base: *Scope) *Scope.File { |
| 473 | var cur = base; | 473 | var cur = base; |
| 474 | while (true) { | 474 | while (true) { |
| 475 | cur = switch (cur.tag) { | 475 | cur = switch (cur.tag) { |
| 476 | .container => return @fieldParentPtr(Container, "base", cur).file_scope.pkg, | 476 | .container => return @fieldParentPtr(Container, "base", cur).file_scope, |
| 477 | .file => return @fieldParentPtr(File, "base", cur).pkg, | 477 | .file => return @fieldParentPtr(File, "base", cur), |
| 478 | .zir_module => unreachable, // TODO are zir modules allowed to import packages? | 478 | .zir_module => unreachable, // TODO are zir modules allowed to import packages? |
| 479 | .gen_zir => @fieldParentPtr(GenZIR, "base", cur).parent, | 479 | .gen_zir => @fieldParentPtr(GenZIR, "base", cur).parent, |
| 480 | .local_val => @fieldParentPtr(LocalVal, "base", cur).parent, | 480 | .local_val => @fieldParentPtr(LocalVal, "base", cur).parent, |
| ... | @@ -550,7 +550,7 @@ pub const Scope = struct { | ... | @@ -550,7 +550,7 @@ pub const Scope = struct { |
| 550 | file_scope: *Scope.File, | 550 | file_scope: *Scope.File, |
| 551 | 551 | ||
| 552 | /// Direct children of the file. | 552 | /// Direct children of the file. |
| 553 | decls: std.AutoArrayHashMapUnmanaged(*Decl, void), | 553 | decls: std.AutoArrayHashMapUnmanaged(*Decl, void) = .{}, |
| 554 | ty: Type, | 554 | ty: Type, |
| 555 | 555 | ||
| 556 | pub fn deinit(self: *Container, gpa: *Allocator) void { | 556 | pub fn deinit(self: *Container, gpa: *Allocator) void { |
| ... | @@ -2273,8 +2273,15 @@ pub fn createAnonymousDecl( | ... | @@ -2273,8 +2273,15 @@ pub fn createAnonymousDecl( |
| 2273 | return new_decl; | 2273 | return new_decl; |
| 2274 | } | 2274 | } |
| 2275 | 2275 | ||
| 2276 | fn createContainerDecl(self: *Module, scope: *Scope, container_node: *std.zig.ast.Node.ContainerDecl) !*Decl { | 2276 | pub fn createContainerDecl( |
| 2277 | const name = try self.getAnonTypeName(scope, container_node.kind_token); | 2277 | self: *Module, |
| 2278 | scope: *Scope, | ||
| 2279 | base_token: std.zig.ast.TokenIndex, | ||
| 2280 | decl_arena: *std.heap.ArenaAllocator, | ||
| 2281 | typed_value: TypedValue, | ||
| 2282 | ) !*Decl { | ||
| 2283 | const scope_decl = scope.decl().?; | ||
| 2284 | const name = try self.getAnonTypeName(scope, base_token); | ||
| 2278 | defer self.gpa.free(name); | 2285 | defer self.gpa.free(name); |
| 2279 | const name_hash = scope.namespace().fullyQualifiedNameHash(name); | 2286 | const name_hash = scope.namespace().fullyQualifiedNameHash(name); |
| 2280 | const src_hash: std.zig.SrcHash = undefined; | 2287 | const src_hash: std.zig.SrcHash = undefined; |
| ... | @@ -2282,18 +2289,25 @@ fn createContainerDecl(self: *Module, scope: *Scope, container_node: *std.zig.as | ... | @@ -2282,18 +2289,25 @@ fn createContainerDecl(self: *Module, scope: *Scope, container_node: *std.zig.as |
| 2282 | const decl_arena_state = try decl_arena.allocator.create(std.heap.ArenaAllocator.State); | 2289 | const decl_arena_state = try decl_arena.allocator.create(std.heap.ArenaAllocator.State); |
| 2283 | 2290 | ||
| 2284 | decl_arena_state.* = decl_arena.state; | 2291 | decl_arena_state.* = decl_arena.state; |
| 2292 | new_decl.typed_value = .{ | ||
| 2293 | .most_recent = .{ | ||
| 2294 | .typed_value = typed_value, | ||
| 2295 | .arena = decl_arena_state, | ||
| 2296 | }, | ||
| 2297 | }; | ||
| 2298 | new_decl.analysis = .complete; | ||
| 2285 | new_decl.generation = self.generation; | 2299 | new_decl.generation = self.generation; |
| 2286 | 2300 | ||
| 2287 | return new_decl; | 2301 | return new_decl; |
| 2288 | } | 2302 | } |
| 2289 | 2303 | ||
| 2290 | fn getAnonTypeName(self: *Module, scope: *Scope, base_token: std.zig.ast.TokenIndex) ![]u8 { | 2304 | fn getAnonTypeName(self: *Module, scope: *Scope, base_token: std.zig.ast.TokenIndex) ![]u8 { |
| 2291 | const container = scope.getContainer(); | 2305 | const tree = scope.tree(); |
| 2292 | const tree = self.getAstTree(container); | ||
| 2293 | const base_name = switch (tree.token_ids[base_token]) { | 2306 | const base_name = switch (tree.token_ids[base_token]) { |
| 2294 | .Keyword_struct => "struct", | 2307 | .Keyword_struct => "struct", |
| 2295 | .Keyword_enum => "enum", | 2308 | .Keyword_enum => "enum", |
| 2296 | .Keyword_union => "union", | 2309 | .Keyword_union => "union", |
| 2310 | .Keyword_opaque => "opaque", | ||
| 2297 | else => unreachable, | 2311 | else => unreachable, |
| 2298 | }; | 2312 | }; |
| 2299 | const loc = tree.tokenLocationLoc(0, tree.token_locs[base_token]); | 2313 | const loc = tree.tokenLocationLoc(0, tree.token_locs[base_token]); |
| ... | @@ -2487,7 +2501,7 @@ pub fn analyzeSlice(self: *Module, scope: *Scope, src: usize, array_ptr: *Inst, | ... | @@ -2487,7 +2501,7 @@ pub fn analyzeSlice(self: *Module, scope: *Scope, src: usize, array_ptr: *Inst, |
| 2487 | } | 2501 | } |
| 2488 | 2502 | ||
| 2489 | pub fn analyzeImport(self: *Module, scope: *Scope, src: usize, target_string: []const u8) !*Scope.File { | 2503 | pub fn analyzeImport(self: *Module, scope: *Scope, src: usize, target_string: []const u8) !*Scope.File { |
| 2490 | const cur_pkg = scope.getOwnerPkg(); | 2504 | const cur_pkg = scope.getFileScope().pkg; |
| 2491 | const cur_pkg_dir_path = cur_pkg.root_src_directory.path orelse "."; | 2505 | const cur_pkg_dir_path = cur_pkg.root_src_directory.path orelse "."; |
| 2492 | const found_pkg = cur_pkg.table.get(target_string); | 2506 | const found_pkg = cur_pkg.table.get(target_string); |
| 2493 | 2507 |
src/astgen.zig+163-1| ... | @@ -281,6 +281,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr | ... | @@ -281,6 +281,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr |
| 281 | .Comptime => return comptimeKeyword(mod, scope, rl, node.castTag(.Comptime).?), | 281 | .Comptime => return comptimeKeyword(mod, scope, rl, node.castTag(.Comptime).?), |
| 282 | .OrElse => return orelseExpr(mod, scope, rl, node.castTag(.OrElse).?), | 282 | .OrElse => return orelseExpr(mod, scope, rl, node.castTag(.OrElse).?), |
| 283 | .Switch => return switchExpr(mod, scope, rl, node.castTag(.Switch).?), | 283 | .Switch => return switchExpr(mod, scope, rl, node.castTag(.Switch).?), |
| 284 | .ContainerDecl => return containerDecl(mod, scope, rl, node.castTag(.ContainerDecl).?), | ||
| 284 | 285 | ||
| 285 | .Defer => return mod.failNode(scope, node, "TODO implement astgen.expr for .Defer", .{}), | 286 | .Defer => return mod.failNode(scope, node, "TODO implement astgen.expr for .Defer", .{}), |
| 286 | .Await => return mod.failNode(scope, node, "TODO implement astgen.expr for .Await", .{}), | 287 | .Await => return mod.failNode(scope, node, "TODO implement astgen.expr for .Await", .{}), |
| ... | @@ -294,7 +295,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr | ... | @@ -294,7 +295,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr |
| 294 | .Continue => return mod.failNode(scope, node, "TODO implement astgen.expr for .Continue", .{}), | 295 | .Continue => return mod.failNode(scope, node, "TODO implement astgen.expr for .Continue", .{}), |
| 295 | .AnyType => return mod.failNode(scope, node, "TODO implement astgen.expr for .AnyType", .{}), | 296 | .AnyType => return mod.failNode(scope, node, "TODO implement astgen.expr for .AnyType", .{}), |
| 296 | .FnProto => return mod.failNode(scope, node, "TODO implement astgen.expr for .FnProto", .{}), | 297 | .FnProto => return mod.failNode(scope, node, "TODO implement astgen.expr for .FnProto", .{}), |
| 297 | .ContainerDecl => return mod.failNode(scope, node, "TODO implement astgen.expr for .ContainerDecl", .{}), | ||
| 298 | .Nosuspend => return mod.failNode(scope, node, "TODO implement astgen.expr for .Nosuspend", .{}), | 298 | .Nosuspend => return mod.failNode(scope, node, "TODO implement astgen.expr for .Nosuspend", .{}), |
| 299 | } | 299 | } |
| 300 | } | 300 | } |
| ... | @@ -765,6 +765,168 @@ fn unwrapOptional(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.Si | ... | @@ -765,6 +765,168 @@ fn unwrapOptional(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.Si |
| 765 | return rlWrapPtr(mod, scope, rl, try addZIRUnOp(mod, scope, src, .unwrap_optional_safe, operand)); | 765 | return rlWrapPtr(mod, scope, rl, try addZIRUnOp(mod, scope, src, .unwrap_optional_safe, operand)); |
| 766 | } | 766 | } |
| 767 | 767 | ||
| 768 | fn containerField(mod: *Module, scope: *Scope, node: *ast.Node.ContainerField) InnerError!*zir.Inst { | ||
| 769 | const tree = scope.tree(); | ||
| 770 | const src = tree.token_locs[node.firstToken()].start; | ||
| 771 | const name = try identifierTokenString(mod, scope, node.name_token); | ||
| 772 | |||
| 773 | if (node.comptime_token == null and node.value_expr == null and node.align_expr == null) { | ||
| 774 | if (node.type_expr) |some| { | ||
| 775 | const ty = try typeExpr(mod, scope, some); | ||
| 776 | return addZIRInst(mod, scope, src, zir.Inst.ContainerFieldTyped, .{ | ||
| 777 | .bytes = name, | ||
| 778 | .ty = ty, | ||
| 779 | }, .{}); | ||
| 780 | } else { | ||
| 781 | return addZIRInst(mod, scope, src, zir.Inst.ContainerFieldNamed, .{ | ||
| 782 | .bytes = name, | ||
| 783 | }, .{}); | ||
| 784 | } | ||
| 785 | } | ||
| 786 | |||
| 787 | const ty = if (node.type_expr) |some| try typeExpr(mod, scope, some) else null; | ||
| 788 | const alignment = if (node.align_expr) |some| try expr(mod, scope, .none, some) else null; | ||
| 789 | const init = if (node.value_expr) |some| try expr(mod, scope, .none, some) else null; | ||
| 790 | |||
| 791 | return addZIRInst(mod, scope, src, zir.Inst.ContainerField, .{ | ||
| 792 | .bytes = name, | ||
| 793 | }, .{ | ||
| 794 | .ty = ty, | ||
| 795 | .init = init, | ||
| 796 | .alignment = alignment, | ||
| 797 | .is_comptime = node.comptime_token != null, | ||
| 798 | }); | ||
| 799 | } | ||
| 800 | |||
| 801 | fn containerDecl(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.ContainerDecl) InnerError!*zir.Inst { | ||
| 802 | const tree = scope.tree(); | ||
| 803 | const src = tree.token_locs[node.kind_token].start; | ||
| 804 | |||
| 805 | var gen_scope: Scope.GenZIR = .{ | ||
| 806 | .parent = scope, | ||
| 807 | .decl = scope.decl().?, | ||
| 808 | .arena = scope.arena(), | ||
| 809 | .instructions = .{}, | ||
| 810 | }; | ||
| 811 | defer gen_scope.instructions.deinit(mod.gpa); | ||
| 812 | |||
| 813 | var fields = std.ArrayList(*zir.Inst).init(mod.gpa); | ||
| 814 | defer fields.deinit(); | ||
| 815 | |||
| 816 | for (node.fieldsAndDecls()) |fd| { | ||
| 817 | if (fd.castTag(.ContainerField)) |f| { | ||
| 818 | try fields.append(try containerField(mod, &gen_scope.base, f)); | ||
| 819 | } | ||
| 820 | } | ||
| 821 | |||
| 822 | var decl_arena = std.heap.ArenaAllocator.init(mod.gpa); | ||
| 823 | errdefer decl_arena.deinit(); | ||
| 824 | const arena = &decl_arena.allocator; | ||
| 825 | |||
| 826 | var layout: std.builtin.TypeInfo.ContainerLayout = .Auto; | ||
| 827 | if (node.layout_token) |some| switch (tree.token_ids[some]) { | ||
| 828 | .Keyword_extern => layout = .Extern, | ||
| 829 | .Keyword_packed => layout = .Packed, | ||
| 830 | else => unreachable, | ||
| 831 | }; | ||
| 832 | |||
| 833 | const container_type = switch (tree.token_ids[node.kind_token]) { | ||
| 834 | .Keyword_enum => blk: { | ||
| 835 | const tag_type: ?*zir.Inst = switch (node.init_arg_expr) { | ||
| 836 | .Type => |t| try typeExpr(mod, &gen_scope.base, t), | ||
| 837 | .None => null, | ||
| 838 | .Enum => unreachable, | ||
| 839 | }; | ||
| 840 | const inst = try addZIRInst(mod, &gen_scope.base, src, zir.Inst.EnumType, .{ | ||
| 841 | .fields = try arena.dupe(*zir.Inst, fields.items), | ||
| 842 | }, .{ | ||
| 843 | .layout = layout, | ||
| 844 | .tag_type = tag_type, | ||
| 845 | }); | ||
| 846 | const enum_type = try arena.create(Type.Payload.Enum); | ||
| 847 | enum_type.* = .{ | ||
| 848 | .analysis = .{ | ||
| 849 | .queued = .{ | ||
| 850 | .body = .{ .instructions = try arena.dupe(*zir.Inst, gen_scope.instructions.items) }, | ||
| 851 | .inst = inst, | ||
| 852 | }, | ||
| 853 | }, | ||
| 854 | .scope = .{ | ||
| 855 | .file_scope = scope.getFileScope(), | ||
| 856 | .ty = Type.initPayload(&enum_type.base), | ||
| 857 | }, | ||
| 858 | }; | ||
| 859 | break :blk Type.initPayload(&enum_type.base); | ||
| 860 | }, | ||
| 861 | .Keyword_struct => blk: { | ||
| 862 | assert(node.init_arg_expr == .None); | ||
| 863 | const inst = try addZIRInst(mod, &gen_scope.base, src, zir.Inst.StructType, .{ | ||
| 864 | .fields = try arena.dupe(*zir.Inst, fields.items), | ||
| 865 | }, .{ | ||
| 866 | .layout = layout, | ||
| 867 | }); | ||
| 868 | const struct_type = try arena.create(Type.Payload.Struct); | ||
| 869 | struct_type.* = .{ | ||
| 870 | .analysis = .{ | ||
| 871 | .queued = .{ | ||
| 872 | .body = .{ .instructions = try arena.dupe(*zir.Inst, gen_scope.instructions.items) }, | ||
| 873 | .inst = inst, | ||
| 874 | }, | ||
| 875 | }, | ||
| 876 | .scope = .{ | ||
| 877 | .file_scope = scope.getFileScope(), | ||
| 878 | .ty = Type.initPayload(&struct_type.base), | ||
| 879 | }, | ||
| 880 | }; | ||
| 881 | break :blk Type.initPayload(&struct_type.base); | ||
| 882 | }, | ||
| 883 | .Keyword_union => blk: { | ||
| 884 | const init_inst = switch (node.init_arg_expr) { | ||
| 885 | .Enum => |e| if (e) |t| try typeExpr(mod, &gen_scope.base, t) else null, | ||
| 886 | .None => null, | ||
| 887 | .Type => |t| try typeExpr(mod, &gen_scope.base, t), | ||
| 888 | }; | ||
| 889 | const init_kind: zir.Inst.UnionType.InitKind = switch (node.init_arg_expr) { | ||
| 890 | .Enum => .enum_type, | ||
| 891 | .None => .none, | ||
| 892 | .Type => .tag_type, | ||
| 893 | }; | ||
| 894 | const inst = try addZIRInst(mod, &gen_scope.base, src, zir.Inst.UnionType, .{ | ||
| 895 | .fields = try arena.dupe(*zir.Inst, fields.items), | ||
| 896 | }, .{ | ||
| 897 | .layout = layout, | ||
| 898 | .init_kind = init_kind, | ||
| 899 | .init_inst = init_inst, | ||
| 900 | }); | ||
| 901 | const union_type = try arena.create(Type.Payload.Union); | ||
| 902 | union_type.* = .{ | ||
| 903 | .analysis = .{ | ||
| 904 | .queued = .{ | ||
| 905 | .body = .{ .instructions = try arena.dupe(*zir.Inst, gen_scope.instructions.items) }, | ||
| 906 | .inst = inst, | ||
| 907 | }, | ||
| 908 | }, | ||
| 909 | .scope = .{ | ||
| 910 | .file_scope = scope.getFileScope(), | ||
| 911 | .ty = Type.initPayload(&union_type.base), | ||
| 912 | }, | ||
| 913 | }; | ||
| 914 | break :blk Type.initPayload(&union_type.base); | ||
| 915 | }, | ||
| 916 | .Keyword_opaque => return mod.fail(scope, src, "TODO opaque containers", .{}), | ||
| 917 | else => unreachable, | ||
| 918 | }; | ||
| 919 | const type_payload = try arena.create(Value.Payload.Ty); | ||
| 920 | type_payload.* = .{ | ||
| 921 | .ty = container_type, | ||
| 922 | }; | ||
| 923 | const decl = try mod.createContainerDecl(scope, node.kind_token, &decl_arena, .{ | ||
| 924 | .ty = Type.initTag(.type), | ||
| 925 | .val = Value.initPayload(&type_payload.base), | ||
| 926 | }); | ||
| 927 | return rlWrapPtr(mod, scope, rl, try addZIRInst(mod, scope, src, zir.Inst.DeclValInModule, .{ .decl = decl }, .{})); | ||
| 928 | } | ||
| 929 | |||
| 768 | fn errorSetDecl(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.ErrorSetDecl) InnerError!*zir.Inst { | 930 | fn errorSetDecl(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.ErrorSetDecl) InnerError!*zir.Inst { |
| 769 | const tree = scope.tree(); | 931 | const tree = scope.tree(); |
| 770 | const src = tree.token_locs[node.error_token].start; | 932 | const src = tree.token_locs[node.error_token].start; |
src/type.zig+4-4| ... | @@ -923,7 +923,7 @@ pub const Type = extern union { | ... | @@ -923,7 +923,7 @@ pub const Type = extern union { |
| 923 | @panic("TODO abiAlignment error union"); | 923 | @panic("TODO abiAlignment error union"); |
| 924 | }, | 924 | }, |
| 925 | 925 | ||
| 926 | .@"enum" => self.cast(Payload.Enum).?.abiAlignment(), | 926 | .@"enum" => self.cast(Payload.Enum).?.abiAlignment(target), |
| 927 | .@"struct" => @panic("TODO"), | 927 | .@"struct" => @panic("TODO"), |
| 928 | .@"union" => @panic("TODO"), | 928 | .@"union" => @panic("TODO"), |
| 929 | 929 | ||
| ... | @@ -3221,9 +3221,9 @@ pub const Type = extern union { | ... | @@ -3221,9 +3221,9 @@ pub const Type = extern union { |
| 3221 | scope: *Module.Scope.Container, | 3221 | scope: *Module.Scope.Container, |
| 3222 | }; | 3222 | }; |
| 3223 | 3223 | ||
| 3224 | pub const Enum = @import("value/Enum.zig"); | 3224 | pub const Enum = @import("type/Enum.zig"); |
| 3225 | pub const Struct = @import("value/Struct.zig"); | 3225 | pub const Struct = @import("type/Struct.zig"); |
| 3226 | pub const Union = @import("value/Union.zig"); | 3226 | pub const Union = @import("type/Union.zig"); |
| 3227 | }; | 3227 | }; |
| 3228 | }; | 3228 | }; |
| 3229 | 3229 |
src/type/Enum.zig+6-6| ... | @@ -1,8 +1,10 @@ | ... | @@ -1,8 +1,10 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const zir = @import("../zir.zig"); | ||
| 2 | const Value = @import("../value.zig").Value; | 3 | const Value = @import("../value.zig").Value; |
| 3 | const Type = @import("../type.zig").Type; | 4 | const Type = @import("../type.zig").Type; |
| 4 | const Module = @import("../Module.zig"); | 5 | const Module = @import("../Module.zig"); |
| 5 | const Scope = Module.Scope; | 6 | const Scope = Module.Scope; |
| 7 | const Enum = @This(); | ||
| 6 | 8 | ||
| 7 | base: Type.Payload = .{ .tag = .@"enum" }, | 9 | base: Type.Payload = .{ .tag = .@"enum" }, |
| 8 | 10 | ||
| ... | @@ -12,6 +14,7 @@ analysis: union(enum) { | ... | @@ -12,6 +14,7 @@ analysis: union(enum) { |
| 12 | resolved: Size, | 14 | resolved: Size, |
| 13 | failed, | 15 | failed, |
| 14 | }, | 16 | }, |
| 17 | scope: Scope.Container, | ||
| 15 | 18 | ||
| 16 | pub const Field = struct { | 19 | pub const Field = struct { |
| 17 | value: Value, | 20 | value: Value, |
| ... | @@ -20,13 +23,10 @@ pub const Field = struct { | ... | @@ -20,13 +23,10 @@ pub const Field = struct { |
| 20 | pub const Zir = struct { | 23 | pub const Zir = struct { |
| 21 | body: zir.Module.Body, | 24 | body: zir.Module.Body, |
| 22 | inst: *zir.Inst, | 25 | inst: *zir.Inst, |
| 23 | arena: std.heap.ArenaAllocator.State, | ||
| 24 | }; | 26 | }; |
| 25 | 27 | ||
| 26 | pub const Size = struct { | 28 | pub const Size = struct { |
| 27 | is_zero_bits: bool, | 29 | tag_type: Type, |
| 28 | alignment: u32, | ||
| 29 | size: u32, | ||
| 30 | fields: std.AutoArrayHashMap([]const u8, Field), | 30 | fields: std.AutoArrayHashMap([]const u8, Field), |
| 31 | }; | 31 | }; |
| 32 | 32 | ||
| ... | @@ -45,11 +45,11 @@ pub fn resolve(self: *Enum, mod: *Module, scope: *Scope) !void { | ... | @@ -45,11 +45,11 @@ pub fn resolve(self: *Enum, mod: *Module, scope: *Scope) !void { |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | // TODO should this resolve the type or assert that it has already been resolved? | 47 | // TODO should this resolve the type or assert that it has already been resolved? |
| 48 | pub fn abiAlignment(self: *Enum) u32 { | 48 | pub fn abiAlignment(self: *Enum, target: std.Target) u32 { |
| 49 | switch (self.analysis) { | 49 | switch (self.analysis) { |
| 50 | .queued => unreachable, // alignment has not been resolved | 50 | .queued => unreachable, // alignment has not been resolved |
| 51 | .in_progress => unreachable, // alignment has not been resolved | 51 | .in_progress => unreachable, // alignment has not been resolved |
| 52 | .failed => unreachable, // type resolution failed | 52 | .failed => unreachable, // type resolution failed |
| 53 | .resolved => |r| return r.tag_type.abiAlignment(), | 53 | .resolved => |r| return r.tag_type.abiAlignment(target), |
| 54 | } | 54 | } |
| 55 | } | 55 | } |
src/type/Struct.zig+5-6| ... | @@ -1,8 +1,10 @@ | ... | @@ -1,8 +1,10 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const zir = @import("../zir.zig"); | ||
| 2 | const Value = @import("../value.zig").Value; | 3 | const Value = @import("../value.zig").Value; |
| 3 | const Type = @import("../type.zig").Type; | 4 | const Type = @import("../type.zig").Type; |
| 4 | const Module = @import("../Module.zig"); | 5 | const Module = @import("../Module.zig"); |
| 5 | const Scope = Module.Scope; | 6 | const Scope = Module.Scope; |
| 7 | const Struct = @This(); | ||
| 6 | 8 | ||
| 7 | base: Type.Payload = .{ .tag = .@"struct" }, | 9 | base: Type.Payload = .{ .tag = .@"struct" }, |
| 8 | 10 | ||
| ... | @@ -11,7 +13,7 @@ analysis: union(enum) { | ... | @@ -11,7 +13,7 @@ analysis: union(enum) { |
| 11 | zero_bits_in_progress, | 13 | zero_bits_in_progress, |
| 12 | zero_bits: Zero, | 14 | zero_bits: Zero, |
| 13 | in_progress, | 15 | in_progress, |
| 14 | alignment: Align, | 16 | // alignment: Align, |
| 15 | resolved: Size, | 17 | resolved: Size, |
| 16 | failed, | 18 | failed, |
| 17 | }, | 19 | }, |
| ... | @@ -24,7 +26,6 @@ pub const Field = struct { | ... | @@ -24,7 +26,6 @@ pub const Field = struct { |
| 24 | pub const Zir = struct { | 26 | pub const Zir = struct { |
| 25 | body: zir.Module.Body, | 27 | body: zir.Module.Body, |
| 26 | inst: *zir.Inst, | 28 | inst: *zir.Inst, |
| 27 | arena: std.heap.ArenaAllocator.State, | ||
| 28 | }; | 29 | }; |
| 29 | 30 | ||
| 30 | pub const Zero = struct { | 31 | pub const Zero = struct { |
| ... | @@ -39,11 +40,11 @@ pub const Size = struct { | ... | @@ -39,11 +40,11 @@ pub const Size = struct { |
| 39 | fields: std.AutoArrayHashMap([]const u8, Field), | 40 | fields: std.AutoArrayHashMap([]const u8, Field), |
| 40 | }; | 41 | }; |
| 41 | 42 | ||
| 42 | pub fn resolveZeroBits(self: *Enum, mod: *Module, scope: *Scope) !void { | 43 | pub fn resolveZeroBits(self: *Struct, mod: *Module, scope: *Scope) !void { |
| 43 | const zir = switch (self.analysis) { | 44 | const zir = switch (self.analysis) { |
| 44 | .failed => return error.AnalysisFail, | 45 | .failed => return error.AnalysisFail, |
| 45 | .zero_bits_in_progress => { | 46 | .zero_bits_in_progress => { |
| 46 | return mod.fail(scope, src, "union '{}' depends on itself", .{}); | 47 | return mod.fail(scope, src, "struct '{}' depends on itself", .{}); |
| 47 | }, | 48 | }, |
| 48 | .queued => |zir| zir, | 49 | .queued => |zir| zir, |
| 49 | else => return, | 50 | else => return, |
| ... | @@ -53,5 +54,3 @@ pub fn resolveZeroBits(self: *Enum, mod: *Module, scope: *Scope) !void { | ... | @@ -53,5 +54,3 @@ pub fn resolveZeroBits(self: *Enum, mod: *Module, scope: *Scope) !void { |
| 53 | 54 | ||
| 54 | // TODO | 55 | // TODO |
| 55 | } | 56 | } |
| 56 | |||
| 57 | pub fn resolveSize(self: *Enum,) | ||
| \ No newline at end of file | |||
src/type/Union.zig+4-5| ... | @@ -1,8 +1,10 @@ | ... | @@ -1,8 +1,10 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const zir = @import("../zir.zig"); | ||
| 2 | const Value = @import("../value.zig").Value; | 3 | const Value = @import("../value.zig").Value; |
| 3 | const Type = @import("../type.zig").Type; | 4 | const Type = @import("../type.zig").Type; |
| 4 | const Module = @import("../Module.zig"); | 5 | const Module = @import("../Module.zig"); |
| 5 | const Scope = Module.Scope; | 6 | const Scope = Module.Scope; |
| 7 | const Union = @This(); | ||
| 6 | 8 | ||
| 7 | base: Type.Payload = .{ .tag = .@"struct" }, | 9 | base: Type.Payload = .{ .tag = .@"struct" }, |
| 8 | 10 | ||
| ... | @@ -11,7 +13,7 @@ analysis: union(enum) { | ... | @@ -11,7 +13,7 @@ analysis: union(enum) { |
| 11 | zero_bits_in_progress, | 13 | zero_bits_in_progress, |
| 12 | zero_bits: Zero, | 14 | zero_bits: Zero, |
| 13 | in_progress, | 15 | in_progress, |
| 14 | alignment: Align, | 16 | // alignment: Align, |
| 15 | resolved: Size, | 17 | resolved: Size, |
| 16 | failed, | 18 | failed, |
| 17 | }, | 19 | }, |
| ... | @@ -24,7 +26,6 @@ pub const Field = struct { | ... | @@ -24,7 +26,6 @@ pub const Field = struct { |
| 24 | pub const Zir = struct { | 26 | pub const Zir = struct { |
| 25 | body: zir.Module.Body, | 27 | body: zir.Module.Body, |
| 26 | inst: *zir.Inst, | 28 | inst: *zir.Inst, |
| 27 | arena: std.heap.ArenaAllocator.State, | ||
| 28 | }; | 29 | }; |
| 29 | 30 | ||
| 30 | pub const Zero = struct { | 31 | pub const Zero = struct { |
| ... | @@ -39,7 +40,7 @@ pub const Size = struct { | ... | @@ -39,7 +40,7 @@ pub const Size = struct { |
| 39 | fields: std.AutoArrayHashMap([]const u8, Field), | 40 | fields: std.AutoArrayHashMap([]const u8, Field), |
| 40 | }; | 41 | }; |
| 41 | 42 | ||
| 42 | pub fn resolveZeroBits(self: *Enum, mod: *Module, scope: *Scope) !void { | 43 | pub fn resolveZeroBits(self: *Union, mod: *Module, scope: *Scope) !void { |
| 43 | const zir = switch (self.analysis) { | 44 | const zir = switch (self.analysis) { |
| 44 | .failed => return error.AnalysisFail, | 45 | .failed => return error.AnalysisFail, |
| 45 | .zero_bits_in_progress => { | 46 | .zero_bits_in_progress => { |
| ... | @@ -53,5 +54,3 @@ pub fn resolveZeroBits(self: *Enum, mod: *Module, scope: *Scope) !void { | ... | @@ -53,5 +54,3 @@ pub fn resolveZeroBits(self: *Enum, mod: *Module, scope: *Scope) !void { |
| 53 | 54 | ||
| 54 | // TODO | 55 | // TODO |
| 55 | } | 56 | } |
| 56 | |||
| 57 | pub fn resolveSize(self: *Enum,) | ||
| \ No newline at end of file | |||
src/zir.zig+15-10| ... | @@ -1084,12 +1084,13 @@ pub const Inst = struct { | ... | @@ -1084,12 +1084,13 @@ pub const Inst = struct { |
| 1084 | 1084 | ||
| 1085 | positionals: struct { | 1085 | positionals: struct { |
| 1086 | bytes: []const u8, | 1086 | bytes: []const u8, |
| 1087 | ty: ?*Inst, | ||
| 1088 | init: ?*Inst, | ||
| 1089 | alignment: ?*Inst, | ||
| 1090 | is_comptime: bool, | ||
| 1091 | }, | 1087 | }, |
| 1092 | kw_args: struct {}, | 1088 | kw_args: struct { |
| 1089 | ty: ?*Inst = null, | ||
| 1090 | init: ?*Inst = null, | ||
| 1091 | alignment: ?*Inst = null, | ||
| 1092 | is_comptime: bool = false, | ||
| 1093 | }, | ||
| 1093 | }; | 1094 | }; |
| 1094 | 1095 | ||
| 1095 | pub const EnumType = struct { | 1096 | pub const EnumType = struct { |
| ... | @@ -1125,13 +1126,17 @@ pub const Inst = struct { | ... | @@ -1125,13 +1126,17 @@ pub const Inst = struct { |
| 1125 | fields: []*Inst, | 1126 | fields: []*Inst, |
| 1126 | }, | 1127 | }, |
| 1127 | kw_args: struct { | 1128 | kw_args: struct { |
| 1128 | init_expr: union(enum) { | 1129 | init_inst: ?*Inst = null, |
| 1129 | enum_type: ?*Inst, | 1130 | init_kind: InitKind = .none, |
| 1130 | tag_type: *Inst, | ||
| 1131 | none, | ||
| 1132 | }, | ||
| 1133 | layout: std.builtin.TypeInfo.ContainerLayout = .Auto, | 1131 | layout: std.builtin.TypeInfo.ContainerLayout = .Auto, |
| 1134 | }, | 1132 | }, |
| 1133 | |||
| 1134 | // TODO error: values of type '(enum literal)' must be comptime known | ||
| 1135 | pub const InitKind = enum { | ||
| 1136 | enum_type, | ||
| 1137 | tag_type, | ||
| 1138 | none, | ||
| 1139 | }; | ||
| 1135 | }; | 1140 | }; |
| 1136 | }; | 1141 | }; |
| 1137 | 1142 |
src/zir_sema.zig+8| ... | @@ -139,6 +139,14 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError! | ... | @@ -139,6 +139,14 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError! |
| 139 | .switch_range => return analyzeInstSwitchRange(mod, scope, old_inst.castTag(.switch_range).?), | 139 | .switch_range => return analyzeInstSwitchRange(mod, scope, old_inst.castTag(.switch_range).?), |
| 140 | .booland => return analyzeInstBoolOp(mod, scope, old_inst.castTag(.booland).?), | 140 | .booland => return analyzeInstBoolOp(mod, scope, old_inst.castTag(.booland).?), |
| 141 | .boolor => return analyzeInstBoolOp(mod, scope, old_inst.castTag(.boolor).?), | 141 | .boolor => return analyzeInstBoolOp(mod, scope, old_inst.castTag(.boolor).?), |
| 142 | |||
| 143 | .container_field_named, | ||
| 144 | .container_field_typed, | ||
| 145 | .container_field, | ||
| 146 | .enum_type, | ||
| 147 | .union_type, | ||
| 148 | .struct_type, | ||
| 149 | => return mod.fail(scope, old_inst.src, "TODO analyze container instructions", .{}), | ||
| 142 | } | 150 | } |
| 143 | } | 151 | } |
| 144 | 152 |