|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +/*! |
| 20 | + * \file tvm/ffi/reflection/creator.h |
| 21 | + * \brief Reflection-based creator to create objects from type key and fields. |
| 22 | + */ |
| 23 | +#ifndef TVM_FFI_REFLECTION_CREATOR_H_ |
| 24 | +#define TVM_FFI_REFLECTION_CREATOR_H_ |
| 25 | + |
| 26 | +#include <tvm/ffi/any.h> |
| 27 | +#include <tvm/ffi/container/map.h> |
| 28 | +#include <tvm/ffi/reflection/accessor.h> |
| 29 | +#include <tvm/ffi/string.h> |
| 30 | + |
| 31 | +namespace tvm { |
| 32 | +namespace ffi { |
| 33 | +namespace reflection { |
| 34 | +/*! |
| 35 | + * \brief helper wrapper class of TVMFFITypeInfo to create object based on reflection. |
| 36 | + */ |
| 37 | +class ObjectCreator { |
| 38 | + public: |
| 39 | + explicit ObjectCreator(std::string_view type_key) |
| 40 | + : ObjectCreator(TVMFFIGetTypeInfo(TypeKeyToIndex(type_key))) {} |
| 41 | + |
| 42 | + explicit ObjectCreator(const TVMFFITypeInfo* type_info) : type_info_(type_info) { |
| 43 | + int32_t type_index = type_info->type_index; |
| 44 | + if (type_info->metadata == nullptr) { |
| 45 | + TVM_FFI_THROW(RuntimeError) << "Type `" << TypeIndexToTypeKey(type_index) |
| 46 | + << "` does not have reflection registered"; |
| 47 | + } |
| 48 | + if (type_info->metadata->creator == nullptr) { |
| 49 | + TVM_FFI_THROW(RuntimeError) << "Type `" << TypeIndexToTypeKey(type_index) |
| 50 | + << "` does not support default constructor, " |
| 51 | + << "as a result cannot be created via reflection"; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * \brief Create an object from a map of fields. |
| 57 | + * \param fields The fields of the object. |
| 58 | + * \return The created object. |
| 59 | + */ |
| 60 | + Any operator()(const Map<String, Any>& fields) const { |
| 61 | + TVMFFIObjectHandle handle; |
| 62 | + TVM_FFI_CHECK_SAFE_CALL(type_info_->metadata->creator(&handle)); |
| 63 | + ObjectPtr<Object> ptr = |
| 64 | + details::ObjectUnsafe::ObjectPtrFromOwned<Object>(static_cast<TVMFFIObject*>(handle)); |
| 65 | + size_t match_field_count = 0; |
| 66 | + ForEachFieldInfo(type_info_, [&](const TVMFFIFieldInfo* field_info) { |
| 67 | + String field_name(field_info->name); |
| 68 | + void* field_addr = reinterpret_cast<char*>(ptr.get()) + field_info->offset; |
| 69 | + if (fields.count(field_name) != 0) { |
| 70 | + Any field_value = fields[field_name]; |
| 71 | + field_info->setter(field_addr, reinterpret_cast<const TVMFFIAny*>(&field_value)); |
| 72 | + ++match_field_count; |
| 73 | + } else if (field_info->flags & kTVMFFIFieldFlagBitMaskHasDefault) { |
| 74 | + field_info->setter(field_addr, &(field_info->default_value)); |
| 75 | + } else { |
| 76 | + TVM_FFI_THROW(TypeError) << "Required field `" |
| 77 | + << String(field_info->name.data, field_info->name.size) |
| 78 | + << "` not set in type `" |
| 79 | + << String(type_info_->type_key.data, type_info_->type_key.size) |
| 80 | + << "`"; |
| 81 | + } |
| 82 | + }); |
| 83 | + if (match_field_count == fields.size()) return ObjectRef(ptr); |
| 84 | + // report error that checks if contains extra fields that are not in the type |
| 85 | + auto check_field_name = [&](const String& field_name) { |
| 86 | + bool found = false; |
| 87 | + ForEachFieldInfoWithEarlyStop(type_info_, [&](const TVMFFIFieldInfo* field_info) { |
| 88 | + if (field_name.compare(field_info->name) == 0) { |
| 89 | + found = true; |
| 90 | + return true; |
| 91 | + } |
| 92 | + return false; |
| 93 | + }); |
| 94 | + return found; |
| 95 | + }; |
| 96 | + for (const auto& [field_name, _] : fields) { |
| 97 | + if (!check_field_name(field_name)) { |
| 98 | + TVM_FFI_THROW(TypeError) << "Type `" |
| 99 | + << String(type_info_->type_key.data, type_info_->type_key.size) |
| 100 | + << "` does not have field `" << field_name << "`"; |
| 101 | + } |
| 102 | + } |
| 103 | + TVM_FFI_UNREACHABLE(); |
| 104 | + } |
| 105 | + |
| 106 | + private: |
| 107 | + const TVMFFITypeInfo* type_info_; |
| 108 | +}; |
| 109 | +} // namespace reflection |
| 110 | +} // namespace ffi |
| 111 | +} // namespace tvm |
| 112 | +#endif // TVM_FFI_REFLECTION_CREATOR_H_ |
0 commit comments