Skip to content

Commit 1ec21a9

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 9562b73 commit 1ec21a9

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
@@ -1128,11 +1128,9 @@ getOverriddenSwiftProtocolObjCName(const ValueDecl *decl,
11281128
return std::nullopt;
11291129

11301130
// If there is an 'objc' attribute with a name, use that name.
1131-
if (auto objc = proto->getAttrs().getAttribute<ObjCAttr>()) {
1132-
if (auto name = objc->getName()) {
1133-
llvm::SmallString<4> buffer;
1134-
return std::string(name->getString(buffer));
1135-
}
1131+
if (auto objcName = proto->getExplicitObjCName()) {
1132+
llvm::SmallString<4> buffer;
1133+
return std::string(objcName->getString(buffer));
11361134
}
11371135

11381136
return std::nullopt;

lib/AST/Decl.cpp

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

4332+
std::optional<ObjCSelector>
4333+
Decl::getExplicitObjCName(bool allowInvalid) const {
4334+
auto objcAttr = getAttrs().getAttribute<ObjCAttr>(allowInvalid);
4335+
if (objcAttr && !objcAttr->isNameImplicit())
4336+
return objcAttr->getName();
4337+
return std::nullopt;
4338+
}
4339+
43324340
bool ValueDecl::canInferObjCFromRequirement(ValueDecl *requirement) {
43334341
// Only makes sense for a requirement of an @objc protocol.
43344342
auto proto = cast<ProtocolDecl>(requirement->getDeclContext());
@@ -4341,9 +4349,8 @@ bool ValueDecl::canInferObjCFromRequirement(ValueDecl *requirement) {
43414349

43424350
// If there is already an @objc attribute with an explicit name, we
43434351
// can't infer a name (it's already there).
4344-
if (auto objcAttr = getAttrs().getAttribute<ObjCAttr>()) {
4345-
if (objcAttr->hasName() && !objcAttr->isNameImplicit())
4346-
return false;
4352+
if (getExplicitObjCName().has_value()) {
4353+
return false;
43474354
}
43484355

43494356
// 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
@@ -6166,11 +6166,7 @@ static bool hasExplicitObjCName(ClassDecl *classDecl) {
61666166
if (classDecl->getAttrs().hasAttribute<ObjCRuntimeNameAttr>())
61676167
return true;
61686168

6169-
auto objcAttr = classDecl->getAttrs().getAttribute<ObjCAttr>();
6170-
if (!objcAttr)
6171-
return false;
6172-
6173-
return objcAttr->hasName() && !objcAttr->isNameImplicit();
6169+
return classDecl->getExplicitObjCName().has_value();
61746170
}
61756171

61766172
/// 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)