Skip to content

Commit 01ec4f2

Browse files
committed
[TypeInterface] Replace generic params with instantiated type when printing type-specific interface.
1 parent 6b125a2 commit 01ec4f2

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace swift {
2020
class GenericParamList;
2121
class CanType;
2222
class ExtensionDecl;
23+
class TypeBase;
2324
enum DeclAttrKind : unsigned;
2425

2526
/// Options for printing AST nodes.
@@ -187,6 +188,12 @@ struct PrintOptions {
187188
/// \brief Print types with alternative names from their canonical names.
188189
llvm::DenseMap<CanType, Identifier> *AlternativeTypeNames = nullptr;
189190

191+
/// \brief When printing a type interface, register the type to print.
192+
TypeBase *TypeToPrint = nullptr;
193+
194+
/// \brief When printing a type interface, whether we instantiate archetypes.
195+
bool InstantiateArchetype = false;
196+
190197
/// Retrieve the set of options for verbose printing to users.
191198
static PrintOptions printVerbose() {
192199
PrintOptions result;
@@ -227,6 +234,13 @@ struct PrintOptions {
227234
return result;
228235
}
229236

237+
static PrintOptions printTypeInterface(TypeBase *T) {
238+
PrintOptions result = printInterface();
239+
result.TypeToPrint = T;
240+
result.InstantiateArchetype = true;
241+
return result;
242+
}
243+
230244
/// Retrive the print options that are suitable to print the testable interface.
231245
static PrintOptions printTestableInterface() {
232246
PrintOptions result = printInterface();

lib/AST/ASTPrinter.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ bool ASTPrinter::printTypeInterface(Type Ty, llvm::raw_ostream &OS) {
7474
if (!Ty)
7575
return false;
7676
Ty = Ty->getRValueType();
77-
PrintOptions Options = PrintOptions::printInterface();
77+
PrintOptions Options = PrintOptions::printTypeInterface(Ty.getPointer());
7878
if (auto ND = Ty->getNominalOrBoundGenericNominal()) {
7979
Options.printExtensionContentAsMembers = [&](const ExtensionDecl *ED) {
8080
return isExtensionApplied(*ND->getDeclContext(), Ty, ED);
@@ -594,18 +594,31 @@ void PrintAST::printGenericParams(GenericParamList *Params) {
594594

595595
Printer << "<";
596596
bool IsFirst = true;
597-
for (auto GP : Params->getParams()) {
598-
if (IsFirst) {
599-
IsFirst = false;
600-
} else {
601-
Printer << ", ";
597+
SmallVector<Type, 4> Scrach;
598+
if (Options.InstantiateArchetype) {
599+
auto ArgArr = Options.TypeToPrint->getAllGenericArgs(Scrach);
600+
for (auto Arg : ArgArr) {
601+
if (IsFirst) {
602+
IsFirst = false;
603+
} else {
604+
Printer << ", ";
605+
}
606+
auto NM = Arg->getAnyNominal();
607+
assert(NM && "Cannot get nominal type.");
608+
Printer << NM->getNameStr();
602609
}
603-
604-
Printer.printName(GP->getName());
605-
printInherited(GP);
610+
} else {
611+
for (auto GP : Params->getParams()) {
612+
if (IsFirst) {
613+
IsFirst = false;
614+
} else {
615+
Printer << ", ";
616+
}
617+
Printer.printName(GP->getName());
618+
printInherited(GP);
619+
}
620+
printWhereClause(Params->getRequirements());
606621
}
607-
608-
printWhereClause(Params->getRequirements());
609622
Printer << ">";
610623
}
611624

test/IDE/print_type_interface.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ class C2 {
4848
// RUN: %target-swift-ide-test -print-type-interface -pos=43:6 -source-filename %s | FileCheck %s -check-prefix=TYPE3
4949

5050

51-
// TYPE2: public class D<T> {
51+
// TYPE2: public class D<T1> {
5252
// TYPE2-NEXT: public func foo()
5353
// TYPE2-NEXT: public func conditionalFunc1()
5454
// TYPE2-NEXT: public func unconditionalFunc1()
5555
// TYPE2-NEXT: }
5656

57-
// TYPE3: public class D<T> {
57+
// TYPE3: public class D<Int> {
5858
// TYPE3-NEXT: public func foo()
5959
// TYPE3-NEXT: public func unconditionalFunc1()
6060
// TYPE3-NEXT: }

0 commit comments

Comments
 (0)