Skip to content

Commit c4771a2

Browse files
authored
Merge pull request #5876 from slavapestov/clean-up-ide-member-type-hack
Clean up IDE member type hack
2 parents a82990c + 1df6e51 commit c4771a2

File tree

10 files changed

+86
-145
lines changed

10 files changed

+86
-145
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct TypeTransformContext {
4343
explicit TypeTransformContext(Type T);
4444
explicit TypeTransformContext(NominalTypeDecl* NTD);
4545

46-
Type getTypeBase() const;
46+
Type getBaseType() const;
4747
NominalTypeDecl *getNominal() const;
4848

4949
bool isPrintingSynthesizedExtension() const;
@@ -186,10 +186,6 @@ struct PrintOptions {
186186
/// ([] and ?), even if there are no sugar type nodes.
187187
bool SynthesizeSugarOnTypes = false;
188188

189-
/// \brief Print a dynamic Self type as its underlying type, rather than
190-
/// the keyword `Self`.
191-
bool StripDynamicSelf = false;
192-
193189
/// \brief If true, the printer will explode a pattern like this:
194190
/// \code
195191
/// var (a, b) = f()
@@ -344,6 +340,8 @@ struct PrintOptions {
344340
/// \brief The information for converting archetypes to specialized types.
345341
llvm::Optional<TypeTransformContext> TransformContext;
346342

343+
bool PrintAsMember = false;
344+
347345
/// \brief If this is not \c nullptr then functions (including accessors and
348346
/// constructors) will be printed with a body that is determined by this
349347
/// function.
@@ -407,15 +405,11 @@ struct PrintOptions {
407405

408406
static PrintOptions printTypeInterface(Type T);
409407

410-
void setArchetypeSelfTransform(Type T);
411-
412-
void setArchetypeSelfTransformForQuickHelp(Type T);
413-
414-
void setArchetypeAndDynamicSelfTransform(Type T);
408+
void setBaseType(Type T);
415409

416-
void initArchetypeTransformerForSynthesizedExtensions(NominalTypeDecl *D);
410+
void initForSynthesizedExtension(NominalTypeDecl *D);
417411

418-
void clearArchetypeTransformerForSynthesizedExtensions();
412+
void clearSynthesizedExtension();
419413

420414
/// Retrieve the print options that are suitable to print the testable interface.
421415
static PrintOptions printTestableInterface() {

include/swift/AST/Types.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -810,12 +810,6 @@ class alignas(1 << TypeAlignInBits) TypeBase {
810810
/// Otherwise, it returns the type itself.
811811
Type getReferenceStorageReferent();
812812

813-
/// This is to check the pre-condition of calling
814-
/// getMemberSubstitutions(const DeclContext *dc)
815-
/// The function checks whether the given context can be treated as a part
816-
/// of the type.
817-
bool canTreatContextAsMember(const DeclContext *dc);
818-
819813
/// Determine the set of substitutions that should be applied to a
820814
/// type spelled within the given DeclContext to treat it as a
821815
/// member of this type.

lib/AST/ASTPrinter.cpp

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -443,25 +443,15 @@ PrintOptions PrintOptions::printTypeInterface(Type T) {
443443
return result;
444444
}
445445

446-
void PrintOptions::setArchetypeSelfTransform(Type T) {
446+
void PrintOptions::setBaseType(Type T) {
447447
TransformContext = TypeTransformContext(T);
448448
}
449449

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) {
461451
TransformContext = TypeTransformContext(D);
462452
}
463453

464-
void PrintOptions::clearArchetypeTransformerForSynthesizedExtensions() {
454+
void PrintOptions::clearSynthesizedExtension() {
465455
TransformContext.reset();
466456
}
467457

@@ -475,7 +465,7 @@ NominalTypeDecl *TypeTransformContext::getNominal() const {
475465
return Nominal;
476466
}
477467

478-
Type TypeTransformContext::getTypeBase() const {
468+
Type TypeTransformContext::getBaseType() const {
479469
return Type(BaseType);
480470
}
481471

@@ -658,11 +648,10 @@ void StreamPrinter::printText(StringRef Text) {
658648

659649
/// Whether we will be printing a TypeLoc by using the TypeRepr printer
660650
static bool willUseTypeReprPrinting(TypeLoc tyLoc,
661-
const PrintOptions &options) {
651+
Type currentType,
652+
PrintOptions options) {
662653
// Special case for when transforming archetypes
663-
if (options.TransformContext &&
664-
options.TransformContext->getTypeBase() &&
665-
tyLoc.getType())
654+
if (currentType && tyLoc.getType())
666655
return false;
667656

668657
return ((options.PreferTypeRepr && tyLoc.hasLocation()) ||
@@ -676,6 +665,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
676665
PrintOptions Options;
677666
unsigned IndentLevel = 0;
678667
Decl *Current = nullptr;
668+
Type CurrentType;
679669

680670
friend DeclVisitor<PrintAST>;
681671

@@ -854,7 +844,6 @@ class PrintAST : public ASTVisitor<PrintAST> {
854844
PrintOptions FreshOptions;
855845
FreshOptions.ExcludeAttrList = Options.ExcludeAttrList;
856846
FreshOptions.ExclusiveAttrList = Options.ExclusiveAttrList;
857-
FreshOptions.StripDynamicSelf = Options.StripDynamicSelf;
858847
T.print(Printer, FreshOptions);
859848
return;
860849
}
@@ -863,46 +852,39 @@ class PrintAST : public ASTVisitor<PrintAST> {
863852
}
864853

865854
void printTransformedType(Type T) {
866-
if (Options.TransformContext &&
867-
Options.TransformContext->getTypeBase()) {
868-
auto *DC = Current->getInnermostDeclContext();
869-
855+
if (CurrentType) {
870856
if (T->hasArchetype()) {
871857
// Get the interface type, since TypeLocs still have
872858
// contextual types in them.
873-
T = ArchetypeBuilder::mapTypeOutOfContext(DC, T);
859+
T = ArchetypeBuilder::mapTypeOutOfContext(
860+
Current->getInnermostDeclContext(), T);
874861
}
875862

876863
// 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);
890874
}
891875

892876
printType(T);
893877
}
894878

895879
void printTypeLoc(const TypeLoc &TL) {
896-
if (Options.TransformContext &&
897-
Options.TransformContext->getTypeBase() &&
898-
TL.getType()) {
880+
if (CurrentType && TL.getType()) {
899881
printTransformedType(TL.getType());
900882
return;
901883
}
902884

903885
// Print a TypeRepr if instructed to do so by options, or if the type
904886
// is null.
905-
if (willUseTypeReprPrinting(TL, Options)) {
887+
if (willUseTypeReprPrinting(TL, CurrentType, Options)) {
906888
if (auto repr = TL.getTypeRepr()) {
907889
llvm::SaveAndRestore<bool> SPTA(Options.SkipParameterTypeAttributes,
908890
true);
@@ -989,7 +971,10 @@ class PrintAST : public ASTVisitor<PrintAST> {
989971

990972
public:
991973
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+
}
993978

994979
using ASTVisitor::visit;
995980

@@ -1001,6 +986,16 @@ class PrintAST : public ASTVisitor<PrintAST> {
1001986
Current = D;
1002987
SWIFT_DEFER { Current = Old; };
1003988

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+
1004999
bool Synthesize =
10051000
Options.TransformContext &&
10061001
Options.TransformContext->isPrintingSynthesizedExtension() &&
@@ -1268,13 +1263,10 @@ void PrintAST::printSingleDepthOfGenericSignature(
12681263
TypeSubstitutionMap subMap;
12691264
ModuleDecl *M = nullptr;
12701265

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()) {
12751268
auto *DC = Current->getInnermostDeclContext()->getInnermostTypeContext();
1276-
subMap = Options.TransformContext->getTypeBase()
1277-
->getMemberSubstitutions(DC);
1269+
subMap = CurrentType->getMemberSubstitutions(DC);
12781270
M = DC->getParentModule();
12791271
}
12801272
}
@@ -2424,7 +2416,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
24242416
// Special case, if we're not going to use the type repr printing, peek
24252417
// through the paren types so that we don't print excessive @escapings.
24262418
unsigned numParens = 0;
2427-
if (!willUseTypeReprPrinting(TheTypeLoc, Options)) {
2419+
if (!willUseTypeReprPrinting(TheTypeLoc, CurrentType, Options)) {
24282420
while (auto parenTy =
24292421
dyn_cast<ParenType>(TheTypeLoc.getType().getPointer())) {
24302422
++numParens;
@@ -3640,11 +3632,6 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
36403632
}
36413633

36423634
void visitDynamicSelfType(DynamicSelfType *T) {
3643-
if (Options.StripDynamicSelf) {
3644-
visit(T->getSelfType());
3645-
return;
3646-
}
3647-
36483635
if (Options.PrintInSILBody) {
36493636
Printer << "@dynamic_self ";
36503637
visit(T->getSelfType());
@@ -3656,9 +3643,11 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
36563643
auto staticSelfT = T->getSelfType();
36573644

36583645
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+
}
36623651
}
36633652

36643653
visit(staticSelfT);

lib/AST/Type.cpp

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2934,8 +2934,9 @@ Type Type::subst(const SubstitutionMap &substitutions,
29342934
return substType(*this, &substitutions, substitutions.getMap(), options);
29352935
}
29362936

2937-
static Type getSuperClassForDeclInternal(Type t, const ClassDecl *baseClass,
2938-
LazyResolver *resolver) {
2937+
Type TypeBase::getSuperclassForDecl(const ClassDecl *baseClass,
2938+
LazyResolver *resolver) {
2939+
Type t(this);
29392940
while (t) {
29402941
auto *derivedClass = t->getClassOrBoundGenericClass();
29412942
assert(derivedClass && "expected a class here");
@@ -2945,51 +2946,9 @@ static Type getSuperClassForDeclInternal(Type t, const ClassDecl *baseClass,
29452946

29462947
t = t->getSuperclass(resolver);
29472948
}
2948-
return t;
2949-
}
2950-
2951-
Type TypeBase::getSuperclassForDecl(const ClassDecl *baseClass,
2952-
LazyResolver *resolver) {
2953-
if (auto result = getSuperClassForDeclInternal(Type(this), baseClass,
2954-
resolver)) {
2955-
return result;
2956-
}
29572949
llvm_unreachable("no inheritance relationship between given classes");
29582950
}
29592951

2960-
bool TypeBase::canTreatContextAsMember(const DeclContext *dc) {
2961-
Type baseTy(getRValueType());
2962-
2963-
if (auto metaBase = baseTy->getAs<AnyMetatypeType>()) {
2964-
baseTy = metaBase->getInstanceType()->getRValueType();
2965-
}
2966-
2967-
if (!baseTy->getAnyNominal())
2968-
return false;
2969-
2970-
// If the context is a protocol or its extensions, the base type should conform
2971-
// to that protocol.
2972-
if (auto Prot = dc->getAsProtocolOrProtocolExtensionContext()) {
2973-
for (auto Conf : baseTy->getAnyNominal()->getAllConformances()) {
2974-
if (Conf->getProtocol() == Prot)
2975-
return true;
2976-
}
2977-
}
2978-
2979-
// If the context is a nominal type context or one of its extensions, the base
2980-
// type should be either exactly that nominal type of sub-class of that type.
2981-
if (auto ownerNominal = dc->getAsNominalTypeOrNominalTypeExtensionContext()) {
2982-
LazyResolver *resolver = dc->getASTContext().getLazyResolver();
2983-
if (auto *ownerClass = dyn_cast<ClassDecl>(ownerNominal))
2984-
baseTy = getSuperClassForDeclInternal(Type(this), ownerClass, resolver);
2985-
if (baseTy && baseTy->getAnyNominal() == ownerNominal)
2986-
return true;
2987-
}
2988-
2989-
// The context cannot be treated as part of the type.
2990-
return false;
2991-
}
2992-
29932952
TypeSubstitutionMap TypeBase::getMemberSubstitutions(const DeclContext *dc) {
29942953

29952954
// Ignore lvalues in the base type.

lib/IDE/CodeCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4103,7 +4103,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
41034103
DeclNameOffsetLocatorPrinter Printer(OS);
41044104
PrintOptions Options;
41054105
if (auto transformType = CurrDeclContext->getDeclaredTypeInContext())
4106-
Options.setArchetypeSelfTransform(transformType);
4106+
Options.setBaseType(transformType);
41074107
Options.PrintDefaultParameterPlaceholder = false;
41084108
Options.PrintImplicitAttrs = false;
41094109
Options.ExclusiveAttrList.push_back(TAK_escaping);

lib/IDE/ModuleInterfacePrinting.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -573,12 +573,10 @@ void swift::ide::printSubmoduleInterface(
573573
AdjustedOptions.BracketOptions = {ET.first, false,
574574
Decls.back().first == ET.first, true};
575575
if (ET.second)
576-
AdjustedOptions.
577-
initArchetypeTransformerForSynthesizedExtensions(NTD);
576+
AdjustedOptions.initForSynthesizedExtension(NTD);
578577
ET.first->print(Printer, AdjustedOptions);
579578
if (ET.second)
580-
AdjustedOptions.
581-
clearArchetypeTransformerForSynthesizedExtensions();
579+
AdjustedOptions.clearSynthesizedExtension();
582580
if (AdjustedOptions.BracketOptions.shouldCloseExtension(ET.first))
583581
Printer << "\n";
584582
}
@@ -605,12 +603,10 @@ void swift::ide::printSubmoduleInterface(
605603
if (AdjustedOptions.BracketOptions.shouldOpenExtension(ET.first))
606604
Printer << "\n";
607605
if (ET.second)
608-
AdjustedOptions.
609-
initArchetypeTransformerForSynthesizedExtensions(NTD);
606+
AdjustedOptions.initForSynthesizedExtension(NTD);
610607
ET.first->print(Printer, AdjustedOptions);
611608
if (ET.second)
612-
AdjustedOptions.
613-
clearArchetypeTransformerForSynthesizedExtensions();
609+
AdjustedOptions.clearSynthesizedExtension();
614610
if (AdjustedOptions.BracketOptions.shouldCloseExtension(ET.first))
615611
Printer << "\n";
616612
}

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,11 +2384,7 @@ static void diagnoseNoWitness(ValueDecl *Requirement, Type RequirementType,
23842384
Options.AccessibilityFilter = Accessibility::Private;
23852385
Options.PrintAccessibility = false;
23862386
Options.FunctionBody = [](const ValueDecl *VD) { return "<#code#>"; };
2387-
Type SelfType = Adopter->getSelfTypeInContext();
2388-
if (Adopter->getAsClassOrClassExtensionContext())
2389-
Options.setArchetypeSelfTransform(SelfType);
2390-
else
2391-
Options.setArchetypeAndDynamicSelfTransform(SelfType);
2387+
Options.setBaseType(Adopter->getSelfTypeInContext());
23922388
Options.CurrentModule = Adopter->getParentModule();
23932389
if (!Adopter->isExtensionContext()) {
23942390
// Create a variable declaration instead of a computed property in

0 commit comments

Comments
 (0)