Skip to content

Commit a58db2e

Browse files
committed
[NFC] Extract Decl::getExplicitObjCName()
Create a helper method which looks for an `@objc` attribute with an explicit name and returns it, and adopt it in various existing places.
1 parent d019b4f commit a58db2e

File tree

5 files changed

+20
-16
lines changed

5 files changed

+20
-16
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
10411041
/// from source code.
10421042
void attachParsedAttrs(DeclAttributes attrs);
10431043

1044+
/// Retrieve the custom name in the \c @objc attribute, if present.
1045+
std::optional<ObjCSelector>
1046+
getExplicitObjCName(bool allowInvalid = false) const;
1047+
10441048
/// True if this declaration provides an implementation for an imported
10451049
/// Objective-C declaration. This implies various restrictions and special
10461050
/// behaviors for it and, if it's an extension, its members.

lib/AST/ASTMangler.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,11 +1193,9 @@ getOverriddenSwiftProtocolObjCName(const ValueDecl *decl,
11931193
return std::nullopt;
11941194

11951195
// If there is an 'objc' attribute with a name, use that name.
1196-
if (auto objc = proto->getAttrs().getAttribute<ObjCAttr>()) {
1197-
if (auto name = objc->getName()) {
1198-
llvm::SmallString<4> buffer;
1199-
return std::string(name->getString(buffer));
1200-
}
1196+
if (auto objcName = proto->getExplicitObjCName()) {
1197+
llvm::SmallString<4> buffer;
1198+
return std::string(objcName->getString(buffer));
12011199
}
12021200

12031201
return std::nullopt;

lib/AST/Decl.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4408,6 +4408,14 @@ ValueDecl::getObjCRuntimeName(bool skipIsObjCResolution) const {
44084408
return std::nullopt;
44094409
}
44104410

4411+
std::optional<ObjCSelector>
4412+
Decl::getExplicitObjCName(bool allowInvalid) const {
4413+
auto objcAttr = getAttrs().getAttribute<ObjCAttr>(allowInvalid);
4414+
if (objcAttr && !objcAttr->isNameImplicit())
4415+
return objcAttr->getName();
4416+
return std::nullopt;
4417+
}
4418+
44114419
bool ValueDecl::canInferObjCFromRequirement(ValueDecl *requirement) {
44124420
// Only makes sense for a requirement of an @objc protocol.
44134421
auto proto = cast<ProtocolDecl>(requirement->getDeclContext());
@@ -4420,9 +4428,8 @@ bool ValueDecl::canInferObjCFromRequirement(ValueDecl *requirement) {
44204428

44214429
// If there is already an @objc attribute with an explicit name, we
44224430
// can't infer a name (it's already there).
4423-
if (auto objcAttr = getAttrs().getAttribute<ObjCAttr>()) {
4424-
if (objcAttr->hasName() && !objcAttr->isNameImplicit())
4425-
return false;
4431+
if (getExplicitObjCName().has_value()) {
4432+
return false;
44264433
}
44274434

44284435
// If the nominal type doesn't conform to the protocol at all, we

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6285,11 +6285,7 @@ static bool hasExplicitObjCName(ClassDecl *classDecl) {
62856285
if (classDecl->getAttrs().hasAttribute<ObjCRuntimeNameAttr>())
62866286
return true;
62876287

6288-
auto objcAttr = classDecl->getAttrs().getAttribute<ObjCAttr>();
6289-
if (!objcAttr)
6290-
return false;
6291-
6292-
return objcAttr->hasName() && !objcAttr->isNameImplicit();
6288+
return classDecl->getExplicitObjCName().has_value();
62936289
}
62946290

62956291
/// Check if the name of a class might be unstable, and if so, emit a

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,9 +1454,8 @@ class SwiftDocumentStructureWalker: public ide::SyntaxModelWalker {
14541454
// We only report runtime name for classes and protocols with an explicitly
14551455
// defined ObjC name, i.e. those that have @objc("SomeName")
14561456
if (D && (isa<ClassDecl>(D) || isa<ProtocolDecl>(D))) {
1457-
auto *ObjCNameAttr = D->getAttrs().getAttribute<ObjCAttr>();
1458-
if (ObjCNameAttr && ObjCNameAttr->hasName())
1459-
return ObjCNameAttr->getName()->getString(Buf);
1457+
if (auto objcName = D->getExplicitObjCName())
1458+
return objcName->getString(Buf);
14601459
}
14611460
return StringRef();
14621461
}

0 commit comments

Comments
 (0)