2010-08-16 11:33:14 +08:00
|
|
|
//===------- ItaniumCXXABI.cpp - AST support for the Itanium C++ ABI ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2011-04-15 13:22:18 +08:00
|
|
|
// This provides C++ AST support targeting the Itanium C++ ABI, which is
|
2010-08-16 11:33:14 +08:00
|
|
|
// documented at:
|
|
|
|
// http://www.codesourcery.com/public/cxx-abi/abi.html
|
|
|
|
// http://www.codesourcery.com/public/cxx-abi/abi-eh.html
|
2010-08-22 06:46:04 +08:00
|
|
|
//
|
|
|
|
// It also supports the closely-related ARM C++ ABI, documented at:
|
|
|
|
// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
|
|
|
|
//
|
2010-08-16 11:33:14 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CXXABI.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
2010-11-25 09:51:53 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2013-09-11 04:14:30 +08:00
|
|
|
#include "clang/AST/MangleNumberingContext.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2010-08-16 11:33:14 +08:00
|
|
|
#include "clang/AST/Type.h"
|
2010-11-25 09:51:53 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2010-08-16 11:33:14 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
2013-09-11 04:14:30 +08:00
|
|
|
|
2014-12-13 00:17:46 +08:00
|
|
|
/// According to Itanium C++ ABI 5.1.2:
|
|
|
|
/// the name of an anonymous union is considered to be
|
|
|
|
/// the name of the first named data member found by a pre-order,
|
|
|
|
/// depth-first, declaration-order walk of the data members of
|
|
|
|
/// the anonymous union.
|
|
|
|
/// If there is no such data member (i.e., if all of the data members
|
|
|
|
/// in the union are unnamed), then there is no way for a program to
|
|
|
|
/// refer to the anonymous union, and there is therefore no need to mangle its name.
|
|
|
|
///
|
|
|
|
/// Returns the name of anonymous union VarDecl or nullptr if it is not found.
|
|
|
|
static const IdentifierInfo *findAnonymousUnionVarDeclName(const VarDecl& VD) {
|
|
|
|
const RecordType *RT = VD.getType()->getAs<RecordType>();
|
|
|
|
assert(RT && "type of VarDecl is expected to be RecordType.");
|
|
|
|
assert(RT->getDecl()->isUnion() && "RecordType is expected to be a union.");
|
|
|
|
if (const FieldDecl *FD = RT->getDecl()->findFirstNamedDataMember()) {
|
|
|
|
return FD->getIdentifier();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-11 04:14:30 +08:00
|
|
|
/// \brief Keeps track of the mangled names of lambda expressions and block
|
|
|
|
/// literals within a particular context.
|
|
|
|
class ItaniumNumberingContext : public MangleNumberingContext {
|
2014-08-22 12:22:50 +08:00
|
|
|
llvm::DenseMap<const Type *, unsigned> ManglingNumbers;
|
2014-12-13 00:17:46 +08:00
|
|
|
llvm::DenseMap<const IdentifierInfo *, unsigned> VarManglingNumbers;
|
|
|
|
llvm::DenseMap<const IdentifierInfo *, unsigned> TagManglingNumbers;
|
2013-09-11 04:14:30 +08:00
|
|
|
|
|
|
|
public:
|
2014-08-22 12:22:50 +08:00
|
|
|
unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
|
|
|
|
const FunctionProtoType *Proto =
|
|
|
|
CallOperator->getType()->getAs<FunctionProtoType>();
|
|
|
|
ASTContext &Context = CallOperator->getASTContext();
|
|
|
|
|
|
|
|
QualType Key =
|
|
|
|
Context.getFunctionType(Context.VoidTy, Proto->getParamTypes(),
|
|
|
|
FunctionProtoType::ExtProtoInfo());
|
|
|
|
Key = Context.getCanonicalType(Key);
|
|
|
|
return ++ManglingNumbers[Key->castAs<FunctionProtoType>()];
|
|
|
|
}
|
|
|
|
|
2014-10-02 00:56:40 +08:00
|
|
|
unsigned getManglingNumber(const BlockDecl *BD) override {
|
2014-08-22 12:22:50 +08:00
|
|
|
const Type *Ty = nullptr;
|
|
|
|
return ++ManglingNumbers[Ty];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned getStaticLocalNumber(const VarDecl *VD) override {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-11 04:14:30 +08:00
|
|
|
/// Variable decls are numbered by identifier.
|
2014-03-11 14:22:39 +08:00
|
|
|
unsigned getManglingNumber(const VarDecl *VD, unsigned) override {
|
2014-12-13 00:17:46 +08:00
|
|
|
const IdentifierInfo *Identifier = VD->getIdentifier();
|
|
|
|
if (!Identifier) {
|
|
|
|
// VarDecl without an identifier represents an anonymous union declaration.
|
|
|
|
Identifier = findAnonymousUnionVarDeclName(*VD);
|
|
|
|
}
|
|
|
|
return ++VarManglingNumbers[Identifier];
|
2013-09-11 04:14:30 +08:00
|
|
|
}
|
2014-03-05 16:57:59 +08:00
|
|
|
|
2014-03-11 14:22:39 +08:00
|
|
|
unsigned getManglingNumber(const TagDecl *TD, unsigned) override {
|
2014-03-05 16:57:59 +08:00
|
|
|
return ++TagManglingNumbers[TD->getIdentifier()];
|
|
|
|
}
|
2013-09-11 04:14:30 +08:00
|
|
|
};
|
|
|
|
|
2010-08-16 11:33:14 +08:00
|
|
|
class ItaniumCXXABI : public CXXABI {
|
2010-08-22 06:46:04 +08:00
|
|
|
protected:
|
2010-08-16 11:33:14 +08:00
|
|
|
ASTContext &Context;
|
|
|
|
public:
|
|
|
|
ItaniumCXXABI(ASTContext &Ctx) : Context(Ctx) { }
|
|
|
|
|
2013-03-29 04:02:56 +08:00
|
|
|
std::pair<uint64_t, unsigned>
|
2014-03-11 14:22:39 +08:00
|
|
|
getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const override {
|
2013-03-29 04:02:56 +08:00
|
|
|
const TargetInfo &Target = Context.getTargetInfo();
|
|
|
|
TargetInfo::IntType PtrDiff = Target.getPtrDiffType(0);
|
|
|
|
uint64_t Width = Target.getTypeWidth(PtrDiff);
|
|
|
|
unsigned Align = Target.getTypeAlign(PtrDiff);
|
|
|
|
if (MPT->getPointeeType()->isFunctionType())
|
|
|
|
Width = 2 * Width;
|
|
|
|
return std::make_pair(Width, Align);
|
2010-08-16 11:33:14 +08:00
|
|
|
}
|
2010-10-29 11:25:11 +08:00
|
|
|
|
2014-03-11 14:22:39 +08:00
|
|
|
CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
|
2013-12-13 00:07:11 +08:00
|
|
|
const llvm::Triple &T = Context.getTargetInfo().getTriple();
|
2014-03-28 06:50:18 +08:00
|
|
|
if (!isVariadic && T.isWindowsGNUEnvironment() &&
|
2013-12-13 00:07:11 +08:00
|
|
|
T.getArch() == llvm::Triple::x86)
|
|
|
|
return CC_X86ThisCall;
|
2010-10-29 11:25:11 +08:00
|
|
|
return CC_C;
|
|
|
|
}
|
2010-11-25 09:51:53 +08:00
|
|
|
|
|
|
|
// We cheat and just check that the class has a vtable pointer, and that it's
|
|
|
|
// only big enough to have a vtable pointer and nothing more (or less).
|
2014-03-11 14:22:39 +08:00
|
|
|
bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
|
2010-11-25 09:51:53 +08:00
|
|
|
|
|
|
|
// Check that the class has a vtable pointer.
|
|
|
|
if (!RD->isDynamicClass())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
2011-02-01 09:52:10 +08:00
|
|
|
CharUnits PointerSize =
|
2011-09-02 08:18:52 +08:00
|
|
|
Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
|
2011-02-01 09:52:10 +08:00
|
|
|
return Layout.getNonVirtualSize() == PointerSize;
|
2010-11-25 09:51:53 +08:00
|
|
|
}
|
2013-09-11 04:14:30 +08:00
|
|
|
|
2014-03-11 14:22:39 +08:00
|
|
|
MangleNumberingContext *createMangleNumberingContext() const override {
|
2013-09-11 04:14:30 +08:00
|
|
|
return new ItaniumNumberingContext();
|
|
|
|
}
|
2010-08-16 11:33:14 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) {
|
|
|
|
return new ItaniumCXXABI(Ctx);
|
|
|
|
}
|