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::type {
12 
13 namespace map {
14 
16 class Iterator : public TypeBase,
23 public:
24  Iterator(Type ktype, Type vtype, bool const_, const Meta& m = Meta())
25  : TypeBase(nodes(type::Tuple({std::move(ktype), std::move(vtype)}, m)), m), _const(const_) {}
26  Iterator(Wildcard /*unused*/, bool const_ = true, Meta m = Meta())
27  : TypeBase(nodes(type::unknown, type::unknown), std::move(m)), _wildcard(true), _const(const_) {}
28 
29  const Type& keyType() const {
30  if ( auto t = children()[0].tryAs<type::Tuple>() )
31  return t->elements()[0].type();
32  else
33  return child<Type>(0);
34  }
35 
36  const Type& valueType() const {
37  if ( auto t = children()[0].tryAs<type::Tuple>() )
38  return t->elements()[1].type();
39  else
40  return child<Type>(0);
41  }
42 
44  bool isConstant() const { return _const; }
45 
47  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
49  auto _isResolved(ResolvedState* rstate) const { return type::detail::isResolved(dereferencedType(), rstate); }
51  const Type& dereferencedType() const { return child<Type>(0); }
53  auto isWildcard() const { return _wildcard; }
55  auto typeParameters() const { return children(); }
57  auto properties() const { return node::Properties{{"const", _const}}; }
58 
59  bool operator==(const Iterator& other) const {
60  return keyType() == other.keyType() && valueType() == other.valueType();
61  }
62 
63 private:
64  bool _wildcard = false;
65  bool _const = false;
66 };
67 
68 } // namespace map
69 
71 class Map : public TypeBase,
77 public:
78  Map(const Type& k, const Type& v, const Meta& m = Meta())
79  : TypeBase(nodes(map::Iterator(k, v, true, m), map::Iterator(k, v, false, m)), m) {}
80  Map(Wildcard /*unused*/, const Meta& m = Meta())
81  : TypeBase(nodes(map::Iterator(Wildcard{}, true, m), map::Iterator(Wildcard{}, false, m)), m),
82  _wildcard(true) {}
83 
84  const Type& keyType() const { return child<map::Iterator>(0).keyType(); }
85  const Type& valueType() const { return child<map::Iterator>(0).valueType(); }
86 
88  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
90  auto _isResolved(ResolvedState* rstate) const {
91  return type::detail::isResolved(iteratorType(true), rstate) &&
92  type::detail::isResolved(iteratorType(false), rstate);
93  }
95  const Type& elementType() const { return valueType(); }
97  const Type& iteratorType(bool const_) const { return const_ ? child<Type>(0) : child<Type>(1); }
99  auto isWildcard() const { return _wildcard; }
101  auto typeParameters() const { return children(); }
103  auto properties() const { return node::Properties{}; }
104 
105  bool operator==(const Map& other) const { return iteratorType(true) == other.iteratorType(true); }
106 
107 private:
108  bool _wildcard = false;
109 };
110 
111 } // namespace hilti::type
auto typeParameters() const
Definition: map.h:101
const Type & dereferencedType() const
Definition: map.h:51
Definition: type.h:35
auto typeParameters() const
Definition: map.h:55
const auto & children() const
Definition: node.h:471
Definition: map.h:16
Definition: type.h:36
Definition: meta.h:19
const Type & elementType() const
Definition: map.h:95
Definition: type.h:158
auto isWildcard() const
Definition: map.h:99
Definition: type.h:34
bool isConstant() const
Definition: map.h:44
const Type & iteratorType(bool const_) const
Definition: map.h:97
auto isEqual(const Type &other) const
Definition: map.h:88
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:97
Definition: map.h:71
auto isWildcard() const
Definition: map.h:53
Definition: type.h:197
Definition: type.h:32
Definition: type.h:268
Definition: tuple.h:39
auto _isResolved(ResolvedState *rstate) const
Definition: map.h:49
auto properties() const
Definition: map.h:57
auto isEqual(const Type &other) const
Definition: map.h:47
Definition: type.h:25
auto _isResolved(ResolvedState *rstate) const
Definition: map.h:90
auto properties() const
Definition: map.h:103