source: src/AST/Stmt.hpp@ ee918356

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since ee918356 was 6180274, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

more cleanup, make more function parameters const, remove more std::

  • Property mode set to 100644
File size: 14.3 KB
RevLine 
[2bb4a01]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
[1e97287]10// Created On : Wed May 8 13:00:00 2019
[89a5a1f]11// Last Modified By : Peter A. Buhr
[6180274]12// Last Modified On : Wed Feb 2 20:06:41 2022
13// Update Count : 34
[2bb4a01]14//
15
16#pragma once
17
18#include <list>
[89a5a1f]19#include <utility> // for move
[2bb4a01]20#include <vector>
21
22#include "Label.hpp"
[89a5a1f]23#include "Node.hpp" // for node, ptr
[2bb4a01]24#include "ParseNode.hpp"
25#include "Visitor.hpp"
26#include "Common/CodeLocation.h"
27
[f3cc5b6]28// Must be included in *all* AST classes; should be #undef'd at the end of the file
[89a5a1f]29#define MUTATE_FRIEND \
[99da267]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);
[f3cc5b6]32
[2bb4a01]33namespace ast {
34class Expr;
35
[89a5a1f]36// Base statement node
[2bb4a01]37class Stmt : public ParseNode {
[89a5a1f]38 public:
[2bb4a01]39 std::vector<Label> labels;
40
[6180274]41 Stmt( const CodeLocation & loc, const std::vector<Label> && labels = {} )
[89a5a1f]42 : ParseNode(loc), labels(std::move(labels)) {}
[2bb4a01]43
[3b0bc16]44 Stmt(const Stmt & o) : ParseNode(o), labels(o.labels) {}
[2bb4a01]45
[f3cc5b6]46 const Stmt * accept( Visitor & v ) const override = 0;
[89a5a1f]47 private:
[f3cc5b6]48 Stmt * clone() const override = 0;
49 MUTATE_FRIEND
[2bb4a01]50};
51
[89a5a1f]52// Compound statement: { ... }
[2bb4a01]53class CompoundStmt final : public Stmt {
[89a5a1f]54 public:
[2bb4a01]55 std::list<ptr<Stmt>> kids;
56
[6180274]57 CompoundStmt(const CodeLocation & loc, const std::list<ptr<Stmt>> && ks = {}, const std::vector<Label> && labels = {} )
[89a5a1f]58 : Stmt(loc, std::move(labels)), kids(std::move(ks)) {}
[2bb4a01]59
[3b0bc16]60 CompoundStmt( const CompoundStmt & o );
61 CompoundStmt( CompoundStmt && o ) = default;
[2bb4a01]62
[b8524ca]63 void push_back( const Stmt * s ) { kids.emplace_back( s ); }
64 void push_front( const Stmt * s ) { kids.emplace_front( s ); }
[2bb4a01]65
[f3cc5b6]66 const CompoundStmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]67 private:
[f3cc5b6]68 CompoundStmt * clone() const override { return new CompoundStmt{ *this }; }
69 MUTATE_FRIEND
[2bb4a01]70};
71
[89a5a1f]72// Empty statment: ;
[2bb4a01]73class NullStmt final : public Stmt {
[89a5a1f]74 public:
[6180274]75 NullStmt( const CodeLocation & loc, const std::vector<Label> && labels = {} )
[89a5a1f]76 : Stmt(loc, std::move(labels)) {}
[2bb4a01]77
[f3cc5b6]78 const NullStmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]79 private:
[f3cc5b6]80 NullStmt * clone() const override { return new NullStmt{ *this }; }
81 MUTATE_FRIEND
[2bb4a01]82};
83
[89a5a1f]84// Expression wrapped by statement
[2bb4a01]85class ExprStmt final : public Stmt {
[89a5a1f]86 public:
[2bb4a01]87 ptr<Expr> expr;
88
[6180274]89 ExprStmt( const CodeLocation & loc, const Expr* e, const std::vector<Label> && labels = {} )
[89a5a1f]90 : Stmt(loc, std::move(labels)), expr(e) {}
[2bb4a01]91
[f3cc5b6]92 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]93 private:
[23f99e1]94 ExprStmt * clone() const override { return new ExprStmt{ *this }; }
[f3cc5b6]95 MUTATE_FRIEND
[2bb4a01]96};
97
[89a5a1f]98// Assembly statement: asm ... ( "..." : ... )
[1e97287]99class AsmStmt final : public Stmt {
[89a5a1f]100 public:
[1e97287]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
[f3cc5b6]107 AsmStmt( const CodeLocation & loc, bool isVolatile, const Expr * instruction,
[6180274]108 const std::vector<ptr<Expr>> && output, const std::vector<ptr<Expr>> && input,
109 const std::vector<ptr<ConstantExpr>> && clobber, const std::vector<Label> && gotoLabels,
110 const std::vector<Label> && labels = {})
[89a5a1f]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)) {}
[1e97287]114
[f3cc5b6]115 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]116 private:
[f3cc5b6]117 AsmStmt * clone() const override { return new AsmStmt{ *this }; }
118 MUTATE_FRIEND
[1e97287]119};
120
[89a5a1f]121// C-preprocessor directive: #...
[1e97287]122class DirectiveStmt final : public Stmt {
[89a5a1f]123 public:
[1e97287]124 std::string directive;
125
[f3cc5b6]126 DirectiveStmt( const CodeLocation & loc, const std::string & directive,
[89a5a1f]127 std::vector<Label> && labels = {} )
128 : Stmt(loc, std::move(labels)), directive(directive) {}
[1e97287]129
[f3cc5b6]130 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]131 private:
[f3cc5b6]132 DirectiveStmt * clone() const override { return new DirectiveStmt{ *this }; }
133 MUTATE_FRIEND
[1e97287]134};
135
[89a5a1f]136// If statement: if (...) ... else ...
[1e97287]137class IfStmt final : public Stmt {
[89a5a1f]138 public:
[1e97287]139 ptr<Expr> cond;
[3b0bc16]140 ptr<Stmt> then;
141 ptr<Stmt> else_;
[1e97287]142 std::vector<ptr<Stmt>> inits;
143
[3b0bc16]144 IfStmt( const CodeLocation & loc, const Expr * cond, const Stmt * then,
[6180274]145 const Stmt * else_ = nullptr, const std::vector<ptr<Stmt>> && inits = {},
146 const std::vector<Label> && labels = {} )
[3b0bc16]147 : Stmt(loc, std::move(labels)), cond(cond), then(then), else_(else_),
[89a5a1f]148 inits(std::move(inits)) {}
[1e97287]149
[f3cc5b6]150 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]151 private:
[f3cc5b6]152 IfStmt * clone() const override { return new IfStmt{ *this }; }
153 MUTATE_FRIEND
[1e97287]154};
155
[89a5a1f]156// Switch or choose statement: switch (...) { ... }
[1e97287]157class SwitchStmt final : public Stmt {
[89a5a1f]158 public:
[1e97287]159 ptr<Expr> cond;
160 std::vector<ptr<Stmt>> stmts;
161
[6180274]162 SwitchStmt( const CodeLocation & loc, const Expr * cond, const std::vector<ptr<Stmt>> && stmts,
163 const std::vector<Label> && labels = {} )
[89a5a1f]164 : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
[1e97287]165
[f3cc5b6]166 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]167 private:
[f3cc5b6]168 SwitchStmt * clone() const override { return new SwitchStmt{ *this }; }
169 MUTATE_FRIEND
[1e97287]170};
171
[89a5a1f]172// Case label: case ...: or default:
[1e97287]173class CaseStmt final : public Stmt {
[89a5a1f]174 public:
175 // Null for the default label.
[1e97287]176 ptr<Expr> cond;
177 std::vector<ptr<Stmt>> stmts;
178
[6180274]179 CaseStmt( const CodeLocation & loc, const Expr * cond, const std::vector<ptr<Stmt>> && stmts,
180 const std::vector<Label> && labels = {} )
[89a5a1f]181 : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {}
[1e97287]182
[675d816]183 bool isDefault() const { return !cond; }
[1e97287]184
[f3cc5b6]185 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]186 private:
[f3cc5b6]187 CaseStmt * clone() const override { return new CaseStmt{ *this }; }
188 MUTATE_FRIEND
[1e97287]189};
190
[89a5a1f]191// While loop: while (...) ... else ... or do ... while (...) else ...;
[3b0bc16]192class WhileDoStmt final : public Stmt {
[89a5a1f]193 public:
[1e97287]194 ptr<Expr> cond;
195 ptr<Stmt> body;
[3b0bc16]196 ptr<Stmt> else_;
[1e97287]197 std::vector<ptr<Stmt>> inits;
198 bool isDoWhile;
199
[3b0bc16]200 WhileDoStmt( const CodeLocation & loc, const Expr * cond, const Stmt * body,
[6180274]201 const std::vector<ptr<Stmt>> && inits, bool isDoWhile = false, const std::vector<Label> && labels = {} )
[3b0bc16]202 : Stmt(loc, std::move(labels)), cond(cond), body(body), else_(nullptr), inits(std::move(inits)), isDoWhile(isDoWhile) {}
203
204 WhileDoStmt( const CodeLocation & loc, const Expr * cond, const Stmt * body, const Stmt * else_,
[6180274]205 const std::vector<ptr<Stmt>> && inits, bool isDoWhile = false, const std::vector<Label> && labels = {} )
[3b0bc16]206 : Stmt(loc, std::move(labels)), cond(cond), body(body), else_(else_), inits(std::move(inits)), isDoWhile(isDoWhile) {}
[1e97287]207
[f3cc5b6]208 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]209 private:
[3b0bc16]210 WhileDoStmt * clone() const override { return new WhileDoStmt{ *this }; }
[f3cc5b6]211 MUTATE_FRIEND
[1e97287]212};
213
[89a5a1f]214// For loop: for (... ; ... ; ...) ... else ...
[1e97287]215class ForStmt final : public Stmt {
[89a5a1f]216 public:
[1e97287]217 std::vector<ptr<Stmt>> inits;
218 ptr<Expr> cond;
[8a5530c]219 ptr<Expr> inc;
[1e97287]220 ptr<Stmt> body;
[3b0bc16]221 ptr<Stmt> else_;
222
[6180274]223 ForStmt( const CodeLocation & loc, const std::vector<ptr<Stmt>> && inits, const Expr * cond,
224 const Expr * inc, const Stmt * body, const std::vector<Label> && label = {} )
[3b0bc16]225 : Stmt(loc, std::move(label)), inits(std::move(inits)), cond(cond), inc(inc), body(body), else_(nullptr) {}
[1e97287]226
[6180274]227 ForStmt( const CodeLocation & loc, const std::vector<ptr<Stmt>> && inits, const Expr * cond,
228 const Expr * inc, const Stmt * body, const Stmt * else_, const std::vector<Label> && labels = {} )
[3b0bc16]229 : Stmt(loc, std::move(labels)), inits(std::move(inits)), cond(cond), inc(inc), body(body), else_(else_) {}
[1e97287]230
[f3cc5b6]231 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]232 private:
[f3cc5b6]233 ForStmt * clone() const override { return new ForStmt{ *this }; }
234 MUTATE_FRIEND
[1e97287]235};
236
[89a5a1f]237// Branch control flow statement: goto ... or break or continue or fallthru
[1e97287]238class BranchStmt final : public Stmt {
[89a5a1f]239 public:
[1e97287]240 enum Kind { Goto, Break, Continue, FallThrough, FallThroughDefault };
241 static constexpr size_t kindEnd = 1 + (size_t)FallThroughDefault;
242
243 const Label originalTarget;
244 Label target;
245 ptr<Expr> computedTarget;
246 Kind kind;
247
[6180274]248 BranchStmt( const CodeLocation & loc, Kind kind, Label target, const std::vector<Label> && labels = {} );
249 BranchStmt( const CodeLocation & loc, const Expr * computedTarget, const std::vector<Label> && labels = {} )
250 : Stmt(loc, std::move(labels)), originalTarget(loc), target(loc), computedTarget(computedTarget), kind(Goto) {}
[1e97287]251
[94b1f718]252 const char * kindName() const { return kindNames[kind]; }
[1e97287]253
[f3cc5b6]254 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]255 private:
[f3cc5b6]256 BranchStmt * clone() const override { return new BranchStmt{ *this }; }
257 MUTATE_FRIEND
[8a5530c]258
[1e97287]259 static const char * kindNames[kindEnd];
260};
261
[89a5a1f]262// Return statement: return ...
[1e97287]263class ReturnStmt final : public Stmt {
[89a5a1f]264 public:
[1e97287]265 ptr<Expr> expr;
266
[6180274]267 ReturnStmt( const CodeLocation & loc, const Expr * expr, const std::vector<Label> && labels = {} )
[89a5a1f]268 : Stmt(loc, std::move(labels)), expr(expr) {}
[1e97287]269
[f3cc5b6]270 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]271 private:
[f3cc5b6]272 ReturnStmt * clone() const override { return new ReturnStmt{ *this }; }
273 MUTATE_FRIEND
[1e97287]274};
275
[89a5a1f]276// Kind of exception
[6f4b7f2]277enum ExceptionKind { Terminate, Resume };
278
[89a5a1f]279// Throw statement: throw ...
[1e97287]280class ThrowStmt final : public Stmt {
[89a5a1f]281 public:
[1e97287]282 ptr<Expr> expr;
283 ptr<Expr> target;
[6f4b7f2]284 ExceptionKind kind;
[1e97287]285
[6180274]286 ThrowStmt( const CodeLocation & loc, ExceptionKind kind, const Expr * expr,
287 const Expr * target, const std::vector<Label> && labels = {} )
[89a5a1f]288 : Stmt(loc, std::move(labels)), expr(expr), target(target), kind(kind) {}
[1e97287]289
[f3cc5b6]290 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]291 private:
[f3cc5b6]292 ThrowStmt * clone() const override { return new ThrowStmt{ *this }; }
293 MUTATE_FRIEND
[1e97287]294};
295
[89a5a1f]296// Try statement: try { ... } ...
[1e97287]297class TryStmt final : public Stmt {
[89a5a1f]298 public:
[1e97287]299 ptr<CompoundStmt> body;
300 std::vector<ptr<CatchStmt>> handlers;
301 ptr<FinallyStmt> finally;
302
[6180274]303 TryStmt( const CodeLocation & loc, const CompoundStmt * body,
304 const std::vector<ptr<CatchStmt>> && handlers, const FinallyStmt * finally,
305 const std::vector<Label> && labels = {} )
[89a5a1f]306 : Stmt(loc, std::move(labels)), body(body), handlers(std::move(handlers)), finally(finally) {}
[1e97287]307
[f3cc5b6]308 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]309 private:
[f3cc5b6]310 TryStmt * clone() const override { return new TryStmt{ *this }; }
311 MUTATE_FRIEND
[1e97287]312};
313
[89a5a1f]314// Catch clause of try statement
[1e97287]315class CatchStmt final : public Stmt {
[89a5a1f]316 public:
[1e97287]317 ptr<Decl> decl;
318 ptr<Expr> cond;
319 ptr<Stmt> body;
[6f4b7f2]320 ExceptionKind kind;
[1e97287]321
[6180274]322 CatchStmt( const CodeLocation & loc, ExceptionKind kind, const Decl * decl, const Expr * cond,
323 const Stmt * body, const std::vector<Label> && labels = {} )
[89a5a1f]324 : Stmt(loc, std::move(labels)), decl(decl), cond(cond), body(body), kind(kind) {}
[1e97287]325
[f3cc5b6]326 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]327 private:
[f3cc5b6]328 CatchStmt * clone() const override { return new CatchStmt{ *this }; }
329 MUTATE_FRIEND
[1e97287]330};
331
[89a5a1f]332// Finally clause of try statement
[1e97287]333class FinallyStmt final : public Stmt {
[89a5a1f]334 public:
[1e97287]335 ptr<CompoundStmt> body;
336
[f3cc5b6]337 FinallyStmt( const CodeLocation & loc, const CompoundStmt * body,
[89a5a1f]338 std::vector<Label> && labels = {} )
339 : Stmt(loc, std::move(labels)), body(body) {}
[1e97287]340
[f3cc5b6]341 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]342 private:
[f3cc5b6]343 FinallyStmt * clone() const override { return new FinallyStmt{ *this }; }
344 MUTATE_FRIEND
[1e97287]345};
[2bb4a01]346
[89a5a1f]347// Suspend statement
[37cdd97]348class SuspendStmt final : public Stmt {
[89a5a1f]349 public:
[37cdd97]350 ptr<CompoundStmt> then;
351 enum Type { None, Coroutine, Generator } type = None;
352
[6180274]353 SuspendStmt( const CodeLocation & loc, const CompoundStmt * then, Type type, const std::vector<Label> && labels = {} )
[89a5a1f]354 : Stmt(loc, std::move(labels)), then(then), type(type) {}
[37cdd97]355
356 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]357 private:
[37cdd97]358 SuspendStmt * clone() const override { return new SuspendStmt{ *this }; }
359 MUTATE_FRIEND
360};
361
[89a5a1f]362// Waitfor statement: when (...) waitfor (... , ...) ... timeout(...) ... else ...
[1e97287]363class WaitForStmt final : public Stmt {
[89a5a1f]364 public:
[1e97287]365 struct Target {
[e0016a5]366 ptr<Expr> func;
367 std::vector<ptr<Expr>> args;
[1e97287]368 };
369
370 struct Clause {
371 Target target;
372 ptr<Stmt> stmt;
373 ptr<Expr> cond;
374 };
375
376 struct Timeout {
377 ptr<Expr> time;
378 ptr<Stmt> stmt;
379 ptr<Expr> cond;
380 };
381
382 struct OrElse {
383 ptr<Stmt> stmt;
384 ptr<Expr> cond;
385 };
386
387 std::vector<Clause> clauses;
388 Timeout timeout;
389 OrElse orElse;
390
[6180274]391 WaitForStmt( const CodeLocation & loc, const std::vector<Label> && labels = {} )
[89a5a1f]392 : Stmt(loc, std::move(labels)) {}
[1e97287]393
[f3cc5b6]394 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]395 private:
[f3cc5b6]396 WaitForStmt * clone() const override { return new WaitForStmt{ *this }; }
397 MUTATE_FRIEND
[1e97287]398};
399
[89a5a1f]400// Any declaration in a (compound) statement.
[1e97287]401class DeclStmt final : public Stmt {
[89a5a1f]402 public:
[1e97287]403 ptr<Decl> decl;
404
[6180274]405 DeclStmt( const CodeLocation & loc, const Decl * decl, const std::vector<Label> && labels = {} )
[89a5a1f]406 : Stmt(loc, std::move(labels)), decl(decl) {}
[1e97287]407
[f3cc5b6]408 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]409 private:
[f3cc5b6]410 DeclStmt * clone() const override { return new DeclStmt{ *this }; }
411 MUTATE_FRIEND
[1e97287]412};
413
[89a5a1f]414// Represents an implicit application of a constructor or destructor.
[1e97287]415class ImplicitCtorDtorStmt final : public Stmt {
[89a5a1f]416 public:
[c570806]417 ptr<Stmt> callStmt;
[1e97287]418
[f3cc5b6]419 ImplicitCtorDtorStmt( const CodeLocation & loc, const Stmt * callStmt,
[89a5a1f]420 std::vector<Label> && labels = {} )
421 : Stmt(loc, std::move(labels)), callStmt(callStmt) {}
[1e97287]422
[f3cc5b6]423 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]424 private:
[f3cc5b6]425 ImplicitCtorDtorStmt * clone() const override { return new ImplicitCtorDtorStmt{ *this }; }
426 MUTATE_FRIEND
[1e97287]427};
[2bb4a01]428
[89a5a1f]429// Mutex Statement
[6cebfef]430class MutexStmt final : public Stmt {
[89a5a1f]431 public:
[6cebfef]432 ptr<Stmt> stmt;
433 std::vector<ptr<Expr>> mutexObjs;
434
435 MutexStmt( const CodeLocation & loc, const Stmt * stmt,
[6180274]436 const std::vector<ptr<Expr>> && mutexes, const std::vector<Label> && labels = {} )
[89a5a1f]437 : Stmt(loc, std::move(labels)), stmt(stmt), mutexObjs(std::move(mutexes)) {}
[6cebfef]438
439 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]440 private:
[6cebfef]441 MutexStmt * clone() const override { return new MutexStmt{ *this }; }
442 MUTATE_FRIEND
443};
[89a5a1f]444} // namespace ast
[2bb4a01]445
[f3cc5b6]446#undef MUTATE_FRIEND
447
[2bb4a01]448// Local Variables: //
449// mode: c++ //
[1e97287]450// End: //
Note: See TracBrowser for help on using the repository browser.