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 ctype, bool const_, Meta m = Meta()) : TypeBase({std::move(ctype)}, std::move(m)), _const(const_) {}
26  Iterator(Wildcard /*unused*/, Meta m = Meta()) : TypeBase({node::none}, std::move(m)), _wildcard(true) {}
27 
29  Type containerType() const { return _wildcard ? type::unknown : type::effectiveType(child<Type>(0)); }
30 
32  bool isConstant() const { return _const; }
33 
35  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
37  Type dereferencedType() const;
39  auto isWildcard() const { return _wildcard; }
41  auto typeParameters() const { return childs(); }
43  auto properties() const { return node::Properties{{"const", _const}}; }
44 
45  bool operator==(const Iterator& other) const { return dereferencedType() == other.dereferencedType(); }
46 
47 private:
48  bool _wildcard = false;
49  bool _const = false;
50 };
51 
52 } // namespace map
53 
55 class Map : public TypeBase,
61 public:
62  Map(Type key, Type value, Meta m = Meta()) : TypeBase({std::move(key), std::move(value)}, std::move(m)) {}
63  Map(Wildcard /*unused*/, Meta m = Meta()) : TypeBase({node::none}, std::move(m)), _wildcard(true) {}
64 
65  Type keyType() const { return _wildcard ? type::unknown : type::effectiveType(child<Type>(0)); }
66 
68  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
70  Type elementType() const { return _wildcard ? type::unknown : type::effectiveType(child<Type>(1)); }
72  Type iteratorType(bool const_) const { return map::Iterator(*this, const_, meta()); }
74  auto isWildcard() const { return _wildcard; }
76  auto typeParameters() const { return childs(); }
78  auto properties() const { return node::Properties{}; }
79 
80  bool operator==(const Map& other) const {
81  return keyType() == other.keyType() && elementType() == other.elementType();
82  }
83 
84 private:
85  bool _wildcard = false;
86 };
87 
88 namespace map {
89 inline Type Iterator::dereferencedType() const {
90  if ( _wildcard || containerType().isWildcard() )
91  return type::unknown;
92 
93  return type::Tuple({containerType().as<type::Map>().keyType(), containerType().elementType()});
94 }
95 } // namespace map
96 
97 } // namespace type
98 } // namespace hilti
Type containerType() const
Definition: map.h:29
auto typeParameters() const
Definition: map.h:76
auto & childs() const
Definition: node.h:445
const Node none
Definition: node.cc:12
Definition: type.h:26
auto typeParameters() const
Definition: map.h:41
Definition: map.h:17
Definition: type.h:27
Definition: meta.h:18
Type iteratorType(bool const_) const
Definition: map.h:72
auto isWildcard() const
Definition: map.h:74
Type elementType() const
Definition: map.h:70
Definition: type.h:25
bool isConstant() const
Definition: map.h:32
auto isEqual(const Type &other) const
Definition: map.h:68
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
Definition: map.h:55
auto isWildcard() const
Definition: map.h:39
Definition: type.h:152
Type dereferencedType() const
Definition: map.h:89
Definition: type.h:23
Definition: type.h:249
Definition: tuple.h:15
auto properties() const
Definition: map.h:43
auto & meta() const
Definition: node.h:449
auto isEqual(const Type &other) const
Definition: map.h:35
auto properties() const
Definition: map.h:78