Ignore:
Timestamp:
Aug 15, 2016, 10:18:22 AM (9 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
b1848a0
Parents:
38736854 (diff), 797347f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/StatementNode.cc

    r38736854 r6603c1d  
    1010// Created On       : Sat May 16 14:59:41 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 11 16:19:45 2016
    13 // Update Count     : 210
     12// Last Modified On : Sun Aug 14 13:10:54 2016
     13// Update Count     : 288
    1414//
    1515
     
    3434};
    3535
    36 StatementNode::StatementNode() : ParseNode(), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {}
    37 
    38 StatementNode::StatementNode( const string *name ) : ParseNode( name ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {}
    39 
    40 StatementNode::StatementNode( DeclarationNode *decl ) : type( Decl ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), isCatchRest ( false ) {
     36StatementNode::StatementNode() : ParseNode(), labels( 0 ), decl( 0 ) {}
     37
     38StatementNode::StatementNode( DeclarationNode *decl ) : type( Decl ), labels( 0 ) {
     39        assert( false );
    4140        if ( decl ) {
    4241                if ( DeclarationNode *agg = decl->extractAggregate() ) {
     
    4645                        nextStmt->decl = decl;
    4746                        next = nextStmt;
    48                         if ( decl->get_link() ) {
    49                                 next->set_next( new StatementNode( dynamic_cast< DeclarationNode* >( decl->get_link() ) ) );
     47                        if ( decl->get_next() ) {
     48                                next->set_next( new StatementNode( dynamic_cast<DeclarationNode *>( decl->get_next() ) ) );
    5049                                decl->set_next( 0 );
    5150                        } // if
    5251                } else {
    53                         if ( decl->get_link() ) {
    54                                 next = new StatementNode( dynamic_cast< DeclarationNode* >( decl->get_link() ) );
     52                        if ( decl->get_next() ) {
     53                                next = new StatementNode( dynamic_cast<DeclarationNode *>( decl->get_next() ) );
    5554                                decl->set_next( 0 );
    5655                        } // if
     
    6059}
    6160
    62 StatementNode::StatementNode( Type t, ExpressionNode *ctrl_label, StatementNode *block ) : type( t ), control( ctrl_label ), block( block ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {
    63         this->control = ( t == Default ) ? 0 : control;
    64 }
    65 
    66 StatementNode::StatementNode( Type t, string *target ) : type( t ), control( 0 ), block( 0 ), labels( 0 ), target( target ), decl( 0 ), isCatchRest ( false ) {}
     61StatementNode2::StatementNode2( DeclarationNode *decl ) {
     62        if ( decl ) {
     63                DeclarationNode *agg = decl->extractAggregate();
     64                if ( agg ) {
     65                        StatementNode *nextStmt = new StatementNode;
     66                        nextStmt->type = Decl;
     67                        nextStmt->decl = decl;
     68                        next = nextStmt;
     69                        if ( decl->get_next() ) {
     70                                next->set_next( new StatementNode2( dynamic_cast<DeclarationNode *>(decl->get_next()) ) );
     71                                decl->set_next( 0 );
     72                        } // if
     73                } else {
     74                        if ( decl->get_next() ) {
     75                                next = new StatementNode2( dynamic_cast<DeclarationNode *>( decl->get_next() ) );
     76                                decl->set_next( 0 );
     77                        } // if
     78                        agg = decl;
     79                } // if
     80                stmt = new DeclStmt( noLabels, maybeBuild<Declaration>(agg) );
     81        } else {
     82                assert( false );
     83        } // if
     84}
    6785
    6886StatementNode::~StatementNode() {
    69         delete control;
    70         delete block;
    71         delete target;
    7287        delete decl;
    7388}
    7489
    75 StatementNode * StatementNode::newCatchStmt( DeclarationNode *d, StatementNode *s, bool catchRestP ) {
    76         StatementNode *ret = new StatementNode( StatementNode::Catch, 0, s );
    77         ret->addDeclaration( d );
    78         ret->setCatchRest( catchRestP );
    79 
    80         return ret;
    81 }
    82 
    83 std::string StatementNode::get_target() const{
    84         if ( target )
    85                 return *target;
    86 
    87         return string("");
    88 }
    89 
    9090StatementNode * StatementNode::clone() const {
    91         StatementNode *newnode = new StatementNode( type, maybeClone( control ), maybeClone( block ) );
    92         if ( target ) {
    93                 newnode->target = new string( *target );
    94         } else {
    95                 newnode->target = 0;
    96         } // if
    97         newnode->decl = maybeClone( decl );
    98         return newnode;
     91        assert( false );
     92        return 0;
    9993}
    10094
     
    107101}
    108102
    109 StatementNode *StatementNode::append_block( StatementNode *stmt ) {
    110         if ( stmt != 0 ) {
    111                 if ( block == 0 )
    112                         block = stmt;
    113                 else
    114                         block->set_link( stmt );
    115         } // if
    116         return this;
    117 }
    118 
    119103StatementNode *StatementNode::append_last_case( StatementNode *stmt ) {
    120104        assert( false );
    121         if ( stmt != 0 ) {
    122                 StatementNode *next = ( StatementNode *)get_link();
    123                 if ( next && ( next->get_type() == StatementNode::Case || next->get_type() == StatementNode::Default ) )
    124                         next->append_last_case( stmt );
    125                 else
    126                         if ( block == 0 )
    127                                 block = stmt;
    128                         else
    129                                 block->set_link( stmt );
    130         } // if
    131105        return this;
    132106}
     
    134108StatementNode *StatementNode2::append_last_case( StatementNode *stmt ) {
    135109        StatementNode *prev = this;
    136         for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_link() ) {
     110        // find end of list and maintain previous pointer
     111        for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
    137112                StatementNode2 *node = dynamic_cast<StatementNode2 *>(curr);
    138113                assert( node );
     
    140115                prev = curr;
    141116        } // for
     117        // conver from StatementNode list to Statement list
    142118        StatementNode2 *node = dynamic_cast<StatementNode2 *>(prev);
    143119        std::list<Statement *> stmts;
    144120        buildList( stmt, stmts );
    145         CaseStmt * caseStmt;
    146         caseStmt = dynamic_cast<CaseStmt *>(node->stmt);
     121        // splice any new Statements to end of currents Statements
     122        CaseStmt * caseStmt = dynamic_cast<CaseStmt *>(node->stmt);
    147123        caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
    148124        return this;
    149125}
    150126
    151 void StatementNode::print( std::ostream &os, int indent ) const {
    152         if ( ! labels.empty() ) {
    153                 std::list<std::string>::const_iterator i;
    154 
    155                 os << string( indent, ' ' );
    156                 for ( i = labels.begin(); i != labels.end(); i++ )
    157                         os << *i << ":";
    158                 os << endl;
    159         } // if
    160 
     127Statement *StatementNode::build() const {
    161128        switch ( type ) {
    162129          case Decl:
    163                 decl->print( os, indent );
    164                 break;
     130                return new DeclStmt( noLabels, maybeBuild< Declaration >( decl ) );
     131                assert( false );
    165132          case Exp:
    166                 if ( control ) {
    167                         os << string( indent, ' ' );
    168                         control->print( os, indent );
    169                         os << endl;
    170                 } else
    171                         os << string( indent, ' ' ) << "Null Statement" << endl;
    172                 break;
    173           default:
    174                 os << string( indent, ' ' ) << StatementNode::StType[type] << endl;
    175                 if ( type == Catch ) {
    176                         if ( decl ) {
    177                                 os << string( indent + ParseNode::indent_by, ' ' ) << "Declaration: " << endl;
    178                                 decl->print( os, indent + 2 * ParseNode::indent_by );
    179                         } else if ( isCatchRest ) {
    180                                 os << string( indent + ParseNode::indent_by, ' ' ) << "Catches the rest " << endl;
    181                         } else {
    182                                 ; // should never reach here
    183                         } // if
    184                 } // if
    185                 if ( control ) {
    186                         os << string( indent + ParseNode::indent_by, ' ' ) << "Control: " << endl;
    187                         control->printList( os, indent + 2 * ParseNode::indent_by );
    188                 } // if
    189                 if ( block ) {
    190                         os << string( indent + ParseNode::indent_by, ' ' ) << "Cases: " << endl;
    191                         block->printList( os, indent + 2 * ParseNode::indent_by );
    192                 } // if
    193                 if ( target ) {
    194                         os << string( indent + ParseNode::indent_by, ' ' ) << "Target: " << get_target() << endl;
    195                 } // if
    196                 break;
    197         } // switch
    198 }
    199 
    200 Statement *StatementNode::build() const {
    201         std::list<Statement *> branches;
    202         std::list<Expression *> exps;
    203         std::list<Label> labs;
    204 
    205         if ( ! labels.empty() ) {
    206                 std::back_insert_iterator< std::list<Label> > lab_it( labs );
    207                 copy( labels.begin(), labels.end(), lab_it );
    208         } // if
    209 
    210         // try {
    211         buildList<Statement, StatementNode>( get_block(), branches );
    212 
    213         switch ( type ) {
    214           case Decl:
    215                 return new DeclStmt( labs, maybeBuild< Declaration >( decl ) );
    216           case Exp:
    217                 {
    218                         Expression *e = maybeBuild< Expression >( get_control() );
    219 
    220                         if ( e )
    221                                 return new ExprStmt( labs, e );
    222                         else
    223                                 return new NullStmt( labs );
    224                 }
    225                 assert( false );
    226133          case If:
    227                 // {
    228                 //      Statement *thenb = 0, *elseb = 0;
    229                 //      assert( branches.size() >= 1 );
    230 
    231                 //      thenb = branches.front();
    232                 //      branches.pop_front();
    233                 //      if ( ! branches.empty() ) {
    234                 //              elseb = branches.front();
    235                 //              branches.pop_front();
    236                 //      } // if
    237                 //      return new IfStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), thenb, elseb );
    238                 // }
    239                 assert( false );
    240134          case Switch:
    241                 // return new SwitchStmt( labs, maybeBuild<Expression>(get_control()), branches );
    242                 assert( false );
    243135          case Case:
    244                 //return new CaseStmt( labs, maybeBuild<Expression>(get_control() ), branches );
    245                 assert( false );
    246136          case Default:
    247                 //return new CaseStmt( labs, 0, branches, true );
    248                 assert( false );
    249137          case While:
    250                 // assert( branches.size() == 1 );
    251                 // return new WhileStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), branches.front() );
    252                 assert( false );
    253138          case Do:
    254                 // assert( branches.size() == 1 );
    255                 // return new WhileStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), branches.front(), true );
    256                 assert( false );
    257139          case For:
    258                 // {
    259                 //      assert( branches.size() == 1 );
    260 
    261                 //      ForCtlExprNode *ctl = dynamic_cast<ForCtlExprNode *>( get_control() );
    262                 //      assert( ctl != 0 );
    263 
    264                 //      std::list<Statement *> init;
    265                 //      if ( ctl->get_init() != 0 ) {
    266                 //              buildList( ctl->get_init(), init );
    267                 //      } // if
    268 
    269                 //      Expression *cond = 0;
    270                 //      if ( ctl->get_condition() != 0 )
    271                 //              cond = notZeroExpr( maybeBuild<Expression>(ctl->get_condition()) );
    272 
    273                 //      Expression *incr = 0;
    274                 //      if ( ctl->get_change() != 0 )
    275                 //              incr = maybeBuild<Expression>(ctl->get_change());
    276 
    277                 //      return new ForStmt( labs, init, cond, incr, branches.front() );
    278                 // }
    279                 assert( false );
    280140          case Goto:
    281                 // {
    282                 //      if ( get_target() == "" ) {                                     // computed goto
    283                 //              assert( get_control() != 0 );
    284                 //              return new BranchStmt( labs, maybeBuild<Expression>(get_control()), BranchStmt::Goto );
    285                 //      } // if
    286 
    287                 //      return new BranchStmt( labs, get_target(), BranchStmt::Goto );
    288                 // }
    289                 assert( false );
    290141          case Break:
    291                 // return new BranchStmt( labs, get_target(), BranchStmt::Break );
    292                 assert( false );
    293142          case Continue:
    294                 // return new BranchStmt( labs, get_target(), BranchStmt::Continue );
    295                 assert( false );
    296143          case Return:
    297144          case Throw :
    298                 // buildList( get_control(), exps );
    299                 // if ( exps.size() ==0 )
    300                 //      return new ReturnStmt( labs, 0, type == Throw );
    301                 // if ( exps.size() > 0 )
    302                 //      return new ReturnStmt( labs, exps.back(), type == Throw );
     145          case Try:
     146          case Catch:
     147          case Finally:
     148          case Asm:
     149          default:
    303150                assert( false );
    304           case Try:
    305                 {
    306                         assert( branches.size() >= 0 );
    307                         CompoundStmt *tryBlock = dynamic_cast<CompoundStmt *>( branches.front());
    308                         branches.pop_front();
    309                         FinallyStmt *finallyBlock = 0;
    310                         if ( ( finallyBlock = dynamic_cast<FinallyStmt *>( branches.back())) ) {
    311                                 branches.pop_back();
    312                         } // if
    313                         return new TryStmt( labs, tryBlock, branches, finallyBlock );
    314                 }
    315           case Catch:
    316                 {
    317                         assert( branches.size() == 1 );
    318 
    319                         return new CatchStmt( labs, maybeBuild< Declaration >( decl ), branches.front(), isCatchRest );
    320                 }
    321           case Finally:
    322                 {
    323                         assert( branches.size() == 1 );
    324                         CompoundStmt *block = dynamic_cast<CompoundStmt *>( branches.front() );
    325                         assert( block != 0 );
    326 
    327                         return new FinallyStmt( labs, block );
    328                 }
    329           case Asm:
    330                 assert( false );
    331           default:
    332                 // shouldn't be here
    333151                return 0;
    334152        } // switch
     
    422240}
    423241
     242Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
     243        std::list<Statement *> branches;
     244        buildList<Statement, StatementNode>( catch_stmt, branches );
     245        CompoundStmt *tryBlock = dynamic_cast<CompoundStmt *>(maybeBuild<Statement>(try_stmt));
     246        assert( tryBlock );
     247        FinallyStmt *finallyBlock = dynamic_cast<FinallyStmt *>(maybeBuild<Statement>(finally_stmt) );
     248        return new TryStmt( noLabels, tryBlock, branches, finallyBlock );
     249}
     250Statement *build_catch( DeclarationNode *decl, StatementNode *stmt, bool catchAny ) {
     251        std::list<Statement *> branches;
     252        buildList<Statement, StatementNode>( stmt, branches );
     253        assert( branches.size() == 1 );
     254        return new CatchStmt( noLabels, maybeBuild<Declaration>(decl), branches.front(), catchAny );
     255}
     256Statement *build_finally( StatementNode *stmt ) {
     257        std::list<Statement *> branches;
     258        buildList<Statement, StatementNode>( stmt, branches );
     259        assert( branches.size() == 1 );
     260        return new FinallyStmt( noLabels, dynamic_cast<CompoundStmt *>( branches.front() ) );
     261}
     262
    424263
    425264CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {}
    426 
    427 CompoundStmtNode::CompoundStmtNode( const string *name_ ) : StatementNode( name_ ), first( 0 ), last( 0 ) {}
    428265
    429266CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) {
     
    441278void CompoundStmtNode::add_statement( StatementNode *stmt ) {
    442279        if ( stmt != 0 ) {
    443                 last->set_link( stmt );
    444                 last = ( StatementNode *)( stmt->get_link());
     280                last->set_last( stmt );
     281                last = ( StatementNode *)( stmt->get_next());
    445282        } // if
    446283}
     
    467304
    468305
    469 AsmStmtNode::AsmStmtNode( Type t, bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) :
    470         StatementNode( t ), voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ) {
     306AsmStmtNode::AsmStmtNode( bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) :
     307        voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ) {
     308        type = Asm;
    471309        if ( gotolabels ) {
    472310                this->gotolabels = gotolabels->get_labels();
     
    477315AsmStmtNode::~AsmStmtNode() {
    478316        delete output; delete input; delete clobber;
    479 }
    480 
    481 void AsmStmtNode::print( std::ostream &os, int indent ) const {
    482         StatementNode::print( os, indent );                                     // print statement labels
    483         os << string( indent + ParseNode::indent_by, ' ' ) << "volatile:" << voltile << endl;
    484         if ( instruction ) {
    485                 os << string( indent + ParseNode::indent_by, ' ' ) << "Instruction:" << endl;
    486 //              instruction->printList( os, indent + 2 * ParseNode::indent_by );
    487         } // if
    488         if ( output ) {
    489                 os << string( indent + ParseNode::indent_by, ' ' ) << "Output:" << endl;
    490                 output->printList( os, indent + 2 * ParseNode::indent_by );
    491         } // if
    492         if ( input ) {
    493                 os << string( indent + ParseNode::indent_by, ' ' ) << "Input:" << endl;
    494                 input->printList( os, indent + 2 * ParseNode::indent_by );
    495         } // if
    496         if ( clobber ) {
    497                 os << string( indent + ParseNode::indent_by, ' ' ) << "Clobber:" << endl;
    498 //              clobber->printList( os, indent + 2 * ParseNode::indent_by );
    499         } // if
    500         if ( ! gotolabels.empty() ) {
    501                 os << string( indent + ParseNode::indent_by, ' ' ) << "Goto Labels:" << endl;
    502                 os << string( indent + 2 * ParseNode::indent_by, ' ' );
    503                 for ( std::list<Label>::const_iterator i = gotolabels.begin();; ) {
    504                         os << *i;
    505                         i++;
    506                   if ( i == gotolabels.end() ) break;
    507                         os << ", ";
    508                 }
    509                 os << endl;
    510         } // if
    511317}
    512318
     
    528334}
    529335
     336// Statement *build_asm( bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 ) {
     337//      std::list<Label> labs;
     338
     339//      if ( ! get_labels().empty() ) {
     340//              std::back_insert_iterator< std::list<Label> > lab_it( labs );
     341//              copy( get_labels().begin(), get_labels().end(), lab_it );
     342//      } // if
     343
     344//      std::list< Expression * > out, in;
     345//      std::list< ConstantExpr * > clob;
     346//      buildList( output, out );
     347//      buildList( input, in );
     348//      buildList( clobber, clob );
     349//      std::list< Label > gotolabs = gotolabels;
     350//      return new AsmStmt( labs, voltile, instruction, out, in, clob, gotolabs );
     351// }
     352
    530353// Local Variables: //
    531354// tab-width: 4 //
Note: See TracChangeset for help on using the changeset viewer.