source: src/Parser/StatementNode.cc@ 23b6f4d7

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii 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 23b6f4d7 was 0f8e4ac, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

change Label from a string typedef to a class

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