2010-08-16 11:33:14 +08:00
|
|
|
//===------- ItaniumCXXABI.cpp - AST support for the Itanium C++ ABI ------===//
|
|
|
|
//
|
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
|
2010-08-16 11:33:14 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
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"
|
2019-09-05 09:23:47 +08:00
|
|
|
#include "clang/AST/Mangle.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"
|
2019-09-05 09:23:47 +08:00
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
2018-05-08 06:23:38 +08:00
|
|
|
#include "llvm/ADT/iterator.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;
|
|
|
|
}
|
|
|
|
|
2018-05-08 06:23:38 +08:00
|
|
|
/// The name of a decomposition declaration.
|
|
|
|
struct DecompositionDeclName {
|
|
|
|
using BindingArray = ArrayRef<const BindingDecl*>;
|
|
|
|
|
|
|
|
/// Representative example of a set of bindings with these names.
|
|
|
|
BindingArray Bindings;
|
|
|
|
|
|
|
|
/// Iterators over the sequence of identifiers in the name.
|
|
|
|
struct Iterator
|
|
|
|
: llvm::iterator_adaptor_base<Iterator, BindingArray::const_iterator,
|
|
|
|
std::random_access_iterator_tag,
|
|
|
|
const IdentifierInfo *> {
|
|
|
|
Iterator(BindingArray::const_iterator It) : iterator_adaptor_base(It) {}
|
|
|
|
const IdentifierInfo *operator*() const {
|
|
|
|
return (*this->I)->getIdentifier();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Iterator begin() const { return Iterator(Bindings.begin()); }
|
|
|
|
Iterator end() const { return Iterator(Bindings.end()); }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
2019-09-05 09:23:47 +08:00
|
|
|
template<typename T> bool isDenseMapKeyEmpty(T V) {
|
|
|
|
return llvm::DenseMapInfo<T>::isEqual(
|
|
|
|
V, llvm::DenseMapInfo<T>::getEmptyKey());
|
|
|
|
}
|
|
|
|
template<typename T> bool isDenseMapKeyTombstone(T V) {
|
|
|
|
return llvm::DenseMapInfo<T>::isEqual(
|
|
|
|
V, llvm::DenseMapInfo<T>::getTombstoneKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
Optional<bool> areDenseMapKeysEqualSpecialValues(T LHS, T RHS) {
|
|
|
|
bool LHSEmpty = isDenseMapKeyEmpty(LHS);
|
|
|
|
bool RHSEmpty = isDenseMapKeyEmpty(RHS);
|
|
|
|
if (LHSEmpty || RHSEmpty)
|
|
|
|
return LHSEmpty && RHSEmpty;
|
|
|
|
|
|
|
|
bool LHSTombstone = isDenseMapKeyTombstone(LHS);
|
|
|
|
bool RHSTombstone = isDenseMapKeyTombstone(RHS);
|
|
|
|
if (LHSTombstone || RHSTombstone)
|
|
|
|
return LHSTombstone && RHSTombstone;
|
|
|
|
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2018-05-08 06:23:38 +08:00
|
|
|
template<>
|
|
|
|
struct DenseMapInfo<DecompositionDeclName> {
|
|
|
|
using ArrayInfo = llvm::DenseMapInfo<ArrayRef<const BindingDecl*>>;
|
|
|
|
static DecompositionDeclName getEmptyKey() {
|
|
|
|
return {ArrayInfo::getEmptyKey()};
|
|
|
|
}
|
|
|
|
static DecompositionDeclName getTombstoneKey() {
|
|
|
|
return {ArrayInfo::getTombstoneKey()};
|
|
|
|
}
|
|
|
|
static unsigned getHashValue(DecompositionDeclName Key) {
|
|
|
|
assert(!isEqual(Key, getEmptyKey()) && !isEqual(Key, getTombstoneKey()));
|
|
|
|
return llvm::hash_combine_range(Key.begin(), Key.end());
|
|
|
|
}
|
|
|
|
static bool isEqual(DecompositionDeclName LHS, DecompositionDeclName RHS) {
|
2019-09-05 09:23:47 +08:00
|
|
|
if (Optional<bool> Result = areDenseMapKeysEqualSpecialValues(
|
|
|
|
LHS.Bindings, RHS.Bindings))
|
|
|
|
return *Result;
|
|
|
|
|
2018-05-08 06:23:38 +08:00
|
|
|
return LHS.Bindings.size() == RHS.Bindings.size() &&
|
|
|
|
std::equal(LHS.begin(), LHS.end(), RHS.begin());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Keeps track of the mangled names of lambda expressions and block
|
2013-09-11 04:14:30 +08:00
|
|
|
/// literals within a particular context.
|
|
|
|
class ItaniumNumberingContext : public MangleNumberingContext {
|
2019-09-05 09:23:47 +08:00
|
|
|
ItaniumMangleContext *Mangler;
|
|
|
|
llvm::StringMap<unsigned> LambdaManglingNumbers;
|
|
|
|
unsigned BlockManglingNumber = 0;
|
2014-12-13 00:17:46 +08:00
|
|
|
llvm::DenseMap<const IdentifierInfo *, unsigned> VarManglingNumbers;
|
|
|
|
llvm::DenseMap<const IdentifierInfo *, unsigned> TagManglingNumbers;
|
2018-05-08 06:23:38 +08:00
|
|
|
llvm::DenseMap<DecompositionDeclName, unsigned>
|
|
|
|
DecompsitionDeclManglingNumbers;
|
2013-09-11 04:14:30 +08:00
|
|
|
|
|
|
|
public:
|
2019-09-05 09:23:47 +08:00
|
|
|
ItaniumNumberingContext(ItaniumMangleContext *Mangler) : Mangler(Mangler) {}
|
|
|
|
|
2014-08-22 12:22:50 +08:00
|
|
|
unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
|
2019-09-05 09:23:47 +08:00
|
|
|
const CXXRecordDecl *Lambda = CallOperator->getParent();
|
|
|
|
assert(Lambda->isLambda());
|
|
|
|
|
|
|
|
// Computation of the <lambda-sig> is non-trivial and subtle. Rather than
|
|
|
|
// duplicating it here, just mangle the <lambda-sig> directly.
|
|
|
|
llvm::SmallString<128> LambdaSig;
|
|
|
|
llvm::raw_svector_ostream Out(LambdaSig);
|
|
|
|
Mangler->mangleLambdaSig(Lambda, Out);
|
2014-08-22 12:22:50 +08:00
|
|
|
|
2019-09-05 09:23:47 +08:00
|
|
|
return ++LambdaManglingNumbers[LambdaSig];
|
2014-08-22 12:22:50 +08:00
|
|
|
}
|
|
|
|
|
2014-10-02 00:56:40 +08:00
|
|
|
unsigned getManglingNumber(const BlockDecl *BD) override {
|
2019-09-05 09:23:47 +08:00
|
|
|
return ++BlockManglingNumber;
|
2014-08-22 12:22:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-06-26 23:20:20 +08:00
|
|
|
if (auto *DD = dyn_cast<DecompositionDecl>(VD)) {
|
|
|
|
DecompositionDeclName Name{DD->bindings()};
|
|
|
|
return ++DecompsitionDeclManglingNumbers[Name];
|
|
|
|
}
|
2018-05-08 06:23:38 +08:00
|
|
|
|
2014-12-13 00:17:46 +08:00
|
|
|
const IdentifierInfo *Identifier = VD->getIdentifier();
|
|
|
|
if (!Identifier) {
|
2018-05-08 06:23:38 +08:00
|
|
|
// VarDecl without an identifier represents an anonymous union
|
|
|
|
// declaration.
|
2014-12-13 00:17:46 +08:00
|
|
|
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 {
|
2019-09-05 09:23:47 +08:00
|
|
|
private:
|
|
|
|
std::unique_ptr<MangleContext> Mangler;
|
2010-08-22 06:46:04 +08:00
|
|
|
protected:
|
2010-08-16 11:33:14 +08:00
|
|
|
ASTContext &Context;
|
|
|
|
public:
|
2019-09-05 09:23:47 +08:00
|
|
|
ItaniumCXXABI(ASTContext &Ctx)
|
|
|
|
: Mangler(Ctx.createMangleContext()), Context(Ctx) {}
|
2010-08-16 11:33:14 +08:00
|
|
|
|
2017-12-01 00:37:02 +08:00
|
|
|
MemberPointerInfo
|
|
|
|
getMemberPointerInfo(const MemberPointerType *MPT) const override {
|
2013-03-29 04:02:56 +08:00
|
|
|
const TargetInfo &Target = Context.getTargetInfo();
|
|
|
|
TargetInfo::IntType PtrDiff = Target.getPtrDiffType(0);
|
2017-12-01 00:37:02 +08:00
|
|
|
MemberPointerInfo MPI;
|
|
|
|
MPI.Width = Target.getTypeWidth(PtrDiff);
|
|
|
|
MPI.Align = Target.getTypeAlign(PtrDiff);
|
|
|
|
MPI.HasPadding = false;
|
2015-04-24 09:25:08 +08:00
|
|
|
if (MPT->isMemberFunctionPointer())
|
2017-12-01 00:37:02 +08:00
|
|
|
MPI.Width *= 2;
|
|
|
|
return MPI;
|
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;
|
2019-07-26 20:36:12 +08:00
|
|
|
return Context.getTargetInfo().getDefaultCallingConv();
|
2010-10-29 11:25:11 +08:00
|
|
|
}
|
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);
|
2018-07-31 03:24:48 +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
|
|
|
|
2015-03-07 02:53:55 +08:00
|
|
|
const CXXConstructorDecl *
|
|
|
|
getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
|
|
|
|
CXXConstructorDecl *CD) override {}
|
|
|
|
|
2015-09-01 02:48:39 +08:00
|
|
|
void addTypedefNameForUnnamedTagDecl(TagDecl *TD,
|
|
|
|
TypedefNameDecl *DD) override {}
|
|
|
|
|
|
|
|
TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addDeclaratorForUnnamedTagDecl(TagDecl *TD,
|
|
|
|
DeclaratorDecl *DD) override {}
|
|
|
|
|
|
|
|
DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-10-11 00:26:19 +08:00
|
|
|
std::unique_ptr<MangleNumberingContext>
|
|
|
|
createMangleNumberingContext() const override {
|
2019-09-05 09:23:47 +08:00
|
|
|
return std::make_unique<ItaniumNumberingContext>(
|
|
|
|
cast<ItaniumMangleContext>(Mangler.get()));
|
2013-09-11 04:14:30 +08:00
|
|
|
}
|
2010-08-16 11:33:14 +08:00
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2010-08-16 11:33:14 +08:00
|
|
|
|
|
|
|
CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) {
|
|
|
|
return new ItaniumCXXABI(Ctx);
|
|
|
|
}
|