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.h -- |
---|
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 : Thu Aug 4 11:26:02 2016 |
---|
13 | // Update Count : 64 |
---|
14 | // |
---|
15 | |
---|
16 | #ifndef STATEMENT_H |
---|
17 | #define STATEMENT_H |
---|
18 | |
---|
19 | #include "SynTree.h" |
---|
20 | #include "Visitor.h" |
---|
21 | #include "Mutator.h" |
---|
22 | #include "Common/SemanticError.h" |
---|
23 | #include "Type.h" |
---|
24 | #include "Label.h" |
---|
25 | |
---|
26 | class Statement { |
---|
27 | public: |
---|
28 | Statement( std::list<Label> labels ); |
---|
29 | virtual ~Statement(); |
---|
30 | |
---|
31 | std::list<Label> & get_labels() { return labels; } |
---|
32 | const std::list<Label> & get_labels() const { return labels; } |
---|
33 | |
---|
34 | virtual Statement *clone() const = 0; |
---|
35 | virtual void accept( Visitor &v ) = 0; |
---|
36 | virtual Statement *acceptMutator( Mutator &m ) = 0; |
---|
37 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
38 | protected: |
---|
39 | std::list<Label> labels; |
---|
40 | }; |
---|
41 | |
---|
42 | class CompoundStmt : public Statement { |
---|
43 | public: |
---|
44 | CompoundStmt( std::list<Label> labels ); |
---|
45 | CompoundStmt( const CompoundStmt &other ); |
---|
46 | virtual ~CompoundStmt(); |
---|
47 | |
---|
48 | std::list<Statement*>& get_kids() { return kids; } |
---|
49 | |
---|
50 | virtual CompoundStmt *clone() const { return new CompoundStmt( *this ); } |
---|
51 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
52 | virtual CompoundStmt *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
53 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
54 | private: |
---|
55 | std::list<Statement*> kids; |
---|
56 | }; |
---|
57 | |
---|
58 | class ExprStmt : public Statement { |
---|
59 | public: |
---|
60 | ExprStmt( std::list<Label> labels, Expression *expr ); |
---|
61 | ExprStmt( const ExprStmt &other ); |
---|
62 | virtual ~ExprStmt(); |
---|
63 | |
---|
64 | Expression *get_expr() { return expr; } |
---|
65 | void set_expr( Expression *newValue ) { expr = newValue; } |
---|
66 | |
---|
67 | virtual ExprStmt *clone() const { return new ExprStmt( *this ); } |
---|
68 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
69 | virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
70 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
71 | private: |
---|
72 | Expression *expr; |
---|
73 | }; |
---|
74 | |
---|
75 | class AsmStmt : public Statement { |
---|
76 | public: |
---|
77 | AsmStmt( std::list<Label> labels, bool voltile, ConstantExpr *instruction, std::list<Expression *> input, std::list<Expression *> output, std::list<ConstantExpr *> clobber, std::list<Label> gotolabels ); |
---|
78 | AsmStmt( const AsmStmt &other ); |
---|
79 | virtual ~AsmStmt(); |
---|
80 | |
---|
81 | bool get_voltile() { return voltile; } |
---|
82 | void set_voltile( bool newValue ) { voltile = newValue; } |
---|
83 | ConstantExpr *get_instruction() { return instruction; } |
---|
84 | void set_instruction( ConstantExpr *newValue ) { instruction = newValue; } |
---|
85 | std::list<Expression *> &get_output() { return output; } |
---|
86 | void set_output( const std::list<Expression *> &newValue ) { output = newValue; } |
---|
87 | std::list<Expression *> &get_input() { return input; } |
---|
88 | void set_input( const std::list<Expression *> &newValue ) { input = newValue; } |
---|
89 | std::list<ConstantExpr *> &get_clobber() { return clobber; } |
---|
90 | void set_clobber( const std::list<ConstantExpr *> &newValue ) { clobber = newValue; } |
---|
91 | std::list<Label> &get_gotolabels() { return gotolabels; } |
---|
92 | void set_gotolabels( const std::list<Label> &newValue ) { gotolabels = newValue; } |
---|
93 | |
---|
94 | virtual AsmStmt *clone() const { return new AsmStmt( *this ); } |
---|
95 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
96 | virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
97 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
98 | private: |
---|
99 | bool voltile; |
---|
100 | ConstantExpr *instruction; |
---|
101 | std::list<Expression *> output, input; |
---|
102 | std::list<ConstantExpr *> clobber; |
---|
103 | std::list<Label> gotolabels; |
---|
104 | }; |
---|
105 | |
---|
106 | class IfStmt : public Statement { |
---|
107 | public: |
---|
108 | IfStmt( std::list<Label> labels, Expression *condition, Statement *thenPart, Statement *elsePart ); |
---|
109 | IfStmt( const IfStmt &other ); |
---|
110 | virtual ~IfStmt(); |
---|
111 | |
---|
112 | Expression *get_condition() { return condition; } |
---|
113 | void set_condition( Expression *newValue ) { condition = newValue; } |
---|
114 | Statement *get_thenPart() { return thenPart; } |
---|
115 | void set_thenPart( Statement *newValue ) { thenPart = newValue; } |
---|
116 | Statement *get_elsePart() { return elsePart; } |
---|
117 | void set_elsePart( Statement *newValue ) { elsePart = newValue; } |
---|
118 | |
---|
119 | virtual IfStmt *clone() const { return new IfStmt( *this ); } |
---|
120 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
121 | virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
122 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
123 | private: |
---|
124 | Expression *condition; |
---|
125 | Statement *thenPart; |
---|
126 | Statement *elsePart; |
---|
127 | }; |
---|
128 | |
---|
129 | class SwitchStmt : public Statement { |
---|
130 | public: |
---|
131 | SwitchStmt( std::list<Label> labels, Expression *condition, std::list<Statement *> &statements ); |
---|
132 | SwitchStmt( const SwitchStmt &other ); |
---|
133 | virtual ~SwitchStmt(); |
---|
134 | |
---|
135 | Expression *get_condition() { return condition; } |
---|
136 | void set_condition( Expression *newValue ) { condition = newValue; } |
---|
137 | |
---|
138 | std::list<Statement *> & get_statements() { return statements; } |
---|
139 | |
---|
140 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
141 | virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
142 | |
---|
143 | virtual SwitchStmt *clone() const { return new SwitchStmt( *this ); } |
---|
144 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
145 | private: |
---|
146 | Expression * condition; |
---|
147 | std::list<Statement *> statements; |
---|
148 | }; |
---|
149 | |
---|
150 | class CaseStmt : public Statement { |
---|
151 | public: |
---|
152 | CaseStmt( std::list<Label> labels, Expression *conditions, std::list<Statement *> &stmts, bool isdef = false ) throw(SemanticError); |
---|
153 | CaseStmt( const CaseStmt &other ); |
---|
154 | virtual ~CaseStmt(); |
---|
155 | |
---|
156 | static CaseStmt * makeDefault( std::list<Label> labels = std::list<Label>(), std::list<Statement *> stmts = std::list<Statement *>() ); |
---|
157 | |
---|
158 | bool isDefault() const { return _isDefault; } |
---|
159 | void set_default(bool b) { _isDefault = b; } |
---|
160 | |
---|
161 | Expression * &get_condition() { return condition; } |
---|
162 | void set_condition( Expression *newValue ) { condition = newValue; } |
---|
163 | |
---|
164 | std::list<Statement *> &get_statements() { return stmts; } |
---|
165 | void set_statements( std::list<Statement *> &newValue ) { stmts = newValue; } |
---|
166 | |
---|
167 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
168 | virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
169 | |
---|
170 | virtual CaseStmt *clone() const { return new CaseStmt( *this ); } |
---|
171 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
172 | private: |
---|
173 | Expression * condition; |
---|
174 | std::list<Statement *> stmts; |
---|
175 | bool _isDefault; |
---|
176 | }; |
---|
177 | |
---|
178 | class WhileStmt : public Statement { |
---|
179 | public: |
---|
180 | WhileStmt( std::list<Label> labels, Expression *condition, |
---|
181 | Statement *body, bool isDoWhile = false ); |
---|
182 | WhileStmt( const WhileStmt &other ); |
---|
183 | virtual ~WhileStmt(); |
---|
184 | |
---|
185 | Expression *get_condition() { return condition; } |
---|
186 | void set_condition( Expression *newValue ) { condition = newValue; } |
---|
187 | Statement *get_body() { return body; } |
---|
188 | void set_body( Statement *newValue ) { body = newValue; } |
---|
189 | bool get_isDoWhile() { return isDoWhile; } |
---|
190 | void set_isDoWhile( bool newValue ) { isDoWhile = newValue; } |
---|
191 | |
---|
192 | virtual WhileStmt *clone() const { return new WhileStmt( *this ); } |
---|
193 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
194 | virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
195 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
196 | private: |
---|
197 | Expression *condition; |
---|
198 | Statement *body; |
---|
199 | bool isDoWhile; |
---|
200 | }; |
---|
201 | |
---|
202 | class ForStmt : public Statement { |
---|
203 | public: |
---|
204 | ForStmt( std::list<Label> labels, std::list<Statement *> initialization, |
---|
205 | Expression *condition = 0, Expression *increment = 0, Statement *body = 0 ); |
---|
206 | ForStmt( const ForStmt &other ); |
---|
207 | virtual ~ForStmt(); |
---|
208 | |
---|
209 | std::list<Statement *> &get_initialization() { return initialization; } |
---|
210 | void set_initialization( std::list<Statement *> newValue ) { initialization = newValue; } |
---|
211 | Expression *get_condition() { return condition; } |
---|
212 | void set_condition( Expression *newValue ) { condition = newValue; } |
---|
213 | Expression *get_increment() { return increment; } |
---|
214 | void set_increment( Expression *newValue ) { increment = newValue; } |
---|
215 | Statement *get_body() { return body; } |
---|
216 | void set_body( Statement *newValue ) { body = newValue; } |
---|
217 | |
---|
218 | virtual ForStmt *clone() const { return new ForStmt( *this ); } |
---|
219 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
220 | virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
221 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
222 | private: |
---|
223 | std::list<Statement *> initialization; |
---|
224 | Expression *condition; |
---|
225 | Expression *increment; |
---|
226 | Statement *body; |
---|
227 | }; |
---|
228 | |
---|
229 | class BranchStmt : public Statement { |
---|
230 | public: |
---|
231 | enum Type { Goto = 0, Break, Continue }; |
---|
232 | |
---|
233 | BranchStmt( std::list<Label> labels, Label target, Type ) throw (SemanticError); |
---|
234 | BranchStmt( std::list<Label> labels, Expression *computedTarget, Type ) throw (SemanticError); |
---|
235 | |
---|
236 | Label get_originalTarget() { return originalTarget; } |
---|
237 | Label get_target() { return target; } |
---|
238 | void set_target( Label newValue ) { target = newValue; } |
---|
239 | |
---|
240 | Expression *get_computedTarget() { return computedTarget; } |
---|
241 | void set_target( Expression * newValue ) { computedTarget = newValue; } |
---|
242 | |
---|
243 | Type get_type() { return type; } |
---|
244 | const char *get_typename() { return brType[ type ]; } |
---|
245 | |
---|
246 | virtual BranchStmt *clone() const { return new BranchStmt( *this ); } |
---|
247 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
248 | virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
249 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
250 | private: |
---|
251 | static const char *brType[]; |
---|
252 | Label originalTarget; // can give better error messages if we remember the label name that the user entered |
---|
253 | Label target; |
---|
254 | Expression *computedTarget; |
---|
255 | Type type; |
---|
256 | }; |
---|
257 | |
---|
258 | class ReturnStmt : public Statement { |
---|
259 | public: |
---|
260 | ReturnStmt( std::list<Label> labels, Expression *expr, bool throwP = false ); |
---|
261 | ReturnStmt( const ReturnStmt &other ); |
---|
262 | virtual ~ReturnStmt(); |
---|
263 | |
---|
264 | Expression *get_expr() { return expr; } |
---|
265 | void set_expr( Expression *newValue ) { expr = newValue; } |
---|
266 | |
---|
267 | virtual ReturnStmt *clone() const { return new ReturnStmt( *this ); } |
---|
268 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
269 | virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
270 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
271 | private: |
---|
272 | Expression *expr; |
---|
273 | bool isThrow; |
---|
274 | }; |
---|
275 | |
---|
276 | |
---|
277 | class NullStmt : public CompoundStmt { |
---|
278 | public: |
---|
279 | NullStmt(); |
---|
280 | NullStmt( std::list<Label> labels ); |
---|
281 | |
---|
282 | virtual NullStmt *clone() const { return new NullStmt( *this ); } |
---|
283 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
284 | virtual NullStmt *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
285 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
286 | |
---|
287 | private: |
---|
288 | }; |
---|
289 | |
---|
290 | class TryStmt : public Statement { |
---|
291 | public: |
---|
292 | TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<Statement *> &handlers, FinallyStmt *finallyBlock = 0 ); |
---|
293 | TryStmt( const TryStmt &other ); |
---|
294 | virtual ~TryStmt(); |
---|
295 | |
---|
296 | CompoundStmt *get_block() const { return block; } |
---|
297 | void set_block( CompoundStmt *newValue ) { block = newValue; } |
---|
298 | std::list<Statement *>& get_catchers() { return handlers; } |
---|
299 | |
---|
300 | FinallyStmt *get_finally() const { return finallyBlock; } |
---|
301 | void set_finally( FinallyStmt *newValue ) { finallyBlock = newValue; } |
---|
302 | |
---|
303 | virtual TryStmt *clone() const { return new TryStmt( *this ); } |
---|
304 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
305 | virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
306 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
307 | |
---|
308 | private: |
---|
309 | CompoundStmt *block; |
---|
310 | std::list<Statement *> handlers; |
---|
311 | FinallyStmt *finallyBlock; |
---|
312 | }; |
---|
313 | |
---|
314 | class CatchStmt : public Statement { |
---|
315 | public: |
---|
316 | CatchStmt( std::list<Label> labels, Declaration *decl, Statement *body, bool isCatchRest = false ); |
---|
317 | CatchStmt( const CatchStmt &other ); |
---|
318 | virtual ~CatchStmt(); |
---|
319 | |
---|
320 | Declaration *get_decl() { return decl; } |
---|
321 | void set_decl( Declaration *newValue ) { decl = newValue; } |
---|
322 | |
---|
323 | Statement *get_body() { return body; } |
---|
324 | void set_body( Statement *newValue ) { body = newValue; } |
---|
325 | |
---|
326 | virtual CatchStmt *clone() const { return new CatchStmt( *this ); } |
---|
327 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
328 | virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
329 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
330 | |
---|
331 | private: |
---|
332 | Declaration *decl; |
---|
333 | Statement *body; |
---|
334 | bool catchRest; |
---|
335 | }; |
---|
336 | |
---|
337 | class FinallyStmt : public Statement { |
---|
338 | public: |
---|
339 | FinallyStmt( std::list<Label> labels, CompoundStmt *block ); |
---|
340 | FinallyStmt( const FinallyStmt &other ); |
---|
341 | virtual ~FinallyStmt(); |
---|
342 | |
---|
343 | CompoundStmt *get_block() const { return block; } |
---|
344 | void set_block( CompoundStmt *newValue ) { block = newValue; } |
---|
345 | |
---|
346 | virtual FinallyStmt *clone() const { return new FinallyStmt( *this ); } |
---|
347 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
348 | virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
349 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
350 | private: |
---|
351 | CompoundStmt *block; |
---|
352 | }; |
---|
353 | |
---|
354 | |
---|
355 | // represents a declaration that occurs as part of a compound statement |
---|
356 | class DeclStmt : public Statement { |
---|
357 | public: |
---|
358 | DeclStmt( std::list<Label> labels, Declaration *decl ); |
---|
359 | DeclStmt( const DeclStmt &other ); |
---|
360 | virtual ~DeclStmt(); |
---|
361 | |
---|
362 | Declaration *get_decl() const { return decl; } |
---|
363 | void set_decl( Declaration *newValue ) { decl = newValue; } |
---|
364 | |
---|
365 | virtual DeclStmt *clone() const { return new DeclStmt( *this ); } |
---|
366 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
367 | virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
368 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
369 | private: |
---|
370 | Declaration *decl; |
---|
371 | }; |
---|
372 | |
---|
373 | |
---|
374 | /// represents an implicit application of a constructor or destructor. Qualifiers are replaced |
---|
375 | /// immediately before and after the call so that qualified objects can be constructed |
---|
376 | /// with the same functions as unqualified objects. |
---|
377 | class ImplicitCtorDtorStmt : public Statement { |
---|
378 | public: |
---|
379 | ImplicitCtorDtorStmt( Statement * callStmt ); |
---|
380 | ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ); |
---|
381 | virtual ~ImplicitCtorDtorStmt(); |
---|
382 | |
---|
383 | Statement *get_callStmt() const { return callStmt; } |
---|
384 | void set_callStmt( Statement * newValue ) { callStmt = newValue; } |
---|
385 | |
---|
386 | virtual ImplicitCtorDtorStmt *clone() const { return new ImplicitCtorDtorStmt( *this ); } |
---|
387 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
388 | virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
389 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
390 | |
---|
391 | private: |
---|
392 | // Non-owned pointer to the constructor/destructor statement |
---|
393 | Statement * callStmt; |
---|
394 | }; |
---|
395 | |
---|
396 | |
---|
397 | std::ostream & operator<<( std::ostream & out, Statement * statement ); |
---|
398 | |
---|
399 | #endif // STATEMENT_H |
---|
400 | |
---|
401 | // Local Variables: // |
---|
402 | // tab-width: 4 // |
---|
403 | // mode: c++ // |
---|
404 | // compile-command: "make install" // |
---|
405 | // End: // |
---|