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 | |
---|
31 | namespace ast { |
---|
32 | |
---|
33 | class Expr; |
---|
34 | |
---|
35 | /// Base statement node |
---|
36 | class Stmt : public ParseNode { |
---|
37 | public: |
---|
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; |
---|
46 | private: |
---|
47 | Stmt * clone() const override = 0; |
---|
48 | MUTATE_FRIEND |
---|
49 | }; |
---|
50 | |
---|
51 | /// Compound statement `{ ... }` |
---|
52 | class CompoundStmt final : public Stmt { |
---|
53 | public: |
---|
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( 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 `;` |
---|
73 | class NullStmt final : public Stmt { |
---|
74 | public: |
---|
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 ); } |
---|
79 | private: |
---|
80 | NullStmt * clone() const override { return new NullStmt{ *this }; } |
---|
81 | MUTATE_FRIEND |
---|
82 | }; |
---|
83 | |
---|
84 | /// Expression wrapped by statement |
---|
85 | class ExprStmt final : public Stmt { |
---|
86 | public: |
---|
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 ); } |
---|
93 | private: |
---|
94 | ExprStmt * clone() const override { return new ExprStmt{ *this }; } |
---|
95 | MUTATE_FRIEND |
---|
96 | }; |
---|
97 | |
---|
98 | /// Assembly statement `asm ... ( "..." : ... )` |
---|
99 | class 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 | 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 ); } |
---|
116 | private: |
---|
117 | AsmStmt * clone() const override { return new AsmStmt{ *this }; } |
---|
118 | MUTATE_FRIEND |
---|
119 | }; |
---|
120 | |
---|
121 | /// C-preprocessor directive `#...` |
---|
122 | class 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 conditional statement `if (...) ... else ...` |
---|
137 | class IfStmt final : public Stmt { |
---|
138 | public: |
---|
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 | const Stmt * elsePart = nullptr, 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 ); } |
---|
151 | private: |
---|
152 | IfStmt * clone() const override { return new IfStmt{ *this }; } |
---|
153 | MUTATE_FRIEND |
---|
154 | }; |
---|
155 | |
---|
156 | /// Switch or choose conditional statement `switch (...) { ... }` |
---|
157 | class 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, 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 ); } |
---|
167 | private: |
---|
168 | SwitchStmt * clone() const override { return new SwitchStmt{ *this }; } |
---|
169 | MUTATE_FRIEND |
---|
170 | }; |
---|
171 | |
---|
172 | /// Case label `case ...:` `default:` |
---|
173 | class CaseStmt final : public Stmt { |
---|
174 | public: |
---|
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 ); } |
---|
185 | private: |
---|
186 | CaseStmt * clone() const override { return new CaseStmt{ *this }; } |
---|
187 | MUTATE_FRIEND |
---|
188 | }; |
---|
189 | |
---|
190 | /// While loop `while (...) ...` `do ... while (...); |
---|
191 | class WhileStmt final : public Stmt { |
---|
192 | public: |
---|
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 ); } |
---|
204 | private: |
---|
205 | WhileStmt * clone() const override { return new WhileStmt{ *this }; } |
---|
206 | MUTATE_FRIEND |
---|
207 | }; |
---|
208 | |
---|
209 | /// For loop `for (... ; ... ; ...) ...` |
---|
210 | class ForStmt final : public Stmt { |
---|
211 | public: |
---|
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 ); } |
---|
223 | private: |
---|
224 | ForStmt * clone() const override { return new ForStmt{ *this }; } |
---|
225 | MUTATE_FRIEND |
---|
226 | }; |
---|
227 | |
---|
228 | /// Branch control flow statement `goto ...` `break` `continue` `fallthru` |
---|
229 | class BranchStmt final : public Stmt { |
---|
230 | public: |
---|
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() const { return kindNames[kind]; } |
---|
247 | |
---|
248 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
249 | private: |
---|
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 ...` |
---|
257 | class ReturnStmt final : public Stmt { |
---|
258 | public: |
---|
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 ); } |
---|
265 | private: |
---|
266 | ReturnStmt * clone() const override { return new ReturnStmt{ *this }; } |
---|
267 | MUTATE_FRIEND |
---|
268 | }; |
---|
269 | |
---|
270 | /// Kind of exception |
---|
271 | enum ExceptionKind { Terminate, Resume }; |
---|
272 | |
---|
273 | /// Throw statement `throw ...` |
---|
274 | class ThrowStmt final : public Stmt { |
---|
275 | public: |
---|
276 | ptr<Expr> expr; |
---|
277 | ptr<Expr> target; |
---|
278 | ExceptionKind kind; |
---|
279 | |
---|
280 | ThrowStmt( |
---|
281 | const CodeLocation & loc, ExceptionKind kind, const Expr * expr, const Expr * target, |
---|
282 | std::vector<Label> && labels = {} ) |
---|
283 | : Stmt(loc, std::move(labels)), expr(expr), target(target), kind(kind) {} |
---|
284 | |
---|
285 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
286 | private: |
---|
287 | ThrowStmt * clone() const override { return new ThrowStmt{ *this }; } |
---|
288 | MUTATE_FRIEND |
---|
289 | }; |
---|
290 | |
---|
291 | /// Try statement `try { ... } ...` |
---|
292 | class TryStmt final : public Stmt { |
---|
293 | public: |
---|
294 | ptr<CompoundStmt> body; |
---|
295 | std::vector<ptr<CatchStmt>> handlers; |
---|
296 | ptr<FinallyStmt> finally; |
---|
297 | |
---|
298 | TryStmt( |
---|
299 | const CodeLocation & loc, const CompoundStmt * body, |
---|
300 | std::vector<ptr<CatchStmt>> && handlers, const FinallyStmt * finally, |
---|
301 | std::vector<Label> && labels = {} ) |
---|
302 | : Stmt(loc, std::move(labels)), body(body), handlers(std::move(handlers)), finally(finally) {} |
---|
303 | |
---|
304 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
305 | private: |
---|
306 | TryStmt * clone() const override { return new TryStmt{ *this }; } |
---|
307 | MUTATE_FRIEND |
---|
308 | }; |
---|
309 | |
---|
310 | /// Catch clause of try statement |
---|
311 | class CatchStmt final : public Stmt { |
---|
312 | public: |
---|
313 | ptr<Decl> decl; |
---|
314 | ptr<Expr> cond; |
---|
315 | ptr<Stmt> body; |
---|
316 | ExceptionKind kind; |
---|
317 | |
---|
318 | CatchStmt( |
---|
319 | const CodeLocation & loc, ExceptionKind kind, const Decl * decl, const Expr * cond, |
---|
320 | const Stmt * body, std::vector<Label> && labels = {} ) |
---|
321 | : Stmt(loc, std::move(labels)), decl(decl), cond(cond), body(body), kind(kind) {} |
---|
322 | |
---|
323 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
324 | private: |
---|
325 | CatchStmt * clone() const override { return new CatchStmt{ *this }; } |
---|
326 | MUTATE_FRIEND |
---|
327 | }; |
---|
328 | |
---|
329 | /// Finally clause of try statement |
---|
330 | class FinallyStmt final : public Stmt { |
---|
331 | public: |
---|
332 | ptr<CompoundStmt> body; |
---|
333 | |
---|
334 | FinallyStmt( const CodeLocation & loc, const CompoundStmt * body, |
---|
335 | std::vector<Label> && labels = {} ) |
---|
336 | : Stmt(loc, std::move(labels)), body(body) {} |
---|
337 | |
---|
338 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
339 | private: |
---|
340 | FinallyStmt * clone() const override { return new FinallyStmt{ *this }; } |
---|
341 | MUTATE_FRIEND |
---|
342 | }; |
---|
343 | |
---|
344 | /// Wait for concurrency statement `when (...) waitfor (... , ...) ... timeout(...) ... else ...` |
---|
345 | class WaitForStmt final : public Stmt { |
---|
346 | public: |
---|
347 | struct Target { |
---|
348 | ptr<Expr> func; |
---|
349 | std::vector<ptr<Expr>> args; |
---|
350 | }; |
---|
351 | |
---|
352 | struct Clause { |
---|
353 | Target target; |
---|
354 | ptr<Stmt> stmt; |
---|
355 | ptr<Expr> cond; |
---|
356 | }; |
---|
357 | |
---|
358 | struct Timeout { |
---|
359 | ptr<Expr> time; |
---|
360 | ptr<Stmt> stmt; |
---|
361 | ptr<Expr> cond; |
---|
362 | }; |
---|
363 | |
---|
364 | struct OrElse { |
---|
365 | ptr<Stmt> stmt; |
---|
366 | ptr<Expr> cond; |
---|
367 | }; |
---|
368 | |
---|
369 | std::vector<Clause> clauses; |
---|
370 | Timeout timeout; |
---|
371 | OrElse orElse; |
---|
372 | |
---|
373 | WaitForStmt( const CodeLocation & loc, std::vector<Label> && labels = {} ) |
---|
374 | : Stmt(loc, std::move(labels)) {} |
---|
375 | |
---|
376 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
377 | private: |
---|
378 | WaitForStmt * clone() const override { return new WaitForStmt{ *this }; } |
---|
379 | MUTATE_FRIEND |
---|
380 | }; |
---|
381 | |
---|
382 | /// With statement `with (...) ...` |
---|
383 | class WithStmt final : public Stmt { |
---|
384 | public: |
---|
385 | std::vector<ptr<Expr>> exprs; |
---|
386 | ptr<Stmt> stmt; |
---|
387 | |
---|
388 | WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt, |
---|
389 | std::vector<Label> && labels = {} ) |
---|
390 | : Stmt(loc, std::move(labels)), exprs(std::move(exprs)), stmt(stmt) {} |
---|
391 | |
---|
392 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
393 | private: |
---|
394 | WithStmt * clone() const override { return new WithStmt{ *this }; } |
---|
395 | MUTATE_FRIEND |
---|
396 | }; |
---|
397 | |
---|
398 | /// Any declaration in a (compound) statement. |
---|
399 | class DeclStmt final : public Stmt { |
---|
400 | public: |
---|
401 | ptr<Decl> decl; |
---|
402 | |
---|
403 | DeclStmt( const CodeLocation & loc, const Decl * decl, std::vector<Label> && labels = {} ) |
---|
404 | : Stmt(loc, std::move(labels)), decl(decl) {} |
---|
405 | |
---|
406 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
407 | private: |
---|
408 | DeclStmt * clone() const override { return new DeclStmt{ *this }; } |
---|
409 | MUTATE_FRIEND |
---|
410 | }; |
---|
411 | |
---|
412 | /// Represents an implicit application of a constructor or destructor. |
---|
413 | class ImplicitCtorDtorStmt final : public Stmt { |
---|
414 | public: |
---|
415 | readonly<Stmt> callStmt; |
---|
416 | |
---|
417 | ImplicitCtorDtorStmt( const CodeLocation & loc, const Stmt * callStmt, |
---|
418 | std::vector<Label> && labels = {} ) |
---|
419 | : Stmt(loc, std::move(labels)), callStmt(callStmt) {} |
---|
420 | |
---|
421 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
422 | private: |
---|
423 | ImplicitCtorDtorStmt * clone() const override { return new ImplicitCtorDtorStmt{ *this }; } |
---|
424 | MUTATE_FRIEND |
---|
425 | }; |
---|
426 | |
---|
427 | } |
---|
428 | |
---|
429 | #undef MUTATE_FRIEND |
---|
430 | |
---|
431 | // Local Variables: // |
---|
432 | // tab-width: 4 // |
---|
433 | // mode: c++ // |
---|
434 | // compile-command: "make install" // |
---|
435 | // End: // |
---|