Skip to content

Commit 98ef7e2

Browse files
ellishgrjmccall
authored andcommitted
This reduces code duplication between CGObjCMac.cpp and Mangle.cpp
for generating the mangled name of an Objective-C method. This has no intended functionality change. https://reviews.llvm.org/D88329
1 parent bc868da commit 98ef7e2

File tree

5 files changed

+41
-43
lines changed

5 files changed

+41
-43
lines changed

clang/include/clang/AST/Mangle.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,11 @@ class MangleContext {
123123
void mangleBlock(const DeclContext *DC, const BlockDecl *BD,
124124
raw_ostream &Out);
125125

126-
void mangleObjCMethodNameWithoutSize(const ObjCMethodDecl *MD, raw_ostream &);
127-
void mangleObjCMethodName(const ObjCMethodDecl *MD, raw_ostream &);
126+
void mangleObjCMethodName(const ObjCMethodDecl *MD, raw_ostream &OS,
127+
bool includePrefixByte = true,
128+
bool includeCategoryNamespace = true);
129+
void mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD,
130+
raw_ostream &);
128131

129132
virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &) = 0;
130133

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2516,7 +2516,7 @@ void CXXNameMangler::mangleRefQualifier(RefQualifierKind RefQualifier) {
25162516
}
25172517

25182518
void CXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
2519-
Context.mangleObjCMethodName(MD, Out);
2519+
Context.mangleObjCMethodNameAsSourceName(MD, Out);
25202520
}
25212521

25222522
static bool isTypeSubstitutable(Qualifiers Quals, const Type *Ty,

clang/lib/AST/Mangle.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void MangleContext::mangleName(GlobalDecl GD, raw_ostream &Out) {
175175
const TargetInfo &TI = Context.getTargetInfo();
176176
if (CC == CCM_Other || (MCXX && TI.getCXXABI() == TargetCXXABI::Microsoft)) {
177177
if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
178-
mangleObjCMethodName(OMD, Out);
178+
mangleObjCMethodNameAsSourceName(OMD, Out);
179179
else
180180
mangleCXXName(GD, Out);
181181
return;
@@ -192,7 +192,7 @@ void MangleContext::mangleName(GlobalDecl GD, raw_ostream &Out) {
192192
if (!MCXX)
193193
Out << D->getIdentifier()->getName();
194194
else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
195-
mangleObjCMethodName(OMD, Out);
195+
mangleObjCMethodNameAsSourceName(OMD, Out);
196196
else
197197
mangleCXXName(GD, Out);
198198

@@ -275,7 +275,7 @@ void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
275275
SmallString<64> Buffer;
276276
llvm::raw_svector_ostream Stream(Buffer);
277277
if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
278-
mangleObjCMethodName(Method, Stream);
278+
mangleObjCMethodNameAsSourceName(Method, Stream);
279279
} else {
280280
assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) &&
281281
"expected a NamedDecl or BlockDecl");
@@ -304,29 +304,38 @@ void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
304304
mangleFunctionBlock(*this, Buffer, BD, Out);
305305
}
306306

307-
void MangleContext::mangleObjCMethodNameWithoutSize(const ObjCMethodDecl *MD,
308-
raw_ostream &OS) {
309-
const ObjCContainerDecl *CD =
310-
dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
311-
assert (CD && "Missing container decl in GetNameForMethod");
307+
void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
308+
raw_ostream &OS,
309+
bool includePrefixByte,
310+
bool includeCategoryNamespace) {
311+
// \01+[ContainerName(CategoryName) SelectorName]
312+
if (includePrefixByte) {
313+
OS << '\01';
314+
}
312315
OS << (MD->isInstanceMethod() ? '-' : '+') << '[';
313-
if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD)) {
316+
if (const auto *CID = dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
314317
OS << CID->getClassInterface()->getName();
315-
OS << '(' << *CID << ')';
316-
} else {
318+
if (includeCategoryNamespace) {
319+
OS << '(' << *CID << ')';
320+
}
321+
} else if (const auto *CD =
322+
dyn_cast<ObjCContainerDecl>(MD->getDeclContext())) {
317323
OS << CD->getName();
324+
} else {
325+
llvm_unreachable("Unexpected ObjC method decl context");
318326
}
319327
OS << ' ';
320328
MD->getSelector().print(OS);
321329
OS << ']';
322330
}
323331

324-
void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
325-
raw_ostream &Out) {
332+
void MangleContext::mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD,
333+
raw_ostream &Out) {
326334
SmallString<64> Name;
327335
llvm::raw_svector_ostream OS(Name);
328336

329-
mangleObjCMethodNameWithoutSize(MD, OS);
337+
mangleObjCMethodName(MD, OS, /*includePrefixByte=*/false,
338+
/*includeCategoryNamespace=*/true);
330339
Out << OS.str().size() << OS.str();
331340
}
332341

@@ -352,7 +361,8 @@ class ASTNameGenerator::Implementation {
352361
if (writeFuncOrVarName(VD, FrontendBufOS))
353362
return true;
354363
} else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
355-
MC->mangleObjCMethodNameWithoutSize(MD, OS);
364+
MC->mangleObjCMethodName(MD, OS, /*includePrefixByte=*/false,
365+
/*includeCategoryNamespace=*/true);
356366
return false;
357367
} else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
358368
writeObjCClassName(ID, FrontendBufOS);

clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ void MicrosoftCXXNameMangler::mangleSourceName(StringRef Name) {
13231323
}
13241324

13251325
void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
1326-
Context.mangleObjCMethodName(MD, Out);
1326+
Context.mangleObjCMethodNameAsSourceName(MD, Out);
13271327
}
13281328

13291329
void MicrosoftCXXNameMangler::mangleTemplateInstantiationName(

clang/lib/CodeGen/CGObjCMac.cpp

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "clang/AST/Attr.h"
2121
#include "clang/AST/Decl.h"
2222
#include "clang/AST/DeclObjC.h"
23+
#include "clang/AST/Mangle.h"
2324
#include "clang/AST/RecordLayout.h"
2425
#include "clang/AST/StmtObjC.h"
2526
#include "clang/Basic/CodeGenOptions.h"
@@ -924,13 +925,6 @@ class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
924925

925926
llvm::StringMap<llvm::GlobalVariable *> NSConstantStringMap;
926927

927-
/// GetNameForMethod - Return a name for the given method.
928-
/// \param[out] NameOut - The return value.
929-
void GetNameForMethod(const ObjCMethodDecl *OMD,
930-
const ObjCContainerDecl *CD,
931-
SmallVectorImpl<char> &NameOut,
932-
bool ignoreCategoryNamespace = false);
933-
934928
/// GetMethodVarName - Return a unique constant for the given
935929
/// selector's name. The return value has type char *.
936930
llvm::Constant *GetMethodVarName(Selector Sel);
@@ -4008,7 +4002,10 @@ llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
40084002
Method = GenerateDirectMethod(OMD, CD);
40094003
} else {
40104004
SmallString<256> Name;
4011-
GetNameForMethod(OMD, CD, Name);
4005+
llvm::raw_svector_ostream OS(Name);
4006+
const auto &MC = CGM.getContext().createMangleContext();
4007+
MC->mangleObjCMethodName(OMD, OS, /*includePrefixByte=*/true,
4008+
/*includeCategoryNamespace=*/true);
40124009

40134010
CodeGenTypes &Types = CGM.getTypes();
40144011
llvm::FunctionType *MethodTy =
@@ -4061,7 +4058,10 @@ CGObjCCommonMac::GenerateDirectMethod(const ObjCMethodDecl *OMD,
40614058
I->second = Fn;
40624059
} else {
40634060
SmallString<256> Name;
4064-
GetNameForMethod(OMD, CD, Name, /*ignoreCategoryNamespace*/ true);
4061+
llvm::raw_svector_ostream OS(Name);
4062+
const auto &MC = CGM.getContext().createMangleContext();
4063+
MC->mangleObjCMethodName(OMD, OS, /*includePrefixByte=*/true,
4064+
/*includeCategoryNamespace=*/false);
40654065

40664066
Fn = llvm::Function::Create(MethodTy, llvm::GlobalValue::ExternalLinkage,
40674067
Name.str(), &CGM.getModule());
@@ -5715,21 +5715,6 @@ CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
57155715
return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
57165716
}
57175717

5718-
void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
5719-
const ObjCContainerDecl *CD,
5720-
SmallVectorImpl<char> &Name,
5721-
bool ignoreCategoryNamespace) {
5722-
llvm::raw_svector_ostream OS(Name);
5723-
assert (CD && "Missing container decl in GetNameForMethod");
5724-
OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
5725-
<< '[' << CD->getName();
5726-
if (!ignoreCategoryNamespace)
5727-
if (const ObjCCategoryImplDecl *CID =
5728-
dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
5729-
OS << '(' << *CID << ')';
5730-
OS << ' ' << D->getSelector().getAsString() << ']';
5731-
}
5732-
57335718
void CGObjCMac::FinishModule() {
57345719
EmitModuleInfo();
57355720

0 commit comments

Comments
 (0)