source: src/AST/Stmt.hpp@ f90d10f

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since f90d10f was 37cdd97, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Added a ast node for suspend statements

  • 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
[37cdd97]344/// Suspend statement
345class SuspendStmt final : public Stmt {
346public:
347 ptr<CompoundStmt> then;
348 enum Type { None, Coroutine, Generator } type = None;
349
350 SuspendStmt( const CodeLocation & loc, const CompoundStmt * then, Type type, std::vector<Label> && labels = {} )
351 : Stmt(loc, std::move(labels)), then(then), type(type) {}
352
353 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
354private:
355 SuspendStmt * clone() const override { return new SuspendStmt{ *this }; }
356 MUTATE_FRIEND
357};
358
[0f740d6]359/// Wait for concurrency statement `when (...) waitfor (... , ...) ... timeout(...) ... else ...`
[1e97287]360class WaitForStmt final : public Stmt {
361public:
362 struct Target {
[e0016a5]363 ptr<Expr> func;
364 std::vector<ptr<Expr>> args;
[1e97287]365 };
366
367 struct Clause {
368 Target target;
369 ptr<Stmt> stmt;
370 ptr<Expr> cond;
371 };
372
373 struct Timeout {
374 ptr<Expr> time;
375 ptr<Stmt> stmt;
376 ptr<Expr> cond;
377 };
378
379 struct OrElse {
380 ptr<Stmt> stmt;
381 ptr<Expr> cond;
382 };
383
384 std::vector<Clause> clauses;
385 Timeout timeout;
386 OrElse orElse;
387
[f3cc5b6]388 WaitForStmt( const CodeLocation & loc, std::vector<Label> && labels = {} )
[1e97287]389 : Stmt(loc, std::move(labels)) {}
390
[f3cc5b6]391 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[1e97287]392private:
[f3cc5b6]393 WaitForStmt * clone() const override { return new WaitForStmt{ *this }; }
394 MUTATE_FRIEND
[1e97287]395};
396
[0f740d6]397/// Any declaration in a (compound) statement.
[1e97287]398class DeclStmt final : public Stmt {
399public:
400 ptr<Decl> decl;
401
[f3cc5b6]402 DeclStmt( const CodeLocation & loc, const Decl * decl, std::vector<Label> && labels = {} )
[1e97287]403 : Stmt(loc, std::move(labels)), decl(decl) {}
404
[f3cc5b6]405 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[1e97287]406private:
[f3cc5b6]407 DeclStmt * clone() const override { return new DeclStmt{ *this }; }
408 MUTATE_FRIEND
[1e97287]409};
410
[0f740d6]411/// Represents an implicit application of a constructor or destructor.
[1e97287]412class ImplicitCtorDtorStmt final : public Stmt {
413public:
414 readonly<Stmt> callStmt;
415
[f3cc5b6]416 ImplicitCtorDtorStmt( const CodeLocation & loc, const Stmt * callStmt,
417 std::vector<Label> && labels = {} )
[1e97287]418 : Stmt(loc, std::move(labels)), callStmt(callStmt) {}
419
[f3cc5b6]420 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[1e97287]421private:
[f3cc5b6]422 ImplicitCtorDtorStmt * clone() const override { return new ImplicitCtorDtorStmt{ *this }; }
423 MUTATE_FRIEND
[1e97287]424};
[2bb4a01]425
426}
427
[f3cc5b6]428#undef MUTATE_FRIEND
429
[2bb4a01]430// Local Variables: //
431// tab-width: 4 //
432// mode: c++ //
433// compile-command: "make install" //
[1e97287]434// End: //
Note: See TracBrowser for help on using the repository browser.