authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-26 15:58:49-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-26 15:58:49-05:00
logaa2ca3f02c11c133964d793847c925e3bd131b27
treeff4c1249197827143be19b9d369f16330e930c23
parent1b0e90f70b4dc26c2ba96b7b5709a3ff269bb48a

translate-c: better way to translate switch

previously `continue` would be handled incorrectly

2 files changed, 42 insertions(+), 25 deletions(-)

src/translate_c.cpp+38-22
......@@ -82,6 +82,7 @@ struct TransScopeSwitch {
8282 AstNode *switch_node;
8383 uint32_t case_index;
8484 bool found_default;
85 Buf *end_label_name;
8586};
8687
8788struct TransScopeVar {
......@@ -248,6 +249,18 @@ static AstNode *trans_create_node_addr_of(Context *c, bool is_const, bool is_vol
248249 return node;
249250}
250251
252static AstNode *trans_create_node_goto(Context *c, Buf *label_name) {
253 AstNode *goto_node = trans_create_node(c, NodeTypeGoto);
254 goto_node->data.goto_expr.name = label_name;
255 return goto_node;
256}
257
258static AstNode *trans_create_node_label(Context *c, Buf *label_name) {
259 AstNode *label_node = trans_create_node(c, NodeTypeLabel);
260 label_node->data.label.name = label_name;
261 return label_node;
262}
263
251264static AstNode *trans_create_node_bool(Context *c, bool value) {
252265 AstNode *bool_node = trans_create_node(c, NodeTypeBoolLiteral);
253266 bool_node->data.bool_literal.value = value;
......@@ -2283,11 +2296,7 @@ static AstNode *trans_do_loop(Context *c, TransScope *parent_scope, const DoStmt
22832296}
22842297
22852298static AstNode *trans_switch_stmt(Context *c, TransScope *parent_scope, const SwitchStmt *stmt) {
2286 TransScopeWhile *while_scope = trans_scope_while_create(c, parent_scope);
2287 while_scope->node->data.while_expr.condition = trans_create_node_bool(c, true);
2288
2289 TransScopeBlock *block_scope = trans_scope_block_create(c, &while_scope->base);
2290 while_scope->node->data.while_expr.body = block_scope->node;
2299 TransScopeBlock *block_scope = trans_scope_block_create(c, parent_scope);
22912300
22922301 TransScopeSwitch *switch_scope;
22932302
......@@ -2305,6 +2314,10 @@ static AstNode *trans_switch_stmt(Context *c, TransScope *parent_scope, const Sw
23052314 }
23062315 block_scope->node->data.block.statements.append(switch_scope->switch_node);
23072316
2317 // TODO avoid name collisions
2318 Buf *end_label_name = buf_create_from_str("end");
2319 switch_scope->end_label_name = end_label_name;
2320
23082321 const Expr *cond_expr = stmt->getCond();
23092322 assert(cond_expr != nullptr);
23102323
......@@ -2336,9 +2349,11 @@ static AstNode *trans_switch_stmt(Context *c, TransScope *parent_scope, const Sw
23362349 }
23372350
23382351 // This is necessary if the last switch case "falls through" the end of the switch block
2339 block_scope->node->data.block.statements.append(trans_create_node(c, NodeTypeBreak));
2352 block_scope->node->data.block.statements.append(trans_create_node_goto(c, end_label_name));
23402353
2341 return while_scope->node;
2354 block_scope->node->data.block.statements.append(trans_create_node_label(c, end_label_name));
2355
2356 return block_scope->node;
23422357}
23432358
23442359static int trans_switch_case(Context *c, TransScope *parent_scope, const CaseStmt *stmt, AstNode **out_node,
......@@ -2365,18 +2380,13 @@ static int trans_switch_case(Context *c, TransScope *parent_scope, const CaseStm
23652380 return ErrorUnexpected;
23662381 prong_node->data.switch_prong.items.append(item_node);
23672382
2368 AstNode *goto_node = trans_create_node(c, NodeTypeGoto);
2369 goto_node->data.goto_expr.name = label_name;
2370 prong_node->data.switch_prong.expr = goto_node;
2383 prong_node->data.switch_prong.expr = trans_create_node_goto(c, label_name);
23712384
23722385 switch_scope->switch_node->data.switch_expr.prongs.append(prong_node);
23732386 }
23742387
2375 AstNode *label_node = trans_create_node(c, NodeTypeLabel);
2376 label_node->data.label.name = label_name;
2377
23782388 TransScopeBlock *scope_block = trans_scope_block_find(parent_scope);
2379 scope_block->node->data.block.statements.append(label_node);
2389 scope_block->node->data.block.statements.append(trans_create_node_label(c, label_name));
23802390
23812391 AstNode *sub_stmt_node;
23822392 TransScope *new_scope = trans_stmt(c, parent_scope, stmt->getSubStmt(), &sub_stmt_node);
......@@ -2399,23 +2409,19 @@ static int trans_switch_default(Context *c, TransScope *parent_scope, const Defa
23992409
24002410 Buf *label_name = buf_sprintf("default");
24012411
2402 AstNode *label_node = trans_create_node(c, NodeTypeLabel);
2403 label_node->data.label.name = label_name;
2404
24052412 {
24062413 // Add the prong
24072414 AstNode *prong_node = trans_create_node(c, NodeTypeSwitchProng);
24082415
2409 AstNode *goto_node = trans_create_node(c, NodeTypeGoto);
2410 goto_node->data.goto_expr.name = label_name;
2411 prong_node->data.switch_prong.expr = goto_node;
2416 prong_node->data.switch_prong.expr = trans_create_node_goto(c, label_name);
24122417
24132418 switch_scope->switch_node->data.switch_expr.prongs.append(prong_node);
24142419 switch_scope->found_default = true;
24152420 }
24162421
24172422 TransScopeBlock *scope_block = trans_scope_block_find(parent_scope);
2418 scope_block->node->data.block.statements.append(label_node);
2423 scope_block->node->data.block.statements.append(trans_create_node_label(c, label_name));
2424
24192425
24202426 AstNode *sub_stmt_node;
24212427 TransScope *new_scope = trans_stmt(c, parent_scope, stmt->getSubStmt(), &sub_stmt_node);
......@@ -2500,7 +2506,17 @@ static AstNode *trans_string_literal(Context *c, TransScope *scope, const String
25002506}
25012507
25022508static AstNode *trans_break_stmt(Context *c, TransScope *scope, const BreakStmt *stmt) {
2503 return trans_create_node(c, NodeTypeBreak);
2509 TransScope *cur_scope = scope;
2510 while (cur_scope != nullptr) {
2511 if (cur_scope->id == TransScopeIdWhile) {
2512 return trans_create_node(c, NodeTypeBreak);
2513 } else if (cur_scope->id == TransScopeIdSwitch) {
2514 TransScopeSwitch *switch_scope = (TransScopeSwitch *)cur_scope;
2515 return trans_create_node_goto(c, switch_scope->end_label_name);
2516 }
2517 cur_scope = cur_scope->parent;
2518 }
2519 zig_unreachable();
25042520}
25052521
25062522static AstNode *trans_continue_stmt(Context *c, TransScope *scope, const ContinueStmt *stmt) {
test/translate_c.zig+4-3
......@@ -1019,7 +1019,7 @@ pub fn addCases(cases: &tests.TranslateCContext) {
10191019 ,
10201020 \\fn foo(_arg_x: c_int) -> c_int {
10211021 \\ var x = _arg_x;
1022 \\ while (true) {
1022 \\ {
10231023 \\ switch (x) {
10241024 \\ 1 => goto case_0,
10251025 \\ 2 => goto case_1,
......@@ -1030,13 +1030,14 @@ pub fn addCases(cases: &tests.TranslateCContext) {
10301030 \\ case_0:
10311031 \\ x += 1;
10321032 \\ case_1:
1033 \\ break;
1033 \\ goto end;
10341034 \\ case_2:
10351035 \\ case_3:
10361036 \\ return x + 1;
10371037 \\ default:
10381038 \\ return 10;
1039 \\ break;
1039 \\ goto end;
1040 \\ end:
10401041 \\ };
10411042 \\ return x + 13;
10421043 \\}