source: src/AST/Stmt.hpp @ becb85b9

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since becb85b9 was 3b0bc16, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

change class name WhileStmt? to WhileDoStmt?, add else clause to WhileDoStmt? and ForStmt?, change names thenPart/ElsePart to then/else_

  • Property mode set to 100644
File size: 14.1 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// Stmt.hpp --
8//
9// Author           : Aaron B. Moss
10// Created On       : Wed May  8 13:00:00 2019
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Feb  1 17:44:46 2022
13// Update Count     : 24
14//
15
16#pragma once
17
18#include <list>
19#include <utility>                                                                              // for move
20#include <vector>
21
22#include "Label.hpp"
23#include "Node.hpp"                                                                             // for node, ptr
24#include "ParseNode.hpp"
25#include "Visitor.hpp"
26#include "Common/CodeLocation.h"
27
28// Must be included in *all* AST classes; should be #undef'd at the end of the file
29#define MUTATE_FRIEND                                                                                                   \
30    template<typename node_t> friend node_t * mutate(const node_t * node); \
31        template<typename node_t> friend node_t * shallowCopy(const node_t * node);
32
33namespace ast {
34class Expr;
35
36// Base statement node
37class Stmt : public ParseNode {
38  public:
39        std::vector<Label> labels;
40
41        Stmt( const CodeLocation & loc, std::vector<Label> && labels = {} )
42                : ParseNode(loc), labels(std::move(labels)) {}
43
44        Stmt(const Stmt & o) : ParseNode(o), labels(o.labels) {}
45
46        const Stmt * accept( Visitor & v ) const override = 0;
47  private:
48        Stmt * clone() const override = 0;
49        MUTATE_FRIEND
50};
51
52// Compound statement: { ... }
53class CompoundStmt final : public Stmt {
54  public:
55        std::list<ptr<Stmt>> kids;
56
57        CompoundStmt(const CodeLocation & loc, std::list<ptr<Stmt>> && ks = {},
58                                 std::vector<Label> && labels = {} )
59                : Stmt(loc, std::move(labels)), kids(std::move(ks)) {}
60
61        CompoundStmt( const CompoundStmt & o );
62        CompoundStmt( CompoundStmt && o ) = default;
63
64        void push_back( const Stmt * s ) { kids.emplace_back( s ); }
65        void push_front( const Stmt * s ) { kids.emplace_front( s ); }
66
67        const CompoundStmt * accept( Visitor & v ) const override { return v.visit( this ); }
68  private:
69        CompoundStmt * clone() const override { return new CompoundStmt{ *this }; }
70        MUTATE_FRIEND
71};
72
73// Empty statment: ;
74class NullStmt final : public Stmt {
75  public:
76        NullStmt( const CodeLocation & loc, std::vector<Label> && labels = {} )
77                : Stmt(loc, std::move(labels)) {}
78
79        const NullStmt * accept( Visitor & v ) const override { return v.visit( this ); }
80  private:
81        NullStmt * clone() const override { return new NullStmt{ *this }; }
82        MUTATE_FRIEND
83};
84
85// Expression wrapped by statement
86class ExprStmt final : public Stmt {
87  public:
88        ptr<Expr> expr;
89
90        ExprStmt( const CodeLocation & loc, const Expr* e, std::vector<Label> && labels = {} )
91                : Stmt(loc, std::move(labels)), expr(e) {}
92
93        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
94  private:
95        ExprStmt * clone() const override { return new ExprStmt{ *this }; }
96        MUTATE_FRIEND
97};
98
99// Assembly statement: asm ... ( "..." : ... )
100class AsmStmt final : public Stmt {
101  public:
102        bool isVolatile;
103        ptr<Expr> instruction;
104        std::vector<ptr<Expr>> output, input;
105        std::vector<ptr<ConstantExpr>> clobber;
106        std::vector<Label> gotoLabels;
107
108        AsmStmt( const CodeLocation & loc, bool isVolatile, const Expr * instruction,
109                         std::vector<ptr<Expr>> && output, std::vector<ptr<Expr>> && input,
110                         std::vector<ptr<ConstantExpr>> && clobber, std::vector<Label> && gotoLabels,
111                         std::vector<Label> && labels = {})
112                : Stmt(loc, std::move(labels)), isVolatile(isVolatile), instruction(instruction),
113                  output(std::move(output)), input(std::move(input)), clobber(std::move(clobber)),
114                  gotoLabels(std::move(gotoLabels)) {}
115
116        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
117  private:
118        AsmStmt * clone() const override { return new AsmStmt{ *this }; }
119        MUTATE_FRIEND
120};
121
122// C-preprocessor directive: #...
123class DirectiveStmt final : public Stmt {
124  public:
125        std::string directive;
126
127        DirectiveStmt( const CodeLocation & loc, const std::string & directive,
128                                   std::vector<Label> && labels = {} )
129                : Stmt(loc, std::move(labels)), directive(directive) {}
130
131        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
132  private:
133        DirectiveStmt * clone() const override { return new DirectiveStmt{ *this }; }
134        MUTATE_FRIEND
135};
136
137// If statement: if (...) ... else ...
138class IfStmt final : public Stmt {
139  public:
140        ptr<Expr> cond;
141        ptr<Stmt> then;
142        ptr<Stmt> else_;
143        std::vector<ptr<Stmt>> inits;
144
145        IfStmt( const CodeLocation & loc, const Expr * cond, const Stmt * then,
146                        const Stmt * else_ = nullptr, std::vector<ptr<Stmt>> && inits = {},
147                        std::vector<Label> && labels = {} )
148                : Stmt(loc, std::move(labels)), cond(cond), then(then), else_(else_),
149                  inits(std::move(inits)) {}
150
151        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
152  private:
153        IfStmt * clone() const override { return new IfStmt{ *this }; }
154        MUTATE_FRIEND
155};
156
157// Switch or choose statement: switch (...) { ... }
158class SwitchStmt final : public Stmt {
159  public:
160        ptr<Expr> cond;
161        std::vector<ptr<Stmt>> stmts;
162
163        SwitchStmt( const CodeLocation & loc, const Expr * cond, std::vector<ptr<Stmt>> && stmts,
164                                std::vector<Label> && labels = {} )
165                : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
166
167        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
168  private:
169        SwitchStmt * clone() const override { return new SwitchStmt{ *this }; }
170        MUTATE_FRIEND
171};
172
173// Case label: case ...: or default:
174class CaseStmt final : public Stmt {
175  public:
176        // Null for the default label.
177        ptr<Expr> cond;
178        std::vector<ptr<Stmt>> stmts;
179
180        CaseStmt( const CodeLocation & loc, const Expr * cond, std::vector<ptr<Stmt>> && stmts,
181                          std::vector<Label> && labels = {} )
182                : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
183
184        bool isDefault() const { return !cond; }
185
186        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
187  private:
188        CaseStmt * clone() const override { return new CaseStmt{ *this }; }
189        MUTATE_FRIEND
190};
191
192// While loop: while (...) ... else ... or do ... while (...) else ...;
193class WhileDoStmt final : public Stmt {
194  public:
195        ptr<Expr> cond;
196        ptr<Stmt> body;
197        ptr<Stmt> else_;
198        std::vector<ptr<Stmt>> inits;
199        bool isDoWhile;
200
201        WhileDoStmt( const CodeLocation & loc, const Expr * cond, const Stmt * body,
202                           std::vector<ptr<Stmt>> && inits, bool isDoWhile = false, std::vector<Label> && labels = {} )
203                : Stmt(loc, std::move(labels)), cond(cond), body(body), else_(nullptr), inits(std::move(inits)), isDoWhile(isDoWhile) {}
204
205        WhileDoStmt( const CodeLocation & loc, const Expr * cond, const Stmt * body, const Stmt * else_,
206                           std::vector<ptr<Stmt>> && inits, bool isDoWhile = false, std::vector<Label> && labels = {} )
207                : Stmt(loc, std::move(labels)), cond(cond), body(body), else_(else_), inits(std::move(inits)), isDoWhile(isDoWhile) {}
208
209        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
210  private:
211        WhileDoStmt * clone() const override { return new WhileDoStmt{ *this }; }
212        MUTATE_FRIEND
213};
214
215// For loop: for (... ; ... ; ...) ... else ...
216class ForStmt final : public Stmt {
217  public:
218        std::vector<ptr<Stmt>> inits;
219        ptr<Expr> cond;
220        ptr<Expr> inc;
221        ptr<Stmt> body;
222        ptr<Stmt> else_;
223
224        ForStmt( const CodeLocation & loc, std::vector<ptr<Stmt>> && inits, const Expr * cond,
225                         const Expr * inc, const Stmt * body, std::vector<Label> && label = {} )
226                : Stmt(loc, std::move(label)), inits(std::move(inits)), cond(cond), inc(inc), body(body), else_(nullptr) {}
227
228        ForStmt( const CodeLocation & loc, std::vector<ptr<Stmt>> && inits, const Expr * cond,
229                         const Expr * inc, const Stmt * body, const Stmt * else_, std::vector<Label> && labels = {} )
230                : Stmt(loc, std::move(labels)), inits(std::move(inits)), cond(cond), inc(inc), body(body), else_(else_) {}
231
232        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
233  private:
234        ForStmt * clone() const override { return new ForStmt{ *this }; }
235        MUTATE_FRIEND
236};
237
238// Branch control flow statement: goto ... or break or continue or fallthru
239class BranchStmt final : public Stmt {
240  public:
241        enum Kind { Goto, Break, Continue, FallThrough, FallThroughDefault };
242        static constexpr size_t kindEnd = 1 + (size_t)FallThroughDefault;
243
244        const Label originalTarget;
245        Label target;
246        ptr<Expr> computedTarget;
247        Kind kind;
248
249        BranchStmt( const CodeLocation & loc, Kind kind, Label target,
250                                std::vector<Label> && labels = {} );
251        BranchStmt( const CodeLocation & loc, const Expr * computedTarget,
252                                std::vector<Label> && labels = {} )
253                : Stmt(loc, std::move(labels)), originalTarget(loc), target(loc),
254                  computedTarget(computedTarget), kind(Goto) {}
255
256        const char * kindName() const { return kindNames[kind]; }
257
258        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
259  private:
260        BranchStmt * clone() const override { return new BranchStmt{ *this }; }
261        MUTATE_FRIEND
262
263        static const char * kindNames[kindEnd];
264};
265
266// Return statement: return ...
267class ReturnStmt final : public Stmt {
268  public:
269        ptr<Expr> expr;
270
271        ReturnStmt( const CodeLocation & loc, const Expr * expr, std::vector<Label> && labels = {} )
272                : Stmt(loc, std::move(labels)), expr(expr) {}
273
274        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
275  private:
276        ReturnStmt * clone() const override { return new ReturnStmt{ *this }; }
277        MUTATE_FRIEND
278};
279
280// Kind of exception
281enum ExceptionKind { Terminate, Resume };
282
283// Throw statement: throw ...
284class ThrowStmt final : public Stmt {
285  public:
286        ptr<Expr> expr;
287        ptr<Expr> target;
288        ExceptionKind kind;
289
290        ThrowStmt(
291                const CodeLocation & loc, ExceptionKind kind, const Expr * expr, const Expr * target,
292                std::vector<Label> && labels = {} )
293                : Stmt(loc, std::move(labels)), expr(expr), target(target), kind(kind) {}
294
295        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
296  private:
297        ThrowStmt * clone() const override { return new ThrowStmt{ *this }; }
298        MUTATE_FRIEND
299};
300
301// Try statement: try { ... } ...
302class TryStmt final : public Stmt {
303  public:
304        ptr<CompoundStmt> body;
305        std::vector<ptr<CatchStmt>> handlers;
306        ptr<FinallyStmt> finally;
307
308        TryStmt(
309                const CodeLocation & loc, const CompoundStmt * body,
310                std::vector<ptr<CatchStmt>> && handlers, const FinallyStmt * finally,
311                std::vector<Label> && labels = {} )
312                : Stmt(loc, std::move(labels)), body(body), handlers(std::move(handlers)), finally(finally) {}
313
314        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
315  private:
316        TryStmt * clone() const override { return new TryStmt{ *this }; }
317        MUTATE_FRIEND
318};
319
320// Catch clause of try statement
321class CatchStmt final : public Stmt {
322  public:
323        ptr<Decl> decl;
324        ptr<Expr> cond;
325        ptr<Stmt> body;
326        ExceptionKind kind;
327
328        CatchStmt(
329                const CodeLocation & loc, ExceptionKind kind, const Decl * decl, const Expr * cond,
330                const Stmt * body, std::vector<Label> && labels = {} )
331                : Stmt(loc, std::move(labels)), decl(decl), cond(cond), body(body), kind(kind) {}
332
333        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
334  private:
335        CatchStmt * clone() const override { return new CatchStmt{ *this }; }
336        MUTATE_FRIEND
337};
338
339// Finally clause of try statement
340class FinallyStmt final : public Stmt {
341  public:
342        ptr<CompoundStmt> body;
343
344        FinallyStmt( const CodeLocation & loc, const CompoundStmt * body,
345                                 std::vector<Label> && labels = {} )
346                : Stmt(loc, std::move(labels)), body(body) {}
347
348        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
349  private:
350        FinallyStmt * clone() const override { return new FinallyStmt{ *this }; }
351        MUTATE_FRIEND
352};
353
354// Suspend statement
355class SuspendStmt final : public Stmt {
356  public:
357        ptr<CompoundStmt> then;
358        enum Type { None, Coroutine, Generator } type = None;
359
360        SuspendStmt( const CodeLocation & loc, const CompoundStmt * then, Type type, std::vector<Label> && labels = {} )
361                : Stmt(loc, std::move(labels)), then(then), type(type) {}
362
363        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
364  private:
365        SuspendStmt * clone() const override { return new SuspendStmt{ *this }; }
366        MUTATE_FRIEND
367};
368
369// Waitfor statement: when (...) waitfor (... , ...) ... timeout(...) ... else ...
370class WaitForStmt final : public Stmt {
371  public:
372        struct Target {
373                ptr<Expr> func;
374                std::vector<ptr<Expr>> args;
375        };
376
377        struct Clause {
378                Target target;
379                ptr<Stmt> stmt;
380                ptr<Expr> cond;
381        };
382
383        struct Timeout {
384                ptr<Expr> time;
385                ptr<Stmt> stmt;
386                ptr<Expr> cond;
387        };
388
389        struct OrElse {
390                ptr<Stmt> stmt;
391                ptr<Expr> cond;
392        };
393
394        std::vector<Clause> clauses;
395        Timeout timeout;
396        OrElse orElse;
397
398        WaitForStmt( const CodeLocation & loc, std::vector<Label> && labels = {} )
399                : Stmt(loc, std::move(labels)) {}
400
401        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
402  private:
403        WaitForStmt * clone() const override { return new WaitForStmt{ *this }; }
404        MUTATE_FRIEND
405};
406
407// Any declaration in a (compound) statement.
408class DeclStmt final : public Stmt {
409  public:
410        ptr<Decl> decl;
411
412        DeclStmt( const CodeLocation & loc, const Decl * decl, std::vector<Label> && labels = {} )
413                : Stmt(loc, std::move(labels)), decl(decl) {}
414
415        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
416  private:
417        DeclStmt * clone() const override { return new DeclStmt{ *this }; }
418        MUTATE_FRIEND
419};
420
421// Represents an implicit application of a constructor or destructor.
422class ImplicitCtorDtorStmt final : public Stmt {
423  public:
424        ptr<Stmt> callStmt;
425
426        ImplicitCtorDtorStmt( const CodeLocation & loc, const Stmt * callStmt,
427                                                  std::vector<Label> && labels = {} )
428                : Stmt(loc, std::move(labels)), callStmt(callStmt) {}
429
430        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
431  private:
432        ImplicitCtorDtorStmt * clone() const override { return new ImplicitCtorDtorStmt{ *this }; }
433        MUTATE_FRIEND
434};
435
436// Mutex Statement
437class MutexStmt final : public Stmt {
438  public:
439        ptr<Stmt> stmt;
440        std::vector<ptr<Expr>> mutexObjs;
441
442        MutexStmt( const CodeLocation & loc, const Stmt * stmt, 
443                           std::vector<ptr<Expr>> && mutexes, std::vector<Label> && labels = {} )
444                : Stmt(loc, std::move(labels)), stmt(stmt), mutexObjs(std::move(mutexes)) {}
445
446        const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
447  private:
448        MutexStmt * clone() const override { return new MutexStmt{ *this }; }
449        MUTATE_FRIEND
450};
451} // namespace ast
452
453#undef MUTATE_FRIEND
454
455// Local Variables: //
456// mode: c++ //
457// End: //
Note: See TracBrowser for help on using the repository browser.