Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:01

0001 //===- Any.h - Generic type erased holder of any type -----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 ///  This file provides Any, a non-template class modeled in the spirit of
0011 ///  std::any.  The idea is to provide a type-safe replacement for C's void*.
0012 ///  It can hold a value of any copy-constructible copy-assignable type
0013 ///
0014 //===----------------------------------------------------------------------===//
0015 
0016 #ifndef LLVM_ADT_ANY_H
0017 #define LLVM_ADT_ANY_H
0018 
0019 #include "llvm/ADT/STLForwardCompat.h"
0020 #include "llvm/Support/Compiler.h"
0021 
0022 #include <cassert>
0023 #include <memory>
0024 #include <type_traits>
0025 
0026 namespace llvm {
0027 
0028 class LLVM_ABI Any {
0029 
0030   // The `Typeid<T>::Id` static data member below is a globally unique
0031   // identifier for the type `T`. It is explicitly marked with default
0032   // visibility so that when `-fvisibility=hidden` is used, the loader still
0033   // merges duplicate definitions across DSO boundaries.
0034   // We also cannot mark it as `const`, otherwise msvc merges all definitions
0035   // when lto is enabled, making any comparison return true.
0036   template <typename T> struct TypeId { static char Id; };
0037 
0038   struct StorageBase {
0039     virtual ~StorageBase() = default;
0040     virtual std::unique_ptr<StorageBase> clone() const = 0;
0041     virtual const void *id() const = 0;
0042   };
0043 
0044   template <typename T> struct StorageImpl : public StorageBase {
0045     explicit StorageImpl(const T &Value) : Value(Value) {}
0046 
0047     explicit StorageImpl(T &&Value) : Value(std::move(Value)) {}
0048 
0049     std::unique_ptr<StorageBase> clone() const override {
0050       return std::make_unique<StorageImpl<T>>(Value);
0051     }
0052 
0053     const void *id() const override { return &TypeId<T>::Id; }
0054 
0055     T Value;
0056 
0057   private:
0058     StorageImpl &operator=(const StorageImpl &Other) = delete;
0059     StorageImpl(const StorageImpl &Other) = delete;
0060   };
0061 
0062 public:
0063   Any() = default;
0064 
0065   Any(const Any &Other)
0066       : Storage(Other.Storage ? Other.Storage->clone() : nullptr) {}
0067 
0068   // When T is Any or T is not copy-constructible we need to explicitly disable
0069   // the forwarding constructor so that the copy constructor gets selected
0070   // instead.
0071   template <typename T,
0072             std::enable_if_t<
0073                 std::conjunction<
0074                     std::negation<std::is_same<std::decay_t<T>, Any>>,
0075                     // We also disable this overload when an `Any` object can be
0076                     // converted to the parameter type because in that case,
0077                     // this constructor may combine with that conversion during
0078                     // overload resolution for determining copy
0079                     // constructibility, and then when we try to determine copy
0080                     // constructibility below we may infinitely recurse. This is
0081                     // being evaluated by the standards committee as a potential
0082                     // DR in `std::any` as well, but we're going ahead and
0083                     // adopting it to work-around usage of `Any` with types that
0084                     // need to be implicitly convertible from an `Any`.
0085                     std::negation<std::is_convertible<Any, std::decay_t<T>>>,
0086                     std::is_copy_constructible<std::decay_t<T>>>::value,
0087                 int> = 0>
0088   Any(T &&Value) {
0089     Storage =
0090         std::make_unique<StorageImpl<std::decay_t<T>>>(std::forward<T>(Value));
0091   }
0092 
0093   Any(Any &&Other) : Storage(std::move(Other.Storage)) {}
0094 
0095   Any &swap(Any &Other) {
0096     std::swap(Storage, Other.Storage);
0097     return *this;
0098   }
0099 
0100   Any &operator=(Any Other) {
0101     Storage = std::move(Other.Storage);
0102     return *this;
0103   }
0104 
0105   bool has_value() const { return !!Storage; }
0106 
0107   void reset() { Storage.reset(); }
0108 
0109 private:
0110   // Only used for the internal llvm::Any implementation
0111   template <typename T> bool isa() const {
0112     if (!Storage)
0113       return false;
0114     return Storage->id() == &Any::TypeId<remove_cvref_t<T>>::Id;
0115   }
0116 
0117   template <class T> friend T any_cast(const Any &Value);
0118   template <class T> friend T any_cast(Any &Value);
0119   template <class T> friend T any_cast(Any &&Value);
0120   template <class T> friend const T *any_cast(const Any *Value);
0121   template <class T> friend T *any_cast(Any *Value);
0122   template <typename T> friend bool any_isa(const Any &Value);
0123 
0124   std::unique_ptr<StorageBase> Storage;
0125 };
0126 
0127 // Define the type id and initialize with a non-zero value.
0128 // Initializing with a zero value means the variable can end up in either the
0129 // .data or the .bss section. This can lead to multiple definition linker errors
0130 // when some object files are compiled with a compiler that puts the variable
0131 // into .data but they are linked to object files from a different compiler that
0132 // put the variable into .bss. To prevent this issue from happening, initialize
0133 // the variable with a non-zero value, which forces it to land in .data (because
0134 // .bss is zero-initialized).
0135 // See also https://github.com/llvm/llvm-project/issues/62270
0136 template <typename T> char Any::TypeId<T>::Id = 1;
0137 
0138 template <class T> T any_cast(const Any &Value) {
0139   assert(Value.isa<T>() && "Bad any cast!");
0140   return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
0141 }
0142 
0143 template <class T> T any_cast(Any &Value) {
0144   assert(Value.isa<T>() && "Bad any cast!");
0145   return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
0146 }
0147 
0148 template <class T> T any_cast(Any &&Value) {
0149   assert(Value.isa<T>() && "Bad any cast!");
0150   return static_cast<T>(std::move(*any_cast<remove_cvref_t<T>>(&Value)));
0151 }
0152 
0153 template <class T> const T *any_cast(const Any *Value) {
0154   using U = remove_cvref_t<T>;
0155   if (!Value || !Value->isa<U>())
0156     return nullptr;
0157   return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
0158 }
0159 
0160 template <class T> T *any_cast(Any *Value) {
0161   using U = std::decay_t<T>;
0162   if (!Value || !Value->isa<U>())
0163     return nullptr;
0164   return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
0165 }
0166 
0167 } // end namespace llvm
0168 
0169 #endif // LLVM_ADT_ANY_H