Skip to content

Commit c3225b0

Browse files
committedMay 1, 2024
[NFC] Merge two category name accessors
1 parent 0bbe26d commit c3225b0

File tree

6 files changed

+28
-36
lines changed

6 files changed

+28
-36
lines changed
 

‎include/swift/AST/Decl.h

-5
Original file line numberDiff line numberDiff line change
@@ -1912,11 +1912,6 @@ class ExtensionDecl final : public GenericContext, public Decl,
19121912
/// \endcode
19131913
bool isWrittenWithConstraints() const;
19141914

1915-
/// Returns the name of the category specified by the \c \@_objcImplementation
1916-
/// attribute, or \c None if the name is invalid or
1917-
/// \c isObjCImplementation() is false.
1918-
std::optional<Identifier> getCategoryNameForObjCImplementation() const;
1919-
19201915
/// If this extension represents an imported Objective-C category, returns the
19211916
/// category's name. Otherwise returns the empty identifier.
19221917
Identifier getObjCCategoryName() const;

‎lib/AST/Decl.cpp

-10
Original file line numberDiff line numberDiff line change
@@ -1891,16 +1891,6 @@ bool Decl::isObjCImplementation() const {
18911891
return getAttrs().hasAttribute<ObjCImplementationAttr>(/*AllowInvalid=*/true);
18921892
}
18931893

1894-
std::optional<Identifier>
1895-
ExtensionDecl::getCategoryNameForObjCImplementation() const {
1896-
auto attr = getAttrs()
1897-
.getAttribute<ObjCImplementationAttr>(/*AllowInvalid=*/true);
1898-
if (!attr || attr->isCategoryNameInvalid())
1899-
return std::nullopt;
1900-
1901-
return attr->CategoryName;
1902-
}
1903-
19041894
PatternBindingDecl::PatternBindingDecl(SourceLoc StaticLoc,
19051895
StaticSpellingKind StaticSpelling,
19061896
SourceLoc VarLoc,

‎lib/ClangImporter/ClangImporter.cpp

+21-13
Original file line numberDiff line numberDiff line change
@@ -5971,9 +5971,15 @@ struct OrderDecls {
59715971

59725972
Identifier ExtensionDecl::getObjCCategoryName() const {
59735973
// Could it be an imported category?
5974-
if (!hasClangNode())
5975-
// Nope, not imported.
5974+
if (!hasClangNode()) {
5975+
// Nope, not imported. Is it an @implementation extension?
5976+
auto attr = getAttrs()
5977+
.getAttribute<ObjCImplementationAttr>(/*AllowInvalid=*/true);
5978+
if (attr && !attr->isCategoryNameInvalid())
5979+
return attr->CategoryName;
5980+
59765981
return Identifier();
5982+
}
59775983

59785984
auto category = dyn_cast<clang::ObjCCategoryDecl>(getClangDecl());
59795985
if (!category)
@@ -6014,6 +6020,12 @@ constructResult(const llvm::TinyPtrVector<Decl *> &interfaces,
60146020
return ObjCInterfaceAndImplementation(interfaces, impls.front());
60156021
}
60166022

6023+
static bool isCategoryNameValid(ExtensionDecl *ext) {
6024+
auto attr = ext->getAttrs()
6025+
.getAttribute<ObjCImplementationAttr>(/*AllowInvalid=*/true);
6026+
return attr && !attr->isCategoryNameInvalid();
6027+
}
6028+
60176029
static ObjCInterfaceAndImplementation
60186030
findContextInterfaceAndImplementation(DeclContext *dc) {
60196031
if (!dc)
@@ -6028,16 +6040,11 @@ findContextInterfaceAndImplementation(DeclContext *dc) {
60286040
Identifier categoryName;
60296041

60306042
if (auto ext = dyn_cast<ExtensionDecl>(dc)) {
6031-
if (ext->hasClangNode()) {
6032-
// This is either an interface, or it's not objcImpl at all.
6033-
categoryName = ext->getObjCCategoryName();
6034-
} else {
6035-
// This is either the implementation, or it's not objcImpl at all.
6036-
if (auto name = ext->getCategoryNameForObjCImplementation())
6037-
categoryName = *name;
6038-
else
6039-
return {};
6040-
}
6043+
assert(ext);
6044+
if (!ext->hasClangNode() && !isCategoryNameValid(ext))
6045+
return {};
6046+
6047+
categoryName = ext->getObjCCategoryName();
60416048
} else {
60426049
// Must be an imported class. Look for its main implementation.
60436050
assert(isa_and_nonnull<ClassDecl>(dc));
@@ -6051,7 +6058,8 @@ findContextInterfaceAndImplementation(DeclContext *dc) {
60516058
llvm::TinyPtrVector<Decl *> implDecls;
60526059
for (ExtensionDecl *ext : classDecl->getExtensions()) {
60536060
if (ext->isObjCImplementation()
6054-
&& ext->getCategoryNameForObjCImplementation() == categoryName)
6061+
&& ext->getObjCCategoryName() == categoryName
6062+
&& isCategoryNameValid(ext))
60556063
implDecls.push_back(ext);
60566064
}
60576065

‎lib/IRGen/GenClass.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1141,9 +1141,10 @@ namespace {
11411141
std::optional<StringRef> getObjCImplCategoryName() const {
11421142
if (!TheExtension || !TheExtension->isObjCImplementation())
11431143
return std::nullopt;
1144-
if (auto ident = TheExtension->getCategoryNameForObjCImplementation()) {
1145-
assert(!ident->empty());
1146-
return ident->str();
1144+
assert(!TheExtension->hasClangNode());
1145+
auto ident = TheExtension->getObjCCategoryName();
1146+
if (!ident.empty()) {
1147+
return ident.str();
11471148
}
11481149
return std::nullopt;
11491150
}

‎lib/IRGen/GenDecl.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -5860,7 +5860,7 @@ void IRGenModule::emitNestedTypeDecls(DeclRange members) {
58605860

58615861
static bool shouldEmitCategory(IRGenModule &IGM, ExtensionDecl *ext) {
58625862
if (ext->isObjCImplementation()) {
5863-
assert(ext->getCategoryNameForObjCImplementation() != Identifier());
5863+
assert(!ext->getObjCCategoryName().empty());
58645864
return true;
58655865
}
58665866

@@ -5904,8 +5904,7 @@ void IRGenModule::emitExtension(ExtensionDecl *ext) {
59045904
if (!origClass)
59055905
return;
59065906

5907-
if (ext->isObjCImplementation()
5908-
&& ext->getCategoryNameForObjCImplementation() == Identifier()) {
5907+
if (ext->isObjCImplementation() && ext->getObjCCategoryName().empty()) {
59095908
// This is the @_objcImplementation for the class--generate its class
59105909
// metadata.
59115910
emitClassDecl(origClass);

‎lib/Sema/TypeCheckStorage.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -3510,8 +3510,7 @@ static void finishStorageImplInfo(AbstractStorageDecl *storage,
35103510

35113511
// @_objcImplementation extensions on a non-category can declare stored
35123512
// properties; StoredPropertiesRequest knows to look for them there.
3513-
if (ext->isObjCImplementation() &&
3514-
ext->getCategoryNameForObjCImplementation() == Identifier())
3513+
if (ext->isObjCImplementation() && ext->getObjCCategoryName().empty())
35153514
return;
35163515

35173516
storage->diagnose(diag::extension_stored_property);

0 commit comments

Comments
 (0)