source: src/Parser/StatementNode.cc@ ce2fed5

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 ce2fed5 was 321f55d, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

more refactoring of parser code

  • Property mode set to 100644
File size: 14.6 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
[321f55d]12// Last Modified On : Wed Aug 10 22:08:38 2016
13// Update Count : 173
[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 ) {
120 if ( stmt != 0 ) {
121 StatementNode *next = ( StatementNode *)get_link();
122 if ( next && ( next->get_type() == StatementNode::Case || next->get_type() == StatementNode::Default ) )
123 next->append_last_case ( stmt );
124 else
125 if ( block == 0 )
126 block = stmt;
127 else
128 block->set_link( stmt );
[44b5ca0]129 } // if
[b87a5ed]130 return this;
[51b73452]131}
132
133void StatementNode::print( std::ostream &os, int indent ) const {
[7f5566b]134 if ( ! labels.empty() ) {
[1db21619]135 std::list<std::string>::const_iterator i;
[b87a5ed]136
[1db21619]137 os << string( indent, ' ' );
138 for ( i = labels.begin(); i != labels.end(); i++ )
139 os << *i << ":";
140 os << endl;
[44b5ca0]141 } // if
[b87a5ed]142
[a32b204]143 switch ( type ) {
[b87a5ed]144 case Decl:
145 decl->print( os, indent );
146 break;
147 case Exp:
148 if ( control ) {
149 os << string( indent, ' ' );
150 control->print( os, indent );
151 os << endl;
[0f8e4ac]152 } else
[b87a5ed]153 os << string( indent, ' ' ) << "Null Statement" << endl;
154 break;
155 default:
[44b5ca0]156 os << string( indent, ' ' ) << StatementNode::StType[type] << endl;
[b87a5ed]157 if ( type == Catch ) {
158 if ( decl ) {
[44b5ca0]159 os << string( indent + ParseNode::indent_by, ' ' ) << "Declaration: " << endl;
[7f5566b]160 decl->print( os, indent + 2 * ParseNode::indent_by );
[b87a5ed]161 } else if ( isCatchRest ) {
[44b5ca0]162 os << string( indent + ParseNode::indent_by, ' ' ) << "Catches the rest " << endl;
[b87a5ed]163 } else {
164 ; // should never reach here
[44b5ca0]165 } // if
166 } // if
[b87a5ed]167 if ( control ) {
[7f5566b]168 os << string( indent + ParseNode::indent_by, ' ' ) << "Control: " << endl;
169 control->printList( os, indent + 2 * ParseNode::indent_by );
[44b5ca0]170 } // if
[b87a5ed]171 if ( block ) {
[7bf7fb9]172 os << string( indent + ParseNode::indent_by, ' ' ) << "Cases: " << endl;
[0f8e4ac]173 block->printList( os, indent + 2 * ParseNode::indent_by );
[44b5ca0]174 } // if
[b87a5ed]175 if ( target ) {
[44b5ca0]176 os << string( indent + ParseNode::indent_by, ' ' ) << "Target: " << get_target() << endl;
177 } // if
[b87a5ed]178 break;
[44b5ca0]179 } // switch
[51b73452]180}
181
182Statement *StatementNode::build() const {
[b87a5ed]183 std::list<Statement *> branches;
184 std::list<Expression *> exps;
185 std::list<Label> labs;
186
[1db21619]187 if ( ! labels.empty() ) {
[b87a5ed]188 std::back_insert_iterator< std::list<Label> > lab_it( labs );
[1db21619]189 copy( labels.begin(), labels.end(), lab_it );
[44b5ca0]190 } // if
[b87a5ed]191
192 // try {
193 buildList<Statement, StatementNode>( get_block(), branches );
194
[a32b204]195 switch ( type ) {
[b87a5ed]196 case Decl:
197 return new DeclStmt( labs, maybeBuild< Declaration >( decl ) );
198 case Exp:
199 {
200 Expression *e = maybeBuild< Expression >( get_control() );
201
202 if ( e )
203 return new ExprStmt( labs, e );
204 else
205 return new NullStmt( labs );
206 }
207 case If:
[2f22cc4]208 // {
209 // Statement *thenb = 0, *elseb = 0;
210 // assert( branches.size() >= 1 );
211
212 // thenb = branches.front();
213 // branches.pop_front();
214 // if ( ! branches.empty() ) {
215 // elseb = branches.front();
216 // branches.pop_front();
217 // } // if
218 // return new IfStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), thenb, elseb );
219 // }
220 assert( false );
221 case Switch:
222 //return new SwitchStmt( labs, maybeBuild<Expression>(get_control()), branches );
223 assert( false );
224 case Case:
225 return new CaseStmt( labs, maybeBuild<Expression>(get_control() ), branches );
[321f55d]226 assert( false );
[2f22cc4]227 case Default:
228 return new CaseStmt( labs, 0, branches, true );
[321f55d]229 assert( false );
[b87a5ed]230 case While:
[2f22cc4]231 // assert( branches.size() == 1 );
232 // return new WhileStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), branches.front() );
233 assert( false );
[b87a5ed]234 case Do:
[2f22cc4]235 // assert( branches.size() == 1 );
236 // return new WhileStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), branches.front(), true );
237 assert( false );
[b87a5ed]238 case For:
[2f22cc4]239 // {
240 // assert( branches.size() == 1 );
[b87a5ed]241
[2f22cc4]242 // ForCtlExprNode *ctl = dynamic_cast<ForCtlExprNode *>( get_control() );
243 // assert( ctl != 0 );
[b87a5ed]244
[2f22cc4]245 // std::list<Statement *> init;
246 // if ( ctl->get_init() != 0 ) {
247 // buildList( ctl->get_init(), init );
248 // } // if
[b87a5ed]249
[2f22cc4]250 // Expression *cond = 0;
251 // if ( ctl->get_condition() != 0 )
252 // cond = notZeroExpr( maybeBuild<Expression>(ctl->get_condition()) );
[b87a5ed]253
[2f22cc4]254 // Expression *incr = 0;
255 // if ( ctl->get_change() != 0 )
256 // incr = maybeBuild<Expression>(ctl->get_change());
[b87a5ed]257
[2f22cc4]258 // return new ForStmt( labs, init, cond, incr, branches.front() );
259 // }
260 assert( false );
[b87a5ed]261 case Goto:
262 {
263 if ( get_target() == "" ) { // computed goto
264 assert( get_control() != 0 );
[e04ef3a]265 return new BranchStmt( labs, maybeBuild<Expression>(get_control()), BranchStmt::Goto );
[44b5ca0]266 } // if
[b87a5ed]267
268 return new BranchStmt( labs, get_target(), BranchStmt::Goto );
269 }
270 case Break:
271 return new BranchStmt( labs, get_target(), BranchStmt::Break );
[321f55d]272 assert( false );
[b87a5ed]273 case Continue:
274 return new BranchStmt( labs, get_target(), BranchStmt::Continue );
[321f55d]275 assert( false );
[b87a5ed]276 case Return:
277 case Throw :
278 buildList( get_control(), exps );
279 if ( exps.size() ==0 )
280 return new ReturnStmt( labs, 0, type == Throw );
281 if ( exps.size() > 0 )
282 return new ReturnStmt( labs, exps.back(), type == Throw );
283 case Try:
284 {
285 assert( branches.size() >= 0 );
286 CompoundStmt *tryBlock = dynamic_cast<CompoundStmt *>( branches.front());
287 branches.pop_front();
288 FinallyStmt *finallyBlock = 0;
289 if ( ( finallyBlock = dynamic_cast<FinallyStmt *>( branches.back())) ) {
290 branches.pop_back();
[44b5ca0]291 } // if
[b87a5ed]292 return new TryStmt( labs, tryBlock, branches, finallyBlock );
293 }
294 case Catch:
295 {
296 assert( branches.size() == 1 );
297
298 return new CatchStmt( labs, maybeBuild< Declaration >( decl ), branches.front(), isCatchRest );
299 }
300 case Finally:
301 {
302 assert( branches.size() == 1 );
303 CompoundStmt *block = dynamic_cast<CompoundStmt *>( branches.front() );
304 assert( block != 0 );
305
306 return new FinallyStmt( labs, block );
307 }
[7f5566b]308 case Asm:
309 assert( false );
[b87a5ed]310 default:
311 // shouldn't be here
312 return 0;
[44b5ca0]313 } // switch
[51b73452]314}
315
[2f22cc4]316Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt ) {
317 Statement *thenb, *elseb = 0;
318 std::list<Statement *> branches;
319 buildList<Statement, StatementNode>( then_stmt, branches );
[321f55d]320 assert( branches.size() == 1 );
[2f22cc4]321 thenb = branches.front();
322
323 if ( else_stmt ) {
324 std::list<Statement *> branches;
325 buildList<Statement, StatementNode>( else_stmt, branches );
[321f55d]326 assert( branches.size() == 1 );
[2f22cc4]327 elseb = branches.front();
328 } // if
329 return new IfStmt( noLabels, notZeroExpr( maybeBuild<Expression>(ctl) ), thenb, elseb );
330}
331
332Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) {
333 std::list<Statement *> branches;
334 buildList<Statement, StatementNode>( stmt, branches );
[321f55d]335 assert( branches.size() >= 0 ); // size == 0 for switch (...) {}, i.e., no declaration or statements
[2f22cc4]336 return new SwitchStmt( noLabels, maybeBuild<Expression>(ctl), branches );
337}
[321f55d]338Statement *build_case( ExpressionNode *ctl ) {
339 std::list<Statement *> branches;
340 buildList<Statement, StatementNode>( nullptr, branches );
341 return new CaseStmt( noLabels, maybeBuild<Expression>(ctl), branches );
342}
343Statement *build_default() {
344 std::list<Statement *> branches;
345 return new CaseStmt( noLabels, nullptr, branches, true );
346}
[2f22cc4]347
348Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {
349 std::list<Statement *> branches;
350 buildList<Statement, StatementNode>( stmt, branches );
351 assert( branches.size() == 1 );
352 return new WhileStmt( noLabels, notZeroExpr( maybeBuild<Expression>(ctl) ), branches.front(), kind );
353}
354
355Statement *build_for( ForCtl *forctl, StatementNode *stmt ) {
356 std::list<Statement *> branches;
357 buildList<Statement, StatementNode>( stmt, branches );
358 assert( branches.size() == 1 );
359
360 std::list<Statement *> init;
361 if ( forctl->init != 0 ) {
362 buildList( forctl->init, init );
363 } // if
364
365 Expression *cond = 0;
366 if ( forctl->condition != 0 )
367 cond = notZeroExpr( maybeBuild<Expression>(forctl->condition) );
368
369 Expression *incr = 0;
370 if ( forctl->change != 0 )
371 incr = maybeBuild<Expression>(forctl->change);
372
373 delete forctl;
374 return new ForStmt( noLabels, init, cond, incr, branches.front() );
375}
376
[321f55d]377Statement *build_branch( std::string identifier, BranchStmt::Type kind ) {
378 return new BranchStmt( noLabels, identifier, kind );
379}
380
[7f5566b]381
[59db689]382CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {}
[51b73452]383
[59db689]384CompoundStmtNode::CompoundStmtNode( const string *name_ ) : StatementNode( name_ ), first( 0 ), last( 0 ) {}
[51b73452]385
[59db689]386CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) {
[b87a5ed]387 if ( first ) {
388 last = ( StatementNode *)( stmt->get_last());
389 } else {
390 last = 0;
[44b5ca0]391 } // if
[51b73452]392}
393
[b87a5ed]394CompoundStmtNode::~CompoundStmtNode() {
395 delete first;
[51b73452]396}
397
[b87a5ed]398void CompoundStmtNode::add_statement( StatementNode *stmt ) {
399 if ( stmt != 0 ) {
400 last->set_link( stmt );
401 last = ( StatementNode *)( stmt->get_link());
[44b5ca0]402 } // if
[51b73452]403}
404
[b87a5ed]405void CompoundStmtNode::print( ostream &os, int indent ) const {
406 if ( first ) {
407 first->printList( os, indent+2 );
[44b5ca0]408 } // if
[51b73452]409}
410
411Statement *CompoundStmtNode::build() const {
[b87a5ed]412 std::list<Label> labs;
[1db21619]413 const std::list<std::string> &labels = get_labels();
[51b73452]414
[1db21619]415 if ( ! labels.empty() ) {
[b87a5ed]416 std::back_insert_iterator< std::list<Label> > lab_it( labs );
[1db21619]417 copy( labels.begin(), labels.end(), lab_it );
[44b5ca0]418 } // if
[51b73452]419
[b87a5ed]420 CompoundStmt *cs = new CompoundStmt( labs );
421 buildList( first, cs->get_kids() );
422 return cs;
[51b73452]423}
424
[7f5566b]425
[d1625f8]426AsmStmtNode::AsmStmtNode( Type t, bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) :
[7f5566b]427 StatementNode( t ), voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ) {
428 if ( gotolabels ) {
429 this->gotolabels = gotolabels->get_labels();
430 delete gotolabels;
431 } // if
432}
433
434AsmStmtNode::~AsmStmtNode() {
[d1625f8]435 delete output; delete input; delete clobber;
[7f5566b]436}
437
438void AsmStmtNode::print( std::ostream &os, int indent ) const {
439 StatementNode::print( os, indent ); // print statement labels
440 os << string( indent + ParseNode::indent_by, ' ' ) << "volatile:" << voltile << endl;
441 if ( instruction ) {
442 os << string( indent + ParseNode::indent_by, ' ' ) << "Instruction:" << endl;
[d1625f8]443// instruction->printList( os, indent + 2 * ParseNode::indent_by );
[7f5566b]444 } // if
445 if ( output ) {
446 os << string( indent + ParseNode::indent_by, ' ' ) << "Output:" << endl;
447 output->printList( os, indent + 2 * ParseNode::indent_by );
448 } // if
449 if ( input ) {
450 os << string( indent + ParseNode::indent_by, ' ' ) << "Input:" << endl;
451 input->printList( os, indent + 2 * ParseNode::indent_by );
452 } // if
453 if ( clobber ) {
454 os << string( indent + ParseNode::indent_by, ' ' ) << "Clobber:" << endl;
[d1625f8]455// clobber->printList( os, indent + 2 * ParseNode::indent_by );
[7f5566b]456 } // if
457 if ( ! gotolabels.empty() ) {
458 os << string( indent + ParseNode::indent_by, ' ' ) << "Goto Labels:" << endl;
459 os << string( indent + 2 * ParseNode::indent_by, ' ' );
[0f8e4ac]460 for ( std::list<Label>::const_iterator i = gotolabels.begin();; ) {
[7f5566b]461 os << *i;
462 i++;
463 if ( i == gotolabels.end() ) break;
464 os << ", ";
465 }
466 os << endl;
467 } // if
468}
469
470Statement *AsmStmtNode::build() const {
471 std::list<Label> labs;
472
473 if ( ! get_labels().empty() ) {
474 std::back_insert_iterator< std::list<Label> > lab_it( labs );
475 copy( get_labels().begin(), get_labels().end(), lab_it );
476 } // if
477
478 std::list< Expression * > out, in;
479 std::list< ConstantExpr * > clob;
480 buildList( output, out );
481 buildList( input, in );
482 buildList( clobber, clob );
483 std::list< Label > gotolabs = gotolabels;
[d1625f8]484 return new AsmStmt( labs, voltile, instruction, out, in, clob, gotolabs );
[7f5566b]485}
486
[51b73452]487// Local Variables: //
[b87a5ed]488// tab-width: 4 //
489// mode: c++ //
490// compile-command: "make install" //
[51b73452]491// End: //
Note: See TracBrowser for help on using the repository browser.