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...@@ -8339,7 +8339,7 @@ fn nodeMayNeedMemoryLocation(tree: *const Ast, start_node: Ast.Node.Index) bool
8339 }8339 }
8340}8340}
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 {
8343 const node_tags = tree.nodes.items(.tag);8343 const node_tags = tree.nodes.items(.tag);
8344 const node_datas = tree.nodes.items(.data);8344 const node_datas = tree.nodes.items(.data);
8345 const main_tokens = tree.nodes.items(.main_token);8345 const main_tokens = tree.nodes.items(.main_token);
...@@ -8546,18 +8546,7 @@ fn nodeMayEvalToError(tree: *const Ast, start_node: Ast.Node.Index) enum { never...@@ -8546,18 +8546,7 @@ fn nodeMayEvalToError(tree: *const Ast, start_node: Ast.Node.Index) enum { never
8546 // If the builtin is an invalid name, we don't cause an error here; instead8546 // If the builtin is an invalid name, we don't cause an error here; instead
8547 // let it pass, and the error will be "invalid builtin function" later.8547 // let it pass, and the error will be "invalid builtin function" later.
8548 const builtin_info = BuiltinFn.list.get(builtin_name) orelse return .maybe;8548 const builtin_info = BuiltinFn.list.get(builtin_name) orelse return .maybe;
8549 return switch (builtin_info.tag) {8549 return builtin_info.eval_to_error;
8550 .as,
8551 .call,
8552 .field,
8553 => .maybe,
8554
8555 .err_set_cast,
8556 .int_to_error,
8557 => .always,
8558
8559 else => .never,
8560 };
8561 },8550 },
8562 }8551 }
8563 }8552 }
src/BuiltinFn.zig+16
...@@ -119,10 +119,21 @@ pub const MemLocRequirement = enum {...@@ -119,10 +119,21 @@ pub const MemLocRequirement = enum {
119 forward1,119 forward1,
120};120};
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
122tag: Tag,131tag: Tag,
123132
124/// Info about the builtin call's ability to take advantage of a result location pointer.133/// Info about the builtin call's ability to take advantage of a result location pointer.
125needs_mem_loc: MemLocRequirement = .never,134needs_mem_loc: MemLocRequirement = .never,
135/// Info about the builtin call's possibility of returning an error.
136eval_to_error: EvalToError = .never,
126/// `true` if the builtin call can be the left-hand side of an expression (assigned to).137/// `true` if the builtin call can be the left-hand side of an expression (assigned to).
127allows_lvalue: bool = false,138allows_lvalue: bool = false,
128/// The number of parameters to this builtin function. `null` means variable number139/// The number of parameters to this builtin function. `null` means variable number
...@@ -158,6 +169,7 @@ pub const list = list: {...@@ -158,6 +169,7 @@ pub const list = list: {
158 .{169 .{
159 .tag = .as,170 .tag = .as,
160 .needs_mem_loc = .forward1,171 .needs_mem_loc = .forward1,
172 .eval_to_error = .maybe,
161 .param_count = 2,173 .param_count = 2,
162 },174 },
163 },175 },
...@@ -258,6 +270,7 @@ pub const list = list: {...@@ -258,6 +270,7 @@ pub const list = list: {
258 .{270 .{
259 .tag = .call,271 .tag = .call,
260 .needs_mem_loc = .always,272 .needs_mem_loc = .always,
273 .eval_to_error = .maybe,
261 .param_count = 3,274 .param_count = 3,
262 },275 },
263 },276 },
...@@ -391,6 +404,7 @@ pub const list = list: {...@@ -391,6 +404,7 @@ pub const list = list: {
391 "@errSetCast",404 "@errSetCast",
392 .{405 .{
393 .tag = .err_set_cast,406 .tag = .err_set_cast,
407 .eval_to_error = .always,
394 .param_count = 2,408 .param_count = 2,
395 },409 },
396 },410 },
...@@ -420,6 +434,7 @@ pub const list = list: {...@@ -420,6 +434,7 @@ pub const list = list: {
420 .{434 .{
421 .tag = .field,435 .tag = .field,
422 .needs_mem_loc = .always,436 .needs_mem_loc = .always,
437 .eval_to_error = .maybe,
423 .param_count = 2,438 .param_count = 2,
424 .allows_lvalue = true,439 .allows_lvalue = true,
425 },440 },
...@@ -512,6 +527,7 @@ pub const list = list: {...@@ -512,6 +527,7 @@ pub const list = list: {
512 "@intToError",527 "@intToError",
513 .{528 .{
514 .tag = .int_to_error,529 .tag = .int_to_error,
530 .eval_to_error = .always,
515 .param_count = 1,531 .param_count = 1,
516 },532 },
517 },533 },