diff --git a/include/swift/AST/SubstitutionMap.h b/include/swift/AST/SubstitutionMap.h index deb897f5053bb..fa883df80b080 100644 --- a/include/swift/AST/SubstitutionMap.h +++ b/include/swift/AST/SubstitutionMap.h @@ -150,6 +150,10 @@ class SubstitutionMap { /// generic parameters. ArrayRef getReplacementTypes() const; + /// Retrieve the array of replacement types for the innermost generic + /// parameters. + ArrayRef getInnermostReplacementTypes() const; + /// Query whether any replacement types in the map contain archetypes. bool hasArchetypes() const; diff --git a/include/swift/AST/Types.h b/include/swift/AST/Types.h index 2ae23c4370b64..06d0770f6de36 100644 --- a/include/swift/AST/Types.h +++ b/include/swift/AST/Types.h @@ -1803,13 +1803,10 @@ class TypeAliasType final return *getTrailingObjects(); } - /// Get the innermost generic arguments, which correspond to the generic - /// arguments that are directly applied to the typealias declaration in - /// produced by \c getDecl(). - /// - /// The result can be empty, if the declaration itself is non-generic but - /// the parent is generic. - SmallVector getInnermostGenericArgs() const; + /// Get the direct generic arguments, which correspond to the generic + /// arguments that are directly applied to the typealias declaration + /// this type references. + ArrayRef getDirectGenericArgs() const; // Support for FoldingSet. void Profile(llvm::FoldingSetNodeID &id) const; diff --git a/lib/AST/ASTDumper.cpp b/lib/AST/ASTDumper.cpp index af02d2e0d37c9..c2b9cae6fa30e 100644 --- a/lib/AST/ASTDumper.cpp +++ b/lib/AST/ASTDumper.cpp @@ -3506,7 +3506,7 @@ namespace { if (T->getParent()) printRec("parent", T->getParent()); - for (auto arg : T->getInnermostGenericArgs()) + for (const auto arg : T->getDirectGenericArgs()) printRec(arg); PrintWithColorRAII(OS, ParenthesisColor) << ')'; } diff --git a/lib/AST/ASTPrinter.cpp b/lib/AST/ASTPrinter.cpp index c727d487d340b..7de41165c9c23 100644 --- a/lib/AST/ASTPrinter.cpp +++ b/lib/AST/ASTPrinter.cpp @@ -3727,7 +3727,7 @@ class TypePrinter : public TypeVisitor { } printQualifiedType(T); - printGenericArgs(T->getInnermostGenericArgs()); + printGenericArgs(T->getDirectGenericArgs()); } void visitParenType(ParenType *T) { diff --git a/lib/AST/SubstitutionMap.cpp b/lib/AST/SubstitutionMap.cpp index 026111141f832..654df75c001c4 100644 --- a/lib/AST/SubstitutionMap.cpp +++ b/lib/AST/SubstitutionMap.cpp @@ -97,6 +97,13 @@ ArrayRef SubstitutionMap::getReplacementTypes() const { return getReplacementTypesBuffer(); } +ArrayRef SubstitutionMap::getInnermostReplacementTypes() const { + if (empty()) return { }; + + return getReplacementTypes().take_back( + getGenericSignature()->getInnermostGenericParams().size()); +} + GenericSignature SubstitutionMap::getGenericSignature() const { return storage ? storage->getGenericSignature() : nullptr; } diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp index abdc06e298980..47d777e78bea2 100644 --- a/lib/AST/Type.cpp +++ b/lib/AST/Type.cpp @@ -1385,26 +1385,12 @@ Type SugarType::getSinglyDesugaredTypeSlow() { return UnderlyingType; } -SmallVector TypeAliasType::getInnermostGenericArgs() const { - SmallVector result; +ArrayRef TypeAliasType::getDirectGenericArgs() const { + if (!typealias->isGeneric()) return { }; - // If the typealias is not generic, there are no generic arguments - if (!typealias->isGeneric()) return result; - - // If the substitution map is empty, bail out. - auto subMap = getSubstitutionMap(); - if (subMap.empty()) return result; - - // Retrieve the substitutions for the generic parameters (only). - auto genericSig = subMap.getGenericSignature(); - unsigned numAllGenericParams = genericSig->getGenericParams().size(); - unsigned numMyGenericParams = typealias->getGenericParams()->size(); - result.reserve(numMyGenericParams); - unsigned startIndex = numAllGenericParams - numMyGenericParams; - for (auto gp : genericSig->getGenericParams().slice(startIndex)) { - result.push_back(Type(gp).subst(subMap)); - } - return result; + // Otherwise, the innermost replacement types are the direct + // generic arguments. + return getSubstitutionMap().getInnermostReplacementTypes(); } unsigned GenericTypeParamType::getDepth() const { diff --git a/lib/AST/TypeWalker.cpp b/lib/AST/TypeWalker.cpp index aed203667230e..8f5213adfecfe 100644 --- a/lib/AST/TypeWalker.cpp +++ b/lib/AST/TypeWalker.cpp @@ -39,7 +39,7 @@ class Traversal : public TypeVisitor if (auto parent = ty->getParent()) if (doIt(parent)) return true; - for (auto arg : ty->getInnermostGenericArgs()) + for (const auto arg : ty->getDirectGenericArgs()) if (doIt(arg)) return true;