13 #ifndef MLIR_SUPPORT_INTERFACESUPPORT_H 14 #define MLIR_SUPPORT_INTERFACESUPPORT_H 17 #include "llvm/ADT/ArrayRef.h" 18 #include "llvm/ADT/DenseMap.h" 19 #include "llvm/Support/TypeName.h" 68 template <
typename ConcreteType,
typename ValueT,
typename Traits,
70 template <
typename,
template <
typename>
class>
class BaseTrait>
73 using Concept =
typename Traits::Concept;
79 template <
typename T,
typename U>
83 template <
typename ConcreteT>
93 : BaseType(t),
impl(t ? ConcreteType::getInterfaceFor(t) : nullptr) {
94 assert((!t ||
impl) &&
"expected value to provide interface instance");
100 template <
typename T,
typename std::enable_if_t<
101 std::is_base_of<Trait<T>, T>
::value> * =
nullptr>
103 : BaseType(t),
impl(t ? ConcreteType::getInterfaceFor(t) : nullptr) {
104 assert((!t ||
impl) &&
"expected value to provide interface instance");
109 static bool classof(ValueT t) {
return ConcreteType::getInterfaceFor(t); }
130 template <
template <
class>
class Pred,
size_t N,
typename... Ts>
132 template <
template <
class>
class Pred,
size_t N,
typename T,
typename... Us>
134 :
public std::integral_constant<
136 count_if_t_impl<Pred, N + (Pred<T>::value ? 1 : 0), Us...>::value> {};
137 template <
template <
class>
class Pred,
typename... Ts>
143 template <
typename... Args>
144 struct all_trivially_destructible;
146 template <
typename Arg,
typename... Args>
147 struct all_trivially_destructible<Arg, Args...> {
148 static constexpr
const bool value =
150 all_trivially_destructible<Args...>
::value;
154 struct all_trivially_destructible<> {
155 static constexpr
const bool value =
true;
163 template <
typename T,
typename... Args>
164 using has_get_interface_id = decltype(T::getInterfaceID());
165 template <
typename T>
166 using detect_get_interface_id = llvm::is_detected<has_get_interface_id, T>;
167 template <
typename... Types>
173 for (
auto &it : interfaces)
175 interfaces = std::move(rhs.interfaces);
179 for (
auto &it : interfaces)
187 template <
typename... Types>
190 constexpr
size_t numInterfaces = num_interface_types_t<Types...>
::value;
191 if (numInterfaces == 0)
194 std::array<std::pair<TypeID, void *>, numInterfaces> elements;
195 std::pair<TypeID, void *> *elementIt = elements.data();
197 (
void)std::initializer_list<int>{
198 0, (addModelAndUpdateIterator<Types>(elementIt), 0)...};
204 template <
typename T>
typename T::Concept *
lookup()
const {
205 return reinterpret_cast<typename T::Concept *
>(lookup(T::getInterfaceID()));
218 template <
typename... IfaceModels>
221 "interface models must be trivially destructible");
222 std::pair<TypeID, void *> elements[] = {
223 std::make_pair(IfaceModels::Interface::getInterfaceID(),
224 new (malloc(
sizeof(IfaceModels))) IfaceModels())...};
233 template <
typename T>
235 addModelAndUpdateIterator(std::pair<TypeID, void *> *&elementIt) {
236 *elementIt = {T::getInterfaceID(),
new (malloc(
sizeof(
typename T::ModelT)))
237 typename T::ModelT()};
241 template <
typename T>
243 addModelAndUpdateIterator(std::pair<TypeID, void *> *&) {}
246 void insert(
ArrayRef<std::pair<TypeID, void *>> elements);
255 void *lookup(
TypeID id)
const {
257 llvm::lower_bound(interfaces,
id, [](
const auto &it,
TypeID id) {
260 return (it != interfaces.end() && it->first == id) ? it->second :
nullptr;
Include the generated interface declarations.
This is a special trait that registers a given interface with an object.
Interface(T t)
Construct an interface instance from a type that implements this interface's trait.
This class represents an abstract interface.
Interface(ValueT t=ValueT())
Construct an interface from an instance of the value type.
static constexpr const bool value
This class provides an efficient unique identifier for a specific C++ type.
Template utility that computes the number of elements within T that satisfy the given predicate...
This class provides an efficient mapping between a given Interface type, and a particular implementat...
static TypeID getInterfaceID()
Define an accessor for the ID of this interface.
const void * getAsOpaquePointer() const
Methods for supporting PointerLikeTypeTraits.
typename Traits::template FallbackModel< T > FallbackModel
T::Concept * lookup() const
Returns an instance of the concept object for the given interface if it was registered to this map...
Interface(std::nullptr_t)
void insert()
Insert the given models as implementations of the corresponding interfaces for the concrete attribute...
bool contains(TypeID interfaceID) const
Returns true if the interface map contains an interface for the given id.
typename Traits::template ExternalModel< T, U > ExternalModel
static bool classof(ValueT t)
Support 'classof' by checking if the given object defines the concrete interface. ...
int compare(Fraction x, Fraction y)
Three-way comparison between two fractions.
Model< ConcreteT > ModelT
InterfaceMap & operator=(InterfaceMap &&rhs)
const Concept * getImpl() const
Get the raw concept in the correct derived concept type.
typename Traits::template Model< T > Model
static TypeID getInterfaceID()
Define an accessor for the ID of this interface.