source: src/Parser/StatementNode.cc@ 6ac5223

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox 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 6ac5223 was 936e9f4, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

first attempt, add declarations into "if" conditional

  • Property mode set to 100644
File size: 8.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 : Wed Aug 16 16:39:43 2017
13// Update Count : 340
14//
15
16#include <cassert> // for assert, safe_dynamic_cast, assertf
17#include <list> // for list
18#include <memory> // for unique_ptr
19#include <string> // for string
20
21#include "Common/SemanticError.h" // for SemanticError
22#include "Common/utility.h" // for maybeMoveBuild, maybeBuild
23#include "ParseNode.h" // for StatementNode, ExpressionNode, bui...
24#include "SynTree/Expression.h" // for Expression, ConstantExpr
25#include "SynTree/Label.h" // for Label, noLabels
26#include "SynTree/Statement.h" // for Statement, BranchStmt, CaseStmt
27#include "parserutility.h" // for notZeroExpr
28
29class Declaration;
30
31using namespace std;
32
33
34StatementNode::StatementNode( DeclarationNode *decl ) {
35 assert( decl );
36 DeclarationNode *agg = decl->extractAggregate();
37 if ( agg ) {
38 StatementNode *nextStmt = new StatementNode( new DeclStmt( noLabels, maybeBuild< Declaration >( decl ) ) );
39 set_next( nextStmt );
40 if ( decl->get_next() ) {
41 get_next()->set_next( new StatementNode( dynamic_cast< DeclarationNode * >(decl->get_next()) ) );
42 decl->set_next( 0 );
43 } // if
44 } else {
45 if ( decl->get_next() ) {
46 set_next( new StatementNode( dynamic_cast< DeclarationNode * >( decl->get_next() ) ) );
47 decl->set_next( 0 );
48 } // if
49 agg = decl;
50 } // if
51 stmt.reset( new DeclStmt( noLabels, maybeMoveBuild< Declaration >(agg) ) );
52} // StatementNode::StatementNode
53
54StatementNode *StatementNode::append_last_case( StatementNode *stmt ) {
55 StatementNode *prev = this;
56 // find end of list and maintain previous pointer
57 for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
58 StatementNode *node = safe_dynamic_cast< StatementNode * >(curr);
59 assert( dynamic_cast< CaseStmt * >(node->stmt.get()) );
60 prev = curr;
61 } // for
62 // convert from StatementNode list to Statement list
63 StatementNode *node = dynamic_cast< StatementNode * >(prev);
64 std::list< Statement * > stmts;
65 buildMoveList( stmt, stmts );
66 // splice any new Statements to end of current Statements
67 CaseStmt * caseStmt = dynamic_cast< CaseStmt * >(node->stmt.get());
68 caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
69 return this;
70}
71
72Statement *build_expr( ExpressionNode *ctl ) {
73 Expression *e = maybeMoveBuild< Expression >( ctl );
74
75 if ( e )
76 return new ExprStmt( noLabels, e );
77 else
78 return new NullStmt( noLabels );
79}
80
81Statement *build_if( IfCtl * ctl, StatementNode *then_stmt, StatementNode *else_stmt ) {
82 Statement *thenb, *elseb = 0;
83 std::list< Statement * > branches;
84 buildMoveList< Statement, StatementNode >( then_stmt, branches );
85 assert( branches.size() == 1 );
86 thenb = branches.front();
87
88 if ( else_stmt ) {
89 std::list< Statement * > branches;
90 buildMoveList< Statement, StatementNode >( else_stmt, branches );
91 assert( branches.size() == 1 );
92 elseb = branches.front();
93 } // if
94
95 std::list< Statement * > init;
96 if ( ctl->init != 0 ) {
97 buildMoveList( ctl->init, init );
98 } // if
99
100 return new IfStmt( noLabels, notZeroExpr(
101 /*ctl->condition
102 ?*/ maybeMoveBuild< Expression >(ctl->condition)
103 /*: new VariableExpr( init.end() )*/ )
104 , thenb, elseb );
105 // ret->initialization = init;
106 // delete ctl;
107 // assert( ret );
108 // return ret;
109}
110
111Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) {
112 std::list< Statement * > branches;
113 buildMoveList< Statement, StatementNode >( stmt, branches );
114 // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
115 return new SwitchStmt( noLabels, maybeMoveBuild< Expression >(ctl), branches );
116}
117Statement *build_case( ExpressionNode *ctl ) {
118 std::list< Statement * > branches;
119 return new CaseStmt( noLabels, maybeMoveBuild< Expression >(ctl), branches );
120}
121Statement *build_default() {
122 std::list< Statement * > branches;
123 return new CaseStmt( noLabels, nullptr, branches, true );
124}
125
126Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {
127 std::list< Statement * > branches;
128 buildMoveList< Statement, StatementNode >( stmt, branches );
129 assert( branches.size() == 1 );
130 return new WhileStmt( noLabels, notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind );
131}
132
133Statement *build_for( ForCtl *forctl, StatementNode *stmt ) {
134 std::list< Statement * > branches;
135 buildMoveList< Statement, StatementNode >( stmt, branches );
136 assert( branches.size() == 1 );
137
138 std::list< Statement * > init;
139 if ( forctl->init != 0 ) {
140 buildMoveList( forctl->init, init );
141 } // if
142
143 Expression *cond = 0;
144 if ( forctl->condition != 0 )
145 cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
146
147 Expression *incr = 0;
148 if ( forctl->change != 0 )
149 incr = maybeMoveBuild< Expression >(forctl->change);
150
151 delete forctl;
152 return new ForStmt( noLabels, init, cond, incr, branches.front() );
153}
154
155Statement *build_branch( BranchStmt::Type kind ) {
156 Statement * ret = new BranchStmt( noLabels, "", kind );
157 return ret;
158}
159Statement *build_branch( std::string *identifier, BranchStmt::Type kind ) {
160 Statement * ret = new BranchStmt( noLabels, *identifier, kind );
161 delete identifier; // allocated by lexer
162 return ret;
163}
164Statement *build_computedgoto( ExpressionNode *ctl ) {
165 return new BranchStmt( noLabels, maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
166}
167
168Statement *build_return( ExpressionNode *ctl ) {
169 std::list< Expression * > exps;
170 buildMoveList( ctl, exps );
171 return new ReturnStmt( noLabels, exps.size() > 0 ? exps.back() : nullptr );
172}
173
174Statement *build_throw( ExpressionNode *ctl ) {
175 std::list< Expression * > exps;
176 buildMoveList( ctl, exps );
177 assertf( exps.size() < 2, "This means we are leaking memory");
178 return new ThrowStmt( noLabels, ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
179}
180
181Statement *build_resume( ExpressionNode *ctl ) {
182 std::list< Expression * > exps;
183 buildMoveList( ctl, exps );
184 assertf( exps.size() < 2, "This means we are leaking memory");
185 return new ThrowStmt( noLabels, ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
186}
187
188Statement *build_resume_at( ExpressionNode *ctl, ExpressionNode *target ) {
189 (void)ctl;
190 (void)target;
191 assertf( false, "resume at (non-local throw) is not yet supported," );
192}
193
194Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
195 std::list< CatchStmt * > branches;
196 buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
197 CompoundStmt *tryBlock = safe_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
198 FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
199 return new TryStmt( noLabels, tryBlock, branches, finallyBlock );
200}
201Statement *build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) {
202 std::list< Statement * > branches;
203 buildMoveList< Statement, StatementNode >( body, branches );
204 assert( branches.size() == 1 );
205 return new CatchStmt( noLabels, kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
206}
207Statement *build_finally( StatementNode *stmt ) {
208 std::list< Statement * > branches;
209 buildMoveList< Statement, StatementNode >( stmt, branches );
210 assert( branches.size() == 1 );
211 return new FinallyStmt( noLabels, dynamic_cast< CompoundStmt * >( branches.front() ) );
212}
213
214Statement *build_compound( StatementNode *first ) {
215 CompoundStmt *cs = new CompoundStmt( noLabels );
216 buildMoveList( first, cs->get_kids() );
217 return cs;
218}
219
220Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {
221 std::list< Expression * > out, in;
222 std::list< ConstantExpr * > clob;
223
224 buildMoveList( output, out );
225 buildMoveList( input, in );
226 buildMoveList( clobber, clob );
227 return new AsmStmt( noLabels, voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
228}
229
230// Local Variables: //
231// tab-width: 4 //
232// mode: c++ //
233// compile-command: "make install" //
234// End: //
Note: See TracBrowser for help on using the repository browser.