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" |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame^] | 19 | #include "clang/AST/MangleNumberingContext.h" |
Anders Carlsson | dae0cb5 | 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 | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | namespace { |
Reid Kleckner | 942f9fe | 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 { |
| 31 | unsigned NumStaticLocals; |
| 32 | |
| 33 | public: |
| 34 | MicrosoftNumberingContext() : NumStaticLocals(0) { } |
| 35 | |
| 36 | /// Static locals are numbered by source order. |
| 37 | virtual unsigned getManglingNumber(const VarDecl *VD) { |
| 38 | assert(VD->isStaticLocal()); |
| 39 | return ++NumStaticLocals; |
| 40 | } |
| 41 | }; |
| 42 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 43 | class MicrosoftCXXABI : public CXXABI { |
| 44 | ASTContext &Context; |
| 45 | public: |
| 46 | MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) { } |
| 47 | |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 48 | std::pair<uint64_t, unsigned> |
| 49 | getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const; |
Charles Davis | 424ae98 | 2010-10-29 03:25:11 +0000 | [diff] [blame] | 50 | |
Timur Iskhodzhanov | 8f88a1d | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 51 | CallingConv getDefaultMethodCallConv(bool isVariadic) const { |
Benjamin Kramer | a83297b | 2013-04-04 17:07:04 +0000 | [diff] [blame] | 52 | if (!isVariadic && |
| 53 | Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86) |
Charles Davis | 424ae98 | 2010-10-29 03:25:11 +0000 | [diff] [blame] | 54 | return CC_X86ThisCall; |
Benjamin Kramer | a83297b | 2013-04-04 17:07:04 +0000 | [diff] [blame] | 55 | return CC_C; |
Charles Davis | 424ae98 | 2010-10-29 03:25:11 +0000 | [diff] [blame] | 56 | } |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 57 | |
| 58 | bool isNearlyEmpty(const CXXRecordDecl *RD) const { |
| 59 | // FIXME: Audit the corners |
| 60 | if (!RD->isDynamicClass()) |
| 61 | return false; |
| 62 | |
| 63 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 64 | |
| 65 | // In the Microsoft ABI, classes can have one or two vtable pointers. |
Ken Dyck | 5c3633f | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 66 | CharUnits PointerSize = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 67 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Ken Dyck | 5c3633f | 2011-02-01 01:52:10 +0000 | [diff] [blame] | 68 | return Layout.getNonVirtualSize() == PointerSize || |
| 69 | Layout.getNonVirtualSize() == PointerSize * 2; |
Anders Carlsson | dae0cb5 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 70 | } |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame^] | 71 | |
| 72 | MangleNumberingContext *createMangleNumberingContext() const { |
| 73 | return new MicrosoftNumberingContext(); |
| 74 | } |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 75 | }; |
| 76 | } |
| 77 | |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 78 | // getNumBases() seems to only give us the number of direct bases, and not the |
| 79 | // total. This function tells us if we inherit from anybody that uses MI, or if |
| 80 | // we have a non-primary base class, which uses the multiple inheritance model. |
Benjamin Kramer | a83297b | 2013-04-04 17:07:04 +0000 | [diff] [blame] | 81 | static bool usesMultipleInheritanceModel(const CXXRecordDecl *RD) { |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 82 | while (RD->getNumBases() > 0) { |
| 83 | if (RD->getNumBases() > 1) |
| 84 | return true; |
| 85 | assert(RD->getNumBases() == 1); |
Benjamin Kramer | a83297b | 2013-04-04 17:07:04 +0000 | [diff] [blame] | 86 | const CXXRecordDecl *Base = |
| 87 | RD->bases_begin()->getType()->getAsCXXRecordDecl(); |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 88 | if (RD->isPolymorphic() && !Base->isPolymorphic()) |
| 89 | return true; |
| 90 | RD = Base; |
| 91 | } |
| 92 | return false; |
| 93 | } |
| 94 | |
Benjamin Kramer | a83297b | 2013-04-04 17:07:04 +0000 | [diff] [blame] | 95 | static MSInheritanceModel MSInheritanceAttrToModel(attr::Kind Kind) { |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 96 | switch (Kind) { |
| 97 | default: llvm_unreachable("expected MS inheritance attribute"); |
Reid Kleckner | cb428a1 | 2013-04-02 17:40:19 +0000 | [diff] [blame] | 98 | case attr::SingleInheritance: return MSIM_Single; |
| 99 | case attr::MultipleInheritance: return MSIM_Multiple; |
| 100 | case attr::VirtualInheritance: return MSIM_Virtual; |
| 101 | case attr::UnspecifiedInheritance: return MSIM_Unspecified; |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 102 | } |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 103 | } |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 104 | |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 105 | MSInheritanceModel CXXRecordDecl::getMSInheritanceModel() const { |
Benjamin Kramer | a83297b | 2013-04-04 17:07:04 +0000 | [diff] [blame] | 106 | if (Attr *IA = this->getAttr<MSInheritanceAttr>()) |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 107 | return MSInheritanceAttrToModel(IA->getKind()); |
| 108 | // If there was no explicit attribute, the record must be defined already, and |
| 109 | // we can figure out the inheritance model from its other properties. |
| 110 | if (this->getNumVBases() > 0) |
Reid Kleckner | cb428a1 | 2013-04-02 17:40:19 +0000 | [diff] [blame] | 111 | return MSIM_Virtual; |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 112 | if (usesMultipleInheritanceModel(this)) |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 113 | return this->isPolymorphic() ? MSIM_MultiplePolymorphic : MSIM_Multiple; |
| 114 | return this->isPolymorphic() ? MSIM_SinglePolymorphic : MSIM_Single; |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // Returns the number of pointer and integer slots used to represent a member |
| 118 | // pointer in the MS C++ ABI. |
| 119 | // |
| 120 | // Member function pointers have the following general form; however, fields |
| 121 | // are dropped as permitted (under the MSVC interpretation) by the inheritance |
| 122 | // model of the actual class. |
| 123 | // |
| 124 | // struct { |
| 125 | // // A pointer to the member function to call. If the member function is |
| 126 | // // virtual, this will be a thunk that forwards to the appropriate vftable |
| 127 | // // slot. |
| 128 | // void *FunctionPointerOrVirtualThunk; |
| 129 | // |
| 130 | // // An offset to add to the address of the vbtable pointer after (possibly) |
| 131 | // // selecting the virtual base but before resolving and calling the function. |
| 132 | // // Only needed if the class has any virtual bases or bases at a non-zero |
| 133 | // // offset. |
| 134 | // int NonVirtualBaseAdjustment; |
| 135 | // |
| 136 | // // An offset within the vb-table that selects the virtual base containing |
| 137 | // // the member. Loading from this offset produces a new offset that is |
| 138 | // // added to the address of the vb-table pointer to produce the base. |
| 139 | // int VirtualBaseAdjustmentOffset; |
| 140 | // |
| 141 | // // The offset of the vb-table pointer within the object. Only needed for |
| 142 | // // incomplete types. |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 143 | // int VBPtrOffset; |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 144 | // }; |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 145 | static std::pair<unsigned, unsigned> |
| 146 | getMSMemberPointerSlots(const MemberPointerType *MPT) { |
| 147 | const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 148 | MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); |
| 149 | unsigned Ptrs; |
| 150 | unsigned Ints = 0; |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 151 | if (MPT->isMemberFunctionPointer()) { |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 152 | // Member function pointers are a struct of a function pointer followed by a |
| 153 | // variable number of ints depending on the inheritance model used. The |
| 154 | // function pointer is a real function if it is non-virtual and a vftable |
| 155 | // slot thunk if it is virtual. The ints select the object base passed for |
| 156 | // the 'this' pointer. |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 157 | Ptrs = 1; // First slot is always a function pointer. |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 158 | switch (Inheritance) { |
Reid Kleckner | cb428a1 | 2013-04-02 17:40:19 +0000 | [diff] [blame] | 159 | case MSIM_Unspecified: ++Ints; // VBTableOffset |
| 160 | case MSIM_Virtual: ++Ints; // VirtualBaseAdjustmentOffset |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 161 | case MSIM_MultiplePolymorphic: |
Reid Kleckner | cb428a1 | 2013-04-02 17:40:19 +0000 | [diff] [blame] | 162 | case MSIM_Multiple: ++Ints; // NonVirtualBaseAdjustment |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 163 | case MSIM_SinglePolymorphic: |
Reid Kleckner | cb428a1 | 2013-04-02 17:40:19 +0000 | [diff] [blame] | 164 | case MSIM_Single: break; // Nothing |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 165 | } |
| 166 | } else { |
| 167 | // Data pointers are an aggregate of ints. The first int is an offset |
| 168 | // followed by vbtable-related offsets. |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 169 | Ptrs = 0; |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 170 | switch (Inheritance) { |
Reid Kleckner | cb428a1 | 2013-04-02 17:40:19 +0000 | [diff] [blame] | 171 | case MSIM_Unspecified: ++Ints; // VBTableOffset |
| 172 | case MSIM_Virtual: ++Ints; // VirtualBaseAdjustmentOffset |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 173 | case MSIM_MultiplePolymorphic: |
Reid Kleckner | cb428a1 | 2013-04-02 17:40:19 +0000 | [diff] [blame] | 174 | case MSIM_Multiple: // Nothing |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 175 | case MSIM_SinglePolymorphic: |
Reid Kleckner | cb428a1 | 2013-04-02 17:40:19 +0000 | [diff] [blame] | 176 | case MSIM_Single: ++Ints; // Field offset |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 179 | return std::make_pair(Ptrs, Ints); |
| 180 | } |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 181 | |
Benjamin Kramer | a83297b | 2013-04-04 17:07:04 +0000 | [diff] [blame] | 182 | std::pair<uint64_t, unsigned> MicrosoftCXXABI::getMemberPointerWidthAndAlign( |
| 183 | const MemberPointerType *MPT) const { |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 184 | const TargetInfo &Target = Context.getTargetInfo(); |
| 185 | assert(Target.getTriple().getArch() == llvm::Triple::x86 || |
| 186 | Target.getTriple().getArch() == llvm::Triple::x86_64); |
| 187 | unsigned Ptrs, Ints; |
Reid Kleckner | a3609b0 | 2013-04-11 18:13:19 +0000 | [diff] [blame] | 188 | llvm::tie(Ptrs, Ints) = getMSMemberPointerSlots(MPT); |
Reid Kleckner | 4410489 | 2013-04-02 16:23:57 +0000 | [diff] [blame] | 189 | // The nominal struct is laid out with pointers followed by ints and aligned |
| 190 | // to a pointer width if any are present and an int width otherwise. |
| 191 | unsigned PtrSize = Target.getPointerWidth(0); |
| 192 | unsigned IntSize = Target.getIntWidth(); |
| 193 | uint64_t Width = Ptrs * PtrSize + Ints * IntSize; |
| 194 | unsigned Align = Ptrs > 0 ? Target.getPointerAlign(0) : Target.getIntAlign(); |
| 195 | Width = llvm::RoundUpToAlignment(Width, Align); |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 196 | return std::make_pair(Width, Align); |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | CXXABI *clang::CreateMicrosoftCXXABI(ASTContext &Ctx) { |
| 200 | return new MicrosoftCXXABI(Ctx); |
| 201 | } |
| 202 | |