source: src/ControlStruct/MultiLevelExit.cpp @ 5ee153d

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 5ee153d was cb921d4, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Changed some of the new ast code so they no longer pass around the empty LabelGenerator?.

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