source: src/Parser/StatementNode.cc@ a6dd5b0

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

more refactoring of parser code

  • Property mode set to 100644
File size: 12.0 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
[d1625f8]12// Last Modified On : Tue Aug 9 10:14:33 2016
13// Update Count : 141
[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:
208 {
209 Statement *thenb = 0, *elseb = 0;
210 assert( branches.size() >= 1 );
211
212 thenb = branches.front();
213 branches.pop_front();
[a32b204]214 if ( ! branches.empty() ) {
[b87a5ed]215 elseb = branches.front();
216 branches.pop_front();
[44b5ca0]217 } // if
[e04ef3a]218 return new IfStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), thenb, elseb );
[b87a5ed]219 }
220 case While:
221 assert( branches.size() == 1 );
[e04ef3a]222 return new WhileStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), branches.front() );
[b87a5ed]223 case Do:
224 assert( branches.size() == 1 );
[e04ef3a]225 return new WhileStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), branches.front(), true );
[b87a5ed]226 case For:
227 {
228 assert( branches.size() == 1 );
229
230 ForCtlExprNode *ctl = dynamic_cast<ForCtlExprNode *>( get_control() );
231 assert( ctl != 0 );
232
[145f1fc]233 std::list<Statement *> init;
234 if ( ctl->get_init() != 0 ) {
235 buildList( ctl->get_init(), init );
[1db21619]236 } // if
[b87a5ed]237
238 Expression *cond = 0;
239 if ( ctl->get_condition() != 0 )
[e04ef3a]240 cond = notZeroExpr( maybeBuild<Expression>(ctl->get_condition()) );
[b87a5ed]241
242 Expression *incr = 0;
243 if ( ctl->get_change() != 0 )
[e04ef3a]244 incr = maybeBuild<Expression>(ctl->get_change());
[b87a5ed]245
[145f1fc]246 return new ForStmt( labs, init, cond, incr, branches.front() );
[b87a5ed]247 }
248 case Switch:
[e04ef3a]249 return new SwitchStmt( labs, maybeBuild<Expression>(get_control()), branches );
[0f8e4ac]250 case Case:
[e04ef3a]251 return new CaseStmt( labs, maybeBuild<Expression>(get_control()), branches );
[b87a5ed]252 case Default:
253 return new CaseStmt( labs, 0, branches, true );
254 case Goto:
255 {
256 if ( get_target() == "" ) { // computed goto
257 assert( get_control() != 0 );
[e04ef3a]258 return new BranchStmt( labs, maybeBuild<Expression>(get_control()), BranchStmt::Goto );
[44b5ca0]259 } // if
[b87a5ed]260
261 return new BranchStmt( labs, get_target(), BranchStmt::Goto );
262 }
263 case Break:
264 return new BranchStmt( labs, get_target(), BranchStmt::Break );
265 case Continue:
266 return new BranchStmt( labs, get_target(), BranchStmt::Continue );
267 case Return:
268 case Throw :
269 buildList( get_control(), exps );
270 if ( exps.size() ==0 )
271 return new ReturnStmt( labs, 0, type == Throw );
272 if ( exps.size() > 0 )
273 return new ReturnStmt( labs, exps.back(), type == Throw );
274 case Try:
275 {
276 assert( branches.size() >= 0 );
277 CompoundStmt *tryBlock = dynamic_cast<CompoundStmt *>( branches.front());
278 branches.pop_front();
279 FinallyStmt *finallyBlock = 0;
280 if ( ( finallyBlock = dynamic_cast<FinallyStmt *>( branches.back())) ) {
281 branches.pop_back();
[44b5ca0]282 } // if
[b87a5ed]283 return new TryStmt( labs, tryBlock, branches, finallyBlock );
284 }
285 case Catch:
286 {
287 assert( branches.size() == 1 );
288
289 return new CatchStmt( labs, maybeBuild< Declaration >( decl ), branches.front(), isCatchRest );
290 }
291 case Finally:
292 {
293 assert( branches.size() == 1 );
294 CompoundStmt *block = dynamic_cast<CompoundStmt *>( branches.front() );
295 assert( block != 0 );
296
297 return new FinallyStmt( labs, block );
298 }
[7f5566b]299 case Asm:
300 assert( false );
[b87a5ed]301 default:
302 // shouldn't be here
303 return 0;
[44b5ca0]304 } // switch
[51b73452]305}
306
[7f5566b]307
[59db689]308CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {}
[51b73452]309
[59db689]310CompoundStmtNode::CompoundStmtNode( const string *name_ ) : StatementNode( name_ ), first( 0 ), last( 0 ) {}
[51b73452]311
[59db689]312CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) {
[b87a5ed]313 if ( first ) {
314 last = ( StatementNode *)( stmt->get_last());
315 } else {
316 last = 0;
[44b5ca0]317 } // if
[51b73452]318}
319
[b87a5ed]320CompoundStmtNode::~CompoundStmtNode() {
321 delete first;
[51b73452]322}
323
[b87a5ed]324void CompoundStmtNode::add_statement( StatementNode *stmt ) {
325 if ( stmt != 0 ) {
326 last->set_link( stmt );
327 last = ( StatementNode *)( stmt->get_link());
[44b5ca0]328 } // if
[51b73452]329}
330
[b87a5ed]331void CompoundStmtNode::print( ostream &os, int indent ) const {
332 if ( first ) {
333 first->printList( os, indent+2 );
[44b5ca0]334 } // if
[51b73452]335}
336
337Statement *CompoundStmtNode::build() const {
[b87a5ed]338 std::list<Label> labs;
[1db21619]339 const std::list<std::string> &labels = get_labels();
[51b73452]340
[1db21619]341 if ( ! labels.empty() ) {
[b87a5ed]342 std::back_insert_iterator< std::list<Label> > lab_it( labs );
[1db21619]343 copy( labels.begin(), labels.end(), lab_it );
[44b5ca0]344 } // if
[51b73452]345
[b87a5ed]346 CompoundStmt *cs = new CompoundStmt( labs );
347 buildList( first, cs->get_kids() );
348 return cs;
[51b73452]349}
350
[7f5566b]351
[d1625f8]352AsmStmtNode::AsmStmtNode( Type t, bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) :
[7f5566b]353 StatementNode( t ), voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ) {
354 if ( gotolabels ) {
355 this->gotolabels = gotolabels->get_labels();
356 delete gotolabels;
357 } // if
358}
359
360AsmStmtNode::~AsmStmtNode() {
[d1625f8]361 delete output; delete input; delete clobber;
[7f5566b]362}
363
364void AsmStmtNode::print( std::ostream &os, int indent ) const {
365 StatementNode::print( os, indent ); // print statement labels
366 os << string( indent + ParseNode::indent_by, ' ' ) << "volatile:" << voltile << endl;
367 if ( instruction ) {
368 os << string( indent + ParseNode::indent_by, ' ' ) << "Instruction:" << endl;
[d1625f8]369// instruction->printList( os, indent + 2 * ParseNode::indent_by );
[7f5566b]370 } // if
371 if ( output ) {
372 os << string( indent + ParseNode::indent_by, ' ' ) << "Output:" << endl;
373 output->printList( os, indent + 2 * ParseNode::indent_by );
374 } // if
375 if ( input ) {
376 os << string( indent + ParseNode::indent_by, ' ' ) << "Input:" << endl;
377 input->printList( os, indent + 2 * ParseNode::indent_by );
378 } // if
379 if ( clobber ) {
380 os << string( indent + ParseNode::indent_by, ' ' ) << "Clobber:" << endl;
[d1625f8]381// clobber->printList( os, indent + 2 * ParseNode::indent_by );
[7f5566b]382 } // if
383 if ( ! gotolabels.empty() ) {
384 os << string( indent + ParseNode::indent_by, ' ' ) << "Goto Labels:" << endl;
385 os << string( indent + 2 * ParseNode::indent_by, ' ' );
[0f8e4ac]386 for ( std::list<Label>::const_iterator i = gotolabels.begin();; ) {
[7f5566b]387 os << *i;
388 i++;
389 if ( i == gotolabels.end() ) break;
390 os << ", ";
391 }
392 os << endl;
393 } // if
394}
395
396Statement *AsmStmtNode::build() const {
397 std::list<Label> labs;
398
399 if ( ! get_labels().empty() ) {
400 std::back_insert_iterator< std::list<Label> > lab_it( labs );
401 copy( get_labels().begin(), get_labels().end(), lab_it );
402 } // if
403
404 std::list< Expression * > out, in;
405 std::list< ConstantExpr * > clob;
406 buildList( output, out );
407 buildList( input, in );
408 buildList( clobber, clob );
409 std::list< Label > gotolabs = gotolabels;
[d1625f8]410 return new AsmStmt( labs, voltile, instruction, out, in, clob, gotolabs );
[7f5566b]411}
412
[51b73452]413// Local Variables: //
[b87a5ed]414// tab-width: 4 //
415// mode: c++ //
416// compile-command: "make install" //
[51b73452]417// End: //
Note: See TracBrowser for help on using the repository browser.