Skip to content

Commit f71d7f5

Browse files
committed
Query and cache lifetime dependence info via evaluator requests
1 parent 84ac181 commit f71d7f5

File tree

3 files changed

+30
-56
lines changed

3 files changed

+30
-56
lines changed

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10009,7 +10009,7 @@ void AbstractFunctionDecl::addDerivativeFunctionConfiguration(
1000910009

1001010010
std::optional<LifetimeDependenceInfo>
1001110011
AbstractFunctionDecl::getLifetimeDependenceInfo() const {
10012-
if (!isa<FuncDecl>(this) || !isa<ConstructorDecl>(this)) {
10012+
if (!isa<FuncDecl>(this) && !isa<ConstructorDecl>(this)) {
1001310013
return std::nullopt;
1001410014
}
1001510015

lib/Sema/TypeCheckDecl.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,7 +2550,7 @@ InterfaceTypeRequest::evaluate(Evaluator &eval, ValueDecl *D) const {
25502550
resultTy = TupleType::getEmpty(AFD->getASTContext());
25512551
}
25522552

2553-
std::optional<LifetimeDependenceInfo> lifetimeDependenceInfo;
2553+
auto lifetimeDependenceInfo = AFD->getLifetimeDependenceInfo();
25542554

25552555
// (Args...) -> Result
25562556
Type funcTy;
@@ -2571,7 +2571,6 @@ InterfaceTypeRequest::evaluate(Evaluator &eval, ValueDecl *D) const {
25712571
infoBuilder = infoBuilder.withTransferringResult();
25722572
}
25732573

2574-
lifetimeDependenceInfo = LifetimeDependenceInfo::get(AFD, resultTy);
25752574
if (lifetimeDependenceInfo.has_value()) {
25762575
infoBuilder =
25772576
infoBuilder.withLifetimeDependenceInfo(*lifetimeDependenceInfo);
@@ -3098,3 +3097,18 @@ ImplicitKnownProtocolConformanceRequest::evaluate(Evaluator &evaluator,
30983097
llvm_unreachable("non-implicitly derived KnownProtocol");
30993098
}
31003099
}
3100+
3101+
std::optional<LifetimeDependenceInfo>
3102+
LifetimeDependenceInfoRequest::evaluate(Evaluator &evaluator,
3103+
AbstractFunctionDecl *decl) const {
3104+
Type resultTy;
3105+
if (auto fn = dyn_cast<FuncDecl>(decl)) {
3106+
resultTy = fn->getResultInterfaceType();
3107+
} else if (auto ctor = dyn_cast<ConstructorDecl>(decl)) {
3108+
resultTy = ctor->getResultInterfaceType();
3109+
} else {
3110+
return std::nullopt;
3111+
}
3112+
3113+
return LifetimeDependenceInfo::get(decl, resultTy);
3114+
}

lib/Serialization/Deserialization.cpp

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,14 +3644,12 @@ class DeclDeserializer {
36443644
assert(bodyParams && "missing parameters for constructor");
36453645
ctor->setParameters(bodyParams);
36463646

3647-
SmallVector<LifetimeDependenceSpecifier> specifierList;
3648-
if (MF.maybeReadLifetimeDependenceSpecifier(specifierList,
3649-
bodyParams->size())) {
3650-
auto SelfType = ctor->getDeclaredInterfaceType();
3651-
auto typeRepr = new (ctx) FixedTypeRepr(SelfType, SourceLoc());
3652-
auto lifetimeTypeRepr =
3653-
LifetimeDependentReturnTypeRepr::create(ctx, typeRepr, specifierList);
3654-
ctor->setDeserializedResultTypeLoc(TypeLoc(lifetimeTypeRepr, SelfType));
3647+
auto lifetimeDependenceInfo =
3648+
MF.maybeReadLifetimeDependenceInfo(bodyParams->size());
3649+
3650+
if (lifetimeDependenceInfo.has_value()) {
3651+
ctx.evaluator.cacheOutput(LifetimeDependenceInfoRequest{ctor},
3652+
std::move(lifetimeDependenceInfo.value()));
36553653
}
36563654

36573655
if (auto errorConvention = MF.maybeReadForeignErrorConvention())
@@ -4222,13 +4220,13 @@ class DeclDeserializer {
42224220

42234221
ParameterList *paramList = MF.readParameterList();
42244222
fn->setParameters(paramList);
4225-
SmallVector<LifetimeDependenceSpecifier> specifierList;
4226-
if (MF.maybeReadLifetimeDependenceSpecifier(specifierList,
4227-
paramList->size())) {
4228-
auto typeRepr = new (ctx) FixedTypeRepr(resultType, SourceLoc());
4229-
auto lifetimeTypeRepr =
4230-
LifetimeDependentReturnTypeRepr::create(ctx, typeRepr, specifierList);
4231-
fn->setDeserializedResultTypeLoc(TypeLoc(lifetimeTypeRepr, resultType));
4223+
4224+
auto lifetimeDependenceInfo =
4225+
MF.maybeReadLifetimeDependenceInfo(paramList->size());
4226+
4227+
if (lifetimeDependenceInfo.has_value()) {
4228+
ctx.evaluator.cacheOutput(LifetimeDependenceInfoRequest{fn},
4229+
std::move(lifetimeDependenceInfo.value()));
42324230
}
42334231

42344232
if (auto errorConvention = MF.maybeReadForeignErrorConvention())
@@ -8829,41 +8827,3 @@ ModuleFile::maybeReadLifetimeDependenceInfo(unsigned numParams) {
88298827
? IndexSubset::get(ctx, scopeLifetimeParamIndices)
88308828
: nullptr);
88318829
}
8832-
8833-
bool ModuleFile::maybeReadLifetimeDependenceSpecifier(
8834-
SmallVectorImpl<LifetimeDependenceSpecifier> &specifierList,
8835-
unsigned numDeclParams) {
8836-
using namespace decls_block;
8837-
8838-
SmallVector<uint64_t, 8> scratch;
8839-
if (!maybeReadLifetimeDependenceRecord(scratch)) {
8840-
return false;
8841-
}
8842-
8843-
bool hasInheritLifetimeParamIndices;
8844-
bool hasScopeLifetimeParamIndices;
8845-
ArrayRef<uint64_t> lifetimeDependenceData;
8846-
LifetimeDependenceLayout::readRecord(scratch, hasInheritLifetimeParamIndices,
8847-
hasScopeLifetimeParamIndices,
8848-
lifetimeDependenceData);
8849-
8850-
unsigned startIndex = 0;
8851-
auto pushData = [&](ParsedLifetimeDependenceKind kind) {
8852-
for (unsigned i = 0; i < numDeclParams + 1; i++) {
8853-
if (lifetimeDependenceData[startIndex + i]) {
8854-
specifierList.push_back(
8855-
LifetimeDependenceSpecifier::getOrderedLifetimeDependenceSpecifier(
8856-
SourceLoc(), kind, i));
8857-
}
8858-
}
8859-
startIndex += numDeclParams + 1;
8860-
};
8861-
8862-
if (hasInheritLifetimeParamIndices) {
8863-
pushData(ParsedLifetimeDependenceKind::Inherit);
8864-
}
8865-
if (hasScopeLifetimeParamIndices) {
8866-
pushData(ParsedLifetimeDependenceKind::Scope);
8867-
}
8868-
return true;
8869-
}

0 commit comments

Comments
 (0)