1- // ===--- AnyValue.h - Any Value Existential ---------------------*- C++ -*-===//
1+ // ===--- AnyValue.h ------------------------ ---------------------*- C++ -*-===//
22//
33// This source file is part of the Swift.org open source project
44//
1010//
1111// ===----------------------------------------------------------------------===//
1212//
13- // This file defines the AnyValue class, which is used to store an
14- // immutable value of any type .
13+ // This file defines some miscellaneous overloads of hash_value() and
14+ // simple_display() .
1515//
1616// ===----------------------------------------------------------------------===//
1717
@@ -37,125 +37,7 @@ namespace llvm {
3737 return 1 ;
3838 return hash_value (*opt);
3939 }
40- }
4140
42- namespace swift {
43-
44- // / Stores a value of any type that satisfies a small set of requirements.
45- // /
46- // / Requirements on the values stored within an AnyValue:
47- // /
48- // / - Copy constructor
49- // / - Equality operator (==)
50- // / - TypeID support (see swift/Basic/TypeID.h)
51- // / - Display support (free function):
52- // / void simple_display(llvm::raw_ostream &, const T &);
53- class AnyValue {
54- // / Abstract base class used to hold on to a value.
55- class HolderBase {
56- public:
57- // / Type ID number.
58- const uint64_t typeID;
59-
60- HolderBase () = delete ;
61- HolderBase (const HolderBase &) = delete ;
62- HolderBase (HolderBase &&) = delete ;
63- HolderBase &operator =(const HolderBase &) = delete ;
64- HolderBase &operator =(HolderBase &&) = delete ;
65-
66- // / Initialize base with type ID.
67- HolderBase (uint64_t typeID) : typeID(typeID) { }
68-
69- virtual ~HolderBase ();
70-
71- // / Determine whether this value is equivalent to another.
72- // /
73- // / The caller guarantees that the type IDs are the same.
74- virtual bool equals (const HolderBase &other) const = 0;
75-
76- // / Display.
77- virtual void display (llvm::raw_ostream &out) const = 0;
78- };
79-
80- // / Holds a value that can be used as a request input/output.
81- template <typename T>
82- class Holder final : public HolderBase {
83- public:
84- const T value;
85-
86- Holder (T &&value)
87- : HolderBase(TypeID<T>::value),
88- value (std::move(value)) { }
89-
90- Holder (const T &value) : HolderBase(TypeID<T>::value), value(value) { }
91-
92- virtual ~Holder () { }
93-
94- // / Determine whether this value is equivalent to another.
95- // /
96- // / The caller guarantees that the type IDs are the same.
97- virtual bool equals (const HolderBase &other) const override {
98- assert (typeID == other.typeID && " Caller should match type IDs" );
99- return value == static_cast <const Holder<T> &>(other).value ;
100- }
101-
102- // / Display.
103- virtual void display (llvm::raw_ostream &out) const override {
104- simple_display (out, value);
105- }
106- };
107-
108- // / The data stored in this value.
109- std::shared_ptr<HolderBase> stored;
110-
111- public:
112- // / Construct a new instance with the given value.
113- template <typename T>
114- AnyValue (T&& value) {
115- using ValueType = typename std::remove_reference<T>::type;
116- stored.reset (new Holder<ValueType>(std::forward<T>(value)));
117- }
118-
119- // / Cast to a specific (known) type.
120- template <typename T>
121- const T &castTo () const {
122- assert (stored->typeID == TypeID<T>::value);
123- return static_cast <const Holder<T> *>(stored.get ())->value ;
124- }
125-
126- // / Try casting to a specific (known) type, returning \c nullptr on
127- // / failure.
128- template <typename T>
129- const T *getAs () const {
130- if (stored->typeID != TypeID<T>::value)
131- return nullptr ;
132-
133- return &static_cast <const Holder<T> *>(stored.get ())->value ;
134- }
135-
136- // / Compare two instances for equality.
137- friend bool operator ==(const AnyValue &lhs, const AnyValue &rhs) {
138- if (lhs.stored ->typeID != rhs.stored ->typeID )
139- return false ;
140-
141- return lhs.stored ->equals (*rhs.stored );
142- }
143-
144- friend bool operator !=(const AnyValue &lhs, const AnyValue &rhs) {
145- return !(lhs == rhs);
146- }
147-
148- friend void simple_display (llvm::raw_ostream &out, const AnyValue &value) {
149- value.stored ->display (out);
150- }
151-
152- // / Return the result of calling simple_display as a string.
153- std::string getAsString () const ;
154- };
155-
156- } // end namespace swift
157-
158- namespace llvm {
15941 template <typename T>
16042 bool operator ==(const TinyPtrVector<T> &lhs, const TinyPtrVector<T> &rhs) {
16143 if (lhs.size () != rhs.size ())
0 commit comments