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(hilti::Declaration init, std::optional<hilti::Expression> cond, Statement true_, std::optional<Statement> false_,
19  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 children()[0].tryAs<hilti::declaration::LocalVariable>(); }
29  auto initRef() const {
30  return children()[0].isA<hilti::declaration::LocalVariable>() ? NodeRef(children()[0]) : NodeRef();
31  }
32  auto condition() const { return children()[1].tryAs<hilti::Expression>(); }
33  const auto& true_() const { return child<hilti::Statement>(2); }
34  auto false_() const { return children()[3].tryAs<Statement>(); }
35 
36  void setCondition(hilti::Expression e) { children()[1] = std::move(e); }
37  void removeFalse() { children()[3] = node::none; }
38 
39  bool operator==(const If& other) const {
40  return init() == other.init() && condition() == other.condition() && true_() == other.true_() &&
41  false_() == other.false_();
42  }
43 
45  auto& _trueNode() { return children()[2]; }
46 
48  auto& _falseNode() { return children()[3]; }
49 
51  auto isEqual(const Statement& other) const { return node::isEqual(this, other); }
52 
54  auto properties() const { return node::Properties{}; }
55 };
56 
57 } // namespace statement
58 } // namespace hilti
Definition: local-variable.h:19
Definition: declaration.h:53
auto properties() const
Definition: if.h:54
const Node none
Definition: node.cc:14
const auto & children() const
Definition: node.h:470
auto & _falseNode()
Definition: if.h:48
Definition: meta.h:18
auto & _trueNode()
Definition: if.h:45
Definition: statement.h:14
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:99
auto isEqual(const Statement &other) const
Definition: if.h:51
Definition: node-ref.h:44
Definition: if.h:16
Definition: node.h:358