Skip to content

Commit

Permalink
@typeid proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperAuguste committed May 5, 2024
1 parent 3648d7d commit 197135c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/std/zig/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9691,6 +9691,14 @@ fn builtinCall(
});
return rvalue(gz, ri, result, node);
},
.type_id => {
const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .type_type } }, params[0]);
const result = try gz.addExtendedPayload(.type_id, Zir.Inst.UnNode{
.node = gz.nodeIndexToRelative(node),
.operand = operand,
});
return rvalue(gz, ri, result, node);
},
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/std/zig/AstRlAnnotate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,7 @@ fn builtinCall(astrl: *AstRlAnnotate, block: ?*Block, ri: ResultInfo, node: Ast.
.work_group_size,
.work_group_id,
.field_parent_ptr,
.type_id,
=> {
_ = try astrl.expr(args[0], block, ResultInfo.type_only);
return false;
Expand Down
8 changes: 8 additions & 0 deletions lib/std/zig/BuiltinFn.zig
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ pub const Tag = enum {
work_item_id,
work_group_size,
work_group_id,
type_id,
};

pub const MemLocRequirement = enum {
Expand Down Expand Up @@ -1028,5 +1029,12 @@ pub const list = list: {
.illegal_outside_function = true,
},
},
.{
"@typeId",
.{
.tag = .type_id,
.param_count = 1,
},
},
});
};
3 changes: 3 additions & 0 deletions lib/std/zig/Zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,9 @@ pub const Inst = struct {
/// Guaranteed to not have the `ptr_cast` flag.
/// Uses the `pl_node` union field with payload `FieldParentPtr`.
field_parent_ptr,
/// Implements the `@typeId` builtin.
/// `operand` is payload index to `UnNode`.
type_id,

pub const InstData = struct {
opcode: Extended,
Expand Down
14 changes: 14 additions & 0 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,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),
.type_id => try sema.zirTypeId( block, extended),
.in_comptime => try sema.zirInComptime( block),
.closure_get => try sema.zirClosureGet( block, extended),
// zig fmt: on
Expand Down Expand Up @@ -26486,6 +26487,19 @@ fn zirWorkItem(
});
}

fn zirTypeId(
sema: *Sema,
block: *Block,
extended: Zir.Inst.Extended.InstData,
) CompileError!Air.Inst.Ref {
const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
const ty_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };

const ty = try sema.resolveType(block, ty_src, extra.operand);

return sema.mod.intRef(Type.u32, @intFromEnum(ty.ip_index));
}

fn zirInComptime(
sema: *Sema,
block: *Block,
Expand Down
1 change: 1 addition & 0 deletions src/print_zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ const Writer = struct {
.work_item_id,
.work_group_size,
.work_group_id,
.type_id,
=> {
const inst_data = self.code.extraData(Zir.Inst.UnNode, extended.operand).data;
const src = LazySrcLoc.nodeOffset(inst_data.node);
Expand Down

0 comments on commit 197135c

Please sign in to comment.