CGObjCRuntime.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //===----- CGObjCRuntime.h - Interface to ObjC Runtimes ---------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This provides an abstract class for Objective-C code generation. Concrete
  11. // subclasses of this implement code generation for specific Objective-C
  12. // runtime libraries.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H
  16. #define LLVM_CLANG_LIB_CODEGEN_CGOBJCRUNTIME_H
  17. #include "CGBuilder.h"
  18. #include "CGCall.h"
  19. #include "CGValue.h"
  20. #include "clang/AST/DeclObjC.h"
  21. #include "clang/Basic/IdentifierTable.h" // Selector
  22. namespace llvm {
  23. class Constant;
  24. class Function;
  25. class Module;
  26. class StructLayout;
  27. class StructType;
  28. class Type;
  29. class Value;
  30. }
  31. namespace clang {
  32. namespace CodeGen {
  33. class CodeGenFunction;
  34. }
  35. class FieldDecl;
  36. class ObjCAtTryStmt;
  37. class ObjCAtThrowStmt;
  38. class ObjCAtSynchronizedStmt;
  39. class ObjCContainerDecl;
  40. class ObjCCategoryImplDecl;
  41. class ObjCImplementationDecl;
  42. class ObjCInterfaceDecl;
  43. class ObjCMessageExpr;
  44. class ObjCMethodDecl;
  45. class ObjCProtocolDecl;
  46. class Selector;
  47. class ObjCIvarDecl;
  48. class ObjCStringLiteral;
  49. class BlockDeclRefExpr;
  50. namespace CodeGen {
  51. class CodeGenModule;
  52. class CGBlockInfo;
  53. // FIXME: Several methods should be pure virtual but aren't to avoid the
  54. // partially-implemented subclass breaking.
  55. /// Implements runtime-specific code generation functions.
  56. class CGObjCRuntime {
  57. protected:
  58. CodeGen::CodeGenModule &CGM;
  59. CGObjCRuntime(CodeGen::CodeGenModule &CGM) : CGM(CGM) {}
  60. // Utility functions for unified ivar access. These need to
  61. // eventually be folded into other places (the structure layout
  62. // code).
  63. /// Compute an offset to the given ivar, suitable for passing to
  64. /// EmitValueForIvarAtOffset. Note that the correct handling of
  65. /// bit-fields is carefully coordinated by these two, use caution!
  66. ///
  67. /// The latter overload is suitable for computing the offset of a
  68. /// sythesized ivar.
  69. uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
  70. const ObjCInterfaceDecl *OID,
  71. const ObjCIvarDecl *Ivar);
  72. uint64_t ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
  73. const ObjCImplementationDecl *OID,
  74. const ObjCIvarDecl *Ivar);
  75. LValue EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
  76. const ObjCInterfaceDecl *OID,
  77. llvm::Value *BaseValue,
  78. const ObjCIvarDecl *Ivar,
  79. unsigned CVRQualifiers,
  80. llvm::Value *Offset);
  81. /// Emits a try / catch statement. This function is intended to be called by
  82. /// subclasses, and provides a generic mechanism for generating these, which
  83. /// should be usable by all runtimes. The caller must provide the functions
  84. /// to call when entering and exiting a \@catch() block, and the function
  85. /// used to rethrow exceptions. If the begin and end catch functions are
  86. /// NULL, then the function assumes that the EH personality function provides
  87. /// the thrown object directly.
  88. void EmitTryCatchStmt(CodeGenFunction &CGF,
  89. const ObjCAtTryStmt &S,
  90. llvm::Constant *beginCatchFn,
  91. llvm::Constant *endCatchFn,
  92. llvm::Constant *exceptionRethrowFn);
  93. /// Emits an \@synchronize() statement, using the \p syncEnterFn and
  94. /// \p syncExitFn arguments as the functions called to lock and unlock
  95. /// the object. This function can be called by subclasses that use
  96. /// zero-cost exception handling.
  97. void EmitAtSynchronizedStmt(CodeGenFunction &CGF,
  98. const ObjCAtSynchronizedStmt &S,
  99. llvm::Function *syncEnterFn,
  100. llvm::Function *syncExitFn);
  101. public:
  102. virtual ~CGObjCRuntime();
  103. /// Generate the function required to register all Objective-C components in
  104. /// this compilation unit with the runtime library.
  105. virtual llvm::Function *ModuleInitFunction() = 0;
  106. /// Get a selector for the specified name and type values. The
  107. /// return value should have the LLVM type for pointer-to
  108. /// ASTContext::getObjCSelType().
  109. virtual llvm::Value *GetSelector(CodeGenFunction &CGF,
  110. Selector Sel, bool lval=false) = 0;
  111. /// Get a typed selector.
  112. virtual llvm::Value *GetSelector(CodeGenFunction &CGF,
  113. const ObjCMethodDecl *Method) = 0;
  114. /// Get the type constant to catch for the given ObjC pointer type.
  115. /// This is used externally to implement catching ObjC types in C++.
  116. /// Runtimes which don't support this should add the appropriate
  117. /// error to Sema.
  118. virtual llvm::Constant *GetEHType(QualType T) = 0;
  119. /// Generate a constant string object.
  120. virtual llvm::Constant *GenerateConstantString(const StringLiteral *) = 0;
  121. /// Generate a category. A category contains a list of methods (and
  122. /// accompanying metadata) and a list of protocols.
  123. virtual void GenerateCategory(const ObjCCategoryImplDecl *OCD) = 0;
  124. /// Generate a class structure for this class.
  125. virtual void GenerateClass(const ObjCImplementationDecl *OID) = 0;
  126. /// Register an class alias.
  127. virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) = 0;
  128. /// Generate an Objective-C message send operation.
  129. ///
  130. /// \param Method - The method being called, this may be null if synthesizing
  131. /// a property setter or getter.
  132. virtual CodeGen::RValue
  133. GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
  134. ReturnValueSlot ReturnSlot,
  135. QualType ResultType,
  136. Selector Sel,
  137. llvm::Value *Receiver,
  138. const CallArgList &CallArgs,
  139. const ObjCInterfaceDecl *Class = nullptr,
  140. const ObjCMethodDecl *Method = nullptr) = 0;
  141. /// Generate an Objective-C message send operation to the super
  142. /// class initiated in a method for Class and with the given Self
  143. /// object.
  144. ///
  145. /// \param Method - The method being called, this may be null if synthesizing
  146. /// a property setter or getter.
  147. virtual CodeGen::RValue
  148. GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
  149. ReturnValueSlot ReturnSlot,
  150. QualType ResultType,
  151. Selector Sel,
  152. const ObjCInterfaceDecl *Class,
  153. bool isCategoryImpl,
  154. llvm::Value *Self,
  155. bool IsClassMessage,
  156. const CallArgList &CallArgs,
  157. const ObjCMethodDecl *Method = nullptr) = 0;
  158. /// Emit the code to return the named protocol as an object, as in a
  159. /// \@protocol expression.
  160. virtual llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
  161. const ObjCProtocolDecl *OPD) = 0;
  162. /// Generate the named protocol. Protocols contain method metadata but no
  163. /// implementations.
  164. virtual void GenerateProtocol(const ObjCProtocolDecl *OPD) = 0;
  165. /// Generate a function preamble for a method with the specified
  166. /// types.
  167. // FIXME: Current this just generates the Function definition, but really this
  168. // should also be generating the loads of the parameters, as the runtime
  169. // should have full control over how parameters are passed.
  170. virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
  171. const ObjCContainerDecl *CD) = 0;
  172. /// Return the runtime function for getting properties.
  173. virtual llvm::Constant *GetPropertyGetFunction() = 0;
  174. /// Return the runtime function for setting properties.
  175. virtual llvm::Constant *GetPropertySetFunction() = 0;
  176. /// Return the runtime function for optimized setting properties.
  177. virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
  178. bool copy) = 0;
  179. // API for atomic copying of qualified aggregates in getter.
  180. virtual llvm::Constant *GetGetStructFunction() = 0;
  181. // API for atomic copying of qualified aggregates in setter.
  182. virtual llvm::Constant *GetSetStructFunction() = 0;
  183. /// API for atomic copying of qualified aggregates with non-trivial copy
  184. /// assignment (c++) in setter.
  185. virtual llvm::Constant *GetCppAtomicObjectSetFunction() = 0;
  186. /// API for atomic copying of qualified aggregates with non-trivial copy
  187. /// assignment (c++) in getter.
  188. virtual llvm::Constant *GetCppAtomicObjectGetFunction() = 0;
  189. /// GetClass - Return a reference to the class for the given
  190. /// interface decl.
  191. virtual llvm::Value *GetClass(CodeGenFunction &CGF,
  192. const ObjCInterfaceDecl *OID) = 0;
  193. virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) {
  194. llvm_unreachable("autoreleasepool unsupported in this ABI");
  195. }
  196. /// EnumerationMutationFunction - Return the function that's called by the
  197. /// compiler when a mutation is detected during foreach iteration.
  198. virtual llvm::Constant *EnumerationMutationFunction() = 0;
  199. virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
  200. const ObjCAtSynchronizedStmt &S) = 0;
  201. virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
  202. const ObjCAtTryStmt &S) = 0;
  203. virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
  204. const ObjCAtThrowStmt &S,
  205. bool ClearInsertionPoint=true) = 0;
  206. virtual llvm::Value *EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
  207. llvm::Value *AddrWeakObj) = 0;
  208. virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
  209. llvm::Value *src, llvm::Value *dest) = 0;
  210. virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
  211. llvm::Value *src, llvm::Value *dest,
  212. bool threadlocal=false) = 0;
  213. virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
  214. llvm::Value *src, llvm::Value *dest,
  215. llvm::Value *ivarOffset) = 0;
  216. virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
  217. llvm::Value *src, llvm::Value *dest) = 0;
  218. virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
  219. QualType ObjectTy,
  220. llvm::Value *BaseValue,
  221. const ObjCIvarDecl *Ivar,
  222. unsigned CVRQualifiers) = 0;
  223. virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
  224. const ObjCInterfaceDecl *Interface,
  225. const ObjCIvarDecl *Ivar) = 0;
  226. virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
  227. llvm::Value *DestPtr,
  228. llvm::Value *SrcPtr,
  229. llvm::Value *Size) = 0;
  230. virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
  231. const CodeGen::CGBlockInfo &blockInfo) = 0;
  232. virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,
  233. const CodeGen::CGBlockInfo &blockInfo) = 0;
  234. virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM,
  235. QualType T) = 0;
  236. virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name,
  237. bool Weak = false) = 0;
  238. struct MessageSendInfo {
  239. const CGFunctionInfo &CallInfo;
  240. llvm::PointerType *MessengerType;
  241. MessageSendInfo(const CGFunctionInfo &callInfo,
  242. llvm::PointerType *messengerType)
  243. : CallInfo(callInfo), MessengerType(messengerType) {}
  244. };
  245. MessageSendInfo getMessageSendInfo(const ObjCMethodDecl *method,
  246. QualType resultType,
  247. CallArgList &callArgs);
  248. // FIXME: This probably shouldn't be here, but the code to compute
  249. // it is here.
  250. unsigned ComputeBitfieldBitOffset(CodeGen::CodeGenModule &CGM,
  251. const ObjCInterfaceDecl *ID,
  252. const ObjCIvarDecl *Ivar);
  253. };
  254. /// Creates an instance of an Objective-C runtime class.
  255. //TODO: This should include some way of selecting which runtime to target.
  256. CGObjCRuntime *CreateGNUObjCRuntime(CodeGenModule &CGM);
  257. CGObjCRuntime *CreateMacObjCRuntime(CodeGenModule &CGM);
  258. }
  259. }
  260. #endif