CGObjCRuntime.h 16 KB

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