diff options
| author | Evan Haas <evan@lagerdata.com> | 2021-06-18 17:36:50 -0700 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2021-06-23 08:44:25 +0300 |
| commit | 0e7897a9a2e8dff1ce869b78d5b6fda56f86bb05 (patch) | |
| tree | ee37a14f5b1d6a7a06eb5860d87d3eda9403035d /lib/std | |
| parent | d1f99eabb745a3026b4bf0486ee542d38facee8b (diff) | |
| download | zig-0e7897a9a2e8dff1ce869b78d5b6fda56f86bb05.tar.gz zig-0e7897a9a2e8dff1ce869b78d5b6fda56f86bb05.zip | |
translate-c: Remove usage of `extern enum`
Translate enum types as the underlying integer type. Translate enum constants
as top-level integer constants of the correct type (which does not necessarily
match the enum integer type).
If an enum constant's type cannot be translated for some reason, omit it.
See discussion https://github.com/ziglang/zig/issues/2115#issuecomment-827968279
Fixes #9153
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/zig/c_translation.zig | 37 |
1 files changed, 2 insertions, 35 deletions
diff --git a/lib/std/zig/c_translation.zig b/lib/std/zig/c_translation.zig index 6e0f721b4f..7851525bb7 100644 --- a/lib/std/zig/c_translation.zig +++ b/lib/std/zig/c_translation.zig @@ -11,7 +11,7 @@ const mem = std.mem; /// Given a type and value, cast the value to the type as c would. pub fn cast(comptime DestType: type, target: anytype) DestType { - // this function should behave like transCCast in translate-c, except it's for macros and enums + // this function should behave like transCCast in translate-c, except it's for macros const SourceType = @TypeOf(target); switch (@typeInfo(DestType)) { .Fn, .Pointer => return castToPtr(DestType, SourceType, target), @@ -20,12 +20,6 @@ pub fn cast(comptime DestType: type, target: anytype) DestType { return castToPtr(DestType, SourceType, target); } }, - .Enum => |enum_type| { - if (@typeInfo(SourceType) == .Int or @typeInfo(SourceType) == .ComptimeInt) { - const intermediate = cast(enum_type.tag_type, target); - return @intToEnum(DestType, intermediate); - } - }, .Int => { switch (@typeInfo(SourceType)) { .Pointer => { @@ -36,9 +30,6 @@ pub fn cast(comptime DestType: type, target: anytype) DestType { return castInt(DestType, @ptrToInt(target)); } }, - .Enum => { - return castInt(DestType, @enumToInt(target)); - }, .Int => { return castInt(DestType, target); }, @@ -106,12 +97,6 @@ fn ptrInfo(comptime PtrType: type) std.builtin.TypeInfo.Pointer { } test "cast" { - const E = enum(u2) { - Zero, - One, - Two, - }; - var i = @as(i64, 10); try testing.expect(cast(*u8, 16) == @intToPtr(*u8, 16)); @@ -122,12 +107,9 @@ test "cast" { try testing.expect(cast(?*i64, @as(*align(1) i64, &i)) == &i); try testing.expect(cast(?*i64, @as(?*align(1) i64, &i)) == &i); - try testing.expect(cast(E, 1) == .One); - try testing.expectEqual(@as(u32, 4), cast(u32, @intToPtr(*u32, 4))); try testing.expectEqual(@as(u32, 4), cast(u32, @intToPtr(?*u32, 4))); try testing.expectEqual(@as(u32, 10), cast(u32, @as(u64, 10))); - try testing.expectEqual(@as(u8, 2), cast(u8, E.Two)); try testing.expectEqual(@bitCast(i32, @as(u32, 0x8000_0000)), cast(i32, @as(u32, 0x8000_0000))); @@ -136,17 +118,6 @@ test "cast" { try testing.expectEqual(@intToPtr(?*c_void, 2), cast(?*c_void, @intToPtr(*u8, 2))); - const C_ENUM = enum(c_int) { - A = 0, - B, - C, - _, - }; - try testing.expectEqual(cast(C_ENUM, @as(i64, -1)), @intToEnum(C_ENUM, -1)); - try testing.expectEqual(cast(C_ENUM, @as(i8, 1)), .B); - try testing.expectEqual(cast(C_ENUM, @as(u64, 1)), .B); - try testing.expectEqual(cast(C_ENUM, @as(u64, 42)), @intToEnum(C_ENUM, 42)); - var foo: c_int = -1; try testing.expect(cast(*c_void, -1) == @intToPtr(*c_void, @bitCast(usize, @as(isize, -1)))); try testing.expect(cast(*c_void, foo) == @intToPtr(*c_void, @bitCast(usize, @as(isize, -1)))); @@ -162,7 +133,7 @@ test "cast" { pub fn sizeof(target: anytype) usize { const T: type = if (@TypeOf(target) == type) target else @TypeOf(target); switch (@typeInfo(T)) { - .Float, .Int, .Struct, .Union, .Enum, .Array, .Bool, .Vector => return @sizeOf(T), + .Float, .Int, .Struct, .Union, .Array, .Bool, .Vector => return @sizeOf(T), .Fn => { // sizeof(main) returns 1, sizeof(&main) returns pointer size. // We cannot distinguish those types in Zig, so use pointer size. @@ -228,7 +199,6 @@ pub fn sizeof(target: anytype) usize { } test "sizeof" { - const E = enum(c_int) { One, _ }; const S = extern struct { a: u32 }; const ptr_size = @sizeOf(*c_void); @@ -239,9 +209,6 @@ test "sizeof" { try testing.expect(sizeof(2.0) == @sizeOf(f64)); - try testing.expect(sizeof(E) == @sizeOf(c_int)); - try testing.expect(sizeof(E.One) == @sizeOf(c_int)); - try testing.expect(sizeof(S) == 4); try testing.expect(sizeof([_]u32{ 4, 5, 6 }) == 12); |
