Commit 8c15cfe3da

Charlie Stanton <charlie@shtanton.com>
2020-06-21 22:48:12
Compacts switch statements and string literal
1 parent 6f47513
Changed files (2)
lib
src-self-hosted
lib/std/meta.zig
@@ -694,16 +694,14 @@ pub fn Vector(comptime len: u32, comptime child: type) type {
     });
 }
 
-/// Given a type and value, cast the value to the type as c would
+/// Given a type and value, cast the value to the type as c would.
+/// This is for translate-c and is not intended for general use.
 pub fn cast(comptime DestType: type, target: var) DestType {
     const TargetType = @TypeOf(target);
     switch (@typeInfo(DestType)) {
-        .Pointer => |_| {
+        .Pointer => {
             switch (@typeInfo(TargetType)) {
-                .Int => |_| {
-                    return @intToPtr(DestType, target);
-                },
-                .ComptimeInt => |_| {
+                .Int, .ComptimeInt => {
                     return @intToPtr(DestType, target);
                 },
                 .Pointer => |ptr| {
@@ -720,10 +718,7 @@ pub fn cast(comptime DestType: type, target: var) DestType {
         .Optional => |opt| {
             if (@typeInfo(opt.child) == .Pointer) {
                 switch (@typeInfo(TargetType)) {
-                    .Int => |_| {
-                        return @intToPtr(DestType, target);
-                    },
-                    .ComptimeInt => |_| {
+                    .Int, .ComptimeInt => {
                         return @intToPtr(DestType, target);
                     },
                     .Pointer => |ptr| {
@@ -738,19 +733,14 @@ pub fn cast(comptime DestType: type, target: var) DestType {
                 }
             }
         },
-        .Enum => |_| {
-            if (@typeInfo(TargetType) == .Int or @typeInfo(TargetType) == .ComptimeInt) {
-                return @intToEnum(DestType, target);
-            }
-        },
-        .EnumLiteral => |_| {
+        .Enum, .EnumLiteral => {
             if (@typeInfo(TargetType) == .Int or @typeInfo(TargetType) == .ComptimeInt) {
                 return @intToEnum(DestType, target);
             }
         },
-        .Int => |_| {
+        .Int, .ComptimeInt => {
             switch (@typeInfo(TargetType)) {
-                .Pointer => |_| {
+                .Pointer => {
                     return @as(DestType, @ptrToInt(target));
                 },
                 .Optional => |opt| {
@@ -758,10 +748,7 @@ pub fn cast(comptime DestType: type, target: var) DestType {
                         return @as(DestType, @ptrToInt(target));
                     }
                 },
-                .Enum => |_| {
-                    return @as(DestType, @enumToInt(target));
-                },
-                .EnumLiteral => |_| {
+                .Enum, .EnumLiteral => {
                     return @as(DestType, @enumToInt(target));
                 },
                 else => {},
src-self-hosted/translate_c.zig
@@ -5670,12 +5670,8 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
 
             //(@import("std").meta.cast(dest, x))
             const import_fn_call = try c.createBuiltinCall("@import", 1);
-            const std_token = try appendToken(c, .StringLiteral, "\"std\"");
-            const std_node = try c.arena.create(ast.Node.StringLiteral);
-            std_node.* = .{
-                .token = std_token,
-            };
-            import_fn_call.params()[0] = &std_node.base;
+            const std_node = try transCreateNodeStringLiteral(c, "\"std\"");
+            import_fn_call.params()[0] = std_node;
             import_fn_call.rparen_token = try appendToken(c, .RParen, ")");
             const inner_field_access = try transCreateNodeFieldAccess(c, &import_fn_call.base, "meta");
             const outer_field_access = try transCreateNodeFieldAccess(c, inner_field_access, "cast");