Skip to content

Commit 3759d34

Browse files
committed
AST: Refactor printing of generic types a bit
1 parent 3434a5e commit 3759d34

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

include/swift/AST/Types.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,7 +2081,7 @@ class TypeAliasType final
20812081
/// this type references.
20822082
ArrayRef<Type> getDirectGenericArgs() const;
20832083

2084-
PackType *getExpandedGenericArgsPack();
2084+
SmallVector<Type, 2> getExpandedGenericArgs();
20852085

20862086
// Support for FoldingSet.
20872087
void Profile(llvm::FoldingSetNodeID &id) const;
@@ -2562,7 +2562,7 @@ class BoundGenericType : public NominalOrBoundGenericNominalType,
25622562
return {getTrailingObjectsPointer(), Bits.BoundGenericType.GenericArgCount};
25632563
}
25642564

2565-
PackType *getExpandedGenericArgsPack();
2565+
SmallVector<Type, 2> getExpandedGenericArgs();
25662566

25672567
void Profile(llvm::FoldingSetNodeID &ID) {
25682568
Profile(ID, getDecl(), getParent(), getGenericArgs());
@@ -6820,15 +6820,15 @@ class PackType final : public TypeBase, public llvm::FoldingSetNode,
68206820
/// Creates a pack from the types in \p elements.
68216821
static PackType *get(const ASTContext &C, ArrayRef<Type> elements);
68226822

6823-
static PackType *get(const ASTContext &C,
6824-
TypeArrayView<GenericTypeParamType> params,
6825-
ArrayRef<Type> args);
6826-
68276823
/// Given a type T, which must be a pack parameter, a member type
68286824
/// of a pack parameter, or a pack archetype, construct the type
68296825
/// Pack{repeat each T}.
68306826
static PackType *getSingletonPackExpansion(Type packParameter);
68316827

6828+
static SmallVector<Type, 2> getExpandedGenericArgs(
6829+
TypeArrayView<GenericTypeParamType> params,
6830+
ArrayRef<Type> args);
6831+
68326832
public:
68336833
/// Retrieves the number of elements in this pack.
68346834
unsigned getNumElements() const { return Bits.PackType.Count; }

lib/AST/ASTPrinter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5590,9 +5590,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
55905590
Optional<llvm::DenseMap<const clang::Module *, ModuleDecl *>>
55915591
VisibleClangModules;
55925592

5593-
void printGenericArgs(PackType *flatArgs) {
5593+
void printGenericArgs(ArrayRef<Type> flatArgs) {
55945594
Printer << "<";
5595-
interleave(flatArgs->getElementTypes(),
5595+
interleave(flatArgs,
55965596
[&](Type arg) { visit(arg); },
55975597
[&] { Printer << ", "; });
55985598
Printer << ">";
@@ -5601,7 +5601,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
56015601
void printGenericArgs(ASTContext &ctx,
56025602
TypeArrayView<GenericTypeParamType> params,
56035603
ArrayRef<Type> args) {
5604-
printGenericArgs(PackType::get(ctx, params, args));
5604+
printGenericArgs(PackType::getExpandedGenericArgs(params, args));
56055605
}
56065606

56075607
/// Helper function for printing a type that is embedded within a larger type.
@@ -5952,7 +5952,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
59525952

59535953
auto *typeAliasDecl = T->getDecl();
59545954
if (typeAliasDecl->isGeneric()) {
5955-
printGenericArgs(T->getExpandedGenericArgsPack());
5955+
printGenericArgs(T->getExpandedGenericArgs());
59565956
}
59575957
}
59585958

@@ -6057,7 +6057,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
60576057
}
60586058
printQualifiedType(T);
60596059

6060-
printGenericArgs(T->getExpandedGenericArgsPack());
6060+
printGenericArgs(T->getExpandedGenericArgs());
60616061
}
60626062

60636063
void visitParentType(Type T) {

lib/AST/ParameterPack.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ struct PackTypeParameterCollector: TypeWalker {
4040
if (auto parentType = boundGenericType->getParent())
4141
parentType.walk(*this);
4242

43-
Type(boundGenericType->getExpandedGenericArgsPack()).walk(*this);
43+
for (auto type : boundGenericType->getExpandedGenericArgs())
44+
type.walk(*this);
45+
4446
return Action::SkipChildren;
4547
}
4648

@@ -49,7 +51,9 @@ struct PackTypeParameterCollector: TypeWalker {
4951
if (auto parentType = typeAliasType->getParent())
5052
parentType.walk(*this);
5153

52-
Type(typeAliasType->getExpandedGenericArgsPack()).walk(*this);
54+
for (auto type : typeAliasType->getExpandedGenericArgs())
55+
type.walk(*this);
56+
5357
return Action::SkipChildren;
5458
}
5559
}
@@ -244,7 +248,7 @@ unsigned ParameterList::getOrigParamIndex(SubstitutionMap subMap,
244248
}
245249

246250
/// <T...> Foo<T, Pack{Int, String}> => Pack{T..., Int, String}
247-
PackType *BoundGenericType::getExpandedGenericArgsPack() {
251+
SmallVector<Type, 2> BoundGenericType::getExpandedGenericArgs() {
248252
// It would be nicer to use genericSig.getInnermostGenericParams() here,
249253
// but that triggers a request cycle if we're in the middle of computing
250254
// the generic signature already.
@@ -253,26 +257,26 @@ PackType *BoundGenericType::getExpandedGenericArgsPack() {
253257
params.push_back(paramDecl->getDeclaredInterfaceType());
254258
}
255259

256-
return PackType::get(getASTContext(),
260+
return PackType::getExpandedGenericArgs(
257261
TypeArrayView<GenericTypeParamType>(params),
258262
getGenericArgs());
259263
}
260264

261265
/// <T...> Foo<T, Pack{Int, String}> => Pack{T..., Int, String}
262-
PackType *TypeAliasType::getExpandedGenericArgsPack() {
266+
SmallVector<Type, 2> TypeAliasType::getExpandedGenericArgs() {
263267
if (!getDecl()->isGeneric())
264-
return nullptr;
268+
return SmallVector<Type, 2>();
265269

266270
auto genericSig = getGenericSignature();
267-
return PackType::get(getASTContext(),
271+
return PackType::getExpandedGenericArgs(
268272
genericSig.getInnermostGenericParams(),
269273
getDirectGenericArgs());
270274
}
271275

272-
/// <T...> Pack{T, Pack{Int, String}} => Pack{T..., Int, String}
273-
PackType *PackType::get(const ASTContext &C,
274-
TypeArrayView<GenericTypeParamType> params,
275-
ArrayRef<Type> args) {
276+
/// <T...> Pack{T, Pack{Int, String}} => {T..., Int, String}
277+
SmallVector<Type, 2>
278+
PackType::getExpandedGenericArgs(TypeArrayView<GenericTypeParamType> params,
279+
ArrayRef<Type> args) {
276280
SmallVector<Type, 2> wrappedArgs;
277281

278282
assert(params.size() == args.size());
@@ -288,7 +292,7 @@ PackType *PackType::get(const ASTContext &C,
288292
wrappedArgs.push_back(arg);
289293
}
290294

291-
return get(C, wrappedArgs);
295+
return wrappedArgs;
292296
}
293297

294298
PackType *PackType::getSingletonPackExpansion(Type param) {
@@ -347,7 +351,7 @@ static CanPackType getApproximateFormalPackType(const ASTContext &ctx,
347351
Collection loweredEltTypes) {
348352
// Build an array of formal element types, but be lazy about it:
349353
// use the original array unless we see an element type that doesn't
350-
// work as a legal format type.
354+
// work as a legal formal type.
351355
Optional<SmallVector<CanType, 4>> formalEltTypes;
352356
for (auto i : indices(loweredEltTypes)) {
353357
auto loweredEltType = loweredEltTypes[i];

0 commit comments

Comments
 (0)