Skip to content

Commit

Permalink
Add @inComptime builtin
Browse files Browse the repository at this point in the history
Resolves: ziglang#868
  • Loading branch information
mlugg committed Apr 14, 2023
1 parent b42562b commit 868fa62
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/AstGen.zig
Expand Up @@ -8174,6 +8174,7 @@ fn builtinCall(
.frame => return rvalue(gz, ri, try gz.addNodeExtended(.frame, node), node),
.frame_address => return rvalue(gz, ri, try gz.addNodeExtended(.frame_address, node), node),
.breakpoint => return rvalue(gz, ri, try gz.addNodeExtended(.breakpoint, node), node),
.in_comptime => return rvalue(gz, ri, try gz.addNodeExtended(.in_comptime, node), node),

.type_info => return simpleUnOpType(gz, scope, ri, node, params[0], .type_info),
.size_of => return simpleUnOpType(gz, scope, ri, node, params[0], .size_of),
Expand Down Expand Up @@ -8215,6 +8216,7 @@ fn builtinCall(
.int_cast => return typeCast(gz, scope, ri, node, params[0], params[1], .int_cast),
.ptr_cast => return typeCast(gz, scope, ri, node, params[0], params[1], .ptr_cast),
.truncate => return typeCast(gz, scope, ri, node, params[0], params[1], .truncate),

// zig fmt: on

.Type => {
Expand Down
8 changes: 8 additions & 0 deletions src/BuiltinFn.zig
Expand Up @@ -58,6 +58,7 @@ pub const Tag = enum {
has_decl,
has_field,
import,
in_comptime,
int_cast,
int_to_enum,
int_to_error,
Expand Down Expand Up @@ -560,6 +561,13 @@ pub const list = list: {
.param_count = 1,
},
},
.{
"@inComptime",
.{
.tag = .in_comptime,
.param_count = 0,
},
},
.{
"@intCast",
.{
Expand Down
8 changes: 8 additions & 0 deletions src/Sema.zig
Expand Up @@ -1166,6 +1166,7 @@ fn analyzeBodyInner(
.work_item_id => try sema.zirWorkItem( block, extended, extended.opcode),
.work_group_size => try sema.zirWorkItem( block, extended, extended.opcode),
.work_group_id => try sema.zirWorkItem( block, extended, extended.opcode),
.in_comptime => try sema.zirInComptime( block),
// zig fmt: on

.fence => {
Expand Down Expand Up @@ -22464,6 +22465,13 @@ fn zirWorkItem(
});
}

fn zirInComptime(
sema: *Sema,
block: *Block,
) CompileError!Air.Inst.Ref {
return sema.addConstant(Type.bool, if (block.is_comptime) Value.true else Value.false);
}

fn requireRuntimeBlock(sema: *Sema, block: *Block, src: LazySrcLoc, runtime_src: ?LazySrcLoc) !void {
if (block.is_comptime) {
const msg = msg: {
Expand Down
7 changes: 5 additions & 2 deletions src/Zir.zig
Expand Up @@ -1994,10 +1994,10 @@ pub const Inst = struct {
/// Implement builtin `@cVaArg`.
/// `operand` is payload index to `BinNode`.
c_va_arg,
/// Implement builtin `@cVaStart`.
/// Implement builtin `@cVaCopy`.
/// `operand` is payload index to `UnNode`.
c_va_copy,
/// Implement builtin `@cVaStart`.
/// Implement builtin `@cVaEnd`.
/// `operand` is payload index to `UnNode`.
c_va_end,
/// Implement builtin `@cVaStart`.
Expand All @@ -2018,6 +2018,9 @@ pub const Inst = struct {
/// Implements the `@workGroupId` builtin.
/// `operand` is payload index to `UnNode`.
work_group_id,
/// Implements the `@inComptime` builtin.
/// `operand` is `src_node: i32`.
in_comptime,

pub const InstData = struct {
opcode: Extended,
Expand Down
1 change: 1 addition & 0 deletions src/print_zir.zig
Expand Up @@ -466,6 +466,7 @@ const Writer = struct {
.frame_address,
.breakpoint,
.c_va_start,
.in_comptime,
=> try self.writeExtNode(stream, extended),

.builtin_src => {
Expand Down
12 changes: 12 additions & 0 deletions test/behavior/eval.zig
Expand Up @@ -1649,3 +1649,15 @@ test "early exit in container level const" {
};
try expect(S.value == 1);
}

test "@inComptime" {
const S = struct {
fn inComptime() bool {
return @inComptime();
}
};
try expectEqual(false, @inComptime());
try expectEqual(true, comptime @inComptime());
try expectEqual(false, S.inComptime());
try expectEqual(true, comptime S.inComptime());
}

0 comments on commit 868fa62

Please sign in to comment.