source: src/AST/Stmt.hpp@ 3cc1111

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since 3cc1111 was b8ab91a, checked in by Andrew Beach <ajbeach@…>, 4 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
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:
[b8ab91a]177 /// Null for the default label.
[1e97287]178 ptr<Expr> cond;
179 std::vector<ptr<Stmt>> stmts;
180
[f3cc5b6]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)) {}
[1e97287]184
[675d816]185 bool isDefault() const { return !cond; }
[1e97287]186
[f3cc5b6]187 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[1e97287]188private:
[f3cc5b6]189 CaseStmt * clone() const override { return new CaseStmt{ *this }; }
190 MUTATE_FRIEND
[1e97287]191};
192
[0f740d6]193/// While loop `while (...) ...` `do ... while (...);
[1e97287]194class WhileStmt final : public Stmt {
195public:
196 ptr<Expr> cond;
197 ptr<Stmt> body;
198 std::vector<ptr<Stmt>> inits;
199 bool isDoWhile;
200
[f3cc5b6]201 WhileStmt( const CodeLocation & loc, const Expr * cond, const Stmt * body,
202 std::vector<ptr<Stmt>> && inits, bool isDoWhile = false, std::vector<Label> && labels = {} )
[1e97287]203 : Stmt(loc, std::move(labels)), cond(cond), body(body), inits(std::move(inits)),
204 isDoWhile(isDoWhile) {}
205
[f3cc5b6]206 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[1e97287]207private:
[f3cc5b6]208 WhileStmt * clone() const override { return new WhileStmt{ *this }; }
209 MUTATE_FRIEND
[1e97287]210};
211
[0f740d6]212/// For loop `for (... ; ... ; ...) ...`
[1e97287]213class ForStmt final : public Stmt {
214public:
215 std::vector<ptr<Stmt>> inits;
216 ptr<Expr> cond;
[8a5530c]217 ptr<Expr> inc;
[1e97287]218 ptr<Stmt> body;
219
[f3cc5b6]220 ForStmt( const CodeLocation & loc, std::vector<ptr<Stmt>> && inits, const Expr * cond,
221 const Expr * inc, const Stmt * body, std::vector<Label> && labels = {} )
[8a5530c]222 : Stmt(loc, std::move(labels)), inits(std::move(inits)), cond(cond), inc(inc),
[1e97287]223 body(body) {}
224
[f3cc5b6]225 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[1e97287]226private:
[f3cc5b6]227 ForStmt * clone() const override { return new ForStmt{ *this }; }
228 MUTATE_FRIEND
[1e97287]229};
230
[0f740d6]231/// Branch control flow statement `goto ...` `break` `continue` `fallthru`
[1e97287]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
[f3cc5b6]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 = {} )
[1e97287]246 : Stmt(loc, std::move(labels)), originalTarget(loc), target(loc),
247 computedTarget(computedTarget), kind(Goto) {}
248
[94b1f718]249 const char * kindName() const { return kindNames[kind]; }
[1e97287]250
[f3cc5b6]251 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[1e97287]252private:
[f3cc5b6]253 BranchStmt * clone() const override { return new BranchStmt{ *this }; }
254 MUTATE_FRIEND
[8a5530c]255
[1e97287]256 static const char * kindNames[kindEnd];
257};
258
[0f740d6]259/// Return statement `return ...`
[1e97287]260class ReturnStmt final : public Stmt {
261public:
262 ptr<Expr> expr;
263
[f3cc5b6]264 ReturnStmt( const CodeLocation & loc, const Expr * expr, std::vector<Label> && labels = {} )
[1e97287]265 : Stmt(loc, std::move(labels)), expr(expr) {}
266
[f3cc5b6]267 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[1e97287]268private:
[f3cc5b6]269 ReturnStmt * clone() const override { return new ReturnStmt{ *this }; }
270 MUTATE_FRIEND
[1e97287]271};
272
[6f4b7f2]273/// Kind of exception
274enum ExceptionKind { Terminate, Resume };
275
[0f740d6]276/// Throw statement `throw ...`
[1e97287]277class ThrowStmt final : public Stmt {
278public:
279 ptr<Expr> expr;
280 ptr<Expr> target;
[6f4b7f2]281 ExceptionKind kind;
[1e97287]282
[a7d50b6]283 ThrowStmt(
[6f4b7f2]284 const CodeLocation & loc, ExceptionKind kind, const Expr * expr, const Expr * target,
[f3cc5b6]285 std::vector<Label> && labels = {} )
[1e97287]286 : Stmt(loc, std::move(labels)), expr(expr), target(target), kind(kind) {}
287
[f3cc5b6]288 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[1e97287]289private:
[f3cc5b6]290 ThrowStmt * clone() const override { return new ThrowStmt{ *this }; }
291 MUTATE_FRIEND
[1e97287]292};
293
[0f740d6]294/// Try statement `try { ... } ...`
[1e97287]295class TryStmt final : public Stmt {
296public:
297 ptr<CompoundStmt> body;
298 std::vector<ptr<CatchStmt>> handlers;
299 ptr<FinallyStmt> finally;
300
[a7d50b6]301 TryStmt(
[6f4b7f2]302 const CodeLocation & loc, const CompoundStmt * body,
[f3cc5b6]303 std::vector<ptr<CatchStmt>> && handlers, const FinallyStmt * finally,
304 std::vector<Label> && labels = {} )
[1e97287]305 : Stmt(loc, std::move(labels)), body(body), handlers(std::move(handlers)), finally(finally) {}
306
[f3cc5b6]307 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[1e97287]308private:
[f3cc5b6]309 TryStmt * clone() const override { return new TryStmt{ *this }; }
310 MUTATE_FRIEND
[1e97287]311};
312
[0f740d6]313/// Catch clause of try statement
[1e97287]314class CatchStmt final : public Stmt {
315public:
316 ptr<Decl> decl;
317 ptr<Expr> cond;
318 ptr<Stmt> body;
[6f4b7f2]319 ExceptionKind kind;
[1e97287]320
[a7d50b6]321 CatchStmt(
[6f4b7f2]322 const CodeLocation & loc, ExceptionKind kind, const Decl * decl, const Expr * cond,
[f3cc5b6]323 const Stmt * body, std::vector<Label> && labels = {} )
[1e97287]324 : Stmt(loc, std::move(labels)), decl(decl), cond(cond), body(body), kind(kind) {}
325
[f3cc5b6]326 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[1e97287]327private:
[f3cc5b6]328 CatchStmt * clone() const override { return new CatchStmt{ *this }; }
329 MUTATE_FRIEND
[1e97287]330};
331
[0f740d6]332/// Finally clause of try statement
[1e97287]333class FinallyStmt final : public Stmt {
334public:
335 ptr<CompoundStmt> body;
336
[f3cc5b6]337 FinallyStmt( const CodeLocation & loc, const CompoundStmt * body,
338 std::vector<Label> && labels = {} )
[1e97287]339 : Stmt(loc, std::move(labels)), body(body) {}
340
[f3cc5b6]341 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[1e97287]342private:
[f3cc5b6]343 FinallyStmt * clone() const override { return new FinallyStmt{ *this }; }
344 MUTATE_FRIEND
[1e97287]345};
[2bb4a01]346
[37cdd97]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
[0f740d6]362/// Wait for concurrency statement `when (...) waitfor (... , ...) ... timeout(...) ... else ...`
[1e97287]363class WaitForStmt final : public Stmt {
364public:
365 struct Target {
[e0016a5]366 ptr<Expr> func;
367 std::vector<ptr<Expr>> args;
[1e97287]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
[f3cc5b6]391 WaitForStmt( const CodeLocation & loc, std::vector<Label> && labels = {} )
[1e97287]392 : Stmt(loc, std::move(labels)) {}
393
[f3cc5b6]394 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[1e97287]395private:
[f3cc5b6]396 WaitForStmt * clone() const override { return new WaitForStmt{ *this }; }
397 MUTATE_FRIEND
[1e97287]398};
399
[0f740d6]400/// Any declaration in a (compound) statement.
[1e97287]401class DeclStmt final : public Stmt {
402public:
403 ptr<Decl> decl;
404
[f3cc5b6]405 DeclStmt( const CodeLocation & loc, const Decl * decl, std::vector<Label> && labels = {} )
[1e97287]406 : Stmt(loc, std::move(labels)), decl(decl) {}
407
[f3cc5b6]408 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[1e97287]409private:
[f3cc5b6]410 DeclStmt * clone() const override { return new DeclStmt{ *this }; }
411 MUTATE_FRIEND
[1e97287]412};
413
[0f740d6]414/// Represents an implicit application of a constructor or destructor.
[1e97287]415class ImplicitCtorDtorStmt final : public Stmt {
416public:
[c570806]417 ptr<Stmt> callStmt;
[1e97287]418
[f3cc5b6]419 ImplicitCtorDtorStmt( const CodeLocation & loc, const Stmt * callStmt,
420 std::vector<Label> && labels = {} )
[1e97287]421 : Stmt(loc, std::move(labels)), callStmt(callStmt) {}
422
[f3cc5b6]423 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[1e97287]424private:
[f3cc5b6]425 ImplicitCtorDtorStmt * clone() const override { return new ImplicitCtorDtorStmt{ *this }; }
426 MUTATE_FRIEND
[1e97287]427};
[2bb4a01]428
[6cebfef]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
[2bb4a01]445}
446
[f3cc5b6]447#undef MUTATE_FRIEND
448
[2bb4a01]449// Local Variables: //
450// tab-width: 4 //
451// mode: c++ //
452// compile-command: "make install" //
[1e97287]453// End: //
Note: See TracBrowser for help on using the repository browser.