1 //===----- CGCXXABI.h - Interface to C++ ABIs -------------------*- C++ -*-===//
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 an abstract class for C++ code generation. Concrete subclasses
10 // of this implement code generation for specific C++ ABIs.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGCXXABI_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGCXXABI_H
16 
17 #include "CodeGenFunction.h"
18 #include "clang/Basic/LLVM.h"
19 #include "clang/CodeGen/CodeGenABITypes.h"
20 
21 namespace llvm {
22 class Constant;
23 class Type;
24 class Value;
25 class CallInst;
26 }
27 
28 namespace clang {
29 class CastExpr;
30 class CXXConstructorDecl;
31 class CXXDestructorDecl;
32 class CXXMethodDecl;
33 class CXXRecordDecl;
34 class MangleContext;
35 
36 namespace CodeGen {
37 class CGCallee;
38 class CodeGenFunction;
39 class CodeGenModule;
40 struct CatchTypeInfo;
41 
42 /// Implements C++ ABI-specific code generation functions.
43 class CGCXXABI {
44 protected:
45   CodeGenModule &CGM;
46   std::unique_ptr<MangleContext> MangleCtx;
47 
48   CGCXXABI(CodeGenModule &CGM)
49     : CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
50 
51 protected:
52   ImplicitParamDecl *getThisDecl(CodeGenFunction &CGF) {
53     return CGF.CXXABIThisDecl;
54   }
55   llvm::Value *getThisValue(CodeGenFunction &CGF) {
56     return CGF.CXXABIThisValue;
57   }
58   Address getThisAddress(CodeGenFunction &CGF) {
59     return Address(CGF.CXXABIThisValue, CGF.CXXABIThisAlignment);
60   }
61 
62   /// Issue a diagnostic about unsupported features in the ABI.
63   void ErrorUnsupportedABI(CodeGenFunction &CGF, StringRef S);
64 
65   /// Get a null value for unsupported member pointers.
66   llvm::Constant *GetBogusMemberPointer(QualType T);
67 
68   ImplicitParamDecl *&getStructorImplicitParamDecl(CodeGenFunction &CGF) {
69     return CGF.CXXStructorImplicitParamDecl;
70   }
71   llvm::Value *&getStructorImplicitParamValue(CodeGenFunction &CGF) {
72     return CGF.CXXStructorImplicitParamValue;
73   }
74 
75   /// Loads the incoming C++ this pointer as it was passed by the caller.
76   llvm::Value *loadIncomingCXXThis(CodeGenFunction &CGF);
77 
78   void setCXXABIThisValue(CodeGenFunction &CGF, llvm::Value *ThisPtr);
79 
80   ASTContext &getContext() const { return CGM.getContext(); }
81 
82   bool mayNeedDestruction(const VarDecl *VD) const;
83 
84   /// Determine whether we will definitely emit this variable with a constant
85   /// initializer, either because the language semantics demand it or because
86   /// we know that the initializer is a constant.
87   // For weak definitions, any initializer available in the current translation
88   // is not necessarily reflective of the initializer used; such initializers
89   // are ignored unless if InspectInitForWeakDef is true.
90   bool
91   isEmittedWithConstantInitializer(const VarDecl *VD,
92                                    bool InspectInitForWeakDef = false) const;
93 
94   virtual bool requiresArrayCookie(const CXXDeleteExpr *E, QualType eltType);
95   virtual bool requiresArrayCookie(const CXXNewExpr *E);
96 
97   /// Determine whether there's something special about the rules of
98   /// the ABI tell us that 'this' is a complete object within the
99   /// given function.  Obvious common logic like being defined on a
100   /// final class will have been taken care of by the caller.
101   virtual bool isThisCompleteObject(GlobalDecl GD) const = 0;
102 
103 public:
104 
105   virtual ~CGCXXABI();
106 
107   /// Gets the mangle context.
108   MangleContext &getMangleContext() {
109     return *MangleCtx;
110   }
111 
112   /// Returns true if the given constructor or destructor is one of the
113   /// kinds that the ABI says returns 'this' (only applies when called
114   /// non-virtually for destructors).
115   ///
116   /// There currently is no way to indicate if a destructor returns 'this'
117   /// when called virtually, and code generation does not support the case.
118   virtual bool HasThisReturn(GlobalDecl GD) const { return false; }
119 
120   virtual bool hasMostDerivedReturn(GlobalDecl GD) const { return false; }
121 
122   virtual bool useSinitAndSterm() const { return false; }
123 
124   /// Returns true if the target allows calling a function through a pointer
125   /// with a different signature than the actual function (or equivalently,
126   /// bitcasting a function or function pointer to a different function type).
127   /// In principle in the most general case this could depend on the target, the
128   /// calling convention, and the actual types of the arguments and return
129   /// value. Here it just means whether the signature mismatch could *ever* be
130   /// allowed; in other words, does the target do strict checking of signatures
131   /// for all calls.
132   virtual bool canCallMismatchedFunctionType() const { return true; }
133 
134   /// If the C++ ABI requires the given type be returned in a particular way,
135   /// this method sets RetAI and returns true.
136   virtual bool classifyReturnType(CGFunctionInfo &FI) const = 0;
137 
138   /// Specify how one should pass an argument of a record type.
139   enum RecordArgABI {
140     /// Pass it using the normal C aggregate rules for the ABI, potentially
141     /// introducing extra copies and passing some or all of it in registers.
142     RAA_Default = 0,
143 
144     /// Pass it on the stack using its defined layout.  The argument must be
145     /// evaluated directly into the correct stack position in the arguments area,
146     /// and the call machinery must not move it or introduce extra copies.
147     RAA_DirectInMemory,
148 
149     /// Pass it as a pointer to temporary memory.
150     RAA_Indirect
151   };
152 
153   /// Returns how an argument of the given record type should be passed.
154   virtual RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const = 0;
155 
156   /// Returns true if the implicit 'sret' parameter comes after the implicit
157   /// 'this' parameter of C++ instance methods.
158   virtual bool isSRetParameterAfterThis() const { return false; }
159 
160   /// Returns true if the ABI permits the argument to be a homogeneous
161   /// aggregate.
162   virtual bool
163   isPermittedToBeHomogeneousAggregate(const CXXRecordDecl *RD) const {
164     return true;
165   };
166 
167   /// Find the LLVM type used to represent the given member pointer
168   /// type.
169   virtual llvm::Type *
170   ConvertMemberPointerType(const MemberPointerType *MPT);
171 
172   /// Load a member function from an object and a member function
173   /// pointer.  Apply the this-adjustment and set 'This' to the
174   /// adjusted value.
175   virtual CGCallee EmitLoadOfMemberFunctionPointer(
176       CodeGenFunction &CGF, const Expr *E, Address This,
177       llvm::Value *&ThisPtrForCall, llvm::Value *MemPtr,
178       const MemberPointerType *MPT);
179 
180   /// Calculate an l-value from an object and a data member pointer.
181   virtual llvm::Value *
182   EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E,
183                                Address Base, llvm::Value *MemPtr,
184                                const MemberPointerType *MPT);
185 
186   /// Perform a derived-to-base, base-to-derived, or bitcast member
187   /// pointer conversion.
188   virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
189                                                    const CastExpr *E,
190                                                    llvm::Value *Src);
191 
192   /// Perform a derived-to-base, base-to-derived, or bitcast member
193   /// pointer conversion on a constant value.
194   virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
195                                                       llvm::Constant *Src);
196 
197   /// Return true if the given member pointer can be zero-initialized
198   /// (in the C++ sense) with an LLVM zeroinitializer.
199   virtual bool isZeroInitializable(const MemberPointerType *MPT);
200 
201   /// Return whether or not a member pointers type is convertible to an IR type.
202   virtual bool isMemberPointerConvertible(const MemberPointerType *MPT) const {
203     return true;
204   }
205 
206   /// Create a null member pointer of the given type.
207   virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
208 
209   /// Create a member pointer for the given method.
210   virtual llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD);
211 
212   /// Create a member pointer for the given field.
213   virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
214                                                 CharUnits offset);
215 
216   /// Create a member pointer for the given member pointer constant.
217   virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
218 
219   /// Emit a comparison between two member pointers.  Returns an i1.
220   virtual llvm::Value *
221   EmitMemberPointerComparison(CodeGenFunction &CGF,
222                               llvm::Value *L,
223                               llvm::Value *R,
224                               const MemberPointerType *MPT,
225                               bool Inequality);
226 
227   /// Determine if a member pointer is non-null.  Returns an i1.
228   virtual llvm::Value *
229   EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
230                              llvm::Value *MemPtr,
231                              const MemberPointerType *MPT);
232 
233 protected:
234   /// A utility method for computing the offset required for the given
235   /// base-to-derived or derived-to-base member-pointer conversion.
236   /// Does not handle virtual conversions (in case we ever fully
237   /// support an ABI that allows this).  Returns null if no adjustment
238   /// is required.
239   llvm::Constant *getMemberPointerAdjustment(const CastExpr *E);
240 
241 public:
242   virtual void emitVirtualObjectDelete(CodeGenFunction &CGF,
243                                        const CXXDeleteExpr *DE,
244                                        Address Ptr, QualType ElementType,
245                                        const CXXDestructorDecl *Dtor) = 0;
246   virtual void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) = 0;
247   virtual void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) = 0;
248   virtual llvm::GlobalVariable *getThrowInfo(QualType T) { return nullptr; }
249 
250   /// Determine whether it's possible to emit a vtable for \p RD, even
251   /// though we do not know that the vtable has been marked as used by semantic
252   /// analysis.
253   virtual bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const = 0;
254 
255   virtual void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) = 0;
256 
257   virtual llvm::CallInst *
258   emitTerminateForUnexpectedException(CodeGenFunction &CGF,
259                                       llvm::Value *Exn);
260 
261   virtual llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) = 0;
262   virtual CatchTypeInfo
263   getAddrOfCXXCatchHandlerType(QualType Ty, QualType CatchHandlerType) = 0;
264   virtual CatchTypeInfo getCatchAllTypeInfo();
265 
266   virtual bool shouldTypeidBeNullChecked(bool IsDeref,
267                                          QualType SrcRecordTy) = 0;
268   virtual void EmitBadTypeidCall(CodeGenFunction &CGF) = 0;
269   virtual llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy,
270                                   Address ThisPtr,
271                                   llvm::Type *StdTypeInfoPtrTy) = 0;
272 
273   virtual bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
274                                                   QualType SrcRecordTy) = 0;
275 
276   virtual llvm::Value *
277   EmitDynamicCastCall(CodeGenFunction &CGF, Address Value,
278                       QualType SrcRecordTy, QualType DestTy,
279                       QualType DestRecordTy, llvm::BasicBlock *CastEnd) = 0;
280 
281   virtual llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF,
282                                              Address Value,
283                                              QualType SrcRecordTy,
284                                              QualType DestTy) = 0;
285 
286   virtual bool EmitBadCastCall(CodeGenFunction &CGF) = 0;
287 
288   virtual llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF,
289                                                  Address This,
290                                                  const CXXRecordDecl *ClassDecl,
291                                         const CXXRecordDecl *BaseClassDecl) = 0;
292 
293   virtual llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF,
294                                                           const CXXRecordDecl *RD);
295 
296   /// Emit the code to initialize hidden members required
297   /// to handle virtual inheritance, if needed by the ABI.
298   virtual void
299   initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF,
300                                             const CXXRecordDecl *RD) {}
301 
302   /// Emit constructor variants required by this ABI.
303   virtual void EmitCXXConstructors(const CXXConstructorDecl *D) = 0;
304 
305   /// Additional implicit arguments to add to the beginning (Prefix) and end
306   /// (Suffix) of a constructor / destructor arg list.
307   ///
308   /// Note that Prefix should actually be inserted *after* the first existing
309   /// arg; `this` arguments always come first.
310   struct AddedStructorArgs {
311     struct Arg {
312       llvm::Value *Value;
313       QualType Type;
314     };
315     SmallVector<Arg, 1> Prefix;
316     SmallVector<Arg, 1> Suffix;
317     AddedStructorArgs() = default;
318     AddedStructorArgs(SmallVector<Arg, 1> P, SmallVector<Arg, 1> S)
319         : Prefix(std::move(P)), Suffix(std::move(S)) {}
320     static AddedStructorArgs prefix(SmallVector<Arg, 1> Args) {
321       return {std::move(Args), {}};
322     }
323     static AddedStructorArgs suffix(SmallVector<Arg, 1> Args) {
324       return {{}, std::move(Args)};
325     }
326   };
327 
328   /// Similar to AddedStructorArgs, but only notes the number of additional
329   /// arguments.
330   struct AddedStructorArgCounts {
331     unsigned Prefix = 0;
332     unsigned Suffix = 0;
333     AddedStructorArgCounts() = default;
334     AddedStructorArgCounts(unsigned P, unsigned S) : Prefix(P), Suffix(S) {}
335     static AddedStructorArgCounts prefix(unsigned N) { return {N, 0}; }
336     static AddedStructorArgCounts suffix(unsigned N) { return {0, N}; }
337   };
338 
339   /// Build the signature of the given constructor or destructor variant by
340   /// adding any required parameters.  For convenience, ArgTys has been
341   /// initialized with the type of 'this'.
342   virtual AddedStructorArgCounts
343   buildStructorSignature(GlobalDecl GD,
344                          SmallVectorImpl<CanQualType> &ArgTys) = 0;
345 
346   /// Returns true if the given destructor type should be emitted as a linkonce
347   /// delegating thunk, regardless of whether the dtor is defined in this TU or
348   /// not.
349   virtual bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
350                                       CXXDtorType DT) const = 0;
351 
352   virtual void setCXXDestructorDLLStorage(llvm::GlobalValue *GV,
353                                           const CXXDestructorDecl *Dtor,
354                                           CXXDtorType DT) const;
355 
356   virtual llvm::GlobalValue::LinkageTypes
357   getCXXDestructorLinkage(GVALinkage Linkage, const CXXDestructorDecl *Dtor,
358                           CXXDtorType DT) const;
359 
360   /// Emit destructor variants required by this ABI.
361   virtual void EmitCXXDestructors(const CXXDestructorDecl *D) = 0;
362 
363   /// Get the type of the implicit "this" parameter used by a method. May return
364   /// zero if no specific type is applicable, e.g. if the ABI expects the "this"
365   /// parameter to point to some artificial offset in a complete object due to
366   /// vbases being reordered.
367   virtual const CXXRecordDecl *
368   getThisArgumentTypeForMethod(const CXXMethodDecl *MD) {
369     return MD->getParent();
370   }
371 
372   /// Perform ABI-specific "this" argument adjustment required prior to
373   /// a call of a virtual function.
374   /// The "VirtualCall" argument is true iff the call itself is virtual.
375   virtual Address
376   adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD,
377                                            Address This, bool VirtualCall) {
378     return This;
379   }
380 
381   /// Build a parameter variable suitable for 'this'.
382   void buildThisParam(CodeGenFunction &CGF, FunctionArgList &Params);
383 
384   /// Insert any ABI-specific implicit parameters into the parameter list for a
385   /// function.  This generally involves extra data for constructors and
386   /// destructors.
387   ///
388   /// ABIs may also choose to override the return type, which has been
389   /// initialized with the type of 'this' if HasThisReturn(CGF.CurGD) is true or
390   /// the formal return type of the function otherwise.
391   virtual void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy,
392                                          FunctionArgList &Params) = 0;
393 
394   /// Get the ABI-specific "this" parameter adjustment to apply in the prologue
395   /// of a virtual function.
396   virtual CharUnits getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) {
397     return CharUnits::Zero();
398   }
399 
400   /// Emit the ABI-specific prolog for the function.
401   virtual void EmitInstanceFunctionProlog(CodeGenFunction &CGF) = 0;
402 
403   virtual AddedStructorArgs
404   getImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D,
405                              CXXCtorType Type, bool ForVirtualBase,
406                              bool Delegating) = 0;
407 
408   /// Add any ABI-specific implicit arguments needed to call a constructor.
409   ///
410   /// \return The number of arguments added at the beginning and end of the
411   /// call, which is typically zero or one.
412   AddedStructorArgCounts
413   addImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D,
414                              CXXCtorType Type, bool ForVirtualBase,
415                              bool Delegating, CallArgList &Args);
416 
417   /// Get the implicit (second) parameter that comes after the "this" pointer,
418   /// or nullptr if there is isn't one.
419   virtual llvm::Value *
420   getCXXDestructorImplicitParam(CodeGenFunction &CGF,
421                                 const CXXDestructorDecl *DD, CXXDtorType Type,
422                                 bool ForVirtualBase, bool Delegating) = 0;
423 
424   /// Emit the destructor call.
425   virtual void EmitDestructorCall(CodeGenFunction &CGF,
426                                   const CXXDestructorDecl *DD, CXXDtorType Type,
427                                   bool ForVirtualBase, bool Delegating,
428                                   Address This, QualType ThisTy) = 0;
429 
430   /// Emits the VTable definitions required for the given record type.
431   virtual void emitVTableDefinitions(CodeGenVTables &CGVT,
432                                      const CXXRecordDecl *RD) = 0;
433 
434   /// Checks if ABI requires extra virtual offset for vtable field.
435   virtual bool
436   isVirtualOffsetNeededForVTableField(CodeGenFunction &CGF,
437                                       CodeGenFunction::VPtr Vptr) = 0;
438 
439   /// Checks if ABI requires to initialize vptrs for given dynamic class.
440   virtual bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass) = 0;
441 
442   /// Get the address point of the vtable for the given base subobject.
443   virtual llvm::Constant *
444   getVTableAddressPoint(BaseSubobject Base,
445                         const CXXRecordDecl *VTableClass) = 0;
446 
447   /// Get the address point of the vtable for the given base subobject while
448   /// building a constructor or a destructor.
449   virtual llvm::Value *
450   getVTableAddressPointInStructor(CodeGenFunction &CGF, const CXXRecordDecl *RD,
451                                   BaseSubobject Base,
452                                   const CXXRecordDecl *NearestVBase) = 0;
453 
454   /// Get the address point of the vtable for the given base subobject while
455   /// building a constexpr.
456   virtual llvm::Constant *
457   getVTableAddressPointForConstExpr(BaseSubobject Base,
458                                     const CXXRecordDecl *VTableClass) = 0;
459 
460   /// Get the address of the vtable for the given record decl which should be
461   /// used for the vptr at the given offset in RD.
462   virtual llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD,
463                                                 CharUnits VPtrOffset) = 0;
464 
465   /// Build a virtual function pointer in the ABI-specific way.
466   virtual CGCallee getVirtualFunctionPointer(CodeGenFunction &CGF,
467                                              GlobalDecl GD, Address This,
468                                              llvm::Type *Ty,
469                                              SourceLocation Loc) = 0;
470 
471   using DeleteOrMemberCallExpr =
472       llvm::PointerUnion<const CXXDeleteExpr *, const CXXMemberCallExpr *>;
473 
474   /// Emit the ABI-specific virtual destructor call.
475   virtual llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF,
476                                                  const CXXDestructorDecl *Dtor,
477                                                  CXXDtorType DtorType,
478                                                  Address This,
479                                                  DeleteOrMemberCallExpr E) = 0;
480 
481   virtual void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF,
482                                                 GlobalDecl GD,
483                                                 CallArgList &CallArgs) {}
484 
485   /// Emit any tables needed to implement virtual inheritance.  For Itanium,
486   /// this emits virtual table tables.  For the MSVC++ ABI, this emits virtual
487   /// base tables.
488   virtual void emitVirtualInheritanceTables(const CXXRecordDecl *RD) = 0;
489 
490   virtual bool exportThunk() = 0;
491   virtual void setThunkLinkage(llvm::Function *Thunk, bool ForVTable,
492                                GlobalDecl GD, bool ReturnAdjustment) = 0;
493 
494   virtual llvm::Value *performThisAdjustment(CodeGenFunction &CGF,
495                                              Address This,
496                                              const ThisAdjustment &TA) = 0;
497 
498   virtual llvm::Value *performReturnAdjustment(CodeGenFunction &CGF,
499                                                Address Ret,
500                                                const ReturnAdjustment &RA) = 0;
501 
502   virtual void EmitReturnFromThunk(CodeGenFunction &CGF,
503                                    RValue RV, QualType ResultType);
504 
505   virtual size_t getSrcArgforCopyCtor(const CXXConstructorDecl *,
506                                       FunctionArgList &Args) const = 0;
507 
508   /// Gets the offsets of all the virtual base pointers in a given class.
509   virtual std::vector<CharUnits> getVBPtrOffsets(const CXXRecordDecl *RD);
510 
511   /// Gets the pure virtual member call function.
512   virtual StringRef GetPureVirtualCallName() = 0;
513 
514   /// Gets the deleted virtual member call name.
515   virtual StringRef GetDeletedVirtualCallName() = 0;
516 
517   /**************************** Array cookies ******************************/
518 
519   /// Returns the extra size required in order to store the array
520   /// cookie for the given new-expression.  May return 0 to indicate that no
521   /// array cookie is required.
522   ///
523   /// Several cases are filtered out before this method is called:
524   ///   - non-array allocations never need a cookie
525   ///   - calls to \::operator new(size_t, void*) never need a cookie
526   ///
527   /// \param expr - the new-expression being allocated.
528   virtual CharUnits GetArrayCookieSize(const CXXNewExpr *expr);
529 
530   /// Initialize the array cookie for the given allocation.
531   ///
532   /// \param NewPtr - a char* which is the presumed-non-null
533   ///   return value of the allocation function
534   /// \param NumElements - the computed number of elements,
535   ///   potentially collapsed from the multidimensional array case;
536   ///   always a size_t
537   /// \param ElementType - the base element allocated type,
538   ///   i.e. the allocated type after stripping all array types
539   virtual Address InitializeArrayCookie(CodeGenFunction &CGF,
540                                         Address NewPtr,
541                                         llvm::Value *NumElements,
542                                         const CXXNewExpr *expr,
543                                         QualType ElementType);
544 
545   /// Reads the array cookie associated with the given pointer,
546   /// if it has one.
547   ///
548   /// \param Ptr - a pointer to the first element in the array
549   /// \param ElementType - the base element type of elements of the array
550   /// \param NumElements - an out parameter which will be initialized
551   ///   with the number of elements allocated, or zero if there is no
552   ///   cookie
553   /// \param AllocPtr - an out parameter which will be initialized
554   ///   with a char* pointing to the address returned by the allocation
555   ///   function
556   /// \param CookieSize - an out parameter which will be initialized
557   ///   with the size of the cookie, or zero if there is no cookie
558   virtual void ReadArrayCookie(CodeGenFunction &CGF, Address Ptr,
559                                const CXXDeleteExpr *expr,
560                                QualType ElementType, llvm::Value *&NumElements,
561                                llvm::Value *&AllocPtr, CharUnits &CookieSize);
562 
563   /// Return whether the given global decl needs a VTT parameter.
564   virtual bool NeedsVTTParameter(GlobalDecl GD);
565 
566 protected:
567   /// Returns the extra size required in order to store the array
568   /// cookie for the given type.  Assumes that an array cookie is
569   /// required.
570   virtual CharUnits getArrayCookieSizeImpl(QualType elementType);
571 
572   /// Reads the array cookie for an allocation which is known to have one.
573   /// This is called by the standard implementation of ReadArrayCookie.
574   ///
575   /// \param ptr - a pointer to the allocation made for an array, as a char*
576   /// \param cookieSize - the computed cookie size of an array
577   ///
578   /// Other parameters are as above.
579   ///
580   /// \return a size_t
581   virtual llvm::Value *readArrayCookieImpl(CodeGenFunction &IGF, Address ptr,
582                                            CharUnits cookieSize);
583 
584 public:
585 
586   /*************************** Static local guards ****************************/
587 
588   /// Emits the guarded initializer and destructor setup for the given
589   /// variable, given that it couldn't be emitted as a constant.
590   /// If \p PerformInit is false, the initialization has been folded to a
591   /// constant and should not be performed.
592   ///
593   /// The variable may be:
594   ///   - a static local variable
595   ///   - a static data member of a class template instantiation
596   virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
597                                llvm::GlobalVariable *DeclPtr,
598                                bool PerformInit) = 0;
599 
600   /// Emit code to force the execution of a destructor during global
601   /// teardown.  The default implementation of this uses atexit.
602   ///
603   /// \param Dtor - a function taking a single pointer argument
604   /// \param Addr - a pointer to pass to the destructor function.
605   virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
606                                   llvm::FunctionCallee Dtor,
607                                   llvm::Constant *Addr) = 0;
608 
609   /*************************** thread_local initialization ********************/
610 
611   /// Emits ABI-required functions necessary to initialize thread_local
612   /// variables in this translation unit.
613   ///
614   /// \param CXXThreadLocals - The thread_local declarations in this translation
615   ///        unit.
616   /// \param CXXThreadLocalInits - If this translation unit contains any
617   ///        non-constant initialization or non-trivial destruction for
618   ///        thread_local variables, a list of functions to perform the
619   ///        initialization.
620   virtual void EmitThreadLocalInitFuncs(
621       CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals,
622       ArrayRef<llvm::Function *> CXXThreadLocalInits,
623       ArrayRef<const VarDecl *> CXXThreadLocalInitVars) = 0;
624 
625   // Determine if references to thread_local global variables can be made
626   // directly or require access through a thread wrapper function.
627   virtual bool usesThreadWrapperFunction(const VarDecl *VD) const = 0;
628 
629   /// Emit a reference to a non-local thread_local variable (including
630   /// triggering the initialization of all thread_local variables in its
631   /// translation unit).
632   virtual LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF,
633                                               const VarDecl *VD,
634                                               QualType LValType) = 0;
635 
636   /// Emit a single constructor/destructor with the given type from a C++
637   /// constructor Decl.
638   virtual void emitCXXStructor(GlobalDecl GD) = 0;
639 
640   /// Load a vtable from This, an object of polymorphic type RD, or from one of
641   /// its virtual bases if it does not have its own vtable. Returns the vtable
642   /// and the class from which the vtable was loaded.
643   virtual std::pair<llvm::Value *, const CXXRecordDecl *>
644   LoadVTablePtr(CodeGenFunction &CGF, Address This,
645                 const CXXRecordDecl *RD) = 0;
646 };
647 
648 // Create an instance of a C++ ABI class:
649 
650 /// Creates an Itanium-family ABI.
651 CGCXXABI *CreateItaniumCXXABI(CodeGenModule &CGM);
652 
653 /// Creates a Microsoft-family ABI.
654 CGCXXABI *CreateMicrosoftCXXABI(CodeGenModule &CGM);
655 
656 struct CatchRetScope final : EHScopeStack::Cleanup {
657   llvm::CatchPadInst *CPI;
658 
659   CatchRetScope(llvm::CatchPadInst *CPI) : CPI(CPI) {}
660 
661   void Emit(CodeGenFunction &CGF, Flags flags) override {
662     llvm::BasicBlock *BB = CGF.createBasicBlock("catchret.dest");
663     CGF.Builder.CreateCatchRet(CPI, BB);
664     CGF.EmitBlock(BB);
665   }
666 };
667 }
668 }
669 
670 #endif
671