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