blob: f4037abebbe4a42ffd654346eb80f5ae96b1e64c [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"
Reid Kleckner3a52abf2013-03-28 20:02:56 +000016#include "clang/AST/Attr.h"
Charles Davis53c59df2010-08-16 03:33:14 +000017#include "clang/AST/ASTContext.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
Aaron Ballman8edb5c22013-12-18 23:44:18 +000095static MSInheritanceModel MSInheritanceAttrToModel(MSInheritanceAttr *Attr) {
96 if (Attr->IsSingle())
97 return MSIM_Single;
98 else if (Attr->IsMultiple())
99 return MSIM_Multiple;
100 else if (Attr->IsVirtual())
101 return MSIM_Virtual;
102
103 assert(Attr->IsUnspecified() && "Expected unspecified inheritance attr");
104 return MSIM_Unspecified;
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000105}
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000106
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000107MSInheritanceModel CXXRecordDecl::getMSInheritanceModel() const {
Aaron Ballman8edb5c22013-12-18 23:44:18 +0000108 if (MSInheritanceAttr *IA = this->getAttr<MSInheritanceAttr>())
109 return MSInheritanceAttrToModel(IA);
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000110 // If there was no explicit attribute, the record must be defined already, and
111 // we can figure out the inheritance model from its other properties.
112 if (this->getNumVBases() > 0)
Reid Kleckner08a39a62013-04-02 17:40:19 +0000113 return MSIM_Virtual;
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000114 if (usesMultipleInheritanceModel(this))
Reid Kleckner2341ae32013-04-11 18:13:19 +0000115 return this->isPolymorphic() ? MSIM_MultiplePolymorphic : MSIM_Multiple;
116 return this->isPolymorphic() ? MSIM_SinglePolymorphic : MSIM_Single;
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000117}
118
119// Returns the number of pointer and integer slots used to represent a member
120// pointer in the MS C++ ABI.
121//
122// Member function pointers have the following general form; however, fields
123// are dropped as permitted (under the MSVC interpretation) by the inheritance
124// model of the actual class.
125//
126// struct {
127// // A pointer to the member function to call. If the member function is
128// // virtual, this will be a thunk that forwards to the appropriate vftable
129// // slot.
130// void *FunctionPointerOrVirtualThunk;
131//
132// // An offset to add to the address of the vbtable pointer after (possibly)
133// // selecting the virtual base but before resolving and calling the function.
134// // Only needed if the class has any virtual bases or bases at a non-zero
135// // offset.
136// int NonVirtualBaseAdjustment;
137//
138// // An offset within the vb-table that selects the virtual base containing
139// // the member. Loading from this offset produces a new offset that is
140// // added to the address of the vb-table pointer to produce the base.
141// int VirtualBaseAdjustmentOffset;
142//
143// // The offset of the vb-table pointer within the object. Only needed for
144// // incomplete types.
Reid Kleckner2341ae32013-04-11 18:13:19 +0000145// int VBPtrOffset;
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000146// };
Reid Kleckner2341ae32013-04-11 18:13:19 +0000147static std::pair<unsigned, unsigned>
148getMSMemberPointerSlots(const MemberPointerType *MPT) {
149 const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl();
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000150 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
151 unsigned Ptrs;
152 unsigned Ints = 0;
Reid Kleckner2341ae32013-04-11 18:13:19 +0000153 if (MPT->isMemberFunctionPointer()) {
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000154 // Member function pointers are a struct of a function pointer followed by a
155 // variable number of ints depending on the inheritance model used. The
156 // function pointer is a real function if it is non-virtual and a vftable
157 // slot thunk if it is virtual. The ints select the object base passed for
158 // the 'this' pointer.
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000159 Ptrs = 1; // First slot is always a function pointer.
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000160 switch (Inheritance) {
Reid Kleckner08a39a62013-04-02 17:40:19 +0000161 case MSIM_Unspecified: ++Ints; // VBTableOffset
162 case MSIM_Virtual: ++Ints; // VirtualBaseAdjustmentOffset
Reid Kleckner2341ae32013-04-11 18:13:19 +0000163 case MSIM_MultiplePolymorphic:
Reid Kleckner08a39a62013-04-02 17:40:19 +0000164 case MSIM_Multiple: ++Ints; // NonVirtualBaseAdjustment
Reid Kleckner2341ae32013-04-11 18:13:19 +0000165 case MSIM_SinglePolymorphic:
Reid Kleckner08a39a62013-04-02 17:40:19 +0000166 case MSIM_Single: break; // Nothing
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000167 }
168 } else {
169 // Data pointers are an aggregate of ints. The first int is an offset
170 // followed by vbtable-related offsets.
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000171 Ptrs = 0;
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000172 switch (Inheritance) {
Reid Kleckner08a39a62013-04-02 17:40:19 +0000173 case MSIM_Unspecified: ++Ints; // VBTableOffset
174 case MSIM_Virtual: ++Ints; // VirtualBaseAdjustmentOffset
Reid Kleckner2341ae32013-04-11 18:13:19 +0000175 case MSIM_MultiplePolymorphic:
Reid Kleckner08a39a62013-04-02 17:40:19 +0000176 case MSIM_Multiple: // Nothing
Reid Kleckner2341ae32013-04-11 18:13:19 +0000177 case MSIM_SinglePolymorphic:
Reid Kleckner08a39a62013-04-02 17:40:19 +0000178 case MSIM_Single: ++Ints; // Field offset
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000179 }
180 }
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000181 return std::make_pair(Ptrs, Ints);
182}
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000183
Benjamin Kramer08db4612013-04-04 17:07:04 +0000184std::pair<uint64_t, unsigned> MicrosoftCXXABI::getMemberPointerWidthAndAlign(
185 const MemberPointerType *MPT) const {
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000186 const TargetInfo &Target = Context.getTargetInfo();
187 assert(Target.getTriple().getArch() == llvm::Triple::x86 ||
188 Target.getTriple().getArch() == llvm::Triple::x86_64);
189 unsigned Ptrs, Ints;
Reid Kleckner2341ae32013-04-11 18:13:19 +0000190 llvm::tie(Ptrs, Ints) = getMSMemberPointerSlots(MPT);
Reid Klecknerbe377cd2013-04-02 16:23:57 +0000191 // The nominal struct is laid out with pointers followed by ints and aligned
192 // to a pointer width if any are present and an int width otherwise.
193 unsigned PtrSize = Target.getPointerWidth(0);
194 unsigned IntSize = Target.getIntWidth();
195 uint64_t Width = Ptrs * PtrSize + Ints * IntSize;
196 unsigned Align = Ptrs > 0 ? Target.getPointerAlign(0) : Target.getIntAlign();
197 Width = llvm::RoundUpToAlignment(Width, Align);
Reid Kleckner3a52abf2013-03-28 20:02:56 +0000198 return std::make_pair(Width, Align);
Charles Davis53c59df2010-08-16 03:33:14 +0000199}
200
201CXXABI *clang::CreateMicrosoftCXXABI(ASTContext &Ctx) {
202 return new MicrosoftCXXABI(Ctx);
203}
204