| author | |
| committer | |
| log | 4ae95d7ffc6ce6b011d5e5c6ff747cd3a1aa59bb |
| tree | ae5f51402078cf034267cf7b7d27a802228f5ea8 |
| parent | 6325ffc3f1e5f61caab6d554a760c1b5d061b0d4 |
| signature | Commit is signed but in an unrecognized format. |
4 files changed, 128 insertions(+), 0 deletions(-)
src-self-hosted/clang.zig+5| ... | ... | @@ -960,3 +960,8 @@ pub extern fn ZigClangIntegerLiteral_EvaluateAsInt(*const ZigClangIntegerLiteral |
| 960 | 960 | pub extern fn ZigClangIntegerLiteral_getBeginLoc(*const ZigClangIntegerLiteral) ZigClangSourceLocation; |
| 961 | 961 | |
| 962 | 962 | pub extern fn ZigClangReturnStmt_getRetValue(*const ZigClangReturnStmt) ?*const ZigClangExpr; |
| 963 | ||
| 964 | pub extern fn ZigClangBinaryOperator_getOpcode(*const ZigClangBinaryOperator) ZigClangBO; | |
| 965 | pub extern fn ZigClangBinaryOperator_getBeginLoc(*const ZigClangBinaryOperator) ZigClangSourceLocation; | |
| 966 | pub extern fn ZigClangBinaryOperator_getLHS(*const ZigClangBinaryOperator) *const ZigClangExpr; | |
| 967 | pub extern fn ZigClangBinaryOperator_getRHS(*const ZigClangBinaryOperator) *const ZigClangExpr; |
src-self-hosted/translate_c.zig+98| ... | ... | @@ -328,6 +328,7 @@ fn transStmt( |
| 328 | 328 | ) !TransResult { |
| 329 | 329 | const sc = ZigClangStmt_getStmtClass(stmt); |
| 330 | 330 | switch (sc) { |
| 331 | .BinaryOperatorClass => return transBinaryOperator(rp, scope, @ptrCast(*const ZigClangBinaryOperator, stmt), result_used), | |
| 331 | 332 | .CompoundStmtClass => return transCompoundStmt(rp, scope, @ptrCast(*const ZigClangCompoundStmt, stmt)), |
| 332 | 333 | .CStyleCastExprClass => return transCStyleCastExprClass(rp, scope, @ptrCast(*const ZigClangCStyleCastExpr, stmt), result_used, lrvalue), |
| 333 | 334 | .DeclStmtClass => return transDeclStmt(rp, scope, @ptrCast(*const ZigClangDeclStmt, stmt)), |
| ... | ... | @@ -347,6 +348,66 @@ fn transStmt( |
| 347 | 348 | } |
| 348 | 349 | } |
| 349 | 350 | |
| 351 | fn transBinaryOperator( | |
| 352 | rp: RestorePoint, | |
| 353 | scope: *Scope, | |
| 354 | stmt: *const ZigClangBinaryOperator, | |
| 355 | result_used: ResultUsed, | |
| 356 | ) TransError!TransResult { | |
| 357 | const op = ZigClangBinaryOperator_getOpcode(stmt); | |
| 358 | switch (op) { | |
| 359 | .PtrMemD, .PtrMemI, .Cmp => return revertAndWarn( | |
| 360 | rp, | |
| 361 | error.UnsupportedTranslation, | |
| 362 | ZigClangBinaryOperator_getBeginLoc(stmt), | |
| 363 | "TODO: handle more C binary operators: {}", | |
| 364 | op, | |
| 365 | ), | |
| 366 | .Assign => return TransResult{ | |
| 367 | .node = &(try transCreateNodeAssign(rp, scope, result_used, ZigClangBinaryOperator_getLHS(stmt), ZigClangBinaryOperator_getRHS(stmt))).base, | |
| 368 | .child_scope = scope, | |
| 369 | .node_scope = scope, | |
| 370 | }, | |
| 371 | .Mul, | |
| 372 | .Div, | |
| 373 | .Rem, | |
| 374 | .Sub, | |
| 375 | .Add, | |
| 376 | .Shl, | |
| 377 | .Shr, | |
| 378 | .LT, | |
| 379 | .GT, | |
| 380 | .LE, | |
| 381 | .GE, | |
| 382 | .EQ, | |
| 383 | .NE, | |
| 384 | .And, | |
| 385 | .Xor, | |
| 386 | .Or, | |
| 387 | .LAnd, | |
| 388 | .LOr, | |
| 389 | .Comma, | |
| 390 | => return revertAndWarn( | |
| 391 | rp, | |
| 392 | error.UnsupportedTranslation, | |
| 393 | ZigClangBinaryOperator_getBeginLoc(stmt), | |
| 394 | "TODO: handle more C binary operators: {}", | |
| 395 | op, | |
| 396 | ), | |
| 397 | .MulAssign, | |
| 398 | .DivAssign, | |
| 399 | .RemAssign, | |
| 400 | .AddAssign, | |
| 401 | .SubAssign, | |
| 402 | .ShlAssign, | |
| 403 | .ShrAssign, | |
| 404 | .AndAssign, | |
| 405 | .XorAssign, | |
| 406 | .OrAssign, | |
| 407 | => unreachable, | |
| 408 | } | |
| 409 | } | |
| 410 | ||
| 350 | 411 | fn transCompoundStmtInline( |
| 351 | 412 | rp: RestorePoint, |
| 352 | 413 | parent_scope: *Scope, |
| ... | ... | @@ -796,6 +857,43 @@ fn cIsUnsignedInteger(qt: ZigClangQualType) bool { |
| 796 | 857 | }; |
| 797 | 858 | } |
| 798 | 859 | |
| 860 | fn transCreateNodeAssign( | |
| 861 | rp: RestorePoint, | |
| 862 | scope: *Scope, | |
| 863 | result_used: ResultUsed, | |
| 864 | lhs: *const ZigClangExpr, | |
| 865 | rhs: *const ZigClangExpr, | |
| 866 | ) !*ast.Node.InfixOp { | |
| 867 | // common case | |
| 868 | // c: lhs = rhs | |
| 869 | // zig: lhs = rhs | |
| 870 | if (result_used == .unused) { | |
| 871 | const lhs_node = try transExpr(rp, scope, lhs, .used, .l_value); | |
| 872 | const eq_token = try appendToken(rp.c, .Equal, "="); | |
| 873 | const rhs_node = try transExpr(rp, scope, rhs, .used, .r_value); | |
| 874 | _ = try appendToken(rp.c, .Semicolon, ";"); | |
| 875 | ||
| 876 | const node = try rp.c.a().create(ast.Node.InfixOp); | |
| 877 | node.* = ast.Node.InfixOp{ | |
| 878 | .base = ast.Node{ .id = .InfixOp }, | |
| 879 | .op_token = eq_token, | |
| 880 | .lhs = lhs_node.node, | |
| 881 | .op = .Assign, | |
| 882 | .rhs = rhs_node.node, | |
| 883 | }; | |
| 884 | return node; | |
| 885 | } | |
| 886 | ||
| 887 | // worst case | |
| 888 | // c: lhs = rhs | |
| 889 | // zig: (x: { | |
| 890 | // zig: const _tmp = rhs; | |
| 891 | // zig: lhs = _tmp; | |
| 892 | // zig: break :x _tmp | |
| 893 | // zig: }) | |
| 894 | return revertAndWarn(rp, error.UnsupportedTranslation, ZigClangExpr_getBeginLoc(lhs), "TODO: worst case assign op expr"); | |
| 895 | } | |
| 896 | ||
| 799 | 897 | fn transCreateNodeBuiltinFnCall(c: *Context, name: []const u8) !*ast.Node.BuiltinCall { |
| 800 | 898 | const builtin_token = try appendToken(c, .Builtin, name); |
| 801 | 899 | _ = try appendToken(c, .LParen, "("); |
src/zig_clang.cpp+20| ... | ... | @@ -2022,3 +2022,23 @@ const struct ZigClangExpr *ZigClangReturnStmt_getRetValue(const struct ZigClangR |
| 2022 | 2022 | auto casted = reinterpret_cast<const clang::ReturnStmt *>(self); |
| 2023 | 2023 | return reinterpret_cast<const struct ZigClangExpr *>(casted->getRetValue()); |
| 2024 | 2024 | } |
| 2025 | ||
| 2026 | enum ZigClangBO ZigClangBinaryOperator_getOpcode(const struct ZigClangBinaryOperator *self) { | |
| 2027 | auto casted = reinterpret_cast<const clang::BinaryOperator *>(self); | |
| 2028 | return (ZigClangBO)casted->getOpcode(); | |
| 2029 | } | |
| 2030 | ||
| 2031 | struct ZigClangSourceLocation ZigClangBinaryOperator_getBeginLoc(const struct ZigClangBinaryOperator *self) { | |
| 2032 | auto casted = reinterpret_cast<const clang::BinaryOperator *>(self); | |
| 2033 | return bitcast(casted->getBeginLoc()); | |
| 2034 | } | |
| 2035 | ||
| 2036 | const struct ZigClangExpr *ZigClangBinaryOperator_getLHS(const struct ZigClangBinaryOperator *self) { | |
| 2037 | auto casted = reinterpret_cast<const clang::BinaryOperator *>(self); | |
| 2038 | return reinterpret_cast<const struct ZigClangExpr *>(casted->getLHS()); | |
| 2039 | } | |
| 2040 | ||
| 2041 | const struct ZigClangExpr *ZigClangBinaryOperator_getRHS(const struct ZigClangBinaryOperator *self) { | |
| 2042 | auto casted = reinterpret_cast<const clang::BinaryOperator *>(self); | |
| 2043 | return reinterpret_cast<const struct ZigClangExpr *>(casted->getRHS()); | |
| 2044 | } |
src/zig_clang.h+5| ... | ... | @@ -921,4 +921,9 @@ ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangIntegerLiteral_getBeginLoc(co |
| 921 | 921 | |
| 922 | 922 | ZIG_EXTERN_C const struct ZigClangExpr *ZigClangReturnStmt_getRetValue(const struct ZigClangReturnStmt *); |
| 923 | 923 | |
| 924 | ZIG_EXTERN_C enum ZigClangBO ZigClangBinaryOperator_getOpcode(const struct ZigClangBinaryOperator *); | |
| 925 | ZIG_EXTERN_C struct ZigClangSourceLocation ZigClangBinaryOperator_getBeginLoc(const struct ZigClangBinaryOperator *); | |
| 926 | ZIG_EXTERN_C const struct ZigClangExpr *ZigClangBinaryOperator_getLHS(const struct ZigClangBinaryOperator *); | |
| 927 | ZIG_EXTERN_C const struct ZigClangExpr *ZigClangBinaryOperator_getRHS(const struct ZigClangBinaryOperator *); | |
| 928 | ||
| 924 | 929 | #endif |