Kathryn-cat commented on code in PR #649:
URL: https://github.com/apache/tvm-ffi/pull/649#discussion_r3603469439


##########
include/tvm/ffi/extra/structural_map.h:
##########
@@ -0,0 +1,684 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*!
+ * \file tvm/ffi/extra/structural_map.h
+ * \brief Structural mapping and in-place mutation API.
+ */
+#ifndef TVM_FFI_EXTRA_STRUCTURAL_MAP_H_
+#define TVM_FFI_EXTRA_STRUCTURAL_MAP_H_
+
+#include <tvm/ffi/any.h>
+#include <tvm/ffi/c_api.h>
+#include <tvm/ffi/cast.h>
+#include <tvm/ffi/container/array.h>
+#include <tvm/ffi/container/tuple.h>
+#include <tvm/ffi/container/variant.h>
+#include <tvm/ffi/expected.h>
+#include <tvm/ffi/extra/visit_error_context.h>
+#include <tvm/ffi/function.h>
+#include <tvm/ffi/function_details.h>
+#include <tvm/ffi/optional.h>
+#include <tvm/ffi/reflection/accessor.h>
+
+#include <cstddef>
+#include <exception>
+#include <optional>
+#include <string>
+#include <string_view>
+#include <tuple>
+#include <type_traits>
+#include <utility>
+
+namespace tvm {
+namespace ffi {
+
+class StructuralMapperObj;
+
+/*!
+ * \brief ABI of structural transformation hooks and \ref 
StructuralMapperVTable callbacks.
+ *
+ * The callback receives the active mapper and value as non-owning arguments. 
It returns raw
+ * ``TVMFFIAny`` storage containing the transformed value for map operations, 
FFI None for a
+ * successful in-place mutation, or an Error on failure.
+ */
+using FStructuralTransform = TVMFFIAny (*)(StructuralMapperObj* mapper, 
AnyView value) noexcept;
+
+namespace details {
+
+/*!
+ * \brief Move a structural transformation result to raw ABI storage and 
annotate failures.
+ *
+ * \tparam T The transformation success type.
+ * \param result The transformation result to move into raw ABI storage.
+ * \param error_context The value retained by the current dispatch frame for 
error reporting.
+ * \return Raw ``TVMFFIAny`` storing the success value or Error.
+ */
+template <typename T>
+TVM_FFI_INLINE static TVMFFIAny MoveStructuralTransformResultToTVMFFIAny(
+    Expected<T> result, AnyView error_context) noexcept {
+  if (TVM_FFI_PREDICT_FALSE(result.is_err())) {
+    if (error_context.type_index() >= TypeIndex::kTVMFFIStaticObjectBegin) {
+      Error err = result.error();
+      UpdateVisitErrorContext(err, error_context.cast<ObjectRef>());
+    }
+  }
+  return ExpectedUnsafe::MoveToTVMFFIAny(std::move(result));
+}
+
+// Dispatch a type-specific structural map or in-place mutation hook.
+template <typename T>
+TVM_FFI_INLINE static Expected<T> 
DispatchTypeAttrHookExpected(StructuralMapperObj* mapper,
+                                                               AnyView value, 
AnyView attr,
+                                                               
std::string_view attr_name) noexcept;
+
+// Copy and structurally map the reflected fields of an object-backed value.
+TVM_FFI_INLINE static Expected<Any> 
MapReflectedFieldsExpected(StructuralMapperObj* mapper,
+                                                               AnyView value) 
noexcept;
+
+// Structurally transform the reflected fields of an object-backed value in 
place.
+TVM_FFI_INLINE static Expected<void> InplaceMutateReflectedFieldsExpected(
+    StructuralMapperObj* mapper, AnyView value) noexcept;
+
+}  // namespace details
+
+/*!
+ * \brief VTable ABI for \ref StructuralMapper dispatch. This function table 
provides a stable ABI
+ * for the map and in-place mutation methods.
+ */
+struct StructuralMapperVTable {
+  /*!
+   * \brief Select mapping or in-place mutation for a value.
+   * \param mapper The active structural mapper.
+   * \param value The borrowed value to transform by map or in-place mutation.
+   * \return Raw ``TVMFFIAny`` carrying the transformed value or Error.
+   */
+  FStructuralTransform map_or_inplace_mutate = nullptr;
+  /*!
+   * \brief Map a value without intentionally mutating the source.
+   * \param mapper The active structural mapper.
+   * \param value The borrowed value to map.
+   * \return Raw ``TVMFFIAny`` carrying the mapped value or Error.
+   */
+  FStructuralTransform map = nullptr;
+  /*!
+   * \brief Transform a value using the explicit in-place mutation path.
+   * \param mapper The active structural mapper.
+   * \param value The borrowed value to transform in place.
+   * \return Raw ``TVMFFIAny`` carrying None on success or Error on failure.
+   */
+  FStructuralTransform inplace_mutate = nullptr;
+};
+
+/*!
+ * \brief Object node of a structural mapper.
+ *
+ * A structural mapper recursively transforms values through a manually 
supplied ABI vtable.
+ * The default behavior supports type-specific hooks and reflected-field 
fallback. Values
+ * are accepted as borrowed views so dispatch does not add temporary owners 
and the default combined
+ * operation can select in-place mutation from logical uniqueness.
+ */
+class StructuralMapperObj : public Object {
+ public:
+  /*! \brief Construct the default structural mapper. */
+  StructuralMapperObj() : StructuralMapperObj(VTable()) {}
+
+  /*!
+   * \brief Transform a value, selecting mapping or in-place mutation through 
the mapper vtable.
+   *
+   * \param value The borrowed value to transform.
+   * \return The transformed value.
+   *
+   * \note This method throws the returned Error on failure. Use
+   *       \ref MapOrInplaceMutateExpected for exception-free propagation.
+   */
+  TVM_FFI_INLINE Any MapOrInplaceMutate(AnyView value) {
+    return MapOrInplaceMutateExpected(value).value();
+  }
+
+  /*!
+   * \brief Exception-free form of \ref MapOrInplaceMutate.
+   *
+   * \param value The borrowed value to transform.
+   * \return The transformed value, or an Error if transformation failed.
+   */
+  TVM_FFI_INLINE Expected<Any> MapOrInplaceMutateExpected(AnyView value) 
noexcept {
+    return details::ExpectedUnsafe::MoveFromTVMFFIAny<Any>(
+        (*vtable_->map_or_inplace_mutate)(this, value));
+  }
+
+  /*!
+   * \brief Apply the default map-or-in-place selection logic.
+   *
+   * \param value The value to transform by map or in-place mutation.
+   * \return The transformed value.
+   */
+  TVM_FFI_INLINE Any DefaultMapOrInplaceMutate(AnyView value) {
+    return DefaultMapOrInplaceMutateExpected(value).value();
+  }
+
+  /*!
+   * \brief Exception-free form of \ref DefaultMapOrInplaceMutate.
+   *
+   * \param value The value to transform by map or in-place mutation.
+   * \return The transformed value, or an Error if selection or transformation 
failed.
+   */
+  TVM_FFI_INLINE Expected<Any> DefaultMapOrInplaceMutateExpected(AnyView 
value) noexcept {
+    return DefaultMapOrInplaceMutateExpected(value, false);
+  }
+
+  /*!
+   * \brief Map a value through the mapper vtable.
+   *
+   * \param value The value to map.
+   * \return The mapped value.
+   *
+   * \note This method throws the returned Error on failure. Use \ref 
MapExpected for
+   *       exception-free propagation.
+   */
+  TVM_FFI_INLINE Any Map(AnyView value) { return MapExpected(value).value(); }
+
+  /*!
+   * \brief Exception-free form of \ref Map.
+   *
+   * \param value The value to map.
+   * \return The mapped value, or an Error if mapping failed.
+   */
+  TVM_FFI_INLINE Expected<Any> MapExpected(AnyView value) noexcept {
+    return 
details::ExpectedUnsafe::MoveFromTVMFFIAny<Any>((*vtable_->map)(this, value));
+  }
+
+  /*!
+   * \brief Apply the default structural map with copy-on-write behavior.
+   *
+   * \param value The value to map.
+   * \return The mapped value.
+   */
+  TVM_FFI_INLINE Any DefaultMap(AnyView value) { return 
DefaultMapExpected(value).value(); }
+
+  /*!
+   * \brief Exception-free form of \ref DefaultMap.
+   *
+   * \param value The value to map.
+   * \return The mapped value, or an Error if hook dispatch, copying, or field 
mapping failed.
+   */
+  TVM_FFI_INLINE Expected<Any> DefaultMapExpected(AnyView value) noexcept {
+    int32_t type_index = value.type_index();
+    static reflection::TypeAttrColumn 
column(reflection::type_attr::kStructuralMap);
+    AnyView attr = column[type_index];
+    if (attr.type_index() != TypeIndex::kTVMFFINone) {
+      return details::DispatchTypeAttrHookExpected<Any>(this, value, attr,
+                                                        
reflection::type_attr::kStructuralMap);
+    }
+    if (type_index < TypeIndex::kTVMFFIStaticObjectBegin) {
+      return Any(value);
+    }
+    return details::MapReflectedFieldsExpected(this, value);
+  }
+
+  /*!
+   * \brief Transform a value through the explicit in-place mutation vtable 
entry.
+   *
+   * \param value The value to transform in place.
+   * \return Nothing. The input value is mutated directly.
+   */
+  TVM_FFI_INLINE void InplaceMutate(AnyView value) { 
InplaceMutateExpected(value).value(); }
+
+  /*!
+   * \brief Exception-free form of \ref InplaceMutate.
+   *
+   * \param value The value to transform in place.
+   * \return Successful completion, or an Error if transformation failed.
+   */
+  TVM_FFI_INLINE Expected<void> InplaceMutateExpected(AnyView value) noexcept {
+    return details::ExpectedUnsafe::MoveFromTVMFFIAny<void>(
+        (*vtable_->inplace_mutate)(this, value));
+  }
+
+  /*!
+   * \brief Apply the default structural in-place mutation.
+   *
+   * \param value The value to transform in place.
+   * \return Nothing. The input value is mutated directly.
+   */
+  TVM_FFI_INLINE void DefaultInplaceMutate(AnyView value) {
+    DefaultInplaceMutateExpected(value).value();
+  }
+
+  /*!
+   * \brief Exception-free form of \ref DefaultInplaceMutate.
+   *
+   * \param value The value to transform in place.
+   * \return Successful completion, or an Error if hook dispatch or reflected 
mutation failed.
+   */
+  TVM_FFI_INLINE Expected<void> DefaultInplaceMutateExpected(AnyView value) 
noexcept {
+    int32_t type_index = value.type_index();
+    static reflection::TypeAttrColumn 
column(reflection::type_attr::kStructuralInplaceMutate);
+    AnyView attr = column[type_index];
+    if (attr.type_index() != TypeIndex::kTVMFFINone) {
+      return details::DispatchTypeAttrHookExpected<void>(
+          this, value, attr, reflection::type_attr::kStructuralInplaceMutate);
+    }
+    if (type_index < TypeIndex::kTVMFFIStaticObjectBegin) {
+      return Expected<void>();
+    }
+    return details::InplaceMutateReflectedFieldsExpected(this, value);
+  }
+
+  /*!
+   * \brief Return the current def-region context.
+   * \return The active def-region kind.
+   */
+  TVM_FFI_INLINE TVMFFIDefRegionKind def_region_kind() const { return 
def_region_mode_; }
+
+  /*!
+   * \brief Temporarily switch the def-region context while invoking \p 
callback.
+   *
+   * This helper scopes updates to the transformation state used by 
def/use-region
+   * aware mappers. The previous state is restored when the callback returns
+   * or throws.
+   *
+   * \param kind The def-region kind to set during the callback.
+   * \param callback A nullary callable that performs recursive transformation.
+   * \return The value returned by \p callback.
+   */
+  template <typename Callback>
+  TVM_FFI_INLINE auto WithDefRegionKind(TVMFFIDefRegionKind kind, Callback&& 
callback) {
+    class Scope {
+     public:
+      Scope(StructuralMapperObj* mapper, TVMFFIDefRegionKind kind)
+          : mapper_(mapper), old_kind_(mapper->def_region_mode_) {
+        mapper_->def_region_mode_ = kind;
+      }
+      ~Scope() { mapper_->def_region_mode_ = old_kind_; }
+      Scope(const Scope&) = delete;
+      Scope& operator=(const Scope&) = delete;
+
+     private:
+      StructuralMapperObj* mapper_;
+      TVMFFIDefRegionKind old_kind_;
+    };
+    Scope scope(this, kind);
+    return std::forward<Callback>(callback)();
+  }
+
+  /// \cond Doxygen_Suppress
+  static constexpr const bool _type_mutable = true;
+  TVM_FFI_DECLARE_OBJECT_INFO("ffi.StructuralMapper", StructuralMapperObj, 
Object);
+  /// \endcond
+
+ protected:
+  /*!
+   * \brief Construct a structural mapper subclass with a custom dispatch 
vtable.
+   *
+   * \param vtable The non-null dispatch table for this mapper.
+   *
+   * \note This constructor is for internal subclasses. The vtable and its
+   *       ``map_or_inplace_mutate`` callback must be valid for the lifetime 
of the mapper.
+   */
+  explicit StructuralMapperObj(const StructuralMapperVTable* vtable) : 
vtable_(vtable) {}
+
+  /*!
+   * \brief Apply default combined transformation with a customized uniqueness 
decision.
+   *
+   * \param value The value to transform.
+   * \param can_inplace_mutate Whether the caller has already established 
logical uniqueness before
+   *        adding temporary internal owners.
+   * \return The transformed value.
+   *
+   * \note This protected overload is for ownership-aware internal recursion. 
Passing ``true`` is
+   *       invalid when the object is genuinely shared.
+   */
+  TVM_FFI_INLINE Any DefaultMapOrInplaceMutate(AnyView value, bool 
can_inplace_mutate) {
+    return DefaultMapOrInplaceMutateExpected(value, 
can_inplace_mutate).value();
+  }
+
+  /*!
+   * \brief Exception-free form of the default transformation with a 
customized uniqueness decision.
+   *
+   * \param value The value to transform.
+   * \param can_inplace_mutate Whether the caller has already established 
logical uniqueness before
+   *        adding temporary internal owners.
+   * \return The transformed value, or an Error if validation or 
transformation failed.
+   */
+  TVM_FFI_INLINE Expected<Any> DefaultMapOrInplaceMutateExpected(AnyView value,

Review Comment:
   Oh it's already a protected member function
   ```py
     TVM_FFI_INLINE Expected<Any> DefaultMapOrInplaceMutateExpected(AnyView 
value,
                                                                    bool 
can_inplace_mutate) noexcept {
       return DefaultMapOrInplaceMutateExpected(value, can_inplace_mutate, 
false);
     }
   ```



##########
include/tvm/ffi/extra/structural_map.h:
##########
@@ -0,0 +1,684 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*!
+ * \file tvm/ffi/extra/structural_map.h
+ * \brief Structural mapping and in-place mutation API.
+ */
+#ifndef TVM_FFI_EXTRA_STRUCTURAL_MAP_H_
+#define TVM_FFI_EXTRA_STRUCTURAL_MAP_H_
+
+#include <tvm/ffi/any.h>
+#include <tvm/ffi/c_api.h>
+#include <tvm/ffi/cast.h>
+#include <tvm/ffi/container/array.h>
+#include <tvm/ffi/container/tuple.h>
+#include <tvm/ffi/container/variant.h>
+#include <tvm/ffi/expected.h>
+#include <tvm/ffi/extra/visit_error_context.h>
+#include <tvm/ffi/function.h>
+#include <tvm/ffi/function_details.h>
+#include <tvm/ffi/optional.h>
+#include <tvm/ffi/reflection/accessor.h>
+
+#include <cstddef>
+#include <exception>
+#include <optional>
+#include <string>
+#include <string_view>
+#include <tuple>
+#include <type_traits>
+#include <utility>
+
+namespace tvm {
+namespace ffi {
+
+class StructuralMapperObj;
+
+/*!
+ * \brief ABI of structural transformation hooks and \ref 
StructuralMapperVTable callbacks.
+ *
+ * The callback receives the active mapper and value as non-owning arguments. 
It returns raw
+ * ``TVMFFIAny`` storage containing the transformed value for map operations, 
FFI None for a
+ * successful in-place mutation, or an Error on failure.
+ */
+using FStructuralTransform = TVMFFIAny (*)(StructuralMapperObj* mapper, 
AnyView value) noexcept;
+
+namespace details {
+
+/*!
+ * \brief Move a structural transformation result to raw ABI storage and 
annotate failures.
+ *
+ * \tparam T The transformation success type.
+ * \param result The transformation result to move into raw ABI storage.
+ * \param error_context The value retained by the current dispatch frame for 
error reporting.
+ * \return Raw ``TVMFFIAny`` storing the success value or Error.
+ */
+template <typename T>
+TVM_FFI_INLINE static TVMFFIAny MoveStructuralTransformResultToTVMFFIAny(
+    Expected<T> result, AnyView error_context) noexcept {
+  if (TVM_FFI_PREDICT_FALSE(result.is_err())) {
+    if (error_context.type_index() >= TypeIndex::kTVMFFIStaticObjectBegin) {
+      Error err = result.error();
+      UpdateVisitErrorContext(err, error_context.cast<ObjectRef>());
+    }
+  }
+  return ExpectedUnsafe::MoveToTVMFFIAny(std::move(result));
+}
+
+// Dispatch a type-specific structural map or in-place mutation hook.
+template <typename T>
+TVM_FFI_INLINE static Expected<T> 
DispatchTypeAttrHookExpected(StructuralMapperObj* mapper,
+                                                               AnyView value, 
AnyView attr,
+                                                               
std::string_view attr_name) noexcept;
+
+// Copy and structurally map the reflected fields of an object-backed value.
+TVM_FFI_INLINE static Expected<Any> 
MapReflectedFieldsExpected(StructuralMapperObj* mapper,
+                                                               AnyView value) 
noexcept;
+
+// Structurally transform the reflected fields of an object-backed value in 
place.
+TVM_FFI_INLINE static Expected<void> InplaceMutateReflectedFieldsExpected(
+    StructuralMapperObj* mapper, AnyView value) noexcept;
+
+}  // namespace details
+
+/*!
+ * \brief VTable ABI for \ref StructuralMapper dispatch. This function table 
provides a stable ABI
+ * for the map and in-place mutation methods.
+ */
+struct StructuralMapperVTable {
+  /*!
+   * \brief Select mapping or in-place mutation for a value.
+   * \param mapper The active structural mapper.
+   * \param value The borrowed value to transform by map or in-place mutation.
+   * \return Raw ``TVMFFIAny`` carrying the transformed value or Error.
+   */
+  FStructuralTransform map_or_inplace_mutate = nullptr;
+  /*!
+   * \brief Map a value without intentionally mutating the source.
+   * \param mapper The active structural mapper.
+   * \param value The borrowed value to map.
+   * \return Raw ``TVMFFIAny`` carrying the mapped value or Error.
+   */
+  FStructuralTransform map = nullptr;
+  /*!
+   * \brief Transform a value using the explicit in-place mutation path.
+   * \param mapper The active structural mapper.
+   * \param value The borrowed value to transform in place.
+   * \return Raw ``TVMFFIAny`` carrying None on success or Error on failure.
+   */
+  FStructuralTransform inplace_mutate = nullptr;
+};
+
+/*!
+ * \brief Object node of a structural mapper.
+ *
+ * A structural mapper recursively transforms values through a manually 
supplied ABI vtable.
+ * The default behavior supports type-specific hooks and reflected-field 
fallback. Values
+ * are accepted as borrowed views so dispatch does not add temporary owners 
and the default combined
+ * operation can select in-place mutation from logical uniqueness.
+ */
+class StructuralMapperObj : public Object {
+ public:
+  /*! \brief Construct the default structural mapper. */
+  StructuralMapperObj() : StructuralMapperObj(VTable()) {}
+
+  /*!
+   * \brief Transform a value, selecting mapping or in-place mutation through 
the mapper vtable.
+   *
+   * \param value The borrowed value to transform.
+   * \return The transformed value.
+   *
+   * \note This method throws the returned Error on failure. Use
+   *       \ref MapOrInplaceMutateExpected for exception-free propagation.
+   */
+  TVM_FFI_INLINE Any MapOrInplaceMutate(AnyView value) {
+    return MapOrInplaceMutateExpected(value).value();
+  }
+
+  /*!
+   * \brief Exception-free form of \ref MapOrInplaceMutate.
+   *
+   * \param value The borrowed value to transform.
+   * \return The transformed value, or an Error if transformation failed.
+   */
+  TVM_FFI_INLINE Expected<Any> MapOrInplaceMutateExpected(AnyView value) 
noexcept {
+    return details::ExpectedUnsafe::MoveFromTVMFFIAny<Any>(
+        (*vtable_->map_or_inplace_mutate)(this, value));
+  }
+
+  /*!
+   * \brief Apply the default map-or-in-place selection logic.
+   *
+   * \param value The value to transform by map or in-place mutation.
+   * \return The transformed value.
+   */
+  TVM_FFI_INLINE Any DefaultMapOrInplaceMutate(AnyView value) {
+    return DefaultMapOrInplaceMutateExpected(value).value();
+  }
+
+  /*!
+   * \brief Exception-free form of \ref DefaultMapOrInplaceMutate.
+   *
+   * \param value The value to transform by map or in-place mutation.
+   * \return The transformed value, or an Error if selection or transformation 
failed.
+   */
+  TVM_FFI_INLINE Expected<Any> DefaultMapOrInplaceMutateExpected(AnyView 
value) noexcept {
+    return DefaultMapOrInplaceMutateExpected(value, false);
+  }
+
+  /*!
+   * \brief Map a value through the mapper vtable.
+   *
+   * \param value The value to map.
+   * \return The mapped value.
+   *
+   * \note This method throws the returned Error on failure. Use \ref 
MapExpected for
+   *       exception-free propagation.
+   */
+  TVM_FFI_INLINE Any Map(AnyView value) { return MapExpected(value).value(); }
+
+  /*!
+   * \brief Exception-free form of \ref Map.
+   *
+   * \param value The value to map.
+   * \return The mapped value, or an Error if mapping failed.
+   */
+  TVM_FFI_INLINE Expected<Any> MapExpected(AnyView value) noexcept {
+    return 
details::ExpectedUnsafe::MoveFromTVMFFIAny<Any>((*vtable_->map)(this, value));
+  }
+
+  /*!
+   * \brief Apply the default structural map with copy-on-write behavior.
+   *
+   * \param value The value to map.
+   * \return The mapped value.
+   */
+  TVM_FFI_INLINE Any DefaultMap(AnyView value) { return 
DefaultMapExpected(value).value(); }
+
+  /*!
+   * \brief Exception-free form of \ref DefaultMap.
+   *
+   * \param value The value to map.
+   * \return The mapped value, or an Error if hook dispatch, copying, or field 
mapping failed.
+   */
+  TVM_FFI_INLINE Expected<Any> DefaultMapExpected(AnyView value) noexcept {
+    int32_t type_index = value.type_index();
+    static reflection::TypeAttrColumn 
column(reflection::type_attr::kStructuralMap);
+    AnyView attr = column[type_index];
+    if (attr.type_index() != TypeIndex::kTVMFFINone) {
+      return details::DispatchTypeAttrHookExpected<Any>(this, value, attr,
+                                                        
reflection::type_attr::kStructuralMap);
+    }
+    if (type_index < TypeIndex::kTVMFFIStaticObjectBegin) {
+      return Any(value);
+    }
+    return details::MapReflectedFieldsExpected(this, value);
+  }
+
+  /*!
+   * \brief Transform a value through the explicit in-place mutation vtable 
entry.
+   *
+   * \param value The value to transform in place.
+   * \return Nothing. The input value is mutated directly.
+   */
+  TVM_FFI_INLINE void InplaceMutate(AnyView value) { 
InplaceMutateExpected(value).value(); }
+
+  /*!
+   * \brief Exception-free form of \ref InplaceMutate.
+   *
+   * \param value The value to transform in place.
+   * \return Successful completion, or an Error if transformation failed.
+   */
+  TVM_FFI_INLINE Expected<void> InplaceMutateExpected(AnyView value) noexcept {
+    return details::ExpectedUnsafe::MoveFromTVMFFIAny<void>(
+        (*vtable_->inplace_mutate)(this, value));
+  }
+
+  /*!
+   * \brief Apply the default structural in-place mutation.
+   *
+   * \param value The value to transform in place.
+   * \return Nothing. The input value is mutated directly.
+   */
+  TVM_FFI_INLINE void DefaultInplaceMutate(AnyView value) {
+    DefaultInplaceMutateExpected(value).value();
+  }
+
+  /*!
+   * \brief Exception-free form of \ref DefaultInplaceMutate.
+   *
+   * \param value The value to transform in place.
+   * \return Successful completion, or an Error if hook dispatch or reflected 
mutation failed.
+   */
+  TVM_FFI_INLINE Expected<void> DefaultInplaceMutateExpected(AnyView value) 
noexcept {
+    int32_t type_index = value.type_index();
+    static reflection::TypeAttrColumn 
column(reflection::type_attr::kStructuralInplaceMutate);
+    AnyView attr = column[type_index];
+    if (attr.type_index() != TypeIndex::kTVMFFINone) {
+      return details::DispatchTypeAttrHookExpected<void>(
+          this, value, attr, reflection::type_attr::kStructuralInplaceMutate);
+    }
+    if (type_index < TypeIndex::kTVMFFIStaticObjectBegin) {
+      return Expected<void>();
+    }
+    return details::InplaceMutateReflectedFieldsExpected(this, value);
+  }
+
+  /*!
+   * \brief Return the current def-region context.
+   * \return The active def-region kind.
+   */
+  TVM_FFI_INLINE TVMFFIDefRegionKind def_region_kind() const { return 
def_region_mode_; }
+
+  /*!
+   * \brief Temporarily switch the def-region context while invoking \p 
callback.
+   *
+   * This helper scopes updates to the transformation state used by 
def/use-region
+   * aware mappers. The previous state is restored when the callback returns
+   * or throws.
+   *
+   * \param kind The def-region kind to set during the callback.
+   * \param callback A nullary callable that performs recursive transformation.
+   * \return The value returned by \p callback.
+   */
+  template <typename Callback>
+  TVM_FFI_INLINE auto WithDefRegionKind(TVMFFIDefRegionKind kind, Callback&& 
callback) {
+    class Scope {
+     public:
+      Scope(StructuralMapperObj* mapper, TVMFFIDefRegionKind kind)
+          : mapper_(mapper), old_kind_(mapper->def_region_mode_) {
+        mapper_->def_region_mode_ = kind;
+      }
+      ~Scope() { mapper_->def_region_mode_ = old_kind_; }
+      Scope(const Scope&) = delete;
+      Scope& operator=(const Scope&) = delete;
+
+     private:
+      StructuralMapperObj* mapper_;
+      TVMFFIDefRegionKind old_kind_;
+    };
+    Scope scope(this, kind);
+    return std::forward<Callback>(callback)();
+  }
+
+  /// \cond Doxygen_Suppress
+  static constexpr const bool _type_mutable = true;
+  TVM_FFI_DECLARE_OBJECT_INFO("ffi.StructuralMapper", StructuralMapperObj, 
Object);
+  /// \endcond
+
+ protected:
+  /*!
+   * \brief Construct a structural mapper subclass with a custom dispatch 
vtable.
+   *
+   * \param vtable The non-null dispatch table for this mapper.
+   *
+   * \note This constructor is for internal subclasses. The vtable and its
+   *       ``map_or_inplace_mutate`` callback must be valid for the lifetime 
of the mapper.
+   */
+  explicit StructuralMapperObj(const StructuralMapperVTable* vtable) : 
vtable_(vtable) {}
+
+  /*!
+   * \brief Apply default combined transformation with a customized uniqueness 
decision.
+   *
+   * \param value The value to transform.
+   * \param can_inplace_mutate Whether the caller has already established 
logical uniqueness before
+   *        adding temporary internal owners.
+   * \return The transformed value.
+   *
+   * \note This protected overload is for ownership-aware internal recursion. 
Passing ``true`` is
+   *       invalid when the object is genuinely shared.
+   */
+  TVM_FFI_INLINE Any DefaultMapOrInplaceMutate(AnyView value, bool 
can_inplace_mutate) {
+    return DefaultMapOrInplaceMutateExpected(value, 
can_inplace_mutate).value();
+  }
+
+  /*!
+   * \brief Exception-free form of the default transformation with a 
customized uniqueness decision.
+   *
+   * \param value The value to transform.
+   * \param can_inplace_mutate Whether the caller has already established 
logical uniqueness before
+   *        adding temporary internal owners.
+   * \return The transformed value, or an Error if validation or 
transformation failed.
+   */
+  TVM_FFI_INLINE Expected<Any> DefaultMapOrInplaceMutateExpected(AnyView value,

Review Comment:
   it's already a protected member function
   ```py
     TVM_FFI_INLINE Expected<Any> DefaultMapOrInplaceMutateExpected(AnyView 
value,
                                                                    bool 
can_inplace_mutate) noexcept {
       return DefaultMapOrInplaceMutateExpected(value, can_inplace_mutate, 
false);
     }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to