authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-08 22:38:13-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-08 22:38:13-07:00
log23db96931e5226f09a451663ba1c8bf2398af91b
treea725fb554d7c80bd75116b63483d10873b0a7805
parent6e05d53e88e07d6234aaf6e0b86cd52d8c9aea92

AstGen: implement `@typeInfo` builtin


3 files changed, 18 insertions(+), 1 deletions(-)

src/AstGen.zig+7-1
......@@ -1356,6 +1356,7 @@ fn blockExprStmts(
13561356 .opaque_decl,
13571357 .int_to_enum,
13581358 .enum_to_int,
1359 .type_info,
13591360 => break :b false,
13601361
13611362 // ZIR instructions that are always either `noreturn` or `void`.
......@@ -4198,6 +4199,12 @@ fn builtinCall(
41984199 return rvalue(gz, scope, rl, result, node);
41994200 },
42004201
4202 .type_info => {
4203 const operand = try typeExpr(gz, scope, params[0]);
4204 const result = try gz.addUnNode(.type_info, operand, node);
4205 return rvalue(gz, scope, rl, result, node);
4206 },
4207
42014208 .add_with_overflow,
42024209 .align_cast,
42034210 .align_of,
......@@ -4274,7 +4281,6 @@ fn builtinCall(
42744281 .This,
42754282 .truncate,
42764283 .Type,
4277 .type_info,
42784284 .type_name,
42794285 .union_init,
42804286 => return mod.failNode(scope, node, "TODO: implement builtin function {s}", .{
src/Sema.zig+7
......@@ -259,6 +259,7 @@ pub fn analyzeBody(
259259 .switch_capture_multi_ref => try sema.zirSwitchCapture(block, inst, true, true),
260260 .switch_capture_else => try sema.zirSwitchCaptureElse(block, inst, false),
261261 .switch_capture_else_ref => try sema.zirSwitchCaptureElse(block, inst, true),
262 .type_info => try sema.zirTypeInfo(block, inst),
262263 .typeof => try sema.zirTypeof(block, inst),
263264 .typeof_elem => try sema.zirTypeofElem(block, inst),
264265 .typeof_peer => try sema.zirTypeofPeer(block, inst),
......@@ -4078,6 +4079,12 @@ fn zirCmp(
40784079 return block.addBinOp(src, bool_type, tag, casted_lhs, casted_rhs);
40794080}
40804081
4082fn zirTypeInfo(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
4083 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
4084 const src = inst_data.src();
4085 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirTypeInfo", .{});
4086}
4087
40814088fn zirTypeof(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
40824089 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
40834090 const src = inst_data.src();
src/zir.zig+4
......@@ -687,6 +687,8 @@ pub const Inst = struct {
687687 /// Converts an enum value into an integer. Resulting type will be the tag type
688688 /// of the enum. Uses `un_node`.
689689 enum_to_int,
690 /// Implements the `@typeInfo` builtin. Uses `un_node`.
691 type_info,
690692
691693 /// Returns whether the instruction is one of the control flow "noreturn" types.
692694 /// Function calls do not count.
......@@ -850,6 +852,7 @@ pub const Inst = struct {
850852 .field_type,
851853 .int_to_enum,
852854 .enum_to_int,
855 .type_info,
853856 => false,
854857
855858 .@"break",
......@@ -1652,6 +1655,7 @@ const Writer = struct {
16521655 .typeof_elem,
16531656 .struct_init_empty,
16541657 .enum_to_int,
1658 .type_info,
16551659 => try self.writeUnNode(stream, inst),
16561660
16571661 .ref,