blob: fd932f7330a59e4b88384835a76d2408275011d4 [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"
Reid Kleckner84e9ab42013-03-28 20:02:56 +000016#include "clang/AST/Attr.h"
Charles Davis071cc7d2010-08-16 03:33:14 +000017#include "clang/AST/ASTContext.h"
Charles Davis071cc7d2010-08-16 03:33:14 +000018#include "clang/AST/DeclCXX.h"
Anders Carlssondae0cb52010-11-25 01:51:53 +000019#include "clang/AST/RecordLayout.h"
20#include "clang/AST/Type.h"
21#include "clang/Basic/TargetInfo.h"
Charles Davis071cc7d2010-08-16 03:33:14 +000022
23using namespace clang;
24
25namespace {
26class MicrosoftCXXABI : public CXXABI {
27 ASTContext &Context;
28public:
29 MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) { }
30
Reid Kleckner84e9ab42013-03-28 20:02:56 +000031 std::pair<uint64_t, unsigned>
32 getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const;
Charles Davis424ae982010-10-29 03:25:11 +000033
Timur Iskhodzhanov8f88a1d2012-07-12 09:50:54 +000034 CallingConv getDefaultMethodCallConv(bool isVariadic) const {
Benjamin Kramera83297b2013-04-04 17:07:04 +000035 if (!isVariadic &&
36 Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
Charles Davis424ae982010-10-29 03:25:11 +000037 return CC_X86ThisCall;
Benjamin Kramera83297b2013-04-04 17:07:04 +000038 return CC_C;
Charles Davis424ae982010-10-29 03:25:11 +000039 }
Anders Carlssondae0cb52010-11-25 01:51:53 +000040
41 bool isNearlyEmpty(const CXXRecordDecl *RD) const {
42 // FIXME: Audit the corners
43 if (!RD->isDynamicClass())
44 return false;
45
46 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
47
48 // In the Microsoft ABI, classes can have one or two vtable pointers.
Ken Dyck5c3633f2011-02-01 01:52:10 +000049 CharUnits PointerSize =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +000050 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Ken Dyck5c3633f2011-02-01 01:52:10 +000051 return Layout.getNonVirtualSize() == PointerSize ||
52 Layout.getNonVirtualSize() == PointerSize * 2;
Anders Carlssondae0cb52010-11-25 01:51:53 +000053 }
Charles Davis071cc7d2010-08-16 03:33:14 +000054};
55}
56
Reid Kleckner84e9ab42013-03-28 20:02:56 +000057// getNumBases() seems to only give us the number of direct bases, and not the
58// total. This function tells us if we inherit from anybody that uses MI, or if
59// we have a non-primary base class, which uses the multiple inheritance model.
Benjamin Kramera83297b2013-04-04 17:07:04 +000060static bool usesMultipleInheritanceModel(const CXXRecordDecl *RD) {
Reid Kleckner84e9ab42013-03-28 20:02:56 +000061 while (RD->getNumBases() > 0) {
62 if (RD->getNumBases() > 1)
63 return true;
64 assert(RD->getNumBases() == 1);
Benjamin Kramera83297b2013-04-04 17:07:04 +000065 const CXXRecordDecl *Base =
66 RD->bases_begin()->getType()->getAsCXXRecordDecl();
Reid Kleckner84e9ab42013-03-28 20:02:56 +000067 if (RD->isPolymorphic() && !Base->isPolymorphic())
68 return true;
69 RD = Base;
70 }
71 return false;
72}
73
Benjamin Kramera83297b2013-04-04 17:07:04 +000074static MSInheritanceModel MSInheritanceAttrToModel(attr::Kind Kind) {
Reid Kleckner44104892013-04-02 16:23:57 +000075 switch (Kind) {
76 default: llvm_unreachable("expected MS inheritance attribute");
Reid Klecknercb428a12013-04-02 17:40:19 +000077 case attr::SingleInheritance: return MSIM_Single;
78 case attr::MultipleInheritance: return MSIM_Multiple;
79 case attr::VirtualInheritance: return MSIM_Virtual;
80 case attr::UnspecifiedInheritance: return MSIM_Unspecified;
Reid Kleckner84e9ab42013-03-28 20:02:56 +000081 }
Reid Kleckner44104892013-04-02 16:23:57 +000082}
Reid Kleckner84e9ab42013-03-28 20:02:56 +000083
Reid Kleckner44104892013-04-02 16:23:57 +000084MSInheritanceModel CXXRecordDecl::getMSInheritanceModel() const {
Benjamin Kramera83297b2013-04-04 17:07:04 +000085 if (Attr *IA = this->getAttr<MSInheritanceAttr>())
Reid Kleckner44104892013-04-02 16:23:57 +000086 return MSInheritanceAttrToModel(IA->getKind());
87 // If there was no explicit attribute, the record must be defined already, and
88 // we can figure out the inheritance model from its other properties.
89 if (this->getNumVBases() > 0)
Reid Klecknercb428a12013-04-02 17:40:19 +000090 return MSIM_Virtual;
Reid Kleckner44104892013-04-02 16:23:57 +000091 if (usesMultipleInheritanceModel(this))
Reid Klecknera3609b02013-04-11 18:13:19 +000092 return this->isPolymorphic() ? MSIM_MultiplePolymorphic : MSIM_Multiple;
93 return this->isPolymorphic() ? MSIM_SinglePolymorphic : MSIM_Single;
Reid Kleckner44104892013-04-02 16:23:57 +000094}
95
96// Returns the number of pointer and integer slots used to represent a member
97// pointer in the MS C++ ABI.
98//
99// Member function pointers have the following general form; however, fields
100// are dropped as permitted (under the MSVC interpretation) by the inheritance
101// model of the actual class.
102//
103// struct {
104// // A pointer to the member function to call. If the member function is
105// // virtual, this will be a thunk that forwards to the appropriate vftable
106// // slot.
107// void *FunctionPointerOrVirtualThunk;
108//
109// // An offset to add to the address of the vbtable pointer after (possibly)
110// // selecting the virtual base but before resolving and calling the function.
111// // Only needed if the class has any virtual bases or bases at a non-zero
112// // offset.
113// int NonVirtualBaseAdjustment;
114//
115// // An offset within the vb-table that selects the virtual base containing
116// // the member. Loading from this offset produces a new offset that is
117// // added to the address of the vb-table pointer to produce the base.
118// int VirtualBaseAdjustmentOffset;
119//
120// // The offset of the vb-table pointer within the object. Only needed for
121// // incomplete types.
Reid Klecknera3609b02013-04-11 18:13:19 +0000122// int VBPtrOffset;
Reid Kleckner44104892013-04-02 16:23:57 +0000123// };
Reid Klecknera3609b02013-04-11 18:13:19 +0000124static std::pair<unsigned, unsigned>
125getMSMemberPointerSlots(const MemberPointerType *MPT) {
126 const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl();
Reid Kleckner44104892013-04-02 16:23:57 +0000127 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
128 unsigned Ptrs;
129 unsigned Ints = 0;
Reid Klecknera3609b02013-04-11 18:13:19 +0000130 if (MPT->isMemberFunctionPointer()) {
Reid Kleckner84e9ab42013-03-28 20:02:56 +0000131 // Member function pointers are a struct of a function pointer followed by a
132 // variable number of ints depending on the inheritance model used. The
133 // function pointer is a real function if it is non-virtual and a vftable
134 // slot thunk if it is virtual. The ints select the object base passed for
135 // the 'this' pointer.
Reid Kleckner44104892013-04-02 16:23:57 +0000136 Ptrs = 1; // First slot is always a function pointer.
Reid Kleckner84e9ab42013-03-28 20:02:56 +0000137 switch (Inheritance) {
Reid Klecknercb428a12013-04-02 17:40:19 +0000138 case MSIM_Unspecified: ++Ints; // VBTableOffset
139 case MSIM_Virtual: ++Ints; // VirtualBaseAdjustmentOffset
Reid Klecknera3609b02013-04-11 18:13:19 +0000140 case MSIM_MultiplePolymorphic:
Reid Klecknercb428a12013-04-02 17:40:19 +0000141 case MSIM_Multiple: ++Ints; // NonVirtualBaseAdjustment
Reid Klecknera3609b02013-04-11 18:13:19 +0000142 case MSIM_SinglePolymorphic:
Reid Klecknercb428a12013-04-02 17:40:19 +0000143 case MSIM_Single: break; // Nothing
Reid Kleckner84e9ab42013-03-28 20:02:56 +0000144 }
145 } else {
146 // Data pointers are an aggregate of ints. The first int is an offset
147 // followed by vbtable-related offsets.
Reid Kleckner44104892013-04-02 16:23:57 +0000148 Ptrs = 0;
Reid Kleckner84e9ab42013-03-28 20:02:56 +0000149 switch (Inheritance) {
Reid Klecknercb428a12013-04-02 17:40:19 +0000150 case MSIM_Unspecified: ++Ints; // VBTableOffset
151 case MSIM_Virtual: ++Ints; // VirtualBaseAdjustmentOffset
Reid Klecknera3609b02013-04-11 18:13:19 +0000152 case MSIM_MultiplePolymorphic:
Reid Klecknercb428a12013-04-02 17:40:19 +0000153 case MSIM_Multiple: // Nothing
Reid Klecknera3609b02013-04-11 18:13:19 +0000154 case MSIM_SinglePolymorphic:
Reid Klecknercb428a12013-04-02 17:40:19 +0000155 case MSIM_Single: ++Ints; // Field offset
Reid Kleckner84e9ab42013-03-28 20:02:56 +0000156 }
157 }
Reid Kleckner44104892013-04-02 16:23:57 +0000158 return std::make_pair(Ptrs, Ints);
159}
Reid Kleckner84e9ab42013-03-28 20:02:56 +0000160
Benjamin Kramera83297b2013-04-04 17:07:04 +0000161std::pair<uint64_t, unsigned> MicrosoftCXXABI::getMemberPointerWidthAndAlign(
162 const MemberPointerType *MPT) const {
Reid Kleckner44104892013-04-02 16:23:57 +0000163 const TargetInfo &Target = Context.getTargetInfo();
164 assert(Target.getTriple().getArch() == llvm::Triple::x86 ||
165 Target.getTriple().getArch() == llvm::Triple::x86_64);
166 unsigned Ptrs, Ints;
Reid Klecknera3609b02013-04-11 18:13:19 +0000167 llvm::tie(Ptrs, Ints) = getMSMemberPointerSlots(MPT);
Reid Kleckner44104892013-04-02 16:23:57 +0000168 // The nominal struct is laid out with pointers followed by ints and aligned
169 // to a pointer width if any are present and an int width otherwise.
170 unsigned PtrSize = Target.getPointerWidth(0);
171 unsigned IntSize = Target.getIntWidth();
172 uint64_t Width = Ptrs * PtrSize + Ints * IntSize;
173 unsigned Align = Ptrs > 0 ? Target.getPointerAlign(0) : Target.getIntAlign();
174 Width = llvm::RoundUpToAlignment(Width, Align);
Reid Kleckner84e9ab42013-03-28 20:02:56 +0000175 return std::make_pair(Width, Align);
Charles Davis071cc7d2010-08-16 03:33:14 +0000176}
177
178CXXABI *clang::CreateMicrosoftCXXABI(ASTContext &Ctx) {
179 return new MicrosoftCXXABI(Ctx);
180}
181