Skip to content

Commit 01e6cb3

Browse files
Merge pull request #31812 from AnthonyLatsis/substitution-revolution-pre-1
[NFC] AST: Define SubstitutionMap::getInnermostReplacementTypes
2 parents b2f809a + eedb335 commit 01e6cb3

File tree

7 files changed

+23
-29
lines changed

7 files changed

+23
-29
lines changed

include/swift/AST/SubstitutionMap.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ class SubstitutionMap {
150150
/// generic parameters.
151151
ArrayRef<Type> getReplacementTypes() const;
152152

153+
/// Retrieve the array of replacement types for the innermost generic
154+
/// parameters.
155+
ArrayRef<Type> getInnermostReplacementTypes() const;
156+
153157
/// Query whether any replacement types in the map contain archetypes.
154158
bool hasArchetypes() const;
155159

include/swift/AST/Types.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,13 +1813,10 @@ class TypeAliasType final
18131813
return *getTrailingObjects<SubstitutionMap>();
18141814
}
18151815

1816-
/// Get the innermost generic arguments, which correspond to the generic
1817-
/// arguments that are directly applied to the typealias declaration in
1818-
/// produced by \c getDecl().
1819-
///
1820-
/// The result can be empty, if the declaration itself is non-generic but
1821-
/// the parent is generic.
1822-
SmallVector<Type, 2> getInnermostGenericArgs() const;
1816+
/// Get the direct generic arguments, which correspond to the generic
1817+
/// arguments that are directly applied to the typealias declaration
1818+
/// this type references.
1819+
ArrayRef<Type> getDirectGenericArgs() const;
18231820

18241821
// Support for FoldingSet.
18251822
void Profile(llvm::FoldingSetNodeID &id) const;

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3517,7 +3517,7 @@ namespace {
35173517
if (T->getParent())
35183518
printRec("parent", T->getParent());
35193519

3520-
for (auto arg : T->getInnermostGenericArgs())
3520+
for (const auto arg : T->getDirectGenericArgs())
35213521
printRec(arg);
35223522
PrintWithColorRAII(OS, ParenthesisColor) << ')';
35233523
}

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3812,7 +3812,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
38123812
}
38133813

38143814
printQualifiedType(T);
3815-
printGenericArgs(T->getInnermostGenericArgs());
3815+
printGenericArgs(T->getDirectGenericArgs());
38163816
}
38173817

38183818
void visitParenType(ParenType *T) {

lib/AST/SubstitutionMap.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ ArrayRef<Type> SubstitutionMap::getReplacementTypes() const {
9797
return getReplacementTypesBuffer();
9898
}
9999

100+
ArrayRef<Type> SubstitutionMap::getInnermostReplacementTypes() const {
101+
if (empty()) return { };
102+
103+
return getReplacementTypes().take_back(
104+
getGenericSignature()->getInnermostGenericParams().size());
105+
}
106+
100107
GenericSignature SubstitutionMap::getGenericSignature() const {
101108
return storage ? storage->getGenericSignature() : nullptr;
102109
}

lib/AST/Type.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,26 +1384,12 @@ Type SugarType::getSinglyDesugaredTypeSlow() {
13841384
return UnderlyingType;
13851385
}
13861386

1387-
SmallVector<Type, 2> TypeAliasType::getInnermostGenericArgs() const {
1388-
SmallVector<Type, 2> result;
1387+
ArrayRef<Type> TypeAliasType::getDirectGenericArgs() const {
1388+
if (!typealias->isGeneric()) return { };
13891389

1390-
// If the typealias is not generic, there are no generic arguments
1391-
if (!typealias->isGeneric()) return result;
1392-
1393-
// If the substitution map is empty, bail out.
1394-
auto subMap = getSubstitutionMap();
1395-
if (subMap.empty()) return result;
1396-
1397-
// Retrieve the substitutions for the generic parameters (only).
1398-
auto genericSig = subMap.getGenericSignature();
1399-
unsigned numAllGenericParams = genericSig->getGenericParams().size();
1400-
unsigned numMyGenericParams = typealias->getGenericParams()->size();
1401-
result.reserve(numMyGenericParams);
1402-
unsigned startIndex = numAllGenericParams - numMyGenericParams;
1403-
for (auto gp : genericSig->getGenericParams().slice(startIndex)) {
1404-
result.push_back(Type(gp).subst(subMap));
1405-
}
1406-
return result;
1390+
// Otherwise, the innermost replacement types are the direct
1391+
// generic arguments.
1392+
return getSubstitutionMap().getInnermostReplacementTypes();
14071393
}
14081394

14091395
unsigned GenericTypeParamType::getDepth() const {

lib/AST/TypeWalker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Traversal : public TypeVisitor<Traversal, bool>
3939
if (auto parent = ty->getParent())
4040
if (doIt(parent)) return true;
4141

42-
for (auto arg : ty->getInnermostGenericArgs())
42+
for (const auto arg : ty->getDirectGenericArgs())
4343
if (doIt(arg))
4444
return true;
4545

0 commit comments

Comments
 (0)