authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-26 17:30:43-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-26 17:30:43-05:00
log93fac5f257a80c2ca0abd30aedbeae300f6460f8
tree8f80e28ea0f3849981f5e7a781c25f7cac5105e4
parent9a8545d5903ff60de16f0ddab2b9a4c4c1a798a7

translate-c: support variable name shadowing


2 files changed, 39 insertions(+), 5 deletions(-)

src/translate_c.cpp+19-5
...@@ -157,6 +157,19 @@ static void add_global_weak_alias(Context *c, Buf *new_name, Buf *canon_name) {...@@ -157,6 +157,19 @@ static void add_global_weak_alias(Context *c, Buf *new_name, Buf *canon_name) {
157 alias->canon_name = canon_name;157 alias->canon_name = canon_name;
158}158}
159159
160static Buf *trans_lookup_zig_symbol(Context *c, TransScope *scope, Buf *c_symbol_name) {
161 while (scope != nullptr) {
162 if (scope->id == TransScopeIdVar) {
163 TransScopeVar *var_scope = (TransScopeVar *)scope;
164 if (buf_eql_buf(var_scope->c_name, c_symbol_name)) {
165 return var_scope->zig_name;
166 }
167 }
168 scope = scope->parent;
169 }
170 return c_symbol_name;
171}
172
160static AstNode * trans_create_node(Context *c, NodeType id) {173static AstNode * trans_create_node(Context *c, NodeType id) {
161 AstNode *node = allocate<AstNode>(1);174 AstNode *node = allocate<AstNode>(1);
162 node->type = id;175 node->type = id;
...@@ -1650,13 +1663,14 @@ static AstNode *trans_implicit_cast_expr(Context *c, TransScope *scope, const Im...@@ -1650,13 +1663,14 @@ static AstNode *trans_implicit_cast_expr(Context *c, TransScope *scope, const Im
1650 zig_unreachable();1663 zig_unreachable();
1651}1664}
16521665
1653static AstNode *trans_decl_ref_expr(Context *c, const DeclRefExpr *stmt, TransLRValue lrval) {1666static AstNode *trans_decl_ref_expr(Context *c, TransScope *scope, const DeclRefExpr *stmt, TransLRValue lrval) {
1654 const ValueDecl *value_decl = stmt->getDecl();1667 const ValueDecl *value_decl = stmt->getDecl();
1655 Buf *symbol_name = buf_create_from_str(decl_name(value_decl));1668 Buf *c_symbol_name = buf_create_from_str(decl_name(value_decl));
1669 Buf *zig_symbol_name = trans_lookup_zig_symbol(c, scope, c_symbol_name);
1656 if (lrval == TransLValue) {1670 if (lrval == TransLValue) {
1657 c->ptr_params.put(symbol_name, true);1671 c->ptr_params.put(zig_symbol_name, true);
1658 }1672 }
1659 return trans_create_node_symbol(c, symbol_name);1673 return trans_create_node_symbol(c, zig_symbol_name);
1660}1674}
16611675
1662static AstNode *trans_create_post_crement(Context *c, ResultUsed result_used, TransScope *scope,1676static AstNode *trans_create_post_crement(Context *c, ResultUsed result_used, TransScope *scope,
...@@ -2562,7 +2576,7 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,...@@ -2562,7 +2576,7 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
2562 trans_implicit_cast_expr(c, scope, (const ImplicitCastExpr *)stmt));2576 trans_implicit_cast_expr(c, scope, (const ImplicitCastExpr *)stmt));
2563 case Stmt::DeclRefExprClass:2577 case Stmt::DeclRefExprClass:
2564 return wrap_stmt(out_node, out_child_scope, scope,2578 return wrap_stmt(out_node, out_child_scope, scope,
2565 trans_decl_ref_expr(c, (const DeclRefExpr *)stmt, lrvalue));2579 trans_decl_ref_expr(c, scope, (const DeclRefExpr *)stmt, lrvalue));
2566 case Stmt::UnaryOperatorClass:2580 case Stmt::UnaryOperatorClass:
2567 return wrap_stmt(out_node, out_child_scope, scope,2581 return wrap_stmt(out_node, out_child_scope, scope,
2568 trans_unary_operator(c, result_used, scope, (const UnaryOperator *)stmt));2582 trans_unary_operator(c, result_used, scope, (const UnaryOperator *)stmt));
test/translate_c.zig+20
...@@ -1080,6 +1080,26 @@ pub fn addCases(cases: &tests.TranslateCContext) {...@@ -1080,6 +1080,26 @@ pub fn addCases(cases: &tests.TranslateCContext) {
1080 \\ return x + 13;1080 \\ return x + 13;
1081 \\}1081 \\}
1082 );1082 );
1083
1084 cases.add("variable name shadowing",
1085 \\int foo(void) {
1086 \\ int x = 1;
1087 \\ {
1088 \\ int x = 2;
1089 \\ x += 1;
1090 \\ }
1091 \\ return x;
1092 \\}
1093 ,
1094 \\pub fn foo() -> c_int {
1095 \\ var x: c_int = 1;
1096 \\ {
1097 \\ var x_0: c_int = 2;
1098 \\ x_0 += 1;
1099 \\ };
1100 \\ return x;
1101 \\}
1102 );
1083}1103}
10841104
10851105