Clang Project

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