MLIR
20.0.0git
|
This class provides an efficient unique identifier for a specific C++ type. More...
#include "mlir/Support/TypeID.h"
Public Member Functions | |
TypeID () | |
bool | operator== (const TypeID &other) const |
Comparison operations. More... | |
bool | operator!= (const TypeID &other) const |
const void * | getAsOpaquePointer () const |
Methods for supporting PointerLikeTypeTraits. More... | |
Static Public Member Functions | |
template<typename T > | |
static TypeID | get () |
Construct a type info object for the given type T. More... | |
template<template< typename > class Trait> | |
static TypeID | get () |
static TypeID | getFromOpaquePointer (const void *pointer) |
Friends | |
class | TypeIDAllocator |
::llvm::hash_code | hash_value (TypeID id) |
Enable hashing TypeID. More... | |
This class provides an efficient unique identifier for a specific C++ type.
This allows for a C++ type to be compared, hashed, and stored in an opaque context. This class is similar in some ways to std::type_index, but can be used for any type. For example, this class could be used to implement LLVM style isa/dyn_cast functionality for a type hierarchy:
struct Base { Base(TypeID typeID) : typeID(typeID) {} TypeID typeID; };
struct DerivedA : public Base { DerivedA() : Base(TypeID::get<DerivedA>()) {}
static bool classof(const Base *base) { return base->typeID == TypeID::get<DerivedA>(); } };
void foo(Base *base) { if (DerivedA *a = llvm::dyn_cast<DerivedA>(base)) ... }
C++ RTTI is a notoriously difficult topic; given the nature of shared libraries many different approaches fundamentally break down in either the area of support (i.e. only certain types of classes are supported), or in terms of performance (e.g. by using string comparison). This class intends to strike a balance between performance and the setup required to enable its use.
Assume we are adding support for some class Foo, below are the set of ways in which a given c++ type may be supported:
MLIR_DECLARE_EXPLICIT_TYPE_ID
and MLIR_DEFINE_EXPLICIT_TYPE_ID
This method explicitly defines the type ID for a given type using the given macros. These should be placed at the top-level of the file (i.e. not within any namespace or class). This is the most effective and efficient method, but requires explicit annotations for each type.
Example:
// Foo.h MLIR_DECLARE_EXPLICIT_TYPE_ID(Foo);
// Foo.cpp MLIR_DEFINE_EXPLICIT_TYPE_ID(Foo);
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID
This method explicitly defines the type ID for a given type by annotating the class directly. This has similar effectiveness and efficiency to the above method, but should only be used on internal classes; i.e. those with definitions constrained to a specific library (generally classes in anonymous namespaces).
Example:
namespace { class Foo { public: MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(Foo) }; } // namespace
|
inline |
Definition at line 112 of file TypeID.h.
Referenced by getFromOpaquePointer().
|
static |
Construct a type info object for the given type T.
Definition at line 232 of file TypeID.h.
References mlir::detail::TypeIDResolver< T, Enable >::resolveTypeID().
|
static |
|
inline |
Methods for supporting PointerLikeTypeTraits.
Definition at line 129 of file TypeID.h.
Referenced by llvm::PointerLikeTypeTraits< mlir::TypeID >::getAsVoidPointer().
|
inlinestatic |
Definition at line 132 of file TypeID.h.
References TypeID().
Referenced by mlir::DialectRegistry::addExtension(), llvm::DenseMapInfo< mlir::TypeID >::getEmptyKey(), llvm::PointerLikeTypeTraits< mlir::TypeID >::getFromVoidPointer(), mlir::Pattern::getRootInterfaceID(), mlir::Pattern::getRootTraitID(), llvm::DenseMapInfo< mlir::TypeID >::getTombstoneKey(), mlir::SelfOwningTypeID::getTypeID(), and mlirTypeIDCreate().
|
inline |
|
inline |
|
friend |
|
friend |