From 1f82c7ba2243bb16a5036a22d34e9762302303de Mon Sep 17 00:00:00 2001 From: hryx Date: Sat, 8 Jun 2019 15:54:15 -0700 Subject: [PATCH] transCStyleCastExpr --- src-self-hosted/clang.zig | 4 ++++ src-self-hosted/translate_c.zig | 39 +++++++++++++++++++++++++-------- src/zig_clang.cpp | 15 +++++++++++++ src/zig_clang.h | 4 ++++ 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src-self-hosted/clang.zig b/src-self-hosted/clang.zig index 4bc027dfa8..c8faf1e56e 100644 --- a/src-self-hosted/clang.zig +++ b/src-self-hosted/clang.zig @@ -894,3 +894,7 @@ pub extern fn ZigClangParenType_getInnerType(*const ZigClangParenType) ZigClangQ pub extern fn ZigClangElaboratedType_getNamedType(*const ZigClangElaboratedType) ZigClangQualType; pub extern fn ZigClangAttributedType_getEquivalentType(*const ZigClangAttributedType) ZigClangQualType; + +pub extern fn ZigClangCStyleCastExpr_getBeginLoc(*const ZigClangCStyleCastExpr) ZigClangSourceLocation; +pub extern fn ZigClangCStyleCastExpr_getSubExpr(*const ZigClangCStyleCastExpr) *const ZigClangExpr; +pub extern fn ZigClangCStyleCastExpr_getType(*const ZigClangCStyleCastExpr) ZigClangQualType; diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index a7818de67c..f9f5d52a5f 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -329,9 +329,10 @@ fn transStmt( const sc = ZigClangStmt_getStmtClass(stmt); switch (sc) { .CompoundStmtClass => return transCompoundStmt(rp, scope, @ptrCast(*const ZigClangCompoundStmt, stmt)), + .CStyleCastExprClass => return transCStyleCastExprClass(rp, scope, @ptrCast(*const ZigClangCStyleCastExpr, stmt), result_used, lrvalue), .DeclStmtClass => return transDeclStmt(rp, scope, @ptrCast(*const ZigClangDeclStmt, stmt)), .DeclRefExprClass => return transDeclRefExpr(rp, scope, @ptrCast(*const ZigClangDeclRefExpr, stmt), lrvalue), - .ImplicitCastExprClass => return transImplicitCastExpr(rp, scope, @ptrCast(*const ZigClangImplicitCastExpr, stmt)), + .ImplicitCastExprClass => return transImplicitCastExpr(rp, scope, @ptrCast(*const ZigClangImplicitCastExpr, stmt), result_used), else => { return revertAndWarn( rp, @@ -377,6 +378,30 @@ fn transCompoundStmt(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangCompo }; } +fn transCStyleCastExprClass( + rp: RestorePoint, + scope: *Scope, + stmt: *const ZigClangCStyleCastExpr, + result_used: ResultUsed, + lrvalue: LRValue, +) !TransResult { + const sub_expr = ZigClangCStyleCastExpr_getSubExpr(stmt); + const cast_node = (try transCCast( + rp, + scope, + ZigClangCStyleCastExpr_getBeginLoc(stmt), + ZigClangCStyleCastExpr_getType(stmt), + ZigClangExpr_getType(sub_expr), + (try transExpr(rp, scope, sub_expr, .used, lrvalue)).node, + )); + const cast_res = TransResult{ + .node = cast_node, + .child_scope = scope, + .node_scope = scope, + }; + return maybeSuppressResult(rp, scope, result_used, cast_res); +} + fn transDeclStmt(rp: RestorePoint, parent_scope: *Scope, stmt: *const ZigClangDeclStmt) !TransResult { const c = rp.c; const block_scope = findBlockScope(parent_scope); @@ -478,27 +503,23 @@ fn transImplicitCastExpr( rp: RestorePoint, scope: *Scope, expr: *const ZigClangImplicitCastExpr, + result_used: ResultUsed, ) !TransResult { const c = rp.c; const sub_expr = ZigClangImplicitCastExpr_getSubExpr(expr); + const sub_expr_node = try transExpr(rp, scope, @ptrCast(*const ZigClangExpr, sub_expr), .used, .r_value); switch (ZigClangImplicitCastExpr_getCastKind(expr)) { .BitCast => { - const node = try transExpr(rp, scope, @ptrCast(*const ZigClangExpr, sub_expr), .used, .r_value); const dest_type = getExprQualType(c, @ptrCast(*const ZigClangExpr, expr)); const src_type = getExprQualType(c, sub_expr); return TransResult{ - .node = try transCCast(rp, scope, ZigClangImplicitCastExpr_getBeginLoc(expr), dest_type, src_type, node.node), + .node = try transCCast(rp, scope, ZigClangImplicitCastExpr_getBeginLoc(expr), dest_type, src_type, sub_expr_node.node), .node_scope = scope, .child_scope = scope, }; }, .FunctionToPointerDecay, .ArrayToPointerDecay => { - return maybeSuppressResult( - rp, - scope, - .used, - try transExpr(rp, scope, @ptrCast(*const ZigClangExpr, sub_expr), .used, .r_value), - ); + return maybeSuppressResult(rp, scope, result_used, sub_expr_node); }, else => |kind| return revertAndWarn( rp, diff --git a/src/zig_clang.cpp b/src/zig_clang.cpp index a4c5dfe058..6283f7b67d 100644 --- a/src/zig_clang.cpp +++ b/src/zig_clang.cpp @@ -1964,3 +1964,18 @@ struct ZigClangQualType ZigClangElaboratedType_getNamedType(const struct ZigClan auto casted = reinterpret_cast(self); return bitcast(casted->getNamedType()); } + +struct ZigClangSourceLocation ZigClangCStyleCastExpr_getBeginLoc(const struct ZigClangCStyleCastExpr *self) { + auto casted = reinterpret_cast(self); + return bitcast(casted->getBeginLoc()); +} + +const struct ZigClangExpr *ZigClangCStyleCastExpr_getSubExpr(const struct ZigClangCStyleCastExpr *self) { + auto casted = reinterpret_cast(self); + return reinterpret_cast(casted->getSubExpr()); +} + +struct ZigClangQualType ZigClangCStyleCastExpr_getType(const struct ZigClangCStyleCastExpr *self) { + auto casted = reinterpret_cast(self); + return bitcast(casted->getType()); +} diff --git a/src/zig_clang.h b/src/zig_clang.h index 21cc25eb24..bfaa5867a4 100644 --- a/src/zig_clang.h +++ b/src/zig_clang.h @@ -885,4 +885,8 @@ ZIG_EXTERN_C struct ZigClangQualType ZigClangAttributedType_getEquivalentType(co ZIG_EXTERN_C struct ZigClangQualType ZigClangElaboratedType_getNamedType(const struct ZigClangElaboratedType *); +ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangCStyleCastExpr_getBeginLoc(const struct ZigClangCStyleCastExpr *); +ZIG_EXTERN_C const struct ZigClangExpr *ZigClangCStyleCastExpr_getSubExpr(const struct ZigClangCStyleCastExpr *); +ZIG_EXTERN_C struct ZigClangQualType ZigClangCStyleCastExpr_getType(const struct ZigClangCStyleCastExpr *); + #endif