@@ -2296,33 +2296,55 @@ ExprResult Sema::BuildBlockForLambdaConversion(SourceLocation CurrentLocation,
22962296 return BuildBlock;
22972297}
22982298
2299+ static FunctionDecl *getPatternFunctionDecl (FunctionDecl *FD) {
2300+ if (FD->getTemplatedKind () == FunctionDecl::TK_MemberSpecialization) {
2301+ while (FD->getInstantiatedFromMemberFunction ())
2302+ FD = FD->getInstantiatedFromMemberFunction ();
2303+ return FD;
2304+ }
2305+
2306+ if (FD->getTemplatedKind () == FunctionDecl::TK_DependentNonTemplate)
2307+ return FD->getInstantiatedFromDecl ();
2308+
2309+ FunctionTemplateDecl *FTD = FD->getPrimaryTemplate ();
2310+ if (!FTD)
2311+ return nullptr ;
2312+
2313+ while (FTD->getInstantiatedFromMemberTemplate ())
2314+ FTD = FTD->getInstantiatedFromMemberTemplate ();
2315+
2316+ return FTD->getTemplatedDecl ();
2317+ }
2318+
22992319Sema::LambdaScopeForCallOperatorInstantiationRAII::
23002320 LambdaScopeForCallOperatorInstantiationRAII (
2301- Sema &SemasRef , FunctionDecl *FD, MultiLevelTemplateArgumentList MLTAL,
2302- LocalInstantiationScope &Scope)
2303- : FunctionScopeRAII(SemasRef ) {
2321+ Sema &SemaRef , FunctionDecl *FD, MultiLevelTemplateArgumentList MLTAL,
2322+ LocalInstantiationScope &Scope, bool ShouldAddDeclsFromParentScope )
2323+ : FunctionScopeRAII(SemaRef ) {
23042324 if (!isLambdaCallOperator (FD)) {
23052325 FunctionScopeRAII::disable ();
23062326 return ;
23072327 }
23082328
2309- if (FD->isTemplateInstantiation () && FD->getPrimaryTemplate ()) {
2310- FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate ();
2311- if (const auto *FromMemTempl =
2312- PrimaryTemplate->getInstantiatedFromMemberTemplate ()) {
2313- SemasRef.addInstantiatedCapturesToScope (
2314- FD, FromMemTempl->getTemplatedDecl (), Scope, MLTAL);
2315- }
2316- }
2329+ SemaRef.RebuildLambdaScopeInfo (cast<CXXMethodDecl>(FD));
23172330
2318- else if (FD->getTemplatedKind () == FunctionDecl::TK_MemberSpecialization ||
2319- FD->getTemplatedKind () == FunctionDecl::TK_DependentNonTemplate) {
2320- FunctionDecl *InstantiatedFrom =
2321- FD->getTemplatedKind () == FunctionDecl::TK_MemberSpecialization
2322- ? FD->getInstantiatedFromMemberFunction ()
2323- : FD->getInstantiatedFromDecl ();
2324- SemasRef.addInstantiatedCapturesToScope (FD, InstantiatedFrom, Scope, MLTAL);
2325- }
2331+ FunctionDecl *Pattern = getPatternFunctionDecl (FD);
2332+ if (Pattern) {
2333+ SemaRef.addInstantiatedCapturesToScope (FD, Pattern, Scope, MLTAL);
2334+
2335+ FunctionDecl *ParentFD = FD;
2336+ while (ShouldAddDeclsFromParentScope) {
2337+
2338+ ParentFD =
2339+ dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext (ParentFD));
2340+ Pattern =
2341+ dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext (Pattern));
23262342
2327- SemasRef.RebuildLambdaScopeInfo (cast<CXXMethodDecl>(FD));
2343+ if (!FD || !Pattern)
2344+ break ;
2345+
2346+ SemaRef.addInstantiatedParametersToScope (ParentFD, Pattern, Scope, MLTAL);
2347+ SemaRef.addInstantiatedLocalVarsToScope (ParentFD, Pattern, Scope);
2348+ }
2349+ }
23282350}
0 commit comments