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 | // Statement.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Sun Sep 3 20:46:44 2017 |
---|
13 | // Update Count : 68 |
---|
14 | // |
---|
15 | |
---|
16 | #include "SynTree/Statement.h" |
---|
17 | |
---|
18 | #include <stddef.h> // for NULL |
---|
19 | #include <cassert> // for assert, assertf |
---|
20 | #include <iostream> // for operator<<, basic_ostream, endl |
---|
21 | #include <list> // for list, list<>::const_iterator, _Lis... |
---|
22 | #include <string> // for operator<<, string, char_traits |
---|
23 | |
---|
24 | #include "Common/SemanticError.h" // for SemanticError |
---|
25 | #include "Common/utility.h" // for maybeClone, cloneAll, deleteAll |
---|
26 | #include "Declaration.h" // for Declaration |
---|
27 | #include "Expression.h" // for Expression, ConstantExpr |
---|
28 | #include "Statement.h" // for Statement, ForStmt, AsmStmt, Catch... |
---|
29 | #include "SynTree/Label.h" // for Label, operator<< |
---|
30 | |
---|
31 | using std::string; |
---|
32 | using std::endl; |
---|
33 | |
---|
34 | Statement::Statement( const std::list<Label> & labels ) : labels( labels ) {} |
---|
35 | |
---|
36 | void Statement::print( std::ostream & os, Indenter indent ) const { |
---|
37 | if ( ! labels.empty() ) { |
---|
38 | os << indent << "... Labels: {"; |
---|
39 | for ( const Label & l : labels ) { |
---|
40 | os << l << ","; |
---|
41 | } |
---|
42 | os << "}" << endl; |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | ExprStmt::ExprStmt( Expression *expr ) : Statement(), expr( expr ) {} |
---|
47 | |
---|
48 | ExprStmt::ExprStmt( const ExprStmt &other ) : Statement( other ), expr( maybeClone( other.expr ) ) {} |
---|
49 | |
---|
50 | void ExprStmt::print( std::ostream &os, Indenter indent ) const { |
---|
51 | os << "Expression Statement:" << endl << indent+1; |
---|
52 | expr->print( os, indent+1 ); |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | AsmStmt::AsmStmt( bool voltile, Expression *instruction, std::list<Expression *> output, std::list<Expression *> input, std::list<ConstantExpr *> clobber, std::list<Label> gotolabels ) : Statement(), voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ), gotolabels( gotolabels ) {} |
---|
57 | |
---|
58 | AsmStmt::AsmStmt( const AsmStmt & other ) : Statement( other ), voltile( other.voltile ), instruction( maybeClone( other.instruction ) ), gotolabels( other.gotolabels ) { |
---|
59 | cloneAll( other.output, output ); |
---|
60 | cloneAll( other.input, input ); |
---|
61 | cloneAll( other.clobber, clobber ); |
---|
62 | } |
---|
63 | |
---|
64 | void AsmStmt::print( std::ostream &os, Indenter indent ) const { |
---|
65 | os << "Assembler Statement:" << endl; |
---|
66 | os << indent+1 << "instruction: " << endl << indent; |
---|
67 | instruction->print( os, indent+1 ); |
---|
68 | if ( ! output.empty() ) { |
---|
69 | os << endl << indent+1 << "output: " << endl; |
---|
70 | printAll( output, os, indent+1 ); |
---|
71 | } // if |
---|
72 | if ( ! input.empty() ) { |
---|
73 | os << indent+1 << "input: " << endl; |
---|
74 | printAll( input, os, indent+1 ); |
---|
75 | } // if |
---|
76 | if ( ! clobber.empty() ) { |
---|
77 | os << indent+1 << "clobber: " << endl; |
---|
78 | printAll( clobber, os, indent+1 ); |
---|
79 | } // if |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | DirectiveStmt::DirectiveStmt( const std::string & directive ) : Statement(), directive( directive ) {} |
---|
84 | |
---|
85 | void DirectiveStmt::print( std::ostream &os, Indenter ) const { |
---|
86 | os << "GCC Directive:" << directive << endl; |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" }; |
---|
91 | |
---|
92 | BranchStmt::BranchStmt( Label target, Type type ) throw ( SemanticErrorException ) : |
---|
93 | Statement(), originalTarget( target ), target( target ), computedTarget( nullptr ), type( type ) { |
---|
94 | //actually this is a syntactic error signaled by the parser |
---|
95 | if ( type == BranchStmt::Goto && target.empty() ) { |
---|
96 | SemanticError( target.get_statement()->location, "goto without target"); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | BranchStmt::BranchStmt( Expression *computedTarget, Type type ) throw ( SemanticErrorException ) : |
---|
101 | Statement(), computedTarget( computedTarget ), type( type ) { |
---|
102 | if ( type != BranchStmt::Goto || computedTarget == nullptr ) { |
---|
103 | SemanticError( computedTarget->location, "Computed target not valid in branch statement"); |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | void BranchStmt::print( std::ostream &os, Indenter indent ) const { |
---|
108 | os << "Branch (" << brType[type] << ")" << endl ; |
---|
109 | if ( target != "" ) os << indent+1 << "with target: " << target << endl; |
---|
110 | if ( originalTarget != "" ) os << indent+1 << "with original target: " << originalTarget << endl; |
---|
111 | if ( computedTarget != nullptr ) os << indent+1 << "with computed target: " << computedTarget << endl; |
---|
112 | } |
---|
113 | |
---|
114 | ReturnStmt::ReturnStmt( Expression *expr ) : Statement(), expr( expr ) {} |
---|
115 | |
---|
116 | ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ) {} |
---|
117 | |
---|
118 | void ReturnStmt::print( std::ostream &os, Indenter indent ) const { |
---|
119 | os << "Return Statement, returning: "; |
---|
120 | if ( expr != nullptr ) { |
---|
121 | os << endl << indent+1; |
---|
122 | expr->print( os, indent+1 ); |
---|
123 | } |
---|
124 | os << endl; |
---|
125 | } |
---|
126 | |
---|
127 | IfStmt::IfStmt( Expression *condition, Statement *thenPart, Statement *elsePart, std::list<Statement *> initialization ): |
---|
128 | Statement(), condition( condition ), thenPart( thenPart ), elsePart( elsePart ), initialization( initialization ) {} |
---|
129 | |
---|
130 | IfStmt::IfStmt( const IfStmt & other ) : |
---|
131 | Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) { |
---|
132 | cloneAll( other.initialization, initialization ); |
---|
133 | } |
---|
134 | |
---|
135 | void IfStmt::print( std::ostream &os, Indenter indent ) const { |
---|
136 | os << "If on condition: " << endl; |
---|
137 | os << indent+1; |
---|
138 | condition->print( os, indent+1 ); |
---|
139 | |
---|
140 | if ( !initialization.empty() ) { |
---|
141 | os << indent << "... with initialization: \n"; |
---|
142 | for ( const Statement * stmt : initialization ) { |
---|
143 | os << indent+1; |
---|
144 | stmt->print( os, indent+1 ); |
---|
145 | } |
---|
146 | os << endl; |
---|
147 | } |
---|
148 | |
---|
149 | os << indent << "... then: " << endl; |
---|
150 | |
---|
151 | os << indent+1; |
---|
152 | thenPart->print( os, indent+1 ); |
---|
153 | |
---|
154 | if ( elsePart != 0 ) { |
---|
155 | os << indent << "... else: " << endl; |
---|
156 | os << indent+1; |
---|
157 | elsePart->print( os, indent+1 ); |
---|
158 | } // if |
---|
159 | } |
---|
160 | |
---|
161 | SwitchStmt::SwitchStmt( Expression * condition, const std::list<Statement *> &statements ): |
---|
162 | Statement(), condition( condition ), statements( statements ) { |
---|
163 | } |
---|
164 | |
---|
165 | SwitchStmt::SwitchStmt( const SwitchStmt & other ): |
---|
166 | Statement( other ), condition( maybeClone( other.condition ) ) { |
---|
167 | cloneAll( other.statements, statements ); |
---|
168 | } |
---|
169 | |
---|
170 | void SwitchStmt::print( std::ostream &os, Indenter indent ) const { |
---|
171 | os << "Switch on condition: "; |
---|
172 | condition->print( os ); |
---|
173 | os << endl; |
---|
174 | |
---|
175 | for ( const Statement * stmt : statements ) { |
---|
176 | stmt->print( os, indent+1 ); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | CaseStmt::CaseStmt( Expression *condition, const std::list<Statement *> &statements, bool deflt ) throw ( SemanticErrorException ) : |
---|
181 | Statement(), condition( condition ), stmts( statements ), _isDefault( deflt ) { |
---|
182 | if ( isDefault() && condition != 0 ) SemanticError( condition, "default case with condition: " ); |
---|
183 | } |
---|
184 | |
---|
185 | CaseStmt::CaseStmt( const CaseStmt & other ) : |
---|
186 | Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) { |
---|
187 | cloneAll( other.stmts, stmts ); |
---|
188 | } |
---|
189 | |
---|
190 | CaseStmt * CaseStmt::makeDefault( const std::list<Label> & labels, std::list<Statement *> stmts ) { |
---|
191 | CaseStmt * stmt = new CaseStmt( nullptr, stmts, true ); |
---|
192 | stmt->labels = labels; |
---|
193 | return stmt; |
---|
194 | } |
---|
195 | |
---|
196 | void CaseStmt::print( std::ostream &os, Indenter indent ) const { |
---|
197 | if ( isDefault() ) os << indent << "Default "; |
---|
198 | else { |
---|
199 | os << indent << "Case "; |
---|
200 | condition->print( os, indent ); |
---|
201 | } // if |
---|
202 | os << endl; |
---|
203 | |
---|
204 | for ( Statement * stmt : stmts ) { |
---|
205 | os << indent+1; |
---|
206 | stmt->print( os, indent+1 ); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | WhileStmt::WhileStmt( Expression *condition, Statement *body, std::list< Statement * > & initialization, bool isDoWhile ): |
---|
211 | Statement(), condition( condition), body( body), initialization( initialization ), isDoWhile( isDoWhile) { |
---|
212 | } |
---|
213 | |
---|
214 | WhileStmt::WhileStmt( const WhileStmt & other ): |
---|
215 | Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) { |
---|
216 | } |
---|
217 | |
---|
218 | void WhileStmt::print( std::ostream &os, Indenter indent ) const { |
---|
219 | os << "While on condition: " << endl ; |
---|
220 | condition->print( os, indent+1 ); |
---|
221 | |
---|
222 | os << indent << "... with body: " << endl; |
---|
223 | |
---|
224 | if ( body != 0 ) body->print( os, indent+1 ); |
---|
225 | } |
---|
226 | |
---|
227 | ForStmt::ForStmt( std::list<Statement *> initialization, Expression *condition, Expression *increment, Statement *body ): |
---|
228 | Statement(), initialization( initialization ), condition( condition ), increment( increment ), body( body ) { |
---|
229 | } |
---|
230 | |
---|
231 | ForStmt::ForStmt( const ForStmt & other ): |
---|
232 | Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) { |
---|
233 | cloneAll( other.initialization, initialization ); |
---|
234 | |
---|
235 | } |
---|
236 | |
---|
237 | void ForStmt::print( std::ostream &os, Indenter indent ) const { |
---|
238 | Statement::print( os, indent ); // print labels |
---|
239 | |
---|
240 | os << "For Statement" << endl; |
---|
241 | |
---|
242 | if ( ! initialization.empty() ) { |
---|
243 | os << indent << "... initialization: \n"; |
---|
244 | for ( Statement * stmt : initialization ) { |
---|
245 | os << indent+1; |
---|
246 | stmt->print( os, indent+1 ); |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | if ( condition != nullptr ) { |
---|
251 | os << indent << "... condition: \n" << indent+1; |
---|
252 | condition->print( os, indent+1 ); |
---|
253 | } |
---|
254 | |
---|
255 | if ( increment != nullptr ) { |
---|
256 | os << "\n" << indent << "... increment: \n" << indent+1; |
---|
257 | increment->print( os, indent+1 ); |
---|
258 | } |
---|
259 | |
---|
260 | if ( body != 0 ) { |
---|
261 | os << "\n" << indent << "... with body: \n" << indent+1; |
---|
262 | body->print( os, indent+1 ); |
---|
263 | } |
---|
264 | os << endl; |
---|
265 | } |
---|
266 | |
---|
267 | ThrowStmt::ThrowStmt( Kind kind, Expression * expr, Expression * target ) : |
---|
268 | Statement(), kind(kind), expr(expr), target(target) { |
---|
269 | assertf(Resume == kind || nullptr == target, "Non-local termination throw is not accepted." ); |
---|
270 | } |
---|
271 | |
---|
272 | ThrowStmt::ThrowStmt( const ThrowStmt &other ) : |
---|
273 | Statement ( other ), kind( other.kind ), expr( maybeClone( other.expr ) ), target( maybeClone( other.target ) ) { |
---|
274 | } |
---|
275 | |
---|
276 | void ThrowStmt::print( std::ostream &os, Indenter indent) const { |
---|
277 | if ( target ) os << "Non-Local "; |
---|
278 | os << "Throw Statement, raising: "; |
---|
279 | expr->print(os, indent+1); |
---|
280 | if ( target ) { |
---|
281 | os << "... at: "; |
---|
282 | target->print(os, indent+1); |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | TryStmt::TryStmt( CompoundStmt *tryBlock, std::list<CatchStmt *> &handlers, FinallyStmt *finallyBlock ) : |
---|
287 | Statement(), block( tryBlock ), handlers( handlers ), finallyBlock( finallyBlock ) { |
---|
288 | } |
---|
289 | |
---|
290 | TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) { |
---|
291 | cloneAll( other.handlers, handlers ); |
---|
292 | } |
---|
293 | |
---|
294 | void TryStmt::print( std::ostream &os, Indenter indent ) const { |
---|
295 | os << "Try Statement" << endl; |
---|
296 | os << indent << "... with block:" << endl << indent+1; |
---|
297 | block->print( os, indent+1 ); |
---|
298 | |
---|
299 | // handlers |
---|
300 | os << indent << "... and handlers:" << endl; |
---|
301 | for ( const CatchStmt * stmt : handlers ) { |
---|
302 | os << indent+1; |
---|
303 | stmt->print( os, indent+1 ); |
---|
304 | } |
---|
305 | |
---|
306 | // finally block |
---|
307 | if ( finallyBlock != 0 ) { |
---|
308 | os << indent << "... and finally:" << endl << indent+1; |
---|
309 | finallyBlock->print( os, indent+1 ); |
---|
310 | } // if |
---|
311 | } |
---|
312 | |
---|
313 | CatchStmt::CatchStmt( Kind kind, Declaration *decl, Expression *cond, Statement *body ) : |
---|
314 | Statement(), kind ( kind ), decl ( decl ), cond ( cond ), body( body ) { |
---|
315 | assertf( decl, "Catch clause must have a declaration." ); |
---|
316 | } |
---|
317 | |
---|
318 | CatchStmt::CatchStmt( const CatchStmt & other ) : |
---|
319 | Statement( other ), kind ( other.kind ), decl ( maybeClone( other.decl ) ), cond ( maybeClone( other.cond ) ), body( maybeClone( other.body ) ) { |
---|
320 | } |
---|
321 | |
---|
322 | void CatchStmt::print( std::ostream &os, Indenter indent ) const { |
---|
323 | os << "Catch " << ((Terminate == kind) ? "Terminate" : "Resume") << " Statement" << endl; |
---|
324 | |
---|
325 | os << indent << "... catching: "; |
---|
326 | decl->printShort( os, indent+1 ); |
---|
327 | os << endl; |
---|
328 | |
---|
329 | if ( cond ) { |
---|
330 | os << indent << "... with conditional:" << endl << indent+1; |
---|
331 | cond->print( os, indent+1 ); |
---|
332 | } |
---|
333 | |
---|
334 | os << indent << "... with block:" << endl; |
---|
335 | os << indent+1; |
---|
336 | body->print( os, indent+1 ); |
---|
337 | } |
---|
338 | |
---|
339 | |
---|
340 | FinallyStmt::FinallyStmt( CompoundStmt *block ) : Statement(), block( block ) { |
---|
341 | } |
---|
342 | |
---|
343 | FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) { |
---|
344 | } |
---|
345 | |
---|
346 | void FinallyStmt::print( std::ostream &os, Indenter indent ) const { |
---|
347 | os << "Finally Statement" << endl; |
---|
348 | os << indent << "... with block:" << endl << indent+1; |
---|
349 | block->print( os, indent+1 ); |
---|
350 | } |
---|
351 | |
---|
352 | WaitForStmt::WaitForStmt() : Statement() { |
---|
353 | timeout.time = nullptr; |
---|
354 | timeout.statement = nullptr; |
---|
355 | timeout.condition = nullptr; |
---|
356 | orelse .statement = nullptr; |
---|
357 | orelse .condition = nullptr; |
---|
358 | } |
---|
359 | |
---|
360 | WaitForStmt::WaitForStmt( const WaitForStmt & other ) : Statement( other ) { |
---|
361 | clauses.reserve( other.clauses.size() ); |
---|
362 | for( auto & ocl : other.clauses ) { |
---|
363 | clauses.emplace_back(); |
---|
364 | clauses.back().target.function = ocl.target.function->clone(); |
---|
365 | cloneAll( ocl.target.arguments, clauses.back().target.arguments ); |
---|
366 | clauses.back().statement = ocl.statement->clone(); |
---|
367 | clauses.back().condition = ocl.condition->clone(); |
---|
368 | } |
---|
369 | |
---|
370 | timeout.time = other.timeout.time ->clone(); |
---|
371 | timeout.statement = other.timeout.statement->clone(); |
---|
372 | timeout.condition = other.timeout.condition->clone(); |
---|
373 | orelse .statement = other.orelse .statement->clone(); |
---|
374 | orelse .condition = other.orelse .condition->clone(); |
---|
375 | } |
---|
376 | |
---|
377 | void WaitForStmt::print( std::ostream &os, Indenter indent ) const { |
---|
378 | os << "Waitfor Statement" << endl; |
---|
379 | indent += 1; |
---|
380 | for( auto & clause : clauses ) { |
---|
381 | os << indent << "target function :"; |
---|
382 | if(clause.target.function) { clause.target.function->print(os, indent + 1); } |
---|
383 | os << endl << indent << "with arguments :" << endl; |
---|
384 | for( auto & thing : clause.target.arguments) { |
---|
385 | if(thing) { thing->print(os, indent + 1); } |
---|
386 | } |
---|
387 | os << indent << " with statment :" << endl; |
---|
388 | if(clause.statement) { clause.statement->print(os, indent + 1); } |
---|
389 | |
---|
390 | os << indent << " with condition :" << endl; |
---|
391 | if(clause.condition) { clause.condition->print(os, indent + 1); } |
---|
392 | } |
---|
393 | |
---|
394 | os << indent << " timeout of :" << endl; |
---|
395 | if(timeout.time) { timeout.time->print(os, indent + 1); } |
---|
396 | |
---|
397 | os << indent << " with statment :" << endl; |
---|
398 | if(timeout.statement) { timeout.statement->print(os, indent + 1); } |
---|
399 | |
---|
400 | os << indent << " with condition :" << endl; |
---|
401 | if(timeout.condition) { timeout.condition->print(os, indent + 1); } |
---|
402 | |
---|
403 | |
---|
404 | os << indent << " else :" << endl; |
---|
405 | if(orelse.statement) { orelse.statement->print(os, indent + 1); } |
---|
406 | |
---|
407 | os << indent << " with condition :" << endl; |
---|
408 | if(orelse.condition) { orelse.condition->print(os, indent + 1); } |
---|
409 | } |
---|
410 | |
---|
411 | |
---|
412 | WithStmt::WithStmt( const std::list< Expression * > & exprs, Statement * stmt ) : Statement(), exprs( exprs ), stmt( stmt ) {} |
---|
413 | WithStmt::WithStmt( const WithStmt & other ) : Statement( other ), stmt( maybeClone( other.stmt ) ) { |
---|
414 | cloneAll( other.exprs, exprs ); |
---|
415 | } |
---|
416 | |
---|
417 | void WithStmt::print( std::ostream & os, Indenter indent ) const { |
---|
418 | os << "With statement" << endl; |
---|
419 | os << indent << "... with expressions: " << endl; |
---|
420 | printAll( exprs, os, indent+1 ); |
---|
421 | os << indent << "... with statement:" << endl << indent+1; |
---|
422 | stmt->print( os, indent+1 ); |
---|
423 | } |
---|
424 | |
---|
425 | |
---|
426 | NullStmt::NullStmt( const std::list<Label> & labels ) : Statement( labels ) { |
---|
427 | } |
---|
428 | |
---|
429 | void NullStmt::print( std::ostream &os, Indenter indent ) const { |
---|
430 | os << "Null Statement" << endl; |
---|
431 | Statement::print( os, indent ); |
---|
432 | } |
---|
433 | |
---|
434 | ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement(), callStmt( callStmt ) { |
---|
435 | assert( callStmt ); |
---|
436 | } |
---|
437 | |
---|
438 | ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) { |
---|
439 | } |
---|
440 | |
---|
441 | void ImplicitCtorDtorStmt::print( std::ostream &os, Indenter indent ) const { |
---|
442 | os << "Implicit Ctor Dtor Statement" << endl; |
---|
443 | os << indent << "... with Ctor/Dtor: "; |
---|
444 | callStmt->print( os, indent+1); |
---|
445 | os << endl; |
---|
446 | } |
---|
447 | |
---|
448 | // Local Variables: // |
---|
449 | // tab-width: 4 // |
---|
450 | // mode: c++ // |
---|
451 | // compile-command: "make install" // |
---|
452 | // End: // |
---|