message.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved.
  3. *
  4. * @APPLE_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apple Public Source License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://www.opensource.apple.com/apsl/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. *
  21. * @APPLE_LICENSE_HEADER_END@
  22. */
  23. #ifndef _OBJC_MESSAGE_H
  24. #define _OBJC_MESSAGE_H
  25. #include <objc/objc.h>
  26. #include <objc/runtime.h>
  27. #ifndef OBJC_SUPER
  28. #define OBJC_SUPER
  29. /// Specifies the superclass of an instance.
  30. struct objc_super {
  31. /// Specifies an instance of a class.
  32. __unsafe_unretained _Nonnull id receiver;
  33. /// Specifies the particular superclass of the instance to message.
  34. #if !defined(__cplusplus) && !__OBJC2__
  35. /* For compatibility with old objc-runtime.h header */
  36. __unsafe_unretained _Nonnull Class class;
  37. #else
  38. __unsafe_unretained _Nonnull Class super_class;
  39. #endif
  40. /* super_class is the first class to search */
  41. };
  42. #endif
  43. /* Basic Messaging Primitives
  44. *
  45. * On some architectures, use objc_msgSend_stret for some struct return types.
  46. * On some architectures, use objc_msgSend_fpret for some float return types.
  47. * On some architectures, use objc_msgSend_fp2ret for some float return types.
  48. *
  49. * These functions must be cast to an appropriate function pointer type
  50. * before being called.
  51. */
  52. #if !OBJC_OLD_DISPATCH_PROTOTYPES
  53. OBJC_EXPORT void
  54. objc_msgSend(void /* id self, SEL op, ... */ )
  55. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  56. OBJC_EXPORT void
  57. objc_msgSendSuper(void /* struct objc_super *super, SEL op, ... */ )
  58. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  59. #else
  60. /**
  61. * Sends a message with a simple return value to an instance of a class.
  62. *
  63. * @param self A pointer to the instance of the class that is to receive the message.
  64. * @param op The selector of the method that handles the message.
  65. * @param ...
  66. * A variable argument list containing the arguments to the method.
  67. *
  68. * @return The return value of the method.
  69. *
  70. * @note When it encounters a method call, the compiler generates a call to one of the
  71. * functions \c objc_msgSend, \c objc_msgSend_stret, \c objc_msgSendSuper, or \c objc_msgSendSuper_stret.
  72. * Messages sent to an object’s superclass (using the \c super keyword) are sent using \c objc_msgSendSuper;
  73. * other messages are sent using \c objc_msgSend. Methods that have data structures as return values
  74. * are sent using \c objc_msgSendSuper_stret and \c objc_msgSend_stret.
  75. */
  76. OBJC_EXPORT id _Nullable
  77. objc_msgSend(id _Nullable self, SEL _Nonnull op, ...)
  78. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  79. /**
  80. * Sends a message with a simple return value to the superclass of an instance of a class.
  81. *
  82. * @param super A pointer to an \c objc_super data structure. Pass values identifying the
  83. * context the message was sent to, including the instance of the class that is to receive the
  84. * message and the superclass at which to start searching for the method implementation.
  85. * @param op A pointer of type SEL. Pass the selector of the method that will handle the message.
  86. * @param ...
  87. * A variable argument list containing the arguments to the method.
  88. *
  89. * @return The return value of the method identified by \e op.
  90. *
  91. * @see objc_msgSend
  92. */
  93. OBJC_EXPORT id _Nullable
  94. objc_msgSendSuper(struct objc_super * _Nonnull super, SEL _Nonnull op, ...)
  95. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  96. #endif
  97. /* Struct-returning Messaging Primitives
  98. *
  99. * Use these functions to call methods that return structs on the stack.
  100. * On some architectures, some structures are returned in registers.
  101. * Consult your local function call ABI documentation for details.
  102. *
  103. * These functions must be cast to an appropriate function pointer type
  104. * before being called.
  105. */
  106. #if !OBJC_OLD_DISPATCH_PROTOTYPES
  107. OBJC_EXPORT void
  108. objc_msgSend_stret(void /* id self, SEL op, ... */ )
  109. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
  110. OBJC_ARM64_UNAVAILABLE;
  111. OBJC_EXPORT void
  112. objc_msgSendSuper_stret(void /* struct objc_super *super, SEL op, ... */ )
  113. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
  114. OBJC_ARM64_UNAVAILABLE;
  115. #else
  116. /**
  117. * Sends a message with a data-structure return value to an instance of a class.
  118. *
  119. * @see objc_msgSend
  120. */
  121. OBJC_EXPORT void
  122. objc_msgSend_stret(id _Nullable self, SEL _Nonnull op, ...)
  123. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
  124. OBJC_ARM64_UNAVAILABLE;
  125. /**
  126. * Sends a message with a data-structure return value to the superclass of an instance of a class.
  127. *
  128. * @see objc_msgSendSuper
  129. */
  130. OBJC_EXPORT void
  131. objc_msgSendSuper_stret(struct objc_super * _Nonnull super,
  132. SEL _Nonnull op, ...)
  133. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
  134. OBJC_ARM64_UNAVAILABLE;
  135. #endif
  136. /* Floating-point-returning Messaging Primitives
  137. *
  138. * Use these functions to call methods that return floating-point values
  139. * on the stack.
  140. * Consult your local function call ABI documentation for details.
  141. *
  142. * arm: objc_msgSend_fpret not used
  143. * i386: objc_msgSend_fpret used for `float`, `double`, `long double`.
  144. * x86-64: objc_msgSend_fpret used for `long double`.
  145. *
  146. * arm: objc_msgSend_fp2ret not used
  147. * i386: objc_msgSend_fp2ret not used
  148. * x86-64: objc_msgSend_fp2ret used for `_Complex long double`.
  149. *
  150. * These functions must be cast to an appropriate function pointer type
  151. * before being called.
  152. */
  153. #if !OBJC_OLD_DISPATCH_PROTOTYPES
  154. # if defined(__i386__)
  155. OBJC_EXPORT void
  156. objc_msgSend_fpret(void /* id self, SEL op, ... */ )
  157. OBJC_AVAILABLE(10.4, 2.0, 9.0, 1.0, 2.0);
  158. # elif defined(__x86_64__)
  159. OBJC_EXPORT void
  160. objc_msgSend_fpret(void /* id self, SEL op, ... */ )
  161. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  162. OBJC_EXPORT void
  163. objc_msgSend_fp2ret(void /* id self, SEL op, ... */ )
  164. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  165. # endif
  166. // !OBJC_OLD_DISPATCH_PROTOTYPES
  167. #else
  168. // OBJC_OLD_DISPATCH_PROTOTYPES
  169. # if defined(__i386__)
  170. /**
  171. * Sends a message with a floating-point return value to an instance of a class.
  172. *
  173. * @see objc_msgSend
  174. * @note On the i386 platform, the ABI for functions returning a floating-point value is
  175. * incompatible with that for functions returning an integral type. On the i386 platform, therefore,
  176. * you must use \c objc_msgSend_fpret for functions returning non-integral type. For \c float or
  177. * \c long \c double return types, cast the function to an appropriate function pointer type first.
  178. */
  179. OBJC_EXPORT double
  180. objc_msgSend_fpret(id _Nullable self, SEL _Nonnull op, ...)
  181. OBJC_AVAILABLE(10.4, 2.0, 9.0, 1.0, 2.0);
  182. /* Use objc_msgSendSuper() for fp-returning messages to super. */
  183. /* See also objc_msgSendv_fpret() below. */
  184. # elif defined(__x86_64__)
  185. /**
  186. * Sends a message with a floating-point return value to an instance of a class.
  187. *
  188. * @see objc_msgSend
  189. */
  190. OBJC_EXPORT long double
  191. objc_msgSend_fpret(id _Nullable self, SEL _Nonnull op, ...)
  192. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  193. # if __STDC_VERSION__ >= 199901L
  194. OBJC_EXPORT _Complex long double
  195. objc_msgSend_fp2ret(id _Nullable self, SEL _Nonnull op, ...)
  196. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  197. # else
  198. OBJC_EXPORT void objc_msgSend_fp2ret(id _Nullable self, SEL _Nonnull op, ...)
  199. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  200. # endif
  201. /* Use objc_msgSendSuper() for fp-returning messages to super. */
  202. /* See also objc_msgSendv_fpret() below. */
  203. # endif
  204. // OBJC_OLD_DISPATCH_PROTOTYPES
  205. #endif
  206. /* Direct Method Invocation Primitives
  207. * Use these functions to call the implementation of a given Method.
  208. * This is faster than calling method_getImplementation() and method_getName().
  209. *
  210. * The receiver must not be nil.
  211. *
  212. * These functions must be cast to an appropriate function pointer type
  213. * before being called.
  214. */
  215. #if !OBJC_OLD_DISPATCH_PROTOTYPES
  216. OBJC_EXPORT void
  217. method_invoke(void /* id receiver, Method m, ... */ )
  218. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  219. OBJC_EXPORT void
  220. method_invoke_stret(void /* id receiver, Method m, ... */ )
  221. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0)
  222. OBJC_ARM64_UNAVAILABLE;
  223. #else
  224. OBJC_EXPORT id _Nullable
  225. method_invoke(id _Nullable receiver, Method _Nonnull m, ...)
  226. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  227. OBJC_EXPORT void
  228. method_invoke_stret(id _Nullable receiver, Method _Nonnull m, ...)
  229. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0)
  230. OBJC_ARM64_UNAVAILABLE;
  231. #endif
  232. /* Message Forwarding Primitives
  233. * Use these functions to forward a message as if the receiver did not
  234. * respond to it.
  235. *
  236. * The receiver must not be nil.
  237. *
  238. * class_getMethodImplementation() may return (IMP)_objc_msgForward.
  239. * class_getMethodImplementation_stret() may return (IMP)_objc_msgForward_stret
  240. *
  241. * These functions must be cast to an appropriate function pointer type
  242. * before being called.
  243. *
  244. * Before Mac OS X 10.6, _objc_msgForward must not be called directly
  245. * but may be compared to other IMP values.
  246. */
  247. #if !OBJC_OLD_DISPATCH_PROTOTYPES
  248. OBJC_EXPORT void
  249. _objc_msgForward(void /* id receiver, SEL sel, ... */ )
  250. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  251. OBJC_EXPORT void
  252. _objc_msgForward_stret(void /* id receiver, SEL sel, ... */ )
  253. OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0, 2.0)
  254. OBJC_ARM64_UNAVAILABLE;
  255. #else
  256. OBJC_EXPORT id _Nullable
  257. _objc_msgForward(id _Nonnull receiver, SEL _Nonnull sel, ...)
  258. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  259. OBJC_EXPORT void
  260. _objc_msgForward_stret(id _Nonnull receiver, SEL _Nonnull sel, ...)
  261. OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0, 2.0)
  262. OBJC_ARM64_UNAVAILABLE;
  263. #endif
  264. /* Variable-argument Messaging Primitives
  265. *
  266. * Use these functions to call methods with a list of arguments, such
  267. * as the one passed to forward:: .
  268. *
  269. * The contents of the argument list are architecture-specific.
  270. * Consult your local function call ABI documentation for details.
  271. *
  272. * These functions must be cast to an appropriate function pointer type
  273. * before being called, except for objc_msgSendv_stret() which must not
  274. * be cast to a struct-returning type.
  275. */
  276. typedef void* marg_list;
  277. OBJC_EXPORT id _Nullable
  278. objc_msgSendv(id _Nullable self, SEL _Nonnull op, size_t arg_size,
  279. marg_list _Nonnull arg_frame)
  280. OBJC2_UNAVAILABLE;
  281. OBJC_EXPORT void
  282. objc_msgSendv_stret(void * _Nonnull stretAddr, id _Nullable self,
  283. SEL _Nonnull op, size_t arg_size,
  284. marg_list _Nullable arg_frame)
  285. OBJC2_UNAVAILABLE;
  286. /* Note that objc_msgSendv_stret() does not return a structure type,
  287. * and should not be cast to do so. This is unlike objc_msgSend_stret()
  288. * and objc_msgSendSuper_stret().
  289. */
  290. #if defined(__i386__)
  291. OBJC_EXPORT double
  292. objc_msgSendv_fpret(id _Nullable self, SEL _Nonnull op,
  293. unsigned arg_size, marg_list _Nullable arg_frame)
  294. OBJC2_UNAVAILABLE;
  295. #endif
  296. /* The following marg_list macros are of marginal utility. They
  297. * are included for compatibility with the old objc-class.h header. */
  298. #if !__OBJC2__
  299. #define marg_prearg_size 0
  300. #define marg_malloc(margs, method) \
  301. do { \
  302. margs = (marg_list *)malloc (marg_prearg_size + ((7 + method_getSizeOfArguments(method)) & ~7)); \
  303. } while (0)
  304. #define marg_free(margs) \
  305. do { \
  306. free(margs); \
  307. } while (0)
  308. #define marg_adjustedOffset(method, offset) \
  309. (marg_prearg_size + offset)
  310. #define marg_getRef(margs, offset, type) \
  311. ( (type *)((char *)margs + marg_adjustedOffset(method,offset) ) )
  312. #define marg_getValue(margs, offset, type) \
  313. ( *marg_getRef(margs, offset, type) )
  314. #define marg_setValue(margs, offset, type, value) \
  315. ( marg_getValue(margs, offset, type) = (value) )
  316. #endif
  317. #endif