CGCXXABI.h 27 KB

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