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::statement {
13 
15 class While : public NodeBase, public hilti::trait::isStatement {
16 public:
17  While(const hilti::Declaration& init, std::optional<hilti::Expression> cond, Statement body,
18  std::optional<Statement> else_ = {}, Meta m = Meta())
19  : NodeBase(nodes(init, std::move(cond), std::move(body), std::move(else_)), std::move(m)) {
20  if ( ! init.isA<declaration::LocalVariable>() )
21  logger().internalError("initialization for 'while' must be a local declaration");
22  }
23 
24  While(hilti::Expression cond, Statement body, Meta m = Meta())
25  : NodeBase(nodes(node::none, std::move(cond), std::move(body), node::none), std::move(m)) {}
26 
27  While(hilti::Expression cond, Statement body, std::optional<Statement> else_, Meta m = Meta())
28  : NodeBase(nodes(node::none, std::move(cond), std::move(body), std::move(else_)), std::move(m)) {}
29 
30  auto init() const { return children()[0].tryAs<hilti::declaration::LocalVariable>(); }
31  auto initRef() const {
32  return children()[0].isA<hilti::declaration::LocalVariable>() ? NodeRef(children()[0]) : NodeRef();
33  }
34  auto condition() const { return children()[1].tryAs<hilti::Expression>(); }
35  const auto& body() const { return child<hilti::Statement>(2); }
36  auto else_() const { return children()[3].tryAs<Statement>(); }
37 
38  void setCondition(const hilti::Expression& c) { children()[1] = c; }
39  void setInit(const hilti::Expression& c) { children()[0] = c; }
40 
41  bool operator==(const While& other) const {
42  return init() == other.init() && condition() == other.condition() && body() == other.body() &&
43  else_() == other.else_();
44  }
45 
47  auto& _bodyNode() { return children()[2]; }
48 
50  auto& _elseNode() { return children()[3]; }
51 
53  auto isEqual(const Statement& other) const { return node::isEqual(this, other); }
54 
56  auto properties() const { return node::Properties{}; }
57 };
58 
59 } // namespace hilti::statement
Definition: local-variable.h:18
Definition: declaration.h:54
const Node none
Definition: node.cc:14
const auto & children() const
Definition: node.h:472
auto isEqual(const Statement &other) const
Definition: while.h:53
Definition: meta.h:19
auto & _bodyNode()
Definition: while.h:47
Definition: statement.h:14
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:98
Definition: node-ref.h:45
auto & _elseNode()
Definition: while.h:50
auto properties() const
Definition: while.h:56
Definition: node.h:360
Definition: while.h:15