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