authorgravatar for wrongnull@gmail.comBogdan Romanyuk <wrongnull@gmail.com> 2023-11-26 10:21:58+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-11-26 02:21:58-05:00
log2ff707be789046a733d0d23c6fe42dd2b7210396
tree48b0c105859d1ff21cb7de70aedacd6f02006b1b
parent2252dcc508c53dc302ec1f919c4792a7f00ce125
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

AstGen: check allowed non-function builtins with declarative field (#18120)


2 files changed, 17 insertions(+), 19 deletions(-)

lib/std/zig/BuiltinFn.zig+15
......@@ -152,6 +152,8 @@ needs_mem_loc: MemLocRequirement = .never,
152152eval_to_error: EvalToError = .never,
153153/// `true` if the builtin call can be the left-hand side of an expression (assigned to).
154154allows_lvalue: bool = false,
155/// `true` if builtin call is not available outside function scope
156illegal_outside_function: bool = false,
155157/// The number of parameters to this builtin function. `null` means variable number
156158/// of parameters.
157159param_count: ?u8,
......@@ -258,6 +260,7 @@ pub const list = list: {
258260 .{
259261 .tag = .breakpoint,
260262 .param_count = 0,
263 .illegal_outside_function = true,
261264 },
262265 },
263266 .{
......@@ -378,24 +381,28 @@ pub const list = list: {
378381 "@cVaArg", .{
379382 .tag = .c_va_arg,
380383 .param_count = 2,
384 .illegal_outside_function = true,
381385 },
382386 },
383387 .{
384388 "@cVaCopy", .{
385389 .tag = .c_va_copy,
386390 .param_count = 1,
391 .illegal_outside_function = true,
387392 },
388393 },
389394 .{
390395 "@cVaEnd", .{
391396 .tag = .c_va_end,
392397 .param_count = 1,
398 .illegal_outside_function = true,
393399 },
394400 },
395401 .{
396402 "@cVaStart", .{
397403 .tag = .c_va_start,
398404 .param_count = 0,
405 .illegal_outside_function = true,
399406 },
400407 },
401408 .{
......@@ -533,6 +540,7 @@ pub const list = list: {
533540 .{
534541 .tag = .frame_address,
535542 .param_count = 0,
543 .illegal_outside_function = true,
536544 },
537545 },
538546 .{
......@@ -709,6 +717,7 @@ pub const list = list: {
709717 .{
710718 .tag = .return_address,
711719 .param_count = 0,
720 .illegal_outside_function = true,
712721 },
713722 },
714723 .{
......@@ -723,6 +732,7 @@ pub const list = list: {
723732 .{
724733 .tag = .set_align_stack,
725734 .param_count = 1,
735 .illegal_outside_function = true,
726736 },
727737 },
728738 .{
......@@ -730,6 +740,7 @@ pub const list = list: {
730740 .{
731741 .tag = .set_cold,
732742 .param_count = 1,
743 .illegal_outside_function = true,
733744 },
734745 },
735746 .{
......@@ -808,6 +819,7 @@ pub const list = list: {
808819 .tag = .src,
809820 .needs_mem_loc = .always,
810821 .param_count = 0,
822 .illegal_outside_function = true,
811823 },
812824 },
813825 .{
......@@ -997,6 +1009,7 @@ pub const list = list: {
9971009 "@workItemId", .{
9981010 .tag = .work_item_id,
9991011 .param_count = 1,
1012 .illegal_outside_function = true,
10001013 },
10011014 },
10021015 .{
......@@ -1004,6 +1017,7 @@ pub const list = list: {
10041017 .{
10051018 .tag = .work_group_size,
10061019 .param_count = 1,
1020 .illegal_outside_function = true,
10071021 },
10081022 },
10091023 .{
......@@ -1011,6 +1025,7 @@ pub const list = list: {
10111025 .{
10121026 .tag = .work_group_id,
10131027 .param_count = 1,
1028 .illegal_outside_function = true,
10141029 },
10151030 },
10161031 });
src/AstGen.zig+2-19
......@@ -8268,25 +8268,8 @@ fn builtinCall(
82688268
82698269 // Check function scope-only builtins
82708270
8271 if (astgen.fn_block == null) {
8272 switch (info.tag) {
8273 .c_va_arg,
8274 .c_va_copy,
8275 .c_va_end,
8276 .c_va_start,
8277 .work_item_id,
8278 .work_group_size,
8279 .work_group_id,
8280 .set_align_stack,
8281 .set_cold,
8282 .return_address,
8283 .frame_address,
8284 .breakpoint,
8285 .src,
8286 => return astgen.failNode(node, "'{s}' outside function scope", .{builtin_name}),
8287 else => {},
8288 }
8289 }
8271 if (astgen.fn_block == null and info.illegal_outside_function)
8272 return astgen.failNode(node, "'{s}' outside function scope", .{builtin_name});
82908273
82918274 switch (info.tag) {
82928275 .import => {