authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-24 16:13:10-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-24 16:13:10-07:00
log84d50c892d41850f666f05e05472c23ffabee434
tree8c7bbc4bc5df69091726ed6f79bc3b4121b2e36c
parentb30c5380765bea26647a72ec55f9191824e00d4e

stage2: astgen: kill the "lvalue" ResultLoc tag


1 files changed, 137 insertions(+), 139 deletions(-)

src-self-hosted/astgen.zig+137-139
......@@ -18,9 +18,7 @@ pub const ResultLoc = union(enum) {
1818 /// The expression has an inferred type, and it will be evaluated as an rvalue.
1919 none,
2020 /// The expression must generate a pointer rather than a value. For example, the left hand side
21 /// of an assignment uses an "LValue" result location.
22 lvalue,
23 /// The expression must generate a pointer
21 /// of an assignment uses this kind of result location.
2422 ref,
2523 /// The expression will be type coerced into this type, but it will be evaluated as an rvalue.
2624 ty: *zir.Inst,
......@@ -46,134 +44,136 @@ pub fn typeExpr(mod: *Module, scope: *Scope, type_node: *ast.Node) InnerError!*z
4644 return expr(mod, scope, type_rl, type_node);
4745}
4846
49/// Turn Zig AST into untyped ZIR istructions.
50pub 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 },
47fn lvalExpr(mod: *Module, scope: *Scope, node: *ast.Node) InnerError!*zir.Inst {
48 switch (node.tag) {
49 .Root => unreachable,
50 .Use => unreachable,
51 .TestDecl => unreachable,
52 .DocComment => unreachable,
53 .VarDecl => unreachable,
54 .SwitchCase => unreachable,
55 .SwitchElse => unreachable,
56 .Else => unreachable,
57 .Payload => unreachable,
58 .PointerPayload => unreachable,
59 .PointerIndexPayload => unreachable,
60 .ErrorTag => unreachable,
61 .FieldInitializer => unreachable,
62 .ContainerField => unreachable,
63
64 .Assign,
65 .AssignBitAnd,
66 .AssignBitOr,
67 .AssignBitShiftLeft,
68 .AssignBitShiftRight,
69 .AssignBitXor,
70 .AssignDiv,
71 .AssignSub,
72 .AssignSubWrap,
73 .AssignMod,
74 .AssignAdd,
75 .AssignAddWrap,
76 .AssignMul,
77 .AssignMulWrap,
78 .Add,
79 .AddWrap,
80 .Sub,
81 .SubWrap,
82 .Mul,
83 .MulWrap,
84 .Div,
85 .Mod,
86 .BitAnd,
87 .BitOr,
88 .BitShiftLeft,
89 .BitShiftRight,
90 .BitXor,
91 .BangEqual,
92 .EqualEqual,
93 .GreaterThan,
94 .GreaterOrEqual,
95 .LessThan,
96 .LessOrEqual,
97 .ArrayCat,
98 .ArrayMult,
99 .BoolAnd,
100 .BoolOr,
101 .Asm,
102 .StringLiteral,
103 .IntegerLiteral,
104 .Call,
105 .Unreachable,
106 .Return,
107 .If,
108 .While,
109 .BoolNot,
110 .AddressOf,
111 .FloatLiteral,
112 .UndefinedLiteral,
113 .BoolLiteral,
114 .NullLiteral,
115 .OptionalType,
116 .Block,
117 .LabeledBlock,
118 .Break,
119 .PtrType,
120 .GroupedExpression,
121 .ArrayType,
122 .ArrayTypeSentinel,
123 .EnumLiteral,
124 .MultilineStringLiteral,
125 .CharLiteral,
126 .Defer,
127 .Catch,
128 .ErrorUnion,
129 .MergeErrorSets,
130 .Range,
131 .OrElse,
132 .Await,
133 .BitNot,
134 .Negation,
135 .NegationWrap,
136 .Resume,
137 .Try,
138 .SliceType,
139 .Slice,
140 .ArrayInitializer,
141 .ArrayInitializerDot,
142 .StructInitializer,
143 .StructInitializerDot,
144 .Switch,
145 .For,
146 .Suspend,
147 .Continue,
148 .AnyType,
149 .ErrorType,
150 .FnProto,
151 .AnyFrameType,
152 .ErrorSetDecl,
153 .ContainerDecl,
154 .Comptime,
155 .Nosuspend,
156 => return mod.failNode(scope, node, "invalid left-hand side to assignment", .{}),
157
158 // @field can be assigned to
159 .BuiltinCall => {
160 const call = node.castTag(.BuiltinCall).?;
161 const tree = scope.tree();
162 const builtin_name = tree.tokenSlice(call.builtin_token);
163
164 if (!mem.eql(u8, builtin_name, "@field")) {
165 return mod.failNode(scope, node, "invalid left-hand side to assignment", .{});
166 }
167 },
172168
173 // can be assigned to
174 .UnwrapOptional, .Deref, .Period, .ArrayAccess, .Identifier => {},
175 }
169 // can be assigned to
170 .UnwrapOptional, .Deref, .Period, .ArrayAccess, .Identifier => {},
176171 }
172 return expr(mod, scope, .ref, node);
173}
174
175/// Turn Zig AST into untyped ZIR istructions.
176pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerError!*zir.Inst {
177177 switch (node.tag) {
178178 .Root => unreachable, // Top-level declaration.
179179 .Use => unreachable, // Top-level declaration.
......@@ -317,7 +317,7 @@ fn breakExpr(mod: *Module, parent_scope: *Scope, node: *ast.Node.ControlFlowExpr
317317 // proper type inference requires peer type resolution on the block's
318318 // break operand expressions.
319319 const branch_rl: ResultLoc = switch (label.result_loc) {
320 .discard, .none, .ty, .ptr, .lvalue, .ref => label.result_loc,
320 .discard, .none, .ty, .ptr, .ref => label.result_loc,
321321 .inferred_ptr, .bitcasted_ptr, .block_ptr => .{ .block_ptr = label.block_inst },
322322 };
323323 const operand = try expr(mod, parent_scope, branch_rl, rhs);
......@@ -524,7 +524,7 @@ fn assign(mod: *Module, scope: *Scope, infix_node: *ast.Node.SimpleInfixOp) Inne
524524 return;
525525 }
526526 }
527 const lvalue = try expr(mod, scope, .lvalue, infix_node.lhs);
527 const lvalue = try lvalExpr(mod, scope, infix_node.lhs);
528528 _ = try expr(mod, scope, .{ .ptr = lvalue }, infix_node.rhs);
529529}
530530
......@@ -534,7 +534,7 @@ fn assignOp(
534534 infix_node: *ast.Node.SimpleInfixOp,
535535 op_inst_tag: zir.Inst.Tag,
536536) InnerError!void {
537 const lhs_ptr = try expr(mod, scope, .lvalue, infix_node.lhs);
537 const lhs_ptr = try lvalExpr(mod, scope, infix_node.lhs);
538538 const lhs = try addZIRUnOp(mod, scope, lhs_ptr.src, .deref, lhs_ptr);
539539 const lhs_type = try addZIRUnOp(mod, scope, lhs_ptr.src, .typeof, lhs);
540540 const rhs = try expr(mod, scope, .{ .ty = lhs_type }, infix_node.rhs);
......@@ -794,7 +794,7 @@ fn field(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.SimpleInfix
794794 const field_name = try identifierStringInst(mod, scope, node.rhs.castTag(.Identifier).?);
795795
796796 const pointer = try addZIRInst(mod, scope, src, zir.Inst.FieldPtr, .{ .object_ptr = lhs, .field_name = field_name }, .{});
797 if (rl == .ref or rl == .lvalue) return pointer;
797 if (rl == .ref) return pointer;
798798 return rlWrap(mod, scope, rl, try addZIRUnOp(mod, scope, src, .deref, pointer));
799799}
800800
......@@ -1020,7 +1020,7 @@ fn ifExpr(mod: *Module, scope: *Scope, rl: ResultLoc, if_node: *ast.Node.If) Inn
10201020 // proper type inference requires peer type resolution on the if's
10211021 // branches.
10221022 const branch_rl: ResultLoc = switch (rl) {
1023 .discard, .none, .ty, .ptr, .lvalue, .ref => rl,
1023 .discard, .none, .ty, .ptr, .ref => rl,
10241024 .inferred_ptr, .bitcasted_ptr, .block_ptr => .{ .block_ptr = block },
10251025 };
10261026
......@@ -1150,7 +1150,7 @@ fn whileExpr(mod: *Module, scope: *Scope, rl: ResultLoc, while_node: *ast.Node.W
11501150 // proper type inference requires peer type resolution on the while's
11511151 // branches.
11521152 const branch_rl: ResultLoc = switch (rl) {
1153 .discard, .none, .ty, .ptr, .lvalue, .ref => rl,
1153 .discard, .none, .ty, .ptr, .ref => rl,
11541154 .inferred_ptr, .bitcasted_ptr, .block_ptr => .{ .block_ptr = while_block },
11551155 };
11561156
......@@ -1535,7 +1535,6 @@ fn as(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.BuiltinCall) I
15351535 _ = try addZIRUnOp(mod, scope, result.src, .ensure_result_non_error, result);
15361536 return result;
15371537 },
1538 .lvalue => unreachable,
15391538 .ref => {
15401539 const result = try expr(mod, scope, .{ .ty = dest_type }, params[1]);
15411540 return addZIRUnOp(mod, scope, result.src, .ref, result);
......@@ -1583,7 +1582,6 @@ fn bitCast(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.BuiltinCa
15831582 _ = try addZIRUnOp(mod, scope, result.src, .ensure_result_non_error, result);
15841583 return result;
15851584 },
1586 .lvalue => unreachable,
15871585 .ref => {
15881586 const operand = try expr(mod, scope, .ref, params[1]);
15891587 const result = try addZIRBinOp(mod, scope, src, .bitcast_ref, dest_type, operand);
......@@ -1851,7 +1849,7 @@ fn rlWrap(mod: *Module, scope: *Scope, rl: ResultLoc, result: *zir.Inst) InnerEr
18511849 _ = try addZIRUnOp(mod, scope, result.src, .ensure_result_non_error, result);
18521850 return result;
18531851 },
1854 .lvalue, .ref => {
1852 .ref => {
18551853 // We need a pointer but we have a value.
18561854 return addZIRUnOp(mod, scope, result.src, .ref, result);
18571855 },
......@@ -1886,7 +1884,7 @@ fn rlWrapVoid(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node, resul
18861884}
18871885
18881886fn rlWrapPtr(mod: *Module, scope: *Scope, rl: ResultLoc, ptr: *zir.Inst) InnerError!*zir.Inst {
1889 if (rl == .lvalue or rl == .ref) return ptr;
1887 if (rl == .ref) return ptr;
18901888
18911889 return rlWrap(mod, scope, rl, try addZIRUnOp(mod, scope, ptr.src, .deref, ptr));
18921890}