Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ControlStruct/MultiLevelExit.cpp

    r2f52b18 r3b0bc16  
    1010// Created On       : Mon Nov  1 13:48:00 2021
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Feb  2 23:07:54 2022
    13 // Update Count     : 33
     12// Last Modified On : Tue Feb  1 18:48:47 2022
     13// Update Count     : 29
    1414//
    1515
     
    4949                return target.label;
    5050        }
     51
    5152  public:
    5253        Entry( const ForStmt * stmt, Label breakExit, Label contExit ) :
     
    167168
    168169        // if the stmt is labelled then generate a label to check in postvisit if the label is used
    169         bool isLabeled = ! stmt->labels.empty();
     170        bool isLabeled = !stmt->labels.empty();
    170171        if ( isLabeled ) {
    171172                Label breakLabel = newLabel( "blockBreak", stmt );
     
    179180
    180181        if ( isLabeled ) {
    181                 assert( ! enclosing_control_structures.empty() );
     182                assert( !enclosing_control_structures.empty() );
    182183                Entry & entry = enclosing_control_structures.back();
    183                 if ( ! entry.useBreakExit().empty() ) {
     184                if ( !entry.useBreakExit().empty() ) {
    184185                        break_label = entry.useBreakExit();
    185186                }
     
    205206                }
    206207        }
    207         assertf( false, "CFA internal error: could not find label '%s' on statement %s",
     208        assertf( false, "Could not find label '%s' on statement %s",
    208209                         originalTarget.name.c_str(), toString( stmt ).c_str() );
    209210}
     
    253254                  }
    254255                  // Ensure that selected target is valid.
    255                   if ( targetEntry == enclosing_control_structures.rend() || ( isContinue && ! isContinueTarget( *targetEntry ) ) ) {
     256                  if ( targetEntry == enclosing_control_structures.rend() || ( isContinue && !isContinueTarget( *targetEntry ) ) ) {
    256257                          SemanticError( stmt->location, toString( (isContinue ? "'continue'" : "'break'"),
    257258                                                        " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "),
     
    267268                          SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" );
    268269                  }
    269                   if ( ! stmt->target.empty() ) {
     270                  if ( !stmt->target.empty() ) {
    270271                          // Labelled fallthrough: target must be a valid fallthough label.
    271                           if ( ! fallthrough_labels.count( stmt->target ) ) {
     272                          if ( !fallthrough_labels.count( stmt->target ) ) {
    272273                                  SemanticError( stmt->location, toString( "'fallthrough' target must be a later case statement: ",
    273274                                                                                                                   stmt->originalTarget ) );
    274275                          }
    275                           return new BranchStmt( stmt->location, BranchStmt::Goto, stmt->originalTarget );
     276                          return new BranchStmt(
     277                                  stmt->location, BranchStmt::Goto, stmt->originalTarget );
    276278                  }
    277279                  break;
     
    305307        }
    306308
    307         // Branch error checks: get the appropriate label name, which is always replaced.
     309        // Branch error checks: get the appropriate label name:
     310        // (This label is always replaced.)
    308311        Label exitLabel( CodeLocation(), "" );
    309312        switch ( stmt->kind ) {
    310313          case BranchStmt::Break:
    311                 assert( ! targetEntry->useBreakExit().empty() );
     314                assert( !targetEntry->useBreakExit().empty() );
    312315                exitLabel = targetEntry->useBreakExit();
    313316                break;
    314317          case BranchStmt::Continue:
    315                 assert( ! targetEntry->useContExit().empty() );
     318                assert( !targetEntry->useContExit().empty() );
    316319                exitLabel = targetEntry->useContExit();
    317320                break;
    318321          case BranchStmt::FallThrough:
    319                 assert( ! targetEntry->useFallExit().empty() );
     322                assert( !targetEntry->useFallExit().empty() );
    320323                exitLabel = targetEntry->useFallExit();
    321324                break;
    322325          case BranchStmt::FallThroughDefault:
    323                 assert( ! targetEntry->useFallDefaultExit().empty() );
     326                assert( !targetEntry->useFallDefaultExit().empty() );
    324327                exitLabel = targetEntry->useFallDefaultExit();
    325328                // Check that fallthrough default comes before the default clause.
    326                 if ( ! targetEntry->isFallDefaultValid() ) {
     329                if ( !targetEntry->isFallDefaultValid() ) {
    327330                        SemanticError( stmt->location, "'fallthrough default' must precede the 'default' clause" );
    328331                }
     
    370373        // If default, mark seen.
    371374        if ( stmt->isDefault() ) {
    372                 assert( ! enclosing_control_structures.empty() );
     375                assert( !enclosing_control_structures.empty() );
    373376                enclosing_control_structures.back().seenDefault();
    374377        }
     
    396399                Entry & entry = enclosing_control_structures.back();
    397400                if ( entry.isFallUsed() ) {
    398                         mutStmt->stmts.push_back( labelledNullStmt( mutStmt->location, entry.useFallExit() ) );
     401                        mutStmt->stmts.push_back(
     402                                labelledNullStmt( mutStmt->location, entry.useFallExit() ) );
    399403                }
    400404        }
     
    402406        Entry & entry = enclosing_control_structures.back();
    403407        assertf( dynamic_cast< const SwitchStmt * >( entry.stmt ),
    404                          "CFA internal error: control structure enclosing a case clause must be a switch, but is: %s",
     408                         "Control structure enclosing a case clause must be a switch, but is: %s",
    405409                         toString( entry.stmt ).c_str() );
    406410        if ( mutStmt->isDefault() ) {
    407411                if ( entry.isFallDefaultUsed() ) {
    408412                        // Add fallthrough default label if necessary.
    409                         push_front( mutStmt->stmts, labelledNullStmt( stmt->location, entry.useFallDefaultExit() ) );
     413                        push_front( mutStmt->stmts, labelledNullStmt(
     414                                                        stmt->location, entry.useFallDefaultExit()
     415                                                        ) );
    410416                }
    411417        }
     
    414420
    415421void MultiLevelExitCore::previsit( const IfStmt * stmt ) {
    416         bool labeledBlock = ! stmt->labels.empty();
     422        bool labeledBlock = !stmt->labels.empty();
    417423        if ( labeledBlock ) {
    418424                Label breakLabel = newLabel( "blockBreak", stmt );
     
    423429
    424430const IfStmt * MultiLevelExitCore::postvisit( const IfStmt * stmt ) {
    425         bool labeledBlock = ! stmt->labels.empty();
     431        bool labeledBlock = !stmt->labels.empty();
    426432        if ( labeledBlock ) {
    427433                auto this_label = enclosing_control_structures.back().useBreakExit();
    428                 if ( ! this_label.empty() ) {
     434                if ( !this_label.empty() ) {
    429435                        break_label = this_label;
    430436                }
     
    442448        auto it = find_if( stmt->stmts.rbegin(), stmt->stmts.rend(), isDefaultCase );
    443449
    444         const CaseStmt * defaultCase = it != stmt->stmts.rend() ? (it)->strict_as<CaseStmt>() : nullptr;
    445         Label defaultLabel = defaultCase ? newLabel( "fallThroughDefault", defaultCase ) : Label( stmt->location, "" );
     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, "" );
    446455        enclosing_control_structures.emplace_back( stmt, label, defaultLabel );
    447456        GuardAction( [this]() { enclosing_control_structures.pop_back(); } );
    448457
    449         // Collect valid labels for fallthrough. It starts with all labels at this level, then remove as each is seen during
    450         // traversal.
     458        // Collect valid labels for fallthrough. It starts with all labels at
     459        // this level, then remove as each is seen during traversal.
    451460        for ( const Stmt * stmt : stmt->stmts ) {
    452461                auto * caseStmt = strict_dynamic_cast< const CaseStmt * >( stmt );
     
    462471
    463472const SwitchStmt * MultiLevelExitCore::postvisit( const SwitchStmt * stmt ) {
    464         assert( ! enclosing_control_structures.empty() );
     473        assert( !enclosing_control_structures.empty() );
    465474        Entry & entry = enclosing_control_structures.back();
    466475        assert( entry.stmt == stmt );
     
    468477        // Only run to generate the break label.
    469478        if ( entry.isBreakUsed() ) {
    470                 // To keep the switch statements uniform (all direct children of a SwitchStmt should be CastStmts), append the
    471                 // exit label and break to the last case, create a default case if no cases.
     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.
    472482                SwitchStmt * mutStmt = mutate( stmt );
    473483                if ( mutStmt->stmts.empty() ) {
    474                         mutStmt->stmts.push_back( new CaseStmt( mutStmt->location, nullptr, {} ) );
     484                        mutStmt->stmts.push_back( new CaseStmt(
     485                                                                                  mutStmt->location, nullptr, {} ));
    475486                }
    476487
     
    496507
    497508void MultiLevelExitCore::previsit( const TryStmt * stmt ) {
    498         bool isLabeled = ! stmt->labels.empty();
     509        bool isLabeled = !stmt->labels.empty();
    499510        if ( isLabeled ) {
    500511                Label breakLabel = newLabel( "blockBreak", stmt );
     
    505516
    506517void MultiLevelExitCore::postvisit( const TryStmt * stmt ) {
    507         bool isLabeled = ! stmt->labels.empty();
     518        bool isLabeled = !stmt->labels.empty();
    508519        if ( isLabeled ) {
    509520                auto this_label = enclosing_control_structures.back().useBreakExit();
    510                 if ( ! this_label.empty() ) {
     521                if ( !this_label.empty() ) {
    511522                        break_label = this_label;
    512523                }
     
    515526
    516527void MultiLevelExitCore::previsit( const FinallyStmt * ) {
    517         GuardAction([this, old = move( enclosing_control_structures)](){ enclosing_control_structures = move(old); });
     528        GuardAction([this, old = move(enclosing_control_structures)](){
     529                                        enclosing_control_structures = move(old);
     530                                });
    518531        enclosing_control_structures = vector<Entry>();
    519532        GuardValue( inFinally ) = true;
     
    562575template<typename LoopNode>
    563576const LoopNode * MultiLevelExitCore::posthandleLoopStmt( const LoopNode * loopStmt ) {
    564         assert( ! enclosing_control_structures.empty() );
     577        assert( !enclosing_control_structures.empty() );
    565578        Entry & entry = enclosing_control_structures.back();
    566579        assert( entry.stmt == loopStmt );
    567580
    568581        // Now check if the labels are used and add them if so.
    569         return mutate_field( loopStmt, &LoopNode::body, mutateLoop( loopStmt->body, entry ) );
     582        return mutate_field(
     583                loopStmt, &LoopNode::body, mutateLoop( loopStmt->body, entry ) );
    570584        // this call to mutate_field compares loopStmt->body and the result of mutateLoop
    571585        //              if they are the same the node isn't mutated, if they differ then the new mutated node is returned
     
    595609                }
    596610
    597                 if ( ! break_label.empty() ) {
    598                         ret.push_back( labelledNullStmt( ret.back()->location, break_label ) );
     611                if ( !break_label.empty() ) {
     612                        ret.push_back(
     613                                labelledNullStmt( ret.back()->location, break_label ) );
    599614                        break_label = Label( CodeLocation(), "" );
    600615                }
    601616        }
    602617
    603         if ( ! errors.isEmpty() ) {
     618        if ( !errors.isEmpty() ) {
    604619                throw errors;
    605620        }
Note: See TracChangeset for help on using the changeset viewer.