@@ -55,32 +55,56 @@ void PrintOptions::setBaseType(Type T) {
55
55
TransformContext = TypeTransformContext (T);
56
56
}
57
57
58
- void PrintOptions::initForSynthesizedExtension (NominalTypeDecl * D) {
58
+ void PrintOptions::initForSynthesizedExtension (TypeOrExtensionDecl D) {
59
59
TransformContext = TypeTransformContext (D);
60
60
}
61
61
62
62
void PrintOptions::clearSynthesizedExtension () {
63
63
TransformContext.reset ();
64
64
}
65
65
66
+ TypeOrExtensionDecl::TypeOrExtensionDecl (NominalTypeDecl *D) : Decl(D) {}
67
+ TypeOrExtensionDecl::TypeOrExtensionDecl (ExtensionDecl *D) : Decl(D) {}
68
+
69
+ Decl *TypeOrExtensionDecl::getAsDecl () const {
70
+ if (auto NTD = Decl.dyn_cast <NominalTypeDecl *>())
71
+ return NTD;
72
+
73
+ return Decl.get <ExtensionDecl *>();
74
+ }
75
+ DeclContext *TypeOrExtensionDecl::getAsDeclContext () const {
76
+ return getAsDecl ()->getInnermostDeclContext ();
77
+ }
78
+ NominalTypeDecl *TypeOrExtensionDecl::getBaseNominal () const {
79
+ return getAsDeclContext ()->getAsNominalTypeOrNominalTypeExtensionContext ();
80
+ }
81
+ bool TypeOrExtensionDecl::isNull () const { return Decl.isNull (); }
82
+
66
83
TypeTransformContext::TypeTransformContext (Type T)
67
84
: BaseType(T.getPointer()) {
68
85
assert (T->mayHaveMembers ());
69
86
}
70
87
71
- TypeTransformContext::TypeTransformContext (NominalTypeDecl *NTD)
72
- : BaseType(NTD->getDeclaredTypeInContext ().getPointer()), Nominal(NTD) {}
88
+ TypeTransformContext::TypeTransformContext (TypeOrExtensionDecl D)
89
+ : BaseType(nullptr ), Decl(D) {
90
+ if (auto NTD = Decl.Decl .dyn_cast <NominalTypeDecl *>())
91
+ BaseType = NTD->getDeclaredTypeInContext ().getPointer ();
92
+ else
93
+ BaseType = Decl.Decl .get <ExtensionDecl *>()->getExtendedType ().getPointer ();
94
+ }
95
+
96
+ TypeOrExtensionDecl TypeTransformContext::getDecl () const { return Decl; }
73
97
74
- NominalTypeDecl *TypeTransformContext::getNominal () const {
75
- return Nominal ;
98
+ DeclContext *TypeTransformContext::getDeclContext () const {
99
+ return Decl. getAsDecl ()-> getDeclContext () ;
76
100
}
77
101
78
102
Type TypeTransformContext::getBaseType () const {
79
103
return Type (BaseType);
80
104
}
81
105
82
106
bool TypeTransformContext::isPrintingSynthesizedExtension () const {
83
- return Nominal != nullptr ;
107
+ return !Decl. isNull () ;
84
108
}
85
109
86
110
std::string ASTPrinter::sanitizeUtf8 (StringRef Text) {
@@ -631,8 +655,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
631
655
#define STMT (Name, Parent ) void visit##Name##Stmt(Name##Stmt *stmt);
632
656
#include " swift/AST/StmtNodes.def"
633
657
634
- void printSynthesizedExtension (NominalTypeDecl* Decl,
635
- ExtensionDecl* ExtDecl);
658
+ void printSynthesizedExtension (Type ExtendedType, ExtensionDecl *ExtDecl);
636
659
637
660
void printExtension (ExtensionDecl* ExtDecl);
638
661
@@ -668,8 +691,9 @@ class PrintAST : public ASTVisitor<PrintAST> {
668
691
Options.TransformContext &&
669
692
Options.TransformContext ->isPrintingSynthesizedExtension () &&
670
693
isa<ExtensionDecl>(D);
671
- if (Synthesize)
672
- Printer.setSynthesizedTarget (Options.TransformContext ->getNominal ());
694
+ if (Synthesize) {
695
+ Printer.setSynthesizedTarget (Options.TransformContext ->getDecl ());
696
+ }
673
697
674
698
// We want to print a newline before doc comments. Swift code already
675
699
// handles this, but we need to insert it for clang doc comments when not
@@ -692,10 +716,10 @@ class PrintAST : public ASTVisitor<PrintAST> {
692
716
ASTVisitor::visit (D);
693
717
694
718
if (Synthesize) {
695
- Printer.setSynthesizedTarget (nullptr );
696
- Printer.printSynthesizedExtensionPost (
697
- cast<ExtensionDecl>(D), Options.TransformContext ->getNominal (),
698
- Options.BracketOptions );
719
+ Printer.setSynthesizedTarget ({} );
720
+ Printer.printSynthesizedExtensionPost (cast<ExtensionDecl>(D),
721
+ Options.TransformContext ->getDecl (),
722
+ Options.BracketOptions );
699
723
} else {
700
724
Printer.callPrintDeclPost (D, Options.BracketOptions );
701
725
}
@@ -1775,14 +1799,14 @@ static void printExtendedTypeName(Type ExtendedType, ASTPrinter &Printer,
1775
1799
Printer.printTypeRef (ExtendedType, Nominal, Nominal->getName ());
1776
1800
}
1777
1801
1778
- void PrintAST::
1779
- printSynthesizedExtension (NominalTypeDecl* Decl, ExtensionDecl *ExtDecl) {
1802
+ void PrintAST::printSynthesizedExtension (Type ExtendedType,
1803
+ ExtensionDecl *ExtDecl) {
1780
1804
if (Options.BracketOptions .shouldOpenExtension (ExtDecl)) {
1781
1805
printDocumentationComment (ExtDecl);
1782
1806
printAttributes (ExtDecl);
1783
1807
Printer << tok::kw_extension << " " ;
1784
1808
1785
- printExtendedTypeName (Decl-> getDeclaredType () , Printer, Options);
1809
+ printExtendedTypeName (ExtendedType , Printer, Options);
1786
1810
printInherited (ExtDecl);
1787
1811
1788
1812
if (ExtDecl->getGenericParams ())
@@ -1832,9 +1856,11 @@ void PrintAST::printExtension(ExtensionDecl *decl) {
1832
1856
1833
1857
void PrintAST::visitExtensionDecl (ExtensionDecl *decl) {
1834
1858
if (Options.TransformContext &&
1835
- Options.TransformContext ->isPrintingSynthesizedExtension ())
1836
- printSynthesizedExtension (Options.TransformContext ->getNominal (), decl);
1837
- else
1859
+ Options.TransformContext ->isPrintingSynthesizedExtension ()) {
1860
+ auto extendedType =
1861
+ Options.TransformContext ->getBaseType ()->mapTypeOutOfContext ();
1862
+ printSynthesizedExtension (extendedType, decl);
1863
+ } else
1838
1864
printExtension (decl);
1839
1865
}
1840
1866
0 commit comments