Changeset 400b8be for src/ControlStruct


Ignore:
Timestamp:
Mar 28, 2022, 10:41:45 AM (2 years ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
8e819a9
Parents:
f5bace8
Message:

Added StmtClause? and converted the existing nodes that should be clauses.

Location:
src/ControlStruct
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/ControlStruct/ExceptTranslateNew.cpp

    rf5bace8 r400b8be  
    2626namespace {
    2727
    28         typedef std::list<ast::CatchStmt*> CatchList;
     28        typedef std::list<ast::CatchClause*> CatchList;
    2929
    3030        void appendDeclStmt( ast::CompoundStmt * block, ast::DeclWithType * item ) {
     
    4545        {}
    4646
    47         void previsit( const ast::CatchStmt * stmt );
     47        void previsit( const ast::CatchClause * stmt );
    4848        const ast::Stmt * postvisit( const ast::ThrowStmt * stmt );
    4949};
     
    8888}
    8989
    90 void TranslateThrowsCore::previsit( const ast::CatchStmt * stmt ) {
     90void TranslateThrowsCore::previsit( const ast::CatchClause * stmt ) {
    9191        // Validate the statement's form.
    9292        const ast::ObjectDecl * decl = stmt->decl.as<ast::ObjectDecl>();
     
    147147        ast::FunctionDecl * create_terminate_catch( CatchList &handlers );
    148148        ast::CompoundStmt * create_single_matcher(
    149                 const ast::DeclWithType * except_obj, ast::CatchStmt * modded_handler );
     149                const ast::DeclWithType * except_obj, ast::CatchClause * modded_handler );
    150150        ast::FunctionDecl * create_terminate_match( CatchList &handlers );
    151151        ast::CompoundStmt * create_terminate_caller( CodeLocation loc, ast::FunctionDecl * try_wrapper,
     
    338338ast::FunctionDecl * TryMutatorCore::create_terminate_catch(
    339339                CatchList &handlers ) {
    340         std::vector<ast::ptr<ast::Stmt>> handler_wrappers;
     340        std::vector<ast::ptr<ast::CaseClause>> handler_wrappers;
    341341
    342342        assert (!handlers.empty());
     
    352352        for ( ; it != handlers.end() ; ++it ) {
    353353                ++index;
    354                 ast::CatchStmt * handler = *it;
     354                ast::CatchClause * handler = *it;
    355355                const CodeLocation loc = handler->location;
    356356
     
    390390                // handler->body = nullptr;
    391391
    392                 handler_wrappers.push_back( new ast::CaseStmt(loc,
     392                handler_wrappers.push_back( new ast::CaseClause(loc,
    393393                        ast::ConstantExpr::from_int(loc, index) ,
    394394                        { block, new ast::ReturnStmt( loc, nullptr ) }
     
    410410// except_obj is referenced, modded_handler will be freed.
    411411ast::CompoundStmt * TryMutatorCore::create_single_matcher(
    412                 const ast::DeclWithType * except_obj, ast::CatchStmt * modded_handler ) {
     412                const ast::DeclWithType * except_obj, ast::CatchClause * modded_handler ) {
    413413        // {
    414414        //     `modded_handler.decl`
     
    465465        for ( it = handlers.begin() ; it != handlers.end() ; ++it ) {
    466466                ++index;
    467                 ast::CatchStmt * handler = *it;
     467                ast::CatchClause * handler = *it;
    468468
    469469                // Body should have been taken by create_terminate_catch.
     
    520520        CatchList::iterator it;
    521521        for ( it = handlers.begin() ; it != handlers.end() ; ++it ) {
    522                 ast::CatchStmt * handler = *it;
     522                ast::CatchClause * handler = *it;
    523523                const CodeLocation loc = handler->location;
    524524                // Modifiy body.
     
    587587                ast::TryStmt * tryStmt ) {
    588588        // void finally() { `finally->block` }
    589         const ast::FinallyStmt * finally = tryStmt->finally;
     589        const ast::FinallyClause * finally = tryStmt->finally;
    590590        const ast::CompoundStmt * body = finally->body;
    591591
  • src/ControlStruct/LabelGeneratorNew.cpp

    rf5bace8 r400b8be  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // LabelGenerator.cc --
     7// LabelGeneratorNew.cpp --
    88//
    99// Author           : Peter A. Buhr
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Feb  2 09:11:17 2022
    13 // Update Count     : 72
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Mon Mar 28 10:03:00 2022
     13// Update Count     : 73
    1414//
    1515
     
    2525namespace ControlStruct {
    2626
    27 Label newLabel( const string & suffix, const Stmt * stmt ) {
     27enum { size = 128 };
     28
     29static int newLabelPre( char buf[size], const string & suffix ) {
    2830        static int current = 0;
    2931
    30         assertf( stmt, "CFA internal error: parameter statement cannot be null pointer" );
    31 
    32         enum { size = 128 };
    33         char buf[size];                                                                         // space to build label
    3432        int len = snprintf( buf, size, "__L%d__%s", current++, suffix.c_str() );
    3533        assertf( len < size, "CFA Internal error: buffer overflow creating label" );
     34        return len;
     35}
     36
     37static Label newLabelPost( char buf[size], const CodeLocation & location ) {
     38        Label ret_label( location, buf );
     39        ret_label.attributes.push_back( new Attribute( "unused" ) );
     40        return ret_label;
     41}
     42
     43Label newLabel( const string & suffix, const Stmt * stmt ) {
     44        // Buffer for string manipulation.
     45        char buf[size];
     46
     47        assertf( stmt, "CFA internal error: parameter statement cannot be null pointer" );
     48        int len = newLabelPre( buf, suffix );
    3649
    3750        // What does this do?
     
    4154        } // if
    4255
    43         Label ret_label( stmt->location, buf );
    44         ret_label.attributes.push_back( new Attribute( "unused" ) );
    45         return ret_label;
     56        return newLabelPost( buf, stmt->location );
     57}
     58
     59Label newLabel( const string & suffix, const CodeLocation & location ) {
     60        // Buffer for string manipulation.
     61        char buf[size];
     62
     63        newLabelPre( buf, suffix );
     64        return newLabelPost( buf, location );
    4665}
    4766
  • src/ControlStruct/LabelGeneratorNew.hpp

    rf5bace8 r400b8be  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 31 18:03:09 2022
    13 // Update Count     : 27
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Fir Mar 25 15:40:00 2022
     13// Update Count     : 28
    1414//
    1515
     
    1818#include <string>                                                                               // for string
    1919
    20 class Statement;
     20class CodeLocation;
    2121
    2222namespace ast {
     23        class Label;
    2324        class Stmt;
    24         class Label;
    2525} // namespace ast
    2626
    2727namespace ControlStruct {
    2828        ast::Label newLabel( const std::string &, const ast::Stmt * );
     29        ast::Label newLabel( const std::string &, const CodeLocation & );
    2930} // namespace ControlStruct
    3031
  • src/ControlStruct/MultiLevelExit.cpp

    rf5bace8 r400b8be  
    99// Author           : Andrew Beach
    1010// Created On       : Mon Nov  1 13:48:00 2021
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Feb  2 23:07:54 2022
    13 // Update Count     : 33
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Mon Mar 28  9:42:00 2022
     13// Update Count     : 34
    1414//
    1515
     
    4040
    4141        enum Kind {
    42                 ForStmtK, WhileDoStmtK, CompoundStmtK, IfStmtK, CaseStmtK, SwitchStmtK, TryStmtK
     42                ForStmtK, WhileDoStmtK, CompoundStmtK, IfStmtK, CaseClauseK, SwitchStmtK, TryStmtK
    4343        } kind;
    4444
     
    5858        Entry( const IfStmt *stmt, Label breakExit ) :
    5959                stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( IfStmtK ) {}
    60         Entry( const CaseStmt *stmt, Label fallExit ) :
    61                 stmt( stmt ), firstTarget( fallExit ), secondTarget(), kind( CaseStmtK ) {}
     60        Entry( const CaseClause *, const CompoundStmt *stmt, Label fallExit ) :
     61                stmt( stmt ), firstTarget( fallExit ), secondTarget(), kind( CaseClauseK ) {}
    6262        Entry( const SwitchStmt *stmt, Label breakExit, Label fallDefaultExit ) :
    6363                stmt( stmt ), firstTarget( breakExit ), secondTarget( fallDefaultExit ), kind( SwitchStmtK ) {}
     
    6666
    6767        bool isContTarget() const { return kind <= WhileDoStmtK; }
    68         bool isBreakTarget() const { return kind != CaseStmtK; }
    69         bool isFallTarget() const { return kind == CaseStmtK; }
     68        bool isBreakTarget() const { return kind != CaseClauseK; }
     69        bool isFallTarget() const { return kind == CaseClauseK; }
    7070        bool isFallDefaultTarget() const { return kind == SwitchStmtK; }
    7171
    7272        // These routines set a target as being "used" by a BranchStmt
    7373        Label useContExit() { assert( kind <= WhileDoStmtK ); return useTarget(secondTarget); }
    74         Label useBreakExit() { assert( kind != CaseStmtK ); return useTarget(firstTarget); }
    75         Label useFallExit() { assert( kind == CaseStmtK );  return useTarget(firstTarget); }
     74        Label useBreakExit() { assert( kind != CaseClauseK ); return useTarget(firstTarget); }
     75        Label useFallExit() { assert( kind == CaseClauseK );  return useTarget(firstTarget); }
    7676        Label useFallDefaultExit() { assert( kind == SwitchStmtK ); return useTarget(secondTarget); }
    7777
    7878        // These routines check if a specific label for a statement is used by a BranchStmt
    7979        bool isContUsed() const { assert( kind <= WhileDoStmtK ); return secondTarget.used; }
    80         bool isBreakUsed() const { assert( kind != CaseStmtK ); return firstTarget.used; }
    81         bool isFallUsed() const { assert( kind == CaseStmtK ); return firstTarget.used; }
     80        bool isBreakUsed() const { assert( kind != CaseClauseK ); return firstTarget.used; }
     81        bool isFallUsed() const { assert( kind == CaseClauseK ); return firstTarget.used; }
    8282        bool isFallDefaultUsed() const { assert( kind == SwitchStmtK ); return secondTarget.used; }
    8383        void seenDefault() { fallDefaultValid = false; }
     
    115115        void previsit( const ForStmt * );
    116116        const ForStmt * postvisit( const ForStmt * );
    117         const CaseStmt * previsit( const CaseStmt * );
     117        const CaseClause * previsit( const CaseClause * );
    118118        void previsit( const IfStmt * );
    119119        const IfStmt * postvisit( const IfStmt * );
     
    123123        void previsit( const TryStmt * );
    124124        void postvisit( const TryStmt * );
    125         void previsit( const FinallyStmt * );
     125        void previsit( const FinallyClause * );
    126126
    127127        const Stmt * mutateLoop( const Stmt * body, Entry& );
     
    288288                  auto switchStmt = strict_dynamic_cast< const SwitchStmt * >( targetEntry->stmt );
    289289                  bool foundDefault = false;
    290                   for ( auto subStmt : switchStmt->stmts ) {
    291                           const CaseStmt * caseStmt = subStmt.strict_as<CaseStmt>();
     290                  for ( auto caseStmt : switchStmt->cases ) {
    292291                          if ( caseStmt->isDefault() ) {
    293292                                  foundDefault = true;
     
    365364}
    366365
    367 const CaseStmt * MultiLevelExitCore::previsit( const CaseStmt * stmt ) {
     366const CaseClause * MultiLevelExitCore::previsit( const CaseClause * stmt ) {
    368367        visit_children = false;
    369368
     
    375374
    376375        // The cond may not exist, but if it does update it now.
    377         visitor->maybe_accept( stmt, &CaseStmt::cond );
     376        visitor->maybe_accept( stmt, &CaseClause::cond );
    378377
    379378        // Just save the mutated node for simplicity.
    380         CaseStmt * mutStmt = mutate( stmt );
    381 
    382         Label fallLabel = newLabel( "fallThrough", stmt );
     379        CaseClause * mutStmt = mutate( stmt );
     380
     381        Label fallLabel = newLabel( "fallThrough", stmt->location );
    383382        if ( ! mutStmt->stmts.empty() ) {
     383                // These should already be in a block.
     384                auto first = mutStmt->stmts.front().get_and_mutate();
     385                auto block = strict_dynamic_cast<CompoundStmt *>( first );
     386
    384387                // Ensure that the stack isn't corrupted by exceptions in fixBlock.
    385388                auto guard = makeFuncGuard(
    386                         [&](){ enclosing_control_structures.emplace_back( mutStmt, fallLabel ); },
     389                        [&](){ enclosing_control_structures.emplace_back( mutStmt, block, fallLabel ); },
    387390                        [this](){ enclosing_control_structures.pop_back(); }
    388391                        );
    389392
    390                 // These should already be in a block.
    391                 auto block = mutate( mutStmt->stmts.front().strict_as<CompoundStmt>() );
    392393                block->kids = fixBlock( block->kids, true );
    393394
     
    396397                Entry & entry = enclosing_control_structures.back();
    397398                if ( entry.isFallUsed() ) {
    398                         mutStmt->stmts.push_back( labelledNullStmt( mutStmt->location, entry.useFallExit() ) );
     399                        mutStmt->stmts.push_back( labelledNullStmt( block->location, entry.useFallExit() ) );
    399400                }
    400401        }
     
    433434}
    434435
    435 bool isDefaultCase( const ptr<Stmt> & stmt ) {
    436         const CaseStmt * caseStmt = stmt.strict_as<CaseStmt>();
    437         return caseStmt->isDefault();
     436static bool isDefaultCase( const ptr<CaseClause> & caseClause ) {
     437        return caseClause->isDefault();
    438438}
    439439
    440440void MultiLevelExitCore::previsit( const SwitchStmt * stmt ) {
    441441        Label label = newLabel( "switchBreak", stmt );
    442         auto it = find_if( stmt->stmts.rbegin(), stmt->stmts.rend(), isDefaultCase );
    443 
    444         const CaseStmt * defaultCase = it != stmt->stmts.rend() ? (it)->strict_as<CaseStmt>() : nullptr;
    445         Label defaultLabel = defaultCase ? newLabel( "fallThroughDefault", defaultCase ) : Label( stmt->location, "" );
     442        auto it = find_if( stmt->cases.rbegin(), stmt->cases.rend(), isDefaultCase );
     443
     444        const CaseClause * defaultCase = it != stmt->cases.rend() ? (*it) : nullptr;
     445        Label defaultLabel = defaultCase ? newLabel( "fallThroughDefault", defaultCase->location ) : Label( stmt->location, "" );
    446446        enclosing_control_structures.emplace_back( stmt, label, defaultLabel );
    447447        GuardAction( [this]() { enclosing_control_structures.pop_back(); } );
     
    449449        // Collect valid labels for fallthrough. It starts with all labels at this level, then remove as each is seen during
    450450        // traversal.
    451         for ( const Stmt * stmt : stmt->stmts ) {
    452                 auto * caseStmt = strict_dynamic_cast< const CaseStmt * >( stmt );
     451        for ( const CaseClause * caseStmt : stmt->cases ) {
    453452                if ( caseStmt->stmts.empty() ) continue;
    454453                auto block = caseStmt->stmts.front().strict_as<CompoundStmt>();
     
    471470                // exit label and break to the last case, create a default case if no cases.
    472471                SwitchStmt * mutStmt = mutate( stmt );
    473                 if ( mutStmt->stmts.empty() ) {
    474                         mutStmt->stmts.push_back( new CaseStmt( mutStmt->location, nullptr, {} ) );
    475                 }
    476 
    477                 auto caseStmt = mutStmt->stmts.back().strict_as<CaseStmt>();
     472                if ( mutStmt->cases.empty() ) {
     473                        mutStmt->cases.push_back( new CaseClause( mutStmt->location, nullptr, {} ) );
     474                }
     475
     476                auto caseStmt = mutStmt->cases.back().get();
    478477                auto mutCase = mutate( caseStmt );
    479                 mutStmt->stmts.back() = mutCase;
     478                mutStmt->cases.back() = mutCase;
    480479
    481480                Label label( mutCase->location, "breakLabel" );
     
    514513}
    515514
    516 void MultiLevelExitCore::previsit( const FinallyStmt * ) {
     515void MultiLevelExitCore::previsit( const FinallyClause * ) {
    517516        GuardAction([this, old = move( enclosing_control_structures)](){ enclosing_control_structures = move(old); });
    518517        enclosing_control_structures = vector<Entry>();
Note: See TracChangeset for help on using the changeset viewer.