1 //===------- ItaniumCXXABI.cpp - AST support for the Itanium C++ ABI ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This provides C++ AST support targeting the Itanium C++ ABI, which is
12 // http://www.codesourcery.com/public/cxx-abi/abi.html
13 // http://www.codesourcery.com/public/cxx-abi/abi-eh.html
15 // It also supports the closely-related ARM C++ ABI, documented at:
16 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
18 //===----------------------------------------------------------------------===//
21 #include "clang/AST/ASTContext.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/MangleNumberingContext.h"
24 #include "clang/AST/RecordLayout.h"
25 #include "clang/AST/Type.h"
26 #include "clang/Basic/TargetInfo.h"
28 using namespace clang;
32 /// According to Itanium C++ ABI 5.1.2:
33 /// the name of an anonymous union is considered to be
34 /// the name of the first named data member found by a pre-order,
35 /// depth-first, declaration-order walk of the data members of
36 /// the anonymous union.
37 /// If there is no such data member (i.e., if all of the data members
38 /// in the union are unnamed), then there is no way for a program to
39 /// refer to the anonymous union, and there is therefore no need to mangle its name.
41 /// Returns the name of anonymous union VarDecl or nullptr if it is not found.
42 static const IdentifierInfo *findAnonymousUnionVarDeclName(const VarDecl& VD) {
43 const RecordType *RT = VD.getType()->getAs<RecordType>();
44 assert(RT && "type of VarDecl is expected to be RecordType.");
45 assert(RT->getDecl()->isUnion() && "RecordType is expected to be a union.");
46 if (const FieldDecl *FD = RT->getDecl()->findFirstNamedDataMember()) {
47 return FD->getIdentifier();
53 /// \brief Keeps track of the mangled names of lambda expressions and block
54 /// literals within a particular context.
55 class ItaniumNumberingContext : public MangleNumberingContext {
56 llvm::DenseMap<const Type *, unsigned> ManglingNumbers;
57 llvm::DenseMap<const IdentifierInfo *, unsigned> VarManglingNumbers;
58 llvm::DenseMap<const IdentifierInfo *, unsigned> TagManglingNumbers;
61 unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
62 const FunctionProtoType *Proto =
63 CallOperator->getType()->getAs<FunctionProtoType>();
64 ASTContext &Context = CallOperator->getASTContext();
66 FunctionProtoType::ExtProtoInfo EPI;
67 EPI.Variadic = Proto->isVariadic();
69 Context.getFunctionType(Context.VoidTy, Proto->getParamTypes(), EPI);
70 Key = Context.getCanonicalType(Key);
71 return ++ManglingNumbers[Key->castAs<FunctionProtoType>()];
74 unsigned getManglingNumber(const BlockDecl *BD) override {
75 const Type *Ty = nullptr;
76 return ++ManglingNumbers[Ty];
79 unsigned getStaticLocalNumber(const VarDecl *VD) override {
83 /// Variable decls are numbered by identifier.
84 unsigned getManglingNumber(const VarDecl *VD, unsigned) override {
85 const IdentifierInfo *Identifier = VD->getIdentifier();
87 // VarDecl without an identifier represents an anonymous union declaration.
88 Identifier = findAnonymousUnionVarDeclName(*VD);
90 return ++VarManglingNumbers[Identifier];
93 unsigned getManglingNumber(const TagDecl *TD, unsigned) override {
94 return ++TagManglingNumbers[TD->getIdentifier()];
98 class ItaniumCXXABI : public CXXABI {
102 ItaniumCXXABI(ASTContext &Ctx) : Context(Ctx) { }
105 getMemberPointerInfo(const MemberPointerType *MPT) const override {
106 const TargetInfo &Target = Context.getTargetInfo();
107 TargetInfo::IntType PtrDiff = Target.getPtrDiffType(0);
108 MemberPointerInfo MPI;
109 MPI.Width = Target.getTypeWidth(PtrDiff);
110 MPI.Align = Target.getTypeAlign(PtrDiff);
111 MPI.HasPadding = false;
112 if (MPT->isMemberFunctionPointer())
117 CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
118 const llvm::Triple &T = Context.getTargetInfo().getTriple();
119 if (!isVariadic && T.isWindowsGNUEnvironment() &&
120 T.getArch() == llvm::Triple::x86)
121 return CC_X86ThisCall;
125 // We cheat and just check that the class has a vtable pointer, and that it's
126 // only big enough to have a vtable pointer and nothing more (or less).
127 bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
129 // Check that the class has a vtable pointer.
130 if (!RD->isDynamicClass())
133 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
134 CharUnits PointerSize =
135 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
136 return Layout.getNonVirtualSize() == PointerSize;
139 const CXXConstructorDecl *
140 getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
144 void addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
145 CXXConstructorDecl *CD) override {}
147 void addTypedefNameForUnnamedTagDecl(TagDecl *TD,
148 TypedefNameDecl *DD) override {}
150 TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override {
154 void addDeclaratorForUnnamedTagDecl(TagDecl *TD,
155 DeclaratorDecl *DD) override {}
157 DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override {
161 std::unique_ptr<MangleNumberingContext>
162 createMangleNumberingContext() const override {
163 return llvm::make_unique<ItaniumNumberingContext>();
168 CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) {
169 return new ItaniumCXXABI(Ctx);