Ignore:
Timestamp:
Aug 15, 2016, 4:13:38 PM (8 years ago)
Author:
Peter A. Buhr <pabuhr@…>
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:
797347f
Message:

more refactoring of parser code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/StatementNode.cc

    r797347f re82aa9df  
    1010// Created On       : Sat May 16 14:59:41 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Aug 14 13:10:54 2016
    13 // Update Count     : 288
     12// Last Modified On : Mon Aug 15 14:40:05 2016
     13// Update Count     : 317
    1414//
    1515
     
    2626using namespace std;
    2727
    28 const char *StatementNode::StType[] = {
    29         "Exp",   "If",       "Switch", "Case",    "Default",  "Choose",   "Fallthru",
    30         "While", "Do",       "For",
    31         "Goto",  "Continue", "Break",  "Return",  "Throw",
    32         "Try",   "Catch",    "Finally", "Asm",
    33         "Decl"
    34 };
    3528
    36 StatementNode::StatementNode() : ParseNode(), labels( 0 ), decl( 0 ) {}
    37 
    38 StatementNode::StatementNode( DeclarationNode *decl ) : type( Decl ), labels( 0 ) {
    39         assert( false );
     29StatementNode::StatementNode( DeclarationNode *decl ) {
    4030        if ( decl ) {
    41                 if ( DeclarationNode *agg = decl->extractAggregate() ) {
    42                         this->decl = agg;
    43                         StatementNode *nextStmt = new StatementNode;
    44                         nextStmt->type = Decl;
    45                         nextStmt->decl = decl;
     31                DeclarationNode *agg = decl->extractAggregate();
     32                if ( agg ) {
     33                        StatementNode *nextStmt = new StatementNode( new DeclStmt( noLabels, maybeBuild< Declaration >( decl ) ) );
    4634                        next = nextStmt;
    4735                        if ( decl->get_next() ) {
    48                                 next->set_next( new StatementNode( dynamic_cast<DeclarationNode *>( decl->get_next() ) ) );
     36                                next->set_next( new StatementNode( dynamic_cast<DeclarationNode *>(decl->get_next()) ) );
    4937                                decl->set_next( 0 );
    5038                        } // if
     
    5240                        if ( decl->get_next() ) {
    5341                                next = new StatementNode( dynamic_cast<DeclarationNode *>( decl->get_next() ) );
    54                                 decl->set_next( 0 );
    55                         } // if
    56                         this->decl = decl;
    57                 } // if
    58         } // if
    59 }
    60 
    61 StatementNode2::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() ) );
    7642                                decl->set_next( 0 );
    7743                        } // if
     
    8450}
    8551
    86 StatementNode::~StatementNode() {
    87         delete decl;
    88 }
    89 
    90 StatementNode * StatementNode::clone() const {
    91         assert( false );
    92         return 0;
    93 }
    94 
    95 StatementNode *StatementNode::add_label( const std::string *l ) {
    96         if ( l != 0 ) {
    97                 labels.push_front( *l );
    98                 delete l;
    99         } // if
    100         return this;
    101 }
    102 
    10352StatementNode *StatementNode::append_last_case( StatementNode *stmt ) {
    104         assert( false );
    105         return this;
    106 }
    107 
    108 StatementNode *StatementNode2::append_last_case( StatementNode *stmt ) {
    10953        StatementNode *prev = this;
    11054        // find end of list and maintain previous pointer
    11155        for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
    112                 StatementNode2 *node = dynamic_cast<StatementNode2 *>(curr);
     56                StatementNode *node = dynamic_cast<StatementNode *>(curr);
    11357                assert( node );
    11458                assert( dynamic_cast<CaseStmt *>(node->stmt) );
    11559                prev = curr;
    11660        } // for
    117         // conver from StatementNode list to Statement list
    118         StatementNode2 *node = dynamic_cast<StatementNode2 *>(prev);
     61        // convert from StatementNode list to Statement list
     62        StatementNode *node = dynamic_cast<StatementNode *>(prev);
    11963        std::list<Statement *> stmts;
    12064        buildList( stmt, stmts );
    121         // splice any new Statements to end of currents Statements
     65        // splice any new Statements to end of current Statements
    12266        CaseStmt * caseStmt = dynamic_cast<CaseStmt *>(node->stmt);
    12367        caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
    12468        return this;
    125 }
    126 
    127 Statement *StatementNode::build() const {
    128         switch ( type ) {
    129           case Decl:
    130                 return new DeclStmt( noLabels, maybeBuild< Declaration >( decl ) );
    131                 assert( false );
    132           case Exp:
    133           case If:
    134           case Switch:
    135           case Case:
    136           case Default:
    137           case While:
    138           case Do:
    139           case For:
    140           case Goto:
    141           case Break:
    142           case Continue:
    143           case Return:
    144           case Throw :
    145           case Try:
    146           case Catch:
    147           case Finally:
    148           case Asm:
    149           default:
    150                 assert( false );
    151                 return 0;
    152         } // switch
    15369}
    15470
     
    261177}
    262178
    263 
    264 CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {}
    265 
    266 CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) {
    267         if ( first ) {
    268                 last = ( StatementNode *)( stmt->get_last());
    269         } else {
    270                 last = 0;
    271         } // if
    272 }
    273 
    274 CompoundStmtNode::~CompoundStmtNode() {
    275         delete first;
    276 }
    277 
    278 void CompoundStmtNode::add_statement( StatementNode *stmt ) {
    279         if ( stmt != 0 ) {
    280                 last->set_last( stmt );
    281                 last = ( StatementNode *)( stmt->get_next());
    282         } // if
    283 }
    284 
    285 void CompoundStmtNode::print( ostream &os, int indent ) const {
    286         if ( first ) {
    287                 first->printList( os, indent+2 );
    288         } // if
    289 }
    290 
    291 Statement *CompoundStmtNode::build() const {
    292         std::list<Label> labs;
    293         const std::list<std::string> &labels = get_labels();
    294 
    295         if ( ! labels.empty() ) {
    296                 std::back_insert_iterator< std::list<Label> > lab_it( labs );
    297                 copy( labels.begin(), labels.end(), lab_it );
    298         } // if
    299 
    300         CompoundStmt *cs = new CompoundStmt( labs );
     179Statement *build_compound( StatementNode *first ) {
     180        CompoundStmt *cs = new CompoundStmt( noLabels );
    301181        buildList( first, cs->get_kids() );
    302182        return cs;
    303183}
    304184
    305 
    306 AsmStmtNode::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;
    309         if ( gotolabels ) {
    310                 this->gotolabels = gotolabels->get_labels();
    311                 delete gotolabels;
    312         } // if
    313 }
    314 
    315 AsmStmtNode::~AsmStmtNode() {
    316         delete output; delete input; delete clobber;
    317 }
    318 
    319 Statement *AsmStmtNode::build() const {
    320         std::list<Label> labs;
    321 
    322         if ( ! get_labels().empty() ) {
    323                 std::back_insert_iterator< std::list<Label> > lab_it( labs );
    324                 copy( get_labels().begin(), get_labels().end(), lab_it );
    325         } // if
    326 
     185Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {
    327186        std::list< Expression * > out, in;
    328187        std::list< ConstantExpr * > clob;
     188
    329189        buildList( output, out );
    330190        buildList( input, in );
    331191        buildList( clobber, clob );
    332         std::list< Label > gotolabs = gotolabels;
    333         return new AsmStmt( labs, voltile, instruction, out, in, clob, gotolabs );
     192        return new AsmStmt( noLabels, voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
    334193}
    335 
    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 // }
    352194
    353195// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.