Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1 | //===------- MicrosoftCXXABI.cpp - AST support for the Microsoft 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 | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides C++ AST support targeting the Microsoft Visual C++ |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 11 | // ABI. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "CXXABI.h" |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame^] | 16 | #include "clang/AST/Attr.h" |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecordLayout.h" |
| 20 | #include "clang/AST/Type.h" |
| 21 | #include "clang/Basic/TargetInfo.h" |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | namespace { |
| 26 | class MicrosoftCXXABI : public CXXABI { |
| 27 | ASTContext &Context; |
| 28 | public: |
| 29 | MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) { } |
| 30 | |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame^] | 31 | std::pair<uint64_t, unsigned> |
| 32 | getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const; |
Charles Davis | 424ae98 | 2010-10-29 03:25:11 +0000 | [diff] [blame] | 33 | |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 34 | CallingConv getDefaultMethodCallConv(bool isVariadic) const { |
| 35 | if (!isVariadic && Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86) |
Charles Davis | 424ae98 | 2010-10-29 03:25:11 +0000 | [diff] [blame] | 36 | return CC_X86ThisCall; |
| 37 | else |
| 38 | return CC_C; |
| 39 | } |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 40 | |
| 41 | bool isNearlyEmpty(const CXXRecordDecl *RD) const { |
| 42 | // FIXME: Audit the corners |
| 43 | if (!RD->isDynamicClass()) |
| 44 | return false; |
| 45 | |
| 46 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 47 | |
| 48 | // In the Microsoft ABI, classes can have one or two vtable pointers. |
Ken Dyck | 5c3633f | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 49 | CharUnits PointerSize = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 50 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Ken Dyck | 5c3633f | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 51 | return Layout.getNonVirtualSize() == PointerSize || |
| 52 | Layout.getNonVirtualSize() == PointerSize * 2; |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 53 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 54 | }; |
| 55 | } |
| 56 | |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame^] | 57 | // getNumBases() seems to only give us the number of direct bases, and not the |
| 58 | // total. This function tells us if we inherit from anybody that uses MI, or if |
| 59 | // we have a non-primary base class, which uses the multiple inheritance model. |
| 60 | static bool usesMultipleInheritanceModel(const CXXRecordDecl *RD) { |
| 61 | while (RD->getNumBases() > 0) { |
| 62 | if (RD->getNumBases() > 1) |
| 63 | return true; |
| 64 | assert(RD->getNumBases() == 1); |
| 65 | const CXXRecordDecl *Base = RD->bases_begin()->getType()->getAsCXXRecordDecl(); |
| 66 | if (RD->isPolymorphic() && !Base->isPolymorphic()) |
| 67 | return true; |
| 68 | RD = Base; |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | std::pair<uint64_t, unsigned> |
| 74 | MicrosoftCXXABI::getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const { |
| 75 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
| 76 | const TargetInfo &Target = Context.getTargetInfo(); |
| 77 | |
| 78 | assert(Target.getTriple().getArch() == llvm::Triple::x86 || |
| 79 | Target.getTriple().getArch() == llvm::Triple::x86_64); |
| 80 | |
| 81 | Attr *IA = RD->getAttr<MSInheritanceAttr>(); |
| 82 | attr::Kind Inheritance; |
| 83 | if (IA) { |
| 84 | Inheritance = IA->getKind(); |
| 85 | } else if (RD->getNumVBases() > 0) { |
| 86 | Inheritance = attr::VirtualInheritance; |
| 87 | } else if (MPT->getPointeeType()->isFunctionType() && |
| 88 | usesMultipleInheritanceModel(RD)) { |
| 89 | Inheritance = attr::MultipleInheritance; |
| 90 | } else { |
| 91 | Inheritance = attr::SingleInheritance; |
| 92 | } |
| 93 | |
| 94 | unsigned PtrSize = Target.getPointerWidth(0); |
| 95 | unsigned IntSize = Target.getIntWidth(); |
| 96 | uint64_t Width; |
| 97 | unsigned Align; |
| 98 | if (MPT->getPointeeType()->isFunctionType()) { |
| 99 | // Member function pointers are a struct of a function pointer followed by a |
| 100 | // variable number of ints depending on the inheritance model used. The |
| 101 | // function pointer is a real function if it is non-virtual and a vftable |
| 102 | // slot thunk if it is virtual. The ints select the object base passed for |
| 103 | // the 'this' pointer. |
| 104 | Align = Target.getPointerAlign(0); |
| 105 | switch (Inheritance) { |
| 106 | case attr::SingleInheritance: Width = PtrSize; break; |
| 107 | case attr::MultipleInheritance: Width = PtrSize + 1 * IntSize; break; |
| 108 | case attr::VirtualInheritance: Width = PtrSize + 2 * IntSize; break; |
| 109 | case attr::UnspecifiedInheritance: Width = PtrSize + 3 * IntSize; break; |
| 110 | default: llvm_unreachable("unknown inheritance model"); |
| 111 | } |
| 112 | } else { |
| 113 | // Data pointers are an aggregate of ints. The first int is an offset |
| 114 | // followed by vbtable-related offsets. |
| 115 | Align = Target.getIntAlign(); |
| 116 | switch (Inheritance) { |
| 117 | case attr::SingleInheritance: // Same as multiple inheritance. |
| 118 | case attr::MultipleInheritance: Width = 1 * IntSize; break; |
| 119 | case attr::VirtualInheritance: Width = 2 * IntSize; break; |
| 120 | case attr::UnspecifiedInheritance: Width = 3 * IntSize; break; |
| 121 | default: llvm_unreachable("unknown inheritance model"); |
| 122 | } |
| 123 | } |
| 124 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 125 | |
| 126 | // FIXME: Verify that our alignment matches MSVC. |
| 127 | return std::make_pair(Width, Align); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | CXXABI *clang::CreateMicrosoftCXXABI(ASTContext &Ctx) { |
| 131 | return new MicrosoftCXXABI(Ctx); |
| 132 | } |
| 133 | |