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"
|
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
|
|
|
|
|
|
|
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();
|
|
|
|
}
|