Charles Davis | 53c59df | 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 | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides C++ AST support targeting the Microsoft Visual C++ |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 11 | // ABI. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "CXXABI.h" |
| 16 | #include "clang/AST/ASTContext.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 17 | #include "clang/AST/Attr.h" |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 19 | #include "clang/AST/MangleNumberingContext.h" |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
| 21 | #include "clang/AST/Type.h" |
| 22 | #include "clang/Basic/TargetInfo.h" |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | namespace { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 27 | |
| 28 | /// \brief Numbers things which need to correspond across multiple TUs. |
| 29 | /// Typically these are things like static locals, lambdas, or blocks. |
| 30 | class MicrosoftNumberingContext : public MangleNumberingContext { |
David Majnemer | 118da50 | 2014-08-22 04:22:50 +0000 | [diff] [blame] | 31 | llvm::DenseMap<const Type *, unsigned> ManglingNumbers; |
| 32 | unsigned LambdaManglingNumber; |
| 33 | unsigned StaticLocalNumber; |
| 34 | |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 35 | public: |
David Majnemer | 118da50 | 2014-08-22 04:22:50 +0000 | [diff] [blame] | 36 | MicrosoftNumberingContext() |
| 37 | : MangleNumberingContext(), LambdaManglingNumber(0), |
| 38 | StaticLocalNumber(0) {} |
| 39 | |
| 40 | unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override { |
| 41 | return ++LambdaManglingNumber; |
| 42 | } |
| 43 | |
Fariborz Jahanian | 5afc869 | 2014-10-01 16:56:40 +0000 | [diff] [blame^] | 44 | unsigned getManglingNumber(const BlockDecl *BD) override { |
David Majnemer | 118da50 | 2014-08-22 04:22:50 +0000 | [diff] [blame] | 45 | const Type *Ty = nullptr; |
| 46 | return ++ManglingNumbers[Ty]; |
| 47 | } |
| 48 | |
| 49 | unsigned getStaticLocalNumber(const VarDecl *VD) override { |
| 50 | return ++StaticLocalNumber; |
| 51 | } |
| 52 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 53 | unsigned getManglingNumber(const VarDecl *VD, |
| 54 | unsigned MSLocalManglingNumber) override { |
David Majnemer | f27217f | 2014-03-05 18:55:38 +0000 | [diff] [blame] | 55 | return MSLocalManglingNumber; |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 56 | } |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 57 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 58 | unsigned getManglingNumber(const TagDecl *TD, |
| 59 | unsigned MSLocalManglingNumber) override { |
David Majnemer | f27217f | 2014-03-05 18:55:38 +0000 | [diff] [blame] | 60 | return MSLocalManglingNumber; |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 61 | } |
| 62 | }; |
| 63 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 64 | class MicrosoftCXXABI : public CXXABI { |
| 65 | ASTContext &Context; |
| 66 | public: |
| 67 | MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) { } |
| 68 | |
Reid Kleckner | 3a52abf | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 69 | std::pair<uint64_t, unsigned> |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 70 | getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const override; |
Charles Davis | 31575f7 | 2010-10-29 03:25:11 +0000 | [diff] [blame] | 71 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 72 | CallingConv getDefaultMethodCallConv(bool isVariadic) const override { |
Benjamin Kramer | 08db461 | 2013-04-04 17:07:04 +0000 | [diff] [blame] | 73 | if (!isVariadic && |
| 74 | Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86) |
Charles Davis | 31575f7 | 2010-10-29 03:25:11 +0000 | [diff] [blame] | 75 | return CC_X86ThisCall; |
Benjamin Kramer | 08db461 | 2013-04-04 17:07:04 +0000 | [diff] [blame] | 76 | return CC_C; |
Charles Davis | 31575f7 | 2010-10-29 03:25:11 +0000 | [diff] [blame] | 77 | } |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 78 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 79 | bool isNearlyEmpty(const CXXRecordDecl *RD) const override { |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 80 | // FIXME: Audit the corners |
| 81 | if (!RD->isDynamicClass()) |
| 82 | return false; |
| 83 | |
| 84 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 85 | |
| 86 | // In the Microsoft ABI, classes can have one or two vtable pointers. |
Ken Dyck | 316d6f6 | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 87 | CharUnits PointerSize = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 88 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Ken Dyck | 316d6f6 | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 89 | return Layout.getNonVirtualSize() == PointerSize || |
| 90 | Layout.getNonVirtualSize() == PointerSize * 2; |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 91 | } |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 92 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 93 | MangleNumberingContext *createMangleNumberingContext() const override { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 94 | return new MicrosoftNumberingContext(); |
| 95 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 96 | }; |
| 97 | } |
| 98 | |
Reid Kleckner | 3a52abf | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 99 | // getNumBases() seems to only give us the number of direct bases, and not the |
| 100 | // total. This function tells us if we inherit from anybody that uses MI, or if |
| 101 | // we have a non-primary base class, which uses the multiple inheritance model. |
Benjamin Kramer | 08db461 | 2013-04-04 17:07:04 +0000 | [diff] [blame] | 102 | static bool usesMultipleInheritanceModel(const CXXRecordDecl *RD) { |
Reid Kleckner | 3a52abf | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 103 | while (RD->getNumBases() > 0) { |
| 104 | if (RD->getNumBases() > 1) |
| 105 | return true; |
| 106 | assert(RD->getNumBases() == 1); |
Benjamin Kramer | 08db461 | 2013-04-04 17:07:04 +0000 | [diff] [blame] | 107 | const CXXRecordDecl *Base = |
| 108 | RD->bases_begin()->getType()->getAsCXXRecordDecl(); |
Reid Kleckner | 3a52abf | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 109 | if (RD->isPolymorphic() && !Base->isPolymorphic()) |
| 110 | return true; |
| 111 | RD = Base; |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 116 | MSInheritanceAttr::Spelling CXXRecordDecl::calculateInheritanceModel() const { |
David Majnemer | 5ef4fe7 | 2014-06-13 06:43:46 +0000 | [diff] [blame] | 117 | if (!hasDefinition() || isParsingBaseSpecifiers()) |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 118 | return MSInheritanceAttr::Keyword_unspecified_inheritance; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 119 | if (getNumVBases() > 0) |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 120 | return MSInheritanceAttr::Keyword_virtual_inheritance; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 121 | if (usesMultipleInheritanceModel(this)) |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 122 | return MSInheritanceAttr::Keyword_multiple_inheritance; |
| 123 | return MSInheritanceAttr::Keyword_single_inheritance; |
| 124 | } |
| 125 | |
| 126 | MSInheritanceAttr::Spelling |
| 127 | CXXRecordDecl::getMSInheritanceModel() const { |
| 128 | MSInheritanceAttr *IA = getAttr<MSInheritanceAttr>(); |
| 129 | assert(IA && "Expected MSInheritanceAttr on the CXXRecordDecl!"); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 130 | return IA->getSemanticSpelling(); |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 133 | MSVtorDispAttr::Mode CXXRecordDecl::getMSVtorDispMode() const { |
| 134 | if (MSVtorDispAttr *VDA = getAttr<MSVtorDispAttr>()) |
| 135 | return VDA->getVtorDispMode(); |
| 136 | return MSVtorDispAttr::Mode(getASTContext().getLangOpts().VtorDispMode); |
| 137 | } |
| 138 | |
Reid Kleckner | be377cd | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 139 | // Returns the number of pointer and integer slots used to represent a member |
| 140 | // pointer in the MS C++ ABI. |
| 141 | // |
| 142 | // Member function pointers have the following general form; however, fields |
| 143 | // are dropped as permitted (under the MSVC interpretation) by the inheritance |
| 144 | // model of the actual class. |
| 145 | // |
| 146 | // struct { |
| 147 | // // A pointer to the member function to call. If the member function is |
| 148 | // // virtual, this will be a thunk that forwards to the appropriate vftable |
| 149 | // // slot. |
| 150 | // void *FunctionPointerOrVirtualThunk; |
| 151 | // |
| 152 | // // An offset to add to the address of the vbtable pointer after (possibly) |
| 153 | // // selecting the virtual base but before resolving and calling the function. |
| 154 | // // Only needed if the class has any virtual bases or bases at a non-zero |
| 155 | // // offset. |
| 156 | // int NonVirtualBaseAdjustment; |
| 157 | // |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 158 | // // The offset of the vb-table pointer within the object. Only needed for |
| 159 | // // incomplete types. |
| 160 | // int VBPtrOffset; |
| 161 | // |
Reid Kleckner | be377cd | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 162 | // // An offset within the vb-table that selects the virtual base containing |
| 163 | // // the member. Loading from this offset produces a new offset that is |
| 164 | // // added to the address of the vb-table pointer to produce the base. |
| 165 | // int VirtualBaseAdjustmentOffset; |
Reid Kleckner | be377cd | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 166 | // }; |
Reid Kleckner | 2341ae3 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 167 | static std::pair<unsigned, unsigned> |
| 168 | getMSMemberPointerSlots(const MemberPointerType *MPT) { |
David Majnemer | 1cdd96d | 2014-01-17 09:01:00 +0000 | [diff] [blame] | 169 | const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); |
| 170 | MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); |
| 171 | unsigned Ptrs = 0; |
Reid Kleckner | be377cd | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 172 | unsigned Ints = 0; |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 173 | if (MPT->isMemberFunctionPointer()) |
| 174 | Ptrs = 1; |
| 175 | else |
| 176 | Ints = 1; |
| 177 | if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(), |
| 178 | Inheritance)) |
| 179 | Ints++; |
| 180 | if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) |
| 181 | Ints++; |
| 182 | if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) |
| 183 | Ints++; |
Reid Kleckner | be377cd | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 184 | return std::make_pair(Ptrs, Ints); |
| 185 | } |
Reid Kleckner | 3a52abf | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 186 | |
Benjamin Kramer | 08db461 | 2013-04-04 17:07:04 +0000 | [diff] [blame] | 187 | std::pair<uint64_t, unsigned> MicrosoftCXXABI::getMemberPointerWidthAndAlign( |
| 188 | const MemberPointerType *MPT) const { |
Reid Kleckner | be377cd | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 189 | const TargetInfo &Target = Context.getTargetInfo(); |
| 190 | assert(Target.getTriple().getArch() == llvm::Triple::x86 || |
| 191 | Target.getTriple().getArch() == llvm::Triple::x86_64); |
| 192 | unsigned Ptrs, Ints; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 193 | std::tie(Ptrs, Ints) = getMSMemberPointerSlots(MPT); |
Reid Kleckner | be377cd | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 194 | // The nominal struct is laid out with pointers followed by ints and aligned |
| 195 | // to a pointer width if any are present and an int width otherwise. |
| 196 | unsigned PtrSize = Target.getPointerWidth(0); |
| 197 | unsigned IntSize = Target.getIntWidth(); |
| 198 | uint64_t Width = Ptrs * PtrSize + Ints * IntSize; |
Reid Kleckner | bd63b33 | 2014-01-31 22:28:50 +0000 | [diff] [blame] | 199 | unsigned Align; |
| 200 | |
| 201 | // When MSVC does x86_32 record layout, it aligns aggregate member pointers to |
| 202 | // 8 bytes. However, __alignof usually returns 4 for data memptrs and 8 for |
| 203 | // function memptrs. |
| 204 | if (Ptrs + Ints > 1 && Target.getTriple().getArch() == llvm::Triple::x86) |
| 205 | Align = 8 * 8; |
| 206 | else if (Ptrs) |
| 207 | Align = Target.getPointerAlign(0); |
| 208 | else |
| 209 | Align = Target.getIntAlign(); |
| 210 | |
| 211 | if (Target.getTriple().getArch() == llvm::Triple::x86_64) |
| 212 | Width = llvm::RoundUpToAlignment(Width, Align); |
Reid Kleckner | 3a52abf | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 213 | return std::make_pair(Width, Align); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | CXXABI *clang::CreateMicrosoftCXXABI(ASTContext &Ctx) { |
| 217 | return new MicrosoftCXXABI(Ctx); |
| 218 | } |
| 219 | |