| author | |
| committer | |
| log | 771f40204e769f92bb28bdb9c44e3ddd9d8c4386 |
| tree | 9335aaca57403c0b71916b2304af2568cbdaed05 |
| parent | 626d94c2a11aecebf59348d5031df58e7337cfb1 |
| parent | e4aefc6d0f9b08a98f566cb8280a6195c08f7a82 |
| signature |
Stage2: more astgen stuff8 files changed, 842 insertions(+), 83 deletions(-)
lib/std/zig.zig+101| ... | ... | @@ -80,6 +80,107 @@ pub fn binNameAlloc( |
| 80 | 80 | } |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | /// Only validates escape sequence characters. | |
| 84 | /// Slice must be valid utf8 starting and ending with "'" and exactly one codepoint in between. | |
| 85 | pub fn parseCharLiteral( | |
| 86 | slice: []const u8, | |
| 87 | bad_index: *usize, // populated if error.InvalidCharacter is returned) | |
| 88 | ) error{InvalidCharacter}!u32 { | |
| 89 | std.debug.assert(slice.len >= 3 and slice[0] == '\'' and slice[slice.len - 1] == '\''); | |
| 90 | ||
| 91 | if (slice[1] == '\\') { | |
| 92 | switch (slice[2]) { | |
| 93 | 'n' => return '\n', | |
| 94 | 'r' => return '\r', | |
| 95 | '\\' => return '\\', | |
| 96 | 't' => return '\t', | |
| 97 | '\'' => return '\'', | |
| 98 | '"' => return '"', | |
| 99 | 'x' => { | |
| 100 | if (slice.len != 6) { | |
| 101 | bad_index.* = slice.len - 2; | |
| 102 | return error.InvalidCharacter; | |
| 103 | } | |
| 104 | ||
| 105 | var value: u32 = 0; | |
| 106 | for (slice[3..5]) |c, i| { | |
| 107 | switch (slice[3]) { | |
| 108 | '0'...'9' => { | |
| 109 | value *= 16; | |
| 110 | value += c - '0'; | |
| 111 | }, | |
| 112 | 'a'...'f' => { | |
| 113 | value *= 16; | |
| 114 | value += c - 'a'; | |
| 115 | }, | |
| 116 | 'A'...'F' => { | |
| 117 | value *= 16; | |
| 118 | value += c - 'a'; | |
| 119 | }, | |
| 120 | else => { | |
| 121 | bad_index.* = i; | |
| 122 | return error.InvalidCharacter; | |
| 123 | }, | |
| 124 | } | |
| 125 | } | |
| 126 | return value; | |
| 127 | }, | |
| 128 | 'u' => { | |
| 129 | if (slice.len < 6 or slice[3] != '{') { | |
| 130 | bad_index.* = 2; | |
| 131 | return error.InvalidCharacter; | |
| 132 | } | |
| 133 | var value: u32 = 0; | |
| 134 | for (slice[4..]) |c, i| { | |
| 135 | if (value > 0x10ffff) { | |
| 136 | bad_index.* = i; | |
| 137 | return error.InvalidCharacter; | |
| 138 | } | |
| 139 | switch (c) { | |
| 140 | '0'...'9' => { | |
| 141 | value *= 16; | |
| 142 | value += c - '0'; | |
| 143 | }, | |
| 144 | 'a'...'f' => { | |
| 145 | value *= 16; | |
| 146 | value += c - 'a'; | |
| 147 | }, | |
| 148 | 'A'...'F' => { | |
| 149 | value *= 16; | |
| 150 | value += c - 'A'; | |
| 151 | }, | |
| 152 | '}' => break, | |
| 153 | else => { | |
| 154 | bad_index.* = i; | |
| 155 | return error.InvalidCharacter; | |
| 156 | }, | |
| 157 | } | |
| 158 | } | |
| 159 | return value; | |
| 160 | }, | |
| 161 | else => { | |
| 162 | bad_index.* = 2; | |
| 163 | return error.InvalidCharacter; | |
| 164 | } | |
| 165 | } | |
| 166 | } | |
| 167 | return std.unicode.utf8Decode(slice[1 .. slice.len - 1]) catch unreachable; | |
| 168 | } | |
| 169 | ||
| 170 | test "parseCharLiteral" { | |
| 171 | var bad_index: usize = undefined; | |
| 172 | std.testing.expectEqual(try parseCharLiteral("'a'", &bad_index), 'a'); | |
| 173 | std.testing.expectEqual(try parseCharLiteral("'ä'", &bad_index), 'ä'); | |
| 174 | std.testing.expectEqual(try parseCharLiteral("'\\x00'", &bad_index), 0); | |
| 175 | std.testing.expectEqual(try parseCharLiteral("'ぁ'", &bad_index), 0x3041); | |
| 176 | std.testing.expectEqual(try parseCharLiteral("'\\u{3041}'", &bad_index), 0x3041); | |
| 177 | ||
| 178 | std.testing.expectError(error.InvalidCharacter, parseCharLiteral("'\\x0'", &bad_index)); | |
| 179 | std.testing.expectError(error.InvalidCharacter, parseCharLiteral("'\\y'", &bad_index)); | |
| 180 | std.testing.expectError(error.InvalidCharacter, parseCharLiteral("'\\u'", &bad_index)); | |
| 181 | std.testing.expectError(error.InvalidCharacter, parseCharLiteral("'\\u{FFFFFF}'", &bad_index)); | |
| 182 | } | |
| 183 | ||
| 83 | 184 | test "" { |
| 84 | 185 | @import("std").meta.refAllDecls(@This()); |
| 85 | 186 | } |
src-self-hosted/Module.zig+66-1| ... | ... | @@ -2902,7 +2902,7 @@ pub fn floatSub(self: *Module, scope: *Scope, float_type: Type, src: usize, lhs: |
| 2902 | 2902 | return Value.initPayload(val_payload); |
| 2903 | 2903 | } |
| 2904 | 2904 | |
| 2905 | pub fn singlePtrType(self: *Module, scope: *Scope, src: usize, mutable: bool, elem_ty: Type) error{OutOfMemory}!Type { | |
| 2905 | pub fn singlePtrType(self: *Module, scope: *Scope, src: usize, mutable: bool, elem_ty: Type) Allocator.Error!Type { | |
| 2906 | 2906 | const type_payload = try scope.arena().create(Type.Payload.Pointer); |
| 2907 | 2907 | type_payload.* = .{ |
| 2908 | 2908 | .base = .{ .tag = if (mutable) .single_mut_pointer else .single_const_pointer }, |
| ... | ... | @@ -2911,6 +2911,71 @@ pub fn singlePtrType(self: *Module, scope: *Scope, src: usize, mutable: bool, el |
| 2911 | 2911 | return Type.initPayload(&type_payload.base); |
| 2912 | 2912 | } |
| 2913 | 2913 | |
| 2914 | pub fn optionalType(self: *Module, scope: *Scope, child_type: Type) Allocator.Error!Type { | |
| 2915 | return Type.initPayload(switch (child_type.tag()) { | |
| 2916 | .single_const_pointer => blk: { | |
| 2917 | const payload = try scope.arena().create(Type.Payload.Pointer); | |
| 2918 | payload.* = .{ | |
| 2919 | .base = .{ .tag = .optional_single_const_pointer }, | |
| 2920 | .pointee_type = child_type.elemType(), | |
| 2921 | }; | |
| 2922 | break :blk &payload.base; | |
| 2923 | }, | |
| 2924 | .single_mut_pointer => blk: { | |
| 2925 | const payload = try scope.arena().create(Type.Payload.Pointer); | |
| 2926 | payload.* = .{ | |
| 2927 | .base = .{ .tag = .optional_single_mut_pointer }, | |
| 2928 | .pointee_type = child_type.elemType(), | |
| 2929 | }; | |
| 2930 | break :blk &payload.base; | |
| 2931 | }, | |
| 2932 | else => blk: { | |
| 2933 | const payload = try scope.arena().create(Type.Payload.Optional); | |
| 2934 | payload.* = .{ | |
| 2935 | .child_type = child_type, | |
| 2936 | }; | |
| 2937 | break :blk &payload.base; | |
| 2938 | }, | |
| 2939 | }); | |
| 2940 | } | |
| 2941 | ||
| 2942 | pub fn arrayType(self: *Module, scope: *Scope, len: u64, sentinel: ?Value, elem_type: Type) Allocator.Error!Type { | |
| 2943 | if (elem_type.eql(Type.initTag(.u8))) { | |
| 2944 | if (sentinel) |some| { | |
| 2945 | if (some.eql(Value.initTag(.zero))) { | |
| 2946 | const payload = try scope.arena().create(Type.Payload.Array_u8_Sentinel0); | |
| 2947 | payload.* = .{ | |
| 2948 | .len = len, | |
| 2949 | }; | |
| 2950 | return Type.initPayload(&payload.base); | |
| 2951 | } | |
| 2952 | } else { | |
| 2953 | const payload = try scope.arena().create(Type.Payload.Array_u8); | |
| 2954 | payload.* = .{ | |
| 2955 | .len = len, | |
| 2956 | }; | |
| 2957 | return Type.initPayload(&payload.base); | |
| 2958 | } | |
| 2959 | } | |
| 2960 | ||
| 2961 | if (sentinel) |some| { | |
| 2962 | const payload = try scope.arena().create(Type.Payload.ArraySentinel); | |
| 2963 | payload.* = .{ | |
| 2964 | .len = len, | |
| 2965 | .sentinel = some, | |
| 2966 | .elem_type = elem_type, | |
| 2967 | }; | |
| 2968 | return Type.initPayload(&payload.base); | |
| 2969 | } | |
| 2970 | ||
| 2971 | const payload = try scope.arena().create(Type.Payload.Array); | |
| 2972 | payload.* = .{ | |
| 2973 | .len = len, | |
| 2974 | .elem_type = elem_type, | |
| 2975 | }; | |
| 2976 | return Type.initPayload(&payload.base); | |
| 2977 | } | |
| 2978 | ||
| 2914 | 2979 | pub fn dumpInst(self: *Module, scope: *Scope, inst: *Inst) void { |
| 2915 | 2980 | const zir_module = scope.namespace(); |
| 2916 | 2981 | const source = zir_module.getSource(self) catch @panic("dumpInst failed to get source"); |
src-self-hosted/astgen.zig+340-24| ... | ... | @@ -20,6 +20,8 @@ pub const ResultLoc = union(enum) { |
| 20 | 20 | /// The expression must generate a pointer rather than a value. For example, the left hand side |
| 21 | 21 | /// of an assignment uses an "LValue" result location. |
| 22 | 22 | lvalue, |
| 23 | /// The expression must generate a pointer | |
| 24 | ref, | |
| 23 | 25 | /// The expression will be type coerced into this type, but it will be evaluated as an rvalue. |
| 24 | 26 | ty: *zir.Inst, |
| 25 | 27 | /// The expression must store its result into this typed pointer. |
| ... | ... | @@ -46,6 +48,132 @@ pub fn typeExpr(mod: *Module, scope: *Scope, type_node: *ast.Node) InnerError!*z |
| 46 | 48 | |
| 47 | 49 | /// Turn Zig AST into untyped ZIR istructions. |
| 48 | 50 | pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerError!*zir.Inst { |
| 51 | if (rl == .lvalue) { | |
| 52 | switch (node.tag) { | |
| 53 | .Root => unreachable, | |
| 54 | .Use => unreachable, | |
| 55 | .TestDecl => unreachable, | |
| 56 | .DocComment => unreachable, | |
| 57 | .VarDecl => unreachable, | |
| 58 | .SwitchCase => unreachable, | |
| 59 | .SwitchElse => unreachable, | |
| 60 | .Else => unreachable, | |
| 61 | .Payload => unreachable, | |
| 62 | .PointerPayload => unreachable, | |
| 63 | .PointerIndexPayload => unreachable, | |
| 64 | .ErrorTag => unreachable, | |
| 65 | .FieldInitializer => unreachable, | |
| 66 | .ContainerField => unreachable, | |
| 67 | ||
| 68 | .Assign, | |
| 69 | .AssignBitAnd, | |
| 70 | .AssignBitOr, | |
| 71 | .AssignBitShiftLeft, | |
| 72 | .AssignBitShiftRight, | |
| 73 | .AssignBitXor, | |
| 74 | .AssignDiv, | |
| 75 | .AssignSub, | |
| 76 | .AssignSubWrap, | |
| 77 | .AssignMod, | |
| 78 | .AssignAdd, | |
| 79 | .AssignAddWrap, | |
| 80 | .AssignMul, | |
| 81 | .AssignMulWrap, | |
| 82 | .Add, | |
| 83 | .AddWrap, | |
| 84 | .Sub, | |
| 85 | .SubWrap, | |
| 86 | .Mul, | |
| 87 | .MulWrap, | |
| 88 | .Div, | |
| 89 | .Mod, | |
| 90 | .BitAnd, | |
| 91 | .BitOr, | |
| 92 | .BitShiftLeft, | |
| 93 | .BitShiftRight, | |
| 94 | .BitXor, | |
| 95 | .BangEqual, | |
| 96 | .EqualEqual, | |
| 97 | .GreaterThan, | |
| 98 | .GreaterOrEqual, | |
| 99 | .LessThan, | |
| 100 | .LessOrEqual, | |
| 101 | .ArrayCat, | |
| 102 | .ArrayMult, | |
| 103 | .BoolAnd, | |
| 104 | .BoolOr, | |
| 105 | .Asm, | |
| 106 | .StringLiteral, | |
| 107 | .IntegerLiteral, | |
| 108 | .Call, | |
| 109 | .Unreachable, | |
| 110 | .Return, | |
| 111 | .If, | |
| 112 | .While, | |
| 113 | .BoolNot, | |
| 114 | .AddressOf, | |
| 115 | .FloatLiteral, | |
| 116 | .UndefinedLiteral, | |
| 117 | .BoolLiteral, | |
| 118 | .NullLiteral, | |
| 119 | .OptionalType, | |
| 120 | .Block, | |
| 121 | .LabeledBlock, | |
| 122 | .Break, | |
| 123 | .PtrType, | |
| 124 | .GroupedExpression, | |
| 125 | .ArrayType, | |
| 126 | .ArrayTypeSentinel, | |
| 127 | .EnumLiteral, | |
| 128 | .MultilineStringLiteral, | |
| 129 | .CharLiteral, | |
| 130 | .Defer, | |
| 131 | .Catch, | |
| 132 | .ErrorUnion, | |
| 133 | .MergeErrorSets, | |
| 134 | .Range, | |
| 135 | .OrElse, | |
| 136 | .Await, | |
| 137 | .BitNot, | |
| 138 | .Negation, | |
| 139 | .NegationWrap, | |
| 140 | .Resume, | |
| 141 | .Try, | |
| 142 | .SliceType, | |
| 143 | .Slice, | |
| 144 | .ArrayInitializer, | |
| 145 | .ArrayInitializerDot, | |
| 146 | .StructInitializer, | |
| 147 | .StructInitializerDot, | |
| 148 | .Switch, | |
| 149 | .For, | |
| 150 | .Suspend, | |
| 151 | .Continue, | |
| 152 | .AnyType, | |
| 153 | .ErrorType, | |
| 154 | .FnProto, | |
| 155 | .AnyFrameType, | |
| 156 | .ErrorSetDecl, | |
| 157 | .ContainerDecl, | |
| 158 | .Comptime, | |
| 159 | .Nosuspend, | |
| 160 | => return mod.failNode(scope, node, "invalid left-hand side to assignment", .{}), | |
| 161 | ||
| 162 | // @field can be assigned to | |
| 163 | .BuiltinCall => { | |
| 164 | const call = node.castTag(.BuiltinCall).?; | |
| 165 | const tree = scope.tree(); | |
| 166 | const builtin_name = tree.tokenSlice(call.builtin_token); | |
| 167 | ||
| 168 | if (!mem.eql(u8, builtin_name, "@field")) { | |
| 169 | return mod.failNode(scope, node, "invalid left-hand side to assignment", .{}); | |
| 170 | } | |
| 171 | }, | |
| 172 | ||
| 173 | // can be assigned to | |
| 174 | .UnwrapOptional, .Deref, .Period, .ArrayAccess, .Identifier => {}, | |
| 175 | } | |
| 176 | } | |
| 49 | 177 | switch (node.tag) { |
| 50 | 178 | .Root => unreachable, // Top-level declaration. |
| 51 | 179 | .Use => unreachable, // Top-level declaration. |
| ... | ... | @@ -60,6 +188,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr |
| 60 | 188 | .PointerIndexPayload => unreachable, // Handled explicitly. |
| 61 | 189 | .ErrorTag => unreachable, // Handled explicitly. |
| 62 | 190 | .FieldInitializer => unreachable, // Handled explicitly. |
| 191 | .ContainerField => unreachable, // Handled explicitly. | |
| 63 | 192 | |
| 64 | 193 | .Assign => return rlWrapVoid(mod, scope, rl, node, try assign(mod, scope, node.castTag(.Assign).?)), |
| 65 | 194 | .AssignBitAnd => return rlWrapVoid(mod, scope, rl, node, try assignOp(mod, scope, node.castTag(.AssignBitAnd).?, .bitand)), |
| ... | ... | @@ -100,6 +229,9 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr |
| 100 | 229 | .ArrayCat => return simpleBinOp(mod, scope, rl, node.castTag(.ArrayCat).?, .array_cat), |
| 101 | 230 | .ArrayMult => return simpleBinOp(mod, scope, rl, node.castTag(.ArrayMult).?, .array_mul), |
| 102 | 231 | |
| 232 | .BoolAnd => return boolBinOp(mod, scope, rl, node.castTag(.BoolAnd).?), | |
| 233 | .BoolOr => return boolBinOp(mod, scope, rl, node.castTag(.BoolOr).?), | |
| 234 | ||
| 103 | 235 | .Identifier => return try identifier(mod, scope, rl, node.castTag(.Identifier).?), |
| 104 | 236 | .Asm => return rlWrap(mod, scope, rl, try assembly(mod, scope, node.castTag(.Asm).?)), |
| 105 | 237 | .StringLiteral => return rlWrap(mod, scope, rl, try stringLiteral(mod, scope, node.castTag(.StringLiteral).?)), |
| ... | ... | @@ -124,11 +256,15 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr |
| 124 | 256 | .LabeledBlock => return labeledBlockExpr(mod, scope, rl, node.castTag(.LabeledBlock).?), |
| 125 | 257 | .Break => return rlWrap(mod, scope, rl, try breakExpr(mod, scope, node.castTag(.Break).?)), |
| 126 | 258 | .PtrType => return rlWrap(mod, scope, rl, try ptrType(mod, scope, node.castTag(.PtrType).?)), |
| 259 | .GroupedExpression => return expr(mod, scope, rl, node.castTag(.GroupedExpression).?.expr), | |
| 260 | .ArrayType => return rlWrap(mod, scope, rl, try arrayType(mod, scope, node.castTag(.ArrayType).?)), | |
| 261 | .ArrayTypeSentinel => return rlWrap(mod, scope, rl, try arrayTypeSentinel(mod, scope, node.castTag(.ArrayTypeSentinel).?)), | |
| 262 | .EnumLiteral => return rlWrap(mod, scope, rl, try enumLiteral(mod, scope, node.castTag(.EnumLiteral).?)), | |
| 263 | .MultilineStringLiteral => return rlWrap(mod, scope, rl, try multilineStrLiteral(mod, scope, node.castTag(.MultilineStringLiteral).?)), | |
| 264 | .CharLiteral => return rlWrap(mod, scope, rl, try charLiteral(mod, scope, node.castTag(.CharLiteral).?)), | |
| 127 | 265 | |
| 128 | 266 | .Defer => return mod.failNode(scope, node, "TODO implement astgen.expr for .Defer", .{}), |
| 129 | 267 | .Catch => return mod.failNode(scope, node, "TODO implement astgen.expr for .Catch", .{}), |
| 130 | .BoolAnd => return mod.failNode(scope, node, "TODO implement astgen.expr for .BoolAnd", .{}), | |
| 131 | .BoolOr => return mod.failNode(scope, node, "TODO implement astgen.expr for .BoolOr", .{}), | |
| 132 | 268 | .ErrorUnion => return mod.failNode(scope, node, "TODO implement astgen.expr for .ErrorUnion", .{}), |
| 133 | 269 | .MergeErrorSets => return mod.failNode(scope, node, "TODO implement astgen.expr for .MergeErrorSets", .{}), |
| 134 | 270 | .Range => return mod.failNode(scope, node, "TODO implement astgen.expr for .Range", .{}), |
| ... | ... | @@ -139,8 +275,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr |
| 139 | 275 | .NegationWrap => return mod.failNode(scope, node, "TODO implement astgen.expr for .NegationWrap", .{}), |
| 140 | 276 | .Resume => return mod.failNode(scope, node, "TODO implement astgen.expr for .Resume", .{}), |
| 141 | 277 | .Try => return mod.failNode(scope, node, "TODO implement astgen.expr for .Try", .{}), |
| 142 | .ArrayType => return mod.failNode(scope, node, "TODO implement astgen.expr for .ArrayType", .{}), | |
| 143 | .ArrayTypeSentinel => return mod.failNode(scope, node, "TODO implement astgen.expr for .ArrayTypeSentinel", .{}), | |
| 144 | 278 | .SliceType => return mod.failNode(scope, node, "TODO implement astgen.expr for .SliceType", .{}), |
| 145 | 279 | .Slice => return mod.failNode(scope, node, "TODO implement astgen.expr for .Slice", .{}), |
| 146 | 280 | .ArrayAccess => return mod.failNode(scope, node, "TODO implement astgen.expr for .ArrayAccess", .{}), |
| ... | ... | @@ -156,15 +290,10 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr |
| 156 | 290 | .ErrorType => return mod.failNode(scope, node, "TODO implement astgen.expr for .ErrorType", .{}), |
| 157 | 291 | .FnProto => return mod.failNode(scope, node, "TODO implement astgen.expr for .FnProto", .{}), |
| 158 | 292 | .AnyFrameType => return mod.failNode(scope, node, "TODO implement astgen.expr for .AnyFrameType", .{}), |
| 159 | .EnumLiteral => return mod.failNode(scope, node, "TODO implement astgen.expr for .EnumLiteral", .{}), | |
| 160 | .MultilineStringLiteral => return mod.failNode(scope, node, "TODO implement astgen.expr for .MultilineStringLiteral", .{}), | |
| 161 | .CharLiteral => return mod.failNode(scope, node, "TODO implement astgen.expr for .CharLiteral", .{}), | |
| 162 | .GroupedExpression => return mod.failNode(scope, node, "TODO implement astgen.expr for .GroupedExpression", .{}), | |
| 163 | 293 | .ErrorSetDecl => return mod.failNode(scope, node, "TODO implement astgen.expr for .ErrorSetDecl", .{}), |
| 164 | 294 | .ContainerDecl => return mod.failNode(scope, node, "TODO implement astgen.expr for .ContainerDecl", .{}), |
| 165 | 295 | .Comptime => return mod.failNode(scope, node, "TODO implement astgen.expr for .Comptime", .{}), |
| 166 | 296 | .Nosuspend => return mod.failNode(scope, node, "TODO implement astgen.expr for .Nosuspend", .{}), |
| 167 | .ContainerField => return mod.failNode(scope, node, "TODO implement astgen.expr for .ContainerField", .{}), | |
| 168 | 297 | } |
| 169 | 298 | } |
| 170 | 299 | |
| ... | ... | @@ -187,7 +316,7 @@ fn breakExpr(mod: *Module, parent_scope: *Scope, node: *ast.Node.ControlFlowExpr |
| 187 | 316 | // proper type inference requires peer type resolution on the block's |
| 188 | 317 | // break operand expressions. |
| 189 | 318 | const branch_rl: ResultLoc = switch (label.result_loc) { |
| 190 | .discard, .none, .ty, .ptr, .lvalue => label.result_loc, | |
| 319 | .discard, .none, .ty, .ptr, .lvalue, .ref => label.result_loc, | |
| 191 | 320 | .inferred_ptr, .bitcasted_ptr, .block_ptr => .{ .block_ptr = label.block_inst }, |
| 192 | 321 | }; |
| 193 | 322 | const operand = try expr(mod, parent_scope, branch_rl, rhs); |
| ... | ... | @@ -426,7 +555,7 @@ fn boolNot(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerErr |
| 426 | 555 | } |
| 427 | 556 | |
| 428 | 557 | fn addressOf(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerError!*zir.Inst { |
| 429 | return expr(mod, scope, .lvalue, node.rhs); | |
| 558 | return expr(mod, scope, .ref, node.rhs); | |
| 430 | 559 | } |
| 431 | 560 | |
| 432 | 561 | fn optionalType(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerError!*zir.Inst { |
| ... | ... | @@ -484,13 +613,65 @@ fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir |
| 484 | 613 | return addZIRInst(mod, scope, src, zir.Inst.PtrType, .{ .child_type = child_type }, kw_args); |
| 485 | 614 | } |
| 486 | 615 | |
| 616 | fn arrayType(mod: *Module, scope: *Scope, node: *ast.Node.ArrayType) !*zir.Inst { | |
| 617 | const tree = scope.tree(); | |
| 618 | const src = tree.token_locs[node.op_token].start; | |
| 619 | const meta_type = try addZIRInstConst(mod, scope, src, .{ | |
| 620 | .ty = Type.initTag(.type), | |
| 621 | .val = Value.initTag(.type_type), | |
| 622 | }); | |
| 623 | const usize_type = try addZIRInstConst(mod, scope, src, .{ | |
| 624 | .ty = Type.initTag(.type), | |
| 625 | .val = Value.initTag(.usize_type), | |
| 626 | }); | |
| 627 | ||
| 628 | // TODO check for [_]T | |
| 629 | const len = try expr(mod, scope, .{ .ty = usize_type }, node.len_expr); | |
| 630 | const child_type = try expr(mod, scope, .{ .ty = meta_type }, node.rhs); | |
| 631 | ||
| 632 | return addZIRBinOp(mod, scope, src, .array_type, len, child_type); | |
| 633 | } | |
| 634 | ||
| 635 | fn arrayTypeSentinel(mod: *Module, scope: *Scope, node: *ast.Node.ArrayTypeSentinel) !*zir.Inst { | |
| 636 | const tree = scope.tree(); | |
| 637 | const src = tree.token_locs[node.op_token].start; | |
| 638 | const meta_type = try addZIRInstConst(mod, scope, src, .{ | |
| 639 | .ty = Type.initTag(.type), | |
| 640 | .val = Value.initTag(.type_type), | |
| 641 | }); | |
| 642 | const usize_type = try addZIRInstConst(mod, scope, src, .{ | |
| 643 | .ty = Type.initTag(.type), | |
| 644 | .val = Value.initTag(.usize_type), | |
| 645 | }); | |
| 646 | ||
| 647 | // TODO check for [_]T | |
| 648 | const len = try expr(mod, scope, .{ .ty = usize_type }, node.len_expr); | |
| 649 | const sentinel_uncasted = try expr(mod, scope, .none, node.sentinel); | |
| 650 | const elem_type = try expr(mod, scope, .{ .ty = meta_type }, node.rhs); | |
| 651 | const sentinel = try addZIRBinOp(mod, scope, src, .as, elem_type, sentinel_uncasted); | |
| 652 | ||
| 653 | return addZIRInst(mod, scope, src, zir.Inst.ArrayTypeSentinel, .{ | |
| 654 | .len = len, | |
| 655 | .sentinel = sentinel, | |
| 656 | .elem_type = elem_type, | |
| 657 | }, .{}); | |
| 658 | } | |
| 659 | ||
| 660 | fn enumLiteral(mod: *Module, scope: *Scope, node: *ast.Node.EnumLiteral) !*zir.Inst { | |
| 661 | const tree = scope.tree(); | |
| 662 | const src = tree.token_locs[node.name].start; | |
| 663 | const name = try identifierTokenString(mod, scope, node.name); | |
| 664 | ||
| 665 | return addZIRInst(mod, scope, src, zir.Inst.EnumLiteral, .{ .name = name }, .{}); | |
| 666 | } | |
| 667 | ||
| 487 | 668 | fn unwrapOptional(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.SimpleSuffixOp) InnerError!*zir.Inst { |
| 488 | 669 | const tree = scope.tree(); |
| 489 | 670 | const src = tree.token_locs[node.rtoken].start; |
| 490 | 671 | |
| 491 | const operand = try expr(mod, scope, .lvalue, node.lhs); | |
| 672 | const operand = try expr(mod, scope, .ref, node.lhs); | |
| 492 | 673 | const unwrapped_ptr = try addZIRUnOp(mod, scope, src, .unwrap_optional_safe, operand); |
| 493 | if (rl == .lvalue) return unwrapped_ptr; | |
| 674 | if (rl == .lvalue or rl == .ref) return unwrapped_ptr; | |
| 494 | 675 | |
| 495 | 676 | return rlWrap(mod, scope, rl, try addZIRUnOp(mod, scope, src, .deref, unwrapped_ptr)); |
| 496 | 677 | } |
| ... | ... | @@ -568,6 +749,88 @@ fn simpleBinOp( |
| 568 | 749 | return rlWrap(mod, scope, rl, result); |
| 569 | 750 | } |
| 570 | 751 | |
| 752 | fn boolBinOp( | |
| 753 | mod: *Module, | |
| 754 | scope: *Scope, | |
| 755 | rl: ResultLoc, | |
| 756 | infix_node: *ast.Node.SimpleInfixOp, | |
| 757 | ) InnerError!*zir.Inst { | |
| 758 | const tree = scope.tree(); | |
| 759 | const src = tree.token_locs[infix_node.op_token].start; | |
| 760 | const bool_type = try addZIRInstConst(mod, scope, src, .{ | |
| 761 | .ty = Type.initTag(.type), | |
| 762 | .val = Value.initTag(.bool_type), | |
| 763 | }); | |
| 764 | ||
| 765 | var block_scope: Scope.GenZIR = .{ | |
| 766 | .parent = scope, | |
| 767 | .decl = scope.decl().?, | |
| 768 | .arena = scope.arena(), | |
| 769 | .instructions = .{}, | |
| 770 | }; | |
| 771 | defer block_scope.instructions.deinit(mod.gpa); | |
| 772 | ||
| 773 | const lhs = try expr(mod, scope, .{ .ty = bool_type }, infix_node.lhs); | |
| 774 | const condbr = try addZIRInstSpecial(mod, &block_scope.base, src, zir.Inst.CondBr, .{ | |
| 775 | .condition = lhs, | |
| 776 | .then_body = undefined, // populated below | |
| 777 | .else_body = undefined, // populated below | |
| 778 | }, .{}); | |
| 779 | ||
| 780 | const block = try addZIRInstBlock(mod, scope, src, .{ | |
| 781 | .instructions = try block_scope.arena.dupe(*zir.Inst, block_scope.instructions.items), | |
| 782 | }); | |
| 783 | ||
| 784 | var rhs_scope: Scope.GenZIR = .{ | |
| 785 | .parent = scope, | |
| 786 | .decl = block_scope.decl, | |
| 787 | .arena = block_scope.arena, | |
| 788 | .instructions = .{}, | |
| 789 | }; | |
| 790 | defer rhs_scope.instructions.deinit(mod.gpa); | |
| 791 | ||
| 792 | const rhs = try expr(mod, &rhs_scope.base, .{ .ty = bool_type }, infix_node.rhs); | |
| 793 | _ = try addZIRInst(mod, &rhs_scope.base, src, zir.Inst.Break, .{ | |
| 794 | .block = block, | |
| 795 | .operand = rhs, | |
| 796 | }, .{}); | |
| 797 | ||
| 798 | var const_scope: Scope.GenZIR = .{ | |
| 799 | .parent = scope, | |
| 800 | .decl = block_scope.decl, | |
| 801 | .arena = block_scope.arena, | |
| 802 | .instructions = .{}, | |
| 803 | }; | |
| 804 | defer const_scope.instructions.deinit(mod.gpa); | |
| 805 | ||
| 806 | const is_bool_and = infix_node.base.tag == .BoolAnd; | |
| 807 | _ = try addZIRInst(mod, &const_scope.base, src, zir.Inst.Break, .{ | |
| 808 | .block = block, | |
| 809 | .operand = try addZIRInstConst(mod, &const_scope.base, src, .{ | |
| 810 | .ty = Type.initTag(.bool), | |
| 811 | .val = if (is_bool_and) Value.initTag(.bool_false) else Value.initTag(.bool_true), | |
| 812 | }), | |
| 813 | }, .{}); | |
| 814 | ||
| 815 | if (is_bool_and) { | |
| 816 | // if lhs // AND | |
| 817 | // break rhs | |
| 818 | // else | |
| 819 | // break false | |
| 820 | condbr.positionals.then_body = .{ .instructions = try rhs_scope.arena.dupe(*zir.Inst, rhs_scope.instructions.items) }; | |
| 821 | condbr.positionals.else_body = .{ .instructions = try const_scope.arena.dupe(*zir.Inst, const_scope.instructions.items) }; | |
| 822 | } else { | |
| 823 | // if lhs // OR | |
| 824 | // break true | |
| 825 | // else | |
| 826 | // break rhs | |
| 827 | condbr.positionals.then_body = .{ .instructions = try const_scope.arena.dupe(*zir.Inst, const_scope.instructions.items) }; | |
| 828 | condbr.positionals.else_body = .{ .instructions = try rhs_scope.arena.dupe(*zir.Inst, rhs_scope.instructions.items) }; | |
| 829 | } | |
| 830 | ||
| 831 | return rlWrap(mod, scope, rl, &block.base); | |
| 832 | } | |
| 833 | ||
| 571 | 834 | const CondKind = union(enum) { |
| 572 | 835 | bool, |
| 573 | 836 | optional: ?*zir.Inst, |
| ... | ... | @@ -583,13 +846,13 @@ const CondKind = union(enum) { |
| 583 | 846 | return try expr(mod, &block_scope.base, .{ .ty = bool_type }, cond_node); |
| 584 | 847 | }, |
| 585 | 848 | .optional => { |
| 586 | const cond_ptr = try expr(mod, &block_scope.base, .lvalue, cond_node); | |
| 849 | const cond_ptr = try expr(mod, &block_scope.base, .ref, cond_node); | |
| 587 | 850 | self.* = .{ .optional = cond_ptr }; |
| 588 | 851 | const result = try addZIRUnOp(mod, &block_scope.base, src, .deref, cond_ptr); |
| 589 | 852 | return try addZIRUnOp(mod, &block_scope.base, src, .isnonnull, result); |
| 590 | 853 | }, |
| 591 | 854 | .err_union => { |
| 592 | const err_ptr = try expr(mod, &block_scope.base, .lvalue, cond_node); | |
| 855 | const err_ptr = try expr(mod, &block_scope.base, .ref, cond_node); | |
| 593 | 856 | self.* = .{ .err_union = err_ptr }; |
| 594 | 857 | const result = try addZIRUnOp(mod, &block_scope.base, src, .deref, err_ptr); |
| 595 | 858 | return try addZIRUnOp(mod, &block_scope.base, src, .iserr, result); |
| ... | ... | @@ -600,7 +863,11 @@ const CondKind = union(enum) { |
| 600 | 863 | fn thenSubScope(self: CondKind, mod: *Module, then_scope: *Scope.GenZIR, src: usize, payload_node: ?*ast.Node) !*Scope { |
| 601 | 864 | if (self == .bool) return &then_scope.base; |
| 602 | 865 | |
| 603 | const payload = payload_node.?.castTag(.PointerPayload).?; | |
| 866 | const payload = payload_node.?.castTag(.PointerPayload) orelse { | |
| 867 | // condition is error union and payload is not explicitly ignored | |
| 868 | _ = try addZIRUnOp(mod, &then_scope.base, src, .ensure_err_payload_void, self.err_union.?); | |
| 869 | return &then_scope.base; | |
| 870 | }; | |
| 604 | 871 | const is_ptr = payload.ptr_token != null; |
| 605 | 872 | const ident_node = payload.value_symbol.castTag(.Identifier).?; |
| 606 | 873 | |
| ... | ... | @@ -680,7 +947,7 @@ fn ifExpr(mod: *Module, scope: *Scope, rl: ResultLoc, if_node: *ast.Node.If) Inn |
| 680 | 947 | // proper type inference requires peer type resolution on the if's |
| 681 | 948 | // branches. |
| 682 | 949 | const branch_rl: ResultLoc = switch (rl) { |
| 683 | .discard, .none, .ty, .ptr, .lvalue => rl, | |
| 950 | .discard, .none, .ty, .ptr, .lvalue, .ref => rl, | |
| 684 | 951 | .inferred_ptr, .bitcasted_ptr, .block_ptr => .{ .block_ptr = block }, |
| 685 | 952 | }; |
| 686 | 953 | |
| ... | ... | @@ -810,7 +1077,7 @@ fn whileExpr(mod: *Module, scope: *Scope, rl: ResultLoc, while_node: *ast.Node.W |
| 810 | 1077 | // proper type inference requires peer type resolution on the while's |
| 811 | 1078 | // branches. |
| 812 | 1079 | const branch_rl: ResultLoc = switch (rl) { |
| 813 | .discard, .none, .ty, .ptr, .lvalue => rl, | |
| 1080 | .discard, .none, .ty, .ptr, .lvalue, .ref => rl, | |
| 814 | 1081 | .inferred_ptr, .bitcasted_ptr, .block_ptr => .{ .block_ptr = while_block }, |
| 815 | 1082 | }; |
| 816 | 1083 | |
| ... | ... | @@ -941,7 +1208,7 @@ fn identifier(mod: *Module, scope: *Scope, rl: ResultLoc, ident: *ast.Node.OneTo |
| 941 | 1208 | .local_ptr => { |
| 942 | 1209 | const local_ptr = s.cast(Scope.LocalPtr).?; |
| 943 | 1210 | if (mem.eql(u8, local_ptr.name, ident_name)) { |
| 944 | if (rl == .lvalue) { | |
| 1211 | if (rl == .lvalue or rl == .ref) { | |
| 945 | 1212 | return local_ptr.ptr; |
| 946 | 1213 | } else { |
| 947 | 1214 | const result = try addZIRUnOp(mod, scope, src, .deref, local_ptr.ptr); |
| ... | ... | @@ -983,6 +1250,53 @@ fn stringLiteral(mod: *Module, scope: *Scope, str_lit: *ast.Node.OneToken) Inner |
| 983 | 1250 | return addZIRInst(mod, scope, src, zir.Inst.Str, .{ .bytes = bytes }, .{}); |
| 984 | 1251 | } |
| 985 | 1252 | |
| 1253 | fn multilineStrLiteral(mod: *Module, scope: *Scope, node: *ast.Node.MultilineStringLiteral) !*zir.Inst { | |
| 1254 | const tree = scope.tree(); | |
| 1255 | const lines = node.linesConst(); | |
| 1256 | const src = tree.token_locs[lines[0]].start; | |
| 1257 | ||
| 1258 | // line lengths and new lines | |
| 1259 | var len = lines.len - 1; | |
| 1260 | for (lines) |line| { | |
| 1261 | len += tree.tokenSlice(line).len - 2; | |
| 1262 | } | |
| 1263 | ||
| 1264 | const bytes = try scope.arena().alloc(u8, len); | |
| 1265 | var i: usize = 0; | |
| 1266 | for (lines) |line, line_i| { | |
| 1267 | if (line_i != 0) { | |
| 1268 | bytes[i] = '\n'; | |
| 1269 | i += 1; | |
| 1270 | } | |
| 1271 | const slice = tree.tokenSlice(line)[2..]; | |
| 1272 | mem.copy(u8, bytes[i..], slice); | |
| 1273 | i += slice.len; | |
| 1274 | } | |
| 1275 | ||
| 1276 | return addZIRInst(mod, scope, src, zir.Inst.Str, .{ .bytes = bytes }, .{}); | |
| 1277 | } | |
| 1278 | ||
| 1279 | fn charLiteral(mod: *Module, scope: *Scope, node: *ast.Node.OneToken) !*zir.Inst { | |
| 1280 | const tree = scope.tree(); | |
| 1281 | const src = tree.token_locs[node.token].start; | |
| 1282 | const slice = tree.tokenSlice(node.token); | |
| 1283 | ||
| 1284 | var bad_index: usize = undefined; | |
| 1285 | const value = std.zig.parseCharLiteral(slice, &bad_index) catch |err| switch (err) { | |
| 1286 | error.InvalidCharacter => { | |
| 1287 | const bad_byte = slice[bad_index]; | |
| 1288 | return mod.fail(scope, src + bad_index, "invalid character: '{c}'\n", .{bad_byte}); | |
| 1289 | }, | |
| 1290 | }; | |
| 1291 | ||
| 1292 | const int_payload = try scope.arena().create(Value.Payload.Int_u64); | |
| 1293 | int_payload.* = .{ .int = value }; | |
| 1294 | return addZIRInstConst(mod, scope, src, .{ | |
| 1295 | .ty = Type.initTag(.comptime_int), | |
| 1296 | .val = Value.initPayload(&int_payload.base), | |
| 1297 | }); | |
| 1298 | } | |
| 1299 | ||
| 986 | 1300 | fn integerLiteral(mod: *Module, scope: *Scope, int_lit: *ast.Node.OneToken) InnerError!*zir.Inst { |
| 987 | 1301 | const arena = scope.arena(); |
| 988 | 1302 | const tree = scope.tree(); |
| ... | ... | @@ -1158,7 +1472,8 @@ fn as(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.BuiltinCall) I |
| 1158 | 1472 | _ = try addZIRUnOp(mod, scope, result.src, .ensure_result_non_error, result); |
| 1159 | 1473 | return result; |
| 1160 | 1474 | }, |
| 1161 | .lvalue => { | |
| 1475 | .lvalue => unreachable, | |
| 1476 | .ref => { | |
| 1162 | 1477 | const result = try expr(mod, scope, .{ .ty = dest_type }, params[1]); |
| 1163 | 1478 | return addZIRUnOp(mod, scope, result.src, .ref, result); |
| 1164 | 1479 | }, |
| ... | ... | @@ -1209,9 +1524,10 @@ fn bitCast(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.BuiltinCa |
| 1209 | 1524 | _ = try addZIRUnOp(mod, scope, result.src, .ensure_result_non_error, result); |
| 1210 | 1525 | return result; |
| 1211 | 1526 | }, |
| 1212 | .lvalue => { | |
| 1213 | const operand = try expr(mod, scope, .lvalue, params[1]); | |
| 1214 | const result = try addZIRBinOp(mod, scope, src, .bitcast_lvalue, dest_type, operand); | |
| 1527 | .lvalue => unreachable, | |
| 1528 | .ref => { | |
| 1529 | const operand = try expr(mod, scope, .ref, params[1]); | |
| 1530 | const result = try addZIRBinOp(mod, scope, src, .bitcast_ref, dest_type, operand); | |
| 1215 | 1531 | return result; |
| 1216 | 1532 | }, |
| 1217 | 1533 | .ty => |result_ty| { |
| ... | ... | @@ -1476,7 +1792,7 @@ fn rlWrap(mod: *Module, scope: *Scope, rl: ResultLoc, result: *zir.Inst) InnerEr |
| 1476 | 1792 | _ = try addZIRUnOp(mod, scope, result.src, .ensure_result_non_error, result); |
| 1477 | 1793 | return result; |
| 1478 | 1794 | }, |
| 1479 | .lvalue => { | |
| 1795 | .lvalue, .ref => { | |
| 1480 | 1796 | // We need a pointer but we have a value. |
| 1481 | 1797 | return addZIRUnOp(mod, scope, result.src, .ref, result); |
| 1482 | 1798 | }, |
src-self-hosted/type.zig+172-22| ... | ... | @@ -65,7 +65,7 @@ pub const Type = extern union { |
| 65 | 65 | .fn_ccc_void_no_args => return .Fn, |
| 66 | 66 | .function => return .Fn, |
| 67 | 67 | |
| 68 | .array, .array_u8_sentinel_0 => return .Array, | |
| 68 | .array, .array_u8_sentinel_0, .array_u8, .array_sentinel => return .Array, | |
| 69 | 69 | .single_const_pointer => return .Pointer, |
| 70 | 70 | .single_mut_pointer => return .Pointer, |
| 71 | 71 | .single_const_pointer_to_comptime_int => return .Pointer, |
| ... | ... | @@ -75,6 +75,7 @@ pub const Type = extern union { |
| 75 | 75 | .optional_single_const_pointer, |
| 76 | 76 | .optional_single_mut_pointer, |
| 77 | 77 | => return .Optional, |
| 78 | .enum_literal => return .EnumLiteral, | |
| 78 | 79 | } |
| 79 | 80 | } |
| 80 | 81 | |
| ... | ... | @@ -127,6 +128,7 @@ pub const Type = extern union { |
| 127 | 128 | if (zig_tag_a != zig_tag_b) |
| 128 | 129 | return false; |
| 129 | 130 | switch (zig_tag_a) { |
| 131 | .EnumLiteral => return true, | |
| 130 | 132 | .Type => return true, |
| 131 | 133 | .Void => return true, |
| 132 | 134 | .Bool => return true, |
| ... | ... | @@ -211,7 +213,6 @@ pub const Type = extern union { |
| 211 | 213 | .Frame, |
| 212 | 214 | .AnyFrame, |
| 213 | 215 | .Vector, |
| 214 | .EnumLiteral, | |
| 215 | 216 | => std.debug.panic("TODO implement Type equality comparison of {} and {}", .{ a, b }), |
| 216 | 217 | } |
| 217 | 218 | } |
| ... | ... | @@ -327,9 +328,11 @@ pub const Type = extern union { |
| 327 | 328 | .fn_ccc_void_no_args, |
| 328 | 329 | .single_const_pointer_to_comptime_int, |
| 329 | 330 | .const_slice_u8, |
| 331 | .enum_literal, | |
| 330 | 332 | => unreachable, |
| 331 | 333 | |
| 332 | 334 | .array_u8_sentinel_0 => return self.copyPayloadShallow(allocator, Payload.Array_u8_Sentinel0), |
| 335 | .array_u8 => return self.copyPayloadShallow(allocator, Payload.Array_u8), | |
| 333 | 336 | .array => { |
| 334 | 337 | const payload = @fieldParentPtr(Payload.Array, "base", self.ptr_otherwise); |
| 335 | 338 | const new_payload = try allocator.create(Payload.Array); |
| ... | ... | @@ -340,6 +343,17 @@ pub const Type = extern union { |
| 340 | 343 | }; |
| 341 | 344 | return Type{ .ptr_otherwise = &new_payload.base }; |
| 342 | 345 | }, |
| 346 | .array_sentinel => { | |
| 347 | const payload = @fieldParentPtr(Payload.ArraySentinel, "base", self.ptr_otherwise); | |
| 348 | const new_payload = try allocator.create(Payload.ArraySentinel); | |
| 349 | new_payload.* = .{ | |
| 350 | .base = payload.base, | |
| 351 | .len = payload.len, | |
| 352 | .sentinel = try payload.sentinel.copy(allocator), | |
| 353 | .elem_type = try payload.elem_type.copy(allocator), | |
| 354 | }; | |
| 355 | return Type{ .ptr_otherwise = &new_payload.base }; | |
| 356 | }, | |
| 343 | 357 | .int_signed => return self.copyPayloadShallow(allocator, Payload.IntSigned), |
| 344 | 358 | .int_unsigned => return self.copyPayloadShallow(allocator, Payload.IntUnsigned), |
| 345 | 359 | .function => { |
| ... | ... | @@ -425,6 +439,7 @@ pub const Type = extern union { |
| 425 | 439 | .noreturn, |
| 426 | 440 | => return out_stream.writeAll(@tagName(t)), |
| 427 | 441 | |
| 442 | .enum_literal => return out_stream.writeAll("@TypeOf(.EnumLiteral)"), | |
| 428 | 443 | .@"null" => return out_stream.writeAll("@TypeOf(null)"), |
| 429 | 444 | .@"undefined" => return out_stream.writeAll("@TypeOf(undefined)"), |
| 430 | 445 | |
| ... | ... | @@ -445,6 +460,10 @@ pub const Type = extern union { |
| 445 | 460 | try payload.return_type.format("", .{}, out_stream); |
| 446 | 461 | }, |
| 447 | 462 | |
| 463 | .array_u8 => { | |
| 464 | const payload = @fieldParentPtr(Payload.Array_u8, "base", ty.ptr_otherwise); | |
| 465 | return out_stream.print("[{}]u8", .{payload.len}); | |
| 466 | }, | |
| 448 | 467 | .array_u8_sentinel_0 => { |
| 449 | 468 | const payload = @fieldParentPtr(Payload.Array_u8_Sentinel0, "base", ty.ptr_otherwise); |
| 450 | 469 | return out_stream.print("[{}:0]u8", .{payload.len}); |
| ... | ... | @@ -455,6 +474,12 @@ pub const Type = extern union { |
| 455 | 474 | ty = payload.elem_type; |
| 456 | 475 | continue; |
| 457 | 476 | }, |
| 477 | .array_sentinel => { | |
| 478 | const payload = @fieldParentPtr(Payload.ArraySentinel, "base", ty.ptr_otherwise); | |
| 479 | try out_stream.print("[{}:{}]", .{ payload.len, payload.sentinel }); | |
| 480 | ty = payload.elem_type; | |
| 481 | continue; | |
| 482 | }, | |
| 458 | 483 | .single_const_pointer => { |
| 459 | 484 | const payload = @fieldParentPtr(Payload.Pointer, "base", ty.ptr_otherwise); |
| 460 | 485 | try out_stream.writeAll("*const "); |
| ... | ... | @@ -539,6 +564,7 @@ pub const Type = extern union { |
| 539 | 564 | .fn_ccc_void_no_args => return Value.initTag(.fn_ccc_void_no_args_type), |
| 540 | 565 | .single_const_pointer_to_comptime_int => return Value.initTag(.single_const_pointer_to_comptime_int_type), |
| 541 | 566 | .const_slice_u8 => return Value.initTag(.const_slice_u8_type), |
| 567 | .enum_literal => return Value.initTag(.enum_literal_type), | |
| 542 | 568 | else => { |
| 543 | 569 | const ty_payload = try allocator.create(Value.Payload.Ty); |
| 544 | 570 | ty_payload.* = .{ .ty = self }; |
| ... | ... | @@ -588,6 +614,8 @@ pub const Type = extern union { |
| 588 | 614 | => true, |
| 589 | 615 | // TODO lazy types |
| 590 | 616 | .array => self.elemType().hasCodeGenBits() and self.arrayLen() != 0, |
| 617 | .array_u8 => self.arrayLen() != 0, | |
| 618 | .array_sentinel => self.elemType().hasCodeGenBits(), | |
| 591 | 619 | .single_const_pointer => self.elemType().hasCodeGenBits(), |
| 592 | 620 | .single_mut_pointer => self.elemType().hasCodeGenBits(), |
| 593 | 621 | .int_signed => self.cast(Payload.IntSigned).?.bits == 0, |
| ... | ... | @@ -601,6 +629,7 @@ pub const Type = extern union { |
| 601 | 629 | .noreturn, |
| 602 | 630 | .@"null", |
| 603 | 631 | .@"undefined", |
| 632 | .enum_literal, | |
| 604 | 633 | => false, |
| 605 | 634 | }; |
| 606 | 635 | } |
| ... | ... | @@ -616,6 +645,7 @@ pub const Type = extern union { |
| 616 | 645 | .i8, |
| 617 | 646 | .bool, |
| 618 | 647 | .array_u8_sentinel_0, |
| 648 | .array_u8, | |
| 619 | 649 | => return 1, |
| 620 | 650 | |
| 621 | 651 | .fn_noreturn_no_args, // represents machine code; not a pointer |
| ... | ... | @@ -659,7 +689,7 @@ pub const Type = extern union { |
| 659 | 689 | |
| 660 | 690 | .anyerror => return 2, // TODO revisit this when we have the concept of the error tag type |
| 661 | 691 | |
| 662 | .array => return self.cast(Payload.Array).?.elem_type.abiAlignment(target), | |
| 692 | .array, .array_sentinel => return self.elemType().abiAlignment(target), | |
| 663 | 693 | |
| 664 | 694 | .int_signed, .int_unsigned => { |
| 665 | 695 | const bits: u16 = if (self.cast(Payload.IntSigned)) |pl| |
| ... | ... | @@ -691,6 +721,7 @@ pub const Type = extern union { |
| 691 | 721 | .noreturn, |
| 692 | 722 | .@"null", |
| 693 | 723 | .@"undefined", |
| 724 | .enum_literal, | |
| 694 | 725 | => unreachable, |
| 695 | 726 | }; |
| 696 | 727 | } |
| ... | ... | @@ -711,18 +742,25 @@ pub const Type = extern union { |
| 711 | 742 | .noreturn => unreachable, |
| 712 | 743 | .@"null" => unreachable, |
| 713 | 744 | .@"undefined" => unreachable, |
| 745 | .enum_literal => unreachable, | |
| 714 | 746 | |
| 715 | 747 | .u8, |
| 716 | 748 | .i8, |
| 717 | 749 | .bool, |
| 718 | 750 | => return 1, |
| 719 | 751 | |
| 720 | .array_u8_sentinel_0 => @fieldParentPtr(Payload.Array_u8_Sentinel0, "base", self.ptr_otherwise).len, | |
| 752 | .array_u8 => @fieldParentPtr(Payload.Array_u8_Sentinel0, "base", self.ptr_otherwise).len, | |
| 753 | .array_u8_sentinel_0 => @fieldParentPtr(Payload.Array_u8_Sentinel0, "base", self.ptr_otherwise).len + 1, | |
| 721 | 754 | .array => { |
| 722 | 755 | const payload = @fieldParentPtr(Payload.Array, "base", self.ptr_otherwise); |
| 723 | 756 | const elem_size = std.math.max(payload.elem_type.abiAlignment(target), payload.elem_type.abiSize(target)); |
| 724 | 757 | return payload.len * elem_size; |
| 725 | 758 | }, |
| 759 | .array_sentinel => { | |
| 760 | const payload = @fieldParentPtr(Payload.ArraySentinel, "base", self.ptr_otherwise); | |
| 761 | const elem_size = std.math.max(payload.elem_type.abiAlignment(target), payload.elem_type.abiSize(target)); | |
| 762 | return (payload.len + 1) * elem_size; | |
| 763 | }, | |
| 726 | 764 | .i16, .u16 => return 2, |
| 727 | 765 | .i32, .u32 => return 4, |
| 728 | 766 | .i64, .u64 => return 8, |
| ... | ... | @@ -818,6 +856,8 @@ pub const Type = extern union { |
| 818 | 856 | .@"null", |
| 819 | 857 | .@"undefined", |
| 820 | 858 | .array, |
| 859 | .array_sentinel, | |
| 860 | .array_u8, | |
| 821 | 861 | .array_u8_sentinel_0, |
| 822 | 862 | .const_slice_u8, |
| 823 | 863 | .fn_noreturn_no_args, |
| ... | ... | @@ -830,6 +870,7 @@ pub const Type = extern union { |
| 830 | 870 | .optional, |
| 831 | 871 | .optional_single_mut_pointer, |
| 832 | 872 | .optional_single_const_pointer, |
| 873 | .enum_literal, | |
| 833 | 874 | => false, |
| 834 | 875 | |
| 835 | 876 | .single_const_pointer, |
| ... | ... | @@ -875,6 +916,8 @@ pub const Type = extern union { |
| 875 | 916 | .@"null", |
| 876 | 917 | .@"undefined", |
| 877 | 918 | .array, |
| 919 | .array_sentinel, | |
| 920 | .array_u8, | |
| 878 | 921 | .array_u8_sentinel_0, |
| 879 | 922 | .single_const_pointer, |
| 880 | 923 | .single_mut_pointer, |
| ... | ... | @@ -889,6 +932,7 @@ pub const Type = extern union { |
| 889 | 932 | .optional, |
| 890 | 933 | .optional_single_mut_pointer, |
| 891 | 934 | .optional_single_const_pointer, |
| 935 | .enum_literal, | |
| 892 | 936 | => false, |
| 893 | 937 | |
| 894 | 938 | .const_slice_u8 => true, |
| ... | ... | @@ -931,6 +975,8 @@ pub const Type = extern union { |
| 931 | 975 | .@"null", |
| 932 | 976 | .@"undefined", |
| 933 | 977 | .array, |
| 978 | .array_sentinel, | |
| 979 | .array_u8, | |
| 934 | 980 | .array_u8_sentinel_0, |
| 935 | 981 | .fn_noreturn_no_args, |
| 936 | 982 | .fn_void_no_args, |
| ... | ... | @@ -943,6 +989,7 @@ pub const Type = extern union { |
| 943 | 989 | .optional, |
| 944 | 990 | .optional_single_mut_pointer, |
| 945 | 991 | .optional_single_const_pointer, |
| 992 | .enum_literal, | |
| 946 | 993 | => false, |
| 947 | 994 | |
| 948 | 995 | .single_const_pointer, |
| ... | ... | @@ -988,6 +1035,8 @@ pub const Type = extern union { |
| 988 | 1035 | .@"null", |
| 989 | 1036 | .@"undefined", |
| 990 | 1037 | .array, |
| 1038 | .array_sentinel, | |
| 1039 | .array_u8, | |
| 991 | 1040 | .array_u8_sentinel_0, |
| 992 | 1041 | .fn_noreturn_no_args, |
| 993 | 1042 | .fn_void_no_args, |
| ... | ... | @@ -1003,6 +1052,7 @@ pub const Type = extern union { |
| 1003 | 1052 | .optional, |
| 1004 | 1053 | .optional_single_mut_pointer, |
| 1005 | 1054 | .optional_single_const_pointer, |
| 1055 | .enum_literal, | |
| 1006 | 1056 | => false, |
| 1007 | 1057 | }; |
| 1008 | 1058 | } |
| ... | ... | @@ -1023,6 +1073,45 @@ pub const Type = extern union { |
| 1023 | 1073 | } |
| 1024 | 1074 | } |
| 1025 | 1075 | |
| 1076 | /// Returns if type can be used for a runtime variable | |
| 1077 | pub fn isValidVarType(self: Type) bool { | |
| 1078 | var ty = self; | |
| 1079 | while (true) switch (ty.zigTypeTag()) { | |
| 1080 | .Bool, | |
| 1081 | .Int, | |
| 1082 | .Float, | |
| 1083 | .ErrorSet, | |
| 1084 | .Enum, | |
| 1085 | .Frame, | |
| 1086 | .AnyFrame, | |
| 1087 | .Vector, | |
| 1088 | => return true, | |
| 1089 | ||
| 1090 | .BoundFn, | |
| 1091 | .ComptimeFloat, | |
| 1092 | .ComptimeInt, | |
| 1093 | .EnumLiteral, | |
| 1094 | .NoReturn, | |
| 1095 | .Type, | |
| 1096 | .Void, | |
| 1097 | .Undefined, | |
| 1098 | .Null, | |
| 1099 | .Opaque, | |
| 1100 | => return false, | |
| 1101 | ||
| 1102 | .Optional => { | |
| 1103 | var buf: Payload.Pointer = undefined; | |
| 1104 | return ty.optionalChild(&buf).isValidVarType(); | |
| 1105 | }, | |
| 1106 | .Pointer, .Array => ty = ty.elemType(), | |
| 1107 | ||
| 1108 | .ErrorUnion => @panic("TODO fn isValidVarType"), | |
| 1109 | .Fn => @panic("TODO fn isValidVarType"), | |
| 1110 | .Struct => @panic("TODO struct isValidVarType"), | |
| 1111 | .Union => @panic("TODO union isValidVarType"), | |
| 1112 | }; | |
| 1113 | } | |
| 1114 | ||
| 1026 | 1115 | /// Asserts the type is a pointer or array type. |
| 1027 | 1116 | pub fn elemType(self: Type) Type { |
| 1028 | 1117 | return switch (self.tag()) { |
| ... | ... | @@ -1069,12 +1158,14 @@ pub const Type = extern union { |
| 1069 | 1158 | .optional, |
| 1070 | 1159 | .optional_single_const_pointer, |
| 1071 | 1160 | .optional_single_mut_pointer, |
| 1161 | .enum_literal, | |
| 1072 | 1162 | => unreachable, |
| 1073 | 1163 | |
| 1074 | 1164 | .array => self.cast(Payload.Array).?.elem_type, |
| 1165 | .array_sentinel => self.cast(Payload.ArraySentinel).?.elem_type, | |
| 1075 | 1166 | .single_const_pointer => self.castPointer().?.pointee_type, |
| 1076 | 1167 | .single_mut_pointer => self.castPointer().?.pointee_type, |
| 1077 | .array_u8_sentinel_0, .const_slice_u8 => Type.initTag(.u8), | |
| 1168 | .array_u8, .array_u8_sentinel_0, .const_slice_u8 => Type.initTag(.u8), | |
| 1078 | 1169 | .single_const_pointer_to_comptime_int => Type.initTag(.comptime_int), |
| 1079 | 1170 | }; |
| 1080 | 1171 | } |
| ... | ... | @@ -1173,9 +1264,12 @@ pub const Type = extern union { |
| 1173 | 1264 | .optional, |
| 1174 | 1265 | .optional_single_mut_pointer, |
| 1175 | 1266 | .optional_single_const_pointer, |
| 1267 | .enum_literal, | |
| 1176 | 1268 | => unreachable, |
| 1177 | 1269 | |
| 1178 | 1270 | .array => self.cast(Payload.Array).?.len, |
| 1271 | .array_sentinel => self.cast(Payload.ArraySentinel).?.len, | |
| 1272 | .array_u8 => self.cast(Payload.Array_u8).?.len, | |
| 1179 | 1273 | .array_u8_sentinel_0 => self.cast(Payload.Array_u8_Sentinel0).?.len, |
| 1180 | 1274 | }; |
| 1181 | 1275 | } |
| ... | ... | @@ -1230,9 +1324,11 @@ pub const Type = extern union { |
| 1230 | 1324 | .optional, |
| 1231 | 1325 | .optional_single_mut_pointer, |
| 1232 | 1326 | .optional_single_const_pointer, |
| 1327 | .enum_literal, | |
| 1233 | 1328 | => unreachable, |
| 1234 | 1329 | |
| 1235 | .array => return null, | |
| 1330 | .array, .array_u8 => return null, | |
| 1331 | .array_sentinel => return self.cast(Payload.ArraySentinel).?.sentinel, | |
| 1236 | 1332 | .array_u8_sentinel_0 => return Value.initTag(.zero), |
| 1237 | 1333 | }; |
| 1238 | 1334 | } |
| ... | ... | @@ -1266,10 +1362,12 @@ pub const Type = extern union { |
| 1266 | 1362 | .fn_ccc_void_no_args, |
| 1267 | 1363 | .function, |
| 1268 | 1364 | .array, |
| 1365 | .array_sentinel, | |
| 1366 | .array_u8, | |
| 1367 | .array_u8_sentinel_0, | |
| 1269 | 1368 | .single_const_pointer, |
| 1270 | 1369 | .single_mut_pointer, |
| 1271 | 1370 | .single_const_pointer_to_comptime_int, |
| 1272 | .array_u8_sentinel_0, | |
| 1273 | 1371 | .const_slice_u8, |
| 1274 | 1372 | .int_unsigned, |
| 1275 | 1373 | .u8, |
| ... | ... | @@ -1284,6 +1382,7 @@ pub const Type = extern union { |
| 1284 | 1382 | .optional, |
| 1285 | 1383 | .optional_single_mut_pointer, |
| 1286 | 1384 | .optional_single_const_pointer, |
| 1385 | .enum_literal, | |
| 1287 | 1386 | => false, |
| 1288 | 1387 | |
| 1289 | 1388 | .int_signed, |
| ... | ... | @@ -1324,10 +1423,12 @@ pub const Type = extern union { |
| 1324 | 1423 | .fn_ccc_void_no_args, |
| 1325 | 1424 | .function, |
| 1326 | 1425 | .array, |
| 1426 | .array_sentinel, | |
| 1427 | .array_u8, | |
| 1428 | .array_u8_sentinel_0, | |
| 1327 | 1429 | .single_const_pointer, |
| 1328 | 1430 | .single_mut_pointer, |
| 1329 | 1431 | .single_const_pointer_to_comptime_int, |
| 1330 | .array_u8_sentinel_0, | |
| 1331 | 1432 | .const_slice_u8, |
| 1332 | 1433 | .int_signed, |
| 1333 | 1434 | .i8, |
| ... | ... | @@ -1342,6 +1443,7 @@ pub const Type = extern union { |
| 1342 | 1443 | .optional, |
| 1343 | 1444 | .optional_single_mut_pointer, |
| 1344 | 1445 | .optional_single_const_pointer, |
| 1446 | .enum_literal, | |
| 1345 | 1447 | => false, |
| 1346 | 1448 | |
| 1347 | 1449 | .int_unsigned, |
| ... | ... | @@ -1382,14 +1484,17 @@ pub const Type = extern union { |
| 1382 | 1484 | .fn_ccc_void_no_args, |
| 1383 | 1485 | .function, |
| 1384 | 1486 | .array, |
| 1487 | .array_sentinel, | |
| 1488 | .array_u8, | |
| 1489 | .array_u8_sentinel_0, | |
| 1385 | 1490 | .single_const_pointer, |
| 1386 | 1491 | .single_mut_pointer, |
| 1387 | 1492 | .single_const_pointer_to_comptime_int, |
| 1388 | .array_u8_sentinel_0, | |
| 1389 | 1493 | .const_slice_u8, |
| 1390 | 1494 | .optional, |
| 1391 | 1495 | .optional_single_mut_pointer, |
| 1392 | 1496 | .optional_single_const_pointer, |
| 1497 | .enum_literal, | |
| 1393 | 1498 | => unreachable, |
| 1394 | 1499 | |
| 1395 | 1500 | .int_unsigned => .{ .signed = false, .bits = self.cast(Payload.IntUnsigned).?.bits }, |
| ... | ... | @@ -1438,10 +1543,12 @@ pub const Type = extern union { |
| 1438 | 1543 | .fn_ccc_void_no_args, |
| 1439 | 1544 | .function, |
| 1440 | 1545 | .array, |
| 1546 | .array_sentinel, | |
| 1547 | .array_u8, | |
| 1548 | .array_u8_sentinel_0, | |
| 1441 | 1549 | .single_const_pointer, |
| 1442 | 1550 | .single_mut_pointer, |
| 1443 | 1551 | .single_const_pointer_to_comptime_int, |
| 1444 | .array_u8_sentinel_0, | |
| 1445 | 1552 | .const_slice_u8, |
| 1446 | 1553 | .int_unsigned, |
| 1447 | 1554 | .int_signed, |
| ... | ... | @@ -1456,6 +1563,7 @@ pub const Type = extern union { |
| 1456 | 1563 | .optional, |
| 1457 | 1564 | .optional_single_mut_pointer, |
| 1458 | 1565 | .optional_single_const_pointer, |
| 1566 | .enum_literal, | |
| 1459 | 1567 | => false, |
| 1460 | 1568 | |
| 1461 | 1569 | .usize, |
| ... | ... | @@ -1523,10 +1631,12 @@ pub const Type = extern union { |
| 1523 | 1631 | .@"null", |
| 1524 | 1632 | .@"undefined", |
| 1525 | 1633 | .array, |
| 1634 | .array_sentinel, | |
| 1635 | .array_u8, | |
| 1636 | .array_u8_sentinel_0, | |
| 1526 | 1637 | .single_const_pointer, |
| 1527 | 1638 | .single_mut_pointer, |
| 1528 | 1639 | .single_const_pointer_to_comptime_int, |
| 1529 | .array_u8_sentinel_0, | |
| 1530 | 1640 | .const_slice_u8, |
| 1531 | 1641 | .u8, |
| 1532 | 1642 | .i8, |
| ... | ... | @@ -1551,6 +1661,7 @@ pub const Type = extern union { |
| 1551 | 1661 | .optional, |
| 1552 | 1662 | .optional_single_mut_pointer, |
| 1553 | 1663 | .optional_single_const_pointer, |
| 1664 | .enum_literal, | |
| 1554 | 1665 | => unreachable, |
| 1555 | 1666 | }; |
| 1556 | 1667 | } |
| ... | ... | @@ -1584,10 +1695,12 @@ pub const Type = extern union { |
| 1584 | 1695 | .@"null", |
| 1585 | 1696 | .@"undefined", |
| 1586 | 1697 | .array, |
| 1698 | .array_sentinel, | |
| 1699 | .array_u8, | |
| 1700 | .array_u8_sentinel_0, | |
| 1587 | 1701 | .single_const_pointer, |
| 1588 | 1702 | .single_mut_pointer, |
| 1589 | 1703 | .single_const_pointer_to_comptime_int, |
| 1590 | .array_u8_sentinel_0, | |
| 1591 | 1704 | .const_slice_u8, |
| 1592 | 1705 | .u8, |
| 1593 | 1706 | .i8, |
| ... | ... | @@ -1612,6 +1725,7 @@ pub const Type = extern union { |
| 1612 | 1725 | .optional, |
| 1613 | 1726 | .optional_single_mut_pointer, |
| 1614 | 1727 | .optional_single_const_pointer, |
| 1728 | .enum_literal, | |
| 1615 | 1729 | => unreachable, |
| 1616 | 1730 | } |
| 1617 | 1731 | } |
| ... | ... | @@ -1644,10 +1758,12 @@ pub const Type = extern union { |
| 1644 | 1758 | .@"null", |
| 1645 | 1759 | .@"undefined", |
| 1646 | 1760 | .array, |
| 1761 | .array_sentinel, | |
| 1762 | .array_u8, | |
| 1763 | .array_u8_sentinel_0, | |
| 1647 | 1764 | .single_const_pointer, |
| 1648 | 1765 | .single_mut_pointer, |
| 1649 | 1766 | .single_const_pointer_to_comptime_int, |
| 1650 | .array_u8_sentinel_0, | |
| 1651 | 1767 | .const_slice_u8, |
| 1652 | 1768 | .u8, |
| 1653 | 1769 | .i8, |
| ... | ... | @@ -1672,6 +1788,7 @@ pub const Type = extern union { |
| 1672 | 1788 | .optional, |
| 1673 | 1789 | .optional_single_mut_pointer, |
| 1674 | 1790 | .optional_single_const_pointer, |
| 1791 | .enum_literal, | |
| 1675 | 1792 | => unreachable, |
| 1676 | 1793 | } |
| 1677 | 1794 | } |
| ... | ... | @@ -1704,10 +1821,12 @@ pub const Type = extern union { |
| 1704 | 1821 | .@"null", |
| 1705 | 1822 | .@"undefined", |
| 1706 | 1823 | .array, |
| 1824 | .array_sentinel, | |
| 1825 | .array_u8, | |
| 1826 | .array_u8_sentinel_0, | |
| 1707 | 1827 | .single_const_pointer, |
| 1708 | 1828 | .single_mut_pointer, |
| 1709 | 1829 | .single_const_pointer_to_comptime_int, |
| 1710 | .array_u8_sentinel_0, | |
| 1711 | 1830 | .const_slice_u8, |
| 1712 | 1831 | .u8, |
| 1713 | 1832 | .i8, |
| ... | ... | @@ -1732,6 +1851,7 @@ pub const Type = extern union { |
| 1732 | 1851 | .optional, |
| 1733 | 1852 | .optional_single_mut_pointer, |
| 1734 | 1853 | .optional_single_const_pointer, |
| 1854 | .enum_literal, | |
| 1735 | 1855 | => unreachable, |
| 1736 | 1856 | }; |
| 1737 | 1857 | } |
| ... | ... | @@ -1761,10 +1881,12 @@ pub const Type = extern union { |
| 1761 | 1881 | .@"null", |
| 1762 | 1882 | .@"undefined", |
| 1763 | 1883 | .array, |
| 1884 | .array_sentinel, | |
| 1885 | .array_u8, | |
| 1886 | .array_u8_sentinel_0, | |
| 1764 | 1887 | .single_const_pointer, |
| 1765 | 1888 | .single_mut_pointer, |
| 1766 | 1889 | .single_const_pointer_to_comptime_int, |
| 1767 | .array_u8_sentinel_0, | |
| 1768 | 1890 | .const_slice_u8, |
| 1769 | 1891 | .u8, |
| 1770 | 1892 | .i8, |
| ... | ... | @@ -1789,6 +1911,7 @@ pub const Type = extern union { |
| 1789 | 1911 | .optional, |
| 1790 | 1912 | .optional_single_mut_pointer, |
| 1791 | 1913 | .optional_single_const_pointer, |
| 1914 | .enum_literal, | |
| 1792 | 1915 | => unreachable, |
| 1793 | 1916 | }; |
| 1794 | 1917 | } |
| ... | ... | @@ -1818,10 +1941,12 @@ pub const Type = extern union { |
| 1818 | 1941 | .@"null", |
| 1819 | 1942 | .@"undefined", |
| 1820 | 1943 | .array, |
| 1944 | .array_sentinel, | |
| 1945 | .array_u8, | |
| 1946 | .array_u8_sentinel_0, | |
| 1821 | 1947 | .single_const_pointer, |
| 1822 | 1948 | .single_mut_pointer, |
| 1823 | 1949 | .single_const_pointer_to_comptime_int, |
| 1824 | .array_u8_sentinel_0, | |
| 1825 | 1950 | .const_slice_u8, |
| 1826 | 1951 | .u8, |
| 1827 | 1952 | .i8, |
| ... | ... | @@ -1846,6 +1971,7 @@ pub const Type = extern union { |
| 1846 | 1971 | .optional, |
| 1847 | 1972 | .optional_single_mut_pointer, |
| 1848 | 1973 | .optional_single_const_pointer, |
| 1974 | .enum_literal, | |
| 1849 | 1975 | => unreachable, |
| 1850 | 1976 | }; |
| 1851 | 1977 | } |
| ... | ... | @@ -1895,14 +2021,17 @@ pub const Type = extern union { |
| 1895 | 2021 | .fn_ccc_void_no_args, |
| 1896 | 2022 | .function, |
| 1897 | 2023 | .array, |
| 2024 | .array_sentinel, | |
| 2025 | .array_u8, | |
| 2026 | .array_u8_sentinel_0, | |
| 1898 | 2027 | .single_const_pointer, |
| 1899 | 2028 | .single_mut_pointer, |
| 1900 | 2029 | .single_const_pointer_to_comptime_int, |
| 1901 | .array_u8_sentinel_0, | |
| 1902 | 2030 | .const_slice_u8, |
| 1903 | 2031 | .optional, |
| 1904 | 2032 | .optional_single_mut_pointer, |
| 1905 | 2033 | .optional_single_const_pointer, |
| 2034 | .enum_literal, | |
| 1906 | 2035 | => false, |
| 1907 | 2036 | }; |
| 1908 | 2037 | } |
| ... | ... | @@ -1944,12 +2073,14 @@ pub const Type = extern union { |
| 1944 | 2073 | .fn_ccc_void_no_args, |
| 1945 | 2074 | .function, |
| 1946 | 2075 | .single_const_pointer_to_comptime_int, |
| 2076 | .array_sentinel, | |
| 1947 | 2077 | .array_u8_sentinel_0, |
| 1948 | 2078 | .const_slice_u8, |
| 1949 | 2079 | .c_void, |
| 1950 | 2080 | .optional, |
| 1951 | 2081 | .optional_single_mut_pointer, |
| 1952 | 2082 | .optional_single_const_pointer, |
| 2083 | .enum_literal, | |
| 1953 | 2084 | => return null, |
| 1954 | 2085 | |
| 1955 | 2086 | .void => return Value.initTag(.void_value), |
| ... | ... | @@ -1971,11 +2102,10 @@ pub const Type = extern union { |
| 1971 | 2102 | return null; |
| 1972 | 2103 | } |
| 1973 | 2104 | }, |
| 1974 | .array => { | |
| 1975 | const array = ty.cast(Payload.Array).?; | |
| 1976 | if (array.len == 0) | |
| 2105 | .array, .array_u8 => { | |
| 2106 | if (ty.arrayLen() == 0) | |
| 1977 | 2107 | return Value.initTag(.empty_array); |
| 1978 | ty = array.elem_type; | |
| 2108 | ty = ty.elemType(); | |
| 1979 | 2109 | continue; |
| 1980 | 2110 | }, |
| 1981 | 2111 | .single_const_pointer, .single_mut_pointer => { |
| ... | ... | @@ -2022,7 +2152,6 @@ pub const Type = extern union { |
| 2022 | 2152 | .fn_ccc_void_no_args, |
| 2023 | 2153 | .function, |
| 2024 | 2154 | .single_const_pointer_to_comptime_int, |
| 2025 | .array_u8_sentinel_0, | |
| 2026 | 2155 | .const_slice_u8, |
| 2027 | 2156 | .c_void, |
| 2028 | 2157 | .void, |
| ... | ... | @@ -2032,11 +2161,15 @@ pub const Type = extern union { |
| 2032 | 2161 | .int_unsigned, |
| 2033 | 2162 | .int_signed, |
| 2034 | 2163 | .array, |
| 2164 | .array_sentinel, | |
| 2165 | .array_u8, | |
| 2166 | .array_u8_sentinel_0, | |
| 2035 | 2167 | .single_const_pointer, |
| 2036 | 2168 | .single_mut_pointer, |
| 2037 | 2169 | .optional, |
| 2038 | 2170 | .optional_single_mut_pointer, |
| 2039 | 2171 | .optional_single_const_pointer, |
| 2172 | .enum_literal, | |
| 2040 | 2173 | => return false, |
| 2041 | 2174 | }; |
| 2042 | 2175 | } |
| ... | ... | @@ -2080,6 +2213,7 @@ pub const Type = extern union { |
| 2080 | 2213 | comptime_int, |
| 2081 | 2214 | comptime_float, |
| 2082 | 2215 | noreturn, |
| 2216 | enum_literal, | |
| 2083 | 2217 | @"null", |
| 2084 | 2218 | @"undefined", |
| 2085 | 2219 | fn_noreturn_no_args, |
| ... | ... | @@ -2090,8 +2224,10 @@ pub const Type = extern union { |
| 2090 | 2224 | const_slice_u8, // See last_no_payload_tag below. |
| 2091 | 2225 | // After this, the tag requires a payload. |
| 2092 | 2226 | |
| 2227 | array_u8, | |
| 2093 | 2228 | array_u8_sentinel_0, |
| 2094 | 2229 | array, |
| 2230 | array_sentinel, | |
| 2095 | 2231 | single_const_pointer, |
| 2096 | 2232 | single_mut_pointer, |
| 2097 | 2233 | int_signed, |
| ... | ... | @@ -2114,11 +2250,25 @@ pub const Type = extern union { |
| 2114 | 2250 | len: u64, |
| 2115 | 2251 | }; |
| 2116 | 2252 | |
| 2253 | pub const Array_u8 = struct { | |
| 2254 | base: Payload = Payload{ .tag = .array_u8 }, | |
| 2255 | ||
| 2256 | len: u64, | |
| 2257 | }; | |
| 2258 | ||
| 2117 | 2259 | pub const Array = struct { |
| 2118 | 2260 | base: Payload = Payload{ .tag = .array }, |
| 2119 | 2261 | |
| 2262 | len: u64, | |
| 2120 | 2263 | elem_type: Type, |
| 2264 | }; | |
| 2265 | ||
| 2266 | pub const ArraySentinel = struct { | |
| 2267 | base: Payload = Payload{ .tag = .array_sentinel }, | |
| 2268 | ||
| 2121 | 2269 | len: u64, |
| 2270 | sentinel: Value, | |
| 2271 | elem_type: Type, | |
| 2122 | 2272 | }; |
| 2123 | 2273 | |
| 2124 | 2274 | pub const Pointer = struct { |
src-self-hosted/value.zig+33-2| ... | ... | @@ -60,6 +60,7 @@ pub const Value = extern union { |
| 60 | 60 | fn_ccc_void_no_args_type, |
| 61 | 61 | single_const_pointer_to_comptime_int_type, |
| 62 | 62 | const_slice_u8_type, |
| 63 | enum_literal_type, | |
| 63 | 64 | |
| 64 | 65 | undef, |
| 65 | 66 | zero, |
| ... | ... | @@ -87,6 +88,7 @@ pub const Value = extern union { |
| 87 | 88 | float_32, |
| 88 | 89 | float_64, |
| 89 | 90 | float_128, |
| 91 | enum_literal, | |
| 90 | 92 | |
| 91 | 93 | pub const last_no_payload_tag = Tag.bool_false; |
| 92 | 94 | pub const no_payload_count = @enumToInt(last_no_payload_tag) + 1; |
| ... | ... | @@ -164,6 +166,7 @@ pub const Value = extern union { |
| 164 | 166 | .fn_ccc_void_no_args_type, |
| 165 | 167 | .single_const_pointer_to_comptime_int_type, |
| 166 | 168 | .const_slice_u8_type, |
| 169 | .enum_literal_type, | |
| 167 | 170 | .undef, |
| 168 | 171 | .zero, |
| 169 | 172 | .void_value, |
| ... | ... | @@ -213,7 +216,7 @@ pub const Value = extern union { |
| 213 | 216 | }; |
| 214 | 217 | return Value{ .ptr_otherwise = &new_payload.base }; |
| 215 | 218 | }, |
| 216 | .bytes => return self.copyPayloadShallow(allocator, Payload.Bytes), | |
| 219 | .enum_literal, .bytes => return self.copyPayloadShallow(allocator, Payload.Bytes), | |
| 217 | 220 | .repeated => { |
| 218 | 221 | const payload = @fieldParentPtr(Payload.Repeated, "base", self.ptr_otherwise); |
| 219 | 222 | const new_payload = try allocator.create(Payload.Repeated); |
| ... | ... | @@ -285,6 +288,7 @@ pub const Value = extern union { |
| 285 | 288 | .fn_ccc_void_no_args_type => return out_stream.writeAll("fn() callconv(.C) void"), |
| 286 | 289 | .single_const_pointer_to_comptime_int_type => return out_stream.writeAll("*const comptime_int"), |
| 287 | 290 | .const_slice_u8_type => return out_stream.writeAll("[]const u8"), |
| 291 | .enum_literal_type => return out_stream.writeAll("@TypeOf(.EnumLiteral)"), | |
| 288 | 292 | |
| 289 | 293 | .null_value => return out_stream.writeAll("null"), |
| 290 | 294 | .undef => return out_stream.writeAll("undefined"), |
| ... | ... | @@ -318,7 +322,7 @@ pub const Value = extern union { |
| 318 | 322 | val = elem_ptr.array_ptr; |
| 319 | 323 | }, |
| 320 | 324 | .empty_array => return out_stream.writeAll(".{}"), |
| 321 | .bytes => return std.zig.renderStringLiteral(self.cast(Payload.Bytes).?.data, out_stream), | |
| 325 | .enum_literal, .bytes => return std.zig.renderStringLiteral(self.cast(Payload.Bytes).?.data, out_stream), | |
| 322 | 326 | .repeated => { |
| 323 | 327 | try out_stream.writeAll("(repeated) "); |
| 324 | 328 | val = val.cast(Payload.Repeated).?.val; |
| ... | ... | @@ -391,6 +395,7 @@ pub const Value = extern union { |
| 391 | 395 | .fn_ccc_void_no_args_type => Type.initTag(.fn_ccc_void_no_args), |
| 392 | 396 | .single_const_pointer_to_comptime_int_type => Type.initTag(.single_const_pointer_to_comptime_int), |
| 393 | 397 | .const_slice_u8_type => Type.initTag(.const_slice_u8), |
| 398 | .enum_literal_type => Type.initTag(.enum_literal), | |
| 394 | 399 | |
| 395 | 400 | .undef, |
| 396 | 401 | .zero, |
| ... | ... | @@ -414,6 +419,7 @@ pub const Value = extern union { |
| 414 | 419 | .float_32, |
| 415 | 420 | .float_64, |
| 416 | 421 | .float_128, |
| 422 | .enum_literal, | |
| 417 | 423 | => unreachable, |
| 418 | 424 | }; |
| 419 | 425 | } |
| ... | ... | @@ -462,6 +468,7 @@ pub const Value = extern union { |
| 462 | 468 | .fn_ccc_void_no_args_type, |
| 463 | 469 | .single_const_pointer_to_comptime_int_type, |
| 464 | 470 | .const_slice_u8_type, |
| 471 | .enum_literal_type, | |
| 465 | 472 | .null_value, |
| 466 | 473 | .function, |
| 467 | 474 | .ref_val, |
| ... | ... | @@ -476,6 +483,7 @@ pub const Value = extern union { |
| 476 | 483 | .void_value, |
| 477 | 484 | .unreachable_value, |
| 478 | 485 | .empty_array, |
| 486 | .enum_literal, | |
| 479 | 487 | => unreachable, |
| 480 | 488 | |
| 481 | 489 | .undef => unreachable, |
| ... | ... | @@ -537,6 +545,7 @@ pub const Value = extern union { |
| 537 | 545 | .fn_ccc_void_no_args_type, |
| 538 | 546 | .single_const_pointer_to_comptime_int_type, |
| 539 | 547 | .const_slice_u8_type, |
| 548 | .enum_literal_type, | |
| 540 | 549 | .null_value, |
| 541 | 550 | .function, |
| 542 | 551 | .ref_val, |
| ... | ... | @@ -551,6 +560,7 @@ pub const Value = extern union { |
| 551 | 560 | .void_value, |
| 552 | 561 | .unreachable_value, |
| 553 | 562 | .empty_array, |
| 563 | .enum_literal, | |
| 554 | 564 | => unreachable, |
| 555 | 565 | |
| 556 | 566 | .undef => unreachable, |
| ... | ... | @@ -612,6 +622,7 @@ pub const Value = extern union { |
| 612 | 622 | .fn_ccc_void_no_args_type, |
| 613 | 623 | .single_const_pointer_to_comptime_int_type, |
| 614 | 624 | .const_slice_u8_type, |
| 625 | .enum_literal_type, | |
| 615 | 626 | .null_value, |
| 616 | 627 | .function, |
| 617 | 628 | .ref_val, |
| ... | ... | @@ -626,6 +637,7 @@ pub const Value = extern union { |
| 626 | 637 | .void_value, |
| 627 | 638 | .unreachable_value, |
| 628 | 639 | .empty_array, |
| 640 | .enum_literal, | |
| 629 | 641 | => unreachable, |
| 630 | 642 | |
| 631 | 643 | .undef => unreachable, |
| ... | ... | @@ -713,6 +725,7 @@ pub const Value = extern union { |
| 713 | 725 | .fn_ccc_void_no_args_type, |
| 714 | 726 | .single_const_pointer_to_comptime_int_type, |
| 715 | 727 | .const_slice_u8_type, |
| 728 | .enum_literal_type, | |
| 716 | 729 | .null_value, |
| 717 | 730 | .function, |
| 718 | 731 | .ref_val, |
| ... | ... | @@ -728,6 +741,7 @@ pub const Value = extern union { |
| 728 | 741 | .void_value, |
| 729 | 742 | .unreachable_value, |
| 730 | 743 | .empty_array, |
| 744 | .enum_literal, | |
| 731 | 745 | => unreachable, |
| 732 | 746 | |
| 733 | 747 | .zero, |
| ... | ... | @@ -793,6 +807,7 @@ pub const Value = extern union { |
| 793 | 807 | .fn_ccc_void_no_args_type, |
| 794 | 808 | .single_const_pointer_to_comptime_int_type, |
| 795 | 809 | .const_slice_u8_type, |
| 810 | .enum_literal_type, | |
| 796 | 811 | .null_value, |
| 797 | 812 | .function, |
| 798 | 813 | .ref_val, |
| ... | ... | @@ -807,6 +822,7 @@ pub const Value = extern union { |
| 807 | 822 | .void_value, |
| 808 | 823 | .unreachable_value, |
| 809 | 824 | .empty_array, |
| 825 | .enum_literal, | |
| 810 | 826 | => unreachable, |
| 811 | 827 | |
| 812 | 828 | .zero, |
| ... | ... | @@ -953,6 +969,7 @@ pub const Value = extern union { |
| 953 | 969 | .fn_ccc_void_no_args_type, |
| 954 | 970 | .single_const_pointer_to_comptime_int_type, |
| 955 | 971 | .const_slice_u8_type, |
| 972 | .enum_literal_type, | |
| 956 | 973 | .bool_true, |
| 957 | 974 | .bool_false, |
| 958 | 975 | .null_value, |
| ... | ... | @@ -970,6 +987,7 @@ pub const Value = extern union { |
| 970 | 987 | .empty_array, |
| 971 | 988 | .void_value, |
| 972 | 989 | .unreachable_value, |
| 990 | .enum_literal, | |
| 973 | 991 | => unreachable, |
| 974 | 992 | |
| 975 | 993 | .zero => false, |
| ... | ... | @@ -1025,6 +1043,7 @@ pub const Value = extern union { |
| 1025 | 1043 | .fn_ccc_void_no_args_type, |
| 1026 | 1044 | .single_const_pointer_to_comptime_int_type, |
| 1027 | 1045 | .const_slice_u8_type, |
| 1046 | .enum_literal_type, | |
| 1028 | 1047 | .null_value, |
| 1029 | 1048 | .function, |
| 1030 | 1049 | .ref_val, |
| ... | ... | @@ -1036,6 +1055,7 @@ pub const Value = extern union { |
| 1036 | 1055 | .void_value, |
| 1037 | 1056 | .unreachable_value, |
| 1038 | 1057 | .empty_array, |
| 1058 | .enum_literal, | |
| 1039 | 1059 | => unreachable, |
| 1040 | 1060 | |
| 1041 | 1061 | .zero, |
| ... | ... | @@ -1102,6 +1122,11 @@ pub const Value = extern union { |
| 1102 | 1122 | } |
| 1103 | 1123 | |
| 1104 | 1124 | pub fn eql(a: Value, b: Value) bool { |
| 1125 | if (a.tag() == b.tag() and a.tag() == .enum_literal) { | |
| 1126 | const a_name = @fieldParentPtr(Payload.Bytes, "base", a.ptr_otherwise).data; | |
| 1127 | const b_name = @fieldParentPtr(Payload.Bytes, "base", b.ptr_otherwise).data; | |
| 1128 | return std.mem.eql(u8, a_name, b_name); | |
| 1129 | } | |
| 1105 | 1130 | // TODO non numerical comparisons |
| 1106 | 1131 | return compare(a, .eq, b); |
| 1107 | 1132 | } |
| ... | ... | @@ -1151,6 +1176,7 @@ pub const Value = extern union { |
| 1151 | 1176 | .fn_ccc_void_no_args_type, |
| 1152 | 1177 | .single_const_pointer_to_comptime_int_type, |
| 1153 | 1178 | .const_slice_u8_type, |
| 1179 | .enum_literal_type, | |
| 1154 | 1180 | .zero, |
| 1155 | 1181 | .bool_true, |
| 1156 | 1182 | .bool_false, |
| ... | ... | @@ -1170,6 +1196,7 @@ pub const Value = extern union { |
| 1170 | 1196 | .void_value, |
| 1171 | 1197 | .unreachable_value, |
| 1172 | 1198 | .empty_array, |
| 1199 | .enum_literal, | |
| 1173 | 1200 | => unreachable, |
| 1174 | 1201 | |
| 1175 | 1202 | .ref_val => self.cast(Payload.RefVal).?.val, |
| ... | ... | @@ -1227,6 +1254,7 @@ pub const Value = extern union { |
| 1227 | 1254 | .fn_ccc_void_no_args_type, |
| 1228 | 1255 | .single_const_pointer_to_comptime_int_type, |
| 1229 | 1256 | .const_slice_u8_type, |
| 1257 | .enum_literal_type, | |
| 1230 | 1258 | .zero, |
| 1231 | 1259 | .bool_true, |
| 1232 | 1260 | .bool_false, |
| ... | ... | @@ -1246,6 +1274,7 @@ pub const Value = extern union { |
| 1246 | 1274 | .float_128, |
| 1247 | 1275 | .void_value, |
| 1248 | 1276 | .unreachable_value, |
| 1277 | .enum_literal, | |
| 1249 | 1278 | => unreachable, |
| 1250 | 1279 | |
| 1251 | 1280 | .empty_array => unreachable, // out of bounds array index |
| ... | ... | @@ -1320,6 +1349,7 @@ pub const Value = extern union { |
| 1320 | 1349 | .fn_ccc_void_no_args_type, |
| 1321 | 1350 | .single_const_pointer_to_comptime_int_type, |
| 1322 | 1351 | .const_slice_u8_type, |
| 1352 | .enum_literal_type, | |
| 1323 | 1353 | .zero, |
| 1324 | 1354 | .empty_array, |
| 1325 | 1355 | .bool_true, |
| ... | ... | @@ -1339,6 +1369,7 @@ pub const Value = extern union { |
| 1339 | 1369 | .float_64, |
| 1340 | 1370 | .float_128, |
| 1341 | 1371 | .void_value, |
| 1372 | .enum_literal, | |
| 1342 | 1373 | => false, |
| 1343 | 1374 | |
| 1344 | 1375 | .undef => unreachable, |
src-self-hosted/zir.zig+47-5| ... | ... | @@ -47,6 +47,10 @@ pub const Inst = struct { |
| 47 | 47 | array_cat, |
| 48 | 48 | /// Array multiplication `a ** b` |
| 49 | 49 | array_mul, |
| 50 | /// Create an array type | |
| 51 | array_type, | |
| 52 | /// Create an array type with sentinel | |
| 53 | array_type_sentinel, | |
| 50 | 54 | /// Function parameter value. These must be first in a function's main block, |
| 51 | 55 | /// in respective order with the parameters. |
| 52 | 56 | arg, |
| ... | ... | @@ -58,11 +62,11 @@ pub const Inst = struct { |
| 58 | 62 | bitand, |
| 59 | 63 | /// TODO delete this instruction, it has no purpose. |
| 60 | 64 | bitcast, |
| 61 | /// An arbitrary typed pointer, which is to be used as an L-Value, is pointer-casted | |
| 62 | /// to a new L-Value. The destination type is given by LHS. The cast is to be evaluated | |
| 65 | /// An arbitrary typed pointer is pointer-casted to a new Pointer. | |
| 66 | /// The destination type is given by LHS. The cast is to be evaluated | |
| 63 | 67 | /// as if it were a bit-cast operation from the operand pointer element type to the |
| 64 | 68 | /// provided destination type. |
| 65 | bitcast_lvalue, | |
| 69 | bitcast_ref, | |
| 66 | 70 | /// A typed result location pointer is bitcasted to a new result location pointer. |
| 67 | 71 | /// The new result location pointer has an inferred type. |
| 68 | 72 | bitcast_result_ptr, |
| ... | ... | @@ -225,6 +229,10 @@ pub const Inst = struct { |
| 225 | 229 | unwrap_err_safe, |
| 226 | 230 | /// Same as previous, but without safety checks. Used for orelse, if and while |
| 227 | 231 | unwrap_err_unsafe, |
| 232 | /// Takes a *E!T and raises a compiler error if T != void | |
| 233 | ensure_err_payload_void, | |
| 234 | /// Enum literal | |
| 235 | enum_literal, | |
| 228 | 236 | |
| 229 | 237 | pub fn Type(tag: Tag) type { |
| 230 | 238 | return switch (tag) { |
| ... | ... | @@ -250,7 +258,7 @@ pub const Inst = struct { |
| 250 | 258 | .ensure_result_non_error, |
| 251 | 259 | .bitcast_result_ptr, |
| 252 | 260 | .ref, |
| 253 | .bitcast_lvalue, | |
| 261 | .bitcast_ref, | |
| 254 | 262 | .typeof, |
| 255 | 263 | .single_const_ptr_type, |
| 256 | 264 | .single_mut_ptr_type, |
| ... | ... | @@ -259,12 +267,14 @@ pub const Inst = struct { |
| 259 | 267 | .unwrap_optional_unsafe, |
| 260 | 268 | .unwrap_err_safe, |
| 261 | 269 | .unwrap_err_unsafe, |
| 270 | .ensure_err_payload_void, | |
| 262 | 271 | => UnOp, |
| 263 | 272 | |
| 264 | 273 | .add, |
| 265 | 274 | .addwrap, |
| 266 | 275 | .array_cat, |
| 267 | 276 | .array_mul, |
| 277 | .array_type, | |
| 268 | 278 | .bitand, |
| 269 | 279 | .bitor, |
| 270 | 280 | .div, |
| ... | ... | @@ -291,6 +301,7 @@ pub const Inst = struct { |
| 291 | 301 | => BinOp, |
| 292 | 302 | |
| 293 | 303 | .arg => Arg, |
| 304 | .array_type_sentinel => ArrayTypeSentinel, | |
| 294 | 305 | .block => Block, |
| 295 | 306 | .@"break" => Break, |
| 296 | 307 | .breakvoid => BreakVoid, |
| ... | ... | @@ -317,6 +328,7 @@ pub const Inst = struct { |
| 317 | 328 | .elemptr => ElemPtr, |
| 318 | 329 | .condbr => CondBr, |
| 319 | 330 | .ptr_type => PtrType, |
| 331 | .enum_literal => EnumLiteral, | |
| 320 | 332 | }; |
| 321 | 333 | } |
| 322 | 334 | |
| ... | ... | @@ -330,12 +342,14 @@ pub const Inst = struct { |
| 330 | 342 | .alloc_inferred, |
| 331 | 343 | .array_cat, |
| 332 | 344 | .array_mul, |
| 345 | .array_type, | |
| 346 | .array_type_sentinel, | |
| 333 | 347 | .arg, |
| 334 | 348 | .as, |
| 335 | 349 | .@"asm", |
| 336 | 350 | .bitand, |
| 337 | 351 | .bitcast, |
| 338 | .bitcast_lvalue, | |
| 352 | .bitcast_ref, | |
| 339 | 353 | .bitcast_result_ptr, |
| 340 | 354 | .bitor, |
| 341 | 355 | .block, |
| ... | ... | @@ -398,6 +412,8 @@ pub const Inst = struct { |
| 398 | 412 | .unwrap_err_safe, |
| 399 | 413 | .unwrap_err_unsafe, |
| 400 | 414 | .ptr_type, |
| 415 | .ensure_err_payload_void, | |
| 416 | .enum_literal, | |
| 401 | 417 | => false, |
| 402 | 418 | |
| 403 | 419 | .@"break", |
| ... | ... | @@ -845,6 +861,28 @@ pub const Inst = struct { |
| 845 | 861 | sentinel: ?*Inst = null, |
| 846 | 862 | }, |
| 847 | 863 | }; |
| 864 | ||
| 865 | pub const ArrayTypeSentinel = struct { | |
| 866 | pub const base_tag = Tag.array_type_sentinel; | |
| 867 | base: Inst, | |
| 868 | ||
| 869 | positionals: struct { | |
| 870 | len: *Inst, | |
| 871 | sentinel: *Inst, | |
| 872 | elem_type: *Inst, | |
| 873 | }, | |
| 874 | kw_args: struct {}, | |
| 875 | }; | |
| 876 | ||
| 877 | pub const EnumLiteral = struct { | |
| 878 | pub const base_tag = Tag.enum_literal; | |
| 879 | base: Inst, | |
| 880 | ||
| 881 | positionals: struct { | |
| 882 | name: []const u8, | |
| 883 | }, | |
| 884 | kw_args: struct {}, | |
| 885 | }; | |
| 848 | 886 | }; |
| 849 | 887 | |
| 850 | 888 | pub const ErrorMsg = struct { |
| ... | ... | @@ -1922,6 +1960,10 @@ const EmitZIR = struct { |
| 1922 | 1960 | return self.emitUnnamedDecl(&str_inst.base); |
| 1923 | 1961 | }, |
| 1924 | 1962 | .Void => return self.emitPrimitive(src, .void_value), |
| 1963 | .Bool => if (typed_value.val.toBool()) | |
| 1964 | return self.emitPrimitive(src, .@"true") | |
| 1965 | else | |
| 1966 | return self.emitPrimitive(src, .@"false"), | |
| 1925 | 1967 | else => |t| std.debug.panic("TODO implement emitTypedValue for {}", .{@tagName(t)}), |
| 1926 | 1968 | } |
| 1927 | 1969 | } |
src-self-hosted/zir_sema.zig+51-29| ... | ... | @@ -29,7 +29,7 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError! |
| 29 | 29 | .alloc => return analyzeInstAlloc(mod, scope, old_inst.castTag(.alloc).?), |
| 30 | 30 | .alloc_inferred => return analyzeInstAllocInferred(mod, scope, old_inst.castTag(.alloc_inferred).?), |
| 31 | 31 | .arg => return analyzeInstArg(mod, scope, old_inst.castTag(.arg).?), |
| 32 | .bitcast_lvalue => return analyzeInstBitCastLValue(mod, scope, old_inst.castTag(.bitcast_lvalue).?), | |
| 32 | .bitcast_ref => return analyzeInstBitCastRef(mod, scope, old_inst.castTag(.bitcast_ref).?), | |
| 33 | 33 | .bitcast_result_ptr => return analyzeInstBitCastResultPtr(mod, scope, old_inst.castTag(.bitcast_result_ptr).?), |
| 34 | 34 | .block => return analyzeInstBlock(mod, scope, old_inst.castTag(.block).?), |
| 35 | 35 | .@"break" => return analyzeInstBreak(mod, scope, old_inst.castTag(.@"break").?), |
| ... | ... | @@ -112,6 +112,10 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError! |
| 112 | 112 | .unwrap_optional_unsafe => return analyzeInstUnwrapOptional(mod, scope, old_inst.castTag(.unwrap_optional_unsafe).?, false), |
| 113 | 113 | .unwrap_err_safe => return analyzeInstUnwrapErr(mod, scope, old_inst.castTag(.unwrap_err_safe).?, true), |
| 114 | 114 | .unwrap_err_unsafe => return analyzeInstUnwrapErr(mod, scope, old_inst.castTag(.unwrap_err_unsafe).?, false), |
| 115 | .ensure_err_payload_void => return analyzeInstEnsureErrPayloadVoid(mod, scope, old_inst.castTag(.ensure_err_payload_void).?), | |
| 116 | .array_type => return analyzeInstArrayType(mod, scope, old_inst.castTag(.array_type).?), | |
| 117 | .array_type_sentinel => return analyzeInstArrayTypeSentinel(mod, scope, old_inst.castTag(.array_type_sentinel).?), | |
| 118 | .enum_literal => return analyzeInstEnumLiteral(mod, scope, old_inst.castTag(.enum_literal).?), | |
| 115 | 119 | } |
| 116 | 120 | } |
| 117 | 121 | |
| ... | ... | @@ -295,8 +299,8 @@ fn analyzeInstCoerceResultBlockPtr( |
| 295 | 299 | return mod.fail(scope, inst.base.src, "TODO implement analyzeInstCoerceResultBlockPtr", .{}); |
| 296 | 300 | } |
| 297 | 301 | |
| 298 | fn analyzeInstBitCastLValue(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst { | |
| 299 | return mod.fail(scope, inst.base.src, "TODO implement analyzeInstBitCastLValue", .{}); | |
| 302 | fn analyzeInstBitCastRef(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst { | |
| 303 | return mod.fail(scope, inst.base.src, "TODO implement analyzeInstBitCastRef", .{}); | |
| 300 | 304 | } |
| 301 | 305 | |
| 302 | 306 | fn analyzeInstBitCastResultPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst { |
| ... | ... | @@ -361,6 +365,10 @@ fn analyzeInstEnsureResultNonError(mod: *Module, scope: *Scope, inst: *zir.Inst. |
| 361 | 365 | |
| 362 | 366 | fn analyzeInstAlloc(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst { |
| 363 | 367 | const var_type = try resolveType(mod, scope, inst.positionals.operand); |
| 368 | // TODO this should happen only for var allocs | |
| 369 | if (!var_type.isValidVarType()) { | |
| 370 | return mod.fail(scope, inst.base.src, "variable of type '{}' must be const or comptime", .{var_type}); | |
| 371 | } | |
| 364 | 372 | const ptr_type = try mod.singlePtrType(scope, inst.base.src, true, var_type); |
| 365 | 373 | const b = try mod.requireRuntimeBlock(scope, inst.base.src); |
| 366 | 374 | return mod.addNoOp(b, inst.base.src, ptr_type, .alloc); |
| ... | ... | @@ -675,31 +683,36 @@ fn analyzeInstIntType(mod: *Module, scope: *Scope, inttype: *zir.Inst.IntType) I |
| 675 | 683 | fn analyzeInstOptionalType(mod: *Module, scope: *Scope, optional: *zir.Inst.UnOp) InnerError!*Inst { |
| 676 | 684 | const child_type = try resolveType(mod, scope, optional.positionals.operand); |
| 677 | 685 | |
| 678 | return mod.constType(scope, optional.base.src, Type.initPayload(switch (child_type.tag()) { | |
| 679 | .single_const_pointer => blk: { | |
| 680 | const payload = try scope.arena().create(Type.Payload.Pointer); | |
| 681 | payload.* = .{ | |
| 682 | .base = .{ .tag = .optional_single_const_pointer }, | |
| 683 | .pointee_type = child_type.elemType(), | |
| 684 | }; | |
| 685 | break :blk &payload.base; | |
| 686 | }, | |
| 687 | .single_mut_pointer => blk: { | |
| 688 | const payload = try scope.arena().create(Type.Payload.Pointer); | |
| 689 | payload.* = .{ | |
| 690 | .base = .{ .tag = .optional_single_mut_pointer }, | |
| 691 | .pointee_type = child_type.elemType(), | |
| 692 | }; | |
| 693 | break :blk &payload.base; | |
| 694 | }, | |
| 695 | else => blk: { | |
| 696 | const payload = try scope.arena().create(Type.Payload.Optional); | |
| 697 | payload.* = .{ | |
| 698 | .child_type = child_type, | |
| 699 | }; | |
| 700 | break :blk &payload.base; | |
| 701 | }, | |
| 702 | })); | |
| 686 | return mod.constType(scope, optional.base.src, try mod.optionalType(scope, child_type)); | |
| 687 | } | |
| 688 | ||
| 689 | fn analyzeInstArrayType(mod: *Module, scope: *Scope, array: *zir.Inst.BinOp) InnerError!*Inst { | |
| 690 | // TODO these should be lazily evaluated | |
| 691 | const len = try resolveInstConst(mod, scope, array.positionals.lhs); | |
| 692 | const elem_type = try resolveType(mod, scope, array.positionals.rhs); | |
| 693 | ||
| 694 | return mod.constType(scope, array.base.src, try mod.arrayType(scope, len.val.toUnsignedInt(), null, elem_type)); | |
| 695 | } | |
| 696 | ||
| 697 | fn analyzeInstArrayTypeSentinel(mod: *Module, scope: *Scope, array: *zir.Inst.ArrayTypeSentinel) InnerError!*Inst { | |
| 698 | // TODO these should be lazily evaluated | |
| 699 | const len = try resolveInstConst(mod, scope, array.positionals.len); | |
| 700 | const sentinel = try resolveInstConst(mod, scope, array.positionals.sentinel); | |
| 701 | const elem_type = try resolveType(mod, scope, array.positionals.elem_type); | |
| 702 | ||
| 703 | return mod.constType(scope, array.base.src, try mod.arrayType(scope, len.val.toUnsignedInt(), sentinel.val, elem_type)); | |
| 704 | } | |
| 705 | ||
| 706 | fn analyzeInstEnumLiteral(mod: *Module, scope: *Scope, inst: *zir.Inst.EnumLiteral) InnerError!*Inst { | |
| 707 | const payload = try scope.arena().create(Value.Payload.Bytes); | |
| 708 | payload.* = .{ | |
| 709 | .base = .{ .tag = .enum_literal }, | |
| 710 | .data = try scope.arena().dupe(u8, inst.positionals.name), | |
| 711 | }; | |
| 712 | return mod.constInst(scope, inst.base.src, .{ | |
| 713 | .ty = Type.initTag(.enum_literal), | |
| 714 | .val = Value.initPayload(&payload.base), | |
| 715 | }); | |
| 703 | 716 | } |
| 704 | 717 | |
| 705 | 718 | fn analyzeInstUnwrapOptional(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp, safety_check: bool) InnerError!*Inst { |
| ... | ... | @@ -735,6 +748,10 @@ fn analyzeInstUnwrapErr(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp, saf |
| 735 | 748 | return mod.fail(scope, unwrap.base.src, "TODO implement analyzeInstUnwrapErr", .{}); |
| 736 | 749 | } |
| 737 | 750 | |
| 751 | fn analyzeInstEnsureErrPayloadVoid(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp) InnerError!*Inst { | |
| 752 | return mod.fail(scope, unwrap.base.src, "TODO implement analyzeInstEnsureErrPayloadVoid", .{}); | |
| 753 | } | |
| 754 | ||
| 738 | 755 | fn analyzeInstFnType(mod: *Module, scope: *Scope, fntype: *zir.Inst.FnType) InnerError!*Inst { |
| 739 | 756 | const return_type = try resolveType(mod, scope, fntype.positionals.return_type); |
| 740 | 757 | |
| ... | ... | @@ -760,7 +777,12 @@ fn analyzeInstFnType(mod: *Module, scope: *Scope, fntype: *zir.Inst.FnType) Inne |
| 760 | 777 | const arena = scope.arena(); |
| 761 | 778 | const param_types = try arena.alloc(Type, fntype.positionals.param_types.len); |
| 762 | 779 | for (fntype.positionals.param_types) |param_type, i| { |
| 763 | param_types[i] = try resolveType(mod, scope, param_type); | |
| 780 | const resolved = try resolveType(mod, scope, param_type); | |
| 781 | // TODO skip for comptime params | |
| 782 | if (!resolved.isValidVarType()) { | |
| 783 | return mod.fail(scope, param_type.src, "parameter of type '{}' must be declared comptime", .{resolved}); | |
| 784 | } | |
| 785 | param_types[i] = resolved; | |
| 764 | 786 | } |
| 765 | 787 | |
| 766 | 788 | const payload = try arena.create(Type.Payload.Function); |
test/stage2/compare_output.zig+32| ... | ... | @@ -543,6 +543,38 @@ pub fn addCases(ctx: *TestContext) !void { |
| 543 | 543 | , |
| 544 | 544 | "", |
| 545 | 545 | ); |
| 546 | ||
| 547 | case.addCompareOutput( | |
| 548 | \\export fn _start() noreturn { | |
| 549 | \\ const ignore = | |
| 550 | \\ \\ cool thx | |
| 551 | \\ \\ | |
| 552 | \\ ; | |
| 553 | \\ add('ぁ', '\x03'); | |
| 554 | \\ | |
| 555 | \\ exit(); | |
| 556 | \\} | |
| 557 | \\ | |
| 558 | \\fn add(a: u32, b: u32) void { | |
| 559 | \\ assert(a + b == 12356); | |
| 560 | \\} | |
| 561 | \\ | |
| 562 | \\pub fn assert(ok: bool) void { | |
| 563 | \\ if (!ok) unreachable; // assertion failure | |
| 564 | \\} | |
| 565 | \\ | |
| 566 | \\fn exit() noreturn { | |
| 567 | \\ asm volatile ("syscall" | |
| 568 | \\ : | |
| 569 | \\ : [number] "{rax}" (231), | |
| 570 | \\ [arg1] "{rdi}" (0) | |
| 571 | \\ : "rcx", "r11", "memory" | |
| 572 | \\ ); | |
| 573 | \\ unreachable; | |
| 574 | \\} | |
| 575 | , | |
| 576 | "", | |
| 577 | ); | |
| 546 | 578 | } |
| 547 | 579 | |
| 548 | 580 | { |