zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 8c15cfe3dabe736754b138f16de905e6487bd8b5 (tree)
parent 6f475130098a5913b485828c72cb47ab8146f9ea
Author: Charlie Stanton <charlie@shtanton.com>
Date:   Sun, 21 Jun 2020 21:48:12 +0100

Compacts switch statements and string literal

Diffstat:
Mlib/std/meta.zig | 31+++++++++----------------------
Msrc-self-hosted/translate_c.zig | 8++------
2 files changed, 11 insertions(+), 28 deletions(-)

diff --git a/lib/std/meta.zig b/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 => {}, diff --git a/src-self-hosted/translate_c.zig b/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");