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 {
31 llvm::DenseMap<const Type *, unsigned> ManglingNumbers;
32 unsigned LambdaManglingNumber;
33 unsigned StaticLocalNumber;
34 unsigned StaticThreadlocalNumber;
37 MicrosoftNumberingContext()
38 : MangleNumberingContext(), LambdaManglingNumber(0),
39 StaticLocalNumber(0), StaticThreadlocalNumber(0) {}
41 unsigned getManglingNumber(const CXXMethodDecl *CallOperator) override {
42 return ++LambdaManglingNumber;
45 unsigned getManglingNumber(const BlockDecl *BD) override {
46 const Type *Ty = nullptr;
47 return ++ManglingNumbers[Ty];
50 unsigned getStaticLocalNumber(const VarDecl *VD) override {
52 return ++StaticThreadlocalNumber;
53 return ++StaticLocalNumber;
56 unsigned getManglingNumber(const VarDecl *VD,
57 unsigned MSLocalManglingNumber) override {
58 return MSLocalManglingNumber;
61 unsigned getManglingNumber(const TagDecl *TD,
62 unsigned MSLocalManglingNumber) override {
63 return MSLocalManglingNumber;
67 class MicrosoftCXXABI : public CXXABI {
69 llvm::SmallDenseMap<CXXRecordDecl *, CXXConstructorDecl *> RecordToCopyCtor;
70 llvm::SmallDenseMap<std::pair<const CXXConstructorDecl *, unsigned>, Expr *>
73 llvm::SmallDenseMap<TagDecl *, DeclaratorDecl *>
74 UnnamedTagDeclToDeclaratorDecl;
75 llvm::SmallDenseMap<TagDecl *, TypedefNameDecl *>
76 UnnamedTagDeclToTypedefNameDecl;
79 MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) { }
81 std::pair<uint64_t, unsigned>
82 getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const override;
84 CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
86 Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
87 return CC_X86ThisCall;
91 bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
92 llvm_unreachable("unapplicable to the MS ABI");
95 void addDefaultArgExprForConstructor(const CXXConstructorDecl *CD,
96 unsigned ParmIdx, Expr *DAE) override {
97 CtorToDefaultArgExpr[std::make_pair(CD, ParmIdx)] = DAE;
100 Expr *getDefaultArgExprForConstructor(const CXXConstructorDecl *CD,
101 unsigned ParmIdx) override {
102 return CtorToDefaultArgExpr[std::make_pair(CD, ParmIdx)];
105 const CXXConstructorDecl *
106 getCopyConstructorForExceptionObject(CXXRecordDecl *RD) override {
107 return RecordToCopyCtor[RD];
111 addCopyConstructorForExceptionObject(CXXRecordDecl *RD,
112 CXXConstructorDecl *CD) override {
113 assert(CD != nullptr);
114 assert(RecordToCopyCtor[RD] == nullptr || RecordToCopyCtor[RD] == CD);
115 RecordToCopyCtor[RD] = CD;
118 void addTypedefNameForUnnamedTagDecl(TagDecl *TD,
119 TypedefNameDecl *DD) override {
120 TD = TD->getCanonicalDecl();
121 DD = cast<TypedefNameDecl>(DD->getCanonicalDecl());
122 TypedefNameDecl *&I = UnnamedTagDeclToTypedefNameDecl[TD];
127 TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override {
128 return UnnamedTagDeclToTypedefNameDecl.lookup(
129 const_cast<TagDecl *>(TD->getCanonicalDecl()));
132 void addDeclaratorForUnnamedTagDecl(TagDecl *TD,
133 DeclaratorDecl *DD) override {
134 TD = TD->getCanonicalDecl();
135 DD = cast<DeclaratorDecl>(DD->getCanonicalDecl());
136 DeclaratorDecl *&I = UnnamedTagDeclToDeclaratorDecl[TD];
141 DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override {
142 return UnnamedTagDeclToDeclaratorDecl.lookup(
143 const_cast<TagDecl *>(TD->getCanonicalDecl()));
146 MangleNumberingContext *createMangleNumberingContext() const override {
147 return new MicrosoftNumberingContext();
152 // getNumBases() seems to only give us the number of direct bases, and not the
153 // total. This function tells us if we inherit from anybody that uses MI, or if
154 // we have a non-primary base class, which uses the multiple inheritance model.
155 static bool usesMultipleInheritanceModel(const CXXRecordDecl *RD) {
156 while (RD->getNumBases() > 0) {
157 if (RD->getNumBases() > 1)
159 assert(RD->getNumBases() == 1);
160 const CXXRecordDecl *Base =
161 RD->bases_begin()->getType()->getAsCXXRecordDecl();
162 if (RD->isPolymorphic() && !Base->isPolymorphic())
169 MSInheritanceAttr::Spelling CXXRecordDecl::calculateInheritanceModel() const {
170 if (!hasDefinition() || isParsingBaseSpecifiers())
171 return MSInheritanceAttr::Keyword_unspecified_inheritance;
172 if (getNumVBases() > 0)
173 return MSInheritanceAttr::Keyword_virtual_inheritance;
174 if (usesMultipleInheritanceModel(this))
175 return MSInheritanceAttr::Keyword_multiple_inheritance;
176 return MSInheritanceAttr::Keyword_single_inheritance;
179 MSInheritanceAttr::Spelling
180 CXXRecordDecl::getMSInheritanceModel() const {
181 MSInheritanceAttr *IA = getAttr<MSInheritanceAttr>();
182 assert(IA && "Expected MSInheritanceAttr on the CXXRecordDecl!");
183 return IA->getSemanticSpelling();
186 MSVtorDispAttr::Mode CXXRecordDecl::getMSVtorDispMode() const {
187 if (MSVtorDispAttr *VDA = getAttr<MSVtorDispAttr>())
188 return VDA->getVtorDispMode();
189 return MSVtorDispAttr::Mode(getASTContext().getLangOpts().VtorDispMode);
192 // Returns the number of pointer and integer slots used to represent a member
193 // pointer in the MS C++ ABI.
195 // Member function pointers have the following general form; however, fields
196 // are dropped as permitted (under the MSVC interpretation) by the inheritance
197 // model of the actual class.
200 // // A pointer to the member function to call. If the member function is
201 // // virtual, this will be a thunk that forwards to the appropriate vftable
203 // void *FunctionPointerOrVirtualThunk;
205 // // An offset to add to the address of the vbtable pointer after
206 // // (possibly) selecting the virtual base but before resolving and calling
208 // // Only needed if the class has any virtual bases or bases at a non-zero
210 // int NonVirtualBaseAdjustment;
212 // // The offset of the vb-table pointer within the object. Only needed for
213 // // incomplete types.
216 // // An offset within the vb-table that selects the virtual base containing
217 // // the member. Loading from this offset produces a new offset that is
218 // // added to the address of the vb-table pointer to produce the base.
219 // int VirtualBaseAdjustmentOffset;
221 static std::pair<unsigned, unsigned>
222 getMSMemberPointerSlots(const MemberPointerType *MPT) {
223 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
224 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
227 if (MPT->isMemberFunctionPointer())
231 if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(),
234 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
236 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
238 return std::make_pair(Ptrs, Ints);
241 std::pair<uint64_t, unsigned> MicrosoftCXXABI::getMemberPointerWidthAndAlign(
242 const MemberPointerType *MPT) const {
243 // The nominal struct is laid out with pointers followed by ints and aligned
244 // to a pointer width if any are present and an int width otherwise.
245 const TargetInfo &Target = Context.getTargetInfo();
246 unsigned PtrSize = Target.getPointerWidth(0);
247 unsigned IntSize = Target.getIntWidth();
250 std::tie(Ptrs, Ints) = getMSMemberPointerSlots(MPT);
251 uint64_t Width = Ptrs * PtrSize + Ints * IntSize;
254 // When MSVC does x86_32 record layout, it aligns aggregate member pointers to
255 // 8 bytes. However, __alignof usually returns 4 for data memptrs and 8 for
257 if (Ptrs + Ints > 1 && Target.getTriple().isArch32Bit())
260 Align = Target.getPointerAlign(0);
262 Align = Target.getIntAlign();
264 if (Target.getTriple().isArch64Bit())
265 Width = llvm::RoundUpToAlignment(Width, Align);
266 return std::make_pair(Width, Align);
269 CXXABI *clang::CreateMicrosoftCXXABI(ASTContext &Ctx) {
270 return new MicrosoftCXXABI(Ctx);