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 : Wed Apr 20 14:34:00 2022 |
---|
13 | // Update Count : 36 |
---|
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 | |
---|
33 | namespace ast { |
---|
34 | class Expr; |
---|
35 | |
---|
36 | // Base statement node |
---|
37 | class 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 | // Base statement component node (only serves to group them). |
---|
53 | class StmtClause : public ParseNode { |
---|
54 | public: |
---|
55 | // This is for non-statements that still belong with the statements, |
---|
56 | // but are not statements, usually some sort of clause. Often these can |
---|
57 | // (and should) be folded into the approprate parent node, but if they |
---|
58 | // cannot be, they are sub-types of this type, for organization. |
---|
59 | |
---|
60 | StmtClause( const CodeLocation & loc ) |
---|
61 | : ParseNode(loc) {} |
---|
62 | |
---|
63 | private: |
---|
64 | StmtClause * clone() const override = 0; |
---|
65 | MUTATE_FRIEND |
---|
66 | }; |
---|
67 | |
---|
68 | // Compound statement: { ... } |
---|
69 | class CompoundStmt final : public Stmt { |
---|
70 | public: |
---|
71 | std::list<ptr<Stmt>> kids; |
---|
72 | |
---|
73 | CompoundStmt(const CodeLocation & loc, const std::list<ptr<Stmt>> && ks = {}, const std::vector<Label> && labels = {} ) |
---|
74 | : Stmt(loc, std::move(labels)), kids(std::move(ks)) {} |
---|
75 | |
---|
76 | CompoundStmt( const CompoundStmt & o ); |
---|
77 | CompoundStmt( CompoundStmt && o ) = default; |
---|
78 | |
---|
79 | void push_back( const Stmt * s ) { kids.emplace_back( s ); } |
---|
80 | void push_front( const Stmt * s ) { kids.emplace_front( s ); } |
---|
81 | |
---|
82 | const CompoundStmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
83 | private: |
---|
84 | CompoundStmt * clone() const override { return new CompoundStmt{ *this }; } |
---|
85 | MUTATE_FRIEND |
---|
86 | }; |
---|
87 | |
---|
88 | // Empty statment: ; |
---|
89 | class NullStmt final : public Stmt { |
---|
90 | public: |
---|
91 | NullStmt( const CodeLocation & loc, const std::vector<Label> && labels = {} ) |
---|
92 | : Stmt(loc, std::move(labels)) {} |
---|
93 | |
---|
94 | const NullStmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
95 | private: |
---|
96 | NullStmt * clone() const override { return new NullStmt{ *this }; } |
---|
97 | MUTATE_FRIEND |
---|
98 | }; |
---|
99 | |
---|
100 | // Expression wrapped by statement |
---|
101 | class ExprStmt final : public Stmt { |
---|
102 | public: |
---|
103 | ptr<Expr> expr; |
---|
104 | |
---|
105 | ExprStmt( const CodeLocation & loc, const Expr* e, const std::vector<Label> && labels = {} ) |
---|
106 | : Stmt(loc, std::move(labels)), expr(e) {} |
---|
107 | |
---|
108 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
109 | private: |
---|
110 | ExprStmt * clone() const override { return new ExprStmt{ *this }; } |
---|
111 | MUTATE_FRIEND |
---|
112 | }; |
---|
113 | |
---|
114 | // Assembly statement: asm ... ( "..." : ... ) |
---|
115 | class AsmStmt final : public Stmt { |
---|
116 | public: |
---|
117 | bool isVolatile; |
---|
118 | ptr<Expr> instruction; |
---|
119 | std::vector<ptr<Expr>> output, input; |
---|
120 | std::vector<ptr<ConstantExpr>> clobber; |
---|
121 | std::vector<Label> gotoLabels; |
---|
122 | |
---|
123 | AsmStmt( const CodeLocation & loc, bool isVolatile, const Expr * instruction, |
---|
124 | const std::vector<ptr<Expr>> && output, const std::vector<ptr<Expr>> && input, |
---|
125 | const std::vector<ptr<ConstantExpr>> && clobber, const std::vector<Label> && gotoLabels, |
---|
126 | const std::vector<Label> && labels = {}) |
---|
127 | : Stmt(loc, std::move(labels)), isVolatile(isVolatile), instruction(instruction), |
---|
128 | output(std::move(output)), input(std::move(input)), clobber(std::move(clobber)), |
---|
129 | gotoLabels(std::move(gotoLabels)) {} |
---|
130 | |
---|
131 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
132 | private: |
---|
133 | AsmStmt * clone() const override { return new AsmStmt{ *this }; } |
---|
134 | MUTATE_FRIEND |
---|
135 | }; |
---|
136 | |
---|
137 | // C-preprocessor directive: #... |
---|
138 | class DirectiveStmt final : public Stmt { |
---|
139 | public: |
---|
140 | std::string directive; |
---|
141 | |
---|
142 | DirectiveStmt( const CodeLocation & loc, const std::string & directive, |
---|
143 | std::vector<Label> && labels = {} ) |
---|
144 | : Stmt(loc, std::move(labels)), directive(directive) {} |
---|
145 | |
---|
146 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
147 | private: |
---|
148 | DirectiveStmt * clone() const override { return new DirectiveStmt{ *this }; } |
---|
149 | MUTATE_FRIEND |
---|
150 | }; |
---|
151 | |
---|
152 | // If statement: if (...) ... else ... |
---|
153 | class IfStmt final : public Stmt { |
---|
154 | public: |
---|
155 | ptr<Expr> cond; |
---|
156 | ptr<Stmt> then; |
---|
157 | ptr<Stmt> else_; |
---|
158 | std::vector<ptr<Stmt>> inits; |
---|
159 | |
---|
160 | IfStmt( const CodeLocation & loc, const Expr * cond, const Stmt * then, |
---|
161 | const Stmt * else_ = nullptr, const std::vector<ptr<Stmt>> && inits = {}, |
---|
162 | const std::vector<Label> && labels = {} ) |
---|
163 | : Stmt(loc, std::move(labels)), cond(cond), then(then), else_(else_), |
---|
164 | inits(std::move(inits)) {} |
---|
165 | |
---|
166 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
167 | private: |
---|
168 | IfStmt * clone() const override { return new IfStmt{ *this }; } |
---|
169 | MUTATE_FRIEND |
---|
170 | }; |
---|
171 | |
---|
172 | // Switch or choose statement: switch (...) { ... } |
---|
173 | class SwitchStmt final : public Stmt { |
---|
174 | public: |
---|
175 | ptr<Expr> cond; |
---|
176 | std::vector<ptr<CaseClause>> cases; |
---|
177 | |
---|
178 | SwitchStmt( const CodeLocation & loc, const Expr * cond, |
---|
179 | const std::vector<ptr<CaseClause>> && cases, |
---|
180 | const std::vector<Label> && labels = {} ) |
---|
181 | : Stmt(loc, std::move(labels)), cond(cond), cases(std::move(cases)) {} |
---|
182 | |
---|
183 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
184 | private: |
---|
185 | SwitchStmt * clone() const override { return new SwitchStmt{ *this }; } |
---|
186 | MUTATE_FRIEND |
---|
187 | }; |
---|
188 | |
---|
189 | // Case label: case ...: or default: |
---|
190 | class CaseClause final : public StmtClause { |
---|
191 | public: |
---|
192 | // Null for the default label. |
---|
193 | ptr<Expr> cond; |
---|
194 | std::vector<ptr<Stmt>> stmts; |
---|
195 | |
---|
196 | CaseClause( const CodeLocation & loc, const Expr * cond, const std::vector<ptr<Stmt>> && stmts ) |
---|
197 | : StmtClause(loc), cond(cond), stmts(std::move(stmts)) {} |
---|
198 | |
---|
199 | bool isDefault() const { return !cond; } |
---|
200 | |
---|
201 | const CaseClause * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
202 | private: |
---|
203 | CaseClause * clone() const override { return new CaseClause{ *this }; } |
---|
204 | MUTATE_FRIEND |
---|
205 | }; |
---|
206 | |
---|
207 | // While loop: while (...) ... else ... or do ... while (...) else ...; |
---|
208 | class WhileDoStmt final : public Stmt { |
---|
209 | public: |
---|
210 | ptr<Expr> cond; |
---|
211 | ptr<Stmt> body; |
---|
212 | ptr<Stmt> else_; |
---|
213 | std::vector<ptr<Stmt>> inits; |
---|
214 | bool isDoWhile; |
---|
215 | |
---|
216 | WhileDoStmt( const CodeLocation & loc, const Expr * cond, const Stmt * body, |
---|
217 | const std::vector<ptr<Stmt>> && inits, bool isDoWhile = false, const std::vector<Label> && labels = {} ) |
---|
218 | : Stmt(loc, std::move(labels)), cond(cond), body(body), else_(nullptr), inits(std::move(inits)), isDoWhile(isDoWhile) {} |
---|
219 | |
---|
220 | WhileDoStmt( const CodeLocation & loc, const Expr * cond, const Stmt * body, const Stmt * else_, |
---|
221 | const std::vector<ptr<Stmt>> && inits, bool isDoWhile = false, const std::vector<Label> && labels = {} ) |
---|
222 | : Stmt(loc, std::move(labels)), cond(cond), body(body), else_(else_), inits(std::move(inits)), isDoWhile(isDoWhile) {} |
---|
223 | |
---|
224 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
225 | private: |
---|
226 | WhileDoStmt * clone() const override { return new WhileDoStmt{ *this }; } |
---|
227 | MUTATE_FRIEND |
---|
228 | }; |
---|
229 | |
---|
230 | // For loop: for (... ; ... ; ...) ... else ... |
---|
231 | class ForStmt final : public Stmt { |
---|
232 | public: |
---|
233 | std::vector<ptr<Stmt>> inits; |
---|
234 | ptr<Expr> cond; |
---|
235 | ptr<Expr> inc; |
---|
236 | ptr<Stmt> body; |
---|
237 | ptr<Stmt> else_; |
---|
238 | |
---|
239 | ForStmt( const CodeLocation & loc, const std::vector<ptr<Stmt>> && inits, const Expr * cond, |
---|
240 | const Expr * inc, const Stmt * body, const std::vector<Label> && label = {} ) |
---|
241 | : Stmt(loc, std::move(label)), inits(std::move(inits)), cond(cond), inc(inc), body(body), else_(nullptr) {} |
---|
242 | |
---|
243 | ForStmt( const CodeLocation & loc, const std::vector<ptr<Stmt>> && inits, const Expr * cond, |
---|
244 | const Expr * inc, const Stmt * body, const Stmt * else_, const std::vector<Label> && labels = {} ) |
---|
245 | : Stmt(loc, std::move(labels)), inits(std::move(inits)), cond(cond), inc(inc), body(body), else_(else_) {} |
---|
246 | |
---|
247 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
248 | private: |
---|
249 | ForStmt * clone() const override { return new ForStmt{ *this }; } |
---|
250 | MUTATE_FRIEND |
---|
251 | }; |
---|
252 | |
---|
253 | // Branch control flow statement: goto ... or break or continue or fallthru |
---|
254 | class BranchStmt final : public Stmt { |
---|
255 | public: |
---|
256 | enum Kind { Goto, Break, Continue, FallThrough, FallThroughDefault }; |
---|
257 | static constexpr size_t kindEnd = 1 + (size_t)FallThroughDefault; |
---|
258 | |
---|
259 | const Label originalTarget; |
---|
260 | Label target; |
---|
261 | ptr<Expr> computedTarget; |
---|
262 | Kind kind; |
---|
263 | |
---|
264 | BranchStmt( const CodeLocation & loc, Kind kind, Label target, const std::vector<Label> && labels = {} ); |
---|
265 | BranchStmt( const CodeLocation & loc, const Expr * computedTarget, const std::vector<Label> && labels = {} ) |
---|
266 | : Stmt(loc, std::move(labels)), originalTarget(loc), target(loc), computedTarget(computedTarget), kind(Goto) {} |
---|
267 | |
---|
268 | const char * kindName() const { return kindNames[kind]; } |
---|
269 | |
---|
270 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
271 | private: |
---|
272 | BranchStmt * clone() const override { return new BranchStmt{ *this }; } |
---|
273 | MUTATE_FRIEND |
---|
274 | |
---|
275 | static const char * kindNames[kindEnd]; |
---|
276 | }; |
---|
277 | |
---|
278 | // Return statement: return ... |
---|
279 | class ReturnStmt final : public Stmt { |
---|
280 | public: |
---|
281 | ptr<Expr> expr; |
---|
282 | |
---|
283 | ReturnStmt( const CodeLocation & loc, const Expr * expr, const std::vector<Label> && labels = {} ) |
---|
284 | : Stmt(loc, std::move(labels)), expr(expr) {} |
---|
285 | |
---|
286 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
287 | private: |
---|
288 | ReturnStmt * clone() const override { return new ReturnStmt{ *this }; } |
---|
289 | MUTATE_FRIEND |
---|
290 | }; |
---|
291 | |
---|
292 | // Kind of exception |
---|
293 | enum ExceptionKind { Terminate, Resume }; |
---|
294 | |
---|
295 | // Throw statement: throw ... |
---|
296 | class ThrowStmt final : public Stmt { |
---|
297 | public: |
---|
298 | ptr<Expr> expr; |
---|
299 | ptr<Expr> target; |
---|
300 | ExceptionKind kind; |
---|
301 | |
---|
302 | ThrowStmt( const CodeLocation & loc, ExceptionKind kind, const Expr * expr, |
---|
303 | const Expr * target, const std::vector<Label> && labels = {} ) |
---|
304 | : Stmt(loc, std::move(labels)), expr(expr), target(target), kind(kind) {} |
---|
305 | |
---|
306 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
307 | private: |
---|
308 | ThrowStmt * clone() const override { return new ThrowStmt{ *this }; } |
---|
309 | MUTATE_FRIEND |
---|
310 | }; |
---|
311 | |
---|
312 | // Try statement: try { ... } ... |
---|
313 | class TryStmt final : public Stmt { |
---|
314 | public: |
---|
315 | ptr<CompoundStmt> body; |
---|
316 | std::vector<ptr<CatchClause>> handlers; |
---|
317 | ptr<FinallyClause> finally; |
---|
318 | |
---|
319 | TryStmt( const CodeLocation & loc, const CompoundStmt * body, |
---|
320 | const std::vector<ptr<CatchClause>> && handlers, const FinallyClause * finally, |
---|
321 | const std::vector<Label> && labels = {} ) |
---|
322 | : Stmt(loc, std::move(labels)), body(body), handlers(std::move(handlers)), finally(finally) {} |
---|
323 | |
---|
324 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
325 | private: |
---|
326 | TryStmt * clone() const override { return new TryStmt{ *this }; } |
---|
327 | MUTATE_FRIEND |
---|
328 | }; |
---|
329 | |
---|
330 | // Catch clause of try statement |
---|
331 | class CatchClause final : public StmtClause { |
---|
332 | public: |
---|
333 | ptr<Decl> decl; |
---|
334 | ptr<Expr> cond; |
---|
335 | ptr<Stmt> body; |
---|
336 | ExceptionKind kind; |
---|
337 | |
---|
338 | CatchClause( const CodeLocation & loc, ExceptionKind kind, const Decl * decl, const Expr * cond, |
---|
339 | const Stmt * body ) |
---|
340 | : StmtClause(loc), decl(decl), cond(cond), body(body), kind(kind) {} |
---|
341 | |
---|
342 | const CatchClause * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
343 | private: |
---|
344 | CatchClause * clone() const override { return new CatchClause{ *this }; } |
---|
345 | MUTATE_FRIEND |
---|
346 | }; |
---|
347 | |
---|
348 | // Finally clause of try statement |
---|
349 | class FinallyClause final : public StmtClause { |
---|
350 | public: |
---|
351 | ptr<CompoundStmt> body; |
---|
352 | |
---|
353 | FinallyClause( const CodeLocation & loc, const CompoundStmt * body ) |
---|
354 | : StmtClause(loc), body(body) {} |
---|
355 | |
---|
356 | const FinallyClause * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
357 | private: |
---|
358 | FinallyClause * clone() const override { return new FinallyClause{ *this }; } |
---|
359 | MUTATE_FRIEND |
---|
360 | }; |
---|
361 | |
---|
362 | // Suspend statement |
---|
363 | class SuspendStmt final : public Stmt { |
---|
364 | public: |
---|
365 | ptr<CompoundStmt> then; |
---|
366 | enum Type { None, Coroutine, Generator } type = None; |
---|
367 | |
---|
368 | SuspendStmt( const CodeLocation & loc, const CompoundStmt * then, Type type, const std::vector<Label> && labels = {} ) |
---|
369 | : Stmt(loc, std::move(labels)), then(then), type(type) {} |
---|
370 | |
---|
371 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
372 | private: |
---|
373 | SuspendStmt * clone() const override { return new SuspendStmt{ *this }; } |
---|
374 | MUTATE_FRIEND |
---|
375 | }; |
---|
376 | |
---|
377 | // Waitfor statement: when (...) waitfor (... , ...) ... timeout(...) ... else ... |
---|
378 | class WaitForStmt final : public Stmt { |
---|
379 | public: |
---|
380 | std::vector<ptr<WaitForClause>> clauses; |
---|
381 | ptr<Expr> timeout_time; |
---|
382 | ptr<Stmt> timeout_stmt; |
---|
383 | ptr<Expr> timeout_cond; |
---|
384 | ptr<Stmt> else_stmt; |
---|
385 | ptr<Expr> else_cond; |
---|
386 | |
---|
387 | WaitForStmt( const CodeLocation & loc, const std::vector<Label> && labels = {} ) |
---|
388 | : Stmt(loc, std::move(labels)) {} |
---|
389 | |
---|
390 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
391 | private: |
---|
392 | WaitForStmt * clone() const override { return new WaitForStmt{ *this }; } |
---|
393 | MUTATE_FRIEND |
---|
394 | }; |
---|
395 | |
---|
396 | class WaitForClause final : public StmtClause { |
---|
397 | public: |
---|
398 | ptr<Expr> target_func; |
---|
399 | std::vector<ptr<Expr>> target_args; |
---|
400 | ptr<Stmt> stmt; |
---|
401 | ptr<Expr> cond; |
---|
402 | |
---|
403 | WaitForClause( const CodeLocation & loc ) |
---|
404 | : StmtClause( loc ) {} |
---|
405 | |
---|
406 | const WaitForClause * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
407 | private: |
---|
408 | WaitForClause * clone() const override { return new WaitForClause{ *this }; } |
---|
409 | MUTATE_FRIEND |
---|
410 | }; |
---|
411 | |
---|
412 | // Any declaration in a (compound) statement. |
---|
413 | class DeclStmt final : public Stmt { |
---|
414 | public: |
---|
415 | ptr<Decl> decl; |
---|
416 | |
---|
417 | DeclStmt( const CodeLocation & loc, const Decl * decl, const std::vector<Label> && labels = {} ) |
---|
418 | : Stmt(loc, std::move(labels)), decl(decl) {} |
---|
419 | |
---|
420 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
421 | private: |
---|
422 | DeclStmt * clone() const override { return new DeclStmt{ *this }; } |
---|
423 | MUTATE_FRIEND |
---|
424 | }; |
---|
425 | |
---|
426 | // Represents an implicit application of a constructor or destructor. |
---|
427 | class ImplicitCtorDtorStmt final : public Stmt { |
---|
428 | public: |
---|
429 | ptr<Stmt> callStmt; |
---|
430 | |
---|
431 | ImplicitCtorDtorStmt( const CodeLocation & loc, const Stmt * callStmt, |
---|
432 | std::vector<Label> && labels = {} ) |
---|
433 | : Stmt(loc, std::move(labels)), callStmt(callStmt) {} |
---|
434 | |
---|
435 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
436 | private: |
---|
437 | ImplicitCtorDtorStmt * clone() const override { return new ImplicitCtorDtorStmt{ *this }; } |
---|
438 | MUTATE_FRIEND |
---|
439 | }; |
---|
440 | |
---|
441 | // Mutex Statement |
---|
442 | class MutexStmt final : public Stmt { |
---|
443 | public: |
---|
444 | ptr<Stmt> stmt; |
---|
445 | std::vector<ptr<Expr>> mutexObjs; |
---|
446 | |
---|
447 | MutexStmt( const CodeLocation & loc, const Stmt * stmt, |
---|
448 | const std::vector<ptr<Expr>> && mutexes, const std::vector<Label> && labels = {} ) |
---|
449 | : Stmt(loc, std::move(labels)), stmt(stmt), mutexObjs(std::move(mutexes)) {} |
---|
450 | |
---|
451 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
452 | private: |
---|
453 | MutexStmt * clone() const override { return new MutexStmt{ *this }; } |
---|
454 | MUTATE_FRIEND |
---|
455 | }; |
---|
456 | } // namespace ast |
---|
457 | |
---|
458 | #undef MUTATE_FRIEND |
---|
459 | |
---|
460 | // Local Variables: // |
---|
461 | // mode: c++ // |
---|
462 | // End: // |
---|