| ... | @@ -328,6 +328,7 @@ fn transStmt( | ... | @@ -328,6 +328,7 @@ fn transStmt( |
| 328 | ) !TransResult { | 328 | ) !TransResult { |
| 329 | const sc = ZigClangStmt_getStmtClass(stmt); | 329 | const sc = ZigClangStmt_getStmtClass(stmt); |
| 330 | switch (sc) { | 330 | switch (sc) { |
| | 331 | .BinaryOperatorClass => return transBinaryOperator(rp, scope, @ptrCast(*const ZigClangBinaryOperator, stmt), result_used), |
| 331 | .CompoundStmtClass => return transCompoundStmt(rp, scope, @ptrCast(*const ZigClangCompoundStmt, stmt)), | 332 | .CompoundStmtClass => return transCompoundStmt(rp, scope, @ptrCast(*const ZigClangCompoundStmt, stmt)), |
| 332 | .CStyleCastExprClass => return transCStyleCastExprClass(rp, scope, @ptrCast(*const ZigClangCStyleCastExpr, stmt), result_used, lrvalue), | 333 | .CStyleCastExprClass => return transCStyleCastExprClass(rp, scope, @ptrCast(*const ZigClangCStyleCastExpr, stmt), result_used, lrvalue), |
| 333 | .DeclStmtClass => return transDeclStmt(rp, scope, @ptrCast(*const ZigClangDeclStmt, stmt)), | 334 | .DeclStmtClass => return transDeclStmt(rp, scope, @ptrCast(*const ZigClangDeclStmt, stmt)), |
| ... | @@ -347,6 +348,66 @@ fn transStmt( | ... | @@ -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 | fn transCompoundStmtInline( | 411 | fn transCompoundStmtInline( |
| 351 | rp: RestorePoint, | 412 | rp: RestorePoint, |
| 352 | parent_scope: *Scope, | 413 | parent_scope: *Scope, |
| ... | @@ -796,6 +857,43 @@ fn cIsUnsignedInteger(qt: ZigClangQualType) bool { | ... | @@ -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 | fn transCreateNodeBuiltinFnCall(c: *Context, name: []const u8) !*ast.Node.BuiltinCall { | 897 | fn transCreateNodeBuiltinFnCall(c: *Context, name: []const u8) !*ast.Node.BuiltinCall { |
| 800 | const builtin_token = try appendToken(c, .Builtin, name); | 898 | const builtin_token = try appendToken(c, .Builtin, name); |
| 801 | _ = try appendToken(c, .LParen, "("); | 899 | _ = try appendToken(c, .LParen, "("); |