source: src/Parser/StatementNode.cc@ aaf1f4d

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 aaf1f4d 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
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 : Thu Jun 9 14:18:46 2016
13// Update Count : 132
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::add_controlexp( ExpressionNode *e ) {
110 if ( control && e )
111 control->add_to_list( e ); // xxx - check this
112 return this;
113}
114
115StatementNode *StatementNode::append_block( StatementNode *stmt ) {
116 if ( stmt != 0 ) {
117 if ( block == 0 )
118 block = stmt;
119 else
120 block->set_link( stmt );
121 } // if
122 return this;
123}
124
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 );
135 } // if
136 return this;
137}
138
139void StatementNode::print( std::ostream &os, int indent ) const {
140 if ( ! labels.empty() ) {
141 std::list<std::string>::const_iterator i;
142
143 os << string( indent, ' ' );
144 for ( i = labels.begin(); i != labels.end(); i++ )
145 os << *i << ":";
146 os << endl;
147 } // if
148
149 switch ( type ) {
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;
158 } else
159 os << string( indent, ' ' ) << "Null Statement" << endl;
160 break;
161 default:
162 os << string( indent, ' ' ) << StatementNode::StType[type] << endl;
163 if ( type == Catch ) {
164 if ( decl ) {
165 os << string( indent + ParseNode::indent_by, ' ' ) << "Declaration: " << endl;
166 decl->print( os, indent + 2 * ParseNode::indent_by );
167 } else if ( isCatchRest ) {
168 os << string( indent + ParseNode::indent_by, ' ' ) << "Catches the rest " << endl;
169 } else {
170 ; // should never reach here
171 } // if
172 } // if
173 if ( control ) {
174 os << string( indent + ParseNode::indent_by, ' ' ) << "Control: " << endl;
175 control->printList( os, indent + 2 * ParseNode::indent_by );
176 } // if
177 if ( block ) {
178 os << string( indent + ParseNode::indent_by, ' ' ) << "Branches of execution: " << endl;
179 block->printList( os, indent + 2 * ParseNode::indent_by );
180 } // if
181 if ( target ) {
182 os << string( indent + ParseNode::indent_by, ' ' ) << "Target: " << get_target() << endl;
183 } // if
184 break;
185 } // switch
186}
187
188Statement *StatementNode::build() const {
189 std::list<Statement *> branches;
190 std::list<Expression *> exps;
191 std::list<Label> labs;
192
193 if ( ! labels.empty() ) {
194 std::back_insert_iterator< std::list<Label> > lab_it( labs );
195 copy( labels.begin(), labels.end(), lab_it );
196 } // if
197
198 // try {
199 buildList<Statement, StatementNode>( get_block(), branches );
200
201 switch ( type ) {
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();
220 if ( ! branches.empty() ) {
221 elseb = branches.front();
222 branches.pop_front();
223 } // if
224 return new IfStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), thenb, elseb );
225 }
226 case While:
227 assert( branches.size() == 1 );
228 return new WhileStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), branches.front() );
229 case Do:
230 assert( branches.size() == 1 );
231 return new WhileStmt( labs, notZeroExpr( maybeBuild<Expression>(get_control()) ), branches.front(), true );
232 case For:
233 {
234 assert( branches.size() == 1 );
235
236 ForCtlExprNode *ctl = dynamic_cast<ForCtlExprNode *>( get_control() );
237 assert( ctl != 0 );
238
239 std::list<Statement *> init;
240 if ( ctl->get_init() != 0 ) {
241 buildList( ctl->get_init(), init );
242 } // if
243
244 Expression *cond = 0;
245 if ( ctl->get_condition() != 0 )
246 cond = notZeroExpr( maybeBuild<Expression>(ctl->get_condition()) );
247
248 Expression *incr = 0;
249 if ( ctl->get_change() != 0 )
250 incr = maybeBuild<Expression>(ctl->get_change());
251
252 return new ForStmt( labs, init, cond, incr, branches.front() );
253 }
254 case Switch:
255 return new SwitchStmt( labs, maybeBuild<Expression>(get_control()), branches );
256 case Choose:
257 return new ChooseStmt( labs, maybeBuild<Expression>(get_control()), branches );
258 case Fallthru:
259 return new FallthruStmt( labs );
260 case Case:
261 return new CaseStmt( labs, maybeBuild<Expression>(get_control()), branches );
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 );
268 return new BranchStmt( labs, maybeBuild<Expression>(get_control()), BranchStmt::Goto );
269 } // if
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();
292 } // if
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 }
309 case Asm:
310 assert( false );
311 default:
312 // shouldn't be here
313 return 0;
314 } // switch
315}
316
317
318CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {}
319
320CompoundStmtNode::CompoundStmtNode( const string *name_ ) : StatementNode( name_ ), first( 0 ), last( 0 ) {}
321
322CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) {
323 if ( first ) {
324 last = ( StatementNode *)( stmt->get_last());
325 } else {
326 last = 0;
327 } // if
328}
329
330CompoundStmtNode::~CompoundStmtNode() {
331 delete first;
332}
333
334void CompoundStmtNode::add_statement( StatementNode *stmt ) {
335 if ( stmt != 0 ) {
336 last->set_link( stmt );
337 last = ( StatementNode *)( stmt->get_link());
338 } // if
339}
340
341void CompoundStmtNode::print( ostream &os, int indent ) const {
342 if ( first ) {
343 first->printList( os, indent+2 );
344 } // if
345}
346
347Statement *CompoundStmtNode::build() const {
348 std::list<Label> labs;
349 const std::list<std::string> &labels = get_labels();
350
351 if ( ! labels.empty() ) {
352 std::back_insert_iterator< std::list<Label> > lab_it( labs );
353 copy( labels.begin(), labels.end(), lab_it );
354 } // if
355
356 CompoundStmt *cs = new CompoundStmt( labs );
357 buildList( first, cs->get_kids() );
358 return cs;
359}
360
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, ' ' );
396 for ( std::list<Label>::const_iterator i = gotolabels.begin();; ) {
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
424void NullStmtNode::print( ostream &os, int indent ) const {
425 os << string( indent, ' ' ) << "Null Statement:" << endl;
426}
427
428Statement *NullStmtNode::build() const {
429 return new NullStmt;
430}
431
432// Local Variables: //
433// tab-width: 4 //
434// mode: c++ //
435// compile-command: "make install" //
436// End: //
Note: See TracBrowser for help on using the repository browser.