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