source: src/AST/Stmt.hpp@ ee918356

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since ee918356 was 6180274, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

more cleanup, make more function parameters const, remove more std::

  • Property mode set to 100644
File size: 14.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 : Peter A. Buhr
12// Last Modified On : Wed Feb 2 20:06:41 2022
13// Update Count : 34
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 {
34class Expr;
35
36// Base statement node
37class Stmt : public ParseNode {
38 public:
39 std::vector<Label> labels;
40
41 Stmt( const CodeLocation & loc, const std::vector<Label> && labels = {} )
42 : ParseNode(loc), labels(std::move(labels)) {}
43
44 Stmt(const Stmt & o) : ParseNode(o), labels(o.labels) {}
45
46 const Stmt * accept( Visitor & v ) const override = 0;
47 private:
48 Stmt * clone() const override = 0;
49 MUTATE_FRIEND
50};
51
52// Compound statement: { ... }
53class CompoundStmt final : public Stmt {
54 public:
55 std::list<ptr<Stmt>> kids;
56
57 CompoundStmt(const CodeLocation & loc, const std::list<ptr<Stmt>> && ks = {}, const std::vector<Label> && labels = {} )
58 : Stmt(loc, std::move(labels)), kids(std::move(ks)) {}
59
60 CompoundStmt( const CompoundStmt & o );
61 CompoundStmt( CompoundStmt && o ) = default;
62
63 void push_back( const Stmt * s ) { kids.emplace_back( s ); }
64 void push_front( const Stmt * s ) { kids.emplace_front( s ); }
65
66 const CompoundStmt * accept( Visitor & v ) const override { return v.visit( this ); }
67 private:
68 CompoundStmt * clone() const override { return new CompoundStmt{ *this }; }
69 MUTATE_FRIEND
70};
71
72// Empty statment: ;
73class NullStmt final : public Stmt {
74 public:
75 NullStmt( const CodeLocation & loc, const std::vector<Label> && labels = {} )
76 : Stmt(loc, std::move(labels)) {}
77
78 const NullStmt * accept( Visitor & v ) const override { return v.visit( this ); }
79 private:
80 NullStmt * clone() const override { return new NullStmt{ *this }; }
81 MUTATE_FRIEND
82};
83
84// Expression wrapped by statement
85class ExprStmt final : public Stmt {
86 public:
87 ptr<Expr> expr;
88
89 ExprStmt( const CodeLocation & loc, const Expr* e, const std::vector<Label> && labels = {} )
90 : Stmt(loc, std::move(labels)), expr(e) {}
91
92 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
93 private:
94 ExprStmt * clone() const override { return new ExprStmt{ *this }; }
95 MUTATE_FRIEND
96};
97
98// Assembly statement: asm ... ( "..." : ... )
99class AsmStmt final : public Stmt {
100 public:
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
107 AsmStmt( const CodeLocation & loc, bool isVolatile, const Expr * instruction,
108 const std::vector<ptr<Expr>> && output, const std::vector<ptr<Expr>> && input,
109 const std::vector<ptr<ConstantExpr>> && clobber, const std::vector<Label> && gotoLabels,
110 const std::vector<Label> && labels = {})
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
115 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
116 private:
117 AsmStmt * clone() const override { return new AsmStmt{ *this }; }
118 MUTATE_FRIEND
119};
120
121// C-preprocessor directive: #...
122class DirectiveStmt final : public Stmt {
123 public:
124 std::string directive;
125
126 DirectiveStmt( const CodeLocation & loc, const std::string & directive,
127 std::vector<Label> && labels = {} )
128 : Stmt(loc, std::move(labels)), directive(directive) {}
129
130 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
131 private:
132 DirectiveStmt * clone() const override { return new DirectiveStmt{ *this }; }
133 MUTATE_FRIEND
134};
135
136// If statement: if (...) ... else ...
137class IfStmt final : public Stmt {
138 public:
139 ptr<Expr> cond;
140 ptr<Stmt> then;
141 ptr<Stmt> else_;
142 std::vector<ptr<Stmt>> inits;
143
144 IfStmt( const CodeLocation & loc, const Expr * cond, const Stmt * then,
145 const Stmt * else_ = nullptr, const std::vector<ptr<Stmt>> && inits = {},
146 const std::vector<Label> && labels = {} )
147 : Stmt(loc, std::move(labels)), cond(cond), then(then), else_(else_),
148 inits(std::move(inits)) {}
149
150 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
151 private:
152 IfStmt * clone() const override { return new IfStmt{ *this }; }
153 MUTATE_FRIEND
154};
155
156// Switch or choose statement: switch (...) { ... }
157class SwitchStmt final : public Stmt {
158 public:
159 ptr<Expr> cond;
160 std::vector<ptr<Stmt>> stmts;
161
162 SwitchStmt( const CodeLocation & loc, const Expr * cond, const std::vector<ptr<Stmt>> && stmts,
163 const std::vector<Label> && labels = {} )
164 : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
165
166 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
167 private:
168 SwitchStmt * clone() const override { return new SwitchStmt{ *this }; }
169 MUTATE_FRIEND
170};
171
172// Case label: case ...: or default:
173class CaseStmt final : public Stmt {
174 public:
175 // Null for the default label.
176 ptr<Expr> cond;
177 std::vector<ptr<Stmt>> stmts;
178
179 CaseStmt( const CodeLocation & loc, const Expr * cond, const std::vector<ptr<Stmt>> && stmts,
180 const std::vector<Label> && labels = {} )
181 : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
182
183 bool isDefault() const { return !cond; }
184
185 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
186 private:
187 CaseStmt * clone() const override { return new CaseStmt{ *this }; }
188 MUTATE_FRIEND
189};
190
191// While loop: while (...) ... else ... or do ... while (...) else ...;
192class WhileDoStmt final : public Stmt {
193 public:
194 ptr<Expr> cond;
195 ptr<Stmt> body;
196 ptr<Stmt> else_;
197 std::vector<ptr<Stmt>> inits;
198 bool isDoWhile;
199
200 WhileDoStmt( const CodeLocation & loc, const Expr * cond, const Stmt * body,
201 const std::vector<ptr<Stmt>> && inits, bool isDoWhile = false, const std::vector<Label> && labels = {} )
202 : Stmt(loc, std::move(labels)), cond(cond), body(body), else_(nullptr), inits(std::move(inits)), isDoWhile(isDoWhile) {}
203
204 WhileDoStmt( const CodeLocation & loc, const Expr * cond, const Stmt * body, const Stmt * else_,
205 const std::vector<ptr<Stmt>> && inits, bool isDoWhile = false, const std::vector<Label> && labels = {} )
206 : Stmt(loc, std::move(labels)), cond(cond), body(body), else_(else_), inits(std::move(inits)), isDoWhile(isDoWhile) {}
207
208 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
209 private:
210 WhileDoStmt * clone() const override { return new WhileDoStmt{ *this }; }
211 MUTATE_FRIEND
212};
213
214// For loop: for (... ; ... ; ...) ... else ...
215class ForStmt final : public Stmt {
216 public:
217 std::vector<ptr<Stmt>> inits;
218 ptr<Expr> cond;
219 ptr<Expr> inc;
220 ptr<Stmt> body;
221 ptr<Stmt> else_;
222
223 ForStmt( const CodeLocation & loc, const std::vector<ptr<Stmt>> && inits, const Expr * cond,
224 const Expr * inc, const Stmt * body, const std::vector<Label> && label = {} )
225 : Stmt(loc, std::move(label)), inits(std::move(inits)), cond(cond), inc(inc), body(body), else_(nullptr) {}
226
227 ForStmt( const CodeLocation & loc, const std::vector<ptr<Stmt>> && inits, const Expr * cond,
228 const Expr * inc, const Stmt * body, const Stmt * else_, const std::vector<Label> && labels = {} )
229 : Stmt(loc, std::move(labels)), inits(std::move(inits)), cond(cond), inc(inc), body(body), else_(else_) {}
230
231 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
232 private:
233 ForStmt * clone() const override { return new ForStmt{ *this }; }
234 MUTATE_FRIEND
235};
236
237// Branch control flow statement: goto ... or break or continue or fallthru
238class BranchStmt final : public Stmt {
239 public:
240 enum Kind { Goto, Break, Continue, FallThrough, FallThroughDefault };
241 static constexpr size_t kindEnd = 1 + (size_t)FallThroughDefault;
242
243 const Label originalTarget;
244 Label target;
245 ptr<Expr> computedTarget;
246 Kind kind;
247
248 BranchStmt( const CodeLocation & loc, Kind kind, Label target, const std::vector<Label> && labels = {} );
249 BranchStmt( const CodeLocation & loc, const Expr * computedTarget, const std::vector<Label> && labels = {} )
250 : Stmt(loc, std::move(labels)), originalTarget(loc), target(loc), computedTarget(computedTarget), kind(Goto) {}
251
252 const char * kindName() const { return kindNames[kind]; }
253
254 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
255 private:
256 BranchStmt * clone() const override { return new BranchStmt{ *this }; }
257 MUTATE_FRIEND
258
259 static const char * kindNames[kindEnd];
260};
261
262// Return statement: return ...
263class ReturnStmt final : public Stmt {
264 public:
265 ptr<Expr> expr;
266
267 ReturnStmt( const CodeLocation & loc, const Expr * expr, const std::vector<Label> && labels = {} )
268 : Stmt(loc, std::move(labels)), expr(expr) {}
269
270 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
271 private:
272 ReturnStmt * clone() const override { return new ReturnStmt{ *this }; }
273 MUTATE_FRIEND
274};
275
276// Kind of exception
277enum ExceptionKind { Terminate, Resume };
278
279// Throw statement: throw ...
280class ThrowStmt final : public Stmt {
281 public:
282 ptr<Expr> expr;
283 ptr<Expr> target;
284 ExceptionKind kind;
285
286 ThrowStmt( const CodeLocation & loc, ExceptionKind kind, const Expr * expr,
287 const Expr * target, const std::vector<Label> && labels = {} )
288 : Stmt(loc, std::move(labels)), expr(expr), target(target), kind(kind) {}
289
290 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
291 private:
292 ThrowStmt * clone() const override { return new ThrowStmt{ *this }; }
293 MUTATE_FRIEND
294};
295
296// Try statement: try { ... } ...
297class TryStmt final : public Stmt {
298 public:
299 ptr<CompoundStmt> body;
300 std::vector<ptr<CatchStmt>> handlers;
301 ptr<FinallyStmt> finally;
302
303 TryStmt( const CodeLocation & loc, const CompoundStmt * body,
304 const std::vector<ptr<CatchStmt>> && handlers, const FinallyStmt * finally,
305 const std::vector<Label> && labels = {} )
306 : Stmt(loc, std::move(labels)), body(body), handlers(std::move(handlers)), finally(finally) {}
307
308 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
309 private:
310 TryStmt * clone() const override { return new TryStmt{ *this }; }
311 MUTATE_FRIEND
312};
313
314// Catch clause of try statement
315class CatchStmt final : public Stmt {
316 public:
317 ptr<Decl> decl;
318 ptr<Expr> cond;
319 ptr<Stmt> body;
320 ExceptionKind kind;
321
322 CatchStmt( const CodeLocation & loc, ExceptionKind kind, const Decl * decl, const Expr * cond,
323 const Stmt * body, const 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 ); }
327 private:
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 {
334 public:
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 ); }
342 private:
343 FinallyStmt * clone() const override { return new FinallyStmt{ *this }; }
344 MUTATE_FRIEND
345};
346
347// Suspend statement
348class SuspendStmt final : public Stmt {
349 public:
350 ptr<CompoundStmt> then;
351 enum Type { None, Coroutine, Generator } type = None;
352
353 SuspendStmt( const CodeLocation & loc, const CompoundStmt * then, Type type, const 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 ); }
357 private:
358 SuspendStmt * clone() const override { return new SuspendStmt{ *this }; }
359 MUTATE_FRIEND
360};
361
362// Waitfor statement: when (...) waitfor (... , ...) ... timeout(...) ... else ...
363class WaitForStmt final : public Stmt {
364 public:
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, const std::vector<Label> && labels = {} )
392 : Stmt(loc, std::move(labels)) {}
393
394 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
395 private:
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 {
402 public:
403 ptr<Decl> decl;
404
405 DeclStmt( const CodeLocation & loc, const Decl * decl, const 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 ); }
409 private:
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 {
416 public:
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 ); }
424 private:
425 ImplicitCtorDtorStmt * clone() const override { return new ImplicitCtorDtorStmt{ *this }; }
426 MUTATE_FRIEND
427};
428
429// Mutex Statement
430class MutexStmt final : public Stmt {
431 public:
432 ptr<Stmt> stmt;
433 std::vector<ptr<Expr>> mutexObjs;
434
435 MutexStmt( const CodeLocation & loc, const Stmt * stmt,
436 const std::vector<ptr<Expr>> && mutexes, const 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 ); }
440 private:
441 MutexStmt * clone() const override { return new MutexStmt{ *this }; }
442 MUTATE_FRIEND
443};
444} // namespace ast
445
446#undef MUTATE_FRIEND
447
448// Local Variables: //
449// mode: c++ //
450// End: //
Note: See TracBrowser for help on using the repository browser.