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/Attr.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/RecordLayout.h"
20 #include "clang/AST/Type.h"
21 #include "clang/Basic/TargetInfo.h"
23 using namespace clang;
26 class MicrosoftCXXABI : public CXXABI {
29 MicrosoftCXXABI(ASTContext &Ctx) : Context(Ctx) { }
31 std::pair<uint64_t, unsigned>
32 getMemberPointerWidthAndAlign(const MemberPointerType *MPT) const;
34 CallingConv getDefaultMethodCallConv(bool isVariadic) const {
36 Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
37 return CC_X86ThisCall;
41 bool isNearlyEmpty(const CXXRecordDecl *RD) const {
42 // FIXME: Audit the corners
43 if (!RD->isDynamicClass())
46 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
48 // In the Microsoft ABI, classes can have one or two vtable pointers.
49 CharUnits PointerSize =
50 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
51 return Layout.getNonVirtualSize() == PointerSize ||
52 Layout.getNonVirtualSize() == PointerSize * 2;
57 // 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.
60 static bool usesMultipleInheritanceModel(const CXXRecordDecl *RD) {
61 while (RD->getNumBases() > 0) {
62 if (RD->getNumBases() > 1)
64 assert(RD->getNumBases() == 1);
65 const CXXRecordDecl *Base =
66 RD->bases_begin()->getType()->getAsCXXRecordDecl();
67 if (RD->isPolymorphic() && !Base->isPolymorphic())
74 static MSInheritanceModel MSInheritanceAttrToModel(attr::Kind Kind) {
76 default: llvm_unreachable("expected MS inheritance attribute");
77 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;
84 MSInheritanceModel CXXRecordDecl::getMSInheritanceModel() const {
85 if (Attr *IA = this->getAttr<MSInheritanceAttr>())
86 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)
91 if (usesMultipleInheritanceModel(this))
92 return this->isPolymorphic() ? MSIM_MultiplePolymorphic : MSIM_Multiple;
93 return this->isPolymorphic() ? MSIM_SinglePolymorphic : MSIM_Single;
96 // Returns the number of pointer and integer slots used to represent a member
97 // pointer in the MS C++ ABI.
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.
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
107 // void *FunctionPointerOrVirtualThunk;
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
113 // int NonVirtualBaseAdjustment;
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;
120 // // The offset of the vb-table pointer within the object. Only needed for
121 // // incomplete types.
124 static std::pair<unsigned, unsigned>
125 getMSMemberPointerSlots(const MemberPointerType *MPT) {
126 const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl();
127 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
130 if (MPT->isMemberFunctionPointer()) {
131 // 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.
136 Ptrs = 1; // First slot is always a function pointer.
137 switch (Inheritance) {
138 case MSIM_Unspecified: ++Ints; // VBTableOffset
139 case MSIM_Virtual: ++Ints; // VirtualBaseAdjustmentOffset
140 case MSIM_MultiplePolymorphic:
141 case MSIM_Multiple: ++Ints; // NonVirtualBaseAdjustment
142 case MSIM_SinglePolymorphic:
143 case MSIM_Single: break; // Nothing
146 // Data pointers are an aggregate of ints. The first int is an offset
147 // followed by vbtable-related offsets.
149 switch (Inheritance) {
150 case MSIM_Unspecified: ++Ints; // VBTableOffset
151 case MSIM_Virtual: ++Ints; // VirtualBaseAdjustmentOffset
152 case MSIM_MultiplePolymorphic:
153 case MSIM_Multiple: // Nothing
154 case MSIM_SinglePolymorphic:
155 case MSIM_Single: ++Ints; // Field offset
158 return std::make_pair(Ptrs, Ints);
161 std::pair<uint64_t, unsigned> MicrosoftCXXABI::getMemberPointerWidthAndAlign(
162 const MemberPointerType *MPT) const {
163 const TargetInfo &Target = Context.getTargetInfo();
164 assert(Target.getTriple().getArch() == llvm::Triple::x86 ||
165 Target.getTriple().getArch() == llvm::Triple::x86_64);
167 llvm::tie(Ptrs, Ints) = getMSMemberPointerSlots(MPT);
168 // 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);
175 return std::make_pair(Width, Align);
178 CXXABI *clang::CreateMicrosoftCXXABI(ASTContext &Ctx) {
179 return new MicrosoftCXXABI(Ctx);