[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 | //
|
---|
| 7 | // StatementNode.cc --
|
---|
| 8 | //
|
---|
| 9 | // Author : Rodolfo G. Esteves
|
---|
| 10 | // Created On : Sat May 16 14:59:41 2015
|
---|
| 11 | // Last Modified By : Peter A. Buhr
|
---|
[59db689] | 12 | // Last Modified On : Sat Jun 6 23:25:41 2015
|
---|
| 13 | // Update Count : 19
|
---|
[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"
|
---|
| 24 | #include "utility.h"
|
---|
| 25 |
|
---|
| 26 | using namespace std;
|
---|
| 27 |
|
---|
[b87a5ed] | 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 | };
|
---|
| 35 |
|
---|
| 36 | StatementNode::StatementNode() : ParseNode(), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {}
|
---|
| 37 |
|
---|
[59db689] | 38 | StatementNode::StatementNode( const string *name_ ) : ParseNode( name_ ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {}
|
---|
[b87a5ed] | 39 |
|
---|
| 40 | StatementNode::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 |
|
---|
[b87a5ed] | 62 | StatementNode::StatementNode( Type t, ExpressionNode *ctrl_label, StatementNode *block_ ) :
|
---|
| 63 | type( t ), control( ctrl_label ), block( block_), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {
|
---|
| 64 | if ( t == Default )
|
---|
| 65 | control = 0;
|
---|
[51b73452] | 66 | }
|
---|
| 67 |
|
---|
[b87a5ed] | 68 | StatementNode::StatementNode( Type t, string *_target ) :
|
---|
[59db689] | 69 | type( t ), control( 0 ), block( 0 ), labels( 0 ), target(_target ), decl( 0 ), isCatchRest ( false ) {}
|
---|
[51b73452] | 70 |
|
---|
[b87a5ed] | 71 | StatementNode::~StatementNode() {
|
---|
| 72 | delete control;
|
---|
| 73 | delete block;
|
---|
| 74 | delete labels;
|
---|
| 75 | delete target;
|
---|
| 76 | delete decl;
|
---|
[51b73452] | 77 | }
|
---|
| 78 |
|
---|
[b87a5ed] | 79 | StatementNode * StatementNode::newCatchStmt( DeclarationNode *d, StatementNode *s, bool catchRestP ) {
|
---|
| 80 | StatementNode *ret = new StatementNode( StatementNode::Catch, 0, s );
|
---|
| 81 | ret->addDeclaration( d );
|
---|
| 82 | ret->setCatchRest( catchRestP );
|
---|
[51b73452] | 83 |
|
---|
[b87a5ed] | 84 | return ret;
|
---|
[51b73452] | 85 | }
|
---|
| 86 |
|
---|
| 87 | std::string StatementNode::get_target() const{
|
---|
[b87a5ed] | 88 | if ( target )
|
---|
| 89 | return *target;
|
---|
[51b73452] | 90 |
|
---|
[b87a5ed] | 91 | return string("");
|
---|
[51b73452] | 92 | }
|
---|
| 93 |
|
---|
[b87a5ed] | 94 | StatementNode * StatementNode::clone() const {
|
---|
| 95 | StatementNode *newnode = new StatementNode( type, maybeClone( control ), maybeClone( block ) );
|
---|
| 96 | if ( target ) {
|
---|
| 97 | newnode->target = new string( *target );
|
---|
| 98 | } else {
|
---|
| 99 | newnode->target = 0;
|
---|
[44b5ca0] | 100 | } // if
|
---|
[b87a5ed] | 101 | newnode->decl = maybeClone( decl );
|
---|
| 102 | return newnode;
|
---|
[51b73452] | 103 | }
|
---|
| 104 |
|
---|
[b87a5ed] | 105 | void StatementNode::set_control( ExpressionNode *c ) {
|
---|
| 106 | control = c;
|
---|
[51b73452] | 107 | }
|
---|
| 108 |
|
---|
[b87a5ed] | 109 | StatementNode * StatementNode::set_block( StatementNode *b ) {
|
---|
| 110 | block = b;
|
---|
[51b73452] | 111 |
|
---|
[b87a5ed] | 112 | return this;
|
---|
[51b73452] | 113 | }
|
---|
| 114 |
|
---|
[b87a5ed] | 115 | ExpressionNode *StatementNode::get_control() const {
|
---|
| 116 | return control;
|
---|
[51b73452] | 117 | }
|
---|
| 118 |
|
---|
[b87a5ed] | 119 | StatementNode *StatementNode::get_block() const {
|
---|
| 120 | return block;
|
---|
[51b73452] | 121 | }
|
---|
| 122 |
|
---|
[b87a5ed] | 123 | StatementNode::Type StatementNode::get_type() const {
|
---|
| 124 | return type;
|
---|
[51b73452] | 125 | }
|
---|
| 126 |
|
---|
[59db689] | 127 | StatementNode *StatementNode::add_label( const std::string *l ) {
|
---|
[b87a5ed] | 128 | if ( l != 0 ) {
|
---|
| 129 | if ( labels == 0 )
|
---|
| 130 | labels = new std::list<std::string>();
|
---|
[51b73452] | 131 |
|
---|
[b87a5ed] | 132 | labels->push_front(*l );
|
---|
| 133 | delete l;
|
---|
[44b5ca0] | 134 | } // if
|
---|
[b87a5ed] | 135 | return this;
|
---|
[51b73452] | 136 | }
|
---|
| 137 |
|
---|
[b87a5ed] | 138 | std::list<std::string> *StatementNode::get_labels() const { return labels; }
|
---|
[51b73452] | 139 |
|
---|
[b87a5ed] | 140 | StatementNode *StatementNode::add_controlexp( ExpressionNode *e ) {
|
---|
| 141 | if ( control && e )
|
---|
| 142 | control->add_to_list( e ); // xxx - check this
|
---|
[51b73452] | 143 |
|
---|
[b87a5ed] | 144 | return this;
|
---|
[51b73452] | 145 | }
|
---|
| 146 |
|
---|
[b87a5ed] | 147 | StatementNode *StatementNode::append_block( StatementNode *stmt ) {
|
---|
| 148 | if ( stmt != 0 ) {
|
---|
| 149 | if ( block == 0 )
|
---|
| 150 | block = stmt;
|
---|
| 151 | else
|
---|
| 152 | block->set_link( stmt );
|
---|
[44b5ca0] | 153 | } // if
|
---|
[b87a5ed] | 154 | return this;
|
---|
[51b73452] | 155 | }
|
---|
| 156 |
|
---|
[b87a5ed] | 157 | StatementNode *StatementNode::append_last_case( StatementNode *stmt ) {
|
---|
| 158 | if ( stmt != 0 ) {
|
---|
| 159 | StatementNode *next = ( StatementNode *)get_link();
|
---|
| 160 | if ( next && ( next->get_type() == StatementNode::Case || next->get_type() == StatementNode::Default ) )
|
---|
| 161 | next->append_last_case ( stmt );
|
---|
| 162 | else
|
---|
| 163 | if ( block == 0 )
|
---|
| 164 | block = stmt;
|
---|
| 165 | else
|
---|
| 166 | block->set_link( stmt );
|
---|
[44b5ca0] | 167 | } // if
|
---|
[b87a5ed] | 168 | return this;
|
---|
[51b73452] | 169 | }
|
---|
| 170 |
|
---|
| 171 | void StatementNode::print( std::ostream &os, int indent ) const {
|
---|
[44b5ca0] | 172 | if ( labels != 0 ) {
|
---|
[a08ba92] | 173 | if ( ! labels->empty()) {
|
---|
[b87a5ed] | 174 | std::list<std::string>::const_iterator i;
|
---|
| 175 |
|
---|
[44b5ca0] | 176 | os << string( indent, ' ' );
|
---|
[a32b204] | 177 | for ( i = labels->begin(); i != labels->end(); i++ )
|
---|
[b87a5ed] | 178 | os << *i << ":";
|
---|
| 179 | os << endl;
|
---|
[44b5ca0] | 180 | } // if
|
---|
| 181 | } // if
|
---|
[b87a5ed] | 182 |
|
---|
[a32b204] | 183 | switch ( type ) {
|
---|
[b87a5ed] | 184 | case Decl:
|
---|
| 185 | decl->print( os, indent );
|
---|
| 186 | break;
|
---|
| 187 | case Exp:
|
---|
| 188 | if ( control ) {
|
---|
| 189 | os << string( indent, ' ' );
|
---|
| 190 | control->print( os, indent );
|
---|
| 191 | os << endl;
|
---|
| 192 | } else
|
---|
| 193 | os << string( indent, ' ' ) << "Null Statement" << endl;
|
---|
| 194 | break;
|
---|
| 195 | default:
|
---|
[44b5ca0] | 196 | os << string( indent, ' ' ) << StatementNode::StType[type] << endl;
|
---|
[b87a5ed] | 197 | if ( type == Catch ) {
|
---|
| 198 | if ( decl ) {
|
---|
[44b5ca0] | 199 | os << string( indent + ParseNode::indent_by, ' ' ) << "Declaration: " << endl;
|
---|
[b87a5ed] | 200 | decl->print( os, indent + 2*ParseNode::indent_by );
|
---|
| 201 | } else if ( isCatchRest ) {
|
---|
[44b5ca0] | 202 | os << string( indent + ParseNode::indent_by, ' ' ) << "Catches the rest " << endl;
|
---|
[b87a5ed] | 203 | } else {
|
---|
| 204 | ; // should never reach here
|
---|
[44b5ca0] | 205 | } // if
|
---|
| 206 | } // if
|
---|
[b87a5ed] | 207 | if ( control ) {
|
---|
[44b5ca0] | 208 | os << string( indent + ParseNode::indent_by, ' ' ) << "Expression: " << endl;
|
---|
[b87a5ed] | 209 | control->printList( os, indent + 2*ParseNode::indent_by );
|
---|
[44b5ca0] | 210 | } // if
|
---|
[b87a5ed] | 211 | if ( block ) {
|
---|
[44b5ca0] | 212 | os << string( indent + ParseNode::indent_by, ' ' ) << "Branches of execution: " << endl;
|
---|
[b87a5ed] | 213 | block->printList( os, indent + 2*ParseNode::indent_by );
|
---|
[44b5ca0] | 214 | } // if
|
---|
[b87a5ed] | 215 | if ( target ) {
|
---|
[44b5ca0] | 216 | os << string( indent + ParseNode::indent_by, ' ' ) << "Target: " << get_target() << endl;
|
---|
| 217 | } // if
|
---|
[b87a5ed] | 218 | break;
|
---|
[44b5ca0] | 219 | } // switch
|
---|
[51b73452] | 220 | }
|
---|
| 221 |
|
---|
| 222 | Statement *StatementNode::build() const {
|
---|
[b87a5ed] | 223 | std::list<Statement *> branches;
|
---|
| 224 | std::list<Expression *> exps;
|
---|
| 225 | std::list<Label> labs;
|
---|
| 226 |
|
---|
| 227 | if ( labels != 0 ) {
|
---|
| 228 | std::back_insert_iterator< std::list<Label> > lab_it( labs );
|
---|
| 229 | copy( labels->begin(), labels->end(), lab_it );
|
---|
[44b5ca0] | 230 | } // if
|
---|
[b87a5ed] | 231 |
|
---|
| 232 | // try {
|
---|
| 233 | buildList<Statement, StatementNode>( get_block(), branches );
|
---|
| 234 |
|
---|
[a32b204] | 235 | switch ( type ) {
|
---|
[b87a5ed] | 236 | case Decl:
|
---|
| 237 | return new DeclStmt( labs, maybeBuild< Declaration >( decl ) );
|
---|
| 238 | case Exp:
|
---|
| 239 | {
|
---|
| 240 | Expression *e = maybeBuild< Expression >( get_control() );
|
---|
| 241 |
|
---|
| 242 | if ( e )
|
---|
| 243 | return new ExprStmt( labs, e );
|
---|
| 244 | else
|
---|
| 245 | return new NullStmt( labs );
|
---|
| 246 | }
|
---|
| 247 | case If:
|
---|
| 248 | {
|
---|
| 249 | Statement *thenb = 0, *elseb = 0;
|
---|
| 250 | assert( branches.size() >= 1 );
|
---|
| 251 |
|
---|
| 252 | thenb = branches.front();
|
---|
| 253 | branches.pop_front();
|
---|
[a32b204] | 254 | if ( ! branches.empty() ) {
|
---|
[b87a5ed] | 255 | elseb = branches.front();
|
---|
| 256 | branches.pop_front();
|
---|
[44b5ca0] | 257 | } // if
|
---|
[b87a5ed] | 258 | return new IfStmt( labs, notZeroExpr( get_control()->build() ), thenb, elseb );
|
---|
| 259 | }
|
---|
| 260 | case While:
|
---|
| 261 | assert( branches.size() == 1 );
|
---|
| 262 | return new WhileStmt( labs, notZeroExpr( get_control()->build() ), branches.front() );
|
---|
| 263 | case Do:
|
---|
| 264 | assert( branches.size() == 1 );
|
---|
| 265 | return new WhileStmt( labs, notZeroExpr( get_control()->build() ), branches.front(), true );
|
---|
| 266 | case For:
|
---|
| 267 | {
|
---|
| 268 | assert( branches.size() == 1 );
|
---|
| 269 |
|
---|
| 270 | ForCtlExprNode *ctl = dynamic_cast<ForCtlExprNode *>( get_control() );
|
---|
| 271 | assert( ctl != 0 );
|
---|
| 272 |
|
---|
| 273 | Statement *stmt = 0;
|
---|
| 274 | if ( ctl->get_init() != 0 )
|
---|
| 275 | stmt = ctl->get_init()->build();
|
---|
| 276 |
|
---|
| 277 | Expression *cond = 0;
|
---|
| 278 | if ( ctl->get_condition() != 0 )
|
---|
| 279 | cond = notZeroExpr( ctl->get_condition()->build() );
|
---|
| 280 |
|
---|
| 281 | Expression *incr = 0;
|
---|
| 282 | if ( ctl->get_change() != 0 )
|
---|
| 283 | incr = ctl->get_change()->build();
|
---|
| 284 |
|
---|
| 285 | return new ForStmt( labs, stmt, cond, incr, branches.front() );
|
---|
| 286 | }
|
---|
| 287 | case Switch:
|
---|
| 288 | return new SwitchStmt( labs, get_control()->build(), branches );
|
---|
| 289 | case Choose:
|
---|
| 290 | return new ChooseStmt( labs, get_control()->build(), branches );
|
---|
| 291 | case Fallthru:
|
---|
| 292 | return new FallthruStmt( labs );
|
---|
| 293 | case Case:
|
---|
| 294 | return new CaseStmt( labs, get_control()->build(), branches );
|
---|
| 295 | case Default:
|
---|
| 296 | return new CaseStmt( labs, 0, branches, true );
|
---|
| 297 | case Goto:
|
---|
| 298 | {
|
---|
| 299 | if ( get_target() == "" ) { // computed goto
|
---|
| 300 | assert( get_control() != 0 );
|
---|
| 301 | return new BranchStmt( labs, get_control()->build(), BranchStmt::Goto );
|
---|
[44b5ca0] | 302 | } // if
|
---|
[b87a5ed] | 303 |
|
---|
| 304 | return new BranchStmt( labs, get_target(), BranchStmt::Goto );
|
---|
| 305 | }
|
---|
| 306 | case Break:
|
---|
| 307 | return new BranchStmt( labs, get_target(), BranchStmt::Break );
|
---|
| 308 | case Continue:
|
---|
| 309 | return new BranchStmt( labs, get_target(), BranchStmt::Continue );
|
---|
| 310 | case Return:
|
---|
| 311 | case Throw :
|
---|
| 312 | buildList( get_control(), exps );
|
---|
| 313 | if ( exps.size() ==0 )
|
---|
| 314 | return new ReturnStmt( labs, 0, type == Throw );
|
---|
| 315 | if ( exps.size() > 0 )
|
---|
| 316 | return new ReturnStmt( labs, exps.back(), type == Throw );
|
---|
| 317 | case Try:
|
---|
| 318 | {
|
---|
| 319 | assert( branches.size() >= 0 );
|
---|
| 320 | CompoundStmt *tryBlock = dynamic_cast<CompoundStmt *>( branches.front());
|
---|
| 321 | branches.pop_front();
|
---|
| 322 | FinallyStmt *finallyBlock = 0;
|
---|
| 323 | if ( ( finallyBlock = dynamic_cast<FinallyStmt *>( branches.back())) ) {
|
---|
| 324 | branches.pop_back();
|
---|
[44b5ca0] | 325 | } // if
|
---|
[b87a5ed] | 326 | return new TryStmt( labs, tryBlock, branches, finallyBlock );
|
---|
| 327 | }
|
---|
| 328 | case Catch:
|
---|
| 329 | {
|
---|
| 330 | assert( branches.size() == 1 );
|
---|
| 331 |
|
---|
| 332 | return new CatchStmt( labs, maybeBuild< Declaration >( decl ), branches.front(), isCatchRest );
|
---|
| 333 | }
|
---|
| 334 | case Finally:
|
---|
| 335 | {
|
---|
| 336 | assert( branches.size() == 1 );
|
---|
| 337 | CompoundStmt *block = dynamic_cast<CompoundStmt *>( branches.front() );
|
---|
| 338 | assert( block != 0 );
|
---|
| 339 |
|
---|
| 340 | return new FinallyStmt( labs, block );
|
---|
| 341 | }
|
---|
| 342 | default:
|
---|
| 343 | // shouldn't be here
|
---|
| 344 | return 0;
|
---|
[44b5ca0] | 345 | } // switch
|
---|
[51b73452] | 346 | }
|
---|
| 347 |
|
---|
[59db689] | 348 | CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {}
|
---|
[51b73452] | 349 |
|
---|
[59db689] | 350 | CompoundStmtNode::CompoundStmtNode( const string *name_ ) : StatementNode( name_ ), first( 0 ), last( 0 ) {}
|
---|
[51b73452] | 351 |
|
---|
[59db689] | 352 | CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) {
|
---|
[b87a5ed] | 353 | if ( first ) {
|
---|
| 354 | last = ( StatementNode *)( stmt->get_last());
|
---|
| 355 | } else {
|
---|
| 356 | last = 0;
|
---|
[44b5ca0] | 357 | } // if
|
---|
[51b73452] | 358 | }
|
---|
| 359 |
|
---|
[b87a5ed] | 360 | CompoundStmtNode::~CompoundStmtNode() {
|
---|
| 361 | delete first;
|
---|
[51b73452] | 362 | }
|
---|
| 363 |
|
---|
[b87a5ed] | 364 | void CompoundStmtNode::add_statement( StatementNode *stmt ) {
|
---|
| 365 | if ( stmt != 0 ) {
|
---|
| 366 | last->set_link( stmt );
|
---|
| 367 | last = ( StatementNode *)( stmt->get_link());
|
---|
[44b5ca0] | 368 | } // if
|
---|
[51b73452] | 369 | }
|
---|
| 370 |
|
---|
[b87a5ed] | 371 | void CompoundStmtNode::print( ostream &os, int indent ) const {
|
---|
| 372 | if ( first ) {
|
---|
| 373 | first->printList( os, indent+2 );
|
---|
[44b5ca0] | 374 | } // if
|
---|
[51b73452] | 375 | }
|
---|
| 376 |
|
---|
| 377 | Statement *CompoundStmtNode::build() const {
|
---|
[b87a5ed] | 378 | std::list<Label> labs;
|
---|
| 379 | std::list<std::string> *labels = get_labels();
|
---|
[51b73452] | 380 |
|
---|
[b87a5ed] | 381 | if ( labels != 0 ) {
|
---|
| 382 | std::back_insert_iterator< std::list<Label> > lab_it( labs );
|
---|
| 383 | copy( labels->begin(), labels->end(), lab_it );
|
---|
[44b5ca0] | 384 | } // if
|
---|
[51b73452] | 385 |
|
---|
[b87a5ed] | 386 | CompoundStmt *cs = new CompoundStmt( labs );
|
---|
| 387 | buildList( first, cs->get_kids() );
|
---|
| 388 | return cs;
|
---|
[51b73452] | 389 | }
|
---|
| 390 |
|
---|
[b87a5ed] | 391 | void NullStmtNode::print( ostream &os, int indent ) const {
|
---|
[44b5ca0] | 392 | os << string( indent, ' ' ) << "Null Statement:" << endl;
|
---|
[51b73452] | 393 | }
|
---|
| 394 |
|
---|
| 395 | Statement *NullStmtNode::build() const {
|
---|
[b87a5ed] | 396 | return new NullStmt;
|
---|
[51b73452] | 397 | }
|
---|
| 398 |
|
---|
| 399 | // Local Variables: //
|
---|
[b87a5ed] | 400 | // tab-width: 4 //
|
---|
| 401 | // mode: c++ //
|
---|
| 402 | // compile-command: "make install" //
|
---|
[51b73452] | 403 | // End: //
|
---|