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