Changeset 1d4580a for src/Parser/StatementNode.cc
- Timestamp:
- Aug 14, 2016, 8:36:29 AM (8 years ago)
- 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:
- 777bfcf
- Parents:
- 46b4259
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/StatementNode.cc
r46b4259 r1d4580a 10 10 // Created On : Sat May 16 14:59:41 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Aug 11 16:19:45201613 // Update Count : 2 1012 // Last Modified On : Sun Aug 14 08:09:31 2016 13 // Update Count : 271 14 14 // 15 15 … … 34 34 }; 35 35 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 ) { 36 StatementNode::StatementNode() : ParseNode(), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), catchAny( false ) {} 37 38 StatementNode::StatementNode( const string *name ) : ParseNode( name ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), catchAny( false ) { assert( false ); } 39 40 StatementNode::StatementNode( DeclarationNode *decl ) : type( Decl ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), catchAny( false ) { 41 assert( false ); 41 42 if ( decl ) { 42 43 if ( DeclarationNode *agg = decl->extractAggregate() ) { … … 46 47 nextStmt->decl = decl; 47 48 next = nextStmt; 48 if ( decl->get_ link() ) {49 next->set_next( new StatementNode( dynamic_cast< DeclarationNode* >( decl->get_link() ) ) );49 if ( decl->get_next() ) { 50 next->set_next( new StatementNode( dynamic_cast<DeclarationNode *>( decl->get_next() ) ) ); 50 51 decl->set_next( 0 ); 51 52 } // if 52 53 } else { 53 if ( decl->get_ link() ) {54 next = new StatementNode( dynamic_cast< DeclarationNode* >( decl->get_link() ) );54 if ( decl->get_next() ) { 55 next = new StatementNode( dynamic_cast<DeclarationNode *>( decl->get_next() ) ); 55 56 decl->set_next( 0 ); 56 57 } // if … … 60 61 } 61 62 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 StatementNode2::StatementNode2( DeclarationNode *decl ) { 64 if ( decl ) { 65 DeclarationNode *agg = decl->extractAggregate(); 66 if ( agg ) { 67 StatementNode *nextStmt = new StatementNode; 68 nextStmt->type = Decl; 69 nextStmt->decl = decl; 70 next = nextStmt; 71 if ( decl->get_next() ) { 72 next->set_next( new StatementNode2( dynamic_cast<DeclarationNode *>(decl->get_next()) ) ); 73 decl->set_next( 0 ); 74 } // if 75 } else { 76 if ( decl->get_next() ) { 77 next = new StatementNode2( dynamic_cast<DeclarationNode *>( decl->get_next() ) ); 78 decl->set_next( 0 ); 79 } // if 80 agg = decl; 81 } // if 82 stmt = new DeclStmt( noLabels, maybeBuild<Declaration>(agg) ); 83 } else { 84 assert( false ); 85 } // if 86 } 87 88 StatementNode::StatementNode( Type t, ExpressionNode *ctrl_label, StatementNode *block ) : type( t ), control( ctrl_label ), block( block ), labels( 0 ), target( 0 ), decl( 0 ), catchAny( false ) { 63 89 this->control = ( t == Default ) ? 0 : control; 64 90 } 65 91 66 StatementNode::StatementNode( Type t, string *target ) : type( t ), control( 0 ), block( 0 ), labels( 0 ), target( target ), decl( 0 ), isCatchRest( false ) {}92 StatementNode::StatementNode( Type t, string *target ) : type( t ), control( 0 ), block( 0 ), labels( 0 ), target( target ), decl( 0 ), catchAny( false ) {} 67 93 68 94 StatementNode::~StatementNode() { … … 73 99 } 74 100 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 90 101 StatementNode * StatementNode::clone() const { 102 assert( false ); 91 103 StatementNode *newnode = new StatementNode( type, maybeClone( control ), maybeClone( block ) ); 92 104 if ( target ) { … … 112 124 block = stmt; 113 125 else 114 block->set_l ink( stmt );126 block->set_last( stmt ); 115 127 } // if 116 128 return this; … … 120 132 assert( false ); 121 133 if ( stmt != 0 ) { 122 StatementNode *next = ( StatementNode *)get_ link();134 StatementNode *next = ( StatementNode *)get_next(); 123 135 if ( next && ( next->get_type() == StatementNode::Case || next->get_type() == StatementNode::Default ) ) 124 136 next->append_last_case( stmt ); … … 127 139 block = stmt; 128 140 else 129 block->set_l ink( stmt );141 block->set_last( stmt ); 130 142 } // if 131 143 return this; … … 134 146 StatementNode *StatementNode2::append_last_case( StatementNode *stmt ) { 135 147 StatementNode *prev = this; 136 for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_link() ) { 148 // find end of list and maintain previous pointer 149 for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) { 137 150 StatementNode2 *node = dynamic_cast<StatementNode2 *>(curr); 138 151 assert( node ); … … 140 153 prev = curr; 141 154 } // for 155 // conver from StatementNode list to Statement list 142 156 StatementNode2 *node = dynamic_cast<StatementNode2 *>(prev); 143 157 std::list<Statement *> stmts; 144 158 buildList( stmt, stmts ); 145 CaseStmt * caseStmt;146 caseStmt = dynamic_cast<CaseStmt *>(node->stmt);159 // splice any new Statements to end of currents Statements 160 CaseStmt * caseStmt = dynamic_cast<CaseStmt *>(node->stmt); 147 161 caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts ); 148 162 return this; … … 177 191 os << string( indent + ParseNode::indent_by, ' ' ) << "Declaration: " << endl; 178 192 decl->print( os, indent + 2 * ParseNode::indent_by ); 179 } else if ( isCatchRest) {193 } else if ( catchAny ) { 180 194 os << string( indent + ParseNode::indent_by, ' ' ) << "Catches the rest " << endl; 181 195 } else { … … 191 205 block->printList( os, indent + 2 * ParseNode::indent_by ); 192 206 } // if 193 if ( target ) {194 os << string( indent + ParseNode::indent_by, ' ' ) << "Target: " << get_target() << endl;195 } // if196 207 break; 197 208 } // switch … … 208 219 } // if 209 220 210 // try {211 221 buildList<Statement, StatementNode>( get_block(), branches ); 212 222 … … 214 224 case Decl: 215 225 return new DeclStmt( labs, maybeBuild< Declaration >( decl ) ); 226 assert( false ); 216 227 case Exp: 217 {218 Expression *e = maybeBuild< Expression >( get_control() );219 220 if ( e )221 return new ExprStmt( labs, e );222 else223 return new NullStmt( labs );224 }228 // { 229 // Expression *e = maybeBuild< Expression >( get_control() ); 230 231 // if ( e ) 232 // return new ExprStmt( labs, e ); 233 // else 234 // return new NullStmt( labs ); 235 // } 225 236 assert( false ); 226 237 case If: … … 279 290 assert( false ); 280 291 case Goto: 281 // {282 // if ( get_target() == "" ) { // computed goto283 // assert( get_control() != 0 );284 // return new BranchStmt( labs, maybeBuild<Expression>(get_control()), BranchStmt::Goto );285 // } // if286 287 // return new BranchStmt( labs, get_target(), BranchStmt::Goto );288 // }289 292 assert( false ); 290 293 case Break: 291 // return new BranchStmt( labs, get_target(), BranchStmt::Break );292 294 assert( false ); 293 295 case Continue: 294 // return new BranchStmt( labs, get_target(), BranchStmt::Continue );295 296 assert( false ); 296 297 case Return: … … 303 304 assert( false ); 304 305 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 } 306 // { 307 // assert( branches.size() >= 0 ); 308 // CompoundStmt *tryBlock = dynamic_cast<CompoundStmt *>( branches.front()); 309 // branches.pop_front(); 310 // FinallyStmt *finallyBlock = 0; 311 // if ( ( finallyBlock = dynamic_cast<FinallyStmt *>( branches.back())) ) { 312 // branches.pop_back(); 313 // } // if 314 // return new TryStmt( labs, tryBlock, branches, finallyBlock ); 315 // } 316 assert( false ); 315 317 case Catch: 316 { 317 assert( branches.size() == 1 ); 318 319 return new CatchStmt( labs, maybeBuild< Declaration >( decl ), branches.front(), isCatchRest ); 320 } 318 // { 319 // assert( branches.size() == 1 ); 320 321 // return new CatchStmt( labs, maybeBuild< Declaration >( decl ), branches.front(), catchAny ); 322 // } 323 assert( false ); 321 324 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 } 325 // { 326 // assert( branches.size() == 1 ); 327 // CompoundStmt *block = dynamic_cast<CompoundStmt *>( branches.front() ); 328 // assert( block != 0 ); 329 330 // return new FinallyStmt( labs, block ); 331 // } 332 assert( false ); 329 333 case Asm: 330 334 assert( false ); 331 335 default: 332 // shouldn't be here336 assert( false ); 333 337 return 0; 334 338 } // switch … … 422 426 } 423 427 428 Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) { 429 std::list<Statement *> branches; 430 buildList<Statement, StatementNode>( catch_stmt, branches ); 431 CompoundStmt *tryBlock = dynamic_cast<CompoundStmt *>(maybeBuild<Statement>(try_stmt)); 432 assert( tryBlock ); 433 FinallyStmt *finallyBlock = dynamic_cast<FinallyStmt *>(maybeBuild<Statement>(finally_stmt) ); 434 return new TryStmt( noLabels, tryBlock, branches, finallyBlock ); 435 } 436 Statement *build_catch( DeclarationNode *decl, StatementNode *stmt, bool catchAny ) { 437 std::list<Statement *> branches; 438 buildList<Statement, StatementNode>( stmt, branches ); 439 assert( branches.size() == 1 ); 440 return new CatchStmt( noLabels, maybeBuild<Declaration>(decl), branches.front(), catchAny ); 441 } 442 Statement *build_finally( StatementNode *stmt ) { 443 std::list<Statement *> branches; 444 buildList<Statement, StatementNode>( stmt, branches ); 445 assert( branches.size() == 1 ); 446 return new FinallyStmt( noLabels, dynamic_cast<CompoundStmt *>( branches.front() ) ); 447 } 448 424 449 425 450 CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {} 426 427 CompoundStmtNode::CompoundStmtNode( const string *name_ ) : StatementNode( name_ ), first( 0 ), last( 0 ) {}428 451 429 452 CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) { … … 441 464 void CompoundStmtNode::add_statement( StatementNode *stmt ) { 442 465 if ( stmt != 0 ) { 443 last->set_l ink( stmt );444 last = ( StatementNode *)( stmt->get_ link());466 last->set_last( stmt ); 467 last = ( StatementNode *)( stmt->get_next()); 445 468 } // if 446 469 }
Note: See TracChangeset
for help on using the changeset viewer.