Skip to content

Commit 7e78cb9

Browse files
committed
[NFC] Add accessor for @_cdecl name
It also reads the C name out of imported C functions, which will be convenient for @cdecl @implementation.
1 parent 3b88a50 commit 7e78cb9

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,6 +2778,10 @@ class ValueDecl : public Decl {
27782778
return DeclNameRef(Name);
27792779
}
27802780

2781+
/// Retrieve the C declaration name that names this function, or empty
2782+
/// string if it has none.
2783+
StringRef getCDeclName() const;
2784+
27812785
/// Retrieve the name to use for this declaration when interoperating
27822786
/// with the Objective-C runtime.
27832787
///

lib/AST/Decl.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3869,6 +3869,21 @@ void ValueDecl::setInterfaceType(Type type) {
38693869
std::move(type));
38703870
}
38713871

3872+
StringRef ValueDecl::getCDeclName() const {
3873+
// Treat imported C functions as implicitly @_cdecl.
3874+
if (auto clangDecl = dyn_cast_or_null<clang::FunctionDecl>(getClangDecl())) {
3875+
if (clangDecl->getLanguageLinkage() == clang::CLanguageLinkage
3876+
&& clangDecl->getIdentifier())
3877+
return clangDecl->getName();
3878+
}
3879+
3880+
// Handle explicit cdecl attributes.
3881+
if (auto *cdecl = getAttrs().getAttribute<CDeclAttr>())
3882+
return cdecl->Name;
3883+
3884+
return "";
3885+
}
3886+
38723887
llvm::Optional<ObjCSelector>
38733888
ValueDecl::getObjCRuntimeName(bool skipIsObjCResolution) const {
38743889
if (auto func = dyn_cast<AbstractFunctionDecl>(this))

lib/AST/SwiftNameTranslation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ getObjCNameForSwiftDecl(const ValueDecl *VD, DeclName PreferredName){
139139
bool swift::objc_translation::
140140
isVisibleToObjC(const ValueDecl *VD, AccessLevel minRequiredAccess,
141141
bool checkParent) {
142-
if (!(VD->isObjC() || VD->getAttrs().hasAttribute<CDeclAttr>()))
142+
if (!(VD->isObjC() || !VD->getCDeclName().empty()))
143143
return false;
144144
if (VD->getFormalAccess() >= minRequiredAccess) {
145145
return true;

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,8 +1414,7 @@ class DeclAndTypePrinter::Implementation
14141414

14151415
assert(FD->getAttrs().hasAttribute<CDeclAttr>() && "not a cdecl function");
14161416
os << "SWIFT_EXTERN ";
1417-
printFunctionDeclAsCFunctionDecl(
1418-
FD, FD->getAttrs().getAttribute<CDeclAttr>()->Name, resultTy);
1417+
printFunctionDeclAsCFunctionDecl(FD, FD->getCDeclName(), resultTy);
14191418
printFunctionClangAttributes(FD, funcTy);
14201419
printAvailability(FD);
14211420
os << ";\n";
@@ -1425,12 +1424,12 @@ class DeclAndTypePrinter::Implementation
14251424
FunctionSwiftABIInformation(AbstractFunctionDecl *FD,
14261425
LoweredFunctionSignature signature)
14271426
: signature(signature) {
1428-
isCDecl = FD->getAttrs().hasAttribute<CDeclAttr>();
1427+
isCDecl = !FD->getCDeclName().empty();
14291428
if (!isCDecl) {
14301429
auto mangledName = SILDeclRef(FD).mangle();
14311430
symbolName = FD->getASTContext().AllocateCopy(mangledName);
14321431
} else {
1433-
symbolName = FD->getAttrs().getAttribute<CDeclAttr>()->Name;
1432+
symbolName = FD->getCDeclName();
14341433
}
14351434
}
14361435

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2118,7 +2118,7 @@ void AttributeChecker::visitExposeAttr(ExposeAttr *attr) {
21182118
case ExposureKind::Cxx: {
21192119
auto *VD = cast<ValueDecl>(D);
21202120
// Expose cannot be mixed with '@_cdecl' declarations.
2121-
if (VD->getAttrs().hasAttribute<CDeclAttr>())
2121+
if (!VD->getCDeclName().empty())
21222122
diagnose(attr->getLocation(), diag::expose_only_non_other_attr, "@_cdecl");
21232123

21242124
// Nested exposed declarations are expected to be inside

0 commit comments

Comments
 (0)