MicrosoftCXXABI.cpp 8.6 KB

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