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 : Sat Jul 25 12:19:50 2015 |
---|
13 | // Update Count : 53 |
---|
14 | // |
---|
15 | |
---|
16 | #include <functional> |
---|
17 | #include <algorithm> |
---|
18 | #include <iostream> |
---|
19 | #include <list> |
---|
20 | #include <cassert> |
---|
21 | |
---|
22 | #include "Statement.h" |
---|
23 | #include "Expression.h" |
---|
24 | #include "Declaration.h" |
---|
25 | #include "Common/SemanticError.h" |
---|
26 | |
---|
27 | using std::string; |
---|
28 | using std::endl; |
---|
29 | |
---|
30 | Statement::Statement( std::list<Label> _labels ) : labels( _labels ) {} |
---|
31 | |
---|
32 | void Statement::print( std::ostream &, int indent ) const {} |
---|
33 | |
---|
34 | Statement::~Statement() {} |
---|
35 | |
---|
36 | ExprStmt::ExprStmt( std::list<Label> _labels, Expression *_expr ) : Statement( _labels ), expr( _expr ) {} |
---|
37 | |
---|
38 | ExprStmt::~ExprStmt() {} |
---|
39 | |
---|
40 | void ExprStmt::print( std::ostream &os, int indent ) const { |
---|
41 | os << string( indent, ' ' ) << "Expression Statement:" << endl; |
---|
42 | expr->print( os, indent + 2 ); |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | AsmStmt::AsmStmt( std::list<Label> labels, bool voltile, ConstantExpr *instruction, std::list<Expression *> output, std::list<Expression *> input, std::list<ConstantExpr *> clobber, std::list<Label> gotolabels ) : Statement( labels ), voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ), gotolabels( gotolabels ) {} |
---|
47 | |
---|
48 | AsmStmt::~AsmStmt() { |
---|
49 | delete instruction; |
---|
50 | deleteAll( output ); |
---|
51 | deleteAll( input ); |
---|
52 | deleteAll( clobber ); |
---|
53 | } |
---|
54 | |
---|
55 | void AsmStmt::print( std::ostream &os, int indent ) const { |
---|
56 | os << "Assembler Statement:" << endl; |
---|
57 | os << std::string( indent, ' ' ) << "instruction: " << endl << std::string( indent, ' ' ); |
---|
58 | instruction->print( os, indent + 2 ); |
---|
59 | if ( ! output.empty() ) { |
---|
60 | os << endl << std::string( indent, ' ' ) << "output: " << endl; |
---|
61 | printAll( output, os, indent + 2 ); |
---|
62 | } // if |
---|
63 | if ( ! input.empty() ) { |
---|
64 | os << std::string( indent, ' ' ) << "input: " << endl << std::string( indent, ' ' ); |
---|
65 | printAll( input, os, indent + 2 ); |
---|
66 | } // if |
---|
67 | if ( ! clobber.empty() ) { |
---|
68 | os << std::string( indent, ' ' ) << "clobber: " << endl; |
---|
69 | printAll( clobber, os, indent + 2 ); |
---|
70 | } // if |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" }; |
---|
75 | |
---|
76 | BranchStmt::BranchStmt( std::list<Label> labels, Label _target, Type _type ) throw ( SemanticError ) : |
---|
77 | Statement( labels ), originalTarget( _target ), target( _target ), computedTarget( NULL ), type( _type ) { |
---|
78 | //actually this is a syntactic error signaled by the parser |
---|
79 | if ( type == BranchStmt::Goto && target.size() == 0 ) |
---|
80 | throw SemanticError("goto without target"); |
---|
81 | } |
---|
82 | |
---|
83 | BranchStmt::BranchStmt( std::list<Label> labels, Expression *_computedTarget, Type _type ) throw ( SemanticError ) : |
---|
84 | Statement( labels ), computedTarget( _computedTarget ), type( _type ) { |
---|
85 | if ( type != BranchStmt::Goto || computedTarget == 0 ) |
---|
86 | throw SemanticError("Computed target not valid in branch statement"); |
---|
87 | } |
---|
88 | |
---|
89 | void BranchStmt::print( std::ostream &os, int indent ) const { |
---|
90 | os << string( indent, ' ' ) << "Branch (" << brType[type] << ")" << endl ; |
---|
91 | } |
---|
92 | |
---|
93 | ReturnStmt::ReturnStmt( std::list<Label> labels, Expression *_expr, bool throwP ) : Statement( labels ), expr( _expr ), isThrow( throwP ) {} |
---|
94 | |
---|
95 | ReturnStmt::~ReturnStmt() { |
---|
96 | delete expr; |
---|
97 | } |
---|
98 | |
---|
99 | void ReturnStmt::print( std::ostream &os, int indent ) const { |
---|
100 | os << std::string( indent, ' ' ) << string ( isThrow? "Throw":"Return" ) << " Statement, returning: "; |
---|
101 | if ( expr != 0 ) expr->print( os ); |
---|
102 | os << endl; |
---|
103 | } |
---|
104 | |
---|
105 | IfStmt::IfStmt( std::list<Label> _labels, Expression *_condition, Statement *_thenPart, Statement *_elsePart ): |
---|
106 | Statement( _labels ), condition( _condition ), thenPart( _thenPart ), elsePart( _elsePart ) {} |
---|
107 | |
---|
108 | IfStmt::~IfStmt() {} |
---|
109 | |
---|
110 | void IfStmt::print( std::ostream &os, int indent ) const { |
---|
111 | os << string( indent, ' ' ) << "If on condition: " << endl ; |
---|
112 | condition->print( os, indent + 4 ); |
---|
113 | |
---|
114 | os << string( indent, ' ' ) << ".... and branches: " << endl; |
---|
115 | |
---|
116 | thenPart->print( os, indent + 4 ); |
---|
117 | |
---|
118 | if ( elsePart != 0 ) { |
---|
119 | elsePart->print( os, indent + 4 ); |
---|
120 | } // if |
---|
121 | } |
---|
122 | |
---|
123 | SwitchStmt::SwitchStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_branches ): |
---|
124 | Statement( _labels ), condition( _condition ), branches( _branches ) { |
---|
125 | } |
---|
126 | |
---|
127 | SwitchStmt::~SwitchStmt() { |
---|
128 | delete condition; |
---|
129 | // destroy branches |
---|
130 | } |
---|
131 | |
---|
132 | void SwitchStmt::add_case( CaseStmt *c ) {} |
---|
133 | |
---|
134 | void SwitchStmt::print( std::ostream &os, int indent ) const { |
---|
135 | os << string( indent, ' ' ) << "Switch on condition: "; |
---|
136 | condition->print( os ); |
---|
137 | os << endl; |
---|
138 | |
---|
139 | // branches |
---|
140 | std::list<Statement *>::const_iterator i; |
---|
141 | for ( i = branches.begin(); i != branches.end(); i++) |
---|
142 | (*i)->print( os, indent + 4 ); |
---|
143 | |
---|
144 | //for_each( branches.begin(), branches.end(), mem_fun( bind1st(&Statement::print ), os )); |
---|
145 | } |
---|
146 | |
---|
147 | CaseStmt::CaseStmt( std::list<Label> _labels, Expression *_condition, std::list<Statement *> &_statements, bool deflt ) throw ( SemanticError ) : |
---|
148 | Statement( _labels ), condition( _condition ), stmts( _statements ), _isDefault( deflt ) { |
---|
149 | if ( isDefault() && condition != 0 ) |
---|
150 | throw SemanticError("default with conditions"); |
---|
151 | } |
---|
152 | |
---|
153 | CaseStmt::~CaseStmt() { |
---|
154 | delete condition; |
---|
155 | } |
---|
156 | |
---|
157 | CaseStmt * CaseStmt::makeDefault( std::list<Label> labels, std::list<Statement *> branches ) { |
---|
158 | return new CaseStmt( labels, 0, branches, true ); |
---|
159 | } |
---|
160 | |
---|
161 | void CaseStmt::print( std::ostream &os, int indent ) const { |
---|
162 | os << string( indent, ' ' ); |
---|
163 | |
---|
164 | if ( isDefault() ) |
---|
165 | os << "Default "; |
---|
166 | else { |
---|
167 | os << "Case "; |
---|
168 | condition->print( os ); |
---|
169 | } // if |
---|
170 | |
---|
171 | os << endl; |
---|
172 | |
---|
173 | std::list<Statement *>::const_iterator i; |
---|
174 | for ( i = stmts.begin(); i != stmts.end(); i++) |
---|
175 | (*i )->print( os, indent + 4 ); |
---|
176 | } |
---|
177 | |
---|
178 | //ChooseStmt::ChooseStmt( std::list<Label> labels, Expression *condition, Statement *body ) {} |
---|
179 | ChooseStmt::ChooseStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_branches ): |
---|
180 | Statement( _labels ), condition( _condition ), branches( _branches ) { |
---|
181 | } |
---|
182 | |
---|
183 | ChooseStmt::~ChooseStmt() { |
---|
184 | delete condition; |
---|
185 | } |
---|
186 | |
---|
187 | void ChooseStmt::add_case( CaseStmt *c ) {} |
---|
188 | |
---|
189 | void ChooseStmt::print( std::ostream &os, int indent ) const { |
---|
190 | os << string( indent, ' ' ) << "Choose on condition: "; |
---|
191 | condition->print( os ); |
---|
192 | os << endl; |
---|
193 | |
---|
194 | // branches |
---|
195 | std::list<Statement *>::const_iterator i; |
---|
196 | for ( i = branches.begin(); i != branches.end(); i++) |
---|
197 | (*i )->print( os, indent + 4 ); |
---|
198 | |
---|
199 | //for_each( branches.begin(), branches.end(), mem_fun( bind1st(&Statement::print ), os )); |
---|
200 | } |
---|
201 | |
---|
202 | void FallthruStmt::print( std::ostream &os, int indent ) const { |
---|
203 | os << string( indent, ' ' ) << "Fall-through statement" << endl; |
---|
204 | } |
---|
205 | |
---|
206 | WhileStmt::WhileStmt( std::list<Label> labels, Expression *condition_, Statement *body_, bool isDoWhile_ ): |
---|
207 | Statement( labels ), condition( condition_), body( body_), isDoWhile( isDoWhile_) { |
---|
208 | } |
---|
209 | |
---|
210 | WhileStmt::~WhileStmt() { |
---|
211 | delete body; |
---|
212 | } |
---|
213 | |
---|
214 | void WhileStmt::print( std::ostream &os, int indent ) const { |
---|
215 | os << string( indent, ' ' ) << "While on condition: " << endl ; |
---|
216 | condition->print( os, indent + 4 ); |
---|
217 | |
---|
218 | os << string( indent, ' ' ) << ".... with body: " << endl; |
---|
219 | |
---|
220 | if ( body != 0 ) body->print( os, indent + 4 ); |
---|
221 | } |
---|
222 | |
---|
223 | ForStmt::ForStmt( std::list<Label> labels, std::list<Statement *> initialization_, Expression *condition_, Expression *increment_, Statement *body_ ): |
---|
224 | Statement( labels ), initialization( initialization_ ), condition( condition_ ), increment( increment_ ), body( body_ ) { |
---|
225 | } |
---|
226 | |
---|
227 | ForStmt::~ForStmt() { |
---|
228 | deleteAll( initialization ); |
---|
229 | delete condition; |
---|
230 | delete increment; |
---|
231 | delete body; |
---|
232 | } |
---|
233 | |
---|
234 | void ForStmt::print( std::ostream &os, int indent ) const { |
---|
235 | os << string( indent, ' ' ) << "Labels: {"; |
---|
236 | for ( std::list<Label>::const_iterator it = get_labels().begin(); it != get_labels().end(); ++it) { |
---|
237 | os << *it << ","; |
---|
238 | } |
---|
239 | os << "}" << endl; |
---|
240 | |
---|
241 | os << string( indent, ' ' ) << "For Statement" << endl ; |
---|
242 | |
---|
243 | os << string( indent + 2, ' ' ) << "initialization: \n"; |
---|
244 | for ( std::list<Statement *>::const_iterator it = initialization.begin(); it != initialization.end(); ++it ) { |
---|
245 | (*it)->print( os, indent + 4 ); |
---|
246 | } |
---|
247 | |
---|
248 | os << "\n" << string( indent + 2, ' ' ) << "condition: \n"; |
---|
249 | if ( condition != 0 ) |
---|
250 | condition->print( os, indent + 4 ); |
---|
251 | |
---|
252 | os << "\n" << string( indent + 2, ' ' ) << "increment: \n"; |
---|
253 | if ( increment != 0 ) |
---|
254 | increment->print( os, indent + 4 ); |
---|
255 | |
---|
256 | os << "\n" << string( indent + 2, ' ' ) << "statement block: \n"; |
---|
257 | if ( body != 0 ) |
---|
258 | body->print( os, indent + 4 ); |
---|
259 | |
---|
260 | os << endl; |
---|
261 | } |
---|
262 | |
---|
263 | TryStmt::TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<Statement *> &_handlers, FinallyStmt *_finallyBlock ) : |
---|
264 | Statement( labels ), block( tryBlock ), handlers( _handlers ), finallyBlock( _finallyBlock ) { |
---|
265 | } |
---|
266 | |
---|
267 | TryStmt::TryStmt( const TryStmt &other ) : Statement( other.labels ) { |
---|
268 | block = other.block; |
---|
269 | std::copy( other.handlers.begin(), other.handlers.end(), back_inserter( handlers ) ); |
---|
270 | finallyBlock = other.finallyBlock; |
---|
271 | } |
---|
272 | |
---|
273 | TryStmt::~TryStmt() { |
---|
274 | delete block; |
---|
275 | } |
---|
276 | |
---|
277 | void TryStmt::print( std::ostream &os, int indent ) const { |
---|
278 | os << string( indent, ' ' ) << "Try Statement" << endl; |
---|
279 | os << string( indent + 2, ' ' ) << "with block: " << endl; |
---|
280 | block->print( os, indent + 4 ); |
---|
281 | |
---|
282 | // handlers |
---|
283 | os << string( indent + 2, ' ' ) << "and handlers: " << endl; |
---|
284 | for ( std::list<Statement *>::const_iterator i = handlers.begin(); i != handlers.end(); i++) |
---|
285 | (*i )->print( os, indent + 4 ); |
---|
286 | |
---|
287 | // finally block |
---|
288 | if ( finallyBlock != 0 ) { |
---|
289 | os << string( indent + 2, ' ' ) << "Finally block: " << endl; |
---|
290 | finallyBlock->print( os, indent + 4 ); |
---|
291 | } // if |
---|
292 | } |
---|
293 | |
---|
294 | CatchStmt::CatchStmt( std::list<Label> labels, Declaration *_decl, Statement *_body, bool isCatchRest ) : |
---|
295 | Statement( labels ), decl ( _decl ), body( _body ), catchRest ( isCatchRest ) { |
---|
296 | } |
---|
297 | |
---|
298 | CatchStmt::~CatchStmt() { |
---|
299 | delete decl; |
---|
300 | delete body; |
---|
301 | } |
---|
302 | |
---|
303 | void CatchStmt::print( std::ostream &os, int indent ) const { |
---|
304 | os << string( indent, ' ' ) << "Catch Statement" << endl; |
---|
305 | |
---|
306 | os << string( indent, ' ' ) << "... catching" << endl; |
---|
307 | if ( decl ) { |
---|
308 | decl->printShort( os, indent + 4 ); |
---|
309 | os << endl; |
---|
310 | } else if ( catchRest ) |
---|
311 | os << string( indent + 4 , ' ' ) << "the rest" << endl; |
---|
312 | else |
---|
313 | os << string( indent + 4 , ' ' ) << ">>> Error: this catch clause must have a declaration <<<" << endl; |
---|
314 | } |
---|
315 | |
---|
316 | |
---|
317 | FinallyStmt::FinallyStmt( std::list<Label> labels, CompoundStmt *_block ) : Statement( labels ), block( _block ) { |
---|
318 | assert( labels.empty() ); // finally statement cannot be labeled |
---|
319 | } |
---|
320 | |
---|
321 | FinallyStmt::~FinallyStmt() { |
---|
322 | delete block; |
---|
323 | } |
---|
324 | |
---|
325 | void FinallyStmt::print( std::ostream &os, int indent ) const { |
---|
326 | os << string( indent, ' ' ) << "Finally Statement" << endl; |
---|
327 | os << string( indent + 2, ' ' ) << "with block: " << endl; |
---|
328 | block->print( os, indent + 4 ); |
---|
329 | } |
---|
330 | |
---|
331 | NullStmt::NullStmt( std::list<Label> labels ) : CompoundStmt( labels ) {} |
---|
332 | NullStmt::NullStmt() : CompoundStmt( std::list<Label>() ) {} |
---|
333 | NullStmt::~NullStmt() {} |
---|
334 | |
---|
335 | void NullStmt::print( std::ostream &os, int indent ) const { |
---|
336 | os << string( indent, ' ' ) << "Null Statement" << endl ; |
---|
337 | } |
---|
338 | |
---|
339 | // Local Variables: // |
---|
340 | // tab-width: 4 // |
---|
341 | // mode: c++ // |
---|
342 | // compile-command: "make install" // |
---|
343 | // End: // |
---|