Skip to content

Commit 0cf2bf1

Browse files
committed
API refactoring: add multiple interpreter instance support
1 parent 3c6ce53 commit 0cf2bf1

File tree

2 files changed

+59
-32
lines changed

2 files changed

+59
-32
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ namespace Cpp {
7878
class JitCall {
7979
public:
8080
friend CPPINTEROP_API JitCall MakeFunctionCallable(TCppConstFunction_t);
81+
friend JitCall MakeFunctionCallable(TInterp_t I, TCppConstFunction_t func);
8182
enum Kind : char {
8283
kUnknown = 0,
8384
kGenericCall,

lib/Interpreter/CppInterOp.cpp

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,13 +1207,11 @@ namespace Cpp {
12071207
return 0;
12081208
}
12091209

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

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

12181216
if (auto* FD = llvm::dyn_cast<FieldDecl>(D)) {
12191217
const clang::RecordDecl* RD = FD->getParent();
@@ -1242,7 +1240,7 @@ namespace Cpp {
12421240
compat::maybeMangleDeclName(GD, mangledName);
12431241
void* address = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
12441242
mangledName.c_str());
1245-
auto &I = getInterp();
1243+
12461244
if (!address)
12471245
address = I.getAddressOfGlobal(GD);
12481246
if (!address) {
@@ -1292,6 +1290,11 @@ namespace Cpp {
12921290
return 0;
12931291
}
12941292

1293+
intptr_t GetVariableOffset(TCppScope_t var) {
1294+
auto* D = static_cast<Decl*>(var);
1295+
return GetVariableOffset(getInterp(), D);
1296+
}
1297+
12951298
// Check if the Access Specifier of the variable matches the provided value.
12961299
bool CheckVariableAccess(TCppScope_t var, AccessSpecifier AS)
12971300
{
@@ -1453,6 +1456,10 @@ namespace Cpp {
14531456
}
14541457
}
14551458

1459+
QualType GetBuiltinType(const std::string& name, Sema& sema) {
1460+
return findBuiltinType(name, sema.getASTContext());
1461+
}
1462+
14561463
TCppType_t GetType(const std::string &name) {
14571464
QualType builtin = findBuiltinType(name, getASTContext());
14581465
if (!builtin.isNull())
@@ -2574,26 +2581,31 @@ namespace Cpp {
25742581
} // namespace
25752582
// End of JitCall Helper Functions
25762583

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

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};
2589+
auto* interp = static_cast<compat::Interpreter*>(I);
2590+
2591+
// FIXME: Unify with make_wrapper.
2592+
if (const auto* Dtor = dyn_cast<CXXDestructorDecl>(D)) {
2593+
if (auto Wrapper = make_dtor_wrapper(*interp, Dtor->getParent()))
2594+
return {JitCall::kDestructorCall, Wrapper, Dtor};
2595+
// FIXME: else error we failed to compile the wrapper.
2596+
return {};
2597+
}
2598+
2599+
if (auto Wrapper = make_wrapper(*interp, cast<FunctionDecl>(D))) {
2600+
return {JitCall::kGenericCall, Wrapper, cast<FunctionDecl>(D)};
2601+
}
25872602
// FIXME: else error we failed to compile the wrapper.
25882603
return {};
25892604
}
25902605

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

25982610
namespace {
25992611
static std::string MakeResourcesPath() {
@@ -2842,8 +2854,9 @@ namespace Cpp {
28422854
return DLM->searchLibrariesForSymbol(mangled_name, search_system);
28432855
}
28442856

2845-
bool InsertOrReplaceJitSymbol(const char* linker_mangled_name,
2846-
uint64_t address) {
2857+
bool InsertOrReplaceJitSymbol(compat::Interpreter& I,
2858+
const char* linker_mangled_name,
2859+
uint64_t address) {
28472860
// FIXME: This approach is problematic since we could replace a symbol
28482861
// whose address was already taken by clients.
28492862
//
@@ -2869,7 +2882,6 @@ namespace Cpp {
28692882
using namespace llvm;
28702883
using namespace llvm::orc;
28712884

2872-
auto& I = getInterp();
28732885
auto Symbol = compat::getSymbolAddress(I, linker_mangled_name);
28742886
llvm::orc::LLJIT& Jit = *compat::getExecutionEngine(I);
28752887
llvm::orc::ExecutionSession& ES = Jit.getExecutionSession();
@@ -2930,6 +2942,12 @@ namespace Cpp {
29302942
return false;
29312943
}
29322944

2945+
bool InsertOrReplaceJitSymbol(const char* linker_mangled_name,
2946+
uint64_t address) {
2947+
return InsertOrReplaceJitSymbol(getInterp(), linker_mangled_name,
2948+
address);
2949+
}
2950+
29332951
std::string ObjToString(const char *type, void *obj) {
29342952
return getInterp().toString(type, obj);
29352953
}
@@ -2979,9 +2997,8 @@ namespace Cpp {
29792997
// return C.getElaboratedType(ETK_None, NS, TT);
29802998
}
29812999

2982-
static Decl* InstantiateTemplate(TemplateDecl* TemplateD,
2983-
ArrayRef<TemplateArgument> TemplateArgs,
2984-
Sema& S) {
3000+
Decl* InstantiateTemplate(TemplateDecl* TemplateD,
3001+
ArrayRef<TemplateArgument> TemplateArgs, Sema& S) {
29853002
// Create a list of template arguments.
29863003
TemplateArgumentListInfo TLI{};
29873004
for (auto TA : TemplateArgs)
@@ -3241,15 +3258,15 @@ namespace Cpp {
32413258
}
32423259

32433260
// FIXME: Add optional arguments to the operator new.
3244-
TCppObject_t Construct(TCppScope_t scope,
3245-
void* arena/*=nullptr*/) {
3261+
TCppObject_t Construct(compat::Interpreter& interp, TCppScope_t scope,
3262+
void* arena /*=nullptr*/) {
32463263
auto* Class = (Decl*) scope;
32473264
// FIXME: Diagnose.
32483265
if (!HasDefaultConstructor(Class))
32493266
return nullptr;
32503267

32513268
auto* const Ctor = GetDefaultConstructor(Class);
3252-
if (JitCall JC = MakeFunctionCallable(Ctor)) {
3269+
if (JitCall JC = MakeFunctionCallable(&interp, Ctor)) {
32533270
if (arena) {
32543271
JC.Invoke(&arena, {}, (void*)~0); // Tell Invoke to use placement new.
32553272
return arena;
@@ -3262,15 +3279,24 @@ namespace Cpp {
32623279
return nullptr;
32633280
}
32643281

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)) {
3282+
TCppObject_t Construct(TCppScope_t scope, void* arena /*=nullptr*/) {
3283+
return Construct(getInterp(), scope, arena);
3284+
}
3285+
3286+
void Destruct(compat::Interpreter& interp, TCppObject_t This, Decl* Class,
3287+
bool withFree) {
3288+
if (auto wrapper = make_dtor_wrapper(interp, Class)) {
32683289
(*wrapper)(This, /*nary=*/0, withFree);
32693290
return;
32703291
}
32713292
// FIXME: Diagnose.
32723293
}
32733294

3295+
void Destruct(TCppObject_t This, TCppScope_t scope, bool withFree /*=true*/) {
3296+
auto* Class = static_cast<Decl*>(scope);
3297+
Destruct(getInterp(), This, Class, withFree);
3298+
}
3299+
32743300
class StreamCaptureInfo {
32753301
std::unique_ptr<FILE, decltype(std::fclose)*> m_TempFile;
32763302
int m_FD = -1;

0 commit comments

Comments
 (0)