1 //===- Any.h - Generic type erased holder of any type -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 ///  This file provides Any, a non-template class modeled in the spirit of
11 ///  std::any.  The idea is to provide a type-safe replacement for C's void*.
12 ///  It can hold a value of any copy-constructible copy-assignable type
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_ADT_ANY_H
17 #define LLVM_ADT_ANY_H
18 
19 #include "llvm/ADT/STLForwardCompat.h"
20 #include "llvm/Support/Compiler.h"
21 
22 #include <cassert>
23 #include <memory>
24 #include <type_traits>
25 
26 namespace llvm {
27 
28 class LLVM_EXTERNAL_VISIBILITY Any {
29 
30   // The `Typeid<T>::Id` static data member below is a globally unique
31   // identifier for the type `T`. It is explicitly marked with default
32   // visibility so that when `-fvisibility=hidden` is used, the loader still
33   // merges duplicate definitions across DSO boundaries.
34   // We also cannot mark it as `const`, otherwise msvc merges all definitions
35   // when lto is enabled, making any comparison return true.
36   template <typename T> struct TypeId { static char Id; };
37 
38   struct StorageBase {
39     virtual ~StorageBase() = default;
40     virtual std::unique_ptr<StorageBase> clone() const = 0;
41     virtual const void *id() const = 0;
42   };
43 
44   template <typename T> struct StorageImpl : public StorageBase {
45     explicit StorageImpl(const T &Value) : Value(Value) {}
46 
47     explicit StorageImpl(T &&Value) : Value(std::move(Value)) {}
48 
49     std::unique_ptr<StorageBase> clone() const override {
50       return std::make_unique<StorageImpl<T>>(Value);
51     }
52 
53     const void *id() const override { return &TypeId<T>::Id; }
54 
55     T Value;
56 
57   private:
58     StorageImpl &operator=(const StorageImpl &Other) = delete;
59     StorageImpl(const StorageImpl &Other) = delete;
60   };
61 
62 public:
63   Any() = default;
64 
65   Any(const Any &Other)
66       : Storage(Other.Storage ? Other.Storage->clone() : nullptr) {}
67 
68   // When T is Any or T is not copy-constructible we need to explicitly disable
69   // the forwarding constructor so that the copy constructor gets selected
70   // instead.
71   template <typename T,
72             std::enable_if_t<
73                 std::conjunction<
74                     std::negation<std::is_same<std::decay_t<T>, Any>>,
75                     // We also disable this overload when an `Any` object can be
76                     // converted to the parameter type because in that case,
77                     // this constructor may combine with that conversion during
78                     // overload resolution for determining copy
79                     // constructibility, and then when we try to determine copy
80                     // constructibility below we may infinitely recurse. This is
81                     // being evaluated by the standards committee as a potential
82                     // DR in `std::any` as well, but we're going ahead and
83                     // adopting it to work-around usage of `Any` with types that
84                     // need to be implicitly convertible from an `Any`.
85                     std::negation<std::is_convertible<Any, std::decay_t<T>>>,
86                     std::is_copy_constructible<std::decay_t<T>>>::value,
87                 int> = 0>
88   Any(T &&Value) {
89     Storage =
90         std::make_unique<StorageImpl<std::decay_t<T>>>(std::forward<T>(Value));
91   }
92 
93   Any(Any &&Other) : Storage(std::move(Other.Storage)) {}
94 
95   Any &swap(Any &Other) {
96     std::swap(Storage, Other.Storage);
97     return *this;
98   }
99 
100   Any &operator=(Any Other) {
101     Storage = std::move(Other.Storage);
102     return *this;
103   }
104 
105   bool has_value() const { return !!Storage; }
106 
107   void reset() { Storage.reset(); }
108 
109 private:
110   // Only used for the internal llvm::Any implementation
111   template <typename T> bool isa() const {
112     if (!Storage)
113       return false;
114     return Storage->id() == &Any::TypeId<remove_cvref_t<T>>::Id;
115   }
116 
117   template <class T> friend T any_cast(const Any &Value);
118   template <class T> friend T any_cast(Any &Value);
119   template <class T> friend T any_cast(Any &&Value);
120   template <class T> friend const T *any_cast(const Any *Value);
121   template <class T> friend T *any_cast(Any *Value);
122   template <typename T> friend bool any_isa(const Any &Value);
123 
124   std::unique_ptr<StorageBase> Storage;
125 };
126 
127 // Define the type id and initialize with a non-zero value.
128 // Initializing with a zero value means the variable can end up in either the
129 // .data or the .bss section. This can lead to multiple definition linker errors
130 // when some object files are compiled with a compiler that puts the variable
131 // into .data but they are linked to object files from a different compiler that
132 // put the variable into .bss. To prevent this issue from happening, initialize
133 // the variable with a non-zero value, which forces it to land in .data (because
134 // .bss is zero-initialized).
135 // See also https://github.com/llvm/llvm-project/issues/62270
136 template <typename T> char Any::TypeId<T>::Id = 1;
137 
138 template <typename T>
139 LLVM_DEPRECATED("Use any_cast(Any*) != nullptr instead", "any_cast")
140 bool any_isa(const Any &Value) {
141   return Value.isa<T>();
142 }
143 
144 template <class T> T any_cast(const Any &Value) {
145   assert(Value.isa<T>() && "Bad any cast!");
146   return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
147 }
148 
149 template <class T> T any_cast(Any &Value) {
150   assert(Value.isa<T>() && "Bad any cast!");
151   return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
152 }
153 
154 template <class T> T any_cast(Any &&Value) {
155   assert(Value.isa<T>() && "Bad any cast!");
156   return static_cast<T>(std::move(*any_cast<remove_cvref_t<T>>(&Value)));
157 }
158 
159 template <class T> const T *any_cast(const Any *Value) {
160   using U = remove_cvref_t<T>;
161   if (!Value || !Value->isa<U>())
162     return nullptr;
163   return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
164 }
165 
166 template <class T> T *any_cast(Any *Value) {
167   using U = std::decay_t<T>;
168   if (!Value || !Value->isa<U>())
169     return nullptr;
170   return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
171 }
172 
173 } // end namespace llvm
174 
175 #endif // LLVM_ADT_ANY_H
176