CGCXXABI.h 28 KB

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