clang 23.0.0git
MicrosoftCXXABI.cpp
Go to the documentation of this file.
1//===--- MicrosoftCXXABI.cpp - Emit LLVM Code from ASTs for a Module ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This provides C++ code generation targeting the Microsoft Visual C++ ABI.
10// The class in this file generates structures that follow the Microsoft
11// Visual C++ ABI, which is actually not very well documented at all outside
12// of Microsoft.
13//
14//===----------------------------------------------------------------------===//
15
16#include "ABIInfo.h"
17#include "CGCXXABI.h"
18#include "CGCleanup.h"
19#include "CGDebugInfo.h"
20#include "CGVTables.h"
21#include "CodeGenModule.h"
22#include "CodeGenTypes.h"
23#include "TargetInfo.h"
24#include "clang/AST/Attr.h"
26#include "clang/AST/Decl.h"
27#include "clang/AST/DeclCXX.h"
28#include "clang/AST/StmtCXX.h"
32#include "llvm/ADT/StringExtras.h"
33#include "llvm/ADT/StringSet.h"
34#include "llvm/IR/Intrinsics.h"
35
36using namespace clang;
37using namespace CodeGen;
38
39namespace {
40
41/// Holds all the vbtable globals for a given class.
42struct VBTableGlobals {
43 const VPtrInfoVector *VBTables;
44 SmallVector<llvm::GlobalVariable *, 2> Globals;
45};
46
47class MicrosoftCXXABI : public CGCXXABI {
48public:
49 MicrosoftCXXABI(CodeGenModule &CGM)
50 : CGCXXABI(CGM), BaseClassDescriptorType(nullptr),
51 ClassHierarchyDescriptorType(nullptr),
52 CompleteObjectLocatorType(nullptr), CatchableTypeType(nullptr),
53 ThrowInfoType(nullptr) {
56 "visibility export mapping option unimplemented in this ABI");
57 }
58
59 bool HasThisReturn(GlobalDecl GD) const override;
60 bool hasMostDerivedReturn(GlobalDecl GD) const override;
61
62 bool classifyReturnType(CGFunctionInfo &FI) const override;
63
64 RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override;
65
66 bool isSRetParameterAfterThis() const override { return true; }
67
68 bool isThisCompleteObject(GlobalDecl GD) const override {
69 // The Microsoft ABI doesn't use separate complete-object vs.
70 // base-object variants of constructors, but it does of destructors.
72 switch (GD.getDtorType()) {
73 case Dtor_Complete:
74 case Dtor_Deleting:
76 return true;
77 case Dtor_Base:
78 return false;
79
80 case Dtor_Comdat: llvm_unreachable("emitting dtor comdat as function?");
81 case Dtor_Unified:
82 llvm_unreachable("unexpected unified dtor type");
83 }
84 llvm_unreachable("bad dtor kind");
85 }
86
87 // No other kinds.
88 return false;
89 }
90
91 size_t getSrcArgforCopyCtor(const CXXConstructorDecl *CD,
92 FunctionArgList &Args) const override {
93 assert(Args.size() >= 2 &&
94 "expected the arglist to have at least two args!");
95 // The 'most_derived' parameter goes second if the ctor is variadic and
96 // has v-bases.
97 if (CD->getParent()->getNumVBases() > 0 &&
98 CD->getType()->castAs<FunctionProtoType>()->isVariadic())
99 return 2;
100 return 1;
101 }
102
103 std::vector<CharUnits> getVBPtrOffsets(const CXXRecordDecl *RD) override {
104 std::vector<CharUnits> VBPtrOffsets;
105 const ASTContext &Context = getContext();
106 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
107
108 const VBTableGlobals &VBGlobals = enumerateVBTables(RD);
109 for (const std::unique_ptr<VPtrInfo> &VBT : *VBGlobals.VBTables) {
110 const ASTRecordLayout &SubobjectLayout =
111 Context.getASTRecordLayout(VBT->IntroducingObject);
112 CharUnits Offs = VBT->NonVirtualOffset;
113 Offs += SubobjectLayout.getVBPtrOffset();
114 if (VBT->getVBaseWithVPtr())
115 Offs += Layout.getVBaseClassOffset(VBT->getVBaseWithVPtr());
116 VBPtrOffsets.push_back(Offs);
117 }
118 llvm::array_pod_sort(VBPtrOffsets.begin(), VBPtrOffsets.end());
119 return VBPtrOffsets;
120 }
121
122 StringRef GetPureVirtualCallName() override { return "_purecall"; }
123 StringRef GetDeletedVirtualCallName() override { return "_purecall"; }
124
125 void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE,
126 Address Ptr, QualType ElementType,
127 const CXXDestructorDecl *Dtor) override;
128
129 void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override;
130 void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) override;
131
132 void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override;
133
134 llvm::GlobalVariable *getMSCompleteObjectLocator(const CXXRecordDecl *RD,
135 const VPtrInfo &Info);
136
137 llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override;
138 CatchTypeInfo
139 getAddrOfCXXCatchHandlerType(QualType Ty, QualType CatchHandlerType) override;
140
141 /// MSVC needs an extra flag to indicate a catchall.
142 CatchTypeInfo getCatchAllTypeInfo() override {
143 // For -EHa catch(...) must handle HW exception
144 // Adjective = HT_IsStdDotDot (0x40), only catch C++ exceptions
145 if (getContext().getLangOpts().EHAsynch)
146 return CatchTypeInfo{nullptr, 0};
147 else
148 return CatchTypeInfo{nullptr, 0x40};
149 }
150
151 bool shouldTypeidBeNullChecked(QualType SrcRecordTy) override;
152 void EmitBadTypeidCall(CodeGenFunction &CGF) override;
153 llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
154 Address ThisPtr,
155 llvm::Type *StdTypeInfoPtrTy) override;
156
157 bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
158 QualType SrcRecordTy) override;
159
160 bool shouldEmitExactDynamicCast(QualType DestRecordTy) override {
161 // TODO: Add support for exact dynamic_casts.
162 return false;
163 }
164 std::optional<ExactDynamicCastInfo>
165 getExactDynamicCastInfo(QualType SrcRecordTy, QualType DestTy,
166 QualType DestRecordTy) override {
167 llvm_unreachable("unsupported");
168 }
169 llvm::Value *emitExactDynamicCast(CodeGenFunction &CGF, Address Value,
170 QualType SrcRecordTy, QualType DestTy,
171 QualType DestRecordTy,
172 const ExactDynamicCastInfo &CastInfo,
173 llvm::BasicBlock *CastSuccess,
174 llvm::BasicBlock *CastFail) override {
175 llvm_unreachable("unsupported");
176 }
177
178 llvm::Value *emitDynamicCastCall(CodeGenFunction &CGF, Address Value,
179 QualType SrcRecordTy, QualType DestTy,
180 QualType DestRecordTy,
181 llvm::BasicBlock *CastEnd) override;
182
183 llvm::Value *emitDynamicCastToVoid(CodeGenFunction &CGF, Address Value,
184 QualType SrcRecordTy) override;
185
186 bool EmitBadCastCall(CodeGenFunction &CGF) override;
187 bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const override {
188 return false;
189 }
190
191 llvm::Value *
192 GetVirtualBaseClassOffset(CodeGenFunction &CGF, Address This,
193 const CXXRecordDecl *ClassDecl,
194 const CXXRecordDecl *BaseClassDecl) override;
195
196 llvm::BasicBlock *
197 EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
198 const CXXRecordDecl *RD) override;
199
200 llvm::BasicBlock *
201 EmitDtorCompleteObjectHandler(CodeGenFunction &CGF);
202
203 void initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF,
204 const CXXRecordDecl *RD) override;
205
206 void EmitCXXConstructors(const CXXConstructorDecl *D) override;
207
208 // Background on MSVC destructors
209 // ==============================
210 //
211 // Both Itanium and MSVC ABIs have destructor variants. The variant names
212 // roughly correspond in the following way:
213 // Itanium Microsoft
214 // Base -> no name, just ~Class
215 // Complete -> vbase destructor
216 // Deleting -> scalar deleting destructor
217 // vector deleting destructor
218 //
219 // The base and complete destructors are the same as in Itanium, although the
220 // complete destructor does not accept a VTT parameter when there are virtual
221 // bases. A separate mechanism involving vtordisps is used to ensure that
222 // virtual methods of destroyed subobjects are not called.
223 //
224 // The deleting destructors accept an i32 bitfield as a second parameter. Bit
225 // 1 indicates if the memory should be deleted. Bit 2 indicates if the this
226 // pointer points to an array. The scalar deleting destructor assumes that
227 // bit 2 is zero, and therefore does not contain a loop.
228 //
229 // For virtual destructors, only one entry is reserved in the vftable, and it
230 // always points to the vector deleting destructor. The vector deleting
231 // destructor is the most general, so it can be used to destroy objects in
232 // place, delete single heap objects, or delete arrays.
233 //
234 // A TU defining a non-inline destructor is only guaranteed to emit a base
235 // destructor, and all of the other variants are emitted on an as-needed basis
236 // in COMDATs. Because a non-base destructor can be emitted in a TU that
237 // lacks a definition for the destructor, non-base destructors must always
238 // delegate to or alias the base destructor.
239
240 AddedStructorArgCounts
241 buildStructorSignature(GlobalDecl GD,
242 SmallVectorImpl<CanQualType> &ArgTys) override;
243
244 /// Non-base dtors should be emitted as delegating thunks in this ABI.
245 bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
246 CXXDtorType DT) const override {
247 return DT != Dtor_Base;
248 }
249
250 void setCXXDestructorDLLStorage(llvm::GlobalValue *GV,
251 const CXXDestructorDecl *Dtor,
252 CXXDtorType DT) const override;
253
254 llvm::GlobalValue::LinkageTypes
255 getCXXDestructorLinkage(GVALinkage Linkage, const CXXDestructorDecl *Dtor,
256 CXXDtorType DT) const override;
257
258 void EmitCXXDestructors(const CXXDestructorDecl *D) override;
259
260 const CXXRecordDecl *getThisArgumentTypeForMethod(GlobalDecl GD) override {
261 auto *MD = cast<CXXMethodDecl>(GD.getDecl());
262
263 if (MD->isVirtual()) {
264 GlobalDecl LookupGD = GD;
265 if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) {
266 // Complete dtors take a pointer to the complete object,
267 // thus don't need adjustment.
268 if (GD.getDtorType() == Dtor_Complete)
269 return MD->getParent();
270
271 // There's only Dtor_Deleting in vftable but it shares the this
272 // adjustment with the base one, so look up the deleting one instead.
273 LookupGD = GlobalDecl(
275 CGM.getContext().getLangOpts())
277 : Dtor_Deleting);
278 }
279 MethodVFTableLocation ML =
281
282 // The vbases might be ordered differently in the final overrider object
283 // and the complete object, so the "this" argument may sometimes point to
284 // memory that has no particular type (e.g. past the complete object).
285 // In this case, we just use a generic pointer type.
286 // FIXME: might want to have a more precise type in the non-virtual
287 // multiple inheritance case.
288 if (ML.VBase || !ML.VFPtrOffset.isZero())
289 return nullptr;
290 }
291 return MD->getParent();
292 }
293
294 Address
295 adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD,
296 Address This,
297 bool VirtualCall) override;
298
299 void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
300 FunctionArgList &Params) override;
301
302 void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override;
303
304 AddedStructorArgs getImplicitConstructorArgs(CodeGenFunction &CGF,
305 const CXXConstructorDecl *D,
307 bool ForVirtualBase,
308 bool Delegating) override;
309
310 llvm::Value *getCXXDestructorImplicitParam(CodeGenFunction &CGF,
311 const CXXDestructorDecl *DD,
313 bool ForVirtualBase,
314 bool Delegating) override;
315
316 void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD,
317 CXXDtorType Type, bool ForVirtualBase,
318 bool Delegating, Address This,
319 QualType ThisTy) override;
320
321 void emitVTableTypeMetadata(const VPtrInfo &Info, const CXXRecordDecl *RD,
322 llvm::GlobalVariable *VTable);
323
324 void emitVTableDefinitions(CodeGenVTables &CGVT,
325 const CXXRecordDecl *RD) override;
326
327 bool isVirtualOffsetNeededForVTableField(CodeGenFunction &CGF,
328 CodeGenFunction::VPtr Vptr) override;
329
330 /// Don't initialize vptrs if dynamic class
331 /// is marked with the 'novtable' attribute.
332 bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass) override {
333 return !VTableClass->hasAttr<MSNoVTableAttr>();
334 }
335
336 llvm::Constant *
337 getVTableAddressPoint(BaseSubobject Base,
338 const CXXRecordDecl *VTableClass) override;
339
340 llvm::Value *getVTableAddressPointInStructor(
341 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
342 BaseSubobject Base, const CXXRecordDecl *NearestVBase) override;
343
344 llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
345 CharUnits VPtrOffset) override;
346
347 CGCallee getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD,
348 Address This, llvm::Type *Ty,
349 SourceLocation Loc) override;
350
351 llvm::Value *
352 EmitVirtualDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *Dtor,
353 CXXDtorType DtorType, Address This,
354 DeleteOrMemberCallExpr E,
355 llvm::CallBase **CallOrInvoke) override;
356
357 void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, GlobalDecl GD,
358 CallArgList &CallArgs) override {
359 assert((GD.getDtorType() == Dtor_VectorDeleting ||
360 GD.getDtorType() == Dtor_Deleting) &&
361 "Only vector deleting destructor thunks are available in this ABI");
362 CallArgs.add(RValue::get(getStructorImplicitParamValue(CGF)),
363 getContext().IntTy);
364 }
365
366 void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override;
367
368 llvm::GlobalVariable *
369 getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD,
370 llvm::GlobalVariable::LinkageTypes Linkage);
371
372 llvm::GlobalVariable *
373 getAddrOfVirtualDisplacementMap(const CXXRecordDecl *SrcRD,
374 const CXXRecordDecl *DstRD) {
375 SmallString<256> OutName;
376 llvm::raw_svector_ostream Out(OutName);
377 getMangleContext().mangleCXXVirtualDisplacementMap(SrcRD, DstRD, Out);
378 StringRef MangledName = OutName.str();
379
380 if (auto *VDispMap = CGM.getModule().getNamedGlobal(MangledName))
381 return VDispMap;
382
383 MicrosoftVTableContext &VTContext = CGM.getMicrosoftVTableContext();
384 unsigned NumEntries = 1 + SrcRD->getNumVBases();
385 SmallVector<llvm::Constant *, 4> Map(NumEntries,
386 llvm::PoisonValue::get(CGM.IntTy));
387 Map[0] = llvm::ConstantInt::get(CGM.IntTy, 0);
388 bool AnyDifferent = false;
389 for (const auto &I : SrcRD->vbases()) {
390 const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl();
391 if (!DstRD->isVirtuallyDerivedFrom(VBase))
392 continue;
393
394 unsigned SrcVBIndex = VTContext.getVBTableIndex(SrcRD, VBase);
395 unsigned DstVBIndex = VTContext.getVBTableIndex(DstRD, VBase);
396 Map[SrcVBIndex] = llvm::ConstantInt::get(CGM.IntTy, DstVBIndex * 4);
397 AnyDifferent |= SrcVBIndex != DstVBIndex;
398 }
399 // This map would be useless, don't use it.
400 if (!AnyDifferent)
401 return nullptr;
402
403 llvm::ArrayType *VDispMapTy = llvm::ArrayType::get(CGM.IntTy, Map.size());
404 llvm::Constant *Init = llvm::ConstantArray::get(VDispMapTy, Map);
405 llvm::GlobalValue::LinkageTypes Linkage =
406 SrcRD->isExternallyVisible() && DstRD->isExternallyVisible()
407 ? llvm::GlobalValue::LinkOnceODRLinkage
408 : llvm::GlobalValue::InternalLinkage;
409 auto *VDispMap = new llvm::GlobalVariable(
410 CGM.getModule(), VDispMapTy, /*isConstant=*/true, Linkage,
411 /*Initializer=*/Init, MangledName);
412 return VDispMap;
413 }
414
415 void emitVBTableDefinition(const VPtrInfo &VBT, const CXXRecordDecl *RD,
416 llvm::GlobalVariable *GV) const;
417
418 void setThunkLinkage(llvm::Function *Thunk, bool ForVTable,
419 GlobalDecl GD, bool ReturnAdjustment) override {
421 getContext().GetGVALinkageForFunction(cast<FunctionDecl>(GD.getDecl()));
422
423 if (Linkage == GVA_Internal)
424 Thunk->setLinkage(llvm::GlobalValue::InternalLinkage);
425 else if (ReturnAdjustment)
426 Thunk->setLinkage(llvm::GlobalValue::WeakODRLinkage);
427 else
428 Thunk->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage);
429 }
430
431 bool exportThunk() override { return false; }
432
433 llvm::Value *performThisAdjustment(CodeGenFunction &CGF, Address This,
434 const CXXRecordDecl * /*UnadjustedClass*/,
435 const ThunkInfo &TI) override;
436
437 llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, Address Ret,
438 const CXXRecordDecl * /*UnadjustedClass*/,
439 const ReturnAdjustment &RA) override;
440
441 void EmitThreadLocalInitFuncs(
442 CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals,
443 ArrayRef<llvm::Function *> CXXThreadLocalInits,
444 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) override;
445
446 bool usesThreadWrapperFunction(const VarDecl *VD) const override {
447 return getContext().getLangOpts().isCompatibleWithMSVC(
448 LangOptions::MSVC2019_5) &&
449 CGM.getCodeGenOpts().TlsGuards &&
450 (!isEmittedWithConstantInitializer(VD) || mayNeedDestruction(VD));
451 }
452 LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD,
453 QualType LValType) override;
454
455 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
456 llvm::GlobalVariable *DeclPtr,
457 bool PerformInit) override;
458 void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
459 llvm::FunctionCallee Dtor,
460 llvm::Constant *Addr) override;
461
462 // ==== Notes on array cookies =========
463 //
464 // MSVC seems to only use cookies when the class has a destructor; a
465 // two-argument usual array deallocation function isn't sufficient.
466 //
467 // For example, this code prints "100" and "1":
468 // struct A {
469 // char x;
470 // void *operator new[](size_t sz) {
471 // printf("%u\n", sz);
472 // return malloc(sz);
473 // }
474 // void operator delete[](void *p, size_t sz) {
475 // printf("%u\n", sz);
476 // free(p);
477 // }
478 // };
479 // int main() {
480 // A *p = new A[100];
481 // delete[] p;
482 // }
483 // Whereas it prints "104" and "104" if you give A a destructor.
484
485 bool requiresArrayCookie(const CXXDeleteExpr *expr,
486 QualType elementType) override;
487 bool requiresArrayCookie(const CXXNewExpr *expr) override;
488 CharUnits getArrayCookieSizeImpl(QualType type) override;
489 Address InitializeArrayCookie(CodeGenFunction &CGF,
490 Address NewPtr,
491 llvm::Value *NumElements,
492 const CXXNewExpr *expr,
493 QualType ElementType) override;
494 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
495 Address allocPtr,
496 CharUnits cookieSize) override;
497
498 friend struct MSRTTIBuilder;
499
500 bool isImageRelative() const {
501 return CGM.getTarget().getPointerWidth(LangAS::Default) == 64;
502 }
503
504 // 5 routines for constructing the llvm types for MS RTTI structs.
505 llvm::StructType *getTypeDescriptorType(StringRef TypeInfoString) {
506 llvm::SmallString<32> TDTypeName("rtti.TypeDescriptor");
507 TDTypeName += llvm::utostr(TypeInfoString.size());
508 llvm::StructType *&TypeDescriptorType =
509 TypeDescriptorTypeMap[TypeInfoString.size()];
510 if (TypeDescriptorType)
511 return TypeDescriptorType;
512 llvm::Type *FieldTypes[] = {
513 CGM.Int8PtrPtrTy,
514 CGM.Int8PtrTy,
515 llvm::ArrayType::get(CGM.Int8Ty, TypeInfoString.size() + 1)};
516 TypeDescriptorType =
517 llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, TDTypeName);
518 return TypeDescriptorType;
519 }
520
521 llvm::Type *getImageRelativeType(llvm::Type *PtrType) {
522 if (!isImageRelative())
523 return PtrType;
524 return CGM.IntTy;
525 }
526
527 llvm::StructType *getBaseClassDescriptorType() {
528 if (BaseClassDescriptorType)
529 return BaseClassDescriptorType;
530 llvm::Type *FieldTypes[] = {
531 getImageRelativeType(CGM.Int8PtrTy),
532 CGM.IntTy,
533 CGM.IntTy,
534 CGM.IntTy,
535 CGM.IntTy,
536 CGM.IntTy,
537 getImageRelativeType(CGM.DefaultPtrTy),
538 };
539 BaseClassDescriptorType = llvm::StructType::create(
540 CGM.getLLVMContext(), FieldTypes, "rtti.BaseClassDescriptor");
541 return BaseClassDescriptorType;
542 }
543
544 llvm::StructType *getClassHierarchyDescriptorType() {
545 if (ClassHierarchyDescriptorType)
546 return ClassHierarchyDescriptorType;
547 // Forward-declare RTTIClassHierarchyDescriptor to break a cycle.
548 llvm::Type *FieldTypes[] = {CGM.IntTy, CGM.IntTy, CGM.IntTy,
549 getImageRelativeType(CGM.DefaultPtrTy)};
550 ClassHierarchyDescriptorType =
551 llvm::StructType::create(FieldTypes, "rtti.ClassHierarchyDescriptor");
552 return ClassHierarchyDescriptorType;
553 }
554
555 llvm::StructType *getCompleteObjectLocatorType() {
556 if (CompleteObjectLocatorType)
557 return CompleteObjectLocatorType;
558 llvm::Type *FieldTypes[] = {
559 CGM.IntTy,
560 CGM.IntTy,
561 CGM.IntTy,
562 getImageRelativeType(CGM.Int8PtrTy),
563 getImageRelativeType(CGM.DefaultPtrTy),
564 getImageRelativeType(CGM.VoidTy),
565 };
566 llvm::ArrayRef<llvm::Type *> FieldTypesRef(FieldTypes);
567 if (!isImageRelative())
568 FieldTypesRef = FieldTypesRef.drop_back();
569 CompleteObjectLocatorType =
570 llvm::StructType::create(FieldTypesRef, "rtti.CompleteObjectLocator");
571 return CompleteObjectLocatorType;
572 }
573
574 llvm::GlobalVariable *getImageBase() {
575 StringRef Name = "__ImageBase";
576 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name))
577 return GV;
578
579 auto *GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty,
580 /*isConstant=*/true,
581 llvm::GlobalValue::ExternalLinkage,
582 /*Initializer=*/nullptr, Name);
583 CGM.setDSOLocal(GV);
584 return GV;
585 }
586
587 llvm::Constant *getImageRelativeConstant(llvm::Constant *PtrVal) {
588 if (!isImageRelative())
589 return PtrVal;
590
591 if (PtrVal->isNullValue())
592 return llvm::Constant::getNullValue(CGM.IntTy);
593
594 llvm::Constant *ImageBaseAsInt =
595 llvm::ConstantExpr::getPtrToInt(getImageBase(), CGM.IntPtrTy);
596 llvm::Constant *PtrValAsInt =
597 llvm::ConstantExpr::getPtrToInt(PtrVal, CGM.IntPtrTy);
598 llvm::Constant *Diff =
599 llvm::ConstantExpr::getSub(PtrValAsInt, ImageBaseAsInt,
600 /*HasNUW=*/true, /*HasNSW=*/true);
601 return llvm::ConstantExpr::getTrunc(Diff, CGM.IntTy);
602 }
603
604private:
605 MicrosoftMangleContext &getMangleContext() {
607 }
608
609 llvm::Constant *getZeroInt() {
610 return llvm::ConstantInt::get(CGM.IntTy, 0);
611 }
612
613 llvm::Constant *getAllOnesInt() {
614 return llvm::Constant::getAllOnesValue(CGM.IntTy);
615 }
616
617 CharUnits getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) override;
618
619 void
620 GetNullMemberPointerFields(const MemberPointerType *MPT,
621 llvm::SmallVectorImpl<llvm::Constant *> &fields);
622
623 /// Shared code for virtual base adjustment. Returns the offset from
624 /// the vbptr to the virtual base. Optionally returns the address of the
625 /// vbptr itself.
626 llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF,
627 Address Base,
628 llvm::Value *VBPtrOffset,
629 llvm::Value *VBTableOffset,
630 llvm::Value **VBPtr = nullptr);
631
632 llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF,
633 Address Base,
634 int32_t VBPtrOffset,
635 int32_t VBTableOffset,
636 llvm::Value **VBPtr = nullptr) {
637 assert(VBTableOffset % 4 == 0 && "should be byte offset into table of i32s");
638 llvm::Value *VBPOffset =
639 llvm::ConstantInt::getSigned(CGM.IntTy, VBPtrOffset);
640 llvm::Value *VBTOffset = llvm::ConstantInt::get(CGM.IntTy, VBTableOffset);
641 return GetVBaseOffsetFromVBPtr(CGF, Base, VBPOffset, VBTOffset, VBPtr);
642 }
643
644 std::tuple<Address, llvm::Value *, const CXXRecordDecl *>
645 performBaseAdjustment(CodeGenFunction &CGF, Address Value,
646 QualType SrcRecordTy);
647
648 /// Performs a full virtual base adjustment. Used to dereference
649 /// pointers to members of virtual bases.
650 llvm::Value *AdjustVirtualBase(CodeGenFunction &CGF, const Expr *E,
651 const CXXRecordDecl *RD, Address Base,
652 llvm::Value *VirtualBaseAdjustmentOffset,
653 llvm::Value *VBPtrOffset /* optional */);
654
655 /// Emits a full member pointer with the fields common to data and
656 /// function member pointers.
657 llvm::Constant *EmitFullMemberPointer(llvm::Constant *FirstField,
658 bool IsMemberFunction,
659 const CXXRecordDecl *RD,
660 CharUnits NonVirtualBaseAdjustment,
661 unsigned VBTableIndex);
662
663 bool MemberPointerConstantIsNull(const MemberPointerType *MPT,
664 llvm::Constant *MP);
665
666 /// - Initialize all vbptrs of 'this' with RD as the complete type.
667 void EmitVBPtrStores(CodeGenFunction &CGF, const CXXRecordDecl *RD);
668
669 /// Caching wrapper around VBTableBuilder::enumerateVBTables().
670 const VBTableGlobals &enumerateVBTables(const CXXRecordDecl *RD);
671
672 /// Generate a thunk for calling a virtual member function MD.
673 llvm::Function *EmitVirtualMemPtrThunk(const CXXMethodDecl *MD,
674 const MethodVFTableLocation &ML);
675
676 llvm::Constant *EmitMemberDataPointer(const CXXRecordDecl *RD,
677 CharUnits offset);
678
679public:
680 llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override;
681
682 bool isZeroInitializable(const MemberPointerType *MPT) override;
683
684 bool isMemberPointerConvertible(const MemberPointerType *MPT) const override {
685 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
686 return RD->hasAttr<MSInheritanceAttr>();
687 }
688
689 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override;
690
691 llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
692 CharUnits offset) override;
693 llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD) override;
694 llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override;
695
696 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
697 llvm::Value *L,
698 llvm::Value *R,
699 const MemberPointerType *MPT,
700 bool Inequality) override;
701
702 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
703 llvm::Value *MemPtr,
704 const MemberPointerType *MPT) override;
705
706 llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
707 Address Base, llvm::Value *MemPtr,
708 const MemberPointerType *MPT,
709 bool IsInBounds) override;
710
711 llvm::Value *EmitNonNullMemberPointerConversion(
712 const MemberPointerType *SrcTy, const MemberPointerType *DstTy,
714 CastExpr::path_const_iterator PathEnd, llvm::Value *Src,
715 CGBuilderTy &Builder);
716
717 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
718 const CastExpr *E,
719 llvm::Value *Src) override;
720
721 llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
722 llvm::Constant *Src) override;
723
724 llvm::Constant *EmitMemberPointerConversion(
725 const MemberPointerType *SrcTy, const MemberPointerType *DstTy,
727 CastExpr::path_const_iterator PathEnd, llvm::Constant *Src);
728
729 CGCallee
730 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, const Expr *E,
731 Address This, llvm::Value *&ThisPtrForCall,
732 llvm::Value *MemPtr,
733 const MemberPointerType *MPT) override;
734
735 void emitCXXStructor(GlobalDecl GD) override;
736
737 llvm::StructType *getCatchableTypeType() {
738 if (CatchableTypeType)
739 return CatchableTypeType;
740 llvm::Type *FieldTypes[] = {
741 CGM.IntTy, // Flags
742 getImageRelativeType(CGM.Int8PtrTy), // TypeDescriptor
743 CGM.IntTy, // NonVirtualAdjustment
744 CGM.IntTy, // OffsetToVBPtr
745 CGM.IntTy, // VBTableIndex
746 CGM.IntTy, // Size
747 getImageRelativeType(CGM.Int8PtrTy) // CopyCtor
748 };
749 CatchableTypeType = llvm::StructType::create(
750 CGM.getLLVMContext(), FieldTypes, "eh.CatchableType");
751 return CatchableTypeType;
752 }
753
754 llvm::StructType *getCatchableTypeArrayType(uint32_t NumEntries) {
755 llvm::StructType *&CatchableTypeArrayType =
756 CatchableTypeArrayTypeMap[NumEntries];
757 if (CatchableTypeArrayType)
758 return CatchableTypeArrayType;
759
760 llvm::SmallString<23> CTATypeName("eh.CatchableTypeArray.");
761 CTATypeName += llvm::utostr(NumEntries);
762 llvm::Type *CTType = getImageRelativeType(CGM.DefaultPtrTy);
763 llvm::Type *FieldTypes[] = {
764 CGM.IntTy, // NumEntries
765 llvm::ArrayType::get(CTType, NumEntries) // CatchableTypes
766 };
767 CatchableTypeArrayType =
768 llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, CTATypeName);
769 return CatchableTypeArrayType;
770 }
771
772 llvm::StructType *getThrowInfoType() {
773 if (ThrowInfoType)
774 return ThrowInfoType;
775 llvm::Type *FieldTypes[] = {
776 CGM.IntTy, // Flags
777 getImageRelativeType(CGM.Int8PtrTy), // CleanupFn
778 getImageRelativeType(CGM.Int8PtrTy), // ForwardCompat
779 getImageRelativeType(CGM.Int8PtrTy) // CatchableTypeArray
780 };
781 ThrowInfoType = llvm::StructType::create(CGM.getLLVMContext(), FieldTypes,
782 "eh.ThrowInfo");
783 return ThrowInfoType;
784 }
785
786 llvm::FunctionCallee getThrowFn() {
787 // _CxxThrowException is passed an exception object and a ThrowInfo object
788 // which describes the exception.
789 llvm::Type *Args[] = {CGM.Int8PtrTy, CGM.DefaultPtrTy};
790 llvm::FunctionType *FTy =
791 llvm::FunctionType::get(CGM.VoidTy, Args, /*isVarArg=*/false);
792 llvm::FunctionCallee Throw =
793 CGM.CreateRuntimeFunction(FTy, "_CxxThrowException");
794 // _CxxThrowException is stdcall on 32-bit x86 platforms.
795 if (CGM.getTarget().getTriple().getArch() == llvm::Triple::x86) {
796 if (auto *Fn = dyn_cast<llvm::Function>(Throw.getCallee()))
797 Fn->setCallingConv(llvm::CallingConv::X86_StdCall);
798 }
799 return Throw;
800 }
801
802 llvm::Function *getAddrOfCXXCtorClosure(const CXXConstructorDecl *CD,
803 CXXCtorType CT);
804
805 llvm::Constant *getCatchableType(QualType T,
806 uint32_t NVOffset = 0,
807 int32_t VBPtrOffset = -1,
808 uint32_t VBIndex = 0);
809
810 llvm::GlobalVariable *getCatchableTypeArray(QualType T);
811
812 llvm::GlobalVariable *getThrowInfo(QualType T) override;
813
814 std::pair<llvm::Value *, const CXXRecordDecl *>
815 LoadVTablePtr(CodeGenFunction &CGF, Address This,
816 const CXXRecordDecl *RD) override;
817
818 bool
819 isPermittedToBeHomogeneousAggregate(const CXXRecordDecl *RD) const override;
820
821private:
822 typedef std::pair<const CXXRecordDecl *, CharUnits> VFTableIdTy;
823 typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalVariable *> VTablesMapTy;
824 typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalValue *> VFTablesMapTy;
825 /// All the vftables that have been referenced.
826 VFTablesMapTy VFTablesMap;
827 VTablesMapTy VTablesMap;
828
829 /// This set holds the record decls we've deferred vtable emission for.
830 llvm::SmallPtrSet<const CXXRecordDecl *, 4> DeferredVFTables;
831
832
833 /// All the vbtables which have been referenced.
834 llvm::DenseMap<const CXXRecordDecl *, VBTableGlobals> VBTablesMap;
835
836 /// Info on the global variable used to guard initialization of static locals.
837 /// The BitIndex field is only used for externally invisible declarations.
838 struct GuardInfo {
839 GuardInfo() = default;
840 llvm::GlobalVariable *Guard = nullptr;
841 unsigned BitIndex = 0;
842 };
843
844 /// Map from DeclContext to the current guard variable. We assume that the
845 /// AST is visited in source code order.
846 llvm::DenseMap<const DeclContext *, GuardInfo> GuardVariableMap;
847 llvm::DenseMap<const DeclContext *, GuardInfo> ThreadLocalGuardVariableMap;
848 llvm::DenseMap<const DeclContext *, unsigned> ThreadSafeGuardNumMap;
849
850 llvm::DenseMap<size_t, llvm::StructType *> TypeDescriptorTypeMap;
851 llvm::StructType *BaseClassDescriptorType;
852 llvm::StructType *ClassHierarchyDescriptorType;
853 llvm::StructType *CompleteObjectLocatorType;
854
855 llvm::DenseMap<QualType, llvm::GlobalVariable *> CatchableTypeArrays;
856
857 llvm::StructType *CatchableTypeType;
858 llvm::DenseMap<uint32_t, llvm::StructType *> CatchableTypeArrayTypeMap;
859 llvm::StructType *ThrowInfoType;
860};
861
862}
863
865MicrosoftCXXABI::getRecordArgABI(const CXXRecordDecl *RD) const {
866 // Use the default C calling convention rules for things that can be passed in
867 // registers, i.e. non-trivially copyable records or records marked with
868 // [[trivial_abi]].
869 if (RD->canPassInRegisters())
870 return RAA_Default;
871
872 switch (CGM.getTarget().getTriple().getArch()) {
873 default:
874 // FIXME: Implement for other architectures.
875 return RAA_Indirect;
876
877 case llvm::Triple::thumb:
878 // Pass things indirectly for now because it is simple.
879 // FIXME: This is incompatible with MSVC for arguments with a dtor and no
880 // copy ctor.
881 return RAA_Indirect;
882
883 case llvm::Triple::x86: {
884 // If the argument has *required* alignment greater than four bytes, pass
885 // it indirectly. Prior to MSVC version 19.14, passing overaligned
886 // arguments was not supported and resulted in a compiler error. In 19.14
887 // and later versions, such arguments are now passed indirectly.
888 TypeInfo Info =
889 getContext().getTypeInfo(getContext().getCanonicalTagType(RD));
890 if (Info.isAlignRequired() && Info.Align > 4)
891 return RAA_Indirect;
892
893 // If C++ prohibits us from making a copy, construct the arguments directly
894 // into argument memory.
895 return RAA_DirectInMemory;
896 }
897
898 case llvm::Triple::x86_64:
899 case llvm::Triple::aarch64:
900 return RAA_Indirect;
901 }
902
903 llvm_unreachable("invalid enum");
904}
905
906void MicrosoftCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
907 const CXXDeleteExpr *DE,
908 Address Ptr,
909 QualType ElementType,
910 const CXXDestructorDecl *Dtor) {
911 // FIXME: Provide a source location here even though there's no
912 // CXXMemberCallExpr for dtor call.
913 if (!getContext().getTargetInfo().callGlobalDeleteInDeletingDtor(
914 getContext().getLangOpts())) {
915 bool UseGlobalDelete = DE->isGlobalDelete();
916 CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting;
917 llvm::Value *MDThis =
918 EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, DE,
919 /*CallOrInvoke=*/nullptr);
920 if (UseGlobalDelete)
921 CGF.EmitDeleteCall(DE->getOperatorDelete(), MDThis, ElementType);
922 } else {
923 EmitVirtualDestructorCall(CGF, Dtor, Dtor_Deleting, Ptr, DE,
924 /*CallOrInvoke=*/nullptr);
925 }
926}
927
928void MicrosoftCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) {
929 llvm::Value *Args[] = {llvm::ConstantPointerNull::get(CGM.Int8PtrTy),
930 llvm::ConstantPointerNull::get(CGM.DefaultPtrTy)};
931 llvm::FunctionCallee Fn = getThrowFn();
932 if (isNoReturn)
934 else
935 CGF.EmitRuntimeCallOrInvoke(Fn, Args);
936}
937
938void MicrosoftCXXABI::emitBeginCatch(CodeGenFunction &CGF,
939 const CXXCatchStmt *S) {
940 // In the MS ABI, the runtime handles the copy, and the catch handler is
941 // responsible for destruction.
942 VarDecl *CatchParam = S->getExceptionDecl();
943 llvm::BasicBlock *CatchPadBB = CGF.Builder.GetInsertBlock();
944 llvm::CatchPadInst *CPI =
945 cast<llvm::CatchPadInst>(CatchPadBB->getFirstNonPHIIt());
946 CGF.CurrentFuncletPad = CPI;
947
948 // If this is a catch-all or the catch parameter is unnamed, we don't need to
949 // emit an alloca to the object.
950 if (!CatchParam || !CatchParam->getDeclName()) {
951 CGF.EHStack.pushCleanup<CatchRetScope>(NormalCleanup, CPI);
952 return;
953 }
954
955 CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
956 CPI->setArgOperand(2, var.getObjectAddress(CGF).emitRawPointer(CGF));
957 CGF.EHStack.pushCleanup<CatchRetScope>(NormalCleanup, CPI);
958 CGF.EmitAutoVarCleanups(var);
959}
960
961/// We need to perform a generic polymorphic operation (like a typeid
962/// or a cast), which requires an object with a vfptr. Adjust the
963/// address to point to an object with a vfptr.
964std::tuple<Address, llvm::Value *, const CXXRecordDecl *>
965MicrosoftCXXABI::performBaseAdjustment(CodeGenFunction &CGF, Address Value,
966 QualType SrcRecordTy) {
967 Value = Value.withElementType(CGF.Int8Ty);
968 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
969 const ASTContext &Context = getContext();
970
971 // If the class itself has a vfptr, great. This check implicitly
972 // covers non-virtual base subobjects: a class with its own virtual
973 // functions would be a candidate to be a primary base.
974 if (Context.getASTRecordLayout(SrcDecl).hasExtendableVFPtr())
975 return std::make_tuple(Value, llvm::ConstantInt::get(CGF.Int32Ty, 0),
976 SrcDecl);
977
978 // Okay, one of the vbases must have a vfptr, or else this isn't
979 // actually a polymorphic class.
980 const CXXRecordDecl *PolymorphicBase = nullptr;
981 for (auto &Base : SrcDecl->vbases()) {
982 const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
983 if (Context.getASTRecordLayout(BaseDecl).hasExtendableVFPtr()) {
984 PolymorphicBase = BaseDecl;
985 break;
986 }
987 }
988 assert(PolymorphicBase && "polymorphic class has no apparent vfptr?");
989
990 llvm::Value *Offset =
991 GetVirtualBaseClassOffset(CGF, Value, SrcDecl, PolymorphicBase);
992 llvm::Value *Ptr = CGF.Builder.CreateInBoundsGEP(
993 Value.getElementType(), Value.emitRawPointer(CGF), Offset);
994 CharUnits VBaseAlign =
995 CGF.CGM.getVBaseAlignment(Value.getAlignment(), SrcDecl, PolymorphicBase);
996 return std::make_tuple(Address(Ptr, CGF.Int8Ty, VBaseAlign), Offset,
997 PolymorphicBase);
998}
999
1000bool MicrosoftCXXABI::shouldTypeidBeNullChecked(QualType SrcRecordTy) {
1001 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
1002 return !getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr();
1003}
1004
1005static llvm::CallBase *emitRTtypeidCall(CodeGenFunction &CGF,
1006 llvm::Value *Argument) {
1007 llvm::Type *ArgTypes[] = {CGF.Int8PtrTy};
1008 llvm::FunctionType *FTy =
1009 llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false);
1010 llvm::Value *Args[] = {Argument};
1011 llvm::FunctionCallee Fn = CGF.CGM.CreateRuntimeFunction(FTy, "__RTtypeid");
1012 return CGF.EmitRuntimeCallOrInvoke(Fn, Args);
1013}
1014
1015void MicrosoftCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) {
1016 llvm::CallBase *Call =
1017 emitRTtypeidCall(CGF, llvm::Constant::getNullValue(CGM.VoidPtrTy));
1018 Call->setDoesNotReturn();
1019 CGF.Builder.CreateUnreachable();
1020}
1021
1022llvm::Value *MicrosoftCXXABI::EmitTypeid(CodeGenFunction &CGF,
1023 QualType SrcRecordTy,
1024 Address ThisPtr,
1025 llvm::Type *StdTypeInfoPtrTy) {
1026 std::tie(ThisPtr, std::ignore, std::ignore) =
1027 performBaseAdjustment(CGF, ThisPtr, SrcRecordTy);
1028 llvm::CallBase *Typeid = emitRTtypeidCall(CGF, ThisPtr.emitRawPointer(CGF));
1029 return CGF.Builder.CreateBitCast(Typeid, StdTypeInfoPtrTy);
1030}
1031
1032bool MicrosoftCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
1033 QualType SrcRecordTy) {
1034 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
1035 return SrcIsPtr &&
1036 !getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr();
1037}
1038
1039llvm::Value *MicrosoftCXXABI::emitDynamicCastCall(
1040 CodeGenFunction &CGF, Address This, QualType SrcRecordTy, QualType DestTy,
1041 QualType DestRecordTy, llvm::BasicBlock *CastEnd) {
1042 llvm::Value *SrcRTTI =
1044 llvm::Value *DestRTTI =
1046
1047 llvm::Value *Offset;
1048 std::tie(This, Offset, std::ignore) =
1049 performBaseAdjustment(CGF, This, SrcRecordTy);
1050 llvm::Value *ThisPtr = This.emitRawPointer(CGF);
1051 Offset = CGF.Builder.CreateTrunc(Offset, CGF.Int32Ty);
1052
1053 // PVOID __RTDynamicCast(
1054 // PVOID inptr,
1055 // LONG VfDelta,
1056 // PVOID SrcType,
1057 // PVOID TargetType,
1058 // BOOL isReference)
1059 llvm::Type *ArgTypes[] = {CGF.Int8PtrTy, CGF.Int32Ty, CGF.Int8PtrTy,
1060 CGF.Int8PtrTy, CGF.Int32Ty};
1061 llvm::FunctionCallee Function = CGF.CGM.CreateRuntimeFunction(
1062 llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false),
1063 "__RTDynamicCast");
1064 llvm::Value *Args[] = {
1065 ThisPtr, Offset, SrcRTTI, DestRTTI,
1066 llvm::ConstantInt::get(CGF.Int32Ty, DestTy->isReferenceType())};
1067 return CGF.EmitRuntimeCallOrInvoke(Function, Args);
1068}
1069
1070llvm::Value *MicrosoftCXXABI::emitDynamicCastToVoid(CodeGenFunction &CGF,
1071 Address Value,
1072 QualType SrcRecordTy) {
1073 std::tie(Value, std::ignore, std::ignore) =
1074 performBaseAdjustment(CGF, Value, SrcRecordTy);
1075
1076 // PVOID __RTCastToVoid(
1077 // PVOID inptr)
1078 llvm::Type *ArgTypes[] = {CGF.Int8PtrTy};
1079 llvm::FunctionCallee Function = CGF.CGM.CreateRuntimeFunction(
1080 llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false),
1081 "__RTCastToVoid");
1082 llvm::Value *Args[] = {Value.emitRawPointer(CGF)};
1083 return CGF.EmitRuntimeCall(Function, Args);
1084}
1085
1086bool MicrosoftCXXABI::EmitBadCastCall(CodeGenFunction &CGF) {
1087 return false;
1088}
1089
1090llvm::Value *MicrosoftCXXABI::GetVirtualBaseClassOffset(
1091 CodeGenFunction &CGF, Address This, const CXXRecordDecl *ClassDecl,
1092 const CXXRecordDecl *BaseClassDecl) {
1093 const ASTContext &Context = getContext();
1094 int64_t VBPtrChars =
1095 Context.getASTRecordLayout(ClassDecl).getVBPtrOffset().getQuantity();
1096 llvm::Value *VBPtrOffset = llvm::ConstantInt::get(CGM.PtrDiffTy, VBPtrChars);
1097 CharUnits IntSize = Context.getTypeSizeInChars(Context.IntTy);
1098 CharUnits VBTableChars =
1099 IntSize *
1100 CGM.getMicrosoftVTableContext().getVBTableIndex(ClassDecl, BaseClassDecl);
1101 llvm::Value *VBTableOffset =
1102 llvm::ConstantInt::get(CGM.IntTy, VBTableChars.getQuantity());
1103
1104 llvm::Value *VBPtrToNewBase =
1105 GetVBaseOffsetFromVBPtr(CGF, This, VBPtrOffset, VBTableOffset);
1106 VBPtrToNewBase =
1107 CGF.Builder.CreateSExtOrBitCast(VBPtrToNewBase, CGM.PtrDiffTy);
1108 return CGF.Builder.CreateNSWAdd(VBPtrOffset, VBPtrToNewBase);
1109}
1110
1111bool MicrosoftCXXABI::HasThisReturn(GlobalDecl GD) const {
1112 return isa<CXXConstructorDecl>(GD.getDecl());
1113}
1114
1116 return isa<CXXDestructorDecl>(GD.getDecl()) &&
1117 (GD.getDtorType() == Dtor_Deleting ||
1119}
1120
1121bool MicrosoftCXXABI::hasMostDerivedReturn(GlobalDecl GD) const {
1122 return isDeletingDtor(GD);
1123}
1124
1125static bool isTrivialForMSVC(const CXXRecordDecl *RD, QualType Ty,
1126 CodeGenModule &CGM) {
1127 // On AArch64, HVAs that can be passed in registers can also be returned
1128 // in registers. (Note this is using the MSVC definition of an HVA; see
1129 // isPermittedToBeHomogeneousAggregate().)
1130 const Type *Base = nullptr;
1131 uint64_t NumElts = 0;
1132 if (CGM.getTarget().getTriple().isAArch64() &&
1133 CGM.getABIInfo().isHomogeneousAggregate(Ty, Base, NumElts) &&
1135 return true;
1136 }
1137
1138 // We use the C++14 definition of an aggregate, so we also
1139 // check for:
1140 // No private or protected non static data members.
1141 // No base classes
1142 // No virtual functions
1143 // Additionally, we need to ensure that there is a trivial copy assignment
1144 // operator, a trivial destructor, no user-provided constructors and no
1145 // deleted copy assignment operator.
1146
1147 // We need to cover two cases when checking for a deleted copy assignment
1148 // operator.
1149 //
1150 // struct S { int& r; };
1151 // The above will have an implicit copy assignment operator that is deleted
1152 // and there will not be a `CXXMethodDecl` for the copy assignment operator.
1153 // This is handled by the `needsImplicitCopyAssignment()` check below.
1154 //
1155 // struct S { S& operator=(const S&) = delete; int i; };
1156 // The above will not have an implicit copy assignment operator that is
1157 // deleted but there is a deleted `CXXMethodDecl` for the declared copy
1158 // assignment operator. This is handled by the `isDeleted()` check below.
1159
1160 if (RD->hasProtectedFields() || RD->hasPrivateFields())
1161 return false;
1162 if (RD->getNumBases() > 0)
1163 return false;
1164 if (RD->isPolymorphic())
1165 return false;
1167 return false;
1169 return false;
1170 for (const Decl *D : RD->decls()) {
1171 if (auto *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
1172 if (Ctor->isUserProvided())
1173 return false;
1174 } else if (auto *Template = dyn_cast<FunctionTemplateDecl>(D)) {
1175 if (isa<CXXConstructorDecl>(Template->getTemplatedDecl()))
1176 return false;
1177 } else if (auto *MethodDecl = dyn_cast<CXXMethodDecl>(D)) {
1178 if (MethodDecl->isCopyAssignmentOperator() && MethodDecl->isDeleted())
1179 return false;
1180 }
1181 }
1182 if (RD->hasNonTrivialDestructor())
1183 return false;
1184 return true;
1185}
1186
1187bool MicrosoftCXXABI::classifyReturnType(CGFunctionInfo &FI) const {
1188 const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl();
1189 if (!RD)
1190 return false;
1191
1192 bool isTrivialForABI = RD->canPassInRegisters() &&
1193 isTrivialForMSVC(RD, FI.getReturnType(), CGM);
1194
1195 // MSVC always returns structs indirectly from C++ instance methods.
1196 bool isIndirectReturn = !isTrivialForABI || FI.isInstanceMethod();
1197
1198 if (isIndirectReturn) {
1199 CharUnits Align = CGM.getContext().getTypeAlignInChars(FI.getReturnType());
1200 LangAS SRetAS = CGM.getTargetCodeGenInfo().getSRetAddrSpace(RD);
1201 unsigned AS = CGM.getContext().getTargetAddressSpace(SRetAS);
1202 FI.getReturnInfo() =
1203 ABIArgInfo::getIndirect(Align, /*AddrSpace=*/AS, /*ByVal=*/false);
1204
1205 // MSVC always passes `this` before the `sret` parameter.
1207
1208 // On AArch64, use the `inreg` attribute if the object is considered to not
1209 // be trivially copyable, or if this is an instance method struct return.
1210 FI.getReturnInfo().setInReg(CGM.getTarget().getTriple().isAArch64());
1211
1212 return true;
1213 }
1214
1215 // Otherwise, use the C ABI rules.
1216 return false;
1217}
1218
1219llvm::BasicBlock *
1220MicrosoftCXXABI::EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
1221 const CXXRecordDecl *RD) {
1222 llvm::Value *IsMostDerivedClass = getStructorImplicitParamValue(CGF);
1223 assert(IsMostDerivedClass &&
1224 "ctor for a class with virtual bases must have an implicit parameter");
1225 llvm::Value *IsCompleteObject =
1226 CGF.Builder.CreateIsNotNull(IsMostDerivedClass, "is_complete_object");
1227
1228 llvm::BasicBlock *CallVbaseCtorsBB = CGF.createBasicBlock("ctor.init_vbases");
1229 llvm::BasicBlock *SkipVbaseCtorsBB = CGF.createBasicBlock("ctor.skip_vbases");
1230 CGF.Builder.CreateCondBr(IsCompleteObject,
1231 CallVbaseCtorsBB, SkipVbaseCtorsBB);
1232
1233 CGF.EmitBlock(CallVbaseCtorsBB);
1234
1235 // Fill in the vbtable pointers here.
1236 EmitVBPtrStores(CGF, RD);
1237
1238 // CGF will put the base ctor calls in this basic block for us later.
1239
1240 return SkipVbaseCtorsBB;
1241}
1242
1243llvm::BasicBlock *
1244MicrosoftCXXABI::EmitDtorCompleteObjectHandler(CodeGenFunction &CGF) {
1245 llvm::Value *IsMostDerivedClass = getStructorImplicitParamValue(CGF);
1246 assert(IsMostDerivedClass &&
1247 "ctor for a class with virtual bases must have an implicit parameter");
1248 llvm::Value *IsCompleteObject =
1249 CGF.Builder.CreateIsNotNull(IsMostDerivedClass, "is_complete_object");
1250
1251 llvm::BasicBlock *CallVbaseDtorsBB = CGF.createBasicBlock("Dtor.dtor_vbases");
1252 llvm::BasicBlock *SkipVbaseDtorsBB = CGF.createBasicBlock("Dtor.skip_vbases");
1253 CGF.Builder.CreateCondBr(IsCompleteObject,
1254 CallVbaseDtorsBB, SkipVbaseDtorsBB);
1255
1256 CGF.EmitBlock(CallVbaseDtorsBB);
1257 // CGF will put the base dtor calls in this basic block for us later.
1258
1259 return SkipVbaseDtorsBB;
1260}
1261
1262void MicrosoftCXXABI::initializeHiddenVirtualInheritanceMembers(
1263 CodeGenFunction &CGF, const CXXRecordDecl *RD) {
1264 // In most cases, an override for a vbase virtual method can adjust
1265 // the "this" parameter by applying a constant offset.
1266 // However, this is not enough while a constructor or a destructor of some
1267 // class X is being executed if all the following conditions are met:
1268 // - X has virtual bases, (1)
1269 // - X overrides a virtual method M of a vbase Y, (2)
1270 // - X itself is a vbase of the most derived class.
1271 //
1272 // If (1) and (2) are true, the vtorDisp for vbase Y is a hidden member of X
1273 // which holds the extra amount of "this" adjustment we must do when we use
1274 // the X vftables (i.e. during X ctor or dtor).
1275 // Outside the ctors and dtors, the values of vtorDisps are zero.
1276
1277 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
1278 typedef ASTRecordLayout::VBaseOffsetsMapTy VBOffsets;
1279 const VBOffsets &VBaseMap = Layout.getVBaseOffsetsMap();
1280 CGBuilderTy &Builder = CGF.Builder;
1281
1282 llvm::Value *Int8This = nullptr; // Initialize lazily.
1283
1284 for (const CXXBaseSpecifier &S : RD->vbases()) {
1285 const CXXRecordDecl *VBase = S.getType()->getAsCXXRecordDecl();
1286 auto I = VBaseMap.find(VBase);
1287 assert(I != VBaseMap.end());
1288 if (!I->second.hasVtorDisp())
1289 continue;
1290
1291 llvm::Value *VBaseOffset =
1292 GetVirtualBaseClassOffset(CGF, getThisAddress(CGF), RD, VBase);
1293 uint64_t ConstantVBaseOffset = I->second.VBaseOffset.getQuantity();
1294
1295 // vtorDisp_for_vbase = vbptr[vbase_idx] - offsetof(RD, vbase).
1296 llvm::Value *VtorDispValue = Builder.CreateSub(
1297 VBaseOffset, llvm::ConstantInt::get(CGM.PtrDiffTy, ConstantVBaseOffset),
1298 "vtordisp.value");
1299 VtorDispValue = Builder.CreateTruncOrBitCast(VtorDispValue, CGF.Int32Ty);
1300
1301 if (!Int8This)
1302 Int8This = getThisValue(CGF);
1303
1304 llvm::Value *VtorDispPtr =
1305 Builder.CreateInBoundsGEP(CGF.Int8Ty, Int8This, VBaseOffset);
1306 // vtorDisp is always the 32-bits before the vbase in the class layout.
1307 VtorDispPtr = Builder.CreateConstGEP1_32(CGF.Int8Ty, VtorDispPtr, -4);
1308
1309 Builder.CreateAlignedStore(VtorDispValue, VtorDispPtr,
1311 }
1312}
1313
1315 const CXXMethodDecl *MD) {
1316 CallingConv ExpectedCallingConv = Context.getDefaultCallingConvention(
1317 /*IsVariadic=*/false, /*IsCXXMethod=*/true);
1318 CallingConv ActualCallingConv =
1319 MD->getType()->castAs<FunctionProtoType>()->getCallConv();
1320 return ExpectedCallingConv == ActualCallingConv;
1321}
1322
1323void MicrosoftCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) {
1324 // There's only one constructor type in this ABI.
1325 CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete));
1326
1327 // Exported default constructors either have a simple call-site where they use
1328 // the typical calling convention and have a single 'this' pointer for an
1329 // argument -or- they get a wrapper function which appropriately thunks to the
1330 // real default constructor. This thunk is the default constructor closure.
1331 if (D->hasAttr<DLLExportAttr>() && D->isDefaultConstructor() &&
1332 D->isDefined()) {
1333 if (!hasDefaultCXXMethodCC(getContext(), D) || D->getNumParams() != 0) {
1334 llvm::Function *Fn = getAddrOfCXXCtorClosure(D, Ctor_DefaultClosure);
1335 Fn->setLinkage(llvm::GlobalValue::WeakODRLinkage);
1336 CGM.setGVProperties(Fn, D);
1337 }
1338 }
1339}
1340
1341void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF,
1342 const CXXRecordDecl *RD) {
1343 Address This = getThisAddress(CGF);
1344 This = This.withElementType(CGM.Int8Ty);
1345 const ASTContext &Context = getContext();
1346 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1347
1348 const VBTableGlobals &VBGlobals = enumerateVBTables(RD);
1349 for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) {
1350 const std::unique_ptr<VPtrInfo> &VBT = (*VBGlobals.VBTables)[I];
1351 llvm::GlobalVariable *GV = VBGlobals.Globals[I];
1352 const ASTRecordLayout &SubobjectLayout =
1353 Context.getASTRecordLayout(VBT->IntroducingObject);
1354 CharUnits Offs = VBT->NonVirtualOffset;
1355 Offs += SubobjectLayout.getVBPtrOffset();
1356 if (VBT->getVBaseWithVPtr())
1357 Offs += Layout.getVBaseClassOffset(VBT->getVBaseWithVPtr());
1358 Address VBPtr = CGF.Builder.CreateConstInBoundsByteGEP(This, Offs);
1359 llvm::Value *GVPtr =
1360 CGF.Builder.CreateConstInBoundsGEP2_32(GV->getValueType(), GV, 0, 0);
1361 VBPtr = VBPtr.withElementType(GVPtr->getType());
1362 CGF.Builder.CreateStore(GVPtr, VBPtr);
1363 }
1364}
1365
1366CGCXXABI::AddedStructorArgCounts
1367MicrosoftCXXABI::buildStructorSignature(GlobalDecl GD,
1368 SmallVectorImpl<CanQualType> &ArgTys) {
1369 AddedStructorArgCounts Added;
1370 // TODO: 'for base' flag
1371 if (isa<CXXDestructorDecl>(GD.getDecl()) &&
1372 (GD.getDtorType() == Dtor_Deleting ||
1374 // The scalar deleting destructor takes an implicit int parameter.
1375 ArgTys.push_back(getContext().IntTy);
1376 ++Added.Suffix;
1377 }
1378 auto *CD = dyn_cast<CXXConstructorDecl>(GD.getDecl());
1379 if (!CD)
1380 return Added;
1381
1382 // All parameters are already in place except is_most_derived, which goes
1383 // after 'this' if it's variadic and last if it's not.
1384
1385 const CXXRecordDecl *Class = CD->getParent();
1386 const FunctionProtoType *FPT = CD->getType()->castAs<FunctionProtoType>();
1387 if (Class->getNumVBases()) {
1388 if (FPT->isVariadic()) {
1389 ArgTys.insert(ArgTys.begin() + 1, getContext().IntTy);
1390 ++Added.Prefix;
1391 } else {
1392 ArgTys.push_back(getContext().IntTy);
1393 ++Added.Suffix;
1394 }
1395 }
1396
1397 return Added;
1398}
1399
1400void MicrosoftCXXABI::setCXXDestructorDLLStorage(llvm::GlobalValue *GV,
1401 const CXXDestructorDecl *Dtor,
1402 CXXDtorType DT) const {
1403 // Deleting destructor variants are never imported or exported. Give them the
1404 // default storage class.
1405 if (DT == Dtor_Deleting || DT == Dtor_VectorDeleting) {
1406 GV->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
1407 } else {
1408 const NamedDecl *ND = Dtor;
1409 CGM.setDLLImportDLLExport(GV, ND);
1410 }
1411}
1412
1413llvm::GlobalValue::LinkageTypes MicrosoftCXXABI::getCXXDestructorLinkage(
1414 GVALinkage Linkage, const CXXDestructorDecl *Dtor, CXXDtorType DT) const {
1415 // Internal things are always internal, regardless of attributes. After this,
1416 // we know the thunk is externally visible.
1417 if (Linkage == GVA_Internal)
1418 return llvm::GlobalValue::InternalLinkage;
1419
1420 switch (DT) {
1421 case Dtor_Base:
1422 // The base destructor most closely tracks the user-declared constructor, so
1423 // we delegate back to the normal declarator case.
1424 return CGM.getLLVMLinkageForDeclarator(Dtor, Linkage);
1425 case Dtor_Complete:
1426 // The complete destructor is like an inline function, but it may be
1427 // imported and therefore must be exported as well. This requires changing
1428 // the linkage if a DLL attribute is present.
1429 if (Dtor->hasAttr<DLLExportAttr>())
1430 return llvm::GlobalValue::WeakODRLinkage;
1431 if (Dtor->hasAttr<DLLImportAttr>())
1432 return llvm::GlobalValue::AvailableExternallyLinkage;
1433 return llvm::GlobalValue::LinkOnceODRLinkage;
1434 case Dtor_Deleting:
1435 // Deleting destructors are like inline functions. They have vague linkage
1436 // and are emitted everywhere they are used. They are internal if the class
1437 // is internal.
1438 return llvm::GlobalValue::LinkOnceODRLinkage;
1439 case Dtor_Unified:
1440 llvm_unreachable("MS C++ ABI does not support unified dtors");
1442 // Use the weak, non-ODR linkage for vector deleting destructors to block
1443 // inlining. This enables an MS ABI code-size saving optimization that
1444 // allows us to avoid emitting array deletion code when arrays of a given
1445 // type are not allocated within the final linkage unit.
1446 return llvm::GlobalValue::WeakAnyLinkage;
1447 case Dtor_Comdat:
1448 llvm_unreachable("MS C++ ABI does not support comdat dtors");
1449 }
1450 llvm_unreachable("invalid dtor type");
1451}
1452
1453void MicrosoftCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) {
1454 // The TU defining a dtor is only guaranteed to emit a base destructor. All
1455 // other destructor variants are delegating thunks.
1456 CGM.EmitGlobal(GlobalDecl(D, Dtor_Base));
1457
1458 // If the class is dllexported, emit the complete (vbase) destructor wherever
1459 // the base dtor is emitted.
1460 // FIXME: To match MSVC, this should only be done when the class is exported
1461 // with -fdllexport-inlines enabled.
1462 if (D->getParent()->getNumVBases() > 0 && D->hasAttr<DLLExportAttr>())
1463 CGM.EmitGlobal(GlobalDecl(D, Dtor_Complete));
1464}
1465
1466CharUnits
1467MicrosoftCXXABI::getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) {
1468 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1469
1470 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1471 // Complete destructors take a pointer to the complete object as a
1472 // parameter, thus don't need this adjustment.
1473 if (GD.getDtorType() == Dtor_Complete)
1474 return CharUnits();
1475
1476 // There's no Dtor_Base in vftable but it shares the this adjustment with
1477 // the deleting one, so look it up instead.
1478 GD =
1479 GlobalDecl(DD, CGM.getContext().getTargetInfo().emitVectorDeletingDtors(
1480 CGM.getContext().getLangOpts())
1482 : Dtor_Deleting);
1483 }
1484
1485 MethodVFTableLocation ML =
1487 CharUnits Adjustment = ML.VFPtrOffset;
1488
1489 // Normal virtual instance methods need to adjust from the vfptr that first
1490 // defined the virtual method to the virtual base subobject, but destructors
1491 // do not. The vector deleting destructor thunk applies this adjustment for
1492 // us if necessary.
1493 if (isa<CXXDestructorDecl>(MD))
1494 Adjustment = CharUnits::Zero();
1495
1496 if (ML.VBase) {
1497 const ASTRecordLayout &DerivedLayout =
1498 getContext().getASTRecordLayout(MD->getParent());
1499 Adjustment += DerivedLayout.getVBaseClassOffset(ML.VBase);
1500 }
1501
1502 return Adjustment;
1503}
1504
1505Address MicrosoftCXXABI::adjustThisArgumentForVirtualFunctionCall(
1506 CodeGenFunction &CGF, GlobalDecl GD, Address This,
1507 bool VirtualCall) {
1508 if (!VirtualCall) {
1509 // If the call of a virtual function is not virtual, we just have to
1510 // compensate for the adjustment the virtual function does in its prologue.
1511 CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD);
1512 if (Adjustment.isZero())
1513 return This;
1514
1515 This = This.withElementType(CGF.Int8Ty);
1516 assert(Adjustment.isPositive());
1517 return CGF.Builder.CreateConstByteGEP(This, Adjustment);
1518 }
1519
1520 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1521
1522 GlobalDecl LookupGD = GD;
1523 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1524 // Complete dtors take a pointer to the complete object,
1525 // thus don't need adjustment.
1526 if (GD.getDtorType() == Dtor_Complete)
1527 return This;
1528
1529 // There's only Dtor_Deleting in vftable but it shares the this adjustment
1530 // with the base one, so look up the deleting one instead.
1531 LookupGD =
1532 GlobalDecl(DD, CGM.getContext().getTargetInfo().emitVectorDeletingDtors(
1533 CGM.getContext().getLangOpts())
1535 : Dtor_Deleting);
1536 }
1537 MethodVFTableLocation ML =
1539
1540 CharUnits StaticOffset = ML.VFPtrOffset;
1541
1542 // Base destructors expect 'this' to point to the beginning of the base
1543 // subobject, not the first vfptr that happens to contain the virtual dtor.
1544 // However, we still need to apply the virtual base adjustment.
1545 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
1546 StaticOffset = CharUnits::Zero();
1547
1549 if (ML.VBase) {
1550 Result = Result.withElementType(CGF.Int8Ty);
1551
1552 const CXXRecordDecl *Derived = MD->getParent();
1553 const CXXRecordDecl *VBase = ML.VBase;
1554 llvm::Value *VBaseOffset =
1555 GetVirtualBaseClassOffset(CGF, Result, Derived, VBase);
1556 llvm::Value *VBasePtr = CGF.Builder.CreateInBoundsGEP(
1557 Result.getElementType(), Result.emitRawPointer(CGF), VBaseOffset);
1558 CharUnits VBaseAlign =
1559 CGF.CGM.getVBaseAlignment(Result.getAlignment(), Derived, VBase);
1560 Result = Address(VBasePtr, CGF.Int8Ty, VBaseAlign);
1561 }
1562 if (!StaticOffset.isZero()) {
1563 assert(StaticOffset.isPositive());
1564 Result = Result.withElementType(CGF.Int8Ty);
1565 if (ML.VBase) {
1566 // Non-virtual adjustment might result in a pointer outside the allocated
1567 // object, e.g. if the final overrider class is laid out after the virtual
1568 // base that declares a method in the most derived class.
1569 // FIXME: Update the code that emits this adjustment in thunks prologues.
1570 Result = CGF.Builder.CreateConstByteGEP(Result, StaticOffset);
1571 } else {
1572 Result = CGF.Builder.CreateConstInBoundsByteGEP(Result, StaticOffset);
1573 }
1574 }
1575 return Result;
1576}
1577
1578void MicrosoftCXXABI::addImplicitStructorParams(CodeGenFunction &CGF,
1579 QualType &ResTy,
1580 FunctionArgList &Params) {
1581 ASTContext &Context = getContext();
1582 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
1584 if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) {
1585 auto *IsMostDerived = ImplicitParamDecl::Create(
1586 Context, /*DC=*/nullptr, CGF.CurGD.getDecl()->getLocation(),
1587 &Context.Idents.get("is_most_derived"), Context.IntTy,
1588 ImplicitParamKind::Other);
1589 // The 'most_derived' parameter goes second if the ctor is variadic and last
1590 // if it's not. Dtors can't be variadic.
1591 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
1592 if (FPT->isVariadic())
1593 Params.insert(Params.begin() + 1, IsMostDerived);
1594 else
1595 Params.push_back(IsMostDerived);
1596 getStructorImplicitParamDecl(CGF) = IsMostDerived;
1597 } else if (isDeletingDtor(CGF.CurGD)) {
1598 auto *ShouldDelete = ImplicitParamDecl::Create(
1599 Context, /*DC=*/nullptr, CGF.CurGD.getDecl()->getLocation(),
1600 &Context.Idents.get("should_call_delete"), Context.IntTy,
1601 ImplicitParamKind::Other);
1602 Params.push_back(ShouldDelete);
1603 getStructorImplicitParamDecl(CGF) = ShouldDelete;
1604 }
1605}
1606
1607void MicrosoftCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
1608 // Naked functions have no prolog.
1609 if (CGF.CurFuncDecl && CGF.CurFuncDecl->hasAttr<NakedAttr>())
1610 return;
1611
1612 // Overridden virtual methods of non-primary bases need to adjust the incoming
1613 // 'this' pointer in the prologue. In this hierarchy, C::b will subtract
1614 // sizeof(void*) to adjust from B* to C*:
1615 // struct A { virtual void a(); };
1616 // struct B { virtual void b(); };
1617 // struct C : A, B { virtual void b(); };
1618 //
1619 // Leave the value stored in the 'this' alloca unadjusted, so that the
1620 // debugger sees the unadjusted value. Microsoft debuggers require this, and
1621 // will apply the ThisAdjustment in the method type information.
1622 // FIXME: Do something better for DWARF debuggers, which won't expect this,
1623 // without making our codegen depend on debug info settings.
1624 llvm::Value *This = loadIncomingCXXThis(CGF);
1625 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
1626 if (!CGF.CurFuncIsThunk && MD->isVirtual()) {
1627 CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(CGF.CurGD);
1628 if (!Adjustment.isZero()) {
1629 assert(Adjustment.isPositive());
1630 This = CGF.Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, This,
1631 -Adjustment.getQuantity());
1632 }
1633 }
1634 setCXXABIThisValue(CGF, This);
1635
1636 // If this is a function that the ABI specifies returns 'this', initialize
1637 // the return slot to 'this' at the start of the function.
1638 //
1639 // Unlike the setting of return types, this is done within the ABI
1640 // implementation instead of by clients of CGCXXABI because:
1641 // 1) getThisValue is currently protected
1642 // 2) in theory, an ABI could implement 'this' returns some other way;
1643 // HasThisReturn only specifies a contract, not the implementation
1644 if (HasThisReturn(CGF.CurGD) || hasMostDerivedReturn(CGF.CurGD))
1645 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
1646
1647 if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) {
1648 assert(getStructorImplicitParamDecl(CGF) &&
1649 "no implicit parameter for a constructor with virtual bases?");
1650 getStructorImplicitParamValue(CGF)
1651 = CGF.Builder.CreateLoad(
1652 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)),
1653 "is_most_derived");
1654 }
1655
1656 if (isDeletingDtor(CGF.CurGD)) {
1657 assert(getStructorImplicitParamDecl(CGF) &&
1658 "no implicit parameter for a deleting destructor?");
1659 getStructorImplicitParamValue(CGF)
1660 = CGF.Builder.CreateLoad(
1661 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)),
1662 "should_call_delete");
1663 }
1664}
1665
1666CGCXXABI::AddedStructorArgs MicrosoftCXXABI::getImplicitConstructorArgs(
1667 CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type,
1668 bool ForVirtualBase, bool Delegating) {
1669 assert(Type == Ctor_Complete || Type == Ctor_Base);
1670
1671 // Check if we need a 'most_derived' parameter.
1672 if (!D->getParent()->getNumVBases())
1673 return AddedStructorArgs{};
1674
1675 // Add the 'most_derived' argument second if we are variadic or last if not.
1676 const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
1677 llvm::Value *MostDerivedArg;
1678 if (Delegating) {
1679 MostDerivedArg = getStructorImplicitParamValue(CGF);
1680 } else {
1681 MostDerivedArg = llvm::ConstantInt::get(CGM.Int32Ty, Type == Ctor_Complete);
1682 }
1683 if (FPT->isVariadic()) {
1684 return AddedStructorArgs::prefix({{MostDerivedArg, getContext().IntTy}});
1685 }
1686 return AddedStructorArgs::suffix({{MostDerivedArg, getContext().IntTy}});
1687}
1688
1689llvm::Value *MicrosoftCXXABI::getCXXDestructorImplicitParam(
1690 CodeGenFunction &CGF, const CXXDestructorDecl *DD, CXXDtorType Type,
1691 bool ForVirtualBase, bool Delegating) {
1692 return nullptr;
1693}
1694
1695void MicrosoftCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
1696 const CXXDestructorDecl *DD,
1697 CXXDtorType Type, bool ForVirtualBase,
1698 bool Delegating, Address This,
1699 QualType ThisTy) {
1700 // Use the base destructor variant in place of the complete destructor variant
1701 // if the class has no virtual bases. This effectively implements some of the
1702 // -mconstructor-aliases optimization, but as part of the MS C++ ABI.
1703 if (Type == Dtor_Complete && DD->getParent()->getNumVBases() == 0)
1704 Type = Dtor_Base;
1705
1706 GlobalDecl GD(DD, Type);
1707 CGCallee Callee = CGCallee::forDirect(CGM.getAddrOfCXXStructor(GD), GD);
1708
1709 if (DD->isVirtual()) {
1710 assert(Type != CXXDtorType::Dtor_Deleting &&
1711 "The deleting destructor should only be called via a virtual call");
1712 This = adjustThisArgumentForVirtualFunctionCall(CGF, GlobalDecl(DD, Type),
1713 This, false);
1714 }
1715
1716 llvm::BasicBlock *BaseDtorEndBB = nullptr;
1717 if (ForVirtualBase && isa<CXXConstructorDecl>(CGF.CurCodeDecl)) {
1718 BaseDtorEndBB = EmitDtorCompleteObjectHandler(CGF);
1719 }
1720
1721 llvm::Value *Implicit =
1722 getCXXDestructorImplicitParam(CGF, DD, Type, ForVirtualBase,
1723 Delegating); // = nullptr
1724 CGF.EmitCXXDestructorCall(GD, Callee, CGF.getAsNaturalPointerTo(This, ThisTy),
1725 ThisTy,
1726 /*ImplicitParam=*/Implicit,
1727 /*ImplicitParamTy=*/QualType(), /*E=*/nullptr);
1728 if (BaseDtorEndBB) {
1729 // Complete object handler should continue to be the remaining
1730 CGF.Builder.CreateBr(BaseDtorEndBB);
1731 CGF.EmitBlock(BaseDtorEndBB);
1732 }
1733}
1734
1735void MicrosoftCXXABI::emitVTableTypeMetadata(const VPtrInfo &Info,
1736 const CXXRecordDecl *RD,
1737 llvm::GlobalVariable *VTable) {
1738 // Emit type metadata on vtables with LTO or IR instrumentation.
1739 // In IR instrumentation, the type metadata could be used to find out vtable
1740 // definitions (for type profiling) among all global variables.
1741 if (!CGM.getCodeGenOpts().LTOUnit &&
1743 return;
1744
1745 // TODO: Should VirtualFunctionElimination also be supported here?
1746 // See similar handling in CodeGenModule::EmitVTableTypeMetadata.
1747 if (CGM.getCodeGenOpts().WholeProgramVTables) {
1748 llvm::DenseSet<const CXXRecordDecl *> Visited;
1749 llvm::GlobalObject::VCallVisibility TypeVis =
1750 CGM.GetVCallVisibilityLevel(RD, Visited);
1751 if (TypeVis != llvm::GlobalObject::VCallVisibilityPublic)
1752 VTable->setVCallVisibilityMetadata(TypeVis);
1753 }
1754
1755 // The location of the first virtual function pointer in the virtual table,
1756 // aka the "address point" on Itanium. This is at offset 0 if RTTI is
1757 // disabled, or sizeof(void*) if RTTI is enabled.
1758 CharUnits AddressPoint =
1759 getContext().getLangOpts().RTTIData
1760 ? getContext().toCharUnitsFromBits(
1761 getContext().getTargetInfo().getPointerWidth(LangAS::Default))
1762 : CharUnits::Zero();
1763
1764 if (Info.PathToIntroducingObject.empty()) {
1765 CGM.AddVTableTypeMetadata(VTable, AddressPoint, RD);
1766 return;
1767 }
1768
1769 // Add a bitset entry for the least derived base belonging to this vftable.
1770 CGM.AddVTableTypeMetadata(VTable, AddressPoint,
1771 Info.PathToIntroducingObject.back());
1772
1773 // Add a bitset entry for each derived class that is laid out at the same
1774 // offset as the least derived base.
1775 for (unsigned I = Info.PathToIntroducingObject.size() - 1; I != 0; --I) {
1776 const CXXRecordDecl *DerivedRD = Info.PathToIntroducingObject[I - 1];
1777 const CXXRecordDecl *BaseRD = Info.PathToIntroducingObject[I];
1778
1779 const ASTRecordLayout &Layout =
1780 getContext().getASTRecordLayout(DerivedRD);
1781 CharUnits Offset;
1782 auto VBI = Layout.getVBaseOffsetsMap().find(BaseRD);
1783 if (VBI == Layout.getVBaseOffsetsMap().end())
1784 Offset = Layout.getBaseClassOffset(BaseRD);
1785 else
1786 Offset = VBI->second.VBaseOffset;
1787 if (!Offset.isZero())
1788 return;
1789 CGM.AddVTableTypeMetadata(VTable, AddressPoint, DerivedRD);
1790 }
1791
1792 // Finally do the same for the most derived class.
1793 if (Info.FullOffsetInMDC.isZero())
1794 CGM.AddVTableTypeMetadata(VTable, AddressPoint, RD);
1795}
1796
1797void MicrosoftCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT,
1798 const CXXRecordDecl *RD) {
1799 MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext();
1800 const VPtrInfoVector &VFPtrs = VFTContext.getVFPtrOffsets(RD);
1801
1802 for (const std::unique_ptr<VPtrInfo>& Info : VFPtrs) {
1803 llvm::GlobalVariable *VTable = getAddrOfVTable(RD, Info->FullOffsetInMDC);
1804 if (VTable->hasInitializer())
1805 continue;
1806
1807 const VTableLayout &VTLayout =
1808 VFTContext.getVFTableLayout(RD, Info->FullOffsetInMDC);
1809
1810 llvm::Constant *RTTI = nullptr;
1811 if (any_of(VTLayout.vtable_components(),
1812 [](const VTableComponent &VTC) { return VTC.isRTTIKind(); }))
1813 RTTI = getMSCompleteObjectLocator(RD, *Info);
1814
1815 ConstantInitBuilder builder(CGM);
1816 auto components = builder.beginStruct();
1817 CGVT.createVTableInitializer(components, VTLayout, RTTI,
1818 VTable->hasLocalLinkage());
1819 components.finishAndSetAsInitializer(VTable);
1820
1821 emitVTableTypeMetadata(*Info, RD, VTable);
1822 }
1823}
1824
1825bool MicrosoftCXXABI::isVirtualOffsetNeededForVTableField(
1826 CodeGenFunction &CGF, CodeGenFunction::VPtr Vptr) {
1827 return Vptr.NearestVBase != nullptr;
1828}
1829
1830llvm::Value *MicrosoftCXXABI::getVTableAddressPointInStructor(
1831 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1832 const CXXRecordDecl *NearestVBase) {
1833 llvm::Constant *VTableAddressPoint = getVTableAddressPoint(Base, VTableClass);
1834 if (!VTableAddressPoint) {
1835 assert(Base.getBase()->getNumVBases() &&
1836 !getContext().getASTRecordLayout(Base.getBase()).hasOwnVFPtr());
1837 }
1838 return VTableAddressPoint;
1839}
1840
1842 const CXXRecordDecl *RD, const VPtrInfo &VFPtr,
1843 SmallString<256> &Name) {
1844 llvm::raw_svector_ostream Out(Name);
1845 MangleContext.mangleCXXVFTable(RD, VFPtr.MangledPath, Out);
1846}
1847
1848llvm::Constant *
1849MicrosoftCXXABI::getVTableAddressPoint(BaseSubobject Base,
1850 const CXXRecordDecl *VTableClass) {
1851 (void)getAddrOfVTable(VTableClass, Base.getBaseOffset());
1852 VFTableIdTy ID(VTableClass, Base.getBaseOffset());
1853 return VFTablesMap[ID];
1854}
1855
1856llvm::GlobalVariable *MicrosoftCXXABI::getAddrOfVTable(const CXXRecordDecl *RD,
1857 CharUnits VPtrOffset) {
1858 // getAddrOfVTable may return 0 if asked to get an address of a vtable which
1859 // shouldn't be used in the given record type. We want to cache this result in
1860 // VFTablesMap, thus a simple zero check is not sufficient.
1861
1862 VFTableIdTy ID(RD, VPtrOffset);
1863 auto [I, Inserted] = VTablesMap.try_emplace(ID);
1864 if (!Inserted)
1865 return I->second;
1866
1867 llvm::GlobalVariable *&VTable = I->second;
1868
1869 MicrosoftVTableContext &VTContext = CGM.getMicrosoftVTableContext();
1870 const VPtrInfoVector &VFPtrs = VTContext.getVFPtrOffsets(RD);
1871
1872 if (DeferredVFTables.insert(RD).second) {
1873 // We haven't processed this record type before.
1874 // Queue up this vtable for possible deferred emission.
1875 CGM.addDeferredVTable(RD);
1876
1877#ifndef NDEBUG
1878 // Create all the vftables at once in order to make sure each vftable has
1879 // a unique mangled name.
1880 llvm::StringSet<> ObservedMangledNames;
1881 for (const auto &VFPtr : VFPtrs) {
1882 SmallString<256> Name;
1883 mangleVFTableName(getMangleContext(), RD, *VFPtr, Name);
1884 if (!ObservedMangledNames.insert(Name.str()).second)
1885 llvm_unreachable("Already saw this mangling before?");
1886 }
1887#endif
1888 }
1889
1890 const std::unique_ptr<VPtrInfo> *VFPtrI =
1891 llvm::find_if(VFPtrs, [&](const std::unique_ptr<VPtrInfo> &VPI) {
1892 return VPI->FullOffsetInMDC == VPtrOffset;
1893 });
1894 if (VFPtrI == VFPtrs.end()) {
1895 VFTablesMap[ID] = nullptr;
1896 return nullptr;
1897 }
1898 const std::unique_ptr<VPtrInfo> &VFPtr = *VFPtrI;
1899
1900 SmallString<256> VFTableName;
1901 mangleVFTableName(getMangleContext(), RD, *VFPtr, VFTableName);
1902
1903 // Classes marked __declspec(dllimport) need vftables generated on the
1904 // import-side in order to support features like constexpr. No other
1905 // translation unit relies on the emission of the local vftable, translation
1906 // units are expected to generate them as needed.
1907 //
1908 // Because of this unique behavior, we maintain this logic here instead of
1909 // getVTableLinkage.
1910 llvm::GlobalValue::LinkageTypes VFTableLinkage =
1911 RD->hasAttr<DLLImportAttr>() ? llvm::GlobalValue::LinkOnceODRLinkage
1912 : CGM.getVTableLinkage(RD);
1913 bool VFTableComesFromAnotherTU =
1914 llvm::GlobalValue::isAvailableExternallyLinkage(VFTableLinkage) ||
1915 llvm::GlobalValue::isExternalLinkage(VFTableLinkage);
1916 bool VTableAliasIsRequred =
1917 !VFTableComesFromAnotherTU && getContext().getLangOpts().RTTIData;
1918
1919 if (llvm::GlobalValue *VFTable =
1920 CGM.getModule().getNamedGlobal(VFTableName)) {
1921 VFTablesMap[ID] = VFTable;
1922 VTable = VTableAliasIsRequred
1924 cast<llvm::GlobalAlias>(VFTable)->getAliaseeObject())
1925 : cast<llvm::GlobalVariable>(VFTable);
1926 return VTable;
1927 }
1928
1929 const VTableLayout &VTLayout =
1930 VTContext.getVFTableLayout(RD, VFPtr->FullOffsetInMDC);
1931 llvm::GlobalValue::LinkageTypes VTableLinkage =
1932 VTableAliasIsRequred ? llvm::GlobalValue::PrivateLinkage : VFTableLinkage;
1933
1934 StringRef VTableName = VTableAliasIsRequred ? StringRef() : VFTableName.str();
1935
1936 llvm::Type *VTableType = CGM.getVTables().getVTableType(VTLayout);
1937
1938 // Create a backing variable for the contents of VTable. The VTable may
1939 // or may not include space for a pointer to RTTI data.
1940 llvm::GlobalValue *VFTable;
1941 VTable = new llvm::GlobalVariable(CGM.getModule(), VTableType,
1942 /*isConstant=*/true, VTableLinkage,
1943 /*Initializer=*/nullptr, VTableName);
1944 VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1945
1946 llvm::Comdat *C = nullptr;
1947 if (!VFTableComesFromAnotherTU &&
1948 llvm::GlobalValue::isWeakForLinker(VFTableLinkage))
1949 C = CGM.getModule().getOrInsertComdat(VFTableName.str());
1950
1951 // Only insert a pointer into the VFTable for RTTI data if we are not
1952 // importing it. We never reference the RTTI data directly so there is no
1953 // need to make room for it.
1954 if (VTableAliasIsRequred) {
1955 llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.Int32Ty, 0),
1956 llvm::ConstantInt::get(CGM.Int32Ty, 0),
1957 llvm::ConstantInt::get(CGM.Int32Ty, 1)};
1958 // Create a GEP which points just after the first entry in the VFTable,
1959 // this should be the location of the first virtual method.
1960 llvm::Constant *VTableGEP = llvm::ConstantExpr::getInBoundsGetElementPtr(
1961 VTable->getValueType(), VTable, GEPIndices);
1962 if (llvm::GlobalValue::isWeakForLinker(VFTableLinkage)) {
1963 VFTableLinkage = llvm::GlobalValue::ExternalLinkage;
1964 if (C)
1965 C->setSelectionKind(llvm::Comdat::Largest);
1966 }
1967 VFTable = llvm::GlobalAlias::create(CGM.Int8PtrTy,
1968 /*AddressSpace=*/0, VFTableLinkage,
1969 VFTableName.str(), VTableGEP,
1970 &CGM.getModule());
1971 VFTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1972 } else {
1973 // We don't need a GlobalAlias to be a symbol for the VTable if we won't
1974 // be referencing any RTTI data.
1975 // The GlobalVariable will end up being an appropriate definition of the
1976 // VFTable.
1977 VFTable = VTable;
1978 }
1979 if (C)
1980 VTable->setComdat(C);
1981
1982 if (RD->hasAttr<DLLExportAttr>())
1983 VFTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
1984
1985 VFTablesMap[ID] = VFTable;
1986 return VTable;
1987}
1988
1989CGCallee MicrosoftCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF,
1990 GlobalDecl GD,
1991 Address This,
1992 llvm::Type *Ty,
1993 SourceLocation Loc) {
1994 CGBuilderTy &Builder = CGF.Builder;
1995
1996 Ty = CGF.DefaultPtrTy;
1997 Address VPtr =
1998 adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true);
1999
2000 auto *MethodDecl = cast<CXXMethodDecl>(GD.getDecl());
2001 llvm::Value *VTable =
2002 CGF.GetVTablePtr(VPtr, CGF.DefaultPtrTy, MethodDecl->getParent());
2003
2004 MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext();
2005 MethodVFTableLocation ML = VFTContext.getMethodVFTableLocation(GD);
2006
2007 // Compute the identity of the most derived class whose virtual table is
2008 // located at the MethodVFTableLocation ML.
2009 auto getObjectWithVPtr = [&] {
2010 return llvm::find_if(VFTContext.getVFPtrOffsets(
2011 ML.VBase ? ML.VBase : MethodDecl->getParent()),
2012 [&](const std::unique_ptr<VPtrInfo> &Info) {
2013 return Info->FullOffsetInMDC == ML.VFPtrOffset;
2014 })
2015 ->get()
2016 ->ObjectWithVPtr;
2017 };
2018
2019 llvm::Value *VFunc;
2020 if (CGF.ShouldEmitVTableTypeCheckedLoad(MethodDecl->getParent())) {
2021 VFunc = CGF.EmitVTableTypeCheckedLoad(
2022 getObjectWithVPtr(), VTable, Ty,
2023 ML.Index *
2024 CGM.getContext().getTargetInfo().getPointerWidth(LangAS::Default) /
2025 8);
2026 } else {
2027 if (CGM.getCodeGenOpts().PrepareForLTO)
2028 CGF.EmitTypeMetadataCodeForVCall(getObjectWithVPtr(), VTable, Loc);
2029
2030 llvm::Value *VFuncPtr =
2031 Builder.CreateConstInBoundsGEP1_64(Ty, VTable, ML.Index, "vfn");
2032 VFunc = Builder.CreateAlignedLoad(Ty, VFuncPtr, CGF.getPointerAlign());
2033 }
2034
2035 CGCallee Callee(GD, VFunc);
2036 return Callee;
2037}
2038
2039llvm::Value *MicrosoftCXXABI::EmitVirtualDestructorCall(
2040 CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType,
2041 Address This, DeleteOrMemberCallExpr E, llvm::CallBase **CallOrInvoke) {
2042 auto *CE = dyn_cast<const CXXMemberCallExpr *>(E);
2043 auto *D = dyn_cast<const CXXDeleteExpr *>(E);
2044 assert((CE != nullptr) ^ (D != nullptr));
2045 assert(CE == nullptr || CE->arg_begin() == CE->arg_end());
2046 assert(DtorType == Dtor_VectorDeleting || DtorType == Dtor_Complete ||
2047 DtorType == Dtor_Deleting);
2048
2049 // We have only one destructor in the vftable but can get both behaviors
2050 // by passing an implicit int parameter.
2051 ASTContext &Context = getContext();
2052 bool VectorDeletingDtorsEnabled =
2054 GlobalDecl GD(Dtor, VectorDeletingDtorsEnabled ? Dtor_VectorDeleting
2055 : Dtor_Deleting);
2056 const CGFunctionInfo *FInfo =
2058 llvm::FunctionType *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo);
2059 CGCallee Callee = CGCallee::forVirtual(CE, GD, This, Ty);
2060
2061 bool IsDeleting = DtorType == Dtor_Deleting;
2062 bool IsArrayDelete = D && D->isArrayForm() && VectorDeletingDtorsEnabled;
2063 bool IsGlobalDelete = D && D->isGlobalDelete() &&
2065 Context.getLangOpts());
2066 llvm::Value *ImplicitParam =
2067 CGF.Builder.getInt32((IsDeleting ? 1 : 0) | (IsGlobalDelete ? 4 : 0) |
2068 (IsArrayDelete ? 2 : 0));
2069
2070 QualType ThisTy;
2071 if (CE) {
2072 ThisTy = CE->getObjectType();
2073 } else {
2074 ThisTy = D->getDestroyedType();
2075 }
2076
2077 while (const ArrayType *ATy = Context.getAsArrayType(ThisTy))
2078 ThisTy = ATy->getElementType();
2079
2080 This = adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true);
2081 RValue RV =
2082 CGF.EmitCXXDestructorCall(GD, Callee, This.emitRawPointer(CGF), ThisTy,
2083 ImplicitParam, Context.IntTy, CE, CallOrInvoke);
2084 return RV.getScalarVal();
2085}
2086
2087const VBTableGlobals &
2088MicrosoftCXXABI::enumerateVBTables(const CXXRecordDecl *RD) {
2089 // At this layer, we can key the cache off of a single class, which is much
2090 // easier than caching each vbtable individually.
2091 auto [Entry, Added] = VBTablesMap.try_emplace(RD);
2092 VBTableGlobals &VBGlobals = Entry->second;
2093 if (!Added)
2094 return VBGlobals;
2095
2096 MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext();
2097 VBGlobals.VBTables = &Context.enumerateVBTables(RD);
2098
2099 // Cache the globals for all vbtables so we don't have to recompute the
2100 // mangled names.
2101 llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD);
2102 for (VPtrInfoVector::const_iterator I = VBGlobals.VBTables->begin(),
2103 E = VBGlobals.VBTables->end();
2104 I != E; ++I) {
2105 VBGlobals.Globals.push_back(getAddrOfVBTable(**I, RD, Linkage));
2106 }
2107
2108 return VBGlobals;
2109}
2110
2111llvm::Function *
2112MicrosoftCXXABI::EmitVirtualMemPtrThunk(const CXXMethodDecl *MD,
2113 const MethodVFTableLocation &ML) {
2114 assert(!isa<CXXConstructorDecl>(MD) && !isa<CXXDestructorDecl>(MD) &&
2115 "can't form pointers to ctors or virtual dtors");
2116
2117 // Calculate the mangled name.
2118 SmallString<256> ThunkName;
2119 llvm::raw_svector_ostream Out(ThunkName);
2120 getMangleContext().mangleVirtualMemPtrThunk(MD, ML, Out);
2121
2122 // If the thunk has been generated previously, just return it.
2123 if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(ThunkName))
2124 return cast<llvm::Function>(GV);
2125
2126 // Create the llvm::Function.
2127 const CGFunctionInfo &FnInfo =
2129 llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(FnInfo);
2130 llvm::Function *ThunkFn =
2131 llvm::Function::Create(ThunkTy, llvm::Function::ExternalLinkage,
2132 ThunkName.str(), &CGM.getModule());
2133 assert(ThunkFn->getName() == ThunkName && "name was uniqued!");
2134
2135 ThunkFn->setLinkage(MD->isExternallyVisible()
2136 ? llvm::GlobalValue::LinkOnceODRLinkage
2137 : llvm::GlobalValue::InternalLinkage);
2138 if (MD->isExternallyVisible())
2139 ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(ThunkFn->getName()));
2140
2141 CGM.SetLLVMFunctionAttributes(MD, FnInfo, ThunkFn, /*IsThunk=*/false);
2143
2144 // Add the "thunk" attribute so that LLVM knows that the return type is
2145 // meaningless. These thunks can be used to call functions with differing
2146 // return types, and the caller is required to cast the prototype
2147 // appropriately to extract the correct value.
2148 ThunkFn->addFnAttr("thunk");
2149
2150 // These thunks can be compared, so they are not unnamed.
2151 ThunkFn->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::None);
2152
2153 // Start codegen.
2154 CodeGenFunction CGF(CGM);
2155 CGF.CurGD = GlobalDecl(MD);
2156 CGF.CurFuncIsThunk = true;
2157
2158 // Build FunctionArgs, but only include the implicit 'this' parameter
2159 // declaration.
2160 FunctionArgList FunctionArgs;
2161 buildThisParam(CGF, FunctionArgs);
2162
2163 // Start defining the function.
2164 CGF.StartFunction(GlobalDecl(), FnInfo.getReturnType(), ThunkFn, FnInfo,
2165 FunctionArgs, MD->getLocation(), SourceLocation());
2166
2167 ApplyDebugLocation AL(CGF, MD->getLocation());
2168 setCXXABIThisValue(CGF, loadIncomingCXXThis(CGF));
2169
2170 // Load the vfptr and then callee from the vftable. The callee should have
2171 // adjusted 'this' so that the vfptr is at offset zero.
2172 llvm::Type *ThunkPtrTy = CGF.DefaultPtrTy;
2173 llvm::Value *VTable =
2174 CGF.GetVTablePtr(getThisAddress(CGF), CGF.DefaultPtrTy, MD->getParent());
2175
2176 llvm::Value *VFuncPtr = CGF.Builder.CreateConstInBoundsGEP1_64(
2177 ThunkPtrTy, VTable, ML.Index, "vfn");
2178 llvm::Value *Callee =
2179 CGF.Builder.CreateAlignedLoad(ThunkPtrTy, VFuncPtr, CGF.getPointerAlign());
2180
2181 CGF.EmitMustTailThunk(MD, getThisValue(CGF), {ThunkTy, Callee});
2182
2183 return ThunkFn;
2184}
2185
2186void MicrosoftCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) {
2187 const VBTableGlobals &VBGlobals = enumerateVBTables(RD);
2188 for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) {
2189 const std::unique_ptr<VPtrInfo>& VBT = (*VBGlobals.VBTables)[I];
2190 llvm::GlobalVariable *GV = VBGlobals.Globals[I];
2191 if (GV->isDeclaration())
2192 emitVBTableDefinition(*VBT, RD, GV);
2193 }
2194}
2195
2196llvm::GlobalVariable *
2197MicrosoftCXXABI::getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD,
2198 llvm::GlobalVariable::LinkageTypes Linkage) {
2199 SmallString<256> OutName;
2200 llvm::raw_svector_ostream Out(OutName);
2201 getMangleContext().mangleCXXVBTable(RD, VBT.MangledPath, Out);
2202 StringRef Name = OutName.str();
2203
2204 llvm::ArrayType *VBTableType =
2205 llvm::ArrayType::get(CGM.IntTy, 1 + VBT.ObjectWithVPtr->getNumVBases());
2206
2207 assert(!CGM.getModule().getNamedGlobal(Name) &&
2208 "vbtable with this name already exists: mangling bug?");
2209 CharUnits Alignment =
2211 llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
2212 Name, VBTableType, Linkage, Alignment.getAsAlign());
2213 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2214
2215 if (RD->hasAttr<DLLImportAttr>())
2216 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
2217 else if (RD->hasAttr<DLLExportAttr>())
2218 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
2219
2220 if (!GV->hasExternalLinkage())
2221 emitVBTableDefinition(VBT, RD, GV);
2222
2223 return GV;
2224}
2225
2226void MicrosoftCXXABI::emitVBTableDefinition(const VPtrInfo &VBT,
2227 const CXXRecordDecl *RD,
2228 llvm::GlobalVariable *GV) const {
2229 const CXXRecordDecl *ObjectWithVPtr = VBT.ObjectWithVPtr;
2230
2231 assert(RD->getNumVBases() && ObjectWithVPtr->getNumVBases() &&
2232 "should only emit vbtables for classes with vbtables");
2233
2234 const ASTRecordLayout &BaseLayout =
2235 getContext().getASTRecordLayout(VBT.IntroducingObject);
2236 const ASTRecordLayout &DerivedLayout = getContext().getASTRecordLayout(RD);
2237
2238 SmallVector<llvm::Constant *, 4> Offsets(1 + ObjectWithVPtr->getNumVBases(),
2239 nullptr);
2240
2241 // The offset from ObjectWithVPtr's vbptr to itself always leads.
2242 CharUnits VBPtrOffset = BaseLayout.getVBPtrOffset();
2243 Offsets[0] =
2244 llvm::ConstantInt::getSigned(CGM.IntTy, -VBPtrOffset.getQuantity());
2245
2246 MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext();
2247 for (const auto &I : ObjectWithVPtr->vbases()) {
2248 const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl();
2249 CharUnits Offset = DerivedLayout.getVBaseClassOffset(VBase);
2250 assert(!Offset.isNegative());
2251
2252 // Make it relative to the subobject vbptr.
2253 CharUnits CompleteVBPtrOffset = VBT.NonVirtualOffset + VBPtrOffset;
2254 if (VBT.getVBaseWithVPtr())
2255 CompleteVBPtrOffset +=
2256 DerivedLayout.getVBaseClassOffset(VBT.getVBaseWithVPtr());
2257 Offset -= CompleteVBPtrOffset;
2258
2259 unsigned VBIndex = Context.getVBTableIndex(ObjectWithVPtr, VBase);
2260 assert(Offsets[VBIndex] == nullptr && "The same vbindex seen twice?");
2261 Offsets[VBIndex] =
2262 llvm::ConstantInt::getSigned(CGM.IntTy, Offset.getQuantity());
2263 }
2264
2265 assert(Offsets.size() ==
2266 cast<llvm::ArrayType>(GV->getValueType())->getNumElements());
2267 llvm::ArrayType *VBTableType =
2268 llvm::ArrayType::get(CGM.IntTy, Offsets.size());
2269 llvm::Constant *Init = llvm::ConstantArray::get(VBTableType, Offsets);
2270 GV->setInitializer(Init);
2271
2272 if (RD->hasAttr<DLLImportAttr>())
2273 GV->setLinkage(llvm::GlobalVariable::AvailableExternallyLinkage);
2274}
2275
2276llvm::Value *MicrosoftCXXABI::performThisAdjustment(
2277 CodeGenFunction &CGF, Address This,
2278 const CXXRecordDecl * /*UnadjustedClass*/, const ThunkInfo &TI) {
2279 const ThisAdjustment &TA = TI.This;
2280 if (TA.isEmpty())
2281 return This.emitRawPointer(CGF);
2282
2283 This = This.withElementType(CGF.Int8Ty);
2284
2285 llvm::Value *V;
2286 if (TA.Virtual.isEmpty()) {
2287 V = This.emitRawPointer(CGF);
2288 } else {
2289 assert(TA.Virtual.Microsoft.VtordispOffset < 0);
2290 // Adjust the this argument based on the vtordisp value.
2291 Address VtorDispPtr =
2294 VtorDispPtr = VtorDispPtr.withElementType(CGF.Int32Ty);
2295 llvm::Value *VtorDisp = CGF.Builder.CreateLoad(VtorDispPtr, "vtordisp");
2296 V = CGF.Builder.CreateGEP(This.getElementType(), This.emitRawPointer(CGF),
2297 CGF.Builder.CreateNeg(VtorDisp));
2298
2299 // Unfortunately, having applied the vtordisp means that we no
2300 // longer really have a known alignment for the vbptr step.
2301 // We'll assume the vbptr is pointer-aligned.
2302
2303 if (TA.Virtual.Microsoft.VBPtrOffset) {
2304 // If the final overrider is defined in a virtual base other than the one
2305 // that holds the vfptr, we have to use a vtordispex thunk which looks up
2306 // the vbtable of the derived class.
2307 assert(TA.Virtual.Microsoft.VBPtrOffset > 0);
2308 assert(TA.Virtual.Microsoft.VBOffsetOffset >= 0);
2309 llvm::Value *VBPtr;
2310 llvm::Value *VBaseOffset = GetVBaseOffsetFromVBPtr(
2311 CGF, Address(V, CGF.Int8Ty, CGF.getPointerAlign()),
2313 TA.Virtual.Microsoft.VBOffsetOffset, &VBPtr);
2314 V = CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, VBPtr, VBaseOffset);
2315 }
2316 }
2317
2318 if (TA.NonVirtual) {
2319 // Non-virtual adjustment might result in a pointer outside the allocated
2320 // object, e.g. if the final overrider class is laid out after the virtual
2321 // base that declares a method in the most derived class.
2322 V = CGF.Builder.CreateConstGEP1_32(CGF.Int8Ty, V, TA.NonVirtual);
2323 }
2324
2325 // Don't need to bitcast back, the call CodeGen will handle this.
2326 return V;
2327}
2328
2329llvm::Value *MicrosoftCXXABI::performReturnAdjustment(
2330 CodeGenFunction &CGF, Address Ret,
2331 const CXXRecordDecl * /*UnadjustedClass*/, const ReturnAdjustment &RA) {
2332
2333 if (RA.isEmpty())
2334 return Ret.emitRawPointer(CGF);
2335
2336 Ret = Ret.withElementType(CGF.Int8Ty);
2337
2338 llvm::Value *V = Ret.emitRawPointer(CGF);
2339 if (RA.Virtual.Microsoft.VBIndex) {
2340 assert(RA.Virtual.Microsoft.VBIndex > 0);
2341 int32_t IntSize = CGF.getIntSize().getQuantity();
2342 llvm::Value *VBPtr;
2343 llvm::Value *VBaseOffset =
2344 GetVBaseOffsetFromVBPtr(CGF, Ret, RA.Virtual.Microsoft.VBPtrOffset,
2345 IntSize * RA.Virtual.Microsoft.VBIndex, &VBPtr);
2346 V = CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, VBPtr, VBaseOffset);
2347 }
2348
2349 if (RA.NonVirtual)
2350 V = CGF.Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, V, RA.NonVirtual);
2351
2352 return V;
2353}
2354
2355bool MicrosoftCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr,
2356 QualType elementType) {
2357 // Microsoft seems to completely ignore the possibility of a
2358 // two-argument usual deallocation function.
2359 return elementType.isDestructedType();
2360}
2361
2362bool MicrosoftCXXABI::requiresArrayCookie(const CXXNewExpr *expr) {
2363 // Microsoft seems to completely ignore the possibility of a
2364 // two-argument usual deallocation function.
2365 return expr->getAllocatedType().isDestructedType();
2366}
2367
2368CharUnits MicrosoftCXXABI::getArrayCookieSizeImpl(QualType type) {
2369 // The array cookie is always a size_t; we then pad that out to the
2370 // alignment of the element type.
2371 ASTContext &Ctx = getContext();
2372 return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()),
2374}
2375
2376llvm::Value *MicrosoftCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
2377 Address allocPtr,
2378 CharUnits cookieSize) {
2379 Address numElementsPtr = allocPtr.withElementType(CGF.SizeTy);
2380 return CGF.Builder.CreateLoad(numElementsPtr);
2381}
2382
2383Address MicrosoftCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
2384 Address newPtr,
2385 llvm::Value *numElements,
2386 const CXXNewExpr *expr,
2387 QualType elementType) {
2388 assert(requiresArrayCookie(expr));
2389
2390 // The size of the cookie.
2391 CharUnits cookieSize = getArrayCookieSizeImpl(elementType);
2392
2393 // Compute an offset to the cookie.
2394 Address cookiePtr = newPtr;
2395
2396 // Write the number of elements into the appropriate slot.
2397 Address numElementsPtr = cookiePtr.withElementType(CGF.SizeTy);
2398 CGF.Builder.CreateStore(numElements, numElementsPtr);
2399
2400 // Finally, compute a pointer to the actual data buffer by skipping
2401 // over the cookie completely.
2402 return CGF.Builder.CreateConstInBoundsByteGEP(newPtr, cookieSize);
2403}
2404
2406 llvm::FunctionCallee Dtor,
2407 llvm::Constant *Addr) {
2408 // Create a function which calls the destructor.
2409 llvm::Constant *DtorStub = CGF.createAtExitStub(VD, Dtor, Addr);
2410
2411 // extern "C" int __tlregdtor(void (*f)(void));
2412 llvm::FunctionType *TLRegDtorTy = llvm::FunctionType::get(
2413 CGF.IntTy, DtorStub->getType(), /*isVarArg=*/false);
2414
2415 llvm::FunctionCallee TLRegDtor = CGF.CGM.CreateRuntimeFunction(
2416 TLRegDtorTy, "__tlregdtor", llvm::AttributeList(), /*Local=*/true);
2417 if (llvm::Function *TLRegDtorFn =
2418 dyn_cast<llvm::Function>(TLRegDtor.getCallee()))
2419 TLRegDtorFn->setDoesNotThrow();
2420
2421 CGF.EmitNounwindRuntimeCall(TLRegDtor, DtorStub);
2422}
2423
2424void MicrosoftCXXABI::registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
2425 llvm::FunctionCallee Dtor,
2426 llvm::Constant *Addr) {
2427 if (D.isNoDestroy(CGM.getContext()))
2428 return;
2429
2430 if (D.getTLSKind())
2431 return emitGlobalDtorWithTLRegDtor(CGF, D, Dtor, Addr);
2432
2433 // HLSL doesn't support atexit.
2434 if (CGM.getLangOpts().HLSL)
2435 return CGM.AddCXXDtorEntry(Dtor, Addr);
2436
2437 // The default behavior is to use atexit.
2438 CGF.registerGlobalDtorWithAtExit(D, Dtor, Addr);
2439}
2440
2441void MicrosoftCXXABI::EmitThreadLocalInitFuncs(
2442 CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals,
2443 ArrayRef<llvm::Function *> CXXThreadLocalInits,
2444 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) {
2445 if (CXXThreadLocalInits.empty())
2446 return;
2447
2448 CGM.AppendLinkerOptions(CGM.getTarget().getTriple().getArch() ==
2449 llvm::Triple::x86
2450 ? "/include:___dyn_tls_init@12"
2451 : "/include:__dyn_tls_init");
2452
2453 // This will create a GV in the .CRT$XDU section. It will point to our
2454 // initialization function. The CRT will call all of these function
2455 // pointers at start-up time and, eventually, at thread-creation time.
2456 auto AddToXDU = [&CGM](llvm::Function *InitFunc) {
2457 llvm::GlobalVariable *InitFuncPtr = new llvm::GlobalVariable(
2458 CGM.getModule(), InitFunc->getType(), /*isConstant=*/true,
2459 llvm::GlobalVariable::InternalLinkage, InitFunc,
2460 Twine(InitFunc->getName(), "$initializer$"));
2461 InitFuncPtr->setSection(".CRT$XDU");
2462 // This variable has discardable linkage, we have to add it to @llvm.used to
2463 // ensure it won't get discarded.
2464 CGM.addUsedGlobal(InitFuncPtr);
2465 return InitFuncPtr;
2466 };
2467
2468 std::vector<llvm::Function *> NonComdatInits;
2469 for (size_t I = 0, E = CXXThreadLocalInitVars.size(); I != E; ++I) {
2470 llvm::GlobalVariable *GV = cast<llvm::GlobalVariable>(
2471 CGM.GetGlobalValue(CGM.getMangledName(CXXThreadLocalInitVars[I])));
2472 llvm::Function *F = CXXThreadLocalInits[I];
2473
2474 // If the GV is already in a comdat group, then we have to join it.
2475 if (llvm::Comdat *C = GV->getComdat())
2476 AddToXDU(F)->setComdat(C);
2477 else
2478 NonComdatInits.push_back(F);
2479 }
2480
2481 if (!NonComdatInits.empty()) {
2482 llvm::FunctionType *FTy =
2483 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
2484 llvm::Function *InitFunc = CGM.CreateGlobalInitOrCleanUpFunction(
2485 FTy, "__tls_init", CGM.getTypes().arrangeNullaryFunction(),
2486 SourceLocation(), /*TLS=*/true);
2487 CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(InitFunc, NonComdatInits);
2488
2489 AddToXDU(InitFunc);
2490 }
2491}
2492
2493static llvm::GlobalValue *getTlsGuardVar(CodeGenModule &CGM) {
2494 // __tls_guard comes from the MSVC runtime and reflects
2495 // whether TLS has been initialized for a particular thread.
2496 // It is set from within __dyn_tls_init by the runtime.
2497 // Every library and executable has its own variable.
2498 llvm::Type *VTy = llvm::Type::getInt8Ty(CGM.getLLVMContext());
2499 llvm::Constant *TlsGuardConstant =
2500 CGM.CreateRuntimeVariable(VTy, "__tls_guard");
2501 llvm::GlobalValue *TlsGuard = cast<llvm::GlobalValue>(TlsGuardConstant);
2502
2503 TlsGuard->setThreadLocal(true);
2504
2505 return TlsGuard;
2506}
2507
2508static llvm::FunctionCallee getDynTlsOnDemandInitFn(CodeGenModule &CGM) {
2509 // __dyn_tls_on_demand_init comes from the MSVC runtime and triggers
2510 // dynamic TLS initialization by calling __dyn_tls_init internally.
2511 llvm::FunctionType *FTy =
2512 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()), {},
2513 /*isVarArg=*/false);
2514 return CGM.CreateRuntimeFunction(
2515 FTy, "__dyn_tls_on_demand_init",
2516 llvm::AttributeList::get(CGM.getLLVMContext(),
2517 llvm::AttributeList::FunctionIndex,
2518 llvm::Attribute::NoUnwind),
2519 /*Local=*/true);
2520}
2521
2522static void emitTlsGuardCheck(CodeGenFunction &CGF, llvm::GlobalValue *TlsGuard,
2523 llvm::BasicBlock *DynInitBB,
2524 llvm::BasicBlock *ContinueBB) {
2525 llvm::LoadInst *TlsGuardValue =
2526 CGF.Builder.CreateLoad(Address(TlsGuard, CGF.Int8Ty, CharUnits::One()));
2527 llvm::Value *CmpResult =
2528 CGF.Builder.CreateICmpEQ(TlsGuardValue, CGF.Builder.getInt8(0));
2529 CGF.Builder.CreateCondBr(CmpResult, DynInitBB, ContinueBB);
2530}
2531
2533 llvm::GlobalValue *TlsGuard,
2534 llvm::BasicBlock *ContinueBB) {
2535 llvm::FunctionCallee Initializer = getDynTlsOnDemandInitFn(CGF.CGM);
2536 llvm::Function *InitializerFunction =
2537 cast<llvm::Function>(Initializer.getCallee());
2538 llvm::CallInst *CallVal = CGF.Builder.CreateCall(InitializerFunction);
2539 CallVal->setCallingConv(InitializerFunction->getCallingConv());
2540
2541 CGF.Builder.CreateBr(ContinueBB);
2542}
2543
2545 llvm::BasicBlock *DynInitBB =
2546 CGF.createBasicBlock("dyntls.dyn_init", CGF.CurFn);
2547 llvm::BasicBlock *ContinueBB =
2548 CGF.createBasicBlock("dyntls.continue", CGF.CurFn);
2549
2550 llvm::GlobalValue *TlsGuard = getTlsGuardVar(CGF.CGM);
2551
2552 emitTlsGuardCheck(CGF, TlsGuard, DynInitBB, ContinueBB);
2553 CGF.Builder.SetInsertPoint(DynInitBB);
2554 emitDynamicTlsInitializationCall(CGF, TlsGuard, ContinueBB);
2555 CGF.Builder.SetInsertPoint(ContinueBB);
2556}
2557
2558LValue MicrosoftCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
2559 const VarDecl *VD,
2560 QualType LValType) {
2561 // Dynamic TLS initialization works by checking the state of a
2562 // guard variable (__tls_guard) to see whether TLS initialization
2563 // for a thread has happend yet.
2564 // If not, the initialization is triggered on-demand
2565 // by calling __dyn_tls_on_demand_init.
2567
2568 // Emit the variable just like any regular global variable.
2569
2570 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
2571 llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType());
2572
2573 CharUnits Alignment = CGF.getContext().getDeclAlign(VD);
2574 Address Addr(V, RealVarTy, Alignment);
2575
2576 LValue LV = VD->getType()->isReferenceType()
2578 AlignmentSource::Decl)
2579 : CGF.MakeAddrLValue(Addr, LValType, AlignmentSource::Decl);
2580 return LV;
2581}
2582
2584 StringRef VarName("_Init_thread_epoch");
2585 CharUnits Align = CGM.getIntAlign();
2586 if (auto *GV = CGM.getModule().getNamedGlobal(VarName))
2587 return ConstantAddress(GV, GV->getValueType(), Align);
2588 auto *GV = new llvm::GlobalVariable(
2589 CGM.getModule(), CGM.IntTy,
2590 /*isConstant=*/false, llvm::GlobalVariable::ExternalLinkage,
2591 /*Initializer=*/nullptr, VarName,
2592 /*InsertBefore=*/nullptr, llvm::GlobalVariable::GeneralDynamicTLSModel);
2593 GV->setAlignment(Align.getAsAlign());
2594 return ConstantAddress(GV, GV->getValueType(), Align);
2595}
2596
2597static llvm::FunctionCallee getInitThreadHeaderFn(CodeGenModule &CGM) {
2598 llvm::FunctionType *FTy =
2599 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
2600 CGM.DefaultPtrTy, /*isVarArg=*/false);
2601 return CGM.CreateRuntimeFunction(
2602 FTy, "_Init_thread_header",
2603 llvm::AttributeList::get(CGM.getLLVMContext(),
2604 llvm::AttributeList::FunctionIndex,
2605 llvm::Attribute::NoUnwind),
2606 /*Local=*/true);
2607}
2608
2609static llvm::FunctionCallee getInitThreadFooterFn(CodeGenModule &CGM) {
2610 llvm::FunctionType *FTy =
2611 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
2612 CGM.DefaultPtrTy, /*isVarArg=*/false);
2613 return CGM.CreateRuntimeFunction(
2614 FTy, "_Init_thread_footer",
2615 llvm::AttributeList::get(CGM.getLLVMContext(),
2616 llvm::AttributeList::FunctionIndex,
2617 llvm::Attribute::NoUnwind),
2618 /*Local=*/true);
2619}
2620
2621static llvm::FunctionCallee getInitThreadAbortFn(CodeGenModule &CGM) {
2622 llvm::FunctionType *FTy =
2623 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
2624 CGM.DefaultPtrTy, /*isVarArg=*/false);
2625 return CGM.CreateRuntimeFunction(
2626 FTy, "_Init_thread_abort",
2627 llvm::AttributeList::get(CGM.getLLVMContext(),
2628 llvm::AttributeList::FunctionIndex,
2629 llvm::Attribute::NoUnwind),
2630 /*Local=*/true);
2631}
2632
2633namespace {
2634struct ResetGuardBit final : EHScopeStack::Cleanup {
2635 Address Guard;
2636 unsigned GuardNum;
2637 ResetGuardBit(Address Guard, unsigned GuardNum)
2638 : Guard(Guard), GuardNum(GuardNum) {}
2639
2640 void Emit(CodeGenFunction &CGF, Flags flags) override {
2641 // Reset the bit in the mask so that the static variable may be
2642 // reinitialized.
2643 CGBuilderTy &Builder = CGF.Builder;
2644 llvm::LoadInst *LI = Builder.CreateLoad(Guard);
2645 llvm::ConstantInt *Mask =
2646 llvm::ConstantInt::getSigned(CGF.IntTy, ~(1ULL << GuardNum));
2647 Builder.CreateStore(Builder.CreateAnd(LI, Mask), Guard);
2648 }
2649};
2650
2651struct CallInitThreadAbort final : EHScopeStack::Cleanup {
2652 llvm::Value *Guard;
2653 CallInitThreadAbort(RawAddress Guard) : Guard(Guard.getPointer()) {}
2654
2655 void Emit(CodeGenFunction &CGF, Flags flags) override {
2656 // Calling _Init_thread_abort will reset the guard's state.
2658 }
2659};
2660}
2661
2662void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
2663 llvm::GlobalVariable *GV,
2664 bool PerformInit) {
2665 // MSVC only uses guards for static locals.
2666 if (!D.isStaticLocal()) {
2667 assert(GV->hasWeakLinkage() || GV->hasLinkOnceLinkage());
2668 // GlobalOpt is allowed to discard the initializer, so use linkonce_odr.
2669 llvm::Function *F = CGF.CurFn;
2670 F->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage);
2671 F->setComdat(CGM.getModule().getOrInsertComdat(F->getName()));
2672 CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit);
2673 return;
2674 }
2675
2676 bool ThreadlocalStatic = D.getTLSKind();
2677 bool ThreadsafeStatic = getContext().getLangOpts().ThreadsafeStatics;
2678
2679 // Thread-safe static variables which aren't thread-specific have a
2680 // per-variable guard.
2681 bool HasPerVariableGuard = ThreadsafeStatic && !ThreadlocalStatic;
2682
2683 CGBuilderTy &Builder = CGF.Builder;
2684 llvm::IntegerType *GuardTy = CGF.Int32Ty;
2685 llvm::ConstantInt *Zero = llvm::ConstantInt::get(GuardTy, 0);
2686 CharUnits GuardAlign = CharUnits::fromQuantity(4);
2687
2688 // Get the guard variable for this function if we have one already.
2689 GuardInfo *GI = nullptr;
2690 if (ThreadlocalStatic)
2691 GI = &ThreadLocalGuardVariableMap[D.getDeclContext()];
2692 else if (!ThreadsafeStatic)
2693 GI = &GuardVariableMap[D.getDeclContext()];
2694
2695 llvm::GlobalVariable *GuardVar = GI ? GI->Guard : nullptr;
2696 unsigned GuardNum;
2697 if (D.isExternallyVisible()) {
2698 // Externally visible variables have to be numbered in Sema to properly
2699 // handle unreachable VarDecls.
2700 GuardNum = getContext().getStaticLocalNumber(&D);
2701 assert(GuardNum > 0);
2702 GuardNum--;
2703 } else if (HasPerVariableGuard) {
2704 GuardNum = ThreadSafeGuardNumMap[D.getDeclContext()]++;
2705 } else {
2706 // Non-externally visible variables are numbered here in CodeGen.
2707 GuardNum = GI->BitIndex++;
2708 }
2709
2710 if (!HasPerVariableGuard && GuardNum >= 32) {
2711 if (D.isExternallyVisible())
2712 ErrorUnsupportedABI(CGF, "more than 32 guarded initializations");
2713 GuardNum %= 32;
2714 GuardVar = nullptr;
2715 }
2716
2717 if (!GuardVar) {
2718 // Mangle the name for the guard.
2719 SmallString<256> GuardName;
2720 {
2721 llvm::raw_svector_ostream Out(GuardName);
2722 if (HasPerVariableGuard)
2723 getMangleContext().mangleThreadSafeStaticGuardVariable(&D, GuardNum,
2724 Out);
2725 else
2726 getMangleContext().mangleStaticGuardVariable(&D, Out);
2727 }
2728
2729 // Create the guard variable with a zero-initializer. Just absorb linkage,
2730 // visibility and dll storage class from the guarded variable.
2731 GuardVar =
2732 new llvm::GlobalVariable(CGM.getModule(), GuardTy, /*isConstant=*/false,
2733 GV->getLinkage(), Zero, GuardName.str());
2734 GuardVar->setVisibility(GV->getVisibility());
2735 GuardVar->setDLLStorageClass(GV->getDLLStorageClass());
2736 GuardVar->setAlignment(GuardAlign.getAsAlign());
2737 if (GuardVar->isWeakForLinker())
2738 GuardVar->setComdat(
2739 CGM.getModule().getOrInsertComdat(GuardVar->getName()));
2740 if (D.getTLSKind())
2741 CGM.setTLSMode(GuardVar, D);
2742 if (GI && !HasPerVariableGuard)
2743 GI->Guard = GuardVar;
2744 }
2745
2746 ConstantAddress GuardAddr(GuardVar, GuardTy, GuardAlign);
2747
2748 assert(GuardVar->getLinkage() == GV->getLinkage() &&
2749 "static local from the same function had different linkage");
2750
2751 if (!HasPerVariableGuard) {
2752 // Pseudo code for the test:
2753 // if (!(GuardVar & MyGuardBit)) {
2754 // GuardVar |= MyGuardBit;
2755 // ... initialize the object ...;
2756 // }
2757
2758 // Test our bit from the guard variable.
2759 llvm::ConstantInt *Bit = llvm::ConstantInt::get(GuardTy, 1ULL << GuardNum);
2760 llvm::LoadInst *LI = Builder.CreateLoad(GuardAddr);
2761 llvm::Value *NeedsInit =
2762 Builder.CreateICmpEQ(Builder.CreateAnd(LI, Bit), Zero);
2763 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
2764 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
2765 CGF.EmitCXXGuardedInitBranch(NeedsInit, InitBlock, EndBlock,
2766 CodeGenFunction::GuardKind::VariableGuard, &D);
2767
2768 // Set our bit in the guard variable and emit the initializer and add a global
2769 // destructor if appropriate.
2770 CGF.EmitBlock(InitBlock);
2771 Builder.CreateStore(Builder.CreateOr(LI, Bit), GuardAddr);
2772 CGF.EHStack.pushCleanup<ResetGuardBit>(EHCleanup, GuardAddr, GuardNum);
2773 CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit);
2774 CGF.PopCleanupBlock();
2775 Builder.CreateBr(EndBlock);
2776
2777 // Continue.
2778 CGF.EmitBlock(EndBlock);
2779 } else {
2780 // Pseudo code for the test:
2781 // if (TSS > _Init_thread_epoch) {
2782 // _Init_thread_header(&TSS);
2783 // if (TSS == -1) {
2784 // ... initialize the object ...;
2785 // _Init_thread_footer(&TSS);
2786 // }
2787 // }
2788 //
2789 // The algorithm is almost identical to what can be found in the appendix
2790 // found in N2325.
2791
2792 // This BasicBLock determines whether or not we have any work to do.
2793 llvm::LoadInst *FirstGuardLoad = Builder.CreateLoad(GuardAddr);
2794 FirstGuardLoad->setOrdering(llvm::AtomicOrdering::Unordered);
2795 llvm::LoadInst *InitThreadEpoch =
2796 Builder.CreateLoad(getInitThreadEpochPtr(CGM));
2797 llvm::Value *IsUninitialized =
2798 Builder.CreateICmpSGT(FirstGuardLoad, InitThreadEpoch);
2799 llvm::BasicBlock *AttemptInitBlock = CGF.createBasicBlock("init.attempt");
2800 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
2801 CGF.EmitCXXGuardedInitBranch(IsUninitialized, AttemptInitBlock, EndBlock,
2802 CodeGenFunction::GuardKind::VariableGuard, &D);
2803
2804 // This BasicBlock attempts to determine whether or not this thread is
2805 // responsible for doing the initialization.
2806 CGF.EmitBlock(AttemptInitBlock);
2808 GuardAddr.getPointer());
2809 llvm::LoadInst *SecondGuardLoad = Builder.CreateLoad(GuardAddr);
2810 SecondGuardLoad->setOrdering(llvm::AtomicOrdering::Unordered);
2811 llvm::Value *ShouldDoInit =
2812 Builder.CreateICmpEQ(SecondGuardLoad, getAllOnesInt());
2813 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
2814 Builder.CreateCondBr(ShouldDoInit, InitBlock, EndBlock);
2815
2816 // Ok, we ended up getting selected as the initializing thread.
2817 CGF.EmitBlock(InitBlock);
2818 CGF.EHStack.pushCleanup<CallInitThreadAbort>(EHCleanup, GuardAddr);
2819 CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit);
2820 CGF.PopCleanupBlock();
2822 GuardAddr.getPointer());
2823 Builder.CreateBr(EndBlock);
2824
2825 CGF.EmitBlock(EndBlock);
2826 }
2827}
2828
2829bool MicrosoftCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
2830 // Null-ness for function memptrs only depends on the first field, which is
2831 // the function pointer. The rest don't matter, so we can zero initialize.
2832 if (MPT->isMemberFunctionPointer())
2833 return true;
2834
2835 // The virtual base adjustment field is always -1 for null, so if we have one
2836 // we can't zero initialize. The field offset is sometimes also -1 if 0 is a
2837 // valid field offset.
2838 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
2839 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
2840 return (!inheritanceModelHasVBTableOffsetField(Inheritance) &&
2841 RD->nullFieldOffsetIsZero());
2842}
2843
2844llvm::Type *
2845MicrosoftCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
2846 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
2847 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
2848 llvm::SmallVector<llvm::Type *, 4> fields;
2849 if (MPT->isMemberFunctionPointer())
2850 fields.push_back(CGM.VoidPtrTy); // FunctionPointerOrVirtualThunk
2851 else
2852 fields.push_back(CGM.IntTy); // FieldOffset
2853
2855 Inheritance))
2856 fields.push_back(CGM.IntTy);
2857 if (inheritanceModelHasVBPtrOffsetField(Inheritance))
2858 fields.push_back(CGM.IntTy);
2860 fields.push_back(CGM.IntTy); // VirtualBaseAdjustmentOffset
2861
2862 if (fields.size() == 1)
2863 return fields[0];
2864 return llvm::StructType::get(CGM.getLLVMContext(), fields);
2865}
2866
2867void MicrosoftCXXABI::
2868GetNullMemberPointerFields(const MemberPointerType *MPT,
2869 llvm::SmallVectorImpl<llvm::Constant *> &fields) {
2870 assert(fields.empty());
2871 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
2872 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
2873 if (MPT->isMemberFunctionPointer()) {
2874 // FunctionPointerOrVirtualThunk
2875 fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy));
2876 } else {
2877 if (RD->nullFieldOffsetIsZero())
2878 fields.push_back(getZeroInt()); // FieldOffset
2879 else
2880 fields.push_back(getAllOnesInt()); // FieldOffset
2881 }
2882
2884 Inheritance))
2885 fields.push_back(getZeroInt());
2886 if (inheritanceModelHasVBPtrOffsetField(Inheritance))
2887 fields.push_back(getZeroInt());
2889 fields.push_back(getAllOnesInt());
2890}
2891
2892llvm::Constant *
2893MicrosoftCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
2894 llvm::SmallVector<llvm::Constant *, 4> fields;
2895 GetNullMemberPointerFields(MPT, fields);
2896 if (fields.size() == 1)
2897 return fields[0];
2898 llvm::Constant *Res = llvm::ConstantStruct::getAnon(fields);
2899 assert(Res->getType() == ConvertMemberPointerType(MPT));
2900 return Res;
2901}
2902
2903llvm::Constant *
2904MicrosoftCXXABI::EmitFullMemberPointer(llvm::Constant *FirstField,
2905 bool IsMemberFunction,
2906 const CXXRecordDecl *RD,
2907 CharUnits NonVirtualBaseAdjustment,
2908 unsigned VBTableIndex) {
2909 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
2910
2911 // Single inheritance class member pointer are represented as scalars instead
2912 // of aggregates.
2913 if (inheritanceModelHasOnlyOneField(IsMemberFunction, Inheritance))
2914 return FirstField;
2915
2916 llvm::SmallVector<llvm::Constant *, 4> fields;
2917 fields.push_back(FirstField);
2918
2919 if (inheritanceModelHasNVOffsetField(IsMemberFunction, Inheritance))
2920 fields.push_back(llvm::ConstantInt::getSigned(
2921 CGM.IntTy, NonVirtualBaseAdjustment.getQuantity()));
2922
2923 if (inheritanceModelHasVBPtrOffsetField(Inheritance)) {
2924 CharUnits Offs = CharUnits::Zero();
2925 if (VBTableIndex)
2926 Offs = getContext().getASTRecordLayout(RD).getVBPtrOffset();
2927 fields.push_back(llvm::ConstantInt::get(CGM.IntTy, Offs.getQuantity()));
2928 }
2929
2930 // The rest of the fields are adjusted by conversions to a more derived class.
2932 fields.push_back(llvm::ConstantInt::get(CGM.IntTy, VBTableIndex));
2933
2934 return llvm::ConstantStruct::getAnon(fields);
2935}
2936
2937llvm::Constant *
2938MicrosoftCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
2939 CharUnits offset) {
2940 return EmitMemberDataPointer(MPT->getMostRecentCXXRecordDecl(), offset);
2941}
2942
2943llvm::Constant *MicrosoftCXXABI::EmitMemberDataPointer(const CXXRecordDecl *RD,
2944 CharUnits offset) {
2945 if (RD->getMSInheritanceModel() ==
2946 MSInheritanceModel::Virtual)
2947 offset -= getContext().getOffsetOfBaseWithVBPtr(RD);
2948 llvm::Constant *FirstField =
2949 llvm::ConstantInt::get(CGM.IntTy, offset.getQuantity());
2950 return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/false, RD,
2951 CharUnits::Zero(), /*VBTableIndex=*/0);
2952}
2953
2954llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const APValue &MP,
2955 QualType MPType) {
2956 const MemberPointerType *DstTy = MPType->castAs<MemberPointerType>();
2957 const ValueDecl *MPD = MP.getMemberPointerDecl();
2958 if (!MPD)
2959 return EmitNullMemberPointer(DstTy);
2960
2961 ASTContext &Ctx = getContext();
2962 ArrayRef<const CXXRecordDecl *> MemberPointerPath = MP.getMemberPointerPath();
2963
2964 llvm::Constant *C;
2965 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) {
2966 C = EmitMemberFunctionPointer(MD);
2967 } else {
2968 // For a pointer to data member, start off with the offset of the field in
2969 // the class in which it was declared, and convert from there if necessary.
2970 // For indirect field decls, get the outermost anonymous field and use the
2971 // parent class.
2973 CharUnits FieldOffset = Ctx.toCharUnitsFromBits(Ctx.getFieldOffset(MPD));
2974 const FieldDecl *FD = dyn_cast<FieldDecl>(MPD);
2975 if (!FD)
2976 FD = cast<FieldDecl>(*cast<IndirectFieldDecl>(MPD)->chain_begin());
2977 const CXXRecordDecl *RD = cast<CXXRecordDecl>(FD->getParent());
2978 RD = RD->getMostRecentDecl();
2979 C = EmitMemberDataPointer(RD, FieldOffset);
2980 }
2981
2982 if (!MemberPointerPath.empty()) {
2983 const CXXRecordDecl *SrcRD = cast<CXXRecordDecl>(MPD->getDeclContext());
2984 const MemberPointerType *SrcTy =
2986 /*Qualifier=*/std::nullopt, SrcRD)
2987 ->castAs<MemberPointerType>();
2988
2989 bool DerivedMember = MP.isMemberPointerToDerivedMember();
2990 SmallVector<const CXXBaseSpecifier *, 4> DerivedToBasePath;
2991 const CXXRecordDecl *PrevRD = SrcRD;
2992 for (const CXXRecordDecl *PathElem : MemberPointerPath) {
2993 const CXXRecordDecl *Base = nullptr;
2994 const CXXRecordDecl *Derived = nullptr;
2995 if (DerivedMember) {
2996 Base = PathElem;
2997 Derived = PrevRD;
2998 } else {
2999 Base = PrevRD;
3000 Derived = PathElem;
3001 }
3002 for (const CXXBaseSpecifier &BS : Derived->bases())
3003 if (BS.getType()->getAsCXXRecordDecl()->getCanonicalDecl() ==
3004 Base->getCanonicalDecl())
3005 DerivedToBasePath.push_back(&BS);
3006 PrevRD = PathElem;
3007 }
3008 assert(DerivedToBasePath.size() == MemberPointerPath.size());
3009
3010 CastKind CK = DerivedMember ? CK_DerivedToBaseMemberPointer
3011 : CK_BaseToDerivedMemberPointer;
3012 C = EmitMemberPointerConversion(SrcTy, DstTy, CK, DerivedToBasePath.begin(),
3013 DerivedToBasePath.end(), C);
3014 }
3015 return C;
3016}
3017
3018llvm::Constant *
3019MicrosoftCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) {
3020 assert(MD->isInstance() && "Member function must not be static!");
3021
3022 CharUnits NonVirtualBaseAdjustment = CharUnits::Zero();
3023 const CXXRecordDecl *RD = MD->getParent()->getMostRecentDecl();
3024 CodeGenTypes &Types = CGM.getTypes();
3025
3026 unsigned VBTableIndex = 0;
3027 llvm::Constant *FirstField;
3028 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
3029 if (!MD->isVirtual()) {
3030 llvm::Type *Ty;
3031 // Check whether the function has a computable LLVM signature.
3032 if (Types.isFuncTypeConvertible(FPT)) {
3033 // The function has a computable LLVM signature; use the correct type.
3034 Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
3035 } else {
3036 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
3037 // function type is incomplete.
3038 Ty = CGM.PtrDiffTy;
3039 }
3040 FirstField = CGM.GetAddrOfFunction(MD, Ty);
3041 } else {
3042 auto &VTableContext = CGM.getMicrosoftVTableContext();
3043 MethodVFTableLocation ML = VTableContext.getMethodVFTableLocation(MD);
3044 FirstField = EmitVirtualMemPtrThunk(MD, ML);
3045 // Include the vfptr adjustment if the method is in a non-primary vftable.
3046 NonVirtualBaseAdjustment += ML.VFPtrOffset;
3047 if (ML.VBase)
3048 VBTableIndex = VTableContext.getVBTableIndex(RD, ML.VBase) * 4;
3049 }
3050
3051 if (VBTableIndex == 0 &&
3052 RD->getMSInheritanceModel() ==
3053 MSInheritanceModel::Virtual)
3054 NonVirtualBaseAdjustment -= getContext().getOffsetOfBaseWithVBPtr(RD);
3055
3056 // The rest of the fields are common with data member pointers.
3057 return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/true, RD,
3058 NonVirtualBaseAdjustment, VBTableIndex);
3059}
3060
3061/// Member pointers are the same if they're either bitwise identical *or* both
3062/// null. Null-ness for function members is determined by the first field,
3063/// while for data member pointers we must compare all fields.
3064llvm::Value *
3065MicrosoftCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
3066 llvm::Value *L,
3067 llvm::Value *R,
3068 const MemberPointerType *MPT,
3069 bool Inequality) {
3070 CGBuilderTy &Builder = CGF.Builder;
3071
3072 // Handle != comparisons by switching the sense of all boolean operations.
3073 llvm::ICmpInst::Predicate Eq;
3074 llvm::Instruction::BinaryOps And, Or;
3075 if (Inequality) {
3076 Eq = llvm::ICmpInst::ICMP_NE;
3077 And = llvm::Instruction::Or;
3078 Or = llvm::Instruction::And;
3079 } else {
3080 Eq = llvm::ICmpInst::ICMP_EQ;
3081 And = llvm::Instruction::And;
3082 Or = llvm::Instruction::Or;
3083 }
3084
3085 // If this is a single field member pointer (single inheritance), this is a
3086 // single icmp.
3087 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
3088 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
3090 Inheritance))
3091 return Builder.CreateICmp(Eq, L, R);
3092
3093 // Compare the first field.
3094 llvm::Value *L0 = Builder.CreateExtractValue(L, 0, "lhs.0");
3095 llvm::Value *R0 = Builder.CreateExtractValue(R, 0, "rhs.0");
3096 llvm::Value *Cmp0 = Builder.CreateICmp(Eq, L0, R0, "memptr.cmp.first");
3097
3098 // Compare everything other than the first field.
3099 llvm::Value *Res = nullptr;
3100 llvm::StructType *LType = cast<llvm::StructType>(L->getType());
3101 for (unsigned I = 1, E = LType->getNumElements(); I != E; ++I) {
3102 llvm::Value *LF = Builder.CreateExtractValue(L, I);
3103 llvm::Value *RF = Builder.CreateExtractValue(R, I);
3104 llvm::Value *Cmp = Builder.CreateICmp(Eq, LF, RF, "memptr.cmp.rest");
3105 if (Res)
3106 Res = Builder.CreateBinOp(And, Res, Cmp);
3107 else
3108 Res = Cmp;
3109 }
3110
3111 // Check if the first field is 0 if this is a function pointer.
3112 if (MPT->isMemberFunctionPointer()) {
3113 // (l1 == r1 && ...) || l0 == 0
3114 llvm::Value *Zero = llvm::Constant::getNullValue(L0->getType());
3115 llvm::Value *IsZero = Builder.CreateICmp(Eq, L0, Zero, "memptr.cmp.iszero");
3116 Res = Builder.CreateBinOp(Or, Res, IsZero);
3117 }
3118
3119 // Combine the comparison of the first field, which must always be true for
3120 // this comparison to succeeed.
3121 return Builder.CreateBinOp(And, Res, Cmp0, "memptr.cmp");
3122}
3123
3124llvm::Value *
3125MicrosoftCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
3126 llvm::Value *MemPtr,
3127 const MemberPointerType *MPT) {
3128 CGBuilderTy &Builder = CGF.Builder;
3129 llvm::SmallVector<llvm::Constant *, 4> fields;
3130 // We only need one field for member functions.
3131 if (MPT->isMemberFunctionPointer())
3132 fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy));
3133 else
3134 GetNullMemberPointerFields(MPT, fields);
3135 assert(!fields.empty());
3136 llvm::Value *FirstField = MemPtr;
3137 if (MemPtr->getType()->isStructTy())
3138 FirstField = Builder.CreateExtractValue(MemPtr, 0);
3139 llvm::Value *Res = Builder.CreateICmpNE(FirstField, fields[0], "memptr.cmp0");
3140
3141 // For function member pointers, we only need to test the function pointer
3142 // field. The other fields if any can be garbage.
3143 if (MPT->isMemberFunctionPointer())
3144 return Res;
3145
3146 // Otherwise, emit a series of compares and combine the results.
3147 for (int I = 1, E = fields.size(); I < E; ++I) {
3148 llvm::Value *Field = Builder.CreateExtractValue(MemPtr, I);
3149 llvm::Value *Next = Builder.CreateICmpNE(Field, fields[I], "memptr.cmp");
3150 Res = Builder.CreateOr(Res, Next, "memptr.tobool");
3151 }
3152 return Res;
3153}
3154
3155bool MicrosoftCXXABI::MemberPointerConstantIsNull(const MemberPointerType *MPT,
3156 llvm::Constant *Val) {
3157 // Function pointers are null if the pointer in the first field is null.
3158 if (MPT->isMemberFunctionPointer()) {
3159 llvm::Constant *FirstField = Val->getType()->isStructTy() ?
3160 Val->getAggregateElement(0U) : Val;
3161 return FirstField->isNullValue();
3162 }
3163
3164 // If it's not a function pointer and it's zero initializable, we can easily
3165 // check zero.
3166 if (isZeroInitializable(MPT) && Val->isNullValue())
3167 return true;
3168
3169 // Otherwise, break down all the fields for comparison. Hopefully these
3170 // little Constants are reused, while a big null struct might not be.
3171 llvm::SmallVector<llvm::Constant *, 4> Fields;
3172 GetNullMemberPointerFields(MPT, Fields);
3173 if (Fields.size() == 1) {
3174 assert(Val->getType()->isIntegerTy());
3175 return Val == Fields[0];
3176 }
3177
3178 unsigned I, E;
3179 for (I = 0, E = Fields.size(); I != E; ++I) {
3180 if (Val->getAggregateElement(I) != Fields[I])
3181 break;
3182 }
3183 return I == E;
3184}
3185
3186llvm::Value *
3187MicrosoftCXXABI::GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF,
3188 Address This,
3189 llvm::Value *VBPtrOffset,
3190 llvm::Value *VBTableOffset,
3191 llvm::Value **VBPtrOut) {
3192 CGBuilderTy &Builder = CGF.Builder;
3193 // Load the vbtable pointer from the vbptr in the instance.
3194 llvm::Value *VBPtr = Builder.CreateInBoundsGEP(
3195 CGM.Int8Ty, This.emitRawPointer(CGF), VBPtrOffset, "vbptr");
3196 if (VBPtrOut)
3197 *VBPtrOut = VBPtr;
3198
3199 CharUnits VBPtrAlign;
3200 if (auto CI = dyn_cast<llvm::ConstantInt>(VBPtrOffset)) {
3201 VBPtrAlign = This.getAlignment().alignmentAtOffset(
3202 CharUnits::fromQuantity(CI->getSExtValue()));
3203 } else {
3204 VBPtrAlign = CGF.getPointerAlign();
3205 }
3206
3207 llvm::Value *VBTable =
3208 Builder.CreateAlignedLoad(CGM.DefaultPtrTy, VBPtr, VBPtrAlign, "vbtable");
3209
3210 // Translate from byte offset to table index. It improves analyzability.
3211 llvm::Value *VBTableIndex = Builder.CreateAShr(
3212 VBTableOffset, llvm::ConstantInt::get(VBTableOffset->getType(), 2),
3213 "vbtindex", /*isExact=*/true);
3214
3215 // Load an i32 offset from the vb-table.
3216 llvm::Value *VBaseOffs =
3217 Builder.CreateInBoundsGEP(CGM.Int32Ty, VBTable, VBTableIndex);
3218 return Builder.CreateAlignedLoad(CGM.Int32Ty, VBaseOffs,
3219 CharUnits::fromQuantity(4), "vbase_offs");
3220}
3221
3222// Returns an adjusted base cast to i8*, since we do more address arithmetic on
3223// it.
3224llvm::Value *MicrosoftCXXABI::AdjustVirtualBase(
3225 CodeGenFunction &CGF, const Expr *E, const CXXRecordDecl *RD,
3226 Address Base, llvm::Value *VBTableOffset, llvm::Value *VBPtrOffset) {
3227 CGBuilderTy &Builder = CGF.Builder;
3228 Base = Base.withElementType(CGM.Int8Ty);
3229 llvm::BasicBlock *OriginalBB = nullptr;
3230 llvm::BasicBlock *SkipAdjustBB = nullptr;
3231 llvm::BasicBlock *VBaseAdjustBB = nullptr;
3232
3233 // In the unspecified inheritance model, there might not be a vbtable at all,
3234 // in which case we need to skip the virtual base lookup. If there is a
3235 // vbtable, the first entry is a no-op entry that gives back the original
3236 // base, so look for a virtual base adjustment offset of zero.
3237 if (VBPtrOffset) {
3238 OriginalBB = Builder.GetInsertBlock();
3239 VBaseAdjustBB = CGF.createBasicBlock("memptr.vadjust");
3240 SkipAdjustBB = CGF.createBasicBlock("memptr.skip_vadjust");
3241 llvm::Value *IsVirtual =
3242 Builder.CreateICmpNE(VBTableOffset, getZeroInt(),
3243 "memptr.is_vbase");
3244 Builder.CreateCondBr(IsVirtual, VBaseAdjustBB, SkipAdjustBB);
3245 CGF.EmitBlock(VBaseAdjustBB);
3246 }
3247
3248 // If we weren't given a dynamic vbptr offset, RD should be complete and we'll
3249 // know the vbptr offset.
3250 if (!VBPtrOffset) {
3251 CharUnits offs = CharUnits::Zero();
3252 if (!RD->hasDefinition()) {
3253 DiagnosticsEngine &Diags = CGF.CGM.getDiags();
3254 Diags.Report(E->getExprLoc(), diag::err_member_ptr_requires_complete_type)
3255 << RD << E->getSourceRange();
3256 } else if (RD->getNumVBases())
3257 offs = getContext().getASTRecordLayout(RD).getVBPtrOffset();
3258 VBPtrOffset = llvm::ConstantInt::get(CGM.IntTy, offs.getQuantity());
3259 }
3260 llvm::Value *VBPtr = nullptr;
3261 llvm::Value *VBaseOffs =
3262 GetVBaseOffsetFromVBPtr(CGF, Base, VBPtrOffset, VBTableOffset, &VBPtr);
3263 llvm::Value *AdjustedBase =
3264 Builder.CreateInBoundsGEP(CGM.Int8Ty, VBPtr, VBaseOffs);
3265
3266 // Merge control flow with the case where we didn't have to adjust.
3267 if (VBaseAdjustBB) {
3268 Builder.CreateBr(SkipAdjustBB);
3269 CGF.EmitBlock(SkipAdjustBB);
3270 llvm::PHINode *Phi = Builder.CreatePHI(CGM.Int8PtrTy, 2, "memptr.base");
3271 Phi->addIncoming(Base.emitRawPointer(CGF), OriginalBB);
3272 Phi->addIncoming(AdjustedBase, VBaseAdjustBB);
3273 return Phi;
3274 }
3275 return AdjustedBase;
3276}
3277
3278llvm::Value *MicrosoftCXXABI::EmitMemberDataPointerAddress(
3279 CodeGenFunction &CGF, const Expr *E, Address Base, llvm::Value *MemPtr,
3280 const MemberPointerType *MPT, bool IsInBounds) {
3281 assert(MPT->isMemberDataPointer());
3282 CGBuilderTy &Builder = CGF.Builder;
3283 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
3284 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
3285
3286 // Extract the fields we need, regardless of model. We'll apply them if we
3287 // have them.
3288 llvm::Value *FieldOffset = MemPtr;
3289 llvm::Value *VirtualBaseAdjustmentOffset = nullptr;
3290 llvm::Value *VBPtrOffset = nullptr;
3291 if (MemPtr->getType()->isStructTy()) {
3292 // We need to extract values.
3293 unsigned I = 0;
3294 FieldOffset = Builder.CreateExtractValue(MemPtr, I++);
3295 if (inheritanceModelHasVBPtrOffsetField(Inheritance))
3296 VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++);
3298 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++);
3299 }
3300
3301 llvm::Value *Addr;
3302 if (VirtualBaseAdjustmentOffset) {
3303 Addr = AdjustVirtualBase(CGF, E, RD, Base, VirtualBaseAdjustmentOffset,
3304 VBPtrOffset);
3305 } else {
3306 Addr = Base.emitRawPointer(CGF);
3307 }
3308
3309 // Apply the offset.
3310 return Builder.CreateGEP(CGF.Int8Ty, Addr, FieldOffset, "memptr.offset",
3311 IsInBounds ? llvm::GEPNoWrapFlags::inBounds()
3312 : llvm::GEPNoWrapFlags::none());
3313}
3314
3315llvm::Value *
3316MicrosoftCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
3317 const CastExpr *E,
3318 llvm::Value *Src) {
3319 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
3320 E->getCastKind() == CK_BaseToDerivedMemberPointer ||
3321 E->getCastKind() == CK_ReinterpretMemberPointer);
3322
3323 // Use constant emission if we can.
3324 if (isa<llvm::Constant>(Src))
3325 return EmitMemberPointerConversion(E, cast<llvm::Constant>(Src));
3326
3327 // We may be adding or dropping fields from the member pointer, so we need
3328 // both types and the inheritance models of both records.
3329 const MemberPointerType *SrcTy =
3330 E->getSubExpr()->getType()->castAs<MemberPointerType>();
3331 const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>();
3332 bool IsFunc = SrcTy->isMemberFunctionPointer();
3333
3334 // If the classes use the same null representation, reinterpret_cast is a nop.
3335 bool IsReinterpret = E->getCastKind() == CK_ReinterpretMemberPointer;
3336 if (IsReinterpret && IsFunc)
3337 return Src;
3338
3339 CXXRecordDecl *SrcRD = SrcTy->getMostRecentCXXRecordDecl();
3340 CXXRecordDecl *DstRD = DstTy->getMostRecentCXXRecordDecl();
3341 if (IsReinterpret &&
3342 SrcRD->nullFieldOffsetIsZero() == DstRD->nullFieldOffsetIsZero())
3343 return Src;
3344
3345 CGBuilderTy &Builder = CGF.Builder;
3346
3347 // Branch past the conversion if Src is null.
3348 llvm::Value *IsNotNull = EmitMemberPointerIsNotNull(CGF, Src, SrcTy);
3349 llvm::Constant *DstNull = EmitNullMemberPointer(DstTy);
3350
3351 // C++ 5.2.10p9: The null member pointer value is converted to the null member
3352 // pointer value of the destination type.
3353 if (IsReinterpret) {
3354 // For reinterpret casts, sema ensures that src and dst are both functions
3355 // or data and have the same size, which means the LLVM types should match.
3356 assert(Src->getType() == DstNull->getType());
3357 return Builder.CreateSelect(IsNotNull, Src, DstNull);
3358 }
3359
3360 llvm::BasicBlock *OriginalBB = Builder.GetInsertBlock();
3361 llvm::BasicBlock *ConvertBB = CGF.createBasicBlock("memptr.convert");
3362 llvm::BasicBlock *ContinueBB = CGF.createBasicBlock("memptr.converted");
3363 Builder.CreateCondBr(IsNotNull, ConvertBB, ContinueBB);
3364 CGF.EmitBlock(ConvertBB);
3365
3366 llvm::Value *Dst = EmitNonNullMemberPointerConversion(
3367 SrcTy, DstTy, E->getCastKind(), E->path_begin(), E->path_end(), Src,
3368 Builder);
3369
3370 Builder.CreateBr(ContinueBB);
3371
3372 // In the continuation, choose between DstNull and Dst.
3373 CGF.EmitBlock(ContinueBB);
3374 llvm::PHINode *Phi = Builder.CreatePHI(DstNull->getType(), 2, "memptr.converted");
3375 Phi->addIncoming(DstNull, OriginalBB);
3376 Phi->addIncoming(Dst, ConvertBB);
3377 return Phi;
3378}
3379
3380llvm::Value *MicrosoftCXXABI::EmitNonNullMemberPointerConversion(
3381 const MemberPointerType *SrcTy, const MemberPointerType *DstTy, CastKind CK,
3383 CastExpr::path_const_iterator PathEnd, llvm::Value *Src,
3384 CGBuilderTy &Builder) {
3385 const CXXRecordDecl *SrcRD = SrcTy->getMostRecentCXXRecordDecl();
3386 const CXXRecordDecl *DstRD = DstTy->getMostRecentCXXRecordDecl();
3387 MSInheritanceModel SrcInheritance = SrcRD->getMSInheritanceModel();
3388 MSInheritanceModel DstInheritance = DstRD->getMSInheritanceModel();
3389 bool IsFunc = SrcTy->isMemberFunctionPointer();
3390 bool IsConstant = isa<llvm::Constant>(Src);
3391
3392 // Decompose src.
3393 llvm::Value *FirstField = Src;
3394 llvm::Value *NonVirtualBaseAdjustment = getZeroInt();
3395 llvm::Value *VirtualBaseAdjustmentOffset = getZeroInt();
3396 llvm::Value *VBPtrOffset = getZeroInt();
3397 if (!inheritanceModelHasOnlyOneField(IsFunc, SrcInheritance)) {
3398 // We need to extract values.
3399 unsigned I = 0;
3400 FirstField = Builder.CreateExtractValue(Src, I++);
3401 if (inheritanceModelHasNVOffsetField(IsFunc, SrcInheritance))
3402 NonVirtualBaseAdjustment = Builder.CreateExtractValue(Src, I++);
3403 if (inheritanceModelHasVBPtrOffsetField(SrcInheritance))
3404 VBPtrOffset = Builder.CreateExtractValue(Src, I++);
3405 if (inheritanceModelHasVBTableOffsetField(SrcInheritance))
3406 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(Src, I++);
3407 }
3408
3409 bool IsDerivedToBase = (CK == CK_DerivedToBaseMemberPointer);
3410 const MemberPointerType *DerivedTy = IsDerivedToBase ? SrcTy : DstTy;
3411 const CXXRecordDecl *DerivedClass = DerivedTy->getMostRecentCXXRecordDecl();
3412
3413 // For data pointers, we adjust the field offset directly. For functions, we
3414 // have a separate field.
3415 llvm::Value *&NVAdjustField = IsFunc ? NonVirtualBaseAdjustment : FirstField;
3416
3417 // The virtual inheritance model has a quirk: the virtual base table is always
3418 // referenced when dereferencing a member pointer even if the member pointer
3419 // is non-virtual. This is accounted for by adjusting the non-virtual offset
3420 // to point backwards to the top of the MDC from the first VBase. Undo this
3421 // adjustment to normalize the member pointer.
3422 llvm::Value *SrcVBIndexEqZero =
3423 Builder.CreateICmpEQ(VirtualBaseAdjustmentOffset, getZeroInt());
3424 if (SrcInheritance == MSInheritanceModel::Virtual) {
3425 if (int64_t SrcOffsetToFirstVBase =
3426 getContext().getOffsetOfBaseWithVBPtr(SrcRD).getQuantity()) {
3427 llvm::Value *UndoSrcAdjustment = Builder.CreateSelect(
3428 SrcVBIndexEqZero,
3429 llvm::ConstantInt::get(CGM.IntTy, SrcOffsetToFirstVBase),
3430 getZeroInt());
3431 NVAdjustField = Builder.CreateNSWAdd(NVAdjustField, UndoSrcAdjustment);
3432 }
3433 }
3434
3435 // A non-zero vbindex implies that we are dealing with a source member in a
3436 // floating virtual base in addition to some non-virtual offset. If the
3437 // vbindex is zero, we are dealing with a source that exists in a non-virtual,
3438 // fixed, base. The difference between these two cases is that the vbindex +
3439 // nvoffset *always* point to the member regardless of what context they are
3440 // evaluated in so long as the vbindex is adjusted. A member inside a fixed
3441 // base requires explicit nv adjustment.
3442 llvm::Constant *BaseClassOffset = llvm::ConstantInt::get(
3443 CGM.IntTy,
3444 CGM.computeNonVirtualBaseClassOffset(DerivedClass, PathBegin, PathEnd)
3445 .getQuantity());
3446
3447 llvm::Value *NVDisp;
3448 if (IsDerivedToBase)
3449 NVDisp = Builder.CreateNSWSub(NVAdjustField, BaseClassOffset, "adj");
3450 else
3451 NVDisp = Builder.CreateNSWAdd(NVAdjustField, BaseClassOffset, "adj");
3452
3453 NVAdjustField = Builder.CreateSelect(SrcVBIndexEqZero, NVDisp, getZeroInt());
3454
3455 // Update the vbindex to an appropriate value in the destination because
3456 // SrcRD's vbtable might not be a strict prefix of the one in DstRD.
3457 llvm::Value *DstVBIndexEqZero = SrcVBIndexEqZero;
3458 if (inheritanceModelHasVBTableOffsetField(DstInheritance) &&
3459 inheritanceModelHasVBTableOffsetField(SrcInheritance)) {
3460 if (llvm::GlobalVariable *VDispMap =
3461 getAddrOfVirtualDisplacementMap(SrcRD, DstRD)) {
3462 llvm::Value *VBIndex = Builder.CreateExactUDiv(
3463 VirtualBaseAdjustmentOffset, llvm::ConstantInt::get(CGM.IntTy, 4));
3464 if (IsConstant) {
3465 llvm::Constant *Mapping = VDispMap->getInitializer();
3466 VirtualBaseAdjustmentOffset =
3467 Mapping->getAggregateElement(cast<llvm::Constant>(VBIndex));
3468 } else {
3469 llvm::Value *Idxs[] = {getZeroInt(), VBIndex};
3470 VirtualBaseAdjustmentOffset = Builder.CreateAlignedLoad(
3471 CGM.IntTy, Builder.CreateInBoundsGEP(VDispMap->getValueType(),
3472 VDispMap, Idxs),
3474 }
3475
3476 DstVBIndexEqZero =
3477 Builder.CreateICmpEQ(VirtualBaseAdjustmentOffset, getZeroInt());
3478 }
3479 }
3480
3481 // Set the VBPtrOffset to zero if the vbindex is zero. Otherwise, initialize
3482 // it to the offset of the vbptr.
3483 if (inheritanceModelHasVBPtrOffsetField(DstInheritance)) {
3484 llvm::Value *DstVBPtrOffset = llvm::ConstantInt::getSigned(
3485 CGM.IntTy,
3486 getContext().getASTRecordLayout(DstRD).getVBPtrOffset().getQuantity());
3487 VBPtrOffset =
3488 Builder.CreateSelect(DstVBIndexEqZero, getZeroInt(), DstVBPtrOffset);
3489 }
3490
3491 // Likewise, apply a similar adjustment so that dereferencing the member
3492 // pointer correctly accounts for the distance between the start of the first
3493 // virtual base and the top of the MDC.
3494 if (DstInheritance == MSInheritanceModel::Virtual) {
3495 if (int64_t DstOffsetToFirstVBase =
3496 getContext().getOffsetOfBaseWithVBPtr(DstRD).getQuantity()) {
3497 llvm::Value *DoDstAdjustment = Builder.CreateSelect(
3498 DstVBIndexEqZero,
3499 llvm::ConstantInt::get(CGM.IntTy, DstOffsetToFirstVBase),
3500 getZeroInt());
3501 NVAdjustField = Builder.CreateNSWSub(NVAdjustField, DoDstAdjustment);
3502 }
3503 }
3504
3505 // Recompose dst from the null struct and the adjusted fields from src.
3506 llvm::Value *Dst;
3507 if (inheritanceModelHasOnlyOneField(IsFunc, DstInheritance)) {
3508 Dst = FirstField;
3509 } else {
3510 Dst = llvm::PoisonValue::get(ConvertMemberPointerType(DstTy));
3511 unsigned Idx = 0;
3512 Dst = Builder.CreateInsertValue(Dst, FirstField, Idx++);
3513 if (inheritanceModelHasNVOffsetField(IsFunc, DstInheritance))
3514 Dst = Builder.CreateInsertValue(Dst, NonVirtualBaseAdjustment, Idx++);
3515 if (inheritanceModelHasVBPtrOffsetField(DstInheritance))
3516 Dst = Builder.CreateInsertValue(Dst, VBPtrOffset, Idx++);
3517 if (inheritanceModelHasVBTableOffsetField(DstInheritance))
3518 Dst = Builder.CreateInsertValue(Dst, VirtualBaseAdjustmentOffset, Idx++);
3519 }
3520 return Dst;
3521}
3522
3523llvm::Constant *
3524MicrosoftCXXABI::EmitMemberPointerConversion(const CastExpr *E,
3525 llvm::Constant *Src) {
3526 const MemberPointerType *SrcTy =
3527 E->getSubExpr()->getType()->castAs<MemberPointerType>();
3528 const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>();
3529
3530 CastKind CK = E->getCastKind();
3531
3532 return EmitMemberPointerConversion(SrcTy, DstTy, CK, E->path_begin(),
3533 E->path_end(), Src);
3534}
3535
3536llvm::Constant *MicrosoftCXXABI::EmitMemberPointerConversion(
3537 const MemberPointerType *SrcTy, const MemberPointerType *DstTy, CastKind CK,
3539 CastExpr::path_const_iterator PathEnd, llvm::Constant *Src) {
3540 assert(CK == CK_DerivedToBaseMemberPointer ||
3541 CK == CK_BaseToDerivedMemberPointer ||
3542 CK == CK_ReinterpretMemberPointer);
3543 // If src is null, emit a new null for dst. We can't return src because dst
3544 // might have a new representation.
3545 if (MemberPointerConstantIsNull(SrcTy, Src))
3546 return EmitNullMemberPointer(DstTy);
3547
3548 // We don't need to do anything for reinterpret_casts of non-null member
3549 // pointers. We should only get here when the two type representations have
3550 // the same size.
3551 if (CK == CK_ReinterpretMemberPointer)
3552 return Src;
3553
3554 CGBuilderTy Builder(CGM, CGM.getLLVMContext());
3555 auto *Dst = cast<llvm::Constant>(EmitNonNullMemberPointerConversion(
3556 SrcTy, DstTy, CK, PathBegin, PathEnd, Src, Builder));
3557
3558 return Dst;
3559}
3560
3561CGCallee MicrosoftCXXABI::EmitLoadOfMemberFunctionPointer(
3562 CodeGenFunction &CGF, const Expr *E, Address This,
3563 llvm::Value *&ThisPtrForCall, llvm::Value *MemPtr,
3564 const MemberPointerType *MPT) {
3565 assert(MPT->isMemberFunctionPointer());
3566 const FunctionProtoType *FPT =
3567 MPT->getPointeeType()->castAs<FunctionProtoType>();
3568 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
3569 CGBuilderTy &Builder = CGF.Builder;
3570
3571 MSInheritanceModel Inheritance = RD->getMSInheritanceModel();
3572
3573 // Extract the fields we need, regardless of model. We'll apply them if we
3574 // have them.
3575 llvm::Value *FunctionPointer = MemPtr;
3576 llvm::Value *NonVirtualBaseAdjustment = nullptr;
3577 llvm::Value *VirtualBaseAdjustmentOffset = nullptr;
3578 llvm::Value *VBPtrOffset = nullptr;
3579 if (MemPtr->getType()->isStructTy()) {
3580 // We need to extract values.
3581 unsigned I = 0;
3582 FunctionPointer = Builder.CreateExtractValue(MemPtr, I++);
3583 if (inheritanceModelHasNVOffsetField(MPT, Inheritance))
3584 NonVirtualBaseAdjustment = Builder.CreateExtractValue(MemPtr, I++);
3585 if (inheritanceModelHasVBPtrOffsetField(Inheritance))
3586 VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++);
3588 VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++);
3589 }
3590
3591 if (VirtualBaseAdjustmentOffset) {
3592 ThisPtrForCall = AdjustVirtualBase(CGF, E, RD, This,
3593 VirtualBaseAdjustmentOffset, VBPtrOffset);
3594 } else {
3595 ThisPtrForCall = This.emitRawPointer(CGF);
3596 }
3597
3598 if (NonVirtualBaseAdjustment)
3599 ThisPtrForCall = Builder.CreateInBoundsGEP(CGF.Int8Ty, ThisPtrForCall,
3600 NonVirtualBaseAdjustment);
3601
3602 CGCallee Callee(FPT, FunctionPointer);
3603 return Callee;
3604}
3605
3607 return new MicrosoftCXXABI(CGM);
3608}
3609
3610// MS RTTI Overview:
3611// The run time type information emitted by cl.exe contains 5 distinct types of
3612// structures. Many of them reference each other.
3613//
3614// TypeInfo: Static classes that are returned by typeid.
3615//
3616// CompleteObjectLocator: Referenced by vftables. They contain information
3617// required for dynamic casting, including OffsetFromTop. They also contain
3618// a reference to the TypeInfo for the type and a reference to the
3619// CompleteHierarchyDescriptor for the type.
3620//
3621// ClassHierarchyDescriptor: Contains information about a class hierarchy.
3622// Used during dynamic_cast to walk a class hierarchy. References a base
3623// class array and the size of said array.
3624//
3625// BaseClassArray: Contains a list of classes in a hierarchy. BaseClassArray is
3626// somewhat of a misnomer because the most derived class is also in the list
3627// as well as multiple copies of virtual bases (if they occur multiple times
3628// in the hierarchy.) The BaseClassArray contains one BaseClassDescriptor for
3629// every path in the hierarchy, in pre-order depth first order. Note, we do
3630// not declare a specific llvm type for BaseClassArray, it's merely an array
3631// of BaseClassDescriptor pointers.
3632//
3633// BaseClassDescriptor: Contains information about a class in a class hierarchy.
3634// BaseClassDescriptor is also somewhat of a misnomer for the same reason that
3635// BaseClassArray is. It contains information about a class within a
3636// hierarchy such as: is this base is ambiguous and what is its offset in the
3637// vbtable. The names of the BaseClassDescriptors have all of their fields
3638// mangled into them so they can be aggressively deduplicated by the linker.
3639
3640static llvm::GlobalVariable *getTypeInfoVTable(CodeGenModule &CGM) {
3641 StringRef MangledName("??_7type_info@@6B@");
3642 if (auto VTable = CGM.getModule().getNamedGlobal(MangledName))
3643 return VTable;
3644 return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
3645 /*isConstant=*/true,
3646 llvm::GlobalVariable::ExternalLinkage,
3647 /*Initializer=*/nullptr, MangledName);
3648}
3649
3650namespace {
3651
3652/// A Helper struct that stores information about a class in a class
3653/// hierarchy. The information stored in these structs struct is used during
3654/// the generation of ClassHierarchyDescriptors and BaseClassDescriptors.
3655// During RTTI creation, MSRTTIClasses are stored in a contiguous array with
3656// implicit depth first pre-order tree connectivity. getFirstChild and
3657// getNextSibling allow us to walk the tree efficiently.
3658struct MSRTTIClass {
3659 enum {
3660 IsPrivateOnPath = 1 | 8,
3661 IsAmbiguous = 2,
3662 IsPrivate = 4,
3663 IsVirtual = 16,
3664 HasHierarchyDescriptor = 64
3665 };
3666 MSRTTIClass(const CXXRecordDecl *RD) : RD(RD) {}
3667 uint32_t initialize(const MSRTTIClass *Parent,
3668 const CXXBaseSpecifier *Specifier);
3669
3670 MSRTTIClass *getFirstChild() { return this + 1; }
3671 static MSRTTIClass *getNextChild(MSRTTIClass *Child) {
3672 return Child + 1 + Child->NumBases;
3673 }
3674
3675 const CXXRecordDecl *RD, *VirtualRoot;
3676 uint32_t Flags, NumBases, OffsetInVBase;
3677};
3678
3679/// Recursively initialize the base class array.
3680uint32_t MSRTTIClass::initialize(const MSRTTIClass *Parent,
3681 const CXXBaseSpecifier *Specifier) {
3682 Flags = HasHierarchyDescriptor;
3683 if (!Parent) {
3684 VirtualRoot = nullptr;
3685 OffsetInVBase = 0;
3686 } else {
3687 if (Specifier->getAccessSpecifier() != AS_public)
3688 Flags |= IsPrivate | IsPrivateOnPath;
3689 if (Specifier->isVirtual()) {
3690 Flags |= IsVirtual;
3691 VirtualRoot = RD;
3692 OffsetInVBase = 0;
3693 } else {
3694 if (Parent->Flags & IsPrivateOnPath)
3695 Flags |= IsPrivateOnPath;
3696 VirtualRoot = Parent->VirtualRoot;
3697 OffsetInVBase = Parent->OffsetInVBase + RD->getASTContext()
3698 .getASTRecordLayout(Parent->RD).getBaseClassOffset(RD).getQuantity();
3699 }
3700 }
3701 NumBases = 0;
3702 MSRTTIClass *Child = getFirstChild();
3703 for (const CXXBaseSpecifier &Base : RD->bases()) {
3704 NumBases += Child->initialize(this, &Base) + 1;
3705 Child = getNextChild(Child);
3706 }
3707 return NumBases;
3708}
3709
3710static llvm::GlobalValue::LinkageTypes getLinkageForRTTI(QualType Ty) {
3711 switch (Ty->getLinkage()) {
3712 case Linkage::Invalid:
3713 llvm_unreachable("Linkage hasn't been computed!");
3714
3715 case Linkage::None:
3716 case Linkage::Internal:
3717 case Linkage::UniqueExternal:
3718 return llvm::GlobalValue::InternalLinkage;
3719
3720 case Linkage::VisibleNone:
3721 case Linkage::Module:
3722 case Linkage::External:
3723 return llvm::GlobalValue::LinkOnceODRLinkage;
3724 }
3725 llvm_unreachable("Invalid linkage!");
3726}
3727
3728/// An ephemeral helper class for building MS RTTI types. It caches some
3729/// calls to the module and information about the most derived class in a
3730/// hierarchy.
3731struct MSRTTIBuilder {
3732 enum {
3733 HasBranchingHierarchy = 1,
3734 HasVirtualBranchingHierarchy = 2,
3735 HasAmbiguousBases = 4
3736 };
3737
3738 MSRTTIBuilder(MicrosoftCXXABI &ABI, const CXXRecordDecl *RD)
3739 : CGM(ABI.CGM), Context(CGM.getContext()),
3740 VMContext(CGM.getLLVMContext()), Module(CGM.getModule()), RD(RD),
3741 Linkage(getLinkageForRTTI(CGM.getContext().getCanonicalTagType(RD))),
3742 ABI(ABI) {}
3743
3744 llvm::GlobalVariable *getBaseClassDescriptor(const MSRTTIClass &Classes);
3745 llvm::GlobalVariable *
3746 getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes);
3747 llvm::GlobalVariable *getClassHierarchyDescriptor();
3748 llvm::GlobalVariable *getCompleteObjectLocator(const VPtrInfo &Info);
3749
3750 CodeGenModule &CGM;
3751 ASTContext &Context;
3752 llvm::LLVMContext &VMContext;
3753 llvm::Module &Module;
3754 const CXXRecordDecl *RD;
3755 llvm::GlobalVariable::LinkageTypes Linkage;
3756 MicrosoftCXXABI &ABI;
3757};
3758
3759} // namespace
3760
3761/// Recursively serializes a class hierarchy in pre-order depth first
3762/// order.
3764 const CXXRecordDecl *RD) {
3765 Classes.push_back(MSRTTIClass(RD));
3766 for (const CXXBaseSpecifier &Base : RD->bases())
3767 serializeClassHierarchy(Classes, Base.getType()->getAsCXXRecordDecl());
3768}
3769
3770/// Find ambiguity among base classes.
3771static void
3776 for (MSRTTIClass *Class = &Classes.front(); Class <= &Classes.back();) {
3777 if ((Class->Flags & MSRTTIClass::IsVirtual) &&
3778 !VirtualBases.insert(Class->RD).second) {
3779 Class = MSRTTIClass::getNextChild(Class);
3780 continue;
3781 }
3782 if (!UniqueBases.insert(Class->RD).second)
3783 AmbiguousBases.insert(Class->RD);
3784 Class++;
3785 }
3786 if (AmbiguousBases.empty())
3787 return;
3788 for (MSRTTIClass &Class : Classes)
3789 if (AmbiguousBases.count(Class.RD))
3790 Class.Flags |= MSRTTIClass::IsAmbiguous;
3791}
3792
3793llvm::GlobalVariable *MSRTTIBuilder::getClassHierarchyDescriptor() {
3794 SmallString<256> MangledName;
3795 {
3796 llvm::raw_svector_ostream Out(MangledName);
3797 ABI.getMangleContext().mangleCXXRTTIClassHierarchyDescriptor(RD, Out);
3798 }
3799
3800 // Check to see if we've already declared this ClassHierarchyDescriptor.
3801 if (auto CHD = Module.getNamedGlobal(MangledName))
3802 return CHD;
3803
3804 // Serialize the class hierarchy and initialize the CHD Fields.
3805 SmallVector<MSRTTIClass, 8> Classes;
3806 serializeClassHierarchy(Classes, RD);
3807 Classes.front().initialize(/*Parent=*/nullptr, /*Specifier=*/nullptr);
3808 detectAmbiguousBases(Classes);
3809 int Flags = 0;
3810 for (const MSRTTIClass &Class : Classes) {
3811 if (Class.RD->getNumBases() > 1)
3812 Flags |= HasBranchingHierarchy;
3813 // Note: cl.exe does not calculate "HasAmbiguousBases" correctly. We
3814 // believe the field isn't actually used.
3815 if (Class.Flags & MSRTTIClass::IsAmbiguous)
3816 Flags |= HasAmbiguousBases;
3817 }
3818 if ((Flags & HasBranchingHierarchy) && RD->getNumVBases() != 0)
3819 Flags |= HasVirtualBranchingHierarchy;
3820 // These gep indices are used to get the address of the first element of the
3821 // base class array.
3822 llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0),
3823 llvm::ConstantInt::get(CGM.IntTy, 0)};
3824
3825 // Forward-declare the class hierarchy descriptor
3826 auto Type = ABI.getClassHierarchyDescriptorType();
3827 auto CHD = new llvm::GlobalVariable(Module, Type, /*isConstant=*/true, Linkage,
3828 /*Initializer=*/nullptr,
3829 MangledName);
3830 if (CHD->isWeakForLinker())
3831 CHD->setComdat(CGM.getModule().getOrInsertComdat(CHD->getName()));
3832
3833 auto *Bases = getBaseClassArray(Classes);
3834
3835 // Initialize the base class ClassHierarchyDescriptor.
3836 llvm::Constant *Fields[] = {
3837 llvm::ConstantInt::get(CGM.IntTy, 0), // reserved by the runtime
3838 llvm::ConstantInt::get(CGM.IntTy, Flags),
3839 llvm::ConstantInt::get(CGM.IntTy, Classes.size()),
3840 ABI.getImageRelativeConstant(llvm::ConstantExpr::getInBoundsGetElementPtr(
3841 Bases->getValueType(), Bases,
3842 llvm::ArrayRef<llvm::Value *>(GEPIndices))),
3843 };
3844 CHD->setInitializer(llvm::ConstantStruct::get(Type, Fields));
3845 return CHD;
3846}
3847
3848llvm::GlobalVariable *
3849MSRTTIBuilder::getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes) {
3850 SmallString<256> MangledName;
3851 {
3852 llvm::raw_svector_ostream Out(MangledName);
3853 ABI.getMangleContext().mangleCXXRTTIBaseClassArray(RD, Out);
3854 }
3855
3856 // Forward-declare the base class array.
3857 // cl.exe pads the base class array with 1 (in 32 bit mode) or 4 (in 64 bit
3858 // mode) bytes of padding. We provide a pointer sized amount of padding by
3859 // adding +1 to Classes.size(). The sections have pointer alignment and are
3860 // marked pick-any so it shouldn't matter.
3861 llvm::Type *PtrType = ABI.getImageRelativeType(CGM.DefaultPtrTy);
3862 auto *ArrType = llvm::ArrayType::get(PtrType, Classes.size() + 1);
3863 auto *BCA =
3864 new llvm::GlobalVariable(Module, ArrType,
3865 /*isConstant=*/true, Linkage,
3866 /*Initializer=*/nullptr, MangledName);
3867 if (BCA->isWeakForLinker())
3868 BCA->setComdat(CGM.getModule().getOrInsertComdat(BCA->getName()));
3869
3870 // Initialize the BaseClassArray.
3871 SmallVector<llvm::Constant *, 8> BaseClassArrayData;
3872 for (MSRTTIClass &Class : Classes)
3873 BaseClassArrayData.push_back(
3874 ABI.getImageRelativeConstant(getBaseClassDescriptor(Class)));
3875 BaseClassArrayData.push_back(llvm::Constant::getNullValue(PtrType));
3876 BCA->setInitializer(llvm::ConstantArray::get(ArrType, BaseClassArrayData));
3877 return BCA;
3878}
3879
3880llvm::GlobalVariable *
3881MSRTTIBuilder::getBaseClassDescriptor(const MSRTTIClass &Class) {
3882 // Compute the fields for the BaseClassDescriptor. They are computed up front
3883 // because they are mangled into the name of the object.
3884 uint32_t OffsetInVBTable = 0;
3885 int32_t VBPtrOffset = -1;
3886 if (Class.VirtualRoot) {
3887 auto &VTableContext = CGM.getMicrosoftVTableContext();
3888 OffsetInVBTable = VTableContext.getVBTableIndex(RD, Class.VirtualRoot) * 4;
3889 VBPtrOffset = Context.getASTRecordLayout(RD).getVBPtrOffset().getQuantity();
3890 }
3891
3892 SmallString<256> MangledName;
3893 {
3894 llvm::raw_svector_ostream Out(MangledName);
3895 ABI.getMangleContext().mangleCXXRTTIBaseClassDescriptor(
3896 Class.RD, Class.OffsetInVBase, VBPtrOffset, OffsetInVBTable,
3897 Class.Flags, Out);
3898 }
3899
3900 // Check to see if we've already declared this object.
3901 if (auto BCD = Module.getNamedGlobal(MangledName))
3902 return BCD;
3903
3904 // Forward-declare the base class descriptor.
3905 auto Type = ABI.getBaseClassDescriptorType();
3906 auto BCD =
3907 new llvm::GlobalVariable(Module, Type, /*isConstant=*/true, Linkage,
3908 /*Initializer=*/nullptr, MangledName);
3909 if (BCD->isWeakForLinker())
3910 BCD->setComdat(CGM.getModule().getOrInsertComdat(BCD->getName()));
3911
3912 // Initialize the BaseClassDescriptor.
3913 llvm::Constant *Fields[] = {
3914 ABI.getImageRelativeConstant(
3915 ABI.getAddrOfRTTIDescriptor(Context.getCanonicalTagType(Class.RD))),
3916 llvm::ConstantInt::get(CGM.IntTy, Class.NumBases),
3917 llvm::ConstantInt::get(CGM.IntTy, Class.OffsetInVBase),
3918 llvm::ConstantInt::getSigned(CGM.IntTy, VBPtrOffset),
3919 llvm::ConstantInt::get(CGM.IntTy, OffsetInVBTable),
3920 llvm::ConstantInt::get(CGM.IntTy, Class.Flags),
3921 ABI.getImageRelativeConstant(
3922 MSRTTIBuilder(ABI, Class.RD).getClassHierarchyDescriptor()),
3923 };
3924 BCD->setInitializer(llvm::ConstantStruct::get(Type, Fields));
3925 return BCD;
3926}
3927
3928llvm::GlobalVariable *
3929MSRTTIBuilder::getCompleteObjectLocator(const VPtrInfo &Info) {
3930 SmallString<256> MangledName;
3931 {
3932 llvm::raw_svector_ostream Out(MangledName);
3933 ABI.getMangleContext().mangleCXXRTTICompleteObjectLocator(RD, Info.MangledPath, Out);
3934 }
3935
3936 // Check to see if we've already computed this complete object locator.
3937 if (auto COL = Module.getNamedGlobal(MangledName))
3938 return COL;
3939
3940 // Compute the fields of the complete object locator.
3941 int OffsetToTop = Info.FullOffsetInMDC.getQuantity();
3942 int VFPtrOffset = 0;
3943 // The offset includes the vtordisp if one exists.
3944 if (const CXXRecordDecl *VBase = Info.getVBaseWithVPtr())
3945 if (Context.getASTRecordLayout(RD)
3947 .find(VBase)
3948 ->second.hasVtorDisp())
3949 VFPtrOffset = Info.NonVirtualOffset.getQuantity() + 4;
3950
3951 // Forward-declare the complete object locator.
3952 llvm::StructType *Type = ABI.getCompleteObjectLocatorType();
3953 auto COL = new llvm::GlobalVariable(Module, Type, /*isConstant=*/true, Linkage,
3954 /*Initializer=*/nullptr, MangledName);
3955
3956 // Initialize the CompleteObjectLocator.
3957 llvm::Constant *Fields[] = {
3958 llvm::ConstantInt::get(CGM.IntTy, ABI.isImageRelative()),
3959 llvm::ConstantInt::get(CGM.IntTy, OffsetToTop),
3960 llvm::ConstantInt::get(CGM.IntTy, VFPtrOffset),
3961 ABI.getImageRelativeConstant(
3963 ABI.getImageRelativeConstant(getClassHierarchyDescriptor()),
3964 ABI.getImageRelativeConstant(COL),
3965 };
3966 llvm::ArrayRef<llvm::Constant *> FieldsRef(Fields);
3967 if (!ABI.isImageRelative())
3968 FieldsRef = FieldsRef.drop_back();
3969 COL->setInitializer(llvm::ConstantStruct::get(Type, FieldsRef));
3970 if (COL->isWeakForLinker())
3971 COL->setComdat(CGM.getModule().getOrInsertComdat(COL->getName()));
3972 return COL;
3973}
3974
3976 bool &IsConst, bool &IsVolatile,
3977 bool &IsUnaligned) {
3978 T = Context.getExceptionObjectType(T);
3979
3980 // C++14 [except.handle]p3:
3981 // A handler is a match for an exception object of type E if [...]
3982 // - the handler is of type cv T or const T& where T is a pointer type and
3983 // E is a pointer type that can be converted to T by [...]
3984 // - a qualification conversion
3985 IsConst = false;
3986 IsVolatile = false;
3987 IsUnaligned = false;
3988 QualType PointeeType = T->getPointeeType();
3989 if (!PointeeType.isNull()) {
3990 IsConst = PointeeType.isConstQualified();
3991 IsVolatile = PointeeType.isVolatileQualified();
3992 IsUnaligned = PointeeType.getQualifiers().hasUnaligned();
3993 }
3994
3995 // Member pointer types like "const int A::*" are represented by having RTTI
3996 // for "int A::*" and separately storing the const qualifier.
3997 if (const auto *MPTy = T->getAs<MemberPointerType>())
3998 T = Context.getMemberPointerType(PointeeType.getUnqualifiedType(),
3999 MPTy->getQualifier(),
4000 MPTy->getMostRecentCXXRecordDecl());
4001
4002 // Pointer types like "const int * const *" are represented by having RTTI
4003 // for "const int **" and separately storing the const qualifier.
4004 if (T->isPointerType())
4005 T = Context.getPointerType(PointeeType.getUnqualifiedType());
4006
4007 return T;
4008}
4009
4010CatchTypeInfo
4011MicrosoftCXXABI::getAddrOfCXXCatchHandlerType(QualType Type,
4012 QualType CatchHandlerType) {
4013 // TypeDescriptors for exceptions never have qualified pointer types,
4014 // qualifiers are stored separately in order to support qualification
4015 // conversions.
4016 bool IsConst, IsVolatile, IsUnaligned;
4017 Type =
4018 decomposeTypeForEH(getContext(), Type, IsConst, IsVolatile, IsUnaligned);
4019
4020 bool IsReference = CatchHandlerType->isReferenceType();
4021
4022 uint32_t Flags = 0;
4023 if (IsConst)
4024 Flags |= 1;
4025 if (IsVolatile)
4026 Flags |= 2;
4027 if (IsUnaligned)
4028 Flags |= 4;
4029 if (IsReference)
4030 Flags |= 8;
4031
4032 return CatchTypeInfo{getAddrOfRTTIDescriptor(Type)->stripPointerCasts(),
4033 Flags};
4034}
4035
4036/// Gets a TypeDescriptor. Returns a llvm::Constant * rather than a
4037/// llvm::GlobalVariable * because different type descriptors have different
4038/// types, and need to be abstracted. They are abstracting by casting the
4039/// address to an Int8PtrTy.
4040llvm::Constant *MicrosoftCXXABI::getAddrOfRTTIDescriptor(QualType Type) {
4041 SmallString<256> MangledName;
4042 {
4043 llvm::raw_svector_ostream Out(MangledName);
4044 getMangleContext().mangleCXXRTTI(Type, Out);
4045 }
4046
4047 // Check to see if we've already declared this TypeDescriptor.
4048 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName))
4049 return GV;
4050
4051 // Note for the future: If we would ever like to do deferred emission of
4052 // RTTI, check if emitting vtables opportunistically need any adjustment.
4053
4054 // Compute the fields for the TypeDescriptor.
4055 SmallString<256> TypeInfoString;
4056 {
4057 llvm::raw_svector_ostream Out(TypeInfoString);
4058 getMangleContext().mangleCXXRTTIName(Type, Out);
4059 }
4060
4061 // Declare and initialize the TypeDescriptor.
4062 llvm::Constant *Fields[] = {
4063 getTypeInfoVTable(CGM), // VFPtr
4064 llvm::ConstantPointerNull::get(CGM.Int8PtrTy), // Runtime data
4065 llvm::ConstantDataArray::getString(CGM.getLLVMContext(), TypeInfoString)};
4066 llvm::StructType *TypeDescriptorType =
4067 getTypeDescriptorType(TypeInfoString);
4068 auto *Var = new llvm::GlobalVariable(
4069 CGM.getModule(), TypeDescriptorType, /*isConstant=*/false,
4070 getLinkageForRTTI(Type),
4071 llvm::ConstantStruct::get(TypeDescriptorType, Fields),
4072 MangledName);
4073 if (Var->isWeakForLinker())
4074 Var->setComdat(CGM.getModule().getOrInsertComdat(Var->getName()));
4075 return Var;
4076}
4077
4078/// Gets or a creates a Microsoft CompleteObjectLocator.
4079llvm::GlobalVariable *
4080MicrosoftCXXABI::getMSCompleteObjectLocator(const CXXRecordDecl *RD,
4081 const VPtrInfo &Info) {
4082 return MSRTTIBuilder(*this, RD).getCompleteObjectLocator(Info);
4083}
4084
4085void MicrosoftCXXABI::emitCXXStructor(GlobalDecl GD) {
4086 if (auto *ctor = dyn_cast<CXXConstructorDecl>(GD.getDecl())) {
4087 // There are no constructor variants, always emit the complete destructor.
4088 llvm::Function *Fn =
4090 CGM.maybeSetTrivialComdat(*ctor, *Fn);
4091 return;
4092 }
4093
4094 auto *dtor = cast<CXXDestructorDecl>(GD.getDecl());
4095
4096 // Emit the base destructor if the base and complete (vbase) destructors are
4097 // equivalent. This effectively implements -mconstructor-aliases as part of
4098 // the ABI.
4099 if (GD.getDtorType() == Dtor_Complete &&
4100 dtor->getParent()->getNumVBases() == 0)
4101 GD = GD.getWithDtorType(Dtor_Base);
4102
4103 // The base destructor is equivalent to the base destructor of its
4104 // base class if there is exactly one non-virtual base class with a
4105 // non-trivial destructor, there are no fields with a non-trivial
4106 // destructor, and the body of the destructor is trivial.
4107 if (GD.getDtorType() == Dtor_Base && !CGM.TryEmitBaseDestructorAsAlias(dtor))
4108 return;
4109
4110 if (GD.getDtorType() == Dtor_VectorDeleting &&
4111 !CGM.classNeedsVectorDestructor(dtor->getParent())) {
4112 // Create GlobalDecl object with the correct type for the scalar
4113 // deleting destructor.
4114 GlobalDecl ScalarDtorGD(dtor, Dtor_Deleting);
4115
4116 // Emit an alias from the vector deleting destructor to the scalar deleting
4117 // destructor.
4118 CGM.EmitDefinitionAsAlias(GD, ScalarDtorGD);
4119 return;
4120 }
4121
4122 llvm::Function *Fn = CGM.codegenCXXStructor(GD);
4123 if (Fn->isWeakForLinker())
4124 Fn->setComdat(CGM.getModule().getOrInsertComdat(Fn->getName()));
4125}
4126
4127llvm::Function *
4128MicrosoftCXXABI::getAddrOfCXXCtorClosure(const CXXConstructorDecl *CD,
4129 CXXCtorType CT) {
4130 assert(CT == Ctor_CopyingClosure || CT == Ctor_DefaultClosure);
4131
4132 // Calculate the mangled name.
4133 SmallString<256> ThunkName;
4134 llvm::raw_svector_ostream Out(ThunkName);
4135 getMangleContext().mangleName(GlobalDecl(CD, CT), Out);
4136
4137 // If the thunk has been generated previously, just return it.
4138 if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(ThunkName))
4139 return cast<llvm::Function>(GV);
4140
4141 // Create the llvm::Function.
4142 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeMSCtorClosure(CD, CT);
4143 llvm::FunctionType *ThunkTy = CGM.getTypes().GetFunctionType(FnInfo);
4144 const CXXRecordDecl *RD = CD->getParent();
4145 CanQualType RecordTy = getContext().getCanonicalTagType(RD);
4146 llvm::Function *ThunkFn = llvm::Function::Create(
4147 ThunkTy, getLinkageForRTTI(RecordTy), ThunkName.str(), &CGM.getModule());
4148 ThunkFn->setCallingConv(static_cast<llvm::CallingConv::ID>(
4150 if (ThunkFn->isWeakForLinker())
4151 ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(ThunkFn->getName()));
4152 bool IsCopy = CT == Ctor_CopyingClosure;
4153
4154 // Start codegen.
4155 CodeGenFunction CGF(CGM);
4156 CGF.CurGD = GlobalDecl(CD, Ctor_Complete);
4157
4158 // Build FunctionArgs.
4159 FunctionArgList FunctionArgs;
4160
4161 // A constructor always starts with a 'this' pointer as its first argument.
4162 buildThisParam(CGF, FunctionArgs);
4163
4164 // Following the 'this' pointer is a reference to the source object that we
4165 // are copying from.
4166 auto *SrcParam = ImplicitParamDecl::Create(
4167 getContext(), /*DC=*/nullptr, SourceLocation(),
4168 &getContext().Idents.get("src"),
4169 getContext().getLValueReferenceType(RecordTy,
4170 /*SpelledAsLValue=*/true),
4171 ImplicitParamKind::Other);
4172 if (IsCopy)
4173 FunctionArgs.push_back(SrcParam);
4174
4175 // Constructors for classes which utilize virtual bases have an additional
4176 // parameter which indicates whether or not it is being delegated to by a more
4177 // derived constructor.
4178 auto *IsMostDerived =
4179 ImplicitParamDecl::Create(getContext(), /*DC=*/nullptr, SourceLocation(),
4180 &getContext().Idents.get("is_most_derived"),
4181 getContext().IntTy, ImplicitParamKind::Other);
4182 // Only add the parameter to the list if the class has virtual bases.
4183 if (RD->getNumVBases() > 0)
4184 FunctionArgs.push_back(IsMostDerived);
4185
4186 // Start defining the function.
4187 auto NL = ApplyDebugLocation::CreateEmpty(CGF);
4188 CGF.StartFunction(GlobalDecl(), FnInfo.getReturnType(), ThunkFn, FnInfo,
4189 FunctionArgs, CD->getLocation(), SourceLocation());
4190 // Create a scope with an artificial location for the body of this function.
4192 setCXXABIThisValue(CGF, loadIncomingCXXThis(CGF));
4193 llvm::Value *This = getThisValue(CGF);
4194
4195 llvm::Value *SrcVal =
4196 IsCopy ? CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(SrcParam), "src")
4197 : nullptr;
4198
4199 CallArgList Args;
4200
4201 // Push the this ptr.
4202 Args.add(RValue::get(This), CD->getThisType());
4203
4204 // Push the src ptr.
4205 if (SrcVal)
4206 Args.add(RValue::get(SrcVal), SrcParam->getType());
4207
4208 // Add the rest of the default arguments.
4209 SmallVector<const Stmt *, 4> ArgVec;
4210 ArrayRef<ParmVarDecl *> params = CD->parameters().drop_front(IsCopy ? 1 : 0);
4211 for (const ParmVarDecl *PD : params) {
4212 assert(PD->hasDefaultArg() && "ctor closure lacks default args");
4213 ArgVec.push_back(PD->getDefaultArg());
4214 }
4215
4216 CodeGenFunction::RunCleanupsScope Cleanups(CGF);
4217
4218 const auto *FPT = CD->getType()->castAs<FunctionProtoType>();
4219 CGF.EmitCallArgs(Args, FPT, llvm::ArrayRef(ArgVec), CD, IsCopy ? 1 : 0);
4220
4221 // Insert any ABI-specific implicit constructor arguments.
4222 AddedStructorArgCounts ExtraArgs =
4223 addImplicitConstructorArgs(CGF, CD, Ctor_Complete,
4224 /*ForVirtualBase=*/false,
4225 /*Delegating=*/false, Args);
4226 // Call the destructor with our arguments.
4227 llvm::Constant *CalleePtr =
4228 CGM.getAddrOfCXXStructor(GlobalDecl(CD, Ctor_Complete));
4229 CGCallee Callee =
4230 CGCallee::forDirect(CalleePtr, GlobalDecl(CD, Ctor_Complete));
4231 const CGFunctionInfo &CalleeInfo = CGM.getTypes().arrangeCXXConstructorCall(
4232 Args, CD, Ctor_Complete, ExtraArgs.Prefix, ExtraArgs.Suffix);
4233 CGF.EmitCall(CalleeInfo, Callee, ReturnValueSlot(), Args);
4234
4235 Cleanups.ForceCleanup();
4236
4237 // Emit the ret instruction, remove any temporary instructions created for the
4238 // aid of CodeGen.
4239 CGF.FinishFunction(SourceLocation());
4240
4241 return ThunkFn;
4242}
4243
4244llvm::Constant *MicrosoftCXXABI::getCatchableType(QualType T,
4245 uint32_t NVOffset,
4246 int32_t VBPtrOffset,
4247 uint32_t VBIndex) {
4248 assert(!T->isReferenceType());
4249
4250 CXXRecordDecl *RD = T->getAsCXXRecordDecl();
4251 const CXXConstructorDecl *CD =
4252 RD ? CGM.getContext().getCopyConstructorForExceptionObject(RD) : nullptr;
4254 if (CD)
4255 if (!hasDefaultCXXMethodCC(getContext(), CD) || CD->getNumParams() != 1)
4257
4258 uint32_t Size = getContext().getTypeSizeInChars(T).getQuantity();
4259 SmallString<256> MangledName;
4260 {
4261 llvm::raw_svector_ostream Out(MangledName);
4262 getMangleContext().mangleCXXCatchableType(T, CD, CT, Size, NVOffset,
4263 VBPtrOffset, VBIndex, Out);
4264 }
4265 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName))
4266 return getImageRelativeConstant(GV);
4267
4268 // The TypeDescriptor is used by the runtime to determine if a catch handler
4269 // is appropriate for the exception object.
4270 llvm::Constant *TD = getImageRelativeConstant(getAddrOfRTTIDescriptor(T));
4271
4272 // The runtime is responsible for calling the copy constructor if the
4273 // exception is caught by value.
4274 llvm::Constant *CopyCtor;
4275 if (CD) {
4276 if (CT == Ctor_CopyingClosure)
4277 CopyCtor = getAddrOfCXXCtorClosure(CD, Ctor_CopyingClosure);
4278 else
4279 CopyCtor = CGM.getAddrOfCXXStructor(GlobalDecl(CD, Ctor_Complete));
4280 } else {
4281 CopyCtor = llvm::Constant::getNullValue(CGM.Int8PtrTy);
4282 }
4283 CopyCtor = getImageRelativeConstant(CopyCtor);
4284
4285 bool IsScalar = !RD;
4286 bool HasVirtualBases = false;
4287 bool IsStdBadAlloc = false; // std::bad_alloc is special for some reason.
4288 QualType PointeeType = T;
4289 if (T->isPointerType())
4290 PointeeType = T->getPointeeType();
4291 if (const CXXRecordDecl *RD = PointeeType->getAsCXXRecordDecl()) {
4292 HasVirtualBases = RD->getNumVBases() > 0;
4293 if (IdentifierInfo *II = RD->getIdentifier())
4294 IsStdBadAlloc = II->isStr("bad_alloc") && RD->isInStdNamespace();
4295 }
4296
4297 // Encode the relevant CatchableType properties into the Flags bitfield.
4298 // FIXME: Figure out how bits 2 or 8 can get set.
4299 uint32_t Flags = 0;
4300 if (IsScalar)
4301 Flags |= 1;
4302 if (HasVirtualBases)
4303 Flags |= 4;
4304 if (IsStdBadAlloc)
4305 Flags |= 16;
4306
4307 llvm::Constant *Fields[] = {
4308 llvm::ConstantInt::get(CGM.IntTy, Flags), // Flags
4309 TD, // TypeDescriptor
4310 llvm::ConstantInt::get(CGM.IntTy, NVOffset), // NonVirtualAdjustment
4311 llvm::ConstantInt::getSigned(CGM.IntTy, VBPtrOffset), // OffsetToVBPtr
4312 llvm::ConstantInt::get(CGM.IntTy, VBIndex), // VBTableIndex
4313 llvm::ConstantInt::get(CGM.IntTy, Size), // Size
4314 CopyCtor // CopyCtor
4315 };
4316 llvm::StructType *CTType = getCatchableTypeType();
4317 auto *GV = new llvm::GlobalVariable(
4318 CGM.getModule(), CTType, /*isConstant=*/true, getLinkageForRTTI(T),
4319 llvm::ConstantStruct::get(CTType, Fields), MangledName);
4320 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
4321 GV->setSection(".xdata");
4322 if (GV->isWeakForLinker())
4323 GV->setComdat(CGM.getModule().getOrInsertComdat(GV->getName()));
4324 return getImageRelativeConstant(GV);
4325}
4326
4327llvm::GlobalVariable *MicrosoftCXXABI::getCatchableTypeArray(QualType T) {
4328 assert(!T->isReferenceType());
4329
4330 // See if we've already generated a CatchableTypeArray for this type before.
4331 llvm::GlobalVariable *&CTA = CatchableTypeArrays[T];
4332 if (CTA)
4333 return CTA;
4334
4335 // Ensure that we don't have duplicate entries in our CatchableTypeArray by
4336 // using a SmallSetVector. Duplicates may arise due to virtual bases
4337 // occurring more than once in the hierarchy.
4338 llvm::SmallSetVector<llvm::Constant *, 2> CatchableTypes;
4339
4340 // C++14 [except.handle]p3:
4341 // A handler is a match for an exception object of type E if [...]
4342 // - the handler is of type cv T or cv T& and T is an unambiguous public
4343 // base class of E, or
4344 // - the handler is of type cv T or const T& where T is a pointer type and
4345 // E is a pointer type that can be converted to T by [...]
4346 // - a standard pointer conversion (4.10) not involving conversions to
4347 // pointers to private or protected or ambiguous classes
4348 const CXXRecordDecl *MostDerivedClass = nullptr;
4349 bool IsPointer = T->isPointerType();
4350 if (IsPointer)
4351 MostDerivedClass = T->getPointeeType()->getAsCXXRecordDecl();
4352 else
4353 MostDerivedClass = T->getAsCXXRecordDecl();
4354
4355 // Collect all the unambiguous public bases of the MostDerivedClass.
4356 if (MostDerivedClass) {
4357 const ASTContext &Context = getContext();
4358 const ASTRecordLayout &MostDerivedLayout =
4359 Context.getASTRecordLayout(MostDerivedClass);
4360 MicrosoftVTableContext &VTableContext = CGM.getMicrosoftVTableContext();
4361 SmallVector<MSRTTIClass, 8> Classes;
4362 serializeClassHierarchy(Classes, MostDerivedClass);
4363 Classes.front().initialize(/*Parent=*/nullptr, /*Specifier=*/nullptr);
4364 detectAmbiguousBases(Classes);
4365 for (const MSRTTIClass &Class : Classes) {
4366 // Skip any ambiguous or private bases.
4367 if (Class.Flags &
4368 (MSRTTIClass::IsPrivateOnPath | MSRTTIClass::IsAmbiguous))
4369 continue;
4370 // Write down how to convert from a derived pointer to a base pointer.
4371 uint32_t OffsetInVBTable = 0;
4372 int32_t VBPtrOffset = -1;
4373 if (Class.VirtualRoot) {
4374 OffsetInVBTable =
4375 VTableContext.getVBTableIndex(MostDerivedClass, Class.VirtualRoot)*4;
4376 VBPtrOffset = MostDerivedLayout.getVBPtrOffset().getQuantity();
4377 }
4378
4379 // Turn our record back into a pointer if the exception object is a
4380 // pointer.
4381 CanQualType RTTITy = Context.getCanonicalTagType(Class.RD);
4382 if (IsPointer)
4383 RTTITy = Context.getPointerType(RTTITy);
4384 CatchableTypes.insert(getCatchableType(RTTITy, Class.OffsetInVBase,
4385 VBPtrOffset, OffsetInVBTable));
4386 }
4387 }
4388
4389 // C++14 [except.handle]p3:
4390 // A handler is a match for an exception object of type E if
4391 // - The handler is of type cv T or cv T& and E and T are the same type
4392 // (ignoring the top-level cv-qualifiers)
4393 CatchableTypes.insert(getCatchableType(T));
4394
4395 // C++14 [except.handle]p3:
4396 // A handler is a match for an exception object of type E if
4397 // - the handler is of type cv T or const T& where T is a pointer type and
4398 // E is a pointer type that can be converted to T by [...]
4399 // - a standard pointer conversion (4.10) not involving conversions to
4400 // pointers to private or protected or ambiguous classes
4401 //
4402 // C++14 [conv.ptr]p2:
4403 // A prvalue of type "pointer to cv T," where T is an object type, can be
4404 // converted to a prvalue of type "pointer to cv void".
4405 if (IsPointer && T->getPointeeType()->isObjectType())
4406 CatchableTypes.insert(getCatchableType(getContext().VoidPtrTy));
4407
4408 // C++14 [except.handle]p3:
4409 // A handler is a match for an exception object of type E if [...]
4410 // - the handler is of type cv T or const T& where T is a pointer or
4411 // pointer to member type and E is std::nullptr_t.
4412 //
4413 // We cannot possibly list all possible pointer types here, making this
4414 // implementation incompatible with the standard. However, MSVC includes an
4415 // entry for pointer-to-void in this case. Let's do the same.
4416 if (T->isNullPtrType())
4417 CatchableTypes.insert(getCatchableType(getContext().VoidPtrTy));
4418
4419 uint32_t NumEntries = CatchableTypes.size();
4420 llvm::Type *CTType = getImageRelativeType(CGM.DefaultPtrTy);
4421 llvm::ArrayType *AT = llvm::ArrayType::get(CTType, NumEntries);
4422 llvm::StructType *CTAType = getCatchableTypeArrayType(NumEntries);
4423 llvm::Constant *Fields[] = {
4424 llvm::ConstantInt::get(CGM.IntTy, NumEntries), // NumEntries
4425 llvm::ConstantArray::get(
4426 AT, llvm::ArrayRef(CatchableTypes.begin(),
4427 CatchableTypes.end())) // CatchableTypes
4428 };
4429 SmallString<256> MangledName;
4430 {
4431 llvm::raw_svector_ostream Out(MangledName);
4432 getMangleContext().mangleCXXCatchableTypeArray(T, NumEntries, Out);
4433 }
4434 CTA = new llvm::GlobalVariable(
4435 CGM.getModule(), CTAType, /*isConstant=*/true, getLinkageForRTTI(T),
4436 llvm::ConstantStruct::get(CTAType, Fields), MangledName);
4437 CTA->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
4438 CTA->setSection(".xdata");
4439 if (CTA->isWeakForLinker())
4440 CTA->setComdat(CGM.getModule().getOrInsertComdat(CTA->getName()));
4441 return CTA;
4442}
4443
4444llvm::GlobalVariable *MicrosoftCXXABI::getThrowInfo(QualType T) {
4445 bool IsConst, IsVolatile, IsUnaligned;
4446 T = decomposeTypeForEH(getContext(), T, IsConst, IsVolatile, IsUnaligned);
4447
4448 // The CatchableTypeArray enumerates the various (CV-unqualified) types that
4449 // the exception object may be caught as.
4450 llvm::GlobalVariable *CTA = getCatchableTypeArray(T);
4451 // The first field in a CatchableTypeArray is the number of CatchableTypes.
4452 // This is used as a component of the mangled name which means that we need to
4453 // know what it is in order to see if we have previously generated the
4454 // ThrowInfo.
4455 uint32_t NumEntries =
4456 cast<llvm::ConstantInt>(CTA->getInitializer()->getAggregateElement(0U))
4457 ->getLimitedValue();
4458
4459 SmallString<256> MangledName;
4460 {
4461 llvm::raw_svector_ostream Out(MangledName);
4462 getMangleContext().mangleCXXThrowInfo(T, IsConst, IsVolatile, IsUnaligned,
4463 NumEntries, Out);
4464 }
4465
4466 // Reuse a previously generated ThrowInfo if we have generated an appropriate
4467 // one before.
4468 if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName))
4469 return GV;
4470
4471 // The RTTI TypeDescriptor uses an unqualified type but catch clauses must
4472 // be at least as CV qualified. Encode this requirement into the Flags
4473 // bitfield.
4474 uint32_t Flags = 0;
4475 if (IsConst)
4476 Flags |= 1;
4477 if (IsVolatile)
4478 Flags |= 2;
4479 if (IsUnaligned)
4480 Flags |= 4;
4481
4482 // The cleanup-function (a destructor) must be called when the exception
4483 // object's lifetime ends.
4484 llvm::Constant *CleanupFn = llvm::Constant::getNullValue(CGM.Int8PtrTy);
4485 if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
4486 if (CXXDestructorDecl *DtorD = RD->getDestructor())
4487 if (!DtorD->isTrivial())
4488 CleanupFn = CGM.getAddrOfCXXStructor(GlobalDecl(DtorD, Dtor_Complete));
4489 // This is unused as far as we can tell, initialize it to null.
4490 llvm::Constant *ForwardCompat =
4491 getImageRelativeConstant(llvm::Constant::getNullValue(CGM.Int8PtrTy));
4492 llvm::Constant *PointerToCatchableTypes = getImageRelativeConstant(CTA);
4493 llvm::StructType *TIType = getThrowInfoType();
4494 llvm::Constant *Fields[] = {
4495 llvm::ConstantInt::get(CGM.IntTy, Flags), // Flags
4496 getImageRelativeConstant(CleanupFn), // CleanupFn
4497 ForwardCompat, // ForwardCompat
4498 PointerToCatchableTypes // CatchableTypeArray
4499 };
4500 auto *GV = new llvm::GlobalVariable(
4501 CGM.getModule(), TIType, /*isConstant=*/true, getLinkageForRTTI(T),
4502 llvm::ConstantStruct::get(TIType, Fields), MangledName.str());
4503 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
4504 GV->setSection(".xdata");
4505 if (GV->isWeakForLinker())
4506 GV->setComdat(CGM.getModule().getOrInsertComdat(GV->getName()));
4507 return GV;
4508}
4509
4510void MicrosoftCXXABI::emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) {
4511 const Expr *SubExpr = E->getSubExpr();
4512 assert(SubExpr && "SubExpr cannot be null");
4513 QualType ThrowType = SubExpr->getType();
4514 // The exception object lives on the stack and it's address is passed to the
4515 // runtime function.
4516 Address AI = CGF.CreateMemTemp(ThrowType);
4517 CGF.EmitAnyExprToMem(SubExpr, AI, ThrowType.getQualifiers(),
4518 /*IsInit=*/true);
4519
4520 // The so-called ThrowInfo is used to describe how the exception object may be
4521 // caught.
4522 llvm::GlobalVariable *TI = getThrowInfo(ThrowType);
4523
4524 // Call into the runtime to throw the exception.
4525 llvm::Value *Args[] = {AI.emitRawPointer(CGF), TI};
4527}
4528
4529std::pair<llvm::Value *, const CXXRecordDecl *>
4530MicrosoftCXXABI::LoadVTablePtr(CodeGenFunction &CGF, Address This,
4531 const CXXRecordDecl *RD) {
4533 std::tie(This, std::ignore, RD) = performBaseAdjustment(CGF, This, T);
4534 return {CGF.GetVTablePtr(This, CGM.Int8PtrTy, RD), RD};
4535}
4536
4537bool MicrosoftCXXABI::isPermittedToBeHomogeneousAggregate(
4538 const CXXRecordDecl *RD) const {
4539 // All aggregates are permitted to be HFA on non-ARM platforms, which mostly
4540 // affects vectorcall on x64/x86.
4541 if (!CGM.getTarget().getTriple().isAArch64())
4542 return true;
4543 // MSVC Windows on Arm64 has its own rules for determining if a type is HFA
4544 // that are inconsistent with the AAPCS64 ABI. The following are our best
4545 // determination of those rules so far, based on observation of MSVC's
4546 // behavior.
4547 if (RD->isEmpty())
4548 return false;
4549 if (RD->isPolymorphic())
4550 return false;
4552 return false;
4553 if (RD->hasNonTrivialDestructor())
4554 return false;
4556 return false;
4557 // These two are somewhat redundant given the caller
4558 // (ABIInfo::isHomogeneousAggregate) checks the bases and fields, but that
4559 // caller doesn't consider empty bases/fields to be non-homogenous, but it
4560 // looks like Microsoft's AArch64 ABI does care about these empty types &
4561 // anything containing/derived from one is non-homogeneous.
4562 // Instead we could add another CXXABI entry point to query this property and
4563 // have ABIInfo::isHomogeneousAggregate use that property.
4564 // I don't think any other of the features listed above could be true of a
4565 // base/field while not true of the outer struct. For example, if you have a
4566 // base/field that has an non-trivial copy assignment/dtor/default ctor, then
4567 // the outer struct's corresponding operation must be non-trivial.
4568 for (const CXXBaseSpecifier &B : RD->bases()) {
4569 if (const CXXRecordDecl *FRD = B.getType()->getAsCXXRecordDecl()) {
4570 if (!isPermittedToBeHomogeneousAggregate(FRD))
4571 return false;
4572 }
4573 }
4574 // empty fields seem to be caught by the ABIInfo::isHomogeneousAggregate
4575 // checking for padding - but maybe there are ways to end up with an empty
4576 // field without padding? Not that I know of, so don't check fields here &
4577 // rely on the padding check.
4578 return true;
4579}
#define V(N, I)
static Address emitDynamicCastToVoid(CIRGenFunction &cgf, mlir::Location loc, QualType srcRecordTy, Address src)
static mlir::Value emitExactDynamicCast(CIRGenItaniumCXXABI &abi, CIRGenFunction &cgf, mlir::Location loc, QualType srcRecordTy, QualType destRecordTy, cir::PointerType destCIRTy, bool isRefCast, Address src)
static RValue performReturnAdjustment(CIRGenFunction &cgf, QualType resultType, RValue rv, const ThunkInfo &thunk)
static llvm::FunctionCallee getThrowFn(CodeGenModule &CGM)
static void emitTlsGuardCheck(CodeGenFunction &CGF, llvm::GlobalValue *TlsGuard, llvm::BasicBlock *DynInitBB, llvm::BasicBlock *ContinueBB)
static llvm::GlobalVariable * getTypeInfoVTable(CodeGenModule &CGM)
static bool hasDefaultCXXMethodCC(ASTContext &Context, const CXXMethodDecl *MD)
static bool isTrivialForMSVC(const CXXRecordDecl *RD, QualType Ty, CodeGenModule &CGM)
static llvm::FunctionCallee getInitThreadAbortFn(CodeGenModule &CGM)
static llvm::FunctionCallee getInitThreadHeaderFn(CodeGenModule &CGM)
static llvm::GlobalValue * getTlsGuardVar(CodeGenModule &CGM)
static QualType decomposeTypeForEH(ASTContext &Context, QualType T, bool &IsConst, bool &IsVolatile, bool &IsUnaligned)
static llvm::CallBase * emitRTtypeidCall(CodeGenFunction &CGF, llvm::Value *Argument)
static llvm::FunctionCallee getDynTlsOnDemandInitFn(CodeGenModule &CGM)
static void emitDynamicTlsInitializationCall(CodeGenFunction &CGF, llvm::GlobalValue *TlsGuard, llvm::BasicBlock *ContinueBB)
static void detectAmbiguousBases(SmallVectorImpl< MSRTTIClass > &Classes)
Find ambiguity among base classes.
static void emitDynamicTlsInitialization(CodeGenFunction &CGF)
static void serializeClassHierarchy(SmallVectorImpl< MSRTTIClass > &Classes, const CXXRecordDecl *RD)
Recursively serializes a class hierarchy in pre-order depth first order.
static llvm::FunctionCallee getInitThreadFooterFn(CodeGenModule &CGM)
static bool isDeletingDtor(GlobalDecl GD)
static void emitGlobalDtorWithTLRegDtor(CodeGenFunction &CGF, const VarDecl &VD, llvm::FunctionCallee Dtor, llvm::Constant *Addr)
static ConstantAddress getInitThreadEpochPtr(CodeGenModule &CGM)
static void mangleVFTableName(MicrosoftMangleContext &MangleContext, const CXXRecordDecl *RD, const VPtrInfo &VFPtr, SmallString< 256 > &Name)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
FormatToken * Next
The next token in the unwrapped line.
Result
Implement __builtin_bit_cast and related operations.
bool isMemberPointerToDerivedMember() const
Definition APValue.cpp:1105
const ValueDecl * getMemberPointerDecl() const
Definition APValue.cpp:1098
ArrayRef< const CXXRecordDecl * > getMemberPointerPath() const
Definition APValue.cpp:1112
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:227
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
uint64_t getFieldOffset(const ValueDecl *FD) const
Get the offset of a FieldDecl or IndirectFieldDecl, in bits.
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
IdentifierTable & Idents
Definition ASTContext.h:805
const LangOptions & getLangOpts() const
Definition ASTContext.h:959
CanQualType IntTy
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
QualType getMemberPointerType(QualType T, NestedNameSpecifier Qualifier, const CXXRecordDecl *Cls) const
Return the uniqued reference to the type for a member pointer to the specified type in the specified ...
const CXXConstructorDecl * getCopyConstructorForExceptionObject(CXXRecordDecl *RD)
QualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:924
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
CanQualType getCanonicalTagType(const TagDecl *TD) const
unsigned getTargetAddressSpace(LangAS AS) const
void recordMemberDataPointerEvaluation(const ValueDecl *VD)
llvm::DenseMap< const CXXRecordDecl *, VBaseInfo > VBaseOffsetsMapTy
CharUnits getVBPtrOffset() const
getVBPtrOffset - Get the offset for virtual base table pointer.
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const
getVBaseClassOffset - Get the offset, in chars, for the given base class.
const VBaseOffsetsMapTy & getVBaseOffsetsMap() const
bool hasExtendableVFPtr() const
hasVFPtr - Does this class have a virtual function table pointer that can be extended by a derived cl...
Represents a base class of a C++ class.
Definition DeclCXX.h:146
VarDecl * getExceptionDecl() const
Definition StmtCXX.h:49
bool isDefaultConstructor() const
Whether this constructor is a default constructor (C++ [class.ctor]p5), which can be used to default-...
Definition DeclCXX.cpp:3051
FunctionDecl * getOperatorDelete() const
Definition ExprCXX.h:2669
bool isGlobalDelete() const
Definition ExprCXX.h:2655
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2132
bool isVirtual() const
Definition DeclCXX.h:2187
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition DeclCXX.h:2271
QualType getThisType() const
Return the type of the this pointer.
Definition DeclCXX.cpp:2861
bool isInstance() const
Definition DeclCXX.h:2159
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
bool hasNonTrivialCopyAssignment() const
Determine whether this class has a non-trivial copy assignment operator (C++ [class....
Definition DeclCXX.h:1340
CXXRecordDecl * getMostRecentDecl()
Definition DeclCXX.h:539
bool hasPrivateFields() const
Definition DeclCXX.h:1191
base_class_range bases()
Definition DeclCXX.h:608
bool hasProtectedFields() const
Definition DeclCXX.h:1195
bool hasNonTrivialDestructor() const
Determine whether this class has a non-trivial destructor (C++ [class.dtor]p3)
Definition DeclCXX.h:1382
bool isPolymorphic() const
Whether this class is polymorphic (C++ [class.virtual]), which means that the class contains or inher...
Definition DeclCXX.h:1214
unsigned getNumBases() const
Retrieves the number of base classes of this class.
Definition DeclCXX.h:602
base_class_range vbases()
Definition DeclCXX.h:625
MSInheritanceModel getMSInheritanceModel() const
Returns the inheritance model used for this record.
bool nullFieldOffsetIsZero() const
In the Microsoft C++ ABI, use zero for the field offset of a null data member pointer if we can guara...
bool hasDefinition() const
Definition DeclCXX.h:561
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
Definition DeclCXX.h:1186
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition DeclCXX.cpp:2131
bool hasNonTrivialDefaultConstructor() const
Determine whether this class has a non-trivial default constructor (C++11 [class.ctor]p5).
Definition DeclCXX.h:1253
bool isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const
Determine whether this class is virtually derived from the class Base.
bool needsImplicitCopyAssignment() const
Determine whether this class needs an implicit copy assignment operator to be lazily declared.
Definition DeclCXX.h:925
bool hasSimpleCopyAssignment() const
true if we know for sure that this class has a single, accessible, unambiguous copy assignment operat...
Definition DeclCXX.h:737
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
Definition DeclCXX.h:623
const Expr * getSubExpr() const
Definition ExprCXX.h:1232
path_iterator path_begin()
Definition Expr.h:3749
CastKind getCastKind() const
Definition Expr.h:3723
path_iterator path_end()
Definition Expr.h:3750
const CXXBaseSpecifier *const * path_const_iterator
Definition Expr.h:3746
Expr * getSubExpr()
Definition Expr.h:3729
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
bool isNegative() const
isNegative - Test whether the quantity is less than zero.
Definition CharUnits.h:131
bool isPositive() const
isPositive - Test whether the quantity is greater than zero.
Definition CharUnits.h:128
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition CharUnits.h:122
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
Definition CharUnits.h:189
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition CharUnits.h:58
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:63
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition CharUnits.h:53
bool hasProfileIRInstr() const
Check if IR level profile instrumentation is on.
static ABIArgInfo getIndirect(CharUnits Alignment, unsigned AddrSpace, bool ByVal=true, bool Realign=false, llvm::Type *Padding=nullptr)
void setSRetAfterThis(bool AfterThis)
bool isHomogeneousAggregate(QualType Ty, const Type *&Base, uint64_t &Members) const
isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous aggregate.
Definition ABIInfo.cpp:61
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition Address.h:128
llvm::Value * emitRawPointer(CodeGenFunction &CGF) const
Return the pointer contained in this class after authenticating it and adding offset to it if necessa...
Definition Address.h:253
Address withElementType(llvm::Type *ElemTy) const
Return address with different element type, but same pointer and alignment.
Definition Address.h:276
static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF)
Apply TemporaryLocation if it is valid.
static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF)
Set the IRBuilder to not attach debug locations.
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition CGBuilder.h:146
Address CreateConstInBoundsByteGEP(Address Addr, CharUnits Offset, const llvm::Twine &Name="")
Given a pointer to i8, adjust it by a given constant offset.
Definition CGBuilder.h:315
Address CreateConstInBoundsGEP2_32(Address Addr, unsigned Idx0, unsigned Idx1, const llvm::Twine &Name="")
Definition CGBuilder.h:335
Address CreateGEP(CodeGenFunction &CGF, Address Addr, llvm::Value *Index, const llvm::Twine &Name="")
Definition CGBuilder.h:302
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition CGBuilder.h:118
Address CreateConstByteGEP(Address Addr, CharUnits Offset, const llvm::Twine &Name="")
Definition CGBuilder.h:325
llvm::LoadInst * CreateAlignedLoad(llvm::Type *Ty, llvm::Value *Addr, CharUnits Align, const llvm::Twine &Name="")
Definition CGBuilder.h:138
Address CreateInBoundsGEP(Address Addr, ArrayRef< llvm::Value * > IdxList, llvm::Type *ElementType, CharUnits Align, const Twine &Name="")
Definition CGBuilder.h:356
Implements C++ ABI-specific code generation functions.
Definition CGCXXABI.h:43
RecordArgABI
Specify how one should pass an argument of a record type.
Definition CGCXXABI.h:150
MangleContext & getMangleContext()
Gets the mangle context.
Definition CGCXXABI.h:113
static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr, llvm::FunctionType *FTy)
Definition CGCall.h:147
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition CGCall.h:137
CanQualType getReturnType() const
unsigned getEffectiveCallingConvention() const
getEffectiveCallingConvention - Return the actual calling convention to use, which may depend on the ...
void add(RValue rvalue, QualType type)
Definition CGCall.h:302
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
llvm::Value * GetVTablePtr(Address This, llvm::Type *VTableTy, const CXXRecordDecl *VTableClass, VTableAuthMode AuthMode=VTableAuthMode::Authenticate)
GetVTablePtr - Return the Value of the vtable pointer member pointed to by This.
Definition CGClass.cpp:2812
LValue EmitLoadOfReferenceLValue(LValue RefLVal)
Definition CGExpr.cpp:3417
GlobalDecl CurGD
CurGD - The GlobalDecl for the current function being compiled.
llvm::Constant * createAtExitStub(const VarDecl &VD, llvm::FunctionCallee Dtor, llvm::Constant *Addr)
Create a stub function, suitable for being passed to atexit, which passes the given address to the gi...
void EmitMustTailThunk(GlobalDecl GD, llvm::Value *AdjustedThisPtr, llvm::FunctionCallee Callee)
Emit a musttail call for a thunk with a potentially adjusted this pointer.
void EmitNoreturnRuntimeCallOrInvoke(llvm::FunctionCallee callee, ArrayRef< llvm::Value * > args)
Emits a call or invoke to the given noreturn runtime function.
Definition CGCall.cpp:5176
llvm::CallBase * EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee, ArrayRef< llvm::Value * > args, const Twine &name="")
Emits a call or invoke instruction to the given runtime function.
Definition CGCall.cpp:5203
bool CurFuncIsThunk
In C++, whether we are code generating a thunk.
void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::FunctionCallee fn, llvm::Constant *addr)
Call atexit() with a function that passes the given argument to the given function.
llvm::Value * getAsNaturalPointerTo(Address Addr, QualType PointeeType)
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
AutoVarEmission EmitAutoVarAlloca(const VarDecl &var)
EmitAutoVarAlloca - Emit the alloca and debug information for a local variable.
Definition CGDecl.cpp:1483
void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This, QualType ThisTy)
Definition CGClass.cpp:2635
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::GlobalVariable *GV, bool PerformInit)
EmitCXXGlobalVarDeclInit - Create the initializer for a C++ variable with global storage.
void StartFunction(GlobalDecl GD, QualType RetTy, llvm::Function *Fn, const CGFunctionInfo &FnInfo, const FunctionArgList &Args, SourceLocation Loc=SourceLocation(), SourceLocation StartLoc=SourceLocation())
Emit code for the start of a function.
void EmitCXXGuardedInitBranch(llvm::Value *NeedsInit, llvm::BasicBlock *InitBlock, llvm::BasicBlock *NoInitBlock, GuardKind Kind, const VarDecl *D)
Emit a branch to select whether or not to perform guarded initialization.
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **CallOrInvoke, bool IsMustTail, SourceLocation Loc, bool IsVirtualFunctionPointerThunk=false)
EmitCall - Generate a call of the given function, expecting the given result type,...
Definition CGCall.cpp:5359
void EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD, llvm::Value *VTable, SourceLocation Loc)
If whole-program virtual table optimization is enabled, emit an assumption that VTable is a member of...
Definition CGClass.cpp:2884
llvm::CallInst * EmitNounwindRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
const Decl * CurFuncDecl
CurFuncDecl - Holds the Decl for the current outermost non-closure context.
void EmitAutoVarCleanups(const AutoVarEmission &emission)
Definition CGDecl.cpp:2218
void EmitAnyExprToMem(const Expr *E, Address Location, Qualifiers Quals, bool IsInitializer)
EmitAnyExprToMem - Emits the code necessary to evaluate an arbitrary expression into the given memory...
Definition CGExpr.cpp:307
llvm::CallInst * EmitRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr, QualType DeleteTy, llvm::Value *NumElements=nullptr, CharUnits CookieSize=CharUnits())
CodeGenTypes & getTypes() const
RawAddress CreateMemTemp(QualType T, const Twine &Name="tmp", RawAddress *Alloca=nullptr)
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignmen and cas...
Definition CGExpr.cpp:194
void EmitCallArgs(CallArgList &Args, PrototypeWrapper Prototype, llvm::iterator_range< CallExpr::const_arg_iterator > ArgRange, AbstractCallee AC=AbstractCallee(), unsigned ParamsToSkip=0, EvaluationOrder Order=EvaluationOrder::Default)
EmitCallArgs - Emit call arguments for a function.
Definition CGCall.cpp:4796
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
void FinishFunction(SourceLocation EndLoc=SourceLocation())
FinishFunction - Complete IR generation of the current function.
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
llvm::Value * EmitVTableTypeCheckedLoad(const CXXRecordDecl *RD, llvm::Value *VTable, llvm::Type *VTableTy, uint64_t VTableByteOffset)
Emit a type checked load from the given vtable.
Definition CGClass.cpp:3071
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
llvm::Instruction * CurrentFuncletPad
bool ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD)
Returns whether we should perform a type checked load when loading a virtual function for virtual cal...
Definition CGClass.cpp:3053
void PopCleanupBlock(bool FallThroughIsBranchThrough=false, bool ForDeactivation=false)
PopCleanupBlock - Will pop the cleanup entry on the stack and process all branch fixups.
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition CGStmt.cpp:643
This class organizes the cross-function state that is used while generating LLVM code.
void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const
Set visibility, dllimport/dllexport and dso_local.
void AddCXXDtorEntry(llvm::FunctionCallee DtorFn, llvm::Constant *Object)
Add a destructor and object to add to the C++ global destructor function.
void AddVTableTypeMetadata(llvm::GlobalVariable *VTable, CharUnits Offset, const CXXRecordDecl *RD)
Create and attach type metadata for the given vtable.
void setDSOLocal(llvm::GlobalValue *GV) const
llvm::GlobalObject::VCallVisibility GetVCallVisibilityLevel(const CXXRecordDecl *RD, llvm::DenseSet< const CXXRecordDecl * > &Visited)
Returns the vcall visibility of the given type.
llvm::Module & getModule() const
llvm::FunctionCallee CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false, bool AssumeConvergent=false)
Create or return a runtime function declaration with the specified type and name.
CodeGenVTables & getVTables()
bool classNeedsVectorDestructor(const CXXRecordDecl *RD)
Check that class need vector deleting destructor body.
llvm::Constant * GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH=false)
Get the address of the RTTI descriptor for the given type.
llvm::Constant * GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty=nullptr, bool ForVTable=false, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Return the address of the given function.
DiagnosticsEngine & getDiags() const
llvm::Constant * getAddrOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo=nullptr, llvm::FunctionType *FnType=nullptr, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Return the address of the constructor/destructor of the given type.
llvm::GlobalValue::LinkageTypes getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage)
Returns LLVM linkage for a declarator.
const LangOptions & getLangOpts() const
const TargetInfo & getTarget() const
void EmitGlobal(GlobalDecl D)
Emit code for a single global function or var decl.
void addUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.used metadata.
void AppendLinkerOptions(StringRef Opts)
Appends Opts to the "llvm.linker.options" metadata value.
bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D)
Try to emit a base destructor as an alias to its primary base-class destructor.
Definition CGCXX.cpp:32
CharUnits computeNonVirtualBaseClassOffset(const CXXRecordDecl *DerivedClass, CastExpr::path_const_iterator Start, CastExpr::path_const_iterator End)
Definition CGClass.cpp:168
CharUnits getVBaseAlignment(CharUnits DerivedAlign, const CXXRecordDecl *Derived, const CXXRecordDecl *VBase)
Returns the assumed alignment of a virtual base of a class.
Definition CGClass.cpp:76
void EmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target)
Emit a definition as a global alias for another definition, unconditionally.
Definition CGCXX.cpp:203
llvm::Function * codegenCXXStructor(GlobalDecl GD)
Definition CGCXX.cpp:238
llvm::Constant * CreateRuntimeVariable(llvm::Type *Ty, StringRef Name)
Create a new runtime global variable with the specified type and name.
void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const
Set the TLS mode for the given LLVM GlobalValue for the thread-local variable declaration D.
ASTContext & getContext() const
llvm::Constant * GetAddrOfGlobalVar(const VarDecl *D, llvm::Type *Ty=nullptr, ForDefinition_t IsForDefinition=NotForDefinition)
Return the llvm::Constant for the address of the given global variable.
MicrosoftVTableContext & getMicrosoftVTableContext()
const TargetCodeGenInfo & getTargetCodeGenInfo()
const CodeGenOptions & getCodeGenOpts() const
StringRef getMangledName(GlobalDecl GD)
llvm::GlobalVariable * CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage, llvm::Align Alignment)
Will return a global variable of the given type.
llvm::LLVMContext & getLLVMContext()
llvm::GlobalValue * GetGlobalValue(StringRef Ref)
void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO)
void setDLLImportDLLExport(llvm::GlobalValue *GV, GlobalDecl D) const
llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD)
Return the appropriate linkage for the vtable, VTT, and type information of the given class.
void SetLLVMFunctionAttributes(GlobalDecl GD, const CGFunctionInfo &Info, llvm::Function *F, bool IsThunk)
Set the LLVM function attributes (sext, zext, etc).
void addDeferredVTable(const CXXRecordDecl *RD)
void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F)
Set the LLVM function attributes which only apply to a function definition.
llvm::Function * CreateGlobalInitOrCleanUpFunction(llvm::FunctionType *ty, const Twine &name, const CGFunctionInfo &FI, SourceLocation Loc=SourceLocation(), bool TLS=false, llvm::GlobalVariable::LinkageTypes Linkage=llvm::GlobalVariable::InternalLinkage)
const CGFunctionInfo & arrangeCXXMethodDeclaration(const CXXMethodDecl *MD)
C++ methods have some special rules and also have implicit parameters.
Definition CGCall.cpp:376
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition CGCall.cpp:1805
bool isFuncTypeConvertible(const FunctionType *FT)
isFuncTypeConvertible - Utility to check whether a function type can be converted to an LLVM type (i....
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
const CGFunctionInfo & arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD)
Arrange a thunk that takes 'this' as the first parameter followed by varargs.
Definition CGCall.cpp:629
const CGFunctionInfo & arrangeCXXConstructorCall(const CallArgList &Args, const CXXConstructorDecl *D, CXXCtorType CtorKind, unsigned ExtraPrefixArgs, unsigned ExtraSuffixArgs, bool PassProtoArgs=true)
Arrange a call to a C++ method, passing the given arguments.
Definition CGCall.cpp:487
const CGFunctionInfo & arrangeCXXStructorDeclaration(GlobalDecl GD)
Definition CGCall.cpp:404
const CGFunctionInfo & arrangeMSCtorClosure(const CXXConstructorDecl *CD, CXXCtorType CT)
Definition CGCall.cpp:638
const CGFunctionInfo & arrangeNullaryFunction()
A nullary function is a freestanding function of type 'void ()'.
Definition CGCall.cpp:790
void createVTableInitializer(ConstantStructBuilder &builder, const VTableLayout &layout, llvm::Constant *rtti, bool vtableHasLocalLinkage)
Add vtable components for the given vtable layout to the given global initializer.
A specialization of Address that requires the address to be an LLVM Constant.
Definition Address.h:296
static RValue get(llvm::Value *V)
Definition CGValue.h:99
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition CGValue.h:72
virtual LangAS getSRetAddrSpace(const CXXRecordDecl *RD) const
Get the address space for an indirect (sret) return of the given type.
Definition TargetInfo.h:327
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2122
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition DeclBase.h:2386
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
bool isInStdNamespace() const
Definition DeclBase.cpp:450
SourceLocation getLocation() const
Definition DeclBase.h:447
DeclContext * getDeclContext()
Definition DeclBase.h:456
bool hasAttr() const
Definition DeclBase.h:585
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:277
QualType getType() const
Definition Expr.h:144
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition Decl.h:3414
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2792
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition Decl.cpp:3821
bool isDefined(const FunctionDecl *&Definition, bool CheckForPendingFriendDefinition=false) const
Returns true if the function has a definition that does not need to be instantiated.
Definition Decl.cpp:3241
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5362
bool isVariadic() const
Whether this function prototype is variadic.
Definition TypeBase.h:5766
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
GlobalDecl getWithCtorType(CXXCtorType Type)
Definition GlobalDecl.h:178
GlobalDecl getWithDtorType(CXXDtorType Type)
Definition GlobalDecl.h:185
CXXDtorType getDtorType() const
Definition GlobalDecl.h:113
const Decl * getDecl() const
Definition GlobalDecl.h:106
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
static ImplicitParamDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, ImplicitParamKind ParamKind)
Create implicit parameter.
Definition Decl.cpp:5597
bool isExplicitDefaultVisibilityExportMapping() const
bool isAllDefaultVisibilityExportMapping() const
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition Mangle.h:56
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition TypeBase.h:3708
CXXRecordDecl * getMostRecentCXXRecordDecl() const
Note: this can trigger extra deserialization when external AST sources are used.
Definition Type.cpp:5582
QualType getPointeeType() const
Definition TypeBase.h:3726
bool isMemberFunctionPointer() const
Returns true if the member type (i.e.
Definition TypeBase.h:3730
bool isMemberDataPointer() const
Returns true if the member type (i.e.
Definition TypeBase.h:3736
unsigned getVBTableIndex(const CXXRecordDecl *Derived, const CXXRecordDecl *VBase)
Returns the index of VBase in the vbtable of Derived.
MethodVFTableLocation getMethodVFTableLocation(GlobalDecl GD)
const VPtrInfoVector & getVFPtrOffsets(const CXXRecordDecl *RD)
const VTableLayout & getVFTableLayout(const CXXRecordDecl *RD, CharUnits VFPtrOffset)
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition Decl.h:295
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition Decl.h:340
bool isExternallyVisible() const
Definition Decl.h:433
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition TypeBase.h:8520
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1004
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition TypeBase.h:8476
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition TypeBase.h:8530
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition TypeBase.h:8509
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after.
Definition TypeBase.h:1556
bool hasUnaligned() const
Definition TypeBase.h:511
bool canPassInRegisters() const
Determine whether this class can be passed in registers.
Definition Decl.h:4480
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
virtual bool emitVectorDeletingDtors(const LangOptions &) const
Controls whether to emit MSVC vector deleting destructors.
uint64_t getPointerWidth(LangAS AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition TargetInfo.h:490
virtual bool callGlobalDeleteInDeletingDtor(const LangOptions &) const
Controls whether global operator delete is called by the deleting destructor or at the point where de...
The base class of the type hierarchy.
Definition TypeBase.h:1871
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
bool isPointerType() const
Definition TypeBase.h:8673
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9333
bool isReferenceType() const
Definition TypeBase.h:8697
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:789
bool isObjectType() const
Determine whether this type is an object type.
Definition TypeBase.h:2563
Linkage getLinkage() const
Determine the linkage of this type.
Definition Type.cpp:5021
bool isNullPtrType() const
Definition TypeBase.h:9076
ArrayRef< VTableComponent > vtable_components() const
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:924
TLSKind getTLSKind() const
Definition Decl.cpp:2147
bool isNoDestroy(const ASTContext &) const
Is destruction of this variable entirely suppressed?
Definition Decl.cpp:2815
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition Decl.h:1206
llvm::Value * getCXXDestructorImplicitParam(CodeGenModule &CGM, llvm::BasicBlock *InsertBlock, llvm::BasicBlock::iterator InsertPoint, const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating)
CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, CGCXXABI &CXXABI)
bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI, const ABIInfo &Info)
@ NormalCleanup
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
@ EHCleanup
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
CGCXXABI * CreateMicrosoftCXXABI(CodeGenModule &CGM)
Creates a Microsoft-family ABI.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
constexpr Variable var(Literal L)
Returns the variable of L.
Definition CNFFormula.h:64
bool This(InterpState &S, CodePtr OpPC)
Definition Interp.h:3066
@ Address
A pointer to a ValueDecl.
Definition Primitives.h:28
PRESERVE_NONE bool Ret(InterpState &S, CodePtr &PC)
Definition Interp.h:258
The JSON file list parser is used to communicate input to InstallAPI.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
CXXCtorType
C++ constructor types.
Definition ABI.h:24
@ Ctor_Base
Base object ctor.
Definition ABI.h:26
@ Ctor_DefaultClosure
Default closure variant of a ctor.
Definition ABI.h:29
@ Ctor_CopyingClosure
Copying closure variant of a ctor.
Definition ABI.h:28
@ Ctor_Complete
Complete object ctor.
Definition ABI.h:25
bool isa(CodeGen::Address addr)
Definition Address.h:330
GVALinkage
A more specific kind of linkage than enum Linkage.
Definition Linkage.h:72
@ GVA_Internal
Definition Linkage.h:73
void initialize(TemplateInstantiationCallbackPtrs &Callbacks, const Sema &TheSema)
bool inheritanceModelHasNVOffsetField(bool IsMemberFunction, MSInheritanceModel Inheritance)
@ AS_public
Definition Specifiers.h:125
bool inheritanceModelHasOnlyOneField(bool IsMemberFunction, MSInheritanceModel Inheritance)
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
bool inheritanceModelHasVBPtrOffsetField(MSInheritanceModel Inheritance)
bool inheritanceModelHasVBTableOffsetField(MSInheritanceModel Inheritance)
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition Linkage.h:24
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
Definition Linkage.h:54
@ Template
We are parsing a template declaration.
Definition Parser.h:81
SmallVector< std::unique_ptr< VPtrInfo >, 2 > VPtrInfoVector
CXXDtorType
C++ destructor types.
Definition ABI.h:34
@ Dtor_VectorDeleting
Vector deleting dtor.
Definition ABI.h:40
@ Dtor_Comdat
The COMDAT used for dtors.
Definition ABI.h:38
@ Dtor_Unified
GCC-style unified dtor.
Definition ABI.h:39
@ Dtor_Base
Base object dtor.
Definition ABI.h:37
@ Dtor_Complete
Complete object dtor.
Definition ABI.h:36
@ Dtor_Deleting
Deleting dtor.
Definition ABI.h:35
@ Type
The name was classified as a type.
Definition Sema.h:564
LangAS
Defines the address space values used by the address space qualifier of QualType.
CastKind
CastKind - The kind of operation required for a conversion.
MSInheritanceModel
Assigned inheritance model for a class in the MS C++ ABI.
Definition Specifiers.h:413
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition Specifiers.h:279
U cast(CodeGen::Address addr)
Definition Address.h:327
@ Class
The "class" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:5972
@ Implicit
An implicit conversion.
Definition Sema.h:440
unsigned long uint64_t
long int64_t
unsigned int uint32_t
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
const CXXRecordDecl * VBase
If nonnull, holds the last vbase which contains the vfptr that the method definition is adjusted to.
CharUnits VFPtrOffset
This is the offset of the vfptr from the start of the last vbase, or the complete type if there are n...
uint64_t Index
Method's index in the vftable.
bool isEmpty() const
Definition Thunk.h:70
union clang::ReturnAdjustment::VirtualAdjustment Virtual
int64_t NonVirtual
The non-virtual adjustment from the derived object to its nearest virtual base.
Definition Thunk.h:30
union clang::ThisAdjustment::VirtualAdjustment Virtual
bool isEmpty() const
Definition Thunk.h:137
int64_t NonVirtual
The non-virtual adjustment from the derived object to its nearest virtual base.
Definition Thunk.h:95
ThisAdjustment This
The this pointer adjustment.
Definition Thunk.h:159
bool isAlignRequired()
Definition ASTContext.h:201
Holds information about the inheritance path to a virtual base or function table pointer.
CharUnits NonVirtualOffset
IntroducingObject is at this offset from its containing complete object or virtual base.
CharUnits FullOffsetInMDC
Static offset from the top of the most derived class to this vfptr, including any virtual base offset...
const CXXRecordDecl * getVBaseWithVPtr() const
The vptr is stored inside the non-virtual component of this virtual base.
const CXXRecordDecl * IntroducingObject
This is the class that introduced the vptr by declaring new virtual methods or virtual bases.
BasePath MangledPath
The bases from the inheritance path that got used to mangle the vbtable name.
BasePath PathToIntroducingObject
This holds the base classes path from the complete type to the first base with the given vfptr offset...
const CXXRecordDecl * ObjectWithVPtr
This is the most derived class that has this vptr at offset zero.
uint32_t VBPtrOffset
The offset (in bytes) of the vbptr, relative to the beginning of the derived class.
Definition Thunk.h:46
struct clang::ReturnAdjustment::VirtualAdjustment::@167242364125115315334113104006210160165266060257 Microsoft
uint32_t VBIndex
Index of the virtual base in the vbtable.
Definition Thunk.h:49
int32_t VtordispOffset
The offset of the vtordisp (in bytes), relative to the ECX.
Definition Thunk.h:109
struct clang::ThisAdjustment::VirtualAdjustment::@312251255113040203233347230177110330127151157305 Microsoft
int32_t VBOffsetOffset
The offset (in bytes) of the vbase offset in the vbtable.
Definition Thunk.h:116
int32_t VBPtrOffset
The offset of the vbptr of the derived class (in bytes), relative to the ECX after vtordisp adjustmen...
Definition Thunk.h:113