1 //===------- MicrosoftCXXABI.cpp - AST support for the Microsoft C++ ABI --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This provides C++ AST support targeting the Microsoft Visual C++
13 //===----------------------------------------------------------------------===//
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/MangleNumberingContext.h"
20 #include "clang/AST/RecordLayout.h"
21 #include "clang/AST/Type.h"
22 #include "clang/Basic/TargetInfo.h"
24 using namespace clang;
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 {
32 unsigned getManglingNumber(const VarDecl *VD,
33 unsigned MSLocalManglingNumber) override {
34 return MSLocalManglingNumber;
37 unsigned getManglingNumber(const TagDecl *TD,
38 unsigned MSLocalManglingNumber) override {
39 return MSLocalManglingNumber;
43 class MicrosoftCXXABI : public CXXABI {
46 MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) { }
48 std::pair<uint64_t, unsigned>
49 getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const override;
51 CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
53 Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
54 return CC_X86ThisCall;
58 bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
59 // FIXME: Audit the corners
60 if (!RD->isDynamicClass())
63 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
65 // In the Microsoft ABI, classes can have one or two vtable pointers.
66 CharUnits PointerSize =
67 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
68 return Layout.getNonVirtualSize() == PointerSize ||
69 Layout.getNonVirtualSize() == PointerSize * 2;
72 MangleNumberingContext *createMangleNumberingContext() const override {
73 return new MicrosoftNumberingContext();
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.
81 static bool usesMultipleInheritanceModel(const CXXRecordDecl *RD) {
82 while (RD->getNumBases() > 0) {
83 if (RD->getNumBases() > 1)
85 assert(RD->getNumBases() == 1);
86 const CXXRecordDecl *Base =
87 RD->bases_begin()->getType()->getAsCXXRecordDecl();
88 if (RD->isPolymorphic() && !Base->isPolymorphic())
95 MSInheritanceAttr::Spelling CXXRecordDecl::calculateInheritanceModel() const {
96 if (!hasDefinition() || isParsingBaseSpecifiers())
97 return MSInheritanceAttr::Keyword_unspecified_inheritance;
98 if (getNumVBases() > 0)
99 return MSInheritanceAttr::Keyword_virtual_inheritance;
100 if (usesMultipleInheritanceModel(this))
101 return MSInheritanceAttr::Keyword_multiple_inheritance;
102 return MSInheritanceAttr::Keyword_single_inheritance;
105 MSInheritanceAttr::Spelling
106 CXXRecordDecl::getMSInheritanceModel() const {
107 MSInheritanceAttr *IA = getAttr<MSInheritanceAttr>();
108 assert(IA && "Expected MSInheritanceAttr on the CXXRecordDecl!");
109 return IA->getSemanticSpelling();
112 MSVtorDispAttr::Mode CXXRecordDecl::getMSVtorDispMode() const {
113 if (MSVtorDispAttr *VDA = getAttr<MSVtorDispAttr>())
114 return VDA->getVtorDispMode();
115 return MSVtorDispAttr::Mode(getASTContext().getLangOpts().VtorDispMode);
118 // Returns the number of pointer and integer slots used to represent a member
119 // pointer in the MS C++ ABI.
121 // Member function pointers have the following general form; however, fields
122 // are dropped as permitted (under the MSVC interpretation) by the inheritance
123 // model of the actual class.
126 // // A pointer to the member function to call. If the member function is
127 // // virtual, this will be a thunk that forwards to the appropriate vftable
129 // void *FunctionPointerOrVirtualThunk;
131 // // An offset to add to the address of the vbtable pointer after (possibly)
132 // // selecting the virtual base but before resolving and calling the function.
133 // // Only needed if the class has any virtual bases or bases at a non-zero
135 // int NonVirtualBaseAdjustment;
137 // // The offset of the vb-table pointer within the object. Only needed for
138 // // incomplete types.
141 // // An offset within the vb-table that selects the virtual base containing
142 // // the member. Loading from this offset produces a new offset that is
143 // // added to the address of the vb-table pointer to produce the base.
144 // int VirtualBaseAdjustmentOffset;
146 static std::pair<unsigned, unsigned>
147 getMSMemberPointerSlots(const MemberPointerType *MPT) {
148 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
149 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
152 if (MPT->isMemberFunctionPointer())
156 if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(),
159 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
161 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
163 return std::make_pair(Ptrs, Ints);
166 std::pair<uint64_t, unsigned> MicrosoftCXXABI::getMemberPointerWidthAndAlign(
167 const MemberPointerType *MPT) const {
168 const TargetInfo &Target = Context.getTargetInfo();
169 assert(Target.getTriple().getArch() == llvm::Triple::x86 ||
170 Target.getTriple().getArch() == llvm::Triple::x86_64);
172 std::tie(Ptrs, Ints) = getMSMemberPointerSlots(MPT);
173 // The nominal struct is laid out with pointers followed by ints and aligned
174 // to a pointer width if any are present and an int width otherwise.
175 unsigned PtrSize = Target.getPointerWidth(0);
176 unsigned IntSize = Target.getIntWidth();
177 uint64_t Width = Ptrs * PtrSize + Ints * IntSize;
180 // When MSVC does x86_32 record layout, it aligns aggregate member pointers to
181 // 8 bytes. However, __alignof usually returns 4 for data memptrs and 8 for
183 if (Ptrs + Ints > 1 && Target.getTriple().getArch() == llvm::Triple::x86)
186 Align = Target.getPointerAlign(0);
188 Align = Target.getIntAlign();
190 if (Target.getTriple().getArch() == llvm::Triple::x86_64)
191 Width = llvm::RoundUpToAlignment(Width, Align);
192 return std::make_pair(Width, Align);
195 CXXABI *clang::CreateMicrosoftCXXABI(ASTContext &Ctx) {
196 return new MicrosoftCXXABI(Ctx);