@@ -199,31 +199,62 @@ class ArchetypeSelfTransformer : public PrinterArchetypeTransformer {
199
199
200
200
PrintOptions PrintOptions::printTypeInterface (Type T, const DeclContext *DC) {
201
201
PrintOptions result = printInterface ();
202
- result.setArchetypeTransform (T, DC);
203
- result. TypeToPrint = T. getPointer ( );
202
+ result.TransformContext = std::make_shared<ArchetypeTransformContext>(
203
+ new PrinterArchetypeNameTransformer (T, DC), T );
204
204
return result;
205
205
}
206
206
207
207
void PrintOptions::setArchetypeTransform (Type T, const DeclContext *DC) {
208
- pTransformer = std::make_shared<PrinterArchetypeNameTransformer>(T, DC);
208
+ TransformContext = std::make_shared<ArchetypeTransformContext>(
209
+ new PrinterArchetypeNameTransformer (T, DC));
209
210
}
210
211
211
212
void PrintOptions::setArchetypeTransformForQuickHelp (Type T, DeclContext *DC) {
212
- pTransformer = std::make_shared<ArchetypeSelfTransformer>(T, *DC);
213
+ TransformContext = std::make_shared<ArchetypeTransformContext>(
214
+ new ArchetypeSelfTransformer (T, *DC));
213
215
}
214
216
215
217
void PrintOptions::initArchetypeTransformerForSynthesizedExtensions (NominalTypeDecl *D) {
216
- pTransformer = std::make_shared<ArchetypeSelfTransformer>(D);
217
- SynthesizedTarget = D ;
218
+ TransformContext = std::make_shared<ArchetypeTransformContext>(
219
+ new ArchetypeSelfTransformer (D), D) ;
218
220
}
219
221
220
- bool PrintOptions::isPrintingSynthesizedExtension () {
221
- return pTransformer && SynthesizedTarget ;
222
+ void PrintOptions::clearArchetypeTransformerForSynthesizedExtensions () {
223
+ TransformContext. reset () ;
222
224
}
223
225
224
- void PrintOptions::clearArchetypeTransformerForSynthesizedExtensions () {
225
- pTransformer = nullptr ;
226
- SynthesizedTarget = nullptr ;
226
+ ArchetypeTransformContext::ArchetypeTransformContext (
227
+ PrinterArchetypeTransformer *Transformer): Transformer(Transformer){};
228
+
229
+ ArchetypeTransformContext::ArchetypeTransformContext (
230
+ PrinterArchetypeTransformer *Transformer, Type T):
231
+ Transformer(Transformer), TypeBaseOrNominal(T.getPointer()) {};
232
+
233
+ ArchetypeTransformContext::ArchetypeTransformContext (
234
+ PrinterArchetypeTransformer *Transformer, NominalTypeDecl *NTD) :
235
+ Transformer(Transformer), TypeBaseOrNominal(NTD){};
236
+
237
+ NominalTypeDecl *ArchetypeTransformContext::getNominal () {
238
+ return TypeBaseOrNominal.get <NominalTypeDecl*>();
239
+ }
240
+
241
+ Type ArchetypeTransformContext::getTypeBase () {
242
+ return TypeBaseOrNominal.get <TypeBase*>();
243
+ }
244
+
245
+ bool ArchetypeTransformContext::isPrintingSynthesizedExtension () {
246
+ return !TypeBaseOrNominal.isNull () && TypeBaseOrNominal.is <NominalTypeDecl*>();
247
+ }
248
+ bool ArchetypeTransformContext::isPrintingTypeInteface () {
249
+ return !TypeBaseOrNominal.isNull () && TypeBaseOrNominal.is <TypeBase*>();
250
+ }
251
+
252
+ Type ArchetypeTransformContext::transform (Type Input) {
253
+ return Transformer->transform (Input);
254
+ }
255
+
256
+ StringRef ArchetypeTransformContext::transform (StringRef Input) {
257
+ return Transformer->transform (Input);
227
258
}
228
259
229
260
std::string ASTPrinter::sanitizeUtf8 (StringRef Text) {
@@ -557,8 +588,8 @@ class PrintAST : public ASTVisitor<PrintAST> {
557
588
}
558
589
559
590
void printTypeLoc (const TypeLoc &TL) {
560
- if (Options.pTransformer && TL.getType ()) {
561
- if (auto RT = Options.pTransformer ->transform (TL.getType ())) {
591
+ if (Options.TransformContext && TL.getType ()) {
592
+ if (auto RT = Options.TransformContext ->transform (TL.getType ())) {
562
593
PrintOptions FreshOptions;
563
594
RT.print (Printer, FreshOptions);
564
595
return ;
@@ -643,16 +674,17 @@ class PrintAST : public ASTVisitor<PrintAST> {
643
674
if (!shouldPrint (D, true ))
644
675
return false ;
645
676
646
- bool Synthesize = Options.isPrintingSynthesizedExtension () &&
677
+ bool Synthesize = Options.TransformContext &&
678
+ Options.TransformContext ->isPrintingSynthesizedExtension () &&
647
679
D->getKind () == DeclKind::Extension;
648
680
if (Synthesize)
649
- Printer.setSynthesizedTarget (Options.SynthesizedTarget );
681
+ Printer.setSynthesizedTarget (Options.TransformContext -> getNominal () );
650
682
Printer.callPrintDeclPre (D);
651
683
ASTVisitor::visit (D);
652
684
if (Synthesize) {
653
685
Printer.setSynthesizedTarget (nullptr );
654
686
Printer.printSynthesizedExtensionPost (cast<ExtensionDecl>(D),
655
- Options.SynthesizedTarget );
687
+ Options.TransformContext -> getNominal () );
656
688
} else {
657
689
Printer.printDeclPost (D);
658
690
}
@@ -798,8 +830,10 @@ void PrintAST::printGenericParams(GenericParamList *Params) {
798
830
Printer << " <" ;
799
831
bool IsFirst = true ;
800
832
SmallVector<Type, 4 > Scrach;
801
- if (Options.TypeToPrint ) {
802
- auto ArgArr = Options.TypeToPrint ->getAllGenericArgs (Scrach);
833
+ if (Options.TransformContext &&
834
+ Options.TransformContext ->isPrintingTypeInteface ()) {
835
+ auto ArgArr = Options.TransformContext ->getTypeBase ()->
836
+ getAllGenericArgs (Scrach);
803
837
for (auto Arg : ArgArr) {
804
838
if (IsFirst) {
805
839
IsFirst = false ;
@@ -841,9 +875,9 @@ void PrintAST::printWhereClause(ArrayRef<RequirementRepr> requirements) {
841
875
auto FirstType = std::get<0 >(Tuple);
842
876
auto SecondType = std::get<1 >(Tuple);
843
877
auto Kind = std::get<2 >(Tuple);
844
- if (Options.pTransformer ) {
845
- FirstType = Options.pTransformer ->transform (FirstType);
846
- SecondType = Options.pTransformer ->transform (SecondType);
878
+ if (Options.TransformContext ) {
879
+ FirstType = Options.TransformContext ->transform (FirstType);
880
+ SecondType = Options.TransformContext ->transform (SecondType);
847
881
}
848
882
if (FirstType == SecondType)
849
883
continue ;
@@ -913,8 +947,8 @@ bool PrintAST::shouldPrintPattern(const Pattern *P) {
913
947
void PrintAST::printPatternType (const Pattern *P) {
914
948
if (P->hasType ()) {
915
949
Type T = P->getType ();
916
- if (Options.pTransformer ) {
917
- T = Options.pTransformer ->transform (T);
950
+ if (Options.TransformContext ) {
951
+ T = Options.TransformContext ->transform (T);
918
952
}
919
953
Printer << " : " ;
920
954
T.print (Printer, Options);
@@ -1493,8 +1527,9 @@ void PrintAST::printExtension(ExtensionDecl* decl) {
1493
1527
}
1494
1528
1495
1529
void PrintAST::visitExtensionDecl (ExtensionDecl *decl) {
1496
- if (Options.SynthesizedTarget && Options.pTransformer )
1497
- printSynthesizedExtension (Options.SynthesizedTarget , decl);
1530
+ if (Options.TransformContext &&
1531
+ Options.TransformContext ->isPrintingSynthesizedExtension ())
1532
+ printSynthesizedExtension (Options.TransformContext ->getNominal (), decl);
1498
1533
else
1499
1534
printExtension (decl);
1500
1535
}
@@ -1758,8 +1793,9 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
1758
1793
});
1759
1794
if (decl->hasType ()) {
1760
1795
Printer << " : " ;
1761
- if (Options.pTransformer )
1762
- Options.pTransformer ->transform (decl->getType ()).print (Printer, Options);
1796
+ if (Options.TransformContext )
1797
+ Options.TransformContext ->transform (decl->getType ()).
1798
+ print (Printer, Options);
1763
1799
else
1764
1800
decl->getType ().print (Printer, Options);
1765
1801
}
@@ -2054,8 +2090,8 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
2054
2090
Type ResultTy = decl->getResultType ();
2055
2091
if (ResultTy && !ResultTy->isEqual (TupleType::getEmpty (Context))) {
2056
2092
Printer << " -> " ;
2057
- if (Options.pTransformer ) {
2058
- ResultTy = Options.pTransformer ->transform (ResultTy);
2093
+ if (Options.TransformContext ) {
2094
+ ResultTy = Options.TransformContext ->transform (ResultTy);
2059
2095
PrintOptions FreshOptions;
2060
2096
ResultTy->print (Printer, FreshOptions);
2061
2097
} else
0 commit comments