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( 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 ); }
|
---|
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 | class AsmStmt final : public Stmt {
|
---|
99 | public:
|
---|
100 | bool isVolatile;
|
---|
101 | ptr<Expr> instruction;
|
---|
102 | std::vector<ptr<Expr>> output, input;
|
---|
103 | std::vector<ptr<ConstantExpr>> clobber;
|
---|
104 | std::vector<Label> gotoLabels;
|
---|
105 |
|
---|
106 | AsmStmt( const CodeLocation & loc, bool isVolatile, const Expr * instruction,
|
---|
107 | std::vector<ptr<Expr>> && output, std::vector<ptr<Expr>> && input,
|
---|
108 | std::vector<ptr<ConstantExpr>> && clobber, std::vector<Label> && gotoLabels,
|
---|
109 | std::vector<Label> && labels = {})
|
---|
110 | : Stmt(loc, std::move(labels)), isVolatile(isVolatile), instruction(instruction),
|
---|
111 | output(std::move(output)), input(std::move(input)), clobber(std::move(clobber)),
|
---|
112 | gotoLabels(std::move(gotoLabels)) {}
|
---|
113 |
|
---|
114 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
115 | private:
|
---|
116 | AsmStmt * clone() const override { return new AsmStmt{ *this }; }
|
---|
117 | MUTATE_FRIEND
|
---|
118 | };
|
---|
119 |
|
---|
120 | class DirectiveStmt final : public Stmt {
|
---|
121 | public:
|
---|
122 | std::string directive;
|
---|
123 |
|
---|
124 | DirectiveStmt( const CodeLocation & loc, const std::string & directive,
|
---|
125 | std::vector<Label> && labels = {} )
|
---|
126 | : Stmt(loc, std::move(labels)), directive(directive) {}
|
---|
127 |
|
---|
128 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
129 | private:
|
---|
130 | DirectiveStmt * clone() const override { return new DirectiveStmt{ *this }; }
|
---|
131 | MUTATE_FRIEND
|
---|
132 | };
|
---|
133 |
|
---|
134 | class IfStmt final : public Stmt {
|
---|
135 | public:
|
---|
136 | ptr<Expr> cond;
|
---|
137 | ptr<Stmt> thenPart;
|
---|
138 | ptr<Stmt> elsePart;
|
---|
139 | std::vector<ptr<Stmt>> inits;
|
---|
140 |
|
---|
141 | IfStmt( const CodeLocation & loc, const Expr * cond, const Stmt * thenPart,
|
---|
142 | Stmt * const elsePart, std::vector<ptr<Stmt>> && inits,
|
---|
143 | std::vector<Label> && labels = {} )
|
---|
144 | : Stmt(loc, std::move(labels)), cond(cond), thenPart(thenPart), elsePart(elsePart),
|
---|
145 | inits(std::move(inits)) {}
|
---|
146 |
|
---|
147 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
148 | private:
|
---|
149 | IfStmt * clone() const override { return new IfStmt{ *this }; }
|
---|
150 | MUTATE_FRIEND
|
---|
151 | };
|
---|
152 |
|
---|
153 | class SwitchStmt final : public Stmt {
|
---|
154 | public:
|
---|
155 | ptr<Expr> cond;
|
---|
156 | std::vector<ptr<Stmt>> stmts;
|
---|
157 |
|
---|
158 | SwitchStmt( const CodeLocation & loc, const Expr * cond, std::vector<ptr<Stmt>> && stmts,
|
---|
159 | std::vector<Label> && labels = {} )
|
---|
160 | : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
|
---|
161 |
|
---|
162 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
163 | private:
|
---|
164 | SwitchStmt * clone() const override { return new SwitchStmt{ *this }; }
|
---|
165 | MUTATE_FRIEND
|
---|
166 | };
|
---|
167 |
|
---|
168 | class CaseStmt final : public Stmt {
|
---|
169 | public:
|
---|
170 | ptr<Expr> cond;
|
---|
171 | std::vector<ptr<Stmt>> stmts;
|
---|
172 |
|
---|
173 | CaseStmt( const CodeLocation & loc, const Expr * cond, std::vector<ptr<Stmt>> && stmts,
|
---|
174 | std::vector<Label> && labels = {} )
|
---|
175 | : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
|
---|
176 |
|
---|
177 | bool isDefault() const { return !cond; }
|
---|
178 |
|
---|
179 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
180 | private:
|
---|
181 | CaseStmt * clone() const override { return new CaseStmt{ *this }; }
|
---|
182 | MUTATE_FRIEND
|
---|
183 | };
|
---|
184 |
|
---|
185 | class WhileStmt final : public Stmt {
|
---|
186 | public:
|
---|
187 | ptr<Expr> cond;
|
---|
188 | ptr<Stmt> body;
|
---|
189 | std::vector<ptr<Stmt>> inits;
|
---|
190 | bool isDoWhile;
|
---|
191 |
|
---|
192 | WhileStmt( const CodeLocation & loc, const Expr * cond, const Stmt * body,
|
---|
193 | std::vector<ptr<Stmt>> && inits, bool isDoWhile = false, std::vector<Label> && labels = {} )
|
---|
194 | : Stmt(loc, std::move(labels)), cond(cond), body(body), inits(std::move(inits)),
|
---|
195 | isDoWhile(isDoWhile) {}
|
---|
196 |
|
---|
197 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
198 | private:
|
---|
199 | WhileStmt * clone() const override { return new WhileStmt{ *this }; }
|
---|
200 | MUTATE_FRIEND
|
---|
201 | };
|
---|
202 |
|
---|
203 | class ForStmt final : public Stmt {
|
---|
204 | public:
|
---|
205 | std::vector<ptr<Stmt>> inits;
|
---|
206 | ptr<Expr> cond;
|
---|
207 | ptr<Expr> inc;
|
---|
208 | ptr<Stmt> body;
|
---|
209 |
|
---|
210 | ForStmt( const CodeLocation & loc, std::vector<ptr<Stmt>> && inits, const Expr * cond,
|
---|
211 | const Expr * inc, const Stmt * body, std::vector<Label> && labels = {} )
|
---|
212 | : Stmt(loc, std::move(labels)), inits(std::move(inits)), cond(cond), inc(inc),
|
---|
213 | body(body) {}
|
---|
214 |
|
---|
215 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
216 | private:
|
---|
217 | ForStmt * clone() const override { return new ForStmt{ *this }; }
|
---|
218 | MUTATE_FRIEND
|
---|
219 | };
|
---|
220 |
|
---|
221 | class BranchStmt final : public Stmt {
|
---|
222 | public:
|
---|
223 | enum Kind { Goto, Break, Continue, FallThrough, FallThroughDefault };
|
---|
224 | static constexpr size_t kindEnd = 1 + (size_t)FallThroughDefault;
|
---|
225 |
|
---|
226 | const Label originalTarget;
|
---|
227 | Label target;
|
---|
228 | ptr<Expr> computedTarget;
|
---|
229 | Kind kind;
|
---|
230 |
|
---|
231 | BranchStmt( const CodeLocation & loc, Kind kind, Label target,
|
---|
232 | std::vector<Label> && labels = {} );
|
---|
233 | BranchStmt( const CodeLocation & loc, const Expr * computedTarget,
|
---|
234 | std::vector<Label> && labels = {} )
|
---|
235 | : Stmt(loc, std::move(labels)), originalTarget(loc), target(loc),
|
---|
236 | computedTarget(computedTarget), kind(Goto) {}
|
---|
237 |
|
---|
238 | const char * kindName() { return kindNames[kind]; }
|
---|
239 |
|
---|
240 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
241 | private:
|
---|
242 | BranchStmt * clone() const override { return new BranchStmt{ *this }; }
|
---|
243 | MUTATE_FRIEND
|
---|
244 |
|
---|
245 | static const char * kindNames[kindEnd];
|
---|
246 | };
|
---|
247 |
|
---|
248 | class ReturnStmt final : public Stmt {
|
---|
249 | public:
|
---|
250 | ptr<Expr> expr;
|
---|
251 |
|
---|
252 | ReturnStmt( const CodeLocation & loc, const Expr * expr, std::vector<Label> && labels = {} )
|
---|
253 | : Stmt(loc, std::move(labels)), expr(expr) {}
|
---|
254 |
|
---|
255 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
256 | private:
|
---|
257 | ReturnStmt * clone() const override { return new ReturnStmt{ *this }; }
|
---|
258 | MUTATE_FRIEND
|
---|
259 | };
|
---|
260 |
|
---|
261 | class ThrowStmt final : public Stmt {
|
---|
262 | public:
|
---|
263 | enum Kind { Terminate, Resume };
|
---|
264 |
|
---|
265 | ptr<Expr> expr;
|
---|
266 | ptr<Expr> target;
|
---|
267 | Kind kind;
|
---|
268 |
|
---|
269 | ThrowStmt( const CodeLocation & loc, Kind kind, const Expr * expr, const Expr * target,
|
---|
270 | std::vector<Label> && labels = {} )
|
---|
271 | : Stmt(loc, std::move(labels)), expr(expr), target(target), kind(kind) {}
|
---|
272 |
|
---|
273 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
274 | private:
|
---|
275 | ThrowStmt * clone() const override { return new ThrowStmt{ *this }; }
|
---|
276 | MUTATE_FRIEND
|
---|
277 | };
|
---|
278 |
|
---|
279 | class TryStmt final : public Stmt {
|
---|
280 | public:
|
---|
281 | ptr<CompoundStmt> body;
|
---|
282 | std::vector<ptr<CatchStmt>> handlers;
|
---|
283 | ptr<FinallyStmt> finally;
|
---|
284 |
|
---|
285 | TryStmt( const CodeLocation & loc, const CompoundStmt * body,
|
---|
286 | std::vector<ptr<CatchStmt>> && handlers, const FinallyStmt * finally,
|
---|
287 | std::vector<Label> && labels = {} )
|
---|
288 | : Stmt(loc, std::move(labels)), body(body), handlers(std::move(handlers)), finally(finally) {}
|
---|
289 |
|
---|
290 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
291 | private:
|
---|
292 | TryStmt * clone() const override { return new TryStmt{ *this }; }
|
---|
293 | MUTATE_FRIEND
|
---|
294 | };
|
---|
295 |
|
---|
296 | class CatchStmt final : public Stmt {
|
---|
297 | public:
|
---|
298 | enum Kind { Terminate, Resume };
|
---|
299 |
|
---|
300 | ptr<Decl> decl;
|
---|
301 | ptr<Expr> cond;
|
---|
302 | ptr<Stmt> body;
|
---|
303 | Kind kind;
|
---|
304 |
|
---|
305 | CatchStmt( const CodeLocation & loc, Kind kind, const Decl * decl, const Expr * cond,
|
---|
306 | const Stmt * body, std::vector<Label> && labels = {} )
|
---|
307 | : Stmt(loc, std::move(labels)), decl(decl), cond(cond), body(body), kind(kind) {}
|
---|
308 |
|
---|
309 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
310 | private:
|
---|
311 | CatchStmt * clone() const override { return new CatchStmt{ *this }; }
|
---|
312 | MUTATE_FRIEND
|
---|
313 | };
|
---|
314 |
|
---|
315 | class FinallyStmt final : public Stmt {
|
---|
316 | public:
|
---|
317 | ptr<CompoundStmt> body;
|
---|
318 |
|
---|
319 | FinallyStmt( const CodeLocation & loc, const CompoundStmt * body,
|
---|
320 | std::vector<Label> && labels = {} )
|
---|
321 | : Stmt(loc, std::move(labels)), body(body) {}
|
---|
322 |
|
---|
323 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
324 | private:
|
---|
325 | FinallyStmt * clone() const override { return new FinallyStmt{ *this }; }
|
---|
326 | MUTATE_FRIEND
|
---|
327 | };
|
---|
328 |
|
---|
329 | class WaitForStmt final : public Stmt {
|
---|
330 | public:
|
---|
331 | struct Target {
|
---|
332 | ptr<Expr> func;
|
---|
333 | std::vector<ptr<Expr>> args;
|
---|
334 | };
|
---|
335 |
|
---|
336 | struct Clause {
|
---|
337 | Target target;
|
---|
338 | ptr<Stmt> stmt;
|
---|
339 | ptr<Expr> cond;
|
---|
340 | };
|
---|
341 |
|
---|
342 | struct Timeout {
|
---|
343 | ptr<Expr> time;
|
---|
344 | ptr<Stmt> stmt;
|
---|
345 | ptr<Expr> cond;
|
---|
346 | };
|
---|
347 |
|
---|
348 | struct OrElse {
|
---|
349 | ptr<Stmt> stmt;
|
---|
350 | ptr<Expr> cond;
|
---|
351 | };
|
---|
352 |
|
---|
353 | std::vector<Clause> clauses;
|
---|
354 | Timeout timeout;
|
---|
355 | OrElse orElse;
|
---|
356 |
|
---|
357 | WaitForStmt( const CodeLocation & loc, std::vector<Label> && labels = {} )
|
---|
358 | : Stmt(loc, std::move(labels)) {}
|
---|
359 |
|
---|
360 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
361 | private:
|
---|
362 | WaitForStmt * clone() const override { return new WaitForStmt{ *this }; }
|
---|
363 | MUTATE_FRIEND
|
---|
364 | };
|
---|
365 |
|
---|
366 | class WithStmt final : public Stmt {
|
---|
367 | public:
|
---|
368 | std::vector<ptr<Expr>> exprs;
|
---|
369 | ptr<Stmt> stmt;
|
---|
370 |
|
---|
371 | WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt,
|
---|
372 | std::vector<Label> && labels = {} )
|
---|
373 | : Stmt(loc, std::move(labels)), exprs(std::move(exprs)), stmt(stmt) {}
|
---|
374 |
|
---|
375 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
376 | private:
|
---|
377 | WithStmt * clone() const override { return new WithStmt{ *this }; }
|
---|
378 | MUTATE_FRIEND
|
---|
379 | };
|
---|
380 |
|
---|
381 | class DeclStmt final : public Stmt {
|
---|
382 | public:
|
---|
383 | ptr<Decl> decl;
|
---|
384 |
|
---|
385 | DeclStmt( const CodeLocation & loc, const Decl * decl, std::vector<Label> && labels = {} )
|
---|
386 | : Stmt(loc, std::move(labels)), decl(decl) {}
|
---|
387 |
|
---|
388 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
389 | private:
|
---|
390 | DeclStmt * clone() const override { return new DeclStmt{ *this }; }
|
---|
391 | MUTATE_FRIEND
|
---|
392 | };
|
---|
393 |
|
---|
394 | class ImplicitCtorDtorStmt final : public Stmt {
|
---|
395 | public:
|
---|
396 | readonly<Stmt> callStmt;
|
---|
397 |
|
---|
398 | ImplicitCtorDtorStmt( const CodeLocation & loc, const Stmt * callStmt,
|
---|
399 | std::vector<Label> && labels = {} )
|
---|
400 | : Stmt(loc, std::move(labels)), callStmt(callStmt) {}
|
---|
401 |
|
---|
402 | const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
403 | private:
|
---|
404 | ImplicitCtorDtorStmt * clone() const override { return new ImplicitCtorDtorStmt{ *this }; }
|
---|
405 | MUTATE_FRIEND
|
---|
406 | };
|
---|
407 |
|
---|
408 | }
|
---|
409 |
|
---|
410 | #undef MUTATE_FRIEND
|
---|
411 |
|
---|
412 | // Local Variables: //
|
---|
413 | // tab-width: 4 //
|
---|
414 | // mode: c++ //
|
---|
415 | // compile-command: "make install" //
|
---|
416 | // End: //
|
---|