Skip to content

Commit df18ae5

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 2aeb78b commit df18ae5

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
@@ -2722,6 +2722,10 @@ class ValueDecl : public Decl {
27222722
return DeclNameRef(Name);
27232723
}
27242724

2725+
/// Retrieve the C declaration name that names this function, or empty
2726+
/// string if it has none.
2727+
StringRef getCDeclName() const;
2728+
27252729
/// Retrieve the name to use for this declaration when interoperating
27262730
/// with the Objective-C runtime.
27272731
///

lib/AST/Decl.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3823,6 +3823,21 @@ void ValueDecl::setInterfaceType(Type type) {
38233823
std::move(type));
38243824
}
38253825

3826+
StringRef ValueDecl::getCDeclName() const {
3827+
// Treat imported C functions as implicitly @_cdecl.
3828+
if (auto clangDecl = dyn_cast_or_null<clang::FunctionDecl>(getClangDecl())) {
3829+
if (clangDecl->getLanguageLinkage() == clang::CLanguageLinkage
3830+
&& clangDecl->getIdentifier())
3831+
return clangDecl->getName();
3832+
}
3833+
3834+
// Handle explicit cdecl attributes.
3835+
if (auto *cdecl = getAttrs().getAttribute<CDeclAttr>())
3836+
return cdecl->Name;
3837+
3838+
return "";
3839+
}
3840+
38263841
llvm::Optional<ObjCSelector>
38273842
ValueDecl::getObjCRuntimeName(bool skipIsObjCResolution) const {
38283843
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
@@ -2066,7 +2066,7 @@ void AttributeChecker::visitExposeAttr(ExposeAttr *attr) {
20662066
case ExposureKind::Cxx: {
20672067
auto *VD = cast<ValueDecl>(D);
20682068
// Expose cannot be mixed with '@_cdecl' declarations.
2069-
if (VD->getAttrs().hasAttribute<CDeclAttr>())
2069+
if (!VD->getCDeclName().empty())
20702070
diagnose(attr->getLocation(), diag::expose_only_non_other_attr, "@_cdecl");
20712071

20722072
// Nested exposed declarations are expected to be inside

0 commit comments

Comments
 (0)