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::detail {
22 #include <hilti/autogen/__resolved-operator.h>
23 
24 inline Node to_node(ResolvedOperator t) { return Node(std::move(t)); }
25 
26 inline std::ostream& operator<<(std::ostream& out, ResolvedOperator i) { return out << to_node(std::move(i)); }
27 
28 } // namespace resolved_operator::detail
29 
30 using ResolvedOperator = resolved_operator::detail::ResolvedOperator;
31 
38 public:
39  ResolvedOperatorBase(const Operator& op, const std::vector<Expression>& operands, Meta meta = Meta())
40  : NodeBase(nodes(node::none, operands), std::move(meta)), _operator(op) {
41  // Must not have instantiated this before we know the result type.
42  children()[0] = type::pruneWalk(op.result(children<Expression>(1, -1)));
43  }
44 
45  const auto& operator_() const { return _operator; }
46  auto kind() const { return _operator.kind(); }
47 
48  // ResolvedOperator interface with common implementation.
49  auto operands() const { return children<Expression>(1, -1); }
50  const auto& result() const { return child<Type>(0); }
51  const auto& op0() const { return child<Expression>(1); }
52  const auto& op1() const { return child<Expression>(2); }
53  const auto& op2() const { return child<Expression>(3); }
54  auto hasOp0() const { return ! children().empty(); }
55  auto hasOp1() const { return children().size() >= 3; }
56  auto hasOp2() const { return children().size() >= 4; }
57  void setOp0(const Expression& e) { children()[1] = e; }
58  void setOp1(const Expression& e) { children()[2] = e; }
59  void setOp2(const Expression& e) { children()[3] = e; }
60 
61  bool operator==(const ResolvedOperator& other) const {
62  return operator_() == other.operator_() && operands() == other.operands();
63  }
64 
66  bool isLhs() const { return operator_().isLhs(); }
68  bool isTemporary() const { return isLhs(); }
70  const Type& type() const { return result(); }
72  auto isEqual(const Expression& other) const { return node::isEqual(this, other); }
73 
75  bool isConstant() const { return type::isConstant(type()); }
76 
78  auto properties() const { return node::Properties{{"kind", to_string(_operator.kind())}}; }
79 
80 private:
81  ::hilti::operator_::detail::Operator _operator;
82 };
83 
84 namespace resolved_operator {
85 
93 inline hilti::Expression setOp0(const expression::ResolvedOperator& r, Expression e) {
94  auto x = r._clone().as<expression::ResolvedOperator>();
95  x.setOp0(std::move(e));
96  return x;
97 }
98 
106 inline hilti::Expression setOp1(const expression::ResolvedOperator& r, Expression e) {
107  auto x = r._clone().as<expression::ResolvedOperator>();
108  x.setOp1(std::move(e));
109  return x;
110 }
111 
119 inline hilti::Expression setOp2(const expression::ResolvedOperator& r, Expression e) {
120  auto x = r._clone().as<expression::ResolvedOperator>();
121  x.setOp2(std::move(e));
122  return x;
123 }
124 
125 } // namespace resolved_operator
126 } // namespace expression
127 } // namespace hilti
const Node none
Definition: node.cc:14
auto isEqual(const Expression &other) const
Definition: resolved-operator.h:72
bool isTemporary() const
Definition: resolved-operator.h:68
Definition: expression.h:18
auto properties() const
Definition: resolved-operator.h:78
Definition: meta.h:19
Definition: type.h:160
Definition: resolved-operator.h:37
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:98
bool isLhs() const
Definition: resolved-operator.h:66
bool isConstant() const
Definition: resolved-operator.h:75
Definition: node.h:112
const Type & type() const
Definition: resolved-operator.h:70
Definition: resolved-operator.h:17
Definition: node.h:360