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