clang  6.0.0
ItaniumCXXABI.cpp
Go to the documentation of this file.
1 //===------- ItaniumCXXABI.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 Itanium C++ ABI. The class
11 // in this file generates structures that follow the Itanium C++ ABI, which is
12 // documented at:
13 // http://www.codesourcery.com/public/cxx-abi/abi.html
14 // http://www.codesourcery.com/public/cxx-abi/abi-eh.html
15 //
16 // It also supports the closely-related ARM ABI, documented at:
17 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #include "CGCXXABI.h"
22 #include "CGCleanup.h"
23 #include "CGRecordLayout.h"
24 #include "CGVTables.h"
25 #include "CodeGenFunction.h"
26 #include "CodeGenModule.h"
27 #include "TargetInfo.h"
29 #include "clang/AST/Mangle.h"
30 #include "clang/AST/Type.h"
31 #include "clang/AST/StmtCXX.h"
32 #include "llvm/IR/CallSite.h"
33 #include "llvm/IR/DataLayout.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/Intrinsics.h"
36 #include "llvm/IR/Value.h"
37 
38 using namespace clang;
39 using namespace CodeGen;
40 
41 namespace {
42 class ItaniumCXXABI : public CodeGen::CGCXXABI {
43  /// VTables - All the vtables which have been defined.
44  llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables;
45 
46 protected:
47  bool UseARMMethodPtrABI;
48  bool UseARMGuardVarABI;
49  bool Use32BitVTableOffsetABI;
50 
51  ItaniumMangleContext &getMangleContext() {
52  return cast<ItaniumMangleContext>(CodeGen::CGCXXABI::getMangleContext());
53  }
54 
55 public:
56  ItaniumCXXABI(CodeGen::CodeGenModule &CGM,
57  bool UseARMMethodPtrABI = false,
58  bool UseARMGuardVarABI = false) :
59  CGCXXABI(CGM), UseARMMethodPtrABI(UseARMMethodPtrABI),
60  UseARMGuardVarABI(UseARMGuardVarABI),
61  Use32BitVTableOffsetABI(false) { }
62 
63  bool classifyReturnType(CGFunctionInfo &FI) const override;
64 
65  bool passClassIndirect(const CXXRecordDecl *RD) const {
66  // Clang <= 4 used the pre-C++11 rule, which ignores move operations.
67  // The PS4 platform ABI follows the behavior of Clang 3.2.
68  if (CGM.getCodeGenOpts().getClangABICompat() <=
70  CGM.getTriple().getOS() == llvm::Triple::PS4)
71  return RD->hasNonTrivialDestructor() ||
73  return !canCopyArgument(RD);
74  }
75 
76  RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override {
77  // If C++ prohibits us from making a copy, pass by address.
78  if (passClassIndirect(RD))
79  return RAA_Indirect;
80  return RAA_Default;
81  }
82 
83  bool isThisCompleteObject(GlobalDecl GD) const override {
84  // The Itanium ABI has separate complete-object vs. base-object
85  // variants of both constructors and destructors.
86  if (isa<CXXDestructorDecl>(GD.getDecl())) {
87  switch (GD.getDtorType()) {
88  case Dtor_Complete:
89  case Dtor_Deleting:
90  return true;
91 
92  case Dtor_Base:
93  return false;
94 
95  case Dtor_Comdat:
96  llvm_unreachable("emitting dtor comdat as function?");
97  }
98  llvm_unreachable("bad dtor kind");
99  }
100  if (isa<CXXConstructorDecl>(GD.getDecl())) {
101  switch (GD.getCtorType()) {
102  case Ctor_Complete:
103  return true;
104 
105  case Ctor_Base:
106  return false;
107 
108  case Ctor_CopyingClosure:
109  case Ctor_DefaultClosure:
110  llvm_unreachable("closure ctors in Itanium ABI?");
111 
112  case Ctor_Comdat:
113  llvm_unreachable("emitting ctor comdat as function?");
114  }
115  llvm_unreachable("bad dtor kind");
116  }
117 
118  // No other kinds.
119  return false;
120  }
121 
122  bool isZeroInitializable(const MemberPointerType *MPT) override;
123 
124  llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override;
125 
126  CGCallee
127  EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
128  const Expr *E,
129  Address This,
130  llvm::Value *&ThisPtrForCall,
131  llvm::Value *MemFnPtr,
132  const MemberPointerType *MPT) override;
133 
134  llvm::Value *
135  EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
136  Address Base,
137  llvm::Value *MemPtr,
138  const MemberPointerType *MPT) override;
139 
140  llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
141  const CastExpr *E,
142  llvm::Value *Src) override;
143  llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
144  llvm::Constant *Src) override;
145 
146  llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override;
147 
148  llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD) override;
149  llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
150  CharUnits offset) override;
151  llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override;
152  llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD,
154 
155  llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
156  llvm::Value *L, llvm::Value *R,
157  const MemberPointerType *MPT,
158  bool Inequality) override;
159 
160  llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
161  llvm::Value *Addr,
162  const MemberPointerType *MPT) override;
163 
164  void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE,
165  Address Ptr, QualType ElementType,
166  const CXXDestructorDecl *Dtor) override;
167 
168  /// Itanium says that an _Unwind_Exception has to be "double-word"
169  /// aligned (and thus the end of it is also so-aligned), meaning 16
170  /// bytes. Of course, that was written for the actual Itanium,
171  /// which is a 64-bit platform. Classically, the ABI doesn't really
172  /// specify the alignment on other platforms, but in practice
173  /// libUnwind declares the struct with __attribute__((aligned)), so
174  /// we assume that alignment here. (It's generally 16 bytes, but
175  /// some targets overwrite it.)
176  CharUnits getAlignmentOfExnObject() {
177  auto align = CGM.getContext().getTargetDefaultAlignForAttributeAligned();
178  return CGM.getContext().toCharUnitsFromBits(align);
179  }
180 
181  void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override;
182  void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) override;
183 
184  void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override;
185 
186  llvm::CallInst *
187  emitTerminateForUnexpectedException(CodeGenFunction &CGF,
188  llvm::Value *Exn) override;
189 
190  void EmitFundamentalRTTIDescriptor(QualType Type, bool DLLExport);
191  void EmitFundamentalRTTIDescriptors(bool DLLExport);
192  llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override;
194  getAddrOfCXXCatchHandlerType(QualType Ty,
195  QualType CatchHandlerType) override {
196  return CatchTypeInfo{getAddrOfRTTIDescriptor(Ty), 0};
197  }
198 
199  bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override;
200  void EmitBadTypeidCall(CodeGenFunction &CGF) override;
201  llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
202  Address ThisPtr,
203  llvm::Type *StdTypeInfoPtrTy) override;
204 
205  bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
206  QualType SrcRecordTy) override;
207 
208  llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, Address Value,
209  QualType SrcRecordTy, QualType DestTy,
210  QualType DestRecordTy,
211  llvm::BasicBlock *CastEnd) override;
212 
213  llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, Address Value,
214  QualType SrcRecordTy,
215  QualType DestTy) override;
216 
217  bool EmitBadCastCall(CodeGenFunction &CGF) override;
218 
219  llvm::Value *
220  GetVirtualBaseClassOffset(CodeGenFunction &CGF, Address This,
221  const CXXRecordDecl *ClassDecl,
222  const CXXRecordDecl *BaseClassDecl) override;
223 
224  void EmitCXXConstructors(const CXXConstructorDecl *D) override;
225 
226  AddedStructorArgs
227  buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
228  SmallVectorImpl<CanQualType> &ArgTys) override;
229 
230  bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
231  CXXDtorType DT) const override {
232  // Itanium does not emit any destructor variant as an inline thunk.
233  // Delegating may occur as an optimization, but all variants are either
234  // emitted with external linkage or as linkonce if they are inline and used.
235  return false;
236  }
237 
238  void EmitCXXDestructors(const CXXDestructorDecl *D) override;
239 
240  void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
241  FunctionArgList &Params) override;
242 
243  void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override;
244 
245  AddedStructorArgs
246  addImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D,
247  CXXCtorType Type, bool ForVirtualBase,
248  bool Delegating, CallArgList &Args) override;
249 
250  void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD,
251  CXXDtorType Type, bool ForVirtualBase,
252  bool Delegating, Address This) override;
253 
254  void emitVTableDefinitions(CodeGenVTables &CGVT,
255  const CXXRecordDecl *RD) override;
256 
257  bool isVirtualOffsetNeededForVTableField(CodeGenFunction &CGF,
258  CodeGenFunction::VPtr Vptr) override;
259 
260  bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass) override {
261  return true;
262  }
263 
264  llvm::Constant *
265  getVTableAddressPoint(BaseSubobject Base,
266  const CXXRecordDecl *VTableClass) override;
267 
268  llvm::Value *getVTableAddressPointInStructor(
269  CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
270  BaseSubobject Base, const CXXRecordDecl *NearestVBase) override;
271 
272  llvm::Value *getVTableAddressPointInStructorWithVTT(
273  CodeGenFunction &CGF, const CXXRecordDecl *VTableClass,
274  BaseSubobject Base, const CXXRecordDecl *NearestVBase);
275 
276  llvm::Constant *
277  getVTableAddressPointForConstExpr(BaseSubobject Base,
278  const CXXRecordDecl *VTableClass) override;
279 
280  llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
281  CharUnits VPtrOffset) override;
282 
283  CGCallee getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD,
284  Address This, llvm::Type *Ty,
285  SourceLocation Loc) override;
286 
287  llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF,
288  const CXXDestructorDecl *Dtor,
289  CXXDtorType DtorType,
290  Address This,
291  const CXXMemberCallExpr *CE) override;
292 
293  void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override;
294 
295  bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const override;
296 
297  void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, GlobalDecl GD,
298  bool ReturnAdjustment) override {
299  // Allow inlining of thunks by emitting them with available_externally
300  // linkage together with vtables when needed.
301  if (ForVTable && !Thunk->hasLocalLinkage())
302  Thunk->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
303 
304  // Propagate dllexport storage, to enable the linker to generate import
305  // thunks as necessary (e.g. when a parent class has a key function and a
306  // child class doesn't, and the construction vtable for the parent in the
307  // child needs to reference the parent's thunks).
308  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
309  if (MD->hasAttr<DLLExportAttr>())
310  Thunk->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
311  }
312 
313  llvm::Value *performThisAdjustment(CodeGenFunction &CGF, Address This,
314  const ThisAdjustment &TA) override;
315 
316  llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, Address Ret,
317  const ReturnAdjustment &RA) override;
318 
319  size_t getSrcArgforCopyCtor(const CXXConstructorDecl *,
320  FunctionArgList &Args) const override {
321  assert(!Args.empty() && "expected the arglist to not be empty!");
322  return Args.size() - 1;
323  }
324 
325  StringRef GetPureVirtualCallName() override { return "__cxa_pure_virtual"; }
326  StringRef GetDeletedVirtualCallName() override
327  { return "__cxa_deleted_virtual"; }
328 
329  CharUnits getArrayCookieSizeImpl(QualType elementType) override;
330  Address InitializeArrayCookie(CodeGenFunction &CGF,
331  Address NewPtr,
332  llvm::Value *NumElements,
333  const CXXNewExpr *expr,
334  QualType ElementType) override;
335  llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
336  Address allocPtr,
337  CharUnits cookieSize) override;
338 
339  void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
340  llvm::GlobalVariable *DeclPtr,
341  bool PerformInit) override;
342  void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
343  llvm::Constant *dtor, llvm::Constant *addr) override;
344 
345  llvm::Function *getOrCreateThreadLocalWrapper(const VarDecl *VD,
346  llvm::Value *Val);
347  void EmitThreadLocalInitFuncs(
348  CodeGenModule &CGM,
349  ArrayRef<const VarDecl *> CXXThreadLocals,
350  ArrayRef<llvm::Function *> CXXThreadLocalInits,
351  ArrayRef<const VarDecl *> CXXThreadLocalInitVars) override;
352 
353  bool usesThreadWrapperFunction() const override { return true; }
354  LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD,
355  QualType LValType) override;
356 
357  bool NeedsVTTParameter(GlobalDecl GD) override;
358 
359  /**************************** RTTI Uniqueness ******************************/
360 
361 protected:
362  /// Returns true if the ABI requires RTTI type_info objects to be unique
363  /// across a program.
364  virtual bool shouldRTTIBeUnique() const { return true; }
365 
366 public:
367  /// What sort of unique-RTTI behavior should we use?
368  enum RTTIUniquenessKind {
369  /// We are guaranteeing, or need to guarantee, that the RTTI string
370  /// is unique.
371  RUK_Unique,
372 
373  /// We are not guaranteeing uniqueness for the RTTI string, so we
374  /// can demote to hidden visibility but must use string comparisons.
375  RUK_NonUniqueHidden,
376 
377  /// We are not guaranteeing uniqueness for the RTTI string, so we
378  /// have to use string comparisons, but we also have to emit it with
379  /// non-hidden visibility.
380  RUK_NonUniqueVisible
381  };
382 
383  /// Return the required visibility status for the given type and linkage in
384  /// the current ABI.
385  RTTIUniquenessKind
386  classifyRTTIUniqueness(QualType CanTy,
387  llvm::GlobalValue::LinkageTypes Linkage) const;
388  friend class ItaniumRTTIBuilder;
389 
390  void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) override;
391 
392  std::pair<llvm::Value *, const CXXRecordDecl *>
393  LoadVTablePtr(CodeGenFunction &CGF, Address This,
394  const CXXRecordDecl *RD) override;
395 
396  private:
397  bool hasAnyUnusedVirtualInlineFunction(const CXXRecordDecl *RD) const {
398  const auto &VtableLayout =
399  CGM.getItaniumVTableContext().getVTableLayout(RD);
400 
401  for (const auto &VtableComponent : VtableLayout.vtable_components()) {
402  // Skip empty slot.
403  if (!VtableComponent.isUsedFunctionPointerKind())
404  continue;
405 
406  const CXXMethodDecl *Method = VtableComponent.getFunctionDecl();
407  if (!Method->getCanonicalDecl()->isInlined())
408  continue;
409 
410  StringRef Name = CGM.getMangledName(VtableComponent.getGlobalDecl());
411  auto *Entry = CGM.GetGlobalValue(Name);
412  // This checks if virtual inline function has already been emitted.
413  // Note that it is possible that this inline function would be emitted
414  // after trying to emit vtable speculatively. Because of this we do
415  // an extra pass after emitting all deferred vtables to find and emit
416  // these vtables opportunistically.
417  if (!Entry || Entry->isDeclaration())
418  return true;
419  }
420  return false;
421  }
422 
423  bool isVTableHidden(const CXXRecordDecl *RD) const {
424  const auto &VtableLayout =
425  CGM.getItaniumVTableContext().getVTableLayout(RD);
426 
427  for (const auto &VtableComponent : VtableLayout.vtable_components()) {
428  if (VtableComponent.isRTTIKind()) {
429  const CXXRecordDecl *RTTIDecl = VtableComponent.getRTTIDecl();
430  if (RTTIDecl->getVisibility() == Visibility::HiddenVisibility)
431  return true;
432  } else if (VtableComponent.isUsedFunctionPointerKind()) {
433  const CXXMethodDecl *Method = VtableComponent.getFunctionDecl();
434  if (Method->getVisibility() == Visibility::HiddenVisibility &&
435  !Method->isDefined())
436  return true;
437  }
438  }
439  return false;
440  }
441 };
442 
443 class ARMCXXABI : public ItaniumCXXABI {
444 public:
445  ARMCXXABI(CodeGen::CodeGenModule &CGM) :
446  ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
447  /* UseARMGuardVarABI = */ true) {}
448 
449  bool HasThisReturn(GlobalDecl GD) const override {
450  return (isa<CXXConstructorDecl>(GD.getDecl()) || (
451  isa<CXXDestructorDecl>(GD.getDecl()) &&
452  GD.getDtorType() != Dtor_Deleting));
453  }
454 
455  void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV,
456  QualType ResTy) override;
457 
458  CharUnits getArrayCookieSizeImpl(QualType elementType) override;
459  Address InitializeArrayCookie(CodeGenFunction &CGF,
460  Address NewPtr,
461  llvm::Value *NumElements,
462  const CXXNewExpr *expr,
463  QualType ElementType) override;
464  llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, Address allocPtr,
465  CharUnits cookieSize) override;
466 };
467 
468 class iOS64CXXABI : public ARMCXXABI {
469 public:
470  iOS64CXXABI(CodeGen::CodeGenModule &CGM) : ARMCXXABI(CGM) {
471  Use32BitVTableOffsetABI = true;
472  }
473 
474  // ARM64 libraries are prepared for non-unique RTTI.
475  bool shouldRTTIBeUnique() const override { return false; }
476 };
477 
478 class WebAssemblyCXXABI final : public ItaniumCXXABI {
479 public:
480  explicit WebAssemblyCXXABI(CodeGen::CodeGenModule &CGM)
481  : ItaniumCXXABI(CGM, /*UseARMMethodPtrABI=*/true,
482  /*UseARMGuardVarABI=*/true) {}
483 
484 private:
485  bool HasThisReturn(GlobalDecl GD) const override {
486  return isa<CXXConstructorDecl>(GD.getDecl()) ||
487  (isa<CXXDestructorDecl>(GD.getDecl()) &&
488  GD.getDtorType() != Dtor_Deleting);
489  }
490  bool canCallMismatchedFunctionType() const override { return false; }
491 };
492 }
493 
495  switch (CGM.getTarget().getCXXABI().getKind()) {
496  // For IR-generation purposes, there's no significant difference
497  // between the ARM and iOS ABIs.
499  case TargetCXXABI::iOS:
501  return new ARMCXXABI(CGM);
502 
503  case TargetCXXABI::iOS64:
504  return new iOS64CXXABI(CGM);
505 
506  // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't
507  // include the other 32-bit ARM oddities: constructor/destructor return values
508  // and array cookies.
510  return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
511  /* UseARMGuardVarABI = */ true);
512 
514  return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true);
515 
517  return new WebAssemblyCXXABI(CGM);
518 
520  if (CGM.getContext().getTargetInfo().getTriple().getArch()
521  == llvm::Triple::le32) {
522  // For PNaCl, use ARM-style method pointers so that PNaCl code
523  // does not assume anything about the alignment of function
524  // pointers.
525  return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
526  /* UseARMGuardVarABI = */ false);
527  }
528  return new ItaniumCXXABI(CGM);
529 
531  llvm_unreachable("Microsoft ABI is not Itanium-based");
532  }
533  llvm_unreachable("bad ABI kind");
534 }
535 
536 llvm::Type *
537 ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
538  if (MPT->isMemberDataPointer())
539  return CGM.PtrDiffTy;
540  return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy);
541 }
542 
543 /// In the Itanium and ARM ABIs, method pointers have the form:
544 /// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
545 ///
546 /// In the Itanium ABI:
547 /// - method pointers are virtual if (memptr.ptr & 1) is nonzero
548 /// - the this-adjustment is (memptr.adj)
549 /// - the virtual offset is (memptr.ptr - 1)
550 ///
551 /// In the ARM ABI:
552 /// - method pointers are virtual if (memptr.adj & 1) is nonzero
553 /// - the this-adjustment is (memptr.adj >> 1)
554 /// - the virtual offset is (memptr.ptr)
555 /// ARM uses 'adj' for the virtual flag because Thumb functions
556 /// may be only single-byte aligned.
557 ///
558 /// If the member is virtual, the adjusted 'this' pointer points
559 /// to a vtable pointer from which the virtual offset is applied.
560 ///
561 /// If the member is non-virtual, memptr.ptr is the address of
562 /// the function to call.
563 CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(
564  CodeGenFunction &CGF, const Expr *E, Address ThisAddr,
565  llvm::Value *&ThisPtrForCall,
566  llvm::Value *MemFnPtr, const MemberPointerType *MPT) {
567  CGBuilderTy &Builder = CGF.Builder;
568 
569  const FunctionProtoType *FPT =
571  const CXXRecordDecl *RD =
572  cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
573 
574  llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(
575  CGM.getTypes().arrangeCXXMethodType(RD, FPT, /*FD=*/nullptr));
576 
577  llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1);
578 
579  llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
580  llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
581  llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
582 
583  // Extract memptr.adj, which is in the second field.
584  llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
585 
586  // Compute the true adjustment.
587  llvm::Value *Adj = RawAdj;
588  if (UseARMMethodPtrABI)
589  Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
590 
591  // Apply the adjustment and cast back to the original struct type
592  // for consistency.
593  llvm::Value *This = ThisAddr.getPointer();
594  llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
595  Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
596  This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
597  ThisPtrForCall = This;
598 
599  // Load the function pointer.
600  llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
601 
602  // If the LSB in the function pointer is 1, the function pointer points to
603  // a virtual function.
604  llvm::Value *IsVirtual;
605  if (UseARMMethodPtrABI)
606  IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
607  else
608  IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
609  IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
610  Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
611 
612  // In the virtual path, the adjustment left 'This' pointing to the
613  // vtable of the correct base subobject. The "function pointer" is an
614  // offset within the vtable (+1 for the virtual flag on non-ARM).
615  CGF.EmitBlock(FnVirtual);
616 
617  // Cast the adjusted this to a pointer to vtable pointer and load.
618  llvm::Type *VTableTy = Builder.getInt8PtrTy();
619  CharUnits VTablePtrAlign =
620  CGF.CGM.getDynamicOffsetAlignment(ThisAddr.getAlignment(), RD,
621  CGF.getPointerAlign());
622  llvm::Value *VTable =
623  CGF.GetVTablePtr(Address(This, VTablePtrAlign), VTableTy, RD);
624 
625  // Apply the offset.
626  // On ARM64, to reserve extra space in virtual member function pointers,
627  // we only pay attention to the low 32 bits of the offset.
628  llvm::Value *VTableOffset = FnAsInt;
629  if (!UseARMMethodPtrABI)
630  VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
631  if (Use32BitVTableOffsetABI) {
632  VTableOffset = Builder.CreateTrunc(VTableOffset, CGF.Int32Ty);
633  VTableOffset = Builder.CreateZExt(VTableOffset, CGM.PtrDiffTy);
634  }
635  VTable = Builder.CreateGEP(VTable, VTableOffset);
636 
637  // Load the virtual function to call.
638  VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
639  llvm::Value *VirtualFn =
640  Builder.CreateAlignedLoad(VTable, CGF.getPointerAlign(),
641  "memptr.virtualfn");
642  CGF.EmitBranch(FnEnd);
643 
644  // In the non-virtual path, the function pointer is actually a
645  // function pointer.
646  CGF.EmitBlock(FnNonVirtual);
647  llvm::Value *NonVirtualFn =
648  Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
649 
650  // We're done.
651  CGF.EmitBlock(FnEnd);
652  llvm::PHINode *CalleePtr = Builder.CreatePHI(FTy->getPointerTo(), 2);
653  CalleePtr->addIncoming(VirtualFn, FnVirtual);
654  CalleePtr->addIncoming(NonVirtualFn, FnNonVirtual);
655 
656  CGCallee Callee(FPT, CalleePtr);
657  return Callee;
658 }
659 
660 /// Compute an l-value by applying the given pointer-to-member to a
661 /// base object.
662 llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(
663  CodeGenFunction &CGF, const Expr *E, Address Base, llvm::Value *MemPtr,
664  const MemberPointerType *MPT) {
665  assert(MemPtr->getType() == CGM.PtrDiffTy);
666 
667  CGBuilderTy &Builder = CGF.Builder;
668 
669  // Cast to char*.
670  Base = Builder.CreateElementBitCast(Base, CGF.Int8Ty);
671 
672  // Apply the offset, which we assume is non-null.
673  llvm::Value *Addr =
674  Builder.CreateInBoundsGEP(Base.getPointer(), MemPtr, "memptr.offset");
675 
676  // Cast the address to the appropriate pointer type, adopting the
677  // address space of the base pointer.
678  llvm::Type *PType = CGF.ConvertTypeForMem(MPT->getPointeeType())
679  ->getPointerTo(Base.getAddressSpace());
680  return Builder.CreateBitCast(Addr, PType);
681 }
682 
683 /// Perform a bitcast, derived-to-base, or base-to-derived member pointer
684 /// conversion.
685 ///
686 /// Bitcast conversions are always a no-op under Itanium.
687 ///
688 /// Obligatory offset/adjustment diagram:
689 /// <-- offset --> <-- adjustment -->
690 /// |--------------------------|----------------------|--------------------|
691 /// ^Derived address point ^Base address point ^Member address point
692 ///
693 /// So when converting a base member pointer to a derived member pointer,
694 /// we add the offset to the adjustment because the address point has
695 /// decreased; and conversely, when converting a derived MP to a base MP
696 /// we subtract the offset from the adjustment because the address point
697 /// has increased.
698 ///
699 /// The standard forbids (at compile time) conversion to and from
700 /// virtual bases, which is why we don't have to consider them here.
701 ///
702 /// The standard forbids (at run time) casting a derived MP to a base
703 /// MP when the derived MP does not point to a member of the base.
704 /// This is why -1 is a reasonable choice for null data member
705 /// pointers.
706 llvm::Value *
707 ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
708  const CastExpr *E,
709  llvm::Value *src) {
710  assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
711  E->getCastKind() == CK_BaseToDerivedMemberPointer ||
712  E->getCastKind() == CK_ReinterpretMemberPointer);
713 
714  // Under Itanium, reinterprets don't require any additional processing.
715  if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
716 
717  // Use constant emission if we can.
718  if (isa<llvm::Constant>(src))
719  return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
720 
721  llvm::Constant *adj = getMemberPointerAdjustment(E);
722  if (!adj) return src;
723 
724  CGBuilderTy &Builder = CGF.Builder;
725  bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
726 
727  const MemberPointerType *destTy =
729 
730  // For member data pointers, this is just a matter of adding the
731  // offset if the source is non-null.
732  if (destTy->isMemberDataPointer()) {
733  llvm::Value *dst;
734  if (isDerivedToBase)
735  dst = Builder.CreateNSWSub(src, adj, "adj");
736  else
737  dst = Builder.CreateNSWAdd(src, adj, "adj");
738 
739  // Null check.
740  llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType());
741  llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull");
742  return Builder.CreateSelect(isNull, src, dst);
743  }
744 
745  // The this-adjustment is left-shifted by 1 on ARM.
746  if (UseARMMethodPtrABI) {
747  uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
748  offset <<= 1;
749  adj = llvm::ConstantInt::get(adj->getType(), offset);
750  }
751 
752  llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
753  llvm::Value *dstAdj;
754  if (isDerivedToBase)
755  dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
756  else
757  dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
758 
759  return Builder.CreateInsertValue(src, dstAdj, 1);
760 }
761 
762 llvm::Constant *
763 ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
764  llvm::Constant *src) {
765  assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
766  E->getCastKind() == CK_BaseToDerivedMemberPointer ||
767  E->getCastKind() == CK_ReinterpretMemberPointer);
768 
769  // Under Itanium, reinterprets don't require any additional processing.
770  if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
771 
772  // If the adjustment is trivial, we don't need to do anything.
773  llvm::Constant *adj = getMemberPointerAdjustment(E);
774  if (!adj) return src;
775 
776  bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
777 
778  const MemberPointerType *destTy =
780 
781  // For member data pointers, this is just a matter of adding the
782  // offset if the source is non-null.
783  if (destTy->isMemberDataPointer()) {
784  // null maps to null.
785  if (src->isAllOnesValue()) return src;
786 
787  if (isDerivedToBase)
788  return llvm::ConstantExpr::getNSWSub(src, adj);
789  else
790  return llvm::ConstantExpr::getNSWAdd(src, adj);
791  }
792 
793  // The this-adjustment is left-shifted by 1 on ARM.
794  if (UseARMMethodPtrABI) {
795  uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
796  offset <<= 1;
797  adj = llvm::ConstantInt::get(adj->getType(), offset);
798  }
799 
800  llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
801  llvm::Constant *dstAdj;
802  if (isDerivedToBase)
803  dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
804  else
805  dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
806 
807  return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1);
808 }
809 
810 llvm::Constant *
811 ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
812  // Itanium C++ ABI 2.3:
813  // A NULL pointer is represented as -1.
814  if (MPT->isMemberDataPointer())
815  return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true);
816 
817  llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0);
818  llvm::Constant *Values[2] = { Zero, Zero };
819  return llvm::ConstantStruct::getAnon(Values);
820 }
821 
822 llvm::Constant *
823 ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
824  CharUnits offset) {
825  // Itanium C++ ABI 2.3:
826  // A pointer to data member is an offset from the base address of
827  // the class object containing it, represented as a ptrdiff_t
828  return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity());
829 }
830 
831 llvm::Constant *
832 ItaniumCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) {
833  return BuildMemberPointer(MD, CharUnits::Zero());
834 }
835 
836 llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
838  assert(MD->isInstance() && "Member function must not be static!");
839  MD = MD->getCanonicalDecl();
840 
841  CodeGenTypes &Types = CGM.getTypes();
842 
843  // Get the function pointer (or index if this is a virtual function).
844  llvm::Constant *MemPtr[2];
845  if (MD->isVirtual()) {
846  uint64_t Index = CGM.getItaniumVTableContext().getMethodVTableIndex(MD);
847 
848  const ASTContext &Context = getContext();
849  CharUnits PointerWidth =
850  Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
851  uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
852 
853  if (UseARMMethodPtrABI) {
854  // ARM C++ ABI 3.2.1:
855  // This ABI specifies that adj contains twice the this
856  // adjustment, plus 1 if the member function is virtual. The
857  // least significant bit of adj then makes exactly the same
858  // discrimination as the least significant bit of ptr does for
859  // Itanium.
860  MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset);
861  MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
862  2 * ThisAdjustment.getQuantity() + 1);
863  } else {
864  // Itanium C++ ABI 2.3:
865  // For a virtual function, [the pointer field] is 1 plus the
866  // virtual table offset (in bytes) of the function,
867  // represented as a ptrdiff_t.
868  MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1);
869  MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
870  ThisAdjustment.getQuantity());
871  }
872  } else {
873  const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
874  llvm::Type *Ty;
875  // Check whether the function has a computable LLVM signature.
876  if (Types.isFuncTypeConvertible(FPT)) {
877  // The function has a computable LLVM signature; use the correct type.
878  Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
879  } else {
880  // Use an arbitrary non-function type to tell GetAddrOfFunction that the
881  // function type is incomplete.
882  Ty = CGM.PtrDiffTy;
883  }
884  llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
885 
886  MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy);
887  MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
888  (UseARMMethodPtrABI ? 2 : 1) *
889  ThisAdjustment.getQuantity());
890  }
891 
892  return llvm::ConstantStruct::getAnon(MemPtr);
893 }
894 
895 llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
896  QualType MPType) {
897  const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
898  const ValueDecl *MPD = MP.getMemberPointerDecl();
899  if (!MPD)
900  return EmitNullMemberPointer(MPT);
901 
902  CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP);
903 
904  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
905  return BuildMemberPointer(MD, ThisAdjustment);
906 
907  CharUnits FieldOffset =
908  getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
909  return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
910 }
911 
912 /// The comparison algorithm is pretty easy: the member pointers are
913 /// the same if they're either bitwise identical *or* both null.
914 ///
915 /// ARM is different here only because null-ness is more complicated.
916 llvm::Value *
917 ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
918  llvm::Value *L,
919  llvm::Value *R,
920  const MemberPointerType *MPT,
921  bool Inequality) {
922  CGBuilderTy &Builder = CGF.Builder;
923 
924  llvm::ICmpInst::Predicate Eq;
925  llvm::Instruction::BinaryOps And, Or;
926  if (Inequality) {
927  Eq = llvm::ICmpInst::ICMP_NE;
928  And = llvm::Instruction::Or;
930  } else {
931  Eq = llvm::ICmpInst::ICMP_EQ;
933  Or = llvm::Instruction::Or;
934  }
935 
936  // Member data pointers are easy because there's a unique null
937  // value, so it just comes down to bitwise equality.
938  if (MPT->isMemberDataPointer())
939  return Builder.CreateICmp(Eq, L, R);
940 
941  // For member function pointers, the tautologies are more complex.
942  // The Itanium tautology is:
943  // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
944  // The ARM tautology is:
945  // (L == R) <==> (L.ptr == R.ptr &&
946  // (L.adj == R.adj ||
947  // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
948  // The inequality tautologies have exactly the same structure, except
949  // applying De Morgan's laws.
950 
951  llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
952  llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
953 
954  // This condition tests whether L.ptr == R.ptr. This must always be
955  // true for equality to hold.
956  llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
957 
958  // This condition, together with the assumption that L.ptr == R.ptr,
959  // tests whether the pointers are both null. ARM imposes an extra
960  // condition.
961  llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
962  llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
963 
964  // This condition tests whether L.adj == R.adj. If this isn't
965  // true, the pointers are unequal unless they're both null.
966  llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
967  llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
968  llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
969 
970  // Null member function pointers on ARM clear the low bit of Adj,
971  // so the zero condition has to check that neither low bit is set.
972  if (UseARMMethodPtrABI) {
973  llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
974 
975  // Compute (l.adj | r.adj) & 1 and test it against zero.
976  llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
977  llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
978  llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
979  "cmp.or.adj");
980  EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
981  }
982 
983  // Tie together all our conditions.
984  llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
985  Result = Builder.CreateBinOp(And, PtrEq, Result,
986  Inequality ? "memptr.ne" : "memptr.eq");
987  return Result;
988 }
989 
990 llvm::Value *
991 ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
992  llvm::Value *MemPtr,
993  const MemberPointerType *MPT) {
994  CGBuilderTy &Builder = CGF.Builder;
995 
996  /// For member data pointers, this is just a check against -1.
997  if (MPT->isMemberDataPointer()) {
998  assert(MemPtr->getType() == CGM.PtrDiffTy);
999  llvm::Value *NegativeOne =
1000  llvm::Constant::getAllOnesValue(MemPtr->getType());
1001  return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
1002  }
1003 
1004  // In Itanium, a member function pointer is not null if 'ptr' is not null.
1005  llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
1006 
1007  llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
1008  llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
1009 
1010  // On ARM, a member function pointer is also non-null if the low bit of 'adj'
1011  // (the virtual bit) is set.
1012  if (UseARMMethodPtrABI) {
1013  llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
1014  llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
1015  llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
1016  llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
1017  "memptr.isvirtual");
1018  Result = Builder.CreateOr(Result, IsVirtual);
1019  }
1020 
1021  return Result;
1022 }
1023 
1025  const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl();
1026  if (!RD)
1027  return false;
1028 
1029  // If C++ prohibits us from making a copy, return by address.
1030  if (passClassIndirect(RD)) {
1031  auto Align = CGM.getContext().getTypeAlignInChars(FI.getReturnType());
1032  FI.getReturnInfo() = ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
1033  return true;
1034  }
1035  return false;
1036 }
1037 
1038 /// The Itanium ABI requires non-zero initialization only for data
1039 /// member pointers, for which '0' is a valid offset.
1040 bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
1041  return MPT->isMemberFunctionPointer();
1042 }
1043 
1044 /// The Itanium ABI always places an offset to the complete object
1045 /// at entry -2 in the vtable.
1046 void ItaniumCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
1047  const CXXDeleteExpr *DE,
1048  Address Ptr,
1049  QualType ElementType,
1050  const CXXDestructorDecl *Dtor) {
1051  bool UseGlobalDelete = DE->isGlobalDelete();
1052  if (UseGlobalDelete) {
1053  // Derive the complete-object pointer, which is what we need
1054  // to pass to the deallocation function.
1055 
1056  // Grab the vtable pointer as an intptr_t*.
1057  auto *ClassDecl =
1058  cast<CXXRecordDecl>(ElementType->getAs<RecordType>()->getDecl());
1059  llvm::Value *VTable =
1060  CGF.GetVTablePtr(Ptr, CGF.IntPtrTy->getPointerTo(), ClassDecl);
1061 
1062  // Track back to entry -2 and pull out the offset there.
1063  llvm::Value *OffsetPtr = CGF.Builder.CreateConstInBoundsGEP1_64(
1064  VTable, -2, "complete-offset.ptr");
1065  llvm::Value *Offset =
1066  CGF.Builder.CreateAlignedLoad(OffsetPtr, CGF.getPointerAlign());
1067 
1068  // Apply the offset.
1069  llvm::Value *CompletePtr =
1070  CGF.Builder.CreateBitCast(Ptr.getPointer(), CGF.Int8PtrTy);
1071  CompletePtr = CGF.Builder.CreateInBoundsGEP(CompletePtr, Offset);
1072 
1073  // If we're supposed to call the global delete, make sure we do so
1074  // even if the destructor throws.
1075  CGF.pushCallObjectDeleteCleanup(DE->getOperatorDelete(), CompletePtr,
1076  ElementType);
1077  }
1078 
1079  // FIXME: Provide a source location here even though there's no
1080  // CXXMemberCallExpr for dtor call.
1081  CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting;
1082  EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr);
1083 
1084  if (UseGlobalDelete)
1085  CGF.PopCleanupBlock();
1086 }
1087 
1088 void ItaniumCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) {
1089  // void __cxa_rethrow();
1090 
1091  llvm::FunctionType *FTy =
1092  llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
1093 
1094  llvm::Constant *Fn = CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
1095 
1096  if (isNoReturn)
1097  CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, None);
1098  else
1099  CGF.EmitRuntimeCallOrInvoke(Fn);
1100 }
1101 
1102 static llvm::Constant *getAllocateExceptionFn(CodeGenModule &CGM) {
1103  // void *__cxa_allocate_exception(size_t thrown_size);
1104 
1105  llvm::FunctionType *FTy =
1106  llvm::FunctionType::get(CGM.Int8PtrTy, CGM.SizeTy, /*IsVarArgs=*/false);
1107 
1108  return CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
1109 }
1110 
1111 static llvm::Constant *getThrowFn(CodeGenModule &CGM) {
1112  // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
1113  // void (*dest) (void *));
1114 
1115  llvm::Type *Args[3] = { CGM.Int8PtrTy, CGM.Int8PtrTy, CGM.Int8PtrTy };
1116  llvm::FunctionType *FTy =
1117  llvm::FunctionType::get(CGM.VoidTy, Args, /*IsVarArgs=*/false);
1118 
1119  return CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
1120 }
1121 
1122 void ItaniumCXXABI::emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) {
1123  QualType ThrowType = E->getSubExpr()->getType();
1124  // Now allocate the exception object.
1125  llvm::Type *SizeTy = CGF.ConvertType(getContext().getSizeType());
1126  uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
1127 
1128  llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(CGM);
1129  llvm::CallInst *ExceptionPtr = CGF.EmitNounwindRuntimeCall(
1130  AllocExceptionFn, llvm::ConstantInt::get(SizeTy, TypeSize), "exception");
1131 
1132  CharUnits ExnAlign = getAlignmentOfExnObject();
1133  CGF.EmitAnyExprToExn(E->getSubExpr(), Address(ExceptionPtr, ExnAlign));
1134 
1135  // Now throw the exception.
1136  llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType,
1137  /*ForEH=*/true);
1138 
1139  // The address of the destructor. If the exception type has a
1140  // trivial destructor (or isn't a record), we just pass null.
1141  llvm::Constant *Dtor = nullptr;
1142  if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
1143  CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
1144  if (!Record->hasTrivialDestructor()) {
1145  CXXDestructorDecl *DtorD = Record->getDestructor();
1146  Dtor = CGM.getAddrOfCXXStructor(DtorD, StructorType::Complete);
1147  Dtor = llvm::ConstantExpr::getBitCast(Dtor, CGM.Int8PtrTy);
1148  }
1149  }
1150  if (!Dtor) Dtor = llvm::Constant::getNullValue(CGM.Int8PtrTy);
1151 
1152  llvm::Value *args[] = { ExceptionPtr, TypeInfo, Dtor };
1154 }
1155 
1156 static llvm::Constant *getItaniumDynamicCastFn(CodeGenFunction &CGF) {
1157  // void *__dynamic_cast(const void *sub,
1158  // const abi::__class_type_info *src,
1159  // const abi::__class_type_info *dst,
1160  // std::ptrdiff_t src2dst_offset);
1161 
1162  llvm::Type *Int8PtrTy = CGF.Int8PtrTy;
1163  llvm::Type *PtrDiffTy =
1165 
1166  llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy };
1167 
1168  llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
1169 
1170  // Mark the function as nounwind readonly.
1171  llvm::Attribute::AttrKind FuncAttrs[] = { llvm::Attribute::NoUnwind,
1172  llvm::Attribute::ReadOnly };
1173  llvm::AttributeList Attrs = llvm::AttributeList::get(
1174  CGF.getLLVMContext(), llvm::AttributeList::FunctionIndex, FuncAttrs);
1175 
1176  return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast", Attrs);
1177 }
1178 
1179 static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) {
1180  // void __cxa_bad_cast();
1181  llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1182  return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast");
1183 }
1184 
1185 /// \brief Compute the src2dst_offset hint as described in the
1186 /// Itanium C++ ABI [2.9.7]
1188  const CXXRecordDecl *Src,
1189  const CXXRecordDecl *Dst) {
1190  CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
1191  /*DetectVirtual=*/false);
1192 
1193  // If Dst is not derived from Src we can skip the whole computation below and
1194  // return that Src is not a public base of Dst. Record all inheritance paths.
1195  if (!Dst->isDerivedFrom(Src, Paths))
1196  return CharUnits::fromQuantity(-2ULL);
1197 
1198  unsigned NumPublicPaths = 0;
1199  CharUnits Offset;
1200 
1201  // Now walk all possible inheritance paths.
1202  for (const CXXBasePath &Path : Paths) {
1203  if (Path.Access != AS_public) // Ignore non-public inheritance.
1204  continue;
1205 
1206  ++NumPublicPaths;
1207 
1208  for (const CXXBasePathElement &PathElement : Path) {
1209  // If the path contains a virtual base class we can't give any hint.
1210  // -1: no hint.
1211  if (PathElement.Base->isVirtual())
1212  return CharUnits::fromQuantity(-1ULL);
1213 
1214  if (NumPublicPaths > 1) // Won't use offsets, skip computation.
1215  continue;
1216 
1217  // Accumulate the base class offsets.
1218  const ASTRecordLayout &L = Context.getASTRecordLayout(PathElement.Class);
1219  Offset += L.getBaseClassOffset(
1220  PathElement.Base->getType()->getAsCXXRecordDecl());
1221  }
1222  }
1223 
1224  // -2: Src is not a public base of Dst.
1225  if (NumPublicPaths == 0)
1226  return CharUnits::fromQuantity(-2ULL);
1227 
1228  // -3: Src is a multiple public base type but never a virtual base type.
1229  if (NumPublicPaths > 1)
1230  return CharUnits::fromQuantity(-3ULL);
1231 
1232  // Otherwise, the Src type is a unique public nonvirtual base type of Dst.
1233  // Return the offset of Src from the origin of Dst.
1234  return Offset;
1235 }
1236 
1237 static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) {
1238  // void __cxa_bad_typeid();
1239  llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false);
1240 
1241  return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid");
1242 }
1243 
1244 bool ItaniumCXXABI::shouldTypeidBeNullChecked(bool IsDeref,
1245  QualType SrcRecordTy) {
1246  return IsDeref;
1247 }
1248 
1249 void ItaniumCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) {
1250  llvm::Value *Fn = getBadTypeidFn(CGF);
1251  CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn();
1252  CGF.Builder.CreateUnreachable();
1253 }
1254 
1255 llvm::Value *ItaniumCXXABI::EmitTypeid(CodeGenFunction &CGF,
1256  QualType SrcRecordTy,
1257  Address ThisPtr,
1258  llvm::Type *StdTypeInfoPtrTy) {
1259  auto *ClassDecl =
1260  cast<CXXRecordDecl>(SrcRecordTy->getAs<RecordType>()->getDecl());
1261  llvm::Value *Value =
1262  CGF.GetVTablePtr(ThisPtr, StdTypeInfoPtrTy->getPointerTo(), ClassDecl);
1263 
1264  // Load the type info.
1265  Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL);
1266  return CGF.Builder.CreateAlignedLoad(Value, CGF.getPointerAlign());
1267 }
1268 
1269 bool ItaniumCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
1270  QualType SrcRecordTy) {
1271  return SrcIsPtr;
1272 }
1273 
1274 llvm::Value *ItaniumCXXABI::EmitDynamicCastCall(
1275  CodeGenFunction &CGF, Address ThisAddr, QualType SrcRecordTy,
1276  QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) {
1277  llvm::Type *PtrDiffLTy =
1279  llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1280 
1281  llvm::Value *SrcRTTI =
1282  CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType());
1283  llvm::Value *DestRTTI =
1284  CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType());
1285 
1286  // Compute the offset hint.
1287  const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
1288  const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl();
1289  llvm::Value *OffsetHint = llvm::ConstantInt::get(
1290  PtrDiffLTy,
1291  computeOffsetHint(CGF.getContext(), SrcDecl, DestDecl).getQuantity());
1292 
1293  // Emit the call to __dynamic_cast.
1294  llvm::Value *Value = ThisAddr.getPointer();
1295  Value = CGF.EmitCastToVoidPtr(Value);
1296 
1297  llvm::Value *args[] = {Value, SrcRTTI, DestRTTI, OffsetHint};
1298  Value = CGF.EmitNounwindRuntimeCall(getItaniumDynamicCastFn(CGF), args);
1299  Value = CGF.Builder.CreateBitCast(Value, DestLTy);
1300 
1301  /// C++ [expr.dynamic.cast]p9:
1302  /// A failed cast to reference type throws std::bad_cast
1303  if (DestTy->isReferenceType()) {
1304  llvm::BasicBlock *BadCastBlock =
1305  CGF.createBasicBlock("dynamic_cast.bad_cast");
1306 
1307  llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value);
1308  CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd);
1309 
1310  CGF.EmitBlock(BadCastBlock);
1311  EmitBadCastCall(CGF);
1312  }
1313 
1314  return Value;
1315 }
1316 
1317 llvm::Value *ItaniumCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF,
1318  Address ThisAddr,
1319  QualType SrcRecordTy,
1320  QualType DestTy) {
1321  llvm::Type *PtrDiffLTy =
1323  llvm::Type *DestLTy = CGF.ConvertType(DestTy);
1324 
1325  auto *ClassDecl =
1326  cast<CXXRecordDecl>(SrcRecordTy->getAs<RecordType>()->getDecl());
1327  // Get the vtable pointer.
1328  llvm::Value *VTable = CGF.GetVTablePtr(ThisAddr, PtrDiffLTy->getPointerTo(),
1329  ClassDecl);
1330 
1331  // Get the offset-to-top from the vtable.
1332  llvm::Value *OffsetToTop =
1333  CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL);
1334  OffsetToTop =
1335  CGF.Builder.CreateAlignedLoad(OffsetToTop, CGF.getPointerAlign(),
1336  "offset.to.top");
1337 
1338  // Finally, add the offset to the pointer.
1339  llvm::Value *Value = ThisAddr.getPointer();
1340  Value = CGF.EmitCastToVoidPtr(Value);
1341  Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop);
1342 
1343  return CGF.Builder.CreateBitCast(Value, DestLTy);
1344 }
1345 
1346 bool ItaniumCXXABI::EmitBadCastCall(CodeGenFunction &CGF) {
1347  llvm::Value *Fn = getBadCastFn(CGF);
1348  CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn();
1349  CGF.Builder.CreateUnreachable();
1350  return true;
1351 }
1352 
1353 llvm::Value *
1354 ItaniumCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF,
1355  Address This,
1356  const CXXRecordDecl *ClassDecl,
1357  const CXXRecordDecl *BaseClassDecl) {
1358  llvm::Value *VTablePtr = CGF.GetVTablePtr(This, CGM.Int8PtrTy, ClassDecl);
1359  CharUnits VBaseOffsetOffset =
1360  CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(ClassDecl,
1361  BaseClassDecl);
1362 
1363  llvm::Value *VBaseOffsetPtr =
1364  CGF.Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(),
1365  "vbase.offset.ptr");
1366  VBaseOffsetPtr = CGF.Builder.CreateBitCast(VBaseOffsetPtr,
1367  CGM.PtrDiffTy->getPointerTo());
1368 
1369  llvm::Value *VBaseOffset =
1370  CGF.Builder.CreateAlignedLoad(VBaseOffsetPtr, CGF.getPointerAlign(),
1371  "vbase.offset");
1372 
1373  return VBaseOffset;
1374 }
1375 
1376 void ItaniumCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) {
1377  // Just make sure we're in sync with TargetCXXABI.
1378  assert(CGM.getTarget().getCXXABI().hasConstructorVariants());
1379 
1380  // The constructor used for constructing this as a base class;
1381  // ignores virtual bases.
1382  CGM.EmitGlobal(GlobalDecl(D, Ctor_Base));
1383 
1384  // The constructor used for constructing this as a complete class;
1385  // constructs the virtual bases, then calls the base constructor.
1386  if (!D->getParent()->isAbstract()) {
1387  // We don't need to emit the complete ctor if the class is abstract.
1388  CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete));
1389  }
1390 }
1391 
1393 ItaniumCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T,
1394  SmallVectorImpl<CanQualType> &ArgTys) {
1395  ASTContext &Context = getContext();
1396 
1397  // All parameters are already in place except VTT, which goes after 'this'.
1398  // These are Clang types, so we don't need to worry about sret yet.
1399 
1400  // Check if we need to add a VTT parameter (which has type void **).
1401  if (T == StructorType::Base && MD->getParent()->getNumVBases() != 0) {
1402  ArgTys.insert(ArgTys.begin() + 1,
1403  Context.getPointerType(Context.VoidPtrTy));
1404  return AddedStructorArgs::prefix(1);
1405  }
1406  return AddedStructorArgs{};
1407 }
1408 
1409 void ItaniumCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) {
1410  // The destructor used for destructing this as a base class; ignores
1411  // virtual bases.
1412  CGM.EmitGlobal(GlobalDecl(D, Dtor_Base));
1413 
1414  // The destructor used for destructing this as a most-derived class;
1415  // call the base destructor and then destructs any virtual bases.
1416  CGM.EmitGlobal(GlobalDecl(D, Dtor_Complete));
1417 
1418  // The destructor in a virtual table is always a 'deleting'
1419  // destructor, which calls the complete destructor and then uses the
1420  // appropriate operator delete.
1421  if (D->isVirtual())
1422  CGM.EmitGlobal(GlobalDecl(D, Dtor_Deleting));
1423 }
1424 
1425 void ItaniumCXXABI::addImplicitStructorParams(CodeGenFunction &CGF,
1426  QualType &ResTy,
1427  FunctionArgList &Params) {
1428  const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
1429  assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD));
1430 
1431  // Check if we need a VTT parameter as well.
1432  if (NeedsVTTParameter(CGF.CurGD)) {
1433  ASTContext &Context = getContext();
1434 
1435  // FIXME: avoid the fake decl
1436  QualType T = Context.getPointerType(Context.VoidPtrTy);
1437  auto *VTTDecl = ImplicitParamDecl::Create(
1438  Context, /*DC=*/nullptr, MD->getLocation(), &Context.Idents.get("vtt"),
1440  Params.insert(Params.begin() + 1, VTTDecl);
1441  getStructorImplicitParamDecl(CGF) = VTTDecl;
1442  }
1443 }
1444 
1445 void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
1446  // Naked functions have no prolog.
1447  if (CGF.CurFuncDecl && CGF.CurFuncDecl->hasAttr<NakedAttr>())
1448  return;
1449 
1450  /// Initialize the 'this' slot. In the Itanium C++ ABI, no prologue
1451  /// adjustments are required, becuase they are all handled by thunks.
1452  setCXXABIThisValue(CGF, loadIncomingCXXThis(CGF));
1453 
1454  /// Initialize the 'vtt' slot if needed.
1455  if (getStructorImplicitParamDecl(CGF)) {
1456  getStructorImplicitParamValue(CGF) = CGF.Builder.CreateLoad(
1457  CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), "vtt");
1458  }
1459 
1460  /// If this is a function that the ABI specifies returns 'this', initialize
1461  /// the return slot to 'this' at the start of the function.
1462  ///
1463  /// Unlike the setting of return types, this is done within the ABI
1464  /// implementation instead of by clients of CGCXXABI because:
1465  /// 1) getThisValue is currently protected
1466  /// 2) in theory, an ABI could implement 'this' returns some other way;
1467  /// HasThisReturn only specifies a contract, not the implementation
1468  if (HasThisReturn(CGF.CurGD))
1469  CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
1470 }
1471 
1472 CGCXXABI::AddedStructorArgs ItaniumCXXABI::addImplicitConstructorArgs(
1474  bool ForVirtualBase, bool Delegating, CallArgList &Args) {
1475  if (!NeedsVTTParameter(GlobalDecl(D, Type)))
1476  return AddedStructorArgs{};
1477 
1478  // Insert the implicit 'vtt' argument as the second argument.
1479  llvm::Value *VTT =
1480  CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase, Delegating);
1481  QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
1482  Args.insert(Args.begin() + 1,
1483  CallArg(RValue::get(VTT), VTTTy, /*needscopy=*/false));
1484  return AddedStructorArgs::prefix(1); // Added one arg.
1485 }
1486 
1487 void ItaniumCXXABI::EmitDestructorCall(CodeGenFunction &CGF,
1488  const CXXDestructorDecl *DD,
1489  CXXDtorType Type, bool ForVirtualBase,
1490  bool Delegating, Address This) {
1491  GlobalDecl GD(DD, Type);
1492  llvm::Value *VTT = CGF.GetVTTParameter(GD, ForVirtualBase, Delegating);
1493  QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
1494 
1495  CGCallee Callee;
1496  if (getContext().getLangOpts().AppleKext &&
1497  Type != Dtor_Base && DD->isVirtual())
1498  Callee = CGF.BuildAppleKextVirtualDestructorCall(DD, Type, DD->getParent());
1499  else
1500  Callee =
1501  CGCallee::forDirect(CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type)),
1502  DD);
1503 
1504  CGF.EmitCXXMemberOrOperatorCall(DD, Callee, ReturnValueSlot(),
1505  This.getPointer(), VTT, VTTTy,
1506  nullptr, nullptr);
1507 }
1508 
1509 void ItaniumCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT,
1510  const CXXRecordDecl *RD) {
1511  llvm::GlobalVariable *VTable = getAddrOfVTable(RD, CharUnits());
1512  if (VTable->hasInitializer())
1513  return;
1514 
1515  ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext();
1516  const VTableLayout &VTLayout = VTContext.getVTableLayout(RD);
1517  llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD);
1518  llvm::Constant *RTTI =
1519  CGM.GetAddrOfRTTIDescriptor(CGM.getContext().getTagDeclType(RD));
1520 
1521  // Create and set the initializer.
1522  ConstantInitBuilder Builder(CGM);
1523  auto Components = Builder.beginStruct();
1524  CGVT.createVTableInitializer(Components, VTLayout, RTTI);
1525  Components.finishAndSetAsInitializer(VTable);
1526 
1527  // Set the correct linkage.
1528  VTable->setLinkage(Linkage);
1529 
1530  if (CGM.supportsCOMDAT() && VTable->isWeakForLinker())
1531  VTable->setComdat(CGM.getModule().getOrInsertComdat(VTable->getName()));
1532 
1533  // Set the right visibility.
1534  CGM.setGlobalVisibility(VTable, RD, ForDefinition);
1535 
1536  // Use pointer alignment for the vtable. Otherwise we would align them based
1537  // on the size of the initializer which doesn't make sense as only single
1538  // values are read.
1539  unsigned PAlign = CGM.getTarget().getPointerAlign(0);
1540  VTable->setAlignment(getContext().toCharUnitsFromBits(PAlign).getQuantity());
1541 
1542  // If this is the magic class __cxxabiv1::__fundamental_type_info,
1543  // we will emit the typeinfo for the fundamental types. This is the
1544  // same behaviour as GCC.
1545  const DeclContext *DC = RD->getDeclContext();
1546  if (RD->getIdentifier() &&
1547  RD->getIdentifier()->isStr("__fundamental_type_info") &&
1548  isa<NamespaceDecl>(DC) && cast<NamespaceDecl>(DC)->getIdentifier() &&
1549  cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
1550  DC->getParent()->isTranslationUnit())
1551  EmitFundamentalRTTIDescriptors(RD->hasAttr<DLLExportAttr>());
1552 
1553  if (!VTable->isDeclarationForLinker())
1554  CGM.EmitVTableTypeMetadata(VTable, VTLayout);
1555 }
1556 
1557 bool ItaniumCXXABI::isVirtualOffsetNeededForVTableField(
1559  if (Vptr.NearestVBase == nullptr)
1560  return false;
1561  return NeedsVTTParameter(CGF.CurGD);
1562 }
1563 
1564 llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructor(
1565  CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1566  const CXXRecordDecl *NearestVBase) {
1567 
1568  if ((Base.getBase()->getNumVBases() || NearestVBase != nullptr) &&
1569  NeedsVTTParameter(CGF.CurGD)) {
1570  return getVTableAddressPointInStructorWithVTT(CGF, VTableClass, Base,
1571  NearestVBase);
1572  }
1573  return getVTableAddressPoint(Base, VTableClass);
1574 }
1575 
1576 llvm::Constant *
1577 ItaniumCXXABI::getVTableAddressPoint(BaseSubobject Base,
1578  const CXXRecordDecl *VTableClass) {
1579  llvm::GlobalValue *VTable = getAddrOfVTable(VTableClass, CharUnits());
1580 
1581  // Find the appropriate vtable within the vtable group, and the address point
1582  // within that vtable.
1583  VTableLayout::AddressPointLocation AddressPoint =
1584  CGM.getItaniumVTableContext()
1585  .getVTableLayout(VTableClass)
1586  .getAddressPoint(Base);
1587  llvm::Value *Indices[] = {
1588  llvm::ConstantInt::get(CGM.Int32Ty, 0),
1589  llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.VTableIndex),
1590  llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.AddressPointIndex),
1591  };
1592 
1593  return llvm::ConstantExpr::getGetElementPtr(VTable->getValueType(), VTable,
1594  Indices, /*InBounds=*/true,
1595  /*InRangeIndex=*/1);
1596 }
1597 
1598 llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructorWithVTT(
1599  CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base,
1600  const CXXRecordDecl *NearestVBase) {
1601  assert((Base.getBase()->getNumVBases() || NearestVBase != nullptr) &&
1602  NeedsVTTParameter(CGF.CurGD) && "This class doesn't have VTT");
1603 
1604  // Get the secondary vpointer index.
1605  uint64_t VirtualPointerIndex =
1606  CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base);
1607 
1608  /// Load the VTT.
1609  llvm::Value *VTT = CGF.LoadCXXVTT();
1610  if (VirtualPointerIndex)
1611  VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex);
1612 
1613  // And load the address point from the VTT.
1614  return CGF.Builder.CreateAlignedLoad(VTT, CGF.getPointerAlign());
1615 }
1616 
1617 llvm::Constant *ItaniumCXXABI::getVTableAddressPointForConstExpr(
1618  BaseSubobject Base, const CXXRecordDecl *VTableClass) {
1619  return getVTableAddressPoint(Base, VTableClass);
1620 }
1621 
1622 llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD,
1623  CharUnits VPtrOffset) {
1624  assert(VPtrOffset.isZero() && "Itanium ABI only supports zero vptr offsets");
1625 
1626  llvm::GlobalVariable *&VTable = VTables[RD];
1627  if (VTable)
1628  return VTable;
1629 
1630  // Queue up this vtable for possible deferred emission.
1631  CGM.addDeferredVTable(RD);
1632 
1633  SmallString<256> Name;
1634  llvm::raw_svector_ostream Out(Name);
1635  getMangleContext().mangleCXXVTable(RD, Out);
1636 
1637  const VTableLayout &VTLayout =
1638  CGM.getItaniumVTableContext().getVTableLayout(RD);
1639  llvm::Type *VTableType = CGM.getVTables().getVTableType(VTLayout);
1640 
1641  VTable = CGM.CreateOrReplaceCXXRuntimeVariable(
1642  Name, VTableType, llvm::GlobalValue::ExternalLinkage);
1643  VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1644  CGM.setGlobalVisibility(VTable, RD, NotForDefinition);
1645 
1646  if (RD->hasAttr<DLLImportAttr>())
1647  VTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
1648  else if (RD->hasAttr<DLLExportAttr>())
1649  VTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
1650 
1651  return VTable;
1652 }
1653 
1654 CGCallee ItaniumCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF,
1655  GlobalDecl GD,
1656  Address This,
1657  llvm::Type *Ty,
1658  SourceLocation Loc) {
1659  GD = GD.getCanonicalDecl();
1660  Ty = Ty->getPointerTo()->getPointerTo();
1661  auto *MethodDecl = cast<CXXMethodDecl>(GD.getDecl());
1662  llvm::Value *VTable = CGF.GetVTablePtr(This, Ty, MethodDecl->getParent());
1663 
1664  uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD);
1665  llvm::Value *VFunc;
1666  if (CGF.ShouldEmitVTableTypeCheckedLoad(MethodDecl->getParent())) {
1667  VFunc = CGF.EmitVTableTypeCheckedLoad(
1668  MethodDecl->getParent(), VTable,
1669  VTableIndex * CGM.getContext().getTargetInfo().getPointerWidth(0) / 8);
1670  } else {
1671  CGF.EmitTypeMetadataCodeForVCall(MethodDecl->getParent(), VTable, Loc);
1672 
1673  llvm::Value *VFuncPtr =
1674  CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn");
1675  auto *VFuncLoad =
1676  CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.getPointerAlign());
1677 
1678  // Add !invariant.load md to virtual function load to indicate that
1679  // function didn't change inside vtable.
1680  // It's safe to add it without -fstrict-vtable-pointers, but it would not
1681  // help in devirtualization because it will only matter if we will have 2
1682  // the same virtual function loads from the same vtable load, which won't
1683  // happen without enabled devirtualization with -fstrict-vtable-pointers.
1684  if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
1685  CGM.getCodeGenOpts().StrictVTablePointers)
1686  VFuncLoad->setMetadata(
1687  llvm::LLVMContext::MD_invariant_load,
1688  llvm::MDNode::get(CGM.getLLVMContext(),
1690  VFunc = VFuncLoad;
1691  }
1692 
1693  CGCallee Callee(MethodDecl, VFunc);
1694  return Callee;
1695 }
1696 
1697 llvm::Value *ItaniumCXXABI::EmitVirtualDestructorCall(
1698  CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType,
1699  Address This, const CXXMemberCallExpr *CE) {
1700  assert(CE == nullptr || CE->arg_begin() == CE->arg_end());
1701  assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete);
1702 
1703  const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration(
1704  Dtor, getFromDtorType(DtorType));
1705  llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo);
1706  CGCallee Callee =
1707  getVirtualFunctionPointer(CGF, GlobalDecl(Dtor, DtorType), This, Ty,
1708  CE ? CE->getLocStart() : SourceLocation());
1709 
1710  CGF.EmitCXXMemberOrOperatorCall(Dtor, Callee, ReturnValueSlot(),
1711  This.getPointer(), /*ImplicitParam=*/nullptr,
1712  QualType(), CE, nullptr);
1713  return nullptr;
1714 }
1715 
1716 void ItaniumCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) {
1717  CodeGenVTables &VTables = CGM.getVTables();
1718  llvm::GlobalVariable *VTT = VTables.GetAddrOfVTT(RD);
1719  VTables.EmitVTTDefinition(VTT, CGM.getVTableLinkage(RD), RD);
1720 }
1721 
1722 bool ItaniumCXXABI::canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const {
1723  // We don't emit available_externally vtables if we are in -fapple-kext mode
1724  // because kext mode does not permit devirtualization.
1725  if (CGM.getLangOpts().AppleKext)
1726  return false;
1727 
1728  // If we don't have any not emitted inline virtual function, and if vtable is
1729  // not hidden, then we are safe to emit available_externally copy of vtable.
1730  // FIXME we can still emit a copy of the vtable if we
1731  // can emit definition of the inline functions.
1732  return !hasAnyUnusedVirtualInlineFunction(RD) && !isVTableHidden(RD);
1733 }
1735  Address InitialPtr,
1736  int64_t NonVirtualAdjustment,
1737  int64_t VirtualAdjustment,
1738  bool IsReturnAdjustment) {
1739  if (!NonVirtualAdjustment && !VirtualAdjustment)
1740  return InitialPtr.getPointer();
1741 
1742  Address V = CGF.Builder.CreateElementBitCast(InitialPtr, CGF.Int8Ty);
1743 
1744  // In a base-to-derived cast, the non-virtual adjustment is applied first.
1745  if (NonVirtualAdjustment && !IsReturnAdjustment) {
1747  CharUnits::fromQuantity(NonVirtualAdjustment));
1748  }
1749 
1750  // Perform the virtual adjustment if we have one.
1751  llvm::Value *ResultPtr;
1752  if (VirtualAdjustment) {
1753  llvm::Type *PtrDiffTy =
1755 
1756  Address VTablePtrPtr = CGF.Builder.CreateElementBitCast(V, CGF.Int8PtrTy);
1757  llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
1758 
1759  llvm::Value *OffsetPtr =
1760  CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
1761 
1762  OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
1763 
1764  // Load the adjustment offset from the vtable.
1765  llvm::Value *Offset =
1766  CGF.Builder.CreateAlignedLoad(OffsetPtr, CGF.getPointerAlign());
1767 
1768  // Adjust our pointer.
1769  ResultPtr = CGF.Builder.CreateInBoundsGEP(V.getPointer(), Offset);
1770  } else {
1771  ResultPtr = V.getPointer();
1772  }
1773 
1774  // In a derived-to-base conversion, the non-virtual adjustment is
1775  // applied second.
1776  if (NonVirtualAdjustment && IsReturnAdjustment) {
1777  ResultPtr = CGF.Builder.CreateConstInBoundsGEP1_64(ResultPtr,
1778  NonVirtualAdjustment);
1779  }
1780 
1781  // Cast back to the original type.
1782  return CGF.Builder.CreateBitCast(ResultPtr, InitialPtr.getType());
1783 }
1784 
1785 llvm::Value *ItaniumCXXABI::performThisAdjustment(CodeGenFunction &CGF,
1786  Address This,
1787  const ThisAdjustment &TA) {
1788  return performTypeAdjustment(CGF, This, TA.NonVirtual,
1790  /*IsReturnAdjustment=*/false);
1791 }
1792 
1793 llvm::Value *
1794 ItaniumCXXABI::performReturnAdjustment(CodeGenFunction &CGF, Address Ret,
1795  const ReturnAdjustment &RA) {
1796  return performTypeAdjustment(CGF, Ret, RA.NonVirtual,
1798  /*IsReturnAdjustment=*/true);
1799 }
1800 
1801 void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
1802  RValue RV, QualType ResultType) {
1803  if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
1804  return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
1805 
1806  // Destructor thunks in the ARM ABI have indeterminate results.
1808  RValue Undef = RValue::get(llvm::UndefValue::get(T));
1809  return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
1810 }
1811 
1812 /************************** Array allocation cookies **************************/
1813 
1814 CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
1815  // The array cookie is a size_t; pad that up to the element alignment.
1816  // The cookie is actually right-justified in that space.
1817  return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes),
1818  CGM.getContext().getTypeAlignInChars(elementType));
1819 }
1820 
1821 Address ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
1822  Address NewPtr,
1823  llvm::Value *NumElements,
1824  const CXXNewExpr *expr,
1825  QualType ElementType) {
1826  assert(requiresArrayCookie(expr));
1827 
1828  unsigned AS = NewPtr.getAddressSpace();
1829 
1830  ASTContext &Ctx = getContext();
1831  CharUnits SizeSize = CGF.getSizeSize();
1832 
1833  // The size of the cookie.
1834  CharUnits CookieSize =
1835  std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
1836  assert(CookieSize == getArrayCookieSizeImpl(ElementType));
1837 
1838  // Compute an offset to the cookie.
1839  Address CookiePtr = NewPtr;
1840  CharUnits CookieOffset = CookieSize - SizeSize;
1841  if (!CookieOffset.isZero())
1842  CookiePtr = CGF.Builder.CreateConstInBoundsByteGEP(CookiePtr, CookieOffset);
1843 
1844  // Write the number of elements into the appropriate slot.
1845  Address NumElementsPtr =
1846  CGF.Builder.CreateElementBitCast(CookiePtr, CGF.SizeTy);
1847  llvm::Instruction *SI = CGF.Builder.CreateStore(NumElements, NumElementsPtr);
1848 
1849  // Handle the array cookie specially in ASan.
1850  if (CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) && AS == 0 &&
1852  // The store to the CookiePtr does not need to be instrumented.
1853  CGM.getSanitizerMetadata()->disableSanitizerForInstruction(SI);
1854  llvm::FunctionType *FTy =
1855  llvm::FunctionType::get(CGM.VoidTy, NumElementsPtr.getType(), false);
1856  llvm::Constant *F =
1857  CGM.CreateRuntimeFunction(FTy, "__asan_poison_cxx_array_cookie");
1858  CGF.Builder.CreateCall(F, NumElementsPtr.getPointer());
1859  }
1860 
1861  // Finally, compute a pointer to the actual data buffer by skipping
1862  // over the cookie completely.
1863  return CGF.Builder.CreateConstInBoundsByteGEP(NewPtr, CookieSize);
1864 }
1865 
1866 llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
1867  Address allocPtr,
1868  CharUnits cookieSize) {
1869  // The element size is right-justified in the cookie.
1870  Address numElementsPtr = allocPtr;
1871  CharUnits numElementsOffset = cookieSize - CGF.getSizeSize();
1872  if (!numElementsOffset.isZero())
1873  numElementsPtr =
1874  CGF.Builder.CreateConstInBoundsByteGEP(numElementsPtr, numElementsOffset);
1875 
1876  unsigned AS = allocPtr.getAddressSpace();
1877  numElementsPtr = CGF.Builder.CreateElementBitCast(numElementsPtr, CGF.SizeTy);
1878  if (!CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) || AS != 0)
1879  return CGF.Builder.CreateLoad(numElementsPtr);
1880  // In asan mode emit a function call instead of a regular load and let the
1881  // run-time deal with it: if the shadow is properly poisoned return the
1882  // cookie, otherwise return 0 to avoid an infinite loop calling DTORs.
1883  // We can't simply ignore this load using nosanitize metadata because
1884  // the metadata may be lost.
1885  llvm::FunctionType *FTy =
1886  llvm::FunctionType::get(CGF.SizeTy, CGF.SizeTy->getPointerTo(0), false);
1887  llvm::Constant *F =
1888  CGM.CreateRuntimeFunction(FTy, "__asan_load_cxx_array_cookie");
1889  return CGF.Builder.CreateCall(F, numElementsPtr.getPointer());
1890 }
1891 
1892 CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) {
1893  // ARM says that the cookie is always:
1894  // struct array_cookie {
1895  // std::size_t element_size; // element_size != 0
1896  // std::size_t element_count;
1897  // };
1898  // But the base ABI doesn't give anything an alignment greater than
1899  // 8, so we can dismiss this as typical ABI-author blindness to
1900  // actual language complexity and round up to the element alignment.
1901  return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes),
1902  CGM.getContext().getTypeAlignInChars(elementType));
1903 }
1904 
1905 Address ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
1906  Address newPtr,
1907  llvm::Value *numElements,
1908  const CXXNewExpr *expr,
1909  QualType elementType) {
1910  assert(requiresArrayCookie(expr));
1911 
1912  // The cookie is always at the start of the buffer.
1913  Address cookie = newPtr;
1914 
1915  // The first element is the element size.
1916  cookie = CGF.Builder.CreateElementBitCast(cookie, CGF.SizeTy);
1917  llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy,
1918  getContext().getTypeSizeInChars(elementType).getQuantity());
1919  CGF.Builder.CreateStore(elementSize, cookie);
1920 
1921  // The second element is the element count.
1922  cookie = CGF.Builder.CreateConstInBoundsGEP(cookie, 1, CGF.getSizeSize());
1923  CGF.Builder.CreateStore(numElements, cookie);
1924 
1925  // Finally, compute a pointer to the actual data buffer by skipping
1926  // over the cookie completely.
1927  CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType);
1928  return CGF.Builder.CreateConstInBoundsByteGEP(newPtr, cookieSize);
1929 }
1930 
1931 llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
1932  Address allocPtr,
1933  CharUnits cookieSize) {
1934  // The number of elements is at offset sizeof(size_t) relative to
1935  // the allocated pointer.
1936  Address numElementsPtr
1937  = CGF.Builder.CreateConstInBoundsByteGEP(allocPtr, CGF.getSizeSize());
1938 
1939  numElementsPtr = CGF.Builder.CreateElementBitCast(numElementsPtr, CGF.SizeTy);
1940  return CGF.Builder.CreateLoad(numElementsPtr);
1941 }
1942 
1943 /*********************** Static local initialization **************************/
1944 
1945 static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
1946  llvm::PointerType *GuardPtrTy) {
1947  // int __cxa_guard_acquire(__guard *guard_object);
1948  llvm::FunctionType *FTy =
1949  llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
1950  GuardPtrTy, /*isVarArg=*/false);
1951  return CGM.CreateRuntimeFunction(
1952  FTy, "__cxa_guard_acquire",
1953  llvm::AttributeList::get(CGM.getLLVMContext(),
1954  llvm::AttributeList::FunctionIndex,
1955  llvm::Attribute::NoUnwind));
1956 }
1957 
1958 static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
1959  llvm::PointerType *GuardPtrTy) {
1960  // void __cxa_guard_release(__guard *guard_object);
1961  llvm::FunctionType *FTy =
1962  llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
1963  return CGM.CreateRuntimeFunction(
1964  FTy, "__cxa_guard_release",
1965  llvm::AttributeList::get(CGM.getLLVMContext(),
1966  llvm::AttributeList::FunctionIndex,
1967  llvm::Attribute::NoUnwind));
1968 }
1969 
1970 static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
1971  llvm::PointerType *GuardPtrTy) {
1972  // void __cxa_guard_abort(__guard *guard_object);
1973  llvm::FunctionType *FTy =
1974  llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
1975  return CGM.CreateRuntimeFunction(
1976  FTy, "__cxa_guard_abort",
1977  llvm::AttributeList::get(CGM.getLLVMContext(),
1978  llvm::AttributeList::FunctionIndex,
1979  llvm::Attribute::NoUnwind));
1980 }
1981 
1982 namespace {
1983  struct CallGuardAbort final : EHScopeStack::Cleanup {
1984  llvm::GlobalVariable *Guard;
1985  CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
1986 
1987  void Emit(CodeGenFunction &CGF, Flags flags) override {
1988  CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()),
1989  Guard);
1990  }
1991  };
1992 }
1993 
1994 /// The ARM code here follows the Itanium code closely enough that we
1995 /// just special-case it at particular places.
1996 void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
1997  const VarDecl &D,
1998  llvm::GlobalVariable *var,
1999  bool shouldPerformInit) {
2000  CGBuilderTy &Builder = CGF.Builder;
2001 
2002  // Inline variables that weren't instantiated from variable templates have
2003  // partially-ordered initialization within their translation unit.
2004  bool NonTemplateInline =
2005  D.isInline() &&
2007 
2008  // We only need to use thread-safe statics for local non-TLS variables and
2009  // inline variables; other global initialization is always single-threaded
2010  // or (through lazy dynamic loading in multiple threads) unsequenced.
2011  bool threadsafe = getContext().getLangOpts().ThreadsafeStatics &&
2012  (D.isLocalVarDecl() || NonTemplateInline) &&
2013  !D.getTLSKind();
2014 
2015  // If we have a global variable with internal linkage and thread-safe statics
2016  // are disabled, we can just let the guard variable be of type i8.
2017  bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
2018 
2019  llvm::IntegerType *guardTy;
2020  CharUnits guardAlignment;
2021  if (useInt8GuardVariable) {
2022  guardTy = CGF.Int8Ty;
2023  guardAlignment = CharUnits::One();
2024  } else {
2025  // Guard variables are 64 bits in the generic ABI and size width on ARM
2026  // (i.e. 32-bit on AArch32, 64-bit on AArch64).
2027  if (UseARMGuardVarABI) {
2028  guardTy = CGF.SizeTy;
2029  guardAlignment = CGF.getSizeAlign();
2030  } else {
2031  guardTy = CGF.Int64Ty;
2032  guardAlignment = CharUnits::fromQuantity(
2033  CGM.getDataLayout().getABITypeAlignment(guardTy));
2034  }
2035  }
2036  llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
2037 
2038  // Create the guard variable if we don't already have it (as we
2039  // might if we're double-emitting this function body).
2040  llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
2041  if (!guard) {
2042  // Mangle the name for the guard.
2043  SmallString<256> guardName;
2044  {
2045  llvm::raw_svector_ostream out(guardName);
2046  getMangleContext().mangleStaticGuardVariable(&D, out);
2047  }
2048 
2049  // Create the guard variable with a zero-initializer.
2050  // Just absorb linkage and visibility from the guarded variable.
2051  guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
2052  false, var->getLinkage(),
2053  llvm::ConstantInt::get(guardTy, 0),
2054  guardName.str());
2055  guard->setVisibility(var->getVisibility());
2056  // If the variable is thread-local, so is its guard variable.
2057  guard->setThreadLocalMode(var->getThreadLocalMode());
2058  guard->setAlignment(guardAlignment.getQuantity());
2059 
2060  // The ABI says: "It is suggested that it be emitted in the same COMDAT
2061  // group as the associated data object." In practice, this doesn't work for
2062  // non-ELF and non-Wasm object formats, so only do it for ELF and Wasm.
2063  llvm::Comdat *C = var->getComdat();
2064  if (!D.isLocalVarDecl() && C &&
2065  (CGM.getTarget().getTriple().isOSBinFormatELF() ||
2066  CGM.getTarget().getTriple().isOSBinFormatWasm())) {
2067  guard->setComdat(C);
2068  // An inline variable's guard function is run from the per-TU
2069  // initialization function, not via a dedicated global ctor function, so
2070  // we can't put it in a comdat.
2071  if (!NonTemplateInline)
2072  CGF.CurFn->setComdat(C);
2073  } else if (CGM.supportsCOMDAT() && guard->isWeakForLinker()) {
2074  guard->setComdat(CGM.getModule().getOrInsertComdat(guard->getName()));
2075  }
2076 
2077  CGM.setStaticLocalDeclGuardAddress(&D, guard);
2078  }
2079 
2080  Address guardAddr = Address(guard, guardAlignment);
2081 
2082  // Test whether the variable has completed initialization.
2083  //
2084  // Itanium C++ ABI 3.3.2:
2085  // The following is pseudo-code showing how these functions can be used:
2086  // if (obj_guard.first_byte == 0) {
2087  // if ( __cxa_guard_acquire (&obj_guard) ) {
2088  // try {
2089  // ... initialize the object ...;
2090  // } catch (...) {
2091  // __cxa_guard_abort (&obj_guard);
2092  // throw;
2093  // }
2094  // ... queue object destructor with __cxa_atexit() ...;
2095  // __cxa_guard_release (&obj_guard);
2096  // }
2097  // }
2098 
2099  // Load the first byte of the guard variable.
2100  llvm::LoadInst *LI =
2101  Builder.CreateLoad(Builder.CreateElementBitCast(guardAddr, CGM.Int8Ty));
2102 
2103  // Itanium ABI:
2104  // An implementation supporting thread-safety on multiprocessor
2105  // systems must also guarantee that references to the initialized
2106  // object do not occur before the load of the initialization flag.
2107  //
2108  // In LLVM, we do this by marking the load Acquire.
2109  if (threadsafe)
2110  LI->setAtomic(llvm::AtomicOrdering::Acquire);
2111 
2112  // For ARM, we should only check the first bit, rather than the entire byte:
2113  //
2114  // ARM C++ ABI 3.2.3.1:
2115  // To support the potential use of initialization guard variables
2116  // as semaphores that are the target of ARM SWP and LDREX/STREX
2117  // synchronizing instructions we define a static initialization
2118  // guard variable to be a 4-byte aligned, 4-byte word with the
2119  // following inline access protocol.
2120  // #define INITIALIZED 1
2121  // if ((obj_guard & INITIALIZED) != INITIALIZED) {
2122  // if (__cxa_guard_acquire(&obj_guard))
2123  // ...
2124  // }
2125  //
2126  // and similarly for ARM64:
2127  //
2128  // ARM64 C++ ABI 3.2.2:
2129  // This ABI instead only specifies the value bit 0 of the static guard
2130  // variable; all other bits are platform defined. Bit 0 shall be 0 when the
2131  // variable is not initialized and 1 when it is.
2132  llvm::Value *V =
2133  (UseARMGuardVarABI && !useInt8GuardVariable)
2134  ? Builder.CreateAnd(LI, llvm::ConstantInt::get(CGM.Int8Ty, 1))
2135  : LI;
2136  llvm::Value *NeedsInit = Builder.CreateIsNull(V, "guard.uninitialized");
2137 
2138  llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
2139  llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
2140 
2141  // Check if the first byte of the guard variable is zero.
2142  CGF.EmitCXXGuardedInitBranch(NeedsInit, InitCheckBlock, EndBlock,
2144 
2145  CGF.EmitBlock(InitCheckBlock);
2146 
2147  // Variables used when coping with thread-safe statics and exceptions.
2148  if (threadsafe) {
2149  // Call __cxa_guard_acquire.
2150  llvm::Value *V
2151  = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
2152 
2153  llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
2154 
2155  Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
2156  InitBlock, EndBlock);
2157 
2158  // Call __cxa_guard_abort along the exceptional edge.
2159  CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
2160 
2161  CGF.EmitBlock(InitBlock);
2162  }
2163 
2164  // Emit the initializer and add a global destructor if appropriate.
2165  CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
2166 
2167  if (threadsafe) {
2168  // Pop the guard-abort cleanup if we pushed one.
2169  CGF.PopCleanupBlock();
2170 
2171  // Call __cxa_guard_release. This cannot throw.
2172  CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy),
2173  guardAddr.getPointer());
2174  } else {
2175  Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guardAddr);
2176  }
2177 
2178  CGF.EmitBlock(EndBlock);
2179 }
2180 
2181 /// Register a global destructor using __cxa_atexit.
2183  llvm::Constant *dtor,
2184  llvm::Constant *addr,
2185  bool TLS) {
2186  const char *Name = "__cxa_atexit";
2187  if (TLS) {
2188  const llvm::Triple &T = CGF.getTarget().getTriple();
2189  Name = T.isOSDarwin() ? "_tlv_atexit" : "__cxa_thread_atexit";
2190  }
2191 
2192  // We're assuming that the destructor function is something we can
2193  // reasonably call with the default CC. Go ahead and cast it to the
2194  // right prototype.
2195  llvm::Type *dtorTy =
2196  llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo();
2197 
2198  // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
2199  llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
2200  llvm::FunctionType *atexitTy =
2201  llvm::FunctionType::get(CGF.IntTy, paramTys, false);
2202 
2203  // Fetch the actual function.
2204  llvm::Constant *atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name);
2205  if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit))
2206  fn->setDoesNotThrow();
2207 
2208  // Create a variable that binds the atexit to this shared object.
2209  llvm::Constant *handle =
2210  CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle");
2211  auto *GV = cast<llvm::GlobalValue>(handle->stripPointerCasts());
2212  GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
2213 
2214  llvm::Value *args[] = {
2215  llvm::ConstantExpr::getBitCast(dtor, dtorTy),
2216  llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy),
2217  handle
2218  };
2219  CGF.EmitNounwindRuntimeCall(atexit, args);
2220 }
2221 
2222 /// Register a global destructor as best as we know how.
2223 void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
2224  const VarDecl &D,
2225  llvm::Constant *dtor,
2226  llvm::Constant *addr) {
2227  // Use __cxa_atexit if available.
2228  if (CGM.getCodeGenOpts().CXAAtExit)
2229  return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind());
2230 
2231  if (D.getTLSKind())
2232  CGM.ErrorUnsupported(&D, "non-trivial TLS destruction");
2233 
2234  // In Apple kexts, we want to add a global destructor entry.
2235  // FIXME: shouldn't this be guarded by some variable?
2236  if (CGM.getLangOpts().AppleKext) {
2237  // Generate a global destructor entry.
2238  return CGM.AddCXXDtorEntry(dtor, addr);
2239  }
2240 
2241  CGF.registerGlobalDtorWithAtExit(D, dtor, addr);
2242 }
2243 
2244 static bool isThreadWrapperReplaceable(const VarDecl *VD,
2245  CodeGen::CodeGenModule &CGM) {
2246  assert(!VD->isStaticLocal() && "static local VarDecls don't need wrappers!");
2247  // Darwin prefers to have references to thread local variables to go through
2248  // the thread wrapper instead of directly referencing the backing variable.
2249  return VD->getTLSKind() == VarDecl::TLS_Dynamic &&
2250  CGM.getTarget().getTriple().isOSDarwin();
2251 }
2252 
2253 /// Get the appropriate linkage for the wrapper function. This is essentially
2254 /// the weak form of the variable's linkage; every translation unit which needs
2255 /// the wrapper emits a copy, and we want the linker to merge them.
2256 static llvm::GlobalValue::LinkageTypes
2258  llvm::GlobalValue::LinkageTypes VarLinkage =
2259  CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false);
2260 
2261  // For internal linkage variables, we don't need an external or weak wrapper.
2262  if (llvm::GlobalValue::isLocalLinkage(VarLinkage))
2263  return VarLinkage;
2264 
2265  // If the thread wrapper is replaceable, give it appropriate linkage.
2266  if (isThreadWrapperReplaceable(VD, CGM))
2267  if (!llvm::GlobalVariable::isLinkOnceLinkage(VarLinkage) &&
2268  !llvm::GlobalVariable::isWeakODRLinkage(VarLinkage))
2269  return VarLinkage;
2270  return llvm::GlobalValue::WeakODRLinkage;
2271 }
2272 
2273 llvm::Function *
2274 ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD,
2275  llvm::Value *Val) {
2276  // Mangle the name for the thread_local wrapper function.
2277  SmallString<256> WrapperName;
2278  {
2279  llvm::raw_svector_ostream Out(WrapperName);
2280  getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out);
2281  }
2282 
2283  // FIXME: If VD is a definition, we should regenerate the function attributes
2284  // before returning.
2285  if (llvm::Value *V = CGM.getModule().getNamedValue(WrapperName))
2286  return cast<llvm::Function>(V);
2287 
2288  QualType RetQT = VD->getType();
2289  if (RetQT->isReferenceType())
2290  RetQT = RetQT.getNonReferenceType();
2291 
2292  const CGFunctionInfo &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(
2293  getContext().getPointerType(RetQT), FunctionArgList());
2294 
2295  llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FI);
2296  llvm::Function *Wrapper =
2298  WrapperName.str(), &CGM.getModule());
2299 
2300  CGM.SetLLVMFunctionAttributes(nullptr, FI, Wrapper);
2301 
2302  if (VD->hasDefinition())
2303  CGM.SetLLVMFunctionAttributesForDefinition(nullptr, Wrapper);
2304 
2305  // Always resolve references to the wrapper at link time.
2306  if (!Wrapper->hasLocalLinkage() && !(isThreadWrapperReplaceable(VD, CGM) &&
2307  !llvm::GlobalVariable::isLinkOnceLinkage(Wrapper->getLinkage()) &&
2308  !llvm::GlobalVariable::isWeakODRLinkage(Wrapper->getLinkage())))
2309  Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility);
2310 
2311  if (isThreadWrapperReplaceable(VD, CGM)) {
2312  Wrapper->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
2313  Wrapper->addFnAttr(llvm::Attribute::NoUnwind);
2314  }
2315  return Wrapper;
2316 }
2317 
2318 void ItaniumCXXABI::EmitThreadLocalInitFuncs(
2319  CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals,
2320  ArrayRef<llvm::Function *> CXXThreadLocalInits,
2321  ArrayRef<const VarDecl *> CXXThreadLocalInitVars) {
2322  llvm::Function *InitFunc = nullptr;
2323 
2324  // Separate initializers into those with ordered (or partially-ordered)
2325  // initialization and those with unordered initialization.
2327  llvm::SmallDenseMap<const VarDecl *, llvm::Function *> UnorderedInits;
2328  for (unsigned I = 0; I != CXXThreadLocalInits.size(); ++I) {
2330  CXXThreadLocalInitVars[I]->getTemplateSpecializationKind()))
2331  UnorderedInits[CXXThreadLocalInitVars[I]->getCanonicalDecl()] =
2332  CXXThreadLocalInits[I];
2333  else
2334  OrderedInits.push_back(CXXThreadLocalInits[I]);
2335  }
2336 
2337  if (!OrderedInits.empty()) {
2338  // Generate a guarded initialization function.
2339  llvm::FunctionType *FTy =
2340  llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
2341  const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
2342  InitFunc = CGM.CreateGlobalInitOrDestructFunction(FTy, "__tls_init", FI,
2343  SourceLocation(),
2344  /*TLS=*/true);
2345  llvm::GlobalVariable *Guard = new llvm::GlobalVariable(
2346  CGM.getModule(), CGM.Int8Ty, /*isConstant=*/false,
2348  llvm::ConstantInt::get(CGM.Int8Ty, 0), "__tls_guard");
2349  Guard->setThreadLocal(true);
2350 
2351  CharUnits GuardAlign = CharUnits::One();
2352  Guard->setAlignment(GuardAlign.getQuantity());
2353 
2354  CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(InitFunc, OrderedInits,
2355  Address(Guard, GuardAlign));
2356  // On Darwin platforms, use CXX_FAST_TLS calling convention.
2357  if (CGM.getTarget().getTriple().isOSDarwin()) {
2358  InitFunc->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
2359  InitFunc->addFnAttr(llvm::Attribute::NoUnwind);
2360  }
2361  }
2362 
2363  // Emit thread wrappers.
2364  for (const VarDecl *VD : CXXThreadLocals) {
2365  llvm::GlobalVariable *Var =
2366  cast<llvm::GlobalVariable>(CGM.GetGlobalValue(CGM.getMangledName(VD)));
2367  llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Var);
2368 
2369  // Some targets require that all access to thread local variables go through
2370  // the thread wrapper. This means that we cannot attempt to create a thread
2371  // wrapper or a thread helper.
2372  if (isThreadWrapperReplaceable(VD, CGM) && !VD->hasDefinition()) {
2373  Wrapper->setLinkage(llvm::Function::ExternalLinkage);
2374  continue;
2375  }
2376 
2377  // Mangle the name for the thread_local initialization function.
2378  SmallString<256> InitFnName;
2379  {
2380  llvm::raw_svector_ostream Out(InitFnName);
2381  getMangleContext().mangleItaniumThreadLocalInit(VD, Out);
2382  }
2383 
2384  // If we have a definition for the variable, emit the initialization
2385  // function as an alias to the global Init function (if any). Otherwise,
2386  // produce a declaration of the initialization function.
2387  llvm::GlobalValue *Init = nullptr;
2388  bool InitIsInitFunc = false;
2389  if (VD->hasDefinition()) {
2390  InitIsInitFunc = true;
2391  llvm::Function *InitFuncToUse = InitFunc;
2393  InitFuncToUse = UnorderedInits.lookup(VD->getCanonicalDecl());
2394  if (InitFuncToUse)
2395  Init = llvm::GlobalAlias::create(Var->getLinkage(), InitFnName.str(),
2396  InitFuncToUse);
2397  } else {
2398  // Emit a weak global function referring to the initialization function.
2399  // This function will not exist if the TU defining the thread_local
2400  // variable in question does not need any dynamic initialization for
2401  // its thread_local variables.
2402  llvm::FunctionType *FnTy = llvm::FunctionType::get(CGM.VoidTy, false);
2403  Init = llvm::Function::Create(FnTy,
2404  llvm::GlobalVariable::ExternalWeakLinkage,
2405  InitFnName.str(), &CGM.getModule());
2406  const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
2407  CGM.SetLLVMFunctionAttributes(nullptr, FI, cast<llvm::Function>(Init));
2408  }
2409 
2410  if (Init)
2411  Init->setVisibility(Var->getVisibility());
2412 
2413  llvm::LLVMContext &Context = CGM.getModule().getContext();
2414  llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper);
2415  CGBuilderTy Builder(CGM, Entry);
2416  if (InitIsInitFunc) {
2417  if (Init) {
2418  llvm::CallInst *CallVal = Builder.CreateCall(Init);
2419  if (isThreadWrapperReplaceable(VD, CGM))
2420  CallVal->setCallingConv(llvm::CallingConv::CXX_FAST_TLS);
2421  }
2422  } else {
2423  // Don't know whether we have an init function. Call it if it exists.
2424  llvm::Value *Have = Builder.CreateIsNotNull(Init);
2425  llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2426  llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
2427  Builder.CreateCondBr(Have, InitBB, ExitBB);
2428 
2429  Builder.SetInsertPoint(InitBB);
2430  Builder.CreateCall(Init);
2431  Builder.CreateBr(ExitBB);
2432 
2433  Builder.SetInsertPoint(ExitBB);
2434  }
2435 
2436  // For a reference, the result of the wrapper function is a pointer to
2437  // the referenced object.
2438  llvm::Value *Val = Var;
2439  if (VD->getType()->isReferenceType()) {
2440  CharUnits Align = CGM.getContext().getDeclAlign(VD);
2441  Val = Builder.CreateAlignedLoad(Val, Align);
2442  }
2443  if (Val->getType() != Wrapper->getReturnType())
2445  Val, Wrapper->getReturnType(), "");
2446  Builder.CreateRet(Val);
2447  }
2448 }
2449 
2450 LValue ItaniumCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
2451  const VarDecl *VD,
2452  QualType LValType) {
2453  llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD);
2454  llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Val);
2455 
2456  llvm::CallInst *CallVal = CGF.Builder.CreateCall(Wrapper);
2457  CallVal->setCallingConv(Wrapper->getCallingConv());
2458 
2459  LValue LV;
2460  if (VD->getType()->isReferenceType())
2461  LV = CGF.MakeNaturalAlignAddrLValue(CallVal, LValType);
2462  else
2463  LV = CGF.MakeAddrLValue(CallVal, LValType,
2464  CGF.getContext().getDeclAlign(VD));
2465  // FIXME: need setObjCGCLValueClass?
2466  return LV;
2467 }
2468 
2469 /// Return whether the given global decl needs a VTT parameter, which it does
2470 /// if it's a base constructor or destructor with virtual bases.
2471 bool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) {
2472  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2473 
2474  // We don't have any virtual bases, just return early.
2475  if (!MD->getParent()->getNumVBases())
2476  return false;
2477 
2478  // Check if we have a base constructor.
2479  if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base)
2480  return true;
2481 
2482  // Check if we have a base destructor.
2483  if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
2484  return true;
2485 
2486  return false;
2487 }
2488 
2489 namespace {
2490 class ItaniumRTTIBuilder {
2491  CodeGenModule &CGM; // Per-module state.
2492  llvm::LLVMContext &VMContext;
2493  const ItaniumCXXABI &CXXABI; // Per-module state.
2494 
2495  /// Fields - The fields of the RTTI descriptor currently being built.
2497 
2498  /// GetAddrOfTypeName - Returns the mangled type name of the given type.
2499  llvm::GlobalVariable *
2500  GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage);
2501 
2502  /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
2503  /// descriptor of the given type.
2504  llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
2505 
2506  /// BuildVTablePointer - Build the vtable pointer for the given type.
2507  void BuildVTablePointer(const Type *Ty);
2508 
2509  /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
2510  /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
2511  void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
2512 
2513  /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
2514  /// classes with bases that do not satisfy the abi::__si_class_type_info
2515  /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
2516  void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
2517 
2518  /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
2519  /// for pointer types.
2520  void BuildPointerTypeInfo(QualType PointeeTy);
2521 
2522  /// BuildObjCObjectTypeInfo - Build the appropriate kind of
2523  /// type_info for an object type.
2524  void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
2525 
2526  /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
2527  /// struct, used for member pointer types.
2528  void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
2529 
2530 public:
2531  ItaniumRTTIBuilder(const ItaniumCXXABI &ABI)
2532  : CGM(ABI.CGM), VMContext(CGM.getModule().getContext()), CXXABI(ABI) {}
2533 
2534  // Pointer type info flags.
2535  enum {
2536  /// PTI_Const - Type has const qualifier.
2537  PTI_Const = 0x1,
2538 
2539  /// PTI_Volatile - Type has volatile qualifier.
2540  PTI_Volatile = 0x2,
2541 
2542  /// PTI_Restrict - Type has restrict qualifier.
2543  PTI_Restrict = 0x4,
2544 
2545  /// PTI_Incomplete - Type is incomplete.
2546  PTI_Incomplete = 0x8,
2547 
2548  /// PTI_ContainingClassIncomplete - Containing class is incomplete.
2549  /// (in pointer to member).
2550  PTI_ContainingClassIncomplete = 0x10,
2551 
2552  /// PTI_TransactionSafe - Pointee is transaction_safe function (C++ TM TS).
2553  //PTI_TransactionSafe = 0x20,
2554 
2555  /// PTI_Noexcept - Pointee is noexcept function (C++1z).
2556  PTI_Noexcept = 0x40,
2557  };
2558 
2559  // VMI type info flags.
2560  enum {
2561  /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
2562  VMI_NonDiamondRepeat = 0x1,
2563 
2564  /// VMI_DiamondShaped - Class is diamond shaped.
2565  VMI_DiamondShaped = 0x2
2566  };
2567 
2568  // Base class type info flags.
2569  enum {
2570  /// BCTI_Virtual - Base class is virtual.
2571  BCTI_Virtual = 0x1,
2572 
2573  /// BCTI_Public - Base class is public.
2574  BCTI_Public = 0x2
2575  };
2576 
2577  /// BuildTypeInfo - Build the RTTI type info struct for the given type.
2578  ///
2579  /// \param Force - true to force the creation of this RTTI value
2580  /// \param DLLExport - true to mark the RTTI value as DLLExport
2581  llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false,
2582  bool DLLExport = false);
2583 };
2584 }
2585 
2586 llvm::GlobalVariable *ItaniumRTTIBuilder::GetAddrOfTypeName(
2587  QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage) {
2588  SmallString<256> Name;
2589  llvm::raw_svector_ostream Out(Name);
2590  CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
2591 
2592  // We know that the mangled name of the type starts at index 4 of the
2593  // mangled name of the typename, so we can just index into it in order to
2594  // get the mangled name of the type.
2595  llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext,
2596  Name.substr(4));
2597 
2598  llvm::GlobalVariable *GV =
2599  CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage);
2600 
2601  GV->setInitializer(Init);
2602 
2603  return GV;
2604 }
2605 
2606 llvm::Constant *
2607 ItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
2608  // Mangle the RTTI name.
2609  SmallString<256> Name;
2610  llvm::raw_svector_ostream Out(Name);
2611  CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
2612 
2613  // Look for an existing global.
2614  llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
2615 
2616  if (!GV) {
2617  // Create a new global variable.
2618  // Note for the future: If we would ever like to do deferred emission of
2619  // RTTI, check if emitting vtables opportunistically need any adjustment.
2620 
2621  GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
2622  /*Constant=*/true,
2624  Name);
2625  if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2626  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
2627  if (RD->hasAttr<DLLImportAttr>())
2628  GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
2629  }
2630  }
2631 
2632  return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
2633 }
2634 
2635 /// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
2636 /// info for that type is defined in the standard library.
2638  // Itanium C++ ABI 2.9.2:
2639  // Basic type information (e.g. for "int", "bool", etc.) will be kept in
2640  // the run-time support library. Specifically, the run-time support
2641  // library should contain type_info objects for the types X, X* and
2642  // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
2643  // unsigned char, signed char, short, unsigned short, int, unsigned int,
2644  // long, unsigned long, long long, unsigned long long, float, double,
2645  // long double, char16_t, char32_t, and the IEEE 754r decimal and
2646  // half-precision floating point types.
2647  //
2648  // GCC also emits RTTI for __int128.
2649  // FIXME: We do not emit RTTI information for decimal types here.
2650 
2651  // Types added here must also be added to EmitFundamentalRTTIDescriptors.
2652  switch (Ty->getKind()) {
2653  case BuiltinType::Void:
2654  case BuiltinType::NullPtr:
2655  case BuiltinType::Bool:
2656  case BuiltinType::WChar_S:
2657  case BuiltinType::WChar_U:
2658  case BuiltinType::Char_U:
2659  case BuiltinType::Char_S:
2660  case BuiltinType::UChar:
2661  case BuiltinType::SChar:
2662  case BuiltinType::Short:
2663  case BuiltinType::UShort:
2664  case BuiltinType::Int:
2665  case BuiltinType::UInt:
2666  case BuiltinType::Long:
2667  case BuiltinType::ULong:
2668  case BuiltinType::LongLong:
2669  case BuiltinType::ULongLong:
2670  case BuiltinType::Half:
2671  case BuiltinType::Float:
2672  case BuiltinType::Double:
2673  case BuiltinType::LongDouble:
2674  case BuiltinType::Float16:
2675  case BuiltinType::Float128:
2676  case BuiltinType::Char16:
2677  case BuiltinType::Char32:
2678  case BuiltinType::Int128:
2679  case BuiltinType::UInt128:
2680  return true;
2681 
2682 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
2683  case BuiltinType::Id:
2684 #include "clang/Basic/OpenCLImageTypes.def"
2685  case BuiltinType::OCLSampler:
2686  case BuiltinType::OCLEvent:
2687  case BuiltinType::OCLClkEvent:
2688  case BuiltinType::OCLQueue:
2689  case BuiltinType::OCLReserveID:
2690  return false;
2691 
2692  case BuiltinType::Dependent:
2693 #define BUILTIN_TYPE(Id, SingletonId)
2694 #define PLACEHOLDER_TYPE(Id, SingletonId) \
2695  case BuiltinType::Id:
2696 #include "clang/AST/BuiltinTypes.def"
2697  llvm_unreachable("asking for RRTI for a placeholder type!");
2698 
2699  case BuiltinType::ObjCId:
2700  case BuiltinType::ObjCClass:
2701  case BuiltinType::ObjCSel:
2702  llvm_unreachable("FIXME: Objective-C types are unsupported!");
2703  }
2704 
2705  llvm_unreachable("Invalid BuiltinType Kind!");
2706 }
2707 
2708 static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
2709  QualType PointeeTy = PointerTy->getPointeeType();
2710  const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
2711  if (!BuiltinTy)
2712  return false;
2713 
2714  // Check the qualifiers.
2715  Qualifiers Quals = PointeeTy.getQualifiers();
2716  Quals.removeConst();
2717 
2718  if (!Quals.empty())
2719  return false;
2720 
2721  return TypeInfoIsInStandardLibrary(BuiltinTy);
2722 }
2723 
2724 /// IsStandardLibraryRTTIDescriptor - Returns whether the type
2725 /// information for the given type exists in the standard library.
2727  // Type info for builtin types is defined in the standard library.
2728  if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
2729  return TypeInfoIsInStandardLibrary(BuiltinTy);
2730 
2731  // Type info for some pointer types to builtin types is defined in the
2732  // standard library.
2733  if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
2734  return TypeInfoIsInStandardLibrary(PointerTy);
2735 
2736  return false;
2737 }
2738 
2739 /// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
2740 /// the given type exists somewhere else, and that we should not emit the type
2741 /// information in this translation unit. Assumes that it is not a
2742 /// standard-library type.
2744  QualType Ty) {
2745  ASTContext &Context = CGM.getContext();
2746 
2747  // If RTTI is disabled, assume it might be disabled in the
2748  // translation unit that defines any potential key function, too.
2749  if (!Context.getLangOpts().RTTI) return false;
2750 
2751  if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2752  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
2753  if (!RD->hasDefinition())
2754  return false;
2755 
2756  if (!RD->isDynamicClass())
2757  return false;
2758 
2759  // FIXME: this may need to be reconsidered if the key function
2760  // changes.
2761  // N.B. We must always emit the RTTI data ourselves if there exists a key
2762  // function.
2763  bool IsDLLImport = RD->hasAttr<DLLImportAttr>();
2764 
2765  // Don't import the RTTI but emit it locally.
2766  if (CGM.getTriple().isWindowsGNUEnvironment() && IsDLLImport)
2767  return false;
2768 
2769  if (CGM.getVTables().isVTableExternal(RD))
2770  return IsDLLImport && !CGM.getTriple().isWindowsItaniumEnvironment()
2771  ? false
2772  : true;
2773 
2774  if (IsDLLImport)
2775  return true;
2776  }
2777 
2778  return false;
2779 }
2780 
2781 /// IsIncompleteClassType - Returns whether the given record type is incomplete.
2782 static bool IsIncompleteClassType(const RecordType *RecordTy) {
2783  return !RecordTy->getDecl()->isCompleteDefinition();
2784 }
2785 
2786 /// ContainsIncompleteClassType - Returns whether the given type contains an
2787 /// incomplete class type. This is true if
2788 ///
2789 /// * The given type is an incomplete class type.
2790 /// * The given type is a pointer type whose pointee type contains an
2791 /// incomplete class type.
2792 /// * The given type is a member pointer type whose class is an incomplete
2793 /// class type.
2794 /// * The given type is a member pointer type whoise pointee type contains an
2795 /// incomplete class type.
2796 /// is an indirect or direct pointer to an incomplete class type.
2798  if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
2799  if (IsIncompleteClassType(RecordTy))
2800  return true;
2801  }
2802 
2803  if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
2804  return ContainsIncompleteClassType(PointerTy->getPointeeType());
2805 
2806  if (const MemberPointerType *MemberPointerTy =
2807  dyn_cast<MemberPointerType>(Ty)) {
2808  // Check if the class type is incomplete.
2809  const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
2810  if (IsIncompleteClassType(ClassType))
2811  return true;
2812 
2813  return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
2814  }
2815 
2816  return false;
2817 }
2818 
2819 // CanUseSingleInheritance - Return whether the given record decl has a "single,
2820 // public, non-virtual base at offset zero (i.e. the derived class is dynamic
2821 // iff the base is)", according to Itanium C++ ABI, 2.95p6b.
2822 static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
2823  // Check the number of bases.
2824  if (RD->getNumBases() != 1)
2825  return false;
2826 
2827  // Get the base.
2829 
2830  // Check that the base is not virtual.
2831  if (Base->isVirtual())
2832  return false;
2833 
2834  // Check that the base is public.
2835  if (Base->getAccessSpecifier() != AS_public)
2836  return false;
2837 
2838  // Check that the class is dynamic iff the base is.
2839  const CXXRecordDecl *BaseDecl =
2840  cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
2841  if (!BaseDecl->isEmpty() &&
2842  BaseDecl->isDynamicClass() != RD->isDynamicClass())
2843  return false;
2844 
2845  return true;
2846 }
2847 
2848 void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) {
2849  // abi::__class_type_info.
2850  static const char * const ClassTypeInfo =
2851  "_ZTVN10__cxxabiv117__class_type_infoE";
2852  // abi::__si_class_type_info.
2853  static const char * const SIClassTypeInfo =
2854  "_ZTVN10__cxxabiv120__si_class_type_infoE";
2855  // abi::__vmi_class_type_info.
2856  static const char * const VMIClassTypeInfo =
2857  "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
2858 
2859  const char *VTableName = nullptr;
2860 
2861  switch (Ty->getTypeClass()) {
2862 #define TYPE(Class, Base)
2863 #define ABSTRACT_TYPE(Class, Base)
2864 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
2865 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2866 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
2867 #include "clang/AST/TypeNodes.def"
2868  llvm_unreachable("Non-canonical and dependent types shouldn't get here");
2869 
2870  case Type::LValueReference:
2871  case Type::RValueReference:
2872  llvm_unreachable("References shouldn't get here");
2873 
2874  case Type::Auto:
2875  case Type::DeducedTemplateSpecialization:
2876  llvm_unreachable("Undeduced type shouldn't get here");
2877 
2878  case Type::Pipe:
2879  llvm_unreachable("Pipe types shouldn't get here");
2880 
2881  case Type::Builtin:
2882  // GCC treats vector and complex types as fundamental types.
2883  case Type::Vector:
2884  case Type::ExtVector:
2885  case Type::Complex:
2886  case Type::Atomic:
2887  // FIXME: GCC treats block pointers as fundamental types?!
2888  case Type::BlockPointer:
2889  // abi::__fundamental_type_info.
2890  VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
2891  break;
2892 
2893  case Type::ConstantArray:
2894  case Type::IncompleteArray:
2895  case Type::VariableArray:
2896  // abi::__array_type_info.
2897  VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
2898  break;
2899 
2900  case Type::FunctionNoProto:
2901  case Type::FunctionProto:
2902  // abi::__function_type_info.
2903  VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
2904  break;
2905 
2906  case Type::Enum:
2907  // abi::__enum_type_info.
2908  VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
2909  break;
2910 
2911  case Type::Record: {
2912  const CXXRecordDecl *RD =
2913  cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
2914 
2915  if (!RD->hasDefinition() || !RD->getNumBases()) {
2916  VTableName = ClassTypeInfo;
2917  } else if (CanUseSingleInheritance(RD)) {
2918  VTableName = SIClassTypeInfo;
2919  } else {
2920  VTableName = VMIClassTypeInfo;
2921  }
2922 
2923  break;
2924  }
2925 
2926  case Type::ObjCObject:
2927  // Ignore protocol qualifiers.
2928  Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
2929 
2930  // Handle id and Class.
2931  if (isa<BuiltinType>(Ty)) {
2932  VTableName = ClassTypeInfo;
2933  break;
2934  }
2935 
2936  assert(isa<ObjCInterfaceType>(Ty));
2937  // Fall through.
2938 
2939  case Type::ObjCInterface:
2940  if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
2941  VTableName = SIClassTypeInfo;
2942  } else {
2943  VTableName = ClassTypeInfo;
2944  }
2945  break;
2946 
2947  case Type::ObjCObjectPointer:
2948  case Type::Pointer:
2949  // abi::__pointer_type_info.
2950  VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
2951  break;
2952 
2953  case Type::MemberPointer:
2954  // abi::__pointer_to_member_type_info.
2955  VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
2956  break;
2957  }
2958 
2959  llvm::Constant *VTable =
2960  CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy);
2961 
2962  llvm::Type *PtrDiffTy =
2964 
2965  // The vtable address point is 2.
2966  llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
2967  VTable =
2968  llvm::ConstantExpr::getInBoundsGetElementPtr(CGM.Int8PtrTy, VTable, Two);
2969  VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy);
2970 
2971  Fields.push_back(VTable);
2972 }
2973 
2974 /// \brief Return the linkage that the type info and type info name constants
2975 /// should have for the given type.
2976 static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(CodeGenModule &CGM,
2977  QualType Ty) {
2978  // Itanium C++ ABI 2.9.5p7:
2979  // In addition, it and all of the intermediate abi::__pointer_type_info
2980  // structs in the chain down to the abi::__class_type_info for the
2981  // incomplete class type must be prevented from resolving to the
2982  // corresponding type_info structs for the complete class type, possibly
2983  // by making them local static objects. Finally, a dummy class RTTI is
2984  // generated for the incomplete type that will not resolve to the final
2985  // complete class RTTI (because the latter need not exist), possibly by
2986  // making it a local static object.
2989 
2990  switch (Ty->getLinkage()) {
2991  case NoLinkage:
2992  case InternalLinkage:
2993  case UniqueExternalLinkage:
2995 
2996  case VisibleNoLinkage:
2997  case ModuleInternalLinkage:
2998  case ModuleLinkage:
2999  case ExternalLinkage:
3000  // RTTI is not enabled, which means that this type info struct is going
3001  // to be used for exception handling. Give it linkonce_odr linkage.
3002  if (!CGM.getLangOpts().RTTI)
3003  return llvm::GlobalValue::LinkOnceODRLinkage;
3004 
3005  if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
3006  const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
3007  if (RD->hasAttr<WeakAttr>())
3008  return llvm::GlobalValue::WeakODRLinkage;
3009  if (CGM.getTriple().isWindowsItaniumEnvironment())
3010  if (RD->hasAttr<DLLImportAttr>() &&
3013  // MinGW always uses LinkOnceODRLinkage for type info.
3014  if (RD->isDynamicClass() &&
3015  !CGM.getContext()
3016  .getTargetInfo()
3017  .getTriple()
3018  .isWindowsGNUEnvironment())
3019  return CGM.getVTableLinkage(RD);
3020  }
3021 
3022  return llvm::GlobalValue::LinkOnceODRLinkage;
3023  }
3024 
3025  llvm_unreachable("Invalid linkage!");
3026 }
3027 
3028 llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty, bool Force,
3029  bool DLLExport) {
3030  // We want to operate on the canonical type.
3031  Ty = Ty.getCanonicalType();
3032 
3033  // Check if we've already emitted an RTTI descriptor for this type.
3034  SmallString<256> Name;
3035  llvm::raw_svector_ostream Out(Name);
3036  CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
3037 
3038  llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
3039  if (OldGV && !OldGV->isDeclaration()) {
3040  assert(!OldGV->hasAvailableExternallyLinkage() &&
3041  "available_externally typeinfos not yet implemented");
3042 
3043  return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy);
3044  }
3045 
3046  // Check if there is already an external RTTI descriptor for this type.
3047  bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
3048  if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
3049  return GetAddrOfExternalRTTIDescriptor(Ty);
3050 
3051  // Emit the standard library with external linkage.
3052  llvm::GlobalVariable::LinkageTypes Linkage;
3053  if (IsStdLib)
3055  else
3056  Linkage = getTypeInfoLinkage(CGM, Ty);
3057 
3058  // Add the vtable pointer.
3059  BuildVTablePointer(cast<Type>(Ty));
3060 
3061  // And the name.
3062  llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
3063  llvm::Constant *TypeNameField;
3064 
3065  // If we're supposed to demote the visibility, be sure to set a flag
3066  // to use a string comparison for type_info comparisons.
3067  ItaniumCXXABI::RTTIUniquenessKind RTTIUniqueness =
3068  CXXABI.classifyRTTIUniqueness(Ty, Linkage);
3069  if (RTTIUniqueness != ItaniumCXXABI::RUK_Unique) {
3070  // The flag is the sign bit, which on ARM64 is defined to be clear
3071  // for global pointers. This is very ARM64-specific.
3072  TypeNameField = llvm::ConstantExpr::getPtrToInt(TypeName, CGM.Int64Ty);
3073  llvm::Constant *flag =
3074  llvm::ConstantInt::get(CGM.Int64Ty, ((uint64_t)1) << 63);
3075  TypeNameField = llvm::ConstantExpr::getAdd(TypeNameField, flag);
3076  TypeNameField =
3077  llvm::ConstantExpr::getIntToPtr(TypeNameField, CGM.Int8PtrTy);
3078  } else {
3079  TypeNameField = llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy);
3080  }
3081  Fields.push_back(TypeNameField);
3082 
3083  switch (Ty->getTypeClass()) {
3084 #define TYPE(Class, Base)
3085 #define ABSTRACT_TYPE(Class, Base)
3086 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
3087 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3088 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
3089 #include "clang/AST/TypeNodes.def"
3090  llvm_unreachable("Non-canonical and dependent types shouldn't get here");
3091 
3092  // GCC treats vector types as fundamental types.
3093  case Type::Builtin:
3094  case Type::Vector:
3095  case Type::ExtVector:
3096  case Type::Complex:
3097  case Type::BlockPointer:
3098  // Itanium C++ ABI 2.9.5p4:
3099  // abi::__fundamental_type_info adds no data members to std::type_info.
3100  break;
3101 
3102  case Type::LValueReference:
3103  case Type::RValueReference:
3104  llvm_unreachable("References shouldn't get here");
3105 
3106  case Type::Auto:
3107  case Type::DeducedTemplateSpecialization:
3108  llvm_unreachable("Undeduced type shouldn't get here");
3109 
3110  case Type::Pipe:
3111  llvm_unreachable("Pipe type shouldn't get here");
3112 
3113  case Type::ConstantArray:
3114  case Type::IncompleteArray:
3115  case Type::VariableArray:
3116  // Itanium C++ ABI 2.9.5p5:
3117  // abi::__array_type_info adds no data members to std::type_info.
3118  break;
3119 
3120  case Type::FunctionNoProto:
3121  case Type::FunctionProto:
3122  // Itanium C++ ABI 2.9.5p5:
3123  // abi::__function_type_info adds no data members to std::type_info.
3124  break;
3125 
3126  case Type::Enum:
3127  // Itanium C++ ABI 2.9.5p5:
3128  // abi::__enum_type_info adds no data members to std::type_info.
3129  break;
3130 
3131  case Type::Record: {
3132  const CXXRecordDecl *RD =
3133  cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
3134  if (!RD->hasDefinition() || !RD->getNumBases()) {
3135  // We don't need to emit any fields.
3136  break;
3137  }
3138 
3139  if (CanUseSingleInheritance(RD))
3140  BuildSIClassTypeInfo(RD);
3141  else
3142  BuildVMIClassTypeInfo(RD);
3143 
3144  break;
3145  }
3146 
3147  case Type::ObjCObject:
3148  case Type::ObjCInterface:
3149  BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
3150  break;
3151 
3152  case Type::ObjCObjectPointer:
3153  BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
3154  break;
3155 
3156  case Type::Pointer:
3157  BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
3158  break;
3159 
3160  case Type::MemberPointer:
3161  BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
3162  break;
3163 
3164  case Type::Atomic:
3165  // No fields, at least for the moment.
3166  break;
3167  }
3168 
3169  llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields);
3170 
3171  llvm::Module &M = CGM.getModule();
3172  llvm::GlobalVariable *GV =
3173  new llvm::GlobalVariable(M, Init->getType(),
3174  /*Constant=*/true, Linkage, Init, Name);
3175 
3176  // If there's already an old global variable, replace it with the new one.
3177  if (OldGV) {
3178  GV->takeName(OldGV);
3179  llvm::Constant *NewPtr =
3180  llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
3181  OldGV->replaceAllUsesWith(NewPtr);
3182  OldGV->eraseFromParent();
3183  }
3184 
3185  if (CGM.supportsCOMDAT() && GV->isWeakForLinker())
3186  GV->setComdat(M.getOrInsertComdat(GV->getName()));
3187 
3188  // The Itanium ABI specifies that type_info objects must be globally
3189  // unique, with one exception: if the type is an incomplete class
3190  // type or a (possibly indirect) pointer to one. That exception
3191  // affects the general case of comparing type_info objects produced
3192  // by the typeid operator, which is why the comparison operators on
3193  // std::type_info generally use the type_info name pointers instead
3194  // of the object addresses. However, the language's built-in uses
3195  // of RTTI generally require class types to be complete, even when
3196  // manipulating pointers to those class types. This allows the
3197  // implementation of dynamic_cast to rely on address equality tests,
3198  // which is much faster.
3199 
3200  // All of this is to say that it's important that both the type_info
3201  // object and the type_info name be uniqued when weakly emitted.
3202 
3203  // Give the type_info object and name the formal visibility of the
3204  // type itself.
3205  llvm::GlobalValue::VisibilityTypes llvmVisibility;
3206  if (llvm::GlobalValue::isLocalLinkage(Linkage))
3207  // If the linkage is local, only default visibility makes sense.
3208  llvmVisibility = llvm::GlobalValue::DefaultVisibility;
3209  else if (RTTIUniqueness == ItaniumCXXABI::RUK_NonUniqueHidden)
3210  llvmVisibility = llvm::GlobalValue::HiddenVisibility;
3211  else
3212  llvmVisibility = CodeGenModule::GetLLVMVisibility(Ty->getVisibility());
3213 
3214  TypeName->setVisibility(llvmVisibility);
3215  GV->setVisibility(llvmVisibility);
3216 
3217  if (CGM.getTriple().isWindowsItaniumEnvironment()) {
3218  auto RD = Ty->getAsCXXRecordDecl();
3219  if (DLLExport || (RD && RD->hasAttr<DLLExportAttr>())) {
3220  TypeName->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
3221  GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
3222  } else if (RD && RD->hasAttr<DLLImportAttr>() &&
3224  TypeName->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
3225  GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
3226 
3227  // Because the typename and the typeinfo are DLL import, convert them to
3228  // declarations rather than definitions. The initializers still need to
3229  // be constructed to calculate the type for the declarations.
3230  TypeName->setInitializer(nullptr);
3231  GV->setInitializer(nullptr);
3232  }
3233  }
3234 
3235  return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
3236 }
3237 
3238 /// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
3239 /// for the given Objective-C object type.
3240 void ItaniumRTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
3241  // Drop qualifiers.
3242  const Type *T = OT->getBaseType().getTypePtr();
3243  assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
3244 
3245  // The builtin types are abi::__class_type_infos and don't require
3246  // extra fields.
3247  if (isa<BuiltinType>(T)) return;
3248 
3249  ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
3250  ObjCInterfaceDecl *Super = Class->getSuperClass();
3251 
3252  // Root classes are also __class_type_info.
3253  if (!Super) return;
3254 
3255  QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
3256 
3257  // Everything else is single inheritance.
3258  llvm::Constant *BaseTypeInfo =
3259  ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(SuperTy);
3260  Fields.push_back(BaseTypeInfo);
3261 }
3262 
3263 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
3264 /// inheritance, according to the Itanium C++ ABI, 2.95p6b.
3265 void ItaniumRTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
3266  // Itanium C++ ABI 2.9.5p6b:
3267  // It adds to abi::__class_type_info a single member pointing to the
3268  // type_info structure for the base type,
3269  llvm::Constant *BaseTypeInfo =
3270  ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(RD->bases_begin()->getType());
3271  Fields.push_back(BaseTypeInfo);
3272 }
3273 
3274 namespace {
3275  /// SeenBases - Contains virtual and non-virtual bases seen when traversing
3276  /// a class hierarchy.
3277  struct SeenBases {
3278  llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
3279  llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
3280  };
3281 }
3282 
3283 /// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
3284 /// abi::__vmi_class_type_info.
3285 ///
3287  SeenBases &Bases) {
3288 
3289  unsigned Flags = 0;
3290 
3291  const CXXRecordDecl *BaseDecl =
3292  cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
3293 
3294  if (Base->isVirtual()) {
3295  // Mark the virtual base as seen.
3296  if (!Bases.VirtualBases.insert(BaseDecl).second) {
3297  // If this virtual base has been seen before, then the class is diamond
3298  // shaped.
3299  Flags |= ItaniumRTTIBuilder::VMI_DiamondShaped;
3300  } else {
3301  if (Bases.NonVirtualBases.count(BaseDecl))
3302  Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3303  }
3304  } else {
3305  // Mark the non-virtual base as seen.
3306  if (!Bases.NonVirtualBases.insert(BaseDecl).second) {
3307  // If this non-virtual base has been seen before, then the class has non-
3308  // diamond shaped repeated inheritance.
3309  Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3310  } else {
3311  if (Bases.VirtualBases.count(BaseDecl))
3312  Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat;
3313  }
3314  }
3315 
3316  // Walk all bases.
3317  for (const auto &I : BaseDecl->bases())
3318  Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
3319 
3320  return Flags;
3321 }
3322 
3323 static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
3324  unsigned Flags = 0;
3325  SeenBases Bases;
3326 
3327  // Walk all bases.
3328  for (const auto &I : RD->bases())
3329  Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases);
3330 
3331  return Flags;
3332 }
3333 
3334 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
3335 /// classes with bases that do not satisfy the abi::__si_class_type_info
3336 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
3337 void ItaniumRTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
3338  llvm::Type *UnsignedIntLTy =
3340 
3341  // Itanium C++ ABI 2.9.5p6c:
3342  // __flags is a word with flags describing details about the class
3343  // structure, which may be referenced by using the __flags_masks
3344  // enumeration. These flags refer to both direct and indirect bases.
3345  unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
3346  Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3347 
3348  // Itanium C++ ABI 2.9.5p6c:
3349  // __base_count is a word with the number of direct proper base class
3350  // descriptions that follow.
3351  Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
3352 
3353  if (!RD->getNumBases())
3354  return;
3355 
3356  // Now add the base class descriptions.
3357 
3358  // Itanium C++ ABI 2.9.5p6c:
3359  // __base_info[] is an array of base class descriptions -- one for every
3360  // direct proper base. Each description is of the type:
3361  //
3362  // struct abi::__base_class_type_info {
3363  // public:
3364  // const __class_type_info *__base_type;
3365  // long __offset_flags;
3366  //
3367  // enum __offset_flags_masks {
3368  // __virtual_mask = 0x1,
3369  // __public_mask = 0x2,
3370  // __offset_shift = 8
3371  // };
3372  // };
3373 
3374  // If we're in mingw and 'long' isn't wide enough for a pointer, use 'long
3375  // long' instead of 'long' for __offset_flags. libstdc++abi uses long long on
3376  // LLP64 platforms.
3377  // FIXME: Consider updating libc++abi to match, and extend this logic to all
3378  // LLP64 platforms.
3379  QualType OffsetFlagsTy = CGM.getContext().LongTy;
3380  const TargetInfo &TI = CGM.getContext().getTargetInfo();
3381  if (TI.getTriple().isOSCygMing() && TI.getPointerWidth(0) > TI.getLongWidth())
3382  OffsetFlagsTy = CGM.getContext().LongLongTy;
3383  llvm::Type *OffsetFlagsLTy =
3384  CGM.getTypes().ConvertType(OffsetFlagsTy);
3385 
3386  for (const auto &Base : RD->bases()) {
3387  // The __base_type member points to the RTTI for the base type.
3388  Fields.push_back(ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(Base.getType()));
3389 
3390  const CXXRecordDecl *BaseDecl =
3391  cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
3392 
3393  int64_t OffsetFlags = 0;
3394 
3395  // All but the lower 8 bits of __offset_flags are a signed offset.
3396  // For a non-virtual base, this is the offset in the object of the base
3397  // subobject. For a virtual base, this is the offset in the virtual table of
3398  // the virtual base offset for the virtual base referenced (negative).
3399  CharUnits Offset;
3400  if (Base.isVirtual())
3401  Offset =
3403  else {
3404  const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
3405  Offset = Layout.getBaseClassOffset(BaseDecl);
3406  };
3407 
3408  OffsetFlags = uint64_t(Offset.getQuantity()) << 8;
3409 
3410  // The low-order byte of __offset_flags contains flags, as given by the
3411  // masks from the enumeration __offset_flags_masks.
3412  if (Base.isVirtual())
3413  OffsetFlags |= BCTI_Virtual;
3414  if (Base.getAccessSpecifier() == AS_public)
3415  OffsetFlags |= BCTI_Public;
3416 
3417  Fields.push_back(llvm::ConstantInt::get(OffsetFlagsLTy, OffsetFlags));
3418  }
3419 }
3420 
3421 /// Compute the flags for a __pbase_type_info, and remove the corresponding
3422 /// pieces from \p Type.
3423 static unsigned extractPBaseFlags(ASTContext &Ctx, QualType &Type) {
3424  unsigned Flags = 0;
3425 
3426  if (Type.isConstQualified())
3427  Flags |= ItaniumRTTIBuilder::PTI_Const;
3428  if (Type.isVolatileQualified())
3429  Flags |= ItaniumRTTIBuilder::PTI_Volatile;
3430  if (Type.isRestrictQualified())
3431  Flags |= ItaniumRTTIBuilder::PTI_Restrict;
3432  Type = Type.getUnqualifiedType();
3433 
3434  // Itanium C++ ABI 2.9.5p7:
3435  // When the abi::__pbase_type_info is for a direct or indirect pointer to an
3436  // incomplete class type, the incomplete target type flag is set.
3437  if (ContainsIncompleteClassType(Type))
3438  Flags |= ItaniumRTTIBuilder::PTI_Incomplete;
3439 
3440  if (auto *Proto = Type->getAs<FunctionProtoType>()) {
3441  if (Proto->isNothrow(Ctx)) {
3442  Flags |= ItaniumRTTIBuilder::PTI_Noexcept;
3443  Type = Ctx.getFunctionType(
3444  Proto->getReturnType(), Proto->getParamTypes(),
3445  Proto->getExtProtoInfo().withExceptionSpec(EST_None));
3446  }
3447  }
3448 
3449  return Flags;
3450 }
3451 
3452 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
3453 /// used for pointer types.
3454 void ItaniumRTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
3455  // Itanium C++ ABI 2.9.5p7:
3456  // __flags is a flag word describing the cv-qualification and other
3457  // attributes of the type pointed to
3458  unsigned Flags = extractPBaseFlags(CGM.getContext(), PointeeTy);
3459 
3460  llvm::Type *UnsignedIntLTy =
3462  Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3463 
3464  // Itanium C++ ABI 2.9.5p7:
3465  // __pointee is a pointer to the std::type_info derivation for the
3466  // unqualified type being pointed to.
3467  llvm::Constant *PointeeTypeInfo =
3468  ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(PointeeTy);
3469  Fields.push_back(PointeeTypeInfo);
3470 }
3471 
3472 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
3473 /// struct, used for member pointer types.
3474 void
3475 ItaniumRTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
3476  QualType PointeeTy = Ty->getPointeeType();
3477 
3478  // Itanium C++ ABI 2.9.5p7:
3479  // __flags is a flag word describing the cv-qualification and other
3480  // attributes of the type pointed to.
3481  unsigned Flags = extractPBaseFlags(CGM.getContext(), PointeeTy);
3482 
3483  const RecordType *ClassType = cast<RecordType>(Ty->getClass());
3484  if (IsIncompleteClassType(ClassType))
3485  Flags |= PTI_ContainingClassIncomplete;
3486 
3487  llvm::Type *UnsignedIntLTy =
3489  Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
3490 
3491  // Itanium C++ ABI 2.9.5p7:
3492  // __pointee is a pointer to the std::type_info derivation for the
3493  // unqualified type being pointed to.
3494  llvm::Constant *PointeeTypeInfo =
3495  ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(PointeeTy);
3496  Fields.push_back(PointeeTypeInfo);
3497 
3498  // Itanium C++ ABI 2.9.5p9:
3499  // __context is a pointer to an abi::__class_type_info corresponding to the
3500  // class type containing the member pointed to
3501  // (e.g., the "A" in "int A::*").
3502  Fields.push_back(
3503  ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(QualType(ClassType, 0)));
3504 }
3505 
3506 llvm::Constant *ItaniumCXXABI::getAddrOfRTTIDescriptor(QualType Ty) {
3507  return ItaniumRTTIBuilder(*this).BuildTypeInfo(Ty);
3508 }
3509 
3510 void ItaniumCXXABI::EmitFundamentalRTTIDescriptor(QualType Type,
3511  bool DLLExport) {
3512  QualType PointerType = getContext().getPointerType(Type);
3513  QualType PointerTypeConst = getContext().getPointerType(Type.withConst());
3514  ItaniumRTTIBuilder(*this).BuildTypeInfo(Type, /*Force=*/true, DLLExport);
3515  ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerType, /*Force=*/true,
3516  DLLExport);
3517  ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, /*Force=*/true,
3518  DLLExport);
3519 }
3520 
3521 void ItaniumCXXABI::EmitFundamentalRTTIDescriptors(bool DLLExport) {
3522  // Types added here must also be added to TypeInfoIsInStandardLibrary.
3523  QualType FundamentalTypes[] = {
3524  getContext().VoidTy, getContext().NullPtrTy,
3525  getContext().BoolTy, getContext().WCharTy,
3526  getContext().CharTy, getContext().UnsignedCharTy,
3527  getContext().SignedCharTy, getContext().ShortTy,
3528  getContext().UnsignedShortTy, getContext().IntTy,
3529  getContext().UnsignedIntTy, getContext().LongTy,
3530  getContext().UnsignedLongTy, getContext().LongLongTy,
3531  getContext().UnsignedLongLongTy, getContext().Int128Ty,
3532  getContext().UnsignedInt128Ty, getContext().HalfTy,
3533  getContext().FloatTy, getContext().DoubleTy,
3534  getContext().LongDoubleTy, getContext().Float128Ty,
3535  getContext().Char16Ty, getContext().Char32Ty
3536  };
3537  for (const QualType &FundamentalType : FundamentalTypes)
3538  EmitFundamentalRTTIDescriptor(FundamentalType, DLLExport);
3539 }
3540 
3541 /// What sort of uniqueness rules should we use for the RTTI for the
3542 /// given type?
3543 ItaniumCXXABI::RTTIUniquenessKind ItaniumCXXABI::classifyRTTIUniqueness(
3544  QualType CanTy, llvm::GlobalValue::LinkageTypes Linkage) const {
3545  if (shouldRTTIBeUnique())
3546  return RUK_Unique;
3547 
3548  // It's only necessary for linkonce_odr or weak_odr linkage.
3549  if (Linkage != llvm::GlobalValue::LinkOnceODRLinkage &&
3550  Linkage != llvm::GlobalValue::WeakODRLinkage)
3551  return RUK_Unique;
3552 
3553  // It's only necessary with default visibility.
3554  if (CanTy->getVisibility() != DefaultVisibility)
3555  return RUK_Unique;
3556 
3557  // If we're not required to publish this symbol, hide it.
3558  if (Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
3559  return RUK_NonUniqueHidden;
3560 
3561  // If we're required to publish this symbol, as we might be under an
3562  // explicit instantiation, leave it with default visibility but
3563  // enable string-comparisons.
3564  assert(Linkage == llvm::GlobalValue::WeakODRLinkage);
3565  return RUK_NonUniqueVisible;
3566 }
3567 
3568 // Find out how to codegen the complete destructor and constructor
3569 namespace {
3570 enum class StructorCodegen { Emit, RAUW, Alias, COMDAT };
3571 }
3573  const CXXMethodDecl *MD) {
3574  if (!CGM.getCodeGenOpts().CXXCtorDtorAliases)
3575  return StructorCodegen::Emit;
3576 
3577  // The complete and base structors are not equivalent if there are any virtual
3578  // bases, so emit separate functions.
3579  if (MD->getParent()->getNumVBases())
3580  return StructorCodegen::Emit;
3581 
3582  GlobalDecl AliasDecl;
3583  if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) {
3584  AliasDecl = GlobalDecl(DD, Dtor_Complete);
3585  } else {
3586  const auto *CD = cast<CXXConstructorDecl>(MD);
3587  AliasDecl = GlobalDecl(CD, Ctor_Complete);
3588  }
3589  llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3590 
3591  if (llvm::GlobalValue::isDiscardableIfUnused(Linkage))
3592  return StructorCodegen::RAUW;
3593 
3594  // FIXME: Should we allow available_externally aliases?
3595  if (!llvm::GlobalAlias::isValidLinkage(Linkage))
3596  return StructorCodegen::RAUW;
3597 
3598  if (llvm::GlobalValue::isWeakForLinker(Linkage)) {
3599  // Only ELF and wasm support COMDATs with arbitrary names (C5/D5).
3600  if (CGM.getTarget().getTriple().isOSBinFormatELF() ||
3601  CGM.getTarget().getTriple().isOSBinFormatWasm())
3602  return StructorCodegen::COMDAT;
3603  return StructorCodegen::Emit;
3604  }
3605 
3606  return StructorCodegen::Alias;
3607 }
3608 
3610  GlobalDecl AliasDecl,
3611  GlobalDecl TargetDecl) {
3612  llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl);
3613 
3614  StringRef MangledName = CGM.getMangledName(AliasDecl);
3615  llvm::GlobalValue *Entry = CGM.GetGlobalValue(MangledName);
3616  if (Entry && !Entry->isDeclaration())
3617  return;
3618 
3619  auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(TargetDecl));
3620 
3621  // Create the alias with no name.
3622  auto *Alias = llvm::GlobalAlias::create(Linkage, "", Aliasee);
3623 
3624  // Switch any previous uses to the alias.
3625  if (Entry) {
3626  assert(Entry->getType() == Aliasee->getType() &&
3627  "declaration exists with different type");
3628  Alias->takeName(Entry);
3629  Entry->replaceAllUsesWith(Alias);
3630  Entry->eraseFromParent();
3631  } else {
3632  Alias->setName(MangledName);
3633  }
3634 
3635  // Finally, set up the alias with its proper name and attributes.
3636  CGM.setAliasAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias);
3637 }
3638 
3639 void ItaniumCXXABI::emitCXXStructor(const CXXMethodDecl *MD,
3640  StructorType Type) {
3641  auto *CD = dyn_cast<CXXConstructorDecl>(MD);
3642  const CXXDestructorDecl *DD = CD ? nullptr : cast<CXXDestructorDecl>(MD);
3643 
3644  StructorCodegen CGType = getCodegenToUse(CGM, MD);
3645 
3646  if (Type == StructorType::Complete) {
3647  GlobalDecl CompleteDecl;
3648  GlobalDecl BaseDecl;
3649  if (CD) {
3650  CompleteDecl = GlobalDecl(CD, Ctor_Complete);
3651  BaseDecl = GlobalDecl(CD, Ctor_Base);
3652  } else {
3653  CompleteDecl = GlobalDecl(DD, Dtor_Complete);
3654  BaseDecl = GlobalDecl(DD, Dtor_Base);
3655  }
3656 
3657  if (CGType == StructorCodegen::Alias || CGType == StructorCodegen::COMDAT) {
3658  emitConstructorDestructorAlias(CGM, CompleteDecl, BaseDecl);
3659  return;
3660  }
3661 
3662  if (CGType == StructorCodegen::RAUW) {
3663  StringRef MangledName = CGM.getMangledName(CompleteDecl);
3664  auto *Aliasee = CGM.GetAddrOfGlobal(BaseDecl);
3665  CGM.addReplacement(MangledName, Aliasee);
3666  return;
3667  }
3668  }
3669 
3670  // The base destructor is equivalent to the base destructor of its
3671  // base class if there is exactly one non-virtual base class with a
3672  // non-trivial destructor, there are no fields with a non-trivial
3673  // destructor, and the body of the destructor is trivial.
3674  if (DD && Type == StructorType::Base && CGType != StructorCodegen::COMDAT &&
3676  return;
3677 
3678  // FIXME: The deleting destructor is equivalent to the selected operator
3679  // delete if:
3680  // * either the delete is a destroying operator delete or the destructor
3681  // would be trivial if it weren't virtual,
3682  // * the conversion from the 'this' parameter to the first parameter of the
3683  // destructor is equivalent to a bitcast,
3684  // * the destructor does not have an implicit "this" return, and
3685  // * the operator delete has the same calling convention and IR function type
3686  // as the destructor.
3687  // In such cases we should try to emit the deleting dtor as an alias to the
3688  // selected 'operator delete'.
3689 
3690  llvm::Function *Fn = CGM.codegenCXXStructor(MD, Type);
3691 
3692  if (CGType == StructorCodegen::COMDAT) {
3693  SmallString<256> Buffer;
3694  llvm::raw_svector_ostream Out(Buffer);
3695  if (DD)
3696  getMangleContext().mangleCXXDtorComdat(DD, Out);
3697  else
3698  getMangleContext().mangleCXXCtorComdat(CD, Out);
3699  llvm::Comdat *C = CGM.getModule().getOrInsertComdat(Out.str());
3700  Fn->setComdat(C);
3701  } else {
3702  CGM.maybeSetTrivialComdat(*MD, *Fn);
3703  }
3704 }
3705 
3706 static llvm::Constant *getBeginCatchFn(CodeGenModule &CGM) {
3707  // void *__cxa_begin_catch(void*);
3708  llvm::FunctionType *FTy = llvm::FunctionType::get(
3709  CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
3710 
3711  return CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
3712 }
3713 
3714 static llvm::Constant *getEndCatchFn(CodeGenModule &CGM) {
3715  // void __cxa_end_catch();
3716  llvm::FunctionType *FTy =
3717  llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
3718 
3719  return CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
3720 }
3721 
3722 static llvm::Constant *getGetExceptionPtrFn(CodeGenModule &CGM) {
3723  // void *__cxa_get_exception_ptr(void*);
3724  llvm::FunctionType *FTy = llvm::FunctionType::get(
3725  CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
3726 
3727  return CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr");
3728 }
3729 
3730 namespace {
3731  /// A cleanup to call __cxa_end_catch. In many cases, the caught
3732  /// exception type lets us state definitively that the thrown exception
3733  /// type does not have a destructor. In particular:
3734  /// - Catch-alls tell us nothing, so we have to conservatively
3735  /// assume that the thrown exception might have a destructor.
3736  /// - Catches by reference behave according to their base types.
3737  /// - Catches of non-record types will only trigger for exceptions
3738  /// of non-record types, which never have destructors.
3739  /// - Catches of record types can trigger for arbitrary subclasses
3740  /// of the caught type, so we have to assume the actual thrown
3741  /// exception type might have a throwing destructor, even if the
3742  /// caught type's destructor is trivial or nothrow.
3743  struct CallEndCatch final : EHScopeStack::Cleanup {
3744  CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {}
3745  bool MightThrow;
3746 
3747  void Emit(CodeGenFunction &CGF, Flags flags) override {
3748  if (!MightThrow) {
3750  return;
3751  }
3752 
3754  }
3755  };
3756 }
3757 
3758 /// Emits a call to __cxa_begin_catch and enters a cleanup to call
3759 /// __cxa_end_catch.
3760 ///
3761 /// \param EndMightThrow - true if __cxa_end_catch might throw
3763  llvm::Value *Exn,
3764  bool EndMightThrow) {
3765  llvm::CallInst *call =
3767 
3768  CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow);
3769 
3770  return call;
3771 }
3772 
3773 /// A "special initializer" callback for initializing a catch
3774 /// parameter during catch initialization.
3776  const VarDecl &CatchParam,
3777  Address ParamAddr,
3778  SourceLocation Loc) {
3779  // Load the exception from where the landing pad saved it.
3780  llvm::Value *Exn = CGF.getExceptionFromSlot();
3781 
3782  CanQualType CatchType =
3783  CGF.CGM.getContext().getCanonicalType(CatchParam.getType());
3784  llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType);
3785 
3786  // If we're catching by reference, we can just cast the object
3787  // pointer to the appropriate pointer.
3788  if (isa<ReferenceType>(CatchType)) {
3789  QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType();
3790  bool EndCatchMightThrow = CaughtType->isRecordType();
3791 
3792  // __cxa_begin_catch returns the adjusted object pointer.
3793  llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow);
3794 
3795  // We have no way to tell the personality function that we're
3796  // catching by reference, so if we're catching a pointer,
3797  // __cxa_begin_catch will actually return that pointer by value.
3798  if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) {
3799  QualType PointeeType = PT->getPointeeType();
3800 
3801  // When catching by reference, generally we should just ignore
3802  // this by-value pointer and use the exception object instead.
3803  if (!PointeeType->isRecordType()) {
3804 
3805  // Exn points to the struct _Unwind_Exception header, which
3806  // we have to skip past in order to reach the exception data.
3807  unsigned HeaderSize =
3809  AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize);
3810 
3811  // However, if we're catching a pointer-to-record type that won't
3812  // work, because the personality function might have adjusted
3813  // the pointer. There's actually no way for us to fully satisfy
3814  // the language/ABI contract here: we can't use Exn because it
3815  // might have the wrong adjustment, but we can't use the by-value
3816  // pointer because it's off by a level of abstraction.
3817  //
3818  // The current solution is to dump the adjusted pointer into an
3819  // alloca, which breaks language semantics (because changing the
3820  // pointer doesn't change the exception) but at least works.
3821  // The better solution would be to filter out non-exact matches
3822  // and rethrow them, but this is tricky because the rethrow
3823  // really needs to be catchable by other sites at this landing
3824  // pad. The best solution is to fix the personality function.
3825  } else {
3826  // Pull the pointer for the reference type off.
3827  llvm::Type *PtrTy =
3828  cast<llvm::PointerType>(LLVMCatchTy)->getElementType();
3829 
3830  // Create the temporary and write the adjusted pointer into it.
3831  Address ExnPtrTmp =
3832  CGF.CreateTempAlloca(PtrTy, CGF.getPointerAlign(), "exn.byref.tmp");
3833  llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
3834  CGF.Builder.CreateStore(Casted, ExnPtrTmp);
3835 
3836  // Bind the reference to the temporary.
3837  AdjustedExn = ExnPtrTmp.getPointer();
3838  }
3839  }
3840 
3841  llvm::Value *ExnCast =
3842  CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref");
3843  CGF.Builder.CreateStore(ExnCast, ParamAddr);
3844  return;
3845  }
3846 
3847  // Scalars and complexes.
3848  TypeEvaluationKind TEK = CGF.getEvaluationKind(CatchType);
3849  if (TEK != TEK_Aggregate) {
3850  llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false);
3851 
3852  // If the catch type is a pointer type, __cxa_begin_catch returns
3853  // the pointer by value.
3854  if (CatchType->hasPointerRepresentation()) {
3855  llvm::Value *CastExn =
3856  CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted");
3857 
3858  switch (CatchType.getQualifiers().getObjCLifetime()) {
3860  CastExn = CGF.EmitARCRetainNonBlock(CastExn);
3861  // fallthrough
3862 
3863  case Qualifiers::OCL_None:
3866  CGF.Builder.CreateStore(CastExn, ParamAddr);
3867  return;
3868 
3869  case Qualifiers::OCL_Weak:
3870  CGF.EmitARCInitWeak(ParamAddr, CastExn);
3871  return;
3872  }
3873  llvm_unreachable("bad ownership qualifier!");
3874  }
3875 
3876  // Otherwise, it returns a pointer into the exception object.
3877 
3878  llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
3879  llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
3880 
3881  LValue srcLV = CGF.MakeNaturalAlignAddrLValue(Cast, CatchType);
3882  LValue destLV = CGF.MakeAddrLValue(ParamAddr, CatchType);
3883  switch (TEK) {
3884  case TEK_Complex:
3885  CGF.EmitStoreOfComplex(CGF.EmitLoadOfComplex(srcLV, Loc), destLV,
3886  /*init*/ true);
3887  return;
3888  case TEK_Scalar: {
3889  llvm::Value *ExnLoad = CGF.EmitLoadOfScalar(srcLV, Loc);
3890  CGF.EmitStoreOfScalar(ExnLoad, destLV, /*init*/ true);
3891  return;
3892  }
3893  case TEK_Aggregate:
3894  llvm_unreachable("evaluation kind filtered out!");
3895  }
3896  llvm_unreachable("bad evaluation kind");
3897  }
3898 
3899  assert(isa<RecordType>(CatchType) && "unexpected catch type!");
3900  auto catchRD = CatchType->getAsCXXRecordDecl();
3901  CharUnits caughtExnAlignment = CGF.CGM.getClassPointerAlignment(catchRD);
3902 
3903  llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
3904 
3905  // Check for a copy expression. If we don't have a copy expression,
3906  // that means a trivial copy is okay.
3907  const Expr *copyExpr = CatchParam.getInit();
3908  if (!copyExpr) {
3909  llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true);
3910  Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy),
3911  caughtExnAlignment);
3912  CGF.EmitAggregateCopy(ParamAddr, adjustedExn, CatchType);
3913  return;
3914  }
3915 
3916  // We have to call __cxa_get_exception_ptr to get the adjusted
3917  // pointer before copying.
3918  llvm::CallInst *rawAdjustedExn =
3920 
3921  // Cast that to the appropriate type.
3922  Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy),
3923  caughtExnAlignment);
3924 
3925  // The copy expression is defined in terms of an OpaqueValueExpr.
3926  // Find it and map it to the adjusted expression.
3928  opaque(CGF, OpaqueValueExpr::findInCopyConstruct(copyExpr),
3929  CGF.MakeAddrLValue(adjustedExn, CatchParam.getType()));
3930 
3931  // Call the copy ctor in a terminate scope.
3932  CGF.EHStack.pushTerminate();
3933 
3934  // Perform the copy construction.
3935  CGF.EmitAggExpr(copyExpr,
3936  AggValueSlot::forAddr(ParamAddr, Qualifiers(),
3940 
3941  // Leave the terminate scope.
3942  CGF.EHStack.popTerminate();
3943 
3944  // Undo the opaque value mapping.
3945  opaque.pop();
3946 
3947  // Finally we can call __cxa_begin_catch.
3948  CallBeginCatch(CGF, Exn, true);
3949 }
3950 
3951 /// Begins a catch statement by initializing the catch variable and
3952 /// calling __cxa_begin_catch.
3953 void ItaniumCXXABI::emitBeginCatch(CodeGenFunction &CGF,
3954  const CXXCatchStmt *S) {
3955  // We have to be very careful with the ordering of cleanups here:
3956  // C++ [except.throw]p4:
3957  // The destruction [of the exception temporary] occurs
3958  // immediately after the destruction of the object declared in
3959  // the exception-declaration in the handler.
3960  //
3961  // So the precise ordering is:
3962  // 1. Construct catch variable.
3963  // 2. __cxa_begin_catch
3964  // 3. Enter __cxa_end_catch cleanup
3965  // 4. Enter dtor cleanup
3966  //
3967  // We do this by using a slightly abnormal initialization process.
3968  // Delegation sequence:
3969  // - ExitCXXTryStmt opens a RunCleanupsScope
3970  // - EmitAutoVarAlloca creates the variable and debug info
3971  // - InitCatchParam initializes the variable from the exception
3972  // - CallBeginCatch calls __cxa_begin_catch
3973  // - CallBeginCatch enters the __cxa_end_catch cleanup
3974  // - EmitAutoVarCleanups enters the variable destructor cleanup
3975  // - EmitCXXTryStmt emits the code for the catch body
3976  // - EmitCXXTryStmt close the RunCleanupsScope
3977 
3978  VarDecl *CatchParam = S->getExceptionDecl();
3979  if (!CatchParam) {
3980  llvm::Value *Exn = CGF.getExceptionFromSlot();
3981  CallBeginCatch(CGF, Exn, true);
3982  return;
3983  }
3984 
3985  // Emit the local.
3986  CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
3987  InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF), S->getLocStart());
3988  CGF.EmitAutoVarCleanups(var);
3989 }
3990 
3991 /// Get or define the following function:
3992 /// void @__clang_call_terminate(i8* %exn) nounwind noreturn
3993 /// This code is used only in C++.
3994 static llvm::Constant *getClangCallTerminateFn(CodeGenModule &CGM) {
3995  llvm::FunctionType *fnTy =
3996  llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
3997  llvm::Constant *fnRef = CGM.CreateRuntimeFunction(
3998  fnTy, "__clang_call_terminate", llvm::AttributeList(), /*Local=*/true);
3999 
4000  llvm::Function *fn = dyn_cast<llvm::Function>(fnRef);
4001  if (fn && fn->empty()) {
4002  fn->setDoesNotThrow();
4003  fn->setDoesNotReturn();
4004 
4005  // What we really want is to massively penalize inlining without
4006  // forbidding it completely. The difference between that and
4007  // 'noinline' is negligible.
4008  fn->addFnAttr(llvm::Attribute::NoInline);
4009 
4010  // Allow this function to be shared across translation units, but
4011  // we don't want it to turn into an exported symbol.
4012  fn->setLinkage(llvm::Function::LinkOnceODRLinkage);
4013  fn->setVisibility(llvm::Function::HiddenVisibility);
4014  if (CGM.supportsCOMDAT())
4015  fn->setComdat(CGM.getModule().getOrInsertComdat(fn->getName()));
4016 
4017  // Set up the function.
4018  llvm::BasicBlock *entry =
4020  CGBuilderTy builder(CGM, entry);
4021 
4022  // Pull the exception pointer out of the parameter list.
4023  llvm::Value *exn = &*fn->arg_begin();
4024 
4025  // Call __cxa_begin_catch(exn).
4026  llvm::CallInst *catchCall = builder.CreateCall(getBeginCatchFn(CGM), exn);
4027  catchCall->setDoesNotThrow();
4028  catchCall->setCallingConv(CGM.getRuntimeCC());
4029 
4030  // Call std::terminate().
4031  llvm::CallInst *termCall = builder.CreateCall(CGM.getTerminateFn());
4032  termCall->setDoesNotThrow();
4033  termCall->setDoesNotReturn();
4034  termCall->setCallingConv(CGM.getRuntimeCC());
4035 
4036  // std::terminate cannot return.
4037  builder.CreateUnreachable();
4038  }
4039 
4040  return fnRef;
4041 }
4042 
4043 llvm::CallInst *
4044 ItaniumCXXABI::emitTerminateForUnexpectedException(CodeGenFunction &CGF,
4045  llvm::Value *Exn) {
4046  // In C++, we want to call __cxa_begin_catch() before terminating.
4047  if (Exn) {
4048  assert(CGF.CGM.getLangOpts().CPlusPlus);
4049  return CGF.EmitNounwindRuntimeCall(getClangCallTerminateFn(CGF.CGM), Exn);
4050  }
4051  return CGF.EmitNounwindRuntimeCall(CGF.CGM.getTerminateFn());
4052 }
4053 
4054 std::pair<llvm::Value *, const CXXRecordDecl *>
4055 ItaniumCXXABI::LoadVTablePtr(CodeGenFunction &CGF, Address This,
4056  const CXXRecordDecl *RD) {
4057  return {CGF.GetVTablePtr(This, CGM.Int8PtrTy, RD), RD};
4058 }
static uint64_t getFieldOffset(const ASTContext &C, const FieldDecl *FD)
void pushTerminate()
Push a terminate handler on the stack.
Definition: CGCleanup.cpp:259
ReturnValueSlot - Contains the address where the return value of a function can be stored...
Definition: CGCall.h:281
The generic AArch64 ABI is also a modified version of the Itanium ABI, but it has fewer divergences t...
Definition: TargetCXXABI.h:84
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:265
void EmitVTTDefinition(llvm::GlobalVariable *VTT, llvm::GlobalVariable::LinkageTypes Linkage, const CXXRecordDecl *RD)
EmitVTTDefinition - Emit the definition of the given vtable.
Definition: CGVTT.cpp:42
CanQualType LongLongTy
Definition: ASTContext.h:1004
static const Decl * getCanonicalDecl(const Decl *D)
static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(CodeGenModule &CGM, QualType Ty)
Return the linkage that the type info and type info name constants should have for the given type...
llvm::IntegerType * IntTy
int
static llvm::GlobalValue::LinkageTypes getThreadLocalWrapperLinkage(const VarDecl *VD, CodeGen::CodeGenModule &CGM)
Get the appropriate linkage for the wrapper function.
External linkage, which indicates that the entity can be referred to from other translation units...
Definition: Linkage.h:61
static void InitCatchParam(CodeGenFunction &CGF, const VarDecl &CatchParam, Address ParamAddr, SourceLocation Loc)
A "special initializer" callback for initializing a catch parameter during catch initialization.
no exception specification
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2283
Complete object ctor.
Definition: ABI.h:26
QualType getPointeeType() const
Definition: Type.h:2296
CanQualType VoidPtrTy
Definition: ASTContext.h:1012
A (possibly-)qualified type.
Definition: Type.h:653
The iOS 64-bit ABI is follows ARM&#39;s published 64-bit ABI more closely, but we don&#39;t guarantee to foll...
Definition: TargetCXXABI.h:71
static llvm::Constant * getAllocateExceptionFn(CodeGenModule &CGM)
base_class_range bases()
Definition: DeclCXX.h:773
llvm::Type * ConvertTypeForMem(QualType T)
const CodeGenOptions & getCodeGenOpts() const
unsigned getNumBases() const
Retrieves the number of base classes of this class.
Definition: DeclCXX.h:767
Internal linkage according to the Modules TS, but can be referred to from other translation units ind...
Definition: Linkage.h:51
const Expr * getSubExpr() const
Definition: ExprCXX.h:1007
unsigned getLongWidth() const
getLongWidth/Align - Return the size of &#39;signed long&#39; and &#39;unsigned long&#39; for this target...
Definition: TargetInfo.h:351
llvm::LLVMContext & getLLVMContext()
The COMDAT used for ctors.
Definition: ABI.h:28
CXXDtorType getDtorType() const
Definition: GlobalDecl.h:71
QualType getPointerDiffType() const
Return the unique type for "ptrdiff_t" (C99 7.17) defined in <stddef.h>.
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:571
CharUnits getClassPointerAlignment(const CXXRecordDecl *CD)
Returns the assumed alignment of an opaque pointer to the given class.
Definition: CGClass.cpp:36
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
Kind getKind() const
Definition: Type.h:2164
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Definition: Type.cpp:456
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:790
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
Definition: RecordLayout.h:223
No linkage, which means that the entity is unique and can only be referred to from within its scope...
Definition: Linkage.h:28
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
Definition: DeclCXX.h:788
C Language Family Type Representation.
bool hasNonTrivialCopyConstructor() const
Determine whether this class has a non-trivial copy constructor (C++ [class.copy]p6, C++11 [class.copy]p12)
Definition: DeclCXX.h:1354
bool isRecordType() const
Definition: Type.h:6015
static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D)
Determine what kind of template specialization the given declaration is.
bool isVirtual() const
Definition: DeclCXX.h:2009
FunctionDecl * getOperatorNew() const
Definition: ExprCXX.h:1942
bool isVirtual() const
Determines whether the base class is a virtual base class (or not).
Definition: DeclCXX.h:244
void setAliasAttributes(const Decl *D, llvm::GlobalValue *GV)
Set attributes which must be preserved by an alias.
Kind getKind() const
Definition: TargetCXXABI.h:132
GlobalDecl getCanonicalDecl() const
Definition: GlobalDecl.h:56
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition: Type.h:5891
The base class of the type hierarchy.
Definition: Type.h:1351
int64_t NonVirtual
The non-virtual adjustment from the derived object to its nearest virtual base.
Definition: ABI.h:111
CanQualType LongTy
Definition: ASTContext.h:1004
bool isDerivedFrom(const CXXRecordDecl *Base) const
Determine whether this class is derived from the class Base.
bool isRestrictQualified() const
Determine whether this type is restrict-qualified.
Definition: Type.h:5782
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:116
QualType withConst() const
Definition: Type.h:818
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:671
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:3491
bool isDefined(const FunctionDecl *&Definition) const
Returns true if the function is defined at all, including a deleted definition.
Definition: Decl.cpp:2605
llvm::Value * EmitARCRetainNonBlock(llvm::Value *value)
Retain the given object, with normal retain semantics.
Definition: CGObjC.cpp:1981
Represents a path from a specific derived class (which is not represented as part of the path) to a p...
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2397
bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D)
Try to emit a base destructor as an alias to its primary base-class destructor.
Definition: CGCXX.cpp:34
static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V)
Default closure variant of a ctor.
Definition: ABI.h:30
bool isCompleteDefinition() const
isCompleteDefinition - Return true if this decl has its body fully specified.
Definition: Decl.h:3097
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
TypeEvaluationKind
The kind of evaluation to perform on values of a particular type.
static const OpaqueValueExpr * findInCopyConstruct(const Expr *expr)
Given an expression which invokes a copy constructor — i.e.
Definition: Expr.cpp:3883
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:806
Address getObjectAddress(CodeGenFunction &CGF) const
Returns the address of the object within this declaration.
Objects with "hidden" visibility are not seen by the dynamic linker.
Definition: Visibility.h:35
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6305
SourceLocation getLocStart() const LLVM_READONLY
Definition: StmtCXX.h:44
A this pointer adjustment.
Definition: ABI.h:108
llvm::Value * GetVTTParameter(GlobalDecl GD, bool ForVirtualBase, bool Delegating)
GetVTTParameter - Return the VTT parameter that should be passed to a base constructor/destructor wit...
Definition: CGClass.cpp:431
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
llvm::Value * getPointer() const
Definition: Address.h:38
Attempt to be ABI-compatible with code generated by Clang 4.0.x (SVN r291814).
A C++ throw-expression (C++ [except.throw]).
Definition: ExprCXX.h:985
bool hasDefinition() const
Definition: DeclCXX.h:738
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have...
Definition: Linkage.h:25
RValue EmitCXXMemberOrOperatorCall(const CXXMethodDecl *Method, const CGCallee &Callee, ReturnValueSlot ReturnValue, llvm::Value *This, llvm::Value *ImplicitParam, QualType ImplicitParamTy, const CallExpr *E, CallArgList *RtlArgs)
Definition: CGExprCXX.cpp:81
unsigned getAddressSpace() const
Return the address space that this address resides in.
Definition: Address.h:57
The collection of all-type qualifiers we support.
Definition: Type.h:152
const ValueDecl * getMemberPointerDecl() const
Definition: APValue.cpp:615
Qualifiers getQualifiers() const
Retrieve all qualifiers.
IdentifierInfo * getIdentifier() const
getIdentifier - Get the identifier that names this declaration, if there is one.
Definition: Decl.h:265
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
Definition: DeclCXX.h:1279
uint64_t getPointerWidth(unsigned AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition: TargetInfo.h:311
const TargetInfo & getTarget() const
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Definition: TargetInfo.h:859
static llvm::Constant * getClangCallTerminateFn(CodeGenModule &CGM)
Get or define the following function: void (i8* exn) nounwind noreturn This code is used only in C++...
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
Represents a class type in Objective C.
Definition: Type.h:5184
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
CGCallee BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD, CXXDtorType Type, const CXXRecordDecl *RD)
BuildVirtualCall - This routine makes indirect vtable call for call to virtual destructors.
Definition: CGCXX.cpp:311
The generic Mips ABI is a modified version of the Itanium ABI.
Definition: TargetCXXABI.h:90
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:2714
Parameter for C++ virtual table pointers.
Definition: Decl.h:1467
void removeConst()
Definition: Type.h:273
An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
StructorType getFromDtorType(CXXDtorType T)
Definition: CodeGenTypes.h:104
CXXMethodDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition: DeclCXX.h:2046
ABIArgInfo classifyReturnType(CodeGenModule &CGM, CanQualType type)
Classify the rules for how to return a particular type.
bool isReferenceType() const
Definition: Type.h:5954
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
llvm::Function * codegenCXXStructor(const CXXMethodDecl *MD, StructorType Type)
Definition: CGCXX.cpp:213
IdentifierTable & Idents
Definition: ASTContext.h:537
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, AlignmentSource Source=AlignmentSource::Type, bool isInit=false, bool isNontemporal=false)
EmitStoreOfScalar - Store a scalar value to an address, taking care to appropriately convert from the...
ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc)
EmitLoadOfComplex - Load a complex number from the specified l-value.
Objects with "default" visibility are seen by the dynamic linker and act like normal objects...
Definition: Visibility.h:44
CharUnits getDynamicOffsetAlignment(CharUnits ActualAlign, const CXXRecordDecl *Class, CharUnits ExpectedTargetAlign)
Given a class pointer with an actual known alignment, and the expected alignment of an object at a dy...
Definition: CGClass.cpp:70
bool isReplaceableGlobalAllocationFunction(bool *IsAligned=nullptr) const
Determines whether this function is one of the replaceable global allocation functions: void *operato...
Definition: Decl.cpp:2703
static unsigned extractPBaseFlags(ASTContext &Ctx, QualType &Type)
Compute the flags for a __pbase_type_info, and remove the corresponding pieces from Type...
FunctionDecl * getOperatorDelete() const
Definition: ExprCXX.h:2123
Base object ctor.
Definition: ABI.h:27
static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base, SeenBases &Bases)
ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in abi::__vmi_class_type_info.
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:2566
uint32_t Offset
Definition: CacheTokens.cpp:43
AccessSpecifier getAccessSpecifier() const
Returns the access specifier for this base specifier.
Definition: DeclCXX.h:271
The Microsoft ABI is the ABI used by Microsoft Visual Studio (and compatible compilers).
Definition: TargetCXXABI.h:113
Deleting dtor.
Definition: ABI.h:35
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:67
Visibility getVisibility() const
Determine the visibility of this type.
Definition: Type.h:2053
static CharUnits computeOffsetHint(ASTContext &Context, const CXXRecordDecl *Src, const CXXRecordDecl *Dst)
Compute the src2dst_offset hint as described in the Itanium C++ ABI [2.9.7].
TemplateSpecializationKind getTemplateSpecializationKind() const
If this variable is an instantiation of a variable template or a static data member of a class templa...
Definition: Decl.cpp:2377
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition: Type.h:5788
CXXCtorType getCtorType() const
Definition: GlobalDecl.h:66
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:232
const Type * getClass() const
Definition: Type.h:2536
llvm::Constant * getTerminateFn()
Get the declaration of std::terminate for the platform.
Definition: CGException.cpp:51
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
static StructorCodegen getCodegenToUse(CodeGenModule &CGM, const CXXMethodDecl *MD)
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:5718
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition: Expr.h:2710
base_class_iterator bases_begin()
Definition: DeclCXX.h:780
llvm::Constant * CreateRuntimeVariable(llvm::Type *Ty, StringRef Name)
Create a new runtime global variable with the specified type and name.
const CGFunctionInfo & arrangeCXXMethodDeclaration(const CXXMethodDecl *MD)
C++ methods have some special rules and also have implicit parameters.
Definition: CGCall.cpp:263
bool hasTrivialDestructor() const
Determine whether this class has a trivial destructor (C++ [class.dtor]p3)
Definition: DeclCXX.h:1404
GlobalDecl CurGD
CurGD - The GlobalDecl for the current function being compiled.
bool isInstance() const
Definition: DeclCXX.h:1992
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition: DeclCXX.cpp:1458
llvm::AllocaInst * CreateTempAlloca(llvm::Type *Ty, const Twine &Name="tmp", llvm::Value *ArraySize=nullptr)
CreateTempAlloca - This creates an alloca and inserts it into the entry block if ArraySize is nullptr...
Definition: CGExpr.cpp:94
Represents an ObjC class declaration.
Definition: DeclObjC.h:1191
bool isAbstract() const
Determine whether this class has a pure virtual function.
Definition: DeclCXX.h:1296
CharUnits getVirtualBaseOffsetOffset(const CXXRecordDecl *RD, const CXXRecordDecl *VBase)
Return the offset in chars (relative to the vtable address point) where the offset of the virtual bas...
The iOS ABI is a partial implementation of the ARM ABI.
Definition: TargetCXXABI.h:63
virtual void mangleCXXRTTI(QualType T, raw_ostream &)=0
This object can be modified without requiring retains or releases.
Definition: Type.h:173
arg_iterator arg_end()
Definition: Expr.h:2309
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.
llvm::Value * EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, AlignmentSource Source=AlignmentSource::Type, bool isNontemporal=false)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
static ImplicitParamDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, IdentifierInfo *Id, QualType T, ImplicitParamKind ParamKind)
Create implicit parameter.
Definition: Decl.cpp:4210
bool hasAttr() const
Definition: DeclBase.h:535
CanQualType getReturnType() const
QualType getBaseType() const
Gets the base type of this object type.
Definition: Type.h:5247
static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty)
TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type info for that type is de...
static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, QualType Ty)
ShouldUseExternalRTTIDescriptor - Returns whether the type information for the given type exists some...
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition: CharUnits.h:58
AutoVarEmission EmitAutoVarAlloca(const VarDecl &var)
EmitAutoVarAlloca - Emit the alloca and debug information for a local variable.
Definition: CGDecl.cpp:961
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1590
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3268
The generic ARM ABI is a modified version of the Itanium ABI proposed by ARM for use on ARM-based pla...
Definition: TargetCXXABI.h:52
llvm::CallInst * EmitNounwindRuntimeCall(llvm::Value *callee, const Twine &name="")
bool isDynamicClass() const
Definition: DeclCXX.h:751
const TargetCodeGenInfo & getTargetCodeGenInfo()
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:57
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:179
bool isVTableExternal(const CXXRecordDecl *RD)
At this point in the translation unit, does it appear that can we rely on the vtable being defined el...
Definition: CGVTables.cpp:879
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:39
llvm::CallingConv::ID getRuntimeCC() const
Exposes information about the current target.
Definition: TargetInfo.h:54
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:2516
static TypeEvaluationKind getEvaluationKind(QualType T)
getEvaluationKind - Return the TypeEvaluationKind of QualType T.
void SetLLVMFunctionAttributes(const Decl *D, const CGFunctionInfo &Info, llvm::Function *F)
Set the LLVM function attributes (sext, zext, etc).
CXXDtorType
C++ destructor types.
Definition: ABI.h:34
ValueDecl - Represent the declaration of a variable (in which case it is an lvalue) a function (in wh...
Definition: Decl.h:627
Expr - This represents one expression.
Definition: Expr.h:106
static llvm::Constant * getBadTypeidFn(CodeGenFunction &CGF)
const FunctionProtoType * T
void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete, llvm::Value *CompletePtr, QualType ElementType)
Definition: CGExprCXX.cpp:1809
const CGFunctionInfo & arrangeNullaryFunction()
A nullary function is a freestanding function of type &#39;void ()&#39;.
Definition: CGCall.cpp:682
llvm::Constant * GetAddrOfGlobalVar(const VarDecl *D, llvm::Type *Ty=nullptr, ForDefinition_t IsForDefinition=NotForDefinition)
Return the llvm::Constant for the address of the given global variable.
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:6368
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:125
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2620
VarDecl * getExceptionDecl() const
Definition: StmtCXX.h:50
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition: Address.h:44
ObjCLifetime getObjCLifetime() const
Definition: Type.h:341
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
DeclContext * getDeclContext()
Definition: DeclBase.h:425
static bool IsIncompleteClassType(const RecordType *RecordTy)
IsIncompleteClassType - Returns whether the given record type is incomplete.
ObjCInterfaceDecl * getSuperClass() const
Definition: DeclObjC.cpp:330
TLSKind getTLSKind() const
Definition: Decl.cpp:1887
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
llvm::Value * getExceptionFromSlot()
Returns the contents of the function&#39;s exception object and selector slots.
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
static llvm::Constant * getGetExceptionPtrFn(CodeGenModule &CGM)
llvm::GlobalValue::LinkageTypes getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant)
Returns LLVM linkage for a declarator.
DefinitionKind hasDefinition(ASTContext &) const
Check whether this variable is defined in this translation unit.
Definition: Decl.cpp:2092
bool hasNonTrivialDestructor() const
Determine whether this class has a non-trivial destructor (C++ [class.dtor]p3)
Definition: DeclCXX.h:1410
StructorCodegen
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:1335
LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T)
llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD)
QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl, ObjCInterfaceDecl *PrevDecl=nullptr) const
getObjCInterfaceType - Return the unique reference to the type for the specified ObjC interface decl...
QualType getFunctionType(QualType ResultTy, ArrayRef< QualType > Args, const FunctionProtoType::ExtProtoInfo &EPI) const
Return a normal function type with a typed argument list.
Definition: ASTContext.h:1333
llvm::Value * EmitCastToVoidPtr(llvm::Value *value)
Emit a cast to void* in the appropriate address space.
Definition: CGExpr.cpp:50
virtual unsigned getSizeOfUnwindException() const
Determines the size of struct _Unwind_Exception on this platform, in 8-bit units. ...
Definition: TargetInfo.cpp:378
struct clang::ThisAdjustment::VirtualAdjustment::@118 Itanium
Implements C++ ABI-specific semantic analysis functions.
Definition: CXXABI.h:30
const TargetInfo & getTarget() const
void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr, bool PerformInit)
EmitCXXGlobalVarDeclInit - Create the initializer for a C++ variable with global storage.
Definition: CGDeclCXX.cpp:143
const LangOptions & getLangOpts() const
ASTContext & getContext() const
static AggValueSlot forAddr(Address addr, Qualifiers quals, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, IsZeroed_t isZeroed=IsNotZeroed)
forAddr - Make a slot for an aggregate value.
Definition: CGValue.h:495
bool isTemplateInstantiation(TemplateSpecializationKind Kind)
Determine whether this template specialization kind refers to an instantiation of an entity (as oppos...
Definition: Specifiers.h:167
The COMDAT used for dtors.
Definition: ABI.h:38
static StringRef getIdentifier(const Token &Tok)
VarDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition: Decl.cpp:1977
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:35
llvm::GlobalVariable * CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage)
Will return a global variable of the given type.
WatchOS is a modernisation of the iOS ABI, which roughly means it&#39;s the iOS64 ABI ported to 32-bits...
Definition: TargetCXXABI.h:76
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition: Type.h:5777
The l-value was considered opaque, so the alignment was determined from a type.
RecordDecl * getDecl() const
Definition: Type.h:3986
int64_t NonVirtual
The non-virtual adjustment from the derived object to its nearest virtual base.
Definition: ABI.h:45
static llvm::Value * performTypeAdjustment(CodeGenFunction &CGF, Address InitialPtr, int64_t NonVirtualAdjustment, int64_t VirtualAdjustment, bool IsReturnAdjustment)
There is no lifetime qualification on this type.
Definition: Type.h:169
Address CreateBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Definition: CGBuilder.h:142
#define false
Definition: stdbool.h:33
Assigning into this object requires the old value to be released and the new value to be retained...
Definition: Type.h:180
QualType getCanonicalType() const
Definition: Type.h:5757
virtual void mangleCXXRTTIName(QualType T, raw_ostream &)=0
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:2532
CastKind getCastKind() const
Definition: Expr.h:2757
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)"...
Definition: ExprCXX.h:1842
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:3672
Represents a call to a member function that may be written either with member call syntax (e...
Definition: ExprCXX.h:164
const Decl * getDecl() const
Definition: GlobalDecl.h:64
llvm::GlobalVariable * GetAddrOfVTT(const CXXRecordDecl *RD)
GetAddrOfVTT - Get the address of the VTT for the given record decl.
Definition: CGVTT.cpp:106
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1964
static llvm::Constant * getGuardAbortFn(CodeGenModule &CGM, llvm::PointerType *GuardPtrTy)
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:681
An aligned address.
Definition: Address.h:25
The generic Itanium ABI is the standard ABI of most open-source and Unix-like platforms.
Definition: TargetCXXABI.h:34
All available information about a concrete callee.
Definition: CGCall.h:66
TypeClass getTypeClass() const
Definition: Type.h:1613
int64_t VCallOffsetOffset
The offset (in bytes), relative to the address point, of the virtual call offset. ...
Definition: ABI.h:120
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:97
Complete object dtor.
Definition: ABI.h:36
ItaniumVTableContext & getItaniumVTableContext()
Assigning into this object requires a lifetime extension.
Definition: Type.h:186
static llvm::Constant * getGuardReleaseFn(CodeGenModule &CGM, llvm::PointerType *GuardPtrTy)
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
CXXCtorType
C++ constructor types.
Definition: ABI.h:25
The WebAssembly ABI is a modified version of the Itanium ABI.
Definition: TargetCXXABI.h:105
void addReplacement(StringRef Name, llvm::Constant *C)
llvm::Constant * GetAddrOfGlobal(GlobalDecl GD, ForDefinition_t IsForDefinition=NotForDefinition)
FunctionArgList - Type for representing both the decl and type of parameters to a function...
Definition: CGCall.h:276
Represents an element in a path from a derived class to a base class.
TLS with a dynamic initializer.
Definition: Decl.h:829
CGFunctionInfo - Class to encapsulate the information about a function definition.
This class organizes the cross-function state that is used while generating LLVM code.
llvm::GlobalValue * GetGlobalValue(StringRef Ref)
Dataflow Directional Tag Classes.
External linkage within a unique namespace.
Definition: Linkage.h:42
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1256
Represents a delete expression for memory deallocation and destructor calls, e.g. ...
Definition: ExprCXX.h:2071
void EmitAnyExprToExn(const Expr *E, Address Addr)
void EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee, ArrayRef< llvm::Value *> args)
Emits a call or invoke to the given noreturn runtime function.
Definition: CGCall.cpp:3641
llvm::LoadInst * CreateAlignedLoad(llvm::Value *Addr, CharUnits Align, const llvm::Twine &Name="")
Definition: CGBuilder.h:91
const Expr * getInit() const
Definition: Decl.h:1212
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
static llvm::Value * CallBeginCatch(CodeGenFunction &CGF, llvm::Value *Exn, bool EndMightThrow)
Emits a call to __cxa_begin_catch and enters a cleanup to call __cxa_end_catch.
const CXXRecordDecl * getParent() const
Returns the parent of this method declaration, which is the class in which this method is defined...
Definition: DeclCXX.h:2085
int64_t VBaseOffsetOffset
The offset (in bytes), relative to the address point of the virtual base class offset.
Definition: ABI.h:54
Address CreateConstInBoundsGEP(Address Addr, uint64_t Index, CharUnits EltSize, const llvm::Twine &Name="")
Given addr = T* ...
Definition: CGBuilder.h:211
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: Type.h:2502
void EmitAggregateCopy(Address DestPtr, Address SrcPtr, QualType EltTy, bool isVolatile=false, bool isAssignment=false)
EmitAggregateCopy - Emit an aggregate copy.
Definition: CGExprAgg.cpp:1544
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)
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
union clang::ThisAdjustment::VirtualAdjustment Virtual
void EmitAggExpr(const Expr *E, AggValueSlot AS)
EmitAggExpr - Emit the computation of the specified expression of aggregate type. ...
Definition: CGExprAgg.cpp:1522
llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD)
Return the appropriate linkage for the vtable, VTT, and type information of the given class...
Definition: CGVTables.cpp:762
void EmitAutoVarCleanups(const AutoVarEmission &emission)
Definition: CGDecl.cpp:1421
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:3976
bool empty() const
Definition: Type.h:429
StructBuilder beginStruct(llvm::StructType *structTy=nullptr)
void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
static bool isThreadWrapperReplaceable(const VarDecl *VD, CodeGen::CodeGenModule &CGM)
arg_iterator arg_begin()
Definition: Expr.h:2308
Implements C++ ABI-specific code generation functions.
Definition: CGCXXABI.h:44
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition: Address.h:52
struct clang::ReturnAdjustment::VirtualAdjustment::@116 Itanium
This class organizes the cross-module state that is used while lowering AST types to LLVM types...
Definition: CodeGenTypes.h:120
bool isStaticLocal() const
isStaticLocal - Returns true if a variable with function scope is a static local variable.
Definition: Decl.h:1049
SourceLocation getLocStart() const LLVM_READONLY
Definition: Expr.cpp:1326
StringRef getMangledName(GlobalDecl GD)
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition: Linkage.h:33
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition: CGStmt.cpp:445
void EmitARCInitWeak(Address addr, llvm::Value *value)
i8* @objc_initWeak(i8** addr, i8* value) Returns value.
Definition: CGObjC.cpp:2278
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:191
llvm::Value * LoadCXXVTT()
LoadCXXVTT - Load the VTT parameter to base constructors/destructors have virtual bases...
char __ovld __cnfn max(char x, char y)
Returns y if x < y, otherwise it returns x.
static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, CGCXXABI &CXXABI)
Definition: TargetInfo.cpp:140
bool isInlined() const
Determine whether this function should be inlined, because it is either marked "inline" or "constexpr...
Definition: Decl.h:2257
const Decl * CurFuncDecl
CurFuncDecl - Holds the Decl for the current outermost non-closure context.
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2174
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
Reading or writing from this object requires a barrier call.
Definition: Type.h:183
const VTableLayout & getVTableLayout(const CXXRecordDecl *RD)
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:5798
true
A convenience builder class for complex constant initializers, especially for anonymous global struct...
Represents a C++ struct/union/class.
Definition: DeclCXX.h:299
CGCXXABI * CreateItaniumCXXABI(CodeGenModule &CGM)
Creates an Itanium-family ABI.
void EmitBranch(llvm::BasicBlock *Block)
EmitBranch - Emit a branch to the specified basic block from the current insert block, taking care to avoid creation of branches from dummy blocks.
Definition: CGStmt.cpp:465
bool isMemberFunctionPointer() const
Returns true if the member type (i.e.
Definition: Type.h:2526
static bool ContainsIncompleteClassType(QualType Ty)
ContainsIncompleteClassType - Returns whether the given type contains an incomplete class type...
llvm::Function * CreateGlobalInitOrDestructFunction(llvm::FunctionType *ty, const Twine &name, const CGFunctionInfo &FI, SourceLocation Loc=SourceLocation(), bool TLS=false)
Definition: CGDeclCXX.cpp:302
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:5745
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
void popTerminate()
Pops a terminate handler off the stack.
Definition: CGCleanup.h:583
No linkage according to the standard, but is visible from other translation units because of types de...
Definition: Linkage.h:46
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
This class is used for builtin types like &#39;int&#39;.
Definition: Type.h:2143
bool isGlobalDelete() const
Definition: ExprCXX.h:2111
static llvm::Constant * getItaniumDynamicCastFn(CodeGenFunction &CGF)
Copying closure variant of a ctor.
Definition: ABI.h:29
CGCXXABI & getCXXABI() const
CanQualType IntTy
Definition: ASTContext.h:1004
static llvm::Constant * getEndCatchFn(CodeGenModule &CGM)
Struct with all informations about dynamic [sub]class needed to set vptr.
static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF, llvm::Constant *dtor, llvm::Constant *addr, bool TLS)
Register a global destructor using __cxa_atexit.
static RValue get(llvm::Value *V)
Definition: CGValue.h:86
Visibility getVisibility() const
Determines the visibility of this entity.
Definition: Decl.h:381
static llvm::Constant * getGuardAcquireFn(CodeGenModule &CGM, llvm::PointerType *GuardPtrTy)
BasePaths - Represents the set of paths from a derived class to one of its (direct or indirect) bases...
bool isLocalVarDecl() const
isLocalVarDecl - Returns true for local variable declarations other than parameters.
Definition: Decl.h:1095
QualType getType() const
Definition: Decl.h:638
CodeGenVTables & getVTables()
static void emitConstructorDestructorAlias(CodeGenModule &CGM, GlobalDecl AliasDecl, GlobalDecl TargetDecl)
LValue - This represents an lvalue references.
Definition: CGValue.h:167
Information for lazily generating a cleanup.
Definition: EHScopeStack.h:147
bool isTranslationUnit() const
Definition: DeclBase.h:1405
static llvm::Constant * getBadCastFn(CodeGenFunction &CGF)
bool isInline() const
Whether this variable is (C++1z) inline.
Definition: Decl.h:1348
Notes how many arguments were added to the beginning (Prefix) and ending (Suffix) of an arg list...
Definition: CGCXXABI.h:300
static bool IsStandardLibraryRTTIDescriptor(QualType Ty)
IsStandardLibraryRTTIDescriptor - Returns whether the type information for the given type exists in t...
static bool CanUseSingleInheritance(const CXXRecordDecl *RD)
Address CreatePointerBitCastOrAddrSpaceCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Definition: CGBuilder.h:164
static llvm::Constant * getBeginCatchFn(CodeGenModule &CGM)
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:182
const LangOptions & getLangOpts() const
Definition: ASTContext.h:688
void PopCleanupBlock(bool FallThroughIsBranchThrough=false)
PopCleanupBlock - Will pop the cleanup entry on the stack and process all branch fixups.
Definition: CGCleanup.cpp:640
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:2726
SourceLocation getLocation() const
Definition: DeclBase.h:416
QualType getPointeeType() const
Definition: Type.h:2522
CanQualType UnsignedIntTy
Definition: ASTContext.h:1005
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.
static llvm::Constant * getThrowFn(CodeGenModule &CGM)
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1524
QualType getType() const
Retrieves the type of the base class.
Definition: DeclCXX.h:290
const llvm::Triple & getTriple() const