@@ -443,25 +443,15 @@ PrintOptions PrintOptions::printTypeInterface(Type T) {
443
443
return result;
444
444
}
445
445
446
- void PrintOptions::setArchetypeSelfTransform (Type T) {
446
+ void PrintOptions::setBaseType (Type T) {
447
447
TransformContext = TypeTransformContext (T);
448
448
}
449
449
450
- void PrintOptions::setArchetypeSelfTransformForQuickHelp (Type T) {
451
- TransformContext = TypeTransformContext (T);
452
- }
453
-
454
- void PrintOptions::setArchetypeAndDynamicSelfTransform (Type T) {
455
- TransformContext = TypeTransformContext (T);
456
- StripDynamicSelf = true ;
457
- }
458
-
459
- void PrintOptions::
460
- initArchetypeTransformerForSynthesizedExtensions (NominalTypeDecl *D) {
450
+ void PrintOptions::initForSynthesizedExtension (NominalTypeDecl *D) {
461
451
TransformContext = TypeTransformContext (D);
462
452
}
463
453
464
- void PrintOptions::clearArchetypeTransformerForSynthesizedExtensions () {
454
+ void PrintOptions::clearSynthesizedExtension () {
465
455
TransformContext.reset ();
466
456
}
467
457
@@ -475,7 +465,7 @@ NominalTypeDecl *TypeTransformContext::getNominal() const {
475
465
return Nominal;
476
466
}
477
467
478
- Type TypeTransformContext::getTypeBase () const {
468
+ Type TypeTransformContext::getBaseType () const {
479
469
return Type (BaseType);
480
470
}
481
471
@@ -658,11 +648,10 @@ void StreamPrinter::printText(StringRef Text) {
658
648
659
649
// / Whether we will be printing a TypeLoc by using the TypeRepr printer
660
650
static bool willUseTypeReprPrinting (TypeLoc tyLoc,
661
- const PrintOptions &options) {
651
+ Type currentType,
652
+ PrintOptions options) {
662
653
// Special case for when transforming archetypes
663
- if (options.TransformContext &&
664
- options.TransformContext ->getTypeBase () &&
665
- tyLoc.getType ())
654
+ if (currentType && tyLoc.getType ())
666
655
return false ;
667
656
668
657
return ((options.PreferTypeRepr && tyLoc.hasLocation ()) ||
@@ -676,6 +665,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
676
665
PrintOptions Options;
677
666
unsigned IndentLevel = 0 ;
678
667
Decl *Current = nullptr ;
668
+ Type CurrentType;
679
669
680
670
friend DeclVisitor<PrintAST>;
681
671
@@ -854,7 +844,6 @@ class PrintAST : public ASTVisitor<PrintAST> {
854
844
PrintOptions FreshOptions;
855
845
FreshOptions.ExcludeAttrList = Options.ExcludeAttrList ;
856
846
FreshOptions.ExclusiveAttrList = Options.ExclusiveAttrList ;
857
- FreshOptions.StripDynamicSelf = Options.StripDynamicSelf ;
858
847
T.print (Printer, FreshOptions);
859
848
return ;
860
849
}
@@ -863,46 +852,39 @@ class PrintAST : public ASTVisitor<PrintAST> {
863
852
}
864
853
865
854
void printTransformedType (Type T) {
866
- if (Options.TransformContext &&
867
- Options.TransformContext ->getTypeBase ()) {
868
- auto *DC = Current->getInnermostDeclContext ();
869
-
855
+ if (CurrentType) {
870
856
if (T->hasArchetype ()) {
871
857
// Get the interface type, since TypeLocs still have
872
858
// contextual types in them.
873
- T = ArchetypeBuilder::mapTypeOutOfContext (DC, T);
859
+ T = ArchetypeBuilder::mapTypeOutOfContext (
860
+ Current->getInnermostDeclContext (), T);
874
861
}
875
862
876
863
// Get the innermost nominal type context.
877
- DC = DC->getInnermostTypeContext ();
878
- if (isa<TypeAliasDecl>(DC))
879
- DC = DC->getParent ()->getInnermostTypeContext ();
880
- assert (DC);
881
-
882
-
883
- if (Options.TransformContext ->getTypeBase ()->canTreatContextAsMember (DC)) {
884
- // Get the substitutions from our base type.
885
- auto subMap = Options.TransformContext ->getTypeBase ()
886
- ->getMemberSubstitutions (DC);
887
- auto *M = DC->getParentModule ();
888
- T = T.subst (M, subMap, SubstFlags::DesugarMemberTypes);
889
- }
864
+ DeclContext *DC;
865
+ if (isa<NominalTypeDecl>(Current))
866
+ DC = Current->getInnermostDeclContext ();
867
+ else
868
+ DC = Current->getDeclContext ();
869
+
870
+ // Get the substitutions from our base type.
871
+ auto subMap = CurrentType->getMemberSubstitutions (DC);
872
+ auto *M = DC->getParentModule ();
873
+ T = T.subst (M, subMap, SubstFlags::DesugarMemberTypes);
890
874
}
891
875
892
876
printType (T);
893
877
}
894
878
895
879
void printTypeLoc (const TypeLoc &TL) {
896
- if (Options.TransformContext &&
897
- Options.TransformContext ->getTypeBase () &&
898
- TL.getType ()) {
880
+ if (CurrentType && TL.getType ()) {
899
881
printTransformedType (TL.getType ());
900
882
return ;
901
883
}
902
884
903
885
// Print a TypeRepr if instructed to do so by options, or if the type
904
886
// is null.
905
- if (willUseTypeReprPrinting (TL, Options)) {
887
+ if (willUseTypeReprPrinting (TL, CurrentType, Options)) {
906
888
if (auto repr = TL.getTypeRepr ()) {
907
889
llvm::SaveAndRestore<bool > SPTA (Options.SkipParameterTypeAttributes ,
908
890
true );
@@ -989,7 +971,10 @@ class PrintAST : public ASTVisitor<PrintAST> {
989
971
990
972
public:
991
973
PrintAST (ASTPrinter &Printer, const PrintOptions &Options)
992
- : Printer(Printer), Options(Options) {}
974
+ : Printer(Printer), Options(Options) {
975
+ if (Options.TransformContext )
976
+ CurrentType = Options.TransformContext ->getBaseType ();
977
+ }
993
978
994
979
using ASTVisitor::visit;
995
980
@@ -1001,6 +986,16 @@ class PrintAST : public ASTVisitor<PrintAST> {
1001
986
Current = D;
1002
987
SWIFT_DEFER { Current = Old; };
1003
988
989
+ Type OldType = CurrentType;
990
+ if (CurrentType && (Old != nullptr || Options.PrintAsMember )) {
991
+ if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {
992
+ CurrentType = CurrentType->getTypeOfMember (
993
+ Options.CurrentModule , NTD, nullptr );
994
+ }
995
+ }
996
+
997
+ SWIFT_DEFER { CurrentType = OldType; };
998
+
1004
999
bool Synthesize =
1005
1000
Options.TransformContext &&
1006
1001
Options.TransformContext ->isPrintingSynthesizedExtension () &&
@@ -1268,13 +1263,10 @@ void PrintAST::printSingleDepthOfGenericSignature(
1268
1263
TypeSubstitutionMap subMap;
1269
1264
ModuleDecl *M = nullptr ;
1270
1265
1271
- if (Options.TransformContext &&
1272
- Options.TransformContext ->getTypeBase ()) {
1273
- auto BaseType = Options.TransformContext ->getTypeBase ();
1274
- if (!BaseType->isAnyExistentialType ()) {
1266
+ if (CurrentType) {
1267
+ if (!CurrentType->isAnyExistentialType ()) {
1275
1268
auto *DC = Current->getInnermostDeclContext ()->getInnermostTypeContext ();
1276
- subMap = Options.TransformContext ->getTypeBase ()
1277
- ->getMemberSubstitutions (DC);
1269
+ subMap = CurrentType->getMemberSubstitutions (DC);
1278
1270
M = DC->getParentModule ();
1279
1271
}
1280
1272
}
@@ -2424,7 +2416,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
2424
2416
// Special case, if we're not going to use the type repr printing, peek
2425
2417
// through the paren types so that we don't print excessive @escapings.
2426
2418
unsigned numParens = 0 ;
2427
- if (!willUseTypeReprPrinting (TheTypeLoc, Options)) {
2419
+ if (!willUseTypeReprPrinting (TheTypeLoc, CurrentType, Options)) {
2428
2420
while (auto parenTy =
2429
2421
dyn_cast<ParenType>(TheTypeLoc.getType ().getPointer ())) {
2430
2422
++numParens;
@@ -3640,11 +3632,6 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
3640
3632
}
3641
3633
3642
3634
void visitDynamicSelfType (DynamicSelfType *T) {
3643
- if (Options.StripDynamicSelf ) {
3644
- visit (T->getSelfType ());
3645
- return ;
3646
- }
3647
-
3648
3635
if (Options.PrintInSILBody ) {
3649
3636
Printer << " @dynamic_self " ;
3650
3637
visit (T->getSelfType ());
@@ -3656,9 +3643,11 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
3656
3643
auto staticSelfT = T->getSelfType ();
3657
3644
3658
3645
if (auto *NTD = staticSelfT->getAnyNominal ()) {
3659
- auto Name = T->getASTContext ().Id_Self ;
3660
- Printer.printTypeRef (T, NTD, Name);
3661
- return ;
3646
+ if (isa<ClassDecl>(NTD)) {
3647
+ auto Name = T->getASTContext ().Id_Self ;
3648
+ Printer.printTypeRef (T, NTD, Name);
3649
+ return ;
3650
+ }
3662
3651
}
3663
3652
3664
3653
visit (staticSelfT);
0 commit comments