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