authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-02 14:57:48-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-01-02 14:57:48-05:00
logfe21d84c94e16db56cf6f87040a02213615873a4
tree1fc8c259945a81fde2ae915bbfbe3132b48d6b60
parentb0fa2ff85334c2af626a313bb3254ce741435d55
parent0f1595e72c9b9b74b650c0b5144c285bb99d5f23
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4040 from Vexu/translate-c-cast-fixes

Translate c cast fixes

2 files changed, 36 insertions(+), 55 deletions(-)

src-self-hosted/translate_c.zig+18-12
......@@ -898,11 +898,25 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
898898
899899 try container_node.fields_and_decls.push(&field_node.base);
900900 _ = try appendToken(c, .Comma, ",");
901
901902 // In C each enum value is in the global namespace. So we put them there too.
902903 // At this point we can rely on the enum emitting successfully.
903904 const tld_node = try transCreateNodeVarDecl(c, true, true, enum_val_name);
904905 tld_node.eq_token = try appendToken(c, .Equal, "=");
905 tld_node.init_node = try transCreateNodeAPInt(c, ZigClangEnumConstantDecl_getInitVal(enum_const));
906 const cast_node = try transCreateNodeBuiltinFnCall(rp.c, "@enumToInt");
907 const enum_ident = try transCreateNodeIdentifier(c, name);
908 const period_tok = try appendToken(c, .Period, ".");
909 const field_ident = try transCreateNodeIdentifier(c, field_name);
910 const field_access_node = try c.a().create(ast.Node.InfixOp);
911 field_access_node.* = .{
912 .op_token = period_tok,
913 .lhs = enum_ident,
914 .op = .Period,
915 .rhs = field_ident,
916 };
917 try cast_node.params.push(&field_access_node.base);
918 cast_node.rparen_token = try appendToken(rp.c, .RParen, ")");
919 tld_node.init_node = &cast_node.base;
906920 tld_node.semicolon_token = try appendToken(c, .Semicolon, ";");
907921 try addTopLevelDecl(c, field_name, &tld_node.base);
908922 }
......@@ -1712,15 +1726,7 @@ fn transCCast(
17121726 builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")");
17131727 return &builtin_node.base;
17141728 }
1715 if (ZigClangQualType_getTypeClass(src_type) == .Elaborated) {
1716 const elaborated_ty = @ptrCast(*const ZigClangElaboratedType, ZigClangQualType_getTypePtr(src_type));
1717 return transCCast(rp, scope, loc, dst_type, ZigClangElaboratedType_getNamedType(elaborated_ty), expr);
1718 }
1719 if (ZigClangQualType_getTypeClass(dst_type) == .Elaborated) {
1720 const elaborated_ty = @ptrCast(*const ZigClangElaboratedType, ZigClangQualType_getTypePtr(dst_type));
1721 return transCCast(rp, scope, loc, ZigClangElaboratedType_getNamedType(elaborated_ty), src_type, expr);
1722 }
1723 if (ZigClangQualType_getTypeClass(dst_type) == .Enum) {
1729 if (ZigClangQualType_getTypeClass(ZigClangQualType_getCanonicalType(dst_type)) == .Enum) {
17241730 const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, "@intToEnum");
17251731 try builtin_node.params.push(try transQualType(rp, dst_type, loc));
17261732 _ = try appendToken(rp.c, .Comma, ",");
......@@ -1728,8 +1734,8 @@ fn transCCast(
17281734 builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")");
17291735 return &builtin_node.base;
17301736 }
1731 if (ZigClangQualType_getTypeClass(src_type) == .Enum and
1732 ZigClangQualType_getTypeClass(dst_type) != .Enum)
1737 if (ZigClangQualType_getTypeClass(ZigClangQualType_getCanonicalType(src_type)) == .Enum and
1738 ZigClangQualType_getTypeClass(ZigClangQualType_getCanonicalType(dst_type)) != .Enum)
17331739 {
17341740 const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, "@enumToInt");
17351741 try builtin_node.params.push(expr);
test/translate_c.zig+18-43
......@@ -912,7 +912,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
912912 \\extern enum enum_ty my_enum;
913913 \\enum enum_ty { FOO };
914914 , &[_][]const u8{
915 \\pub const FOO = 0;
915 \\pub const FOO = @enumToInt(enum_enum_ty.FOO);
916916 \\pub const enum_enum_ty = extern enum {
917917 \\ FOO,
918918 \\};
......@@ -990,27 +990,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
990990 \\ p,
991991 \\};
992992 , &[_][]const u8{
993 \\pub const a = 0;
994 \\pub const b = 1;
995 \\pub const c = 2;
993 \\pub const a = @enumToInt(enum_unnamed_1.a);
994 \\pub const b = @enumToInt(enum_unnamed_1.b);
995 \\pub const c = @enumToInt(enum_unnamed_1.c);
996996 \\const enum_unnamed_1 = extern enum {
997997 \\ a,
998998 \\ b,
999999 \\ c,
10001000 \\};
10011001 \\pub const d = enum_unnamed_1;
1002 \\pub const e = 0;
1003 \\pub const f = 4;
1004 \\pub const g = 5;
1002 \\pub const e = @enumToInt(enum_unnamed_2.e);
1003 \\pub const f = @enumToInt(enum_unnamed_2.f);
1004 \\pub const g = @enumToInt(enum_unnamed_2.g);
10051005 \\const enum_unnamed_2 = extern enum {
10061006 \\ e = 0,
10071007 \\ f = 4,
10081008 \\ g = 5,
10091009 \\};
10101010 \\pub export var h: enum_unnamed_2 = @intToEnum(enum_unnamed_2, e);
1011 \\pub const i = 0;
1012 \\pub const j = 1;
1013 \\pub const k = 2;
1011 \\pub const i = @enumToInt(enum_unnamed_3.i);
1012 \\pub const j = @enumToInt(enum_unnamed_3.j);
1013 \\pub const k = @enumToInt(enum_unnamed_3.k);
10141014 \\const enum_unnamed_3 = extern enum {
10151015 \\ i,
10161016 \\ j,
......@@ -1020,9 +1020,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
10201020 \\ l: enum_unnamed_3,
10211021 \\ m: d,
10221022 \\};
1023 \\pub const n = 0;
1024 \\pub const o = 1;
1025 \\pub const p = 2;
1023 \\pub const n = @enumToInt(enum_i.n);
1024 \\pub const o = @enumToInt(enum_i.o);
1025 \\pub const p = @enumToInt(enum_i.p);
10261026 \\pub const enum_i = extern enum {
10271027 \\ n,
10281028 \\ o,
......@@ -1448,8 +1448,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14481448 \\ Two,
14491449 \\};
14501450 , &[_][]const u8{
1451 \\pub const One = 0;
1452 \\pub const Two = 1;
1451 \\pub const One = @enumToInt(enum_unnamed_1.One);
1452 \\pub const Two = @enumToInt(enum_unnamed_1.Two);
14531453 \\const enum_unnamed_1 = extern enum {
14541454 \\ One,
14551455 \\ Two,
......@@ -2252,28 +2252,6 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
22522252 \\pub const FOO_CHAR = '\x3f';
22532253 });
22542254
2255 cases.add("enums",
2256 \\enum Foo {
2257 \\ FooA,
2258 \\ FooB,
2259 \\ Foo1,
2260 \\};
2261 , &[_][]const u8{
2262 \\pub const enum_Foo = extern enum {
2263 \\ A,
2264 \\ B,
2265 \\ @"1",
2266 \\};
2267 ,
2268 \\pub const FooA = 0;
2269 ,
2270 \\pub const FooB = 1;
2271 ,
2272 \\pub const Foo1 = 2;
2273 ,
2274 \\pub const Foo = enum_Foo;
2275 });
2276
22772255 cases.add("enums",
22782256 \\enum Foo {
22792257 \\ FooA = 2,
......@@ -2281,17 +2259,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
22812259 \\ Foo1,
22822260 \\};
22832261 , &[_][]const u8{
2262 \\pub const FooA = @enumToInt(enum_Foo.A);
2263 \\pub const FooB = @enumToInt(enum_Foo.B);
2264 \\pub const Foo1 = @enumToInt(enum_Foo.@"1");
22842265 \\pub const enum_Foo = extern enum {
22852266 \\ A = 2,
22862267 \\ B = 5,
22872268 \\ @"1" = 6,
22882269 \\};
2289 ,
2290 \\pub const FooA = 2;
2291 ,
2292 \\pub const FooB = 5;
2293 ,
2294 \\pub const Foo1 = 6;
22952270 ,
22962271 \\pub const Foo = enum_Foo;
22972272 });