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