CppInterOp
C++ Language Interoperability Layer
Loading...
Searching...
No Matches
CppInterOpThunks.h
Go to the documentation of this file.
1//===--- CppInterOpThunks.h - Variadic dispatcher template ------*- C++ -*-===//
2//
3// Part of the compiler-research project, under the Apache License v2.0 with
4// LLVM Exceptions.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Header-only variadic-template dispatcher that bindings instantiate per
10// signature × slot to populate a precompiled thunk catalog. CppInterOp ships
11// the template body; the binding contributes the per-instance handler lookup
12// and the language-specific marshaling.
13//
14// The binding provides a HandlerTraits struct of this shape:
15//
16// struct HandlerTraits {
17// using Handler = /* binding-specific per-instance state */;
18//
19// // Per-instance lookup -- typically reads the handler pointer from an
20// // extra prefix slot reserved via MakeVTableOverlay's
21// // n_extra_prefix_slots, i.e. vptr[-(kVTableOverlayPrefixSize + 1)].
22// static Handler& From(void* self);
23//
24// // Marshal Args... into the target language, call the bound callable
25// // at index Slot, and marshal the result back as R.
26// template <class R, class... Args>
27// static R Call(Handler&, std::size_t Slot, Args... args);
28// };
29//
30// Each instantiation of dispatch<T, Slot, R, Args...> produces a concrete
31// function with the right calling convention to install in a patched vtable
32// slot. The catalog is a per-binding table of such instantiations indexed by
33// (signature shape, slot).
34//
35//===----------------------------------------------------------------------===//
36
37#ifndef CPPINTEROP_CPPINTEROPTHUNKS_H
38#define CPPINTEROP_CPPINTEROPTHUNKS_H
39
40#include <cstddef>
41
42namespace Cpp {
43namespace Thunks {
44
45template <class T, std::size_t Slot, class R, class... Args>
46R dispatch(void* self, Args... args) {
47 return T::template Call<R, Args...>(T::From(self), Slot, args...);
48}
49
50} // namespace Thunks
51} // namespace Cpp
52
53#endif // CPPINTEROP_CPPINTEROPTHUNKS_H
R dispatch(void *self, Args... args)
Definition Box.h:70