Skip to content

Commit 37382fe

Browse files
author
Harlan Haskins
authored
Merge pull request swiftlang#27065 from harlanhaskins/type-and-circumstance
[ModuleInterfaces] Escape `Type` and `Protocol` when module-qualifying
2 parents f65a703 + 2893d96 commit 37382fe

File tree

8 files changed

+105
-69
lines changed

8 files changed

+105
-69
lines changed

include/swift/AST/ASTPrinter.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ class ASTPrinter {
136136
/// \param T the original \c Type being referenced. May be null.
137137
/// \param RefTo the \c TypeDecl this is considered a reference to.
138138
/// \param Name the name to be printed.
139-
virtual void printTypeRef(Type T, const TypeDecl *RefTo, Identifier Name);
139+
/// \param NameContext the \c PrintNameContext which this type is being
140+
/// printed in, used to determine how to escape type names.
141+
virtual void printTypeRef(
142+
Type T, const TypeDecl *RefTo, Identifier Name,
143+
PrintNameContext NameContext = PrintNameContext::Normal);
140144

141145
/// Called when printing the referenced name of a module.
142146
virtual void printModuleRef(ModuleEntity Mod, Identifier Name);

lib/AST/ASTPrinter.cpp

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ void ASTPrinter::printEscapedStringLiteral(StringRef str) {
285285
printTextImpl(escapeBuf.str());
286286
}
287287

288-
void ASTPrinter::printTypeRef(Type T, const TypeDecl *RefTo, Identifier Name) {
289-
PrintNameContext Context = PrintNameContext::Normal;
288+
void ASTPrinter::printTypeRef(Type T, const TypeDecl *RefTo, Identifier Name,
289+
PrintNameContext Context) {
290290
if (isa<GenericTypeParamDecl>(RefTo)) {
291291
Context = PrintNameContext::GenericParameter;
292292
} else if (T && T->is<DynamicSelfType>()) {
@@ -3490,9 +3490,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
34903490
}
34913491

34923492
template <typename T>
3493-
void printTypeDeclName(T *Ty) {
3493+
void printTypeDeclName(
3494+
T *Ty, PrintNameContext NameContext = PrintNameContext::Normal) {
34943495
TypeDecl *TD = Ty->getDecl();
3495-
Printer.printTypeRef(Ty, TD, TD->getName());
3496+
Printer.printTypeRef(Ty, TD, TD->getName(), NameContext);
34963497
}
34973498

34983499
// FIXME: we should have a callback that would tell us
@@ -3548,6 +3549,24 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
35483549
TypePrinter(ASTPrinter &Printer, const PrintOptions &PO)
35493550
: Printer(Printer), Options(PO) {}
35503551

3552+
template <typename T>
3553+
void printQualifiedType(T *Ty) {
3554+
PrintNameContext NameContext = PrintNameContext::Normal;
3555+
3556+
// If we printed a parent type or a module qualification, let the printer
3557+
// know we're printing a type member so it escapes `Type` and `Protocol`.
3558+
if (auto parent = Ty->getParent()) {
3559+
visitParentType(parent);
3560+
Printer << ".";
3561+
NameContext = PrintNameContext::TypeMember;
3562+
} else if (shouldPrintFullyQualified(Ty)) {
3563+
printModuleContext(Ty);
3564+
NameContext = PrintNameContext::TypeMember;
3565+
}
3566+
3567+
printTypeDeclName(Ty, NameContext);
3568+
}
3569+
35513570
void visit(Type T) {
35523571
Printer.printTypePre(TypeLoc::withoutLoc(T));
35533572
SWIFT_DEFER { Printer.printTypePost(TypeLoc::withoutLoc(T)); };
@@ -3600,14 +3619,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
36003619
return;
36013620
}
36023621

3603-
if (auto parent = T->getParent()) {
3604-
visit(parent);
3605-
Printer << ".";
3606-
} else if (shouldPrintFullyQualified(T)) {
3607-
printModuleContext(T);
3608-
}
3609-
3610-
printTypeDeclName(T);
3622+
printQualifiedType(T);
36113623
printGenericArgs(T->getInnermostGenericArgs());
36123624
}
36133625

@@ -3654,13 +3666,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
36543666
}
36553667

36563668
void visitUnboundGenericType(UnboundGenericType *T) {
3657-
if (auto ParentType = T->getParent()) {
3658-
visit(ParentType);
3659-
Printer << ".";
3660-
} else if (shouldPrintFullyQualified(T)) {
3661-
printModuleContext(T);
3662-
}
3663-
printTypeDeclName(T);
3669+
printQualifiedType(T);
36643670
}
36653671

36663672
void visitBoundGenericType(BoundGenericType *T) {
@@ -3687,14 +3693,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
36873693
return;
36883694
}
36893695
}
3690-
if (auto ParentType = T->getParent()) {
3691-
visit(ParentType);
3692-
Printer << ".";
3693-
} else if (shouldPrintFullyQualified(T)) {
3694-
printModuleContext(T);
3695-
}
3696-
3697-
printTypeDeclName(T);
3696+
printQualifiedType(T);
36983697
printGenericArgs(T->getGenericArgs());
36993698
}
37003699

@@ -3709,36 +3708,15 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
37093708
}
37103709

37113710
void visitEnumType(EnumType *T) {
3712-
if (auto ParentType = T->getParent()) {
3713-
visitParentType(ParentType);
3714-
Printer << ".";
3715-
} else if (shouldPrintFullyQualified(T)) {
3716-
printModuleContext(T);
3717-
}
3718-
3719-
printTypeDeclName(T);
3711+
printQualifiedType(T);
37203712
}
37213713

37223714
void visitStructType(StructType *T) {
3723-
if (auto ParentType = T->getParent()) {
3724-
visitParentType(ParentType);
3725-
Printer << ".";
3726-
} else if (shouldPrintFullyQualified(T)) {
3727-
printModuleContext(T);
3728-
}
3729-
3730-
printTypeDeclName(T);
3715+
printQualifiedType(T);
37313716
}
37323717

37333718
void visitClassType(ClassType *T) {
3734-
if (auto ParentType = T->getParent()) {
3735-
visitParentType(ParentType);
3736-
Printer << ".";
3737-
} else if (shouldPrintFullyQualified(T)) {
3738-
printModuleContext(T);
3739-
}
3740-
3741-
printTypeDeclName(T);
3719+
printQualifiedType(T);
37423720
}
37433721

37443722
void visitAnyMetatypeType(AnyMetatypeType *T) {
@@ -4129,11 +4107,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
41294107
}
41304108

41314109
void visitProtocolType(ProtocolType *T) {
4132-
if (shouldPrintFullyQualified(T)) {
4133-
printModuleContext(T);
4134-
}
4135-
4136-
printTypeDeclName(T);
4110+
printQualifiedType(T);
41374111
}
41384112

41394113
void visitProtocolCompositionType(ProtocolCompositionType *T) {

lib/IDE/ModuleInterfacePrinting.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ class ClangCommentPrinter : public ASTPrinter {
7474
void printTypePost(const TypeLoc &TL) override {
7575
return OtherPrinter.printTypePost(TL);
7676
}
77-
void printTypeRef(Type T, const TypeDecl *TD, Identifier Name) override {
78-
return OtherPrinter.printTypeRef(T, TD, Name);
77+
void printTypeRef(Type T, const TypeDecl *TD, Identifier Name,
78+
PrintNameContext NameContext) override {
79+
return OtherPrinter.printTypeRef(T, TD, Name, NameContext);
7980
}
8081
void printModuleRef(ModuleEntity Mod, Identifier Name) override {
8182
return OtherPrinter.printModuleRef(Mod, Name);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path %t/MyModule.swiftinterface -enable-library-evolution %s -module-name MyModule
4+
// RUN: %FileCheck %s < %t/MyModule.swiftinterface
5+
6+
// RUN: %target-swift-frontend -compile-module-from-interface %t/MyModule.swiftinterface -o %t/MyModule.swiftmodule
7+
8+
// CHECK: public struct Type {
9+
// CHECK-NEXT: }
10+
public struct Type {}
11+
12+
// CHECK: public protocol Protocol {
13+
// CHECK-NEXT: }
14+
public protocol Protocol {}
15+
16+
// CHECK: public func usesType(_ x: MyModule.`Type`)
17+
public func usesType(_ x: Type) {}
18+
19+
// CHECK: public func genericProtocol<T>(_ x: T) where T : MyModule.`Protocol`
20+
public func genericProtocol<T: Protocol>(_ x: T) {}
21+
22+
// CHECK: public func existentialProtocol(_ x: MyModule.`Protocol`)
23+
public func existentialProtocol(_ x: Protocol) {}
24+
25+
// CHECK: public struct Parent {
26+
public struct Parent {
27+
// CHECK: public struct `Type` {
28+
public struct `Type` {
29+
// CHECK: public struct `Protocol` {
30+
// CHECK-NEXT: }
31+
public struct `Protocol` {}
32+
// CHECK-NEXT: }
33+
}
34+
// CHECK: public struct `Protocol` {
35+
// CHECK-NEXT: }
36+
public struct `Protocol` {}
37+
// CHECK-NEXT: }
38+
}
39+
40+
// CHECK: public func usesNestedType(_ x: MyModule.Parent.`Type`)
41+
public func usesNestedType(_ x: Parent.`Type`) {}
42+
43+
// CHECK: public func usesNestedTypeProtocol(_ x: MyModule.Parent.`Type`.`Protocol`)
44+
public func usesNestedTypeProtocol(_ x: Parent.`Type`.`Protocol`) {}
45+
46+
// CHECK: public func usesNestedProtocol(_ x: MyModule.Parent.`Protocol`)
47+
public func usesNestedProtocol(_ x: Parent.`Protocol`) {}

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,12 @@ class AnnotatingPrinter : public StreamPrinter {
241241
deinitDefaultMapToUse(D);
242242
}
243243

244-
void printTypeRef(Type T, const TypeDecl *TD, Identifier Name) override {
244+
void printTypeRef(
245+
Type T, const TypeDecl *TD, Identifier Name,
246+
PrintNameContext NameContext = PrintNameContext::Normal) override {
245247
unsigned StartOffset = OS.tell();
246248
References.emplace_back(TD, StartOffset, Name.str().size());
247-
StreamPrinter::printTypeRef(T, TD, Name);
249+
StreamPrinter::printTypeRef(T, TD, Name, NameContext);
248250
}
249251
};
250252

tools/SourceKit/lib/SwiftLang/SwiftEditorInterfaceGen.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,12 @@ class AnnotatingPrinter : public StreamPrinter {
191191
}
192192
}
193193

194-
void printTypeRef(Type T, const TypeDecl *TD, Identifier Name) override {
194+
void printTypeRef(
195+
Type T, const TypeDecl *TD, Identifier Name,
196+
PrintNameContext NameContext = PrintNameContext::Normal) override {
195197
unsigned StartOffset = OS.tell();
196198
Info.References.emplace_back(TD, StartOffset, Name.str().size());
197-
StreamPrinter::printTypeRef(T, TD, Name);
199+
StreamPrinter::printTypeRef(T, TD, Name, NameContext);
198200
}
199201

200202
void printModuleRef(ModuleEntity Mod, Identifier Name) override {

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ class AnnotatedDeclarationPrinter : public XMLEscapingPrinter {
5757
:XMLEscapingPrinter(OS) { }
5858

5959
private:
60-
void printTypeRef(Type T, const TypeDecl *TD, Identifier Name) override {
60+
void printTypeRef(
61+
Type T, const TypeDecl *TD, Identifier Name,
62+
PrintNameContext NameContext = PrintNameContext::Normal) override {
6163
printXML("<Type usr=\"");
6264
SwiftLangSupport::printUSR(TD, OS);
6365
printXML("\">");
64-
StreamPrinter::printTypeRef(T, TD, Name);
66+
StreamPrinter::printTypeRef(T, TD, Name, NameContext);
6567
printXML("</Type>");
6668
}
6769
};
@@ -272,11 +274,13 @@ class FullyAnnotatedDeclarationPrinter final : public XMLEscapingPrinter {
272274
closeTag(tag);
273275
}
274276

275-
void printTypeRef(Type T, const TypeDecl *TD, Identifier name) override {
277+
void printTypeRef(
278+
Type T, const TypeDecl *TD, Identifier name,
279+
PrintNameContext NameContext = PrintNameContext::Normal) override {
276280
auto tag = getTagForDecl(TD, /*isRef=*/true);
277281
openTagWithUSRForDecl(tag, TD);
278282
insideRef = true;
279-
XMLEscapingPrinter::printTypeRef(T, TD, name);
283+
XMLEscapingPrinter::printTypeRef(T, TD, name, NameContext);
280284
insideRef = false;
281285
closeTag(tag);
282286
}

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,9 +1965,11 @@ class AnnotatingPrinter : public StreamPrinter {
19651965
OS << "</synthesized>";
19661966
}
19671967

1968-
void printTypeRef(Type T, const TypeDecl *TD, Identifier Name) override {
1968+
void printTypeRef(
1969+
Type T, const TypeDecl *TD, Identifier Name,
1970+
PrintNameContext NameContext = PrintNameContext::Normal) override {
19691971
OS << "<ref:" << Decl::getKindName(TD->getKind()) << '>';
1970-
StreamPrinter::printTypeRef(T, TD, Name);
1972+
StreamPrinter::printTypeRef(T, TD, Name, NameContext);
19711973
OS << "</ref>";
19721974
}
19731975
void printModuleRef(ModuleEntity Mod, Identifier Name) override {

0 commit comments

Comments
 (0)