authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-25 10:50:47-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-25 10:50:47-07:00
log8f3e1ea0f07cdc7e94b0816ae3c58733df0f2786
tree056a6a26d0186e17249aa36d918d364c34b48841
parentd4bf44024a005fcd6931fd0975d78c890dedb4e6

AstGen: move nodeMayEvalToError logic for builtins

to the declarative BuiltinFn.zig file which lists info about all the builtin functions.

2 files changed, 18 insertions(+), 13 deletions(-)

src/AstGen.zig+2-13
......@@ -8339,7 +8339,7 @@ fn nodeMayNeedMemoryLocation(tree: *const Ast, start_node: Ast.Node.Index) bool
83398339 }
83408340}
83418341
8342fn nodeMayEvalToError(tree: *const Ast, start_node: Ast.Node.Index) enum { never, always, maybe } {
8342fn nodeMayEvalToError(tree: *const Ast, start_node: Ast.Node.Index) BuiltinFn.EvalToError {
83438343 const node_tags = tree.nodes.items(.tag);
83448344 const node_datas = tree.nodes.items(.data);
83458345 const main_tokens = tree.nodes.items(.main_token);
......@@ -8546,18 +8546,7 @@ fn nodeMayEvalToError(tree: *const Ast, start_node: Ast.Node.Index) enum { never
85468546 // If the builtin is an invalid name, we don't cause an error here; instead
85478547 // let it pass, and the error will be "invalid builtin function" later.
85488548 const builtin_info = BuiltinFn.list.get(builtin_name) orelse return .maybe;
8549 return switch (builtin_info.tag) {
8550 .as,
8551 .call,
8552 .field,
8553 => .maybe,
8554
8555 .err_set_cast,
8556 .int_to_error,
8557 => .always,
8558
8559 else => .never,
8560 };
8549 return builtin_info.eval_to_error;
85618550 },
85628551 }
85638552 }
src/BuiltinFn.zig+16
......@@ -119,10 +119,21 @@ pub const MemLocRequirement = enum {
119119 forward1,
120120};
121121
122pub const EvalToError = enum {
123 /// The builtin cannot possibly evaluate to an error.
124 never,
125 /// The builtin will always evaluate to an error.
126 always,
127 /// The builtin may or may not evaluate to an error depending on the parameters.
128 maybe,
129};
130
122131tag: Tag,
123132
124133/// Info about the builtin call's ability to take advantage of a result location pointer.
125134needs_mem_loc: MemLocRequirement = .never,
135/// Info about the builtin call's possibility of returning an error.
136eval_to_error: EvalToError = .never,
126137/// `true` if the builtin call can be the left-hand side of an expression (assigned to).
127138allows_lvalue: bool = false,
128139/// The number of parameters to this builtin function. `null` means variable number
......@@ -158,6 +169,7 @@ pub const list = list: {
158169 .{
159170 .tag = .as,
160171 .needs_mem_loc = .forward1,
172 .eval_to_error = .maybe,
161173 .param_count = 2,
162174 },
163175 },
......@@ -258,6 +270,7 @@ pub const list = list: {
258270 .{
259271 .tag = .call,
260272 .needs_mem_loc = .always,
273 .eval_to_error = .maybe,
261274 .param_count = 3,
262275 },
263276 },
......@@ -391,6 +404,7 @@ pub const list = list: {
391404 "@errSetCast",
392405 .{
393406 .tag = .err_set_cast,
407 .eval_to_error = .always,
394408 .param_count = 2,
395409 },
396410 },
......@@ -420,6 +434,7 @@ pub const list = list: {
420434 .{
421435 .tag = .field,
422436 .needs_mem_loc = .always,
437 .eval_to_error = .maybe,
423438 .param_count = 2,
424439 .allows_lvalue = true,
425440 },
......@@ -512,6 +527,7 @@ pub const list = list: {
512527 "@intToError",
513528 .{
514529 .tag = .int_to_error,
530 .eval_to_error = .always,
515531 .param_count = 1,
516532 },
517533 },