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