source: src/Parser/StatementNode.cc@ dac593fd

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since dac593fd was 8cc5cb0, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

more refactoring of parser code

  • Property mode set to 100644
File size: 15.9 KB
RevLine 
[b87a5ed]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[0f8e4ac]7// StatementNode.cc --
[b87a5ed]8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 14:59:41 2015
[1db21619]11// Last Modified By : Peter A. Buhr
[8cc5cb0]12// Last Modified On : Thu Aug 11 16:19:45 2016
13// Update Count : 210
[b87a5ed]14//
15
[51b73452]16#include <list>
17#include <algorithm>
18#include <cassert>
19
20#include "ParseNode.h"
21#include "SynTree/Statement.h"
22#include "SynTree/Expression.h"
23#include "parseutility.h"
[d3b7937]24#include "Common/utility.h"
[51b73452]25
26using namespace std;
27
[b87a5ed]28const char *StatementNode::StType[] = {
[0f8e4ac]29 "Exp", "If", "Switch", "Case", "Default", "Choose", "Fallthru",
30 "While", "Do", "For",
[b87a5ed]31 "Goto", "Continue", "Break", "Return", "Throw",
32 "Try", "Catch", "Finally", "Asm",
33 "Decl"
34};
35
36StatementNode::StatementNode() : ParseNode(), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {}
37
[7f5566b]38StatementNode::StatementNode( const string *name ) : ParseNode( name ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {}
[b87a5ed]39
40StatementNode::StatementNode( DeclarationNode *decl ) : type( Decl ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), isCatchRest ( false ) {
41 if ( decl ) {
42 if ( DeclarationNode *agg = decl->extractAggregate() ) {
43 this->decl = agg;
44 StatementNode *nextStmt = new StatementNode;
45 nextStmt->type = Decl;
46 nextStmt->decl = decl;
47 next = nextStmt;
48 if ( decl->get_link() ) {
49 next->set_next( new StatementNode( dynamic_cast< DeclarationNode* >( decl->get_link() ) ) );
50 decl->set_next( 0 );
[44b5ca0]51 } // if
[b87a5ed]52 } else {
53 if ( decl->get_link() ) {
54 next = new StatementNode( dynamic_cast< DeclarationNode* >( decl->get_link() ) );
55 decl->set_next( 0 );
[44b5ca0]56 } // if
[b87a5ed]57 this->decl = decl;
[44b5ca0]58 } // if
59 } // if
[51b73452]60}
61
[7f5566b]62StatementNode::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;
[0f8e4ac]64}
[51b73452]65
[7f5566b]66StatementNode::StatementNode( Type t, string *target ) : type( t ), control( 0 ), block( 0 ), labels( 0 ), target( target ), decl( 0 ), isCatchRest ( false ) {}
[51b73452]67
[b87a5ed]68StatementNode::~StatementNode() {
69 delete control;
70 delete block;
71 delete target;
72 delete decl;
[51b73452]73}
74
[b87a5ed]75StatementNode * StatementNode::newCatchStmt( DeclarationNode *d, StatementNode *s, bool catchRestP ) {
[0f8e4ac]76 StatementNode *ret = new StatementNode( StatementNode::Catch, 0, s );
[b87a5ed]77 ret->addDeclaration( d );
78 ret->setCatchRest( catchRestP );
[51b73452]79
[b87a5ed]80 return ret;
[51b73452]81}
82
83std::string StatementNode::get_target() const{
[b87a5ed]84 if ( target )
85 return *target;
[51b73452]86
[b87a5ed]87 return string("");
[51b73452]88}
89
[b87a5ed]90StatementNode * 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;
[44b5ca0]96 } // if
[b87a5ed]97 newnode->decl = maybeClone( decl );
98 return newnode;
[51b73452]99}
100
[59db689]101StatementNode *StatementNode::add_label( const std::string *l ) {
[b87a5ed]102 if ( l != 0 ) {
[0f8e4ac]103 labels.push_front( *l );
[b87a5ed]104 delete l;
[44b5ca0]105 } // if
[b87a5ed]106 return this;
[51b73452]107}
108
[b87a5ed]109StatementNode *StatementNode::append_block( StatementNode *stmt ) {
110 if ( stmt != 0 ) {
111 if ( block == 0 )
112 block = stmt;
113 else
114 block->set_link( stmt );
[44b5ca0]115 } // if
[b87a5ed]116 return this;
[51b73452]117}
118
[b87a5ed]119StatementNode *StatementNode::append_last_case( StatementNode *stmt ) {
[8cc5cb0]120 assert( false );
[b87a5ed]121 if ( stmt != 0 ) {
122 StatementNode *next = ( StatementNode *)get_link();
123 if ( next && ( next->get_type() == StatementNode::Case || next->get_type() == StatementNode::Default ) )
[8cc5cb0]124 next->append_last_case( stmt );
[b87a5ed]125 else
126 if ( block == 0 )
127 block = stmt;
128 else
129 block->set_link( stmt );
[44b5ca0]130 } // if
[b87a5ed]131 return this;
[51b73452]132}
133
[8cc5cb0]134StatementNode *StatementNode2::append_last_case( StatementNode *stmt ) {
135 StatementNode *prev = this;
136 for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_link() ) {
137 StatementNode2 *node = dynamic_cast<StatementNode2 *>(curr);
138 assert( node );
139 assert( dynamic_cast<CaseStmt *>(node->stmt) );
140 prev = curr;
141 } // for
142 StatementNode2 *node = dynamic_cast<StatementNode2 *>(prev);
143 std::list<Statement *> stmts;
144 buildList( stmt, stmts );
145 CaseStmt * caseStmt;
146 caseStmt = dynamic_cast<CaseStmt *>(node->stmt);
147 caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
148 return this;
149}
150
[51b73452]151void StatementNode::print( std::ostream &os, int indent ) const {
[7f5566b]152 if ( ! labels.empty() ) {
[1db21619]153 std::list<std::string>::const_iterator i;
[b87a5ed]154
[1db21619]155 os << string( indent, ' ' );
156 for ( i = labels.begin(); i != labels.end(); i++ )
157 os << *i << ":";
158 os << endl;
[44b5ca0]159 } // if
[b87a5ed]160
[a32b204]161 switch ( type ) {
[b87a5ed]162 case Decl:
163 decl->print( os, indent );
164 break;
165 case Exp:
166 if ( control ) {
167 os << string( indent, ' ' );
168 control->print( os, indent );
169 os << endl;
[0f8e4ac]170 } else
[b87a5ed]171 os << string( indent, ' ' ) << "Null Statement" << endl;
172 break;
173 default:
[44b5ca0]174 os << string( indent, ' ' ) << StatementNode::StType[type] << endl;
[b87a5ed]175 if ( type == Catch ) {
176 if ( decl ) {
[44b5ca0]177 os << string( indent + ParseNode::indent_by, ' ' ) << "Declaration: " << endl;
[7f5566b]178 decl->print( os, indent + 2 * ParseNode::indent_by );
[b87a5ed]179 } else if ( isCatchRest ) {
[44b5ca0]180 os << string( indent + ParseNode::indent_by, ' ' ) << "Catches the rest " << endl;
[b87a5ed]181 } else {
182 ; // should never reach here
[44b5ca0]183 } // if
184 } // if
[b87a5ed]185 if ( control ) {
[7f5566b]186 os << string( indent + ParseNode::indent_by, ' ' ) << "Control: " << endl;
187 control->printList( os, indent + 2 * ParseNode::indent_by );
[44b5ca0]188 } // if
[b87a5ed]189 if ( block ) {
[7bf7fb9]190 os << string( indent + ParseNode::indent_by, ' ' ) << "Cases: " << endl;
[0f8e4ac]191 block->printList( os, indent + 2 * ParseNode::indent_by );
[44b5ca0]192 } // if
[b87a5ed]193 if ( target ) {
[44b5ca0]194 os << string( indent + ParseNode::indent_by, ' ' ) << "Target: " << get_target() << endl;
195 } // if
[b87a5ed]196 break;
[44b5ca0]197 } // switch
[51b73452]198}
199
200Statement *StatementNode::build() const {
[b87a5ed]201 std::list<Statement *> branches;
202 std::list<Expression *> exps;
203 std::list<Label> labs;
204
[1db21619]205 if ( ! labels.empty() ) {
[b87a5ed]206 std::back_insert_iterator< std::list<Label> > lab_it( labs );
[1db21619]207 copy( labels.begin(), labels.end(), lab_it );
[44b5ca0]208 } // if
[b87a5ed]209
210 // try {
211 buildList<Statement, StatementNode>( get_block(), branches );
212
[a32b204]213 switch ( type ) {
[b87a5ed]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 }
[8cc5cb0]225 assert( false );
[b87a5ed]226 case If:
[2f22cc4]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 );
240 case Switch:
[8cc5cb0]241 // return new SwitchStmt( labs, maybeBuild<Expression>(get_control()), branches );
[2f22cc4]242 assert( false );
243 case Case:
[8cc5cb0]244 //return new CaseStmt( labs, maybeBuild<Expression>(get_control() ), branches );
[321f55d]245 assert( false );
[2f22cc4]246 case Default:
[8cc5cb0]247 //return new CaseStmt( labs, 0, branches, true );
[321f55d]248 assert( false );
[b87a5ed]249 case While:
[2f22cc4]250 // assert( branches.size() == 1 );
251 // return new WhileStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), branches.front() );
252 assert( false );
[b87a5ed]253 case Do:
[2f22cc4]254 // assert( branches.size() == 1 );
255 // return new WhileStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), branches.front(), true );
256 assert( false );
[b87a5ed]257 case For:
[2f22cc4]258 // {
259 // assert( branches.size() == 1 );
[b87a5ed]260
[2f22cc4]261 // ForCtlExprNode *ctl = dynamic_cast<ForCtlExprNode *>( get_control() );
262 // assert( ctl != 0 );
[b87a5ed]263
[2f22cc4]264 // std::list<Statement *> init;
265 // if ( ctl->get_init() != 0 ) {
266 // buildList( ctl->get_init(), init );
267 // } // if
[b87a5ed]268
[2f22cc4]269 // Expression *cond = 0;
270 // if ( ctl->get_condition() != 0 )
271 // cond = notZeroExpr( maybeBuild<Expression>(ctl->get_condition()) );
[b87a5ed]272
[2f22cc4]273 // Expression *incr = 0;
274 // if ( ctl->get_change() != 0 )
275 // incr = maybeBuild<Expression>(ctl->get_change());
[b87a5ed]276
[2f22cc4]277 // return new ForStmt( labs, init, cond, incr, branches.front() );
278 // }
279 assert( false );
[b87a5ed]280 case Goto:
[8cc5cb0]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
[b87a5ed]286
[8cc5cb0]287 // return new BranchStmt( labs, get_target(), BranchStmt::Goto );
288 // }
289 assert( false );
[b87a5ed]290 case Break:
[8cc5cb0]291 // return new BranchStmt( labs, get_target(), BranchStmt::Break );
[321f55d]292 assert( false );
[b87a5ed]293 case Continue:
[8cc5cb0]294 // return new BranchStmt( labs, get_target(), BranchStmt::Continue );
[321f55d]295 assert( false );
[b87a5ed]296 case Return:
297 case Throw :
[8cc5cb0]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 );
303 assert( false );
[b87a5ed]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();
[44b5ca0]312 } // if
[b87a5ed]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 }
[7f5566b]329 case Asm:
330 assert( false );
[b87a5ed]331 default:
332 // shouldn't be here
333 return 0;
[44b5ca0]334 } // switch
[51b73452]335}
336
[8cc5cb0]337Statement *build_expr( ExpressionNode *ctl ) {
338 Expression *e = maybeBuild< Expression >( ctl );
339
340 if ( e )
341 return new ExprStmt( noLabels, e );
342 else
343 return new NullStmt( noLabels );
344}
345
[2f22cc4]346Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt ) {
347 Statement *thenb, *elseb = 0;
348 std::list<Statement *> branches;
349 buildList<Statement, StatementNode>( then_stmt, branches );
[321f55d]350 assert( branches.size() == 1 );
[2f22cc4]351 thenb = branches.front();
352
353 if ( else_stmt ) {
354 std::list<Statement *> branches;
355 buildList<Statement, StatementNode>( else_stmt, branches );
[321f55d]356 assert( branches.size() == 1 );
[2f22cc4]357 elseb = branches.front();
358 } // if
359 return new IfStmt( noLabels, notZeroExpr( maybeBuild<Expression>(ctl) ), thenb, elseb );
360}
361
362Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) {
363 std::list<Statement *> branches;
364 buildList<Statement, StatementNode>( stmt, branches );
[321f55d]365 assert( branches.size() >= 0 ); // size == 0 for switch (...) {}, i.e., no declaration or statements
[2f22cc4]366 return new SwitchStmt( noLabels, maybeBuild<Expression>(ctl), branches );
367}
[321f55d]368Statement *build_case( ExpressionNode *ctl ) {
369 std::list<Statement *> branches;
370 return new CaseStmt( noLabels, maybeBuild<Expression>(ctl), branches );
371}
372Statement *build_default() {
373 std::list<Statement *> branches;
374 return new CaseStmt( noLabels, nullptr, branches, true );
375}
[2f22cc4]376
377Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {
378 std::list<Statement *> branches;
379 buildList<Statement, StatementNode>( stmt, branches );
380 assert( branches.size() == 1 );
381 return new WhileStmt( noLabels, notZeroExpr( maybeBuild<Expression>(ctl) ), branches.front(), kind );
382}
383
384Statement *build_for( ForCtl *forctl, StatementNode *stmt ) {
385 std::list<Statement *> branches;
386 buildList<Statement, StatementNode>( stmt, branches );
387 assert( branches.size() == 1 );
388
389 std::list<Statement *> init;
390 if ( forctl->init != 0 ) {
391 buildList( forctl->init, init );
392 } // if
393
394 Expression *cond = 0;
395 if ( forctl->condition != 0 )
396 cond = notZeroExpr( maybeBuild<Expression>(forctl->condition) );
397
398 Expression *incr = 0;
399 if ( forctl->change != 0 )
400 incr = maybeBuild<Expression>(forctl->change);
401
402 delete forctl;
403 return new ForStmt( noLabels, init, cond, incr, branches.front() );
404}
405
[321f55d]406Statement *build_branch( std::string identifier, BranchStmt::Type kind ) {
407 return new BranchStmt( noLabels, identifier, kind );
408}
[8cc5cb0]409Statement *build_computedgoto( ExpressionNode *ctl ) {
410 return new BranchStmt( noLabels, maybeBuild<Expression>(ctl), BranchStmt::Goto );
411}
412
413Statement *build_return( ExpressionNode *ctl ) {
414 std::list<Expression *> exps;
415 buildList( ctl, exps );
416 return new ReturnStmt( noLabels, exps.size() > 0 ? exps.back() : nullptr );
417}
418Statement *build_throw( ExpressionNode *ctl ) {
419 std::list<Expression *> exps;
420 buildList( ctl, exps );
421 return new ReturnStmt( noLabels, exps.size() > 0 ? exps.back() : nullptr, true );
422}
[321f55d]423
[7f5566b]424
[59db689]425CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {}
[51b73452]426
[59db689]427CompoundStmtNode::CompoundStmtNode( const string *name_ ) : StatementNode( name_ ), first( 0 ), last( 0 ) {}
[51b73452]428
[59db689]429CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) {
[b87a5ed]430 if ( first ) {
431 last = ( StatementNode *)( stmt->get_last());
432 } else {
433 last = 0;
[44b5ca0]434 } // if
[51b73452]435}
436
[b87a5ed]437CompoundStmtNode::~CompoundStmtNode() {
438 delete first;
[51b73452]439}
440
[b87a5ed]441void CompoundStmtNode::add_statement( StatementNode *stmt ) {
442 if ( stmt != 0 ) {
443 last->set_link( stmt );
444 last = ( StatementNode *)( stmt->get_link());
[44b5ca0]445 } // if
[51b73452]446}
447
[b87a5ed]448void CompoundStmtNode::print( ostream &os, int indent ) const {
449 if ( first ) {
450 first->printList( os, indent+2 );
[44b5ca0]451 } // if
[51b73452]452}
453
454Statement *CompoundStmtNode::build() const {
[b87a5ed]455 std::list<Label> labs;
[1db21619]456 const std::list<std::string> &labels = get_labels();
[51b73452]457
[1db21619]458 if ( ! labels.empty() ) {
[b87a5ed]459 std::back_insert_iterator< std::list<Label> > lab_it( labs );
[1db21619]460 copy( labels.begin(), labels.end(), lab_it );
[44b5ca0]461 } // if
[51b73452]462
[b87a5ed]463 CompoundStmt *cs = new CompoundStmt( labs );
464 buildList( first, cs->get_kids() );
465 return cs;
[51b73452]466}
467
[7f5566b]468
[d1625f8]469AsmStmtNode::AsmStmtNode( Type t, bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) :
[7f5566b]470 StatementNode( t ), voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ) {
471 if ( gotolabels ) {
472 this->gotolabels = gotolabels->get_labels();
473 delete gotolabels;
474 } // if
475}
476
477AsmStmtNode::~AsmStmtNode() {
[d1625f8]478 delete output; delete input; delete clobber;
[7f5566b]479}
480
481void 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;
[d1625f8]486// instruction->printList( os, indent + 2 * ParseNode::indent_by );
[7f5566b]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;
[d1625f8]498// clobber->printList( os, indent + 2 * ParseNode::indent_by );
[7f5566b]499 } // if
500 if ( ! gotolabels.empty() ) {
501 os << string( indent + ParseNode::indent_by, ' ' ) << "Goto Labels:" << endl;
502 os << string( indent + 2 * ParseNode::indent_by, ' ' );
[0f8e4ac]503 for ( std::list<Label>::const_iterator i = gotolabels.begin();; ) {
[7f5566b]504 os << *i;
505 i++;
506 if ( i == gotolabels.end() ) break;
507 os << ", ";
508 }
509 os << endl;
510 } // if
511}
512
513Statement *AsmStmtNode::build() const {
514 std::list<Label> labs;
515
516 if ( ! get_labels().empty() ) {
517 std::back_insert_iterator< std::list<Label> > lab_it( labs );
518 copy( get_labels().begin(), get_labels().end(), lab_it );
519 } // if
520
521 std::list< Expression * > out, in;
522 std::list< ConstantExpr * > clob;
523 buildList( output, out );
524 buildList( input, in );
525 buildList( clobber, clob );
526 std::list< Label > gotolabs = gotolabels;
[d1625f8]527 return new AsmStmt( labs, voltile, instruction, out, in, clob, gotolabs );
[7f5566b]528}
529
[51b73452]530// Local Variables: //
[b87a5ed]531// tab-width: 4 //
532// mode: c++ //
533// compile-command: "make install" //
[51b73452]534// End: //
Note: See TracBrowser for help on using the repository browser.