source: src/SynTree/Statement.h @ 994ec2c

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 994ec2c was 145f1fc, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

modified ForStmt? to have a list of statements for the initialization portion, and reverted to hoisting the initialization

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