Commit 23db96931e

Andrew Kelley <andrew@ziglang.org>
2021-04-09 07:38:13
AstGen: implement `@typeInfo` builtin
1 parent 6e05d53
Changed files (3)
src/AstGen.zig
@@ -1356,6 +1356,7 @@ fn blockExprStmts(
                         .opaque_decl,
                         .int_to_enum,
                         .enum_to_int,
+                        .type_info,
                         => break :b false,
 
                         // ZIR instructions that are always either `noreturn` or `void`.
@@ -4198,6 +4199,12 @@ fn builtinCall(
             return rvalue(gz, scope, rl, result, node);
         },
 
+        .type_info => {
+            const operand = try typeExpr(gz, scope, params[0]);
+            const result = try gz.addUnNode(.type_info, operand, node);
+            return rvalue(gz, scope, rl, result, node);
+        },
+
         .add_with_overflow,
         .align_cast,
         .align_of,
@@ -4274,7 +4281,6 @@ fn builtinCall(
         .This,
         .truncate,
         .Type,
-        .type_info,
         .type_name,
         .union_init,
         => return mod.failNode(scope, node, "TODO: implement builtin function {s}", .{
src/Sema.zig
@@ -259,6 +259,7 @@ pub fn analyzeBody(
             .switch_capture_multi_ref => try sema.zirSwitchCapture(block, inst, true, true),
             .switch_capture_else => try sema.zirSwitchCaptureElse(block, inst, false),
             .switch_capture_else_ref => try sema.zirSwitchCaptureElse(block, inst, true),
+            .type_info => try sema.zirTypeInfo(block, inst),
             .typeof => try sema.zirTypeof(block, inst),
             .typeof_elem => try sema.zirTypeofElem(block, inst),
             .typeof_peer => try sema.zirTypeofPeer(block, inst),
@@ -4078,6 +4079,12 @@ fn zirCmp(
     return block.addBinOp(src, bool_type, tag, casted_lhs, casted_rhs);
 }
 
+fn zirTypeInfo(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
+    const inst_data = sema.code.instructions.items(.data)[inst].un_node;
+    const src = inst_data.src();
+    return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirTypeInfo", .{});
+}
+
 fn zirTypeof(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst {
     const inst_data = sema.code.instructions.items(.data)[inst].un_node;
     const src = inst_data.src();
src/zir.zig
@@ -687,6 +687,8 @@ pub const Inst = struct {
         /// Converts an enum value into an integer. Resulting type will be the tag type
         /// of the enum. Uses `un_node`.
         enum_to_int,
+        /// Implements the `@typeInfo` builtin. Uses `un_node`.
+        type_info,
 
         /// Returns whether the instruction is one of the control flow "noreturn" types.
         /// Function calls do not count.
@@ -850,6 +852,7 @@ pub const Inst = struct {
                 .field_type,
                 .int_to_enum,
                 .enum_to_int,
+                .type_info,
                 => false,
 
                 .@"break",
@@ -1652,6 +1655,7 @@ const Writer = struct {
             .typeof_elem,
             .struct_init_empty,
             .enum_to_int,
+            .type_info,
             => try self.writeUnNode(stream, inst),
 
             .ref,