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