Commit 1f82c7ba22

hryx <codroid@gmail.com>
2019-06-09 00:54:15
transCStyleCastExpr
1 parent 3e14f86
Changed files (4)
src/zig_clang.cpp
@@ -1964,3 +1964,18 @@ struct ZigClangQualType ZigClangElaboratedType_getNamedType(const struct ZigClan
     auto casted = reinterpret_cast<const clang::ElaboratedType *>(self);
     return bitcast(casted->getNamedType());
 }
+
+struct ZigClangSourceLocation ZigClangCStyleCastExpr_getBeginLoc(const struct ZigClangCStyleCastExpr *self) {
+    auto casted = reinterpret_cast<const clang::CStyleCastExpr *>(self);
+    return bitcast(casted->getBeginLoc());
+}
+
+const struct ZigClangExpr *ZigClangCStyleCastExpr_getSubExpr(const struct ZigClangCStyleCastExpr *self) {
+    auto casted = reinterpret_cast<const clang::CStyleCastExpr *>(self);
+    return reinterpret_cast<const struct ZigClangExpr *>(casted->getSubExpr());
+}
+
+struct ZigClangQualType ZigClangCStyleCastExpr_getType(const struct ZigClangCStyleCastExpr *self) {
+    auto casted = reinterpret_cast<const clang::CStyleCastExpr *>(self);
+    return bitcast(casted->getType());
+}
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
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;
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,