Skip to content

Commit 5e157b8

Browse files
authored
Merge pull request swiftlang#19645 from pitiphong-p/objc-rename-attribute-type-following
Improve the behavior of the custom @objc name in the available attribute when generates ObjC headers
2 parents f28e440 + 08204ef commit 5e157b8

File tree

2 files changed

+232
-118
lines changed

2 files changed

+232
-118
lines changed

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 92 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ namespace {
137137
using DelayedMemberSet = llvm::SmallSetVector<const ValueDecl *, 32>;
138138

139139
class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
140-
private TypeVisitor<ObjCPrinter, void,
140+
private TypeVisitor<ObjCPrinter, void,
141141
Optional<OptionalTypeKind>>
142142
{
143143
friend ASTVisitor;
@@ -528,7 +528,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
528528
return sel.getNumArgs() == 0 &&
529529
sel.getSelectorPieces().front().str() == "init";
530530
}
531-
531+
532532
void printAbstractFunctionAsMethod(AbstractFunctionDecl *AFD,
533533
bool isClassMethod,
534534
bool isNSUIntegerSubscript = false) {
@@ -553,7 +553,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
553553
auto resultTy = getForeignResultType(AFD, methodTy, errorConvention);
554554

555555
// Constructors and methods returning DynamicSelf return
556-
// instancetype.
556+
// instancetype.
557557
if (isa<ConstructorDecl>(AFD) ||
558558
(isa<FuncDecl>(AFD) && cast<FuncDecl>(AFD)->hasDynamicSelf())) {
559559
if (errorConvention && errorConvention->stripsResultOptionality()) {
@@ -776,9 +776,8 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
776776
};
777777

778778
/// Returns \c true if anything was printed.
779-
bool printAvailability(
780-
const Decl *D,
781-
PrintLeadingSpace printLeadingSpace = PrintLeadingSpace::Yes) {
779+
bool printAvailability(const Decl *D, PrintLeadingSpace printLeadingSpace =
780+
PrintLeadingSpace::Yes) {
782781
bool hasPrintedAnything = false;
783782
auto maybePrintLeadingSpace = [&] {
784783
if (printLeadingSpace == PrintLeadingSpace::Yes || hasPrintedAnything)
@@ -788,7 +787,8 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
788787

789788
for (auto AvAttr : D->getAttrs().getAttributes<AvailableAttr>()) {
790789
if (AvAttr->Platform == PlatformKind::none) {
791-
if (AvAttr->PlatformAgnostic == PlatformAgnosticAvailabilityKind::Unavailable) {
790+
if (AvAttr->PlatformAgnostic ==
791+
PlatformAgnosticAvailabilityKind::Unavailable) {
792792
// Availability for *
793793
if (!AvAttr->Rename.empty() && isa<ValueDecl>(D)) {
794794
// rename
@@ -833,11 +833,10 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
833833
}
834834

835835
// Availability for a specific platform
836-
if (!AvAttr->Introduced.hasValue()
837-
&& !AvAttr->Deprecated.hasValue()
838-
&& !AvAttr->Obsoleted.hasValue()
839-
&& !AvAttr->isUnconditionallyDeprecated()
840-
&& !AvAttr->isUnconditionallyUnavailable()) {
836+
if (!AvAttr->Introduced.hasValue() && !AvAttr->Deprecated.hasValue() &&
837+
!AvAttr->Obsoleted.hasValue() &&
838+
!AvAttr->isUnconditionallyDeprecated() &&
839+
!AvAttr->isUnconditionallyUnavailable()) {
841840
continue;
842841
}
843842

@@ -912,43 +911,75 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
912911
}
913912
return hasPrintedAnything;
914913
}
915-
916-
void printRenameForDecl(const AvailableAttr *AvAttr, const ValueDecl *D,
917-
bool includeQuotes) {
918-
assert(!AvAttr->Rename.empty());
919-
920-
auto renamedParsedDeclName = parseDeclName(AvAttr->Rename);
921-
auto renamedDeclName = renamedParsedDeclName.formDeclName(D->getASTContext());
922-
914+
915+
const ValueDecl *getRenameDecl(const ValueDecl *D,
916+
const ParsedDeclName renamedParsedDeclName) {
923917
auto declContext = D->getDeclContext();
924-
const ValueDecl *renamedDecl = nullptr;
925-
918+
ASTContext &astContext = D->getASTContext();
919+
auto renamedDeclName = renamedParsedDeclName.formDeclName(astContext);
920+
926921
if (isa<ClassDecl>(D) || isa<ProtocolDecl>(D)) {
922+
if (!renamedParsedDeclName.ContextName.empty()) {
923+
return nullptr;
924+
}
927925
UnqualifiedLookup lookup(renamedDeclName.getBaseIdentifier(),
928-
declContext->getModuleScopeContext(),
929-
nullptr,
926+
declContext->getModuleScopeContext(), nullptr,
930927
SourceLoc(),
931928
UnqualifiedLookup::Flags::TypeLookup);
932-
renamedDecl = lookup.getSingleTypeResult();
929+
return lookup.getSingleTypeResult();
930+
}
931+
932+
TypeDecl *typeDecl = declContext->getSelfNominalTypeDecl();
933+
934+
const ValueDecl *renamedDecl = nullptr;
935+
SmallVector<ValueDecl *, 4> lookupResults;
936+
declContext->lookupQualified(typeDecl->getDeclaredInterfaceType(),
937+
renamedDeclName, NL_QualifiedDefault, nullptr,
938+
lookupResults);
939+
940+
if (lookupResults.size() == 1) {
941+
auto candidate = lookupResults[0];
942+
if (!shouldInclude(candidate))
943+
return nullptr;
944+
if (candidate->getKind() != D->getKind() ||
945+
(candidate->isInstanceMember() !=
946+
cast<ValueDecl>(D)->isInstanceMember()))
947+
return nullptr;
948+
949+
renamedDecl = candidate;
933950
} else {
934-
SmallVector<ValueDecl *, 4> lookupResults;
935-
declContext->lookupQualified(
936-
declContext->getSelfNominalTypeDecl(),
937-
renamedDeclName, NL_QualifiedDefault, lookupResults);
938951
for (auto candidate : lookupResults) {
939952
if (!shouldInclude(candidate))
940953
continue;
941-
954+
942955
if (candidate->getKind() != D->getKind() ||
943956
(candidate->isInstanceMember() !=
944957
cast<ValueDecl>(D)->isInstanceMember()))
945958
continue;
946-
947-
if (isa<FuncDecl>(candidate) &&
948-
(cast<FuncDecl>(candidate)->getParameters()->size() !=
949-
cast<FuncDecl>(D)->getParameters()->size()))
950-
continue;
951-
959+
960+
if (isa<AbstractFunctionDecl>(candidate)) {
961+
auto cParams = cast<AbstractFunctionDecl>(candidate)->getParameters();
962+
auto dParams = cast<AbstractFunctionDecl>(D)->getParameters();
963+
964+
if (cParams->size() != dParams->size())
965+
continue;
966+
967+
bool hasSameParameterTypes = true;
968+
for (auto index : indices(*cParams)) {
969+
auto cParamsType = cParams->get(index)->getType();
970+
auto dParamsType = dParams->get(index)->getType();
971+
if (!cParamsType->matchesParameter(dParamsType,
972+
TypeMatchOptions())) {
973+
hasSameParameterTypes = false;
974+
break;
975+
}
976+
}
977+
978+
if (!hasSameParameterTypes) {
979+
continue;
980+
}
981+
}
982+
952983
if (renamedDecl) {
953984
// If we found a duplicated candidate then we would silently fail.
954985
renamedDecl = nullptr;
@@ -957,11 +988,20 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
957988
renamedDecl = candidate;
958989
}
959990
}
960-
991+
return renamedDecl;
992+
}
993+
994+
void printRenameForDecl(const AvailableAttr *AvAttr, const ValueDecl *D,
995+
bool includeQuotes) {
996+
assert(!AvAttr->Rename.empty());
997+
998+
const ValueDecl *renamedDecl =
999+
getRenameDecl(D, parseDeclName(AvAttr->Rename));
1000+
9611001
if (renamedDecl) {
9621002
SmallString<128> scratch;
963-
auto renamedObjCRuntimeName = renamedDecl->getObjCRuntimeName()
964-
->getString(scratch);
1003+
auto renamedObjCRuntimeName =
1004+
renamedDecl->getObjCRuntimeName()->getString(scratch);
9651005
printEncodedString(renamedObjCRuntimeName, includeQuotes);
9661006
} else {
9671007
printEncodedString(AvAttr->Rename, includeQuotes);
@@ -1476,9 +1516,9 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
14761516
MAP(UnsafeMutableRawPointer, "void *", true);
14771517

14781518
Identifier ID_ObjectiveC = ctx.Id_ObjectiveC;
1479-
specialNames[{ID_ObjectiveC, ctx.getIdentifier("ObjCBool")}]
1519+
specialNames[{ID_ObjectiveC, ctx.getIdentifier("ObjCBool")}]
14801520
= { "BOOL", false};
1481-
specialNames[{ID_ObjectiveC, ctx.getIdentifier("Selector")}]
1521+
specialNames[{ID_ObjectiveC, ctx.getIdentifier("Selector")}]
14821522
= { "SEL", true };
14831523
specialNames[{ID_ObjectiveC,
14841524
ctx.getIdentifier(
@@ -1605,7 +1645,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
16051645
os << clangDecl->getKindName() << " ";
16061646
}
16071647

1608-
void visitStructType(StructType *ST,
1648+
void visitStructType(StructType *ST,
16091649
Optional<OptionalTypeKind> optionalKind) {
16101650
const StructDecl *SD = ST->getStructOrBoundGenericStruct();
16111651

@@ -1829,7 +1869,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
18291869
visitExistentialType(PT, optionalKind, /*isMetatype=*/false);
18301870
}
18311871

1832-
void visitProtocolCompositionType(ProtocolCompositionType *PCT,
1872+
void visitProtocolCompositionType(ProtocolCompositionType *PCT,
18331873
Optional<OptionalTypeKind> optionalKind) {
18341874
visitExistentialType(PCT, optionalKind, /*isMetatype=*/false);
18351875
}
@@ -1840,7 +1880,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
18401880
visitExistentialType(instanceTy, optionalKind, /*isMetatype=*/true);
18411881
}
18421882

1843-
void visitMetatypeType(MetatypeType *MT,
1883+
void visitMetatypeType(MetatypeType *MT,
18441884
Optional<OptionalTypeKind> optionalKind) {
18451885
Type instanceTy = MT->getInstanceType();
18461886
if (auto classTy = instanceTy->getAs<ClassType>()) {
@@ -1875,7 +1915,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
18751915
os << cast<clang::ObjCTypeParamDecl>(decl->getClangDecl())->getName();
18761916
printNullability(optionalKind);
18771917
}
1878-
1918+
18791919
void printFunctionType(FunctionType *FT, char pointerSigil,
18801920
Optional<OptionalTypeKind> optionalKind) {
18811921
visitPart(FT->getResult(), OTK_None);
@@ -1884,7 +1924,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
18841924
openFunctionTypes.push_back(FT);
18851925
}
18861926

1887-
void visitFunctionType(FunctionType *FT,
1927+
void visitFunctionType(FunctionType *FT,
18881928
Optional<OptionalTypeKind> optionalKind) {
18891929
switch (FT->getRepresentation()) {
18901930
case AnyFunctionType::Representation::Thin:
@@ -1928,18 +1968,18 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
19281968
visitPart(PT->getSinglyDesugaredType(), optionalKind);
19291969
}
19301970

1931-
void visitSyntaxSugarType(SyntaxSugarType *SST,
1971+
void visitSyntaxSugarType(SyntaxSugarType *SST,
19321972
Optional<OptionalTypeKind> optionalKind) {
19331973
visitPart(SST->getSinglyDesugaredType(), optionalKind);
19341974
}
19351975

1936-
void visitDynamicSelfType(DynamicSelfType *DST,
1976+
void visitDynamicSelfType(DynamicSelfType *DST,
19371977
Optional<OptionalTypeKind> optionalKind) {
19381978
printNullability(optionalKind, NullabilityPrintKind::ContextSensitive);
19391979
os << "instancetype";
19401980
}
19411981

1942-
void visitReferenceStorageType(ReferenceStorageType *RST,
1982+
void visitReferenceStorageType(ReferenceStorageType *RST,
19431983
Optional<OptionalTypeKind> optionalKind) {
19441984
visitPart(RST->getReferentType(), optionalKind);
19451985
}
@@ -1977,7 +2017,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
19772017
/// finishFunctionType()). If only a part of a type is being printed, use
19782018
/// visitPart().
19792019
public:
1980-
void print(Type ty, Optional<OptionalTypeKind> optionalKind,
2020+
void print(Type ty, Optional<OptionalTypeKind> optionalKind,
19812021
Identifier name = Identifier(),
19822022
IsFunctionParam_t isFuncParam = IsNotFunctionParam) {
19832023
PrettyStackTraceType trace(M.getASTContext(), "printing", ty);
@@ -2829,9 +2869,9 @@ class ModuleWriter {
28292869
// FIXME: This will end up taking linear time.
28302870
auto lhsMembers = cast<ExtensionDecl>(*lhs)->getMembers();
28312871
auto rhsMembers = cast<ExtensionDecl>(*rhs)->getMembers();
2832-
unsigned numLHSMembers = std::distance(lhsMembers.begin(),
2872+
unsigned numLHSMembers = std::distance(lhsMembers.begin(),
28332873
lhsMembers.end());
2834-
unsigned numRHSMembers = std::distance(rhsMembers.begin(),
2874+
unsigned numRHSMembers = std::distance(rhsMembers.begin(),
28352875
rhsMembers.end());
28362876
if (numLHSMembers != numRHSMembers)
28372877
return numLHSMembers < numRHSMembers ? Descending : Ascending;

0 commit comments

Comments
 (0)