source: src/AST/Stmt.hpp@ ab1a9ea

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

formatting, starting adding elsePart to while, do, and for statements

  • Property mode set to 100644
File size: 13.4 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
12// Last Modified On : Mon Jan 31 22:38:53 2022
13// Update Count : 12
[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
44 Stmt(const Stmt& o) : ParseNode(o), labels(o.labels) {}
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 = {},
[89a5a1f]58 std::vector<Label>&& labels = {} )
59 : Stmt(loc, std::move(labels)), kids(std::move(ks)) {}
[2bb4a01]60
61 CompoundStmt( const CompoundStmt& o );
62 CompoundStmt( CompoundStmt&& o ) = default;
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
[6f8e87d]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;
141 ptr<Stmt> thenPart;
142 ptr<Stmt> elsePart;
143 std::vector<ptr<Stmt>> inits;
144
[f3cc5b6]145 IfStmt( const CodeLocation & loc, const Expr * cond, const Stmt * thenPart,
[89a5a1f]146 const Stmt * elsePart = nullptr, std::vector<ptr<Stmt>> && inits = {},
147 std::vector<Label> && labels = {} )
148 : Stmt(loc, std::move(labels)), cond(cond), thenPart(thenPart), elsePart(elsePart),
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 ...;
[1e97287]193class WhileStmt final : public Stmt {
[89a5a1f]194 public:
[1e97287]195 ptr<Expr> cond;
196 ptr<Stmt> body;
[89a5a1f]197 ptr<Stmt> elsePart;
[1e97287]198 std::vector<ptr<Stmt>> inits;
199 bool isDoWhile;
200
[f3cc5b6]201 WhileStmt( const CodeLocation & loc, const Expr * cond, const Stmt * body,
[89a5a1f]202 std::vector<ptr<Stmt>> && inits, bool isDoWhile = false, std::vector<Label> && labels = {} )
203 : Stmt(loc, std::move(labels)), cond(cond), body(body), inits(std::move(inits)), isDoWhile(isDoWhile) {}
[1e97287]204
[f3cc5b6]205 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]206 private:
[f3cc5b6]207 WhileStmt * clone() const override { return new WhileStmt{ *this }; }
208 MUTATE_FRIEND
[1e97287]209};
210
[89a5a1f]211// For loop: for (... ; ... ; ...) ... else ...
[1e97287]212class ForStmt final : public Stmt {
[89a5a1f]213 public:
[1e97287]214 std::vector<ptr<Stmt>> inits;
215 ptr<Expr> cond;
[8a5530c]216 ptr<Expr> inc;
[1e97287]217 ptr<Stmt> body;
[89a5a1f]218 ptr<Stmt> elsePart;
[1e97287]219
[f3cc5b6]220 ForStmt( const CodeLocation & loc, std::vector<ptr<Stmt>> && inits, const Expr * cond,
[89a5a1f]221 const Expr * inc, const Stmt * body, std::vector<Label> && labels = {} )
222 : Stmt(loc, std::move(labels)), inits(std::move(inits)), cond(cond), inc(inc), body(body) {}
[1e97287]223
[f3cc5b6]224 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]225 private:
[f3cc5b6]226 ForStmt * clone() const override { return new ForStmt{ *this }; }
227 MUTATE_FRIEND
[1e97287]228};
229
[89a5a1f]230// Branch control flow statement: goto ... or break or continue or fallthru
[1e97287]231class BranchStmt final : public Stmt {
[89a5a1f]232 public:
[1e97287]233 enum Kind { Goto, Break, Continue, FallThrough, FallThroughDefault };
234 static constexpr size_t kindEnd = 1 + (size_t)FallThroughDefault;
235
236 const Label originalTarget;
237 Label target;
238 ptr<Expr> computedTarget;
239 Kind kind;
240
[f3cc5b6]241 BranchStmt( const CodeLocation & loc, Kind kind, Label target,
[89a5a1f]242 std::vector<Label> && labels = {} );
[f3cc5b6]243 BranchStmt( const CodeLocation & loc, const Expr * computedTarget,
[89a5a1f]244 std::vector<Label> && labels = {} )
245 : Stmt(loc, std::move(labels)), originalTarget(loc), target(loc),
246 computedTarget(computedTarget), kind(Goto) {}
[1e97287]247
[94b1f718]248 const char * kindName() const { return kindNames[kind]; }
[1e97287]249
[f3cc5b6]250 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]251 private:
[f3cc5b6]252 BranchStmt * clone() const override { return new BranchStmt{ *this }; }
253 MUTATE_FRIEND
[8a5530c]254
[1e97287]255 static const char * kindNames[kindEnd];
256};
257
[89a5a1f]258// Return statement: return ...
[1e97287]259class ReturnStmt final : public Stmt {
[89a5a1f]260 public:
[1e97287]261 ptr<Expr> expr;
262
[f3cc5b6]263 ReturnStmt( const CodeLocation & loc, const Expr * expr, std::vector<Label> && labels = {} )
[89a5a1f]264 : Stmt(loc, std::move(labels)), expr(expr) {}
[1e97287]265
[f3cc5b6]266 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]267 private:
[f3cc5b6]268 ReturnStmt * clone() const override { return new ReturnStmt{ *this }; }
269 MUTATE_FRIEND
[1e97287]270};
271
[89a5a1f]272// Kind of exception
[6f4b7f2]273enum ExceptionKind { Terminate, Resume };
274
[89a5a1f]275// Throw statement: throw ...
[1e97287]276class ThrowStmt final : public Stmt {
[89a5a1f]277 public:
[1e97287]278 ptr<Expr> expr;
279 ptr<Expr> target;
[6f4b7f2]280 ExceptionKind kind;
[1e97287]281
[a7d50b6]282 ThrowStmt(
[6f4b7f2]283 const CodeLocation & loc, ExceptionKind kind, const Expr * expr, const Expr * target,
[f3cc5b6]284 std::vector<Label> && labels = {} )
[89a5a1f]285 : Stmt(loc, std::move(labels)), expr(expr), target(target), kind(kind) {}
[1e97287]286
[f3cc5b6]287 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]288 private:
[f3cc5b6]289 ThrowStmt * clone() const override { return new ThrowStmt{ *this }; }
290 MUTATE_FRIEND
[1e97287]291};
292
[89a5a1f]293// Try statement: try { ... } ...
[1e97287]294class TryStmt final : public Stmt {
[89a5a1f]295 public:
[1e97287]296 ptr<CompoundStmt> body;
297 std::vector<ptr<CatchStmt>> handlers;
298 ptr<FinallyStmt> finally;
299
[a7d50b6]300 TryStmt(
[6f4b7f2]301 const CodeLocation & loc, const CompoundStmt * body,
[f3cc5b6]302 std::vector<ptr<CatchStmt>> && handlers, const FinallyStmt * finally,
303 std::vector<Label> && labels = {} )
[89a5a1f]304 : Stmt(loc, std::move(labels)), body(body), handlers(std::move(handlers)), finally(finally) {}
[1e97287]305
[f3cc5b6]306 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]307 private:
[f3cc5b6]308 TryStmt * clone() const override { return new TryStmt{ *this }; }
309 MUTATE_FRIEND
[1e97287]310};
311
[89a5a1f]312// Catch clause of try statement
[1e97287]313class CatchStmt final : public Stmt {
[89a5a1f]314 public:
[1e97287]315 ptr<Decl> decl;
316 ptr<Expr> cond;
317 ptr<Stmt> body;
[6f4b7f2]318 ExceptionKind kind;
[1e97287]319
[a7d50b6]320 CatchStmt(
[6f4b7f2]321 const CodeLocation & loc, ExceptionKind kind, const Decl * decl, const Expr * cond,
[f3cc5b6]322 const Stmt * body, std::vector<Label> && labels = {} )
[89a5a1f]323 : Stmt(loc, std::move(labels)), decl(decl), cond(cond), body(body), kind(kind) {}
[1e97287]324
[f3cc5b6]325 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]326 private:
[f3cc5b6]327 CatchStmt * clone() const override { return new CatchStmt{ *this }; }
328 MUTATE_FRIEND
[1e97287]329};
330
[89a5a1f]331// Finally clause of try statement
[1e97287]332class FinallyStmt final : public Stmt {
[89a5a1f]333 public:
[1e97287]334 ptr<CompoundStmt> body;
335
[f3cc5b6]336 FinallyStmt( const CodeLocation & loc, const CompoundStmt * body,
[89a5a1f]337 std::vector<Label> && labels = {} )
338 : Stmt(loc, std::move(labels)), body(body) {}
[1e97287]339
[f3cc5b6]340 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]341 private:
[f3cc5b6]342 FinallyStmt * clone() const override { return new FinallyStmt{ *this }; }
343 MUTATE_FRIEND
[1e97287]344};
[2bb4a01]345
[89a5a1f]346// Suspend statement
[37cdd97]347class SuspendStmt final : public Stmt {
[89a5a1f]348 public:
[37cdd97]349 ptr<CompoundStmt> then;
350 enum Type { None, Coroutine, Generator } type = None;
351
352 SuspendStmt( const CodeLocation & loc, const CompoundStmt * then, Type type, std::vector<Label> && labels = {} )
[89a5a1f]353 : Stmt(loc, std::move(labels)), then(then), type(type) {}
[37cdd97]354
355 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]356 private:
[37cdd97]357 SuspendStmt * clone() const override { return new SuspendStmt{ *this }; }
358 MUTATE_FRIEND
359};
360
[89a5a1f]361// Waitfor statement: when (...) waitfor (... , ...) ... timeout(...) ... else ...
[1e97287]362class WaitForStmt final : public Stmt {
[89a5a1f]363 public:
[1e97287]364 struct Target {
[e0016a5]365 ptr<Expr> func;
366 std::vector<ptr<Expr>> args;
[1e97287]367 };
368
369 struct Clause {
370 Target target;
371 ptr<Stmt> stmt;
372 ptr<Expr> cond;
373 };
374
375 struct Timeout {
376 ptr<Expr> time;
377 ptr<Stmt> stmt;
378 ptr<Expr> cond;
379 };
380
381 struct OrElse {
382 ptr<Stmt> stmt;
383 ptr<Expr> cond;
384 };
385
386 std::vector<Clause> clauses;
387 Timeout timeout;
388 OrElse orElse;
389
[f3cc5b6]390 WaitForStmt( const CodeLocation & loc, std::vector<Label> && labels = {} )
[89a5a1f]391 : Stmt(loc, std::move(labels)) {}
[1e97287]392
[f3cc5b6]393 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]394 private:
[f3cc5b6]395 WaitForStmt * clone() const override { return new WaitForStmt{ *this }; }
396 MUTATE_FRIEND
[1e97287]397};
398
[89a5a1f]399// Any declaration in a (compound) statement.
[1e97287]400class DeclStmt final : public Stmt {
[89a5a1f]401 public:
[1e97287]402 ptr<Decl> decl;
403
[f3cc5b6]404 DeclStmt( const CodeLocation & loc, const Decl * decl, std::vector<Label> && labels = {} )
[89a5a1f]405 : Stmt(loc, std::move(labels)), decl(decl) {}
[1e97287]406
[f3cc5b6]407 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]408 private:
[f3cc5b6]409 DeclStmt * clone() const override { return new DeclStmt{ *this }; }
410 MUTATE_FRIEND
[1e97287]411};
412
[89a5a1f]413// Represents an implicit application of a constructor or destructor.
[1e97287]414class ImplicitCtorDtorStmt final : public Stmt {
[89a5a1f]415 public:
[c570806]416 ptr<Stmt> callStmt;
[1e97287]417
[f3cc5b6]418 ImplicitCtorDtorStmt( const CodeLocation & loc, const Stmt * callStmt,
[89a5a1f]419 std::vector<Label> && labels = {} )
420 : Stmt(loc, std::move(labels)), callStmt(callStmt) {}
[1e97287]421
[f3cc5b6]422 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]423 private:
[f3cc5b6]424 ImplicitCtorDtorStmt * clone() const override { return new ImplicitCtorDtorStmt{ *this }; }
425 MUTATE_FRIEND
[1e97287]426};
[2bb4a01]427
[89a5a1f]428// Mutex Statement
[6cebfef]429class MutexStmt final : public Stmt {
[89a5a1f]430 public:
[6cebfef]431 ptr<Stmt> stmt;
432 std::vector<ptr<Expr>> mutexObjs;
433
434 MutexStmt( const CodeLocation & loc, const Stmt * stmt,
[89a5a1f]435 std::vector<ptr<Expr>> && mutexes, std::vector<Label> && labels = {} )
436 : Stmt(loc, std::move(labels)), stmt(stmt), mutexObjs(std::move(mutexes)) {}
[6cebfef]437
438 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
[89a5a1f]439 private:
[6cebfef]440 MutexStmt * clone() const override { return new MutexStmt{ *this }; }
441 MUTATE_FRIEND
442};
[89a5a1f]443} // namespace ast
[2bb4a01]444
[f3cc5b6]445#undef MUTATE_FRIEND
446
[2bb4a01]447// Local Variables: //
448// mode: c++ //
[1e97287]449// End: //
Note: See TracBrowser for help on using the repository browser.