blob: 8a2cc0fbee429f29f951f3e41526871d54ec9255 [file] [log] [blame]
Charles Davis53c59df2010-08-16 03:33:14 +00001//===------- ItaniumCXXABI.cpp - AST support for the Itanium C++ ABI ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner57540c52011-04-15 05:22:18 +000010// This provides C++ AST support targeting the Itanium C++ ABI, which is
Charles Davis53c59df2010-08-16 03:33:14 +000011// documented at:
12// http://www.codesourcery.com/public/cxx-abi/abi.html
13// http://www.codesourcery.com/public/cxx-abi/abi-eh.html
John McCall86353412010-08-21 22:46:04 +000014//
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
17//
Charles Davis53c59df2010-08-16 03:33:14 +000018//===----------------------------------------------------------------------===//
19
20#include "CXXABI.h"
21#include "clang/AST/ASTContext.h"
Anders Carlsson60a62632010-11-25 01:51:53 +000022#include "clang/AST/DeclCXX.h"
Reid Klecknerd8110b62013-09-10 20:14:30 +000023#include "clang/AST/MangleNumberingContext.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/AST/RecordLayout.h"
Charles Davis53c59df2010-08-16 03:33:14 +000025#include "clang/AST/Type.h"
Anders Carlsson60a62632010-11-25 01:51:53 +000026#include "clang/Basic/TargetInfo.h"
Charles Davis53c59df2010-08-16 03:33:14 +000027
28using namespace clang;
29
30namespace {
Reid Klecknerd8110b62013-09-10 20:14:30 +000031
Evgeny Astigeevich665027d2014-12-12 16:17:46 +000032/// 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.
40///
41/// Returns the name of anonymous union VarDecl or nullptr if it is not found.
42static 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();
48 }
49
50 return nullptr;
51}
52
Reid Klecknerd8110b62013-09-10 20:14:30 +000053/// \brief Keeps track of the mangled names of lambda expressions and block
54/// literals within a particular context.
55class ItaniumNumberingContext : public MangleNumberingContext {
David Majnemer118da502014-08-22 04:22:50 +000056 llvm::DenseMap<const Type *, unsigned> ManglingNumbers;
Evgeny Astigeevich665027d2014-12-12 16:17:46 +000057 llvm::DenseMap<const IdentifierInfo *, unsigned> VarManglingNumbers;
58 llvm::DenseMap<const IdentifierInfo *, unsigned> TagManglingNumbers;
Reid Klecknerd8110b62013-09-10 20:14:30 +000059
60public:
David Majnemer118da502014-08-22 04:22:50 +000061 unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
62 const FunctionProtoType *Proto =
63 CallOperator->getType()->getAs<FunctionProtoType>();
64 ASTContext &Context = CallOperator->getASTContext();
65
66 QualType Key =
67 Context.getFunctionType(Context.VoidTy, Proto->getParamTypes(),
68 FunctionProtoType::ExtProtoInfo());
69 Key = Context.getCanonicalType(Key);
70 return ++ManglingNumbers[Key->castAs<FunctionProtoType>()];
71 }
72
Fariborz Jahanian5afc8692014-10-01 16:56:40 +000073 unsigned getManglingNumber(const BlockDecl *BD) override {
David Majnemer118da502014-08-22 04:22:50 +000074 const Type *Ty = nullptr;
75 return ++ManglingNumbers[Ty];
76 }
77
78 unsigned getStaticLocalNumber(const VarDecl *VD) override {
79 return 0;
80 }
81
Reid Klecknerd8110b62013-09-10 20:14:30 +000082 /// Variable decls are numbered by identifier.
Craig Toppercbce6e92014-03-11 06:22:39 +000083 unsigned getManglingNumber(const VarDecl *VD, unsigned) override {
Evgeny Astigeevich665027d2014-12-12 16:17:46 +000084 const IdentifierInfo *Identifier = VD->getIdentifier();
85 if (!Identifier) {
86 // VarDecl without an identifier represents an anonymous union declaration.
87 Identifier = findAnonymousUnionVarDeclName(*VD);
88 }
89 return ++VarManglingNumbers[Identifier];
Reid Klecknerd8110b62013-09-10 20:14:30 +000090 }
David Majnemer2206bf52014-03-05 08:57:59 +000091
Craig Toppercbce6e92014-03-11 06:22:39 +000092 unsigned getManglingNumber(const TagDecl *TD, unsigned) override {
David Majnemer2206bf52014-03-05 08:57:59 +000093 return ++TagManglingNumbers[TD->getIdentifier()];
94 }
Reid Klecknerd8110b62013-09-10 20:14:30 +000095};
96
Charles Davis53c59df2010-08-16 03:33:14 +000097class ItaniumCXXABI : public CXXABI {
John McCall86353412010-08-21 22:46:04 +000098protected:
Charles Davis53c59df2010-08-16 03:33:14 +000099 ASTContext &Context;
100public:
101 ItaniumCXXABI(ASTContext &Ctx) : Context(Ctx) { }
102
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000103 std::pair<uint64_t, unsigned>
Craig Toppercbce6e92014-03-11 06:22:39 +0000104 getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const override {
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000105 const TargetInfo &Target = Context.getTargetInfo();
106 TargetInfo::IntType PtrDiff = Target.getPtrDiffType(0);
107 uint64_t Width = Target.getTypeWidth(PtrDiff);
108 unsigned Align = Target.getTypeAlign(PtrDiff);
David Majnemer5fd33e02015-04-24 01:25:08 +0000109 if (MPT->isMemberFunctionPointer())
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000110 Width = 2 * Width;
111 return std::make_pair(Width, Align);
Charles Davis53c59df2010-08-16 03:33:14 +0000112 }
Charles Davis31575f72010-10-29 03:25:11 +0000113
Craig Toppercbce6e92014-03-11 06:22:39 +0000114 CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
Rafael Espindola34970692013-12-12 16:07:11 +0000115 const llvm::Triple &T = Context.getTargetInfo().getTriple();
Saleem Abdulrasool377066a2014-03-27 22:50:18 +0000116 if (!isVariadic && T.isWindowsGNUEnvironment() &&
Rafael Espindola34970692013-12-12 16:07:11 +0000117 T.getArch() == llvm::Triple::x86)
118 return CC_X86ThisCall;
Charles Davis31575f72010-10-29 03:25:11 +0000119 return CC_C;
120 }
Anders Carlsson60a62632010-11-25 01:51:53 +0000121
122 // We cheat and just check that the class has a vtable pointer, and that it's
123 // only big enough to have a vtable pointer and nothing more (or less).
Craig Toppercbce6e92014-03-11 06:22:39 +0000124 bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
Anders Carlsson60a62632010-11-25 01:51:53 +0000125
126 // Check that the class has a vtable pointer.
127 if (!RD->isDynamicClass())
128 return false;
129
130 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Ken Dyck316d6f62011-02-01 01:52:10 +0000131 CharUnits PointerSize =
Douglas Gregore8bbc122011-09-02 00:18:52 +0000132 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Ken Dyck316d6f62011-02-01 01:52:10 +0000133 return Layout.getNonVirtualSize() == PointerSize;
Anders Carlsson60a62632010-11-25 01:51:53 +0000134 }
Reid Klecknerd8110b62013-09-10 20:14:30 +0000135
David Majnemere7a818f2015-03-06 18:53:55 +0000136 const CXXConstructorDecl *
137 getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
138 return nullptr;
139 }
140
141 void addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
142 CXXConstructorDecl *CD) override {}
143
David Majnemerdfa6d202015-03-11 18:36:39 +0000144 void addDefaultArgExprForConstructor(const CXXConstructorDecl *CD,
145 unsigned ParmIdx, Expr *DAE) override {}
146
147 Expr *getDefaultArgExprForConstructor(const CXXConstructorDecl *CD,
148 unsigned ParmIdx) override {
149 return nullptr;
150 }
151
David Majnemer00350522015-08-31 18:48:39 +0000152 void addTypedefNameForUnnamedTagDecl(TagDecl *TD,
153 TypedefNameDecl *DD) override {}
154
155 TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override {
156 return nullptr;
157 }
158
159 void addDeclaratorForUnnamedTagDecl(TagDecl *TD,
160 DeclaratorDecl *DD) override {}
161
162 DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override {
163 return nullptr;
164 }
165
Craig Toppercbce6e92014-03-11 06:22:39 +0000166 MangleNumberingContext *createMangleNumberingContext() const override {
Reid Klecknerd8110b62013-09-10 20:14:30 +0000167 return new ItaniumNumberingContext();
168 }
Charles Davis53c59df2010-08-16 03:33:14 +0000169};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000170}
Charles Davis53c59df2010-08-16 03:33:14 +0000171
172CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) {
173 return new ItaniumCXXABI(Ctx);
174}