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 children()[0].tryAs<hilti::declaration::LocalVariable>(); }
32  auto initRef() const {
33  return children()[0].isA<hilti::declaration::LocalVariable>() ? NodeRef(children()[0]) : NodeRef();
34  }
35  auto condition() const { return children()[1].tryAs<hilti::Expression>(); }
36  const auto& body() const { return child<hilti::Statement>(2); }
37  auto else_() const { return children()[3].tryAs<Statement>(); }
38 
39  void setCondition(hilti::Expression c) { children()[1] = std::move(c); }
40  void setInit(hilti::Expression c) { children()[0] = std::move(c); }
41 
42  bool operator==(const While& other) const {
43  return init() == other.init() && condition() == other.condition() && body() == other.body() &&
44  else_() == other.else_();
45  }
46 
48  auto& _bodyNode() { return children()[2]; }
49 
51  auto& _elseNode() { return children()[3]; }
52 
54  auto isEqual(const Statement& other) const { return node::isEqual(this, other); }
55 
57  auto properties() const { return node::Properties{}; }
58 };
59 
60 } // namespace statement
61 } // namespace hilti
Definition: local-variable.h:19
Definition: declaration.h:53
const Node none
Definition: node.cc:14
const auto & children() const
Definition: node.h:470
auto isEqual(const Statement &other) const
Definition: while.h:54
Definition: meta.h:18
auto & _bodyNode()
Definition: while.h:48
Definition: statement.h:14
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:99
Definition: node-ref.h:44
auto & _elseNode()
Definition: while.h:51
auto properties() const
Definition: while.h:57
Definition: node.h:358
Definition: while.h:16