/*
 * 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 src/ir/function.cc
 * \brief The function data structure.
 */
#include <tvm/ffi/function.h>
#include <tvm/ffi/reflection/registry.h>
#include <tvm/ffi/rvalue_ref.h>
#include <tvm/ir/function.h>
#include <tvm/relax/expr.h>
#include <tvm/tirx/function.h>

namespace tvm {

TVM_FFI_STATIC_INIT_BLOCK() {
  namespace refl = tvm::ffi::reflection;
  refl::GlobalDef()
      .def("ir.BaseFunc_Attrs", [](BaseFunc func) { return func->attrs; })
      .def("ir.BaseFuncCopy", [](BaseFunc func) { return func; })
      .def("ir.BaseFuncWithAttr",
           [](ffi::RValueRef<BaseFunc> func_ref, ffi::String key, Any value) -> BaseFunc {
             BaseFunc func = *std::move(func_ref);
             if (func->IsInstance<tirx::PrimFuncNode>()) {
               return WithAttr(std::move(func).as_or_throw<tirx::PrimFunc>(), key, value);
             } else if (func->IsInstance<relax::FunctionNode>()) {
               return WithAttr(std::move(func).as_or_throw<relax::Function>(), key, value);
             } else if (func->IsInstance<relax::ExternFuncNode>()) {
               return WithAttr(std::move(func).as_or_throw<relax::ExternFunc>(), key, value);
             } else {
               TVM_FFI_THROW(InternalError)
                   << "Do not support function type " << func->GetTypeKey();
             }
           })
      .def("ir.BaseFuncWithAttrs",
           [](ffi::RValueRef<BaseFunc> func_ref,
              ffi::Map<ffi::String, ffi::Any> attr_map) -> BaseFunc {
             BaseFunc func = *std::move(func_ref);
             if (func->IsInstance<tirx::PrimFuncNode>()) {
               return WithAttrs(std::move(func).as_or_throw<tirx::PrimFunc>(), attr_map);
             }
             if (const auto f = tvm::ffi::Function::GetGlobal("relax.FuncWithAttrs")) {
               if (auto ret = (*f)(func, attr_map).cast<ffi::Optional<BaseFunc>>()) {
                 return ret.value();
               }
             }
             if (func->IsInstance<relax::ExternFuncNode>()) {
               return WithAttrs(std::move(func).as_or_throw<relax::ExternFunc>(), attr_map);
             }
             TVM_FFI_THROW(InternalError) << "Do not support function type " << func->GetTypeKey();
             TVM_FFI_UNREACHABLE();
           })
      .def("ir.BaseFuncWithoutAttr",
           [](ffi::RValueRef<BaseFunc> func_ref, ffi::String key) -> BaseFunc {
             BaseFunc func = *std::move(func_ref);
             if (func->IsInstance<tirx::PrimFuncNode>()) {
               return WithoutAttr(std::move(func).as_or_throw<tirx::PrimFunc>(), key);
             } else if (func->IsInstance<relax::FunctionNode>()) {
               return WithoutAttr(std::move(func).as_or_throw<relax::Function>(), key);
             } else {
               TVM_FFI_THROW(InternalError)
                   << "Do not support function type " << func->GetTypeKey();
               TVM_FFI_UNREACHABLE();
             }
           });
}

}  // namespace tvm
