Spicy
tuple.h
1 // Copyrights (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <functional>
6 #include <utility>
7 #include <vector>
8 
9 #include <hilti/ast/id.h>
10 #include <hilti/ast/type.h>
11 
12 namespace hilti {
13 namespace type {
14 
15 namespace tuple {
16 
18 class Element : public NodeBase {
19 public:
20  explicit Element(Type t, Meta m = Meta()) : NodeBase(nodes(node::none, std::move(t)), std::move(m)) {}
21  Element(ID id, Type t, Meta m = Meta())
22  : NodeBase(nodes(id ? std::move(id) : node::none, std::move(t)), std::move(m)) {}
23  Element(Meta m = Meta()) : NodeBase(nodes(node::none, node::none), std::move(m)) {}
24 
25  auto id() const { return childs()[0].tryAs<ID>(); }
26  const auto& type() const { return child<Type>(1); }
27 
29  auto properties() const { return node::Properties{}; }
30 
31  bool operator==(const Element& other) const { return id() == other.id() && type() == other.type(); }
32 };
33 
34 inline Node to_node(Element f) { return Node(std::move(f)); }
35 
36 } // namespace tuple
37 
40 public:
41  Tuple(std::vector<Type> t, Meta m = Meta()) : TypeBase(nodes(_typesToElements(std::move(t))), std::move(m)) {}
42  Tuple(std::vector<tuple::Element> e, Meta m = Meta()) : TypeBase(nodes(std::move(e)), std::move(m)) {}
43  Tuple(Wildcard /*unused*/, Meta m = Meta()) : TypeBase(std::move(m)), _wildcard(true) {}
44 
45  auto elements() const { return childs<tuple::Element>(0, -1); }
46  std::optional<std::pair<int, const type::tuple::Element*>> elementByID(const ID& id) const;
47 
48  bool operator==(const Tuple& other) const {
49  if ( _wildcard || other._wildcard )
50  return _wildcard && other._wildcard;
51 
52  return elements() == other.elements();
53  }
54 
56  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
58  auto _isResolved(ResolvedState* rstate) const {
59  for ( const auto& c : childs() ) {
60  if ( auto t = c.tryAs<Type>(); t && ! type::detail::isResolved(*t, rstate) )
61  return false;
62  }
63 
64  return true;
65  }
66 
68  auto typeParameters() const { return childs(); }
70  auto isWildcard() const { return _wildcard; }
71 
73  auto properties() const { return node::Properties{{"wildcard", _wildcard}}; }
74 
75 private:
76  std::vector<tuple::Element> _typesToElements(std::vector<Type>&& types) {
77  std::vector<tuple::Element> elements;
78  for ( auto&& t : types )
79  elements.emplace_back(std::move(t), t.meta());
80 
81  return elements;
82  }
83 
84  bool _wildcard = false;
85 };
86 
87 } // namespace type
88 } // namespace hilti
auto properties() const
Definition: tuple.h:73
const Node none
Definition: node.cc:14
Definition: meta.h:18
Definition: type.h:159
auto isEqual(const Type &other) const
Definition: tuple.h:56
auto _isResolved(ResolvedState *rstate) const
Definition: tuple.h:58
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:99
auto properties() const
Definition: tuple.h:29
Definition: type.h:198
Definition: node.h:113
Definition: type.h:33
Definition: type.h:269
const auto & childs() const
Definition: node.h:470
Definition: tuple.h:39
auto typeParameters() const
Definition: tuple.h:68
auto isWildcard() const
Definition: tuple.h:70
Definition: id.h:18
Definition: tuple.h:18
Definition: node.h:358