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