Spicy
resolved-operator.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <utility>
6 #include <vector>
7 
8 #include <hilti/ast/expression.h>
9 #include <hilti/ast/expressions/unresolved-operator.h>
10 #include <hilti/ast/node.h>
11 #include <hilti/ast/operator.h>
12 #include <hilti/ast/types/unresolved-id.h>
13 
14 namespace hilti {
15 
16 namespace trait {
18 } // namespace trait
19 
20 namespace expression {
21 namespace resolved_operator {
22 namespace detail {
23 #include <hilti/autogen/__resolved-operator.h>
24 
25 inline Node to_node(ResolvedOperator t) { return Node(std::move(t)); }
26 
27 inline std::ostream& operator<<(std::ostream& out, ResolvedOperator i) { return out << to_node(std::move(i)); }
28 
29 } // namespace detail
30 } // namespace resolved_operator
31 
32 using ResolvedOperator = resolved_operator::detail::ResolvedOperator;
33 using resolved_operator::detail::to_node;
34 
41 public:
42  ResolvedOperatorBase(const Operator& op, const std::vector<Expression>& operands, Meta meta = Meta())
43  : NodeBase(nodes(node::none, operands), std::move(meta)), _operator(op) {
44  // Must not have instantiated this before we know the result type.
45  children()[0] = type::pruneWalk(op.result(children<Expression>(1, -1)));
46  }
47 
48  const auto& operator_() const { return _operator; }
49  auto kind() const { return _operator.kind(); }
50 
51  // ResolvedOperator interface with common implementation.
52  auto operands() const { return children<Expression>(1, -1); }
53  const auto& result() const { return child<Type>(0); }
54  const auto& op0() const { return child<Expression>(1); }
55  const auto& op1() const { return child<Expression>(2); }
56  const auto& op2() const { return child<Expression>(3); }
57  auto hasOp0() const { return ! children().empty(); }
58  auto hasOp1() const { return children().size() >= 3; }
59  auto hasOp2() const { return children().size() >= 4; }
60  void setOp0(const Expression& e) { children()[1] = e; }
61  void setOp1(const Expression& e) { children()[2] = e; }
62  void setOp2(const Expression& e) { children()[3] = e; }
63 
64  bool operator==(const ResolvedOperator& other) const {
65  return operator_() == other.operator_() && operands() == other.operands();
66  }
67 
69  bool isLhs() const { return operator_().isLhs(); }
71  bool isTemporary() const { return isLhs(); }
73  const Type& type() const { return result(); }
75  auto isEqual(const Expression& other) const { return node::isEqual(this, other); }
76 
78  bool isConstant() const { return type::isConstant(type()); }
79 
81  auto properties() const { return node::Properties{{"kind", to_string(_operator.kind())}}; }
82 
83 private:
84  ::hilti::operator_::detail::Operator _operator;
85 };
86 
87 namespace resolved_operator {
88 
96 inline hilti::Expression setOp0(const expression::ResolvedOperator& r, Expression e) {
97  auto x = r._clone().as<expression::ResolvedOperator>();
98  x.setOp0(std::move(e));
99  return x;
100 }
101 
109 inline hilti::Expression setOp1(const expression::ResolvedOperator& r, Expression e) {
110  auto x = r._clone().as<expression::ResolvedOperator>();
111  x.setOp1(std::move(e));
112  return x;
113 }
114 
122 inline hilti::Expression setOp2(const expression::ResolvedOperator& r, Expression e) {
123  auto x = r._clone().as<expression::ResolvedOperator>();
124  x.setOp2(std::move(e));
125  return x;
126 }
127 
128 } // namespace resolved_operator
129 } // namespace expression
130 } // namespace hilti
const Node none
Definition: node.cc:14
auto isEqual(const Expression &other) const
Definition: resolved-operator.h:75
bool isTemporary() const
Definition: resolved-operator.h:71
Definition: expression.h:17
auto properties() const
Definition: resolved-operator.h:81
Definition: meta.h:18
Definition: type.h:159
Definition: resolved-operator.h:40
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:99
bool isLhs() const
Definition: resolved-operator.h:69
bool isConstant() const
Definition: resolved-operator.h:78
Definition: node.h:113
const Type & type() const
Definition: resolved-operator.h:73
Definition: resolved-operator.h:17
Definition: node.h:358