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::SynthesizingCodeRAII 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
}
@@ -2491,23 +2497,7 @@ int get_wrapper_code(compat::Interpreter& I, const FunctionDecl* FD,
2491
2497
}
2492
2498
if (needInstantiation) {
2493
2499
clang::FunctionDecl* FDmod = const_cast <clang::FunctionDecl*>(FD);
2494
- clang::Sema& S = I.getCI ()->getSema ();
2495
- // Could trigger deserialization of decls.
2496
- #ifdef CPPINTEROP_USE_CLING
2497
- cling::Interpreter::PushTransactionRAII RAII (&I);
2498
- #endif
2499
- S.InstantiateFunctionDefinition (SourceLocation (), FDmod,
2500
- /* Recursive=*/ true ,
2501
- /* DefinitionRequired=*/ true );
2502
- #ifndef CPPINTEROP_USE_CLING
2503
- // TODO: Will need to replace this with a RAII for clang-repl too
2504
- auto GeneratedPTU = I.Parse (" " );
2505
- if (!GeneratedPTU)
2506
- llvm::logAllUnhandledErrors (
2507
- GeneratedPTU.takeError (), llvm::errs (),
2508
- " [MakeFunctionCallable -> InstantiateFunctionDefinition] Failed to "
2509
- " generate PTU:" );
2510
- #endif
2500
+ InstantiateFunctionDefinition (FDmod);
2511
2501
2512
2502
if (!FD->isDefined (Definition)) {
2513
2503
llvm::errs () << " TClingCallFunc::make_wrapper"
@@ -3313,7 +3303,8 @@ std::string ObjToString(const char* type, void* obj) {
3313
3303
}
3314
3304
3315
3305
static Decl* InstantiateTemplate (TemplateDecl* TemplateD,
3316
- TemplateArgumentListInfo& TLI, Sema& S) {
3306
+ TemplateArgumentListInfo& TLI, Sema& S,
3307
+ bool instantiate_body) {
3317
3308
// This is not right but we don't have a lot of options to choose from as a
3318
3309
// template instantiation requires a valid source location.
3319
3310
SourceLocation fakeLoc = GetValidSLoc (S);
@@ -3327,6 +3318,8 @@ static Decl* InstantiateTemplate(TemplateDecl* TemplateD,
3327
3318
// FIXME: Diagnose what happened.
3328
3319
(void )Result;
3329
3320
}
3321
+ if (instantiate_body)
3322
+ InstantiateFunctionDefinition (Specialization);
3330
3323
return Specialization;
3331
3324
}
3332
3325
@@ -3358,19 +3351,21 @@ static Decl* InstantiateTemplate(TemplateDecl* TemplateD,
3358
3351
}
3359
3352
3360
3353
Decl* InstantiateTemplate (TemplateDecl* TemplateD,
3361
- ArrayRef<TemplateArgument> TemplateArgs, Sema& S) {
3354
+ ArrayRef<TemplateArgument> TemplateArgs, Sema& S,
3355
+ bool instantiate_body) {
3362
3356
// Create a list of template arguments.
3363
3357
TemplateArgumentListInfo TLI{};
3364
3358
for (auto TA : TemplateArgs)
3365
3359
TLI.addArgument (
3366
3360
S.getTrivialTemplateArgumentLoc (TA, QualType (), SourceLocation ()));
3367
3361
3368
- return InstantiateTemplate (TemplateD, TLI, S);
3362
+ return InstantiateTemplate (TemplateD, TLI, S, instantiate_body );
3369
3363
}
3370
3364
3371
3365
TCppScope_t InstantiateTemplate (compat::Interpreter& I, TCppScope_t tmpl,
3372
3366
const TemplateArgInfo* template_args,
3373
- size_t template_args_size) {
3367
+ size_t template_args_size,
3368
+ bool instantiate_body) {
3374
3369
auto & S = I.getSema ();
3375
3370
auto & C = S.getASTContext ();
3376
3371
@@ -3395,14 +3390,15 @@ TCppScope_t InstantiateTemplate(compat::Interpreter& I, TCppScope_t tmpl,
3395
3390
#ifdef CPPINTEROP_USE_CLING
3396
3391
cling::Interpreter::PushTransactionRAII RAII (&I);
3397
3392
#endif
3398
- return InstantiateTemplate (TmplD, TemplateArgs, S);
3393
+ return InstantiateTemplate (TmplD, TemplateArgs, S, instantiate_body );
3399
3394
}
3400
3395
3401
3396
TCppScope_t InstantiateTemplate (TCppScope_t tmpl,
3402
3397
const TemplateArgInfo* template_args,
3403
- size_t template_args_size) {
3398
+ size_t template_args_size,
3399
+ bool instantiate_body) {
3404
3400
return InstantiateTemplate (getInterp (), tmpl, template_args,
3405
- template_args_size);
3401
+ template_args_size, instantiate_body );
3406
3402
}
3407
3403
3408
3404
void GetClassTemplateInstantiationArgs (TCppScope_t templ_instance,
0 commit comments