Skip to content

Commit 9acb9e8

Browse files
committed
Add new API for multiple interpreter instance support
also fix argument type typos and c-style casts
1 parent c0a4dfe commit 9acb9e8

File tree

2 files changed

+83
-47
lines changed

2 files changed

+83
-47
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ namespace Cpp {
7777
/// function, constructor or destructor.
7878
class JitCall {
7979
public:
80-
friend CPPINTEROP_API JitCall MakeFunctionCallable(TCppConstFunction_t);
80+
friend CPPINTEROP_API JitCall
81+
MakeFunctionCallable(TInterp_t I, TCppConstFunction_t func);
8182
enum Kind : char {
8283
kUnknown = 0,
8384
kGenericCall,
@@ -262,16 +263,16 @@ namespace Cpp {
262263

263264
/// This is similar to GetName() function, but besides
264265
/// the name, it also gets the template arguments.
265-
CPPINTEROP_API std::string GetCompleteName(TCppType_t klass);
266+
CPPINTEROP_API std::string GetCompleteName(TCppScope_t klass);
266267

267268
/// Gets the "qualified" name (including the namespace) of any
268269
/// named decl (a class, namespace, variable, or a function).
269-
CPPINTEROP_API std::string GetQualifiedName(TCppType_t klass);
270+
CPPINTEROP_API std::string GetQualifiedName(TCppScope_t klass);
270271

271272
/// This is similar to GetQualifiedName() function, but besides
272273
/// the "qualified" name (including the namespace), it also
273274
/// gets the template arguments.
274-
CPPINTEROP_API std::string GetQualifiedCompleteName(TCppType_t klass);
275+
CPPINTEROP_API std::string GetQualifiedCompleteName(TCppScope_t klass);
275276

276277
/// Gets the list of namespaces utilized in the supplied scope.
277278
CPPINTEROP_API std::vector<TCppScope_t> GetUsingNamespaces(TCppScope_t scope);
@@ -307,13 +308,13 @@ namespace Cpp {
307308

308309
/// Gets the number of Base Classes for the Derived Class that
309310
/// is passed as a parameter.
310-
CPPINTEROP_API TCppIndex_t GetNumBases(TCppType_t klass);
311+
CPPINTEROP_API TCppIndex_t GetNumBases(TCppScope_t klass);
311312

312313
/// Gets a specific Base Class using its index. Typically GetNumBases()
313314
/// is used to get the number of Base Classes, and then that number
314315
/// can be used to iterate through the index value to get each specific
315316
/// base class.
316-
CPPINTEROP_API TCppScope_t GetBaseClass(TCppType_t klass, TCppIndex_t ibase);
317+
CPPINTEROP_API TCppScope_t GetBaseClass(TCppScope_t klass, TCppIndex_t ibase);
317318

318319
/// Checks if the supplied Derived Class is a sub-class of the
319320
/// provided Base Class.
@@ -496,6 +497,9 @@ namespace Cpp {
496497
/// uniform interface to call it from compiled code.
497498
CPPINTEROP_API JitCall MakeFunctionCallable(TCppConstFunction_t func);
498499

500+
CPPINTEROP_API JitCall MakeFunctionCallable(TInterp_t I,
501+
TCppConstFunction_t func);
502+
499503
/// Checks if a function declared is of const type or not.
500504
CPPINTEROP_API bool IsConstMethod(TCppFunction_t method);
501505

lib/Interpreter/CppInterOp.cpp

Lines changed: 73 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ namespace Cpp {
857857

858858
TCppIndex_t GetFunctionRequiredArgs(TCppConstFunction_t func)
859859
{
860-
auto *D = (const clang::Decl *) func;
860+
const auto* D = static_cast<const clang::Decl*>(func);
861861
if (auto* FD = llvm::dyn_cast_or_null<FunctionDecl>(D))
862862
return FD->getMinRequiredArguments();
863863

@@ -923,7 +923,8 @@ namespace Cpp {
923923
}
924924

925925
bool IsFunctionDeleted(TCppConstFunction_t function) {
926-
auto *FD = cast<const FunctionDecl>((const clang::Decl*)function);
926+
const auto* FD =
927+
cast<const FunctionDecl>(static_cast<const clang::Decl*>(function));
927928
return FD->isDeleted();
928929
}
929930

@@ -1054,7 +1055,8 @@ namespace Cpp {
10541055

10551056
bool IsMethod(TCppConstFunction_t method)
10561057
{
1057-
return dyn_cast_or_null<CXXMethodDecl>((const clang::Decl*)method);
1058+
return dyn_cast_or_null<CXXMethodDecl>(
1059+
static_cast<const clang::Decl*>(method));
10581060
}
10591061

10601062
bool IsPublicMethod(TCppFunction_t method)
@@ -1073,18 +1075,18 @@ namespace Cpp {
10731075

10741076
bool IsConstructor(TCppConstFunction_t method)
10751077
{
1076-
auto *D = (const Decl *) method;
1078+
const auto* D = static_cast<const Decl*>(method);
10771079
return llvm::isa_and_nonnull<CXXConstructorDecl>(D);
10781080
}
10791081

10801082
bool IsDestructor(TCppConstFunction_t method)
10811083
{
1082-
auto *D = (const Decl *) method;
1084+
const auto* D = static_cast<const Decl*>(method);
10831085
return llvm::isa_and_nonnull<CXXDestructorDecl>(D);
10841086
}
10851087

10861088
bool IsStaticMethod(TCppConstFunction_t method) {
1087-
const auto* D = (const Decl*)method;
1089+
const auto* D = static_cast<const Decl*>(method);
10881090
if (auto *CXXMD = llvm::dyn_cast_or_null<CXXMethodDecl>(D)) {
10891091
return CXXMD->isStatic();
10901092
}
@@ -1207,13 +1209,11 @@ namespace Cpp {
12071209
return 0;
12081210
}
12091211

1210-
intptr_t GetVariableOffset(TCppScope_t var)
1211-
{
1212-
if (!var)
1212+
intptr_t GetVariableOffset(compat::Interpreter& I, Decl* D) {
1213+
if (!D)
12131214
return 0;
12141215

1215-
auto *D = (Decl *) var;
1216-
auto &C = getASTContext();
1216+
auto& C = I.getSema().getASTContext();
12171217

12181218
if (auto* FD = llvm::dyn_cast<FieldDecl>(D)) {
12191219
const clang::RecordDecl* RD = FD->getParent();
@@ -1242,7 +1242,7 @@ namespace Cpp {
12421242
compat::maybeMangleDeclName(GD, mangledName);
12431243
void* address = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
12441244
mangledName.c_str());
1245-
auto &I = getInterp();
1245+
12461246
if (!address)
12471247
address = I.getAddressOfGlobal(GD);
12481248
if (!address) {
@@ -1292,6 +1292,11 @@ namespace Cpp {
12921292
return 0;
12931293
}
12941294

1295+
intptr_t GetVariableOffset(TCppScope_t var) {
1296+
auto* D = static_cast<Decl*>(var);
1297+
return GetVariableOffset(getInterp(), D);
1298+
}
1299+
12951300
// Check if the Access Specifier of the variable matches the provided value.
12961301
bool CheckVariableAccess(TCppScope_t var, AccessSpecifier AS)
12971302
{
@@ -2574,26 +2579,32 @@ namespace Cpp {
25742579
} // namespace
25752580
// End of JitCall Helper Functions
25762581

2577-
CPPINTEROP_API JitCall MakeFunctionCallable(TCppConstFunction_t func) {
2578-
auto* D = (const clang::Decl*)func;
2579-
if (!D)
2580-
return {};
2582+
CPPINTEROP_API JitCall MakeFunctionCallable(TInterp_t I,
2583+
TCppConstFunction_t func) {
2584+
const auto* D = static_cast<const clang::Decl*>(func);
2585+
if (!D)
2586+
return {};
25812587

2582-
auto& I = getInterp();
2583-
// FIXME: Unify with make_wrapper.
2584-
if (auto *Dtor = dyn_cast<CXXDestructorDecl>(D)) {
2585-
if (auto Wrapper = make_dtor_wrapper(I, Dtor->getParent()))
2586-
return {JitCall::kDestructorCall, Wrapper, Dtor};
2588+
auto* interp = static_cast<compat::Interpreter*>(I);
2589+
2590+
// FIXME: Unify with make_wrapper.
2591+
if (const auto* Dtor = dyn_cast<CXXDestructorDecl>(D)) {
2592+
if (auto Wrapper = make_dtor_wrapper(*interp, Dtor->getParent()))
2593+
return {JitCall::kDestructorCall, Wrapper, Dtor};
2594+
// FIXME: else error we failed to compile the wrapper.
2595+
return {};
2596+
}
2597+
2598+
if (auto Wrapper = make_wrapper(*interp, cast<FunctionDecl>(D))) {
2599+
return {JitCall::kGenericCall, Wrapper, cast<FunctionDecl>(D)};
2600+
}
25872601
// FIXME: else error we failed to compile the wrapper.
25882602
return {};
25892603
}
25902604

2591-
if (auto Wrapper = make_wrapper(I, cast<FunctionDecl>(D))) {
2592-
return {JitCall::kGenericCall, Wrapper, cast<FunctionDecl>(D)};
2605+
CPPINTEROP_API JitCall MakeFunctionCallable(TCppConstFunction_t func) {
2606+
return MakeFunctionCallable(&getInterp(), func);
25932607
}
2594-
// FIXME: else error we failed to compile the wrapper.
2595-
return {};
2596-
}
25972608

25982609
namespace {
25992610
static std::string MakeResourcesPath() {
@@ -2842,7 +2853,8 @@ namespace Cpp {
28422853
return DLM->searchLibrariesForSymbol(mangled_name, search_system);
28432854
}
28442855

2845-
bool InsertOrReplaceJitSymbol(const char* linker_mangled_name,
2856+
bool InsertOrReplaceJitSymbol(compat::Interpreter& I,
2857+
const char* linker_mangled_name,
28462858
uint64_t address) {
28472859
// FIXME: This approach is problematic since we could replace a symbol
28482860
// whose address was already taken by clients.
@@ -2869,7 +2881,6 @@ namespace Cpp {
28692881
using namespace llvm;
28702882
using namespace llvm::orc;
28712883

2872-
auto& I = getInterp();
28732884
auto Symbol = compat::getSymbolAddress(I, linker_mangled_name);
28742885
llvm::orc::LLJIT& Jit = *compat::getExecutionEngine(I);
28752886
llvm::orc::ExecutionSession& ES = Jit.getExecutionSession();
@@ -2930,6 +2941,11 @@ namespace Cpp {
29302941
return false;
29312942
}
29322943

2944+
bool InsertOrReplaceJitSymbol(const char* linker_mangled_name,
2945+
uint64_t address) {
2946+
return InsertOrReplaceJitSymbol(getInterp(), linker_mangled_name, address);
2947+
}
2948+
29332949
std::string ObjToString(const char *type, void *obj) {
29342950
return getInterp().toString(type, obj);
29352951
}
@@ -2979,9 +2995,8 @@ namespace Cpp {
29792995
// return C.getElaboratedType(ETK_None, NS, TT);
29802996
}
29812997

2982-
static Decl* InstantiateTemplate(TemplateDecl* TemplateD,
2983-
ArrayRef<TemplateArgument> TemplateArgs,
2984-
Sema& S) {
2998+
Decl* InstantiateTemplate(TemplateDecl* TemplateD,
2999+
ArrayRef<TemplateArgument> TemplateArgs, Sema& S) {
29853000
// Create a list of template arguments.
29863001
TemplateArgumentListInfo TLI{};
29873002
for (auto TA : TemplateArgs)
@@ -2991,10 +3006,11 @@ namespace Cpp {
29913006
return InstantiateTemplate(TemplateD, TLI, S);
29923007
}
29933008

2994-
TCppScope_t InstantiateTemplate(TCppScope_t tmpl,
3009+
TCppScope_t InstantiateTemplate(compat::Interpreter& I, TCppScope_t tmpl,
29953010
const TemplateArgInfo* template_args,
29963011
size_t template_args_size) {
2997-
ASTContext &C = getASTContext();
3012+
auto& S = I.getSema();
3013+
auto& C = S.getASTContext();
29983014

29993015
llvm::SmallVector<TemplateArgument> TemplateArgs;
30003016
TemplateArgs.reserve(template_args_size);
@@ -3015,9 +3031,16 @@ namespace Cpp {
30153031

30163032
// We will create a new decl, push a transaction.
30173033
#ifdef USE_CLING
3018-
cling::Interpreter::PushTransactionRAII RAII(&getInterp());
3034+
cling::Interpreter::PushTransactionRAII RAII(&I);
30193035
#endif
3020-
return InstantiateTemplate(TmplD, TemplateArgs, getSema());
3036+
return InstantiateTemplate(TmplD, TemplateArgs, S);
3037+
}
3038+
3039+
TCppScope_t InstantiateTemplate(TCppScope_t tmpl,
3040+
const TemplateArgInfo* template_args,
3041+
size_t template_args_size) {
3042+
return InstantiateTemplate(getInterp(), tmpl, template_args,
3043+
template_args_size);
30213044
}
30223045

30233046
void GetClassTemplateInstantiationArgs(TCppScope_t templ_instance,
@@ -3241,15 +3264,15 @@ namespace Cpp {
32413264
}
32423265

32433266
// FIXME: Add optional arguments to the operator new.
3244-
TCppObject_t Construct(TCppScope_t scope,
3245-
void* arena/*=nullptr*/) {
3267+
TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
3268+
void* arena /*=nullptr*/) {
32463269
auto* Class = (Decl*) scope;
32473270
// FIXME: Diagnose.
32483271
if (!HasDefaultConstructor(Class))
32493272
return nullptr;
32503273

32513274
auto* const Ctor = GetDefaultConstructor(Class);
3252-
if (JitCall JC = MakeFunctionCallable(Ctor)) {
3275+
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
32533276
if (arena) {
32543277
JC.Invoke(&arena, {}, (void*)~0); // Tell Invoke to use placement new.
32553278
return arena;
@@ -3262,15 +3285,24 @@ namespace Cpp {
32623285
return nullptr;
32633286
}
32643287

3265-
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/) {
3266-
Decl* Class = (Decl*)scope;
3267-
if (auto wrapper = make_dtor_wrapper(getInterp(), Class)) {
3288+
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/) {
3289+
return Construct(getInterp(), scope, arena);
3290+
}
3291+
3292+
void Destruct(compat::Interpreter& interp, TCppObject_t This, Decl* Class,
3293+
bool withFree) {
3294+
if (auto wrapper = make_dtor_wrapper(interp, Class)) {
32683295
(*wrapper)(This, /*nary=*/0, withFree);
32693296
return;
32703297
}
32713298
// FIXME: Diagnose.
32723299
}
32733300

3301+
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/) {
3302+
auto* Class = static_cast<Decl*>(scope);
3303+
Destruct(getInterp(), This, Class, withFree);
3304+
}
3305+
32743306
class StreamCaptureInfo {
32753307
std::unique_ptr<FILE, decltype(std::fclose)*> m_TempFile;
32763308
int m_FD = -1;

0 commit comments

Comments
 (0)