2011-01-14 02:57:25 +08:00
|
|
|
//===--- Mangle.cpp - Mangle C++ Names --------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
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"
|
|
|
|
#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
|
|
|
|
|
|
|
#define MANGLE_CHECKER 0
|
|
|
|
|
|
|
|
#if MANGLE_CHECKER
|
|
|
|
#include <cxxabi.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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
|
|
|
|
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,
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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 << '@';
|
|
|
|
|
|
|
|
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");
|
|
|
|
OS << (MD->isInstanceMethod() ? '-' : '+') << '[' << CD->getName();
|
|
|
|
if (const ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(CD))
|
2012-02-07 19:57:45 +08:00
|
|
|
OS << '(' << *CID << ')';
|
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();
|
|
|
|
}
|