Spicy
while.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <utility>
6 
7 #include <hilti/ast/declarations/local-variable.h>
8 #include <hilti/ast/expression.h>
9 #include <hilti/ast/statement.h>
10 #include <hilti/base/logger.h>
11 
12 namespace hilti {
13 namespace statement {
14 
16 class While : public NodeBase, public hilti::trait::isStatement {
17 public:
18  While(const hilti::Declaration& init, std::optional<hilti::Expression> cond, Statement body,
19  std::optional<Statement> else_ = {}, Meta m = Meta())
20  : NodeBase(nodes(init, std::move(cond), std::move(body), std::move(else_)), std::move(m)) {
21  if ( ! init.isA<declaration::LocalVariable>() )
22  logger().internalError("initialization for 'while' must be a local declaration");
23  }
24 
25  While(hilti::Expression cond, Statement body, Meta m = Meta())
26  : NodeBase(nodes(node::none, std::move(cond), std::move(body), node::none), std::move(m)) {}
27 
28  While(hilti::Expression cond, Statement body, std::optional<Statement> else_, Meta m = Meta())
29  : NodeBase(nodes(node::none, std::move(cond), std::move(body), std::move(else_)), std::move(m)) {}
30 
31  auto init() const { return childs()[0].tryReferenceAs<hilti::Declaration>(); }
32  auto condition() const { return childs()[1].tryReferenceAs<hilti::Expression>(); }
33  const auto& body() const { return child<hilti::Statement>(2); }
34  auto else_() const { return childs()[3].tryReferenceAs<Statement>(); }
35 
36  bool operator==(const While& other) const {
37  return init() == other.init() && condition() == other.condition() && body() == other.body() &&
38  else_() == other.else_();
39  }
40 
42  auto& _bodyNode() { return childs()[2]; }
43 
45  auto& _elseNode() { return childs()[3]; }
46 
48  auto isEqual(const Statement& other) const { return node::isEqual(this, other); }
49 
51  auto properties() const { return node::Properties{}; }
52 
60  static Statement setInit(const While& e, const hilti::Declaration& d) {
61  auto x = Statement(e)._clone().as<While>();
62  x.childs()[0] = d;
63  return x;
64  }
65 
73  static Statement setCondition(const While& e, const hilti::Expression& c) {
74  auto x = Statement(e)._clone().as<While>();
75  x.childs()[1] = c;
76  return x;
77  }
78 };
79 
80 } // namespace statement
81 } // namespace hilti
Definition: local-variable.h:19
auto & childs() const
Definition: node.h:445
std::vector< T > childs(int begin, int end) const
Definition: node.h:373
const Node none
Definition: node.cc:12
auto isEqual(const Statement &other) const
Definition: while.h:48
Definition: meta.h:18
auto & _bodyNode()
Definition: while.h:42
Definition: statement.h:14
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
static Statement setCondition(const While &e, const hilti::Expression &c)
Definition: while.h:73
static Statement setInit(const While &e, const hilti::Declaration &d)
Definition: while.h:60
auto & _elseNode()
Definition: while.h:45
auto properties() const
Definition: while.h:51
Definition: node.h:318
Definition: while.h:16