message.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #pragma clang diagnostic push
  54. #pragma clang diagnostic ignored "-Wincompatible-library-redeclaration"
  55. OBJC_EXPORT void
  56. objc_msgSend(void /* id self, SEL op, ... */ )
  57. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  58. OBJC_EXPORT void
  59. objc_msgSendSuper(void /* struct objc_super *super, SEL op, ... */ )
  60. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  61. #pragma clang diagnostic pop
  62. #else
  63. /**
  64. * Sends a message with a simple return value to an instance of a class.
  65. *
  66. * @param self A pointer to the instance of the class that is to receive the message.
  67. * @param op The selector of the method that handles the message.
  68. * @param ...
  69. * A variable argument list containing the arguments to the method.
  70. *
  71. * @return The return value of the method.
  72. *
  73. * @note When it encounters a method call, the compiler generates a call to one of the
  74. * functions \c objc_msgSend, \c objc_msgSend_stret, \c objc_msgSendSuper, or \c objc_msgSendSuper_stret.
  75. * Messages sent to an object’s superclass (using the \c super keyword) are sent using \c objc_msgSendSuper;
  76. * other messages are sent using \c objc_msgSend. Methods that have data structures as return values
  77. * are sent using \c objc_msgSendSuper_stret and \c objc_msgSend_stret.
  78. */
  79. OBJC_EXPORT id _Nullable
  80. objc_msgSend(id _Nullable self, SEL _Nonnull op, ...)
  81. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  82. /**
  83. * Sends a message with a simple return value to the superclass of an instance of a class.
  84. *
  85. * @param super A pointer to an \c objc_super data structure. Pass values identifying the
  86. * context the message was sent to, including the instance of the class that is to receive the
  87. * message and the superclass at which to start searching for the method implementation.
  88. * @param op A pointer of type SEL. Pass the selector of the method that will handle the message.
  89. * @param ...
  90. * A variable argument list containing the arguments to the method.
  91. *
  92. * @return The return value of the method identified by \e op.
  93. *
  94. * @see objc_msgSend
  95. */
  96. OBJC_EXPORT id _Nullable
  97. objc_msgSendSuper(struct objc_super * _Nonnull super, SEL _Nonnull op, ...)
  98. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  99. #endif
  100. /* Struct-returning Messaging Primitives
  101. *
  102. * Use these functions to call methods that return structs on the stack.
  103. * On some architectures, some structures are returned in registers.
  104. * Consult your local function call ABI documentation for details.
  105. *
  106. * These functions must be cast to an appropriate function pointer type
  107. * before being called.
  108. */
  109. #if !OBJC_OLD_DISPATCH_PROTOTYPES
  110. #pragma clang diagnostic push
  111. #pragma clang diagnostic ignored "-Wincompatible-library-redeclaration"
  112. OBJC_EXPORT void
  113. objc_msgSend_stret(void /* id self, SEL op, ... */ )
  114. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
  115. OBJC_ARM64_UNAVAILABLE;
  116. OBJC_EXPORT void
  117. objc_msgSendSuper_stret(void /* struct objc_super *super, SEL op, ... */ )
  118. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
  119. OBJC_ARM64_UNAVAILABLE;
  120. #pragma clang diagnostic pop
  121. #else
  122. /**
  123. * Sends a message with a data-structure return value to an instance of a class.
  124. *
  125. * @see objc_msgSend
  126. */
  127. OBJC_EXPORT void
  128. objc_msgSend_stret(id _Nullable self, SEL _Nonnull op, ...)
  129. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
  130. OBJC_ARM64_UNAVAILABLE;
  131. /**
  132. * Sends a message with a data-structure return value to the superclass of an instance of a class.
  133. *
  134. * @see objc_msgSendSuper
  135. */
  136. OBJC_EXPORT void
  137. objc_msgSendSuper_stret(struct objc_super * _Nonnull super,
  138. SEL _Nonnull op, ...)
  139. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
  140. OBJC_ARM64_UNAVAILABLE;
  141. #endif
  142. /* Floating-point-returning Messaging Primitives
  143. *
  144. * Use these functions to call methods that return floating-point values
  145. * on the stack.
  146. * Consult your local function call ABI documentation for details.
  147. *
  148. * arm: objc_msgSend_fpret not used
  149. * i386: objc_msgSend_fpret used for `float`, `double`, `long double`.
  150. * x86-64: objc_msgSend_fpret used for `long double`.
  151. *
  152. * arm: objc_msgSend_fp2ret not used
  153. * i386: objc_msgSend_fp2ret not used
  154. * x86-64: objc_msgSend_fp2ret used for `_Complex long double`.
  155. *
  156. * These functions must be cast to an appropriate function pointer type
  157. * before being called.
  158. */
  159. #if !OBJC_OLD_DISPATCH_PROTOTYPES
  160. #pragma clang diagnostic push
  161. #pragma clang diagnostic ignored "-Wincompatible-library-redeclaration"
  162. # if defined(__i386__)
  163. OBJC_EXPORT void
  164. objc_msgSend_fpret(void /* id self, SEL op, ... */ )
  165. OBJC_AVAILABLE(10.4, 2.0, 9.0, 1.0, 2.0);
  166. # elif defined(__x86_64__)
  167. OBJC_EXPORT void
  168. objc_msgSend_fpret(void /* id self, SEL op, ... */ )
  169. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  170. OBJC_EXPORT void
  171. objc_msgSend_fp2ret(void /* id self, SEL op, ... */ )
  172. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  173. #pragma clang diagnostic pop
  174. # endif
  175. // !OBJC_OLD_DISPATCH_PROTOTYPES
  176. #else
  177. // OBJC_OLD_DISPATCH_PROTOTYPES
  178. # if defined(__i386__)
  179. /**
  180. * Sends a message with a floating-point return value to an instance of a class.
  181. *
  182. * @see objc_msgSend
  183. * @note On the i386 platform, the ABI for functions returning a floating-point value is
  184. * incompatible with that for functions returning an integral type. On the i386 platform, therefore,
  185. * you must use \c objc_msgSend_fpret for functions returning non-integral type. For \c float or
  186. * \c long \c double return types, cast the function to an appropriate function pointer type first.
  187. */
  188. #pragma clang diagnostic push
  189. #pragma clang diagnostic ignored "-Wincompatible-library-redeclaration"
  190. OBJC_EXPORT double
  191. objc_msgSend_fpret(id _Nullable self, SEL _Nonnull op, ...)
  192. OBJC_AVAILABLE(10.4, 2.0, 9.0, 1.0, 2.0);
  193. #pragma clang diagnostic pop
  194. /* Use objc_msgSendSuper() for fp-returning messages to super. */
  195. /* See also objc_msgSendv_fpret() below. */
  196. # elif defined(__x86_64__)
  197. /**
  198. * Sends a message with a floating-point return value to an instance of a class.
  199. *
  200. * @see objc_msgSend
  201. */
  202. OBJC_EXPORT long double
  203. objc_msgSend_fpret(id _Nullable self, SEL _Nonnull op, ...)
  204. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  205. # if __STDC_VERSION__ >= 199901L
  206. OBJC_EXPORT _Complex long double
  207. objc_msgSend_fp2ret(id _Nullable self, SEL _Nonnull op, ...)
  208. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  209. # else
  210. OBJC_EXPORT void objc_msgSend_fp2ret(id _Nullable self, SEL _Nonnull op, ...)
  211. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  212. # endif
  213. /* Use objc_msgSendSuper() for fp-returning messages to super. */
  214. /* See also objc_msgSendv_fpret() below. */
  215. # endif
  216. // OBJC_OLD_DISPATCH_PROTOTYPES
  217. #endif
  218. /* Direct Method Invocation Primitives
  219. * Use these functions to call the implementation of a given Method.
  220. * This is faster than calling method_getImplementation() and method_getName().
  221. *
  222. * The receiver must not be nil.
  223. *
  224. * These functions must be cast to an appropriate function pointer type
  225. * before being called.
  226. */
  227. #if !OBJC_OLD_DISPATCH_PROTOTYPES
  228. #pragma clang diagnostic push
  229. #pragma clang diagnostic ignored "-Wincompatible-library-redeclaration"
  230. OBJC_EXPORT void
  231. method_invoke(void /* id receiver, Method m, ... */ )
  232. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  233. OBJC_EXPORT void
  234. method_invoke_stret(void /* id receiver, Method m, ... */ )
  235. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0)
  236. OBJC_ARM64_UNAVAILABLE;
  237. #pragma clang diagnostic pop
  238. #else
  239. OBJC_EXPORT id _Nullable
  240. method_invoke(id _Nullable receiver, Method _Nonnull m, ...)
  241. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
  242. OBJC_EXPORT void
  243. method_invoke_stret(id _Nullable receiver, Method _Nonnull m, ...)
  244. OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0)
  245. OBJC_ARM64_UNAVAILABLE;
  246. #endif
  247. /* Message Forwarding Primitives
  248. * Use these functions to forward a message as if the receiver did not
  249. * respond to it.
  250. *
  251. * The receiver must not be nil.
  252. *
  253. * class_getMethodImplementation() may return (IMP)_objc_msgForward.
  254. * class_getMethodImplementation_stret() may return (IMP)_objc_msgForward_stret
  255. *
  256. * These functions must be cast to an appropriate function pointer type
  257. * before being called.
  258. *
  259. * Before Mac OS X 10.6, _objc_msgForward must not be called directly
  260. * but may be compared to other IMP values.
  261. */
  262. #if !OBJC_OLD_DISPATCH_PROTOTYPES
  263. #pragma clang diagnostic push
  264. #pragma clang diagnostic ignored "-Wincompatible-library-redeclaration"
  265. OBJC_EXPORT void
  266. _objc_msgForward(void /* id receiver, SEL sel, ... */ )
  267. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  268. OBJC_EXPORT void
  269. _objc_msgForward_stret(void /* id receiver, SEL sel, ... */ )
  270. OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0, 2.0)
  271. OBJC_ARM64_UNAVAILABLE;
  272. #pragma clang diagnostic pop
  273. #else
  274. OBJC_EXPORT id _Nullable
  275. _objc_msgForward(id _Nonnull receiver, SEL _Nonnull sel, ...)
  276. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  277. OBJC_EXPORT void
  278. _objc_msgForward_stret(id _Nonnull receiver, SEL _Nonnull sel, ...)
  279. OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0, 2.0)
  280. OBJC_ARM64_UNAVAILABLE;
  281. #endif
  282. /* Variable-argument Messaging Primitives
  283. *
  284. * Use these functions to call methods with a list of arguments, such
  285. * as the one passed to forward:: .
  286. *
  287. * The contents of the argument list are architecture-specific.
  288. * Consult your local function call ABI documentation for details.
  289. *
  290. * These functions must be cast to an appropriate function pointer type
  291. * before being called, except for objc_msgSendv_stret() which must not
  292. * be cast to a struct-returning type.
  293. */
  294. typedef void* marg_list;
  295. OBJC_EXPORT id _Nullable
  296. objc_msgSendv(id _Nullable self, SEL _Nonnull op, size_t arg_size,
  297. marg_list _Nonnull arg_frame)
  298. OBJC2_UNAVAILABLE;
  299. OBJC_EXPORT void
  300. objc_msgSendv_stret(void * _Nonnull stretAddr, id _Nullable self,
  301. SEL _Nonnull op, size_t arg_size,
  302. marg_list _Nullable arg_frame)
  303. OBJC2_UNAVAILABLE;
  304. /* Note that objc_msgSendv_stret() does not return a structure type,
  305. * and should not be cast to do so. This is unlike objc_msgSend_stret()
  306. * and objc_msgSendSuper_stret().
  307. */
  308. #if defined(__i386__)
  309. OBJC_EXPORT double
  310. objc_msgSendv_fpret(id _Nullable self, SEL _Nonnull op,
  311. unsigned arg_size, marg_list _Nullable arg_frame)
  312. OBJC2_UNAVAILABLE;
  313. #endif
  314. /* The following marg_list macros are of marginal utility. They
  315. * are included for compatibility with the old objc-class.h header. */
  316. #if !__OBJC2__
  317. #define marg_prearg_size 0
  318. #define marg_malloc(margs, method) \
  319. do { \
  320. margs = (marg_list *)malloc (marg_prearg_size + ((7 + method_getSizeOfArguments(method)) & ~7)); \
  321. } while (0)
  322. #define marg_free(margs) \
  323. do { \
  324. free(margs); \
  325. } while (0)
  326. #define marg_adjustedOffset(method, offset) \
  327. (marg_prearg_size + offset)
  328. #define marg_getRef(margs, offset, type) \
  329. ( (type *)((char *)margs + marg_adjustedOffset(method,offset) ) )
  330. #define marg_getValue(margs, offset, type) \
  331. ( *marg_getRef(margs, offset, type) )
  332. #define marg_setValue(margs, offset, type, value) \
  333. ( marg_getValue(margs, offset, type) = (value) )
  334. #endif
  335. #endif