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 : Sat Aug 4 09:39:25 2018 |
---|
13 | // Update Count : 363 |
---|
14 | // |
---|
15 | |
---|
16 | #include <cassert> // for assert, strict_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/Declaration.h" |
---|
27 | #include "SynTree/Statement.h" // for Statement, BranchStmt, CaseStmt |
---|
28 | #include "parserutility.h" // for notZeroExpr |
---|
29 | |
---|
30 | class Declaration; |
---|
31 | |
---|
32 | using namespace std; |
---|
33 | |
---|
34 | |
---|
35 | StatementNode::StatementNode( DeclarationNode * decl ) { |
---|
36 | assert( decl ); |
---|
37 | DeclarationNode * agg = decl->extractAggregate(); |
---|
38 | if ( agg ) { |
---|
39 | StatementNode * nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) ); |
---|
40 | set_next( nextStmt ); |
---|
41 | if ( decl->get_next() ) { |
---|
42 | get_next()->set_next( new StatementNode( dynamic_cast< DeclarationNode * >(decl->get_next()) ) ); |
---|
43 | decl->set_next( 0 ); |
---|
44 | } // if |
---|
45 | } else { |
---|
46 | if ( decl->get_next() ) { |
---|
47 | set_next( new StatementNode( dynamic_cast< DeclarationNode * >( decl->get_next() ) ) ); |
---|
48 | decl->set_next( 0 ); |
---|
49 | } // if |
---|
50 | agg = decl; |
---|
51 | } // if |
---|
52 | stmt.reset( new DeclStmt( maybeMoveBuild< Declaration >(agg) ) ); |
---|
53 | } // StatementNode::StatementNode |
---|
54 | |
---|
55 | StatementNode * StatementNode::append_last_case( StatementNode * stmt ) { |
---|
56 | StatementNode * prev = this; |
---|
57 | // find end of list and maintain previous pointer |
---|
58 | for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) { |
---|
59 | StatementNode * node = strict_dynamic_cast< StatementNode * >(curr); |
---|
60 | assert( dynamic_cast< CaseStmt * >(node->stmt.get()) ); |
---|
61 | prev = curr; |
---|
62 | } // for |
---|
63 | // convert from StatementNode list to Statement list |
---|
64 | StatementNode * node = dynamic_cast< StatementNode * >(prev); |
---|
65 | std::list< Statement * > stmts; |
---|
66 | buildMoveList( stmt, stmts ); |
---|
67 | // splice any new Statements to end of current Statements |
---|
68 | CaseStmt * caseStmt = dynamic_cast< CaseStmt * >(node->stmt.get()); |
---|
69 | caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts ); |
---|
70 | return this; |
---|
71 | } // StatementNode::append_last_case |
---|
72 | |
---|
73 | Statement * build_expr( ExpressionNode * ctl ) { |
---|
74 | Expression * e = maybeMoveBuild< Expression >( ctl ); |
---|
75 | |
---|
76 | if ( e ) return new ExprStmt( e ); |
---|
77 | else return new NullStmt(); |
---|
78 | } // build_expr |
---|
79 | |
---|
80 | Expression * build_if_control( IfCtrl * ctl, std::list< Statement * > & init ) { |
---|
81 | if ( ctl->init != 0 ) { |
---|
82 | buildMoveList( ctl->init, init ); |
---|
83 | } // if |
---|
84 | |
---|
85 | Expression * cond = nullptr; |
---|
86 | if ( ctl->condition ) { |
---|
87 | // compare the provided condition against 0 |
---|
88 | cond = notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) ); |
---|
89 | } else { |
---|
90 | for ( Statement * stmt : init ) { |
---|
91 | // build the && of all of the declared variables compared against 0 |
---|
92 | DeclStmt * declStmt = strict_dynamic_cast< DeclStmt * >( stmt ); |
---|
93 | DeclarationWithType * dwt = strict_dynamic_cast< DeclarationWithType * >( declStmt->decl ); |
---|
94 | Expression * nze = notZeroExpr( new VariableExpr( dwt ) ); |
---|
95 | cond = cond ? new LogicalExpr( cond, nze, true ) : nze; |
---|
96 | } |
---|
97 | } |
---|
98 | delete ctl; |
---|
99 | return cond; |
---|
100 | } // build_if_control |
---|
101 | |
---|
102 | Statement * build_if( IfCtrl * ctl, StatementNode * then_stmt, StatementNode * else_stmt ) { |
---|
103 | Statement * thenb, * elseb = nullptr; |
---|
104 | std::list< Statement * > branches; |
---|
105 | buildMoveList< Statement, StatementNode >( then_stmt, branches ); |
---|
106 | assert( branches.size() == 1 ); |
---|
107 | thenb = branches.front(); |
---|
108 | |
---|
109 | if ( else_stmt ) { |
---|
110 | std::list< Statement * > branches; |
---|
111 | buildMoveList< Statement, StatementNode >( else_stmt, branches ); |
---|
112 | assert( branches.size() == 1 ); |
---|
113 | elseb = branches.front(); |
---|
114 | } // if |
---|
115 | |
---|
116 | std::list< Statement * > init; |
---|
117 | Expression * cond = build_if_control( ctl, init ); |
---|
118 | return new IfStmt( cond, thenb, elseb, init ); |
---|
119 | } // build_if |
---|
120 | |
---|
121 | Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) { |
---|
122 | std::list< Statement * > branches; |
---|
123 | buildMoveList< Statement, StatementNode >( stmt, branches ); |
---|
124 | if ( ! isSwitch ) { // choose statement |
---|
125 | for ( Statement * stmt : branches ) { |
---|
126 | CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt ); |
---|
127 | if ( ! caseStmt->stmts.empty() ) { // code after "case" => end of case list |
---|
128 | CompoundStmt * block = strict_dynamic_cast< CompoundStmt * >( caseStmt->stmts.front() ); |
---|
129 | block->kids.push_back( new BranchStmt( "", BranchStmt::Break ) ); |
---|
130 | } // if |
---|
131 | } // for |
---|
132 | } // if |
---|
133 | // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements |
---|
134 | return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches ); |
---|
135 | } // build_switch |
---|
136 | |
---|
137 | Statement * build_case( ExpressionNode * ctl ) { |
---|
138 | std::list< Statement * > branches; |
---|
139 | return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches ); |
---|
140 | } // build_case |
---|
141 | |
---|
142 | Statement * build_default() { |
---|
143 | std::list< Statement * > branches; |
---|
144 | return new CaseStmt( nullptr, branches, true ); |
---|
145 | } // build_default |
---|
146 | |
---|
147 | Statement * build_while( IfCtrl * ctl, StatementNode * stmt ) { |
---|
148 | std::list< Statement * > branches; |
---|
149 | buildMoveList< Statement, StatementNode >( stmt, branches ); |
---|
150 | assert( branches.size() == 1 ); |
---|
151 | |
---|
152 | std::list< Statement * > init; |
---|
153 | Expression * cond = build_if_control( ctl, init ); |
---|
154 | return new WhileStmt( cond, branches.front(), init, false ); |
---|
155 | } // build_while |
---|
156 | |
---|
157 | Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt ) { |
---|
158 | std::list< Statement * > branches; |
---|
159 | buildMoveList< Statement, StatementNode >( stmt, branches ); |
---|
160 | assert( branches.size() == 1 ); |
---|
161 | |
---|
162 | std::list< Statement * > init; |
---|
163 | return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), init, true ); |
---|
164 | } // build_do_while |
---|
165 | |
---|
166 | Statement * build_for( ForCtrl * forctl, StatementNode * stmt ) { |
---|
167 | std::list< Statement * > branches; |
---|
168 | buildMoveList< Statement, StatementNode >( stmt, branches ); |
---|
169 | assert( branches.size() == 1 ); |
---|
170 | |
---|
171 | std::list< Statement * > init; |
---|
172 | if ( forctl->init != 0 ) { |
---|
173 | buildMoveList( forctl->init, init ); |
---|
174 | } // if |
---|
175 | |
---|
176 | Expression * cond = 0; |
---|
177 | if ( forctl->condition != 0 ) |
---|
178 | cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) ); |
---|
179 | |
---|
180 | Expression * incr = 0; |
---|
181 | if ( forctl->change != 0 ) |
---|
182 | incr = maybeMoveBuild< Expression >(forctl->change); |
---|
183 | |
---|
184 | delete forctl; |
---|
185 | return new ForStmt( init, cond, incr, branches.front() ); |
---|
186 | } // build_for |
---|
187 | |
---|
188 | Statement * build_branch( BranchStmt::Type kind ) { |
---|
189 | Statement * ret = new BranchStmt( "", kind ); |
---|
190 | return ret; |
---|
191 | } // build_branch |
---|
192 | |
---|
193 | Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) { |
---|
194 | Statement * ret = new BranchStmt( * identifier, kind ); |
---|
195 | delete identifier; // allocated by lexer |
---|
196 | return ret; |
---|
197 | } // build_branch |
---|
198 | |
---|
199 | Statement * build_computedgoto( ExpressionNode * ctl ) { |
---|
200 | return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto ); |
---|
201 | } // build_computedgoto |
---|
202 | |
---|
203 | Statement * build_return( ExpressionNode * ctl ) { |
---|
204 | std::list< Expression * > exps; |
---|
205 | buildMoveList( ctl, exps ); |
---|
206 | return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr ); |
---|
207 | } // build_return |
---|
208 | |
---|
209 | Statement * build_throw( ExpressionNode * ctl ) { |
---|
210 | std::list< Expression * > exps; |
---|
211 | buildMoveList( ctl, exps ); |
---|
212 | assertf( exps.size() < 2, "This means we are leaking memory"); |
---|
213 | return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr ); |
---|
214 | } // build_throw |
---|
215 | |
---|
216 | Statement * build_resume( ExpressionNode * ctl ) { |
---|
217 | std::list< Expression * > exps; |
---|
218 | buildMoveList( ctl, exps ); |
---|
219 | assertf( exps.size() < 2, "This means we are leaking memory"); |
---|
220 | return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr ); |
---|
221 | } // build_resume |
---|
222 | |
---|
223 | Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) { |
---|
224 | (void)ctl; |
---|
225 | (void)target; |
---|
226 | assertf( false, "resume at (non-local throw) is not yet supported," ); |
---|
227 | } // build_resume_at |
---|
228 | |
---|
229 | Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt ) { |
---|
230 | std::list< CatchStmt * > branches; |
---|
231 | buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches ); |
---|
232 | CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt)); |
---|
233 | FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) ); |
---|
234 | return new TryStmt( tryBlock, branches, finallyBlock ); |
---|
235 | } // build_try |
---|
236 | |
---|
237 | Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) { |
---|
238 | std::list< Statement * > branches; |
---|
239 | buildMoveList< Statement, StatementNode >( body, branches ); |
---|
240 | assert( branches.size() == 1 ); |
---|
241 | return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() ); |
---|
242 | } // build_catch |
---|
243 | |
---|
244 | Statement * build_finally( StatementNode * stmt ) { |
---|
245 | std::list< Statement * > branches; |
---|
246 | buildMoveList< Statement, StatementNode >( stmt, branches ); |
---|
247 | assert( branches.size() == 1 ); |
---|
248 | return new FinallyStmt( dynamic_cast< CompoundStmt * >( branches.front() ) ); |
---|
249 | } // build_finally |
---|
250 | |
---|
251 | WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) { |
---|
252 | auto node = new WaitForStmt(); |
---|
253 | |
---|
254 | WaitForStmt::Target target; |
---|
255 | target.function = maybeBuild<Expression>( targetExpr ); |
---|
256 | |
---|
257 | ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() ); |
---|
258 | targetExpr->set_next( nullptr ); |
---|
259 | buildMoveList< Expression >( next, target.arguments ); |
---|
260 | |
---|
261 | delete targetExpr; |
---|
262 | |
---|
263 | node->clauses.push_back( WaitForStmt::Clause{ |
---|
264 | target, |
---|
265 | maybeMoveBuild<Statement >( stmt ), |
---|
266 | notZeroExpr( maybeMoveBuild<Expression>( when ) ) |
---|
267 | }); |
---|
268 | |
---|
269 | return node; |
---|
270 | } // build_waitfor |
---|
271 | |
---|
272 | WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) { |
---|
273 | WaitForStmt::Target target; |
---|
274 | target.function = maybeBuild<Expression>( targetExpr ); |
---|
275 | |
---|
276 | ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() ); |
---|
277 | targetExpr->set_next( nullptr ); |
---|
278 | buildMoveList< Expression >( next, target.arguments ); |
---|
279 | |
---|
280 | delete targetExpr; |
---|
281 | |
---|
282 | node->clauses.insert( node->clauses.begin(), WaitForStmt::Clause{ |
---|
283 | std::move( target ), |
---|
284 | maybeMoveBuild<Statement >( stmt ), |
---|
285 | notZeroExpr( maybeMoveBuild<Expression>( when ) ) |
---|
286 | }); |
---|
287 | |
---|
288 | return node; |
---|
289 | } // build_waitfor |
---|
290 | |
---|
291 | WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) { |
---|
292 | auto node = new WaitForStmt(); |
---|
293 | |
---|
294 | if( timeout ) { |
---|
295 | node->timeout.time = maybeMoveBuild<Expression>( timeout ); |
---|
296 | node->timeout.statement = maybeMoveBuild<Statement >( stmt ); |
---|
297 | node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) ); |
---|
298 | } else { |
---|
299 | node->orelse.statement = maybeMoveBuild<Statement >( stmt ); |
---|
300 | node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) ); |
---|
301 | } // if |
---|
302 | |
---|
303 | return node; |
---|
304 | } // build_waitfor_timeout |
---|
305 | |
---|
306 | WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when, StatementNode * else_stmt, ExpressionNode * else_when ) { |
---|
307 | auto node = new WaitForStmt(); |
---|
308 | |
---|
309 | node->timeout.time = maybeMoveBuild<Expression>( timeout ); |
---|
310 | node->timeout.statement = maybeMoveBuild<Statement >( stmt ); |
---|
311 | node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) ); |
---|
312 | |
---|
313 | node->orelse.statement = maybeMoveBuild<Statement >( else_stmt ); |
---|
314 | node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( else_when ) ); |
---|
315 | |
---|
316 | return node; |
---|
317 | } // build_waitfor_timeout |
---|
318 | |
---|
319 | WithStmt * build_with( ExpressionNode * exprs, StatementNode * stmt ) { |
---|
320 | std::list< Expression * > e; |
---|
321 | buildMoveList( exprs, e ); |
---|
322 | Statement * s = maybeMoveBuild<Statement>( stmt ); |
---|
323 | return new WithStmt( e, s ); |
---|
324 | } // build_with |
---|
325 | |
---|
326 | Statement * build_compound( StatementNode * first ) { |
---|
327 | CompoundStmt * cs = new CompoundStmt(); |
---|
328 | buildMoveList( first, cs->get_kids() ); |
---|
329 | return cs; |
---|
330 | } // build_compound |
---|
331 | |
---|
332 | Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) { |
---|
333 | std::list< Expression * > out, in; |
---|
334 | std::list< ConstantExpr * > clob; |
---|
335 | |
---|
336 | buildMoveList( output, out ); |
---|
337 | buildMoveList( input, in ); |
---|
338 | buildMoveList( clobber, clob ); |
---|
339 | return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels ); |
---|
340 | } // build_asm |
---|
341 | |
---|
342 | Statement * build_directive( string * directive ) { |
---|
343 | return new DirectiveStmt( *directive ); |
---|
344 | } // build_directive |
---|
345 | |
---|
346 | // Local Variables: // |
---|
347 | // tab-width: 4 // |
---|
348 | // mode: c++ // |
---|
349 | // compile-command: "make install" // |
---|
350 | // End: // |
---|