source: src/AST/Stmt.hpp@ 9d6e7fa9

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 9d6e7fa9 was 0f740d6, checked in by Andrew Beach <ajbeach@…>, 6 years ago

Clean-up. Added one line docs for Stmts.

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