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