2011-01-14 02:57:25 +08:00
|
|
|
//===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-01-14 02:57:25 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implements generic name mangling support for blocks and Objective-C.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-10-16 09:40:34 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2011-01-14 02:57:25 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/AST/DeclTemplate.h"
|
|
|
|
#include "clang/AST/ExprCXX.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "clang/AST/Mangle.h"
|
2019-06-20 04:51:35 +08:00
|
|
|
#include "clang/AST/VTableBuilder.h"
|
2011-01-14 02:57:25 +08:00
|
|
|
#include "clang/Basic/ABI.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2013-10-16 09:40:34 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2011-01-14 02:57:25 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2019-06-20 14:01:06 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2019-06-20 04:51:35 +08:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2011-01-14 02:57:25 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-01-14 02:57:25 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
// FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves
|
|
|
|
// much to be desired. Come up with a better mangling scheme.
|
|
|
|
|
|
|
|
static void mangleFunctionBlock(MangleContext &Context,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Outer,
|
2011-01-14 02:57:25 +08:00
|
|
|
const BlockDecl *BD,
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &Out) {
|
2012-06-27 00:06:38 +08:00
|
|
|
unsigned discriminator = Context.getBlockId(BD, true);
|
|
|
|
if (discriminator == 0)
|
|
|
|
Out << "__" << Outer << "_block_invoke";
|
|
|
|
else
|
2018-07-31 03:24:48 +08:00
|
|
|
Out << "__" << Outer << "_block_invoke_" << discriminator+1;
|
2011-01-14 02:57:25 +08:00
|
|
|
}
|
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void MangleContext::anchor() { }
|
|
|
|
|
2014-11-01 06:00:51 +08:00
|
|
|
enum CCMangling {
|
|
|
|
CCM_Other,
|
|
|
|
CCM_Fast,
|
2016-11-03 02:29:35 +08:00
|
|
|
CCM_RegCall,
|
2014-11-01 06:00:51 +08:00
|
|
|
CCM_Vector,
|
|
|
|
CCM_Std
|
2013-10-16 09:40:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool isExternC(const NamedDecl *ND) {
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
|
|
|
|
return FD->isExternC();
|
|
|
|
return cast<VarDecl>(ND)->isExternC();
|
|
|
|
}
|
|
|
|
|
2014-11-01 06:00:51 +08:00
|
|
|
static CCMangling getCallingConvMangling(const ASTContext &Context,
|
|
|
|
const NamedDecl *ND) {
|
2013-10-16 09:40:34 +08:00
|
|
|
const TargetInfo &TI = Context.getTargetInfo();
|
2014-03-05 03:31:42 +08:00
|
|
|
const llvm::Triple &Triple = TI.getTriple();
|
2014-11-01 06:00:51 +08:00
|
|
|
if (!Triple.isOSWindows() ||
|
|
|
|
!(Triple.getArch() == llvm::Triple::x86 ||
|
|
|
|
Triple.getArch() == llvm::Triple::x86_64))
|
|
|
|
return CCM_Other;
|
2013-10-16 09:40:34 +08:00
|
|
|
|
|
|
|
if (Context.getLangOpts().CPlusPlus && !isExternC(ND) &&
|
|
|
|
TI.getCXXABI() == TargetCXXABI::Microsoft)
|
2014-11-01 06:00:51 +08:00
|
|
|
return CCM_Other;
|
2013-10-16 09:40:34 +08:00
|
|
|
|
|
|
|
const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
|
|
|
|
if (!FD)
|
2014-11-01 06:00:51 +08:00
|
|
|
return CCM_Other;
|
2013-10-16 09:40:34 +08:00
|
|
|
QualType T = FD->getType();
|
|
|
|
|
|
|
|
const FunctionType *FT = T->castAs<FunctionType>();
|
|
|
|
|
|
|
|
CallingConv CC = FT->getCallConv();
|
|
|
|
switch (CC) {
|
|
|
|
default:
|
2014-11-01 06:00:51 +08:00
|
|
|
return CCM_Other;
|
2013-10-16 09:40:34 +08:00
|
|
|
case CC_X86FastCall:
|
2014-11-01 06:00:51 +08:00
|
|
|
return CCM_Fast;
|
2013-10-16 09:40:34 +08:00
|
|
|
case CC_X86StdCall:
|
2014-11-01 06:00:51 +08:00
|
|
|
return CCM_Std;
|
|
|
|
case CC_X86VectorCall:
|
|
|
|
return CCM_Vector;
|
2013-10-16 09:40:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
|
|
|
|
const ASTContext &ASTContext = getASTContext();
|
|
|
|
|
2014-11-01 06:00:51 +08:00
|
|
|
CCMangling CC = getCallingConvMangling(ASTContext, D);
|
|
|
|
if (CC != CCM_Other)
|
2013-10-16 09:40:34 +08:00
|
|
|
return true;
|
|
|
|
|
2017-09-07 08:55:55 +08:00
|
|
|
// If the declaration has an owning module for linkage purposes that needs to
|
|
|
|
// be mangled, we must mangle its name.
|
|
|
|
if (!D->hasExternalFormalLinkage() && D->getOwningModuleForLinkage())
|
|
|
|
return true;
|
|
|
|
|
2013-10-16 09:40:34 +08:00
|
|
|
// In C, functions with no attributes never need to be mangled. Fastpath them.
|
|
|
|
if (!getASTContext().getLangOpts().CPlusPlus && !D->hasAttrs())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Any decl can be declared with __asm("foo") on it, and this takes precedence
|
|
|
|
// over all other naming in the .o file.
|
|
|
|
if (D->hasAttr<AsmLabelAttr>())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return shouldMangleCXXName(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MangleContext::mangleName(const NamedDecl *D, raw_ostream &Out) {
|
|
|
|
// Any decl can be declared with __asm("foo") on it, and this takes precedence
|
|
|
|
// over all other naming in the .o file.
|
|
|
|
if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) {
|
|
|
|
// If we have an asm name, then we use it as the mangling.
|
|
|
|
|
|
|
|
// Adding the prefix can cause problems when one file has a "foo" and
|
|
|
|
// another has a "\01foo". That is known to happen on ELF with the
|
|
|
|
// tricks normally used for producing aliases (PR9177). Fortunately the
|
|
|
|
// llvm mangler on ELF is a nop, so we can just avoid adding the \01
|
|
|
|
// marker. We also avoid adding the marker if this is an alias for an
|
|
|
|
// LLVM intrinsic.
|
2016-03-05 03:00:41 +08:00
|
|
|
char GlobalPrefix =
|
|
|
|
getASTContext().getTargetInfo().getDataLayout().getGlobalPrefix();
|
|
|
|
if (GlobalPrefix && !ALA->getLabel().startswith("llvm."))
|
2013-10-16 09:40:34 +08:00
|
|
|
Out << '\01'; // LLVM IR Marker for __asm("foo")
|
|
|
|
|
|
|
|
Out << ALA->getLabel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ASTContext &ASTContext = getASTContext();
|
2014-11-01 06:00:51 +08:00
|
|
|
CCMangling CC = getCallingConvMangling(ASTContext, D);
|
2013-10-16 09:40:34 +08:00
|
|
|
bool MCXX = shouldMangleCXXName(D);
|
|
|
|
const TargetInfo &TI = Context.getTargetInfo();
|
2014-11-01 06:00:51 +08:00
|
|
|
if (CC == CCM_Other || (MCXX && TI.getCXXABI() == TargetCXXABI::Microsoft)) {
|
2014-10-15 01:20:18 +08:00
|
|
|
if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
|
|
|
|
mangleObjCMethodName(OMD, Out);
|
|
|
|
else
|
|
|
|
mangleCXXName(D, Out);
|
2013-10-16 09:40:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << '\01';
|
2014-11-01 06:00:51 +08:00
|
|
|
if (CC == CCM_Std)
|
2013-10-16 09:40:34 +08:00
|
|
|
Out << '_';
|
2014-11-01 06:00:51 +08:00
|
|
|
else if (CC == CCM_Fast)
|
2013-10-16 09:40:34 +08:00
|
|
|
Out << '@';
|
2016-11-03 02:29:35 +08:00
|
|
|
else if (CC == CCM_RegCall)
|
|
|
|
Out << "__regcall3__";
|
2013-10-16 09:40:34 +08:00
|
|
|
|
|
|
|
if (!MCXX)
|
|
|
|
Out << D->getIdentifier()->getName();
|
2014-10-15 01:20:18 +08:00
|
|
|
else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
|
|
|
|
mangleObjCMethodName(OMD, Out);
|
2013-10-16 09:40:34 +08:00
|
|
|
else
|
|
|
|
mangleCXXName(D, Out);
|
|
|
|
|
|
|
|
const FunctionDecl *FD = cast<FunctionDecl>(D);
|
|
|
|
const FunctionType *FT = FD->getType()->castAs<FunctionType>();
|
|
|
|
const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT);
|
2014-11-01 06:00:51 +08:00
|
|
|
if (CC == CCM_Vector)
|
|
|
|
Out << '@';
|
2013-10-16 09:40:34 +08:00
|
|
|
Out << '@';
|
|
|
|
if (!Proto) {
|
|
|
|
Out << '0';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(!Proto->isVariadic());
|
|
|
|
unsigned ArgWords = 0;
|
|
|
|
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
|
|
|
|
if (!MD->isStatic())
|
|
|
|
++ArgWords;
|
2014-03-17 23:23:01 +08:00
|
|
|
for (const auto &AT : Proto->param_types())
|
2014-11-01 06:00:51 +08:00
|
|
|
// Size should be aligned to pointer size.
|
2016-01-15 05:00:27 +08:00
|
|
|
ArgWords +=
|
|
|
|
llvm::alignTo(ASTContext.getTypeSize(AT), TI.getPointerWidth(0)) /
|
|
|
|
TI.getPointerWidth(0);
|
2014-11-01 06:00:51 +08:00
|
|
|
Out << ((TI.getPointerWidth(0) / 8) * ArgWords);
|
2013-10-16 09:40:34 +08:00
|
|
|
}
|
|
|
|
|
2011-01-14 02:57:25 +08:00
|
|
|
void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
|
2012-06-27 00:06:38 +08:00
|
|
|
const NamedDecl *ID,
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &Out) {
|
2012-06-27 00:06:38 +08:00
|
|
|
unsigned discriminator = getBlockId(BD, false);
|
|
|
|
if (ID) {
|
|
|
|
if (shouldMangleDeclName(ID))
|
|
|
|
mangleName(ID, Out);
|
|
|
|
else {
|
|
|
|
Out << ID->getIdentifier()->getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (discriminator == 0)
|
|
|
|
Out << "_block_invoke";
|
|
|
|
else
|
|
|
|
Out << "_block_invoke_" << discriminator+1;
|
2011-01-14 02:57:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
|
|
|
|
CXXCtorType CT, const BlockDecl *BD,
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &ResStream) {
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<64> Buffer;
|
2011-02-11 07:59:36 +08:00
|
|
|
llvm::raw_svector_ostream Out(Buffer);
|
|
|
|
mangleCXXCtor(CD, CT, Out);
|
|
|
|
mangleFunctionBlock(*this, Buffer, BD, ResStream);
|
2011-01-14 02:57:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MangleContext::mangleDtorBlock(const CXXDestructorDecl *DD,
|
|
|
|
CXXDtorType DT, const BlockDecl *BD,
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &ResStream) {
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<64> Buffer;
|
2011-02-11 07:59:36 +08:00
|
|
|
llvm::raw_svector_ostream Out(Buffer);
|
|
|
|
mangleCXXDtor(DD, DT, Out);
|
|
|
|
mangleFunctionBlock(*this, Buffer, BD, ResStream);
|
2011-01-14 02:57:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &Out) {
|
2011-01-14 02:57:25 +08:00
|
|
|
assert(!isa<CXXConstructorDecl>(DC) && !isa<CXXDestructorDecl>(DC));
|
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<64> Buffer;
|
2011-02-11 10:52:17 +08:00
|
|
|
llvm::raw_svector_ostream Stream(Buffer);
|
2011-01-14 02:57:25 +08:00
|
|
|
if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(DC)) {
|
2011-02-11 10:52:17 +08:00
|
|
|
mangleObjCMethodName(Method, Stream);
|
2011-01-14 02:57:25 +08:00
|
|
|
} else {
|
2014-10-15 01:20:14 +08:00
|
|
|
assert((isa<NamedDecl>(DC) || isa<BlockDecl>(DC)) &&
|
|
|
|
"expected a NamedDecl or BlockDecl");
|
|
|
|
if (isa<BlockDecl>(DC))
|
|
|
|
for (; DC && isa<BlockDecl>(DC); DC = DC->getParent())
|
|
|
|
(void) getBlockId(cast<BlockDecl>(DC), true);
|
2014-11-15 07:55:27 +08:00
|
|
|
assert((isa<TranslationUnitDecl>(DC) || isa<NamedDecl>(DC)) &&
|
|
|
|
"expected a TranslationUnitDecl or a NamedDecl");
|
2014-12-03 02:42:51 +08:00
|
|
|
if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
|
|
|
|
mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
|
|
|
|
else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
|
|
|
|
mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
|
|
|
|
else if (auto ND = dyn_cast<NamedDecl>(DC)) {
|
2014-11-15 07:55:27 +08:00
|
|
|
if (!shouldMangleDeclName(ND) && ND->getIdentifier())
|
|
|
|
Stream << ND->getIdentifier()->getName();
|
|
|
|
else {
|
|
|
|
// FIXME: We were doing a mangleUnqualifiedName() before, but that's
|
|
|
|
// a private member of a class that will soon itself be private to the
|
|
|
|
// Itanium C++ ABI object. What should we do now? Right now, I'm just
|
|
|
|
// calling the mangleName() method on the MangleContext; is there a
|
|
|
|
// better way?
|
|
|
|
mangleName(ND, Stream);
|
|
|
|
}
|
2011-01-14 02:57:25 +08:00
|
|
|
}
|
|
|
|
}
|
2011-02-11 07:59:36 +08:00
|
|
|
mangleFunctionBlock(*this, Buffer, BD, Out);
|
2011-01-14 02:57:25 +08:00
|
|
|
}
|
|
|
|
|
2016-02-15 06:30:14 +08:00
|
|
|
void MangleContext::mangleObjCMethodNameWithoutSize(const ObjCMethodDecl *MD,
|
|
|
|
raw_ostream &OS) {
|
2011-01-14 02:57:25 +08:00
|
|
|
const ObjCContainerDecl *CD =
|
|
|
|
dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
|
|
|
|
assert (CD && "Missing container decl in GetNameForMethod");
|
2017-03-07 17:26:07 +08:00
|
|
|
OS << (MD->isInstanceMethod() ? '-' : '+') << '[';
|
|
|
|
if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD)) {
|
|
|
|
OS << CID->getClassInterface()->getName();
|
2012-02-07 19:57:45 +08:00
|
|
|
OS << '(' << *CID << ')';
|
2017-03-07 17:26:07 +08:00
|
|
|
} else {
|
|
|
|
OS << CD->getName();
|
|
|
|
}
|
2014-01-04 01:59:55 +08:00
|
|
|
OS << ' ';
|
|
|
|
MD->getSelector().print(OS);
|
|
|
|
OS << ']';
|
2016-02-15 06:30:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
|
|
|
|
raw_ostream &Out) {
|
|
|
|
SmallString<64> Name;
|
|
|
|
llvm::raw_svector_ostream OS(Name);
|
|
|
|
|
|
|
|
mangleObjCMethodNameWithoutSize(MD, OS);
|
2011-01-14 02:57:25 +08:00
|
|
|
Out << OS.str().size() << OS.str();
|
|
|
|
}
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
class ASTNameGenerator::Implementation {
|
|
|
|
std::unique_ptr<MangleContext> MC;
|
|
|
|
llvm::DataLayout DL;
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
public:
|
|
|
|
explicit Implementation(ASTContext &Ctx)
|
|
|
|
: MC(Ctx.createMangleContext()), DL(Ctx.getTargetInfo().getDataLayout()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool writeName(const Decl *D, raw_ostream &OS) {
|
|
|
|
// First apply frontend mangling.
|
|
|
|
SmallString<128> FrontendBuf;
|
|
|
|
llvm::raw_svector_ostream FrontendBufOS(FrontendBuf);
|
|
|
|
if (auto *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
if (FD->isDependentContext())
|
|
|
|
return true;
|
|
|
|
if (writeFuncOrVarName(FD, FrontendBufOS))
|
|
|
|
return true;
|
|
|
|
} else if (auto *VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
if (writeFuncOrVarName(VD, FrontendBufOS))
|
|
|
|
return true;
|
|
|
|
} else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
|
|
|
|
MC->mangleObjCMethodNameWithoutSize(MD, OS);
|
|
|
|
return false;
|
|
|
|
} else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
|
|
|
|
writeObjCClassName(ID, FrontendBufOS);
|
|
|
|
} else {
|
2019-06-20 04:51:35 +08:00
|
|
|
return true;
|
2019-06-20 14:01:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now apply backend mangling.
|
|
|
|
llvm::Mangler::getNameWithPrefix(OS, FrontendBufOS.str(), DL);
|
2019-06-20 04:51:35 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
std::string getName(const Decl *D) {
|
|
|
|
std::string Name;
|
|
|
|
{
|
|
|
|
llvm::raw_string_ostream OS(Name);
|
|
|
|
writeName(D, OS);
|
|
|
|
}
|
|
|
|
return Name;
|
2019-06-20 04:51:35 +08:00
|
|
|
}
|
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
enum ObjCKind {
|
|
|
|
ObjCClass,
|
|
|
|
ObjCMetaclass,
|
|
|
|
};
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
static StringRef getClassSymbolPrefix(ObjCKind Kind,
|
|
|
|
const ASTContext &Context) {
|
|
|
|
if (Context.getLangOpts().ObjCRuntime.isGNUFamily())
|
|
|
|
return Kind == ObjCMetaclass ? "_OBJC_METACLASS_" : "_OBJC_CLASS_";
|
|
|
|
return Kind == ObjCMetaclass ? "OBJC_METACLASS_$_" : "OBJC_CLASS_$_";
|
|
|
|
}
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
std::vector<std::string> getAllManglings(const ObjCContainerDecl *OCD) {
|
|
|
|
StringRef ClassName;
|
|
|
|
if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(OCD))
|
|
|
|
ClassName = OID->getObjCRuntimeNameAsString();
|
|
|
|
else if (const auto *OID = dyn_cast<ObjCImplementationDecl>(OCD))
|
|
|
|
ClassName = OID->getObjCRuntimeNameAsString();
|
|
|
|
|
|
|
|
if (ClassName.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto Mangle = [&](ObjCKind Kind, StringRef ClassName) -> std::string {
|
|
|
|
SmallString<40> Mangled;
|
|
|
|
auto Prefix = getClassSymbolPrefix(Kind, OCD->getASTContext());
|
|
|
|
llvm::Mangler::getNameWithPrefix(Mangled, Prefix + ClassName, DL);
|
|
|
|
return Mangled.str();
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
Mangle(ObjCClass, ClassName),
|
|
|
|
Mangle(ObjCMetaclass, ClassName),
|
|
|
|
};
|
|
|
|
}
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
std::vector<std::string> getAllManglings(const Decl *D) {
|
|
|
|
if (const auto *OCD = dyn_cast<ObjCContainerDecl>(D))
|
|
|
|
return getAllManglings(OCD);
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D)))
|
|
|
|
return {};
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
const NamedDecl *ND = cast<NamedDecl>(D);
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
ASTContext &Ctx = ND->getASTContext();
|
|
|
|
std::unique_ptr<MangleContext> M(Ctx.createMangleContext());
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
std::vector<std::string> Manglings;
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
auto hasDefaultCXXMethodCC = [](ASTContext &C, const CXXMethodDecl *MD) {
|
|
|
|
auto DefaultCC = C.getDefaultCallingConvention(/*IsVariadic=*/false,
|
|
|
|
/*IsCSSMethod=*/true);
|
|
|
|
auto CC = MD->getType()->getAs<FunctionProtoType>()->getCallConv();
|
|
|
|
return CC == DefaultCC;
|
|
|
|
};
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND)) {
|
|
|
|
Manglings.emplace_back(getMangledStructor(CD, Ctor_Base));
|
|
|
|
|
|
|
|
if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily())
|
|
|
|
if (!CD->getParent()->isAbstract())
|
|
|
|
Manglings.emplace_back(getMangledStructor(CD, Ctor_Complete));
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
if (Ctx.getTargetInfo().getCXXABI().isMicrosoft())
|
|
|
|
if (CD->hasAttr<DLLExportAttr>() && CD->isDefaultConstructor())
|
|
|
|
if (!(hasDefaultCXXMethodCC(Ctx, CD) && CD->getNumParams() == 0))
|
|
|
|
Manglings.emplace_back(getMangledStructor(CD, Ctor_DefaultClosure));
|
|
|
|
} else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) {
|
|
|
|
Manglings.emplace_back(getMangledStructor(DD, Dtor_Base));
|
|
|
|
if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily()) {
|
|
|
|
Manglings.emplace_back(getMangledStructor(DD, Dtor_Complete));
|
|
|
|
if (DD->isVirtual())
|
|
|
|
Manglings.emplace_back(getMangledStructor(DD, Dtor_Deleting));
|
|
|
|
}
|
|
|
|
} else if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) {
|
|
|
|
Manglings.emplace_back(getName(ND));
|
|
|
|
if (MD->isVirtual())
|
|
|
|
if (const auto *TIV = Ctx.getVTableContext()->getThunkInfo(MD))
|
|
|
|
for (const auto &T : *TIV)
|
|
|
|
Manglings.emplace_back(getMangledThunk(MD, T));
|
2019-06-20 04:51:35 +08:00
|
|
|
}
|
2019-06-20 14:01:06 +08:00
|
|
|
|
|
|
|
return Manglings;
|
2019-06-20 04:51:35 +08:00
|
|
|
}
|
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
private:
|
|
|
|
bool writeFuncOrVarName(const NamedDecl *D, raw_ostream &OS) {
|
|
|
|
if (MC->shouldMangleDeclName(D)) {
|
|
|
|
if (const auto *CtorD = dyn_cast<CXXConstructorDecl>(D))
|
|
|
|
MC->mangleCXXCtor(CtorD, Ctor_Complete, OS);
|
|
|
|
else if (const auto *DtorD = dyn_cast<CXXDestructorDecl>(D))
|
|
|
|
MC->mangleCXXDtor(DtorD, Dtor_Complete, OS);
|
|
|
|
else
|
|
|
|
MC->mangleName(D, OS);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
IdentifierInfo *II = D->getIdentifier();
|
|
|
|
if (!II)
|
|
|
|
return true;
|
|
|
|
OS << II->getName();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
void writeObjCClassName(const ObjCInterfaceDecl *D, raw_ostream &OS) {
|
|
|
|
OS << getClassSymbolPrefix(ObjCClass, D->getASTContext());
|
|
|
|
OS << D->getObjCRuntimeNameAsString();
|
2019-06-20 04:51:35 +08:00
|
|
|
}
|
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
std::string getMangledStructor(const NamedDecl *ND, unsigned StructorType) {
|
|
|
|
std::string FrontendBuf;
|
|
|
|
llvm::raw_string_ostream FOS(FrontendBuf);
|
|
|
|
|
|
|
|
if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND))
|
|
|
|
MC->mangleCXXCtor(CD, static_cast<CXXCtorType>(StructorType), FOS);
|
|
|
|
else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND))
|
|
|
|
MC->mangleCXXDtor(DD, static_cast<CXXDtorType>(StructorType), FOS);
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
std::string BackendBuf;
|
|
|
|
llvm::raw_string_ostream BOS(BackendBuf);
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
return BOS.str();
|
|
|
|
}
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
std::string getMangledThunk(const CXXMethodDecl *MD, const ThunkInfo &T) {
|
|
|
|
std::string FrontendBuf;
|
|
|
|
llvm::raw_string_ostream FOS(FrontendBuf);
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
MC->mangleThunk(MD, T, FOS);
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
std::string BackendBuf;
|
|
|
|
llvm::raw_string_ostream BOS(BackendBuf);
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
return BOS.str();
|
|
|
|
}
|
|
|
|
};
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
ASTNameGenerator::ASTNameGenerator(ASTContext &Ctx)
|
|
|
|
: Impl(llvm::make_unique<Implementation>(Ctx)) {}
|
|
|
|
|
|
|
|
ASTNameGenerator::~ASTNameGenerator() {}
|
2019-06-20 04:51:35 +08:00
|
|
|
|
2019-06-20 14:01:06 +08:00
|
|
|
bool ASTNameGenerator::writeName(const Decl *D, raw_ostream &OS) {
|
|
|
|
return Impl->writeName(D, OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ASTNameGenerator::getName(const Decl *D) {
|
|
|
|
return Impl->getName(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> ASTNameGenerator::getAllManglings(const Decl *D) {
|
|
|
|
return Impl->getAllManglings(D);
|
2019-06-20 04:51:35 +08:00
|
|
|
}
|