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 | |
| 32 | /// \brief Keeps track of the mangled names of lambda expressions and block |
| 33 | /// literals within a particular context. |
| 34 | class ItaniumNumberingContext : public MangleNumberingContext { |
| 35 | llvm::DenseMap<IdentifierInfo*, unsigned> VarManglingNumbers; |
| 36 | |
| 37 | public: |
| 38 | /// Variable decls are numbered by identifier. |
| 39 | virtual unsigned getManglingNumber(const VarDecl *VD) { |
| 40 | return ++VarManglingNumbers[VD->getIdentifier()]; |
| 41 | } |
| 42 | }; |
| 43 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 44 | class ItaniumCXXABI : public CXXABI { |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 45 | protected: |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 46 | ASTContext &Context; |
| 47 | public: |
| 48 | ItaniumCXXABI(ASTContext &Ctx) : Context(Ctx) { } |
| 49 | |
Reid Kleckner | 3a52abf | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 50 | std::pair<uint64_t, unsigned> |
| 51 | getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const { |
| 52 | const TargetInfo &Target = Context.getTargetInfo(); |
| 53 | TargetInfo::IntType PtrDiff = Target.getPtrDiffType(0); |
| 54 | uint64_t Width = Target.getTypeWidth(PtrDiff); |
| 55 | unsigned Align = Target.getTypeAlign(PtrDiff); |
| 56 | if (MPT->getPointeeType()->isFunctionType()) |
| 57 | Width = 2 * Width; |
| 58 | return std::make_pair(Width, Align); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 59 | } |
Charles Davis | 31575f7 | 2010-10-29 03:25:11 +0000 | [diff] [blame] | 60 | |
Timur Iskhodzhanov | c5098ad | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 61 | CallingConv getDefaultMethodCallConv(bool isVariadic) const { |
Rafael Espindola | 3497069 | 2013-12-12 16:07:11 +0000 | [diff] [blame] | 62 | const llvm::Triple &T = Context.getTargetInfo().getTriple(); |
| 63 | if (!isVariadic && T.getOS() == llvm::Triple::MinGW32 && |
| 64 | T.getArch() == llvm::Triple::x86) |
| 65 | return CC_X86ThisCall; |
Charles Davis | 31575f7 | 2010-10-29 03:25:11 +0000 | [diff] [blame] | 66 | return CC_C; |
| 67 | } |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 68 | |
| 69 | // We cheat and just check that the class has a vtable pointer, and that it's |
| 70 | // only big enough to have a vtable pointer and nothing more (or less). |
| 71 | bool isNearlyEmpty(const CXXRecordDecl *RD) const { |
| 72 | |
| 73 | // Check that the class has a vtable pointer. |
| 74 | if (!RD->isDynamicClass()) |
| 75 | return false; |
| 76 | |
| 77 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Ken Dyck | 316d6f6 | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 78 | CharUnits PointerSize = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 79 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Ken Dyck | 316d6f6 | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 80 | return Layout.getNonVirtualSize() == PointerSize; |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 81 | } |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 82 | |
| 83 | virtual MangleNumberingContext *createMangleNumberingContext() const { |
| 84 | return new ItaniumNumberingContext(); |
| 85 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 86 | }; |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 87 | |
| 88 | class ARMCXXABI : public ItaniumCXXABI { |
| 89 | public: |
| 90 | ARMCXXABI(ASTContext &Ctx) : ItaniumCXXABI(Ctx) { } |
| 91 | }; |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | CXXABI *clang::CreateItaniumCXXABI(ASTContext &Ctx) { |
| 95 | return new ItaniumCXXABI(Ctx); |
| 96 | } |
| 97 | |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 98 | CXXABI *clang::CreateARMCXXABI(ASTContext &Ctx) { |
| 99 | return new ARMCXXABI(Ctx); |
| 100 | } |