source: src/Parser/StatementNode.cc@ 797347f

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 797347f was 777bfcf, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

more refactoring of parser code

  • Property mode set to 100644
File size: 10.5 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 : Sun Aug 14 13:10:54 2016
13// Update Count : 288
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(), labels( 0 ), decl( 0 ) {}
37
38StatementNode::StatementNode( DeclarationNode *decl ) : type( Decl ), labels( 0 ) {
39 assert( false );
40 if ( decl ) {
41 if ( DeclarationNode *agg = decl->extractAggregate() ) {
42 this->decl = agg;
43 StatementNode *nextStmt = new StatementNode;
44 nextStmt->type = Decl;
45 nextStmt->decl = decl;
46 next = nextStmt;
47 if ( decl->get_next() ) {
48 next->set_next( new StatementNode( dynamic_cast<DeclarationNode *>( decl->get_next() ) ) );
49 decl->set_next( 0 );
50 } // if
51 } else {
52 if ( decl->get_next() ) {
53 next = new StatementNode( dynamic_cast<DeclarationNode *>( decl->get_next() ) );
54 decl->set_next( 0 );
55 } // if
56 this->decl = decl;
57 } // if
58 } // if
59}
60
61StatementNode2::StatementNode2( DeclarationNode *decl ) {
62 if ( decl ) {
63 DeclarationNode *agg = decl->extractAggregate();
64 if ( agg ) {
65 StatementNode *nextStmt = new StatementNode;
66 nextStmt->type = Decl;
67 nextStmt->decl = decl;
68 next = nextStmt;
69 if ( decl->get_next() ) {
70 next->set_next( new StatementNode2( dynamic_cast<DeclarationNode *>(decl->get_next()) ) );
71 decl->set_next( 0 );
72 } // if
73 } else {
74 if ( decl->get_next() ) {
75 next = new StatementNode2( dynamic_cast<DeclarationNode *>( decl->get_next() ) );
76 decl->set_next( 0 );
77 } // if
78 agg = decl;
79 } // if
80 stmt = new DeclStmt( noLabels, maybeBuild<Declaration>(agg) );
81 } else {
82 assert( false );
83 } // if
84}
85
86StatementNode::~StatementNode() {
87 delete decl;
88}
89
90StatementNode * StatementNode::clone() const {
91 assert( false );
92 return 0;
93}
94
95StatementNode *StatementNode::add_label( const std::string *l ) {
96 if ( l != 0 ) {
97 labels.push_front( *l );
98 delete l;
99 } // if
100 return this;
101}
102
103StatementNode *StatementNode::append_last_case( StatementNode *stmt ) {
104 assert( false );
105 return this;
106}
107
108StatementNode *StatementNode2::append_last_case( StatementNode *stmt ) {
109 StatementNode *prev = this;
110 // find end of list and maintain previous pointer
111 for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
112 StatementNode2 *node = dynamic_cast<StatementNode2 *>(curr);
113 assert( node );
114 assert( dynamic_cast<CaseStmt *>(node->stmt) );
115 prev = curr;
116 } // for
117 // conver from StatementNode list to Statement list
118 StatementNode2 *node = dynamic_cast<StatementNode2 *>(prev);
119 std::list<Statement *> stmts;
120 buildList( stmt, stmts );
121 // splice any new Statements to end of currents Statements
122 CaseStmt * caseStmt = dynamic_cast<CaseStmt *>(node->stmt);
123 caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
124 return this;
125}
126
127Statement *StatementNode::build() const {
128 switch ( type ) {
129 case Decl:
130 return new DeclStmt( noLabels, maybeBuild< Declaration >( decl ) );
131 assert( false );
132 case Exp:
133 case If:
134 case Switch:
135 case Case:
136 case Default:
137 case While:
138 case Do:
139 case For:
140 case Goto:
141 case Break:
142 case Continue:
143 case Return:
144 case Throw :
145 case Try:
146 case Catch:
147 case Finally:
148 case Asm:
149 default:
150 assert( false );
151 return 0;
152 } // switch
153}
154
155Statement *build_expr( ExpressionNode *ctl ) {
156 Expression *e = maybeBuild< Expression >( ctl );
157
158 if ( e )
159 return new ExprStmt( noLabels, e );
160 else
161 return new NullStmt( noLabels );
162}
163
164Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt ) {
165 Statement *thenb, *elseb = 0;
166 std::list<Statement *> branches;
167 buildList<Statement, StatementNode>( then_stmt, branches );
168 assert( branches.size() == 1 );
169 thenb = branches.front();
170
171 if ( else_stmt ) {
172 std::list<Statement *> branches;
173 buildList<Statement, StatementNode>( else_stmt, branches );
174 assert( branches.size() == 1 );
175 elseb = branches.front();
176 } // if
177 return new IfStmt( noLabels, notZeroExpr( maybeBuild<Expression>(ctl) ), thenb, elseb );
178}
179
180Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) {
181 std::list<Statement *> branches;
182 buildList<Statement, StatementNode>( stmt, branches );
183 assert( branches.size() >= 0 ); // size == 0 for switch (...) {}, i.e., no declaration or statements
184 return new SwitchStmt( noLabels, maybeBuild<Expression>(ctl), branches );
185}
186Statement *build_case( ExpressionNode *ctl ) {
187 std::list<Statement *> branches;
188 return new CaseStmt( noLabels, maybeBuild<Expression>(ctl), branches );
189}
190Statement *build_default() {
191 std::list<Statement *> branches;
192 return new CaseStmt( noLabels, nullptr, branches, true );
193}
194
195Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {
196 std::list<Statement *> branches;
197 buildList<Statement, StatementNode>( stmt, branches );
198 assert( branches.size() == 1 );
199 return new WhileStmt( noLabels, notZeroExpr( maybeBuild<Expression>(ctl) ), branches.front(), kind );
200}
201
202Statement *build_for( ForCtl *forctl, StatementNode *stmt ) {
203 std::list<Statement *> branches;
204 buildList<Statement, StatementNode>( stmt, branches );
205 assert( branches.size() == 1 );
206
207 std::list<Statement *> init;
208 if ( forctl->init != 0 ) {
209 buildList( forctl->init, init );
210 } // if
211
212 Expression *cond = 0;
213 if ( forctl->condition != 0 )
214 cond = notZeroExpr( maybeBuild<Expression>(forctl->condition) );
215
216 Expression *incr = 0;
217 if ( forctl->change != 0 )
218 incr = maybeBuild<Expression>(forctl->change);
219
220 delete forctl;
221 return new ForStmt( noLabels, init, cond, incr, branches.front() );
222}
223
224Statement *build_branch( std::string identifier, BranchStmt::Type kind ) {
225 return new BranchStmt( noLabels, identifier, kind );
226}
227Statement *build_computedgoto( ExpressionNode *ctl ) {
228 return new BranchStmt( noLabels, maybeBuild<Expression>(ctl), BranchStmt::Goto );
229}
230
231Statement *build_return( ExpressionNode *ctl ) {
232 std::list<Expression *> exps;
233 buildList( ctl, exps );
234 return new ReturnStmt( noLabels, exps.size() > 0 ? exps.back() : nullptr );
235}
236Statement *build_throw( ExpressionNode *ctl ) {
237 std::list<Expression *> exps;
238 buildList( ctl, exps );
239 return new ReturnStmt( noLabels, exps.size() > 0 ? exps.back() : nullptr, true );
240}
241
242Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
243 std::list<Statement *> branches;
244 buildList<Statement, StatementNode>( catch_stmt, branches );
245 CompoundStmt *tryBlock = dynamic_cast<CompoundStmt *>(maybeBuild<Statement>(try_stmt));
246 assert( tryBlock );
247 FinallyStmt *finallyBlock = dynamic_cast<FinallyStmt *>(maybeBuild<Statement>(finally_stmt) );
248 return new TryStmt( noLabels, tryBlock, branches, finallyBlock );
249}
250Statement *build_catch( DeclarationNode *decl, StatementNode *stmt, bool catchAny ) {
251 std::list<Statement *> branches;
252 buildList<Statement, StatementNode>( stmt, branches );
253 assert( branches.size() == 1 );
254 return new CatchStmt( noLabels, maybeBuild<Declaration>(decl), branches.front(), catchAny );
255}
256Statement *build_finally( StatementNode *stmt ) {
257 std::list<Statement *> branches;
258 buildList<Statement, StatementNode>( stmt, branches );
259 assert( branches.size() == 1 );
260 return new FinallyStmt( noLabels, dynamic_cast<CompoundStmt *>( branches.front() ) );
261}
262
263
264CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {}
265
266CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) {
267 if ( first ) {
268 last = ( StatementNode *)( stmt->get_last());
269 } else {
270 last = 0;
271 } // if
272}
273
274CompoundStmtNode::~CompoundStmtNode() {
275 delete first;
276}
277
278void CompoundStmtNode::add_statement( StatementNode *stmt ) {
279 if ( stmt != 0 ) {
280 last->set_last( stmt );
281 last = ( StatementNode *)( stmt->get_next());
282 } // if
283}
284
285void CompoundStmtNode::print( ostream &os, int indent ) const {
286 if ( first ) {
287 first->printList( os, indent+2 );
288 } // if
289}
290
291Statement *CompoundStmtNode::build() const {
292 std::list<Label> labs;
293 const std::list<std::string> &labels = get_labels();
294
295 if ( ! labels.empty() ) {
296 std::back_insert_iterator< std::list<Label> > lab_it( labs );
297 copy( labels.begin(), labels.end(), lab_it );
298 } // if
299
300 CompoundStmt *cs = new CompoundStmt( labs );
301 buildList( first, cs->get_kids() );
302 return cs;
303}
304
305
306AsmStmtNode::AsmStmtNode( bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) :
307 voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ) {
308 type = Asm;
309 if ( gotolabels ) {
310 this->gotolabels = gotolabels->get_labels();
311 delete gotolabels;
312 } // if
313}
314
315AsmStmtNode::~AsmStmtNode() {
316 delete output; delete input; delete clobber;
317}
318
319Statement *AsmStmtNode::build() const {
320 std::list<Label> labs;
321
322 if ( ! get_labels().empty() ) {
323 std::back_insert_iterator< std::list<Label> > lab_it( labs );
324 copy( get_labels().begin(), get_labels().end(), lab_it );
325 } // if
326
327 std::list< Expression * > out, in;
328 std::list< ConstantExpr * > clob;
329 buildList( output, out );
330 buildList( input, in );
331 buildList( clobber, clob );
332 std::list< Label > gotolabs = gotolabels;
333 return new AsmStmt( labs, voltile, instruction, out, in, clob, gotolabs );
334}
335
336// Statement *build_asm( bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 ) {
337// std::list<Label> labs;
338
339// if ( ! get_labels().empty() ) {
340// std::back_insert_iterator< std::list<Label> > lab_it( labs );
341// copy( get_labels().begin(), get_labels().end(), lab_it );
342// } // if
343
344// std::list< Expression * > out, in;
345// std::list< ConstantExpr * > clob;
346// buildList( output, out );
347// buildList( input, in );
348// buildList( clobber, clob );
349// std::list< Label > gotolabs = gotolabels;
350// return new AsmStmt( labs, voltile, instruction, out, in, clob, gotolabs );
351// }
352
353// Local Variables: //
354// tab-width: 4 //
355// mode: c++ //
356// compile-command: "make install" //
357// End: //
Note: See TracBrowser for help on using the repository browser.