@@ -1041,13 +1041,44 @@ bool SILDeclRef::isBackDeploymentThunk() const {
1041
1041
}
1042
1042
1043
1043
// / Use the Clang importer to mangle a Clang declaration.
1044
- static void mangleClangDecl (raw_ostream &buffer,
1045
- const clang::NamedDecl *clangDecl,
1046
- ASTContext &ctx) {
1044
+ static void mangleClangDeclViaImporter (raw_ostream &buffer,
1045
+ const clang::NamedDecl *clangDecl,
1046
+ ASTContext &ctx) {
1047
1047
auto *importer = static_cast <ClangImporter *>(ctx.getClangModuleLoader ());
1048
1048
importer->getMangledName (buffer, clangDecl);
1049
1049
}
1050
1050
1051
+ static std::string mangleClangDecl (Decl *decl, bool isForeign) {
1052
+ auto clangDecl = decl->getClangDecl ();
1053
+
1054
+ if (auto namedClangDecl = dyn_cast<clang::DeclaratorDecl>(clangDecl)) {
1055
+ if (auto asmLabel = namedClangDecl->getAttr <clang::AsmLabelAttr>()) {
1056
+ std::string s (1 , ' \01 ' );
1057
+ s += asmLabel->getLabel ();
1058
+ return s;
1059
+ } else if (namedClangDecl->hasAttr <clang::OverloadableAttr>() ||
1060
+ decl->getASTContext ().LangOpts .EnableCXXInterop ) {
1061
+ std::string storage;
1062
+ llvm::raw_string_ostream SS (storage);
1063
+ mangleClangDeclViaImporter (SS, namedClangDecl, decl->getASTContext ());
1064
+ return SS.str ();
1065
+ }
1066
+ return namedClangDecl->getName ().str ();
1067
+ } else if (auto objcDecl = dyn_cast<clang::ObjCMethodDecl>(clangDecl)) {
1068
+ if (objcDecl->isDirectMethod () && isForeign) {
1069
+ std::string storage;
1070
+ llvm::raw_string_ostream SS (storage);
1071
+ clang::ASTContext &ctx = clangDecl->getASTContext ();
1072
+ std::unique_ptr<clang::MangleContext> mangler (ctx.createMangleContext ());
1073
+ mangler->mangleObjCMethodName (objcDecl, SS, /* includePrefixByte=*/ true ,
1074
+ /* includeCategoryNamespace=*/ false );
1075
+ return SS.str ();
1076
+ }
1077
+ }
1078
+
1079
+ return " " ;
1080
+ }
1081
+
1051
1082
std::string SILDeclRef::mangle (ManglingKind MKind) const {
1052
1083
using namespace Mangle ;
1053
1084
ASTMangler mangler;
@@ -1073,32 +1104,11 @@ std::string SILDeclRef::mangle(ManglingKind MKind) const {
1073
1104
// As a special case, Clang functions and globals don't get mangled at all
1074
1105
// - except \c objc_direct decls.
1075
1106
if (hasDecl ()) {
1076
- if (auto clangDecl = getDecl ()->getClangDecl ()) {
1107
+ if (getDecl ()->getClangDecl ()) {
1077
1108
if (!isForeignToNativeThunk () && !isNativeToForeignThunk ()) {
1078
- if (auto namedClangDecl = dyn_cast<clang::DeclaratorDecl>(clangDecl)) {
1079
- if (auto asmLabel = namedClangDecl->getAttr <clang::AsmLabelAttr>()) {
1080
- std::string s (1 , ' \01 ' );
1081
- s += asmLabel->getLabel ();
1082
- return s;
1083
- } else if (namedClangDecl->hasAttr <clang::OverloadableAttr>() ||
1084
- getDecl ()->getASTContext ().LangOpts .EnableCXXInterop ) {
1085
- std::string storage;
1086
- llvm::raw_string_ostream SS (storage);
1087
- mangleClangDecl (SS, namedClangDecl, getDecl ()->getASTContext ());
1088
- return SS.str ();
1089
- }
1090
- return namedClangDecl->getName ().str ();
1091
- } else if (auto objcDecl = dyn_cast<clang::ObjCMethodDecl>(clangDecl)) {
1092
- if (objcDecl->isDirectMethod () && isForeign) {
1093
- std::string storage;
1094
- llvm::raw_string_ostream SS (storage);
1095
- clang::ASTContext &ctx = clangDecl->getASTContext ();
1096
- std::unique_ptr<clang::MangleContext> mangler (ctx.createMangleContext ());
1097
- mangler->mangleObjCMethodName (objcDecl, SS, /* includePrefixByte=*/ true ,
1098
- /* includeCategoryNamespace=*/ false );
1099
- return SS.str ();
1100
- }
1101
- }
1109
+ auto clangMangling = mangleClangDecl (getDecl (), isForeign);
1110
+ if (!clangMangling.empty ())
1111
+ return clangMangling;
1102
1112
}
1103
1113
}
1104
1114
}
@@ -1156,6 +1166,13 @@ std::string SILDeclRef::mangle(ManglingKind MKind) const {
1156
1166
// Use a given cdecl name for native-to-foreign thunks.
1157
1167
if (auto CDeclA = getDecl ()->getAttrs ().getAttribute <CDeclAttr>())
1158
1168
if (isNativeToForeignThunk ()) {
1169
+ // If this is an @implementation @_cdecl, mangle it like the clang
1170
+ // function it implements.
1171
+ if (auto objcInterface = getDecl ()->getImplementedObjCDecl ()) {
1172
+ auto clangMangling = mangleClangDecl (objcInterface, isForeign);
1173
+ if (!clangMangling.empty ())
1174
+ return clangMangling;
1175
+ }
1159
1176
return CDeclA->Name .str ();
1160
1177
}
1161
1178
0 commit comments