Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler: move checking function-scope-only builtins to AstGen #18096

Merged
merged 11 commits into from
Nov 25, 2023
43 changes: 22 additions & 21 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8276,6 +8276,28 @@ fn builtinCall(
}
}

// Check function scope-only builtins

if (astgen.fn_block == null) {
switch (info.tag) {
.c_va_arg,
.c_va_copy,
.c_va_end,
.c_va_start,
.work_item_id,
.work_group_size,
.work_group_id,
.set_align_stack,
.set_cold,
.return_address,
.frame_address,
.breakpoint,
.src,
=> return astgen.failNode(node, "'{s}' outside function scope", .{builtin_name}),
else => {},
}
}

switch (info.tag) {
.import => {
const node_tags = tree.nodes.items(.tag);
Expand Down Expand Up @@ -8812,9 +8834,6 @@ fn builtinCall(
return rvalue(gz, ri, .void_value, node);
},
.c_va_arg => {
if (astgen.fn_block == null) {
return astgen.failNode(node, "'@cVaArg' outside function scope", .{});
}
const result = try gz.addExtendedPayload(.c_va_arg, Zir.Inst.BinNode{
.node = gz.nodeIndexToRelative(node),
.lhs = try expr(gz, scope, .{ .rl = .none }, params[0]),
Expand All @@ -8823,39 +8842,27 @@ fn builtinCall(
return rvalue(gz, ri, result, node);
},
.c_va_copy => {
if (astgen.fn_block == null) {
return astgen.failNode(node, "'@cVaCopy' outside function scope", .{});
}
const result = try gz.addExtendedPayload(.c_va_copy, Zir.Inst.UnNode{
.node = gz.nodeIndexToRelative(node),
.operand = try expr(gz, scope, .{ .rl = .none }, params[0]),
});
return rvalue(gz, ri, result, node);
},
.c_va_end => {
if (astgen.fn_block == null) {
return astgen.failNode(node, "'@cVaEnd' outside function scope", .{});
}
const result = try gz.addExtendedPayload(.c_va_end, Zir.Inst.UnNode{
.node = gz.nodeIndexToRelative(node),
.operand = try expr(gz, scope, .{ .rl = .none }, params[0]),
});
return rvalue(gz, ri, result, node);
},
.c_va_start => {
if (astgen.fn_block == null) {
return astgen.failNode(node, "'@cVaStart' outside function scope", .{});
}
if (!astgen.fn_var_args) {
return astgen.failNode(node, "'@cVaStart' in a non-variadic function", .{});
}
return rvalue(gz, ri, try gz.addNodeExtended(.c_va_start, node), node);
},

.work_item_id => {
if (astgen.fn_block == null) {
return astgen.failNode(node, "'@workItemId' outside function scope", .{});
}
const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);
const result = try gz.addExtendedPayload(.work_item_id, Zir.Inst.UnNode{
.node = gz.nodeIndexToRelative(node),
Expand All @@ -8864,9 +8871,6 @@ fn builtinCall(
return rvalue(gz, ri, result, node);
},
.work_group_size => {
if (astgen.fn_block == null) {
return astgen.failNode(node, "'@workGroupSize' outside function scope", .{});
}
const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);
const result = try gz.addExtendedPayload(.work_group_size, Zir.Inst.UnNode{
.node = gz.nodeIndexToRelative(node),
Expand All @@ -8875,9 +8879,6 @@ fn builtinCall(
return rvalue(gz, ri, result, node);
},
.work_group_id => {
if (astgen.fn_block == null) {
return astgen.failNode(node, "'@workGroupId' outside function scope", .{});
}
const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);
const result = try gz.addExtendedPayload(.work_group_id, Zir.Inst.UnNode{
.node = gz.nodeIndexToRelative(node),
Expand Down
6 changes: 1 addition & 5 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6151,9 +6151,6 @@ fn zirSetAlignStack(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Inst
alignment.toByteUnitsOptional().?,
});
}
if (sema.func_index == .none) {
return sema.fail(block, src, "@setAlignStack outside function body", .{});
}

const fn_owner_decl = mod.funcOwnerDeclPtr(sema.func_index);
switch (fn_owner_decl.ty.fnCallingConvention(mod)) {
Expand Down Expand Up @@ -16780,13 +16777,12 @@ fn zirBuiltinSrc(
block: *Block,
extended: Zir.Inst.Extended.InstData,
) CompileError!Air.Inst.Ref {
_ = block;
const tracy = trace(@src());
defer tracy.end();

const mod = sema.mod;
const extra = sema.code.extraData(Zir.Inst.Src, extended.operand).data;
const src = LazySrcLoc.nodeOffset(extra.node);
if (sema.func_index == .none) return sema.fail(block, src, "@src outside function", .{});
const fn_owner_decl = mod.funcOwnerDeclPtr(sema.func_index);
const ip = &mod.intern_pool;
const gpa = sema.gpa;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
comptime {
@setAlignStack(1);
}

comptime {
@setCold(true);
}

comptime {
@src();
}

comptime {
@returnAddress();
}

comptime {
@frameAddress();
}

comptime {
@breakpoint();
}

comptime {
@cVaArg(1, 2);
}

comptime {
@cVaCopy(1);
}

comptime {
@cVaEnd(1);
}

comptime {
@cVaStart();
}

comptime {
@workItemId(42);
}

comptime {
@workGroupSize(42);
}

comptime {
@workGroupId(42);
}

// error
// backend=stage2
// target=native
//
// :2:5: error: '@setAlignStack' outside function scope
// :6:5: error: '@setCold' outside function scope
// :10:5: error: '@src' outside function scope
// :14:5: error: '@returnAddress' outside function scope
// :18:5: error: '@frameAddress' outside function scope
// :22:5: error: '@breakpoint' outside function scope
// :26:5: error: '@cVaArg' outside function scope
// :30:5: error: '@cVaCopy' outside function scope
// :34:5: error: '@cVaEnd' outside function scope
// :38:5: error: '@cVaStart' outside function scope
// :42:5: error: '@workItemId' outside function scope
// :46:5: error: '@workGroupSize' outside function scope
// :50:5: error: '@workGroupId' outside function scope
9 changes: 0 additions & 9 deletions test/cases/compile_errors/setAlignStack_outside_function.zig

This file was deleted.

9 changes: 0 additions & 9 deletions test/cases/compile_errors/src_outside_function.zig

This file was deleted.