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,...@@ -152,6 +152,8 @@ needs_mem_loc: MemLocRequirement = .never,
152eval_to_error: EvalToError = .never,152eval_to_error: EvalToError = .never,
153/// `true` if the builtin call can be the left-hand side of an expression (assigned to).153/// `true` if the builtin call can be the left-hand side of an expression (assigned to).
154allows_lvalue: bool = false,154allows_lvalue: bool = false,
155/// `true` if builtin call is not available outside function scope
156illegal_outside_function: bool = false,
155/// The number of parameters to this builtin function. `null` means variable number157/// The number of parameters to this builtin function. `null` means variable number
156/// of parameters.158/// of parameters.
157param_count: ?u8,159param_count: ?u8,
...@@ -258,6 +260,7 @@ pub const list = list: {...@@ -258,6 +260,7 @@ pub const list = list: {
258 .{260 .{
259 .tag = .breakpoint,261 .tag = .breakpoint,
260 .param_count = 0,262 .param_count = 0,
263 .illegal_outside_function = true,
261 },264 },
262 },265 },
263 .{266 .{
...@@ -378,24 +381,28 @@ pub const list = list: {...@@ -378,24 +381,28 @@ pub const list = list: {
378 "@cVaArg", .{381 "@cVaArg", .{
379 .tag = .c_va_arg,382 .tag = .c_va_arg,
380 .param_count = 2,383 .param_count = 2,
384 .illegal_outside_function = true,
381 },385 },
382 },386 },
383 .{387 .{
384 "@cVaCopy", .{388 "@cVaCopy", .{
385 .tag = .c_va_copy,389 .tag = .c_va_copy,
386 .param_count = 1,390 .param_count = 1,
391 .illegal_outside_function = true,
387 },392 },
388 },393 },
389 .{394 .{
390 "@cVaEnd", .{395 "@cVaEnd", .{
391 .tag = .c_va_end,396 .tag = .c_va_end,
392 .param_count = 1,397 .param_count = 1,
398 .illegal_outside_function = true,
393 },399 },
394 },400 },
395 .{401 .{
396 "@cVaStart", .{402 "@cVaStart", .{
397 .tag = .c_va_start,403 .tag = .c_va_start,
398 .param_count = 0,404 .param_count = 0,
405 .illegal_outside_function = true,
399 },406 },
400 },407 },
401 .{408 .{
...@@ -533,6 +540,7 @@ pub const list = list: {...@@ -533,6 +540,7 @@ pub const list = list: {
533 .{540 .{
534 .tag = .frame_address,541 .tag = .frame_address,
535 .param_count = 0,542 .param_count = 0,
543 .illegal_outside_function = true,
536 },544 },
537 },545 },
538 .{546 .{
...@@ -709,6 +717,7 @@ pub const list = list: {...@@ -709,6 +717,7 @@ pub const list = list: {
709 .{717 .{
710 .tag = .return_address,718 .tag = .return_address,
711 .param_count = 0,719 .param_count = 0,
720 .illegal_outside_function = true,
712 },721 },
713 },722 },
714 .{723 .{
...@@ -723,6 +732,7 @@ pub const list = list: {...@@ -723,6 +732,7 @@ pub const list = list: {
723 .{732 .{
724 .tag = .set_align_stack,733 .tag = .set_align_stack,
725 .param_count = 1,734 .param_count = 1,
735 .illegal_outside_function = true,
726 },736 },
727 },737 },
728 .{738 .{
...@@ -730,6 +740,7 @@ pub const list = list: {...@@ -730,6 +740,7 @@ pub const list = list: {
730 .{740 .{
731 .tag = .set_cold,741 .tag = .set_cold,
732 .param_count = 1,742 .param_count = 1,
743 .illegal_outside_function = true,
733 },744 },
734 },745 },
735 .{746 .{
...@@ -808,6 +819,7 @@ pub const list = list: {...@@ -808,6 +819,7 @@ pub const list = list: {
808 .tag = .src,819 .tag = .src,
809 .needs_mem_loc = .always,820 .needs_mem_loc = .always,
810 .param_count = 0,821 .param_count = 0,
822 .illegal_outside_function = true,
811 },823 },
812 },824 },
813 .{825 .{
...@@ -997,6 +1009,7 @@ pub const list = list: {...@@ -997,6 +1009,7 @@ pub const list = list: {
997 "@workItemId", .{1009 "@workItemId", .{
998 .tag = .work_item_id,1010 .tag = .work_item_id,
999 .param_count = 1,1011 .param_count = 1,
1012 .illegal_outside_function = true,
1000 },1013 },
1001 },1014 },
1002 .{1015 .{
...@@ -1004,6 +1017,7 @@ pub const list = list: {...@@ -1004,6 +1017,7 @@ pub const list = list: {
1004 .{1017 .{
1005 .tag = .work_group_size,1018 .tag = .work_group_size,
1006 .param_count = 1,1019 .param_count = 1,
1020 .illegal_outside_function = true,
1007 },1021 },
1008 },1022 },
1009 .{1023 .{
...@@ -1011,6 +1025,7 @@ pub const list = list: {...@@ -1011,6 +1025,7 @@ pub const list = list: {
1011 .{1025 .{
1012 .tag = .work_group_id,1026 .tag = .work_group_id,
1013 .param_count = 1,1027 .param_count = 1,
1028 .illegal_outside_function = true,
1014 },1029 },
1015 },1030 },
1016 });1031 });
src/AstGen.zig+2-19
...@@ -8268,25 +8268,8 @@ fn builtinCall(...@@ -8268,25 +8268,8 @@ fn builtinCall(
82688268
8269 // Check function scope-only builtins8269 // Check function scope-only builtins
82708270
8271 if (astgen.fn_block == null) {8271 if (astgen.fn_block == null and info.illegal_outside_function)
8272 switch (info.tag) {8272 return astgen.failNode(node, "'{s}' outside function scope", .{builtin_name});
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 }
82908273
8291 switch (info.tag) {8274 switch (info.tag) {
8292 .import => {8275 .import => {