source: src/SynTree/Statement.h @ b2152e7a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since b2152e7a was b2152e7a, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

fix MLE hack that puts a dangling break statement outside of a case statement in a switch

  • Property mode set to 100644
File size: 12.4 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.h --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Rob Schluntz
12// Last Modified On : Tue Jun 02 13:07:25 2015
13// Update Count     : 13
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
24class Statement {
25  public:
26        Statement( std::list<Label> labels );
27        virtual ~Statement();
28
29        std::list<Label> & get_labels() { return labels; }
30
31        virtual Statement *clone() const = 0;
32        virtual void accept( Visitor &v ) = 0;
33        virtual Statement *acceptMutator( Mutator &m ) = 0;
34        virtual void print( std::ostream &os, int indent = 0 );
35  protected:
36        std::list<Label> labels;
37};
38
39class CompoundStmt : public Statement {
40  public:
41        CompoundStmt( std::list<Label> labels );
42        CompoundStmt( const CompoundStmt &other );
43        virtual ~CompoundStmt();
44
45        std::list<Statement*>& get_kids() { return kids; }
46
47        virtual CompoundStmt *clone() const { return new CompoundStmt( *this ); }
48        virtual void accept( Visitor &v ) { v.visit( this ); }
49        virtual CompoundStmt *acceptMutator( Mutator &m ) { return m.mutate( this ); }
50        virtual void print( std::ostream &os, int indent = 0 );
51  private:
52        std::list<Statement*> kids;
53};
54
55class ExprStmt : public Statement {
56  public:
57        ExprStmt( std::list<Label> labels, Expression *expr );
58        virtual ~ExprStmt();
59
60        Expression *get_expr() { return expr; }
61        void set_expr( Expression *newValue ) { expr = newValue; }
62
63        virtual ExprStmt *clone() const { return new ExprStmt( *this ); }
64        virtual void accept( Visitor &v ) { v.visit( this ); }
65        virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); }
66        virtual void print( std::ostream &os, int indent = 0 );
67  private:
68        Expression *expr;
69};
70
71class IfStmt : public Statement {
72  public:
73        IfStmt( std::list<Label> labels, Expression *condition, Statement *thenPart, Statement *elsePart );
74        virtual ~IfStmt();
75
76        Expression *get_condition() { return condition; }
77        void set_condition( Expression *newValue ) { condition = newValue; }
78        Statement *get_thenPart() { return thenPart; }
79        void set_thenPart( Statement *newValue ) { thenPart = newValue; }
80        Statement *get_elsePart() { return elsePart; }
81        void set_elsePart( Statement *newValue ) { elsePart = newValue; }
82       
83        virtual IfStmt *clone() const { return new IfStmt( *this ); }
84        virtual void accept( Visitor &v ) { v.visit( this ); }
85        virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); }
86        virtual void print( std::ostream &os, int indent = 0 );
87  private:
88        Expression *condition;
89        Statement *thenPart;
90        Statement *elsePart;
91};
92
93class SwitchStmt : public Statement {
94  public:
95        SwitchStmt( std::list<Label> labels, Expression *condition, std::list<Statement *> &branches );
96        virtual ~SwitchStmt();
97
98        Expression *get_condition() { return condition; }
99        void set_condition( Expression *newValue ) { condition = newValue; }
100
101        std::list<Statement *>& get_branches() { return branches; }
102        void add_case( CaseStmt * );
103
104        virtual void accept( Visitor &v ) { v.visit( this ); }
105        virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); }
106
107        virtual SwitchStmt *clone() const { return new SwitchStmt( *this ); }
108        virtual void print( std::ostream &os, int indent = 0 );
109  private:
110        Expression * condition;
111        std::list<Statement *> branches; // should be list of CaseStmt
112};
113
114class ChooseStmt : public Statement {
115  public:
116        ChooseStmt( std::list<Label> labels, Expression *condition, std::list<Statement *> &branches );
117        virtual ~ChooseStmt();
118
119        Expression *get_condition() { return condition; }
120        void set_condition( Expression *newValue ) { condition = newValue; }
121
122        std::list<Statement *>& get_branches() { return branches; }
123        void add_case( CaseStmt * );
124
125        virtual void accept( Visitor &v ) { v.visit( this ); }
126        virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); }
127
128        virtual ChooseStmt *clone() const { return new ChooseStmt( *this ); }
129        virtual void print( std::ostream &os, int indent = 0 );
130  private:
131        Expression *condition;
132        std::list<Statement *> branches; // should be list of CaseStmt
133};
134
135class FallthruStmt : public Statement {
136  public:
137        FallthruStmt( std::list<Label> labels ) : Statement( labels ) { }
138
139        virtual void accept( Visitor &v ) { v.visit( this ); }
140        virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); }
141
142        virtual FallthruStmt *clone() const { return new FallthruStmt( *this ); }
143        virtual void print( std::ostream &os, int indent = 0 );
144};
145
146class CaseStmt : public Statement {
147  public:
148        CaseStmt( std::list<Label> labels, Expression *conditions, 
149              std::list<Statement *> &stmts, bool isdef = false ) throw(SemanticError);
150        virtual ~CaseStmt();
151
152        static CaseStmt * makeDefault( std::list<Label> labels = std::list<Label>(),
153                std::list<Statement *> stmts = std::list<Statement *>() );
154
155        bool isDefault() { return _isDefault; }
156        void set_default(bool b) { _isDefault = b; }
157
158        Expression * &get_condition() { return condition; }
159        void set_condition( Expression *newValue ) { condition = newValue; }
160
161        std::list<Statement *> &get_statements() { return stmts; }
162        void set_statements( std::list<Statement *> &newValue ) { stmts = newValue; }
163       
164        virtual void accept( Visitor &v ) { v.visit( this ); }
165        virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); }
166
167        virtual CaseStmt *clone() const { return new CaseStmt( *this ); }
168        virtual void print( std::ostream &os, int indent = 0 );
169  private:
170        Expression * condition;
171        std::list<Statement *> stmts;
172        bool _isDefault;
173};
174
175class WhileStmt : public Statement {
176  public:
177        WhileStmt( std::list<Label> labels, Expression *condition,
178               Statement *body, bool isDoWhile = false );
179        virtual ~WhileStmt();
180
181        Expression *get_condition() { return condition; }
182        void set_condition( Expression *newValue ) { condition = newValue; }
183        Statement *get_body() { return body; }
184        void set_body( Statement *newValue ) { body = newValue; }
185        bool get_isDoWhile() { return isDoWhile; }
186        void set_isDoWhile( bool newValue ) { isDoWhile = newValue; }
187       
188        virtual WhileStmt *clone() const { return new WhileStmt( *this ); }
189        virtual void accept( Visitor &v ) { v.visit( this ); }
190        virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); }
191        virtual void print( std::ostream &os, int indent = 0 );
192  private:
193        Expression *condition;
194        Statement *body;
195        bool isDoWhile;
196};
197
198class ForStmt : public Statement {
199  public:
200        ForStmt( std::list<Label> labels, Statement *initialization = 0,
201             Expression *condition = 0, Expression *increment = 0, Statement *body = 0 );
202        virtual ~ForStmt();
203
204        Statement *get_initialization() { return initialization; }
205        void set_initialization( Statement *newValue ) { initialization = newValue; }
206        Expression *get_condition() { return condition; }
207        void set_condition( Expression *newValue ) { condition = newValue; }
208        Expression *get_increment() { return increment; }
209        void set_increment( Expression *newValue ) { increment = newValue; }
210        Statement *get_body() { return body; }
211        void set_body( Statement *newValue ) { body = newValue; }
212       
213        virtual ForStmt *clone() const { return new ForStmt( *this ); }
214        virtual void accept( Visitor &v ) { v.visit( this ); }
215        virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); }
216        virtual void print( std::ostream &os, int indent = 0 );
217  private:
218        Statement *initialization;
219        Expression *condition;
220        Expression *increment;
221        Statement *body;
222};
223
224class BranchStmt : public Statement {
225  public:
226        enum Type { Goto = 0 , Break, Continue };
227
228        BranchStmt( std::list<Label> labels, Label target, Type ) throw (SemanticError);
229        BranchStmt( std::list<Label> labels, Expression *computedTarget, Type ) throw (SemanticError);
230        virtual ~BranchStmt() {}
231
232        Label get_originalTarget() { return originalTarget; }
233        Label get_target() { return target; }
234        void set_target( Label newValue ) { target = newValue; }
235       
236        Expression *get_computedTarget() { return computedTarget; }
237        void set_target( Expression * newValue ) { computedTarget = newValue; }
238
239        Type get_type() { return type; }
240        const char *get_typename() { return brType[ type ]; }
241
242        virtual BranchStmt *clone() const { return new BranchStmt( *this ); }
243        virtual void accept( Visitor &v ) { v.visit( this ); }
244        virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); }
245        virtual void print( std::ostream &os, int indent = 0 );
246  private:
247        static const char *brType[];
248        Label originalTarget;  // can give better error messages if we remember the label name that the user entered
249        Label target;
250        Expression *computedTarget;
251        Type type;
252};
253
254class ReturnStmt : public Statement {
255  public:
256        ReturnStmt( std::list<Label> labels, Expression *expr, bool throwP = false );
257        virtual ~ReturnStmt();
258
259        Expression *get_expr() { return expr; }
260        void set_expr( Expression *newValue ) { expr = newValue; }
261       
262        virtual ReturnStmt *clone() const { return new ReturnStmt( *this ); }
263        virtual void accept( Visitor &v ) { v.visit( this ); }
264        virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); }
265        virtual void print( std::ostream &os, int indent = 0 );
266  private:
267        Expression *expr;
268        bool isThrow;
269};
270
271
272class NullStmt : public CompoundStmt {
273  public:
274        NullStmt();
275        NullStmt( std::list<Label> labels );
276        virtual ~NullStmt();
277
278        virtual NullStmt *clone() const { return new NullStmt( *this ); }
279        virtual void accept( Visitor &v ) { v.visit( this ); }
280        virtual NullStmt *acceptMutator( Mutator &m ) { return m.mutate( this ); }
281        virtual void print( std::ostream &os, int indent = 0 );
282       
283  private:
284};
285
286class TryStmt : public Statement { 
287  public:
288        TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<Statement *> &handlers, FinallyStmt *finallyBlock = 0 );
289        TryStmt( const TryStmt &other );
290        virtual ~TryStmt();
291
292        CompoundStmt *get_block() const { return block; }
293        void set_block( CompoundStmt *newValue ) { block = newValue; }
294        std::list<Statement *>& get_catchers() { return handlers; }
295
296        FinallyStmt *get_finally() const { return finallyBlock; }
297        void set_finally( FinallyStmt *newValue ) { finallyBlock = newValue; }
298
299        virtual TryStmt *clone() const { return new TryStmt( *this ); }
300        virtual void accept( Visitor &v ) { v.visit( this ); }
301        virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); }
302        virtual void print( std::ostream &os, int indent = 0 );
303       
304  private:
305        CompoundStmt *block;
306        std::list<Statement *> handlers;
307        FinallyStmt *finallyBlock;
308}; 
309
310class CatchStmt : public Statement {
311  public:
312        CatchStmt( std::list<Label> labels, Declaration *decl, Statement *body, bool isCatchRest = false );
313        virtual ~CatchStmt();
314
315        Declaration *get_decl() { return decl; }
316        void set_decl( Declaration *newValue ) { decl = newValue; }
317
318        Statement *get_body() { return body; }
319        void set_body( Statement *newValue ) { body = newValue; }
320       
321        virtual CatchStmt *clone() const { return new CatchStmt( *this ); }
322        virtual void accept( Visitor &v ) { v.visit( this ); }
323        virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); }
324        virtual void print( std::ostream &os, int indent = 0 );
325       
326  private:
327        Declaration *decl;
328        Statement *body;
329        bool catchRest;
330};
331
332class FinallyStmt : public Statement { 
333  public:
334        FinallyStmt( std::list<Label> labels, CompoundStmt *block );
335        virtual ~FinallyStmt();
336
337        CompoundStmt *get_block() const { return block; }
338        void set_block( CompoundStmt *newValue ) { block = newValue; }
339       
340        virtual FinallyStmt *clone() const { return new FinallyStmt( *this ); }
341        virtual void accept( Visitor &v ) { v.visit( this ); }
342        virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); }
343        virtual void print( std::ostream &os, int indent = 0 );
344  private:
345        CompoundStmt *block;
346}; 
347
348
349// represents a declaration that occurs as part of a compound statement
350class DeclStmt : public Statement {
351  public:
352        DeclStmt( std::list<Label> labels, Declaration *decl );
353        DeclStmt( const DeclStmt &other );
354        virtual ~DeclStmt();
355
356        Declaration *get_decl() { return decl; }
357        void set_decl( Declaration *newValue ) { decl = newValue; }
358
359        virtual DeclStmt *clone() const { return new DeclStmt( *this ); }
360        virtual void accept( Visitor &v ) { v.visit( this ); }
361        virtual Statement *acceptMutator( Mutator &m ) { return m.mutate( this ); }
362        virtual void print( std::ostream &os, int indent = 0 );
363  private:
364        Declaration *decl;
365};
366
367#endif // STATEMENT_H
368
369// Local Variables: //
370// tab-width: 4 //
371// mode: c++ //
372// compile-command: "make install" //
373// End: //
Note: See TracBrowser for help on using the repository browser.