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(...@@ -1356,6 +1356,7 @@ fn blockExprStmts(
1356 .opaque_decl,1356 .opaque_decl,
1357 .int_to_enum,1357 .int_to_enum,
1358 .enum_to_int,1358 .enum_to_int,
1359 .type_info,
1359 => break :b false,1360 => break :b false,
13601361
1361 // ZIR instructions that are always either `noreturn` or `void`.1362 // ZIR instructions that are always either `noreturn` or `void`.
...@@ -4198,6 +4199,12 @@ fn builtinCall(...@@ -4198,6 +4199,12 @@ fn builtinCall(
4198 return rvalue(gz, scope, rl, result, node);4199 return rvalue(gz, scope, rl, result, node);
4199 },4200 },
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
4201 .add_with_overflow,4208 .add_with_overflow,
4202 .align_cast,4209 .align_cast,
4203 .align_of,4210 .align_of,
...@@ -4274,7 +4281,6 @@ fn builtinCall(...@@ -4274,7 +4281,6 @@ fn builtinCall(
4274 .This,4281 .This,
4275 .truncate,4282 .truncate,
4276 .Type,4283 .Type,
4277 .type_info,
4278 .type_name,4284 .type_name,
4279 .union_init,4285 .union_init,
4280 => return mod.failNode(scope, node, "TODO: implement builtin function {s}", .{4286 => return mod.failNode(scope, node, "TODO: implement builtin function {s}", .{
src/Sema.zig+7
...@@ -259,6 +259,7 @@ pub fn analyzeBody(...@@ -259,6 +259,7 @@ pub fn analyzeBody(
259 .switch_capture_multi_ref => try sema.zirSwitchCapture(block, inst, true, true),259 .switch_capture_multi_ref => try sema.zirSwitchCapture(block, inst, true, true),
260 .switch_capture_else => try sema.zirSwitchCaptureElse(block, inst, false),260 .switch_capture_else => try sema.zirSwitchCaptureElse(block, inst, false),
261 .switch_capture_else_ref => try sema.zirSwitchCaptureElse(block, inst, true),261 .switch_capture_else_ref => try sema.zirSwitchCaptureElse(block, inst, true),
262 .type_info => try sema.zirTypeInfo(block, inst),
262 .typeof => try sema.zirTypeof(block, inst),263 .typeof => try sema.zirTypeof(block, inst),
263 .typeof_elem => try sema.zirTypeofElem(block, inst),264 .typeof_elem => try sema.zirTypeofElem(block, inst),
264 .typeof_peer => try sema.zirTypeofPeer(block, inst),265 .typeof_peer => try sema.zirTypeofPeer(block, inst),
...@@ -4078,6 +4079,12 @@ fn zirCmp(...@@ -4078,6 +4079,12 @@ fn zirCmp(
4078 return block.addBinOp(src, bool_type, tag, casted_lhs, casted_rhs);4079 return block.addBinOp(src, bool_type, tag, casted_lhs, casted_rhs);
4079}4080}
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
4081fn zirTypeof(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {4088fn zirTypeof(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
4082 const inst_data = sema.code.instructions.items(.data)[inst].un_node;4089 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
4083 const src = inst_data.src();4090 const src = inst_data.src();
src/zir.zig+4
...@@ -687,6 +687,8 @@ pub const Inst = struct {...@@ -687,6 +687,8 @@ pub const Inst = struct {
687 /// Converts an enum value into an integer. Resulting type will be the tag type687 /// Converts an enum value into an integer. Resulting type will be the tag type
688 /// of the enum. Uses `un_node`.688 /// of the enum. Uses `un_node`.
689 enum_to_int,689 enum_to_int,
690 /// Implements the `@typeInfo` builtin. Uses `un_node`.
691 type_info,
690692
691 /// Returns whether the instruction is one of the control flow "noreturn" types.693 /// Returns whether the instruction is one of the control flow "noreturn" types.
692 /// Function calls do not count.694 /// Function calls do not count.
...@@ -850,6 +852,7 @@ pub const Inst = struct {...@@ -850,6 +852,7 @@ pub const Inst = struct {
850 .field_type,852 .field_type,
851 .int_to_enum,853 .int_to_enum,
852 .enum_to_int,854 .enum_to_int,
855 .type_info,
853 => false,856 => false,
854857
855 .@"break",858 .@"break",
...@@ -1652,6 +1655,7 @@ const Writer = struct {...@@ -1652,6 +1655,7 @@ const Writer = struct {
1652 .typeof_elem,1655 .typeof_elem,
1653 .struct_init_empty,1656 .struct_init_empty,
1654 .enum_to_int,1657 .enum_to_int,
1658 .type_info,
1655 => try self.writeUnNode(stream, inst),1659 => try self.writeUnNode(stream, inst),
16561660
1657 .ref,1661 .ref,