authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2021-03-07 13:34:27-08:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-03-08 10:09:12+02:00
logc760532be04667c8a8d49a7cf1b7582da3d1632d
tree01b4338c4d686ba9b41e5783603d52cb80605fc3
parentb988815bf0771dd86a345ce8ed8b0d3eb9d6f55e

translate-c: Add compound literal support


5 files changed, 34 insertions(+), 2 deletions(-)

src/clang.zig+5
...@@ -271,6 +271,11 @@ pub const CompoundAssignOperator = opaque {...@@ -271,6 +271,11 @@ pub const CompoundAssignOperator = opaque {
271 extern fn ZigClangCompoundAssignOperator_getRHS(*const CompoundAssignOperator) *const Expr;271 extern fn ZigClangCompoundAssignOperator_getRHS(*const CompoundAssignOperator) *const Expr;
272};272};
273273
274pub const CompoundLiteralExpr = opaque {
275 pub const getInitializer = ZigClangCompoundLiteralExpr_getInitializer;
276 extern fn ZigClangCompoundLiteralExpr_getInitializer(*const CompoundLiteralExpr) *const Expr;
277};
278
274pub const CompoundStmt = opaque {279pub const CompoundStmt = opaque {
275 pub const body_begin = ZigClangCompoundStmt_body_begin;280 pub const body_begin = ZigClangCompoundStmt_body_begin;
276 extern fn ZigClangCompoundStmt_body_begin(*const CompoundStmt) ConstBodyIterator;281 extern fn ZigClangCompoundStmt_body_begin(*const CompoundStmt) ConstBodyIterator;
src/translate_c.zig+6-2
...@@ -1054,6 +1054,10 @@ fn transStmt(...@@ -1054,6 +1054,10 @@ fn transStmt(
1054 return maybeSuppressResult(c, scope, result_used, expr);1054 return maybeSuppressResult(c, scope, result_used, expr);
1055 },1055 },
1056 .OffsetOfExprClass => return transOffsetOfExpr(c, scope, @ptrCast(*const clang.OffsetOfExpr, stmt), result_used),1056 .OffsetOfExprClass => return transOffsetOfExpr(c, scope, @ptrCast(*const clang.OffsetOfExpr, stmt), result_used),
1057 .CompoundLiteralExprClass => {
1058 const compound_literal = @ptrCast(*const clang.CompoundLiteralExpr, stmt);
1059 return transExpr(c, scope, compound_literal.getInitializer(), result_used);
1060 },
1057 else => {1061 else => {
1058 return fail(c, error.UnsupportedTranslation, stmt.getBeginLoc(), "TODO implement translation of stmt class {s}", .{@tagName(sc)});1062 return fail(c, error.UnsupportedTranslation, stmt.getBeginLoc(), "TODO implement translation of stmt class {s}", .{@tagName(sc)});
1059 },1063 },
...@@ -2560,8 +2564,8 @@ fn transConstantExpr(c: *Context, scope: *Scope, expr: *const clang.Expr, used:...@@ -2560,8 +2564,8 @@ fn transConstantExpr(c: *Context, scope: *Scope, expr: *const clang.Expr, used:
2560 });2564 });
2561 return maybeSuppressResult(c, scope, used, as_node);2565 return maybeSuppressResult(c, scope, used, as_node);
2562 },2566 },
2563 else => {2567 else => |kind| {
2564 return fail(c, error.UnsupportedTranslation, expr.getBeginLoc(), "unsupported constant expression kind", .{});2568 return fail(c, error.UnsupportedTranslation, expr.getBeginLoc(), "unsupported constant expression kind '{s}'", .{kind});
2565 },2569 },
2566 }2570 }
2567}2571}
src/zig_clang.cpp+5
...@@ -2808,6 +2808,11 @@ const struct ZigClangExpr *ZigClangCompoundAssignOperator_getRHS(const struct Zi...@@ -2808,6 +2808,11 @@ const struct ZigClangExpr *ZigClangCompoundAssignOperator_getRHS(const struct Zi
2808 return reinterpret_cast<const struct ZigClangExpr *>(casted->getRHS());2808 return reinterpret_cast<const struct ZigClangExpr *>(casted->getRHS());
2809}2809}
28102810
2811const struct ZigClangExpr *ZigClangCompoundLiteralExpr_getInitializer(const ZigClangCompoundLiteralExpr *self) {
2812 auto casted = reinterpret_cast<const clang::CompoundLiteralExpr *>(self);
2813 return reinterpret_cast<const ZigClangExpr *>(casted->getInitializer());
2814}
2815
2811enum ZigClangUO ZigClangUnaryOperator_getOpcode(const struct ZigClangUnaryOperator *self) {2816enum ZigClangUO ZigClangUnaryOperator_getOpcode(const struct ZigClangUnaryOperator *self) {
2812 auto casted = reinterpret_cast<const clang::UnaryOperator *>(self);2817 auto casted = reinterpret_cast<const clang::UnaryOperator *>(self);
2813 return (ZigClangUO)casted->getOpcode();2818 return (ZigClangUO)casted->getOpcode();
src/zig_clang.h+2
...@@ -1217,6 +1217,8 @@ ZIG_EXTERN_C enum ZigClangBO ZigClangCompoundAssignOperator_getOpcode(const stru...@@ -1217,6 +1217,8 @@ ZIG_EXTERN_C enum ZigClangBO ZigClangCompoundAssignOperator_getOpcode(const stru
1217ZIG_EXTERN_C const struct ZigClangExpr *ZigClangCompoundAssignOperator_getLHS(const struct ZigClangCompoundAssignOperator *);1217ZIG_EXTERN_C const struct ZigClangExpr *ZigClangCompoundAssignOperator_getLHS(const struct ZigClangCompoundAssignOperator *);
1218ZIG_EXTERN_C const struct ZigClangExpr *ZigClangCompoundAssignOperator_getRHS(const struct ZigClangCompoundAssignOperator *);1218ZIG_EXTERN_C const struct ZigClangExpr *ZigClangCompoundAssignOperator_getRHS(const struct ZigClangCompoundAssignOperator *);
12191219
1220ZIG_EXTERN_C const struct ZigClangExpr *ZigClangCompoundLiteralExpr_getInitializer(const struct ZigClangCompoundLiteralExpr *);
1221
1220ZIG_EXTERN_C enum ZigClangUO ZigClangUnaryOperator_getOpcode(const struct ZigClangUnaryOperator *);1222ZIG_EXTERN_C enum ZigClangUO ZigClangUnaryOperator_getOpcode(const struct ZigClangUnaryOperator *);
1221ZIG_EXTERN_C struct ZigClangQualType ZigClangUnaryOperator_getType(const struct ZigClangUnaryOperator *);1223ZIG_EXTERN_C struct ZigClangQualType ZigClangUnaryOperator_getType(const struct ZigClangUnaryOperator *);
1222ZIG_EXTERN_C const struct ZigClangExpr *ZigClangUnaryOperator_getSubExpr(const struct ZigClangUnaryOperator *);1224ZIG_EXTERN_C const struct ZigClangExpr *ZigClangUnaryOperator_getSubExpr(const struct ZigClangUnaryOperator *);
test/run_translated_c.zig+16
...@@ -1171,4 +1171,20 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {...@@ -1171,4 +1171,20 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
1171 \\ return 0;1171 \\ return 0;
1172 \\}1172 \\}
1173 , "");1173 , "");
1174
1175 cases.add("Compound literals",
1176 \\#include <stdlib.h>
1177 \\struct Foo {
1178 \\ int a;
1179 \\ char b[2];
1180 \\ float c;
1181 \\};
1182 \\int main() {
1183 \\ struct Foo foo;
1184 \\ int x = 1, y = 2;
1185 \\ foo = (struct Foo) {x + y, {'a', 'b'}, 42.0f};
1186 \\ if (foo.a != x + y || foo.b[0] != 'a' || foo.b[1] != 'b' || foo.c != 42.0f) abort();
1187 \\ return 0;
1188 \\}
1189 , "");
1174}1190}