diff options
| author | hryx <codroid@gmail.com> | 2019-06-10 23:06:54 -0700 |
|---|---|---|
| committer | hryx <codroid@gmail.com> | 2019-06-10 23:06:54 -0700 |
| commit | 0f545e5a2b664d340fe33fe0a8fbc44956485e18 (patch) | |
| tree | d101d647b181a7565e4f1e84be3a052e0b8adfc8 /src-self-hosted | |
| parent | 84e479d94f9998331bdd73665274684cf3d0ad48 (diff) | |
| download | zig-0f545e5a2b664d340fe33fe0a8fbc44956485e18.tar.gz zig-0f545e5a2b664d340fe33fe0a8fbc44956485e18.zip | |
transReturnStmt
Diffstat (limited to 'src-self-hosted')
| -rw-r--r-- | src-self-hosted/clang.zig | 2 | ||||
| -rw-r--r-- | src-self-hosted/translate_c.zig | 31 |
2 files changed, 33 insertions, 0 deletions
diff --git a/src-self-hosted/clang.zig b/src-self-hosted/clang.zig index e640e3eadc..de3c538c4b 100644 --- a/src-self-hosted/clang.zig +++ b/src-self-hosted/clang.zig @@ -958,3 +958,5 @@ pub const ZigClangAPValue_ValueKind = extern enum { pub extern fn ZigClangIntegerLiteral_EvaluateAsInt(*const ZigClangIntegerLiteral, *ZigClangExprEvalResult, *const ZigClangASTContext) bool; pub extern fn ZigClangIntegerLiteral_getBeginLoc(*const ZigClangIntegerLiteral) ZigClangSourceLocation; + +pub extern fn ZigClangReturnStmt_getRetValue(*const ZigClangReturnStmt) ?*const ZigClangExpr; diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index 7e05df4f56..7de834b4f0 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -334,6 +334,7 @@ fn transStmt( .DeclRefExprClass => return transDeclRefExpr(rp, scope, @ptrCast(*const ZigClangDeclRefExpr, stmt), lrvalue), .ImplicitCastExprClass => return transImplicitCastExpr(rp, scope, @ptrCast(*const ZigClangImplicitCastExpr, stmt), result_used), .IntegerLiteralClass => return transIntegerLiteral(rp, scope, @ptrCast(*const ZigClangIntegerLiteral, stmt), result_used), + .ReturnStmtClass => return transReturnStmt(rp, scope, @ptrCast(*const ZigClangReturnStmt, stmt)), else => { return revertAndWarn( rp, @@ -555,6 +556,24 @@ fn transIntegerLiteral( return maybeSuppressResult(rp, scope, result_used, res); } +fn transReturnStmt( + rp: RestorePoint, + scope: *Scope, + expr: *const ZigClangReturnStmt, +) !TransResult { + const node = try transCreateNodeReturnExpr(rp.c); + if (ZigClangReturnStmt_getRetValue(expr)) |val_expr| { + const ret_node = node.cast(ast.Node.ControlFlowExpression).?; + ret_node.rhs = (try transExpr(rp, scope, val_expr, .used, .r_value)).node; + } + _ = try appendToken(rp.c, .Semicolon, ";"); + return TransResult{ + .node = node, + .child_scope = scope, + .node_scope = scope, + }; +} + fn transCCast( rp: RestorePoint, scope: *Scope, @@ -848,6 +867,18 @@ fn transCreateNodeAPInt(c: *Context, int: ?*const ZigClangAPSInt) !*ast.Node { return &node.base; } +fn transCreateNodeReturnExpr(c: *Context) !*ast.Node { + const ltoken = try appendToken(c, .Keyword_return, "return"); + const node = try c.a().create(ast.Node.ControlFlowExpression); + node.* = ast.Node.ControlFlowExpression{ + .base = ast.Node{ .id = .ControlFlowExpression }, + .ltoken = ltoken, + .kind = .Return, + .rhs = null, + }; + return &node.base; +} + const RestorePoint = struct { c: *Context, token_index: ast.TokenIndex, |
