source: src/Parser/StatementNode.cc@ 911348cd

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 911348cd was 4e06c1e, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

changes for switch and choose statements

  • Property mode set to 100644
File size: 12.4 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 : Tue Jul 12 17:21:02 2016
13// Update Count : 133
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 Case:
257 return new CaseStmt( labs, maybeBuild<Expression>(get_control()), branches );
258 case Default:
259 return new CaseStmt( labs, 0, branches, true );
260 case Goto:
261 {
262 if ( get_target() == "" ) { // computed goto
263 assert( get_control() != 0 );
264 return new BranchStmt( labs, maybeBuild<Expression>(get_control()), BranchStmt::Goto );
265 } // if
266
267 return new BranchStmt( labs, get_target(), BranchStmt::Goto );
268 }
269 case Break:
270 return new BranchStmt( labs, get_target(), BranchStmt::Break );
271 case Continue:
272 return new BranchStmt( labs, get_target(), BranchStmt::Continue );
273 case Return:
274 case Throw :
275 buildList( get_control(), exps );
276 if ( exps.size() ==0 )
277 return new ReturnStmt( labs, 0, type == Throw );
278 if ( exps.size() > 0 )
279 return new ReturnStmt( labs, exps.back(), type == Throw );
280 case Try:
281 {
282 assert( branches.size() >= 0 );
283 CompoundStmt *tryBlock = dynamic_cast<CompoundStmt *>( branches.front());
284 branches.pop_front();
285 FinallyStmt *finallyBlock = 0;
286 if ( ( finallyBlock = dynamic_cast<FinallyStmt *>( branches.back())) ) {
287 branches.pop_back();
288 } // if
289 return new TryStmt( labs, tryBlock, branches, finallyBlock );
290 }
291 case Catch:
292 {
293 assert( branches.size() == 1 );
294
295 return new CatchStmt( labs, maybeBuild< Declaration >( decl ), branches.front(), isCatchRest );
296 }
297 case Finally:
298 {
299 assert( branches.size() == 1 );
300 CompoundStmt *block = dynamic_cast<CompoundStmt *>( branches.front() );
301 assert( block != 0 );
302
303 return new FinallyStmt( labs, block );
304 }
305 case Asm:
306 assert( false );
307 default:
308 // shouldn't be here
309 return 0;
310 } // switch
311}
312
313
314CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {}
315
316CompoundStmtNode::CompoundStmtNode( const string *name_ ) : StatementNode( name_ ), first( 0 ), last( 0 ) {}
317
318CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) {
319 if ( first ) {
320 last = ( StatementNode *)( stmt->get_last());
321 } else {
322 last = 0;
323 } // if
324}
325
326CompoundStmtNode::~CompoundStmtNode() {
327 delete first;
328}
329
330void CompoundStmtNode::add_statement( StatementNode *stmt ) {
331 if ( stmt != 0 ) {
332 last->set_link( stmt );
333 last = ( StatementNode *)( stmt->get_link());
334 } // if
335}
336
337void CompoundStmtNode::print( ostream &os, int indent ) const {
338 if ( first ) {
339 first->printList( os, indent+2 );
340 } // if
341}
342
343Statement *CompoundStmtNode::build() const {
344 std::list<Label> labs;
345 const std::list<std::string> &labels = get_labels();
346
347 if ( ! labels.empty() ) {
348 std::back_insert_iterator< std::list<Label> > lab_it( labs );
349 copy( labels.begin(), labels.end(), lab_it );
350 } // if
351
352 CompoundStmt *cs = new CompoundStmt( labs );
353 buildList( first, cs->get_kids() );
354 return cs;
355}
356
357
358AsmStmtNode::AsmStmtNode( Type t, bool voltile, ConstantNode *instruction, ExpressionNode *output, ExpressionNode *input, ConstantNode *clobber, LabelNode *gotolabels ) :
359 StatementNode( t ), voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ) {
360 if ( gotolabels ) {
361 this->gotolabels = gotolabels->get_labels();
362 delete gotolabels;
363 } // if
364}
365
366AsmStmtNode::~AsmStmtNode() {
367 delete instruction; delete output; delete input; delete clobber;
368}
369
370void AsmStmtNode::print( std::ostream &os, int indent ) const {
371 StatementNode::print( os, indent ); // print statement labels
372 os << string( indent + ParseNode::indent_by, ' ' ) << "volatile:" << voltile << endl;
373 if ( instruction ) {
374 os << string( indent + ParseNode::indent_by, ' ' ) << "Instruction:" << endl;
375 instruction->printList( os, indent + 2 * ParseNode::indent_by );
376 } // if
377 if ( output ) {
378 os << string( indent + ParseNode::indent_by, ' ' ) << "Output:" << endl;
379 output->printList( os, indent + 2 * ParseNode::indent_by );
380 } // if
381 if ( input ) {
382 os << string( indent + ParseNode::indent_by, ' ' ) << "Input:" << endl;
383 input->printList( os, indent + 2 * ParseNode::indent_by );
384 } // if
385 if ( clobber ) {
386 os << string( indent + ParseNode::indent_by, ' ' ) << "Clobber:" << endl;
387 clobber->printList( os, indent + 2 * ParseNode::indent_by );
388 } // if
389 if ( ! gotolabels.empty() ) {
390 os << string( indent + ParseNode::indent_by, ' ' ) << "Goto Labels:" << endl;
391 os << string( indent + 2 * ParseNode::indent_by, ' ' );
392 for ( std::list<Label>::const_iterator i = gotolabels.begin();; ) {
393 os << *i;
394 i++;
395 if ( i == gotolabels.end() ) break;
396 os << ", ";
397 }
398 os << endl;
399 } // if
400}
401
402Statement *AsmStmtNode::build() const {
403 std::list<Label> labs;
404
405 if ( ! get_labels().empty() ) {
406 std::back_insert_iterator< std::list<Label> > lab_it( labs );
407 copy( get_labels().begin(), get_labels().end(), lab_it );
408 } // if
409
410 std::list< Expression * > out, in;
411 std::list< ConstantExpr * > clob;
412 buildList( output, out );
413 buildList( input, in );
414 buildList( clobber, clob );
415 std::list< Label > gotolabs = gotolabels;
416 return new AsmStmt( labs, voltile, (ConstantExpr *)maybeBuild< Expression >( instruction ), out, in, clob, gotolabs );
417}
418
419
420void NullStmtNode::print( ostream &os, int indent ) const {
421 os << string( indent, ' ' ) << "Null Statement:" << endl;
422}
423
424Statement *NullStmtNode::build() const {
425 return new NullStmt;
426}
427
428// Local Variables: //
429// tab-width: 4 //
430// mode: c++ //
431// compile-command: "make install" //
432// End: //
Note: See TracBrowser for help on using the repository browser.