llvm/compiler-rt/lib/orc/wrapper_function_utils.h

//===-- wrapper_function_utils.h - Utilities for wrapper funcs --*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of the ORC runtime support library.
//
//===----------------------------------------------------------------------===//

#ifndef ORC_RT_WRAPPER_FUNCTION_UTILS_H
#define ORC_RT_WRAPPER_FUNCTION_UTILS_H

#include "orc_rt/c_api.h"
#include "common.h"
#include "error.h"
#include "executor_address.h"
#include "simple_packed_serialization.h"
#include <type_traits>

namespace __orc_rt {

/// C++ wrapper function result: Same as CWrapperFunctionResult but
/// auto-releases memory.
class WrapperFunctionResult {};

namespace detail {

template <typename RetT> class WrapperFunctionHandlerCaller {};

template <> class WrapperFunctionHandlerCaller<void> {};

template <typename WrapperFunctionImplT,
          template <typename> class ResultSerializer, typename... SPSTagTs>
class WrapperFunctionHandlerHelper
    : public WrapperFunctionHandlerHelper<
          decltype(&std::remove_reference_t<WrapperFunctionImplT>::operator()),
          ResultSerializer, SPSTagTs...> {};

WrapperFunctionHandlerHelper<RetT (ArgTs...), ResultSerializer, SPSTagTs...>;

// Map function pointers to function types.
WrapperFunctionHandlerHelper<RetT (*)(ArgTs...), ResultSerializer, SPSTagTs...>;

// Map non-const member function types to function types.
WrapperFunctionHandlerHelper<RetT (ClassT::*)(ArgTs...), ResultSerializer, SPSTagTs...>;

// Map const member function types to function types.
WrapperFunctionHandlerHelper<RetT (ClassT::*)(ArgTs...) const, ResultSerializer, SPSTagTs...>;

template <typename SPSRetTagT, typename RetT> class ResultSerializer {};

ResultSerializer<SPSRetTagT, Error>;

ResultSerializer<SPSRetTagT, Expected<T>>;

template <typename SPSRetTagT, typename RetT> class ResultDeserializer {};

template <> class ResultDeserializer<SPSError, Error> {};

ResultDeserializer<SPSExpected<SPSTagT>, Expected<T>>;

} // end namespace detail

template <typename SPSSignature> class WrapperFunction;

WrapperFunction<SPSRetTagT (SPSTagTs...)>;

WrapperFunction<void (SPSTagTs...)>;

/// A function object that takes an ExecutorAddr as its first argument,
/// casts that address to a ClassT*, then calls the given method on that
/// pointer passing in the remaining function arguments. This utility
/// removes some of the boilerplate from writing wrappers for method calls.
///
///   @code{.cpp}
///   class MyClass {
///   public:
///     void myMethod(uint32_t, bool) { ... }
///   };
///
///   // SPS Method signature -- note MyClass object address as first argument.
///   using SPSMyMethodWrapperSignature =
///     SPSTuple<SPSExecutorAddr, uint32_t, bool>;
///
///   WrapperFunctionResult
///   myMethodCallWrapper(const char *ArgData, size_t ArgSize) {
///     return WrapperFunction<SPSMyMethodWrapperSignature>::handle(
///        ArgData, ArgSize, makeMethodWrapperHandler(&MyClass::myMethod));
///   }
///   @endcode
///
template <typename RetT, typename ClassT, typename... ArgTs>
class MethodWrapperHandler {};

/// Create a MethodWrapperHandler object from the given method pointer.
template <typename RetT, typename ClassT, typename... ArgTs>
MethodWrapperHandler<RetT, ClassT, ArgTs...>
makeMethodWrapperHandler(RetT (ClassT::*Method)(ArgTs...)) {}

/// Represents a call to a wrapper function.
class WrapperFunctionCall {};

SPSWrapperFunctionCall;

template <>
class SPSSerializationTraits<SPSWrapperFunctionCall, WrapperFunctionCall> {};

} // end namespace __orc_rt

#endif // ORC_RT_WRAPPER_FUNCTION_UTILS_H