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