Changeset b56ad5e for src/ControlStruct


Ignore:
Timestamp:
Feb 4, 2022, 10:10:34 PM (4 years ago)
Author:
Fangren Yu <f37yu@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
Children:
f8143a6
Parents:
5f3ba11 (diff), 67e86ae6 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
src/ControlStruct
Files:
4 added
14 edited

Legend:

Unmodified
Added
Removed
  • src/ControlStruct/ExceptTranslateNew.cpp

    r5f3ba11 rb56ad5e  
    99// Author           : Andrew Beach
    1010// Created On       : Mon Nov  8 11:53:00 2021
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Mon Nov  8 16:50:00 2021
    13 // Update Count     : 0
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Mon Jan 31 18:49:58 2022
     13// Update Count     : 1
    1414//
    1515
  • src/ControlStruct/FixLabels.cpp

    r5f3ba11 rb56ad5e  
    99// Author           : Andrew Beach
    1010// Created On       : Mon Nov  1 09:39:00 2021
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Mon Nov  8 10:53:00 2021
    13 // Update Count     : 3
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Mon Jan 31 22:19:17 2022
     13// Update Count     : 9
    1414//
    1515
     
    2020#include "AST/Stmt.hpp"
    2121#include "ControlStruct/MultiLevelExit.hpp"
     22using namespace ast;
    2223
    2324namespace ControlStruct {
    24 
    25 namespace {
    26 
    27 class FixLabelsCore final : public ast::WithGuards {
     25class FixLabelsCore final : public WithGuards {
    2826        LabelToStmt labelTable;
    29 public:
     27  public:
    3028        FixLabelsCore() : labelTable() {}
    3129
    32         void previsit( const ast::FunctionDecl * );
    33         const ast::FunctionDecl * postvisit( const ast::FunctionDecl * );
    34         void previsit( const ast::Stmt * );
    35         void previsit( const ast::BranchStmt * );
    36         void previsit( const ast::LabelAddressExpr * );
     30        void previsit( const FunctionDecl * );
     31        const FunctionDecl * postvisit( const FunctionDecl * );
     32        void previsit( const Stmt * );
     33        void previsit( const BranchStmt * );
     34        void previsit( const LabelAddressExpr * );
    3735
    38         void setLabelsDef( const std::vector<ast::Label> &, const ast::Stmt * );
    39         void setLabelsUsage( const ast::Label & );
     36        void setLabelsDef( const std::vector<Label> &, const Stmt * );
     37        void setLabelsUsage( const Label & );
    4038};
    4139
    42 void FixLabelsCore::previsit( const ast::FunctionDecl * ) {
     40void FixLabelsCore::previsit( const FunctionDecl * ) {
    4341        GuardValue( labelTable ).clear();
    4442}
    4543
    46 const ast::FunctionDecl * FixLabelsCore::postvisit(
    47                 const ast::FunctionDecl * decl ) {
     44const FunctionDecl * FixLabelsCore::postvisit(
     45        const FunctionDecl * decl ) {
    4846        if ( nullptr == decl->stmts ) return decl;
    4947        for ( auto kvp : labelTable ) {
    5048                if ( nullptr == kvp.second ) {
    5149                        SemanticError( kvp.first.location,
    52                                 "Use of undefined label: " + kvp.first.name );
     50                                                   "Use of undefined label: " + kvp.first.name );
    5351                }
    5452        }
    55         return ast::mutate_field( decl, &ast::FunctionDecl::stmts,
    56                 multiLevelExitUpdate( decl->stmts.get(), labelTable ) );
     53        return mutate_field( decl, &FunctionDecl::stmts,
     54                                                 multiLevelExitUpdate( decl->stmts.get(), labelTable ) );
    5755}
    5856
    59 void FixLabelsCore::previsit( const ast::Stmt * stmt ) {
     57void FixLabelsCore::previsit( const Stmt * stmt ) {
    6058        if ( !stmt->labels.empty() ) {
    6159                setLabelsDef( stmt->labels, stmt );
     
    6361}
    6462
    65 void FixLabelsCore::previsit( const ast::BranchStmt * stmt ) {
     63void FixLabelsCore::previsit( const BranchStmt * stmt ) {
    6664        if ( !stmt->labels.empty() ) {
    6765                setLabelsDef( stmt->labels, stmt );
     
    7270}
    7371
    74 void FixLabelsCore::previsit( const ast::LabelAddressExpr * expr ) {
     72void FixLabelsCore::previsit( const LabelAddressExpr * expr ) {
    7573        assert( !expr->arg.empty() );
    7674        setLabelsUsage( expr->arg );
     
    7876
    7977void FixLabelsCore::setLabelsDef(
    80                 const std::vector<ast::Label> & labels, const ast::Stmt * stmt ) {
     78        const std::vector<Label> & labels, const Stmt * stmt ) {
    8179        assert( !labels.empty() );
    8280        assert( stmt );
     
    8987                        // Duplicate definition, this is an error.
    9088                        SemanticError( label.location,
    91                                 "Duplicate definition of label: " + label.name );
     89                                                   "Duplicate definition of label: " + label.name );
    9290                } else {
    9391                        // Perviously used, but not defined until now.
     
    9896
    9997// Label was used, if it is new add it to the table.
    100 void FixLabelsCore::setLabelsUsage( const ast::Label & label ) {
     98void FixLabelsCore::setLabelsUsage( const Label & label ) {
    10199        if ( labelTable.find( label ) == labelTable.end() ) {
    102100                labelTable[ label ] = nullptr;
     
    104102}
    105103
    106 } // namespace
    107 
    108 void fixLabels( ast::TranslationUnit & translationUnit ) {
    109         ast::Pass<FixLabelsCore>::run( translationUnit );
     104void fixLabels( TranslationUnit & translationUnit ) {
     105        Pass<FixLabelsCore>::run( translationUnit );
    110106}
    111 
    112107} // namespace ControlStruct
    113108
  • src/ControlStruct/FixLabels.hpp

    r5f3ba11 rb56ad5e  
    99// Author           : Andrew Beach
    1010// Created On       : Mon Nov  1 09:36:00 2021
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Mon Nov  1 09:40:00 2021
    13 // Update Count     : 0
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Mon Jan 31 22:18:43 2022
     13// Update Count     : 2
    1414//
    1515
     
    1717
    1818namespace ast {
    19         class TranslationUnit;
     19class TranslationUnit;
    2020}
    2121
    2222namespace ControlStruct {
    23 
    24 /// normalizes label definitions and generates multi-level exit labels
     23// normalizes label definitions and generates multi-level exit labels
    2524void fixLabels( ast::TranslationUnit & translationUnit );
    26 
    2725}
    2826
  • src/ControlStruct/ForExprMutator.cc

    r5f3ba11 rb56ad5e  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Mar 11 22:26:52 2019
    13 // Update Count     : 14
     12// Last Modified On : Tue Feb  1 09:26:12 2022
     13// Update Count     : 16
    1414//
    1515
     
    4545                return hoist( forStmt, forStmt->initialization );
    4646        }
    47         Statement * ForExprMutator::postmutate( WhileStmt * whileStmt ) {
    48                 return hoist( whileStmt, whileStmt->initialization );
     47        Statement * ForExprMutator::postmutate( WhileDoStmt * whileDoStmt ) {
     48                return hoist( whileDoStmt, whileDoStmt->initialization );
    4949        }
    5050} // namespace ControlStruct
  • src/ControlStruct/ForExprMutator.h

    r5f3ba11 rb56ad5e  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 17 15:32:48 2017
    13 // Update Count     : 5
     12// Last Modified On : Tue Feb  1 09:18:50 2022
     13// Update Count     : 7
    1414//
    1515
     
    1818class IfStmt;
    1919class ForStmt;
    20 class WhileStmt;
     20class WhileDoStmt;
    2121class Statement;
    2222
     
    2424        class ForExprMutator {
    2525          public:
    26                 Statement *postmutate( IfStmt * );
    27                 Statement *postmutate( ForStmt * );
    28                 Statement *postmutate( WhileStmt * );
     26                Statement * postmutate( IfStmt * );
     27                Statement * postmutate( ForStmt * );
     28                Statement * postmutate( WhileDoStmt * );
    2929        };
    3030} // namespace ControlStruct
  • src/ControlStruct/LabelFixer.cc

    r5f3ba11 rb56ad5e  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Tue Jan 21 10:32:00 2020
    13 // Update Count     : 160
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue Feb  1 09:12:09 2022
     13// Update Count     : 162
    1414//
    1515
     
    2727
    2828namespace ControlStruct {
    29         bool LabelFixer::Entry::insideLoop() {
    30                 return ( dynamic_cast< ForStmt * > ( definition ) ||
    31                         dynamic_cast< WhileStmt * > ( definition )  );
     29bool LabelFixer::Entry::insideLoop() {
     30        return ( dynamic_cast< ForStmt * > ( definition ) ||
     31                dynamic_cast< WhileDoStmt * > ( definition )  );
     32}
     33
     34LabelFixer::LabelFixer( LabelGenerator * gen ) : generator ( gen ) {
     35        if ( generator == 0 )
     36                generator = LabelGenerator::getGenerator();
     37}
     38
     39void LabelFixer::previsit( FunctionDecl * ) {
     40        // need to go into a nested function in a fresh state
     41        GuardValue( labelTable );
     42        labelTable.clear();
     43}
     44
     45void LabelFixer::postvisit( FunctionDecl * functionDecl ) {
     46        PassVisitor<MultiLevelExitMutator> mlem( resolveJumps(), generator );
     47        // We start in the body so we can stop when we hit another FunctionDecl.
     48        maybeMutate( functionDecl->statements, mlem );
     49}
     50
     51// prune to at most one label definition for each statement
     52void LabelFixer::previsit( Statement * stmt ) {
     53        std::list< Label > &labels = stmt->get_labels();
     54
     55        if ( ! labels.empty() ) {
     56                // only remember one label for each statement
     57                Label current = setLabelsDef( labels, stmt );
     58        } // if
     59}
     60
     61void LabelFixer::previsit( BranchStmt * branchStmt ) {
     62        previsit( ( Statement *)branchStmt );
     63
     64        // for labeled branches, add an entry to the label table
     65        Label target = branchStmt->get_target();
     66        if ( target != "" ) {
     67                setLabelsUsg( target, branchStmt );
     68        }
     69}
     70
     71void LabelFixer::previsit( LabelAddressExpr * addrExpr ) {
     72        Label & target = addrExpr->arg;
     73        assert( target != "" );
     74        setLabelsUsg( target, addrExpr );
     75}
     76
     77
     78// Sets the definition of the labelTable entry to be the provided statement for every label in
     79// the list parameter. Happens for every kind of statement.
     80Label LabelFixer::setLabelsDef( std::list< Label > & llabel, Statement * definition ) {
     81        assert( definition != 0 );
     82        assert( llabel.size() > 0 );
     83
     84        for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) {
     85                Label & l = *i;
     86                l.set_statement( definition ); // attach statement to the label to be used later
     87                if ( labelTable.find( l ) == labelTable.end() ) {
     88                        // All labels on this statement need to use the same entry,
     89                        // so this should only be created once.
     90                        // undefined and unused until now, add an entry
     91                        labelTable[ l ] = new Entry( definition );
     92                } else if ( labelTable[ l ]->defined() ) {
     93                        // defined twice, error
     94                        SemanticError( l.get_statement()->location,
     95                                "Duplicate definition of label: " + l.get_name() );
     96                } else {
     97                        // used previously, but undefined until now -> link with this entry
     98                        // Question: Is changing objects important?
     99                        delete labelTable[ l ];
     100                        labelTable[ l ] = new Entry( definition );
     101                } // if
     102        } // for
     103
     104        // Produce one of the labels attached to this statement to be temporarily used as the
     105        // canonical label.
     106        return labelTable[ llabel.front() ]->get_label();
     107}
     108
     109// A label was used, add it to the table if it isn't already there
     110template< typename UsageNode >
     111void LabelFixer::setLabelsUsg( Label orgValue, UsageNode *use ) {
     112        assert( use != 0 );
     113
     114        // add label with an unknown origin
     115        if ( labelTable.find( orgValue ) == labelTable.end() ) {
     116                labelTable[ orgValue ] = new Entry( 0 );
     117        }
     118}
     119
     120// Builds a table that maps a label to its defining statement.
     121std::map<Label, Statement * > * LabelFixer::resolveJumps() throw ( SemanticErrorException ) {
     122        std::map< Label, Statement * > *ret = new std::map< Label, Statement * >();
     123        for ( std::map< Label, Entry * >::iterator i = labelTable.begin(); i != labelTable.end(); ++i ) {
     124                if ( ! i->second->defined() ) {
     125                        SemanticError( i->first.get_statement()->location, "Use of undefined label: " + i->first.get_name() );
     126                }
     127                (*ret)[ i->first ] = i->second->get_definition();
    32128        }
    33129
    34         LabelFixer::LabelFixer( LabelGenerator * gen ) : generator ( gen ) {
    35                 if ( generator == 0 )
    36                         generator = LabelGenerator::getGenerator();
    37         }
    38 
    39         void LabelFixer::previsit( FunctionDecl * ) {
    40                 // need to go into a nested function in a fresh state
    41                 GuardValue( labelTable );
    42                 labelTable.clear();
    43         }
    44 
    45         void LabelFixer::postvisit( FunctionDecl * functionDecl ) {
    46                 PassVisitor<MultiLevelExitMutator> mlem( resolveJumps(), generator );
    47                 // We start in the body so we can stop when we hit another FunctionDecl.
    48                 maybeMutate( functionDecl->statements, mlem );
    49         }
    50 
    51         // prune to at most one label definition for each statement
    52         void LabelFixer::previsit( Statement * stmt ) {
    53                 std::list< Label > &labels = stmt->get_labels();
    54 
    55                 if ( ! labels.empty() ) {
    56                         // only remember one label for each statement
    57                         Label current = setLabelsDef( labels, stmt );
    58                 } // if
    59         }
    60 
    61         void LabelFixer::previsit( BranchStmt * branchStmt ) {
    62                 previsit( ( Statement *)branchStmt );
    63 
    64                 // for labeled branches, add an entry to the label table
    65                 Label target = branchStmt->get_target();
    66                 if ( target != "" ) {
    67                         setLabelsUsg( target, branchStmt );
    68                 }
    69         }
    70 
    71         void LabelFixer::previsit( LabelAddressExpr * addrExpr ) {
    72                 Label & target = addrExpr->arg;
    73                 assert( target != "" );
    74                 setLabelsUsg( target, addrExpr );
    75         }
    76 
    77 
    78         // Sets the definition of the labelTable entry to be the provided statement for every label in
    79         // the list parameter. Happens for every kind of statement.
    80         Label LabelFixer::setLabelsDef( std::list< Label > & llabel, Statement * definition ) {
    81                 assert( definition != 0 );
    82                 assert( llabel.size() > 0 );
    83 
    84                 for ( std::list< Label >::iterator i = llabel.begin(); i != llabel.end(); i++ ) {
    85                         Label & l = *i;
    86                         l.set_statement( definition ); // attach statement to the label to be used later
    87                         if ( labelTable.find( l ) == labelTable.end() ) {
    88                                 // All labels on this statement need to use the same entry,
    89                                 // so this should only be created once.
    90                                 // undefined and unused until now, add an entry
    91                                 labelTable[ l ] = new Entry( definition );
    92                         } else if ( labelTable[ l ]->defined() ) {
    93                                 // defined twice, error
    94                                 SemanticError( l.get_statement()->location,
    95                                         "Duplicate definition of label: " + l.get_name() );
    96                         } else {
    97                                 // used previously, but undefined until now -> link with this entry
    98                                 // Question: Is changing objects important?
    99                                 delete labelTable[ l ];
    100                                 labelTable[ l ] = new Entry( definition );
    101                         } // if
    102                 } // for
    103 
    104                 // Produce one of the labels attached to this statement to be temporarily used as the
    105                 // canonical label.
    106                 return labelTable[ llabel.front() ]->get_label();
    107         }
    108 
    109         // A label was used, add it to the table if it isn't already there
    110         template< typename UsageNode >
    111         void LabelFixer::setLabelsUsg( Label orgValue, UsageNode *use ) {
    112                 assert( use != 0 );
    113 
    114                 // add label with an unknown origin
    115                 if ( labelTable.find( orgValue ) == labelTable.end() ) {
    116                         labelTable[ orgValue ] = new Entry( 0 );
    117                 }
    118         }
    119 
    120         // Builds a table that maps a label to its defining statement.
    121         std::map<Label, Statement * > * LabelFixer::resolveJumps() throw ( SemanticErrorException ) {
    122                 std::map< Label, Statement * > *ret = new std::map< Label, Statement * >();
    123                 for ( std::map< Label, Entry * >::iterator i = labelTable.begin(); i != labelTable.end(); ++i ) {
    124                         if ( ! i->second->defined() ) {
    125                                 SemanticError( i->first.get_statement()->location, "Use of undefined label: " + i->first.get_name() );
    126                         }
    127                         (*ret)[ i->first ] = i->second->get_definition();
    128                 }
    129 
    130                 return ret;
    131         }
     130        return ret;
     131}
    132132}  // namespace ControlStruct
    133133
  • src/ControlStruct/LabelFixer.h

    r5f3ba11 rb56ad5e  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jul 22 09:17:24 2017
    13 // Update Count     : 34
     12// Last Modified On : Mon Jan 31 22:28:04 2022
     13// Update Count     : 35
    1414//
    1515
     
    2626
    2727namespace ControlStruct {
    28         /// normalizes label definitions and generates multi-level exit labels
    29         class LabelGenerator;
     28// normalizes label definitions and generates multi-level exit labels
     29class LabelGenerator;
    3030
    31         class LabelFixer final : public WithGuards {
    32           public:
    33                 LabelFixer( LabelGenerator *gen = 0 );
     31class LabelFixer final : public WithGuards {
     32  public:
     33        LabelFixer( LabelGenerator *gen = 0 );
    3434
    35                 std::map < Label, Statement * > *resolveJumps() throw ( SemanticErrorException );
     35        std::map < Label, Statement * > *resolveJumps() throw ( SemanticErrorException );
    3636
    37                 // Declarations
    38                 void previsit( FunctionDecl *functionDecl );
    39                 void postvisit( FunctionDecl *functionDecl );
     37        // Declarations
     38        void previsit( FunctionDecl *functionDecl );
     39        void postvisit( FunctionDecl *functionDecl );
    4040
    41                 // Statements
    42                 void previsit( Statement *stmt );
    43                 void previsit( BranchStmt *branchStmt );
     41        // Statements
     42        void previsit( Statement *stmt );
     43        void previsit( BranchStmt *branchStmt );
    4444
    45                 // Expressions
    46                 void previsit( LabelAddressExpr *addrExpr );
     45        // Expressions
     46        void previsit( LabelAddressExpr *addrExpr );
    4747
    48                 Label setLabelsDef( std::list< Label > &, Statement *definition );
    49                 template< typename UsageNode >
    50                 void setLabelsUsg( Label, UsageNode *usage = 0 );
     48        Label setLabelsDef( std::list< Label > &, Statement *definition );
     49        template< typename UsageNode >
     50        void setLabelsUsg( Label, UsageNode *usage = 0 );
     51
     52  private:
     53        class Entry {
     54                public:
     55                Entry( Statement *to ) : definition( to ) {}
     56                bool defined() { return ( definition != 0 ); }
     57                bool insideLoop();
     58
     59                Label get_label() const { return label; }
     60                void set_label( Label lab ) { label = lab; }
     61
     62                Statement *get_definition() const { return definition; }
     63                void set_definition( Statement *def ) { definition = def; }
    5164
    5265          private:
    53                 class Entry {
    54                         public:
    55                         Entry( Statement *to ) : definition( to ) {}
    56                         bool defined() { return ( definition != 0 ); }
    57                         bool insideLoop();
     66                Label label;
     67                Statement *definition;
     68        };
    5869
    59                         Label get_label() const { return label; }
    60                         void set_label( Label lab ) { label = lab; }
    61 
    62                         Statement *get_definition() const { return definition; }
    63                         void set_definition( Statement *def ) { definition = def; }
    64 
    65                   private:
    66                         Label label;
    67                         Statement *definition;
    68                 };
    69 
    70                 std::map < Label, Entry *> labelTable;
    71                 LabelGenerator *generator;
    72         };
     70        std::map < Label, Entry *> labelTable;
     71        LabelGenerator *generator;
     72};
    7373} // namespace ControlStruct
    7474
  • src/ControlStruct/LabelGenerator.cc

    r5f3ba11 rb56ad5e  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Mon Nov  8 10:18:00 2021
    13 // Update Count     : 17
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Mon Jan 31 22:30:26 2022
     13// Update Count     : 28
    1414//
    1515
     
    1717#include <sstream>              // for ostringstream
    1818#include <list>                 // for list
     19using namespace std;
    1920
    2021#include "LabelGenerator.h"
    2122
    22 #include "AST/Attribute.hpp"
    23 #include "AST/Label.hpp"
    24 #include "AST/Stmt.hpp"
    2523#include "SynTree/Attribute.h"  // for Attribute
    2624#include "SynTree/Label.h"      // for Label, operator<<
     
    2826
    2927namespace ControlStruct {
    30 
    3128int LabelGenerator::current = 0;
    3229LabelGenerator * LabelGenerator::labelGenerator = nullptr;
    3330
    34         LabelGenerator * LabelGenerator::getGenerator() {
    35                 if ( LabelGenerator::labelGenerator == 0 )
    36                         LabelGenerator::labelGenerator = new LabelGenerator();
    37                 return labelGenerator;
    38         }
    39 
    40         Label LabelGenerator::newLabel( std::string suffix, Statement * stmt ) {
    41                 std::ostringstream os;
    42                 os << "__L" << current++ << "__" << suffix;
    43                 if ( stmt && ! stmt->get_labels().empty() ) {
    44                         os << "_" << stmt->get_labels().front() << "__";
    45                 } // if
    46                 std::string ret = os.str();
    47                 Label l( ret );
    48                 l.get_attributes().push_back( new Attribute("unused") );
    49                 return l;
    50         }
    51 
    52 ast::Label LabelGenerator::newLabel(
    53                 const std::string & suffix, const ast::Stmt * stmt ) {
    54         assert( stmt );
    55 
    56         std::ostringstream os;
    57         os << "__L" << current++ << "__" << suffix;
    58         if ( stmt && !stmt->labels.empty() ) {
    59                 os << "_" << stmt->labels.front() << "__";
    60         }
    61         ast::Label ret_label( stmt->location, os.str() );
    62         ret_label.attributes.push_back( new ast::Attribute( "unused" ) );
    63         return ret_label;
     31LabelGenerator * LabelGenerator::getGenerator() {
     32        if ( LabelGenerator::labelGenerator == 0 )
     33                LabelGenerator::labelGenerator = new LabelGenerator();
     34        return labelGenerator;
    6435}
    6536
     37Label LabelGenerator::newLabel( string suffix, Statement * stmt ) {
     38        ostringstream os;
     39        os << "__L_OLD" << current++ << "__" << suffix;
     40        if ( stmt && ! stmt->get_labels().empty() ) {
     41                os << "_" << stmt->get_labels().front() << "__";
     42        } // if
     43        string ret = os.str();
     44        Label l( ret );
     45        l.get_attributes().push_back( new Attribute( "unused" ) );
     46        return l;
     47}
    6648} // namespace ControlStruct
    6749
    6850// Local Variables: //
    69 // tab-width: 4 //
    7051// mode: c++ //
    71 // compile-command: "make install" //
    7252// End: //
  • src/ControlStruct/LabelGenerator.h

    r5f3ba11 rb56ad5e  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Mon Nov  8 10:16:00 2021
    13 // Update Count     : 8
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Mon Jan 31 22:30:10 2022
     13// Update Count     : 16
    1414//
    1515
     
    2121
    2222class Statement;
     23
    2324namespace ast {
    24         class Stmt;
    25         class Label;
     25class Stmt;
     26class Label;
    2627}
    2728
    2829namespace ControlStruct {
    29 
    3030class LabelGenerator {
    3131        static int current;
    3232        static LabelGenerator *labelGenerator;
    33 protected:
     33  protected:
    3434        LabelGenerator() {}
    35 public:
     35  public:
    3636        static LabelGenerator *getGenerator();
    3737        static Label newLabel(std::string suffix, Statement * stmt = nullptr);
    38         static ast::Label newLabel( const std::string&, const ast::Stmt * );
    39         static void reset() { current = 0; }
    40         static void rewind() { current--; }
    4138};
    42 
    4339} // namespace ControlStruct
    4440
  • src/ControlStruct/MLEMutator.cc

    r5f3ba11 rb56ad5e  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Wed Jan 22 11:50:00 2020
    13 // Update Count     : 223
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Wed Feb  2 20:18:57 2022
     13// Update Count     : 227
    1414//
    1515
     
    3939        namespace {
    4040                bool isLoop( const MultiLevelExitMutator::Entry & e ) {
    41                         return dynamic_cast< WhileStmt * >( e.get_controlStructure() )
     41                        return dynamic_cast< WhileDoStmt * >( e.get_controlStructure() )
    4242                                || dynamic_cast< ForStmt * >( e.get_controlStructure() );
    4343                }
     
    136136                        }
    137137                }
    138                 assertf( false, "Could not find label '%s' on statement %s",
     138                assertf( false, "CFA internal error: could not find label '%s' on statement %s",
    139139                        originalTarget.get_name().c_str(), toString( stmt ).c_str() );
    140140        }
     
    295295        }
    296296
    297         void MultiLevelExitMutator::premutate( WhileStmt * whileStmt ) {
    298                 return prehandleLoopStmt( whileStmt );
     297        void MultiLevelExitMutator::premutate( WhileDoStmt * whileDoStmt ) {
     298                return prehandleLoopStmt( whileDoStmt );
    299299        }
    300300
     
    303303        }
    304304
    305         Statement * MultiLevelExitMutator::postmutate( WhileStmt * whileStmt ) {
    306                 return posthandleLoopStmt( whileStmt );
     305        Statement * MultiLevelExitMutator::postmutate( WhileDoStmt * whileDoStmt ) {
     306                return posthandleLoopStmt( whileDoStmt );
    307307        }
    308308
     
    395395                }
    396396                assert( ! enclosingControlStructures.empty() );
    397                 assertf( dynamic_cast<SwitchStmt *>( enclosingControlStructures.back().get_controlStructure() ), "Control structure enclosing a case clause must be a switch, but is: %s", toCString( enclosingControlStructures.back().get_controlStructure() ) );
     397                assertf( dynamic_cast<SwitchStmt *>( enclosingControlStructures.back().get_controlStructure() ),
     398                                 "CFA internal error: control structure enclosing a case clause must be a switch, but is: %s",
     399                                 toCString( enclosingControlStructures.back().get_controlStructure() ) );
    398400                if ( caseStmt->isDefault() ) {
    399401                        if ( enclosingControlStructures.back().isFallDefaultUsed() ) {
  • src/ControlStruct/MLEMutator.h

    r5f3ba11 rb56ad5e  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Wed Jan 22 11:50:00 2020
    13 // Update Count     : 48
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue Feb  1 09:27:24 2022
     13// Update Count     : 50
    1414//
    1515
     
    4242                void premutate( CompoundStmt *cmpndStmt );
    4343                Statement * postmutate( BranchStmt *branchStmt ) throw ( SemanticErrorException );
    44                 void premutate( WhileStmt *whileStmt );
    45                 Statement * postmutate( WhileStmt *whileStmt );
     44                void premutate( WhileDoStmt *whileDoStmt );
     45                Statement * postmutate( WhileDoStmt *whileDoStmt );
    4646                void premutate( ForStmt *forStmt );
    4747                Statement * postmutate( ForStmt *forStmt );
     
    6767                                stmt( stmt ), breakExit( breakExit ), contExit( contExit ) {}
    6868
    69                         explicit Entry( WhileStmt *stmt, Label breakExit, Label contExit ) :
     69                        explicit Entry( WhileDoStmt *stmt, Label breakExit, Label contExit ) :
    7070                                stmt( stmt ), breakExit( breakExit ), contExit( contExit ) {}
    7171
  • src/ControlStruct/MultiLevelExit.cpp

    r5f3ba11 rb56ad5e  
    99// Author           : Andrew Beach
    1010// 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
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Wed Feb  2 23:07:54 2022
     13// Update Count     : 33
    1414//
    1515
     
    1818#include "AST/Pass.hpp"
    1919#include "AST/Stmt.hpp"
    20 #include "ControlStruct/LabelGenerator.h"
     20#include "LabelGeneratorNew.hpp"
    2121
    2222#include <set>
     23using namespace std;
     24using namespace ast;
    2325
    2426namespace ControlStruct {
    25 
    26 namespace {
    27 
    2827class Entry {
    29 public:
    30         const ast::Stmt * stmt;
    31 private:
     28  public:
     29        const Stmt * stmt;
     30  private:
    3231        // Organized like a manual ADT. Avoids creating a bunch of dead data.
    3332        struct Target {
    34                 ast::Label label;
     33                Label label;
    3534                bool used = false;
    36                 Target( const ast::Label & label ) : label( label ) {}
     35                Target( const Label & label ) : label( label ) {}
    3736                Target() : label( CodeLocation() ) {}
    3837        };
     
    4140
    4241        enum Kind {
    43                 ForStmt, WhileStmt, CompoundStmt, IfStmt, CaseStmt, SwitchStmt, TryStmt
     42                ForStmtK, WhileDoStmtK, CompoundStmtK, IfStmtK, CaseStmtK, SwitchStmtK, TryStmtK
    4443        } kind;
    4544
    4645        bool fallDefaultValid = true;
    4746
    48         static ast::Label & useTarget( Target & target ) {
     47        static Label & useTarget( Target & target ) {
    4948                target.used = true;
    5049                return target.label;
    5150        }
    52 
    53 public:
    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; }
     51  public:
     52        Entry( const ForStmt * stmt, Label breakExit, Label contExit ) :
     53                stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( ForStmtK ) {}
     54        Entry( const WhileDoStmt * stmt, Label breakExit, Label contExit ) :
     55                stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( WhileDoStmtK ) {}
     56        Entry( const CompoundStmt *stmt, Label breakExit ) :
     57                stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( CompoundStmtK ) {}
     58        Entry( const IfStmt *stmt, Label breakExit ) :
     59                stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( IfStmtK ) {}
     60        Entry( const CaseStmt *stmt, Label fallExit ) :
     61                stmt( stmt ), firstTarget( fallExit ), secondTarget(), kind( CaseStmtK ) {}
     62        Entry( const SwitchStmt *stmt, Label breakExit, Label fallDefaultExit ) :
     63                stmt( stmt ), firstTarget( breakExit ), secondTarget( fallDefaultExit ), kind( SwitchStmtK ) {}
     64        Entry( const TryStmt *stmt, Label breakExit ) :
     65                stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( TryStmtK ) {}
     66
     67        bool isContTarget() const { return kind <= WhileDoStmtK; }
     68        bool isBreakTarget() const { return kind != CaseStmtK; }
     69        bool isFallTarget() const { return kind == CaseStmtK; }
     70        bool isFallDefaultTarget() const { return kind == SwitchStmtK; }
     71
     72        // These routines set a target as being "used" by a BranchStmt
     73        Label useContExit() { assert( kind <= WhileDoStmtK ); return useTarget(secondTarget); }
     74        Label useBreakExit() { assert( kind != CaseStmtK ); return useTarget(firstTarget); }
     75        Label useFallExit() { assert( kind == CaseStmtK );  return useTarget(firstTarget); }
     76        Label useFallDefaultExit() { assert( kind == SwitchStmtK ); return useTarget(secondTarget); }
     77
     78        // These routines check if a specific label for a statement is used by a BranchStmt
     79        bool isContUsed() const { assert( kind <= WhileDoStmtK ); return secondTarget.used; }
     80        bool isBreakUsed() const { assert( kind != CaseStmtK ); return firstTarget.used; }
     81        bool isFallUsed() const { assert( kind == CaseStmtK ); return firstTarget.used; }
     82        bool isFallDefaultUsed() const { assert( kind == SwitchStmtK ); return secondTarget.used; }
    8383        void seenDefault() { fallDefaultValid = false; }
    8484        bool isFallDefaultValid() const { return fallDefaultValid; }
    8585};
    8686
    87 // Helper predicates used in std::find_if calls (it doesn't take methods):
     87// Helper predicates used in find_if calls (it doesn't take methods):
    8888bool isBreakTarget( const Entry & entry ) {
    8989        return entry.isBreakTarget();
     
    103103
    104104struct MultiLevelExitCore final :
    105                 public ast::WithVisitorRef<MultiLevelExitCore>,
    106                 public ast::WithShortCircuiting, public ast::WithGuards {
     105        public WithVisitorRef<MultiLevelExitCore>,
     106        public WithShortCircuiting, public WithGuards {
    107107        MultiLevelExitCore( const LabelToStmt & lt );
    108108
    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& );
     109        void previsit( const FunctionDecl * );
     110
     111        const CompoundStmt * previsit( const CompoundStmt * );
     112        const BranchStmt * postvisit( const BranchStmt * );
     113        void previsit( const WhileDoStmt * );
     114        const WhileDoStmt * postvisit( const WhileDoStmt * );
     115        void previsit( const ForStmt * );
     116        const ForStmt * postvisit( const ForStmt * );
     117        const CaseStmt * previsit( const CaseStmt * );
     118        void previsit( const IfStmt * );
     119        const IfStmt * postvisit( const IfStmt * );
     120        void previsit( const SwitchStmt * );
     121        const SwitchStmt * postvisit( const SwitchStmt * );
     122        void previsit( const ReturnStmt * );
     123        void previsit( const TryStmt * );
     124        void postvisit( const TryStmt * );
     125        void previsit( const FinallyStmt * );
     126
     127        const Stmt * mutateLoop( const Stmt * body, Entry& );
    128128
    129129        const LabelToStmt & target_table;
    130         std::set<ast::Label> fallthrough_labels;
    131         std::vector<Entry> enclosing_control_structures;
    132         ast::Label break_label;
     130        set<Label> fallthrough_labels;
     131        vector<Entry> enclosing_control_structures;
     132        Label break_label;
    133133        bool inFinally;
    134134
     
    138138        const LoopNode * posthandleLoopStmt( const LoopNode * loopStmt );
    139139
    140         std::list<ast::ptr<ast::Stmt>> fixBlock(
    141                 const std::list<ast::ptr<ast::Stmt>> & kids, bool caseClause );
     140        list<ptr<Stmt>> fixBlock(
     141                const list<ptr<Stmt>> & kids, bool caseClause );
    142142
    143143        template<typename UnaryPredicate>
    144144        auto findEnclosingControlStructure( UnaryPredicate pred ) {
    145                 return std::find_if( enclosing_control_structures.rbegin(),
    146                         enclosing_control_structures.rend(), pred );
     145                return find_if( enclosing_control_structures.rbegin(),
     146                                                enclosing_control_structures.rend(), pred );
    147147        }
    148148};
    149149
    150 ast::NullStmt * labelledNullStmt(
    151                 const CodeLocation & cl, const ast::Label & label ) {
    152         return new ast::NullStmt( cl, std::vector<ast::Label>{ label } );
     150NullStmt * labelledNullStmt(
     151        const CodeLocation & cl, const Label & label ) {
     152        return new NullStmt( cl, vector<Label>{ label } );
    153153}
    154154
     
    158158{}
    159159
    160 void MultiLevelExitCore::previsit( const ast::FunctionDecl * ) {
     160void MultiLevelExitCore::previsit( const FunctionDecl * ) {
    161161        visit_children = false;
    162162}
    163163
    164 const ast::CompoundStmt * MultiLevelExitCore::previsit(
    165                 const ast::CompoundStmt * stmt ) {
     164const CompoundStmt * MultiLevelExitCore::previsit(
     165        const CompoundStmt * stmt ) {
    166166        visit_children = false;
    167         bool isLabeled = !stmt->labels.empty();
     167
     168        // if the stmt is labelled then generate a label to check in postvisit if the label is used
     169        bool isLabeled = ! stmt->labels.empty();
    168170        if ( isLabeled ) {
    169                 ast::Label breakLabel = LabelGenerator::newLabel( "blockBreak", stmt );
     171                Label breakLabel = newLabel( "blockBreak", stmt );
    170172                enclosing_control_structures.emplace_back( stmt, breakLabel );
    171173                GuardAction( [this]() { enclosing_control_structures.pop_back(); } );
    172174        }
    173175
    174         auto mutStmt = ast::mutate( stmt );
     176        auto mutStmt = mutate( stmt );
    175177        // A child statement may set the break label.
    176         mutStmt->kids = std::move( fixBlock( stmt->kids, false ) );
     178        mutStmt->kids = move( fixBlock( stmt->kids, false ) );
    177179
    178180        if ( isLabeled ) {
    179                 assert( !enclosing_control_structures.empty() );
     181                assert( ! enclosing_control_structures.empty() );
    180182                Entry & entry = enclosing_control_structures.back();
    181                 if ( !entry.useBreakExit().empty() ) {
     183                if ( ! entry.useBreakExit().empty() ) {
    182184                        break_label = entry.useBreakExit();
    183185                }
     
    187189
    188190size_t getUnusedIndex(
    189                 const ast::Stmt * stmt, const ast::Label & originalTarget ) {
     191        const Stmt * stmt, const Label & originalTarget ) {
    190192        const size_t size = stmt->labels.size();
    191193
    192         // If the label is empty, we can skip adding the unused attribute:
    193         if ( originalTarget.empty() ) return size;
     194        // If the label is empty, do not add unused attribute.
     195  if ( originalTarget.empty() ) return size;
    194196
    195197        // Search for a label that matches the originalTarget.
    196198        for ( size_t i = 0 ; i < size ; ++i ) {
    197                 const ast::Label & label = stmt->labels[i];
     199                const Label & label = stmt->labels[i];
    198200                if ( label == originalTarget ) {
    199                         for ( const ast::Attribute * attr : label.attributes ) {
     201                        for ( const Attribute * attr : label.attributes ) {
    200202                                if ( attr->name == "unused" ) return size;
    201203                        }
     
    203205                }
    204206        }
    205         assertf( false, "Could not find label '%s' on statement %s",
    206                 originalTarget.name.c_str(), toString( stmt ).c_str() );
    207 }
    208 
    209 const ast::Stmt * addUnused(
    210                 const ast::Stmt * stmt, const ast::Label & originalTarget ) {
     207        assertf( false, "CFA internal error: could not find label '%s' on statement %s",
     208                         originalTarget.name.c_str(), toString( stmt ).c_str() );
     209}
     210
     211const Stmt * addUnused(
     212        const Stmt * stmt, const Label & originalTarget ) {
    211213        size_t i = getUnusedIndex( stmt, originalTarget );
    212214        if ( i == stmt->labels.size() ) {
    213215                return stmt;
    214216        }
    215         ast::Stmt * mutStmt = ast::mutate( stmt );
    216         mutStmt->labels[i].attributes.push_back( new ast::Attribute( "unused" ) );
     217        Stmt * mutStmt = mutate( stmt );
     218        mutStmt->labels[i].attributes.push_back( new Attribute( "unused" ) );
    217219        return mutStmt;
    218220}
    219221
    220 const ast::BranchStmt * MultiLevelExitCore::postvisit( const ast::BranchStmt * stmt ) {
    221         std::vector<Entry>::reverse_iterator targetEntry =
     222// This routine updates targets on enclosing control structures to indicate which
     223//     label is used by the BranchStmt that is passed
     224const BranchStmt * MultiLevelExitCore::postvisit( const BranchStmt * stmt ) {
     225        vector<Entry>::reverse_iterator targetEntry =
    222226                enclosing_control_structures.rend();
     227
     228        // Labels on different stmts require different approaches to access
    223229        switch ( stmt->kind ) {
    224         case ast::BranchStmt::Goto:
     230          case BranchStmt::Goto:
    225231                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:
     232          case BranchStmt::Continue:
     233          case BranchStmt::Break: {
     234                  bool isContinue = stmt->kind == BranchStmt::Continue;
     235                  // Handle unlabeled break and continue.
     236                  if ( stmt->target.empty() ) {
     237                          if ( isContinue ) {
     238                                  targetEntry = findEnclosingControlStructure( isContinueTarget );
     239                          } else {
     240                                  if ( enclosing_control_structures.empty() ) {
     241                                          SemanticError( stmt->location,
     242                                                                         "'break' outside a loop, 'switch', or labelled block" );
     243                                  }
     244                                  targetEntry = findEnclosingControlStructure( isBreakTarget );
     245                          }
     246                          // Handle labeled break and continue.
     247                  } else {
     248                          // Lookup label in table to find attached control structure.
     249                          targetEntry = findEnclosingControlStructure(
     250                                  [ targetStmt = target_table.at(stmt->target) ](auto entry){
     251                                          return entry.stmt == targetStmt;
     252                                  } );
     253                  }
     254                  // Ensure that selected target is valid.
     255                  if ( targetEntry == enclosing_control_structures.rend() || ( isContinue && ! isContinueTarget( *targetEntry ) ) ) {
     256                          SemanticError( stmt->location, toString( (isContinue ? "'continue'" : "'break'"),
     257                                                        " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "),
     258                                                        stmt->originalTarget ) );
     259                  }
     260                  break;
     261          }
     262          // handle fallthrough in case/switch stmts
     263          case BranchStmt::FallThrough: {
     264                  targetEntry = findEnclosingControlStructure( isFallthroughTarget );
     265                  // Check that target is valid.
     266                  if ( targetEntry == enclosing_control_structures.rend() ) {
     267                          SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" );
     268                  }
     269                  if ( ! stmt->target.empty() ) {
     270                          // Labelled fallthrough: target must be a valid fallthough label.
     271                          if ( ! fallthrough_labels.count( stmt->target ) ) {
     272                                  SemanticError( stmt->location, toString( "'fallthrough' target must be a later case statement: ",
     273                                                                                                                   stmt->originalTarget ) );
     274                          }
     275                          return new BranchStmt( stmt->location, BranchStmt::Goto, stmt->originalTarget );
     276                  }
     277                  break;
     278          }
     279          case BranchStmt::FallThroughDefault: {
     280                  targetEntry = findEnclosingControlStructure( isFallthroughDefaultTarget );
     281
     282                  // Check if in switch or choose statement.
     283                  if ( targetEntry == enclosing_control_structures.rend() ) {
     284                          SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" );
     285                  }
     286
     287                  // Check if switch or choose has default clause.
     288                  auto switchStmt = strict_dynamic_cast< const SwitchStmt * >( targetEntry->stmt );
     289                  bool foundDefault = false;
     290                  for ( auto subStmt : switchStmt->stmts ) {
     291                          const CaseStmt * caseStmt = subStmt.strict_as<CaseStmt>();
     292                          if ( caseStmt->isDefault() ) {
     293                                  foundDefault = true;
     294                                  break;
     295                          }
     296                  }
     297                  if ( ! foundDefault ) {
     298                          SemanticError( stmt->location, "'fallthrough default' must be enclosed in a 'switch' or 'choose'"
     299                                                         "control structure with a 'default' clause" );
     300                  }
     301                  break;
     302          }
     303          default:
    300304                assert( false );
    301305        }
    302306
    303         // Branch error checks: get the appropriate label name:
    304         // (This label will always be replaced.)
    305         ast::Label exitLabel( CodeLocation(), "" );
     307        // Branch error checks: get the appropriate label name, which is always replaced.
     308        Label exitLabel( CodeLocation(), "" );
    306309        switch ( stmt->kind ) {
    307         case ast::BranchStmt::Break:
    308                 assert( !targetEntry->useBreakExit().empty() );
     310          case BranchStmt::Break:
     311                assert( ! targetEntry->useBreakExit().empty() );
    309312                exitLabel = targetEntry->useBreakExit();
    310313                break;
    311         case ast::BranchStmt::Continue:
    312                 assert( !targetEntry->useContExit().empty() );
     314          case BranchStmt::Continue:
     315                assert( ! targetEntry->useContExit().empty() );
    313316                exitLabel = targetEntry->useContExit();
    314317                break;
    315         case ast::BranchStmt::FallThrough:
    316                 assert( !targetEntry->useFallExit().empty() );
     318          case BranchStmt::FallThrough:
     319                assert( ! targetEntry->useFallExit().empty() );
    317320                exitLabel = targetEntry->useFallExit();
    318321                break;
    319         case ast::BranchStmt::FallThroughDefault:
    320                 assert( !targetEntry->useFallDefaultExit().empty() );
     322          case BranchStmt::FallThroughDefault:
     323                assert( ! targetEntry->useFallDefaultExit().empty() );
    321324                exitLabel = targetEntry->useFallDefaultExit();
    322325                // 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                if ( ! targetEntry->isFallDefaultValid() ) {
     327                        SemanticError( stmt->location, "'fallthrough default' must precede the 'default' clause" );
    326328                }
    327329                break;
    328         default:
     330          default:
    329331                assert(0);
    330332        }
     
    333335        targetEntry->stmt = addUnused( targetEntry->stmt, stmt->originalTarget );
    334336
    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 
    339 void MultiLevelExitCore::previsit( const ast::WhileStmt * stmt ) {
     337        // Replace with goto to make later passes more uniform.
     338        return new BranchStmt( stmt->location, BranchStmt::Goto, exitLabel );
     339}
     340
     341void MultiLevelExitCore::previsit( const WhileDoStmt * stmt ) {
    340342        return prehandleLoopStmt( stmt );
    341343}
    342344
    343 const ast::WhileStmt * MultiLevelExitCore::postvisit( const ast::WhileStmt * stmt ) {
     345const WhileDoStmt * MultiLevelExitCore::postvisit( const WhileDoStmt * stmt ) {
    344346        return posthandleLoopStmt( stmt );
    345347}
    346348
    347 void MultiLevelExitCore::previsit( const ast::ForStmt * stmt ) {
     349void MultiLevelExitCore::previsit( const ForStmt * stmt ) {
    348350        return prehandleLoopStmt( stmt );
    349351}
    350352
    351 const ast::ForStmt * MultiLevelExitCore::postvisit( const ast::ForStmt * stmt ) {
     353const ForStmt * MultiLevelExitCore::postvisit( const ForStmt * stmt ) {
    352354        return posthandleLoopStmt( stmt );
    353355}
     
    355357// Mimic what the built-in push_front would do anyways. It is O(n).
    356358void push_front(
    357                 std::vector<ast::ptr<ast::Stmt>> & vec, const ast::Stmt * element ) {
     359        vector<ptr<Stmt>> & vec, const Stmt * element ) {
    358360        vec.emplace_back( nullptr );
    359361        for ( size_t i = vec.size() - 1 ; 0 < i ; --i ) {
    360                 vec[ i ] = std::move( vec[ i - 1 ] );
     362                vec[ i ] = move( vec[ i - 1 ] );
    361363        }
    362364        vec[ 0 ] = element;
    363365}
    364366
    365 const ast::CaseStmt * MultiLevelExitCore::previsit( const ast::CaseStmt * stmt ) {
     367const CaseStmt * MultiLevelExitCore::previsit( const CaseStmt * stmt ) {
    366368        visit_children = false;
    367369
    368         // If it is the default, mark the default as seen.
     370        // If default, mark seen.
    369371        if ( stmt->isDefault() ) {
    370                 assert( !enclosing_control_structures.empty() );
     372                assert( ! enclosing_control_structures.empty() );
    371373                enclosing_control_structures.back().seenDefault();
    372374        }
    373375
    374376        // The cond may not exist, but if it does update it now.
    375         visitor->maybe_accept( stmt, &ast::CaseStmt::cond );
     377        visitor->maybe_accept( stmt, &CaseStmt::cond );
    376378
    377379        // 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() ) {
     380        CaseStmt * mutStmt = mutate( stmt );
     381
     382        Label fallLabel = newLabel( "fallThrough", stmt );
     383        if ( ! mutStmt->stmts.empty() ) {
    382384                // Ensure that the stack isn't corrupted by exceptions in fixBlock.
    383385                auto guard = makeFuncGuard(
    384386                        [&](){ enclosing_control_structures.emplace_back( mutStmt, fallLabel ); },
    385387                        [this](){ enclosing_control_structures.pop_back(); }
    386                 );
     388                        );
    387389
    388390                // These should already be in a block.
    389                 auto block = ast::mutate( mutStmt->stmts.front().strict_as<ast::CompoundStmt>() );
     391                auto block = mutate( mutStmt->stmts.front().strict_as<CompoundStmt>() );
    390392                block->kids = fixBlock( block->kids, true );
    391393
    392394                // Add fallthrough label if necessary.
    393                 assert( !enclosing_control_structures.empty() );
     395                assert( ! enclosing_control_structures.empty() );
    394396                Entry & entry = enclosing_control_structures.back();
    395397                if ( entry.isFallUsed() ) {
    396                         mutStmt->stmts.push_back(
    397                                 labelledNullStmt( mutStmt->location, entry.useFallExit() ) );
    398                 }
    399         }
    400         assert( !enclosing_control_structures.empty() );
     398                        mutStmt->stmts.push_back( labelledNullStmt( mutStmt->location, entry.useFallExit() ) );
     399                }
     400        }
     401        assert( ! enclosing_control_structures.empty() );
    401402        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() );
     403        assertf( dynamic_cast< const SwitchStmt * >( entry.stmt ),
     404                         "CFA internal error: control structure enclosing a case clause must be a switch, but is: %s",
     405                         toString( entry.stmt ).c_str() );
    405406        if ( mutStmt->isDefault() ) {
    406407                if ( entry.isFallDefaultUsed() ) {
    407408                        // Add fallthrough default label if necessary.
    408                         push_front( mutStmt->stmts, labelledNullStmt(
    409                                 stmt->location, entry.useFallDefaultExit()
    410                         ) );
     409                        push_front( mutStmt->stmts, labelledNullStmt( stmt->location, entry.useFallDefaultExit() ) );
    411410                }
    412411        }
     
    414413}
    415414
    416 void MultiLevelExitCore::previsit( const ast::IfStmt * stmt ) {
    417         bool labeledBlock = !stmt->labels.empty();
     415void MultiLevelExitCore::previsit( const IfStmt * stmt ) {
     416        bool labeledBlock = ! stmt->labels.empty();
    418417        if ( labeledBlock ) {
    419                 ast::Label breakLabel = LabelGenerator::newLabel( "blockBreak", stmt );
     418                Label breakLabel = newLabel( "blockBreak", stmt );
    420419                enclosing_control_structures.emplace_back( stmt, breakLabel );
    421420                GuardAction( [this](){ enclosing_control_structures.pop_back(); } );
     
    423422}
    424423
    425 const ast::IfStmt * MultiLevelExitCore::postvisit( const ast::IfStmt * stmt ) {
    426         bool labeledBlock = !stmt->labels.empty();
     424const IfStmt * MultiLevelExitCore::postvisit( const IfStmt * stmt ) {
     425        bool labeledBlock = ! stmt->labels.empty();
    427426        if ( labeledBlock ) {
    428427                auto this_label = enclosing_control_structures.back().useBreakExit();
    429                 if ( !this_label.empty() ) {
     428                if ( ! this_label.empty() ) {
    430429                        break_label = this_label;
    431430                }
     
    434433}
    435434
    436 bool isDefaultCase( const ast::ptr<ast::Stmt> & stmt ) {
    437         const ast::CaseStmt * caseStmt = stmt.strict_as<ast::CaseStmt>();
     435bool isDefaultCase( const ptr<Stmt> & stmt ) {
     436        const CaseStmt * caseStmt = stmt.strict_as<CaseStmt>();
    438437        return caseStmt->isDefault();
    439438}
    440439
    441 void 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, "" );
     440void MultiLevelExitCore::previsit( const SwitchStmt * stmt ) {
     441        Label label = newLabel( "switchBreak", stmt );
     442        auto it = find_if( stmt->stmts.rbegin(), stmt->stmts.rend(), isDefaultCase );
     443
     444        const CaseStmt * defaultCase = it != stmt->stmts.rend() ? (it)->strict_as<CaseStmt>() : nullptr;
     445        Label defaultLabel = defaultCase ? newLabel( "fallThroughDefault", defaultCase ) : Label( stmt->location, "" );
    450446        enclosing_control_structures.emplace_back( stmt, label, defaultLabel );
    451447        GuardAction( [this]() { enclosing_control_structures.pop_back(); } );
    452448
    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 );
     449        // Collect valid labels for fallthrough. It starts with all labels at this level, then remove as each is seen during
     450        // traversal.
     451        for ( const Stmt * stmt : stmt->stmts ) {
     452                auto * caseStmt = strict_dynamic_cast< const CaseStmt * >( stmt );
    457453                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 ) {
     454                auto block = caseStmt->stmts.front().strict_as<CompoundStmt>();
     455                for ( const Stmt * stmt : block->kids ) {
     456                        for ( const Label & l : stmt->labels ) {
    461457                                fallthrough_labels.insert( l );
    462458                        }
     
    465461}
    466462
    467 const ast::SwitchStmt * MultiLevelExitCore::postvisit( const ast::SwitchStmt * stmt ) {
    468         assert( !enclosing_control_structures.empty() );
     463const SwitchStmt * MultiLevelExitCore::postvisit( const SwitchStmt * stmt ) {
     464        assert( ! enclosing_control_structures.empty() );
    469465        Entry & entry = enclosing_control_structures.back();
    470466        assert( entry.stmt == stmt );
    471467
    472         // Only run if we need to generate the break label.
     468        // Only run to generate the break label.
    473469        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 );
     470                // To keep the switch statements uniform (all direct children of a SwitchStmt should be CastStmts), append the
     471                // exit label and break to the last case, create a default case if no cases.
     472                SwitchStmt * mutStmt = mutate( stmt );
    478473                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 );
     474                        mutStmt->stmts.push_back( new CaseStmt( mutStmt->location, nullptr, {} ) );
     475                }
     476
     477                auto caseStmt = mutStmt->stmts.back().strict_as<CaseStmt>();
     478                auto mutCase = mutate( caseStmt );
    485479                mutStmt->stmts.back() = mutCase;
    486480
    487                 ast::Label label( mutCase->location, "breakLabel" );
    488                 auto branch = new ast::BranchStmt( mutCase->location, ast::BranchStmt::Break, label );
     481                Label label( mutCase->location, "breakLabel" );
     482                auto branch = new BranchStmt( mutCase->location, BranchStmt::Break, label );
    489483                branch->labels.push_back( entry.useBreakExit() );
    490484                mutCase->stmts.push_back( branch );
     
    495489}
    496490
    497 void MultiLevelExitCore::previsit( const ast::ReturnStmt * stmt ) {
     491void MultiLevelExitCore::previsit( const ReturnStmt * stmt ) {
    498492        if ( inFinally ) {
    499493                SemanticError( stmt->location, "'return' may not appear in a finally clause" );
     
    501495}
    502496
    503 void MultiLevelExitCore::previsit( const ast::TryStmt * stmt ) {
    504         bool isLabeled = !stmt->labels.empty();
     497void MultiLevelExitCore::previsit( const TryStmt * stmt ) {
     498        bool isLabeled = ! stmt->labels.empty();
    505499        if ( isLabeled ) {
    506                 ast::Label breakLabel = LabelGenerator::newLabel( "blockBreak", stmt );
     500                Label breakLabel = newLabel( "blockBreak", stmt );
    507501                enclosing_control_structures.emplace_back( stmt, breakLabel );
    508502                GuardAction([this](){ enclosing_control_structures.pop_back(); } );
     
    510504}
    511505
    512 void MultiLevelExitCore::postvisit( const ast::TryStmt * stmt ) {
    513         bool isLabeled = !stmt->labels.empty();
     506void MultiLevelExitCore::postvisit( const TryStmt * stmt ) {
     507        bool isLabeled = ! stmt->labels.empty();
    514508        if ( isLabeled ) {
    515509                auto this_label = enclosing_control_structures.back().useBreakExit();
    516                 if ( !this_label.empty() ) {
     510                if ( ! this_label.empty() ) {
    517511                        break_label = this_label;
    518512                }
     
    520514}
    521515
    522 void 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>();
     516void MultiLevelExitCore::previsit( const FinallyStmt * ) {
     517        GuardAction([this, old = move( enclosing_control_structures)](){ enclosing_control_structures = move(old); });
     518        enclosing_control_structures = vector<Entry>();
    527519        GuardValue( inFinally ) = true;
    528520}
    529521
    530 const ast::Stmt * MultiLevelExitCore::mutateLoop(
    531                 const ast::Stmt * body, Entry & entry ) {
     522const Stmt * MultiLevelExitCore::mutateLoop(
     523        const Stmt * body, Entry & entry ) {
    532524        if ( entry.isBreakUsed() ) {
    533525                break_label = entry.useBreakExit();
    534526        }
    535527
     528        // if continue is used insert a continue label into the back of the body of the loop
    536529        if ( entry.isContUsed() ) {
    537                 ast::CompoundStmt * new_body = new ast::CompoundStmt( body->location );
     530                CompoundStmt * new_body = new CompoundStmt( body->location );
     531                // {}
    538532                new_body->kids.push_back( body );
     533                // {
     534                //  body
     535                // }
    539536                new_body->kids.push_back(
    540537                        labelledNullStmt( body->location, entry.useContExit() ) );
     538                // {
     539                //  body
     540                //  ContinueLabel: {}
     541                // }
    541542                return new_body;
    542543        }
     
    549550        // Remember is loop before going onto mutate the body.
    550551        // 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 );
     552        Label breakLabel = newLabel( "loopBreak", loopStmt );
     553        Label contLabel = newLabel( "loopContinue", loopStmt );
    553554        enclosing_control_structures.emplace_back( loopStmt, breakLabel, contLabel );
     555        // labels are added temporarily to see if they are used and then added permanently in postvisit if ther are used
     556        // children will tag labels as being used during their traversal which occurs before postvisit
     557
     558        // GuardAction calls the lambda after the node is done being visited
    554559        GuardAction( [this](){ enclosing_control_structures.pop_back(); } );
    555560}
     
    557562template<typename LoopNode>
    558563const LoopNode * MultiLevelExitCore::posthandleLoopStmt( const LoopNode * loopStmt ) {
    559         assert( !enclosing_control_structures.empty() );
     564        assert( ! enclosing_control_structures.empty() );
    560565        Entry & entry = enclosing_control_structures.back();
    561566        assert( entry.stmt == loopStmt );
    562567
    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 
    568 std::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.
     568        // Now check if the labels are used and add them if so.
     569        return mutate_field( loopStmt, &LoopNode::body, mutateLoop( loopStmt->body, entry ) );
     570        // this call to mutate_field compares loopStmt->body and the result of mutateLoop
     571        //              if they are the same the node isn't mutated, if they differ then the new mutated node is returned
     572        //              the stmts will only differ if a label is used
     573}
     574
     575list<ptr<Stmt>> MultiLevelExitCore::fixBlock(
     576        const list<ptr<Stmt>> & kids, bool is_case_clause ) {
     577        // Unfortunately cannot use automatic error collection.
    571578        SemanticErrorException errors;
    572579
    573         std::list<ast::ptr<ast::Stmt>> ret;
     580        list<ptr<Stmt>> ret;
    574581
    575582        // Manually visit each child.
    576         for ( const ast::ptr<ast::Stmt> & kid : kids ) {
     583        for ( const ptr<Stmt> & kid : kids ) {
    577584                if ( is_case_clause ) {
    578585                        // Once a label is seen, it's no longer a valid for fallthrough.
    579                         for ( const ast::Label & l : kid->labels ) {
     586                        for ( const Label & l : kid->labels ) {
    580587                                fallthrough_labels.erase( l );
    581588                        }
     
    588595                }
    589596
    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() ) {
     597                if ( ! break_label.empty() ) {
     598                        ret.push_back( labelledNullStmt( ret.back()->location, break_label ) );
     599                        break_label = Label( CodeLocation(), "" );
     600                }
     601        }
     602
     603        if ( ! errors.isEmpty() ) {
    598604                throw errors;
    599605        }
     
    601607}
    602608
    603 } // namespace
    604 
    605 const ast::CompoundStmt * multiLevelExitUpdate(
    606         const ast::CompoundStmt * stmt,
    607                 const LabelToStmt & labelTable ) {
     609const CompoundStmt * multiLevelExitUpdate(
     610        const CompoundStmt * stmt,
     611        const LabelToStmt & labelTable ) {
    608612        // 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 );
     613        Pass<MultiLevelExitCore> visitor( labelTable );
     614        const CompoundStmt * ret = stmt->accept( visitor );
    611615        return ret;
    612616}
    613 
    614617} // namespace ControlStruct
    615618
  • src/ControlStruct/MultiLevelExit.hpp

    r5f3ba11 rb56ad5e  
    99// Author           : Andrew Beach
    1010// Created On       : Mon Nov  1 13:49:00 2021
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Mon Nov  8 10:53:00 2021
    13 // Update Count     : 3
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Mon Jan 31 22:34:06 2022
     13// Update Count     : 6
    1414//
    1515
     
    1919
    2020namespace ast {
    21         class CompoundStmt;
    22         class Label;
    23         class Stmt;
     21class CompoundStmt;
     22class Label;
     23class Stmt;
    2424}
    2525
    2626namespace ControlStruct {
    27 
    2827using LabelToStmt = std::map<ast::Label, const ast::Stmt *>;
    2928
    30 /// Mutate a function body to handle multi-level exits.
    31 const ast::CompoundStmt * multiLevelExitUpdate(
    32         const ast::CompoundStmt *, const LabelToStmt & );
    33 
     29// Mutate a function body to handle multi-level exits.
     30const ast::CompoundStmt * multiLevelExitUpdate( const ast::CompoundStmt *, const LabelToStmt & );
    3431}
    3532
  • src/ControlStruct/module.mk

    r5f3ba11 rb56ad5e  
    1010## Author           : Richard C. Bilson
    1111## Created On       : Mon Jun  1 17:49:17 2015
    12 ## Last Modified By : Henry Xue
    13 ## Last Modified On : Tue Jul 20 04:10:50 2021
    14 ## Update Count     : 5
     12## Last Modified By : Peter A. Buhr
     13## Last Modified On : Sat Jan 29 12:04:19 2022
     14## Update Count     : 7
    1515###############################################################################
    1616
     
    2222        ControlStruct/ForExprMutator.cc \
    2323        ControlStruct/ForExprMutator.h \
     24        ControlStruct/HoistControlDecls.cpp \
     25        ControlStruct/HoistControlDecls.hpp \
    2426        ControlStruct/LabelFixer.cc \
    2527        ControlStruct/LabelFixer.h \
    2628        ControlStruct/LabelGenerator.cc \
    2729        ControlStruct/LabelGenerator.h \
     30        ControlStruct/LabelGeneratorNew.cpp \
     31        ControlStruct/LabelGeneratorNew.hpp \
    2832        ControlStruct/MLEMutator.cc \
    2933        ControlStruct/MLEMutator.h \
Note: See TracChangeset for help on using the changeset viewer.