1f4a2713aSLionel Sambuc //===--- MicrosoftCXXABI.cpp - Emit LLVM Code from ASTs for a Module ------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This provides C++ code generation targeting the Microsoft Visual C++ ABI.
11f4a2713aSLionel Sambuc // The class in this file generates structures that follow the Microsoft
12f4a2713aSLionel Sambuc // Visual C++ ABI, which is actually not very well documented at all outside
13f4a2713aSLionel Sambuc // of Microsoft.
14f4a2713aSLionel Sambuc //
15f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
16f4a2713aSLionel Sambuc
17f4a2713aSLionel Sambuc #include "CGCXXABI.h"
18f4a2713aSLionel Sambuc #include "CGVTables.h"
19*0a6a1f1dSLionel Sambuc #include "CodeGenModule.h"
20f4a2713aSLionel Sambuc #include "clang/AST/Decl.h"
21f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h"
22f4a2713aSLionel Sambuc #include "clang/AST/VTableBuilder.h"
23*0a6a1f1dSLionel Sambuc #include "llvm/ADT/StringExtras.h"
24f4a2713aSLionel Sambuc #include "llvm/ADT/StringSet.h"
25*0a6a1f1dSLionel Sambuc #include "llvm/IR/CallSite.h"
26f4a2713aSLionel Sambuc
27f4a2713aSLionel Sambuc using namespace clang;
28f4a2713aSLionel Sambuc using namespace CodeGen;
29f4a2713aSLionel Sambuc
30f4a2713aSLionel Sambuc namespace {
31f4a2713aSLionel Sambuc
32*0a6a1f1dSLionel Sambuc /// Holds all the vbtable globals for a given class.
33*0a6a1f1dSLionel Sambuc struct VBTableGlobals {
34*0a6a1f1dSLionel Sambuc const VPtrInfoVector *VBTables;
35*0a6a1f1dSLionel Sambuc SmallVector<llvm::GlobalVariable *, 2> Globals;
36*0a6a1f1dSLionel Sambuc };
37*0a6a1f1dSLionel Sambuc
38f4a2713aSLionel Sambuc class MicrosoftCXXABI : public CGCXXABI {
39f4a2713aSLionel Sambuc public:
MicrosoftCXXABI(CodeGenModule & CGM)40*0a6a1f1dSLionel Sambuc MicrosoftCXXABI(CodeGenModule &CGM)
41*0a6a1f1dSLionel Sambuc : CGCXXABI(CGM), BaseClassDescriptorType(nullptr),
42*0a6a1f1dSLionel Sambuc ClassHierarchyDescriptorType(nullptr),
43*0a6a1f1dSLionel Sambuc CompleteObjectLocatorType(nullptr) {}
44f4a2713aSLionel Sambuc
45*0a6a1f1dSLionel Sambuc bool HasThisReturn(GlobalDecl GD) const override;
46*0a6a1f1dSLionel Sambuc bool hasMostDerivedReturn(GlobalDecl GD) const override;
47f4a2713aSLionel Sambuc
48*0a6a1f1dSLionel Sambuc bool classifyReturnType(CGFunctionInfo &FI) const override;
49*0a6a1f1dSLionel Sambuc
50*0a6a1f1dSLionel Sambuc RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override;
51*0a6a1f1dSLionel Sambuc
isSRetParameterAfterThis() const52*0a6a1f1dSLionel Sambuc bool isSRetParameterAfterThis() const override { return true; }
53*0a6a1f1dSLionel Sambuc
getSrcArgforCopyCtor(const CXXConstructorDecl * CD,FunctionArgList & Args) const54*0a6a1f1dSLionel Sambuc size_t getSrcArgforCopyCtor(const CXXConstructorDecl *CD,
55*0a6a1f1dSLionel Sambuc FunctionArgList &Args) const override {
56*0a6a1f1dSLionel Sambuc assert(Args.size() >= 2 &&
57*0a6a1f1dSLionel Sambuc "expected the arglist to have at least two args!");
58*0a6a1f1dSLionel Sambuc // The 'most_derived' parameter goes second if the ctor is variadic and
59*0a6a1f1dSLionel Sambuc // has v-bases.
60*0a6a1f1dSLionel Sambuc if (CD->getParent()->getNumVBases() > 0 &&
61*0a6a1f1dSLionel Sambuc CD->getType()->castAs<FunctionProtoType>()->isVariadic())
62*0a6a1f1dSLionel Sambuc return 2;
63*0a6a1f1dSLionel Sambuc return 1;
64f4a2713aSLionel Sambuc }
65f4a2713aSLionel Sambuc
GetPureVirtualCallName()66*0a6a1f1dSLionel Sambuc StringRef GetPureVirtualCallName() override { return "_purecall"; }
GetDeletedVirtualCallName()67*0a6a1f1dSLionel Sambuc StringRef GetDeletedVirtualCallName() override { return "_purecall"; }
68f4a2713aSLionel Sambuc
69*0a6a1f1dSLionel Sambuc void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE,
70*0a6a1f1dSLionel Sambuc llvm::Value *Ptr, QualType ElementType,
71*0a6a1f1dSLionel Sambuc const CXXDestructorDecl *Dtor) override;
72f4a2713aSLionel Sambuc
73*0a6a1f1dSLionel Sambuc void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override;
74f4a2713aSLionel Sambuc
75*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *getMSCompleteObjectLocator(const CXXRecordDecl *RD,
76*0a6a1f1dSLionel Sambuc const VPtrInfo *Info);
77f4a2713aSLionel Sambuc
78*0a6a1f1dSLionel Sambuc llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override;
79*0a6a1f1dSLionel Sambuc
80*0a6a1f1dSLionel Sambuc bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override;
81*0a6a1f1dSLionel Sambuc void EmitBadTypeidCall(CodeGenFunction &CGF) override;
82*0a6a1f1dSLionel Sambuc llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
83*0a6a1f1dSLionel Sambuc llvm::Value *ThisPtr,
84*0a6a1f1dSLionel Sambuc llvm::Type *StdTypeInfoPtrTy) override;
85*0a6a1f1dSLionel Sambuc
86*0a6a1f1dSLionel Sambuc bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
87*0a6a1f1dSLionel Sambuc QualType SrcRecordTy) override;
88*0a6a1f1dSLionel Sambuc
89*0a6a1f1dSLionel Sambuc llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value,
90*0a6a1f1dSLionel Sambuc QualType SrcRecordTy, QualType DestTy,
91*0a6a1f1dSLionel Sambuc QualType DestRecordTy,
92*0a6a1f1dSLionel Sambuc llvm::BasicBlock *CastEnd) override;
93*0a6a1f1dSLionel Sambuc
94*0a6a1f1dSLionel Sambuc llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value,
95*0a6a1f1dSLionel Sambuc QualType SrcRecordTy,
96*0a6a1f1dSLionel Sambuc QualType DestTy) override;
97*0a6a1f1dSLionel Sambuc
98*0a6a1f1dSLionel Sambuc bool EmitBadCastCall(CodeGenFunction &CGF) override;
99*0a6a1f1dSLionel Sambuc
100*0a6a1f1dSLionel Sambuc llvm::Value *
101*0a6a1f1dSLionel Sambuc GetVirtualBaseClassOffset(CodeGenFunction &CGF, llvm::Value *This,
102f4a2713aSLionel Sambuc const CXXRecordDecl *ClassDecl,
103*0a6a1f1dSLionel Sambuc const CXXRecordDecl *BaseClassDecl) override;
104f4a2713aSLionel Sambuc
105*0a6a1f1dSLionel Sambuc llvm::BasicBlock *
106*0a6a1f1dSLionel Sambuc EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
107*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD) override;
108f4a2713aSLionel Sambuc
109f4a2713aSLionel Sambuc void initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF,
110*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD) override;
111f4a2713aSLionel Sambuc
112*0a6a1f1dSLionel Sambuc void EmitCXXConstructors(const CXXConstructorDecl *D) override;
113f4a2713aSLionel Sambuc
114f4a2713aSLionel Sambuc // Background on MSVC destructors
115f4a2713aSLionel Sambuc // ==============================
116f4a2713aSLionel Sambuc //
117f4a2713aSLionel Sambuc // Both Itanium and MSVC ABIs have destructor variants. The variant names
118f4a2713aSLionel Sambuc // roughly correspond in the following way:
119f4a2713aSLionel Sambuc // Itanium Microsoft
120f4a2713aSLionel Sambuc // Base -> no name, just ~Class
121f4a2713aSLionel Sambuc // Complete -> vbase destructor
122f4a2713aSLionel Sambuc // Deleting -> scalar deleting destructor
123f4a2713aSLionel Sambuc // vector deleting destructor
124f4a2713aSLionel Sambuc //
125f4a2713aSLionel Sambuc // The base and complete destructors are the same as in Itanium, although the
126f4a2713aSLionel Sambuc // complete destructor does not accept a VTT parameter when there are virtual
127f4a2713aSLionel Sambuc // bases. A separate mechanism involving vtordisps is used to ensure that
128f4a2713aSLionel Sambuc // virtual methods of destroyed subobjects are not called.
129f4a2713aSLionel Sambuc //
130f4a2713aSLionel Sambuc // The deleting destructors accept an i32 bitfield as a second parameter. Bit
131f4a2713aSLionel Sambuc // 1 indicates if the memory should be deleted. Bit 2 indicates if the this
132f4a2713aSLionel Sambuc // pointer points to an array. The scalar deleting destructor assumes that
133f4a2713aSLionel Sambuc // bit 2 is zero, and therefore does not contain a loop.
134f4a2713aSLionel Sambuc //
135f4a2713aSLionel Sambuc // For virtual destructors, only one entry is reserved in the vftable, and it
136f4a2713aSLionel Sambuc // always points to the vector deleting destructor. The vector deleting
137f4a2713aSLionel Sambuc // destructor is the most general, so it can be used to destroy objects in
138f4a2713aSLionel Sambuc // place, delete single heap objects, or delete arrays.
139f4a2713aSLionel Sambuc //
140f4a2713aSLionel Sambuc // A TU defining a non-inline destructor is only guaranteed to emit a base
141f4a2713aSLionel Sambuc // destructor, and all of the other variants are emitted on an as-needed basis
142f4a2713aSLionel Sambuc // in COMDATs. Because a non-base destructor can be emitted in a TU that
143f4a2713aSLionel Sambuc // lacks a definition for the destructor, non-base destructors must always
144f4a2713aSLionel Sambuc // delegate to or alias the base destructor.
145f4a2713aSLionel Sambuc
146*0a6a1f1dSLionel Sambuc void buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
147*0a6a1f1dSLionel Sambuc SmallVectorImpl<CanQualType> &ArgTys) override;
148f4a2713aSLionel Sambuc
149f4a2713aSLionel Sambuc /// Non-base dtors should be emitted as delegating thunks in this ABI.
useThunkForDtorVariant(const CXXDestructorDecl * Dtor,CXXDtorType DT) const150f4a2713aSLionel Sambuc bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
151*0a6a1f1dSLionel Sambuc CXXDtorType DT) const override {
152f4a2713aSLionel Sambuc return DT != Dtor_Base;
153f4a2713aSLionel Sambuc }
154f4a2713aSLionel Sambuc
155*0a6a1f1dSLionel Sambuc void EmitCXXDestructors(const CXXDestructorDecl *D) override;
156f4a2713aSLionel Sambuc
157*0a6a1f1dSLionel Sambuc const CXXRecordDecl *
getThisArgumentTypeForMethod(const CXXMethodDecl * MD)158*0a6a1f1dSLionel Sambuc getThisArgumentTypeForMethod(const CXXMethodDecl *MD) override {
159f4a2713aSLionel Sambuc MD = MD->getCanonicalDecl();
160f4a2713aSLionel Sambuc if (MD->isVirtual() && !isa<CXXDestructorDecl>(MD)) {
161f4a2713aSLionel Sambuc MicrosoftVTableContext::MethodVFTableLocation ML =
162f4a2713aSLionel Sambuc CGM.getMicrosoftVTableContext().getMethodVFTableLocation(MD);
163f4a2713aSLionel Sambuc // The vbases might be ordered differently in the final overrider object
164f4a2713aSLionel Sambuc // and the complete object, so the "this" argument may sometimes point to
165f4a2713aSLionel Sambuc // memory that has no particular type (e.g. past the complete object).
166f4a2713aSLionel Sambuc // In this case, we just use a generic pointer type.
167f4a2713aSLionel Sambuc // FIXME: might want to have a more precise type in the non-virtual
168f4a2713aSLionel Sambuc // multiple inheritance case.
169f4a2713aSLionel Sambuc if (ML.VBase || !ML.VFPtrOffset.isZero())
170*0a6a1f1dSLionel Sambuc return nullptr;
171f4a2713aSLionel Sambuc }
172f4a2713aSLionel Sambuc return MD->getParent();
173f4a2713aSLionel Sambuc }
174f4a2713aSLionel Sambuc
175*0a6a1f1dSLionel Sambuc llvm::Value *
176*0a6a1f1dSLionel Sambuc adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD,
177*0a6a1f1dSLionel Sambuc llvm::Value *This,
178*0a6a1f1dSLionel Sambuc bool VirtualCall) override;
179f4a2713aSLionel Sambuc
180*0a6a1f1dSLionel Sambuc void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
181*0a6a1f1dSLionel Sambuc FunctionArgList &Params) override;
182f4a2713aSLionel Sambuc
183f4a2713aSLionel Sambuc llvm::Value *adjustThisParameterInVirtualFunctionPrologue(
184*0a6a1f1dSLionel Sambuc CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) override;
185f4a2713aSLionel Sambuc
186*0a6a1f1dSLionel Sambuc void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override;
187f4a2713aSLionel Sambuc
188*0a6a1f1dSLionel Sambuc unsigned addImplicitConstructorArgs(CodeGenFunction &CGF,
189*0a6a1f1dSLionel Sambuc const CXXConstructorDecl *D,
190*0a6a1f1dSLionel Sambuc CXXCtorType Type, bool ForVirtualBase,
191*0a6a1f1dSLionel Sambuc bool Delegating,
192*0a6a1f1dSLionel Sambuc CallArgList &Args) override;
193f4a2713aSLionel Sambuc
194*0a6a1f1dSLionel Sambuc void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD,
195*0a6a1f1dSLionel Sambuc CXXDtorType Type, bool ForVirtualBase,
196*0a6a1f1dSLionel Sambuc bool Delegating, llvm::Value *This) override;
197*0a6a1f1dSLionel Sambuc
198*0a6a1f1dSLionel Sambuc void emitVTableDefinitions(CodeGenVTables &CGVT,
199*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD) override;
200f4a2713aSLionel Sambuc
201f4a2713aSLionel Sambuc llvm::Value *getVTableAddressPointInStructor(
202f4a2713aSLionel Sambuc CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
203f4a2713aSLionel Sambuc BaseSubobject Base, const CXXRecordDecl *NearestVBase,
204*0a6a1f1dSLionel Sambuc bool &NeedsVirtualOffset) override;
205f4a2713aSLionel Sambuc
206f4a2713aSLionel Sambuc llvm::Constant *
207f4a2713aSLionel Sambuc getVTableAddressPointForConstExpr(BaseSubobject Base,
208*0a6a1f1dSLionel Sambuc const CXXRecordDecl *VTableClass) override;
209f4a2713aSLionel Sambuc
210f4a2713aSLionel Sambuc llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
211*0a6a1f1dSLionel Sambuc CharUnits VPtrOffset) override;
212f4a2713aSLionel Sambuc
213f4a2713aSLionel Sambuc llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD,
214*0a6a1f1dSLionel Sambuc llvm::Value *This,
215*0a6a1f1dSLionel Sambuc llvm::Type *Ty) override;
216f4a2713aSLionel Sambuc
217*0a6a1f1dSLionel Sambuc llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF,
218f4a2713aSLionel Sambuc const CXXDestructorDecl *Dtor,
219*0a6a1f1dSLionel Sambuc CXXDtorType DtorType,
220*0a6a1f1dSLionel Sambuc llvm::Value *This,
221*0a6a1f1dSLionel Sambuc const CXXMemberCallExpr *CE) override;
222f4a2713aSLionel Sambuc
adjustCallArgsForDestructorThunk(CodeGenFunction & CGF,GlobalDecl GD,CallArgList & CallArgs)223f4a2713aSLionel Sambuc void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, GlobalDecl GD,
224*0a6a1f1dSLionel Sambuc CallArgList &CallArgs) override {
225f4a2713aSLionel Sambuc assert(GD.getDtorType() == Dtor_Deleting &&
226f4a2713aSLionel Sambuc "Only deleting destructor thunks are available in this ABI");
227f4a2713aSLionel Sambuc CallArgs.add(RValue::get(getStructorImplicitParamValue(CGF)),
228f4a2713aSLionel Sambuc CGM.getContext().IntTy);
229f4a2713aSLionel Sambuc }
230f4a2713aSLionel Sambuc
231*0a6a1f1dSLionel Sambuc void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override;
232f4a2713aSLionel Sambuc
233*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *
234*0a6a1f1dSLionel Sambuc getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD,
235*0a6a1f1dSLionel Sambuc llvm::GlobalVariable::LinkageTypes Linkage);
236*0a6a1f1dSLionel Sambuc
237*0a6a1f1dSLionel Sambuc void emitVBTableDefinition(const VPtrInfo &VBT, const CXXRecordDecl *RD,
238*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *GV) const;
239*0a6a1f1dSLionel Sambuc
setThunkLinkage(llvm::Function * Thunk,bool ForVTable,GlobalDecl GD,bool ReturnAdjustment)240*0a6a1f1dSLionel Sambuc void setThunkLinkage(llvm::Function *Thunk, bool ForVTable,
241*0a6a1f1dSLionel Sambuc GlobalDecl GD, bool ReturnAdjustment) override {
242*0a6a1f1dSLionel Sambuc // Never dllimport/dllexport thunks.
243*0a6a1f1dSLionel Sambuc Thunk->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
244*0a6a1f1dSLionel Sambuc
245*0a6a1f1dSLionel Sambuc GVALinkage Linkage =
246*0a6a1f1dSLionel Sambuc getContext().GetGVALinkageForFunction(cast<FunctionDecl>(GD.getDecl()));
247*0a6a1f1dSLionel Sambuc
248*0a6a1f1dSLionel Sambuc if (Linkage == GVA_Internal)
249*0a6a1f1dSLionel Sambuc Thunk->setLinkage(llvm::GlobalValue::InternalLinkage);
250*0a6a1f1dSLionel Sambuc else if (ReturnAdjustment)
251*0a6a1f1dSLionel Sambuc Thunk->setLinkage(llvm::GlobalValue::WeakODRLinkage);
252*0a6a1f1dSLionel Sambuc else
253*0a6a1f1dSLionel Sambuc Thunk->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage);
254f4a2713aSLionel Sambuc }
255f4a2713aSLionel Sambuc
256f4a2713aSLionel Sambuc llvm::Value *performThisAdjustment(CodeGenFunction &CGF, llvm::Value *This,
257*0a6a1f1dSLionel Sambuc const ThisAdjustment &TA) override;
258f4a2713aSLionel Sambuc
259f4a2713aSLionel Sambuc llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret,
260*0a6a1f1dSLionel Sambuc const ReturnAdjustment &RA) override;
261*0a6a1f1dSLionel Sambuc
262*0a6a1f1dSLionel Sambuc void EmitThreadLocalInitFuncs(
263*0a6a1f1dSLionel Sambuc CodeGenModule &CGM,
264*0a6a1f1dSLionel Sambuc ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>>
265*0a6a1f1dSLionel Sambuc CXXThreadLocals,
266*0a6a1f1dSLionel Sambuc ArrayRef<llvm::Function *> CXXThreadLocalInits,
267*0a6a1f1dSLionel Sambuc ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) override;
268*0a6a1f1dSLionel Sambuc
usesThreadWrapperFunction() const269*0a6a1f1dSLionel Sambuc bool usesThreadWrapperFunction() const override { return false; }
270*0a6a1f1dSLionel Sambuc LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD,
271*0a6a1f1dSLionel Sambuc QualType LValType) override;
272f4a2713aSLionel Sambuc
273f4a2713aSLionel Sambuc void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
274f4a2713aSLionel Sambuc llvm::GlobalVariable *DeclPtr,
275*0a6a1f1dSLionel Sambuc bool PerformInit) override;
276*0a6a1f1dSLionel Sambuc void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
277*0a6a1f1dSLionel Sambuc llvm::Constant *Dtor, llvm::Constant *Addr) override;
278f4a2713aSLionel Sambuc
279f4a2713aSLionel Sambuc // ==== Notes on array cookies =========
280f4a2713aSLionel Sambuc //
281f4a2713aSLionel Sambuc // MSVC seems to only use cookies when the class has a destructor; a
282f4a2713aSLionel Sambuc // two-argument usual array deallocation function isn't sufficient.
283f4a2713aSLionel Sambuc //
284f4a2713aSLionel Sambuc // For example, this code prints "100" and "1":
285f4a2713aSLionel Sambuc // struct A {
286f4a2713aSLionel Sambuc // char x;
287f4a2713aSLionel Sambuc // void *operator new[](size_t sz) {
288f4a2713aSLionel Sambuc // printf("%u\n", sz);
289f4a2713aSLionel Sambuc // return malloc(sz);
290f4a2713aSLionel Sambuc // }
291f4a2713aSLionel Sambuc // void operator delete[](void *p, size_t sz) {
292f4a2713aSLionel Sambuc // printf("%u\n", sz);
293f4a2713aSLionel Sambuc // free(p);
294f4a2713aSLionel Sambuc // }
295f4a2713aSLionel Sambuc // };
296f4a2713aSLionel Sambuc // int main() {
297f4a2713aSLionel Sambuc // A *p = new A[100];
298f4a2713aSLionel Sambuc // delete[] p;
299f4a2713aSLionel Sambuc // }
300f4a2713aSLionel Sambuc // Whereas it prints "104" and "104" if you give A a destructor.
301f4a2713aSLionel Sambuc
302*0a6a1f1dSLionel Sambuc bool requiresArrayCookie(const CXXDeleteExpr *expr,
303*0a6a1f1dSLionel Sambuc QualType elementType) override;
304*0a6a1f1dSLionel Sambuc bool requiresArrayCookie(const CXXNewExpr *expr) override;
305*0a6a1f1dSLionel Sambuc CharUnits getArrayCookieSizeImpl(QualType type) override;
306f4a2713aSLionel Sambuc llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
307f4a2713aSLionel Sambuc llvm::Value *NewPtr,
308f4a2713aSLionel Sambuc llvm::Value *NumElements,
309f4a2713aSLionel Sambuc const CXXNewExpr *expr,
310*0a6a1f1dSLionel Sambuc QualType ElementType) override;
311f4a2713aSLionel Sambuc llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
312f4a2713aSLionel Sambuc llvm::Value *allocPtr,
313*0a6a1f1dSLionel Sambuc CharUnits cookieSize) override;
314*0a6a1f1dSLionel Sambuc
315*0a6a1f1dSLionel Sambuc friend struct MSRTTIBuilder;
316*0a6a1f1dSLionel Sambuc
isImageRelative() const317*0a6a1f1dSLionel Sambuc bool isImageRelative() const {
318*0a6a1f1dSLionel Sambuc return CGM.getTarget().getPointerWidth(/*AddressSpace=*/0) == 64;
319*0a6a1f1dSLionel Sambuc }
320*0a6a1f1dSLionel Sambuc
321*0a6a1f1dSLionel Sambuc // 5 routines for constructing the llvm types for MS RTTI structs.
getTypeDescriptorType(StringRef TypeInfoString)322*0a6a1f1dSLionel Sambuc llvm::StructType *getTypeDescriptorType(StringRef TypeInfoString) {
323*0a6a1f1dSLionel Sambuc llvm::SmallString<32> TDTypeName("rtti.TypeDescriptor");
324*0a6a1f1dSLionel Sambuc TDTypeName += llvm::utostr(TypeInfoString.size());
325*0a6a1f1dSLionel Sambuc llvm::StructType *&TypeDescriptorType =
326*0a6a1f1dSLionel Sambuc TypeDescriptorTypeMap[TypeInfoString.size()];
327*0a6a1f1dSLionel Sambuc if (TypeDescriptorType)
328*0a6a1f1dSLionel Sambuc return TypeDescriptorType;
329*0a6a1f1dSLionel Sambuc llvm::Type *FieldTypes[] = {
330*0a6a1f1dSLionel Sambuc CGM.Int8PtrPtrTy,
331*0a6a1f1dSLionel Sambuc CGM.Int8PtrTy,
332*0a6a1f1dSLionel Sambuc llvm::ArrayType::get(CGM.Int8Ty, TypeInfoString.size() + 1)};
333*0a6a1f1dSLionel Sambuc TypeDescriptorType =
334*0a6a1f1dSLionel Sambuc llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, TDTypeName);
335*0a6a1f1dSLionel Sambuc return TypeDescriptorType;
336*0a6a1f1dSLionel Sambuc }
337*0a6a1f1dSLionel Sambuc
getImageRelativeType(llvm::Type * PtrType)338*0a6a1f1dSLionel Sambuc llvm::Type *getImageRelativeType(llvm::Type *PtrType) {
339*0a6a1f1dSLionel Sambuc if (!isImageRelative())
340*0a6a1f1dSLionel Sambuc return PtrType;
341*0a6a1f1dSLionel Sambuc return CGM.IntTy;
342*0a6a1f1dSLionel Sambuc }
343*0a6a1f1dSLionel Sambuc
getBaseClassDescriptorType()344*0a6a1f1dSLionel Sambuc llvm::StructType *getBaseClassDescriptorType() {
345*0a6a1f1dSLionel Sambuc if (BaseClassDescriptorType)
346*0a6a1f1dSLionel Sambuc return BaseClassDescriptorType;
347*0a6a1f1dSLionel Sambuc llvm::Type *FieldTypes[] = {
348*0a6a1f1dSLionel Sambuc getImageRelativeType(CGM.Int8PtrTy),
349*0a6a1f1dSLionel Sambuc CGM.IntTy,
350*0a6a1f1dSLionel Sambuc CGM.IntTy,
351*0a6a1f1dSLionel Sambuc CGM.IntTy,
352*0a6a1f1dSLionel Sambuc CGM.IntTy,
353*0a6a1f1dSLionel Sambuc CGM.IntTy,
354*0a6a1f1dSLionel Sambuc getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()),
355*0a6a1f1dSLionel Sambuc };
356*0a6a1f1dSLionel Sambuc BaseClassDescriptorType = llvm::StructType::create(
357*0a6a1f1dSLionel Sambuc CGM.getLLVMContext(), FieldTypes, "rtti.BaseClassDescriptor");
358*0a6a1f1dSLionel Sambuc return BaseClassDescriptorType;
359*0a6a1f1dSLionel Sambuc }
360*0a6a1f1dSLionel Sambuc
getClassHierarchyDescriptorType()361*0a6a1f1dSLionel Sambuc llvm::StructType *getClassHierarchyDescriptorType() {
362*0a6a1f1dSLionel Sambuc if (ClassHierarchyDescriptorType)
363*0a6a1f1dSLionel Sambuc return ClassHierarchyDescriptorType;
364*0a6a1f1dSLionel Sambuc // Forward-declare RTTIClassHierarchyDescriptor to break a cycle.
365*0a6a1f1dSLionel Sambuc ClassHierarchyDescriptorType = llvm::StructType::create(
366*0a6a1f1dSLionel Sambuc CGM.getLLVMContext(), "rtti.ClassHierarchyDescriptor");
367*0a6a1f1dSLionel Sambuc llvm::Type *FieldTypes[] = {
368*0a6a1f1dSLionel Sambuc CGM.IntTy,
369*0a6a1f1dSLionel Sambuc CGM.IntTy,
370*0a6a1f1dSLionel Sambuc CGM.IntTy,
371*0a6a1f1dSLionel Sambuc getImageRelativeType(
372*0a6a1f1dSLionel Sambuc getBaseClassDescriptorType()->getPointerTo()->getPointerTo()),
373*0a6a1f1dSLionel Sambuc };
374*0a6a1f1dSLionel Sambuc ClassHierarchyDescriptorType->setBody(FieldTypes);
375*0a6a1f1dSLionel Sambuc return ClassHierarchyDescriptorType;
376*0a6a1f1dSLionel Sambuc }
377*0a6a1f1dSLionel Sambuc
getCompleteObjectLocatorType()378*0a6a1f1dSLionel Sambuc llvm::StructType *getCompleteObjectLocatorType() {
379*0a6a1f1dSLionel Sambuc if (CompleteObjectLocatorType)
380*0a6a1f1dSLionel Sambuc return CompleteObjectLocatorType;
381*0a6a1f1dSLionel Sambuc CompleteObjectLocatorType = llvm::StructType::create(
382*0a6a1f1dSLionel Sambuc CGM.getLLVMContext(), "rtti.CompleteObjectLocator");
383*0a6a1f1dSLionel Sambuc llvm::Type *FieldTypes[] = {
384*0a6a1f1dSLionel Sambuc CGM.IntTy,
385*0a6a1f1dSLionel Sambuc CGM.IntTy,
386*0a6a1f1dSLionel Sambuc CGM.IntTy,
387*0a6a1f1dSLionel Sambuc getImageRelativeType(CGM.Int8PtrTy),
388*0a6a1f1dSLionel Sambuc getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()),
389*0a6a1f1dSLionel Sambuc getImageRelativeType(CompleteObjectLocatorType),
390*0a6a1f1dSLionel Sambuc };
391*0a6a1f1dSLionel Sambuc llvm::ArrayRef<llvm::Type *> FieldTypesRef(FieldTypes);
392*0a6a1f1dSLionel Sambuc if (!isImageRelative())
393*0a6a1f1dSLionel Sambuc FieldTypesRef = FieldTypesRef.drop_back();
394*0a6a1f1dSLionel Sambuc CompleteObjectLocatorType->setBody(FieldTypesRef);
395*0a6a1f1dSLionel Sambuc return CompleteObjectLocatorType;
396*0a6a1f1dSLionel Sambuc }
397*0a6a1f1dSLionel Sambuc
getImageBase()398*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *getImageBase() {
399*0a6a1f1dSLionel Sambuc StringRef Name = "__ImageBase";
400*0a6a1f1dSLionel Sambuc if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name))
401*0a6a1f1dSLionel Sambuc return GV;
402*0a6a1f1dSLionel Sambuc
403*0a6a1f1dSLionel Sambuc return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty,
404*0a6a1f1dSLionel Sambuc /*isConstant=*/true,
405*0a6a1f1dSLionel Sambuc llvm::GlobalValue::ExternalLinkage,
406*0a6a1f1dSLionel Sambuc /*Initializer=*/nullptr, Name);
407*0a6a1f1dSLionel Sambuc }
408*0a6a1f1dSLionel Sambuc
getImageRelativeConstant(llvm::Constant * PtrVal)409*0a6a1f1dSLionel Sambuc llvm::Constant *getImageRelativeConstant(llvm::Constant *PtrVal) {
410*0a6a1f1dSLionel Sambuc if (!isImageRelative())
411*0a6a1f1dSLionel Sambuc return PtrVal;
412*0a6a1f1dSLionel Sambuc
413*0a6a1f1dSLionel Sambuc llvm::Constant *ImageBaseAsInt =
414*0a6a1f1dSLionel Sambuc llvm::ConstantExpr::getPtrToInt(getImageBase(), CGM.IntPtrTy);
415*0a6a1f1dSLionel Sambuc llvm::Constant *PtrValAsInt =
416*0a6a1f1dSLionel Sambuc llvm::ConstantExpr::getPtrToInt(PtrVal, CGM.IntPtrTy);
417*0a6a1f1dSLionel Sambuc llvm::Constant *Diff =
418*0a6a1f1dSLionel Sambuc llvm::ConstantExpr::getSub(PtrValAsInt, ImageBaseAsInt,
419*0a6a1f1dSLionel Sambuc /*HasNUW=*/true, /*HasNSW=*/true);
420*0a6a1f1dSLionel Sambuc return llvm::ConstantExpr::getTrunc(Diff, CGM.IntTy);
421*0a6a1f1dSLionel Sambuc }
422f4a2713aSLionel Sambuc
423f4a2713aSLionel Sambuc private:
getMangleContext()424f4a2713aSLionel Sambuc MicrosoftMangleContext &getMangleContext() {
425f4a2713aSLionel Sambuc return cast<MicrosoftMangleContext>(CodeGen::CGCXXABI::getMangleContext());
426f4a2713aSLionel Sambuc }
427f4a2713aSLionel Sambuc
getZeroInt()428f4a2713aSLionel Sambuc llvm::Constant *getZeroInt() {
429f4a2713aSLionel Sambuc return llvm::ConstantInt::get(CGM.IntTy, 0);
430f4a2713aSLionel Sambuc }
431f4a2713aSLionel Sambuc
getAllOnesInt()432f4a2713aSLionel Sambuc llvm::Constant *getAllOnesInt() {
433f4a2713aSLionel Sambuc return llvm::Constant::getAllOnesValue(CGM.IntTy);
434f4a2713aSLionel Sambuc }
435f4a2713aSLionel Sambuc
getConstantOrZeroInt(llvm::Constant * C)436f4a2713aSLionel Sambuc llvm::Constant *getConstantOrZeroInt(llvm::Constant *C) {
437f4a2713aSLionel Sambuc return C ? C : getZeroInt();
438f4a2713aSLionel Sambuc }
439f4a2713aSLionel Sambuc
getValueOrZeroInt(llvm::Value * C)440f4a2713aSLionel Sambuc llvm::Value *getValueOrZeroInt(llvm::Value *C) {
441f4a2713aSLionel Sambuc return C ? C : getZeroInt();
442f4a2713aSLionel Sambuc }
443f4a2713aSLionel Sambuc
444*0a6a1f1dSLionel Sambuc CharUnits getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD);
445*0a6a1f1dSLionel Sambuc
446f4a2713aSLionel Sambuc void
447f4a2713aSLionel Sambuc GetNullMemberPointerFields(const MemberPointerType *MPT,
448f4a2713aSLionel Sambuc llvm::SmallVectorImpl<llvm::Constant *> &fields);
449f4a2713aSLionel Sambuc
450f4a2713aSLionel Sambuc /// \brief Shared code for virtual base adjustment. Returns the offset from
451f4a2713aSLionel Sambuc /// the vbptr to the virtual base. Optionally returns the address of the
452f4a2713aSLionel Sambuc /// vbptr itself.
453f4a2713aSLionel Sambuc llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF,
454f4a2713aSLionel Sambuc llvm::Value *Base,
455f4a2713aSLionel Sambuc llvm::Value *VBPtrOffset,
456f4a2713aSLionel Sambuc llvm::Value *VBTableOffset,
457*0a6a1f1dSLionel Sambuc llvm::Value **VBPtr = nullptr);
458f4a2713aSLionel Sambuc
GetVBaseOffsetFromVBPtr(CodeGenFunction & CGF,llvm::Value * Base,int32_t VBPtrOffset,int32_t VBTableOffset,llvm::Value ** VBPtr=nullptr)459f4a2713aSLionel Sambuc llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF,
460f4a2713aSLionel Sambuc llvm::Value *Base,
461f4a2713aSLionel Sambuc int32_t VBPtrOffset,
462f4a2713aSLionel Sambuc int32_t VBTableOffset,
463*0a6a1f1dSLionel Sambuc llvm::Value **VBPtr = nullptr) {
464*0a6a1f1dSLionel Sambuc assert(VBTableOffset % 4 == 0 && "should be byte offset into table of i32s");
465f4a2713aSLionel Sambuc llvm::Value *VBPOffset = llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset),
466f4a2713aSLionel Sambuc *VBTOffset = llvm::ConstantInt::get(CGM.IntTy, VBTableOffset);
467f4a2713aSLionel Sambuc return GetVBaseOffsetFromVBPtr(CGF, Base, VBPOffset, VBTOffset, VBPtr);
468f4a2713aSLionel Sambuc }
469f4a2713aSLionel Sambuc
470f4a2713aSLionel Sambuc /// \brief Performs a full virtual base adjustment. Used to dereference
471f4a2713aSLionel Sambuc /// pointers to members of virtual bases.
472*0a6a1f1dSLionel Sambuc llvm::Value *AdjustVirtualBase(CodeGenFunction &CGF, const Expr *E,
473*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD, llvm::Value *Base,
474f4a2713aSLionel Sambuc llvm::Value *VirtualBaseAdjustmentOffset,
475f4a2713aSLionel Sambuc llvm::Value *VBPtrOffset /* optional */);
476f4a2713aSLionel Sambuc
477f4a2713aSLionel Sambuc /// \brief Emits a full member pointer with the fields common to data and
478f4a2713aSLionel Sambuc /// function member pointers.
479f4a2713aSLionel Sambuc llvm::Constant *EmitFullMemberPointer(llvm::Constant *FirstField,
480f4a2713aSLionel Sambuc bool IsMemberFunction,
481f4a2713aSLionel Sambuc const CXXRecordDecl *RD,
482f4a2713aSLionel Sambuc CharUnits NonVirtualBaseAdjustment);
483f4a2713aSLionel Sambuc
484f4a2713aSLionel Sambuc llvm::Constant *BuildMemberPointer(const CXXRecordDecl *RD,
485f4a2713aSLionel Sambuc const CXXMethodDecl *MD,
486f4a2713aSLionel Sambuc CharUnits NonVirtualBaseAdjustment);
487f4a2713aSLionel Sambuc
488f4a2713aSLionel Sambuc bool MemberPointerConstantIsNull(const MemberPointerType *MPT,
489f4a2713aSLionel Sambuc llvm::Constant *MP);
490f4a2713aSLionel Sambuc
491f4a2713aSLionel Sambuc /// \brief - Initialize all vbptrs of 'this' with RD as the complete type.
492f4a2713aSLionel Sambuc void EmitVBPtrStores(CodeGenFunction &CGF, const CXXRecordDecl *RD);
493f4a2713aSLionel Sambuc
494f4a2713aSLionel Sambuc /// \brief Caching wrapper around VBTableBuilder::enumerateVBTables().
495*0a6a1f1dSLionel Sambuc const VBTableGlobals &enumerateVBTables(const CXXRecordDecl *RD);
496f4a2713aSLionel Sambuc
497f4a2713aSLionel Sambuc /// \brief Generate a thunk for calling a virtual member function MD.
498*0a6a1f1dSLionel Sambuc llvm::Function *EmitVirtualMemPtrThunk(
499*0a6a1f1dSLionel Sambuc const CXXMethodDecl *MD,
500*0a6a1f1dSLionel Sambuc const MicrosoftVTableContext::MethodVFTableLocation &ML);
501f4a2713aSLionel Sambuc
502f4a2713aSLionel Sambuc public:
503*0a6a1f1dSLionel Sambuc llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override;
504f4a2713aSLionel Sambuc
505*0a6a1f1dSLionel Sambuc bool isZeroInitializable(const MemberPointerType *MPT) override;
506f4a2713aSLionel Sambuc
isMemberPointerConvertible(const MemberPointerType * MPT) const507*0a6a1f1dSLionel Sambuc bool isMemberPointerConvertible(const MemberPointerType *MPT) const override {
508*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
509*0a6a1f1dSLionel Sambuc return RD->hasAttr<MSInheritanceAttr>();
510*0a6a1f1dSLionel Sambuc }
511f4a2713aSLionel Sambuc
isTypeInfoCalculable(QualType Ty) const512*0a6a1f1dSLionel Sambuc bool isTypeInfoCalculable(QualType Ty) const override {
513*0a6a1f1dSLionel Sambuc if (!CGCXXABI::isTypeInfoCalculable(Ty))
514*0a6a1f1dSLionel Sambuc return false;
515*0a6a1f1dSLionel Sambuc if (const auto *MPT = Ty->getAs<MemberPointerType>()) {
516*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
517*0a6a1f1dSLionel Sambuc if (!RD->hasAttr<MSInheritanceAttr>())
518*0a6a1f1dSLionel Sambuc return false;
519*0a6a1f1dSLionel Sambuc }
520*0a6a1f1dSLionel Sambuc return true;
521*0a6a1f1dSLionel Sambuc }
522f4a2713aSLionel Sambuc
523*0a6a1f1dSLionel Sambuc llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override;
524*0a6a1f1dSLionel Sambuc
525*0a6a1f1dSLionel Sambuc llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
526*0a6a1f1dSLionel Sambuc CharUnits offset) override;
527*0a6a1f1dSLionel Sambuc llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD) override;
528*0a6a1f1dSLionel Sambuc llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override;
529*0a6a1f1dSLionel Sambuc
530*0a6a1f1dSLionel Sambuc llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
531f4a2713aSLionel Sambuc llvm::Value *L,
532f4a2713aSLionel Sambuc llvm::Value *R,
533f4a2713aSLionel Sambuc const MemberPointerType *MPT,
534*0a6a1f1dSLionel Sambuc bool Inequality) override;
535f4a2713aSLionel Sambuc
536*0a6a1f1dSLionel Sambuc llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
537f4a2713aSLionel Sambuc llvm::Value *MemPtr,
538*0a6a1f1dSLionel Sambuc const MemberPointerType *MPT) override;
539f4a2713aSLionel Sambuc
540*0a6a1f1dSLionel Sambuc llvm::Value *
541*0a6a1f1dSLionel Sambuc EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
542*0a6a1f1dSLionel Sambuc llvm::Value *Base, llvm::Value *MemPtr,
543*0a6a1f1dSLionel Sambuc const MemberPointerType *MPT) override;
544f4a2713aSLionel Sambuc
545*0a6a1f1dSLionel Sambuc llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
546f4a2713aSLionel Sambuc const CastExpr *E,
547*0a6a1f1dSLionel Sambuc llvm::Value *Src) override;
548f4a2713aSLionel Sambuc
549*0a6a1f1dSLionel Sambuc llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
550*0a6a1f1dSLionel Sambuc llvm::Constant *Src) override;
551f4a2713aSLionel Sambuc
552*0a6a1f1dSLionel Sambuc llvm::Value *
553*0a6a1f1dSLionel Sambuc EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, const Expr *E,
554*0a6a1f1dSLionel Sambuc llvm::Value *&This, llvm::Value *MemPtr,
555*0a6a1f1dSLionel Sambuc const MemberPointerType *MPT) override;
556*0a6a1f1dSLionel Sambuc
557*0a6a1f1dSLionel Sambuc void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) override;
558f4a2713aSLionel Sambuc
559f4a2713aSLionel Sambuc private:
560f4a2713aSLionel Sambuc typedef std::pair<const CXXRecordDecl *, CharUnits> VFTableIdTy;
561*0a6a1f1dSLionel Sambuc typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalVariable *> VTablesMapTy;
562*0a6a1f1dSLionel Sambuc typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalValue *> VFTablesMapTy;
563f4a2713aSLionel Sambuc /// \brief All the vftables that have been referenced.
564f4a2713aSLionel Sambuc VFTablesMapTy VFTablesMap;
565*0a6a1f1dSLionel Sambuc VTablesMapTy VTablesMap;
566f4a2713aSLionel Sambuc
567f4a2713aSLionel Sambuc /// \brief This set holds the record decls we've deferred vtable emission for.
568f4a2713aSLionel Sambuc llvm::SmallPtrSet<const CXXRecordDecl *, 4> DeferredVFTables;
569f4a2713aSLionel Sambuc
570f4a2713aSLionel Sambuc
571f4a2713aSLionel Sambuc /// \brief All the vbtables which have been referenced.
572*0a6a1f1dSLionel Sambuc llvm::DenseMap<const CXXRecordDecl *, VBTableGlobals> VBTablesMap;
573f4a2713aSLionel Sambuc
574f4a2713aSLionel Sambuc /// Info on the global variable used to guard initialization of static locals.
575f4a2713aSLionel Sambuc /// The BitIndex field is only used for externally invisible declarations.
576f4a2713aSLionel Sambuc struct GuardInfo {
GuardInfo__anona9a21ec60111::MicrosoftCXXABI::GuardInfo577*0a6a1f1dSLionel Sambuc GuardInfo() : Guard(nullptr), BitIndex(0) {}
578f4a2713aSLionel Sambuc llvm::GlobalVariable *Guard;
579f4a2713aSLionel Sambuc unsigned BitIndex;
580f4a2713aSLionel Sambuc };
581f4a2713aSLionel Sambuc
582f4a2713aSLionel Sambuc /// Map from DeclContext to the current guard variable. We assume that the
583f4a2713aSLionel Sambuc /// AST is visited in source code order.
584f4a2713aSLionel Sambuc llvm::DenseMap<const DeclContext *, GuardInfo> GuardVariableMap;
585*0a6a1f1dSLionel Sambuc
586*0a6a1f1dSLionel Sambuc llvm::DenseMap<size_t, llvm::StructType *> TypeDescriptorTypeMap;
587*0a6a1f1dSLionel Sambuc llvm::StructType *BaseClassDescriptorType;
588*0a6a1f1dSLionel Sambuc llvm::StructType *ClassHierarchyDescriptorType;
589*0a6a1f1dSLionel Sambuc llvm::StructType *CompleteObjectLocatorType;
590f4a2713aSLionel Sambuc };
591f4a2713aSLionel Sambuc
592f4a2713aSLionel Sambuc }
593f4a2713aSLionel Sambuc
594*0a6a1f1dSLionel Sambuc CGCXXABI::RecordArgABI
getRecordArgABI(const CXXRecordDecl * RD) const595*0a6a1f1dSLionel Sambuc MicrosoftCXXABI::getRecordArgABI(const CXXRecordDecl *RD) const {
596*0a6a1f1dSLionel Sambuc switch (CGM.getTarget().getTriple().getArch()) {
597*0a6a1f1dSLionel Sambuc default:
598*0a6a1f1dSLionel Sambuc // FIXME: Implement for other architectures.
599*0a6a1f1dSLionel Sambuc return RAA_Default;
600*0a6a1f1dSLionel Sambuc
601*0a6a1f1dSLionel Sambuc case llvm::Triple::x86:
602*0a6a1f1dSLionel Sambuc // All record arguments are passed in memory on x86. Decide whether to
603*0a6a1f1dSLionel Sambuc // construct the object directly in argument memory, or to construct the
604*0a6a1f1dSLionel Sambuc // argument elsewhere and copy the bytes during the call.
605*0a6a1f1dSLionel Sambuc
606*0a6a1f1dSLionel Sambuc // If C++ prohibits us from making a copy, construct the arguments directly
607*0a6a1f1dSLionel Sambuc // into argument memory.
608*0a6a1f1dSLionel Sambuc if (!canCopyArgument(RD))
609*0a6a1f1dSLionel Sambuc return RAA_DirectInMemory;
610*0a6a1f1dSLionel Sambuc
611*0a6a1f1dSLionel Sambuc // Otherwise, construct the argument into a temporary and copy the bytes
612*0a6a1f1dSLionel Sambuc // into the outgoing argument memory.
613*0a6a1f1dSLionel Sambuc return RAA_Default;
614*0a6a1f1dSLionel Sambuc
615*0a6a1f1dSLionel Sambuc case llvm::Triple::x86_64:
616*0a6a1f1dSLionel Sambuc // Win64 passes objects with non-trivial copy ctors indirectly.
617*0a6a1f1dSLionel Sambuc if (RD->hasNonTrivialCopyConstructor())
618*0a6a1f1dSLionel Sambuc return RAA_Indirect;
619*0a6a1f1dSLionel Sambuc
620*0a6a1f1dSLionel Sambuc // If an object has a destructor, we'd really like to pass it indirectly
621*0a6a1f1dSLionel Sambuc // because it allows us to elide copies. Unfortunately, MSVC makes that
622*0a6a1f1dSLionel Sambuc // impossible for small types, which it will pass in a single register or
623*0a6a1f1dSLionel Sambuc // stack slot. Most objects with dtors are large-ish, so handle that early.
624*0a6a1f1dSLionel Sambuc // We can't call out all large objects as being indirect because there are
625*0a6a1f1dSLionel Sambuc // multiple x64 calling conventions and the C++ ABI code shouldn't dictate
626*0a6a1f1dSLionel Sambuc // how we pass large POD types.
627*0a6a1f1dSLionel Sambuc if (RD->hasNonTrivialDestructor() &&
628*0a6a1f1dSLionel Sambuc getContext().getTypeSize(RD->getTypeForDecl()) > 64)
629*0a6a1f1dSLionel Sambuc return RAA_Indirect;
630*0a6a1f1dSLionel Sambuc
631*0a6a1f1dSLionel Sambuc // We have a trivial copy constructor or no copy constructors, but we have
632*0a6a1f1dSLionel Sambuc // to make sure it isn't deleted.
633*0a6a1f1dSLionel Sambuc bool CopyDeleted = false;
634*0a6a1f1dSLionel Sambuc for (const CXXConstructorDecl *CD : RD->ctors()) {
635*0a6a1f1dSLionel Sambuc if (CD->isCopyConstructor()) {
636*0a6a1f1dSLionel Sambuc assert(CD->isTrivial());
637*0a6a1f1dSLionel Sambuc // We had at least one undeleted trivial copy ctor. Return directly.
638*0a6a1f1dSLionel Sambuc if (!CD->isDeleted())
639*0a6a1f1dSLionel Sambuc return RAA_Default;
640*0a6a1f1dSLionel Sambuc CopyDeleted = true;
641*0a6a1f1dSLionel Sambuc }
642f4a2713aSLionel Sambuc }
643f4a2713aSLionel Sambuc
644*0a6a1f1dSLionel Sambuc // The trivial copy constructor was deleted. Return indirectly.
645*0a6a1f1dSLionel Sambuc if (CopyDeleted)
646*0a6a1f1dSLionel Sambuc return RAA_Indirect;
647*0a6a1f1dSLionel Sambuc
648*0a6a1f1dSLionel Sambuc // There were no copy ctors. Return in RAX.
649*0a6a1f1dSLionel Sambuc return RAA_Default;
650f4a2713aSLionel Sambuc }
651f4a2713aSLionel Sambuc
652*0a6a1f1dSLionel Sambuc llvm_unreachable("invalid enum");
653f4a2713aSLionel Sambuc }
654*0a6a1f1dSLionel Sambuc
emitVirtualObjectDelete(CodeGenFunction & CGF,const CXXDeleteExpr * DE,llvm::Value * Ptr,QualType ElementType,const CXXDestructorDecl * Dtor)655*0a6a1f1dSLionel Sambuc void MicrosoftCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
656*0a6a1f1dSLionel Sambuc const CXXDeleteExpr *DE,
657*0a6a1f1dSLionel Sambuc llvm::Value *Ptr,
658*0a6a1f1dSLionel Sambuc QualType ElementType,
659*0a6a1f1dSLionel Sambuc const CXXDestructorDecl *Dtor) {
660*0a6a1f1dSLionel Sambuc // FIXME: Provide a source location here even though there's no
661*0a6a1f1dSLionel Sambuc // CXXMemberCallExpr for dtor call.
662*0a6a1f1dSLionel Sambuc bool UseGlobalDelete = DE->isGlobalDelete();
663*0a6a1f1dSLionel Sambuc CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting;
664*0a6a1f1dSLionel Sambuc llvm::Value *MDThis =
665*0a6a1f1dSLionel Sambuc EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr);
666*0a6a1f1dSLionel Sambuc if (UseGlobalDelete)
667*0a6a1f1dSLionel Sambuc CGF.EmitDeleteCall(DE->getOperatorDelete(), MDThis, ElementType);
668f4a2713aSLionel Sambuc }
669*0a6a1f1dSLionel Sambuc
getRethrowFn(CodeGenModule & CGM)670*0a6a1f1dSLionel Sambuc static llvm::Function *getRethrowFn(CodeGenModule &CGM) {
671*0a6a1f1dSLionel Sambuc // _CxxThrowException takes two pointer width arguments: a value and a context
672*0a6a1f1dSLionel Sambuc // object which points to a TypeInfo object.
673*0a6a1f1dSLionel Sambuc llvm::Type *ArgTypes[] = {CGM.Int8PtrTy, CGM.Int8PtrTy};
674*0a6a1f1dSLionel Sambuc llvm::FunctionType *FTy =
675*0a6a1f1dSLionel Sambuc llvm::FunctionType::get(CGM.VoidTy, ArgTypes, false);
676*0a6a1f1dSLionel Sambuc auto *Fn = cast<llvm::Function>(
677*0a6a1f1dSLionel Sambuc CGM.CreateRuntimeFunction(FTy, "_CxxThrowException"));
678*0a6a1f1dSLionel Sambuc // _CxxThrowException is stdcall on 32-bit x86 platforms.
679*0a6a1f1dSLionel Sambuc if (CGM.getTarget().getTriple().getArch() == llvm::Triple::x86)
680*0a6a1f1dSLionel Sambuc Fn->setCallingConv(llvm::CallingConv::X86_StdCall);
681*0a6a1f1dSLionel Sambuc return Fn;
682*0a6a1f1dSLionel Sambuc }
683*0a6a1f1dSLionel Sambuc
emitRethrow(CodeGenFunction & CGF,bool isNoReturn)684*0a6a1f1dSLionel Sambuc void MicrosoftCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) {
685*0a6a1f1dSLionel Sambuc llvm::Value *Args[] = {llvm::ConstantPointerNull::get(CGM.Int8PtrTy),
686*0a6a1f1dSLionel Sambuc llvm::ConstantPointerNull::get(CGM.Int8PtrTy)};
687*0a6a1f1dSLionel Sambuc auto *Fn = getRethrowFn(CGM);
688*0a6a1f1dSLionel Sambuc if (isNoReturn)
689*0a6a1f1dSLionel Sambuc CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, Args);
690*0a6a1f1dSLionel Sambuc else
691*0a6a1f1dSLionel Sambuc CGF.EmitRuntimeCallOrInvoke(Fn, Args);
692*0a6a1f1dSLionel Sambuc }
693*0a6a1f1dSLionel Sambuc
694*0a6a1f1dSLionel Sambuc /// \brief Gets the offset to the virtual base that contains the vfptr for
695*0a6a1f1dSLionel Sambuc /// MS-ABI polymorphic types.
getPolymorphicOffset(CodeGenFunction & CGF,const CXXRecordDecl * RD,llvm::Value * Value)696*0a6a1f1dSLionel Sambuc static llvm::Value *getPolymorphicOffset(CodeGenFunction &CGF,
697*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD,
698*0a6a1f1dSLionel Sambuc llvm::Value *Value) {
699*0a6a1f1dSLionel Sambuc const ASTContext &Context = RD->getASTContext();
700*0a6a1f1dSLionel Sambuc for (const CXXBaseSpecifier &Base : RD->vbases())
701*0a6a1f1dSLionel Sambuc if (Context.getASTRecordLayout(Base.getType()->getAsCXXRecordDecl())
702*0a6a1f1dSLionel Sambuc .hasExtendableVFPtr())
703*0a6a1f1dSLionel Sambuc return CGF.CGM.getCXXABI().GetVirtualBaseClassOffset(
704*0a6a1f1dSLionel Sambuc CGF, Value, RD, Base.getType()->getAsCXXRecordDecl());
705*0a6a1f1dSLionel Sambuc llvm_unreachable("One of our vbases should be polymorphic.");
706*0a6a1f1dSLionel Sambuc }
707*0a6a1f1dSLionel Sambuc
708*0a6a1f1dSLionel Sambuc static std::pair<llvm::Value *, llvm::Value *>
performBaseAdjustment(CodeGenFunction & CGF,llvm::Value * Value,QualType SrcRecordTy)709*0a6a1f1dSLionel Sambuc performBaseAdjustment(CodeGenFunction &CGF, llvm::Value *Value,
710*0a6a1f1dSLionel Sambuc QualType SrcRecordTy) {
711*0a6a1f1dSLionel Sambuc Value = CGF.Builder.CreateBitCast(Value, CGF.Int8PtrTy);
712*0a6a1f1dSLionel Sambuc const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
713*0a6a1f1dSLionel Sambuc
714*0a6a1f1dSLionel Sambuc if (CGF.getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr())
715*0a6a1f1dSLionel Sambuc return std::make_pair(Value, llvm::ConstantInt::get(CGF.Int32Ty, 0));
716*0a6a1f1dSLionel Sambuc
717*0a6a1f1dSLionel Sambuc // Perform a base adjustment.
718*0a6a1f1dSLionel Sambuc llvm::Value *Offset = getPolymorphicOffset(CGF, SrcDecl, Value);
719*0a6a1f1dSLionel Sambuc Value = CGF.Builder.CreateInBoundsGEP(Value, Offset);
720*0a6a1f1dSLionel Sambuc Offset = CGF.Builder.CreateTrunc(Offset, CGF.Int32Ty);
721*0a6a1f1dSLionel Sambuc return std::make_pair(Value, Offset);
722*0a6a1f1dSLionel Sambuc }
723*0a6a1f1dSLionel Sambuc
shouldTypeidBeNullChecked(bool IsDeref,QualType SrcRecordTy)724*0a6a1f1dSLionel Sambuc bool MicrosoftCXXABI::shouldTypeidBeNullChecked(bool IsDeref,
725*0a6a1f1dSLionel Sambuc QualType SrcRecordTy) {
726*0a6a1f1dSLionel Sambuc const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
727*0a6a1f1dSLionel Sambuc return IsDeref &&
728*0a6a1f1dSLionel Sambuc !CGM.getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr();
729*0a6a1f1dSLionel Sambuc }
730*0a6a1f1dSLionel Sambuc
emitRTtypeidCall(CodeGenFunction & CGF,llvm::Value * Argument)731*0a6a1f1dSLionel Sambuc static llvm::CallSite emitRTtypeidCall(CodeGenFunction &CGF,
732*0a6a1f1dSLionel Sambuc llvm::Value *Argument) {
733*0a6a1f1dSLionel Sambuc llvm::Type *ArgTypes[] = {CGF.Int8PtrTy};
734*0a6a1f1dSLionel Sambuc llvm::FunctionType *FTy =
735*0a6a1f1dSLionel Sambuc llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false);
736*0a6a1f1dSLionel Sambuc llvm::Value *Args[] = {Argument};
737*0a6a1f1dSLionel Sambuc llvm::Constant *Fn = CGF.CGM.CreateRuntimeFunction(FTy, "__RTtypeid");
738*0a6a1f1dSLionel Sambuc return CGF.EmitRuntimeCallOrInvoke(Fn, Args);
739*0a6a1f1dSLionel Sambuc }
740*0a6a1f1dSLionel Sambuc
EmitBadTypeidCall(CodeGenFunction & CGF)741*0a6a1f1dSLionel Sambuc void MicrosoftCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) {
742*0a6a1f1dSLionel Sambuc llvm::CallSite Call =
743*0a6a1f1dSLionel Sambuc emitRTtypeidCall(CGF, llvm::Constant::getNullValue(CGM.VoidPtrTy));
744*0a6a1f1dSLionel Sambuc Call.setDoesNotReturn();
745*0a6a1f1dSLionel Sambuc CGF.Builder.CreateUnreachable();
746*0a6a1f1dSLionel Sambuc }
747*0a6a1f1dSLionel Sambuc
EmitTypeid(CodeGenFunction & CGF,QualType SrcRecordTy,llvm::Value * ThisPtr,llvm::Type * StdTypeInfoPtrTy)748*0a6a1f1dSLionel Sambuc llvm::Value *MicrosoftCXXABI::EmitTypeid(CodeGenFunction &CGF,
749*0a6a1f1dSLionel Sambuc QualType SrcRecordTy,
750*0a6a1f1dSLionel Sambuc llvm::Value *ThisPtr,
751*0a6a1f1dSLionel Sambuc llvm::Type *StdTypeInfoPtrTy) {
752*0a6a1f1dSLionel Sambuc llvm::Value *Offset;
753*0a6a1f1dSLionel Sambuc std::tie(ThisPtr, Offset) = performBaseAdjustment(CGF, ThisPtr, SrcRecordTy);
754*0a6a1f1dSLionel Sambuc return CGF.Builder.CreateBitCast(
755*0a6a1f1dSLionel Sambuc emitRTtypeidCall(CGF, ThisPtr).getInstruction(), StdTypeInfoPtrTy);
756*0a6a1f1dSLionel Sambuc }
757*0a6a1f1dSLionel Sambuc
shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,QualType SrcRecordTy)758*0a6a1f1dSLionel Sambuc bool MicrosoftCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
759*0a6a1f1dSLionel Sambuc QualType SrcRecordTy) {
760*0a6a1f1dSLionel Sambuc const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
761*0a6a1f1dSLionel Sambuc return SrcIsPtr &&
762*0a6a1f1dSLionel Sambuc !CGM.getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr();
763*0a6a1f1dSLionel Sambuc }
764*0a6a1f1dSLionel Sambuc
EmitDynamicCastCall(CodeGenFunction & CGF,llvm::Value * Value,QualType SrcRecordTy,QualType DestTy,QualType DestRecordTy,llvm::BasicBlock * CastEnd)765*0a6a1f1dSLionel Sambuc llvm::Value *MicrosoftCXXABI::EmitDynamicCastCall(
766*0a6a1f1dSLionel Sambuc CodeGenFunction &CGF, llvm::Value *Value, QualType SrcRecordTy,
767*0a6a1f1dSLionel Sambuc QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) {
768*0a6a1f1dSLionel Sambuc llvm::Type *DestLTy = CGF.ConvertType(DestTy);
769*0a6a1f1dSLionel Sambuc
770*0a6a1f1dSLionel Sambuc llvm::Value *SrcRTTI =
771*0a6a1f1dSLionel Sambuc CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType());
772*0a6a1f1dSLionel Sambuc llvm::Value *DestRTTI =
773*0a6a1f1dSLionel Sambuc CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType());
774*0a6a1f1dSLionel Sambuc
775*0a6a1f1dSLionel Sambuc llvm::Value *Offset;
776*0a6a1f1dSLionel Sambuc std::tie(Value, Offset) = performBaseAdjustment(CGF, Value, SrcRecordTy);
777*0a6a1f1dSLionel Sambuc
778*0a6a1f1dSLionel Sambuc // PVOID __RTDynamicCast(
779*0a6a1f1dSLionel Sambuc // PVOID inptr,
780*0a6a1f1dSLionel Sambuc // LONG VfDelta,
781*0a6a1f1dSLionel Sambuc // PVOID SrcType,
782*0a6a1f1dSLionel Sambuc // PVOID TargetType,
783*0a6a1f1dSLionel Sambuc // BOOL isReference)
784*0a6a1f1dSLionel Sambuc llvm::Type *ArgTypes[] = {CGF.Int8PtrTy, CGF.Int32Ty, CGF.Int8PtrTy,
785*0a6a1f1dSLionel Sambuc CGF.Int8PtrTy, CGF.Int32Ty};
786*0a6a1f1dSLionel Sambuc llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction(
787*0a6a1f1dSLionel Sambuc llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false),
788*0a6a1f1dSLionel Sambuc "__RTDynamicCast");
789*0a6a1f1dSLionel Sambuc llvm::Value *Args[] = {
790*0a6a1f1dSLionel Sambuc Value, Offset, SrcRTTI, DestRTTI,
791*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(CGF.Int32Ty, DestTy->isReferenceType())};
792*0a6a1f1dSLionel Sambuc Value = CGF.EmitRuntimeCallOrInvoke(Function, Args).getInstruction();
793*0a6a1f1dSLionel Sambuc return CGF.Builder.CreateBitCast(Value, DestLTy);
794f4a2713aSLionel Sambuc }
795f4a2713aSLionel Sambuc
796f4a2713aSLionel Sambuc llvm::Value *
EmitDynamicCastToVoid(CodeGenFunction & CGF,llvm::Value * Value,QualType SrcRecordTy,QualType DestTy)797*0a6a1f1dSLionel Sambuc MicrosoftCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value,
798*0a6a1f1dSLionel Sambuc QualType SrcRecordTy,
799*0a6a1f1dSLionel Sambuc QualType DestTy) {
800*0a6a1f1dSLionel Sambuc llvm::Value *Offset;
801*0a6a1f1dSLionel Sambuc std::tie(Value, Offset) = performBaseAdjustment(CGF, Value, SrcRecordTy);
802*0a6a1f1dSLionel Sambuc
803*0a6a1f1dSLionel Sambuc // PVOID __RTCastToVoid(
804*0a6a1f1dSLionel Sambuc // PVOID inptr)
805*0a6a1f1dSLionel Sambuc llvm::Type *ArgTypes[] = {CGF.Int8PtrTy};
806*0a6a1f1dSLionel Sambuc llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction(
807*0a6a1f1dSLionel Sambuc llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false),
808*0a6a1f1dSLionel Sambuc "__RTCastToVoid");
809*0a6a1f1dSLionel Sambuc llvm::Value *Args[] = {Value};
810*0a6a1f1dSLionel Sambuc return CGF.EmitRuntimeCall(Function, Args);
811*0a6a1f1dSLionel Sambuc }
812*0a6a1f1dSLionel Sambuc
EmitBadCastCall(CodeGenFunction & CGF)813*0a6a1f1dSLionel Sambuc bool MicrosoftCXXABI::EmitBadCastCall(CodeGenFunction &CGF) {
814*0a6a1f1dSLionel Sambuc return false;
815*0a6a1f1dSLionel Sambuc }
816*0a6a1f1dSLionel Sambuc
GetVirtualBaseClassOffset(CodeGenFunction & CGF,llvm::Value * This,const CXXRecordDecl * ClassDecl,const CXXRecordDecl * BaseClassDecl)817*0a6a1f1dSLionel Sambuc llvm::Value *MicrosoftCXXABI::GetVirtualBaseClassOffset(
818*0a6a1f1dSLionel Sambuc CodeGenFunction &CGF, llvm::Value *This, const CXXRecordDecl *ClassDecl,
819f4a2713aSLionel Sambuc const CXXRecordDecl *BaseClassDecl) {
820*0a6a1f1dSLionel Sambuc int64_t VBPtrChars =
821*0a6a1f1dSLionel Sambuc getContext().getASTRecordLayout(ClassDecl).getVBPtrOffset().getQuantity();
822f4a2713aSLionel Sambuc llvm::Value *VBPtrOffset = llvm::ConstantInt::get(CGM.PtrDiffTy, VBPtrChars);
823f4a2713aSLionel Sambuc CharUnits IntSize = getContext().getTypeSizeInChars(getContext().IntTy);
824f4a2713aSLionel Sambuc CharUnits VBTableChars =
825f4a2713aSLionel Sambuc IntSize *
826f4a2713aSLionel Sambuc CGM.getMicrosoftVTableContext().getVBTableIndex(ClassDecl, BaseClassDecl);
827f4a2713aSLionel Sambuc llvm::Value *VBTableOffset =
828f4a2713aSLionel Sambuc llvm::ConstantInt::get(CGM.IntTy, VBTableChars.getQuantity());
829f4a2713aSLionel Sambuc
830f4a2713aSLionel Sambuc llvm::Value *VBPtrToNewBase =
831f4a2713aSLionel Sambuc GetVBaseOffsetFromVBPtr(CGF, This, VBPtrOffset, VBTableOffset);
832f4a2713aSLionel Sambuc VBPtrToNewBase =
833f4a2713aSLionel Sambuc CGF.Builder.CreateSExtOrBitCast(VBPtrToNewBase, CGM.PtrDiffTy);
834f4a2713aSLionel Sambuc return CGF.Builder.CreateNSWAdd(VBPtrOffset, VBPtrToNewBase);
835f4a2713aSLionel Sambuc }
836f4a2713aSLionel Sambuc
HasThisReturn(GlobalDecl GD) const837f4a2713aSLionel Sambuc bool MicrosoftCXXABI::HasThisReturn(GlobalDecl GD) const {
838f4a2713aSLionel Sambuc return isa<CXXConstructorDecl>(GD.getDecl());
839f4a2713aSLionel Sambuc }
840f4a2713aSLionel Sambuc
isDeletingDtor(GlobalDecl GD)841*0a6a1f1dSLionel Sambuc static bool isDeletingDtor(GlobalDecl GD) {
842*0a6a1f1dSLionel Sambuc return isa<CXXDestructorDecl>(GD.getDecl()) &&
843*0a6a1f1dSLionel Sambuc GD.getDtorType() == Dtor_Deleting;
844f4a2713aSLionel Sambuc }
845*0a6a1f1dSLionel Sambuc
hasMostDerivedReturn(GlobalDecl GD) const846*0a6a1f1dSLionel Sambuc bool MicrosoftCXXABI::hasMostDerivedReturn(GlobalDecl GD) const {
847*0a6a1f1dSLionel Sambuc return isDeletingDtor(GD);
848*0a6a1f1dSLionel Sambuc }
849*0a6a1f1dSLionel Sambuc
classifyReturnType(CGFunctionInfo & FI) const850*0a6a1f1dSLionel Sambuc bool MicrosoftCXXABI::classifyReturnType(CGFunctionInfo &FI) const {
851*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl();
852*0a6a1f1dSLionel Sambuc if (!RD)
853*0a6a1f1dSLionel Sambuc return false;
854*0a6a1f1dSLionel Sambuc
855*0a6a1f1dSLionel Sambuc if (FI.isInstanceMethod()) {
856*0a6a1f1dSLionel Sambuc // If it's an instance method, aggregates are always returned indirectly via
857*0a6a1f1dSLionel Sambuc // the second parameter.
858*0a6a1f1dSLionel Sambuc FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false);
859*0a6a1f1dSLionel Sambuc FI.getReturnInfo().setSRetAfterThis(FI.isInstanceMethod());
860*0a6a1f1dSLionel Sambuc return true;
861*0a6a1f1dSLionel Sambuc } else if (!RD->isPOD()) {
862*0a6a1f1dSLionel Sambuc // If it's a free function, non-POD types are returned indirectly.
863*0a6a1f1dSLionel Sambuc FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false);
864*0a6a1f1dSLionel Sambuc return true;
865*0a6a1f1dSLionel Sambuc }
866*0a6a1f1dSLionel Sambuc
867*0a6a1f1dSLionel Sambuc // Otherwise, use the C ABI rules.
868*0a6a1f1dSLionel Sambuc return false;
869f4a2713aSLionel Sambuc }
870f4a2713aSLionel Sambuc
871f4a2713aSLionel Sambuc llvm::BasicBlock *
EmitCtorCompleteObjectHandler(CodeGenFunction & CGF,const CXXRecordDecl * RD)872f4a2713aSLionel Sambuc MicrosoftCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
873f4a2713aSLionel Sambuc const CXXRecordDecl *RD) {
874f4a2713aSLionel Sambuc llvm::Value *IsMostDerivedClass = getStructorImplicitParamValue(CGF);
875f4a2713aSLionel Sambuc assert(IsMostDerivedClass &&
876f4a2713aSLionel Sambuc "ctor for a class with virtual bases must have an implicit parameter");
877f4a2713aSLionel Sambuc llvm::Value *IsCompleteObject =
878f4a2713aSLionel Sambuc CGF.Builder.CreateIsNotNull(IsMostDerivedClass, "is_complete_object");
879f4a2713aSLionel Sambuc
880f4a2713aSLionel Sambuc llvm::BasicBlock *CallVbaseCtorsBB = CGF.createBasicBlock("ctor.init_vbases");
881f4a2713aSLionel Sambuc llvm::BasicBlock *SkipVbaseCtorsBB = CGF.createBasicBlock("ctor.skip_vbases");
882f4a2713aSLionel Sambuc CGF.Builder.CreateCondBr(IsCompleteObject,
883f4a2713aSLionel Sambuc CallVbaseCtorsBB, SkipVbaseCtorsBB);
884f4a2713aSLionel Sambuc
885f4a2713aSLionel Sambuc CGF.EmitBlock(CallVbaseCtorsBB);
886f4a2713aSLionel Sambuc
887f4a2713aSLionel Sambuc // Fill in the vbtable pointers here.
888f4a2713aSLionel Sambuc EmitVBPtrStores(CGF, RD);
889f4a2713aSLionel Sambuc
890f4a2713aSLionel Sambuc // CGF will put the base ctor calls in this basic block for us later.
891f4a2713aSLionel Sambuc
892f4a2713aSLionel Sambuc return SkipVbaseCtorsBB;
893f4a2713aSLionel Sambuc }
894f4a2713aSLionel Sambuc
initializeHiddenVirtualInheritanceMembers(CodeGenFunction & CGF,const CXXRecordDecl * RD)895f4a2713aSLionel Sambuc void MicrosoftCXXABI::initializeHiddenVirtualInheritanceMembers(
896f4a2713aSLionel Sambuc CodeGenFunction &CGF, const CXXRecordDecl *RD) {
897f4a2713aSLionel Sambuc // In most cases, an override for a vbase virtual method can adjust
898f4a2713aSLionel Sambuc // the "this" parameter by applying a constant offset.
899f4a2713aSLionel Sambuc // However, this is not enough while a constructor or a destructor of some
900f4a2713aSLionel Sambuc // class X is being executed if all the following conditions are met:
901f4a2713aSLionel Sambuc // - X has virtual bases, (1)
902f4a2713aSLionel Sambuc // - X overrides a virtual method M of a vbase Y, (2)
903f4a2713aSLionel Sambuc // - X itself is a vbase of the most derived class.
904f4a2713aSLionel Sambuc //
905f4a2713aSLionel Sambuc // If (1) and (2) are true, the vtorDisp for vbase Y is a hidden member of X
906f4a2713aSLionel Sambuc // which holds the extra amount of "this" adjustment we must do when we use
907f4a2713aSLionel Sambuc // the X vftables (i.e. during X ctor or dtor).
908f4a2713aSLionel Sambuc // Outside the ctors and dtors, the values of vtorDisps are zero.
909f4a2713aSLionel Sambuc
910f4a2713aSLionel Sambuc const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
911f4a2713aSLionel Sambuc typedef ASTRecordLayout::VBaseOffsetsMapTy VBOffsets;
912f4a2713aSLionel Sambuc const VBOffsets &VBaseMap = Layout.getVBaseOffsetsMap();
913f4a2713aSLionel Sambuc CGBuilderTy &Builder = CGF.Builder;
914f4a2713aSLionel Sambuc
915f4a2713aSLionel Sambuc unsigned AS =
916f4a2713aSLionel Sambuc cast<llvm::PointerType>(getThisValue(CGF)->getType())->getAddressSpace();
917*0a6a1f1dSLionel Sambuc llvm::Value *Int8This = nullptr; // Initialize lazily.
918f4a2713aSLionel Sambuc
919f4a2713aSLionel Sambuc for (VBOffsets::const_iterator I = VBaseMap.begin(), E = VBaseMap.end();
920f4a2713aSLionel Sambuc I != E; ++I) {
921f4a2713aSLionel Sambuc if (!I->second.hasVtorDisp())
922f4a2713aSLionel Sambuc continue;
923f4a2713aSLionel Sambuc
924f4a2713aSLionel Sambuc llvm::Value *VBaseOffset =
925f4a2713aSLionel Sambuc GetVirtualBaseClassOffset(CGF, getThisValue(CGF), RD, I->first);
926f4a2713aSLionel Sambuc // FIXME: it doesn't look right that we SExt in GetVirtualBaseClassOffset()
927f4a2713aSLionel Sambuc // just to Trunc back immediately.
928f4a2713aSLionel Sambuc VBaseOffset = Builder.CreateTruncOrBitCast(VBaseOffset, CGF.Int32Ty);
929f4a2713aSLionel Sambuc uint64_t ConstantVBaseOffset =
930f4a2713aSLionel Sambuc Layout.getVBaseClassOffset(I->first).getQuantity();
931f4a2713aSLionel Sambuc
932f4a2713aSLionel Sambuc // vtorDisp_for_vbase = vbptr[vbase_idx] - offsetof(RD, vbase).
933f4a2713aSLionel Sambuc llvm::Value *VtorDispValue = Builder.CreateSub(
934f4a2713aSLionel Sambuc VBaseOffset, llvm::ConstantInt::get(CGM.Int32Ty, ConstantVBaseOffset),
935f4a2713aSLionel Sambuc "vtordisp.value");
936f4a2713aSLionel Sambuc
937f4a2713aSLionel Sambuc if (!Int8This)
938f4a2713aSLionel Sambuc Int8This = Builder.CreateBitCast(getThisValue(CGF),
939f4a2713aSLionel Sambuc CGF.Int8Ty->getPointerTo(AS));
940f4a2713aSLionel Sambuc llvm::Value *VtorDispPtr = Builder.CreateInBoundsGEP(Int8This, VBaseOffset);
941f4a2713aSLionel Sambuc // vtorDisp is always the 32-bits before the vbase in the class layout.
942f4a2713aSLionel Sambuc VtorDispPtr = Builder.CreateConstGEP1_32(VtorDispPtr, -4);
943f4a2713aSLionel Sambuc VtorDispPtr = Builder.CreateBitCast(
944f4a2713aSLionel Sambuc VtorDispPtr, CGF.Int32Ty->getPointerTo(AS), "vtordisp.ptr");
945f4a2713aSLionel Sambuc
946f4a2713aSLionel Sambuc Builder.CreateStore(VtorDispValue, VtorDispPtr);
947f4a2713aSLionel Sambuc }
948f4a2713aSLionel Sambuc }
949f4a2713aSLionel Sambuc
EmitCXXConstructors(const CXXConstructorDecl * D)950f4a2713aSLionel Sambuc void MicrosoftCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) {
951f4a2713aSLionel Sambuc // There's only one constructor type in this ABI.
952f4a2713aSLionel Sambuc CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete));
953f4a2713aSLionel Sambuc }
954f4a2713aSLionel Sambuc
EmitVBPtrStores(CodeGenFunction & CGF,const CXXRecordDecl * RD)955f4a2713aSLionel Sambuc void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF,
956f4a2713aSLionel Sambuc const CXXRecordDecl *RD) {
957f4a2713aSLionel Sambuc llvm::Value *ThisInt8Ptr =
958f4a2713aSLionel Sambuc CGF.Builder.CreateBitCast(getThisValue(CGF), CGM.Int8PtrTy, "this.int8");
959*0a6a1f1dSLionel Sambuc const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
960f4a2713aSLionel Sambuc
961*0a6a1f1dSLionel Sambuc const VBTableGlobals &VBGlobals = enumerateVBTables(RD);
962*0a6a1f1dSLionel Sambuc for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) {
963*0a6a1f1dSLionel Sambuc const VPtrInfo *VBT = (*VBGlobals.VBTables)[I];
964*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *GV = VBGlobals.Globals[I];
965f4a2713aSLionel Sambuc const ASTRecordLayout &SubobjectLayout =
966*0a6a1f1dSLionel Sambuc CGM.getContext().getASTRecordLayout(VBT->BaseWithVPtr);
967*0a6a1f1dSLionel Sambuc CharUnits Offs = VBT->NonVirtualOffset;
968*0a6a1f1dSLionel Sambuc Offs += SubobjectLayout.getVBPtrOffset();
969*0a6a1f1dSLionel Sambuc if (VBT->getVBaseWithVPtr())
970*0a6a1f1dSLionel Sambuc Offs += Layout.getVBaseClassOffset(VBT->getVBaseWithVPtr());
971f4a2713aSLionel Sambuc llvm::Value *VBPtr =
972*0a6a1f1dSLionel Sambuc CGF.Builder.CreateConstInBoundsGEP1_64(ThisInt8Ptr, Offs.getQuantity());
973*0a6a1f1dSLionel Sambuc llvm::Value *GVPtr = CGF.Builder.CreateConstInBoundsGEP2_32(GV, 0, 0);
974*0a6a1f1dSLionel Sambuc VBPtr = CGF.Builder.CreateBitCast(VBPtr, GVPtr->getType()->getPointerTo(0),
975*0a6a1f1dSLionel Sambuc "vbptr." + VBT->ReusingBase->getName());
976*0a6a1f1dSLionel Sambuc CGF.Builder.CreateStore(GVPtr, VBPtr);
977f4a2713aSLionel Sambuc }
978f4a2713aSLionel Sambuc }
979f4a2713aSLionel Sambuc
980*0a6a1f1dSLionel Sambuc void
buildStructorSignature(const CXXMethodDecl * MD,StructorType T,SmallVectorImpl<CanQualType> & ArgTys)981*0a6a1f1dSLionel Sambuc MicrosoftCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
982f4a2713aSLionel Sambuc SmallVectorImpl<CanQualType> &ArgTys) {
983f4a2713aSLionel Sambuc // TODO: 'for base' flag
984*0a6a1f1dSLionel Sambuc if (T == StructorType::Deleting) {
985f4a2713aSLionel Sambuc // The scalar deleting destructor takes an implicit int parameter.
986f4a2713aSLionel Sambuc ArgTys.push_back(CGM.getContext().IntTy);
987f4a2713aSLionel Sambuc }
988*0a6a1f1dSLionel Sambuc auto *CD = dyn_cast<CXXConstructorDecl>(MD);
989*0a6a1f1dSLionel Sambuc if (!CD)
990*0a6a1f1dSLionel Sambuc return;
991*0a6a1f1dSLionel Sambuc
992*0a6a1f1dSLionel Sambuc // All parameters are already in place except is_most_derived, which goes
993*0a6a1f1dSLionel Sambuc // after 'this' if it's variadic and last if it's not.
994*0a6a1f1dSLionel Sambuc
995*0a6a1f1dSLionel Sambuc const CXXRecordDecl *Class = CD->getParent();
996*0a6a1f1dSLionel Sambuc const FunctionProtoType *FPT = CD->getType()->castAs<FunctionProtoType>();
997*0a6a1f1dSLionel Sambuc if (Class->getNumVBases()) {
998*0a6a1f1dSLionel Sambuc if (FPT->isVariadic())
999*0a6a1f1dSLionel Sambuc ArgTys.insert(ArgTys.begin() + 1, CGM.getContext().IntTy);
1000*0a6a1f1dSLionel Sambuc else
1001*0a6a1f1dSLionel Sambuc ArgTys.push_back(CGM.getContext().IntTy);
1002*0a6a1f1dSLionel Sambuc }
1003f4a2713aSLionel Sambuc }
1004f4a2713aSLionel Sambuc
EmitCXXDestructors(const CXXDestructorDecl * D)1005f4a2713aSLionel Sambuc void MicrosoftCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) {
1006f4a2713aSLionel Sambuc // The TU defining a dtor is only guaranteed to emit a base destructor. All
1007f4a2713aSLionel Sambuc // other destructor variants are delegating thunks.
1008f4a2713aSLionel Sambuc CGM.EmitGlobal(GlobalDecl(D, Dtor_Base));
1009f4a2713aSLionel Sambuc }
1010f4a2713aSLionel Sambuc
1011*0a6a1f1dSLionel Sambuc CharUnits
getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD)1012*0a6a1f1dSLionel Sambuc MicrosoftCXXABI::getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) {
1013f4a2713aSLionel Sambuc GD = GD.getCanonicalDecl();
1014f4a2713aSLionel Sambuc const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1015*0a6a1f1dSLionel Sambuc
1016*0a6a1f1dSLionel Sambuc GlobalDecl LookupGD = GD;
1017*0a6a1f1dSLionel Sambuc if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1018*0a6a1f1dSLionel Sambuc // Complete destructors take a pointer to the complete object as a
1019*0a6a1f1dSLionel Sambuc // parameter, thus don't need this adjustment.
1020*0a6a1f1dSLionel Sambuc if (GD.getDtorType() == Dtor_Complete)
1021*0a6a1f1dSLionel Sambuc return CharUnits();
1022*0a6a1f1dSLionel Sambuc
1023*0a6a1f1dSLionel Sambuc // There's no Dtor_Base in vftable but it shares the this adjustment with
1024*0a6a1f1dSLionel Sambuc // the deleting one, so look it up instead.
1025*0a6a1f1dSLionel Sambuc LookupGD = GlobalDecl(DD, Dtor_Deleting);
1026*0a6a1f1dSLionel Sambuc }
1027*0a6a1f1dSLionel Sambuc
1028*0a6a1f1dSLionel Sambuc MicrosoftVTableContext::MethodVFTableLocation ML =
1029*0a6a1f1dSLionel Sambuc CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD);
1030*0a6a1f1dSLionel Sambuc CharUnits Adjustment = ML.VFPtrOffset;
1031*0a6a1f1dSLionel Sambuc
1032*0a6a1f1dSLionel Sambuc // Normal virtual instance methods need to adjust from the vfptr that first
1033*0a6a1f1dSLionel Sambuc // defined the virtual method to the virtual base subobject, but destructors
1034*0a6a1f1dSLionel Sambuc // do not. The vector deleting destructor thunk applies this adjustment for
1035*0a6a1f1dSLionel Sambuc // us if necessary.
1036*0a6a1f1dSLionel Sambuc if (isa<CXXDestructorDecl>(MD))
1037*0a6a1f1dSLionel Sambuc Adjustment = CharUnits::Zero();
1038*0a6a1f1dSLionel Sambuc
1039*0a6a1f1dSLionel Sambuc if (ML.VBase) {
1040*0a6a1f1dSLionel Sambuc const ASTRecordLayout &DerivedLayout =
1041*0a6a1f1dSLionel Sambuc CGM.getContext().getASTRecordLayout(MD->getParent());
1042*0a6a1f1dSLionel Sambuc Adjustment += DerivedLayout.getVBaseClassOffset(ML.VBase);
1043*0a6a1f1dSLionel Sambuc }
1044*0a6a1f1dSLionel Sambuc
1045*0a6a1f1dSLionel Sambuc return Adjustment;
1046*0a6a1f1dSLionel Sambuc }
1047*0a6a1f1dSLionel Sambuc
adjustThisArgumentForVirtualFunctionCall(CodeGenFunction & CGF,GlobalDecl GD,llvm::Value * This,bool VirtualCall)1048*0a6a1f1dSLionel Sambuc llvm::Value *MicrosoftCXXABI::adjustThisArgumentForVirtualFunctionCall(
1049*0a6a1f1dSLionel Sambuc CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This, bool VirtualCall) {
1050*0a6a1f1dSLionel Sambuc if (!VirtualCall) {
1051*0a6a1f1dSLionel Sambuc // If the call of a virtual function is not virtual, we just have to
1052*0a6a1f1dSLionel Sambuc // compensate for the adjustment the virtual function does in its prologue.
1053*0a6a1f1dSLionel Sambuc CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD);
1054*0a6a1f1dSLionel Sambuc if (Adjustment.isZero())
1055*0a6a1f1dSLionel Sambuc return This;
1056*0a6a1f1dSLionel Sambuc
1057*0a6a1f1dSLionel Sambuc unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace();
1058*0a6a1f1dSLionel Sambuc llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS);
1059*0a6a1f1dSLionel Sambuc This = CGF.Builder.CreateBitCast(This, charPtrTy);
1060*0a6a1f1dSLionel Sambuc assert(Adjustment.isPositive());
1061*0a6a1f1dSLionel Sambuc return CGF.Builder.CreateConstGEP1_32(This, Adjustment.getQuantity());
1062*0a6a1f1dSLionel Sambuc }
1063*0a6a1f1dSLionel Sambuc
1064*0a6a1f1dSLionel Sambuc GD = GD.getCanonicalDecl();
1065*0a6a1f1dSLionel Sambuc const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1066f4a2713aSLionel Sambuc
1067f4a2713aSLionel Sambuc GlobalDecl LookupGD = GD;
1068f4a2713aSLionel Sambuc if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1069f4a2713aSLionel Sambuc // Complete dtors take a pointer to the complete object,
1070f4a2713aSLionel Sambuc // thus don't need adjustment.
1071f4a2713aSLionel Sambuc if (GD.getDtorType() == Dtor_Complete)
1072f4a2713aSLionel Sambuc return This;
1073f4a2713aSLionel Sambuc
1074f4a2713aSLionel Sambuc // There's only Dtor_Deleting in vftable but it shares the this adjustment
1075f4a2713aSLionel Sambuc // with the base one, so look up the deleting one instead.
1076f4a2713aSLionel Sambuc LookupGD = GlobalDecl(DD, Dtor_Deleting);
1077f4a2713aSLionel Sambuc }
1078f4a2713aSLionel Sambuc MicrosoftVTableContext::MethodVFTableLocation ML =
1079f4a2713aSLionel Sambuc CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD);
1080f4a2713aSLionel Sambuc
1081f4a2713aSLionel Sambuc unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace();
1082f4a2713aSLionel Sambuc llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS);
1083f4a2713aSLionel Sambuc CharUnits StaticOffset = ML.VFPtrOffset;
1084*0a6a1f1dSLionel Sambuc
1085*0a6a1f1dSLionel Sambuc // Base destructors expect 'this' to point to the beginning of the base
1086*0a6a1f1dSLionel Sambuc // subobject, not the first vfptr that happens to contain the virtual dtor.
1087*0a6a1f1dSLionel Sambuc // However, we still need to apply the virtual base adjustment.
1088*0a6a1f1dSLionel Sambuc if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
1089*0a6a1f1dSLionel Sambuc StaticOffset = CharUnits::Zero();
1090*0a6a1f1dSLionel Sambuc
1091f4a2713aSLionel Sambuc if (ML.VBase) {
1092f4a2713aSLionel Sambuc This = CGF.Builder.CreateBitCast(This, charPtrTy);
1093f4a2713aSLionel Sambuc llvm::Value *VBaseOffset =
1094f4a2713aSLionel Sambuc GetVirtualBaseClassOffset(CGF, This, MD->getParent(), ML.VBase);
1095f4a2713aSLionel Sambuc This = CGF.Builder.CreateInBoundsGEP(This, VBaseOffset);
1096f4a2713aSLionel Sambuc }
1097f4a2713aSLionel Sambuc if (!StaticOffset.isZero()) {
1098f4a2713aSLionel Sambuc assert(StaticOffset.isPositive());
1099f4a2713aSLionel Sambuc This = CGF.Builder.CreateBitCast(This, charPtrTy);
1100f4a2713aSLionel Sambuc if (ML.VBase) {
1101f4a2713aSLionel Sambuc // Non-virtual adjustment might result in a pointer outside the allocated
1102f4a2713aSLionel Sambuc // object, e.g. if the final overrider class is laid out after the virtual
1103f4a2713aSLionel Sambuc // base that declares a method in the most derived class.
1104f4a2713aSLionel Sambuc // FIXME: Update the code that emits this adjustment in thunks prologues.
1105f4a2713aSLionel Sambuc This = CGF.Builder.CreateConstGEP1_32(This, StaticOffset.getQuantity());
1106f4a2713aSLionel Sambuc } else {
1107f4a2713aSLionel Sambuc This = CGF.Builder.CreateConstInBoundsGEP1_32(This,
1108f4a2713aSLionel Sambuc StaticOffset.getQuantity());
1109f4a2713aSLionel Sambuc }
1110f4a2713aSLionel Sambuc }
1111f4a2713aSLionel Sambuc return This;
1112f4a2713aSLionel Sambuc }
1113f4a2713aSLionel Sambuc
addImplicitStructorParams(CodeGenFunction & CGF,QualType & ResTy,FunctionArgList & Params)1114*0a6a1f1dSLionel Sambuc void MicrosoftCXXABI::addImplicitStructorParams(CodeGenFunction &CGF,
1115f4a2713aSLionel Sambuc QualType &ResTy,
1116f4a2713aSLionel Sambuc FunctionArgList &Params) {
1117f4a2713aSLionel Sambuc ASTContext &Context = getContext();
1118f4a2713aSLionel Sambuc const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
1119*0a6a1f1dSLionel Sambuc assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD));
1120f4a2713aSLionel Sambuc if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) {
1121f4a2713aSLionel Sambuc ImplicitParamDecl *IsMostDerived
1122*0a6a1f1dSLionel Sambuc = ImplicitParamDecl::Create(Context, nullptr,
1123f4a2713aSLionel Sambuc CGF.CurGD.getDecl()->getLocation(),
1124f4a2713aSLionel Sambuc &Context.Idents.get("is_most_derived"),
1125f4a2713aSLionel Sambuc Context.IntTy);
1126*0a6a1f1dSLionel Sambuc // The 'most_derived' parameter goes second if the ctor is variadic and last
1127*0a6a1f1dSLionel Sambuc // if it's not. Dtors can't be variadic.
1128*0a6a1f1dSLionel Sambuc const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
1129*0a6a1f1dSLionel Sambuc if (FPT->isVariadic())
1130*0a6a1f1dSLionel Sambuc Params.insert(Params.begin() + 1, IsMostDerived);
1131*0a6a1f1dSLionel Sambuc else
1132f4a2713aSLionel Sambuc Params.push_back(IsMostDerived);
1133f4a2713aSLionel Sambuc getStructorImplicitParamDecl(CGF) = IsMostDerived;
1134*0a6a1f1dSLionel Sambuc } else if (isDeletingDtor(CGF.CurGD)) {
1135f4a2713aSLionel Sambuc ImplicitParamDecl *ShouldDelete
1136*0a6a1f1dSLionel Sambuc = ImplicitParamDecl::Create(Context, nullptr,
1137f4a2713aSLionel Sambuc CGF.CurGD.getDecl()->getLocation(),
1138f4a2713aSLionel Sambuc &Context.Idents.get("should_call_delete"),
1139f4a2713aSLionel Sambuc Context.IntTy);
1140f4a2713aSLionel Sambuc Params.push_back(ShouldDelete);
1141f4a2713aSLionel Sambuc getStructorImplicitParamDecl(CGF) = ShouldDelete;
1142f4a2713aSLionel Sambuc }
1143f4a2713aSLionel Sambuc }
1144f4a2713aSLionel Sambuc
adjustThisParameterInVirtualFunctionPrologue(CodeGenFunction & CGF,GlobalDecl GD,llvm::Value * This)1145f4a2713aSLionel Sambuc llvm::Value *MicrosoftCXXABI::adjustThisParameterInVirtualFunctionPrologue(
1146f4a2713aSLionel Sambuc CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) {
1147f4a2713aSLionel Sambuc // In this ABI, every virtual function takes a pointer to one of the
1148f4a2713aSLionel Sambuc // subobjects that first defines it as the 'this' parameter, rather than a
1149*0a6a1f1dSLionel Sambuc // pointer to the final overrider subobject. Thus, we need to adjust it back
1150f4a2713aSLionel Sambuc // to the final overrider subobject before use.
1151f4a2713aSLionel Sambuc // See comments in the MicrosoftVFTableContext implementation for the details.
1152*0a6a1f1dSLionel Sambuc CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD);
1153f4a2713aSLionel Sambuc if (Adjustment.isZero())
1154f4a2713aSLionel Sambuc return This;
1155f4a2713aSLionel Sambuc
1156f4a2713aSLionel Sambuc unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace();
1157f4a2713aSLionel Sambuc llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS),
1158f4a2713aSLionel Sambuc *thisTy = This->getType();
1159f4a2713aSLionel Sambuc
1160f4a2713aSLionel Sambuc This = CGF.Builder.CreateBitCast(This, charPtrTy);
1161f4a2713aSLionel Sambuc assert(Adjustment.isPositive());
1162f4a2713aSLionel Sambuc This =
1163f4a2713aSLionel Sambuc CGF.Builder.CreateConstInBoundsGEP1_32(This, -Adjustment.getQuantity());
1164f4a2713aSLionel Sambuc return CGF.Builder.CreateBitCast(This, thisTy);
1165f4a2713aSLionel Sambuc }
1166f4a2713aSLionel Sambuc
EmitInstanceFunctionProlog(CodeGenFunction & CGF)1167f4a2713aSLionel Sambuc void MicrosoftCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
1168f4a2713aSLionel Sambuc EmitThisParam(CGF);
1169f4a2713aSLionel Sambuc
1170f4a2713aSLionel Sambuc /// If this is a function that the ABI specifies returns 'this', initialize
1171f4a2713aSLionel Sambuc /// the return slot to 'this' at the start of the function.
1172f4a2713aSLionel Sambuc ///
1173f4a2713aSLionel Sambuc /// Unlike the setting of return types, this is done within the ABI
1174f4a2713aSLionel Sambuc /// implementation instead of by clients of CGCXXABI because:
1175f4a2713aSLionel Sambuc /// 1) getThisValue is currently protected
1176f4a2713aSLionel Sambuc /// 2) in theory, an ABI could implement 'this' returns some other way;
1177f4a2713aSLionel Sambuc /// HasThisReturn only specifies a contract, not the implementation
1178f4a2713aSLionel Sambuc if (HasThisReturn(CGF.CurGD))
1179f4a2713aSLionel Sambuc CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
1180*0a6a1f1dSLionel Sambuc else if (hasMostDerivedReturn(CGF.CurGD))
1181*0a6a1f1dSLionel Sambuc CGF.Builder.CreateStore(CGF.EmitCastToVoidPtr(getThisValue(CGF)),
1182*0a6a1f1dSLionel Sambuc CGF.ReturnValue);
1183f4a2713aSLionel Sambuc
1184f4a2713aSLionel Sambuc const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
1185f4a2713aSLionel Sambuc if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) {
1186f4a2713aSLionel Sambuc assert(getStructorImplicitParamDecl(CGF) &&
1187f4a2713aSLionel Sambuc "no implicit parameter for a constructor with virtual bases?");
1188f4a2713aSLionel Sambuc getStructorImplicitParamValue(CGF)
1189f4a2713aSLionel Sambuc = CGF.Builder.CreateLoad(
1190f4a2713aSLionel Sambuc CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)),
1191f4a2713aSLionel Sambuc "is_most_derived");
1192f4a2713aSLionel Sambuc }
1193f4a2713aSLionel Sambuc
1194*0a6a1f1dSLionel Sambuc if (isDeletingDtor(CGF.CurGD)) {
1195f4a2713aSLionel Sambuc assert(getStructorImplicitParamDecl(CGF) &&
1196f4a2713aSLionel Sambuc "no implicit parameter for a deleting destructor?");
1197f4a2713aSLionel Sambuc getStructorImplicitParamValue(CGF)
1198f4a2713aSLionel Sambuc = CGF.Builder.CreateLoad(
1199f4a2713aSLionel Sambuc CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)),
1200f4a2713aSLionel Sambuc "should_call_delete");
1201f4a2713aSLionel Sambuc }
1202f4a2713aSLionel Sambuc }
1203f4a2713aSLionel Sambuc
addImplicitConstructorArgs(CodeGenFunction & CGF,const CXXConstructorDecl * D,CXXCtorType Type,bool ForVirtualBase,bool Delegating,CallArgList & Args)1204*0a6a1f1dSLionel Sambuc unsigned MicrosoftCXXABI::addImplicitConstructorArgs(
1205*0a6a1f1dSLionel Sambuc CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type,
1206*0a6a1f1dSLionel Sambuc bool ForVirtualBase, bool Delegating, CallArgList &Args) {
1207f4a2713aSLionel Sambuc assert(Type == Ctor_Complete || Type == Ctor_Base);
1208f4a2713aSLionel Sambuc
1209*0a6a1f1dSLionel Sambuc // Check if we need a 'most_derived' parameter.
1210*0a6a1f1dSLionel Sambuc if (!D->getParent()->getNumVBases())
1211*0a6a1f1dSLionel Sambuc return 0;
1212*0a6a1f1dSLionel Sambuc
1213*0a6a1f1dSLionel Sambuc // Add the 'most_derived' argument second if we are variadic or last if not.
1214*0a6a1f1dSLionel Sambuc const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
1215*0a6a1f1dSLionel Sambuc llvm::Value *MostDerivedArg =
1216*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(CGM.Int32Ty, Type == Ctor_Complete);
1217*0a6a1f1dSLionel Sambuc RValue RV = RValue::get(MostDerivedArg);
1218*0a6a1f1dSLionel Sambuc if (MostDerivedArg) {
1219*0a6a1f1dSLionel Sambuc if (FPT->isVariadic())
1220*0a6a1f1dSLionel Sambuc Args.insert(Args.begin() + 1,
1221*0a6a1f1dSLionel Sambuc CallArg(RV, getContext().IntTy, /*needscopy=*/false));
1222*0a6a1f1dSLionel Sambuc else
1223*0a6a1f1dSLionel Sambuc Args.add(RV, getContext().IntTy);
1224f4a2713aSLionel Sambuc }
1225f4a2713aSLionel Sambuc
1226*0a6a1f1dSLionel Sambuc return 1; // Added one arg.
1227*0a6a1f1dSLionel Sambuc }
1228*0a6a1f1dSLionel Sambuc
EmitDestructorCall(CodeGenFunction & CGF,const CXXDestructorDecl * DD,CXXDtorType Type,bool ForVirtualBase,bool Delegating,llvm::Value * This)1229*0a6a1f1dSLionel Sambuc void MicrosoftCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
1230*0a6a1f1dSLionel Sambuc const CXXDestructorDecl *DD,
1231*0a6a1f1dSLionel Sambuc CXXDtorType Type, bool ForVirtualBase,
1232*0a6a1f1dSLionel Sambuc bool Delegating, llvm::Value *This) {
1233*0a6a1f1dSLionel Sambuc llvm::Value *Callee = CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type));
1234*0a6a1f1dSLionel Sambuc
1235*0a6a1f1dSLionel Sambuc if (DD->isVirtual()) {
1236*0a6a1f1dSLionel Sambuc assert(Type != CXXDtorType::Dtor_Deleting &&
1237*0a6a1f1dSLionel Sambuc "The deleting destructor should only be called via a virtual call");
1238*0a6a1f1dSLionel Sambuc This = adjustThisArgumentForVirtualFunctionCall(CGF, GlobalDecl(DD, Type),
1239*0a6a1f1dSLionel Sambuc This, false);
1240*0a6a1f1dSLionel Sambuc }
1241*0a6a1f1dSLionel Sambuc
1242*0a6a1f1dSLionel Sambuc CGF.EmitCXXStructorCall(DD, Callee, ReturnValueSlot(), This,
1243*0a6a1f1dSLionel Sambuc /*ImplicitParam=*/nullptr,
1244*0a6a1f1dSLionel Sambuc /*ImplicitParamTy=*/QualType(), nullptr,
1245*0a6a1f1dSLionel Sambuc getFromDtorType(Type));
1246f4a2713aSLionel Sambuc }
1247f4a2713aSLionel Sambuc
emitVTableDefinitions(CodeGenVTables & CGVT,const CXXRecordDecl * RD)1248f4a2713aSLionel Sambuc void MicrosoftCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT,
1249f4a2713aSLionel Sambuc const CXXRecordDecl *RD) {
1250f4a2713aSLionel Sambuc MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext();
1251*0a6a1f1dSLionel Sambuc const VPtrInfoVector &VFPtrs = VFTContext.getVFPtrOffsets(RD);
1252f4a2713aSLionel Sambuc
1253*0a6a1f1dSLionel Sambuc for (VPtrInfo *Info : VFPtrs) {
1254*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *VTable = getAddrOfVTable(RD, Info->FullOffsetInMDC);
1255f4a2713aSLionel Sambuc if (VTable->hasInitializer())
1256f4a2713aSLionel Sambuc continue;
1257f4a2713aSLionel Sambuc
1258*0a6a1f1dSLionel Sambuc llvm::Constant *RTTI = getContext().getLangOpts().RTTIData
1259*0a6a1f1dSLionel Sambuc ? getMSCompleteObjectLocator(RD, Info)
1260*0a6a1f1dSLionel Sambuc : nullptr;
1261*0a6a1f1dSLionel Sambuc
1262f4a2713aSLionel Sambuc const VTableLayout &VTLayout =
1263*0a6a1f1dSLionel Sambuc VFTContext.getVFTableLayout(RD, Info->FullOffsetInMDC);
1264f4a2713aSLionel Sambuc llvm::Constant *Init = CGVT.CreateVTableInitializer(
1265f4a2713aSLionel Sambuc RD, VTLayout.vtable_component_begin(),
1266f4a2713aSLionel Sambuc VTLayout.getNumVTableComponents(), VTLayout.vtable_thunk_begin(),
1267*0a6a1f1dSLionel Sambuc VTLayout.getNumVTableThunks(), RTTI);
1268f4a2713aSLionel Sambuc
1269*0a6a1f1dSLionel Sambuc VTable->setInitializer(Init);
1270f4a2713aSLionel Sambuc }
1271f4a2713aSLionel Sambuc }
1272f4a2713aSLionel Sambuc
getVTableAddressPointInStructor(CodeGenFunction & CGF,const CXXRecordDecl * VTableClass,BaseSubobject Base,const CXXRecordDecl * NearestVBase,bool & NeedsVirtualOffset)1273f4a2713aSLionel Sambuc llvm::Value *MicrosoftCXXABI::getVTableAddressPointInStructor(
1274f4a2713aSLionel Sambuc CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1275f4a2713aSLionel Sambuc const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) {
1276*0a6a1f1dSLionel Sambuc NeedsVirtualOffset = (NearestVBase != nullptr);
1277f4a2713aSLionel Sambuc
1278*0a6a1f1dSLionel Sambuc (void)getAddrOfVTable(VTableClass, Base.getBaseOffset());
1279*0a6a1f1dSLionel Sambuc VFTableIdTy ID(VTableClass, Base.getBaseOffset());
1280*0a6a1f1dSLionel Sambuc llvm::GlobalValue *VTableAddressPoint = VFTablesMap[ID];
1281f4a2713aSLionel Sambuc if (!VTableAddressPoint) {
1282f4a2713aSLionel Sambuc assert(Base.getBase()->getNumVBases() &&
1283f4a2713aSLionel Sambuc !CGM.getContext().getASTRecordLayout(Base.getBase()).hasOwnVFPtr());
1284f4a2713aSLionel Sambuc }
1285f4a2713aSLionel Sambuc return VTableAddressPoint;
1286f4a2713aSLionel Sambuc }
1287f4a2713aSLionel Sambuc
mangleVFTableName(MicrosoftMangleContext & MangleContext,const CXXRecordDecl * RD,const VPtrInfo * VFPtr,SmallString<256> & Name)1288f4a2713aSLionel Sambuc static void mangleVFTableName(MicrosoftMangleContext &MangleContext,
1289*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD, const VPtrInfo *VFPtr,
1290f4a2713aSLionel Sambuc SmallString<256> &Name) {
1291f4a2713aSLionel Sambuc llvm::raw_svector_ostream Out(Name);
1292*0a6a1f1dSLionel Sambuc MangleContext.mangleCXXVFTable(RD, VFPtr->MangledPath, Out);
1293f4a2713aSLionel Sambuc }
1294f4a2713aSLionel Sambuc
getVTableAddressPointForConstExpr(BaseSubobject Base,const CXXRecordDecl * VTableClass)1295f4a2713aSLionel Sambuc llvm::Constant *MicrosoftCXXABI::getVTableAddressPointForConstExpr(
1296f4a2713aSLionel Sambuc BaseSubobject Base, const CXXRecordDecl *VTableClass) {
1297*0a6a1f1dSLionel Sambuc (void)getAddrOfVTable(VTableClass, Base.getBaseOffset());
1298*0a6a1f1dSLionel Sambuc VFTableIdTy ID(VTableClass, Base.getBaseOffset());
1299*0a6a1f1dSLionel Sambuc llvm::GlobalValue *VFTable = VFTablesMap[ID];
1300*0a6a1f1dSLionel Sambuc assert(VFTable && "Couldn't find a vftable for the given base?");
1301*0a6a1f1dSLionel Sambuc return VFTable;
1302f4a2713aSLionel Sambuc }
1303f4a2713aSLionel Sambuc
getAddrOfVTable(const CXXRecordDecl * RD,CharUnits VPtrOffset)1304f4a2713aSLionel Sambuc llvm::GlobalVariable *MicrosoftCXXABI::getAddrOfVTable(const CXXRecordDecl *RD,
1305f4a2713aSLionel Sambuc CharUnits VPtrOffset) {
1306f4a2713aSLionel Sambuc // getAddrOfVTable may return 0 if asked to get an address of a vtable which
1307f4a2713aSLionel Sambuc // shouldn't be used in the given record type. We want to cache this result in
1308f4a2713aSLionel Sambuc // VFTablesMap, thus a simple zero check is not sufficient.
1309f4a2713aSLionel Sambuc VFTableIdTy ID(RD, VPtrOffset);
1310*0a6a1f1dSLionel Sambuc VTablesMapTy::iterator I;
1311f4a2713aSLionel Sambuc bool Inserted;
1312*0a6a1f1dSLionel Sambuc std::tie(I, Inserted) = VTablesMap.insert(std::make_pair(ID, nullptr));
1313f4a2713aSLionel Sambuc if (!Inserted)
1314f4a2713aSLionel Sambuc return I->second;
1315f4a2713aSLionel Sambuc
1316f4a2713aSLionel Sambuc llvm::GlobalVariable *&VTable = I->second;
1317f4a2713aSLionel Sambuc
1318f4a2713aSLionel Sambuc MicrosoftVTableContext &VTContext = CGM.getMicrosoftVTableContext();
1319*0a6a1f1dSLionel Sambuc const VPtrInfoVector &VFPtrs = VTContext.getVFPtrOffsets(RD);
1320f4a2713aSLionel Sambuc
1321*0a6a1f1dSLionel Sambuc if (DeferredVFTables.insert(RD).second) {
1322f4a2713aSLionel Sambuc // We haven't processed this record type before.
1323f4a2713aSLionel Sambuc // Queue up this v-table for possible deferred emission.
1324f4a2713aSLionel Sambuc CGM.addDeferredVTable(RD);
1325f4a2713aSLionel Sambuc
1326f4a2713aSLionel Sambuc #ifndef NDEBUG
1327f4a2713aSLionel Sambuc // Create all the vftables at once in order to make sure each vftable has
1328f4a2713aSLionel Sambuc // a unique mangled name.
1329f4a2713aSLionel Sambuc llvm::StringSet<> ObservedMangledNames;
1330f4a2713aSLionel Sambuc for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) {
1331f4a2713aSLionel Sambuc SmallString<256> Name;
1332f4a2713aSLionel Sambuc mangleVFTableName(getMangleContext(), RD, VFPtrs[J], Name);
1333*0a6a1f1dSLionel Sambuc if (!ObservedMangledNames.insert(Name.str()).second)
1334f4a2713aSLionel Sambuc llvm_unreachable("Already saw this mangling before?");
1335f4a2713aSLionel Sambuc }
1336f4a2713aSLionel Sambuc #endif
1337f4a2713aSLionel Sambuc }
1338f4a2713aSLionel Sambuc
1339f4a2713aSLionel Sambuc for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) {
1340*0a6a1f1dSLionel Sambuc if (VFPtrs[J]->FullOffsetInMDC != VPtrOffset)
1341f4a2713aSLionel Sambuc continue;
1342*0a6a1f1dSLionel Sambuc SmallString<256> VFTableName;
1343*0a6a1f1dSLionel Sambuc mangleVFTableName(getMangleContext(), RD, VFPtrs[J], VFTableName);
1344*0a6a1f1dSLionel Sambuc StringRef VTableName = VFTableName;
1345f4a2713aSLionel Sambuc
1346*0a6a1f1dSLionel Sambuc uint64_t NumVTableSlots =
1347*0a6a1f1dSLionel Sambuc VTContext.getVFTableLayout(RD, VFPtrs[J]->FullOffsetInMDC)
1348*0a6a1f1dSLionel Sambuc .getNumVTableComponents();
1349*0a6a1f1dSLionel Sambuc llvm::GlobalValue::LinkageTypes VTableLinkage =
1350*0a6a1f1dSLionel Sambuc llvm::GlobalValue::ExternalLinkage;
1351*0a6a1f1dSLionel Sambuc llvm::ArrayType *VTableType =
1352*0a6a1f1dSLionel Sambuc llvm::ArrayType::get(CGM.Int8PtrTy, NumVTableSlots);
1353*0a6a1f1dSLionel Sambuc if (getContext().getLangOpts().RTTIData) {
1354*0a6a1f1dSLionel Sambuc VTableLinkage = llvm::GlobalValue::PrivateLinkage;
1355*0a6a1f1dSLionel Sambuc VTableName = "";
1356*0a6a1f1dSLionel Sambuc }
1357f4a2713aSLionel Sambuc
1358*0a6a1f1dSLionel Sambuc VTable = CGM.getModule().getNamedGlobal(VFTableName);
1359*0a6a1f1dSLionel Sambuc if (!VTable) {
1360*0a6a1f1dSLionel Sambuc // Create a backing variable for the contents of VTable. The VTable may
1361*0a6a1f1dSLionel Sambuc // or may not include space for a pointer to RTTI data.
1362*0a6a1f1dSLionel Sambuc llvm::GlobalValue *VFTable = VTable = new llvm::GlobalVariable(
1363*0a6a1f1dSLionel Sambuc CGM.getModule(), VTableType, /*isConstant=*/true, VTableLinkage,
1364*0a6a1f1dSLionel Sambuc /*Initializer=*/nullptr, VTableName);
1365f4a2713aSLionel Sambuc VTable->setUnnamedAddr(true);
1366*0a6a1f1dSLionel Sambuc
1367*0a6a1f1dSLionel Sambuc // Only insert a pointer into the VFTable for RTTI data if we are not
1368*0a6a1f1dSLionel Sambuc // importing it. We never reference the RTTI data directly so there is no
1369*0a6a1f1dSLionel Sambuc // need to make room for it.
1370*0a6a1f1dSLionel Sambuc if (getContext().getLangOpts().RTTIData &&
1371*0a6a1f1dSLionel Sambuc !RD->hasAttr<DLLImportAttr>()) {
1372*0a6a1f1dSLionel Sambuc llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0),
1373*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(CGM.IntTy, 1)};
1374*0a6a1f1dSLionel Sambuc // Create a GEP which points just after the first entry in the VFTable,
1375*0a6a1f1dSLionel Sambuc // this should be the location of the first virtual method.
1376*0a6a1f1dSLionel Sambuc llvm::Constant *VTableGEP =
1377*0a6a1f1dSLionel Sambuc llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, GEPIndices);
1378*0a6a1f1dSLionel Sambuc // The symbol for the VFTable is an alias to the GEP. It is
1379*0a6a1f1dSLionel Sambuc // transparent, to other modules, what the nature of this symbol is; all
1380*0a6a1f1dSLionel Sambuc // that matters is that the alias be the address of the first virtual
1381*0a6a1f1dSLionel Sambuc // method.
1382*0a6a1f1dSLionel Sambuc VFTable = llvm::GlobalAlias::create(
1383*0a6a1f1dSLionel Sambuc cast<llvm::SequentialType>(VTableGEP->getType())->getElementType(),
1384*0a6a1f1dSLionel Sambuc /*AddressSpace=*/0, llvm::GlobalValue::ExternalLinkage,
1385*0a6a1f1dSLionel Sambuc VFTableName.str(), VTableGEP, &CGM.getModule());
1386*0a6a1f1dSLionel Sambuc } else {
1387*0a6a1f1dSLionel Sambuc // We don't need a GlobalAlias to be a symbol for the VTable if we won't
1388*0a6a1f1dSLionel Sambuc // be referencing any RTTI data. The GlobalVariable will end up being
1389*0a6a1f1dSLionel Sambuc // an appropriate definition of the VFTable.
1390*0a6a1f1dSLionel Sambuc VTable->setName(VFTableName.str());
1391*0a6a1f1dSLionel Sambuc }
1392*0a6a1f1dSLionel Sambuc
1393*0a6a1f1dSLionel Sambuc VFTable->setUnnamedAddr(true);
1394*0a6a1f1dSLionel Sambuc if (RD->hasAttr<DLLImportAttr>())
1395*0a6a1f1dSLionel Sambuc VFTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
1396*0a6a1f1dSLionel Sambuc else if (RD->hasAttr<DLLExportAttr>())
1397*0a6a1f1dSLionel Sambuc VFTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
1398*0a6a1f1dSLionel Sambuc
1399*0a6a1f1dSLionel Sambuc llvm::GlobalValue::LinkageTypes VFTableLinkage = CGM.getVTableLinkage(RD);
1400*0a6a1f1dSLionel Sambuc if (VFTable != VTable) {
1401*0a6a1f1dSLionel Sambuc if (llvm::GlobalValue::isAvailableExternallyLinkage(VFTableLinkage)) {
1402*0a6a1f1dSLionel Sambuc // AvailableExternally implies that we grabbed the data from another
1403*0a6a1f1dSLionel Sambuc // executable. No need to stick the alias in a Comdat.
1404*0a6a1f1dSLionel Sambuc } else if (llvm::GlobalValue::isInternalLinkage(VFTableLinkage) ||
1405*0a6a1f1dSLionel Sambuc llvm::GlobalValue::isWeakODRLinkage(VFTableLinkage) ||
1406*0a6a1f1dSLionel Sambuc llvm::GlobalValue::isLinkOnceODRLinkage(VFTableLinkage)) {
1407*0a6a1f1dSLionel Sambuc // The alias is going to be dropped into a Comdat, no need to make it
1408*0a6a1f1dSLionel Sambuc // weak.
1409*0a6a1f1dSLionel Sambuc if (!llvm::GlobalValue::isInternalLinkage(VFTableLinkage))
1410*0a6a1f1dSLionel Sambuc VFTableLinkage = llvm::GlobalValue::ExternalLinkage;
1411*0a6a1f1dSLionel Sambuc llvm::Comdat *C =
1412*0a6a1f1dSLionel Sambuc CGM.getModule().getOrInsertComdat(VFTable->getName());
1413*0a6a1f1dSLionel Sambuc // We must indicate which VFTable is larger to support linking between
1414*0a6a1f1dSLionel Sambuc // translation units which do and do not have RTTI data. The largest
1415*0a6a1f1dSLionel Sambuc // VFTable contains the RTTI data; translation units which reference
1416*0a6a1f1dSLionel Sambuc // the smaller VFTable always reference it relative to the first
1417*0a6a1f1dSLionel Sambuc // virtual method.
1418*0a6a1f1dSLionel Sambuc C->setSelectionKind(llvm::Comdat::Largest);
1419*0a6a1f1dSLionel Sambuc VTable->setComdat(C);
1420*0a6a1f1dSLionel Sambuc } else {
1421*0a6a1f1dSLionel Sambuc llvm_unreachable("unexpected linkage for vftable!");
1422*0a6a1f1dSLionel Sambuc }
1423*0a6a1f1dSLionel Sambuc }
1424*0a6a1f1dSLionel Sambuc VFTable->setLinkage(VFTableLinkage);
1425*0a6a1f1dSLionel Sambuc CGM.setGlobalVisibility(VFTable, RD);
1426*0a6a1f1dSLionel Sambuc VFTablesMap[ID] = VFTable;
1427*0a6a1f1dSLionel Sambuc }
1428f4a2713aSLionel Sambuc break;
1429f4a2713aSLionel Sambuc }
1430f4a2713aSLionel Sambuc
1431f4a2713aSLionel Sambuc return VTable;
1432f4a2713aSLionel Sambuc }
1433f4a2713aSLionel Sambuc
getVirtualFunctionPointer(CodeGenFunction & CGF,GlobalDecl GD,llvm::Value * This,llvm::Type * Ty)1434f4a2713aSLionel Sambuc llvm::Value *MicrosoftCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF,
1435f4a2713aSLionel Sambuc GlobalDecl GD,
1436f4a2713aSLionel Sambuc llvm::Value *This,
1437f4a2713aSLionel Sambuc llvm::Type *Ty) {
1438f4a2713aSLionel Sambuc GD = GD.getCanonicalDecl();
1439f4a2713aSLionel Sambuc CGBuilderTy &Builder = CGF.Builder;
1440f4a2713aSLionel Sambuc
1441f4a2713aSLionel Sambuc Ty = Ty->getPointerTo()->getPointerTo();
1442*0a6a1f1dSLionel Sambuc llvm::Value *VPtr =
1443*0a6a1f1dSLionel Sambuc adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true);
1444f4a2713aSLionel Sambuc llvm::Value *VTable = CGF.GetVTablePtr(VPtr, Ty);
1445f4a2713aSLionel Sambuc
1446f4a2713aSLionel Sambuc MicrosoftVTableContext::MethodVFTableLocation ML =
1447f4a2713aSLionel Sambuc CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD);
1448f4a2713aSLionel Sambuc llvm::Value *VFuncPtr =
1449f4a2713aSLionel Sambuc Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn");
1450f4a2713aSLionel Sambuc return Builder.CreateLoad(VFuncPtr);
1451f4a2713aSLionel Sambuc }
1452f4a2713aSLionel Sambuc
EmitVirtualDestructorCall(CodeGenFunction & CGF,const CXXDestructorDecl * Dtor,CXXDtorType DtorType,llvm::Value * This,const CXXMemberCallExpr * CE)1453*0a6a1f1dSLionel Sambuc llvm::Value *MicrosoftCXXABI::EmitVirtualDestructorCall(
1454*0a6a1f1dSLionel Sambuc CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType,
1455*0a6a1f1dSLionel Sambuc llvm::Value *This, const CXXMemberCallExpr *CE) {
1456*0a6a1f1dSLionel Sambuc assert(CE == nullptr || CE->arg_begin() == CE->arg_end());
1457f4a2713aSLionel Sambuc assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete);
1458f4a2713aSLionel Sambuc
1459f4a2713aSLionel Sambuc // We have only one destructor in the vftable but can get both behaviors
1460f4a2713aSLionel Sambuc // by passing an implicit int parameter.
1461f4a2713aSLionel Sambuc GlobalDecl GD(Dtor, Dtor_Deleting);
1462*0a6a1f1dSLionel Sambuc const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration(
1463*0a6a1f1dSLionel Sambuc Dtor, StructorType::Deleting);
1464f4a2713aSLionel Sambuc llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo);
1465f4a2713aSLionel Sambuc llvm::Value *Callee = getVirtualFunctionPointer(CGF, GD, This, Ty);
1466f4a2713aSLionel Sambuc
1467f4a2713aSLionel Sambuc ASTContext &Context = CGF.getContext();
1468*0a6a1f1dSLionel Sambuc llvm::Value *ImplicitParam = llvm::ConstantInt::get(
1469*0a6a1f1dSLionel Sambuc llvm::IntegerType::getInt32Ty(CGF.getLLVMContext()),
1470f4a2713aSLionel Sambuc DtorType == Dtor_Deleting);
1471f4a2713aSLionel Sambuc
1472*0a6a1f1dSLionel Sambuc This = adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true);
1473*0a6a1f1dSLionel Sambuc RValue RV = CGF.EmitCXXStructorCall(Dtor, Callee, ReturnValueSlot(), This,
1474*0a6a1f1dSLionel Sambuc ImplicitParam, Context.IntTy, CE,
1475*0a6a1f1dSLionel Sambuc StructorType::Deleting);
1476*0a6a1f1dSLionel Sambuc return RV.getScalarVal();
1477f4a2713aSLionel Sambuc }
1478f4a2713aSLionel Sambuc
1479*0a6a1f1dSLionel Sambuc const VBTableGlobals &
enumerateVBTables(const CXXRecordDecl * RD)1480*0a6a1f1dSLionel Sambuc MicrosoftCXXABI::enumerateVBTables(const CXXRecordDecl *RD) {
1481f4a2713aSLionel Sambuc // At this layer, we can key the cache off of a single class, which is much
1482*0a6a1f1dSLionel Sambuc // easier than caching each vbtable individually.
1483*0a6a1f1dSLionel Sambuc llvm::DenseMap<const CXXRecordDecl*, VBTableGlobals>::iterator Entry;
1484*0a6a1f1dSLionel Sambuc bool Added;
1485*0a6a1f1dSLionel Sambuc std::tie(Entry, Added) =
1486*0a6a1f1dSLionel Sambuc VBTablesMap.insert(std::make_pair(RD, VBTableGlobals()));
1487*0a6a1f1dSLionel Sambuc VBTableGlobals &VBGlobals = Entry->second;
1488*0a6a1f1dSLionel Sambuc if (!Added)
1489*0a6a1f1dSLionel Sambuc return VBGlobals;
1490f4a2713aSLionel Sambuc
1491*0a6a1f1dSLionel Sambuc MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext();
1492*0a6a1f1dSLionel Sambuc VBGlobals.VBTables = &Context.enumerateVBTables(RD);
1493f4a2713aSLionel Sambuc
1494*0a6a1f1dSLionel Sambuc // Cache the globals for all vbtables so we don't have to recompute the
1495*0a6a1f1dSLionel Sambuc // mangled names.
1496*0a6a1f1dSLionel Sambuc llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD);
1497*0a6a1f1dSLionel Sambuc for (VPtrInfoVector::const_iterator I = VBGlobals.VBTables->begin(),
1498*0a6a1f1dSLionel Sambuc E = VBGlobals.VBTables->end();
1499*0a6a1f1dSLionel Sambuc I != E; ++I) {
1500*0a6a1f1dSLionel Sambuc VBGlobals.Globals.push_back(getAddrOfVBTable(**I, RD, Linkage));
1501f4a2713aSLionel Sambuc }
1502f4a2713aSLionel Sambuc
1503*0a6a1f1dSLionel Sambuc return VBGlobals;
1504*0a6a1f1dSLionel Sambuc }
1505*0a6a1f1dSLionel Sambuc
EmitVirtualMemPtrThunk(const CXXMethodDecl * MD,const MicrosoftVTableContext::MethodVFTableLocation & ML)1506*0a6a1f1dSLionel Sambuc llvm::Function *MicrosoftCXXABI::EmitVirtualMemPtrThunk(
1507*0a6a1f1dSLionel Sambuc const CXXMethodDecl *MD,
1508*0a6a1f1dSLionel Sambuc const MicrosoftVTableContext::MethodVFTableLocation &ML) {
1509*0a6a1f1dSLionel Sambuc assert(!isa<CXXConstructorDecl>(MD) && !isa<CXXDestructorDecl>(MD) &&
1510*0a6a1f1dSLionel Sambuc "can't form pointers to ctors or virtual dtors");
1511*0a6a1f1dSLionel Sambuc
1512*0a6a1f1dSLionel Sambuc // Calculate the mangled name.
1513*0a6a1f1dSLionel Sambuc SmallString<256> ThunkName;
1514*0a6a1f1dSLionel Sambuc llvm::raw_svector_ostream Out(ThunkName);
1515*0a6a1f1dSLionel Sambuc getMangleContext().mangleVirtualMemPtrThunk(MD, Out);
1516*0a6a1f1dSLionel Sambuc Out.flush();
1517*0a6a1f1dSLionel Sambuc
1518f4a2713aSLionel Sambuc // If the thunk has been generated previously, just return it.
1519f4a2713aSLionel Sambuc if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(ThunkName))
1520f4a2713aSLionel Sambuc return cast<llvm::Function>(GV);
1521f4a2713aSLionel Sambuc
1522f4a2713aSLionel Sambuc // Create the llvm::Function.
1523*0a6a1f1dSLionel Sambuc const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeMSMemberPointerThunk(MD);
1524f4a2713aSLionel Sambuc llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(FnInfo);
1525f4a2713aSLionel Sambuc llvm::Function *ThunkFn =
1526f4a2713aSLionel Sambuc llvm::Function::Create(ThunkTy, llvm::Function::ExternalLinkage,
1527f4a2713aSLionel Sambuc ThunkName.str(), &CGM.getModule());
1528f4a2713aSLionel Sambuc assert(ThunkFn->getName() == ThunkName && "name was uniqued!");
1529f4a2713aSLionel Sambuc
1530f4a2713aSLionel Sambuc ThunkFn->setLinkage(MD->isExternallyVisible()
1531f4a2713aSLionel Sambuc ? llvm::GlobalValue::LinkOnceODRLinkage
1532f4a2713aSLionel Sambuc : llvm::GlobalValue::InternalLinkage);
1533f4a2713aSLionel Sambuc
1534f4a2713aSLionel Sambuc CGM.SetLLVMFunctionAttributes(MD, FnInfo, ThunkFn);
1535f4a2713aSLionel Sambuc CGM.SetLLVMFunctionAttributesForDefinition(MD, ThunkFn);
1536f4a2713aSLionel Sambuc
1537*0a6a1f1dSLionel Sambuc // Add the "thunk" attribute so that LLVM knows that the return type is
1538*0a6a1f1dSLionel Sambuc // meaningless. These thunks can be used to call functions with differing
1539*0a6a1f1dSLionel Sambuc // return types, and the caller is required to cast the prototype
1540*0a6a1f1dSLionel Sambuc // appropriately to extract the correct value.
1541*0a6a1f1dSLionel Sambuc ThunkFn->addFnAttr("thunk");
1542*0a6a1f1dSLionel Sambuc
1543*0a6a1f1dSLionel Sambuc // These thunks can be compared, so they are not unnamed.
1544*0a6a1f1dSLionel Sambuc ThunkFn->setUnnamedAddr(false);
1545*0a6a1f1dSLionel Sambuc
1546f4a2713aSLionel Sambuc // Start codegen.
1547f4a2713aSLionel Sambuc CodeGenFunction CGF(CGM);
1548*0a6a1f1dSLionel Sambuc CGF.CurGD = GlobalDecl(MD);
1549*0a6a1f1dSLionel Sambuc CGF.CurFuncIsThunk = true;
1550f4a2713aSLionel Sambuc
1551*0a6a1f1dSLionel Sambuc // Build FunctionArgs, but only include the implicit 'this' parameter
1552*0a6a1f1dSLionel Sambuc // declaration.
1553*0a6a1f1dSLionel Sambuc FunctionArgList FunctionArgs;
1554*0a6a1f1dSLionel Sambuc buildThisParam(CGF, FunctionArgs);
1555f4a2713aSLionel Sambuc
1556*0a6a1f1dSLionel Sambuc // Start defining the function.
1557*0a6a1f1dSLionel Sambuc CGF.StartFunction(GlobalDecl(), FnInfo.getReturnType(), ThunkFn, FnInfo,
1558*0a6a1f1dSLionel Sambuc FunctionArgs, MD->getLocation(), SourceLocation());
1559*0a6a1f1dSLionel Sambuc EmitThisParam(CGF);
1560*0a6a1f1dSLionel Sambuc
1561*0a6a1f1dSLionel Sambuc // Load the vfptr and then callee from the vftable. The callee should have
1562*0a6a1f1dSLionel Sambuc // adjusted 'this' so that the vfptr is at offset zero.
1563*0a6a1f1dSLionel Sambuc llvm::Value *VTable = CGF.GetVTablePtr(
1564*0a6a1f1dSLionel Sambuc getThisValue(CGF), ThunkTy->getPointerTo()->getPointerTo());
1565*0a6a1f1dSLionel Sambuc llvm::Value *VFuncPtr =
1566*0a6a1f1dSLionel Sambuc CGF.Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn");
1567*0a6a1f1dSLionel Sambuc llvm::Value *Callee = CGF.Builder.CreateLoad(VFuncPtr);
1568*0a6a1f1dSLionel Sambuc
1569*0a6a1f1dSLionel Sambuc CGF.EmitMustTailThunk(MD, getThisValue(CGF), Callee);
1570f4a2713aSLionel Sambuc
1571f4a2713aSLionel Sambuc return ThunkFn;
1572f4a2713aSLionel Sambuc }
1573f4a2713aSLionel Sambuc
emitVirtualInheritanceTables(const CXXRecordDecl * RD)1574f4a2713aSLionel Sambuc void MicrosoftCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) {
1575*0a6a1f1dSLionel Sambuc const VBTableGlobals &VBGlobals = enumerateVBTables(RD);
1576*0a6a1f1dSLionel Sambuc for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) {
1577*0a6a1f1dSLionel Sambuc const VPtrInfo *VBT = (*VBGlobals.VBTables)[I];
1578*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *GV = VBGlobals.Globals[I];
1579*0a6a1f1dSLionel Sambuc emitVBTableDefinition(*VBT, RD, GV);
1580f4a2713aSLionel Sambuc }
1581f4a2713aSLionel Sambuc }
1582f4a2713aSLionel Sambuc
1583*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *
getAddrOfVBTable(const VPtrInfo & VBT,const CXXRecordDecl * RD,llvm::GlobalVariable::LinkageTypes Linkage)1584*0a6a1f1dSLionel Sambuc MicrosoftCXXABI::getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD,
1585*0a6a1f1dSLionel Sambuc llvm::GlobalVariable::LinkageTypes Linkage) {
1586*0a6a1f1dSLionel Sambuc SmallString<256> OutName;
1587*0a6a1f1dSLionel Sambuc llvm::raw_svector_ostream Out(OutName);
1588*0a6a1f1dSLionel Sambuc getMangleContext().mangleCXXVBTable(RD, VBT.MangledPath, Out);
1589*0a6a1f1dSLionel Sambuc Out.flush();
1590*0a6a1f1dSLionel Sambuc StringRef Name = OutName.str();
1591*0a6a1f1dSLionel Sambuc
1592*0a6a1f1dSLionel Sambuc llvm::ArrayType *VBTableType =
1593*0a6a1f1dSLionel Sambuc llvm::ArrayType::get(CGM.IntTy, 1 + VBT.ReusingBase->getNumVBases());
1594*0a6a1f1dSLionel Sambuc
1595*0a6a1f1dSLionel Sambuc assert(!CGM.getModule().getNamedGlobal(Name) &&
1596*0a6a1f1dSLionel Sambuc "vbtable with this name already exists: mangling bug?");
1597*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *GV =
1598*0a6a1f1dSLionel Sambuc CGM.CreateOrReplaceCXXRuntimeVariable(Name, VBTableType, Linkage);
1599*0a6a1f1dSLionel Sambuc GV->setUnnamedAddr(true);
1600*0a6a1f1dSLionel Sambuc
1601*0a6a1f1dSLionel Sambuc if (RD->hasAttr<DLLImportAttr>())
1602*0a6a1f1dSLionel Sambuc GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
1603*0a6a1f1dSLionel Sambuc else if (RD->hasAttr<DLLExportAttr>())
1604*0a6a1f1dSLionel Sambuc GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
1605*0a6a1f1dSLionel Sambuc
1606*0a6a1f1dSLionel Sambuc return GV;
1607*0a6a1f1dSLionel Sambuc }
1608*0a6a1f1dSLionel Sambuc
emitVBTableDefinition(const VPtrInfo & VBT,const CXXRecordDecl * RD,llvm::GlobalVariable * GV) const1609*0a6a1f1dSLionel Sambuc void MicrosoftCXXABI::emitVBTableDefinition(const VPtrInfo &VBT,
1610*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD,
1611*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *GV) const {
1612*0a6a1f1dSLionel Sambuc const CXXRecordDecl *ReusingBase = VBT.ReusingBase;
1613*0a6a1f1dSLionel Sambuc
1614*0a6a1f1dSLionel Sambuc assert(RD->getNumVBases() && ReusingBase->getNumVBases() &&
1615*0a6a1f1dSLionel Sambuc "should only emit vbtables for classes with vbtables");
1616*0a6a1f1dSLionel Sambuc
1617*0a6a1f1dSLionel Sambuc const ASTRecordLayout &BaseLayout =
1618*0a6a1f1dSLionel Sambuc CGM.getContext().getASTRecordLayout(VBT.BaseWithVPtr);
1619*0a6a1f1dSLionel Sambuc const ASTRecordLayout &DerivedLayout =
1620*0a6a1f1dSLionel Sambuc CGM.getContext().getASTRecordLayout(RD);
1621*0a6a1f1dSLionel Sambuc
1622*0a6a1f1dSLionel Sambuc SmallVector<llvm::Constant *, 4> Offsets(1 + ReusingBase->getNumVBases(),
1623*0a6a1f1dSLionel Sambuc nullptr);
1624*0a6a1f1dSLionel Sambuc
1625*0a6a1f1dSLionel Sambuc // The offset from ReusingBase's vbptr to itself always leads.
1626*0a6a1f1dSLionel Sambuc CharUnits VBPtrOffset = BaseLayout.getVBPtrOffset();
1627*0a6a1f1dSLionel Sambuc Offsets[0] = llvm::ConstantInt::get(CGM.IntTy, -VBPtrOffset.getQuantity());
1628*0a6a1f1dSLionel Sambuc
1629*0a6a1f1dSLionel Sambuc MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext();
1630*0a6a1f1dSLionel Sambuc for (const auto &I : ReusingBase->vbases()) {
1631*0a6a1f1dSLionel Sambuc const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl();
1632*0a6a1f1dSLionel Sambuc CharUnits Offset = DerivedLayout.getVBaseClassOffset(VBase);
1633*0a6a1f1dSLionel Sambuc assert(!Offset.isNegative());
1634*0a6a1f1dSLionel Sambuc
1635*0a6a1f1dSLionel Sambuc // Make it relative to the subobject vbptr.
1636*0a6a1f1dSLionel Sambuc CharUnits CompleteVBPtrOffset = VBT.NonVirtualOffset + VBPtrOffset;
1637*0a6a1f1dSLionel Sambuc if (VBT.getVBaseWithVPtr())
1638*0a6a1f1dSLionel Sambuc CompleteVBPtrOffset +=
1639*0a6a1f1dSLionel Sambuc DerivedLayout.getVBaseClassOffset(VBT.getVBaseWithVPtr());
1640*0a6a1f1dSLionel Sambuc Offset -= CompleteVBPtrOffset;
1641*0a6a1f1dSLionel Sambuc
1642*0a6a1f1dSLionel Sambuc unsigned VBIndex = Context.getVBTableIndex(ReusingBase, VBase);
1643*0a6a1f1dSLionel Sambuc assert(Offsets[VBIndex] == nullptr && "The same vbindex seen twice?");
1644*0a6a1f1dSLionel Sambuc Offsets[VBIndex] = llvm::ConstantInt::get(CGM.IntTy, Offset.getQuantity());
1645*0a6a1f1dSLionel Sambuc }
1646*0a6a1f1dSLionel Sambuc
1647*0a6a1f1dSLionel Sambuc assert(Offsets.size() ==
1648*0a6a1f1dSLionel Sambuc cast<llvm::ArrayType>(cast<llvm::PointerType>(GV->getType())
1649*0a6a1f1dSLionel Sambuc ->getElementType())->getNumElements());
1650*0a6a1f1dSLionel Sambuc llvm::ArrayType *VBTableType =
1651*0a6a1f1dSLionel Sambuc llvm::ArrayType::get(CGM.IntTy, Offsets.size());
1652*0a6a1f1dSLionel Sambuc llvm::Constant *Init = llvm::ConstantArray::get(VBTableType, Offsets);
1653*0a6a1f1dSLionel Sambuc GV->setInitializer(Init);
1654*0a6a1f1dSLionel Sambuc
1655*0a6a1f1dSLionel Sambuc // Set the right visibility.
1656*0a6a1f1dSLionel Sambuc CGM.setGlobalVisibility(GV, RD);
1657*0a6a1f1dSLionel Sambuc }
1658*0a6a1f1dSLionel Sambuc
performThisAdjustment(CodeGenFunction & CGF,llvm::Value * This,const ThisAdjustment & TA)1659f4a2713aSLionel Sambuc llvm::Value *MicrosoftCXXABI::performThisAdjustment(CodeGenFunction &CGF,
1660f4a2713aSLionel Sambuc llvm::Value *This,
1661f4a2713aSLionel Sambuc const ThisAdjustment &TA) {
1662f4a2713aSLionel Sambuc if (TA.isEmpty())
1663f4a2713aSLionel Sambuc return This;
1664f4a2713aSLionel Sambuc
1665f4a2713aSLionel Sambuc llvm::Value *V = CGF.Builder.CreateBitCast(This, CGF.Int8PtrTy);
1666f4a2713aSLionel Sambuc
1667f4a2713aSLionel Sambuc if (!TA.Virtual.isEmpty()) {
1668f4a2713aSLionel Sambuc assert(TA.Virtual.Microsoft.VtordispOffset < 0);
1669f4a2713aSLionel Sambuc // Adjust the this argument based on the vtordisp value.
1670f4a2713aSLionel Sambuc llvm::Value *VtorDispPtr =
1671f4a2713aSLionel Sambuc CGF.Builder.CreateConstGEP1_32(V, TA.Virtual.Microsoft.VtordispOffset);
1672f4a2713aSLionel Sambuc VtorDispPtr =
1673f4a2713aSLionel Sambuc CGF.Builder.CreateBitCast(VtorDispPtr, CGF.Int32Ty->getPointerTo());
1674f4a2713aSLionel Sambuc llvm::Value *VtorDisp = CGF.Builder.CreateLoad(VtorDispPtr, "vtordisp");
1675f4a2713aSLionel Sambuc V = CGF.Builder.CreateGEP(V, CGF.Builder.CreateNeg(VtorDisp));
1676f4a2713aSLionel Sambuc
1677f4a2713aSLionel Sambuc if (TA.Virtual.Microsoft.VBPtrOffset) {
1678f4a2713aSLionel Sambuc // If the final overrider is defined in a virtual base other than the one
1679f4a2713aSLionel Sambuc // that holds the vfptr, we have to use a vtordispex thunk which looks up
1680f4a2713aSLionel Sambuc // the vbtable of the derived class.
1681f4a2713aSLionel Sambuc assert(TA.Virtual.Microsoft.VBPtrOffset > 0);
1682f4a2713aSLionel Sambuc assert(TA.Virtual.Microsoft.VBOffsetOffset >= 0);
1683f4a2713aSLionel Sambuc llvm::Value *VBPtr;
1684f4a2713aSLionel Sambuc llvm::Value *VBaseOffset =
1685f4a2713aSLionel Sambuc GetVBaseOffsetFromVBPtr(CGF, V, -TA.Virtual.Microsoft.VBPtrOffset,
1686f4a2713aSLionel Sambuc TA.Virtual.Microsoft.VBOffsetOffset, &VBPtr);
1687f4a2713aSLionel Sambuc V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset);
1688f4a2713aSLionel Sambuc }
1689f4a2713aSLionel Sambuc }
1690f4a2713aSLionel Sambuc
1691f4a2713aSLionel Sambuc if (TA.NonVirtual) {
1692f4a2713aSLionel Sambuc // Non-virtual adjustment might result in a pointer outside the allocated
1693f4a2713aSLionel Sambuc // object, e.g. if the final overrider class is laid out after the virtual
1694f4a2713aSLionel Sambuc // base that declares a method in the most derived class.
1695f4a2713aSLionel Sambuc V = CGF.Builder.CreateConstGEP1_32(V, TA.NonVirtual);
1696f4a2713aSLionel Sambuc }
1697f4a2713aSLionel Sambuc
1698f4a2713aSLionel Sambuc // Don't need to bitcast back, the call CodeGen will handle this.
1699f4a2713aSLionel Sambuc return V;
1700f4a2713aSLionel Sambuc }
1701f4a2713aSLionel Sambuc
1702f4a2713aSLionel Sambuc llvm::Value *
performReturnAdjustment(CodeGenFunction & CGF,llvm::Value * Ret,const ReturnAdjustment & RA)1703f4a2713aSLionel Sambuc MicrosoftCXXABI::performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret,
1704f4a2713aSLionel Sambuc const ReturnAdjustment &RA) {
1705f4a2713aSLionel Sambuc if (RA.isEmpty())
1706f4a2713aSLionel Sambuc return Ret;
1707f4a2713aSLionel Sambuc
1708f4a2713aSLionel Sambuc llvm::Value *V = CGF.Builder.CreateBitCast(Ret, CGF.Int8PtrTy);
1709f4a2713aSLionel Sambuc
1710f4a2713aSLionel Sambuc if (RA.Virtual.Microsoft.VBIndex) {
1711f4a2713aSLionel Sambuc assert(RA.Virtual.Microsoft.VBIndex > 0);
1712f4a2713aSLionel Sambuc int32_t IntSize =
1713f4a2713aSLionel Sambuc getContext().getTypeSizeInChars(getContext().IntTy).getQuantity();
1714f4a2713aSLionel Sambuc llvm::Value *VBPtr;
1715f4a2713aSLionel Sambuc llvm::Value *VBaseOffset =
1716f4a2713aSLionel Sambuc GetVBaseOffsetFromVBPtr(CGF, V, RA.Virtual.Microsoft.VBPtrOffset,
1717f4a2713aSLionel Sambuc IntSize * RA.Virtual.Microsoft.VBIndex, &VBPtr);
1718f4a2713aSLionel Sambuc V = CGF.Builder.CreateInBoundsGEP(VBPtr, VBaseOffset);
1719f4a2713aSLionel Sambuc }
1720f4a2713aSLionel Sambuc
1721f4a2713aSLionel Sambuc if (RA.NonVirtual)
1722f4a2713aSLionel Sambuc V = CGF.Builder.CreateConstInBoundsGEP1_32(V, RA.NonVirtual);
1723f4a2713aSLionel Sambuc
1724f4a2713aSLionel Sambuc // Cast back to the original type.
1725f4a2713aSLionel Sambuc return CGF.Builder.CreateBitCast(V, Ret->getType());
1726f4a2713aSLionel Sambuc }
1727f4a2713aSLionel Sambuc
requiresArrayCookie(const CXXDeleteExpr * expr,QualType elementType)1728f4a2713aSLionel Sambuc bool MicrosoftCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr,
1729f4a2713aSLionel Sambuc QualType elementType) {
1730f4a2713aSLionel Sambuc // Microsoft seems to completely ignore the possibility of a
1731f4a2713aSLionel Sambuc // two-argument usual deallocation function.
1732f4a2713aSLionel Sambuc return elementType.isDestructedType();
1733f4a2713aSLionel Sambuc }
1734f4a2713aSLionel Sambuc
requiresArrayCookie(const CXXNewExpr * expr)1735f4a2713aSLionel Sambuc bool MicrosoftCXXABI::requiresArrayCookie(const CXXNewExpr *expr) {
1736f4a2713aSLionel Sambuc // Microsoft seems to completely ignore the possibility of a
1737f4a2713aSLionel Sambuc // two-argument usual deallocation function.
1738f4a2713aSLionel Sambuc return expr->getAllocatedType().isDestructedType();
1739f4a2713aSLionel Sambuc }
1740f4a2713aSLionel Sambuc
getArrayCookieSizeImpl(QualType type)1741f4a2713aSLionel Sambuc CharUnits MicrosoftCXXABI::getArrayCookieSizeImpl(QualType type) {
1742f4a2713aSLionel Sambuc // The array cookie is always a size_t; we then pad that out to the
1743f4a2713aSLionel Sambuc // alignment of the element type.
1744f4a2713aSLionel Sambuc ASTContext &Ctx = getContext();
1745f4a2713aSLionel Sambuc return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()),
1746f4a2713aSLionel Sambuc Ctx.getTypeAlignInChars(type));
1747f4a2713aSLionel Sambuc }
1748f4a2713aSLionel Sambuc
readArrayCookieImpl(CodeGenFunction & CGF,llvm::Value * allocPtr,CharUnits cookieSize)1749f4a2713aSLionel Sambuc llvm::Value *MicrosoftCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
1750f4a2713aSLionel Sambuc llvm::Value *allocPtr,
1751f4a2713aSLionel Sambuc CharUnits cookieSize) {
1752f4a2713aSLionel Sambuc unsigned AS = allocPtr->getType()->getPointerAddressSpace();
1753f4a2713aSLionel Sambuc llvm::Value *numElementsPtr =
1754f4a2713aSLionel Sambuc CGF.Builder.CreateBitCast(allocPtr, CGF.SizeTy->getPointerTo(AS));
1755f4a2713aSLionel Sambuc return CGF.Builder.CreateLoad(numElementsPtr);
1756f4a2713aSLionel Sambuc }
1757f4a2713aSLionel Sambuc
InitializeArrayCookie(CodeGenFunction & CGF,llvm::Value * newPtr,llvm::Value * numElements,const CXXNewExpr * expr,QualType elementType)1758f4a2713aSLionel Sambuc llvm::Value* MicrosoftCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
1759f4a2713aSLionel Sambuc llvm::Value *newPtr,
1760f4a2713aSLionel Sambuc llvm::Value *numElements,
1761f4a2713aSLionel Sambuc const CXXNewExpr *expr,
1762f4a2713aSLionel Sambuc QualType elementType) {
1763f4a2713aSLionel Sambuc assert(requiresArrayCookie(expr));
1764f4a2713aSLionel Sambuc
1765f4a2713aSLionel Sambuc // The size of the cookie.
1766f4a2713aSLionel Sambuc CharUnits cookieSize = getArrayCookieSizeImpl(elementType);
1767f4a2713aSLionel Sambuc
1768f4a2713aSLionel Sambuc // Compute an offset to the cookie.
1769f4a2713aSLionel Sambuc llvm::Value *cookiePtr = newPtr;
1770f4a2713aSLionel Sambuc
1771f4a2713aSLionel Sambuc // Write the number of elements into the appropriate slot.
1772f4a2713aSLionel Sambuc unsigned AS = newPtr->getType()->getPointerAddressSpace();
1773f4a2713aSLionel Sambuc llvm::Value *numElementsPtr
1774f4a2713aSLionel Sambuc = CGF.Builder.CreateBitCast(cookiePtr, CGF.SizeTy->getPointerTo(AS));
1775f4a2713aSLionel Sambuc CGF.Builder.CreateStore(numElements, numElementsPtr);
1776f4a2713aSLionel Sambuc
1777f4a2713aSLionel Sambuc // Finally, compute a pointer to the actual data buffer by skipping
1778f4a2713aSLionel Sambuc // over the cookie completely.
1779f4a2713aSLionel Sambuc return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr,
1780f4a2713aSLionel Sambuc cookieSize.getQuantity());
1781f4a2713aSLionel Sambuc }
1782f4a2713aSLionel Sambuc
emitGlobalDtorWithTLRegDtor(CodeGenFunction & CGF,const VarDecl & VD,llvm::Constant * Dtor,llvm::Constant * Addr)1783*0a6a1f1dSLionel Sambuc static void emitGlobalDtorWithTLRegDtor(CodeGenFunction &CGF, const VarDecl &VD,
1784*0a6a1f1dSLionel Sambuc llvm::Constant *Dtor,
1785*0a6a1f1dSLionel Sambuc llvm::Constant *Addr) {
1786*0a6a1f1dSLionel Sambuc // Create a function which calls the destructor.
1787*0a6a1f1dSLionel Sambuc llvm::Constant *DtorStub = CGF.createAtExitStub(VD, Dtor, Addr);
1788*0a6a1f1dSLionel Sambuc
1789*0a6a1f1dSLionel Sambuc // extern "C" int __tlregdtor(void (*f)(void));
1790*0a6a1f1dSLionel Sambuc llvm::FunctionType *TLRegDtorTy = llvm::FunctionType::get(
1791*0a6a1f1dSLionel Sambuc CGF.IntTy, DtorStub->getType(), /*IsVarArg=*/false);
1792*0a6a1f1dSLionel Sambuc
1793*0a6a1f1dSLionel Sambuc llvm::Constant *TLRegDtor =
1794*0a6a1f1dSLionel Sambuc CGF.CGM.CreateRuntimeFunction(TLRegDtorTy, "__tlregdtor");
1795*0a6a1f1dSLionel Sambuc if (llvm::Function *TLRegDtorFn = dyn_cast<llvm::Function>(TLRegDtor))
1796*0a6a1f1dSLionel Sambuc TLRegDtorFn->setDoesNotThrow();
1797*0a6a1f1dSLionel Sambuc
1798*0a6a1f1dSLionel Sambuc CGF.EmitNounwindRuntimeCall(TLRegDtor, DtorStub);
1799*0a6a1f1dSLionel Sambuc }
1800*0a6a1f1dSLionel Sambuc
registerGlobalDtor(CodeGenFunction & CGF,const VarDecl & D,llvm::Constant * Dtor,llvm::Constant * Addr)1801*0a6a1f1dSLionel Sambuc void MicrosoftCXXABI::registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
1802*0a6a1f1dSLionel Sambuc llvm::Constant *Dtor,
1803*0a6a1f1dSLionel Sambuc llvm::Constant *Addr) {
1804*0a6a1f1dSLionel Sambuc if (D.getTLSKind())
1805*0a6a1f1dSLionel Sambuc return emitGlobalDtorWithTLRegDtor(CGF, D, Dtor, Addr);
1806*0a6a1f1dSLionel Sambuc
1807*0a6a1f1dSLionel Sambuc // The default behavior is to use atexit.
1808*0a6a1f1dSLionel Sambuc CGF.registerGlobalDtorWithAtExit(D, Dtor, Addr);
1809*0a6a1f1dSLionel Sambuc }
1810*0a6a1f1dSLionel Sambuc
EmitThreadLocalInitFuncs(CodeGenModule & CGM,ArrayRef<std::pair<const VarDecl *,llvm::GlobalVariable * >> CXXThreadLocals,ArrayRef<llvm::Function * > CXXThreadLocalInits,ArrayRef<llvm::GlobalVariable * > CXXThreadLocalInitVars)1811*0a6a1f1dSLionel Sambuc void MicrosoftCXXABI::EmitThreadLocalInitFuncs(
1812*0a6a1f1dSLionel Sambuc CodeGenModule &CGM,
1813*0a6a1f1dSLionel Sambuc ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>>
1814*0a6a1f1dSLionel Sambuc CXXThreadLocals,
1815*0a6a1f1dSLionel Sambuc ArrayRef<llvm::Function *> CXXThreadLocalInits,
1816*0a6a1f1dSLionel Sambuc ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) {
1817*0a6a1f1dSLionel Sambuc // This will create a GV in the .CRT$XDU section. It will point to our
1818*0a6a1f1dSLionel Sambuc // initialization function. The CRT will call all of these function
1819*0a6a1f1dSLionel Sambuc // pointers at start-up time and, eventually, at thread-creation time.
1820*0a6a1f1dSLionel Sambuc auto AddToXDU = [&CGM](llvm::Function *InitFunc) {
1821*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *InitFuncPtr = new llvm::GlobalVariable(
1822*0a6a1f1dSLionel Sambuc CGM.getModule(), InitFunc->getType(), /*IsConstant=*/true,
1823*0a6a1f1dSLionel Sambuc llvm::GlobalVariable::InternalLinkage, InitFunc,
1824*0a6a1f1dSLionel Sambuc Twine(InitFunc->getName(), "$initializer$"));
1825*0a6a1f1dSLionel Sambuc InitFuncPtr->setSection(".CRT$XDU");
1826*0a6a1f1dSLionel Sambuc // This variable has discardable linkage, we have to add it to @llvm.used to
1827*0a6a1f1dSLionel Sambuc // ensure it won't get discarded.
1828*0a6a1f1dSLionel Sambuc CGM.addUsedGlobal(InitFuncPtr);
1829*0a6a1f1dSLionel Sambuc return InitFuncPtr;
1830*0a6a1f1dSLionel Sambuc };
1831*0a6a1f1dSLionel Sambuc
1832*0a6a1f1dSLionel Sambuc std::vector<llvm::Function *> NonComdatInits;
1833*0a6a1f1dSLionel Sambuc for (size_t I = 0, E = CXXThreadLocalInitVars.size(); I != E; ++I) {
1834*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *GV = CXXThreadLocalInitVars[I];
1835*0a6a1f1dSLionel Sambuc llvm::Function *F = CXXThreadLocalInits[I];
1836*0a6a1f1dSLionel Sambuc
1837*0a6a1f1dSLionel Sambuc // If the GV is already in a comdat group, then we have to join it.
1838*0a6a1f1dSLionel Sambuc llvm::Comdat *C = GV->getComdat();
1839*0a6a1f1dSLionel Sambuc
1840*0a6a1f1dSLionel Sambuc // LinkOnce and Weak linkage are lowered down to a single-member comdat
1841*0a6a1f1dSLionel Sambuc // group.
1842*0a6a1f1dSLionel Sambuc // Make an explicit group so we can join it.
1843*0a6a1f1dSLionel Sambuc if (!C && (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())) {
1844*0a6a1f1dSLionel Sambuc C = CGM.getModule().getOrInsertComdat(GV->getName());
1845*0a6a1f1dSLionel Sambuc GV->setComdat(C);
1846*0a6a1f1dSLionel Sambuc AddToXDU(F)->setComdat(C);
1847*0a6a1f1dSLionel Sambuc } else {
1848*0a6a1f1dSLionel Sambuc NonComdatInits.push_back(F);
1849*0a6a1f1dSLionel Sambuc }
1850*0a6a1f1dSLionel Sambuc }
1851*0a6a1f1dSLionel Sambuc
1852*0a6a1f1dSLionel Sambuc if (!NonComdatInits.empty()) {
1853*0a6a1f1dSLionel Sambuc llvm::FunctionType *FTy =
1854*0a6a1f1dSLionel Sambuc llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
1855*0a6a1f1dSLionel Sambuc llvm::Function *InitFunc = CGM.CreateGlobalInitOrDestructFunction(
1856*0a6a1f1dSLionel Sambuc FTy, "__tls_init", SourceLocation(),
1857*0a6a1f1dSLionel Sambuc /*TLS=*/true);
1858*0a6a1f1dSLionel Sambuc CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(InitFunc, NonComdatInits);
1859*0a6a1f1dSLionel Sambuc
1860*0a6a1f1dSLionel Sambuc AddToXDU(InitFunc);
1861*0a6a1f1dSLionel Sambuc }
1862*0a6a1f1dSLionel Sambuc }
1863*0a6a1f1dSLionel Sambuc
EmitThreadLocalVarDeclLValue(CodeGenFunction & CGF,const VarDecl * VD,QualType LValType)1864*0a6a1f1dSLionel Sambuc LValue MicrosoftCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
1865*0a6a1f1dSLionel Sambuc const VarDecl *VD,
1866*0a6a1f1dSLionel Sambuc QualType LValType) {
1867*0a6a1f1dSLionel Sambuc CGF.CGM.ErrorUnsupported(VD, "thread wrappers");
1868*0a6a1f1dSLionel Sambuc return LValue();
1869*0a6a1f1dSLionel Sambuc }
1870*0a6a1f1dSLionel Sambuc
EmitGuardedInit(CodeGenFunction & CGF,const VarDecl & D,llvm::GlobalVariable * GV,bool PerformInit)1871f4a2713aSLionel Sambuc void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
1872f4a2713aSLionel Sambuc llvm::GlobalVariable *GV,
1873f4a2713aSLionel Sambuc bool PerformInit) {
1874*0a6a1f1dSLionel Sambuc // MSVC only uses guards for static locals.
1875*0a6a1f1dSLionel Sambuc if (!D.isStaticLocal()) {
1876*0a6a1f1dSLionel Sambuc assert(GV->hasWeakLinkage() || GV->hasLinkOnceLinkage());
1877*0a6a1f1dSLionel Sambuc // GlobalOpt is allowed to discard the initializer, so use linkonce_odr.
1878*0a6a1f1dSLionel Sambuc CGF.CurFn->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage);
1879*0a6a1f1dSLionel Sambuc CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit);
1880*0a6a1f1dSLionel Sambuc return;
1881*0a6a1f1dSLionel Sambuc }
1882*0a6a1f1dSLionel Sambuc
1883f4a2713aSLionel Sambuc // MSVC always uses an i32 bitfield to guard initialization, which is *not*
1884f4a2713aSLionel Sambuc // threadsafe. Since the user may be linking in inline functions compiled by
1885f4a2713aSLionel Sambuc // cl.exe, there's no reason to provide a false sense of security by using
1886f4a2713aSLionel Sambuc // critical sections here.
1887f4a2713aSLionel Sambuc
1888f4a2713aSLionel Sambuc if (D.getTLSKind())
1889f4a2713aSLionel Sambuc CGM.ErrorUnsupported(&D, "dynamic TLS initialization");
1890f4a2713aSLionel Sambuc
1891f4a2713aSLionel Sambuc CGBuilderTy &Builder = CGF.Builder;
1892f4a2713aSLionel Sambuc llvm::IntegerType *GuardTy = CGF.Int32Ty;
1893f4a2713aSLionel Sambuc llvm::ConstantInt *Zero = llvm::ConstantInt::get(GuardTy, 0);
1894f4a2713aSLionel Sambuc
1895f4a2713aSLionel Sambuc // Get the guard variable for this function if we have one already.
1896*0a6a1f1dSLionel Sambuc GuardInfo *GI = &GuardVariableMap[D.getDeclContext()];
1897f4a2713aSLionel Sambuc
1898f4a2713aSLionel Sambuc unsigned BitIndex;
1899*0a6a1f1dSLionel Sambuc if (D.isStaticLocal() && D.isExternallyVisible()) {
1900f4a2713aSLionel Sambuc // Externally visible variables have to be numbered in Sema to properly
1901f4a2713aSLionel Sambuc // handle unreachable VarDecls.
1902*0a6a1f1dSLionel Sambuc BitIndex = getContext().getStaticLocalNumber(&D);
1903f4a2713aSLionel Sambuc assert(BitIndex > 0);
1904f4a2713aSLionel Sambuc BitIndex--;
1905f4a2713aSLionel Sambuc } else {
1906f4a2713aSLionel Sambuc // Non-externally visible variables are numbered here in CodeGen.
1907*0a6a1f1dSLionel Sambuc BitIndex = GI->BitIndex++;
1908f4a2713aSLionel Sambuc }
1909f4a2713aSLionel Sambuc
1910f4a2713aSLionel Sambuc if (BitIndex >= 32) {
1911f4a2713aSLionel Sambuc if (D.isExternallyVisible())
1912f4a2713aSLionel Sambuc ErrorUnsupportedABI(CGF, "more than 32 guarded initializations");
1913f4a2713aSLionel Sambuc BitIndex %= 32;
1914*0a6a1f1dSLionel Sambuc GI->Guard = nullptr;
1915f4a2713aSLionel Sambuc }
1916f4a2713aSLionel Sambuc
1917f4a2713aSLionel Sambuc // Lazily create the i32 bitfield for this function.
1918*0a6a1f1dSLionel Sambuc if (!GI->Guard) {
1919f4a2713aSLionel Sambuc // Mangle the name for the guard.
1920f4a2713aSLionel Sambuc SmallString<256> GuardName;
1921f4a2713aSLionel Sambuc {
1922f4a2713aSLionel Sambuc llvm::raw_svector_ostream Out(GuardName);
1923f4a2713aSLionel Sambuc getMangleContext().mangleStaticGuardVariable(&D, Out);
1924f4a2713aSLionel Sambuc Out.flush();
1925f4a2713aSLionel Sambuc }
1926f4a2713aSLionel Sambuc
1927*0a6a1f1dSLionel Sambuc // Create the guard variable with a zero-initializer. Just absorb linkage,
1928*0a6a1f1dSLionel Sambuc // visibility and dll storage class from the guarded variable.
1929*0a6a1f1dSLionel Sambuc GI->Guard =
1930*0a6a1f1dSLionel Sambuc new llvm::GlobalVariable(CGM.getModule(), GuardTy, false,
1931f4a2713aSLionel Sambuc GV->getLinkage(), Zero, GuardName.str());
1932*0a6a1f1dSLionel Sambuc GI->Guard->setVisibility(GV->getVisibility());
1933*0a6a1f1dSLionel Sambuc GI->Guard->setDLLStorageClass(GV->getDLLStorageClass());
1934f4a2713aSLionel Sambuc } else {
1935*0a6a1f1dSLionel Sambuc assert(GI->Guard->getLinkage() == GV->getLinkage() &&
1936f4a2713aSLionel Sambuc "static local from the same function had different linkage");
1937f4a2713aSLionel Sambuc }
1938f4a2713aSLionel Sambuc
1939f4a2713aSLionel Sambuc // Pseudo code for the test:
1940f4a2713aSLionel Sambuc // if (!(GuardVar & MyGuardBit)) {
1941f4a2713aSLionel Sambuc // GuardVar |= MyGuardBit;
1942f4a2713aSLionel Sambuc // ... initialize the object ...;
1943f4a2713aSLionel Sambuc // }
1944f4a2713aSLionel Sambuc
1945f4a2713aSLionel Sambuc // Test our bit from the guard variable.
1946f4a2713aSLionel Sambuc llvm::ConstantInt *Bit = llvm::ConstantInt::get(GuardTy, 1U << BitIndex);
1947*0a6a1f1dSLionel Sambuc llvm::LoadInst *LI = Builder.CreateLoad(GI->Guard);
1948f4a2713aSLionel Sambuc llvm::Value *IsInitialized =
1949f4a2713aSLionel Sambuc Builder.CreateICmpNE(Builder.CreateAnd(LI, Bit), Zero);
1950f4a2713aSLionel Sambuc llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
1951f4a2713aSLionel Sambuc llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
1952f4a2713aSLionel Sambuc Builder.CreateCondBr(IsInitialized, EndBlock, InitBlock);
1953f4a2713aSLionel Sambuc
1954f4a2713aSLionel Sambuc // Set our bit in the guard variable and emit the initializer and add a global
1955f4a2713aSLionel Sambuc // destructor if appropriate.
1956f4a2713aSLionel Sambuc CGF.EmitBlock(InitBlock);
1957*0a6a1f1dSLionel Sambuc Builder.CreateStore(Builder.CreateOr(LI, Bit), GI->Guard);
1958f4a2713aSLionel Sambuc CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit);
1959f4a2713aSLionel Sambuc Builder.CreateBr(EndBlock);
1960f4a2713aSLionel Sambuc
1961f4a2713aSLionel Sambuc // Continue.
1962f4a2713aSLionel Sambuc CGF.EmitBlock(EndBlock);
1963f4a2713aSLionel Sambuc }
1964f4a2713aSLionel Sambuc
isZeroInitializable(const MemberPointerType * MPT)1965f4a2713aSLionel Sambuc bool MicrosoftCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
1966f4a2713aSLionel Sambuc // Null-ness for function memptrs only depends on the first field, which is
1967f4a2713aSLionel Sambuc // the function pointer. The rest don't matter, so we can zero initialize.
1968f4a2713aSLionel Sambuc if (MPT->isMemberFunctionPointer())
1969f4a2713aSLionel Sambuc return true;
1970f4a2713aSLionel Sambuc
1971f4a2713aSLionel Sambuc // The virtual base adjustment field is always -1 for null, so if we have one
1972f4a2713aSLionel Sambuc // we can't zero initialize. The field offset is sometimes also -1 if 0 is a
1973f4a2713aSLionel Sambuc // valid field offset.
1974*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
1975*0a6a1f1dSLionel Sambuc MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
1976*0a6a1f1dSLionel Sambuc return (!MSInheritanceAttr::hasVBTableOffsetField(Inheritance) &&
1977*0a6a1f1dSLionel Sambuc RD->nullFieldOffsetIsZero());
1978f4a2713aSLionel Sambuc }
1979f4a2713aSLionel Sambuc
1980f4a2713aSLionel Sambuc llvm::Type *
ConvertMemberPointerType(const MemberPointerType * MPT)1981f4a2713aSLionel Sambuc MicrosoftCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
1982*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
1983*0a6a1f1dSLionel Sambuc MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
1984f4a2713aSLionel Sambuc llvm::SmallVector<llvm::Type *, 4> fields;
1985f4a2713aSLionel Sambuc if (MPT->isMemberFunctionPointer())
1986f4a2713aSLionel Sambuc fields.push_back(CGM.VoidPtrTy); // FunctionPointerOrVirtualThunk
1987f4a2713aSLionel Sambuc else
1988f4a2713aSLionel Sambuc fields.push_back(CGM.IntTy); // FieldOffset
1989f4a2713aSLionel Sambuc
1990*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(),
1991f4a2713aSLionel Sambuc Inheritance))
1992f4a2713aSLionel Sambuc fields.push_back(CGM.IntTy);
1993*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
1994f4a2713aSLionel Sambuc fields.push_back(CGM.IntTy);
1995*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
1996f4a2713aSLionel Sambuc fields.push_back(CGM.IntTy); // VirtualBaseAdjustmentOffset
1997f4a2713aSLionel Sambuc
1998f4a2713aSLionel Sambuc if (fields.size() == 1)
1999f4a2713aSLionel Sambuc return fields[0];
2000f4a2713aSLionel Sambuc return llvm::StructType::get(CGM.getLLVMContext(), fields);
2001f4a2713aSLionel Sambuc }
2002f4a2713aSLionel Sambuc
2003f4a2713aSLionel Sambuc void MicrosoftCXXABI::
GetNullMemberPointerFields(const MemberPointerType * MPT,llvm::SmallVectorImpl<llvm::Constant * > & fields)2004f4a2713aSLionel Sambuc GetNullMemberPointerFields(const MemberPointerType *MPT,
2005f4a2713aSLionel Sambuc llvm::SmallVectorImpl<llvm::Constant *> &fields) {
2006f4a2713aSLionel Sambuc assert(fields.empty());
2007*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
2008*0a6a1f1dSLionel Sambuc MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
2009f4a2713aSLionel Sambuc if (MPT->isMemberFunctionPointer()) {
2010f4a2713aSLionel Sambuc // FunctionPointerOrVirtualThunk
2011f4a2713aSLionel Sambuc fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy));
2012f4a2713aSLionel Sambuc } else {
2013*0a6a1f1dSLionel Sambuc if (RD->nullFieldOffsetIsZero())
2014f4a2713aSLionel Sambuc fields.push_back(getZeroInt()); // FieldOffset
2015f4a2713aSLionel Sambuc else
2016f4a2713aSLionel Sambuc fields.push_back(getAllOnesInt()); // FieldOffset
2017f4a2713aSLionel Sambuc }
2018f4a2713aSLionel Sambuc
2019*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(),
2020f4a2713aSLionel Sambuc Inheritance))
2021f4a2713aSLionel Sambuc fields.push_back(getZeroInt());
2022*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
2023f4a2713aSLionel Sambuc fields.push_back(getZeroInt());
2024*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
2025f4a2713aSLionel Sambuc fields.push_back(getAllOnesInt());
2026f4a2713aSLionel Sambuc }
2027f4a2713aSLionel Sambuc
2028f4a2713aSLionel Sambuc llvm::Constant *
EmitNullMemberPointer(const MemberPointerType * MPT)2029f4a2713aSLionel Sambuc MicrosoftCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
2030f4a2713aSLionel Sambuc llvm::SmallVector<llvm::Constant *, 4> fields;
2031f4a2713aSLionel Sambuc GetNullMemberPointerFields(MPT, fields);
2032f4a2713aSLionel Sambuc if (fields.size() == 1)
2033f4a2713aSLionel Sambuc return fields[0];
2034f4a2713aSLionel Sambuc llvm::Constant *Res = llvm::ConstantStruct::getAnon(fields);
2035f4a2713aSLionel Sambuc assert(Res->getType() == ConvertMemberPointerType(MPT));
2036f4a2713aSLionel Sambuc return Res;
2037f4a2713aSLionel Sambuc }
2038f4a2713aSLionel Sambuc
2039f4a2713aSLionel Sambuc llvm::Constant *
EmitFullMemberPointer(llvm::Constant * FirstField,bool IsMemberFunction,const CXXRecordDecl * RD,CharUnits NonVirtualBaseAdjustment)2040f4a2713aSLionel Sambuc MicrosoftCXXABI::EmitFullMemberPointer(llvm::Constant *FirstField,
2041f4a2713aSLionel Sambuc bool IsMemberFunction,
2042f4a2713aSLionel Sambuc const CXXRecordDecl *RD,
2043f4a2713aSLionel Sambuc CharUnits NonVirtualBaseAdjustment)
2044f4a2713aSLionel Sambuc {
2045*0a6a1f1dSLionel Sambuc MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
2046f4a2713aSLionel Sambuc
2047f4a2713aSLionel Sambuc // Single inheritance class member pointer are represented as scalars instead
2048f4a2713aSLionel Sambuc // of aggregates.
2049*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasOnlyOneField(IsMemberFunction, Inheritance))
2050f4a2713aSLionel Sambuc return FirstField;
2051f4a2713aSLionel Sambuc
2052f4a2713aSLionel Sambuc llvm::SmallVector<llvm::Constant *, 4> fields;
2053f4a2713aSLionel Sambuc fields.push_back(FirstField);
2054f4a2713aSLionel Sambuc
2055*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasNVOffsetField(IsMemberFunction, Inheritance))
2056f4a2713aSLionel Sambuc fields.push_back(llvm::ConstantInt::get(
2057f4a2713aSLionel Sambuc CGM.IntTy, NonVirtualBaseAdjustment.getQuantity()));
2058f4a2713aSLionel Sambuc
2059*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) {
2060f4a2713aSLionel Sambuc CharUnits Offs = CharUnits::Zero();
2061f4a2713aSLionel Sambuc if (RD->getNumVBases())
2062*0a6a1f1dSLionel Sambuc Offs = getContext().getASTRecordLayout(RD).getVBPtrOffset();
2063f4a2713aSLionel Sambuc fields.push_back(llvm::ConstantInt::get(CGM.IntTy, Offs.getQuantity()));
2064f4a2713aSLionel Sambuc }
2065f4a2713aSLionel Sambuc
2066f4a2713aSLionel Sambuc // The rest of the fields are adjusted by conversions to a more derived class.
2067*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
2068f4a2713aSLionel Sambuc fields.push_back(getZeroInt());
2069f4a2713aSLionel Sambuc
2070f4a2713aSLionel Sambuc return llvm::ConstantStruct::getAnon(fields);
2071f4a2713aSLionel Sambuc }
2072f4a2713aSLionel Sambuc
2073f4a2713aSLionel Sambuc llvm::Constant *
EmitMemberDataPointer(const MemberPointerType * MPT,CharUnits offset)2074f4a2713aSLionel Sambuc MicrosoftCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
2075f4a2713aSLionel Sambuc CharUnits offset) {
2076*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
2077f4a2713aSLionel Sambuc llvm::Constant *FirstField =
2078f4a2713aSLionel Sambuc llvm::ConstantInt::get(CGM.IntTy, offset.getQuantity());
2079f4a2713aSLionel Sambuc return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/false, RD,
2080f4a2713aSLionel Sambuc CharUnits::Zero());
2081f4a2713aSLionel Sambuc }
2082f4a2713aSLionel Sambuc
EmitMemberPointer(const CXXMethodDecl * MD)2083f4a2713aSLionel Sambuc llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
2084f4a2713aSLionel Sambuc return BuildMemberPointer(MD->getParent(), MD, CharUnits::Zero());
2085f4a2713aSLionel Sambuc }
2086f4a2713aSLionel Sambuc
EmitMemberPointer(const APValue & MP,QualType MPType)2087f4a2713aSLionel Sambuc llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const APValue &MP,
2088f4a2713aSLionel Sambuc QualType MPType) {
2089f4a2713aSLionel Sambuc const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
2090f4a2713aSLionel Sambuc const ValueDecl *MPD = MP.getMemberPointerDecl();
2091f4a2713aSLionel Sambuc if (!MPD)
2092f4a2713aSLionel Sambuc return EmitNullMemberPointer(MPT);
2093f4a2713aSLionel Sambuc
2094f4a2713aSLionel Sambuc CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP);
2095f4a2713aSLionel Sambuc
2096f4a2713aSLionel Sambuc // FIXME PR15713: Support virtual inheritance paths.
2097f4a2713aSLionel Sambuc
2098f4a2713aSLionel Sambuc if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
2099*0a6a1f1dSLionel Sambuc return BuildMemberPointer(MPT->getMostRecentCXXRecordDecl(), MD,
2100*0a6a1f1dSLionel Sambuc ThisAdjustment);
2101f4a2713aSLionel Sambuc
2102f4a2713aSLionel Sambuc CharUnits FieldOffset =
2103f4a2713aSLionel Sambuc getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
2104f4a2713aSLionel Sambuc return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
2105f4a2713aSLionel Sambuc }
2106f4a2713aSLionel Sambuc
2107f4a2713aSLionel Sambuc llvm::Constant *
BuildMemberPointer(const CXXRecordDecl * RD,const CXXMethodDecl * MD,CharUnits NonVirtualBaseAdjustment)2108f4a2713aSLionel Sambuc MicrosoftCXXABI::BuildMemberPointer(const CXXRecordDecl *RD,
2109f4a2713aSLionel Sambuc const CXXMethodDecl *MD,
2110f4a2713aSLionel Sambuc CharUnits NonVirtualBaseAdjustment) {
2111f4a2713aSLionel Sambuc assert(MD->isInstance() && "Member function must not be static!");
2112f4a2713aSLionel Sambuc MD = MD->getCanonicalDecl();
2113*0a6a1f1dSLionel Sambuc RD = RD->getMostRecentDecl();
2114f4a2713aSLionel Sambuc CodeGenTypes &Types = CGM.getTypes();
2115f4a2713aSLionel Sambuc
2116f4a2713aSLionel Sambuc llvm::Constant *FirstField;
2117f4a2713aSLionel Sambuc const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
2118*0a6a1f1dSLionel Sambuc if (!MD->isVirtual()) {
2119f4a2713aSLionel Sambuc llvm::Type *Ty;
2120f4a2713aSLionel Sambuc // Check whether the function has a computable LLVM signature.
2121f4a2713aSLionel Sambuc if (Types.isFuncTypeConvertible(FPT)) {
2122f4a2713aSLionel Sambuc // The function has a computable LLVM signature; use the correct type.
2123f4a2713aSLionel Sambuc Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
2124f4a2713aSLionel Sambuc } else {
2125f4a2713aSLionel Sambuc // Use an arbitrary non-function type to tell GetAddrOfFunction that the
2126f4a2713aSLionel Sambuc // function type is incomplete.
2127f4a2713aSLionel Sambuc Ty = CGM.PtrDiffTy;
2128f4a2713aSLionel Sambuc }
2129f4a2713aSLionel Sambuc FirstField = CGM.GetAddrOfFunction(MD, Ty);
2130f4a2713aSLionel Sambuc FirstField = llvm::ConstantExpr::getBitCast(FirstField, CGM.VoidPtrTy);
2131f4a2713aSLionel Sambuc } else {
2132f4a2713aSLionel Sambuc MicrosoftVTableContext::MethodVFTableLocation ML =
2133f4a2713aSLionel Sambuc CGM.getMicrosoftVTableContext().getMethodVFTableLocation(MD);
2134*0a6a1f1dSLionel Sambuc if (!CGM.getTypes().isFuncTypeConvertible(
2135f4a2713aSLionel Sambuc MD->getType()->castAs<FunctionType>())) {
2136f4a2713aSLionel Sambuc CGM.ErrorUnsupported(MD, "pointer to virtual member function with "
2137f4a2713aSLionel Sambuc "incomplete return or parameter type");
2138f4a2713aSLionel Sambuc FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy);
2139*0a6a1f1dSLionel Sambuc } else if (FPT->getCallConv() == CC_X86FastCall) {
2140*0a6a1f1dSLionel Sambuc CGM.ErrorUnsupported(MD, "pointer to fastcall virtual member function");
2141*0a6a1f1dSLionel Sambuc FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy);
2142f4a2713aSLionel Sambuc } else if (ML.VBase) {
2143f4a2713aSLionel Sambuc CGM.ErrorUnsupported(MD, "pointer to virtual member function overriding "
2144f4a2713aSLionel Sambuc "member function in virtual base class");
2145f4a2713aSLionel Sambuc FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy);
2146f4a2713aSLionel Sambuc } else {
2147*0a6a1f1dSLionel Sambuc llvm::Function *Thunk = EmitVirtualMemPtrThunk(MD, ML);
2148f4a2713aSLionel Sambuc FirstField = llvm::ConstantExpr::getBitCast(Thunk, CGM.VoidPtrTy);
2149*0a6a1f1dSLionel Sambuc // Include the vfptr adjustment if the method is in a non-primary vftable.
2150*0a6a1f1dSLionel Sambuc NonVirtualBaseAdjustment += ML.VFPtrOffset;
2151f4a2713aSLionel Sambuc }
2152f4a2713aSLionel Sambuc }
2153f4a2713aSLionel Sambuc
2154f4a2713aSLionel Sambuc // The rest of the fields are common with data member pointers.
2155f4a2713aSLionel Sambuc return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/true, RD,
2156f4a2713aSLionel Sambuc NonVirtualBaseAdjustment);
2157f4a2713aSLionel Sambuc }
2158f4a2713aSLionel Sambuc
2159f4a2713aSLionel Sambuc /// Member pointers are the same if they're either bitwise identical *or* both
2160f4a2713aSLionel Sambuc /// null. Null-ness for function members is determined by the first field,
2161f4a2713aSLionel Sambuc /// while for data member pointers we must compare all fields.
2162f4a2713aSLionel Sambuc llvm::Value *
EmitMemberPointerComparison(CodeGenFunction & CGF,llvm::Value * L,llvm::Value * R,const MemberPointerType * MPT,bool Inequality)2163f4a2713aSLionel Sambuc MicrosoftCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
2164f4a2713aSLionel Sambuc llvm::Value *L,
2165f4a2713aSLionel Sambuc llvm::Value *R,
2166f4a2713aSLionel Sambuc const MemberPointerType *MPT,
2167f4a2713aSLionel Sambuc bool Inequality) {
2168f4a2713aSLionel Sambuc CGBuilderTy &Builder = CGF.Builder;
2169f4a2713aSLionel Sambuc
2170f4a2713aSLionel Sambuc // Handle != comparisons by switching the sense of all boolean operations.
2171f4a2713aSLionel Sambuc llvm::ICmpInst::Predicate Eq;
2172f4a2713aSLionel Sambuc llvm::Instruction::BinaryOps And, Or;
2173f4a2713aSLionel Sambuc if (Inequality) {
2174f4a2713aSLionel Sambuc Eq = llvm::ICmpInst::ICMP_NE;
2175f4a2713aSLionel Sambuc And = llvm::Instruction::Or;
2176f4a2713aSLionel Sambuc Or = llvm::Instruction::And;
2177f4a2713aSLionel Sambuc } else {
2178f4a2713aSLionel Sambuc Eq = llvm::ICmpInst::ICMP_EQ;
2179f4a2713aSLionel Sambuc And = llvm::Instruction::And;
2180f4a2713aSLionel Sambuc Or = llvm::Instruction::Or;
2181f4a2713aSLionel Sambuc }
2182f4a2713aSLionel Sambuc
2183f4a2713aSLionel Sambuc // If this is a single field member pointer (single inheritance), this is a
2184f4a2713aSLionel Sambuc // single icmp.
2185*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
2186*0a6a1f1dSLionel Sambuc MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
2187*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasOnlyOneField(MPT->isMemberFunctionPointer(),
2188*0a6a1f1dSLionel Sambuc Inheritance))
2189f4a2713aSLionel Sambuc return Builder.CreateICmp(Eq, L, R);
2190f4a2713aSLionel Sambuc
2191f4a2713aSLionel Sambuc // Compare the first field.
2192f4a2713aSLionel Sambuc llvm::Value *L0 = Builder.CreateExtractValue(L, 0, "lhs.0");
2193f4a2713aSLionel Sambuc llvm::Value *R0 = Builder.CreateExtractValue(R, 0, "rhs.0");
2194f4a2713aSLionel Sambuc llvm::Value *Cmp0 = Builder.CreateICmp(Eq, L0, R0, "memptr.cmp.first");
2195f4a2713aSLionel Sambuc
2196f4a2713aSLionel Sambuc // Compare everything other than the first field.
2197*0a6a1f1dSLionel Sambuc llvm::Value *Res = nullptr;
2198f4a2713aSLionel Sambuc llvm::StructType *LType = cast<llvm::StructType>(L->getType());
2199f4a2713aSLionel Sambuc for (unsigned I = 1, E = LType->getNumElements(); I != E; ++I) {
2200f4a2713aSLionel Sambuc llvm::Value *LF = Builder.CreateExtractValue(L, I);
2201f4a2713aSLionel Sambuc llvm::Value *RF = Builder.CreateExtractValue(R, I);
2202f4a2713aSLionel Sambuc llvm::Value *Cmp = Builder.CreateICmp(Eq, LF, RF, "memptr.cmp.rest");
2203f4a2713aSLionel Sambuc if (Res)
2204f4a2713aSLionel Sambuc Res = Builder.CreateBinOp(And, Res, Cmp);
2205f4a2713aSLionel Sambuc else
2206f4a2713aSLionel Sambuc Res = Cmp;
2207f4a2713aSLionel Sambuc }
2208f4a2713aSLionel Sambuc
2209f4a2713aSLionel Sambuc // Check if the first field is 0 if this is a function pointer.
2210f4a2713aSLionel Sambuc if (MPT->isMemberFunctionPointer()) {
2211f4a2713aSLionel Sambuc // (l1 == r1 && ...) || l0 == 0
2212f4a2713aSLionel Sambuc llvm::Value *Zero = llvm::Constant::getNullValue(L0->getType());
2213f4a2713aSLionel Sambuc llvm::Value *IsZero = Builder.CreateICmp(Eq, L0, Zero, "memptr.cmp.iszero");
2214f4a2713aSLionel Sambuc Res = Builder.CreateBinOp(Or, Res, IsZero);
2215f4a2713aSLionel Sambuc }
2216f4a2713aSLionel Sambuc
2217f4a2713aSLionel Sambuc // Combine the comparison of the first field, which must always be true for
2218f4a2713aSLionel Sambuc // this comparison to succeeed.
2219f4a2713aSLionel Sambuc return Builder.CreateBinOp(And, Res, Cmp0, "memptr.cmp");
2220f4a2713aSLionel Sambuc }
2221f4a2713aSLionel Sambuc
2222f4a2713aSLionel Sambuc llvm::Value *
EmitMemberPointerIsNotNull(CodeGenFunction & CGF,llvm::Value * MemPtr,const MemberPointerType * MPT)2223f4a2713aSLionel Sambuc MicrosoftCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
2224f4a2713aSLionel Sambuc llvm::Value *MemPtr,
2225f4a2713aSLionel Sambuc const MemberPointerType *MPT) {
2226f4a2713aSLionel Sambuc CGBuilderTy &Builder = CGF.Builder;
2227f4a2713aSLionel Sambuc llvm::SmallVector<llvm::Constant *, 4> fields;
2228f4a2713aSLionel Sambuc // We only need one field for member functions.
2229f4a2713aSLionel Sambuc if (MPT->isMemberFunctionPointer())
2230f4a2713aSLionel Sambuc fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy));
2231f4a2713aSLionel Sambuc else
2232f4a2713aSLionel Sambuc GetNullMemberPointerFields(MPT, fields);
2233f4a2713aSLionel Sambuc assert(!fields.empty());
2234f4a2713aSLionel Sambuc llvm::Value *FirstField = MemPtr;
2235f4a2713aSLionel Sambuc if (MemPtr->getType()->isStructTy())
2236f4a2713aSLionel Sambuc FirstField = Builder.CreateExtractValue(MemPtr, 0);
2237f4a2713aSLionel Sambuc llvm::Value *Res = Builder.CreateICmpNE(FirstField, fields[0], "memptr.cmp0");
2238f4a2713aSLionel Sambuc
2239f4a2713aSLionel Sambuc // For function member pointers, we only need to test the function pointer
2240f4a2713aSLionel Sambuc // field. The other fields if any can be garbage.
2241f4a2713aSLionel Sambuc if (MPT->isMemberFunctionPointer())
2242f4a2713aSLionel Sambuc return Res;
2243f4a2713aSLionel Sambuc
2244f4a2713aSLionel Sambuc // Otherwise, emit a series of compares and combine the results.
2245f4a2713aSLionel Sambuc for (int I = 1, E = fields.size(); I < E; ++I) {
2246f4a2713aSLionel Sambuc llvm::Value *Field = Builder.CreateExtractValue(MemPtr, I);
2247f4a2713aSLionel Sambuc llvm::Value *Next = Builder.CreateICmpNE(Field, fields[I], "memptr.cmp");
2248*0a6a1f1dSLionel Sambuc Res = Builder.CreateOr(Res, Next, "memptr.tobool");
2249f4a2713aSLionel Sambuc }
2250f4a2713aSLionel Sambuc return Res;
2251f4a2713aSLionel Sambuc }
2252f4a2713aSLionel Sambuc
MemberPointerConstantIsNull(const MemberPointerType * MPT,llvm::Constant * Val)2253f4a2713aSLionel Sambuc bool MicrosoftCXXABI::MemberPointerConstantIsNull(const MemberPointerType *MPT,
2254f4a2713aSLionel Sambuc llvm::Constant *Val) {
2255f4a2713aSLionel Sambuc // Function pointers are null if the pointer in the first field is null.
2256f4a2713aSLionel Sambuc if (MPT->isMemberFunctionPointer()) {
2257f4a2713aSLionel Sambuc llvm::Constant *FirstField = Val->getType()->isStructTy() ?
2258f4a2713aSLionel Sambuc Val->getAggregateElement(0U) : Val;
2259f4a2713aSLionel Sambuc return FirstField->isNullValue();
2260f4a2713aSLionel Sambuc }
2261f4a2713aSLionel Sambuc
2262f4a2713aSLionel Sambuc // If it's not a function pointer and it's zero initializable, we can easily
2263f4a2713aSLionel Sambuc // check zero.
2264f4a2713aSLionel Sambuc if (isZeroInitializable(MPT) && Val->isNullValue())
2265f4a2713aSLionel Sambuc return true;
2266f4a2713aSLionel Sambuc
2267f4a2713aSLionel Sambuc // Otherwise, break down all the fields for comparison. Hopefully these
2268f4a2713aSLionel Sambuc // little Constants are reused, while a big null struct might not be.
2269f4a2713aSLionel Sambuc llvm::SmallVector<llvm::Constant *, 4> Fields;
2270f4a2713aSLionel Sambuc GetNullMemberPointerFields(MPT, Fields);
2271f4a2713aSLionel Sambuc if (Fields.size() == 1) {
2272f4a2713aSLionel Sambuc assert(Val->getType()->isIntegerTy());
2273f4a2713aSLionel Sambuc return Val == Fields[0];
2274f4a2713aSLionel Sambuc }
2275f4a2713aSLionel Sambuc
2276f4a2713aSLionel Sambuc unsigned I, E;
2277f4a2713aSLionel Sambuc for (I = 0, E = Fields.size(); I != E; ++I) {
2278f4a2713aSLionel Sambuc if (Val->getAggregateElement(I) != Fields[I])
2279f4a2713aSLionel Sambuc break;
2280f4a2713aSLionel Sambuc }
2281f4a2713aSLionel Sambuc return I == E;
2282f4a2713aSLionel Sambuc }
2283f4a2713aSLionel Sambuc
2284f4a2713aSLionel Sambuc llvm::Value *
GetVBaseOffsetFromVBPtr(CodeGenFunction & CGF,llvm::Value * This,llvm::Value * VBPtrOffset,llvm::Value * VBTableOffset,llvm::Value ** VBPtrOut)2285f4a2713aSLionel Sambuc MicrosoftCXXABI::GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF,
2286f4a2713aSLionel Sambuc llvm::Value *This,
2287f4a2713aSLionel Sambuc llvm::Value *VBPtrOffset,
2288f4a2713aSLionel Sambuc llvm::Value *VBTableOffset,
2289f4a2713aSLionel Sambuc llvm::Value **VBPtrOut) {
2290f4a2713aSLionel Sambuc CGBuilderTy &Builder = CGF.Builder;
2291f4a2713aSLionel Sambuc // Load the vbtable pointer from the vbptr in the instance.
2292f4a2713aSLionel Sambuc This = Builder.CreateBitCast(This, CGM.Int8PtrTy);
2293f4a2713aSLionel Sambuc llvm::Value *VBPtr =
2294f4a2713aSLionel Sambuc Builder.CreateInBoundsGEP(This, VBPtrOffset, "vbptr");
2295f4a2713aSLionel Sambuc if (VBPtrOut) *VBPtrOut = VBPtr;
2296*0a6a1f1dSLionel Sambuc VBPtr = Builder.CreateBitCast(VBPtr,
2297*0a6a1f1dSLionel Sambuc CGM.Int32Ty->getPointerTo(0)->getPointerTo(0));
2298f4a2713aSLionel Sambuc llvm::Value *VBTable = Builder.CreateLoad(VBPtr, "vbtable");
2299f4a2713aSLionel Sambuc
2300*0a6a1f1dSLionel Sambuc // Translate from byte offset to table index. It improves analyzability.
2301*0a6a1f1dSLionel Sambuc llvm::Value *VBTableIndex = Builder.CreateAShr(
2302*0a6a1f1dSLionel Sambuc VBTableOffset, llvm::ConstantInt::get(VBTableOffset->getType(), 2),
2303*0a6a1f1dSLionel Sambuc "vbtindex", /*isExact=*/true);
2304*0a6a1f1dSLionel Sambuc
2305f4a2713aSLionel Sambuc // Load an i32 offset from the vb-table.
2306*0a6a1f1dSLionel Sambuc llvm::Value *VBaseOffs = Builder.CreateInBoundsGEP(VBTable, VBTableIndex);
2307f4a2713aSLionel Sambuc VBaseOffs = Builder.CreateBitCast(VBaseOffs, CGM.Int32Ty->getPointerTo(0));
2308f4a2713aSLionel Sambuc return Builder.CreateLoad(VBaseOffs, "vbase_offs");
2309f4a2713aSLionel Sambuc }
2310f4a2713aSLionel Sambuc
2311f4a2713aSLionel Sambuc // Returns an adjusted base cast to i8*, since we do more address arithmetic on
2312f4a2713aSLionel Sambuc // it.
AdjustVirtualBase(CodeGenFunction & CGF,const Expr * E,const CXXRecordDecl * RD,llvm::Value * Base,llvm::Value * VBTableOffset,llvm::Value * VBPtrOffset)2313*0a6a1f1dSLionel Sambuc llvm::Value *MicrosoftCXXABI::AdjustVirtualBase(
2314*0a6a1f1dSLionel Sambuc CodeGenFunction &CGF, const Expr *E, const CXXRecordDecl *RD,
2315*0a6a1f1dSLionel Sambuc llvm::Value *Base, llvm::Value *VBTableOffset, llvm::Value *VBPtrOffset) {
2316f4a2713aSLionel Sambuc CGBuilderTy &Builder = CGF.Builder;
2317f4a2713aSLionel Sambuc Base = Builder.CreateBitCast(Base, CGM.Int8PtrTy);
2318*0a6a1f1dSLionel Sambuc llvm::BasicBlock *OriginalBB = nullptr;
2319*0a6a1f1dSLionel Sambuc llvm::BasicBlock *SkipAdjustBB = nullptr;
2320*0a6a1f1dSLionel Sambuc llvm::BasicBlock *VBaseAdjustBB = nullptr;
2321f4a2713aSLionel Sambuc
2322f4a2713aSLionel Sambuc // In the unspecified inheritance model, there might not be a vbtable at all,
2323f4a2713aSLionel Sambuc // in which case we need to skip the virtual base lookup. If there is a
2324f4a2713aSLionel Sambuc // vbtable, the first entry is a no-op entry that gives back the original
2325f4a2713aSLionel Sambuc // base, so look for a virtual base adjustment offset of zero.
2326f4a2713aSLionel Sambuc if (VBPtrOffset) {
2327f4a2713aSLionel Sambuc OriginalBB = Builder.GetInsertBlock();
2328f4a2713aSLionel Sambuc VBaseAdjustBB = CGF.createBasicBlock("memptr.vadjust");
2329f4a2713aSLionel Sambuc SkipAdjustBB = CGF.createBasicBlock("memptr.skip_vadjust");
2330f4a2713aSLionel Sambuc llvm::Value *IsVirtual =
2331f4a2713aSLionel Sambuc Builder.CreateICmpNE(VBTableOffset, getZeroInt(),
2332f4a2713aSLionel Sambuc "memptr.is_vbase");
2333f4a2713aSLionel Sambuc Builder.CreateCondBr(IsVirtual, VBaseAdjustBB, SkipAdjustBB);
2334f4a2713aSLionel Sambuc CGF.EmitBlock(VBaseAdjustBB);
2335f4a2713aSLionel Sambuc }
2336f4a2713aSLionel Sambuc
2337f4a2713aSLionel Sambuc // If we weren't given a dynamic vbptr offset, RD should be complete and we'll
2338f4a2713aSLionel Sambuc // know the vbptr offset.
2339f4a2713aSLionel Sambuc if (!VBPtrOffset) {
2340f4a2713aSLionel Sambuc CharUnits offs = CharUnits::Zero();
2341*0a6a1f1dSLionel Sambuc if (!RD->hasDefinition()) {
2342*0a6a1f1dSLionel Sambuc DiagnosticsEngine &Diags = CGF.CGM.getDiags();
2343*0a6a1f1dSLionel Sambuc unsigned DiagID = Diags.getCustomDiagID(
2344*0a6a1f1dSLionel Sambuc DiagnosticsEngine::Error,
2345*0a6a1f1dSLionel Sambuc "member pointer representation requires a "
2346*0a6a1f1dSLionel Sambuc "complete class type for %0 to perform this expression");
2347*0a6a1f1dSLionel Sambuc Diags.Report(E->getExprLoc(), DiagID) << RD << E->getSourceRange();
2348*0a6a1f1dSLionel Sambuc } else if (RD->getNumVBases())
2349*0a6a1f1dSLionel Sambuc offs = getContext().getASTRecordLayout(RD).getVBPtrOffset();
2350f4a2713aSLionel Sambuc VBPtrOffset = llvm::ConstantInt::get(CGM.IntTy, offs.getQuantity());
2351f4a2713aSLionel Sambuc }
2352*0a6a1f1dSLionel Sambuc llvm::Value *VBPtr = nullptr;
2353f4a2713aSLionel Sambuc llvm::Value *VBaseOffs =
2354f4a2713aSLionel Sambuc GetVBaseOffsetFromVBPtr(CGF, Base, VBPtrOffset, VBTableOffset, &VBPtr);
2355f4a2713aSLionel Sambuc llvm::Value *AdjustedBase = Builder.CreateInBoundsGEP(VBPtr, VBaseOffs);
2356f4a2713aSLionel Sambuc
2357f4a2713aSLionel Sambuc // Merge control flow with the case where we didn't have to adjust.
2358f4a2713aSLionel Sambuc if (VBaseAdjustBB) {
2359f4a2713aSLionel Sambuc Builder.CreateBr(SkipAdjustBB);
2360f4a2713aSLionel Sambuc CGF.EmitBlock(SkipAdjustBB);
2361f4a2713aSLionel Sambuc llvm::PHINode *Phi = Builder.CreatePHI(CGM.Int8PtrTy, 2, "memptr.base");
2362f4a2713aSLionel Sambuc Phi->addIncoming(Base, OriginalBB);
2363f4a2713aSLionel Sambuc Phi->addIncoming(AdjustedBase, VBaseAdjustBB);
2364f4a2713aSLionel Sambuc return Phi;
2365f4a2713aSLionel Sambuc }
2366f4a2713aSLionel Sambuc return AdjustedBase;
2367f4a2713aSLionel Sambuc }
2368f4a2713aSLionel Sambuc
EmitMemberDataPointerAddress(CodeGenFunction & CGF,const Expr * E,llvm::Value * Base,llvm::Value * MemPtr,const MemberPointerType * MPT)2369*0a6a1f1dSLionel Sambuc llvm::Value *MicrosoftCXXABI::EmitMemberDataPointerAddress(
2370*0a6a1f1dSLionel Sambuc CodeGenFunction &CGF, const Expr *E, llvm::Value *Base, llvm::Value *MemPtr,
2371f4a2713aSLionel Sambuc const MemberPointerType *MPT) {
2372f4a2713aSLionel Sambuc assert(MPT->isMemberDataPointer());
2373f4a2713aSLionel Sambuc unsigned AS = Base->getType()->getPointerAddressSpace();
2374f4a2713aSLionel Sambuc llvm::Type *PType =
2375f4a2713aSLionel Sambuc CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
2376f4a2713aSLionel Sambuc CGBuilderTy &Builder = CGF.Builder;
2377*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
2378*0a6a1f1dSLionel Sambuc MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
2379f4a2713aSLionel Sambuc
2380f4a2713aSLionel Sambuc // Extract the fields we need, regardless of model. We'll apply them if we
2381f4a2713aSLionel Sambuc // have them.
2382f4a2713aSLionel Sambuc llvm::Value *FieldOffset = MemPtr;
2383*0a6a1f1dSLionel Sambuc llvm::Value *VirtualBaseAdjustmentOffset = nullptr;
2384*0a6a1f1dSLionel Sambuc llvm::Value *VBPtrOffset = nullptr;
2385f4a2713aSLionel Sambuc if (MemPtr->getType()->isStructTy()) {
2386f4a2713aSLionel Sambuc // We need to extract values.
2387f4a2713aSLionel Sambuc unsigned I = 0;
2388f4a2713aSLionel Sambuc FieldOffset = Builder.CreateExtractValue(MemPtr, I++);
2389*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
2390f4a2713aSLionel Sambuc VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++);
2391*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
2392f4a2713aSLionel Sambuc VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++);
2393f4a2713aSLionel Sambuc }
2394f4a2713aSLionel Sambuc
2395f4a2713aSLionel Sambuc if (VirtualBaseAdjustmentOffset) {
2396*0a6a1f1dSLionel Sambuc Base = AdjustVirtualBase(CGF, E, RD, Base, VirtualBaseAdjustmentOffset,
2397f4a2713aSLionel Sambuc VBPtrOffset);
2398f4a2713aSLionel Sambuc }
2399*0a6a1f1dSLionel Sambuc
2400*0a6a1f1dSLionel Sambuc // Cast to char*.
2401*0a6a1f1dSLionel Sambuc Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
2402*0a6a1f1dSLionel Sambuc
2403*0a6a1f1dSLionel Sambuc // Apply the offset, which we assume is non-null.
2404f4a2713aSLionel Sambuc llvm::Value *Addr =
2405f4a2713aSLionel Sambuc Builder.CreateInBoundsGEP(Base, FieldOffset, "memptr.offset");
2406f4a2713aSLionel Sambuc
2407f4a2713aSLionel Sambuc // Cast the address to the appropriate pointer type, adopting the address
2408f4a2713aSLionel Sambuc // space of the base pointer.
2409f4a2713aSLionel Sambuc return Builder.CreateBitCast(Addr, PType);
2410f4a2713aSLionel Sambuc }
2411f4a2713aSLionel Sambuc
2412*0a6a1f1dSLionel Sambuc static MSInheritanceAttr::Spelling
getInheritanceFromMemptr(const MemberPointerType * MPT)2413f4a2713aSLionel Sambuc getInheritanceFromMemptr(const MemberPointerType *MPT) {
2414*0a6a1f1dSLionel Sambuc return MPT->getMostRecentCXXRecordDecl()->getMSInheritanceModel();
2415f4a2713aSLionel Sambuc }
2416f4a2713aSLionel Sambuc
2417f4a2713aSLionel Sambuc llvm::Value *
EmitMemberPointerConversion(CodeGenFunction & CGF,const CastExpr * E,llvm::Value * Src)2418f4a2713aSLionel Sambuc MicrosoftCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
2419f4a2713aSLionel Sambuc const CastExpr *E,
2420f4a2713aSLionel Sambuc llvm::Value *Src) {
2421f4a2713aSLionel Sambuc assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
2422f4a2713aSLionel Sambuc E->getCastKind() == CK_BaseToDerivedMemberPointer ||
2423f4a2713aSLionel Sambuc E->getCastKind() == CK_ReinterpretMemberPointer);
2424f4a2713aSLionel Sambuc
2425f4a2713aSLionel Sambuc // Use constant emission if we can.
2426f4a2713aSLionel Sambuc if (isa<llvm::Constant>(Src))
2427f4a2713aSLionel Sambuc return EmitMemberPointerConversion(E, cast<llvm::Constant>(Src));
2428f4a2713aSLionel Sambuc
2429f4a2713aSLionel Sambuc // We may be adding or dropping fields from the member pointer, so we need
2430f4a2713aSLionel Sambuc // both types and the inheritance models of both records.
2431f4a2713aSLionel Sambuc const MemberPointerType *SrcTy =
2432f4a2713aSLionel Sambuc E->getSubExpr()->getType()->castAs<MemberPointerType>();
2433f4a2713aSLionel Sambuc const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>();
2434f4a2713aSLionel Sambuc bool IsFunc = SrcTy->isMemberFunctionPointer();
2435f4a2713aSLionel Sambuc
2436f4a2713aSLionel Sambuc // If the classes use the same null representation, reinterpret_cast is a nop.
2437f4a2713aSLionel Sambuc bool IsReinterpret = E->getCastKind() == CK_ReinterpretMemberPointer;
2438*0a6a1f1dSLionel Sambuc if (IsReinterpret && IsFunc)
2439*0a6a1f1dSLionel Sambuc return Src;
2440*0a6a1f1dSLionel Sambuc
2441*0a6a1f1dSLionel Sambuc CXXRecordDecl *SrcRD = SrcTy->getMostRecentCXXRecordDecl();
2442*0a6a1f1dSLionel Sambuc CXXRecordDecl *DstRD = DstTy->getMostRecentCXXRecordDecl();
2443*0a6a1f1dSLionel Sambuc if (IsReinterpret &&
2444*0a6a1f1dSLionel Sambuc SrcRD->nullFieldOffsetIsZero() == DstRD->nullFieldOffsetIsZero())
2445f4a2713aSLionel Sambuc return Src;
2446f4a2713aSLionel Sambuc
2447f4a2713aSLionel Sambuc CGBuilderTy &Builder = CGF.Builder;
2448f4a2713aSLionel Sambuc
2449f4a2713aSLionel Sambuc // Branch past the conversion if Src is null.
2450f4a2713aSLionel Sambuc llvm::Value *IsNotNull = EmitMemberPointerIsNotNull(CGF, Src, SrcTy);
2451f4a2713aSLionel Sambuc llvm::Constant *DstNull = EmitNullMemberPointer(DstTy);
2452f4a2713aSLionel Sambuc
2453f4a2713aSLionel Sambuc // C++ 5.2.10p9: The null member pointer value is converted to the null member
2454f4a2713aSLionel Sambuc // pointer value of the destination type.
2455f4a2713aSLionel Sambuc if (IsReinterpret) {
2456f4a2713aSLionel Sambuc // For reinterpret casts, sema ensures that src and dst are both functions
2457f4a2713aSLionel Sambuc // or data and have the same size, which means the LLVM types should match.
2458f4a2713aSLionel Sambuc assert(Src->getType() == DstNull->getType());
2459f4a2713aSLionel Sambuc return Builder.CreateSelect(IsNotNull, Src, DstNull);
2460f4a2713aSLionel Sambuc }
2461f4a2713aSLionel Sambuc
2462f4a2713aSLionel Sambuc llvm::BasicBlock *OriginalBB = Builder.GetInsertBlock();
2463f4a2713aSLionel Sambuc llvm::BasicBlock *ConvertBB = CGF.createBasicBlock("memptr.convert");
2464f4a2713aSLionel Sambuc llvm::BasicBlock *ContinueBB = CGF.createBasicBlock("memptr.converted");
2465f4a2713aSLionel Sambuc Builder.CreateCondBr(IsNotNull, ConvertBB, ContinueBB);
2466f4a2713aSLionel Sambuc CGF.EmitBlock(ConvertBB);
2467f4a2713aSLionel Sambuc
2468f4a2713aSLionel Sambuc // Decompose src.
2469f4a2713aSLionel Sambuc llvm::Value *FirstField = Src;
2470*0a6a1f1dSLionel Sambuc llvm::Value *NonVirtualBaseAdjustment = nullptr;
2471*0a6a1f1dSLionel Sambuc llvm::Value *VirtualBaseAdjustmentOffset = nullptr;
2472*0a6a1f1dSLionel Sambuc llvm::Value *VBPtrOffset = nullptr;
2473*0a6a1f1dSLionel Sambuc MSInheritanceAttr::Spelling SrcInheritance = SrcRD->getMSInheritanceModel();
2474*0a6a1f1dSLionel Sambuc if (!MSInheritanceAttr::hasOnlyOneField(IsFunc, SrcInheritance)) {
2475f4a2713aSLionel Sambuc // We need to extract values.
2476f4a2713aSLionel Sambuc unsigned I = 0;
2477f4a2713aSLionel Sambuc FirstField = Builder.CreateExtractValue(Src, I++);
2478*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasNVOffsetField(IsFunc, SrcInheritance))
2479f4a2713aSLionel Sambuc NonVirtualBaseAdjustment = Builder.CreateExtractValue(Src, I++);
2480*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBPtrOffsetField(SrcInheritance))
2481f4a2713aSLionel Sambuc VBPtrOffset = Builder.CreateExtractValue(Src, I++);
2482*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance))
2483f4a2713aSLionel Sambuc VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(Src, I++);
2484f4a2713aSLionel Sambuc }
2485f4a2713aSLionel Sambuc
2486f4a2713aSLionel Sambuc // For data pointers, we adjust the field offset directly. For functions, we
2487f4a2713aSLionel Sambuc // have a separate field.
2488f4a2713aSLionel Sambuc llvm::Constant *Adj = getMemberPointerAdjustment(E);
2489f4a2713aSLionel Sambuc if (Adj) {
2490f4a2713aSLionel Sambuc Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy);
2491f4a2713aSLionel Sambuc llvm::Value *&NVAdjustField = IsFunc ? NonVirtualBaseAdjustment : FirstField;
2492f4a2713aSLionel Sambuc bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
2493f4a2713aSLionel Sambuc if (!NVAdjustField) // If this field didn't exist in src, it's zero.
2494f4a2713aSLionel Sambuc NVAdjustField = getZeroInt();
2495f4a2713aSLionel Sambuc if (isDerivedToBase)
2496f4a2713aSLionel Sambuc NVAdjustField = Builder.CreateNSWSub(NVAdjustField, Adj, "adj");
2497f4a2713aSLionel Sambuc else
2498f4a2713aSLionel Sambuc NVAdjustField = Builder.CreateNSWAdd(NVAdjustField, Adj, "adj");
2499f4a2713aSLionel Sambuc }
2500f4a2713aSLionel Sambuc
2501f4a2713aSLionel Sambuc // FIXME PR15713: Support conversions through virtually derived classes.
2502f4a2713aSLionel Sambuc
2503f4a2713aSLionel Sambuc // Recompose dst from the null struct and the adjusted fields from src.
2504*0a6a1f1dSLionel Sambuc MSInheritanceAttr::Spelling DstInheritance = DstRD->getMSInheritanceModel();
2505f4a2713aSLionel Sambuc llvm::Value *Dst;
2506*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasOnlyOneField(IsFunc, DstInheritance)) {
2507f4a2713aSLionel Sambuc Dst = FirstField;
2508f4a2713aSLionel Sambuc } else {
2509f4a2713aSLionel Sambuc Dst = llvm::UndefValue::get(DstNull->getType());
2510f4a2713aSLionel Sambuc unsigned Idx = 0;
2511f4a2713aSLionel Sambuc Dst = Builder.CreateInsertValue(Dst, FirstField, Idx++);
2512*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasNVOffsetField(IsFunc, DstInheritance))
2513f4a2713aSLionel Sambuc Dst = Builder.CreateInsertValue(
2514f4a2713aSLionel Sambuc Dst, getValueOrZeroInt(NonVirtualBaseAdjustment), Idx++);
2515*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance))
2516f4a2713aSLionel Sambuc Dst = Builder.CreateInsertValue(
2517f4a2713aSLionel Sambuc Dst, getValueOrZeroInt(VBPtrOffset), Idx++);
2518*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance))
2519f4a2713aSLionel Sambuc Dst = Builder.CreateInsertValue(
2520f4a2713aSLionel Sambuc Dst, getValueOrZeroInt(VirtualBaseAdjustmentOffset), Idx++);
2521f4a2713aSLionel Sambuc }
2522f4a2713aSLionel Sambuc Builder.CreateBr(ContinueBB);
2523f4a2713aSLionel Sambuc
2524f4a2713aSLionel Sambuc // In the continuation, choose between DstNull and Dst.
2525f4a2713aSLionel Sambuc CGF.EmitBlock(ContinueBB);
2526f4a2713aSLionel Sambuc llvm::PHINode *Phi = Builder.CreatePHI(DstNull->getType(), 2, "memptr.converted");
2527f4a2713aSLionel Sambuc Phi->addIncoming(DstNull, OriginalBB);
2528f4a2713aSLionel Sambuc Phi->addIncoming(Dst, ConvertBB);
2529f4a2713aSLionel Sambuc return Phi;
2530f4a2713aSLionel Sambuc }
2531f4a2713aSLionel Sambuc
2532f4a2713aSLionel Sambuc llvm::Constant *
EmitMemberPointerConversion(const CastExpr * E,llvm::Constant * Src)2533f4a2713aSLionel Sambuc MicrosoftCXXABI::EmitMemberPointerConversion(const CastExpr *E,
2534f4a2713aSLionel Sambuc llvm::Constant *Src) {
2535f4a2713aSLionel Sambuc const MemberPointerType *SrcTy =
2536f4a2713aSLionel Sambuc E->getSubExpr()->getType()->castAs<MemberPointerType>();
2537f4a2713aSLionel Sambuc const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>();
2538f4a2713aSLionel Sambuc
2539f4a2713aSLionel Sambuc // If src is null, emit a new null for dst. We can't return src because dst
2540f4a2713aSLionel Sambuc // might have a new representation.
2541f4a2713aSLionel Sambuc if (MemberPointerConstantIsNull(SrcTy, Src))
2542f4a2713aSLionel Sambuc return EmitNullMemberPointer(DstTy);
2543f4a2713aSLionel Sambuc
2544f4a2713aSLionel Sambuc // We don't need to do anything for reinterpret_casts of non-null member
2545f4a2713aSLionel Sambuc // pointers. We should only get here when the two type representations have
2546f4a2713aSLionel Sambuc // the same size.
2547f4a2713aSLionel Sambuc if (E->getCastKind() == CK_ReinterpretMemberPointer)
2548f4a2713aSLionel Sambuc return Src;
2549f4a2713aSLionel Sambuc
2550*0a6a1f1dSLionel Sambuc MSInheritanceAttr::Spelling SrcInheritance = getInheritanceFromMemptr(SrcTy);
2551*0a6a1f1dSLionel Sambuc MSInheritanceAttr::Spelling DstInheritance = getInheritanceFromMemptr(DstTy);
2552f4a2713aSLionel Sambuc
2553f4a2713aSLionel Sambuc // Decompose src.
2554f4a2713aSLionel Sambuc llvm::Constant *FirstField = Src;
2555*0a6a1f1dSLionel Sambuc llvm::Constant *NonVirtualBaseAdjustment = nullptr;
2556*0a6a1f1dSLionel Sambuc llvm::Constant *VirtualBaseAdjustmentOffset = nullptr;
2557*0a6a1f1dSLionel Sambuc llvm::Constant *VBPtrOffset = nullptr;
2558f4a2713aSLionel Sambuc bool IsFunc = SrcTy->isMemberFunctionPointer();
2559*0a6a1f1dSLionel Sambuc if (!MSInheritanceAttr::hasOnlyOneField(IsFunc, SrcInheritance)) {
2560f4a2713aSLionel Sambuc // We need to extract values.
2561f4a2713aSLionel Sambuc unsigned I = 0;
2562f4a2713aSLionel Sambuc FirstField = Src->getAggregateElement(I++);
2563*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasNVOffsetField(IsFunc, SrcInheritance))
2564f4a2713aSLionel Sambuc NonVirtualBaseAdjustment = Src->getAggregateElement(I++);
2565*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBPtrOffsetField(SrcInheritance))
2566f4a2713aSLionel Sambuc VBPtrOffset = Src->getAggregateElement(I++);
2567*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance))
2568f4a2713aSLionel Sambuc VirtualBaseAdjustmentOffset = Src->getAggregateElement(I++);
2569f4a2713aSLionel Sambuc }
2570f4a2713aSLionel Sambuc
2571f4a2713aSLionel Sambuc // For data pointers, we adjust the field offset directly. For functions, we
2572f4a2713aSLionel Sambuc // have a separate field.
2573f4a2713aSLionel Sambuc llvm::Constant *Adj = getMemberPointerAdjustment(E);
2574f4a2713aSLionel Sambuc if (Adj) {
2575f4a2713aSLionel Sambuc Adj = llvm::ConstantExpr::getTruncOrBitCast(Adj, CGM.IntTy);
2576f4a2713aSLionel Sambuc llvm::Constant *&NVAdjustField =
2577f4a2713aSLionel Sambuc IsFunc ? NonVirtualBaseAdjustment : FirstField;
2578f4a2713aSLionel Sambuc bool IsDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
2579f4a2713aSLionel Sambuc if (!NVAdjustField) // If this field didn't exist in src, it's zero.
2580f4a2713aSLionel Sambuc NVAdjustField = getZeroInt();
2581f4a2713aSLionel Sambuc if (IsDerivedToBase)
2582f4a2713aSLionel Sambuc NVAdjustField = llvm::ConstantExpr::getNSWSub(NVAdjustField, Adj);
2583f4a2713aSLionel Sambuc else
2584f4a2713aSLionel Sambuc NVAdjustField = llvm::ConstantExpr::getNSWAdd(NVAdjustField, Adj);
2585f4a2713aSLionel Sambuc }
2586f4a2713aSLionel Sambuc
2587f4a2713aSLionel Sambuc // FIXME PR15713: Support conversions through virtually derived classes.
2588f4a2713aSLionel Sambuc
2589f4a2713aSLionel Sambuc // Recompose dst from the null struct and the adjusted fields from src.
2590*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasOnlyOneField(IsFunc, DstInheritance))
2591f4a2713aSLionel Sambuc return FirstField;
2592f4a2713aSLionel Sambuc
2593f4a2713aSLionel Sambuc llvm::SmallVector<llvm::Constant *, 4> Fields;
2594f4a2713aSLionel Sambuc Fields.push_back(FirstField);
2595*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasNVOffsetField(IsFunc, DstInheritance))
2596f4a2713aSLionel Sambuc Fields.push_back(getConstantOrZeroInt(NonVirtualBaseAdjustment));
2597*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance))
2598f4a2713aSLionel Sambuc Fields.push_back(getConstantOrZeroInt(VBPtrOffset));
2599*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance))
2600f4a2713aSLionel Sambuc Fields.push_back(getConstantOrZeroInt(VirtualBaseAdjustmentOffset));
2601f4a2713aSLionel Sambuc return llvm::ConstantStruct::getAnon(Fields);
2602f4a2713aSLionel Sambuc }
2603f4a2713aSLionel Sambuc
EmitLoadOfMemberFunctionPointer(CodeGenFunction & CGF,const Expr * E,llvm::Value * & This,llvm::Value * MemPtr,const MemberPointerType * MPT)2604*0a6a1f1dSLionel Sambuc llvm::Value *MicrosoftCXXABI::EmitLoadOfMemberFunctionPointer(
2605*0a6a1f1dSLionel Sambuc CodeGenFunction &CGF, const Expr *E, llvm::Value *&This,
2606*0a6a1f1dSLionel Sambuc llvm::Value *MemPtr, const MemberPointerType *MPT) {
2607f4a2713aSLionel Sambuc assert(MPT->isMemberFunctionPointer());
2608f4a2713aSLionel Sambuc const FunctionProtoType *FPT =
2609f4a2713aSLionel Sambuc MPT->getPointeeType()->castAs<FunctionProtoType>();
2610*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
2611f4a2713aSLionel Sambuc llvm::FunctionType *FTy =
2612f4a2713aSLionel Sambuc CGM.getTypes().GetFunctionType(
2613f4a2713aSLionel Sambuc CGM.getTypes().arrangeCXXMethodType(RD, FPT));
2614f4a2713aSLionel Sambuc CGBuilderTy &Builder = CGF.Builder;
2615f4a2713aSLionel Sambuc
2616*0a6a1f1dSLionel Sambuc MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel();
2617f4a2713aSLionel Sambuc
2618f4a2713aSLionel Sambuc // Extract the fields we need, regardless of model. We'll apply them if we
2619f4a2713aSLionel Sambuc // have them.
2620f4a2713aSLionel Sambuc llvm::Value *FunctionPointer = MemPtr;
2621*0a6a1f1dSLionel Sambuc llvm::Value *NonVirtualBaseAdjustment = nullptr;
2622*0a6a1f1dSLionel Sambuc llvm::Value *VirtualBaseAdjustmentOffset = nullptr;
2623*0a6a1f1dSLionel Sambuc llvm::Value *VBPtrOffset = nullptr;
2624f4a2713aSLionel Sambuc if (MemPtr->getType()->isStructTy()) {
2625f4a2713aSLionel Sambuc // We need to extract values.
2626f4a2713aSLionel Sambuc unsigned I = 0;
2627f4a2713aSLionel Sambuc FunctionPointer = Builder.CreateExtractValue(MemPtr, I++);
2628*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasNVOffsetField(MPT, Inheritance))
2629f4a2713aSLionel Sambuc NonVirtualBaseAdjustment = Builder.CreateExtractValue(MemPtr, I++);
2630*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance))
2631f4a2713aSLionel Sambuc VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++);
2632*0a6a1f1dSLionel Sambuc if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance))
2633f4a2713aSLionel Sambuc VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++);
2634f4a2713aSLionel Sambuc }
2635f4a2713aSLionel Sambuc
2636f4a2713aSLionel Sambuc if (VirtualBaseAdjustmentOffset) {
2637*0a6a1f1dSLionel Sambuc This = AdjustVirtualBase(CGF, E, RD, This, VirtualBaseAdjustmentOffset,
2638f4a2713aSLionel Sambuc VBPtrOffset);
2639f4a2713aSLionel Sambuc }
2640f4a2713aSLionel Sambuc
2641f4a2713aSLionel Sambuc if (NonVirtualBaseAdjustment) {
2642f4a2713aSLionel Sambuc // Apply the adjustment and cast back to the original struct type.
2643f4a2713aSLionel Sambuc llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
2644f4a2713aSLionel Sambuc Ptr = Builder.CreateInBoundsGEP(Ptr, NonVirtualBaseAdjustment);
2645f4a2713aSLionel Sambuc This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
2646f4a2713aSLionel Sambuc }
2647f4a2713aSLionel Sambuc
2648f4a2713aSLionel Sambuc return Builder.CreateBitCast(FunctionPointer, FTy->getPointerTo());
2649f4a2713aSLionel Sambuc }
2650f4a2713aSLionel Sambuc
CreateMicrosoftCXXABI(CodeGenModule & CGM)2651f4a2713aSLionel Sambuc CGCXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) {
2652f4a2713aSLionel Sambuc return new MicrosoftCXXABI(CGM);
2653f4a2713aSLionel Sambuc }
2654*0a6a1f1dSLionel Sambuc
2655*0a6a1f1dSLionel Sambuc // MS RTTI Overview:
2656*0a6a1f1dSLionel Sambuc // The run time type information emitted by cl.exe contains 5 distinct types of
2657*0a6a1f1dSLionel Sambuc // structures. Many of them reference each other.
2658*0a6a1f1dSLionel Sambuc //
2659*0a6a1f1dSLionel Sambuc // TypeInfo: Static classes that are returned by typeid.
2660*0a6a1f1dSLionel Sambuc //
2661*0a6a1f1dSLionel Sambuc // CompleteObjectLocator: Referenced by vftables. They contain information
2662*0a6a1f1dSLionel Sambuc // required for dynamic casting, including OffsetFromTop. They also contain
2663*0a6a1f1dSLionel Sambuc // a reference to the TypeInfo for the type and a reference to the
2664*0a6a1f1dSLionel Sambuc // CompleteHierarchyDescriptor for the type.
2665*0a6a1f1dSLionel Sambuc //
2666*0a6a1f1dSLionel Sambuc // ClassHieararchyDescriptor: Contains information about a class hierarchy.
2667*0a6a1f1dSLionel Sambuc // Used during dynamic_cast to walk a class hierarchy. References a base
2668*0a6a1f1dSLionel Sambuc // class array and the size of said array.
2669*0a6a1f1dSLionel Sambuc //
2670*0a6a1f1dSLionel Sambuc // BaseClassArray: Contains a list of classes in a hierarchy. BaseClassArray is
2671*0a6a1f1dSLionel Sambuc // somewhat of a misnomer because the most derived class is also in the list
2672*0a6a1f1dSLionel Sambuc // as well as multiple copies of virtual bases (if they occur multiple times
2673*0a6a1f1dSLionel Sambuc // in the hiearchy.) The BaseClassArray contains one BaseClassDescriptor for
2674*0a6a1f1dSLionel Sambuc // every path in the hierarchy, in pre-order depth first order. Note, we do
2675*0a6a1f1dSLionel Sambuc // not declare a specific llvm type for BaseClassArray, it's merely an array
2676*0a6a1f1dSLionel Sambuc // of BaseClassDescriptor pointers.
2677*0a6a1f1dSLionel Sambuc //
2678*0a6a1f1dSLionel Sambuc // BaseClassDescriptor: Contains information about a class in a class hierarchy.
2679*0a6a1f1dSLionel Sambuc // BaseClassDescriptor is also somewhat of a misnomer for the same reason that
2680*0a6a1f1dSLionel Sambuc // BaseClassArray is. It contains information about a class within a
2681*0a6a1f1dSLionel Sambuc // hierarchy such as: is this base is ambiguous and what is its offset in the
2682*0a6a1f1dSLionel Sambuc // vbtable. The names of the BaseClassDescriptors have all of their fields
2683*0a6a1f1dSLionel Sambuc // mangled into them so they can be aggressively deduplicated by the linker.
2684*0a6a1f1dSLionel Sambuc
getTypeInfoVTable(CodeGenModule & CGM)2685*0a6a1f1dSLionel Sambuc static llvm::GlobalVariable *getTypeInfoVTable(CodeGenModule &CGM) {
2686*0a6a1f1dSLionel Sambuc StringRef MangledName("\01??_7type_info@@6B@");
2687*0a6a1f1dSLionel Sambuc if (auto VTable = CGM.getModule().getNamedGlobal(MangledName))
2688*0a6a1f1dSLionel Sambuc return VTable;
2689*0a6a1f1dSLionel Sambuc return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
2690*0a6a1f1dSLionel Sambuc /*Constant=*/true,
2691*0a6a1f1dSLionel Sambuc llvm::GlobalVariable::ExternalLinkage,
2692*0a6a1f1dSLionel Sambuc /*Initializer=*/nullptr, MangledName);
2693*0a6a1f1dSLionel Sambuc }
2694*0a6a1f1dSLionel Sambuc
2695*0a6a1f1dSLionel Sambuc namespace {
2696*0a6a1f1dSLionel Sambuc
2697*0a6a1f1dSLionel Sambuc /// \brief A Helper struct that stores information about a class in a class
2698*0a6a1f1dSLionel Sambuc /// hierarchy. The information stored in these structs struct is used during
2699*0a6a1f1dSLionel Sambuc /// the generation of ClassHierarchyDescriptors and BaseClassDescriptors.
2700*0a6a1f1dSLionel Sambuc // During RTTI creation, MSRTTIClasses are stored in a contiguous array with
2701*0a6a1f1dSLionel Sambuc // implicit depth first pre-order tree connectivity. getFirstChild and
2702*0a6a1f1dSLionel Sambuc // getNextSibling allow us to walk the tree efficiently.
2703*0a6a1f1dSLionel Sambuc struct MSRTTIClass {
2704*0a6a1f1dSLionel Sambuc enum {
2705*0a6a1f1dSLionel Sambuc IsPrivateOnPath = 1 | 8,
2706*0a6a1f1dSLionel Sambuc IsAmbiguous = 2,
2707*0a6a1f1dSLionel Sambuc IsPrivate = 4,
2708*0a6a1f1dSLionel Sambuc IsVirtual = 16,
2709*0a6a1f1dSLionel Sambuc HasHierarchyDescriptor = 64
2710*0a6a1f1dSLionel Sambuc };
MSRTTIClass__anona9a21ec60311::MSRTTIClass2711*0a6a1f1dSLionel Sambuc MSRTTIClass(const CXXRecordDecl *RD) : RD(RD) {}
2712*0a6a1f1dSLionel Sambuc uint32_t initialize(const MSRTTIClass *Parent,
2713*0a6a1f1dSLionel Sambuc const CXXBaseSpecifier *Specifier);
2714*0a6a1f1dSLionel Sambuc
getFirstChild__anona9a21ec60311::MSRTTIClass2715*0a6a1f1dSLionel Sambuc MSRTTIClass *getFirstChild() { return this + 1; }
getNextChild__anona9a21ec60311::MSRTTIClass2716*0a6a1f1dSLionel Sambuc static MSRTTIClass *getNextChild(MSRTTIClass *Child) {
2717*0a6a1f1dSLionel Sambuc return Child + 1 + Child->NumBases;
2718*0a6a1f1dSLionel Sambuc }
2719*0a6a1f1dSLionel Sambuc
2720*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD, *VirtualRoot;
2721*0a6a1f1dSLionel Sambuc uint32_t Flags, NumBases, OffsetInVBase;
2722*0a6a1f1dSLionel Sambuc };
2723*0a6a1f1dSLionel Sambuc
2724*0a6a1f1dSLionel Sambuc /// \brief Recursively initialize the base class array.
initialize(const MSRTTIClass * Parent,const CXXBaseSpecifier * Specifier)2725*0a6a1f1dSLionel Sambuc uint32_t MSRTTIClass::initialize(const MSRTTIClass *Parent,
2726*0a6a1f1dSLionel Sambuc const CXXBaseSpecifier *Specifier) {
2727*0a6a1f1dSLionel Sambuc Flags = HasHierarchyDescriptor;
2728*0a6a1f1dSLionel Sambuc if (!Parent) {
2729*0a6a1f1dSLionel Sambuc VirtualRoot = nullptr;
2730*0a6a1f1dSLionel Sambuc OffsetInVBase = 0;
2731*0a6a1f1dSLionel Sambuc } else {
2732*0a6a1f1dSLionel Sambuc if (Specifier->getAccessSpecifier() != AS_public)
2733*0a6a1f1dSLionel Sambuc Flags |= IsPrivate | IsPrivateOnPath;
2734*0a6a1f1dSLionel Sambuc if (Specifier->isVirtual()) {
2735*0a6a1f1dSLionel Sambuc Flags |= IsVirtual;
2736*0a6a1f1dSLionel Sambuc VirtualRoot = RD;
2737*0a6a1f1dSLionel Sambuc OffsetInVBase = 0;
2738*0a6a1f1dSLionel Sambuc } else {
2739*0a6a1f1dSLionel Sambuc if (Parent->Flags & IsPrivateOnPath)
2740*0a6a1f1dSLionel Sambuc Flags |= IsPrivateOnPath;
2741*0a6a1f1dSLionel Sambuc VirtualRoot = Parent->VirtualRoot;
2742*0a6a1f1dSLionel Sambuc OffsetInVBase = Parent->OffsetInVBase + RD->getASTContext()
2743*0a6a1f1dSLionel Sambuc .getASTRecordLayout(Parent->RD).getBaseClassOffset(RD).getQuantity();
2744*0a6a1f1dSLionel Sambuc }
2745*0a6a1f1dSLionel Sambuc }
2746*0a6a1f1dSLionel Sambuc NumBases = 0;
2747*0a6a1f1dSLionel Sambuc MSRTTIClass *Child = getFirstChild();
2748*0a6a1f1dSLionel Sambuc for (const CXXBaseSpecifier &Base : RD->bases()) {
2749*0a6a1f1dSLionel Sambuc NumBases += Child->initialize(this, &Base) + 1;
2750*0a6a1f1dSLionel Sambuc Child = getNextChild(Child);
2751*0a6a1f1dSLionel Sambuc }
2752*0a6a1f1dSLionel Sambuc return NumBases;
2753*0a6a1f1dSLionel Sambuc }
2754*0a6a1f1dSLionel Sambuc
getLinkageForRTTI(QualType Ty)2755*0a6a1f1dSLionel Sambuc static llvm::GlobalValue::LinkageTypes getLinkageForRTTI(QualType Ty) {
2756*0a6a1f1dSLionel Sambuc switch (Ty->getLinkage()) {
2757*0a6a1f1dSLionel Sambuc case NoLinkage:
2758*0a6a1f1dSLionel Sambuc case InternalLinkage:
2759*0a6a1f1dSLionel Sambuc case UniqueExternalLinkage:
2760*0a6a1f1dSLionel Sambuc return llvm::GlobalValue::InternalLinkage;
2761*0a6a1f1dSLionel Sambuc
2762*0a6a1f1dSLionel Sambuc case VisibleNoLinkage:
2763*0a6a1f1dSLionel Sambuc case ExternalLinkage:
2764*0a6a1f1dSLionel Sambuc return llvm::GlobalValue::LinkOnceODRLinkage;
2765*0a6a1f1dSLionel Sambuc }
2766*0a6a1f1dSLionel Sambuc llvm_unreachable("Invalid linkage!");
2767*0a6a1f1dSLionel Sambuc }
2768*0a6a1f1dSLionel Sambuc
2769*0a6a1f1dSLionel Sambuc /// \brief An ephemeral helper class for building MS RTTI types. It caches some
2770*0a6a1f1dSLionel Sambuc /// calls to the module and information about the most derived class in a
2771*0a6a1f1dSLionel Sambuc /// hierarchy.
2772*0a6a1f1dSLionel Sambuc struct MSRTTIBuilder {
2773*0a6a1f1dSLionel Sambuc enum {
2774*0a6a1f1dSLionel Sambuc HasBranchingHierarchy = 1,
2775*0a6a1f1dSLionel Sambuc HasVirtualBranchingHierarchy = 2,
2776*0a6a1f1dSLionel Sambuc HasAmbiguousBases = 4
2777*0a6a1f1dSLionel Sambuc };
2778*0a6a1f1dSLionel Sambuc
MSRTTIBuilder__anona9a21ec60311::MSRTTIBuilder2779*0a6a1f1dSLionel Sambuc MSRTTIBuilder(MicrosoftCXXABI &ABI, const CXXRecordDecl *RD)
2780*0a6a1f1dSLionel Sambuc : CGM(ABI.CGM), Context(CGM.getContext()),
2781*0a6a1f1dSLionel Sambuc VMContext(CGM.getLLVMContext()), Module(CGM.getModule()), RD(RD),
2782*0a6a1f1dSLionel Sambuc Linkage(getLinkageForRTTI(CGM.getContext().getTagDeclType(RD))),
2783*0a6a1f1dSLionel Sambuc ABI(ABI) {}
2784*0a6a1f1dSLionel Sambuc
2785*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *getBaseClassDescriptor(const MSRTTIClass &Classes);
2786*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *
2787*0a6a1f1dSLionel Sambuc getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes);
2788*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *getClassHierarchyDescriptor();
2789*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *getCompleteObjectLocator(const VPtrInfo *Info);
2790*0a6a1f1dSLionel Sambuc
2791*0a6a1f1dSLionel Sambuc CodeGenModule &CGM;
2792*0a6a1f1dSLionel Sambuc ASTContext &Context;
2793*0a6a1f1dSLionel Sambuc llvm::LLVMContext &VMContext;
2794*0a6a1f1dSLionel Sambuc llvm::Module &Module;
2795*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD;
2796*0a6a1f1dSLionel Sambuc llvm::GlobalVariable::LinkageTypes Linkage;
2797*0a6a1f1dSLionel Sambuc MicrosoftCXXABI &ABI;
2798*0a6a1f1dSLionel Sambuc };
2799*0a6a1f1dSLionel Sambuc
2800*0a6a1f1dSLionel Sambuc } // namespace
2801*0a6a1f1dSLionel Sambuc
2802*0a6a1f1dSLionel Sambuc /// \brief Recursively serializes a class hierarchy in pre-order depth first
2803*0a6a1f1dSLionel Sambuc /// order.
serializeClassHierarchy(SmallVectorImpl<MSRTTIClass> & Classes,const CXXRecordDecl * RD)2804*0a6a1f1dSLionel Sambuc static void serializeClassHierarchy(SmallVectorImpl<MSRTTIClass> &Classes,
2805*0a6a1f1dSLionel Sambuc const CXXRecordDecl *RD) {
2806*0a6a1f1dSLionel Sambuc Classes.push_back(MSRTTIClass(RD));
2807*0a6a1f1dSLionel Sambuc for (const CXXBaseSpecifier &Base : RD->bases())
2808*0a6a1f1dSLionel Sambuc serializeClassHierarchy(Classes, Base.getType()->getAsCXXRecordDecl());
2809*0a6a1f1dSLionel Sambuc }
2810*0a6a1f1dSLionel Sambuc
2811*0a6a1f1dSLionel Sambuc /// \brief Find ambiguity among base classes.
2812*0a6a1f1dSLionel Sambuc static void
detectAmbiguousBases(SmallVectorImpl<MSRTTIClass> & Classes)2813*0a6a1f1dSLionel Sambuc detectAmbiguousBases(SmallVectorImpl<MSRTTIClass> &Classes) {
2814*0a6a1f1dSLionel Sambuc llvm::SmallPtrSet<const CXXRecordDecl *, 8> VirtualBases;
2815*0a6a1f1dSLionel Sambuc llvm::SmallPtrSet<const CXXRecordDecl *, 8> UniqueBases;
2816*0a6a1f1dSLionel Sambuc llvm::SmallPtrSet<const CXXRecordDecl *, 8> AmbiguousBases;
2817*0a6a1f1dSLionel Sambuc for (MSRTTIClass *Class = &Classes.front(); Class <= &Classes.back();) {
2818*0a6a1f1dSLionel Sambuc if ((Class->Flags & MSRTTIClass::IsVirtual) &&
2819*0a6a1f1dSLionel Sambuc !VirtualBases.insert(Class->RD).second) {
2820*0a6a1f1dSLionel Sambuc Class = MSRTTIClass::getNextChild(Class);
2821*0a6a1f1dSLionel Sambuc continue;
2822*0a6a1f1dSLionel Sambuc }
2823*0a6a1f1dSLionel Sambuc if (!UniqueBases.insert(Class->RD).second)
2824*0a6a1f1dSLionel Sambuc AmbiguousBases.insert(Class->RD);
2825*0a6a1f1dSLionel Sambuc Class++;
2826*0a6a1f1dSLionel Sambuc }
2827*0a6a1f1dSLionel Sambuc if (AmbiguousBases.empty())
2828*0a6a1f1dSLionel Sambuc return;
2829*0a6a1f1dSLionel Sambuc for (MSRTTIClass &Class : Classes)
2830*0a6a1f1dSLionel Sambuc if (AmbiguousBases.count(Class.RD))
2831*0a6a1f1dSLionel Sambuc Class.Flags |= MSRTTIClass::IsAmbiguous;
2832*0a6a1f1dSLionel Sambuc }
2833*0a6a1f1dSLionel Sambuc
getClassHierarchyDescriptor()2834*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *MSRTTIBuilder::getClassHierarchyDescriptor() {
2835*0a6a1f1dSLionel Sambuc SmallString<256> MangledName;
2836*0a6a1f1dSLionel Sambuc {
2837*0a6a1f1dSLionel Sambuc llvm::raw_svector_ostream Out(MangledName);
2838*0a6a1f1dSLionel Sambuc ABI.getMangleContext().mangleCXXRTTIClassHierarchyDescriptor(RD, Out);
2839*0a6a1f1dSLionel Sambuc }
2840*0a6a1f1dSLionel Sambuc
2841*0a6a1f1dSLionel Sambuc // Check to see if we've already declared this ClassHierarchyDescriptor.
2842*0a6a1f1dSLionel Sambuc if (auto CHD = Module.getNamedGlobal(MangledName))
2843*0a6a1f1dSLionel Sambuc return CHD;
2844*0a6a1f1dSLionel Sambuc
2845*0a6a1f1dSLionel Sambuc // Serialize the class hierarchy and initialize the CHD Fields.
2846*0a6a1f1dSLionel Sambuc SmallVector<MSRTTIClass, 8> Classes;
2847*0a6a1f1dSLionel Sambuc serializeClassHierarchy(Classes, RD);
2848*0a6a1f1dSLionel Sambuc Classes.front().initialize(/*Parent=*/nullptr, /*Specifier=*/nullptr);
2849*0a6a1f1dSLionel Sambuc detectAmbiguousBases(Classes);
2850*0a6a1f1dSLionel Sambuc int Flags = 0;
2851*0a6a1f1dSLionel Sambuc for (auto Class : Classes) {
2852*0a6a1f1dSLionel Sambuc if (Class.RD->getNumBases() > 1)
2853*0a6a1f1dSLionel Sambuc Flags |= HasBranchingHierarchy;
2854*0a6a1f1dSLionel Sambuc // Note: cl.exe does not calculate "HasAmbiguousBases" correctly. We
2855*0a6a1f1dSLionel Sambuc // believe the field isn't actually used.
2856*0a6a1f1dSLionel Sambuc if (Class.Flags & MSRTTIClass::IsAmbiguous)
2857*0a6a1f1dSLionel Sambuc Flags |= HasAmbiguousBases;
2858*0a6a1f1dSLionel Sambuc }
2859*0a6a1f1dSLionel Sambuc if ((Flags & HasBranchingHierarchy) && RD->getNumVBases() != 0)
2860*0a6a1f1dSLionel Sambuc Flags |= HasVirtualBranchingHierarchy;
2861*0a6a1f1dSLionel Sambuc // These gep indices are used to get the address of the first element of the
2862*0a6a1f1dSLionel Sambuc // base class array.
2863*0a6a1f1dSLionel Sambuc llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0),
2864*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(CGM.IntTy, 0)};
2865*0a6a1f1dSLionel Sambuc
2866*0a6a1f1dSLionel Sambuc // Forward-declare the class hierarchy descriptor
2867*0a6a1f1dSLionel Sambuc auto Type = ABI.getClassHierarchyDescriptorType();
2868*0a6a1f1dSLionel Sambuc auto CHD = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage,
2869*0a6a1f1dSLionel Sambuc /*Initializer=*/nullptr,
2870*0a6a1f1dSLionel Sambuc MangledName.c_str());
2871*0a6a1f1dSLionel Sambuc
2872*0a6a1f1dSLionel Sambuc // Initialize the base class ClassHierarchyDescriptor.
2873*0a6a1f1dSLionel Sambuc llvm::Constant *Fields[] = {
2874*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(CGM.IntTy, 0), // Unknown
2875*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(CGM.IntTy, Flags),
2876*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(CGM.IntTy, Classes.size()),
2877*0a6a1f1dSLionel Sambuc ABI.getImageRelativeConstant(llvm::ConstantExpr::getInBoundsGetElementPtr(
2878*0a6a1f1dSLionel Sambuc getBaseClassArray(Classes),
2879*0a6a1f1dSLionel Sambuc llvm::ArrayRef<llvm::Value *>(GEPIndices))),
2880*0a6a1f1dSLionel Sambuc };
2881*0a6a1f1dSLionel Sambuc CHD->setInitializer(llvm::ConstantStruct::get(Type, Fields));
2882*0a6a1f1dSLionel Sambuc return CHD;
2883*0a6a1f1dSLionel Sambuc }
2884*0a6a1f1dSLionel Sambuc
2885*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *
getBaseClassArray(SmallVectorImpl<MSRTTIClass> & Classes)2886*0a6a1f1dSLionel Sambuc MSRTTIBuilder::getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes) {
2887*0a6a1f1dSLionel Sambuc SmallString<256> MangledName;
2888*0a6a1f1dSLionel Sambuc {
2889*0a6a1f1dSLionel Sambuc llvm::raw_svector_ostream Out(MangledName);
2890*0a6a1f1dSLionel Sambuc ABI.getMangleContext().mangleCXXRTTIBaseClassArray(RD, Out);
2891*0a6a1f1dSLionel Sambuc }
2892*0a6a1f1dSLionel Sambuc
2893*0a6a1f1dSLionel Sambuc // Forward-declare the base class array.
2894*0a6a1f1dSLionel Sambuc // cl.exe pads the base class array with 1 (in 32 bit mode) or 4 (in 64 bit
2895*0a6a1f1dSLionel Sambuc // mode) bytes of padding. We provide a pointer sized amount of padding by
2896*0a6a1f1dSLionel Sambuc // adding +1 to Classes.size(). The sections have pointer alignment and are
2897*0a6a1f1dSLionel Sambuc // marked pick-any so it shouldn't matter.
2898*0a6a1f1dSLionel Sambuc llvm::Type *PtrType = ABI.getImageRelativeType(
2899*0a6a1f1dSLionel Sambuc ABI.getBaseClassDescriptorType()->getPointerTo());
2900*0a6a1f1dSLionel Sambuc auto *ArrType = llvm::ArrayType::get(PtrType, Classes.size() + 1);
2901*0a6a1f1dSLionel Sambuc auto *BCA = new llvm::GlobalVariable(
2902*0a6a1f1dSLionel Sambuc Module, ArrType,
2903*0a6a1f1dSLionel Sambuc /*Constant=*/true, Linkage, /*Initializer=*/nullptr, MangledName.c_str());
2904*0a6a1f1dSLionel Sambuc
2905*0a6a1f1dSLionel Sambuc // Initialize the BaseClassArray.
2906*0a6a1f1dSLionel Sambuc SmallVector<llvm::Constant *, 8> BaseClassArrayData;
2907*0a6a1f1dSLionel Sambuc for (MSRTTIClass &Class : Classes)
2908*0a6a1f1dSLionel Sambuc BaseClassArrayData.push_back(
2909*0a6a1f1dSLionel Sambuc ABI.getImageRelativeConstant(getBaseClassDescriptor(Class)));
2910*0a6a1f1dSLionel Sambuc BaseClassArrayData.push_back(llvm::Constant::getNullValue(PtrType));
2911*0a6a1f1dSLionel Sambuc BCA->setInitializer(llvm::ConstantArray::get(ArrType, BaseClassArrayData));
2912*0a6a1f1dSLionel Sambuc return BCA;
2913*0a6a1f1dSLionel Sambuc }
2914*0a6a1f1dSLionel Sambuc
2915*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *
getBaseClassDescriptor(const MSRTTIClass & Class)2916*0a6a1f1dSLionel Sambuc MSRTTIBuilder::getBaseClassDescriptor(const MSRTTIClass &Class) {
2917*0a6a1f1dSLionel Sambuc // Compute the fields for the BaseClassDescriptor. They are computed up front
2918*0a6a1f1dSLionel Sambuc // because they are mangled into the name of the object.
2919*0a6a1f1dSLionel Sambuc uint32_t OffsetInVBTable = 0;
2920*0a6a1f1dSLionel Sambuc int32_t VBPtrOffset = -1;
2921*0a6a1f1dSLionel Sambuc if (Class.VirtualRoot) {
2922*0a6a1f1dSLionel Sambuc auto &VTableContext = CGM.getMicrosoftVTableContext();
2923*0a6a1f1dSLionel Sambuc OffsetInVBTable = VTableContext.getVBTableIndex(RD, Class.VirtualRoot) * 4;
2924*0a6a1f1dSLionel Sambuc VBPtrOffset = Context.getASTRecordLayout(RD).getVBPtrOffset().getQuantity();
2925*0a6a1f1dSLionel Sambuc }
2926*0a6a1f1dSLionel Sambuc
2927*0a6a1f1dSLionel Sambuc SmallString<256> MangledName;
2928*0a6a1f1dSLionel Sambuc {
2929*0a6a1f1dSLionel Sambuc llvm::raw_svector_ostream Out(MangledName);
2930*0a6a1f1dSLionel Sambuc ABI.getMangleContext().mangleCXXRTTIBaseClassDescriptor(
2931*0a6a1f1dSLionel Sambuc Class.RD, Class.OffsetInVBase, VBPtrOffset, OffsetInVBTable,
2932*0a6a1f1dSLionel Sambuc Class.Flags, Out);
2933*0a6a1f1dSLionel Sambuc }
2934*0a6a1f1dSLionel Sambuc
2935*0a6a1f1dSLionel Sambuc // Check to see if we've already declared this object.
2936*0a6a1f1dSLionel Sambuc if (auto BCD = Module.getNamedGlobal(MangledName))
2937*0a6a1f1dSLionel Sambuc return BCD;
2938*0a6a1f1dSLionel Sambuc
2939*0a6a1f1dSLionel Sambuc // Forward-declare the base class descriptor.
2940*0a6a1f1dSLionel Sambuc auto Type = ABI.getBaseClassDescriptorType();
2941*0a6a1f1dSLionel Sambuc auto BCD = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage,
2942*0a6a1f1dSLionel Sambuc /*Initializer=*/nullptr,
2943*0a6a1f1dSLionel Sambuc MangledName.c_str());
2944*0a6a1f1dSLionel Sambuc
2945*0a6a1f1dSLionel Sambuc // Initialize the BaseClassDescriptor.
2946*0a6a1f1dSLionel Sambuc llvm::Constant *Fields[] = {
2947*0a6a1f1dSLionel Sambuc ABI.getImageRelativeConstant(
2948*0a6a1f1dSLionel Sambuc ABI.getAddrOfRTTIDescriptor(Context.getTypeDeclType(Class.RD))),
2949*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(CGM.IntTy, Class.NumBases),
2950*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(CGM.IntTy, Class.OffsetInVBase),
2951*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset),
2952*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(CGM.IntTy, OffsetInVBTable),
2953*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(CGM.IntTy, Class.Flags),
2954*0a6a1f1dSLionel Sambuc ABI.getImageRelativeConstant(
2955*0a6a1f1dSLionel Sambuc MSRTTIBuilder(ABI, Class.RD).getClassHierarchyDescriptor()),
2956*0a6a1f1dSLionel Sambuc };
2957*0a6a1f1dSLionel Sambuc BCD->setInitializer(llvm::ConstantStruct::get(Type, Fields));
2958*0a6a1f1dSLionel Sambuc return BCD;
2959*0a6a1f1dSLionel Sambuc }
2960*0a6a1f1dSLionel Sambuc
2961*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *
getCompleteObjectLocator(const VPtrInfo * Info)2962*0a6a1f1dSLionel Sambuc MSRTTIBuilder::getCompleteObjectLocator(const VPtrInfo *Info) {
2963*0a6a1f1dSLionel Sambuc SmallString<256> MangledName;
2964*0a6a1f1dSLionel Sambuc {
2965*0a6a1f1dSLionel Sambuc llvm::raw_svector_ostream Out(MangledName);
2966*0a6a1f1dSLionel Sambuc ABI.getMangleContext().mangleCXXRTTICompleteObjectLocator(RD, Info->MangledPath, Out);
2967*0a6a1f1dSLionel Sambuc }
2968*0a6a1f1dSLionel Sambuc
2969*0a6a1f1dSLionel Sambuc // Check to see if we've already computed this complete object locator.
2970*0a6a1f1dSLionel Sambuc if (auto COL = Module.getNamedGlobal(MangledName))
2971*0a6a1f1dSLionel Sambuc return COL;
2972*0a6a1f1dSLionel Sambuc
2973*0a6a1f1dSLionel Sambuc // Compute the fields of the complete object locator.
2974*0a6a1f1dSLionel Sambuc int OffsetToTop = Info->FullOffsetInMDC.getQuantity();
2975*0a6a1f1dSLionel Sambuc int VFPtrOffset = 0;
2976*0a6a1f1dSLionel Sambuc // The offset includes the vtordisp if one exists.
2977*0a6a1f1dSLionel Sambuc if (const CXXRecordDecl *VBase = Info->getVBaseWithVPtr())
2978*0a6a1f1dSLionel Sambuc if (Context.getASTRecordLayout(RD)
2979*0a6a1f1dSLionel Sambuc .getVBaseOffsetsMap()
2980*0a6a1f1dSLionel Sambuc .find(VBase)
2981*0a6a1f1dSLionel Sambuc ->second.hasVtorDisp())
2982*0a6a1f1dSLionel Sambuc VFPtrOffset = Info->NonVirtualOffset.getQuantity() + 4;
2983*0a6a1f1dSLionel Sambuc
2984*0a6a1f1dSLionel Sambuc // Forward-declare the complete object locator.
2985*0a6a1f1dSLionel Sambuc llvm::StructType *Type = ABI.getCompleteObjectLocatorType();
2986*0a6a1f1dSLionel Sambuc auto COL = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage,
2987*0a6a1f1dSLionel Sambuc /*Initializer=*/nullptr, MangledName.c_str());
2988*0a6a1f1dSLionel Sambuc
2989*0a6a1f1dSLionel Sambuc // Initialize the CompleteObjectLocator.
2990*0a6a1f1dSLionel Sambuc llvm::Constant *Fields[] = {
2991*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(CGM.IntTy, ABI.isImageRelative()),
2992*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(CGM.IntTy, OffsetToTop),
2993*0a6a1f1dSLionel Sambuc llvm::ConstantInt::get(CGM.IntTy, VFPtrOffset),
2994*0a6a1f1dSLionel Sambuc ABI.getImageRelativeConstant(
2995*0a6a1f1dSLionel Sambuc CGM.GetAddrOfRTTIDescriptor(Context.getTypeDeclType(RD))),
2996*0a6a1f1dSLionel Sambuc ABI.getImageRelativeConstant(getClassHierarchyDescriptor()),
2997*0a6a1f1dSLionel Sambuc ABI.getImageRelativeConstant(COL),
2998*0a6a1f1dSLionel Sambuc };
2999*0a6a1f1dSLionel Sambuc llvm::ArrayRef<llvm::Constant *> FieldsRef(Fields);
3000*0a6a1f1dSLionel Sambuc if (!ABI.isImageRelative())
3001*0a6a1f1dSLionel Sambuc FieldsRef = FieldsRef.drop_back();
3002*0a6a1f1dSLionel Sambuc COL->setInitializer(llvm::ConstantStruct::get(Type, FieldsRef));
3003*0a6a1f1dSLionel Sambuc return COL;
3004*0a6a1f1dSLionel Sambuc }
3005*0a6a1f1dSLionel Sambuc
3006*0a6a1f1dSLionel Sambuc /// \brief Gets a TypeDescriptor. Returns a llvm::Constant * rather than a
3007*0a6a1f1dSLionel Sambuc /// llvm::GlobalVariable * because different type descriptors have different
3008*0a6a1f1dSLionel Sambuc /// types, and need to be abstracted. They are abstracting by casting the
3009*0a6a1f1dSLionel Sambuc /// address to an Int8PtrTy.
getAddrOfRTTIDescriptor(QualType Type)3010*0a6a1f1dSLionel Sambuc llvm::Constant *MicrosoftCXXABI::getAddrOfRTTIDescriptor(QualType Type) {
3011*0a6a1f1dSLionel Sambuc SmallString<256> MangledName, TypeInfoString;
3012*0a6a1f1dSLionel Sambuc {
3013*0a6a1f1dSLionel Sambuc llvm::raw_svector_ostream Out(MangledName);
3014*0a6a1f1dSLionel Sambuc getMangleContext().mangleCXXRTTI(Type, Out);
3015*0a6a1f1dSLionel Sambuc }
3016*0a6a1f1dSLionel Sambuc
3017*0a6a1f1dSLionel Sambuc // Check to see if we've already declared this TypeDescriptor.
3018*0a6a1f1dSLionel Sambuc if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName))
3019*0a6a1f1dSLionel Sambuc return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
3020*0a6a1f1dSLionel Sambuc
3021*0a6a1f1dSLionel Sambuc // Compute the fields for the TypeDescriptor.
3022*0a6a1f1dSLionel Sambuc {
3023*0a6a1f1dSLionel Sambuc llvm::raw_svector_ostream Out(TypeInfoString);
3024*0a6a1f1dSLionel Sambuc getMangleContext().mangleCXXRTTIName(Type, Out);
3025*0a6a1f1dSLionel Sambuc }
3026*0a6a1f1dSLionel Sambuc
3027*0a6a1f1dSLionel Sambuc // Declare and initialize the TypeDescriptor.
3028*0a6a1f1dSLionel Sambuc llvm::Constant *Fields[] = {
3029*0a6a1f1dSLionel Sambuc getTypeInfoVTable(CGM), // VFPtr
3030*0a6a1f1dSLionel Sambuc llvm::ConstantPointerNull::get(CGM.Int8PtrTy), // Runtime data
3031*0a6a1f1dSLionel Sambuc llvm::ConstantDataArray::getString(CGM.getLLVMContext(), TypeInfoString)};
3032*0a6a1f1dSLionel Sambuc llvm::StructType *TypeDescriptorType =
3033*0a6a1f1dSLionel Sambuc getTypeDescriptorType(TypeInfoString);
3034*0a6a1f1dSLionel Sambuc return llvm::ConstantExpr::getBitCast(
3035*0a6a1f1dSLionel Sambuc new llvm::GlobalVariable(
3036*0a6a1f1dSLionel Sambuc CGM.getModule(), TypeDescriptorType, /*Constant=*/false,
3037*0a6a1f1dSLionel Sambuc getLinkageForRTTI(Type),
3038*0a6a1f1dSLionel Sambuc llvm::ConstantStruct::get(TypeDescriptorType, Fields),
3039*0a6a1f1dSLionel Sambuc MangledName.c_str()),
3040*0a6a1f1dSLionel Sambuc CGM.Int8PtrTy);
3041*0a6a1f1dSLionel Sambuc }
3042*0a6a1f1dSLionel Sambuc
3043*0a6a1f1dSLionel Sambuc /// \brief Gets or a creates a Microsoft CompleteObjectLocator.
3044*0a6a1f1dSLionel Sambuc llvm::GlobalVariable *
getMSCompleteObjectLocator(const CXXRecordDecl * RD,const VPtrInfo * Info)3045*0a6a1f1dSLionel Sambuc MicrosoftCXXABI::getMSCompleteObjectLocator(const CXXRecordDecl *RD,
3046*0a6a1f1dSLionel Sambuc const VPtrInfo *Info) {
3047*0a6a1f1dSLionel Sambuc return MSRTTIBuilder(*this, RD).getCompleteObjectLocator(Info);
3048*0a6a1f1dSLionel Sambuc }
3049*0a6a1f1dSLionel Sambuc
emitCXXConstructor(CodeGenModule & CGM,const CXXConstructorDecl * ctor,StructorType ctorType)3050*0a6a1f1dSLionel Sambuc static void emitCXXConstructor(CodeGenModule &CGM,
3051*0a6a1f1dSLionel Sambuc const CXXConstructorDecl *ctor,
3052*0a6a1f1dSLionel Sambuc StructorType ctorType) {
3053*0a6a1f1dSLionel Sambuc // There are no constructor variants, always emit the complete destructor.
3054*0a6a1f1dSLionel Sambuc CGM.codegenCXXStructor(ctor, StructorType::Complete);
3055*0a6a1f1dSLionel Sambuc }
3056*0a6a1f1dSLionel Sambuc
emitCXXDestructor(CodeGenModule & CGM,const CXXDestructorDecl * dtor,StructorType dtorType)3057*0a6a1f1dSLionel Sambuc static void emitCXXDestructor(CodeGenModule &CGM, const CXXDestructorDecl *dtor,
3058*0a6a1f1dSLionel Sambuc StructorType dtorType) {
3059*0a6a1f1dSLionel Sambuc // The complete destructor is equivalent to the base destructor for
3060*0a6a1f1dSLionel Sambuc // classes with no virtual bases, so try to emit it as an alias.
3061*0a6a1f1dSLionel Sambuc if (!dtor->getParent()->getNumVBases() &&
3062*0a6a1f1dSLionel Sambuc (dtorType == StructorType::Complete || dtorType == StructorType::Base)) {
3063*0a6a1f1dSLionel Sambuc bool ProducedAlias = !CGM.TryEmitDefinitionAsAlias(
3064*0a6a1f1dSLionel Sambuc GlobalDecl(dtor, Dtor_Complete), GlobalDecl(dtor, Dtor_Base), true);
3065*0a6a1f1dSLionel Sambuc if (ProducedAlias) {
3066*0a6a1f1dSLionel Sambuc if (dtorType == StructorType::Complete)
3067*0a6a1f1dSLionel Sambuc return;
3068*0a6a1f1dSLionel Sambuc if (dtor->isVirtual())
3069*0a6a1f1dSLionel Sambuc CGM.getVTables().EmitThunks(GlobalDecl(dtor, Dtor_Complete));
3070*0a6a1f1dSLionel Sambuc }
3071*0a6a1f1dSLionel Sambuc }
3072*0a6a1f1dSLionel Sambuc
3073*0a6a1f1dSLionel Sambuc // The base destructor is equivalent to the base destructor of its
3074*0a6a1f1dSLionel Sambuc // base class if there is exactly one non-virtual base class with a
3075*0a6a1f1dSLionel Sambuc // non-trivial destructor, there are no fields with a non-trivial
3076*0a6a1f1dSLionel Sambuc // destructor, and the body of the destructor is trivial.
3077*0a6a1f1dSLionel Sambuc if (dtorType == StructorType::Base && !CGM.TryEmitBaseDestructorAsAlias(dtor))
3078*0a6a1f1dSLionel Sambuc return;
3079*0a6a1f1dSLionel Sambuc
3080*0a6a1f1dSLionel Sambuc CGM.codegenCXXStructor(dtor, dtorType);
3081*0a6a1f1dSLionel Sambuc }
3082*0a6a1f1dSLionel Sambuc
emitCXXStructor(const CXXMethodDecl * MD,StructorType Type)3083*0a6a1f1dSLionel Sambuc void MicrosoftCXXABI::emitCXXStructor(const CXXMethodDecl *MD,
3084*0a6a1f1dSLionel Sambuc StructorType Type) {
3085*0a6a1f1dSLionel Sambuc if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
3086*0a6a1f1dSLionel Sambuc emitCXXConstructor(CGM, CD, Type);
3087*0a6a1f1dSLionel Sambuc return;
3088*0a6a1f1dSLionel Sambuc }
3089*0a6a1f1dSLionel Sambuc emitCXXDestructor(CGM, cast<CXXDestructorDecl>(MD), Type);
3090*0a6a1f1dSLionel Sambuc }
3091