source: src/SynTree/Statement.cc@ bd06384

new-env with_gc
Last change on this file since bd06384 was 68f9c43, checked in by Aaron Moss <a3moss@…>, 8 years ago

First pass at delete removal

  • Property mode set to 100644
File size: 13.8 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// 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
31using std::string;
32using std::endl;
33
34Statement::Statement( const std::list<Label> & labels ) : labels( labels ) {}
35
36void Statement::print( std::ostream & os, Indenter ) const {
37 if ( ! labels.empty() ) {
38 os << "Labels: {";
39 for ( const Label & l : labels ) {
40 os << l << ",";
41 }
42 os << "}" << endl;
43 }
44}
45
46ExprStmt::ExprStmt( Expression *expr ) : Statement(), expr( expr ) {}
47
48ExprStmt::ExprStmt( const ExprStmt &other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
49
50void ExprStmt::print( std::ostream &os, Indenter indent ) const {
51 os << "Expression Statement:" << endl << indent+1;
52 expr->print( os, indent+1 );
53}
54
55
56AsmStmt::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
58AsmStmt::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
64void 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
83const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };
84
85BranchStmt::BranchStmt( Label target, Type type ) throw ( SemanticErrorException ) :
86 Statement(), originalTarget( target ), target( target ), computedTarget( nullptr ), type( type ) {
87 //actually this is a syntactic error signaled by the parser
88 if ( type == BranchStmt::Goto && target.empty() ) {
89 SemanticError( target.get_statement()->location, "goto without target");
90 }
91}
92
93BranchStmt::BranchStmt( Expression *computedTarget, Type type ) throw ( SemanticErrorException ) :
94 Statement(), computedTarget( computedTarget ), type( type ) {
95 if ( type != BranchStmt::Goto || computedTarget == nullptr ) {
96 SemanticError( computedTarget->location, "Computed target not valid in branch statement");
97 }
98}
99
100void BranchStmt::print( std::ostream &os, Indenter indent ) const {
101 os << "Branch (" << brType[type] << ")" << endl ;
102 if ( target != "" ) os << indent+1 << "with target: " << target << endl;
103 if ( originalTarget != "" ) os << indent+1 << "with original target: " << originalTarget << endl;
104 if ( computedTarget != nullptr ) os << indent+1 << "with computed target: " << computedTarget << endl;
105}
106
107ReturnStmt::ReturnStmt( Expression *expr ) : Statement(), expr( expr ) {}
108
109ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
110
111void ReturnStmt::print( std::ostream &os, Indenter indent ) const {
112 os << "Return Statement, returning: ";
113 if ( expr != nullptr ) {
114 os << endl << indent+1;
115 expr->print( os, indent+1 );
116 }
117 os << endl;
118}
119
120IfStmt::IfStmt( Expression *condition, Statement *thenPart, Statement *elsePart, std::list<Statement *> initialization ):
121 Statement(), condition( condition ), thenPart( thenPart ), elsePart( elsePart ), initialization( initialization ) {}
122
123IfStmt::IfStmt( const IfStmt & other ) :
124 Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {
125 cloneAll( other.initialization, initialization );
126}
127
128void IfStmt::print( std::ostream &os, Indenter indent ) const {
129 os << "If on condition: " << endl;
130 os << indent+1;
131 condition->print( os, indent+1 );
132
133 if ( !initialization.empty() ) {
134 os << indent << "... with initialization: \n";
135 for ( const Statement * stmt : initialization ) {
136 os << indent+1;
137 stmt->print( os, indent+1 );
138 }
139 os << endl;
140 }
141
142 os << indent << "... then: " << endl;
143
144 os << indent+1;
145 thenPart->print( os, indent+1 );
146
147 if ( elsePart != 0 ) {
148 os << indent << "... else: " << endl;
149 os << indent+1;
150 elsePart->print( os, indent+1 );
151 } // if
152}
153
154SwitchStmt::SwitchStmt( Expression * condition, const std::list<Statement *> &statements ):
155 Statement(), condition( condition ), statements( statements ) {
156}
157
158SwitchStmt::SwitchStmt( const SwitchStmt & other ):
159 Statement( other ), condition( maybeClone( other.condition ) ) {
160 cloneAll( other.statements, statements );
161}
162
163void SwitchStmt::print( std::ostream &os, Indenter indent ) const {
164 os << "Switch on condition: ";
165 condition->print( os );
166 os << endl;
167
168 for ( const Statement * stmt : statements ) {
169 stmt->print( os, indent+1 );
170 }
171}
172
173CaseStmt::CaseStmt( Expression *condition, const std::list<Statement *> &statements, bool deflt ) throw ( SemanticErrorException ) :
174 Statement(), condition( condition ), stmts( statements ), _isDefault( deflt ) {
175 if ( isDefault() && condition != 0 ) SemanticError( condition, "default case with condition: " );
176}
177
178CaseStmt::CaseStmt( const CaseStmt & other ) :
179 Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
180 cloneAll( other.stmts, stmts );
181}
182
183CaseStmt * CaseStmt::makeDefault( const std::list<Label> & labels, std::list<Statement *> stmts ) {
184 CaseStmt * stmt = new CaseStmt( nullptr, stmts, true );
185 stmt->labels = labels;
186 return stmt;
187}
188
189void CaseStmt::print( std::ostream &os, Indenter indent ) const {
190 if ( isDefault() ) os << "Default ";
191 else {
192 os << "Case ";
193 condition->print( os, indent );
194 } // if
195 os << endl;
196
197 for ( Statement * stmt : stmts ) {
198 stmt->print( os, indent+1 );
199 }
200}
201
202WhileStmt::WhileStmt( Expression *condition, Statement *body, bool isDoWhile ):
203 Statement(), condition( condition), body( body), isDoWhile( isDoWhile) {
204}
205
206WhileStmt::WhileStmt( const WhileStmt & other ):
207 Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
208}
209
210void WhileStmt::print( std::ostream &os, Indenter indent ) const {
211 os << "While on condition: " << endl ;
212 condition->print( os, indent+1 );
213
214 os << indent << "... with body: " << endl;
215
216 if ( body != 0 ) body->print( os, indent+1 );
217}
218
219ForStmt::ForStmt( std::list<Statement *> initialization, Expression *condition, Expression *increment, Statement *body ):
220 Statement(), initialization( initialization ), condition( condition ), increment( increment ), body( body ) {
221}
222
223ForStmt::ForStmt( const ForStmt & other ):
224 Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
225 cloneAll( other.initialization, initialization );
226
227}
228
229void ForStmt::print( std::ostream &os, Indenter indent ) const {
230 Statement::print( os, indent ); // print labels
231
232 os << "For Statement" << endl;
233
234 if ( ! initialization.empty() ) {
235 os << indent << "... initialization: \n";
236 for ( Statement * stmt : initialization ) {
237 os << indent+1;
238 stmt->print( os, indent+1 );
239 }
240 }
241
242 if ( condition != nullptr ) {
243 os << indent << "... condition: \n" << indent+1;
244 condition->print( os, indent+1 );
245 }
246
247 if ( increment != nullptr ) {
248 os << "\n" << indent << "... increment: \n" << indent+1;
249 increment->print( os, indent+1 );
250 }
251
252 if ( body != 0 ) {
253 os << "\n" << indent << "... with body: \n" << indent+1;
254 body->print( os, indent+1 );
255 }
256 os << endl;
257}
258
259ThrowStmt::ThrowStmt( Kind kind, Expression * expr, Expression * target ) :
260 Statement(), kind(kind), expr(expr), target(target) {
261 assertf(Resume == kind || nullptr == target, "Non-local termination throw is not accepted." );
262}
263
264ThrowStmt::ThrowStmt( const ThrowStmt &other ) :
265 Statement ( other ), kind( other.kind ), expr( maybeClone( other.expr ) ), target( maybeClone( other.target ) ) {
266}
267
268void ThrowStmt::print( std::ostream &os, Indenter indent) const {
269 if ( target ) os << "Non-Local ";
270 os << "Throw Statement, raising: ";
271 expr->print(os, indent+1);
272 if ( target ) {
273 os << "... at: ";
274 target->print(os, indent+1);
275 }
276}
277
278TryStmt::TryStmt( CompoundStmt *tryBlock, std::list<CatchStmt *> &handlers, FinallyStmt *finallyBlock ) :
279 Statement(), block( tryBlock ), handlers( handlers ), finallyBlock( finallyBlock ) {
280}
281
282TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
283 cloneAll( other.handlers, handlers );
284}
285
286void TryStmt::print( std::ostream &os, Indenter indent ) const {
287 os << "Try Statement" << endl;
288 os << indent << "... with block:" << endl << indent+1;
289 block->print( os, indent+1 );
290
291 // handlers
292 os << indent << "... and handlers:" << endl;
293 for ( const CatchStmt * stmt : handlers ) {
294 os << indent+1;
295 stmt->print( os, indent+1 );
296 }
297
298 // finally block
299 if ( finallyBlock != 0 ) {
300 os << indent << "... and finally:" << endl << indent+1;
301 finallyBlock->print( os, indent+1 );
302 } // if
303}
304
305CatchStmt::CatchStmt( Kind kind, Declaration *decl, Expression *cond, Statement *body ) :
306 Statement(), kind ( kind ), decl ( decl ), cond ( cond ), body( body ) {
307 assertf( decl, "Catch clause must have a declaration." );
308}
309
310CatchStmt::CatchStmt( const CatchStmt & other ) :
311 Statement( other ), kind ( other.kind ), decl ( maybeClone( other.decl ) ), cond ( maybeClone( other.cond ) ), body( maybeClone( other.body ) ) {
312}
313
314void CatchStmt::print( std::ostream &os, Indenter indent ) const {
315 os << "Catch " << ((Terminate == kind) ? "Terminate" : "Resume") << " Statement" << endl;
316
317 os << indent << "... catching: ";
318 decl->printShort( os, indent+1 );
319 os << endl;
320
321 if ( cond ) {
322 os << indent << "... with conditional:" << endl << indent+1;
323 cond->print( os, indent+1 );
324 }
325
326 os << indent << "... with block:" << endl;
327 os << indent+1;
328 body->print( os, indent+1 );
329}
330
331
332FinallyStmt::FinallyStmt( CompoundStmt *block ) : Statement(), block( block ) {
333}
334
335FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
336}
337
338void FinallyStmt::print( std::ostream &os, Indenter indent ) const {
339 os << "Finally Statement" << endl;
340 os << indent << "... with block:" << endl << indent+1;
341 block->print( os, indent+1 );
342}
343
344WaitForStmt::WaitForStmt() : Statement() {
345 timeout.time = nullptr;
346 timeout.statement = nullptr;
347 timeout.condition = nullptr;
348 orelse .statement = nullptr;
349 orelse .condition = nullptr;
350}
351
352WaitForStmt::WaitForStmt( const WaitForStmt & other ) : Statement( other ) {
353 clauses.reserve( other.clauses.size() );
354 for( auto & ocl : other.clauses ) {
355 clauses.emplace_back();
356 clauses.back().target.function = ocl.target.function->clone();
357 cloneAll( ocl.target.arguments, clauses.back().target.arguments );
358 clauses.back().statement = ocl.statement->clone();
359 clauses.back().condition = ocl.condition->clone();
360 }
361
362 timeout.time = other.timeout.time ->clone();
363 timeout.statement = other.timeout.statement->clone();
364 timeout.condition = other.timeout.condition->clone();
365 orelse .statement = other.orelse .statement->clone();
366 orelse .condition = other.orelse .condition->clone();
367}
368
369void WaitForStmt::print( std::ostream &os, Indenter indent ) const {
370 os << "Waitfor Statement" << endl;
371 os << indent << "... with block:" << endl << indent+1;
372 // block->print( os, indent + 4 );
373}
374
375
376WithStmt::WithStmt( const std::list< Expression * > & exprs, Statement * stmt ) : Statement(), exprs( exprs ), stmt( stmt ) {}
377WithStmt::WithStmt( const WithStmt & other ) : Statement( other ), stmt( maybeClone( other.stmt ) ) {
378 cloneAll( other.exprs, exprs );
379}
380
381void WithStmt::print( std::ostream & os, Indenter indent ) const {
382 os << "With statement" << endl;
383 os << indent << "... with expressions: " << endl;
384 printAll( exprs, os, indent+1 );
385 os << indent << "... with statement:" << endl << indent+1;
386 stmt->print( os, indent+1 );
387}
388
389
390NullStmt::NullStmt( const std::list<Label> & labels ) : Statement( labels ) {
391}
392
393void NullStmt::print( std::ostream &os, Indenter ) const {
394 os << "Null Statement" << endl;
395}
396
397ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement(), callStmt( callStmt ) {
398 assert( callStmt );
399}
400
401ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
402}
403
404void ImplicitCtorDtorStmt::print( std::ostream &os, Indenter indent ) const {
405 os << "Implicit Ctor Dtor Statement" << endl;
406 os << indent << "... with Ctor/Dtor: ";
407 callStmt->print( os, indent+1);
408 os << endl;
409}
410
411// Local Variables: //
412// tab-width: 4 //
413// mode: c++ //
414// compile-command: "make install" //
415// End: //
Note: See TracBrowser for help on using the repository browser.