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
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
[777bfcf]12// Last Modified On : Sun Aug 14 13:10:54 2016
13// Update Count : 288
[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
[777bfcf]36StatementNode::StatementNode() : ParseNode(), labels( 0 ), decl( 0 ) {}
[b87a5ed]37
[777bfcf]38StatementNode::StatementNode( DeclarationNode *decl ) : type( Decl ), labels( 0 ) {
[1d4580a]39 assert( false );
[b87a5ed]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;
[1d4580a]47 if ( decl->get_next() ) {
48 next->set_next( new StatementNode( dynamic_cast<DeclarationNode *>( decl->get_next() ) ) );
[b87a5ed]49 decl->set_next( 0 );
[44b5ca0]50 } // if
[b87a5ed]51 } else {
[1d4580a]52 if ( decl->get_next() ) {
53 next = new StatementNode( dynamic_cast<DeclarationNode *>( decl->get_next() ) );
[b87a5ed]54 decl->set_next( 0 );
[44b5ca0]55 } // if
[b87a5ed]56 this->decl = decl;
[44b5ca0]57 } // if
58 } // if
[51b73452]59}
60
[1d4580a]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
[b87a5ed]86StatementNode::~StatementNode() {
87 delete decl;
[51b73452]88}
89
[b87a5ed]90StatementNode * StatementNode::clone() const {
[1d4580a]91 assert( false );
[777bfcf]92 return 0;
[51b73452]93}
94
[59db689]95StatementNode *StatementNode::add_label( const std::string *l ) {
[b87a5ed]96 if ( l != 0 ) {
[0f8e4ac]97 labels.push_front( *l );
[b87a5ed]98 delete l;
[44b5ca0]99 } // if
[b87a5ed]100 return this;
[51b73452]101}
102
[b87a5ed]103StatementNode *StatementNode::append_last_case( StatementNode *stmt ) {
[8cc5cb0]104 assert( false );
[b87a5ed]105 return this;
[51b73452]106}
107
[8cc5cb0]108StatementNode *StatementNode2::append_last_case( StatementNode *stmt ) {
109 StatementNode *prev = this;
[1d4580a]110 // find end of list and maintain previous pointer
111 for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
[8cc5cb0]112 StatementNode2 *node = dynamic_cast<StatementNode2 *>(curr);
113 assert( node );
114 assert( dynamic_cast<CaseStmt *>(node->stmt) );
115 prev = curr;
116 } // for
[1d4580a]117 // conver from StatementNode list to Statement list
[8cc5cb0]118 StatementNode2 *node = dynamic_cast<StatementNode2 *>(prev);
119 std::list<Statement *> stmts;
120 buildList( stmt, stmts );
[1d4580a]121 // splice any new Statements to end of currents Statements
122 CaseStmt * caseStmt = dynamic_cast<CaseStmt *>(node->stmt);
[8cc5cb0]123 caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
124 return this;
125}
126
[51b73452]127Statement *StatementNode::build() const {
[a32b204]128 switch ( type ) {
[b87a5ed]129 case Decl:
[777bfcf]130 return new DeclStmt( noLabels, maybeBuild< Declaration >( decl ) );
[1d4580a]131 assert( false );
[b87a5ed]132 case Exp:
133 case If:
[2f22cc4]134 case Switch:
135 case Case:
136 case Default:
[b87a5ed]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:
[7f5566b]148 case Asm:
[b87a5ed]149 default:
[1d4580a]150 assert( false );
[b87a5ed]151 return 0;
[44b5ca0]152 } // switch
[51b73452]153}
154
[8cc5cb0]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
[2f22cc4]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 );
[321f55d]168 assert( branches.size() == 1 );
[2f22cc4]169 thenb = branches.front();
170
171 if ( else_stmt ) {
172 std::list<Statement *> branches;
173 buildList<Statement, StatementNode>( else_stmt, branches );
[321f55d]174 assert( branches.size() == 1 );
[2f22cc4]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 );
[321f55d]183 assert( branches.size() >= 0 ); // size == 0 for switch (...) {}, i.e., no declaration or statements
[2f22cc4]184 return new SwitchStmt( noLabels, maybeBuild<Expression>(ctl), branches );
185}
[321f55d]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}
[2f22cc4]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
[321f55d]224Statement *build_branch( std::string identifier, BranchStmt::Type kind ) {
225 return new BranchStmt( noLabels, identifier, kind );
226}
[8cc5cb0]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}
[321f55d]241
[1d4580a]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
[7f5566b]263
[59db689]264CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {}
[51b73452]265
[59db689]266CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) {
[b87a5ed]267 if ( first ) {
268 last = ( StatementNode *)( stmt->get_last());
269 } else {
270 last = 0;
[44b5ca0]271 } // if
[51b73452]272}
273
[b87a5ed]274CompoundStmtNode::~CompoundStmtNode() {
275 delete first;
[51b73452]276}
277
[b87a5ed]278void CompoundStmtNode::add_statement( StatementNode *stmt ) {
279 if ( stmt != 0 ) {
[1d4580a]280 last->set_last( stmt );
281 last = ( StatementNode *)( stmt->get_next());
[44b5ca0]282 } // if
[51b73452]283}
284
[b87a5ed]285void CompoundStmtNode::print( ostream &os, int indent ) const {
286 if ( first ) {
287 first->printList( os, indent+2 );
[44b5ca0]288 } // if
[51b73452]289}
290
291Statement *CompoundStmtNode::build() const {
[b87a5ed]292 std::list<Label> labs;
[1db21619]293 const std::list<std::string> &labels = get_labels();
[51b73452]294
[1db21619]295 if ( ! labels.empty() ) {
[b87a5ed]296 std::back_insert_iterator< std::list<Label> > lab_it( labs );
[1db21619]297 copy( labels.begin(), labels.end(), lab_it );
[44b5ca0]298 } // if
[51b73452]299
[b87a5ed]300 CompoundStmt *cs = new CompoundStmt( labs );
301 buildList( first, cs->get_kids() );
302 return cs;
[51b73452]303}
304
[7f5566b]305
[777bfcf]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;
[7f5566b]309 if ( gotolabels ) {
310 this->gotolabels = gotolabels->get_labels();
311 delete gotolabels;
312 } // if
313}
314
315AsmStmtNode::~AsmStmtNode() {
[d1625f8]316 delete output; delete input; delete clobber;
[7f5566b]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;
[d1625f8]333 return new AsmStmt( labs, voltile, instruction, out, in, clob, gotolabs );
[7f5566b]334}
335
[777bfcf]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
[51b73452]353// Local Variables: //
[b87a5ed]354// tab-width: 4 //
355// mode: c++ //
356// compile-command: "make install" //
[51b73452]357// End: //
Note: See TracBrowser for help on using the repository browser.