AST: Simplify SubstitutionMap::get()

This commit is contained in:
Slava Pestov
2024-09-04 11:21:49 -04:00
parent 851a829063
commit 7e4e79c69f
4 changed files with 38 additions and 25 deletions

View File

@@ -168,9 +168,28 @@ SubstitutionMap SubstitutionMap::get(GenericSignature genericSig,
SubstitutionMap SubstitutionMap::get(GenericSignature genericSig,
ArrayRef<Type> types,
LookupConformanceFn lookupConformance) {
return get(genericSig,
QueryReplacementTypeArray{genericSig, types},
lookupConformance);
QueryReplacementTypeArray subs{genericSig, types};
InFlightSubstitution IFS(subs, lookupConformance, std::nullopt);
return get(genericSig, types, IFS);
}
SubstitutionMap SubstitutionMap::get(GenericSignature genericSig,
ArrayRef<Type> types,
InFlightSubstitution &IFS) {
// Form the stored conformances.
SmallVector<ProtocolConformanceRef, 4> conformances;
for (const auto &req : genericSig.getRequirements()) {
if (req.getKind() != RequirementKind::Conformance) continue;
CanType depTy = req.getFirstType()->getCanonicalType();
auto replacement = depTy.subst(IFS);
auto *proto = req.getProtocolDecl();
auto conformance = IFS.lookupConformance(depTy, replacement, proto,
/*level=*/0);
conformances.push_back(conformance);
}
return SubstitutionMap(genericSig, types, conformances);
}
SubstitutionMap SubstitutionMap::get(GenericSignature genericSig,
@@ -185,29 +204,17 @@ SubstitutionMap SubstitutionMap::get(GenericSignature genericSig,
for (auto *gp : genericSig.getGenericParams()) {
// Record the replacement.
Type replacement = Type(gp).subst(IFS);
assert((!replacement || replacement->hasError() ||
Type replacement = IFS.substType(gp, /*level=*/0);
if (!replacement)
replacement = ErrorType::get(gp->getASTContext());
assert((replacement->hasError() ||
gp->isParameterPack() == replacement->is<PackType>()) &&
"replacement for pack parameter must be a pack type");
replacementTypes.push_back(replacement);
}
// Form the stored conformances.
SmallVector<ProtocolConformanceRef, 4> conformances;
for (const auto &req : genericSig.getRequirements()) {
if (req.getKind() != RequirementKind::Conformance) continue;
CanType depTy = req.getFirstType()->getCanonicalType();
auto replacement = depTy.subst(IFS);
auto *proto = req.getProtocolDecl();
auto conformance = IFS.lookupConformance(depTy, replacement, proto,
/*level=*/0);
conformances.push_back(conformance);
}
return SubstitutionMap(genericSig, replacementTypes, conformances);
return SubstitutionMap::get(genericSig, replacementTypes, IFS);
}
Type SubstitutionMap::lookupSubstitution(GenericTypeParamType *genericParam) const {