blob: 6870315b2160c227a069a7430735f1110a35580a [file] [log] [blame]
Charles Davis071cc7d2010-08-16 03:33:14 +00001//===------- 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 Lattnerfc8f0e12011-04-15 05:22:18 +000010// This provides C++ AST support targeting the Microsoft Visual C++
Charles Davis071cc7d2010-08-16 03:33:14 +000011// ABI.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CXXABI.h"
16#include "clang/AST/ASTContext.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070017#include "clang/AST/Attr.h"
Charles Davis071cc7d2010-08-16 03:33:14 +000018#include "clang/AST/DeclCXX.h"
Reid Kleckner942f9fe2013-09-10 20:14:30 +000019#include "clang/AST/MangleNumberingContext.h"
Anders Carlssondae0cb52010-11-25 01:51:53 +000020#include "clang/AST/RecordLayout.h"
21#include "clang/AST/Type.h"
22#include "clang/Basic/TargetInfo.h"
Charles Davis071cc7d2010-08-16 03:33:14 +000023
24using namespace clang;
25
26namespace {
Reid Kleckner942f9fe2013-09-10 20:14:30 +000027
28/// \brief Numbers things which need to correspond across multiple TUs.
29/// Typically these are things like static locals, lambdas, or blocks.
30class MicrosoftNumberingContext : public MangleNumberingContext {
Reid Kleckner942f9fe2013-09-10 20:14:30 +000031public:
Stephen Hines651f13c2014-04-23 16:59:28 -070032 unsigned getManglingNumber(const VarDecl *VD,
33 unsigned MSLocalManglingNumber) override {
34 return MSLocalManglingNumber;
35 }
Reid Kleckner942f9fe2013-09-10 20:14:30 +000036
Stephen Hines651f13c2014-04-23 16:59:28 -070037 unsigned getManglingNumber(const TagDecl *TD,
38 unsigned MSLocalManglingNumber) override {
39 return MSLocalManglingNumber;
Reid Kleckner942f9fe2013-09-10 20:14:30 +000040 }
41};
42
Charles Davis071cc7d2010-08-16 03:33:14 +000043class MicrosoftCXXABI : public CXXABI {
44 ASTContext &Context;
45public:
46 MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) { }
47
Reid Kleckner84e9ab42013-03-28 20:02:56 +000048 std::pair<uint64_t, unsigned>
Stephen Hines651f13c2014-04-23 16:59:28 -070049 getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const override;
Charles Davis424ae982010-10-29 03:25:11 +000050
Stephen Hines651f13c2014-04-23 16:59:28 -070051 CallingConv getDefaultMethodCallConv(bool isVariadic) const override {
Benjamin Kramera83297b2013-04-04 17:07:04 +000052 if (!isVariadic &&
53 Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
Charles Davis424ae982010-10-29 03:25:11 +000054 return CC_X86ThisCall;
Benjamin Kramera83297b2013-04-04 17:07:04 +000055 return CC_C;
Charles Davis424ae982010-10-29 03:25:11 +000056 }
Anders Carlssondae0cb52010-11-25 01:51:53 +000057
Stephen Hines651f13c2014-04-23 16:59:28 -070058 bool isNearlyEmpty(const CXXRecordDecl *RD) const override {
Anders Carlssondae0cb52010-11-25 01:51:53 +000059 // 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 Dyck5c3633f2011-02-01 01:52:10 +000066 CharUnits PointerSize =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +000067 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Ken Dyck5c3633f2011-02-01 01:52:10 +000068 return Layout.getNonVirtualSize() == PointerSize ||
69 Layout.getNonVirtualSize() == PointerSize * 2;
Anders Carlssondae0cb52010-11-25 01:51:53 +000070 }
Reid Kleckner942f9fe2013-09-10 20:14:30 +000071
Stephen Hines651f13c2014-04-23 16:59:28 -070072 MangleNumberingContext *createMangleNumberingContext() const override {
Reid Kleckner942f9fe2013-09-10 20:14:30 +000073 return new MicrosoftNumberingContext();
74 }
Charles Davis071cc7d2010-08-16 03:33:14 +000075};
76}
77
Reid Kleckner84e9ab42013-03-28 20:02:56 +000078// 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 Kramera83297b2013-04-04 17:07:04 +000081static bool usesMultipleInheritanceModel(const CXXRecordDecl *RD) {
Reid Kleckner84e9ab42013-03-28 20:02:56 +000082 while (RD->getNumBases() > 0) {
83 if (RD->getNumBases() > 1)
84 return true;
85 assert(RD->getNumBases() == 1);
Benjamin Kramera83297b2013-04-04 17:07:04 +000086 const CXXRecordDecl *Base =
87 RD->bases_begin()->getType()->getAsCXXRecordDecl();
Reid Kleckner84e9ab42013-03-28 20:02:56 +000088 if (RD->isPolymorphic() && !Base->isPolymorphic())
89 return true;
90 RD = Base;
91 }
92 return false;
93}
94
Stephen Hines651f13c2014-04-23 16:59:28 -070095MSInheritanceAttr::Spelling CXXRecordDecl::calculateInheritanceModel() const {
Stephen Hinesef822542014-07-21 00:47:37 -070096 if (!hasDefinition() || isParsingBaseSpecifiers())
Stephen Hines651f13c2014-04-23 16:59:28 -070097 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;
Reid Kleckner44104892013-04-02 16:23:57 +0000103}
Reid Kleckner84e9ab42013-03-28 20:02:56 +0000104
Stephen Hines651f13c2014-04-23 16:59:28 -0700105MSInheritanceAttr::Spelling
106CXXRecordDecl::getMSInheritanceModel() const {
107 MSInheritanceAttr *IA = getAttr<MSInheritanceAttr>();
108 assert(IA && "Expected MSInheritanceAttr on the CXXRecordDecl!");
109 return IA->getSemanticSpelling();
110}
111
112MSVtorDispAttr::Mode CXXRecordDecl::getMSVtorDispMode() const {
113 if (MSVtorDispAttr *VDA = getAttr<MSVtorDispAttr>())
114 return VDA->getVtorDispMode();
115 return MSVtorDispAttr::Mode(getASTContext().getLangOpts().VtorDispMode);
Reid Kleckner44104892013-04-02 16:23:57 +0000116}
117
118// Returns the number of pointer and integer slots used to represent a member
119// pointer in the MS C++ ABI.
120//
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.
124//
125// struct {
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
128// // slot.
129// void *FunctionPointerOrVirtualThunk;
130//
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
134// // offset.
135// int NonVirtualBaseAdjustment;
136//
Stephen Hines651f13c2014-04-23 16:59:28 -0700137// // The offset of the vb-table pointer within the object. Only needed for
138// // incomplete types.
139// int VBPtrOffset;
140//
Reid Kleckner44104892013-04-02 16:23:57 +0000141// // 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;
Reid Kleckner44104892013-04-02 16:23:57 +0000145// };
Reid Klecknera3609b02013-04-11 18:13:19 +0000146static std::pair<unsigned, unsigned>
147getMSMemberPointerSlots(const MemberPointerType *MPT) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700148 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
149 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
150 unsigned Ptrs = 0;
Reid Kleckner44104892013-04-02 16:23:57 +0000151 unsigned Ints = 0;
Stephen Hines651f13c2014-04-23 16:59:28 -0700152 if (MPT->isMemberFunctionPointer())
153 Ptrs = 1;
154 else
155 Ints = 1;
156 if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(),
157 Inheritance))
158 Ints++;
159 if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
160 Ints++;
161 if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
162 Ints++;
Reid Kleckner44104892013-04-02 16:23:57 +0000163 return std::make_pair(Ptrs, Ints);
164}
Reid Kleckner84e9ab42013-03-28 20:02:56 +0000165
Benjamin Kramera83297b2013-04-04 17:07:04 +0000166std::pair<uint64_t, unsigned> MicrosoftCXXABI::getMemberPointerWidthAndAlign(
167 const MemberPointerType *MPT) const {
Reid Kleckner44104892013-04-02 16:23:57 +0000168 const TargetInfo &Target = Context.getTargetInfo();
169 assert(Target.getTriple().getArch() == llvm::Triple::x86 ||
170 Target.getTriple().getArch() == llvm::Triple::x86_64);
171 unsigned Ptrs, Ints;
Stephen Hines651f13c2014-04-23 16:59:28 -0700172 std::tie(Ptrs, Ints) = getMSMemberPointerSlots(MPT);
Reid Kleckner44104892013-04-02 16:23:57 +0000173 // 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;
Stephen Hines651f13c2014-04-23 16:59:28 -0700178 unsigned Align;
179
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
182 // function memptrs.
183 if (Ptrs + Ints > 1 && Target.getTriple().getArch() == llvm::Triple::x86)
184 Align = 8 * 8;
185 else if (Ptrs)
186 Align = Target.getPointerAlign(0);
187 else
188 Align = Target.getIntAlign();
189
190 if (Target.getTriple().getArch() == llvm::Triple::x86_64)
191 Width = llvm::RoundUpToAlignment(Width, Align);
Reid Kleckner84e9ab42013-03-28 20:02:56 +0000192 return std::make_pair(Width, Align);
Charles Davis071cc7d2010-08-16 03:33:14 +0000193}
194
195CXXABI *clang::CreateMicrosoftCXXABI(ASTContext &Ctx) {
196 return new MicrosoftCXXABI(Ctx);
197}
198