source: src/SynTree/Statement.h @ ee3c93d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since ee3c93d was ee3c93d, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Add support for while loops with control declarations

  • Property mode set to 100644
File size: 17.8 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 : Peter A. Buhr
12// Last Modified On : Thu Mar  8 14:53:02 2018
13// Update Count     : 78
14//
15
16#pragma once
17
18#include <iosfwd>                  // for ostream
19#include <list>                    // for list
20#include <memory>                  // for allocator
21#include <vector>                        // for vector
22
23#include "BaseSyntaxNode.h"        // for BaseSyntaxNode
24#include "Common/SemanticError.h"  // for SemanticError
25#include "Label.h"                 // for Label
26#include "Mutator.h"               // for Mutator
27#include "Visitor.h"               // for Visitor
28
29class CatchStmt;
30class ConstantExpr;
31class Declaration;
32class Expression;
33class FinallyStmt;
34
35class Statement : public BaseSyntaxNode {
36  public:
37        std::list<Label> labels;
38
39        Statement( const std::list<Label> & labels = {} );
40        virtual ~Statement();
41
42        std::list<Label> & get_labels() { return labels; }
43        const std::list<Label> & get_labels() const { return labels; }
44
45        virtual Statement *clone() const override = 0;
46        virtual void accept( Visitor &v ) override = 0;
47        virtual Statement *acceptMutator( Mutator &m ) override = 0;
48        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
49};
50
51class CompoundStmt : public Statement {
52  public:
53        std::list<Statement*> kids;
54
55        CompoundStmt();
56        CompoundStmt( std::list<Statement *> stmts );
57        CompoundStmt( const CompoundStmt &other );
58        virtual ~CompoundStmt();
59
60        std::list<Statement*>& get_kids() { return kids; }
61        void push_back( Statement * stmt ) { kids.push_back( stmt ); }
62        void push_front( Statement * stmt ) { kids.push_front( stmt ); }
63
64        virtual CompoundStmt *clone() const override { return new CompoundStmt( *this ); }
65        virtual void accept( Visitor &v ) override { v.visit( this ); }
66        virtual CompoundStmt *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
67        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
68};
69
70class NullStmt : public Statement {
71  public:
72        NullStmt( const std::list<Label> & labels = {} );
73
74        virtual NullStmt *clone() const override { return new NullStmt( *this ); }
75        virtual void accept( Visitor &v ) override { v.visit( this ); }
76        virtual NullStmt *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
77        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
78};
79
80class ExprStmt : public Statement {
81  public:
82        Expression *expr;
83
84        ExprStmt( Expression *expr );
85        ExprStmt( const ExprStmt &other );
86        virtual ~ExprStmt();
87
88        Expression *get_expr() { return expr; }
89        void set_expr( Expression *newValue ) { expr = newValue; }
90
91        virtual ExprStmt *clone() const override { return new ExprStmt( *this ); }
92        virtual void accept( Visitor &v ) override { v.visit( this ); }
93        virtual Statement *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
94        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
95};
96
97class AsmStmt : public Statement {
98  public:
99        bool voltile;
100        Expression *instruction;
101        std::list<Expression *> output, input;
102        std::list<ConstantExpr *> clobber;
103        std::list<Label> gotolabels;
104
105        AsmStmt( bool voltile, Expression *instruction, std::list<Expression *> output, std::list<Expression *> input, std::list<ConstantExpr *> clobber, std::list<Label> gotolabels );
106        AsmStmt( const AsmStmt &other );
107        virtual ~AsmStmt();
108
109        bool get_voltile() { return voltile; }
110        void set_voltile( bool newValue ) { voltile = newValue; }
111        Expression * get_instruction() { return instruction; }
112        void set_instruction( Expression * newValue ) { instruction = newValue; }
113        std::list<Expression *> & get_output() { return output; }
114        void set_output( const std::list<Expression *> & newValue ) { output = newValue; }
115        std::list<Expression *> & get_input() { return input; }
116        void set_input( const std::list<Expression *> &newValue ) { input = newValue; }
117        std::list<ConstantExpr *> & get_clobber() { return clobber; }
118        void set_clobber( const std::list<ConstantExpr *> &newValue ) { clobber = newValue; }
119        std::list<Label> & get_gotolabels() { return gotolabels; }
120        void set_gotolabels( const std::list<Label> &newValue ) { gotolabels = newValue; }
121
122        virtual AsmStmt * clone() const { return new AsmStmt( *this ); }
123        virtual void accept( Visitor & v ) { v.visit( this ); }
124        virtual Statement * acceptMutator( Mutator & m ) { return m.mutate( this ); }
125        virtual void print( std::ostream & os, Indenter indent = {} ) const;
126};
127
128class DirectiveStmt : public Statement {
129        public:
130        std::string directive;
131
132        DirectiveStmt( const std::string & );
133        virtual ~DirectiveStmt(){}
134
135        virtual DirectiveStmt * clone() const { return new DirectiveStmt( *this ); }
136        virtual void accept( Visitor & v ) { v.visit( this ); }
137        virtual Statement * acceptMutator( Mutator & m ) { return m.mutate( this ); }
138        virtual void print( std::ostream & os, Indenter indent = {} ) const;
139};
140
141class IfStmt : public Statement {
142  public:
143        Expression *condition;
144        Statement *thenPart;
145        Statement *elsePart;
146        std::list<Statement *> initialization;
147
148        IfStmt( Expression *condition, Statement *thenPart, Statement *elsePart,
149                        std::list<Statement *> initialization = std::list<Statement *>() );
150        IfStmt( const IfStmt &other );
151        virtual ~IfStmt();
152
153        std::list<Statement *> &get_initialization() { return initialization; }
154        Expression *get_condition() { return condition; }
155        void set_condition( Expression *newValue ) { condition = newValue; }
156        Statement *get_thenPart() { return thenPart; }
157        void set_thenPart( Statement *newValue ) { thenPart = newValue; }
158        Statement *get_elsePart() { return elsePart; }
159        void set_elsePart( Statement *newValue ) { elsePart = newValue; }
160
161        virtual IfStmt *clone() const override { return new IfStmt( *this ); }
162        virtual void accept( Visitor &v ) override { v.visit( this ); }
163        virtual Statement *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
164        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
165};
166
167class SwitchStmt : public Statement {
168  public:
169        Expression * condition;
170        std::list<Statement *> statements;
171
172        SwitchStmt( Expression *condition, const std::list<Statement *> &statements );
173        SwitchStmt( const SwitchStmt &other );
174        virtual ~SwitchStmt();
175
176        Expression *get_condition() { return condition; }
177        void set_condition( Expression *newValue ) { condition = newValue; }
178
179        std::list<Statement *> & get_statements() { return statements; }
180
181        virtual void accept( Visitor &v ) override { v.visit( this ); }
182        virtual Statement *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
183
184        virtual SwitchStmt *clone() const override { return new SwitchStmt( *this ); }
185        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
186
187};
188
189class CaseStmt : public Statement {
190  public:
191        Expression * condition;
192        std::list<Statement *> stmts;
193
194        CaseStmt( Expression *conditions, const std::list<Statement *> &stmts, bool isdef = false ) throw (SemanticErrorException);
195        CaseStmt( const CaseStmt &other );
196        virtual ~CaseStmt();
197
198        static CaseStmt * makeDefault( const std::list<Label> & labels = {}, std::list<Statement *> stmts = std::list<Statement *>() );
199
200        bool isDefault() const { return _isDefault; }
201        void set_default(bool b) { _isDefault = b; }
202
203        Expression * &get_condition() { return condition; }
204        void set_condition( Expression *newValue ) { condition = newValue; }
205
206        std::list<Statement *> &get_statements() { return stmts; }
207        void set_statements( std::list<Statement *> &newValue ) { stmts = newValue; }
208
209        virtual void accept( Visitor &v ) override { v.visit( this ); }
210        virtual Statement *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
211
212        virtual CaseStmt *clone() const override { return new CaseStmt( *this ); }
213        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
214  private:
215        bool _isDefault;
216};
217
218class WhileStmt : public Statement {
219  public:
220        Expression *condition;
221        Statement *body;
222        std::list<Statement *> initialization;
223        bool isDoWhile;
224
225        WhileStmt( Expression *condition,
226               Statement *body, std::list<Statement *> & initialization, bool isDoWhile = false );
227        WhileStmt( const WhileStmt &other );
228        virtual ~WhileStmt();
229
230        Expression *get_condition() { return condition; }
231        void set_condition( Expression *newValue ) { condition = newValue; }
232        Statement *get_body() { return body; }
233        void set_body( Statement *newValue ) { body = newValue; }
234        bool get_isDoWhile() { return isDoWhile; }
235        void set_isDoWhile( bool newValue ) { isDoWhile = newValue; }
236
237        virtual WhileStmt *clone() const override { return new WhileStmt( *this ); }
238        virtual void accept( Visitor &v ) override { v.visit( this ); }
239        virtual Statement *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
240        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
241};
242
243class ForStmt : public Statement {
244  public:
245        std::list<Statement *> initialization;
246        Expression *condition;
247        Expression *increment;
248        Statement *body;
249
250        ForStmt( std::list<Statement *> initialization,
251             Expression *condition = 0, Expression *increment = 0, Statement *body = 0 );
252        ForStmt( const ForStmt &other );
253        virtual ~ForStmt();
254
255        std::list<Statement *> &get_initialization() { return initialization; }
256        Expression *get_condition() { return condition; }
257        void set_condition( Expression *newValue ) { condition = newValue; }
258        Expression *get_increment() { return increment; }
259        void set_increment( Expression *newValue ) { increment = newValue; }
260        Statement *get_body() { return body; }
261        void set_body( Statement *newValue ) { body = newValue; }
262
263        virtual ForStmt *clone() const override { return new ForStmt( *this ); }
264        virtual void accept( Visitor &v ) override { v.visit( this ); }
265        virtual Statement *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
266        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
267};
268
269class BranchStmt : public Statement {
270  public:
271        enum Type { Goto = 0, Break, Continue, FallThrough, FallThroughDefault };
272
273        // originalTarget kept for error messages.
274        const Label originalTarget;
275        Label target;
276        Expression *computedTarget;
277        Type type;
278
279        BranchStmt( Label target, Type ) throw (SemanticErrorException);
280        BranchStmt( Expression *computedTarget, Type ) throw (SemanticErrorException);
281
282        Label get_originalTarget() { return originalTarget; }
283        Label get_target() { return target; }
284        void set_target( Label newValue ) { target = newValue; }
285
286        Expression *get_computedTarget() { return computedTarget; }
287        void set_target( Expression * newValue ) { computedTarget = newValue; }
288
289        Type get_type() { return type; }
290        const char *get_typename() { return brType[ type ]; }
291
292        virtual BranchStmt *clone() const override { return new BranchStmt( *this ); }
293        virtual void accept( Visitor &v ) override { v.visit( this ); }
294        virtual Statement *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
295        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
296  private:
297        static const char *brType[];
298};
299
300class ReturnStmt : public Statement {
301  public:
302        Expression *expr;
303
304        ReturnStmt( Expression *expr );
305        ReturnStmt( const ReturnStmt &other );
306        virtual ~ReturnStmt();
307
308        Expression *get_expr() { return expr; }
309        void set_expr( Expression *newValue ) { expr = newValue; }
310
311        virtual ReturnStmt *clone() const override { return new ReturnStmt( *this ); }
312        virtual void accept( Visitor &v ) override { v.visit( this ); }
313        virtual Statement *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
314        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
315};
316
317class ThrowStmt : public Statement {
318  public:
319        enum Kind { Terminate, Resume };
320
321        const Kind kind;
322        Expression * expr;
323        Expression * target;
324
325        ThrowStmt( Kind kind, Expression * expr, Expression * target = nullptr );
326        ThrowStmt( const ThrowStmt &other );
327        virtual ~ThrowStmt();
328
329        Kind get_kind() { return kind; }
330        Expression * get_expr() { return expr; }
331        void set_expr( Expression * newExpr ) { expr = newExpr; }
332        Expression * get_target() { return target; }
333        void set_target( Expression * newTarget ) { target = newTarget; }
334
335        virtual ThrowStmt *clone() const override { return new ThrowStmt( *this ); }
336        virtual void accept( Visitor &v ) override { v.visit( this ); }
337        virtual Statement *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
338        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
339};
340
341class TryStmt : public Statement {
342  public:
343        CompoundStmt * block;
344        std::list<CatchStmt *> handlers;
345        FinallyStmt * finallyBlock;
346
347        TryStmt( CompoundStmt *tryBlock, std::list<CatchStmt *> &handlers, FinallyStmt *finallyBlock = 0 );
348        TryStmt( const TryStmt &other );
349        virtual ~TryStmt();
350
351        CompoundStmt *get_block() const { return block; }
352        void set_block( CompoundStmt *newValue ) { block = newValue; }
353        std::list<CatchStmt *>& get_catchers() { return handlers; }
354
355        FinallyStmt *get_finally() const { return finallyBlock; }
356        void set_finally( FinallyStmt *newValue ) { finallyBlock = newValue; }
357
358        virtual TryStmt *clone() const override { return new TryStmt( *this ); }
359        virtual void accept( Visitor &v ) override { v.visit( this ); }
360        virtual Statement *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
361        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
362};
363
364class CatchStmt : public Statement {
365  public:
366        enum Kind { Terminate, Resume };
367
368        const Kind kind;
369        Declaration *decl;
370        Expression *cond;
371        Statement *body;
372
373        CatchStmt( Kind kind, Declaration *decl,
374                   Expression *cond, Statement *body );
375        CatchStmt( const CatchStmt &other );
376        virtual ~CatchStmt();
377
378        Kind get_kind() { return kind; }
379        Declaration *get_decl() { return decl; }
380        void set_decl( Declaration *newValue ) { decl = newValue; }
381        Expression *get_cond() { return cond; }
382        void set_cond( Expression *newCond ) { cond = newCond; }
383        Statement *get_body() { return body; }
384        void set_body( Statement *newValue ) { body = newValue; }
385
386        virtual CatchStmt *clone() const override { return new CatchStmt( *this ); }
387        virtual void accept( Visitor &v ) override { v.visit( this ); }
388        virtual Statement *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
389        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
390};
391
392class FinallyStmt : public Statement {
393  public:
394        CompoundStmt *block;
395
396        FinallyStmt( CompoundStmt *block );
397        FinallyStmt( const FinallyStmt &other );
398        virtual ~FinallyStmt();
399
400        CompoundStmt *get_block() const { return block; }
401        void set_block( CompoundStmt *newValue ) { block = newValue; }
402
403        virtual FinallyStmt *clone() const override { return new FinallyStmt( *this ); }
404        virtual void accept( Visitor &v ) override { v.visit( this ); }
405        virtual Statement *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
406        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
407};
408
409class WaitForStmt : public Statement {
410  public:
411
412        struct Target {
413                Expression * function;
414                std::list<Expression * > arguments;
415        };
416
417        struct Clause {
418                Target       target;
419                Statement  * statement;
420                Expression * condition;
421        };
422
423        WaitForStmt();
424        WaitForStmt( const WaitForStmt & );
425        virtual ~WaitForStmt();
426
427        std::vector<Clause> clauses;
428
429        struct {
430                Expression * time;
431                Statement  * statement;
432                Expression * condition;
433        } timeout;
434
435        struct {
436                Statement  * statement;
437                Expression * condition;
438        } orelse;
439
440        virtual WaitForStmt *clone() const override { return new WaitForStmt( *this ); }
441        virtual void accept( Visitor &v ) override { v.visit( this ); }
442        virtual Statement *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
443        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
444
445};
446
447class WithStmt : public Statement {
448public:
449        std::list< Expression * > exprs;
450        Statement * stmt;
451
452        WithStmt( const std::list< Expression * > & exprs, Statement * stmt );
453        WithStmt( const WithStmt & other );
454        virtual ~WithStmt();
455
456        virtual WithStmt * clone() const override { return new WithStmt( *this ); }
457        virtual void accept( Visitor & v ) override { v.visit( this ); }
458        virtual Statement * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
459        virtual void print( std::ostream & os, Indenter indent = {} ) const override;
460};
461
462
463// represents a declaration that occurs as part of a compound statement
464class DeclStmt : public Statement {
465  public:
466        Declaration *decl;
467
468        DeclStmt( Declaration *decl );
469        DeclStmt( const DeclStmt &other );
470        virtual ~DeclStmt();
471
472        Declaration *get_decl() const { return decl; }
473        void set_decl( Declaration *newValue ) { decl = newValue; }
474
475        virtual DeclStmt *clone() const override { return new DeclStmt( *this ); }
476        virtual void accept( Visitor &v ) override { v.visit( this ); }
477        virtual Statement *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
478        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
479};
480
481
482/// represents an implicit application of a constructor or destructor. Qualifiers are replaced
483/// immediately before and after the call so that qualified objects can be constructed
484/// with the same functions as unqualified objects.
485class ImplicitCtorDtorStmt : public Statement {
486  public:
487        // Non-owned pointer to the constructor/destructor statement
488        Statement * callStmt;
489
490        ImplicitCtorDtorStmt( Statement * callStmt );
491        ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other );
492        virtual ~ImplicitCtorDtorStmt();
493
494        Statement *get_callStmt() const { return callStmt; }
495        void set_callStmt( Statement * newValue ) { callStmt = newValue; }
496
497        virtual ImplicitCtorDtorStmt *clone() const override { return new ImplicitCtorDtorStmt( *this ); }
498        virtual void accept( Visitor &v ) override { v.visit( this ); }
499        virtual Statement *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
500        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
501};
502
503// Local Variables: //
504// tab-width: 4 //
505// mode: c++ //
506// compile-command: "make install" //
507// End: //
Note: See TracBrowser for help on using the repository browser.