Spicy
reference.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/unknown.h>
9 
10 namespace hilti {
11 namespace type {
12 
13 /*
14  * AST node for a `strong_ref<T>` type.
15  */
16 class StrongReference : public TypeBase,
21 public:
22  StrongReference(Wildcard /*unused*/, Meta m = Meta()) : TypeBase({node::none}, std::move(m)), _wildcard(true) {}
23  StrongReference(Type ct, Meta m = Meta()) : TypeBase({std::move(ct)}, std::move(m)) {}
24  StrongReference(Type ct, bool treat_as_non_constant, Meta m = Meta()) : TypeBase({std::move(ct)}, std::move(m)) {
25  if ( treat_as_non_constant )
26  _state().flags -= type::Flag::Constant;
27  }
28 
29  Type dereferencedType() const {
30  if ( auto t = childs()[0].tryAs<Type>() )
31  return type::effectiveType(*t);
32 
33  return type::unknown;
34  }
35 
36  bool operator==(const StrongReference& other) const { return dereferencedType() == other.dereferencedType(); }
37 
39  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
41  auto typeParameters() const { return childs(); }
43  auto isWildcard() const { return _wildcard; }
44 
46  auto properties() const { return node::Properties{}; }
47 
48 private:
49  bool _wildcard = false;
50 };
51 
53 class WeakReference : public TypeBase,
58 public:
59  WeakReference(Wildcard /*unused*/, Meta m = Meta()) : TypeBase({node::none}, std::move(m)), _wildcard(true) {}
60  WeakReference(Type ct, Meta m = Meta()) : TypeBase({std::move(ct)}, std::move(m)) {}
61 
62  Type dereferencedType() const {
63  if ( auto t = childs()[0].tryAs<Type>() )
64  return type::effectiveType(*t);
65 
66  return type::unknown;
67  }
68 
69  bool operator==(const WeakReference& other) const { return dereferencedType() == other.dereferencedType(); }
70 
72  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
74  auto typeParameters() const { return childs(); }
76  auto isWildcard() const { return _wildcard; }
77 
79  auto properties() const { return node::Properties{}; }
80 
81 private:
82  bool _wildcard = false;
83 };
84 
86 class ValueReference : public TypeBase,
91 public:
92  ValueReference(Wildcard /*unused*/, Meta m = Meta()) : TypeBase({node::none}, std::move(m)), _wildcard(true) {}
93  ValueReference(Type ct, Meta m = Meta()) : TypeBase({std::move(ct)}, std::move(m)) {}
94 
95  Type dereferencedType() const {
96  if ( auto t = childs()[0].tryAs<Type>() )
97  return type::effectiveType(*t);
98 
99  return type::unknown;
100  }
101 
102  bool operator==(const ValueReference& other) const { return dereferencedType() == other.dereferencedType(); }
103 
105  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
107  auto typeParameters() const { return childs(); }
109  auto isWildcard() const { return _wildcard; }
110 
112  auto properties() const { return node::Properties{}; }
113 
114 private:
115  bool _wildcard = false;
116 };
117 
118 } // namespace type
119 } // namespace hilti
auto & childs() const
Definition: node.h:445
auto isWildcard() const
Definition: reference.h:109
const Node none
Definition: node.cc:12
auto typeParameters() const
Definition: reference.h:107
auto isEqual(const Type &other) const
Definition: reference.h:39
auto properties() const
Definition: reference.h:46
Definition: reference.h:16
auto isEqual(const Type &other) const
Definition: reference.h:105
Definition: meta.h:18
Definition: reference.h:86
const type::detail::State & _state() const
Definition: type.h:167
auto properties() const
Definition: reference.h:79
auto typeParameters() const
Definition: reference.h:74
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
auto typeParameters() const
Definition: reference.h:41
Definition: type.h:152
Definition: type.h:23
Definition: type.h:249
auto isWildcard() const
Definition: reference.h:76
Definition: reference.h:53
auto isWildcard() const
Definition: reference.h:43
auto properties() const
Definition: reference.h:112
auto isEqual(const Type &other) const
Definition: reference.h:72