aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2023-04-14 22:32:31 +0100
committerAndrew Kelley <andrew@ziglang.org>2023-04-23 13:16:42 -0700
commit35d82d31be3d2f2611049f41dc2616f898d70871 (patch)
tree9dfb2332aca1201afb67aea6bf83a9e77bf05c9f /src
parent42ee364e7b698822a69cba4cd2bda17868657e05 (diff)
downloadzig-35d82d31be3d2f2611049f41dc2616f898d70871.tar.gz
zig-35d82d31be3d2f2611049f41dc2616f898d70871.zip
Add `@inComptime` builtin
Resolves: #868
Diffstat (limited to 'src')
-rw-r--r--src/AstGen.zig1
-rw-r--r--src/BuiltinFn.zig8
-rw-r--r--src/Sema.zig13
-rw-r--r--src/Zir.zig7
-rw-r--r--src/print_zir.zig1
5 files changed, 28 insertions, 2 deletions
diff --git a/src/AstGen.zig b/src/AstGen.zig
index 4419f5e803..19bc695db4 100644
--- a/src/AstGen.zig
+++ b/src/AstGen.zig
@@ -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),
diff --git a/src/BuiltinFn.zig b/src/BuiltinFn.zig
index 4a98a5a615..ee11aecbf4 100644
--- a/src/BuiltinFn.zig
+++ b/src/BuiltinFn.zig
@@ -58,6 +58,7 @@ pub const Tag = enum {
has_decl,
has_field,
import,
+ in_comptime,
int_cast,
int_to_enum,
int_to_error,
@@ -561,6 +562,13 @@ pub const list = list: {
},
},
.{
+ "@inComptime",
+ .{
+ .tag = .in_comptime,
+ .param_count = 0,
+ },
+ },
+ .{
"@intCast",
.{
.tag = .int_cast,
diff --git a/src/Sema.zig b/src/Sema.zig
index 4deb2b0f29..00d511149a 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -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 => {
@@ -22466,6 +22467,18 @@ fn zirWorkItem(
});
}
+fn zirInComptime(
+ sema: *Sema,
+ block: *Block,
+) CompileError!Air.Inst.Ref {
+ _ = sema;
+ if (block.is_comptime) {
+ return Air.Inst.Ref.bool_true;
+ } else {
+ return Air.Inst.Ref.bool_false;
+ }
+}
+
fn requireRuntimeBlock(sema: *Sema, block: *Block, src: LazySrcLoc, runtime_src: ?LazySrcLoc) !void {
if (block.is_comptime) {
const msg = msg: {
diff --git a/src/Zir.zig b/src/Zir.zig
index 904e02c755..a58b4b4070 100644
--- a/src/Zir.zig
+++ b/src/Zir.zig
@@ -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`.
@@ -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,
diff --git a/src/print_zir.zig b/src/print_zir.zig
index 79c802f936..927d8a8b33 100644
--- a/src/print_zir.zig
+++ b/src/print_zir.zig
@@ -466,6 +466,7 @@ const Writer = struct {
.frame_address,
.breakpoint,
.c_va_start,
+ .in_comptime,
=> try self.writeExtNode(stream, extended),
.builtin_src => {