authorgravatar for charlie@shtanton.comCharlie Stanton <charlie@shtanton.com> 2020-06-21 21:48:12+01:00
committergravatar for charlie@shtanton.comCharlie Stanton <charlie@shtanton.com> 2020-06-21 21:48:12+01:00
log8c15cfe3dabe736754b138f16de905e6487bd8b5
tree1afd1e6d298171fa35d0cb4c50330ae983446c1a
parent6f475130098a5913b485828c72cb47ab8146f9ea

Compacts switch statements and string literal


2 files changed, 11 insertions(+), 28 deletions(-)

lib/std/meta.zig+9-22
...@@ -694,16 +694,14 @@ pub fn Vector(comptime len: u32, comptime child: type) type {...@@ -694,16 +694,14 @@ pub fn Vector(comptime len: u32, comptime child: type) type {
694 });694 });
695}695}
696696
697/// Given a type and value, cast the value to the type as c would697/// Given a type and value, cast the value to the type as c would.
698/// This is for translate-c and is not intended for general use.
698pub fn cast(comptime DestType: type, target: var) DestType {699pub fn cast(comptime DestType: type, target: var) DestType {
699 const TargetType = @TypeOf(target);700 const TargetType = @TypeOf(target);
700 switch (@typeInfo(DestType)) {701 switch (@typeInfo(DestType)) {
701 .Pointer => |_| {702 .Pointer => {
702 switch (@typeInfo(TargetType)) {703 switch (@typeInfo(TargetType)) {
703 .Int => |_| {704 .Int, .ComptimeInt => {
704 return @intToPtr(DestType, target);
705 },
706 .ComptimeInt => |_| {
707 return @intToPtr(DestType, target);705 return @intToPtr(DestType, target);
708 },706 },
709 .Pointer => |ptr| {707 .Pointer => |ptr| {
...@@ -720,10 +718,7 @@ pub fn cast(comptime DestType: type, target: var) DestType {...@@ -720,10 +718,7 @@ pub fn cast(comptime DestType: type, target: var) DestType {
720 .Optional => |opt| {718 .Optional => |opt| {
721 if (@typeInfo(opt.child) == .Pointer) {719 if (@typeInfo(opt.child) == .Pointer) {
722 switch (@typeInfo(TargetType)) {720 switch (@typeInfo(TargetType)) {
723 .Int => |_| {721 .Int, .ComptimeInt => {
724 return @intToPtr(DestType, target);
725 },
726 .ComptimeInt => |_| {
727 return @intToPtr(DestType, target);722 return @intToPtr(DestType, target);
728 },723 },
729 .Pointer => |ptr| {724 .Pointer => |ptr| {
...@@ -738,19 +733,14 @@ pub fn cast(comptime DestType: type, target: var) DestType {...@@ -738,19 +733,14 @@ pub fn cast(comptime DestType: type, target: var) DestType {
738 }733 }
739 }734 }
740 },735 },
741 .Enum => |_| {736 .Enum, .EnumLiteral => {
742 if (@typeInfo(TargetType) == .Int or @typeInfo(TargetType) == .ComptimeInt) {
743 return @intToEnum(DestType, target);
744 }
745 },
746 .EnumLiteral => |_| {
747 if (@typeInfo(TargetType) == .Int or @typeInfo(TargetType) == .ComptimeInt) {737 if (@typeInfo(TargetType) == .Int or @typeInfo(TargetType) == .ComptimeInt) {
748 return @intToEnum(DestType, target);738 return @intToEnum(DestType, target);
749 }739 }
750 },740 },
751 .Int => |_| {741 .Int, .ComptimeInt => {
752 switch (@typeInfo(TargetType)) {742 switch (@typeInfo(TargetType)) {
753 .Pointer => |_| {743 .Pointer => {
754 return @as(DestType, @ptrToInt(target));744 return @as(DestType, @ptrToInt(target));
755 },745 },
756 .Optional => |opt| {746 .Optional => |opt| {
...@@ -758,10 +748,7 @@ pub fn cast(comptime DestType: type, target: var) DestType {...@@ -758,10 +748,7 @@ pub fn cast(comptime DestType: type, target: var) DestType {
758 return @as(DestType, @ptrToInt(target));748 return @as(DestType, @ptrToInt(target));
759 }749 }
760 },750 },
761 .Enum => |_| {751 .Enum, .EnumLiteral => {
762 return @as(DestType, @enumToInt(target));
763 },
764 .EnumLiteral => |_| {
765 return @as(DestType, @enumToInt(target));752 return @as(DestType, @enumToInt(target));
766 },753 },
767 else => {},754 else => {},
src-self-hosted/translate_c.zig+2-6
...@@ -5670,12 +5670,8 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,...@@ -5670,12 +5670,8 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
56705670
5671 //(@import("std").meta.cast(dest, x))5671 //(@import("std").meta.cast(dest, x))
5672 const import_fn_call = try c.createBuiltinCall("@import", 1);5672 const import_fn_call = try c.createBuiltinCall("@import", 1);
5673 const std_token = try appendToken(c, .StringLiteral, "\"std\"");5673 const std_node = try transCreateNodeStringLiteral(c, "\"std\"");
5674 const std_node = try c.arena.create(ast.Node.StringLiteral);5674 import_fn_call.params()[0] = std_node;
5675 std_node.* = .{
5676 .token = std_token,
5677 };
5678 import_fn_call.params()[0] = &std_node.base;
5679 import_fn_call.rparen_token = try appendToken(c, .RParen, ")");5675 import_fn_call.rparen_token = try appendToken(c, .RParen, ")");
5680 const inner_field_access = try transCreateNodeFieldAccess(c, &import_fn_call.base, "meta");5676 const inner_field_access = try transCreateNodeFieldAccess(c, &import_fn_call.base, "meta");
5681 const outer_field_access = try transCreateNodeFieldAccess(c, inner_field_access, "cast");5677 const outer_field_access = try transCreateNodeFieldAccess(c, inner_field_access, "cast");