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