source: src/AST/Stmt.hpp@ 81da70a5

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 81da70a5 was 7030dab, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Merge branch 'master' into new-ast

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