Skip to content

[NFC] AST: Define SubstitutionMap::getInnermostReplacementTypes #31812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/AST/SubstitutionMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ class SubstitutionMap {
/// generic parameters.
ArrayRef<Type> getReplacementTypes() const;

/// Retrieve the array of replacement types for the innermost generic
/// parameters.
ArrayRef<Type> getInnermostReplacementTypes() const;

/// Query whether any replacement types in the map contain archetypes.
bool hasArchetypes() const;

Expand Down
11 changes: 4 additions & 7 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1803,13 +1803,10 @@ class TypeAliasType final
return *getTrailingObjects<SubstitutionMap>();
}

/// 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<Type, 2> 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<Type> getDirectGenericArgs() const;

// Support for FoldingSet.
void Profile(llvm::FoldingSetNodeID &id) const;
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) << ')';
}
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3727,7 +3727,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
}

printQualifiedType(T);
printGenericArgs(T->getInnermostGenericArgs());
printGenericArgs(T->getDirectGenericArgs());
}

void visitParenType(ParenType *T) {
Expand Down
7 changes: 7 additions & 0 deletions lib/AST/SubstitutionMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ ArrayRef<Type> SubstitutionMap::getReplacementTypes() const {
return getReplacementTypesBuffer();
}

ArrayRef<Type> SubstitutionMap::getInnermostReplacementTypes() const {
if (empty()) return { };

return getReplacementTypes().take_back(
getGenericSignature()->getInnermostGenericParams().size());
}

GenericSignature SubstitutionMap::getGenericSignature() const {
return storage ? storage->getGenericSignature() : nullptr;
}
Expand Down
24 changes: 5 additions & 19 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1385,26 +1385,12 @@ Type SugarType::getSinglyDesugaredTypeSlow() {
return UnderlyingType;
}

SmallVector<Type, 2> TypeAliasType::getInnermostGenericArgs() const {
SmallVector<Type, 2> result;
ArrayRef<Type> 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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/TypeWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Traversal : public TypeVisitor<Traversal, bool>
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;

Expand Down