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 : Fri Sep 1 23:25:23 2017
|
---|
13 | // Update Count : 346
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <cassert> // for assert, strict_dynamic_cast, assertf
|
---|
17 | #include <list> // for list
|
---|
18 | #include <string> // for string
|
---|
19 |
|
---|
20 | #include "Common/SemanticError.h" // for SemanticError
|
---|
21 | #include "Common/utility.h" // for maybeMoveBuild, maybeBuild
|
---|
22 | #include "ParseNode.h" // for StatementNode, ExpressionNode, bui...
|
---|
23 | #include "SynTree/Expression.h" // for Expression, ConstantExpr
|
---|
24 | #include "SynTree/Label.h" // for Label, noLabels
|
---|
25 | #include "SynTree/Declaration.h"
|
---|
26 | #include "SynTree/Statement.h" // for Statement, BranchStmt, CaseStmt
|
---|
27 | #include "parserutility.h" // for notZeroExpr
|
---|
28 |
|
---|
29 | class Declaration;
|
---|
30 |
|
---|
31 | using namespace std;
|
---|
32 |
|
---|
33 |
|
---|
34 | StatementNode::StatementNode( DeclarationNode *decl ) {
|
---|
35 | assert( decl );
|
---|
36 | DeclarationNode *agg = decl->extractAggregate();
|
---|
37 | if ( agg ) {
|
---|
38 | StatementNode *nextStmt = new StatementNode( new DeclStmt( 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 = new DeclStmt{ maybeMoveBuild< Declaration >(agg) };
|
---|
52 | } // StatementNode::StatementNode
|
---|
53 |
|
---|
54 | StatementNode *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 = strict_dynamic_cast< StatementNode * >(curr);
|
---|
59 | assert( dynamic_cast< CaseStmt * >(node->stmt) );
|
---|
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);
|
---|
68 | caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
|
---|
69 | return this;
|
---|
70 | }
|
---|
71 |
|
---|
72 | Statement *build_expr( ExpressionNode *ctl ) {
|
---|
73 | Expression *e = maybeMoveBuild< Expression >( ctl );
|
---|
74 |
|
---|
75 | if ( e )
|
---|
76 | return new ExprStmt( e );
|
---|
77 | else
|
---|
78 | return new NullStmt();
|
---|
79 | }
|
---|
80 |
|
---|
81 | Statement *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 | Expression * cond = nullptr;
|
---|
101 | if ( ctl->condition ) {
|
---|
102 | // compare the provided condition against 0
|
---|
103 | cond = notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) );
|
---|
104 | } else {
|
---|
105 | for ( Statement * stmt : init ) {
|
---|
106 | // build the && of all of the declared variables compared against 0
|
---|
107 | DeclStmt * declStmt = strict_dynamic_cast< DeclStmt * >( stmt );
|
---|
108 | DeclarationWithType * dwt = strict_dynamic_cast< DeclarationWithType * >( declStmt->decl );
|
---|
109 | Expression * nze = notZeroExpr( new VariableExpr( dwt ) );
|
---|
110 | cond = cond ? new LogicalExpr( cond, nze, true ) : nze;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | delete ctl;
|
---|
114 | return new IfStmt( cond, thenb, elseb, init );
|
---|
115 | }
|
---|
116 |
|
---|
117 | Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) {
|
---|
118 | std::list< Statement * > branches;
|
---|
119 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
---|
120 | // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
|
---|
121 | return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches );
|
---|
122 | }
|
---|
123 | Statement *build_case( ExpressionNode *ctl ) {
|
---|
124 | std::list< Statement * > branches;
|
---|
125 | return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches );
|
---|
126 | }
|
---|
127 | Statement *build_default() {
|
---|
128 | std::list< Statement * > branches;
|
---|
129 | return new CaseStmt( nullptr, branches, true );
|
---|
130 | }
|
---|
131 |
|
---|
132 | Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {
|
---|
133 | std::list< Statement * > branches;
|
---|
134 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
---|
135 | assert( branches.size() == 1 );
|
---|
136 | return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind );
|
---|
137 | }
|
---|
138 |
|
---|
139 | Statement *build_for( ForCtl *forctl, StatementNode *stmt ) {
|
---|
140 | std::list< Statement * > branches;
|
---|
141 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
---|
142 | assert( branches.size() == 1 );
|
---|
143 |
|
---|
144 | std::list< Statement * > init;
|
---|
145 | if ( forctl->init != 0 ) {
|
---|
146 | buildMoveList( forctl->init, init );
|
---|
147 | } // if
|
---|
148 |
|
---|
149 | Expression *cond = 0;
|
---|
150 | if ( forctl->condition != 0 )
|
---|
151 | cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
|
---|
152 |
|
---|
153 | Expression *incr = 0;
|
---|
154 | if ( forctl->change != 0 )
|
---|
155 | incr = maybeMoveBuild< Expression >(forctl->change);
|
---|
156 |
|
---|
157 | delete forctl;
|
---|
158 | return new ForStmt( init, cond, incr, branches.front() );
|
---|
159 | }
|
---|
160 |
|
---|
161 | Statement *build_branch( BranchStmt::Type kind ) {
|
---|
162 | Statement * ret = new BranchStmt( "", kind );
|
---|
163 | return ret;
|
---|
164 | }
|
---|
165 | Statement *build_branch( std::string *identifier, BranchStmt::Type kind ) {
|
---|
166 | Statement * ret = new BranchStmt( *identifier, kind );
|
---|
167 | delete identifier; // allocated by lexer
|
---|
168 | return ret;
|
---|
169 | }
|
---|
170 | Statement *build_computedgoto( ExpressionNode *ctl ) {
|
---|
171 | return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
|
---|
172 | }
|
---|
173 |
|
---|
174 | Statement *build_return( ExpressionNode *ctl ) {
|
---|
175 | std::list< Expression * > exps;
|
---|
176 | buildMoveList( ctl, exps );
|
---|
177 | return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
|
---|
178 | }
|
---|
179 |
|
---|
180 | Statement *build_throw( ExpressionNode *ctl ) {
|
---|
181 | std::list< Expression * > exps;
|
---|
182 | buildMoveList( ctl, exps );
|
---|
183 | assertf( exps.size() < 2, "This means we are leaking memory");
|
---|
184 | return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
|
---|
185 | }
|
---|
186 |
|
---|
187 | Statement *build_resume( ExpressionNode *ctl ) {
|
---|
188 | std::list< Expression * > exps;
|
---|
189 | buildMoveList( ctl, exps );
|
---|
190 | assertf( exps.size() < 2, "This means we are leaking memory");
|
---|
191 | return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
|
---|
192 | }
|
---|
193 |
|
---|
194 | Statement *build_resume_at( ExpressionNode *ctl, ExpressionNode *target ) {
|
---|
195 | (void)ctl;
|
---|
196 | (void)target;
|
---|
197 | assertf( false, "resume at (non-local throw) is not yet supported," );
|
---|
198 | }
|
---|
199 |
|
---|
200 | Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
|
---|
201 | std::list< CatchStmt * > branches;
|
---|
202 | buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
|
---|
203 | CompoundStmt *tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
|
---|
204 | FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
|
---|
205 | return new TryStmt( tryBlock, branches, finallyBlock );
|
---|
206 | }
|
---|
207 | Statement *build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) {
|
---|
208 | std::list< Statement * > branches;
|
---|
209 | buildMoveList< Statement, StatementNode >( body, branches );
|
---|
210 | assert( branches.size() == 1 );
|
---|
211 | return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
|
---|
212 | }
|
---|
213 | Statement *build_finally( StatementNode *stmt ) {
|
---|
214 | std::list< Statement * > branches;
|
---|
215 | buildMoveList< Statement, StatementNode >( stmt, branches );
|
---|
216 | assert( branches.size() == 1 );
|
---|
217 | return new FinallyStmt( dynamic_cast< CompoundStmt * >( branches.front() ) );
|
---|
218 | }
|
---|
219 |
|
---|
220 | WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) {
|
---|
221 | auto node = new WaitForStmt();
|
---|
222 |
|
---|
223 | WaitForStmt::Target target;
|
---|
224 | target.function = maybeBuild<Expression>( targetExpr );
|
---|
225 |
|
---|
226 | ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
|
---|
227 | targetExpr->set_next( nullptr );
|
---|
228 | buildMoveList< Expression >( next, target.arguments );
|
---|
229 |
|
---|
230 | delete targetExpr;
|
---|
231 |
|
---|
232 | node->clauses.push_back( WaitForStmt::Clause{
|
---|
233 | target,
|
---|
234 | maybeMoveBuild<Statement >( stmt ),
|
---|
235 | notZeroExpr( maybeMoveBuild<Expression>( when ) )
|
---|
236 | });
|
---|
237 |
|
---|
238 | return node;
|
---|
239 | }
|
---|
240 |
|
---|
241 | WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) {
|
---|
242 | WaitForStmt::Target target;
|
---|
243 | target.function = maybeBuild<Expression>( targetExpr );
|
---|
244 |
|
---|
245 | ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
|
---|
246 | targetExpr->set_next( nullptr );
|
---|
247 | buildMoveList< Expression >( next, target.arguments );
|
---|
248 |
|
---|
249 | delete targetExpr;
|
---|
250 |
|
---|
251 | node->clauses.insert( node->clauses.begin(), WaitForStmt::Clause{
|
---|
252 | std::move( target ),
|
---|
253 | maybeMoveBuild<Statement >( stmt ),
|
---|
254 | notZeroExpr( maybeMoveBuild<Expression>( when ) )
|
---|
255 | });
|
---|
256 |
|
---|
257 | return node;
|
---|
258 | }
|
---|
259 |
|
---|
260 | WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) {
|
---|
261 | auto node = new WaitForStmt();
|
---|
262 |
|
---|
263 | if( timeout ) {
|
---|
264 | node->timeout.time = maybeMoveBuild<Expression>( timeout );
|
---|
265 | node->timeout.statement = maybeMoveBuild<Statement >( stmt );
|
---|
266 | node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
|
---|
267 | }
|
---|
268 | else {
|
---|
269 | node->orelse.statement = maybeMoveBuild<Statement >( stmt );
|
---|
270 | node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
|
---|
271 | }
|
---|
272 |
|
---|
273 | return node;
|
---|
274 | }
|
---|
275 |
|
---|
276 | WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when, StatementNode * else_stmt, ExpressionNode * else_when ) {
|
---|
277 | auto node = new WaitForStmt();
|
---|
278 |
|
---|
279 | node->timeout.time = maybeMoveBuild<Expression>( timeout );
|
---|
280 | node->timeout.statement = maybeMoveBuild<Statement >( stmt );
|
---|
281 | node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
|
---|
282 |
|
---|
283 | node->orelse.statement = maybeMoveBuild<Statement >( else_stmt );
|
---|
284 | node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( else_when ) );
|
---|
285 |
|
---|
286 | return node;
|
---|
287 | }
|
---|
288 |
|
---|
289 | WithStmt * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
|
---|
290 | std::list< Expression * > e;
|
---|
291 | buildMoveList( exprs, e );
|
---|
292 | Statement * s = maybeMoveBuild<Statement>( stmt );
|
---|
293 | return new WithStmt( e, s );
|
---|
294 | }
|
---|
295 |
|
---|
296 | Statement *build_compound( StatementNode *first ) {
|
---|
297 | CompoundStmt *cs = new CompoundStmt();
|
---|
298 | buildMoveList( first, cs->get_kids() );
|
---|
299 | return cs;
|
---|
300 | }
|
---|
301 |
|
---|
302 | Statement *build_asmstmt( bool voltile, Expression *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {
|
---|
303 | std::list< Expression * > out, in;
|
---|
304 | std::list< ConstantExpr * > clob;
|
---|
305 |
|
---|
306 | buildMoveList( output, out );
|
---|
307 | buildMoveList( input, in );
|
---|
308 | buildMoveList( clobber, clob );
|
---|
309 | return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
|
---|
310 | }
|
---|
311 |
|
---|
312 | // Local Variables: //
|
---|
313 | // tab-width: 4 //
|
---|
314 | // mode: c++ //
|
---|
315 | // compile-command: "make install" //
|
---|
316 | // End: //
|
---|