Changeset 376c632a for src/ControlStruct
- Timestamp:
- Feb 1, 2022, 10:10:46 AM (4 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
- Children:
- 7b2c8c3c
- Parents:
- f681823 (diff), 89a5a1f (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. - Location:
- src/ControlStruct
- Files:
-
- 4 added
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ControlStruct/ExceptTranslateNew.cpp
rf681823 r376c632a 9 9 // Author : Andrew Beach 10 10 // Created On : Mon Nov 8 11:53:00 2021 11 // Last Modified By : Andrew Beach12 // Last Modified On : Mon Nov 8 16:50:00 202113 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jan 31 18:49:58 2022 13 // Update Count : 1 14 14 // 15 15 … … 22 22 23 23 namespace ControlStruct { 24 25 namespace {26 24 27 25 class TranslateThrowsCore : public ast::WithGuards { … … 128 126 } 129 127 130 } // namespace131 132 128 void translateThrows( ast::TranslationUnit & transUnit ) { 133 129 ast::Pass<TranslateThrowsCore>::run( transUnit ); -
src/ControlStruct/FixLabels.cpp
rf681823 r376c632a 9 9 // Author : Andrew Beach 10 10 // Created On : Mon Nov 1 09:39:00 2021 11 // Last Modified By : Andrew Beach12 // Last Modified On : Mon Nov 8 10:53:00 202113 // Update Count : 311 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jan 31 22:19:17 2022 13 // Update Count : 9 14 14 // 15 15 … … 20 20 #include "AST/Stmt.hpp" 21 21 #include "ControlStruct/MultiLevelExit.hpp" 22 using namespace ast; 22 23 23 24 namespace ControlStruct { 24 25 namespace { 26 27 class FixLabelsCore final : public ast::WithGuards { 25 class FixLabelsCore final : public WithGuards { 28 26 LabelToStmt labelTable; 29 public:27 public: 30 28 FixLabelsCore() : labelTable() {} 31 29 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 * ); 37 35 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 & ); 40 38 }; 41 39 42 void FixLabelsCore::previsit( const ast::FunctionDecl * ) {40 void FixLabelsCore::previsit( const FunctionDecl * ) { 43 41 GuardValue( labelTable ).clear(); 44 42 } 45 43 46 const ast::FunctionDecl * FixLabelsCore::postvisit(47 const ast::FunctionDecl * decl ) {44 const FunctionDecl * FixLabelsCore::postvisit( 45 const FunctionDecl * decl ) { 48 46 if ( nullptr == decl->stmts ) return decl; 49 47 for ( auto kvp : labelTable ) { 50 48 if ( nullptr == kvp.second ) { 51 49 SemanticError( kvp.first.location, 52 "Use of undefined label: " + kvp.first.name );50 "Use of undefined label: " + kvp.first.name ); 53 51 } 54 52 } 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 ) ); 57 55 } 58 56 59 void FixLabelsCore::previsit( const ast::Stmt * stmt ) {57 void FixLabelsCore::previsit( const Stmt * stmt ) { 60 58 if ( !stmt->labels.empty() ) { 61 59 setLabelsDef( stmt->labels, stmt ); … … 63 61 } 64 62 65 void FixLabelsCore::previsit( const ast::BranchStmt * stmt ) {63 void FixLabelsCore::previsit( const BranchStmt * stmt ) { 66 64 if ( !stmt->labels.empty() ) { 67 65 setLabelsDef( stmt->labels, stmt ); … … 72 70 } 73 71 74 void FixLabelsCore::previsit( const ast::LabelAddressExpr * expr ) {72 void FixLabelsCore::previsit( const LabelAddressExpr * expr ) { 75 73 assert( !expr->arg.empty() ); 76 74 setLabelsUsage( expr->arg ); … … 78 76 79 77 void FixLabelsCore::setLabelsDef( 80 const std::vector<ast::Label> & labels, const ast::Stmt * stmt ) {78 const std::vector<Label> & labels, const Stmt * stmt ) { 81 79 assert( !labels.empty() ); 82 80 assert( stmt ); … … 89 87 // Duplicate definition, this is an error. 90 88 SemanticError( label.location, 91 "Duplicate definition of label: " + label.name );89 "Duplicate definition of label: " + label.name ); 92 90 } else { 93 91 // Perviously used, but not defined until now. … … 98 96 99 97 // Label was used, if it is new add it to the table. 100 void FixLabelsCore::setLabelsUsage( const ast::Label & label ) {98 void FixLabelsCore::setLabelsUsage( const Label & label ) { 101 99 if ( labelTable.find( label ) == labelTable.end() ) { 102 100 labelTable[ label ] = nullptr; … … 104 102 } 105 103 106 } // namespace 107 108 void fixLabels( ast::TranslationUnit & translationUnit ) { 109 ast::Pass<FixLabelsCore>::run( translationUnit ); 104 void fixLabels( TranslationUnit & translationUnit ) { 105 Pass<FixLabelsCore>::run( translationUnit ); 110 106 } 111 112 107 } // namespace ControlStruct 113 108 -
src/ControlStruct/FixLabels.hpp
rf681823 r376c632a 9 9 // Author : Andrew Beach 10 10 // Created On : Mon Nov 1 09:36:00 2021 11 // Last Modified By : Andrew Beach12 // Last Modified On : Mon Nov 1 09:40:00 202113 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jan 31 22:18:43 2022 13 // Update Count : 2 14 14 // 15 15 … … 17 17 18 18 namespace ast { 19 19 class TranslationUnit; 20 20 } 21 21 22 22 namespace ControlStruct { 23 24 /// normalizes label definitions and generates multi-level exit labels 23 // normalizes label definitions and generates multi-level exit labels 25 24 void fixLabels( ast::TranslationUnit & translationUnit ); 26 27 25 } 28 26 -
src/ControlStruct/ForExprMutator.h
rf681823 r376c632a 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Aug 17 15:32:48 201713 // Update Count : 512 // Last Modified On : Sun Jan 30 09:14:46 2022 13 // Update Count : 6 14 14 // 15 15 … … 24 24 class ForExprMutator { 25 25 public: 26 Statement * postmutate( IfStmt * );27 Statement * postmutate( ForStmt * );28 Statement * postmutate( WhileStmt * );26 Statement * postmutate( IfStmt * ); 27 Statement * postmutate( ForStmt * ); 28 Statement * postmutate( WhileStmt * ); 29 29 }; 30 30 } // namespace ControlStruct -
src/ControlStruct/LabelFixer.cc
rf681823 r376c632a 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Andrew Beach12 // Last Modified On : Tue Jan 21 10:32:00 202013 // Update Count : 16 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jan 31 22:28:31 2022 13 // Update Count : 161 14 14 // 15 15 … … 27 27 28 28 namespace ControlStruct { 29 bool LabelFixer::Entry::insideLoop() { 30 return ( dynamic_cast< ForStmt * > ( definition ) || 31 dynamic_cast< WhileStmt * > ( definition ) ); 29 bool LabelFixer::Entry::insideLoop() { 30 return ( dynamic_cast< ForStmt * > ( definition ) || 31 dynamic_cast< WhileStmt * > ( definition ) ); 32 } 33 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(); 32 128 } 33 129 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 } 132 132 } // namespace ControlStruct 133 133 -
src/ControlStruct/LabelFixer.h
rf681823 r376c632a 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Jul 22 09:17:24 201713 // Update Count : 3 412 // Last Modified On : Mon Jan 31 22:28:04 2022 13 // Update Count : 35 14 14 // 15 15 … … 26 26 27 27 namespace ControlStruct { 28 /// normalizes label definitions and generates multi-level exit labels29 28 // normalizes label definitions and generates multi-level exit labels 29 class LabelGenerator; 30 30 31 32 33 31 class LabelFixer final : public WithGuards { 32 public: 33 LabelFixer( LabelGenerator *gen = 0 ); 34 34 35 35 std::map < Label, Statement * > *resolveJumps() throw ( SemanticErrorException ); 36 36 37 38 39 37 // Declarations 38 void previsit( FunctionDecl *functionDecl ); 39 void postvisit( FunctionDecl *functionDecl ); 40 40 41 42 43 41 // Statements 42 void previsit( Statement *stmt ); 43 void previsit( BranchStmt *branchStmt ); 44 44 45 46 45 // Expressions 46 void previsit( LabelAddressExpr *addrExpr ); 47 47 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; } 51 64 52 65 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 }; 58 69 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 }; 73 73 } // namespace ControlStruct 74 74 -
src/ControlStruct/LabelGenerator.cc
rf681823 r376c632a 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Andrew Beach12 // Last Modified On : Mon Nov 8 10:18:00 202113 // Update Count : 1711 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jan 31 22:30:26 2022 13 // Update Count : 28 14 14 // 15 15 … … 17 17 #include <sstream> // for ostringstream 18 18 #include <list> // for list 19 using namespace std; 19 20 20 21 #include "LabelGenerator.h" 21 22 22 #include "AST/Attribute.hpp"23 #include "AST/Label.hpp"24 #include "AST/Stmt.hpp"25 23 #include "SynTree/Attribute.h" // for Attribute 26 24 #include "SynTree/Label.h" // for Label, operator<< … … 28 26 29 27 namespace ControlStruct { 30 31 28 int LabelGenerator::current = 0; 32 29 LabelGenerator * LabelGenerator::labelGenerator = nullptr; 33 30 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; 31 LabelGenerator * LabelGenerator::getGenerator() { 32 if ( LabelGenerator::labelGenerator == 0 ) 33 LabelGenerator::labelGenerator = new LabelGenerator(); 34 return labelGenerator; 64 35 } 65 36 37 Label 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 } 66 48 } // namespace ControlStruct 67 49 68 50 // Local Variables: // 69 // tab-width: 4 //70 51 // mode: c++ // 71 // compile-command: "make install" //72 52 // End: // -
src/ControlStruct/LabelGenerator.h
rf681823 r376c632a 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Andrew Beach12 // Last Modified On : Mon Nov 8 10:16:00 202113 // Update Count : 811 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jan 31 22:30:10 2022 13 // Update Count : 16 14 14 // 15 15 … … 21 21 22 22 class Statement; 23 23 24 namespace ast { 24 25 25 class Stmt; 26 class Label; 26 27 } 27 28 28 29 namespace ControlStruct { 29 30 30 class LabelGenerator { 31 31 static int current; 32 32 static LabelGenerator *labelGenerator; 33 protected:33 protected: 34 34 LabelGenerator() {} 35 public:35 public: 36 36 static LabelGenerator *getGenerator(); 37 37 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--; }41 38 }; 42 43 39 } // namespace ControlStruct 44 40 -
src/ControlStruct/MultiLevelExit.cpp
rf681823 r376c632a 9 9 // Author : Andrew Beach 10 10 // Created On : Mon Nov 1 13:48:00 2021 11 // Last Modified By : Andrew Beach12 // Last Modified On : Mon Nov 8 10:56:00 202113 // Update Count : 2 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jan 31 22:35:08 2022 13 // Update Count : 28 14 14 // 15 15 … … 18 18 #include "AST/Pass.hpp" 19 19 #include "AST/Stmt.hpp" 20 #include " ControlStruct/LabelGenerator.h"20 #include "LabelGeneratorNew.hpp" 21 21 22 22 #include <set> 23 using namespace std; 24 using namespace ast; 23 25 24 26 namespace ControlStruct { 25 26 namespace {27 28 27 class Entry { 29 public:30 const ast::Stmt * stmt;31 private:28 public: 29 const Stmt * stmt; 30 private: 32 31 // Organized like a manual ADT. Avoids creating a bunch of dead data. 33 32 struct Target { 34 ast::Label label;33 Label label; 35 34 bool used = false; 36 Target( const ast::Label & label ) : label( label ) {}35 Target( const Label & label ) : label( label ) {} 37 36 Target() : label( CodeLocation() ) {} 38 37 }; … … 41 40 42 41 enum Kind { 43 ForStmt , WhileStmt, CompoundStmt, IfStmt, CaseStmt, SwitchStmt, TryStmt42 ForStmtK, WhileStmtK, CompoundStmtK, IfStmtK, CaseStmtK, SwitchStmtK, TryStmtK 44 43 } kind; 45 44 46 45 bool fallDefaultValid = true; 47 46 48 static ast::Label & useTarget( Target & target ) {47 static Label & useTarget( Target & target ) { 49 48 target.used = true; 50 49 return target.label; 51 50 } 52 51 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; }52 public: 53 Entry( const ForStmt * stmt, Label breakExit, Label contExit ) : 54 stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( ForStmtK ) {} 55 Entry( const WhileStmt * stmt, Label breakExit, Label contExit ) : 56 stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( WhileStmtK ) {} 57 Entry( const CompoundStmt *stmt, Label breakExit ) : 58 stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( CompoundStmtK ) {} 59 Entry( const IfStmt *stmt, Label breakExit ) : 60 stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( IfStmtK ) {} 61 Entry( const CaseStmt *stmt, Label fallExit ) : 62 stmt( stmt ), firstTarget( fallExit ), secondTarget(), kind( CaseStmtK ) {} 63 Entry( const SwitchStmt *stmt, Label breakExit, Label fallDefaultExit ) : 64 stmt( stmt ), firstTarget( breakExit ), secondTarget( fallDefaultExit ), kind( SwitchStmtK ) {} 65 Entry( const TryStmt *stmt, Label breakExit ) : 66 stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( TryStmtK ) {} 67 68 bool isContTarget() const { return kind <= WhileStmtK; } 69 bool isBreakTarget() const { return kind != CaseStmtK; } 70 bool isFallTarget() const { return kind == CaseStmtK; } 71 bool isFallDefaultTarget() const { return kind == SwitchStmtK; } 72 73 Label useContExit() { assert( kind <= WhileStmtK ); 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 bool isContUsed() const { assert( kind <= WhileStmtK ); return secondTarget.used; } 79 bool isBreakUsed() const { assert( kind != CaseStmtK ); return firstTarget.used; } 80 bool isFallUsed() const { assert( kind == CaseStmtK ); return firstTarget.used; } 81 bool isFallDefaultUsed() const { assert( kind == SwitchStmtK ); return secondTarget.used; } 83 82 void seenDefault() { fallDefaultValid = false; } 84 83 bool isFallDefaultValid() const { return fallDefaultValid; } 85 84 }; 86 85 87 // Helper predicates used in std::find_if calls (it doesn't take methods):86 // Helper predicates used in find_if calls (it doesn't take methods): 88 87 bool isBreakTarget( const Entry & entry ) { 89 88 return entry.isBreakTarget(); … … 103 102 104 103 struct MultiLevelExitCore final : 105 public ast::WithVisitorRef<MultiLevelExitCore>,106 public ast::WithShortCircuiting, public ast::WithGuards {104 public WithVisitorRef<MultiLevelExitCore>, 105 public WithShortCircuiting, public WithGuards { 107 106 MultiLevelExitCore( const LabelToStmt & lt ); 108 107 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& );108 void previsit( const FunctionDecl * ); 109 110 const CompoundStmt * previsit( const CompoundStmt * ); 111 const BranchStmt * postvisit( const BranchStmt * ); 112 void previsit( const WhileStmt * ); 113 const WhileStmt * postvisit( const WhileStmt * ); 114 void previsit( const ForStmt * ); 115 const ForStmt * postvisit( const ForStmt * ); 116 const CaseStmt * previsit( const CaseStmt * ); 117 void previsit( const IfStmt * ); 118 const IfStmt * postvisit( const IfStmt * ); 119 void previsit( const SwitchStmt * ); 120 const SwitchStmt * postvisit( const SwitchStmt * ); 121 void previsit( const ReturnStmt * ); 122 void previsit( const TryStmt * ); 123 void postvisit( const TryStmt * ); 124 void previsit( const FinallyStmt * ); 125 126 const Stmt * mutateLoop( const Stmt * body, Entry& ); 128 127 129 128 const LabelToStmt & target_table; 130 s td::set<ast::Label> fallthrough_labels;131 std::vector<Entry> enclosing_control_structures;132 ast::Label break_label;129 set<Label> fallthrough_labels; 130 vector<Entry> enclosing_control_structures; 131 Label break_label; 133 132 bool inFinally; 134 133 … … 138 137 const LoopNode * posthandleLoopStmt( const LoopNode * loopStmt ); 139 138 140 std::list<ast::ptr<ast::Stmt>> fixBlock(141 const std::list<ast::ptr<ast::Stmt>> & kids, bool caseClause );139 list<ptr<Stmt>> fixBlock( 140 const list<ptr<Stmt>> & kids, bool caseClause ); 142 141 143 142 template<typename UnaryPredicate> 144 143 auto findEnclosingControlStructure( UnaryPredicate pred ) { 145 return std::find_if( enclosing_control_structures.rbegin(),146 enclosing_control_structures.rend(), pred );144 return find_if( enclosing_control_structures.rbegin(), 145 enclosing_control_structures.rend(), pred ); 147 146 } 148 147 }; 149 148 150 ast::NullStmt * labelledNullStmt(151 const CodeLocation & cl, const ast::Label & label ) {152 return new ast::NullStmt( cl, std::vector<ast::Label>{ label } );149 NullStmt * labelledNullStmt( 150 const CodeLocation & cl, const Label & label ) { 151 return new NullStmt( cl, vector<Label>{ label } ); 153 152 } 154 153 … … 158 157 {} 159 158 160 void MultiLevelExitCore::previsit( const ast::FunctionDecl * ) {159 void MultiLevelExitCore::previsit( const FunctionDecl * ) { 161 160 visit_children = false; 162 161 } 163 162 164 const ast::CompoundStmt * MultiLevelExitCore::previsit(165 const ast::CompoundStmt * stmt ) {163 const CompoundStmt * MultiLevelExitCore::previsit( 164 const CompoundStmt * stmt ) { 166 165 visit_children = false; 167 166 bool isLabeled = !stmt->labels.empty(); 168 167 if ( isLabeled ) { 169 ast::Label breakLabel = LabelGenerator::newLabel( "blockBreak", stmt );168 Label breakLabel = newLabel( "blockBreak", stmt ); 170 169 enclosing_control_structures.emplace_back( stmt, breakLabel ); 171 170 GuardAction( [this]() { enclosing_control_structures.pop_back(); } ); 172 171 } 173 172 174 auto mutStmt = ast::mutate( stmt );173 auto mutStmt = mutate( stmt ); 175 174 // A child statement may set the break label. 176 mutStmt->kids = std::move( fixBlock( stmt->kids, false ) );175 mutStmt->kids = move( fixBlock( stmt->kids, false ) ); 177 176 178 177 if ( isLabeled ) { … … 187 186 188 187 size_t getUnusedIndex( 189 const ast::Stmt * stmt, const ast::Label & originalTarget ) {188 const Stmt * stmt, const Label & originalTarget ) { 190 189 const size_t size = stmt->labels.size(); 191 190 192 // If the label is empty, we can skip adding the unused attribute:193 191 // If the label is empty, do not add unused attribute. 192 if ( originalTarget.empty() ) return size; 194 193 195 194 // Search for a label that matches the originalTarget. 196 195 for ( size_t i = 0 ; i < size ; ++i ) { 197 const ast::Label & label = stmt->labels[i];196 const Label & label = stmt->labels[i]; 198 197 if ( label == originalTarget ) { 199 for ( const ast::Attribute * attr : label.attributes ) {198 for ( const Attribute * attr : label.attributes ) { 200 199 if ( attr->name == "unused" ) return size; 201 200 } … … 204 203 } 205 204 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 ) {205 originalTarget.name.c_str(), toString( stmt ).c_str() ); 206 } 207 208 const Stmt * addUnused( 209 const Stmt * stmt, const Label & originalTarget ) { 211 210 size_t i = getUnusedIndex( stmt, originalTarget ); 212 211 if ( i == stmt->labels.size() ) { 213 212 return stmt; 214 213 } 215 ast::Stmt * mutStmt = ast::mutate( stmt );216 mutStmt->labels[i].attributes.push_back( new ast::Attribute( "unused" ) );214 Stmt * mutStmt = mutate( stmt ); 215 mutStmt->labels[i].attributes.push_back( new Attribute( "unused" ) ); 217 216 return mutStmt; 218 217 } 219 218 220 const ast::BranchStmt * MultiLevelExitCore::postvisit( const ast::BranchStmt * stmt ) {221 std::vector<Entry>::reverse_iterator targetEntry =219 const BranchStmt * MultiLevelExitCore::postvisit( const BranchStmt * stmt ) { 220 vector<Entry>::reverse_iterator targetEntry = 222 221 enclosing_control_structures.rend(); 223 222 switch ( stmt->kind ) { 224 case ast::BranchStmt::Goto:223 case BranchStmt::Goto: 225 224 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: 225 case BranchStmt::Continue: 226 case BranchStmt::Break: { 227 bool isContinue = stmt->kind == BranchStmt::Continue; 228 // Handle unlabeled break and continue. 229 if ( stmt->target.empty() ) { 230 if ( isContinue ) { 231 targetEntry = findEnclosingControlStructure( isContinueTarget ); 232 } else { 233 if ( enclosing_control_structures.empty() ) { 234 SemanticError( stmt->location, 235 "'break' outside a loop, 'switch', or labelled block" ); 236 } 237 targetEntry = findEnclosingControlStructure( isBreakTarget ); 238 } 239 // Handle labeled break and continue. 240 } else { 241 // Lookup label in table to find attached control structure. 242 targetEntry = findEnclosingControlStructure( 243 [ targetStmt = target_table.at(stmt->target) ](auto entry){ 244 return entry.stmt == targetStmt; 245 } ); 246 } 247 // Ensure that selected target is valid. 248 if ( targetEntry == enclosing_control_structures.rend() || ( isContinue && !isContinueTarget( *targetEntry ) ) ) { 249 SemanticError( stmt->location, toString( (isContinue ? "'continue'" : "'break'"), 250 " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "), 251 stmt->originalTarget ) ); 252 } 253 break; 254 } 255 case BranchStmt::FallThrough: { 256 targetEntry = findEnclosingControlStructure( isFallthroughTarget ); 257 // Check that target is valid. 258 if ( targetEntry == enclosing_control_structures.rend() ) { 259 SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" ); 260 } 261 if ( !stmt->target.empty() ) { 262 // Labelled fallthrough: target must be a valid fallthough label. 263 if ( !fallthrough_labels.count( stmt->target ) ) { 264 SemanticError( stmt->location, toString( "'fallthrough' target must be a later case statement: ", 265 stmt->originalTarget ) ); 266 } 267 return new BranchStmt( 268 stmt->location, BranchStmt::Goto, stmt->originalTarget ); 269 } 270 break; 271 } 272 case BranchStmt::FallThroughDefault: { 273 targetEntry = findEnclosingControlStructure( isFallthroughDefaultTarget ); 274 275 // Check if in switch or choose statement. 276 if ( targetEntry == enclosing_control_structures.rend() ) { 277 SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" ); 278 } 279 280 // Check if switch or choose has default clause. 281 auto switchStmt = strict_dynamic_cast< const SwitchStmt * >( targetEntry->stmt ); 282 bool foundDefault = false; 283 for ( auto subStmt : switchStmt->stmts ) { 284 const CaseStmt * caseStmt = subStmt.strict_as<CaseStmt>(); 285 if ( caseStmt->isDefault() ) { 286 foundDefault = true; 287 break; 288 } 289 } 290 if ( ! foundDefault ) { 291 SemanticError( stmt->location, "'fallthrough default' must be enclosed in a 'switch' or 'choose'" 292 "control structure with a 'default' clause" ); 293 } 294 break; 295 } 296 default: 300 297 assert( false ); 301 298 } 302 299 303 300 // Branch error checks: get the appropriate label name: 304 // (This label will always bereplaced.)305 ast::Label exitLabel( CodeLocation(), "" );301 // (This label is always replaced.) 302 Label exitLabel( CodeLocation(), "" ); 306 303 switch ( stmt->kind ) { 307 case ast::BranchStmt::Break:304 case BranchStmt::Break: 308 305 assert( !targetEntry->useBreakExit().empty() ); 309 306 exitLabel = targetEntry->useBreakExit(); 310 307 break; 311 case ast::BranchStmt::Continue:308 case BranchStmt::Continue: 312 309 assert( !targetEntry->useContExit().empty() ); 313 310 exitLabel = targetEntry->useContExit(); 314 311 break; 315 case ast::BranchStmt::FallThrough:312 case BranchStmt::FallThrough: 316 313 assert( !targetEntry->useFallExit().empty() ); 317 314 exitLabel = targetEntry->useFallExit(); 318 315 break; 319 case ast::BranchStmt::FallThroughDefault:316 case BranchStmt::FallThroughDefault: 320 317 assert( !targetEntry->useFallDefaultExit().empty() ); 321 318 exitLabel = targetEntry->useFallDefaultExit(); 322 319 // Check that fallthrough default comes before the default clause. 323 320 if ( !targetEntry->isFallDefaultValid() ) { 324 SemanticError( stmt->location, 325 "'fallthrough default' must precede the 'default' clause" ); 321 SemanticError( stmt->location, "'fallthrough default' must precede the 'default' clause" ); 326 322 } 327 323 break; 328 default:324 default: 329 325 assert(0); 330 326 } … … 333 329 targetEntry->stmt = addUnused( targetEntry->stmt, stmt->originalTarget ); 334 330 335 // Replace this with agoto 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 ) {331 // Replace with goto to make later passes more uniform. 332 return new BranchStmt( stmt->location, BranchStmt::Goto, exitLabel ); 333 } 334 335 void MultiLevelExitCore::previsit( const WhileStmt * stmt ) { 340 336 return prehandleLoopStmt( stmt ); 341 337 } 342 338 343 const ast::WhileStmt * MultiLevelExitCore::postvisit( const ast::WhileStmt * stmt ) {339 const WhileStmt * MultiLevelExitCore::postvisit( const WhileStmt * stmt ) { 344 340 return posthandleLoopStmt( stmt ); 345 341 } 346 342 347 void MultiLevelExitCore::previsit( const ast::ForStmt * stmt ) {343 void MultiLevelExitCore::previsit( const ForStmt * stmt ) { 348 344 return prehandleLoopStmt( stmt ); 349 345 } 350 346 351 const ast::ForStmt * MultiLevelExitCore::postvisit( const ast::ForStmt * stmt ) {347 const ForStmt * MultiLevelExitCore::postvisit( const ForStmt * stmt ) { 352 348 return posthandleLoopStmt( stmt ); 353 349 } … … 355 351 // Mimic what the built-in push_front would do anyways. It is O(n). 356 352 void push_front( 357 std::vector<ast::ptr<ast::Stmt>> & vec, const ast::Stmt * element ) {353 vector<ptr<Stmt>> & vec, const Stmt * element ) { 358 354 vec.emplace_back( nullptr ); 359 355 for ( size_t i = vec.size() - 1 ; 0 < i ; --i ) { 360 vec[ i ] = std::move( vec[ i - 1 ] );356 vec[ i ] = move( vec[ i - 1 ] ); 361 357 } 362 358 vec[ 0 ] = element; 363 359 } 364 360 365 const ast::CaseStmt * MultiLevelExitCore::previsit( const ast::CaseStmt * stmt ) {361 const CaseStmt * MultiLevelExitCore::previsit( const CaseStmt * stmt ) { 366 362 visit_children = false; 367 363 368 // If it is the default, mark the default asseen.364 // If default, mark seen. 369 365 if ( stmt->isDefault() ) { 370 366 assert( !enclosing_control_structures.empty() ); … … 373 369 374 370 // The cond may not exist, but if it does update it now. 375 visitor->maybe_accept( stmt, & ast::CaseStmt::cond );371 visitor->maybe_accept( stmt, &CaseStmt::cond ); 376 372 377 373 // 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() ) {374 CaseStmt * mutStmt = mutate( stmt ); 375 376 Label fallLabel = newLabel( "fallThrough", stmt ); 377 if ( ! mutStmt->stmts.empty() ) { 382 378 // Ensure that the stack isn't corrupted by exceptions in fixBlock. 383 379 auto guard = makeFuncGuard( 384 380 [&](){ enclosing_control_structures.emplace_back( mutStmt, fallLabel ); }, 385 381 [this](){ enclosing_control_structures.pop_back(); } 386 );382 ); 387 383 388 384 // These should already be in a block. 389 auto block = ast::mutate( mutStmt->stmts.front().strict_as<ast::CompoundStmt>() );385 auto block = mutate( mutStmt->stmts.front().strict_as<CompoundStmt>() ); 390 386 block->kids = fixBlock( block->kids, true ); 391 387 392 388 // Add fallthrough label if necessary. 393 assert( ! enclosing_control_structures.empty() );389 assert( ! enclosing_control_structures.empty() ); 394 390 Entry & entry = enclosing_control_structures.back(); 395 391 if ( entry.isFallUsed() ) { … … 398 394 } 399 395 } 400 assert( ! enclosing_control_structures.empty() );396 assert( ! enclosing_control_structures.empty() ); 401 397 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() );398 assertf( dynamic_cast< const SwitchStmt * >( entry.stmt ), 399 "Control structure enclosing a case clause must be a switch, but is: %s", 400 toString( entry.stmt ).c_str() ); 405 401 if ( mutStmt->isDefault() ) { 406 402 if ( entry.isFallDefaultUsed() ) { 407 403 // Add fallthrough default label if necessary. 408 404 push_front( mutStmt->stmts, labelledNullStmt( 409 stmt->location, entry.useFallDefaultExit()410 ) );405 stmt->location, entry.useFallDefaultExit() 406 ) ); 411 407 } 412 408 } … … 414 410 } 415 411 416 void MultiLevelExitCore::previsit( const ast::IfStmt * stmt ) {412 void MultiLevelExitCore::previsit( const IfStmt * stmt ) { 417 413 bool labeledBlock = !stmt->labels.empty(); 418 414 if ( labeledBlock ) { 419 ast::Label breakLabel = LabelGenerator::newLabel( "blockBreak", stmt );415 Label breakLabel = newLabel( "blockBreak", stmt ); 420 416 enclosing_control_structures.emplace_back( stmt, breakLabel ); 421 417 GuardAction( [this](){ enclosing_control_structures.pop_back(); } ); … … 423 419 } 424 420 425 const ast::IfStmt * MultiLevelExitCore::postvisit( const ast::IfStmt * stmt ) {421 const IfStmt * MultiLevelExitCore::postvisit( const IfStmt * stmt ) { 426 422 bool labeledBlock = !stmt->labels.empty(); 427 423 if ( labeledBlock ) { … … 434 430 } 435 431 436 bool isDefaultCase( const ast::ptr<ast::Stmt> & stmt ) {437 const ast::CaseStmt * caseStmt = stmt.strict_as<ast::CaseStmt>();432 bool isDefaultCase( const ptr<Stmt> & stmt ) { 433 const CaseStmt * caseStmt = stmt.strict_as<CaseStmt>(); 438 434 return caseStmt->isDefault(); 439 435 } 440 436 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 = defaultCase448 ? LabelGenerator::newLabel( "fallThroughDefault", defaultCase )449 : ast::Label( stmt->location, "" );437 void MultiLevelExitCore::previsit( const SwitchStmt * stmt ) { 438 Label label = newLabel( "switchBreak", stmt ); 439 auto it = find_if( stmt->stmts.rbegin(), stmt->stmts.rend(), isDefaultCase ); 440 441 const CaseStmt * defaultCase = it != stmt->stmts.rend() 442 ? (it)->strict_as<CaseStmt>() : nullptr; 443 Label defaultLabel = defaultCase 444 ? newLabel( "fallThroughDefault", defaultCase ) 445 : Label( stmt->location, "" ); 450 446 enclosing_control_structures.emplace_back( stmt, label, defaultLabel ); 451 447 GuardAction( [this]() { enclosing_control_structures.pop_back(); } ); 452 448 453 449 // Collect valid labels for fallthrough. It starts with all labels at 454 // this level, then remove d as we see them intraversal.455 for ( const ast::Stmt * stmt : stmt->stmts ) {456 auto * caseStmt = strict_dynamic_cast< const ast::CaseStmt * >( stmt );450 // this level, then remove as each is seen during traversal. 451 for ( const Stmt * stmt : stmt->stmts ) { 452 auto * caseStmt = strict_dynamic_cast< const CaseStmt * >( stmt ); 457 453 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 ) { 461 457 fallthrough_labels.insert( l ); 462 458 } … … 465 461 } 466 462 467 const ast::SwitchStmt * MultiLevelExitCore::postvisit( const ast::SwitchStmt * stmt ) {463 const SwitchStmt * MultiLevelExitCore::postvisit( const SwitchStmt * stmt ) { 468 464 assert( !enclosing_control_structures.empty() ); 469 465 Entry & entry = enclosing_control_structures.back(); 470 466 assert( entry.stmt == stmt ); 471 467 472 // Only run if we needto generate the break label.468 // Only run to generate the break label. 473 469 if ( entry.isBreakUsed() ) { 474 470 // To keep the switch statements uniform (all direct children of a 475 471 // SwitchStmt should be CastStmts), append the exit label and break 476 472 // to the last case, create a default case is there are no cases. 477 ast::SwitchStmt * mutStmt = ast::mutate( stmt );473 SwitchStmt * mutStmt = mutate( stmt ); 478 474 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 );475 mutStmt->stmts.push_back( new CaseStmt( 476 mutStmt->location, nullptr, {} )); 477 } 478 479 auto caseStmt = mutStmt->stmts.back().strict_as<CaseStmt>(); 480 auto mutCase = mutate( caseStmt ); 485 481 mutStmt->stmts.back() = mutCase; 486 482 487 ast::Label label( mutCase->location, "breakLabel" );488 auto branch = new ast::BranchStmt( mutCase->location, ast::BranchStmt::Break, label );483 Label label( mutCase->location, "breakLabel" ); 484 auto branch = new BranchStmt( mutCase->location, BranchStmt::Break, label ); 489 485 branch->labels.push_back( entry.useBreakExit() ); 490 486 mutCase->stmts.push_back( branch ); … … 495 491 } 496 492 497 void MultiLevelExitCore::previsit( const ast::ReturnStmt * stmt ) {493 void MultiLevelExitCore::previsit( const ReturnStmt * stmt ) { 498 494 if ( inFinally ) { 499 495 SemanticError( stmt->location, "'return' may not appear in a finally clause" ); … … 501 497 } 502 498 503 void MultiLevelExitCore::previsit( const ast::TryStmt * stmt ) {499 void MultiLevelExitCore::previsit( const TryStmt * stmt ) { 504 500 bool isLabeled = !stmt->labels.empty(); 505 501 if ( isLabeled ) { 506 ast::Label breakLabel = LabelGenerator::newLabel( "blockBreak", stmt );502 Label breakLabel = newLabel( "blockBreak", stmt ); 507 503 enclosing_control_structures.emplace_back( stmt, breakLabel ); 508 504 GuardAction([this](){ enclosing_control_structures.pop_back(); } ); … … 510 506 } 511 507 512 void MultiLevelExitCore::postvisit( const ast::TryStmt * stmt ) {508 void MultiLevelExitCore::postvisit( const TryStmt * stmt ) { 513 509 bool isLabeled = !stmt->labels.empty(); 514 510 if ( isLabeled ) { … … 520 516 } 521 517 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>();518 void MultiLevelExitCore::previsit( const FinallyStmt * ) { 519 GuardAction([this, old = move(enclosing_control_structures)](){ 520 enclosing_control_structures = move(old); 521 }); 522 enclosing_control_structures = vector<Entry>(); 527 523 GuardValue( inFinally ) = true; 528 524 } 529 525 530 const ast::Stmt * MultiLevelExitCore::mutateLoop(531 const ast::Stmt * body, Entry & entry ) {526 const Stmt * MultiLevelExitCore::mutateLoop( 527 const Stmt * body, Entry & entry ) { 532 528 if ( entry.isBreakUsed() ) { 533 529 break_label = entry.useBreakExit(); … … 535 531 536 532 if ( entry.isContUsed() ) { 537 ast::CompoundStmt * new_body = new ast::CompoundStmt( body->location );533 CompoundStmt * new_body = new CompoundStmt( body->location ); 538 534 new_body->kids.push_back( body ); 539 535 new_body->kids.push_back( … … 549 545 // Remember is loop before going onto mutate the body. 550 546 // 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 );547 Label breakLabel = newLabel( "loopBreak", loopStmt ); 548 Label contLabel = newLabel( "loopContinue", loopStmt ); 553 549 enclosing_control_structures.emplace_back( loopStmt, breakLabel, contLabel ); 554 550 GuardAction( [this](){ enclosing_control_structures.pop_back(); } ); … … 561 557 assert( entry.stmt == loopStmt ); 562 558 563 // Now wecheck if the labels are used and add them if so.564 return ast::mutate_field(559 // Now check if the labels are used and add them if so. 560 return mutate_field( 565 561 loopStmt, &LoopNode::body, mutateLoop( loopStmt->body, entry ) ); 566 562 } 567 563 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.564 list<ptr<Stmt>> MultiLevelExitCore::fixBlock( 565 const list<ptr<Stmt>> & kids, bool is_case_clause ) { 566 // Unfortunately cannot use automatic error collection. 571 567 SemanticErrorException errors; 572 568 573 std::list<ast::ptr<ast::Stmt>> ret;569 list<ptr<Stmt>> ret; 574 570 575 571 // Manually visit each child. 576 for ( const ast::ptr<ast::Stmt> & kid : kids ) {572 for ( const ptr<Stmt> & kid : kids ) { 577 573 if ( is_case_clause ) { 578 574 // Once a label is seen, it's no longer a valid for fallthrough. 579 for ( const ast::Label & l : kid->labels ) {575 for ( const Label & l : kid->labels ) { 580 576 fallthrough_labels.erase( l ); 581 577 } … … 591 587 ret.push_back( 592 588 labelledNullStmt( ret.back()->location, break_label ) ); 593 break_label = ast::Label( CodeLocation(), "" );589 break_label = Label( CodeLocation(), "" ); 594 590 } 595 591 } … … 601 597 } 602 598 603 } // namespace 604 605 const ast::CompoundStmt * multiLevelExitUpdate( 606 const ast::CompoundStmt * stmt, 607 const LabelToStmt & labelTable ) { 599 const CompoundStmt * multiLevelExitUpdate( 600 const CompoundStmt * stmt, 601 const LabelToStmt & labelTable ) { 608 602 // 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 );603 Pass<MultiLevelExitCore> visitor( labelTable ); 604 const CompoundStmt * ret = stmt->accept( visitor ); 611 605 return ret; 612 606 } 613 614 607 } // namespace ControlStruct 615 608 -
src/ControlStruct/MultiLevelExit.hpp
rf681823 r376c632a 9 9 // Author : Andrew Beach 10 10 // Created On : Mon Nov 1 13:49:00 2021 11 // Last Modified By : Andrew Beach12 // Last Modified On : Mon Nov 8 10:53:00 202113 // Update Count : 311 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jan 31 22:34:06 2022 13 // Update Count : 6 14 14 // 15 15 … … 19 19 20 20 namespace ast { 21 22 23 21 class CompoundStmt; 22 class Label; 23 class Stmt; 24 24 } 25 25 26 26 namespace ControlStruct { 27 28 27 using LabelToStmt = std::map<ast::Label, const ast::Stmt *>; 29 28 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. 30 const ast::CompoundStmt * multiLevelExitUpdate( const ast::CompoundStmt *, const LabelToStmt & ); 34 31 } 35 32 -
src/ControlStruct/module.mk
rf681823 r376c632a 10 10 ## Author : Richard C. Bilson 11 11 ## Created On : Mon Jun 1 17:49:17 2015 12 ## Last Modified By : Henry Xue13 ## Last Modified On : Tue Jul 20 04:10:50 202114 ## Update Count : 512 ## Last Modified By : Peter A. Buhr 13 ## Last Modified On : Sat Jan 29 12:04:19 2022 14 ## Update Count : 7 15 15 ############################################################################### 16 16 … … 22 22 ControlStruct/ForExprMutator.cc \ 23 23 ControlStruct/ForExprMutator.h \ 24 ControlStruct/HoistControlDecls.cpp \ 25 ControlStruct/HoistControlDecls.hpp \ 24 26 ControlStruct/LabelFixer.cc \ 25 27 ControlStruct/LabelFixer.h \ 26 28 ControlStruct/LabelGenerator.cc \ 27 29 ControlStruct/LabelGenerator.h \ 30 ControlStruct/LabelGeneratorNew.cpp \ 31 ControlStruct/LabelGeneratorNew.hpp \ 28 32 ControlStruct/MLEMutator.cc \ 29 33 ControlStruct/MLEMutator.h \
Note:
See TracChangeset
for help on using the changeset viewer.