source: src/AST/Stmt.hpp@ 8ff178d

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 stuck-waitfor-destruct
Last change on this file since 8ff178d was 6f8e87d, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Filled in ConverterOldToNew for Stmt. Also updated some utilities.

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