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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/AST/Mangle.h"
|
|
|
|
#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"
|
|
|
|
#include "clang/Basic/ABI.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
|
|
|
|
#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.
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
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) {
|
2011-01-14 02:57:25 +08:00
|
|
|
Out << "__" << Outer << "_block_invoke_" << Context.getBlockId(BD, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void checkMangleDC(const DeclContext *DC, const BlockDecl *BD) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
const DeclContext *ExpectedDC = BD->getDeclContext();
|
|
|
|
while (isa<BlockDecl>(ExpectedDC) || isa<EnumDecl>(ExpectedDC))
|
|
|
|
ExpectedDC = ExpectedDC->getParent();
|
2011-06-12 01:19:42 +08:00
|
|
|
// In-class initializers for non-static data members are lexically defined
|
|
|
|
// within the class, but are mangled as if they were specified as constructor
|
|
|
|
// member initializers.
|
|
|
|
if (isa<CXXRecordDecl>(ExpectedDC) && DC != ExpectedDC)
|
|
|
|
DC = DC->getParent();
|
2011-01-14 02:57:25 +08:00
|
|
|
assert(DC == ExpectedDC && "Given decl context did not match expected!");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void MangleContext::mangleGlobalBlock(const BlockDecl *BD,
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &Out) {
|
2011-01-14 02:57:25 +08:00
|
|
|
Out << "__block_global_" << getBlockId(BD, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MangleContext::mangleCtorBlock(const CXXConstructorDecl *CD,
|
|
|
|
CXXCtorType CT, const BlockDecl *BD,
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &ResStream) {
|
2011-01-14 02:57:25 +08:00
|
|
|
checkMangleDC(CD, BD);
|
|
|
|
llvm::SmallString<64> Buffer;
|
2011-02-11 07:59:36 +08:00
|
|
|
llvm::raw_svector_ostream Out(Buffer);
|
|
|
|
mangleCXXCtor(CD, CT, Out);
|
|
|
|
Out.flush();
|
|
|
|
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) {
|
2011-01-14 02:57:25 +08:00
|
|
|
checkMangleDC(DD, BD);
|
|
|
|
llvm::SmallString<64> Buffer;
|
2011-02-11 07:59:36 +08:00
|
|
|
llvm::raw_svector_ostream Out(Buffer);
|
|
|
|
mangleCXXDtor(DD, DT, Out);
|
|
|
|
Out.flush();
|
|
|
|
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));
|
|
|
|
checkMangleDC(DC, BD);
|
|
|
|
|
|
|
|
llvm::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 {
|
|
|
|
const NamedDecl *ND = cast<NamedDecl>(DC);
|
|
|
|
if (IdentifierInfo *II = ND->getIdentifier())
|
2011-02-11 10:52:17 +08:00
|
|
|
Stream << II->getName();
|
2011-01-14 02:57:25 +08:00
|
|
|
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?
|
2011-02-11 10:52:17 +08:00
|
|
|
mangleName(ND, Stream);
|
2011-01-14 02:57:25 +08:00
|
|
|
}
|
|
|
|
}
|
2011-02-11 10:52:17 +08:00
|
|
|
Stream.flush();
|
2011-02-11 07:59:36 +08:00
|
|
|
mangleFunctionBlock(*this, Buffer, BD, Out);
|
2011-01-14 02:57:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &Out) {
|
2011-01-14 02:57:25 +08:00
|
|
|
llvm::SmallString<64> Name;
|
2011-02-11 10:52:17 +08:00
|
|
|
llvm::raw_svector_ostream OS(Name);
|
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))
|
|
|
|
OS << '(' << CID << ')';
|
|
|
|
OS << ' ' << MD->getSelector().getAsString() << ']';
|
|
|
|
|
|
|
|
Out << OS.str().size() << OS.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MangleContext::mangleBlock(const BlockDecl *BD,
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &Out) {
|
2011-01-14 02:57:25 +08:00
|
|
|
const DeclContext *DC = BD->getDeclContext();
|
|
|
|
while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC))
|
|
|
|
DC = DC->getParent();
|
|
|
|
if (DC->isFunctionOrMethod())
|
2011-02-11 07:59:36 +08:00
|
|
|
mangleBlock(DC, BD, Out);
|
2011-01-14 02:57:25 +08:00
|
|
|
else
|
2011-02-11 07:59:36 +08:00
|
|
|
mangleGlobalBlock(BD, Out);
|
2011-01-14 02:57:25 +08:00
|
|
|
}
|