@@ -154,7 +154,7 @@ TVM_FFI_INLINE bool IsObjectInstance(int32_t object_type_index);
154154 * The unique string identifier of the type.
155155 * - _type_final:
156156 * Whether the type is terminal type(there is no subclass of the type in the object system).
157- * This field is automatically set by macro TVM_DECLARE_FINAL_OBJECT_INFO
157+ * This field is automatically set by macro TVM_FFI_DECLARE_OBJECT_INFO_FINAL
158158 * It is still OK to sub-class a terminal object type T and construct it using make_object.
159159 * But IsInstance check will only show that the object type is T(instead of the sub-class).
160160 * - _type_mutable:
@@ -177,8 +177,8 @@ TVM_FFI_INLINE bool IsObjectInstance(int32_t object_type_index);
177177 * Recommendation: set to false for optimal runtime speed if we know exact number of children.
178178 *
179179 * Two macros are used to declare helper functions in the object:
180- * - Use TVM_FFI_DECLARE_BASE_OBJECT_INFO for object classes that can be sub-classed.
181- * - Use TVM_FFI_DECLARE_FINAL_OBJECT_INFO for object classes that cannot be sub-classed.
180+ * - Use TVM_FFI_DECLARE_OBJECT_INFO for object classes that can be sub-classed.
181+ * - Use TVM_FFI_DECLARE_OBJECT_INFO_FINAL for object classes that cannot be sub-classed.
182182 *
183183 * New objects can be created using make_object function.
184184 * Which will automatically populate the type_index and deleter of the object.
@@ -276,7 +276,7 @@ class Object {
276276 /* ! \brief The structural equality and hash kind of the type */
277277 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindUnsupported ;
278278 // The following functions are provided by macro
279- // TVM_FFI_DECLARE_BASE_OBJECT_INFO and TVM_DECLARE_FINAL_OBJECT_INFO
279+ // TVM_FFI_DECLARE_OBJECT_INFO and TVM_FFI_DECLARE_OBJECT_INFO_FINAL
280280 /* !
281281 * \brief Get the runtime allocated type index of the type
282282 * \note Getting this information may need dynamic calls into a global table.
@@ -885,20 +885,24 @@ struct ObjectPtrEqual {
885885// / \endcond
886886
887887/* !
888- * \brief Helper macro to declare a object that comes with static type index.
888+ * \brief Helper macro to declare object information with static type index.
889+ *
890+ * \param TypeKey The type key of the current type.
889891 * \param TypeName The name of the current type.
890892 * \param ParentType The name of the ParentType
891893 */
892- #define TVM_FFI_DECLARE_STATIC_OBJECT_INFO (TypeName, ParentType ) \
893- static int32_t RuntimeTypeIndex () { return TypeName::_type_index; } \
894+ #define TVM_FFI_DECLARE_OBJECT_INFO_STATIC (TypeKey, TypeName, ParentType ) \
895+ static constexpr const char * _type_key = TypeKey; \
896+ static int32_t RuntimeTypeIndex () { return TypeName::_type_index; } \
894897 TVM_FFI_REGISTER_STATIC_TYPE_INFO (TypeName, ParentType)
895898
896899/* !
897- * \brief helper macro to declare a base object type that can be inherited.
900+ * \brief Helper macro to declare object information with type key already defined in class.
901+ *
898902 * \param TypeName The name of the current type.
899903 * \param ParentType The name of the ParentType
900904 */
901- #define TVM_FFI_DECLARE_BASE_OBJECT_INFO (TypeName, ParentType ) \
905+ #define TVM_FFI_DECLARE_OBJECT_INFO_PREDEFINED_TYPE_KEY (TypeName, ParentType ) \
902906 static constexpr int32_t _type_depth = ParentType::_type_depth + 1 ; \
903907 static int32_t _GetOrAllocRuntimeTypeIndex () { \
904908 static_assert (!ParentType::_type_final, " ParentType marked as final" ); \
@@ -916,14 +920,27 @@ struct ObjectPtrEqual {
916920 static inline int32_t _type_index = _GetOrAllocRuntimeTypeIndex()
917921
918922/* !
919- * \brief helper macro to declare type information in a final class.
923+ * \brief Helper macro to declare object information with dynamic type index.
924+ *
925+ * \param TypeKey The type key of the current type.
920926 * \param TypeName The name of the current type.
921927 * \param ParentType The name of the ParentType
922928 */
923- #define TVM_FFI_DECLARE_FINAL_OBJECT_INFO (TypeName, ParentType ) \
924- static const constexpr int _type_child_slots [[maybe_unused]] = 0 ; \
925- static const constexpr bool _type_final [[maybe_unused]] = true ; \
926- TVM_FFI_DECLARE_BASE_OBJECT_INFO (TypeName, ParentType)
929+ #define TVM_FFI_DECLARE_OBJECT_INFO (TypeKey, TypeName, ParentType ) \
930+ static constexpr const char * _type_key = TypeKey; \
931+ TVM_FFI_DECLARE_OBJECT_INFO_PREDEFINED_TYPE_KEY (TypeName, ParentType)
932+
933+ /* !
934+ * \brief Helper macro to declare object information with dynamic type index and is final.
935+ *
936+ * \param TypeKey The type key of the current type.
937+ * \param TypeName The name of the current type.
938+ * \param ParentType The name of the ParentType
939+ */
940+ #define TVM_FFI_DECLARE_OBJECT_INFO_FINAL (TypeKey, TypeName, ParentType ) \
941+ static const constexpr int _type_child_slots [[maybe_unused]] = 0 ; \
942+ static const constexpr bool _type_final [[maybe_unused]] = true ; \
943+ TVM_FFI_DECLARE_OBJECT_INFO (TypeKey, TypeName, ParentType)
927944
928945/* !
929946 * \brief Define object reference methods.
@@ -935,13 +952,15 @@ struct ObjectPtrEqual {
935952 * \note This macro also defines the default constructor that puts the ObjectRef
936953 * in undefined state initially.
937954 */
938- #define TVM_FFI_DEFINE_OBJECT_REF_METHODS (TypeName, ParentType, ObjectName ) \
939- TypeName () = default ; \
940- explicit TypeName (::tvm::ffi::ObjectPtr<ObjectName> n) : ParentType(n) {} \
941- explicit TypeName (::tvm::ffi::UnsafeInit tag) : ParentType(tag) {} \
942- TVM_FFI_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN (TypeName) \
943- const ObjectName* operator ->() const { return static_cast <const ObjectName*>(data_.get ()); } \
944- const ObjectName* get () const { return operator ->(); } \
955+ #define TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE (TypeName, ParentType, ObjectName ) \
956+ TypeName () = default ; \
957+ explicit TypeName (::tvm::ffi::ObjectPtr<ObjectName> n) : ParentType(n) {} \
958+ explicit TypeName (::tvm::ffi::UnsafeInit tag) : ParentType(tag) {} \
959+ TVM_FFI_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN (TypeName) \
960+ using __PtrType = std::conditional_t <ObjectName::_type_mutable, ObjectName*, const ObjectName*>; \
961+ __PtrType operator ->() const { return static_cast <__PtrType>(data_.get ()); } \
962+ __PtrType get () const { return static_cast <__PtrType>(data_.get ()); } \
963+ static constexpr bool _type_is_nullable = true ; \
945964 using ContainerType = ObjectName
946965
947966/* !
@@ -951,46 +970,17 @@ struct ObjectPtrEqual {
951970 * \param ParentType The parent type of the objectref
952971 * \param ObjectName The type name of the object.
953972 */
954- #define TVM_FFI_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS (TypeName, ParentType, ObjectName ) \
955- explicit TypeName (::tvm::ffi::UnsafeInit tag) : ParentType(tag) {} \
956- TVM_FFI_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN (TypeName) \
957- const ObjectName* operator ->() const { return static_cast <const ObjectName*>(data_.get ()); } \
958- const ObjectName* get () const { return operator ->(); } \
959- static constexpr bool _type_is_nullable = false ; \
960- using ContainerType = ObjectName
961-
962- /* !
963- * \brief Define object reference methods of whose content is mutable.
964- * \param TypeName The object type name
965- * \param ParentType The parent type of the objectref
966- * \param ObjectName The type name of the object.
967- * \note We recommend making objects immutable when possible.
968- * This macro is only reserved for objects that stores runtime states.
969- */
970- #define TVM_FFI_DEFINE_MUTABLE_OBJECT_REF_METHODS (TypeName, ParentType, ObjectName ) \
971- TypeName () = default ; \
972- explicit TypeName (::tvm::ffi::UnsafeInit tag) : ParentType(tag) {} \
973- TVM_FFI_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN (TypeName) \
974- explicit TypeName (::tvm::ffi::ObjectPtr<ObjectName> n) : ParentType(n) {} \
975- ObjectName* operator ->() const { return static_cast <ObjectName*>(data_.get ()); } \
976- using ContainerType = ObjectName
977-
978- /* !
979- * \brief Define object reference methods that is both not nullable and mutable.
980- *
981- * \param TypeName The object type name
982- * \param ParentType The parent type of the objectref
983- * \param ObjectName The type name of the object.
984- */
985- #define TVM_FFI_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS (TypeName, ParentType, ObjectName ) \
986- explicit TypeName (::tvm::ffi::UnsafeInit tag) : ParentType(tag) {} \
987- TVM_FFI_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN (TypeName) \
988- ObjectName* operator ->() const { return static_cast <ObjectName*>(data_.get ()); } \
989- ObjectName* get () const { return operator ->(); } \
990- static constexpr bool _type_is_nullable = false ; \
973+ #define TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE (TypeName, ParentType, ObjectName ) \
974+ explicit TypeName (::tvm::ffi::UnsafeInit tag) : ParentType(tag) {} \
975+ TVM_FFI_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN (TypeName) \
976+ using __PtrType = std::conditional_t <ObjectName::_type_mutable, ObjectName*, const ObjectName*>; \
977+ __PtrType operator ->() const { return static_cast <__PtrType>(data_.get ()); } \
978+ __PtrType get () const { return static_cast <__PtrType>(data_.get ()); } \
979+ static constexpr bool _type_is_nullable = false ; \
991980 using ContainerType = ObjectName
992981
993982namespace details {
983+
994984template <typename TargetType>
995985TVM_FFI_INLINE bool IsObjectInstance (int32_t object_type_index) {
996986 static_assert (std::is_base_of_v<Object, TargetType>);
0 commit comments