source: src/Parser/StatementNode.cc@ e109716

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

more refactoring of parser code

  • Property mode set to 100644
File size: 13.9 KB
Line 
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
12// Last Modified On : Wed Aug 10 13:54:21 2016
13// Update Count : 170
14//
15
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 "Common/utility.h"
25
26using namespace std;
27
28const 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
36StatementNode::StatementNode() : ParseNode(), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {}
37
38StatementNode::StatementNode( const string *name ) : ParseNode( name ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {}
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 );
51 } // if
52 } else {
53 if ( decl->get_link() ) {
54 next = new StatementNode( dynamic_cast< DeclarationNode* >( decl->get_link() ) );
55 decl->set_next( 0 );
56 } // if
57 this->decl = decl;
58 } // if
59 } // if
60}
61
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;
64}
65
66StatementNode::StatementNode( Type t, string *target ) : type( t ), control( 0 ), block( 0 ), labels( 0 ), target( target ), decl( 0 ), isCatchRest ( false ) {}
67
68StatementNode::~StatementNode() {
69 delete control;
70 delete block;
71 delete target;
72 delete decl;
73}
74
75StatementNode * StatementNode::newCatchStmt( DeclarationNode *d, StatementNode *s, bool catchRestP ) {
76 StatementNode *ret = new StatementNode( StatementNode::Catch, 0, s );
77 ret->addDeclaration( d );
78 ret->setCatchRest( catchRestP );
79
80 return ret;
81}
82
83std::string StatementNode::get_target() const{
84 if ( target )
85 return *target;
86
87 return string("");
88}
89
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;
96 } // if
97 newnode->decl = maybeClone( decl );
98 return newnode;
99}
100
101StatementNode *StatementNode::add_label( const std::string *l ) {
102 if ( l != 0 ) {
103 labels.push_front( *l );
104 delete l;
105 } // if
106 return this;
107}
108
109StatementNode *StatementNode::append_block( StatementNode *stmt ) {
110 if ( stmt != 0 ) {
111 if ( block == 0 )
112 block = stmt;
113 else
114 block->set_link( stmt );
115 } // if
116 return this;
117}
118
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 );
129 } // if
130 return this;
131}
132
133void StatementNode::print( std::ostream &os, int indent ) const {
134 if ( ! labels.empty() ) {
135 std::list<std::string>::const_iterator i;
136
137 os << string( indent, ' ' );
138 for ( i = labels.begin(); i != labels.end(); i++ )
139 os << *i << ":";
140 os << endl;
141 } // if
142
143 switch ( type ) {
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;
152 } else
153 os << string( indent, ' ' ) << "Null Statement" << endl;
154 break;
155 default:
156 os << string( indent, ' ' ) << StatementNode::StType[type] << endl;
157 if ( type == Catch ) {
158 if ( decl ) {
159 os << string( indent + ParseNode::indent_by, ' ' ) << "Declaration: " << endl;
160 decl->print( os, indent + 2 * ParseNode::indent_by );
161 } else if ( isCatchRest ) {
162 os << string( indent + ParseNode::indent_by, ' ' ) << "Catches the rest " << endl;
163 } else {
164 ; // should never reach here
165 } // if
166 } // if
167 if ( control ) {
168 os << string( indent + ParseNode::indent_by, ' ' ) << "Control: " << endl;
169 control->printList( os, indent + 2 * ParseNode::indent_by );
170 } // if
171 if ( block ) {
172 os << string( indent + ParseNode::indent_by, ' ' ) << "Cases: " << endl;
173 block->printList( os, indent + 2 * ParseNode::indent_by );
174 } // if
175 if ( target ) {
176 os << string( indent + ParseNode::indent_by, ' ' ) << "Target: " << get_target() << endl;
177 } // if
178 break;
179 } // switch
180}
181
182Statement *StatementNode::build() const {
183 std::list<Statement *> branches;
184 std::list<Expression *> exps;
185 std::list<Label> labs;
186
187 if ( ! labels.empty() ) {
188 std::back_insert_iterator< std::list<Label> > lab_it( labs );
189 copy( labels.begin(), labels.end(), lab_it );
190 } // if
191
192 // try {
193 buildList<Statement, StatementNode>( get_block(), branches );
194
195 switch ( type ) {
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();
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 );
226 case Default:
227 return new CaseStmt( labs, 0, branches, true );
228 case While:
229 // assert( branches.size() == 1 );
230 // return new WhileStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), branches.front() );
231 assert( false );
232 case Do:
233 // assert( branches.size() == 1 );
234 // return new WhileStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), branches.front(), true );
235 assert( false );
236 case For:
237 // {
238 // assert( branches.size() == 1 );
239
240 // ForCtlExprNode *ctl = dynamic_cast<ForCtlExprNode *>( get_control() );
241 // assert( ctl != 0 );
242
243 // std::list<Statement *> init;
244 // if ( ctl->get_init() != 0 ) {
245 // buildList( ctl->get_init(), init );
246 // } // if
247
248 // Expression *cond = 0;
249 // if ( ctl->get_condition() != 0 )
250 // cond = notZeroExpr( maybeBuild<Expression>(ctl->get_condition()) );
251
252 // Expression *incr = 0;
253 // if ( ctl->get_change() != 0 )
254 // incr = maybeBuild<Expression>(ctl->get_change());
255
256 // return new ForStmt( labs, init, cond, incr, branches.front() );
257 // }
258 assert( false );
259 case Goto:
260 {
261 if ( get_target() == "" ) { // computed goto
262 assert( get_control() != 0 );
263 return new BranchStmt( labs, maybeBuild<Expression>(get_control()), BranchStmt::Goto );
264 } // if
265
266 return new BranchStmt( labs, get_target(), BranchStmt::Goto );
267 }
268 case Break:
269 return new BranchStmt( labs, get_target(), BranchStmt::Break );
270 case Continue:
271 return new BranchStmt( labs, get_target(), BranchStmt::Continue );
272 case Return:
273 case Throw :
274 buildList( get_control(), exps );
275 if ( exps.size() ==0 )
276 return new ReturnStmt( labs, 0, type == Throw );
277 if ( exps.size() > 0 )
278 return new ReturnStmt( labs, exps.back(), type == Throw );
279 case Try:
280 {
281 assert( branches.size() >= 0 );
282 CompoundStmt *tryBlock = dynamic_cast<CompoundStmt *>( branches.front());
283 branches.pop_front();
284 FinallyStmt *finallyBlock = 0;
285 if ( ( finallyBlock = dynamic_cast<FinallyStmt *>( branches.back())) ) {
286 branches.pop_back();
287 } // if
288 return new TryStmt( labs, tryBlock, branches, finallyBlock );
289 }
290 case Catch:
291 {
292 assert( branches.size() == 1 );
293
294 return new CatchStmt( labs, maybeBuild< Declaration >( decl ), branches.front(), isCatchRest );
295 }
296 case Finally:
297 {
298 assert( branches.size() == 1 );
299 CompoundStmt *block = dynamic_cast<CompoundStmt *>( branches.front() );
300 assert( block != 0 );
301
302 return new FinallyStmt( labs, block );
303 }
304 case Asm:
305 assert( false );
306 default:
307 // shouldn't be here
308 return 0;
309 } // switch
310}
311
312Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt ) {
313 Statement *thenb, *elseb = 0;
314 std::list<Statement *> branches;
315 buildList<Statement, StatementNode>( then_stmt, branches );
316 assert( branches.size() >= 1 );
317 thenb = branches.front();
318
319 if ( else_stmt ) {
320 std::list<Statement *> branches;
321 buildList<Statement, StatementNode>( else_stmt, branches );
322 assert( branches.size() >= 1 );
323 elseb = branches.front();
324 } // if
325 return new IfStmt( noLabels, notZeroExpr( maybeBuild<Expression>(ctl) ), thenb, elseb );
326}
327
328Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) {
329 std::list<Statement *> branches;
330 buildList<Statement, StatementNode>( stmt, branches );
331 assert( branches.size() >= 1 );
332 return new SwitchStmt( noLabels, maybeBuild<Expression>(ctl), branches );
333}
334
335Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {
336 std::list<Statement *> branches;
337 buildList<Statement, StatementNode>( stmt, branches );
338 assert( branches.size() == 1 );
339 return new WhileStmt( noLabels, notZeroExpr( maybeBuild<Expression>(ctl) ), branches.front(), kind );
340}
341
342Statement *build_for( ForCtl *forctl, StatementNode *stmt ) {
343 std::list<Statement *> branches;
344 buildList<Statement, StatementNode>( stmt, branches );
345 assert( branches.size() == 1 );
346
347 std::list<Statement *> init;
348 if ( forctl->init != 0 ) {
349 buildList( forctl->init, init );
350 } // if
351
352 Expression *cond = 0;
353 if ( forctl->condition != 0 )
354 cond = notZeroExpr( maybeBuild<Expression>(forctl->condition) );
355
356 Expression *incr = 0;
357 if ( forctl->change != 0 )
358 incr = maybeBuild<Expression>(forctl->change);
359
360 delete forctl;
361 return new ForStmt( noLabels, init, cond, incr, branches.front() );
362}
363
364
365CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {}
366
367CompoundStmtNode::CompoundStmtNode( const string *name_ ) : StatementNode( name_ ), first( 0 ), last( 0 ) {}
368
369CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) {
370 if ( first ) {
371 last = ( StatementNode *)( stmt->get_last());
372 } else {
373 last = 0;
374 } // if
375}
376
377CompoundStmtNode::~CompoundStmtNode() {
378 delete first;
379}
380
381void CompoundStmtNode::add_statement( StatementNode *stmt ) {
382 if ( stmt != 0 ) {
383 last->set_link( stmt );
384 last = ( StatementNode *)( stmt->get_link());
385 } // if
386}
387
388void CompoundStmtNode::print( ostream &os, int indent ) const {
389 if ( first ) {
390 first->printList( os, indent+2 );
391 } // if
392}
393
394Statement *CompoundStmtNode::build() const {
395 std::list<Label> labs;
396 const std::list<std::string> &labels = get_labels();
397
398 if ( ! labels.empty() ) {
399 std::back_insert_iterator< std::list<Label> > lab_it( labs );
400 copy( labels.begin(), labels.end(), lab_it );
401 } // if
402
403 CompoundStmt *cs = new CompoundStmt( labs );
404 buildList( first, cs->get_kids() );
405 return cs;
406}
407
408
409AsmStmtNode::AsmStmtNode( Type t, bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) :
410 StatementNode( t ), voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ) {
411 if ( gotolabels ) {
412 this->gotolabels = gotolabels->get_labels();
413 delete gotolabels;
414 } // if
415}
416
417AsmStmtNode::~AsmStmtNode() {
418 delete output; delete input; delete clobber;
419}
420
421void AsmStmtNode::print( std::ostream &os, int indent ) const {
422 StatementNode::print( os, indent ); // print statement labels
423 os << string( indent + ParseNode::indent_by, ' ' ) << "volatile:" << voltile << endl;
424 if ( instruction ) {
425 os << string( indent + ParseNode::indent_by, ' ' ) << "Instruction:" << endl;
426// instruction->printList( os, indent + 2 * ParseNode::indent_by );
427 } // if
428 if ( output ) {
429 os << string( indent + ParseNode::indent_by, ' ' ) << "Output:" << endl;
430 output->printList( os, indent + 2 * ParseNode::indent_by );
431 } // if
432 if ( input ) {
433 os << string( indent + ParseNode::indent_by, ' ' ) << "Input:" << endl;
434 input->printList( os, indent + 2 * ParseNode::indent_by );
435 } // if
436 if ( clobber ) {
437 os << string( indent + ParseNode::indent_by, ' ' ) << "Clobber:" << endl;
438// clobber->printList( os, indent + 2 * ParseNode::indent_by );
439 } // if
440 if ( ! gotolabels.empty() ) {
441 os << string( indent + ParseNode::indent_by, ' ' ) << "Goto Labels:" << endl;
442 os << string( indent + 2 * ParseNode::indent_by, ' ' );
443 for ( std::list<Label>::const_iterator i = gotolabels.begin();; ) {
444 os << *i;
445 i++;
446 if ( i == gotolabels.end() ) break;
447 os << ", ";
448 }
449 os << endl;
450 } // if
451}
452
453Statement *AsmStmtNode::build() const {
454 std::list<Label> labs;
455
456 if ( ! get_labels().empty() ) {
457 std::back_insert_iterator< std::list<Label> > lab_it( labs );
458 copy( get_labels().begin(), get_labels().end(), lab_it );
459 } // if
460
461 std::list< Expression * > out, in;
462 std::list< ConstantExpr * > clob;
463 buildList( output, out );
464 buildList( input, in );
465 buildList( clobber, clob );
466 std::list< Label > gotolabs = gotolabels;
467 return new AsmStmt( labs, voltile, instruction, out, in, clob, gotolabs );
468}
469
470// Local Variables: //
471// tab-width: 4 //
472// mode: c++ //
473// compile-command: "make install" //
474// End: //
Note: See TracBrowser for help on using the repository browser.