@@ -2412,6 +2412,75 @@ static FunctionDecl *getPatternFunctionDecl(FunctionDecl *FD) {
24122412 return FTD->getTemplatedDecl ();
24132413}
24142414
2415+ bool Sema::addInstantiatedCapturesToScope (
2416+ FunctionDecl *Function, const FunctionDecl *PatternDecl,
2417+ LocalInstantiationScope &Scope,
2418+ const MultiLevelTemplateArgumentList &TemplateArgs) {
2419+ const auto *LambdaClass = cast<CXXMethodDecl>(Function)->getParent ();
2420+ const auto *LambdaPattern = cast<CXXMethodDecl>(PatternDecl)->getParent ();
2421+
2422+ unsigned Instantiated = 0 ;
2423+
2424+ // FIXME: This is a workaround for not having deferred lambda body
2425+ // instantiation.
2426+ // When transforming a lambda's body, if we encounter another call to a
2427+ // nested lambda that contains a constraint expression, we add all of the
2428+ // outer lambda's instantiated captures to the current instantiation scope to
2429+ // facilitate constraint evaluation. However, these captures don't appear in
2430+ // the CXXRecordDecl until after the lambda expression is rebuilt, so we
2431+ // pull them out from the corresponding LSI.
2432+ LambdaScopeInfo *InstantiatingScope = nullptr ;
2433+ if (LambdaPattern->capture_size () && !LambdaClass->capture_size ()) {
2434+ for (FunctionScopeInfo *Scope : llvm::reverse (FunctionScopes)) {
2435+ auto *LSI = dyn_cast<LambdaScopeInfo>(Scope);
2436+ if (!LSI ||
2437+ LSI->CallOperator ->getTemplateInstantiationPattern () != PatternDecl)
2438+ continue ;
2439+ InstantiatingScope = LSI;
2440+ break ;
2441+ }
2442+ assert (InstantiatingScope);
2443+ }
2444+
2445+ auto AddSingleCapture = [&](const ValueDecl *CapturedPattern,
2446+ unsigned Index) {
2447+ ValueDecl *CapturedVar =
2448+ InstantiatingScope ? InstantiatingScope->Captures [Index].getVariable ()
2449+ : LambdaClass->getCapture (Index)->getCapturedVar ();
2450+ assert (CapturedVar->isInitCapture ());
2451+ Scope.InstantiatedLocal (CapturedPattern, CapturedVar);
2452+ };
2453+
2454+ for (const LambdaCapture &CapturePattern : LambdaPattern->captures ()) {
2455+ if (!CapturePattern.capturesVariable ()) {
2456+ Instantiated++;
2457+ continue ;
2458+ }
2459+ ValueDecl *CapturedPattern = CapturePattern.getCapturedVar ();
2460+
2461+ if (!CapturedPattern->isInitCapture ()) {
2462+ Instantiated++;
2463+ continue ;
2464+ }
2465+
2466+ if (!CapturedPattern->isParameterPack ()) {
2467+ AddSingleCapture (CapturedPattern, Instantiated++);
2468+ } else {
2469+ Scope.MakeInstantiatedLocalArgPack (CapturedPattern);
2470+ SmallVector<UnexpandedParameterPack, 2 > Unexpanded;
2471+ SemaRef.collectUnexpandedParameterPacks (
2472+ dyn_cast<VarDecl>(CapturedPattern)->getInit (), Unexpanded);
2473+ auto NumArgumentsInExpansion =
2474+ getNumArgumentsInExpansionFromUnexpanded (Unexpanded, TemplateArgs);
2475+ if (!NumArgumentsInExpansion)
2476+ continue ;
2477+ for (unsigned Arg = 0 ; Arg < *NumArgumentsInExpansion; ++Arg)
2478+ AddSingleCapture (CapturedPattern, Instantiated++);
2479+ }
2480+ }
2481+ return false ;
2482+ }
2483+
24152484Sema::LambdaScopeForCallOperatorInstantiationRAII::
24162485 LambdaScopeForCallOperatorInstantiationRAII (
24172486 Sema &SemaRef, FunctionDecl *FD, MultiLevelTemplateArgumentList MLTAL,
0 commit comments