CppInterOp
C++ Language Interoperability Layer
Loading...
Searching...
No Matches
CXCppInterOp.cpp
Go to the documentation of this file.
1// Hand-written C API wrappers for functions the TableGen emitter
2// cannot generate mechanically, plus C-ABI-shaped overloads of Cpp::
3// APIs whose primary form (in CppInterOp.cpp) cannot cross the C
4// boundary. Public declarations of these symbols live in
5// include/CppInterOp/CXCppInterOp.h; generated wrappers live in
6// CXCppInterOpGenerated.cpp.
7
9#include "Unwrap.h"
11
12#include "Compatibility.h"
13
14#include <cstdlib>
15#include <cstring>
16#include <vector>
17
18#if defined(__has_feature)
19#if __has_feature(memory_sanitizer)
20#include <sanitizer/msan_interface.h>
21#define CPPINTEROP_MSAN_UNPOISON_VALUE(v) __msan_unpoison(&(v), sizeof(v))
22#else
23#define CPPINTEROP_MSAN_UNPOISON_VALUE(v) ((void)0)
24#endif
25#else
26#define CPPINTEROP_MSAN_UNPOISON_VALUE(v) ((void)0)
27#endif
28
29namespace Cpp {
30
31// Legacy C-ABI overload of Cpp::Evaluate. The Box-returning overload in
32// CppInterOp.cpp cannot cross the C boundary; bindings that go through
33// the generated cppinterop_Evaluate_intptr wrapper (e.g. cppyy) land
34// here instead.
35intptr_t Evaluate(const char* code, bool* HadError) {
36 auto* I = unwrap<compat::Interpreter>(GetInterpreter());
38
39 if (HadError)
40 *HadError = false;
41
42 auto res = I->evaluate(code, V);
44 if (res != 0 || !V.hasValue()) {
45 if (HadError)
46 *HadError = true;
47 // FIXME: Make this return llvm::Expected.
48 return ~0UL;
49 }
50
51 return compat::convertTo<intptr_t>(V);
52}
53
54} // namespace Cpp
55
56extern "C" {
57
58// C-ABI bridge for the intptr_t Cpp::Evaluate overload above. The Box-
59// returning overload generated from CppInterOp.td is C++-only.
60CPPINTEROP_API intptr_t cppinterop_Evaluate(const char* code, bool* HadError) {
61 return Cpp::Evaluate(code, HadError);
62}
63
64// GetClassTemplatedMethods returns bool AND fills a vector out-param.
65// The C wrapper drops the bool (caller checks arr.size > 0 instead).
66CPPINTEROP_API Cpp::CppInterOpArray
68 std::vector<Cpp::FuncRef> out;
69 Cpp::GetClassTemplatedMethods(std::string(name), parent, out);
70 Cpp::CppInterOpArray arr = {nullptr, out.size()};
71 if (arr.size) {
72 arr.data = static_cast<void**>(malloc(arr.size * sizeof(void*)));
73 memcpy(arr.data, out.data(), arr.size * sizeof(void*));
74 }
75 return arr;
76}
77
78} // extern "C"
intptr_t cppinterop_Evaluate(const char *code, bool *HadError)
C-ABI overload of Cpp::Evaluate.
Cpp::CppInterOpArray cppinterop_GetClassTemplatedMethods(const char *name, CppConstDeclRef parent)
Returns the templated method scopes inside parent matching name.
#define CPPINTEROP_API
#define CPPINTEROP_MSAN_UNPOISON_VALUE(v)
Definition Box.h:70
bool GetClassTemplatedMethods(const std::string &name, ConstDeclRef parent, std::vector< FuncRef > &funcs)
Box Evaluate(const char *code)
InterpRef GetInterpreter()
clang::Value Value