Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1 | //===------- 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 Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides C++ AST support targeting the Itanium C++ ABI, which is |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 11 | // documented at: |
| 12 | // http://www.codesourcery.com/public/cxx-abi/abi.html |
| 13 | // http://www.codesourcery.com/public/cxx-abi/abi-eh.html |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 14 | // |
| 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 Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #include "CXXABI.h" |
| 21 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 23 | #include "clang/AST/MangleNumberingContext.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 25 | #include "clang/AST/Type.h" |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 26 | #include "clang/Basic/TargetInfo.h" |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | |
| 30 | namespace { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 31 | |
Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 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. |
| 40 | /// |
| 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(); |
| 48 | } |
| 49 | |
| 50 | return nullptr; |
| 51 | } |
| 52 | |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 53 | /// \brief Keeps track of the mangled names of lambda expressions and block |
| 54 | /// literals within a particular context. |
| 55 | class ItaniumNumberingContext : public MangleNumberingContext { |
David Majnemer | 118da50 | 2014-08-22 04:22:50 +0000 | [diff] [blame] | 56 | llvm::DenseMap<const Type *, unsigned> ManglingNumbers; |
Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 57 | llvm::DenseMap<const IdentifierInfo *, unsigned> VarManglingNumbers; |
| 58 | llvm::DenseMap<const IdentifierInfo *, unsigned> TagManglingNumbers; |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 59 | |
| 60 | public: |
David Majnemer | 118da50 | 2014-08-22 04:22:50 +0000 | [diff] [blame] | 61 | unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override { |
| 62 | const FunctionProtoType *Proto = |
| 63 | CallOperator->getType()->getAs<FunctionProtoType>(); |
| 64 | ASTContext &Context = CallOperator->getASTContext(); |
| 65 | |
Richard Smith | c7f576f | 2016-10-23 04:53:03 +0000 | [diff] [blame] | 66 | FunctionProtoType::ExtProtoInfo EPI; |
| 67 | EPI.Variadic = Proto->isVariadic(); |
David Majnemer | 118da50 | 2014-08-22 04:22:50 +0000 | [diff] [blame] | 68 | QualType Key = |
Richard Smith | c7f576f | 2016-10-23 04:53:03 +0000 | [diff] [blame] | 69 | Context.getFunctionType(Context.VoidTy, Proto->getParamTypes(), EPI); |
David Majnemer | 118da50 | 2014-08-22 04:22:50 +0000 | [diff] [blame] | 70 | Key = Context.getCanonicalType(Key); |
| 71 | return ++ManglingNumbers[Key->castAs<FunctionProtoType>()]; |
| 72 | } |
| 73 | |
Fariborz Jahanian | 5afc869 | 2014-10-01 16:56:40 +0000 | [diff] [blame] | 74 | unsigned getManglingNumber(const BlockDecl *BD) override { |
David Majnemer | 118da50 | 2014-08-22 04:22:50 +0000 | [diff] [blame] | 75 | const Type *Ty = nullptr; |
| 76 | return ++ManglingNumbers[Ty]; |
| 77 | } |
| 78 | |
| 79 | unsigned getStaticLocalNumber(const VarDecl *VD) override { |
| 80 | return 0; |
| 81 | } |
| 82 | |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 83 | /// Variable decls are numbered by identifier. |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 84 | unsigned getManglingNumber(const VarDecl *VD, unsigned) override { |
Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 85 | const IdentifierInfo *Identifier = VD->getIdentifier(); |
| 86 | if (!Identifier) { |
| 87 | // VarDecl without an identifier represents an anonymous union declaration. |
| 88 | Identifier = findAnonymousUnionVarDeclName(*VD); |
| 89 | } |
| 90 | return ++VarManglingNumbers[Identifier]; |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 91 | } |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 92 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 93 | unsigned getManglingNumber(const TagDecl *TD, unsigned) override { |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 94 | return ++TagManglingNumbers[TD->getIdentifier()]; |
| 95 | } |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 98 | class ItaniumCXXABI : public CXXABI { |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 99 | protected: |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 100 | ASTContext &Context; |
| 101 | public: |
| 102 | ItaniumCXXABI(ASTContext &Ctx) : Context(Ctx) { } |
| 103 | |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 104 | MemberPointerInfo |
| 105 | getMemberPointerInfo(const MemberPointerType *MPT) const override { |
Reid Kleckner | 3a52abf | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 106 | const TargetInfo &Target = Context.getTargetInfo(); |
| 107 | TargetInfo::IntType PtrDiff = Target.getPtrDiffType(0); |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 108 | MemberPointerInfo MPI; |
| 109 | MPI.Width = Target.getTypeWidth(PtrDiff); |
| 110 | MPI.Align = Target.getTypeAlign(PtrDiff); |
| 111 | MPI.HasPadding = false; |
David Majnemer | 5fd33e0 | 2015-04-24 01:25:08 +0000 | [diff] [blame] | 112 | if (MPT->isMemberFunctionPointer()) |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 113 | MPI.Width *= 2; |
| 114 | return MPI; |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 115 | } |
Charles Davis | 31575f7 | 2010-10-29 03:25:11 +0000 | [diff] [blame] | 116 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 117 | CallingConv getDefaultMethodCallConv(bool isVariadic) const override { |
Rafael Espindola | 3497069 | 2013-12-12 16:07:11 +0000 | [diff] [blame] | 118 | const llvm::Triple &T = Context.getTargetInfo().getTriple(); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 119 | if (!isVariadic && T.isWindowsGNUEnvironment() && |
Rafael Espindola | 3497069 | 2013-12-12 16:07:11 +0000 | [diff] [blame] | 120 | T.getArch() == llvm::Triple::x86) |
| 121 | return CC_X86ThisCall; |
Charles Davis | 31575f7 | 2010-10-29 03:25:11 +0000 | [diff] [blame] | 122 | return CC_C; |
| 123 | } |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 124 | |
| 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). |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 127 | bool isNearlyEmpty(const CXXRecordDecl *RD) const override { |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 128 | |
| 129 | // Check that the class has a vtable pointer. |
| 130 | if (!RD->isDynamicClass()) |
| 131 | return false; |
| 132 | |
| 133 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Ken Dyck | 316d6f6 | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 134 | CharUnits PointerSize = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 135 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Ken Dyck | 316d6f6 | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 136 | return Layout.getNonVirtualSize() == PointerSize; |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 137 | } |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 138 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 139 | const CXXConstructorDecl * |
| 140 | getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override { |
| 141 | return nullptr; |
| 142 | } |
| 143 | |
| 144 | void addCopyConstructorForExceptionObject(CXXRecordDecl *RD, |
| 145 | CXXConstructorDecl *CD) override {} |
| 146 | |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 147 | void addTypedefNameForUnnamedTagDecl(TagDecl *TD, |
| 148 | TypedefNameDecl *DD) override {} |
| 149 | |
| 150 | TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override { |
| 151 | return nullptr; |
| 152 | } |
| 153 | |
| 154 | void addDeclaratorForUnnamedTagDecl(TagDecl *TD, |
| 155 | DeclaratorDecl *DD) override {} |
| 156 | |
| 157 | DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override { |
| 158 | return nullptr; |
| 159 | } |
| 160 | |
Justin Lebar | 20ebffc | 2016-10-10 16:26:19 +0000 | [diff] [blame] | 161 | std::unique_ptr<MangleNumberingContext> |
| 162 | createMangleNumberingContext() const override { |
| 163 | return llvm::make_unique<ItaniumNumberingContext>(); |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 164 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 165 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 166 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 167 | |
| 168 | CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) { |
| 169 | return new ItaniumCXXABI(Ctx); |
| 170 | } |