source: src/AST/Stmt.hpp @ 36a05d7

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 36a05d7 was b8ab91a, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Fix Labels pass translated. This is fix label, mult-level exit and label generator.

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