CGObjCRuntime.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. //===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- 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 Objective-C code generation. Concrete
  10. // subclasses of this implement code generation for specific Objective-C
  11. // runtime libraries.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H
  15. #define LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H
  16. #include "CGBuilder.h"
  17. #include "CGCall.h"
  18. #include "CGCleanup.h"
  19. #include "CGValue.h"
  20. #include "clang/AST/DeclObjC.h"
  21. #include "clang/Basic/IdentifierTable.h" // Selector
  22. #include "llvm/ADT/UniqueVector.h"
  23. namespace llvm {
  24. class Constant;
  25. class Function;
  26. class Module;
  27. class StructLayout;
  28. class StructType;
  29. class Type;
  30. class Value;
  31. }
  32. namespace clang {
  33. namespace CodeGen {
  34. class CodeGenFunction;
  35. }
  36. class FieldDecl;
  37. class ObjCAtTryStmt;
  38. class ObjCAtThrowStmt;
  39. class ObjCAtSynchronizedStmt;
  40. class ObjCContainerDecl;
  41. class ObjCCategoryImplDecl;
  42. class ObjCImplementationDecl;
  43. class ObjCInterfaceDecl;
  44. class ObjCMessageExpr;
  45. class ObjCMethodDecl;
  46. class ObjCProtocolDecl;
  47. class Selector;
  48. class ObjCIvarDecl;
  49. class ObjCStringLiteral;
  50. class BlockDeclRefExpr;
  51. namespace CodeGen {
  52. class CodeGenModule;
  53. class CGBlockInfo;
  54. // FIXME: Several methods should be pure virtual but aren't to avoid the
  55. // partially-implemented subclass breaking.
  56. /// Implements runtime-specific code generation functions.
  57. class CGObjCRuntime {
  58. protected:
  59. CodeGen::CodeGenModule &CGM;
  60. CGObjCRuntime(CodeGen::CodeGenModule &CGM) : CGM(CGM) {}
  61. // Utility functions for unified ivar access. These need to
  62. // eventually be folded into other places (the structure layout
  63. // code).
  64. /// Compute an offset to the given ivar, suitable for passing to
  65. /// EmitValueForIvarAtOffset. Note that the correct handling of
  66. /// bit-fields is carefully coordinated by these two, use caution!
  67. ///
  68. /// The latter overload is suitable for computing the offset of a
  69. /// sythesized ivar.
  70. uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
  71. const ObjCInterfaceDecl *OID,
  72. const ObjCIvarDecl *Ivar);
  73. uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
  74. const ObjCImplementationDecl *OID,
  75. const ObjCIvarDecl *Ivar);
  76. LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
  77. const ObjCInterfaceDecl *OID,
  78. llvm::Value *BaseValue,
  79. const ObjCIvarDecl *Ivar,
  80. unsigned CVRQualifiers,
  81. llvm::Value *Offset);
  82. /// Emits a try / catch statement. This function is intended to be called by
  83. /// subclasses, and provides a generic mechanism for generating these, which
  84. /// should be usable by all runtimes. The caller must provide the functions
  85. /// to call when entering and exiting a \@catch() block, and the function
  86. /// used to rethrow exceptions. If the begin and end catch functions are
  87. /// NULL, then the function assumes that the EH personality function provides
  88. /// the thrown object directly.
  89. void EmitTryCatchStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S,
  90. llvm::FunctionCallee beginCatchFn,
  91. llvm::FunctionCallee endCatchFn,
  92. llvm::FunctionCallee exceptionRethrowFn);
  93. void EmitInitOfCatchParam(CodeGenFunction &CGF, llvm::Value *exn,
  94. const VarDecl *paramDecl);
  95. /// Emits an \@synchronize() statement, using the \p syncEnterFn and
  96. /// \p syncExitFn arguments as the functions called to lock and unlock
  97. /// the object. This function can be called by subclasses that use
  98. /// zero-cost exception handling.
  99. void EmitAtSynchronizedStmt(CodeGenFunction &CGF,
  100. const ObjCAtSynchronizedStmt &S,
  101. llvm::FunctionCallee syncEnterFn,
  102. llvm::FunctionCallee syncExitFn);
  103. public:
  104. virtual ~CGObjCRuntime();
  105. std::string getSymbolNameForMethod(const ObjCMethodDecl *method,
  106. bool includeCategoryName = true);
  107. /// Generate the function required to register all Objective-C components in
  108. /// this compilation unit with the runtime library.
  109. virtual llvm::Function *ModuleInitFunction() = 0;
  110. /// Get a selector for the specified name and type values.
  111. /// The result should have the LLVM type for ASTContext::getObjCSelType().
  112. virtual llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel) = 0;
  113. /// Get the address of a selector for the specified name and type values.
  114. /// This is a rarely-used language extension, but sadly it exists.
  115. ///
  116. /// The result should have the LLVM type for a pointer to
  117. /// ASTContext::getObjCSelType().
  118. virtual Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) = 0;
  119. /// Get a typed selector.
  120. virtual llvm::Value *GetSelector(CodeGenFunction &CGF,
  121. const ObjCMethodDecl *Method) = 0;
  122. /// Get the type constant to catch for the given ObjC pointer type.
  123. /// This is used externally to implement catching ObjC types in C++.
  124. /// Runtimes which don't support this should add the appropriate
  125. /// error to Sema.
  126. virtual llvm::Constant *GetEHType(QualType T) = 0;
  127. virtual CatchTypeInfo getCatchAllTypeInfo() { return { nullptr, 0 }; }
  128. /// Generate a constant string object.
  129. virtual ConstantAddress GenerateConstantString(const StringLiteral *) = 0;
  130. /// Generate a category. A category contains a list of methods (and
  131. /// accompanying metadata) and a list of protocols.
  132. virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
  133. /// Generate a class structure for this class.
  134. virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
  135. /// Register an class alias.
  136. virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) = 0;
  137. /// Generate an Objective-C message send operation.
  138. ///
  139. /// \param Method - The method being called, this may be null if synthesizing
  140. /// a property setter or getter.
  141. virtual CodeGen::RValue
  142. GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
  143. ReturnValueSlot ReturnSlot,
  144. QualType ResultType,
  145. Selector Sel,
  146. llvm::Value *Receiver,
  147. const CallArgList &CallArgs,
  148. const ObjCInterfaceDecl *Class = nullptr,
  149. const ObjCMethodDecl *Method = nullptr) = 0;
  150. /// Generate an Objective-C message send operation.
  151. ///
  152. /// This variant allows for the call to be substituted with an optimized
  153. /// variant.
  154. CodeGen::RValue
  155. GeneratePossiblySpecializedMessageSend(CodeGenFunction &CGF,
  156. ReturnValueSlot Return,
  157. QualType ResultType,
  158. Selector Sel,
  159. llvm::Value *Receiver,
  160. const CallArgList& Args,
  161. const ObjCInterfaceDecl *OID,
  162. const ObjCMethodDecl *Method,
  163. bool isClassMessage);
  164. /// Generate an Objective-C message send operation to the super
  165. /// class initiated in a method for Class and with the given Self
  166. /// object.
  167. ///
  168. /// \param Method - The method being called, this may be null if synthesizing
  169. /// a property setter or getter.
  170. virtual CodeGen::RValue
  171. GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
  172. ReturnValueSlot ReturnSlot,
  173. QualType ResultType,
  174. Selector Sel,
  175. const ObjCInterfaceDecl *Class,
  176. bool isCategoryImpl,
  177. llvm::Value *Self,
  178. bool IsClassMessage,
  179. const CallArgList &CallArgs,
  180. const ObjCMethodDecl *Method = nullptr) = 0;
  181. /// Walk the list of protocol references from a class, category or
  182. /// protocol to traverse the DAG formed from it's inheritance hierarchy. Find
  183. /// the list of protocols that ends each walk at either a runtime
  184. /// protocol or a non-runtime protocol with no parents. For the common case of
  185. /// just a list of standard runtime protocols this just returns the same list
  186. /// that was passed in.
  187. std::vector<const ObjCProtocolDecl *>
  188. GetRuntimeProtocolList(ObjCProtocolDecl::protocol_iterator begin,
  189. ObjCProtocolDecl::protocol_iterator end);
  190. /// Emit the code to return the named protocol as an object, as in a
  191. /// \@protocol expression.
  192. virtual llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
  193. const ObjCProtocolDecl *OPD) = 0;
  194. /// Generate the named protocol. Protocols contain method metadata but no
  195. /// implementations.
  196. virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
  197. /// GetOrEmitProtocol - Get the protocol object for the given
  198. /// declaration, emitting it if necessary. The return value has type
  199. /// ProtocolPtrTy.
  200. virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD) = 0;
  201. /// Generate a function preamble for a method with the specified
  202. /// types.
  203. // FIXME: Current this just generates the Function definition, but really this
  204. // should also be generating the loads of the parameters, as the runtime
  205. // should have full control over how parameters are passed.
  206. virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
  207. const ObjCContainerDecl *CD) = 0;
  208. /// Generates prologue for direct Objective-C Methods.
  209. virtual void GenerateDirectMethodPrologue(CodeGenFunction &CGF,
  210. llvm::Function *Fn,
  211. const ObjCMethodDecl *OMD,
  212. const ObjCContainerDecl *CD) = 0;
  213. /// Return the runtime function for getting properties.
  214. virtual llvm::FunctionCallee GetPropertyGetFunction() = 0;
  215. /// Return the runtime function for setting properties.
  216. virtual llvm::FunctionCallee GetPropertySetFunction() = 0;
  217. /// Return the runtime function for optimized setting properties.
  218. virtual llvm::FunctionCallee GetOptimizedPropertySetFunction(bool atomic,
  219. bool copy) = 0;
  220. // API for atomic copying of qualified aggregates in getter.
  221. virtual llvm::FunctionCallee GetGetStructFunction() = 0;
  222. // API for atomic copying of qualified aggregates in setter.
  223. virtual llvm::FunctionCallee GetSetStructFunction() = 0;
  224. /// API for atomic copying of qualified aggregates with non-trivial copy
  225. /// assignment (c++) in setter.
  226. virtual llvm::FunctionCallee GetCppAtomicObjectSetFunction() = 0;
  227. /// API for atomic copying of qualified aggregates with non-trivial copy
  228. /// assignment (c++) in getter.
  229. virtual llvm::FunctionCallee GetCppAtomicObjectGetFunction() = 0;
  230. /// GetClass - Return a reference to the class for the given
  231. /// interface decl.
  232. virtual llvm::Value *GetClass(CodeGenFunction &CGF,
  233. const ObjCInterfaceDecl *OID) = 0;
  234. virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) {
  235. llvm_unreachable("autoreleasepool unsupported in this ABI");
  236. }
  237. /// EnumerationMutationFunction - Return the function that's called by the
  238. /// compiler when a mutation is detected during foreach iteration.
  239. virtual llvm::FunctionCallee EnumerationMutationFunction() = 0;
  240. virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
  241. const ObjCAtSynchronizedStmt &S) = 0;
  242. virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
  243. const ObjCAtTryStmt &S) = 0;
  244. virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
  245. const ObjCAtThrowStmt &S,
  246. bool ClearInsertionPoint=true) = 0;
  247. virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
  248. Address AddrWeakObj) = 0;
  249. virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
  250. llvm::Value *src, Address dest) = 0;
  251. virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
  252. llvm::Value *src, Address dest,
  253. bool threadlocal=false) = 0;
  254. virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
  255. llvm::Value *src, Address dest,
  256. llvm::Value *ivarOffset) = 0;
  257. virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
  258. llvm::Value *src, Address dest) = 0;
  259. virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
  260. QualType ObjectTy,
  261. llvm::Value *BaseValue,
  262. const ObjCIvarDecl *Ivar,
  263. unsigned CVRQualifiers) = 0;
  264. virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
  265. const ObjCInterfaceDecl *Interface,
  266. const ObjCIvarDecl *Ivar) = 0;
  267. virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
  268. Address DestPtr,
  269. Address SrcPtr,
  270. llvm::Value *Size) = 0;
  271. virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
  272. const CodeGen::CGBlockInfo &blockInfo) = 0;
  273. virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,
  274. const CodeGen::CGBlockInfo &blockInfo) = 0;
  275. virtual std::string getRCBlockLayoutStr(CodeGen::CodeGenModule &CGM,
  276. const CGBlockInfo &blockInfo) {
  277. return {};
  278. }
  279. /// Returns an i8* which points to the byref layout information.
  280. virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM,
  281. QualType T) = 0;
  282. struct MessageSendInfo {
  283. const CGFunctionInfo &CallInfo;
  284. llvm::PointerType *MessengerType;
  285. MessageSendInfo(const CGFunctionInfo &callInfo,
  286. llvm::PointerType *messengerType)
  287. : CallInfo(callInfo), MessengerType(messengerType) {}
  288. };
  289. MessageSendInfo getMessageSendInfo(const ObjCMethodDecl *method,
  290. QualType resultType,
  291. CallArgList &callArgs);
  292. bool canMessageReceiverBeNull(CodeGenFunction &CGF,
  293. const ObjCMethodDecl *method,
  294. bool isSuper,
  295. const ObjCInterfaceDecl *classReceiver,
  296. llvm::Value *receiver);
  297. static bool isWeakLinkedClass(const ObjCInterfaceDecl *cls);
  298. /// Destroy the callee-destroyed arguments of the given method,
  299. /// if it has any. Used for nil-receiver paths in message sends.
  300. /// Never does anything if the method does not satisfy
  301. /// hasParamDestroyedInCallee().
  302. ///
  303. /// \param callArgs - just the formal arguments, not including implicit
  304. /// arguments such as self and cmd
  305. static void destroyCalleeDestroyedArguments(CodeGenFunction &CGF,
  306. const ObjCMethodDecl *method,
  307. const CallArgList &callArgs);
  308. // FIXME: This probably shouldn't be here, but the code to compute
  309. // it is here.
  310. unsigned ComputeBitfieldBitOffset(CodeGen::CodeGenModule &CGM,
  311. const ObjCInterfaceDecl *ID,
  312. const ObjCIvarDecl *Ivar);
  313. };
  314. /// Creates an instance of an Objective-C runtime class.
  315. //TODO: This should include some way of selecting which runtime to target.
  316. CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
  317. CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
  318. }
  319. }
  320. #endif