33
33
#include " clang/Basic/Specifiers.h"
34
34
#include " clang/Basic/Version.h"
35
35
#include " clang/Frontend/CompilerInstance.h"
36
+ #include " clang/Interpreter/Interpreter.h"
36
37
#include " clang/Sema/Lookup.h"
37
38
#include " clang/Sema/Overload.h"
38
39
#include " clang/Sema/Ownership.h"
@@ -222,6 +223,15 @@ void EnableDebugOutput(bool value /* =true*/) { llvm::DebugFlag = value; }
222
223
223
224
bool IsDebugOutputEnabled () { return llvm::DebugFlag; }
224
225
226
+ static void InstantiateFunctionDefinition (Decl* D) {
227
+ compat::PushTransactionRAII RAII (&getInterp ());
228
+ if (auto * FD = llvm::dyn_cast_or_null<FunctionDecl>(D)) {
229
+ getSema ().InstantiateFunctionDefinition (SourceLocation (), FD,
230
+ /* Recursive=*/ true ,
231
+ /* DefinitionRequired=*/ true );
232
+ }
233
+ }
234
+
225
235
bool IsAggregate (TCppScope_t scope) {
226
236
Decl* D = static_cast <Decl*>(scope);
227
237
@@ -923,11 +933,7 @@ TCppType_t GetFunctionReturnType(TCppFunction_t func) {
923
933
}
924
934
925
935
if (needInstantiation) {
926
- #ifdef CPPINTEROP_USE_CLING
927
- cling::Interpreter::PushTransactionRAII RAII (&getInterp ());
928
- #endif
929
- getSema ().InstantiateFunctionDefinition (SourceLocation (), FD, true ,
930
- true );
936
+ InstantiateFunctionDefinition (FD);
931
937
}
932
938
Type = FD->getReturnType ();
933
939
}
@@ -2482,23 +2488,7 @@ int get_wrapper_code(compat::Interpreter& I, const FunctionDecl* FD,
2482
2488
}
2483
2489
if (needInstantiation) {
2484
2490
clang::FunctionDecl* FDmod = const_cast <clang::FunctionDecl*>(FD);
2485
- clang::Sema& S = I.getCI ()->getSema ();
2486
- // Could trigger deserialization of decls.
2487
- #ifdef CPPINTEROP_USE_CLING
2488
- cling::Interpreter::PushTransactionRAII RAII (&I);
2489
- #endif
2490
- S.InstantiateFunctionDefinition (SourceLocation (), FDmod,
2491
- /* Recursive=*/ true ,
2492
- /* DefinitionRequired=*/ true );
2493
- #ifndef CPPINTEROP_USE_CLING
2494
- // TODO: Will need to replace this with a RAII for clang-repl too
2495
- auto GeneratedPTU = I.Parse (" " );
2496
- if (!GeneratedPTU)
2497
- llvm::logAllUnhandledErrors (
2498
- GeneratedPTU.takeError (), llvm::errs (),
2499
- " [MakeFunctionCallable -> InstantiateFunctionDefinition] Failed to "
2500
- " generate PTU:" );
2501
- #endif
2491
+ InstantiateFunctionDefinition (FDmod);
2502
2492
2503
2493
if (!FD->isDefined (Definition)) {
2504
2494
llvm::errs () << " TClingCallFunc::make_wrapper"
@@ -3304,7 +3294,8 @@ std::string ObjToString(const char* type, void* obj) {
3304
3294
}
3305
3295
3306
3296
static Decl* InstantiateTemplate (TemplateDecl* TemplateD,
3307
- TemplateArgumentListInfo& TLI, Sema& S) {
3297
+ TemplateArgumentListInfo& TLI, Sema& S,
3298
+ bool instantiate_body) {
3308
3299
// This is not right but we don't have a lot of options to choose from as a
3309
3300
// template instantiation requires a valid source location.
3310
3301
SourceLocation fakeLoc = GetValidSLoc (S);
@@ -3318,6 +3309,8 @@ static Decl* InstantiateTemplate(TemplateDecl* TemplateD,
3318
3309
// FIXME: Diagnose what happened.
3319
3310
(void )Result;
3320
3311
}
3312
+ if (instantiate_body)
3313
+ InstantiateFunctionDefinition (Specialization);
3321
3314
return Specialization;
3322
3315
}
3323
3316
@@ -3349,19 +3342,21 @@ static Decl* InstantiateTemplate(TemplateDecl* TemplateD,
3349
3342
}
3350
3343
3351
3344
Decl* InstantiateTemplate (TemplateDecl* TemplateD,
3352
- ArrayRef<TemplateArgument> TemplateArgs, Sema& S) {
3345
+ ArrayRef<TemplateArgument> TemplateArgs, Sema& S,
3346
+ bool instantiate_body) {
3353
3347
// Create a list of template arguments.
3354
3348
TemplateArgumentListInfo TLI{};
3355
3349
for (auto TA : TemplateArgs)
3356
3350
TLI.addArgument (
3357
3351
S.getTrivialTemplateArgumentLoc (TA, QualType (), SourceLocation ()));
3358
3352
3359
- return InstantiateTemplate (TemplateD, TLI, S);
3353
+ return InstantiateTemplate (TemplateD, TLI, S, instantiate_body );
3360
3354
}
3361
3355
3362
3356
TCppScope_t InstantiateTemplate (compat::Interpreter& I, TCppScope_t tmpl,
3363
3357
const TemplateArgInfo* template_args,
3364
- size_t template_args_size) {
3358
+ size_t template_args_size,
3359
+ bool instantiate_body) {
3365
3360
auto & S = I.getSema ();
3366
3361
auto & C = S.getASTContext ();
3367
3362
@@ -3386,14 +3381,15 @@ TCppScope_t InstantiateTemplate(compat::Interpreter& I, TCppScope_t tmpl,
3386
3381
#ifdef CPPINTEROP_USE_CLING
3387
3382
cling::Interpreter::PushTransactionRAII RAII (&I);
3388
3383
#endif
3389
- return InstantiateTemplate (TmplD, TemplateArgs, S);
3384
+ return InstantiateTemplate (TmplD, TemplateArgs, S, instantiate_body );
3390
3385
}
3391
3386
3392
3387
TCppScope_t InstantiateTemplate (TCppScope_t tmpl,
3393
3388
const TemplateArgInfo* template_args,
3394
- size_t template_args_size) {
3389
+ size_t template_args_size,
3390
+ bool instantiate_body) {
3395
3391
return InstantiateTemplate (getInterp (), tmpl, template_args,
3396
- template_args_size);
3392
+ template_args_size, instantiate_body );
3397
3393
}
3398
3394
3399
3395
void GetClassTemplateInstantiationArgs (TCppScope_t templ_instance,
0 commit comments