Spicy
map.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/type.h>
8 #include <hilti/ast/types/tuple.h>
9 #include <hilti/ast/types/unknown.h>
10 
11 namespace hilti {
12 namespace type {
13 
14 namespace map {
15 
17 class Iterator : public TypeBase,
24 public:
25  Iterator(Type ktype, Type vtype, bool const_, Meta m = Meta())
26  : TypeBase(nodes(type::Tuple({std::move(ktype), std::move(vtype)}, m)), m), _const(const_) {}
27  Iterator(Wildcard /*unused*/, bool const_ = true, Meta m = Meta())
28  : TypeBase(nodes(type::unknown, type::unknown), std::move(m)), _wildcard(true), _const(const_) {}
29 
30  const Type& keyType() const {
31  if ( auto t = children()[0].tryAs<type::Tuple>() )
32  return t->elements()[0].type();
33  else
34  return child<Type>(0);
35  }
36 
37  const Type& valueType() const {
38  if ( auto t = children()[0].tryAs<type::Tuple>() )
39  return t->elements()[1].type();
40  else
41  return child<Type>(0);
42  }
43 
45  bool isConstant() const { return _const; }
46 
48  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
50  auto _isResolved(ResolvedState* rstate) const { return type::detail::isResolved(dereferencedType(), rstate); }
52  const Type& dereferencedType() const { return child<Type>(0); }
54  auto isWildcard() const { return _wildcard; }
56  auto typeParameters() const { return children(); }
58  auto properties() const { return node::Properties{{"const", _const}}; }
59 
60  bool operator==(const Iterator& other) const {
61  return keyType() == other.keyType() && valueType() == other.valueType();
62  }
63 
64 private:
65  bool _wildcard = false;
66  bool _const = false;
67 };
68 
69 } // namespace map
70 
72 class Map : public TypeBase,
78 public:
79  Map(Type k, Type v, Meta m = Meta())
80  : TypeBase(nodes(map::Iterator(k, v, true, m), map::Iterator(k, v, false, m)), m) {}
81  Map(Wildcard /*unused*/, Meta m = Meta())
82  : TypeBase(nodes(map::Iterator(Wildcard{}, true, m), map::Iterator(Wildcard{}, false, m)), m),
83  _wildcard(true) {}
84 
85  const Type& keyType() const { return child<map::Iterator>(0).keyType(); }
86  const Type& valueType() const { return child<map::Iterator>(0).valueType(); }
87 
89  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
91  auto _isResolved(ResolvedState* rstate) const {
92  return type::detail::isResolved(iteratorType(true), rstate) &&
93  type::detail::isResolved(iteratorType(false), rstate);
94  }
96  const Type& elementType() const { return valueType(); }
98  const Type& iteratorType(bool const_) const { return const_ ? child<Type>(0) : child<Type>(1); }
100  auto isWildcard() const { return _wildcard; }
102  auto typeParameters() const { return children(); }
104  auto properties() const { return node::Properties{}; }
105 
106  bool operator==(const Map& other) const { return iteratorType(true) == other.iteratorType(true); }
107 
108 private:
109  bool _wildcard = false;
110 };
111 
112 } // namespace type
113 } // namespace hilti
auto typeParameters() const
Definition: map.h:102
const Type & dereferencedType() const
Definition: map.h:52
Definition: type.h:36
auto typeParameters() const
Definition: map.h:56
const auto & children() const
Definition: node.h:470
Definition: map.h:17
Definition: type.h:37
Definition: meta.h:18
const Type & elementType() const
Definition: map.h:96
Definition: type.h:159
auto isWildcard() const
Definition: map.h:100
Definition: type.h:35
bool isConstant() const
Definition: map.h:45
const Type & iteratorType(bool const_) const
Definition: map.h:98
auto isEqual(const Type &other) const
Definition: map.h:89
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:99
Definition: map.h:72
auto isWildcard() const
Definition: map.h:54
Definition: type.h:198
Definition: type.h:33
Definition: type.h:269
Definition: tuple.h:39
auto _isResolved(ResolvedState *rstate) const
Definition: map.h:50
auto properties() const
Definition: map.h:58
auto isEqual(const Type &other) const
Definition: map.h:48
auto _isResolved(ResolvedState *rstate) const
Definition: map.h:91
auto properties() const
Definition: map.h:104