blob: 27b3e341c4b354df965a73f5aa6d5f8ef3944a8b [file] [log] [blame]
Charles Davis53c59df2010-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 Lattner57540c52011-04-15 05:22:18 +000010// This provides C++ AST support targeting the Microsoft Visual C++
Charles Davis53c59df2010-08-16 03:33:14 +000011// ABI.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CXXABI.h"
16#include "clang/AST/ASTContext.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000017#include "clang/AST/Attr.h"
Charles Davis53c59df2010-08-16 03:33:14 +000018#include "clang/AST/DeclCXX.h"
Reid Klecknerd8110b62013-09-10 20:14:30 +000019#include "clang/AST/MangleNumberingContext.h"
Anders Carlsson60a62632010-11-25 01:51:53 +000020#include "clang/AST/RecordLayout.h"
21#include "clang/AST/Type.h"
22#include "clang/Basic/TargetInfo.h"
Charles Davis53c59df2010-08-16 03:33:14 +000023
24using namespace clang;
25
26namespace {
Reid Klecknerd8110b62013-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 {
31 unsigned NumStaticLocals;
32
33public:
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 Davis53c59df2010-08-16 03:33:14 +000043class MicrosoftCXXABI : public CXXABI {
44 ASTContext &Context;
45public:
46 MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) { }
47
Reid Kleckner3a52abf2013-03-28 20:02:56 +000048 std::pair<uint64_t, unsigned>
49 getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const;
Charles Davis31575f72010-10-29 03:25:11 +000050
Timur Iskhodzhanovc5098ad2012-07-12 09:50:54 +000051 CallingConv getDefaultMethodCallConv(bool isVariadic) const {
Benjamin Kramer08db4612013-04-04 17:07:04 +000052 if (!isVariadic &&
53 Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
Charles Davis31575f72010-10-29 03:25:11 +000054 return CC_X86ThisCall;
Benjamin Kramer08db4612013-04-04 17:07:04 +000055 return CC_C;
Charles Davis31575f72010-10-29 03:25:11 +000056 }
Anders Carlsson60a62632010-11-25 01:51:53 +000057
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 Dyck316d6f62011-02-01 01:52:10 +000066 CharUnits PointerSize =
Douglas Gregore8bbc122011-09-02 00:18:52 +000067 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Ken Dyck316d6f62011-02-01 01:52:10 +000068 return Layout.getNonVirtualSize() == PointerSize ||
69 Layout.getNonVirtualSize() == PointerSize * 2;
Anders Carlsson60a62632010-11-25 01:51:53 +000070 }
Reid Klecknerd8110b62013-09-10 20:14:30 +000071
72 MangleNumberingContext *createMangleNumberingContext() const {
73 return new MicrosoftNumberingContext();
74 }
Charles Davis53c59df2010-08-16 03:33:14 +000075};
76}
77
Reid Kleckner3a52abf2013-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 Kramer08db4612013-04-04 17:07:04 +000081static bool usesMultipleInheritanceModel(const CXXRecordDecl *RD) {
Reid Kleckner3a52abf2013-03-28 20:02:56 +000082 while (RD->getNumBases() > 0) {
83 if (RD->getNumBases() > 1)
84 return true;
85 assert(RD->getNumBases() == 1);
Benjamin Kramer08db4612013-04-04 17:07:04 +000086 const CXXRecordDecl *Base =
87 RD->bases_begin()->getType()->getAsCXXRecordDecl();
Reid Kleckner3a52abf2013-03-28 20:02:56 +000088 if (RD->isPolymorphic() && !Base->isPolymorphic())
89 return true;
90 RD = Base;
91 }
92 return false;
93}
94
David Majnemer2c4e00a2014-01-29 22:07:36 +000095MSInheritanceAttr::Spelling CXXRecordDecl::calculateInheritanceModel() const {
96 if (!hasDefinition())
David Majnemer1cdd96d2014-01-17 09:01:00 +000097 return MSInheritanceAttr::Keyword_unspecified_inheritance;
David Majnemer2c4e00a2014-01-29 22:07:36 +000098 if (getNumVBases() > 0)
David Majnemer1cdd96d2014-01-17 09:01:00 +000099 return MSInheritanceAttr::Keyword_virtual_inheritance;
David Majnemer2c4e00a2014-01-29 22:07:36 +0000100 if (usesMultipleInheritanceModel(this))
David Majnemer1cdd96d2014-01-17 09:01:00 +0000101 return MSInheritanceAttr::Keyword_multiple_inheritance;
102 return MSInheritanceAttr::Keyword_single_inheritance;
103}
104
105MSInheritanceAttr::Spelling
106CXXRecordDecl::getMSInheritanceModel() const {
107 MSInheritanceAttr *IA = getAttr<MSInheritanceAttr>();
108 assert(IA && "Expected MSInheritanceAttr on the CXXRecordDecl!");
David Majnemer2c4e00a2014-01-29 22:07:36 +0000109 return IA->getSemanticSpelling();
David Majnemer1cdd96d2014-01-17 09:01:00 +0000110}
111
112void CXXRecordDecl::setMSInheritanceModel() {
113 if (hasAttr<MSInheritanceAttr>())
114 return;
115
116 addAttr(MSInheritanceAttr::CreateImplicit(
David Majnemer2c4e00a2014-01-29 22:07:36 +0000117 getASTContext(), calculateInheritanceModel(), getSourceRange()));
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000118}
119
120// Returns the number of pointer and integer slots used to represent a member
121// pointer in the MS C++ ABI.
122//
123// Member function pointers have the following general form; however, fields
124// are dropped as permitted (under the MSVC interpretation) by the inheritance
125// model of the actual class.
126//
127// struct {
128// // A pointer to the member function to call. If the member function is
129// // virtual, this will be a thunk that forwards to the appropriate vftable
130// // slot.
131// void *FunctionPointerOrVirtualThunk;
132//
133// // An offset to add to the address of the vbtable pointer after (possibly)
134// // selecting the virtual base but before resolving and calling the function.
135// // Only needed if the class has any virtual bases or bases at a non-zero
136// // offset.
137// int NonVirtualBaseAdjustment;
138//
139// // An offset within the vb-table that selects the virtual base containing
140// // the member. Loading from this offset produces a new offset that is
141// // added to the address of the vb-table pointer to produce the base.
142// int VirtualBaseAdjustmentOffset;
143//
144// // The offset of the vb-table pointer within the object. Only needed for
145// // incomplete types.
Reid Kleckner2341ae32013-04-11 18:13:19 +0000146// int VBPtrOffset;
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000147// };
Reid Kleckner2341ae32013-04-11 18:13:19 +0000148static std::pair<unsigned, unsigned>
149getMSMemberPointerSlots(const MemberPointerType *MPT) {
David Majnemer1cdd96d2014-01-17 09:01:00 +0000150 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
151 MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
152 unsigned Ptrs = 0;
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000153 unsigned Ints = 0;
Reid Kleckner2341ae32013-04-11 18:13:19 +0000154 if (MPT->isMemberFunctionPointer()) {
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000155 // Member function pointers are a struct of a function pointer followed by a
156 // variable number of ints depending on the inheritance model used. The
157 // function pointer is a real function if it is non-virtual and a vftable
158 // slot thunk if it is virtual. The ints select the object base passed for
159 // the 'this' pointer.
David Majnemer1cdd96d2014-01-17 09:01:00 +0000160 Ptrs = 1; // First slot is always a function pointer.
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000161 switch (Inheritance) {
David Majnemer1cdd96d2014-01-17 09:01:00 +0000162 case MSInheritanceAttr::Keyword_unspecified_inheritance:
163 ++Ints; // VBTableOffset
164 case MSInheritanceAttr::Keyword_virtual_inheritance:
165 ++Ints; // VirtualBaseAdjustmentOffset
166 case MSInheritanceAttr::Keyword_multiple_inheritance:
167 ++Ints; // NonVirtualBaseAdjustment
168 case MSInheritanceAttr::Keyword_single_inheritance:
169 break; // Nothing
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000170 }
171 } else {
172 // Data pointers are an aggregate of ints. The first int is an offset
173 // followed by vbtable-related offsets.
David Majnemer1cdd96d2014-01-17 09:01:00 +0000174 Ints = 1; // We always have a field offset.
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000175 switch (Inheritance) {
David Majnemer1cdd96d2014-01-17 09:01:00 +0000176 case MSInheritanceAttr::Keyword_unspecified_inheritance:
177 ++Ints; // VBTableOffset
178 case MSInheritanceAttr::Keyword_virtual_inheritance:
179 ++Ints; // VirtualBaseAdjustmentOffset
180 case MSInheritanceAttr::Keyword_multiple_inheritance:
181 case MSInheritanceAttr::Keyword_single_inheritance:
182 break; // Nothing
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000183 }
184 }
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000185 return std::make_pair(Ptrs, Ints);
186}
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000187
Benjamin Kramer08db4612013-04-04 17:07:04 +0000188std::pair<uint64_t, unsigned> MicrosoftCXXABI::getMemberPointerWidthAndAlign(
189 const MemberPointerType *MPT) const {
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000190 const TargetInfo &Target = Context.getTargetInfo();
191 assert(Target.getTriple().getArch() == llvm::Triple::x86 ||
192 Target.getTriple().getArch() == llvm::Triple::x86_64);
193 unsigned Ptrs, Ints;
Reid Kleckner2341ae32013-04-11 18:13:19 +0000194 llvm::tie(Ptrs, Ints) = getMSMemberPointerSlots(MPT);
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000195 // The nominal struct is laid out with pointers followed by ints and aligned
196 // to a pointer width if any are present and an int width otherwise.
197 unsigned PtrSize = Target.getPointerWidth(0);
198 unsigned IntSize = Target.getIntWidth();
199 uint64_t Width = Ptrs * PtrSize + Ints * IntSize;
Reid Klecknerbd63b332014-01-31 22:28:50 +0000200 unsigned Align;
201
202 // When MSVC does x86_32 record layout, it aligns aggregate member pointers to
203 // 8 bytes. However, __alignof usually returns 4 for data memptrs and 8 for
204 // function memptrs.
205 if (Ptrs + Ints > 1 && Target.getTriple().getArch() == llvm::Triple::x86)
206 Align = 8 * 8;
207 else if (Ptrs)
208 Align = Target.getPointerAlign(0);
209 else
210 Align = Target.getIntAlign();
211
212 if (Target.getTriple().getArch() == llvm::Triple::x86_64)
213 Width = llvm::RoundUpToAlignment(Width, Align);
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000214 return std::make_pair(Width, Align);
Charles Davis53c59df2010-08-16 03:33:14 +0000215}
216
217CXXABI *clang::CreateMicrosoftCXXABI(ASTContext &Ctx) {
218 return new MicrosoftCXXABI(Ctx);
219}
220