source: src/AST/Stmt.hpp @ c1ed2ee

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since c1ed2ee was c1ed2ee, checked in by Aaron Moss <a3moss@…>, 5 years ago

Continued resolver porting

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