Spicy
if.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 If : public NodeBase, public hilti::trait::isStatement {
17 public:
18  If(const hilti::Declaration& init, std::optional<hilti::Expression> cond, Statement true_,
19  std::optional<Statement> false_, Meta m = Meta())
20  : NodeBase(nodes(init, std::move(cond), std::move(true_), std::move(false_)), std::move(m)) {
21  if ( ! init.isA<declaration::LocalVariable>() )
22  logger().internalError("initialization for 'if' must be a local declaration");
23  }
24 
25  If(hilti::Expression cond, Statement true_, std::optional<Statement> false_, Meta m = Meta())
26  : NodeBase(nodes(node::none, std::move(cond), std::move(true_), std::move(false_)), std::move(m)) {}
27 
28  auto init() const { return childs()[0].tryReferenceAs<hilti::Declaration>(); }
29  auto condition() const { return childs()[1].tryReferenceAs<hilti::Expression>(); }
30  const auto& true_() const { return child<hilti::Statement>(2); }
31  auto false_() const { return childs()[3].tryReferenceAs<Statement>(); }
32 
33  bool operator==(const If& other) const {
34  return init() == other.init() && condition() == other.condition() && true_() == other.true_() &&
35  false_() == other.false_();
36  }
37 
39  auto& _trueNode() { return childs()[2]; }
40 
42  auto& _falseNode() { return childs()[3]; }
43 
45  auto isEqual(const Statement& other) const { return node::isEqual(this, other); }
46 
48  auto properties() const { return node::Properties{}; }
49 
57  static Statement setInit(const If& e, const hilti::Declaration& d) {
58  auto x = Statement(e)._clone().as<If>();
59  x.childs()[0] = d;
60  return x;
61  }
62 
70  static Statement setCondition(const If& e, const hilti::Expression& c) {
71  auto x = Statement(e)._clone().as<If>();
72  x.childs()[1] = c;
73  return x;
74  }
75 };
76 
77 } // namespace statement
78 } // namespace hilti
Definition: local-variable.h:19
static Statement setCondition(const If &e, const hilti::Expression &c)
Definition: if.h:70
auto & childs() const
Definition: node.h:445
std::vector< T > childs(int begin, int end) const
Definition: node.h:373
auto properties() const
Definition: if.h:48
const Node none
Definition: node.cc:12
auto & _falseNode()
Definition: if.h:42
static Statement setInit(const If &e, const hilti::Declaration &d)
Definition: if.h:57
Definition: meta.h:18
auto & _trueNode()
Definition: if.h:39
Definition: statement.h:14
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
auto isEqual(const Statement &other) const
Definition: if.h:45
Definition: if.h:16
Definition: node.h:318