|
@@ -10,6 +10,7 @@
|
|
|
// Implements generic name mangling support for blocks and Objective-C.
|
|
|
//
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
+#include "clang/AST/Attr.h"
|
|
|
#include "clang/AST/Mangle.h"
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
#include "clang/AST/Decl.h"
|
|
@@ -19,6 +20,7 @@
|
|
|
#include "clang/AST/ExprCXX.h"
|
|
|
#include "clang/Basic/ABI.h"
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
+#include "clang/Basic/TargetInfo.h"
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
@@ -47,6 +49,130 @@ static void mangleFunctionBlock(MangleContext &Context,
|
|
|
|
|
|
void MangleContext::anchor() { }
|
|
|
|
|
|
+enum StdOrFastCC {
|
|
|
+ SOF_OTHER,
|
|
|
+ SOF_FAST,
|
|
|
+ SOF_STD
|
|
|
+};
|
|
|
+
|
|
|
+static bool isExternC(const NamedDecl *ND) {
|
|
|
+ if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
|
|
|
+ return FD->isExternC();
|
|
|
+ return cast<VarDecl>(ND)->isExternC();
|
|
|
+}
|
|
|
+
|
|
|
+static StdOrFastCC getStdOrFastCallMangling(const ASTContext &Context,
|
|
|
+ const NamedDecl *ND) {
|
|
|
+ const TargetInfo &TI = Context.getTargetInfo();
|
|
|
+ llvm::Triple Triple = TI.getTriple();
|
|
|
+ if (!Triple.isOSWindows() || Triple.getArch() != llvm::Triple::x86)
|
|
|
+ return SOF_OTHER;
|
|
|
+
|
|
|
+ if (Context.getLangOpts().CPlusPlus && !isExternC(ND) &&
|
|
|
+ TI.getCXXABI() == TargetCXXABI::Microsoft)
|
|
|
+ return SOF_OTHER;
|
|
|
+
|
|
|
+ const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
|
|
|
+ if (!FD)
|
|
|
+ return SOF_OTHER;
|
|
|
+ QualType T = FD->getType();
|
|
|
+
|
|
|
+ const FunctionType *FT = T->castAs<FunctionType>();
|
|
|
+
|
|
|
+ CallingConv CC = FT->getCallConv();
|
|
|
+ switch (CC) {
|
|
|
+ default:
|
|
|
+ return SOF_OTHER;
|
|
|
+ case CC_X86FastCall:
|
|
|
+ return SOF_FAST;
|
|
|
+ case CC_X86StdCall:
|
|
|
+ return SOF_STD;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+bool MangleContext::shouldMangleDeclName(const NamedDecl *D) {
|
|
|
+ const ASTContext &ASTContext = getASTContext();
|
|
|
+
|
|
|
+ StdOrFastCC CC = getStdOrFastCallMangling(ASTContext, D);
|
|
|
+ if (CC != SOF_OTHER)
|
|
|
+ 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.
|
|
|
+ StringRef UserLabelPrefix =
|
|
|
+ getASTContext().getTargetInfo().getUserLabelPrefix();
|
|
|
+ if (!UserLabelPrefix.empty() && !ALA->getLabel().startswith("llvm."))
|
|
|
+ Out << '\01'; // LLVM IR Marker for __asm("foo")
|
|
|
+
|
|
|
+ Out << ALA->getLabel();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const ASTContext &ASTContext = getASTContext();
|
|
|
+ StdOrFastCC CC = getStdOrFastCallMangling(ASTContext, D);
|
|
|
+ bool MCXX = shouldMangleCXXName(D);
|
|
|
+ const TargetInfo &TI = Context.getTargetInfo();
|
|
|
+ if (CC == SOF_OTHER || (MCXX && TI.getCXXABI() == TargetCXXABI::Microsoft)) {
|
|
|
+ mangleCXXName(D, Out);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Out << '\01';
|
|
|
+ if (CC == SOF_STD)
|
|
|
+ Out << '_';
|
|
|
+ else
|
|
|
+ Out << '@';
|
|
|
+
|
|
|
+ if (!MCXX)
|
|
|
+ Out << D->getIdentifier()->getName();
|
|
|
+ else
|
|
|
+ mangleCXXName(D, Out);
|
|
|
+
|
|
|
+ const FunctionDecl *FD = cast<FunctionDecl>(D);
|
|
|
+ const FunctionType *FT = FD->getType()->castAs<FunctionType>();
|
|
|
+ const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT);
|
|
|
+ Out << '@';
|
|
|
+ if (!Proto) {
|
|
|
+ Out << '0';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ assert(!Proto->isVariadic());
|
|
|
+ unsigned ArgWords = 0;
|
|
|
+ if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
|
|
|
+ if (!MD->isStatic())
|
|
|
+ ++ArgWords;
|
|
|
+ for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
|
|
|
+ ArgEnd = Proto->arg_type_end();
|
|
|
+ Arg != ArgEnd; ++Arg) {
|
|
|
+ QualType AT = *Arg;
|
|
|
+ // Size should be aligned to DWORD boundary
|
|
|
+ ArgWords += llvm::RoundUpToAlignment(ASTContext.getTypeSize(AT), 32) / 32;
|
|
|
+ }
|
|
|
+ Out << 4 * ArgWords;
|
|
|
+}
|
|
|
+
|
|
|
void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
|
|
|
const NamedDecl *ID,
|
|
|
raw_ostream &Out) {
|