@@ -1950,19 +1950,29 @@ gb_internal void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss
19501950 GB_PANIC (" Unknown switch kind" );
19511951 }
19521952
1953+ if (tag.value != nullptr ) {
1954+ String tag_name = str_lit (" typeswitch.tag" );
1955+ LLVMSetValueName2 (tag.value , cast (char const *)tag_name.text , tag_name.len );
1956+ }
1957+
19531958 ast_node (body, BlockStmt, ss->body );
19541959
19551960 lbBlock *done = lb_create_block (p, " typeswitch.done" );
19561961 lbBlock *else_block = done;
19571962 lbBlock *default_block = nullptr ;
19581963 isize num_cases = 0 ;
19591964
1960- for (Ast *clause : body->stmts ) {
1965+ auto body_blocks = slice_make<lbBlock *>(permanent_allocator (), body->stmts .count );
1966+ for_array (i, body->stmts ) {
1967+ Ast *clause = body->stmts [i];
19611968 ast_node (cc, CaseClause, clause);
19621969 num_cases += cc->list .count ;
1970+
1971+ body_blocks[i] = lb_create_block (p, cc->list .count == 0 ? " typeswitch.default.body" : " typeswitch.case.body" );
1972+
19631973 if (cc->list .count == 0 ) {
19641974 GB_ASSERT (default_block == nullptr );
1965- default_block = lb_create_block (p, " typeswitch.default.body " ) ;
1975+ default_block = body_blocks[i] ;
19661976 else_block = default_block;
19671977 }
19681978 }
@@ -2030,7 +2040,8 @@ gb_internal void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss
20302040
20312041 Ast *default_clause = nullptr ;
20322042
2033- for (Ast *clause : body->stmts ) {
2043+ for_array (i, body->stmts ) {
2044+ Ast *clause = body->stmts [i];
20342045 ast_node (cc, CaseClause, clause);
20352046
20362047 Entity *case_entity = implicit_entity_of_node (clause);
@@ -2053,14 +2064,15 @@ gb_internal void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss
20532064
20542065 lb_open_scope (p, cc->scope );
20552066
2056- lbBlock *body = lb_create_block (p, " typeswitch.body " ) ;
2067+ lbBlock *body = body_blocks[i] ;
20572068 if (p->debug_info != nullptr ) {
20582069 LLVMSetCurrentDebugLocation2 (p->builder , lb_debug_location_from_ast (p, clause));
20592070 }
20602071
20612072 lbBlock *next_cond = nullptr ;
20622073 bool saw_nil = false ;
2063- for (Ast *type_expr : cc->list ) {
2074+ for_array (i, cc->list ) {
2075+ Ast *type_expr = cc->list [i];
20642076 Type *case_type = type_of_expr (type_expr);
20652077 lbValue on_val = {};
20662078 if (switch_kind == TypeSwitch_Union) {
@@ -2079,13 +2091,14 @@ gb_internal void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss
20792091
20802092 if (switch_instr != nullptr ) {
20812093 LLVMAddCase (switch_instr, on_val.value , body->block );
2082- } else {
2083- next_cond = lb_create_block (p, " typeswitch.case.next" );
2084- lbValue cond = lb_emit_comp (p, Token_CmpEq, on_val, tag);
2085-
2086- lb_emit_if (p, cond, body, next_cond);
2087- lb_start_block (p, next_cond);
2094+ continue ;
20882095 }
2096+
2097+ lbValue cond = lb_emit_comp (p, Token_CmpEq, on_val, tag);
2098+
2099+ next_cond = lb_create_block (p, " typeswitch.case.next" );
2100+ lb_emit_if (p, cond, body, next_cond);
2101+ lb_start_block (p, next_cond);
20892102 }
20902103
20912104
@@ -2133,13 +2146,15 @@ gb_internal void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss
21332146
21342147 lb_type_case_body (p, ss->label , clause, body, done);
21352148
2136- if (switch_instr == nullptr ) {
2149+ if (next_cond != nullptr ) {
2150+ GB_ASSERT (switch_instr == nullptr );
21372151 lb_start_block (p, next_cond);
21382152 }
21392153 }
21402154
21412155 if (default_block != nullptr && switch_instr == nullptr ) {
21422156 GB_ASSERT (default_clause != nullptr );
2157+ lb_emit_jump (p, default_block);
21432158
21442159 Entity *case_entity = implicit_entity_of_node (default_clause);
21452160 ast_node (cc, CaseClause, default_clause);
0 commit comments