CGCXXABI.h 29 KB

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