Skip to content

Commit f523717

Browse files
committed
Move Sema::addInstantiatedCapturesToScope to SemaLambda.cpp
1 parent fa5025b commit f523717

File tree

2 files changed

+69
-69
lines changed

2 files changed

+69
-69
lines changed

clang/lib/Sema/SemaConcept.cpp

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -702,75 +702,6 @@ bool Sema::CheckConstraintSatisfaction(const Expr *ConstraintExpr,
702702
.isInvalid();
703703
}
704704

705-
bool Sema::addInstantiatedCapturesToScope(
706-
FunctionDecl *Function, const FunctionDecl *PatternDecl,
707-
LocalInstantiationScope &Scope,
708-
const MultiLevelTemplateArgumentList &TemplateArgs) {
709-
const auto *LambdaClass = cast<CXXMethodDecl>(Function)->getParent();
710-
const auto *LambdaPattern = cast<CXXMethodDecl>(PatternDecl)->getParent();
711-
712-
unsigned Instantiated = 0;
713-
714-
// FIXME: This is a workaround for not having deferred lambda body
715-
// instantiation.
716-
// When transforming a lambda's body, if we encounter another call to a
717-
// nested lambda that contains a constraint expression, we add all of the
718-
// outer lambda's instantiated captures to the current instantiation scope to
719-
// facilitate constraint evaluation. However, these captures don't appear in
720-
// the CXXRecordDecl until after the lambda expression is rebuilt, so we
721-
// pull them out from the corresponding LSI.
722-
LambdaScopeInfo *InstantiatingScope = nullptr;
723-
if (LambdaPattern->capture_size() && !LambdaClass->capture_size()) {
724-
for (FunctionScopeInfo *Scope : llvm::reverse(FunctionScopes)) {
725-
auto *LSI = dyn_cast<LambdaScopeInfo>(Scope);
726-
if (!LSI ||
727-
LSI->CallOperator->getTemplateInstantiationPattern() != PatternDecl)
728-
continue;
729-
InstantiatingScope = LSI;
730-
break;
731-
}
732-
assert(InstantiatingScope);
733-
}
734-
735-
auto AddSingleCapture = [&](const ValueDecl *CapturedPattern,
736-
unsigned Index) {
737-
ValueDecl *CapturedVar =
738-
InstantiatingScope ? InstantiatingScope->Captures[Index].getVariable()
739-
: LambdaClass->getCapture(Index)->getCapturedVar();
740-
assert(CapturedVar->isInitCapture());
741-
Scope.InstantiatedLocal(CapturedPattern, CapturedVar);
742-
};
743-
744-
for (const LambdaCapture &CapturePattern : LambdaPattern->captures()) {
745-
if (!CapturePattern.capturesVariable()) {
746-
Instantiated++;
747-
continue;
748-
}
749-
ValueDecl *CapturedPattern = CapturePattern.getCapturedVar();
750-
751-
if (!CapturedPattern->isInitCapture()) {
752-
Instantiated++;
753-
continue;
754-
}
755-
756-
if (!CapturedPattern->isParameterPack()) {
757-
AddSingleCapture(CapturedPattern, Instantiated++);
758-
} else {
759-
Scope.MakeInstantiatedLocalArgPack(CapturedPattern);
760-
SmallVector<UnexpandedParameterPack, 2> Unexpanded;
761-
SemaRef.collectUnexpandedParameterPacks(
762-
dyn_cast<VarDecl>(CapturedPattern)->getInit(), Unexpanded);
763-
auto NumArgumentsInExpansion =
764-
getNumArgumentsInExpansionFromUnexpanded(Unexpanded, TemplateArgs);
765-
if (!NumArgumentsInExpansion)
766-
continue;
767-
for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg)
768-
AddSingleCapture(CapturedPattern, Instantiated++);
769-
}
770-
}
771-
return false;
772-
}
773-
774705
bool Sema::SetupConstraintScope(
775706
FunctionDecl *FD, std::optional<ArrayRef<TemplateArgument>> TemplateArgs,
776707
const MultiLevelTemplateArgumentList &MLTAL,

clang/lib/Sema/SemaLambda.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
24152484
Sema::LambdaScopeForCallOperatorInstantiationRAII::
24162485
LambdaScopeForCallOperatorInstantiationRAII(
24172486
Sema &SemaRef, FunctionDecl *FD, MultiLevelTemplateArgumentList MLTAL,

0 commit comments

Comments
 (0)