source: src/AST/Stmt.hpp@ fde0a58

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

change class name WhileStmt to WhileDoStmt, add else clause to WhileDoStmt and ForStmt, change names thenPart/ElsePart to then/else_

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