source: src/ControlStruct/MultiLevelExit.cpp@ de31a1d

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since de31a1d was de31a1d, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Converted the two LabelGenerator singletons into a single pure-static class.

  • Property mode set to 100644
File size: 21.0 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// MultiLevelExit.cpp -- Replaces CFA's local control flow with C's versions.
8//
9// Author : Andrew Beach
10// Created On : Mon Nov 1 13:48:00 2021
11// Last Modified By : Andrew Beach
12// Last Modified On : Mon Nov 5 19:20:00 2021
13// Update Count : 1
14//
15
16#include "MultiLevelExit.hpp"
17
18#include "AST/Pass.hpp"
19#include "AST/Stmt.hpp"
20#include "ControlStruct/LabelGenerator.h"
21
22#include <set>
23
24namespace ControlStruct {
25
26namespace {
27
28class Entry {
29public:
30 const ast::Stmt * stmt;
31private:
32 // Organized like a manual ADT. Avoids creating a bunch of dead data.
33 struct Target {
34 ast::Label label;
35 bool used = false;
36 Target( const ast::Label & label ) : label( label ) {}
37 Target() : label( CodeLocation() ) {}
38 };
39 Target firstTarget;
40 Target secondTarget;
41
42 enum Kind {
43 ForStmt, WhileStmt, CompoundStmt, IfStmt, CaseStmt, SwitchStmt, TryStmt
44 } kind;
45
46 bool fallDefaultValid = true;
47
48 static ast::Label & useTarget( Target & target ) {
49 target.used = true;
50 return target.label;
51 }
52
53public:
54 Entry( const ast::ForStmt * stmt, ast::Label breakExit, ast::Label contExit ) :
55 stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( ForStmt ) {}
56 Entry( const ast::WhileStmt * stmt, ast::Label breakExit, ast::Label contExit ) :
57 stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( WhileStmt ) {}
58 Entry( const ast::CompoundStmt *stmt, ast::Label breakExit ) :
59 stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( CompoundStmt ) {}
60 Entry( const ast::IfStmt *stmt, ast::Label breakExit ) :
61 stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( IfStmt ) {}
62 Entry( const ast::CaseStmt *stmt, ast::Label fallExit ) :
63 stmt( stmt ), firstTarget( fallExit ), secondTarget(), kind( CaseStmt ) {}
64 Entry( const ast::SwitchStmt *stmt, ast::Label breakExit, ast::Label fallDefaultExit ) :
65 stmt( stmt ), firstTarget( breakExit ), secondTarget( fallDefaultExit ), kind( SwitchStmt ) {}
66 Entry( const ast::TryStmt *stmt, ast::Label breakExit ) :
67 stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( TryStmt ) {}
68
69 bool isContTarget() const { return kind <= WhileStmt; }
70 bool isBreakTarget() const { return CaseStmt != kind; }
71 bool isFallTarget() const { return CaseStmt == kind; }
72 bool isFallDefaultTarget() const { return SwitchStmt == kind; }
73
74 ast::Label useContExit() { assert( kind <= WhileStmt ); return useTarget(secondTarget); }
75 ast::Label useBreakExit() { assert( CaseStmt != kind ); return useTarget(firstTarget); }
76 ast::Label useFallExit() { assert( CaseStmt == kind ); return useTarget(firstTarget); }
77 ast::Label useFallDefaultExit() { assert( SwitchStmt == kind ); return useTarget(secondTarget); }
78
79 bool isContUsed() const { assert( kind <= WhileStmt ); return secondTarget.used; }
80 bool isBreakUsed() const { assert( CaseStmt != kind ); return firstTarget.used; }
81 bool isFallUsed() const { assert( CaseStmt == kind ); return firstTarget.used; }
82 bool isFallDefaultUsed() const { assert( SwitchStmt == kind ); return secondTarget.used; }
83 void seenDefault() { fallDefaultValid = false; }
84 bool isFallDefaultValid() const { return fallDefaultValid; }
85};
86
87// Helper predicates used in std::find_if calls (it doesn't take methods):
88bool isBreakTarget( const Entry & entry ) {
89 return entry.isBreakTarget();
90}
91
92bool isContinueTarget( const Entry & entry ) {
93 return entry.isContTarget();
94}
95
96bool isFallthroughTarget( const Entry & entry ) {
97 return entry.isFallTarget();
98}
99
100bool isFallthroughDefaultTarget( const Entry & entry ) {
101 return entry.isFallDefaultTarget();
102}
103
104struct MultiLevelExitCore final :
105 public ast::WithVisitorRef<MultiLevelExitCore>,
106 public ast::WithShortCircuiting, public ast::WithGuards {
107 MultiLevelExitCore( const LabelToStmt & lt, LabelGenerator * lg );
108
109 void previsit( const ast::FunctionDecl * );
110
111 const ast::CompoundStmt * previsit( const ast::CompoundStmt * );
112 const ast::BranchStmt * postvisit( const ast::BranchStmt * );
113 void previsit( const ast::WhileStmt * );
114 const ast::WhileStmt * postvisit( const ast::WhileStmt * );
115 void previsit( const ast::ForStmt * );
116 const ast::ForStmt * postvisit( const ast::ForStmt * );
117 const ast::CaseStmt * previsit( const ast::CaseStmt * );
118 void previsit( const ast::IfStmt * );
119 const ast::IfStmt * postvisit( const ast::IfStmt * );
120 void previsit( const ast::SwitchStmt * );
121 const ast::SwitchStmt * postvisit( const ast::SwitchStmt * );
122 void previsit( const ast::ReturnStmt * );
123 void previsit( const ast::TryStmt * );
124 void postvisit( const ast::TryStmt * );
125 void previsit( const ast::FinallyStmt * );
126
127 const ast::Stmt * mutateLoop( const ast::Stmt * body, Entry& );
128
129 const LabelToStmt & target_table;
130 std::set<ast::Label> fallthrough_labels;
131 std::vector<Entry> enclosing_control_structures;
132 ast::Label break_label;
133 LabelGenerator * label_gen;
134 bool inFinally;
135
136 template<typename LoopNode>
137 void prehandleLoopStmt( const LoopNode * loopStmt );
138 template<typename LoopNode>
139 const LoopNode * posthandleLoopStmt( const LoopNode * loopStmt );
140
141 std::list<ast::ptr<ast::Stmt>> fixBlock(
142 const std::list<ast::ptr<ast::Stmt>> & kids, bool caseClause );
143
144 template<typename UnaryPredicate>
145 auto findEnclosingControlStructure( UnaryPredicate pred ) {
146 return std::find_if( enclosing_control_structures.rbegin(),
147 enclosing_control_structures.rend(), pred );
148 }
149};
150
151ast::NullStmt * labelledNullStmt(
152 const CodeLocation & cl, const ast::Label & label ) {
153 return new ast::NullStmt( cl, std::vector<ast::Label>{ label } );
154}
155
156MultiLevelExitCore::MultiLevelExitCore(
157 const LabelToStmt & lt, LabelGenerator * lg ) :
158 target_table( lt ), break_label( CodeLocation(), "" ), label_gen( lg ),
159 inFinally( false )
160{}
161
162void MultiLevelExitCore::previsit( const ast::FunctionDecl * ) {
163 visit_children = false;
164}
165
166const ast::CompoundStmt * MultiLevelExitCore::previsit(
167 const ast::CompoundStmt * stmt ) {
168 visit_children = false;
169 bool isLabeled = !stmt->labels.empty();
170 if ( isLabeled ) {
171 ast::Label breakLabel = label_gen->newLabel( "blockBreak", stmt );
172 enclosing_control_structures.emplace_back( stmt, breakLabel );
173 GuardAction( [this]() { enclosing_control_structures.pop_back(); } );
174 }
175
176 auto mutStmt = ast::mutate( stmt );
177 // A child statement may set the break label.
178 mutStmt->kids = std::move( fixBlock( stmt->kids, false ) );
179
180 if ( isLabeled ) {
181 assert( !enclosing_control_structures.empty() );
182 Entry & entry = enclosing_control_structures.back();
183 if ( !entry.useBreakExit().empty() ) {
184 break_label = entry.useBreakExit();
185 }
186 }
187 return mutStmt;
188}
189
190size_t getUnusedIndex(
191 const ast::Stmt * stmt, const ast::Label & originalTarget ) {
192 const size_t size = stmt->labels.size();
193
194 // If the label is empty, we can skip adding the unused attribute:
195 if ( originalTarget.empty() ) return size;
196
197 // Search for a label that matches the originalTarget.
198 for ( size_t i = 0 ; i < size ; ++i ) {
199 const ast::Label & label = stmt->labels[i];
200 if ( label == originalTarget ) {
201 for ( const ast::Attribute * attr : label.attributes ) {
202 if ( attr->name == "unused" ) return size;
203 }
204 return i;
205 }
206 }
207 assertf( false, "Could not find label '%s' on statement %s",
208 originalTarget.name.c_str(), toString( stmt ).c_str() );
209}
210
211const ast::Stmt * addUnused(
212 const ast::Stmt * stmt, const ast::Label & originalTarget ) {
213 size_t i = getUnusedIndex( stmt, originalTarget );
214 if ( i == stmt->labels.size() ) {
215 return stmt;
216 }
217 ast::Stmt * mutStmt = ast::mutate( stmt );
218 mutStmt->labels[i].attributes.push_back( new ast::Attribute( "unused" ) );
219 return mutStmt;
220}
221
222const ast::BranchStmt * MultiLevelExitCore::postvisit( const ast::BranchStmt * stmt ) {
223 std::vector<Entry>::reverse_iterator targetEntry =
224 enclosing_control_structures.rend();
225 switch ( stmt->kind ) {
226 case ast::BranchStmt::Goto:
227 return stmt;
228 case ast::BranchStmt::Continue:
229 case ast::BranchStmt::Break: {
230 bool isContinue = stmt->kind == ast::BranchStmt::Continue;
231 // Handle unlabeled break and continue.
232 if ( stmt->target.empty() ) {
233 if ( isContinue ) {
234 targetEntry = findEnclosingControlStructure( isContinueTarget );
235 } else {
236 if ( enclosing_control_structures.empty() ) {
237 SemanticError( stmt->location,
238 "'break' outside a loop, 'switch', or labelled block" );
239 }
240 targetEntry = findEnclosingControlStructure( isBreakTarget );
241 }
242 // Handle labeled break and continue.
243 } else {
244 // Lookup label in table to find attached control structure.
245 targetEntry = findEnclosingControlStructure(
246 [ targetStmt = target_table.at(stmt->target) ](auto entry){
247 return entry.stmt == targetStmt;
248 } );
249 }
250 // Ensure that selected target is valid.
251 if ( targetEntry == enclosing_control_structures.rend() || ( isContinue && !isContinueTarget( *targetEntry ) ) ) {
252 SemanticError(
253 stmt->location,
254 toString( (isContinue ? "'continue'" : "'break'"),
255 " target must be an enclosing ",
256 (isContinue ? "loop: " : "control structure: "),
257 stmt->originalTarget ) );
258 }
259 break;
260 }
261 case ast::BranchStmt::FallThrough: {
262 targetEntry = findEnclosingControlStructure( isFallthroughTarget );
263 // Check that target is valid.
264 if ( targetEntry == enclosing_control_structures.rend() ) {
265 SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" );
266 }
267 if ( !stmt->target.empty() ) {
268 // Labelled fallthrough: target must be a valid fallthough label.
269 if ( !fallthrough_labels.count( stmt->target ) ) {
270 SemanticError( stmt->location, toString( "'fallthrough' target must be a later case statement: ", stmt->originalTarget ) );
271 }
272 return new ast::BranchStmt(
273 stmt->location, ast::BranchStmt::Goto, stmt->originalTarget );
274 }
275 break;
276 }
277 case ast::BranchStmt::FallThroughDefault: {
278 targetEntry = findEnclosingControlStructure( isFallthroughDefaultTarget );
279
280 // Check that this is in a switch or choose statement.
281 if ( targetEntry == enclosing_control_structures.rend() ) {
282 SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" );
283 }
284
285 // Check that the switch or choose has a default clause.
286 auto switchStmt = strict_dynamic_cast< const ast::SwitchStmt * >(
287 targetEntry->stmt );
288 bool foundDefault = false;
289 for ( auto subStmt : switchStmt->stmts ) {
290 const ast::CaseStmt * caseStmt = subStmt.strict_as<ast::CaseStmt>();
291 if ( caseStmt->isDefault() ) {
292 foundDefault = true;
293 break;
294 }
295 }
296 if ( !foundDefault ) {
297 SemanticError( stmt->location, "'fallthrough default' must be enclosed in a 'switch' or 'choose' control structure with a 'default' clause" );
298 }
299 break;
300 }
301 default:
302 assert( false );
303 }
304
305 // Branch error checks: get the appropriate label name:
306 // (This label will always be replaced.)
307 ast::Label exitLabel( CodeLocation(), "" );
308 switch ( stmt->kind ) {
309 case ast::BranchStmt::Break:
310 assert( !targetEntry->useBreakExit().empty() );
311 exitLabel = targetEntry->useBreakExit();
312 break;
313 case ast::BranchStmt::Continue:
314 assert( !targetEntry->useContExit().empty() );
315 exitLabel = targetEntry->useContExit();
316 break;
317 case ast::BranchStmt::FallThrough:
318 assert( !targetEntry->useFallExit().empty() );
319 exitLabel = targetEntry->useFallExit();
320 break;
321 case ast::BranchStmt::FallThroughDefault:
322 assert( !targetEntry->useFallDefaultExit().empty() );
323 exitLabel = targetEntry->useFallDefaultExit();
324 // Check that fallthrough default comes before the default clause.
325 if ( !targetEntry->isFallDefaultValid() ) {
326 SemanticError( stmt->location,
327 "'fallthrough default' must precede the 'default' clause" );
328 }
329 break;
330 default:
331 assert(0);
332 }
333
334 // Add unused attribute to silence warnings.
335 targetEntry->stmt = addUnused( targetEntry->stmt, stmt->originalTarget );
336
337 // Replace this with a goto to make later passes more uniform.
338 return new ast::BranchStmt( stmt->location, ast::BranchStmt::Goto, exitLabel );
339}
340
341void MultiLevelExitCore::previsit( const ast::WhileStmt * stmt ) {
342 return prehandleLoopStmt( stmt );
343}
344
345const ast::WhileStmt * MultiLevelExitCore::postvisit( const ast::WhileStmt * stmt ) {
346 return posthandleLoopStmt( stmt );
347}
348
349void MultiLevelExitCore::previsit( const ast::ForStmt * stmt ) {
350 return prehandleLoopStmt( stmt );
351}
352
353const ast::ForStmt * MultiLevelExitCore::postvisit( const ast::ForStmt * stmt ) {
354 return posthandleLoopStmt( stmt );
355}
356
357// Mimic what the built-in push_front would do anyways. It is O(n).
358void push_front(
359 std::vector<ast::ptr<ast::Stmt>> & vec, const ast::Stmt * element ) {
360 vec.emplace_back( nullptr );
361 for ( size_t i = vec.size() - 1 ; 0 < i ; --i ) {
362 vec[ i ] = std::move( vec[ i - 1 ] );
363 }
364 vec[ 0 ] = element;
365}
366
367const ast::CaseStmt * MultiLevelExitCore::previsit( const ast::CaseStmt * stmt ) {
368 visit_children = false;
369
370 // If it is the default, mark the default as seen.
371 if ( stmt->isDefault() ) {
372 assert( !enclosing_control_structures.empty() );
373 enclosing_control_structures.back().seenDefault();
374 }
375
376 // The cond may not exist, but if it does update it now.
377 visitor->maybe_accept( stmt, &ast::CaseStmt::cond );
378
379 // Just save the mutated node for simplicity.
380 ast::CaseStmt * mutStmt = ast::mutate( stmt );
381
382 ast::Label fallLabel = label_gen->newLabel( "fallThrough", stmt );
383 if ( !mutStmt->stmts.empty() ) {
384 // Ensure that the stack isn't corrupted by exceptions in fixBlock.
385 auto guard = makeFuncGuard(
386 [&](){ enclosing_control_structures.emplace_back( mutStmt, fallLabel ); },
387 [this](){ enclosing_control_structures.pop_back(); }
388 );
389
390 // These should already be in a block.
391 auto block = ast::mutate( mutStmt->stmts.front().strict_as<ast::CompoundStmt>() );
392 block->kids = fixBlock( block->kids, true );
393
394 // Add fallthrough label if necessary.
395 assert( !enclosing_control_structures.empty() );
396 Entry & entry = enclosing_control_structures.back();
397 if ( entry.isFallUsed() ) {
398 mutStmt->stmts.push_back(
399 labelledNullStmt( mutStmt->location, entry.useFallExit() ) );
400 }
401 }
402 assert( !enclosing_control_structures.empty() );
403 Entry & entry = enclosing_control_structures.back();
404 assertf( dynamic_cast< const ast::SwitchStmt * >( entry.stmt ),
405 "Control structure enclosing a case clause must be a switch, but is: %s",
406 toString( entry.stmt ).c_str() );
407 if ( mutStmt->isDefault() ) {
408 if ( entry.isFallDefaultUsed() ) {
409 // Add fallthrough default label if necessary.
410 push_front( mutStmt->stmts, labelledNullStmt(
411 stmt->location, entry.useFallDefaultExit()
412 ) );
413 }
414 }
415 return mutStmt;
416}
417
418void MultiLevelExitCore::previsit( const ast::IfStmt * stmt ) {
419 bool labeledBlock = !stmt->labels.empty();
420 if ( labeledBlock ) {
421 ast::Label breakLabel = label_gen->newLabel( "blockBreak", stmt );
422 enclosing_control_structures.emplace_back( stmt, breakLabel );
423 GuardAction( [this](){ enclosing_control_structures.pop_back(); } );
424 }
425}
426
427const ast::IfStmt * MultiLevelExitCore::postvisit( const ast::IfStmt * stmt ) {
428 bool labeledBlock = !stmt->labels.empty();
429 if ( labeledBlock ) {
430 auto this_label = enclosing_control_structures.back().useBreakExit();
431 if ( !this_label.empty() ) {
432 break_label = this_label;
433 }
434 }
435 return stmt;
436}
437
438bool isDefaultCase( const ast::ptr<ast::Stmt> & stmt ) {
439 const ast::CaseStmt * caseStmt = stmt.strict_as<ast::CaseStmt>();
440 return caseStmt->isDefault();
441}
442
443void MultiLevelExitCore::previsit( const ast::SwitchStmt * stmt ) {
444 ast::Label label = label_gen->newLabel( "switchBreak", stmt );
445 auto it = std::find_if( stmt->stmts.rbegin(), stmt->stmts.rend(), isDefaultCase );
446
447 const ast::CaseStmt * defaultCase = it != stmt->stmts.rend()
448 ? (it)->strict_as<ast::CaseStmt>() : nullptr;
449 ast::Label defaultLabel = defaultCase
450 ? label_gen->newLabel( "fallThroughDefault", defaultCase )
451 : ast::Label( stmt->location, "" );
452 enclosing_control_structures.emplace_back( stmt, label, defaultLabel );
453 GuardAction( [this]() { enclosing_control_structures.pop_back(); } );
454
455 // Collect valid labels for fallthrough. It starts with all labels at
456 // this level, then removed as we see them in traversal.
457 for ( const ast::Stmt * stmt : stmt->stmts ) {
458 auto * caseStmt = strict_dynamic_cast< const ast::CaseStmt * >( stmt );
459 if ( caseStmt->stmts.empty() ) continue;
460 auto block = caseStmt->stmts.front().strict_as<ast::CompoundStmt>();
461 for ( const ast::Stmt * stmt : block->kids ) {
462 for ( const ast::Label & l : stmt->labels ) {
463 fallthrough_labels.insert( l );
464 }
465 }
466 }
467}
468
469const ast::SwitchStmt * MultiLevelExitCore::postvisit( const ast::SwitchStmt * stmt ) {
470 assert( !enclosing_control_structures.empty() );
471 Entry & entry = enclosing_control_structures.back();
472 assert( entry.stmt == stmt );
473
474 // Only run if we need to generate the break label.
475 if ( entry.isBreakUsed() ) {
476 // To keep the switch statements uniform (all direct children of a
477 // SwitchStmt should be CastStmts), append the exit label and break
478 // to the last case, create a default case is there are no cases.
479 ast::SwitchStmt * mutStmt = ast::mutate( stmt );
480 if ( mutStmt->stmts.empty() ) {
481 mutStmt->stmts.push_back( new ast::CaseStmt(
482 mutStmt->location, nullptr, {} ));
483 }
484
485 auto caseStmt = mutStmt->stmts.back().strict_as<ast::CaseStmt>();
486 auto mutCase = ast::mutate( caseStmt );
487 mutStmt->stmts.back() = mutCase;
488
489 ast::Label label( mutCase->location, "breakLabel" );
490 auto branch = new ast::BranchStmt( mutCase->location, ast::BranchStmt::Break, label );
491 branch->labels.push_back( entry.useBreakExit() );
492 mutCase->stmts.push_back( branch );
493
494 return mutStmt;
495 }
496 return stmt;
497}
498
499void MultiLevelExitCore::previsit( const ast::ReturnStmt * stmt ) {
500 if ( inFinally ) {
501 SemanticError( stmt->location, "'return' may not appear in a finally clause" );
502 }
503}
504
505void MultiLevelExitCore::previsit( const ast::TryStmt * stmt ) {
506 bool isLabeled = !stmt->labels.empty();
507 if ( isLabeled ) {
508 ast::Label breakLabel = label_gen->newLabel( "blockBreak", stmt );
509 enclosing_control_structures.emplace_back( stmt, breakLabel );
510 GuardAction([this](){ enclosing_control_structures.pop_back(); } );
511 }
512}
513
514void MultiLevelExitCore::postvisit( const ast::TryStmt * stmt ) {
515 bool isLabeled = !stmt->labels.empty();
516 if ( isLabeled ) {
517 auto this_label = enclosing_control_structures.back().useBreakExit();
518 if ( !this_label.empty() ) {
519 break_label = this_label;
520 }
521 }
522}
523
524void MultiLevelExitCore::previsit( const ast::FinallyStmt * ) {
525 GuardAction([this, old = std::move(enclosing_control_structures)](){
526 enclosing_control_structures = std::move(old);
527 });
528 enclosing_control_structures = std::vector<Entry>();
529 GuardValue( inFinally ) = true;
530}
531
532const ast::Stmt * MultiLevelExitCore::mutateLoop(
533 const ast::Stmt * body, Entry & entry ) {
534 if ( entry.isBreakUsed() ) {
535 break_label = entry.useBreakExit();
536 }
537
538 if ( entry.isContUsed() ) {
539 ast::CompoundStmt * new_body = new ast::CompoundStmt( body->location );
540 new_body->kids.push_back( body );
541 new_body->kids.push_back(
542 labelledNullStmt( body->location, entry.useContExit() ) );
543 return new_body;
544 }
545
546 return body;
547}
548
549template<typename LoopNode>
550void MultiLevelExitCore::prehandleLoopStmt( const LoopNode * loopStmt ) {
551 // Remember is loop before going onto mutate the body.
552 // The labels will be folded in if they are used.
553 ast::Label breakLabel = label_gen->newLabel( "loopBreak", loopStmt );
554 ast::Label contLabel = label_gen->newLabel( "loopContinue", loopStmt );
555 enclosing_control_structures.emplace_back( loopStmt, breakLabel, contLabel );
556 GuardAction( [this](){ enclosing_control_structures.pop_back(); } );
557}
558
559template<typename LoopNode>
560const LoopNode * MultiLevelExitCore::posthandleLoopStmt( const LoopNode * loopStmt ) {
561 assert( !enclosing_control_structures.empty() );
562 Entry & entry = enclosing_control_structures.back();
563 assert( entry.stmt == loopStmt );
564
565 // Now we check if the labels are used and add them if so.
566 return ast::mutate_field(
567 loopStmt, &LoopNode::body, mutateLoop( loopStmt->body, entry ) );
568}
569
570std::list<ast::ptr<ast::Stmt>> MultiLevelExitCore::fixBlock(
571 const std::list<ast::ptr<ast::Stmt>> & kids, bool is_case_clause ) {
572 // Unfortunately we can't use the automatic error collection.
573 SemanticErrorException errors;
574
575 std::list<ast::ptr<ast::Stmt>> ret;
576
577 // Manually visit each child.
578 for ( const ast::ptr<ast::Stmt> & kid : kids ) {
579 if ( is_case_clause ) {
580 // Once a label is seen, it's no longer a valid for fallthrough.
581 for ( const ast::Label & l : kid->labels ) {
582 fallthrough_labels.erase( l );
583 }
584 }
585
586 try {
587 ret.push_back( kid->accept( *visitor ) );
588 } catch ( SemanticErrorException & e ) {
589 errors.append( e );
590 }
591
592 if ( !break_label.empty() ) {
593 ret.push_back(
594 labelledNullStmt( ret.back()->location, break_label ) );
595 break_label = ast::Label( CodeLocation(), "" );
596 }
597 }
598
599 if ( !errors.isEmpty() ) {
600 throw errors;
601 }
602 return ret;
603}
604
605} // namespace
606
607const ast::CompoundStmt * multiLevelExitUpdate(
608 const ast::CompoundStmt * stmt,
609 const LabelToStmt & labelTable,
610 LabelGenerator * labelGen ) {
611 // Must start in the body, so FunctionDecls can be a stopping point.
612 ast::Pass<MultiLevelExitCore> visitor( labelTable, labelGen );
613 const ast::CompoundStmt * ret = stmt->accept( visitor );
614 return ret;
615}
616
617} // namespace ControlStruct
618
619// Local Variables: //
620// tab-width: 4 //
621// mode: c++ //
622// compile-command: "make install" //
623// End: //
Note: See TracBrowser for help on using the repository browser.