authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-28 14:26:37-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-12-28 14:26:37-05:00
log68aa221995208a7471290235a86bd184d0057782
treee54023212fe78b4cd73eaaedab7097941e0d2b66
parent6c3ccea29b2034654558a0b8e839a4a5ee84c57f
parent6070ffc28ea8aa64894d62df699fa6def6fc6d8a
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #3986 from LemonBoy/translate-c-stuff

Two small translate-c(-2) patches

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

src-self-hosted/translate_c.zig+34-8
......@@ -725,11 +725,15 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
725725 };
726726
727727 const int_type = ZigClangEnumDecl_getIntegerType(enum_decl);
728 // The underlying type may be null in case of forward-declared enum
729 // types, while that's not ISO-C compliant many compilers allow this and
730 // default to the usual integer type used for all the enums.
728731
729732 // TODO only emit this tag type if the enum tag type is not the default.
730733 // I don't know what the default is, need to figure out how clang is deciding.
731734 // it appears to at least be different across gcc/msvc
732 if (!isCBuiltinType(int_type, .UInt) and
735 if (int_type.ptr != null and
736 !isCBuiltinType(int_type, .UInt) and
733737 !isCBuiltinType(int_type, .Int))
734738 {
735739 _ = try appendToken(c, .LParen, "(");
......@@ -1555,8 +1559,7 @@ fn transCCast(
15551559 const elaborated_ty = @ptrCast(*const ZigClangElaboratedType, ZigClangQualType_getTypePtr(dst_type));
15561560 return transCCast(rp, scope, loc, ZigClangElaboratedType_getNamedType(elaborated_ty), src_type, expr);
15571561 }
1558 if (ZigClangQualType_getTypeClass(dst_type) == .Enum)
1559 {
1562 if (ZigClangQualType_getTypeClass(dst_type) == .Enum) {
15601563 const builtin_node = try transCreateNodeBuiltinFnCall(rp.c, "@intToEnum");
15611564 try builtin_node.params.push(try transQualType(rp, dst_type, loc));
15621565 _ = try appendToken(rp.c, .Comma, ",");
......@@ -2145,7 +2148,7 @@ fn transCallExpr(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangCallExpr,
21452148 node.rtoken = try appendToken(rp.c, .RParen, ")");
21462149
21472150 if (fn_ty) |ty| {
2148 const canon = ZigClangQualType_getCanonicalType(ZigClangFunctionProtoType_getReturnType(ty));
2151 const canon = ZigClangQualType_getCanonicalType(ty.getReturnType());
21492152 const ret_ty = ZigClangQualType_getTypePtr(canon);
21502153 if (ZigClangType_isVoidType(ret_ty)) {
21512154 _ = try appendToken(rp.c, .Semicolon, ";");
......@@ -2156,7 +2159,19 @@ fn transCallExpr(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangCallExpr,
21562159 return maybeSuppressResult(rp, scope, result_used, &node.base);
21572160}
21582161
2159fn qualTypeGetFnProto(qt: ZigClangQualType, is_ptr: *bool) ?*const ZigClangFunctionProtoType {
2162const ClangFunctionType = union(enum) {
2163 Proto: *const ZigClangFunctionProtoType,
2164 NoProto: *const ZigClangFunctionType,
2165
2166 fn getReturnType(self: @This()) ZigClangQualType {
2167 switch (@as(@TagType(@This()), self)) {
2168 .Proto => return ZigClangFunctionProtoType_getReturnType(self.Proto),
2169 .NoProto => return ZigClangFunctionType_getReturnType(self.NoProto),
2170 }
2171 }
2172};
2173
2174fn qualTypeGetFnProto(qt: ZigClangQualType, is_ptr: *bool) ?ClangFunctionType {
21602175 const canon = ZigClangQualType_getCanonicalType(qt);
21612176 var ty = ZigClangQualType_getTypePtr(canon);
21622177 is_ptr.* = false;
......@@ -2167,7 +2182,10 @@ fn qualTypeGetFnProto(qt: ZigClangQualType, is_ptr: *bool) ?*const ZigClangFunct
21672182 ty = ZigClangQualType_getTypePtr(child_qt);
21682183 }
21692184 if (ZigClangType_getTypeClass(ty) == .FunctionProto) {
2170 return @ptrCast(*const ZigClangFunctionProtoType, ty);
2185 return ClangFunctionType{ .Proto = @ptrCast(*const ZigClangFunctionProtoType, ty) };
2186 }
2187 if (ZigClangType_getTypeClass(ty) == .FunctionNoProto) {
2188 return ClangFunctionType{ .NoProto = @ptrCast(*const ZigClangFunctionType, ty) };
21712189 }
21722190 return null;
21732191}
......@@ -2771,7 +2789,10 @@ fn qualTypeChildIsFnProto(qt: ZigClangQualType) bool {
27712789 .Paren => {
27722790 const paren_type = @ptrCast(*const ZigClangParenType, ty);
27732791 const inner_type = ZigClangParenType_getInnerType(paren_type);
2774 return ZigClangQualType_getTypeClass(inner_type) == .FunctionProto;
2792 switch (ZigClangQualType_getTypeClass(inner_type)) {
2793 .FunctionProto, .FunctionNoProto => return true,
2794 else => return false,
2795 }
27752796 },
27762797 .Attributed => {
27772798 const attr_type = @ptrCast(*const ZigClangAttributedType, ty);
......@@ -3571,11 +3592,16 @@ fn transType(rp: RestorePoint, ty: *const ZigClangType, source_loc: ZigClangSour
35713592 else => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported builtin type", .{}),
35723593 });
35733594 },
3574 .FunctionProto, .FunctionNoProto => {
3595 .FunctionProto => {
35753596 const fn_proto_ty = @ptrCast(*const ZigClangFunctionProtoType, ty);
35763597 const fn_proto = try transFnProto(rp, null, fn_proto_ty, source_loc, null, false);
35773598 return &fn_proto.base;
35783599 },
3600 .FunctionNoProto => {
3601 const fn_no_proto_ty = @ptrCast(*const ZigClangFunctionType, ty);
3602 const fn_proto = try transFnNoProto(rp, fn_no_proto_ty, source_loc, null, false);
3603 return &fn_proto.base;
3604 },
35793605 .Paren => {
35803606 const paren_ty = @ptrCast(*const ZigClangParenType, ty);
35813607 return transQualType(rp, ZigClangParenType_getInnerType(paren_ty), source_loc);
test/translate_c.zig+21-2
......@@ -814,6 +814,25 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
814814
815815 /////////////// Cases that pass for only stage2 ////////////////
816816
817 cases.add_2("Forward-declared enum",
818 \\extern enum enum_ty my_enum;
819 \\enum enum_ty { FOO };
820 , &[_][]const u8{
821 \\pub const FOO = 0;
822 \\pub const enum_enum_ty = extern enum {
823 \\ FOO,
824 \\};
825 \\pub extern var my_enum: enum_enum_ty;
826 });
827
828 cases.add_2("Parameterless function pointers",
829 \\typedef void (*fn0)();
830 \\typedef void (*fn1)(char);
831 , &[_][]const u8{
832 \\pub const fn0 = ?extern fn (...) void;
833 \\pub const fn1 = ?extern fn (u8) void;
834 });
835
817836 cases.add_2("Parameterless function prototypes",
818837 \\void a() {}
819838 \\void b(void) {}
......@@ -1024,7 +1043,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
10241043 \\pub inline fn glClearUnion(arg_2: GLbitfield) void {
10251044 \\ return glProcs.gl.Clear.?(arg_2);
10261045 \\}
1027 ,
1046 ,
10281047 \\pub const OpenGLProcs = union_OpenGLProcs;
10291048 });
10301049
......@@ -2166,7 +2185,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21662185 });
21672186
21682187 cases.add_2("macro cast",
2169 \\#define FOO(bar) baz((void *)(baz))
2188 \\#define FOO(bar) baz((void *)(baz))
21702189 , &[_][]const u8{
21712190 \\pub inline fn FOO(bar: var) @TypeOf(baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast([*c]void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr([*c]void, baz) else @as([*c]void, baz))) {
21722191 \\ return baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast([*c]void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr([*c]void, baz) else @as([*c]void, baz));