@@ -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) {
@@ -619,8 +643,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
619
643
#define STMT (Name, Parent ) void visit##Name##Stmt(Name##Stmt *stmt);
620
644
#include " swift/AST/StmtNodes.def"
621
645
622
- void printSynthesizedExtension (NominalTypeDecl* Decl,
623
- ExtensionDecl* ExtDecl);
646
+ void printSynthesizedExtension (Type ExtendedType, ExtensionDecl *ExtDecl);
624
647
625
648
void printExtension (ExtensionDecl* ExtDecl);
626
649
@@ -656,8 +679,9 @@ class PrintAST : public ASTVisitor<PrintAST> {
656
679
Options.TransformContext &&
657
680
Options.TransformContext ->isPrintingSynthesizedExtension () &&
658
681
isa<ExtensionDecl>(D);
659
- if (Synthesize)
660
- Printer.setSynthesizedTarget (Options.TransformContext ->getNominal ());
682
+ if (Synthesize) {
683
+ Printer.setSynthesizedTarget (Options.TransformContext ->getDecl ());
684
+ }
661
685
662
686
// We want to print a newline before doc comments. Swift code already
663
687
// handles this, but we need to insert it for clang doc comments when not
@@ -680,10 +704,10 @@ class PrintAST : public ASTVisitor<PrintAST> {
680
704
ASTVisitor::visit (D);
681
705
682
706
if (Synthesize) {
683
- Printer.setSynthesizedTarget (nullptr );
684
- Printer.printSynthesizedExtensionPost (
685
- cast<ExtensionDecl>(D), Options.TransformContext ->getNominal (),
686
- Options.BracketOptions );
707
+ Printer.setSynthesizedTarget ({} );
708
+ Printer.printSynthesizedExtensionPost (cast<ExtensionDecl>(D),
709
+ Options.TransformContext ->getDecl (),
710
+ Options.BracketOptions );
687
711
} else {
688
712
Printer.callPrintDeclPost (D, Options.BracketOptions );
689
713
}
@@ -1766,14 +1790,14 @@ static void printExtendedTypeName(Type ExtendedType, ASTPrinter &Printer,
1766
1790
Printer.printTypeRef (ExtendedType, Nominal, Nominal->getName ());
1767
1791
}
1768
1792
1769
- void PrintAST::
1770
- printSynthesizedExtension (NominalTypeDecl* Decl, ExtensionDecl *ExtDecl) {
1793
+ void PrintAST::printSynthesizedExtension (Type ExtendedType,
1794
+ ExtensionDecl *ExtDecl) {
1771
1795
if (Options.BracketOptions .shouldOpenExtension (ExtDecl)) {
1772
1796
printDocumentationComment (ExtDecl);
1773
1797
printAttributes (ExtDecl);
1774
1798
Printer << tok::kw_extension << " " ;
1775
1799
1776
- printExtendedTypeName (Decl-> getDeclaredType () , Printer, Options);
1800
+ printExtendedTypeName (ExtendedType , Printer, Options);
1777
1801
printInherited (ExtDecl);
1778
1802
1779
1803
if (ExtDecl->getGenericParams ())
@@ -1823,9 +1847,11 @@ void PrintAST::printExtension(ExtensionDecl *decl) {
1823
1847
1824
1848
void PrintAST::visitExtensionDecl (ExtensionDecl *decl) {
1825
1849
if (Options.TransformContext &&
1826
- Options.TransformContext ->isPrintingSynthesizedExtension ())
1827
- printSynthesizedExtension (Options.TransformContext ->getNominal (), decl);
1828
- else
1850
+ Options.TransformContext ->isPrintingSynthesizedExtension ()) {
1851
+ auto extendedType =
1852
+ Options.TransformContext ->getBaseType ()->mapTypeOutOfContext ();
1853
+ printSynthesizedExtension (extendedType, decl);
1854
+ } else
1829
1855
printExtension (decl);
1830
1856
}
1831
1857
0 commit comments