authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2021-02-23 23:25:11-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-25 22:33:42-08:00
log081698156141ba16a168d9448b9883c0f5a3edd0
treea84e7f82a177ff7d152d62125ea42d5124f833b5
parentfd208d9d5913a0929e444deb97b91092c427bb14

translate-c: add typeof support


5 files changed, 57 insertions(+), 0 deletions(-)

src/clang.zig+10
......@@ -583,6 +583,16 @@ pub const MacroQualifiedType = opaque {
583583 extern fn ZigClangMacroQualifiedType_getModifiedType(*const MacroQualifiedType) QualType;
584584};
585585
586pub const TypeOfType = opaque {
587 pub const getUnderlyingType = ZigClangTypeOfType_getUnderlyingType;
588 extern fn ZigClangTypeOfType_getUnderlyingType(*const TypeOfType) QualType;
589};
590
591pub const TypeOfExprType = opaque {
592 pub const getUnderlyingExpr = ZigClangTypeOfExprType_getUnderlyingExpr;
593 extern fn ZigClangTypeOfExprType_getUnderlyingExpr(*const TypeOfExprType) *const Expr;
594};
595
586596pub const MemberExpr = opaque {
587597 pub const getBase = ZigClangMemberExpr_getBase;
588598 extern fn ZigClangMemberExpr_getBase(*const MemberExpr) *const Expr;
src/translate_c.zig+14
......@@ -3827,6 +3827,20 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan
38273827 const macroqualified_ty = @ptrCast(*const clang.MacroQualifiedType, ty);
38283828 return transQualType(c, scope, macroqualified_ty.getModifiedType(), source_loc);
38293829 },
3830 .TypeOf => {
3831 const typeof_ty = @ptrCast(*const clang.TypeOfType, ty);
3832 return transQualType(c, scope, typeof_ty.getUnderlyingType(), source_loc);
3833 },
3834 .TypeOfExpr => {
3835 const typeofexpr_ty = @ptrCast(*const clang.TypeOfExprType, ty);
3836 const underlying_expr = transExpr(c, scope, typeofexpr_ty.getUnderlyingExpr(), .used) catch |err| switch (err) {
3837 error.UnsupportedTranslation => {
3838 return fail(c, error.UnsupportedType, source_loc, "unsupported underlying expression for TypeOfExpr", .{});
3839 },
3840 else => |e| return e,
3841 };
3842 return Tag.typeof.create(c.arena, underlying_expr);
3843 },
38303844 else => {
38313845 const type_name = c.str(ty.getTypeClassName());
38323846 return fail(c, error.UnsupportedType, source_loc, "unsupported type: '{s}'", .{type_name});
src/zig_clang.cpp+10
......@@ -2599,6 +2599,16 @@ struct ZigClangQualType ZigClangMacroQualifiedType_getModifiedType(const struct
25992599 return bitcast(casted->getModifiedType());
26002600}
26012601
2602struct ZigClangQualType ZigClangTypeOfType_getUnderlyingType(const struct ZigClangTypeOfType *self) {
2603 auto casted = reinterpret_cast<const clang::TypeOfType *>(self);
2604 return bitcast(casted->getUnderlyingType());
2605}
2606
2607const struct ZigClangExpr *ZigClangTypeOfExprType_getUnderlyingExpr(const struct ZigClangTypeOfExprType *self) {
2608 auto casted = reinterpret_cast<const clang::TypeOfExprType *>(self);
2609 return reinterpret_cast<const struct ZigClangExpr *>(casted->getUnderlyingExpr());
2610}
2611
26022612struct ZigClangQualType ZigClangElaboratedType_getNamedType(const struct ZigClangElaboratedType *self) {
26032613 auto casted = reinterpret_cast<const clang::ElaboratedType *>(self);
26042614 return bitcast(casted->getNamedType());
src/zig_clang.h+4
......@@ -1157,6 +1157,10 @@ ZIG_EXTERN_C struct ZigClangQualType ZigClangAttributedType_getEquivalentType(co
11571157
11581158ZIG_EXTERN_C struct ZigClangQualType ZigClangMacroQualifiedType_getModifiedType(const struct ZigClangMacroQualifiedType *);
11591159
1160ZIG_EXTERN_C struct ZigClangQualType ZigClangTypeOfType_getUnderlyingType(const struct ZigClangTypeOfType *);
1161
1162ZIG_EXTERN_C const struct ZigClangExpr *ZigClangTypeOfExprType_getUnderlyingExpr(const struct ZigClangTypeOfExprType *);
1163
11601164ZIG_EXTERN_C struct ZigClangQualType ZigClangElaboratedType_getNamedType(const struct ZigClangElaboratedType *);
11611165ZIG_EXTERN_C enum ZigClangElaboratedTypeKeyword ZigClangElaboratedType_getKeyword(const struct ZigClangElaboratedType *);
11621166
test/run_translated_c.zig+19
......@@ -1054,4 +1054,23 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
10541054 \\ return 0;
10551055 \\}
10561056 , "");
1057
1058 cases.add("typeof operator",
1059 \\#include <stdlib.h>
1060 \\static int FOO = 42;
1061 \\typedef typeof(FOO) foo_type;
1062 \\typeof(foo_type) myfunc(typeof(FOO) x) { return (typeof(FOO)) x; }
1063 \\int main(void) {
1064 \\ int x = FOO;
1065 \\ typeof(x) y = x;
1066 \\ foo_type z = y;
1067 \\ if (x != y) abort();
1068 \\ if (myfunc(z) != x) abort();
1069 \\
1070 \\ const char *my_string = "bar";
1071 \\ typeof (typeof (my_string)[4]) string_arr = {"a","b","c","d"};
1072 \\ if (string_arr[0][0] != 'a' || string_arr[3][0] != 'd') abort();
1073 \\ return 0;
1074 \\}
1075 , "");
10571076}