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