Skip to content

Commit 775bdac

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 b4d545f commit 775bdac

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
@@ -4445,6 +4445,14 @@ ValueDecl::getObjCRuntimeName(bool skipIsObjCResolution) const {
44454445
return std::nullopt;
44464446
}
44474447

4448+
std::optional<ObjCSelector>
4449+
Decl::getExplicitObjCName(bool allowInvalid) const {
4450+
auto objcAttr = getAttrs().getAttribute<ObjCAttr>(allowInvalid);
4451+
if (objcAttr && !objcAttr->isNameImplicit())
4452+
return objcAttr->getName();
4453+
return std::nullopt;
4454+
}
4455+
44484456
bool ValueDecl::canInferObjCFromRequirement(ValueDecl *requirement) {
44494457
// Only makes sense for a requirement of an @objc protocol.
44504458
auto proto = cast<ProtocolDecl>(requirement->getDeclContext());
@@ -4457,9 +4465,8 @@ bool ValueDecl::canInferObjCFromRequirement(ValueDecl *requirement) {
44574465

44584466
// If there is already an @objc attribute with an explicit name, we
44594467
// can't infer a name (it's already there).
4460-
if (auto objcAttr = getAttrs().getAttribute<ObjCAttr>()) {
4461-
if (objcAttr->hasName() && !objcAttr->isNameImplicit())
4462-
return false;
4468+
if (getExplicitObjCName().has_value()) {
4469+
return false;
44634470
}
44644471

44654472
// 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
@@ -6279,11 +6279,7 @@ static bool hasExplicitObjCName(ClassDecl *classDecl) {
62796279
if (classDecl->getAttrs().hasAttribute<ObjCRuntimeNameAttr>())
62806280
return true;
62816281

6282-
auto objcAttr = classDecl->getAttrs().getAttribute<ObjCAttr>();
6283-
if (!objcAttr)
6284-
return false;
6285-
6286-
return objcAttr->hasName() && !objcAttr->isNameImplicit();
6282+
return classDecl->getExplicitObjCName().has_value();
62876283
}
62886284

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