]> git.saurik.com Git - apple/objc4.git/blob - runtime/runtime.h
objc4-818.2.tar.gz
[apple/objc4.git] / runtime / runtime.h
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
24 #ifndef _OBJC_RUNTIME_H
25 #define _OBJC_RUNTIME_H
26
27 #include <objc/objc.h>
28 #include <stdarg.h>
29 #include <stdint.h>
30 #include <stddef.h>
31 #include <Availability.h>
32 #include <TargetConditionals.h>
33
34 #if TARGET_OS_MAC
35 #include <sys/types.h>
36 #endif
37
38
39 /* Types */
40
41 #if !OBJC_TYPES_DEFINED
42
43 /// An opaque type that represents a method in a class definition.
44 typedef struct objc_method *Method;
45
46 /// An opaque type that represents an instance variable.
47 typedef struct objc_ivar *Ivar;
48
49 /// An opaque type that represents a category.
50 typedef struct objc_category *Category;
51
52 /// An opaque type that represents an Objective-C declared property.
53 typedef struct objc_property *objc_property_t;
54
55 struct objc_class {
56 Class _Nonnull isa OBJC_ISA_AVAILABILITY;
57
58 #if !__OBJC2__
59 Class _Nullable super_class OBJC2_UNAVAILABLE;
60 const char * _Nonnull name OBJC2_UNAVAILABLE;
61 long version OBJC2_UNAVAILABLE;
62 long info OBJC2_UNAVAILABLE;
63 long instance_size OBJC2_UNAVAILABLE;
64 struct objc_ivar_list * _Nullable ivars OBJC2_UNAVAILABLE;
65 struct objc_method_list * _Nullable * _Nullable methodLists OBJC2_UNAVAILABLE;
66 struct objc_cache * _Nonnull cache OBJC2_UNAVAILABLE;
67 struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE;
68 #endif
69
70 } OBJC2_UNAVAILABLE;
71 /* Use `Class` instead of `struct objc_class *` */
72
73 #endif
74
75 #ifdef __OBJC__
76 @class Protocol;
77 #else
78 typedef struct objc_object Protocol;
79 #endif
80
81 /// Defines a method
82 struct objc_method_description {
83 SEL _Nullable name; /**< The name of the method */
84 char * _Nullable types; /**< The types of the method arguments */
85 };
86
87 /// Defines a property attribute
88 typedef struct {
89 const char * _Nonnull name; /**< The name of the attribute */
90 const char * _Nonnull value; /**< The value of the attribute (usually empty) */
91 } objc_property_attribute_t;
92
93
94 /* Functions */
95
96 /* Working with Instances */
97
98 /**
99 * Returns a copy of a given object.
100 *
101 * @param obj An Objective-C object.
102 * @param size The size of the object \e obj.
103 *
104 * @return A copy of \e obj.
105 */
106 OBJC_EXPORT id _Nullable object_copy(id _Nullable obj, size_t size)
107 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
108 OBJC_ARC_UNAVAILABLE;
109
110 /**
111 * Frees the memory occupied by a given object.
112 *
113 * @param obj An Objective-C object.
114 *
115 * @return nil
116 */
117 OBJC_EXPORT id _Nullable
118 object_dispose(id _Nullable obj)
119 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
120 OBJC_ARC_UNAVAILABLE;
121
122 /**
123 * Returns the class of an object.
124 *
125 * @param obj The object you want to inspect.
126 *
127 * @return The class object of which \e object is an instance,
128 * or \c Nil if \e object is \c nil.
129 */
130 OBJC_EXPORT Class _Nullable
131 object_getClass(id _Nullable obj)
132 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
133
134 /**
135 * Sets the class of an object.
136 *
137 * @param obj The object to modify.
138 * @param cls A class object.
139 *
140 * @return The previous value of \e object's class, or \c Nil if \e object is \c nil.
141 */
142 OBJC_EXPORT Class _Nullable
143 object_setClass(id _Nullable obj, Class _Nonnull cls)
144 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
145
146
147 /**
148 * Returns whether an object is a class object.
149 *
150 * @param obj An Objective-C object.
151 *
152 * @return true if the object is a class or metaclass, false otherwise.
153 */
154 OBJC_EXPORT BOOL
155 object_isClass(id _Nullable obj)
156 OBJC_AVAILABLE(10.10, 8.0, 9.0, 1.0, 2.0);
157
158
159 /**
160 * Reads the value of an instance variable in an object.
161 *
162 * @param obj The object containing the instance variable whose value you want to read.
163 * @param ivar The Ivar describing the instance variable whose value you want to read.
164 *
165 * @return The value of the instance variable specified by \e ivar, or \c nil if \e object is \c nil.
166 *
167 * @note \c object_getIvar is faster than \c object_getInstanceVariable if the Ivar
168 * for the instance variable is already known.
169 */
170 OBJC_EXPORT id _Nullable
171 object_getIvar(id _Nullable obj, Ivar _Nonnull ivar)
172 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
173
174 /**
175 * Sets the value of an instance variable in an object.
176 *
177 * @param obj The object containing the instance variable whose value you want to set.
178 * @param ivar The Ivar describing the instance variable whose value you want to set.
179 * @param value The new value for the instance variable.
180 *
181 * @note Instance variables with known memory management (such as ARC strong and weak)
182 * use that memory management. Instance variables with unknown memory management
183 * are assigned as if they were unsafe_unretained.
184 * @note \c object_setIvar is faster than \c object_setInstanceVariable if the Ivar
185 * for the instance variable is already known.
186 */
187 OBJC_EXPORT void
188 object_setIvar(id _Nullable obj, Ivar _Nonnull ivar, id _Nullable value)
189 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
190
191 /**
192 * Sets the value of an instance variable in an object.
193 *
194 * @param obj The object containing the instance variable whose value you want to set.
195 * @param ivar The Ivar describing the instance variable whose value you want to set.
196 * @param value The new value for the instance variable.
197 *
198 * @note Instance variables with known memory management (such as ARC strong and weak)
199 * use that memory management. Instance variables with unknown memory management
200 * are assigned as if they were strong.
201 * @note \c object_setIvar is faster than \c object_setInstanceVariable if the Ivar
202 * for the instance variable is already known.
203 */
204 OBJC_EXPORT void
205 object_setIvarWithStrongDefault(id _Nullable obj, Ivar _Nonnull ivar,
206 id _Nullable value)
207 OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
208
209 /**
210 * Changes the value of an instance variable of a class instance.
211 *
212 * @param obj A pointer to an instance of a class. Pass the object containing
213 * the instance variable whose value you wish to modify.
214 * @param name A C string. Pass the name of the instance variable whose value you wish to modify.
215 * @param value The new value for the instance variable.
216 *
217 * @return A pointer to the \c Ivar data structure that defines the type and
218 * name of the instance variable specified by \e name.
219 *
220 * @note Instance variables with known memory management (such as ARC strong and weak)
221 * use that memory management. Instance variables with unknown memory management
222 * are assigned as if they were unsafe_unretained.
223 */
224 OBJC_EXPORT Ivar _Nullable
225 object_setInstanceVariable(id _Nullable obj, const char * _Nonnull name,
226 void * _Nullable value)
227 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
228 OBJC_ARC_UNAVAILABLE;
229
230 /**
231 * Changes the value of an instance variable of a class instance.
232 *
233 * @param obj A pointer to an instance of a class. Pass the object containing
234 * the instance variable whose value you wish to modify.
235 * @param name A C string. Pass the name of the instance variable whose value you wish to modify.
236 * @param value The new value for the instance variable.
237 *
238 * @return A pointer to the \c Ivar data structure that defines the type and
239 * name of the instance variable specified by \e name.
240 *
241 * @note Instance variables with known memory management (such as ARC strong and weak)
242 * use that memory management. Instance variables with unknown memory management
243 * are assigned as if they were strong.
244 */
245 OBJC_EXPORT Ivar _Nullable
246 object_setInstanceVariableWithStrongDefault(id _Nullable obj,
247 const char * _Nonnull name,
248 void * _Nullable value)
249 OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0)
250 OBJC_ARC_UNAVAILABLE;
251
252 /**
253 * Obtains the value of an instance variable of a class instance.
254 *
255 * @param obj A pointer to an instance of a class. Pass the object containing
256 * the instance variable whose value you wish to obtain.
257 * @param name A C string. Pass the name of the instance variable whose value you wish to obtain.
258 * @param outValue On return, contains a pointer to the value of the instance variable.
259 *
260 * @return A pointer to the \c Ivar data structure that defines the type and name of
261 * the instance variable specified by \e name.
262 */
263 OBJC_EXPORT Ivar _Nullable
264 object_getInstanceVariable(id _Nullable obj, const char * _Nonnull name,
265 void * _Nullable * _Nullable outValue)
266 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
267 OBJC_ARC_UNAVAILABLE;
268
269
270 /* Obtaining Class Definitions */
271
272 /**
273 * Returns the class definition of a specified class.
274 *
275 * @param name The name of the class to look up.
276 *
277 * @return The Class object for the named class, or \c nil
278 * if the class is not registered with the Objective-C runtime.
279 *
280 * @note \c objc_getClass is different from \c objc_lookUpClass in that if the class
281 * is not registered, \c objc_getClass calls the class handler callback and then checks
282 * a second time to see whether the class is registered. \c objc_lookUpClass does
283 * not call the class handler callback.
284 *
285 * @warning Earlier implementations of this function (prior to OS X v10.0)
286 * terminate the program if the class does not exist.
287 */
288 OBJC_EXPORT Class _Nullable
289 objc_getClass(const char * _Nonnull name)
290 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
291
292 /**
293 * Returns the metaclass definition of a specified class.
294 *
295 * @param name The name of the class to look up.
296 *
297 * @return The \c Class object for the metaclass of the named class, or \c nil if the class
298 * is not registered with the Objective-C runtime.
299 *
300 * @note If the definition for the named class is not registered, this function calls the class handler
301 * callback and then checks a second time to see if the class is registered. However, every class
302 * definition must have a valid metaclass definition, and so the metaclass definition is always returned,
303 * whether it’s valid or not.
304 */
305 OBJC_EXPORT Class _Nullable
306 objc_getMetaClass(const char * _Nonnull name)
307 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
308
309 /**
310 * Returns the class definition of a specified class.
311 *
312 * @param name The name of the class to look up.
313 *
314 * @return The Class object for the named class, or \c nil if the class
315 * is not registered with the Objective-C runtime.
316 *
317 * @note \c objc_getClass is different from this function in that if the class is not
318 * registered, \c objc_getClass calls the class handler callback and then checks a second
319 * time to see whether the class is registered. This function does not call the class handler callback.
320 */
321 OBJC_EXPORT Class _Nullable
322 objc_lookUpClass(const char * _Nonnull name)
323 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
324
325 /**
326 * Returns the class definition of a specified class.
327 *
328 * @param name The name of the class to look up.
329 *
330 * @return The Class object for the named class.
331 *
332 * @note This function is the same as \c objc_getClass, but kills the process if the class is not found.
333 * @note This function is used by ZeroLink, where failing to find a class would be a compile-time link error without ZeroLink.
334 */
335 OBJC_EXPORT Class _Nonnull
336 objc_getRequiredClass(const char * _Nonnull name)
337 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
338
339 /**
340 * Obtains the list of registered class definitions.
341 *
342 * @param buffer An array of \c Class values. On output, each \c Class value points to
343 * one class definition, up to either \e bufferCount or the total number of registered classes,
344 * whichever is less. You can pass \c NULL to obtain the total number of registered class
345 * definitions without actually retrieving any class definitions.
346 * @param bufferCount An integer value. Pass the number of pointers for which you have allocated space
347 * in \e buffer. On return, this function fills in only this number of elements. If this number is less
348 * than the number of registered classes, this function returns an arbitrary subset of the registered classes.
349 *
350 * @return An integer value indicating the total number of registered classes.
351 *
352 * @note The Objective-C runtime library automatically registers all the classes defined in your source code.
353 * You can create class definitions at runtime and register them with the \c objc_addClass function.
354 *
355 * @warning You cannot assume that class objects you get from this function are classes that inherit from \c NSObject,
356 * so you cannot safely call any methods on such classes without detecting that the method is implemented first.
357 */
358 OBJC_EXPORT int
359 objc_getClassList(Class _Nonnull * _Nullable buffer, int bufferCount)
360 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
361
362 /**
363 * Creates and returns a list of pointers to all registered class definitions.
364 *
365 * @param outCount An integer pointer used to store the number of classes returned by
366 * this function in the list. It can be \c nil.
367 *
368 * @return A nil terminated array of classes. It must be freed with \c free().
369 *
370 * @see objc_getClassList
371 */
372 OBJC_EXPORT Class _Nonnull * _Nullable
373 objc_copyClassList(unsigned int * _Nullable outCount)
374 OBJC_AVAILABLE(10.7, 3.1, 9.0, 1.0, 2.0);
375
376
377 /* Working with Classes */
378
379 /**
380 * Returns the name of a class.
381 *
382 * @param cls A class object.
383 *
384 * @return The name of the class, or the empty string if \e cls is \c Nil.
385 */
386 OBJC_EXPORT const char * _Nonnull
387 class_getName(Class _Nullable cls)
388 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
389
390 /**
391 * Returns a Boolean value that indicates whether a class object is a metaclass.
392 *
393 * @param cls A class object.
394 *
395 * @return \c YES if \e cls is a metaclass, \c NO if \e cls is a non-meta class,
396 * \c NO if \e cls is \c Nil.
397 */
398 OBJC_EXPORT BOOL
399 class_isMetaClass(Class _Nullable cls)
400 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
401
402 /**
403 * Returns the superclass of a class.
404 *
405 * @param cls A class object.
406 *
407 * @return The superclass of the class, or \c Nil if
408 * \e cls is a root class, or \c Nil if \e cls is \c Nil.
409 *
410 * @note You should usually use \c NSObject's \c superclass method instead of this function.
411 */
412 OBJC_EXPORT Class _Nullable
413 class_getSuperclass(Class _Nullable cls)
414 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
415
416 /**
417 * Sets the superclass of a given class.
418 *
419 * @param cls The class whose superclass you want to set.
420 * @param newSuper The new superclass for cls.
421 *
422 * @return The old superclass for cls.
423 *
424 * @warning You should not use this function.
425 */
426 OBJC_EXPORT Class _Nonnull
427 class_setSuperclass(Class _Nonnull cls, Class _Nonnull newSuper)
428 __OSX_DEPRECATED(10.5, 10.5, "not recommended")
429 __IOS_DEPRECATED(2.0, 2.0, "not recommended")
430 __TVOS_DEPRECATED(9.0, 9.0, "not recommended")
431 __WATCHOS_DEPRECATED(1.0, 1.0, "not recommended")
432 #ifndef __APPLE_BLEACH_SDK__
433 __BRIDGEOS_DEPRECATED(2.0, 2.0, "not recommended")
434 #endif
435 ;
436
437 /**
438 * Returns the version number of a class definition.
439 *
440 * @param cls A pointer to a \c Class data structure. Pass
441 * the class definition for which you wish to obtain the version.
442 *
443 * @return An integer indicating the version number of the class definition.
444 *
445 * @see class_setVersion
446 */
447 OBJC_EXPORT int
448 class_getVersion(Class _Nullable cls)
449 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
450
451 /**
452 * Sets the version number of a class definition.
453 *
454 * @param cls A pointer to an Class data structure.
455 * Pass the class definition for which you wish to set the version.
456 * @param version An integer. Pass the new version number of the class definition.
457 *
458 * @note You can use the version number of the class definition to provide versioning of the
459 * interface that your class represents to other classes. This is especially useful for object
460 * serialization (that is, archiving of the object in a flattened form), where it is important to
461 * recognize changes to the layout of the instance variables in different class-definition versions.
462 * @note Classes derived from the Foundation framework \c NSObject class can set the class-definition
463 * version number using the \c setVersion: class method, which is implemented using the \c class_setVersion function.
464 */
465 OBJC_EXPORT void
466 class_setVersion(Class _Nullable cls, int version)
467 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
468
469 /**
470 * Returns the size of instances of a class.
471 *
472 * @param cls A class object.
473 *
474 * @return The size in bytes of instances of the class \e cls, or \c 0 if \e cls is \c Nil.
475 */
476 OBJC_EXPORT size_t
477 class_getInstanceSize(Class _Nullable cls)
478 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
479
480 /**
481 * Returns the \c Ivar for a specified instance variable of a given class.
482 *
483 * @param cls The class whose instance variable you wish to obtain.
484 * @param name The name of the instance variable definition to obtain.
485 *
486 * @return A pointer to an \c Ivar data structure containing information about
487 * the instance variable specified by \e name.
488 */
489 OBJC_EXPORT Ivar _Nullable
490 class_getInstanceVariable(Class _Nullable cls, const char * _Nonnull name)
491 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
492
493 /**
494 * Returns the Ivar for a specified class variable of a given class.
495 *
496 * @param cls The class definition whose class variable you wish to obtain.
497 * @param name The name of the class variable definition to obtain.
498 *
499 * @return A pointer to an \c Ivar data structure containing information about the class variable specified by \e name.
500 */
501 OBJC_EXPORT Ivar _Nullable
502 class_getClassVariable(Class _Nullable cls, const char * _Nonnull name)
503 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
504
505 /**
506 * Describes the instance variables declared by a class.
507 *
508 * @param cls The class to inspect.
509 * @param outCount On return, contains the length of the returned array.
510 * If outCount is NULL, the length is not returned.
511 *
512 * @return An array of pointers of type Ivar describing the instance variables declared by the class.
513 * Any instance variables declared by superclasses are not included. The array contains *outCount
514 * pointers followed by a NULL terminator. You must free the array with free().
515 *
516 * If the class declares no instance variables, or cls is Nil, NULL is returned and *outCount is 0.
517 */
518 OBJC_EXPORT Ivar _Nonnull * _Nullable
519 class_copyIvarList(Class _Nullable cls, unsigned int * _Nullable outCount)
520 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
521
522 /**
523 * Returns a specified instance method for a given class.
524 *
525 * @param cls The class you want to inspect.
526 * @param name The selector of the method you want to retrieve.
527 *
528 * @return The method that corresponds to the implementation of the selector specified by
529 * \e name for the class specified by \e cls, or \c NULL if the specified class or its
530 * superclasses do not contain an instance method with the specified selector.
531 *
532 * @note This function searches superclasses for implementations, whereas \c class_copyMethodList does not.
533 */
534 OBJC_EXPORT Method _Nullable
535 class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name)
536 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
537
538 /**
539 * Returns a pointer to the data structure describing a given class method for a given class.
540 *
541 * @param cls A pointer to a class definition. Pass the class that contains the method you want to retrieve.
542 * @param name A pointer of type \c SEL. Pass the selector of the method you want to retrieve.
543 *
544 * @return A pointer to the \c Method data structure that corresponds to the implementation of the
545 * selector specified by aSelector for the class specified by aClass, or NULL if the specified
546 * class or its superclasses do not contain an instance method with the specified selector.
547 *
548 * @note Note that this function searches superclasses for implementations,
549 * whereas \c class_copyMethodList does not.
550 */
551 OBJC_EXPORT Method _Nullable
552 class_getClassMethod(Class _Nullable cls, SEL _Nonnull name)
553 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
554
555 /**
556 * Returns the function pointer that would be called if a
557 * particular message were sent to an instance of a class.
558 *
559 * @param cls The class you want to inspect.
560 * @param name A selector.
561 *
562 * @return The function pointer that would be called if \c [object name] were called
563 * with an instance of the class, or \c NULL if \e cls is \c Nil.
564 *
565 * @note \c class_getMethodImplementation may be faster than \c method_getImplementation(class_getInstanceMethod(cls, name)).
566 * @note The function pointer returned may be a function internal to the runtime instead of
567 * an actual method implementation. For example, if instances of the class do not respond to
568 * the selector, the function pointer returned will be part of the runtime's message forwarding machinery.
569 */
570 OBJC_EXPORT IMP _Nullable
571 class_getMethodImplementation(Class _Nullable cls, SEL _Nonnull name)
572 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
573
574 /**
575 * Returns the function pointer that would be called if a particular
576 * message were sent to an instance of a class.
577 *
578 * @param cls The class you want to inspect.
579 * @param name A selector.
580 *
581 * @return The function pointer that would be called if \c [object name] were called
582 * with an instance of the class, or \c NULL if \e cls is \c Nil.
583 */
584 OBJC_EXPORT IMP _Nullable
585 class_getMethodImplementation_stret(Class _Nullable cls, SEL _Nonnull name)
586 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0)
587 OBJC_ARM64_UNAVAILABLE;
588
589 /**
590 * Returns a Boolean value that indicates whether instances of a class respond to a particular selector.
591 *
592 * @param cls The class you want to inspect.
593 * @param sel A selector.
594 *
595 * @return \c YES if instances of the class respond to the selector, otherwise \c NO.
596 *
597 * @note You should usually use \c NSObject's \c respondsToSelector: or \c instancesRespondToSelector:
598 * methods instead of this function.
599 */
600 OBJC_EXPORT BOOL
601 class_respondsToSelector(Class _Nullable cls, SEL _Nonnull sel)
602 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
603
604 /**
605 * Describes the instance methods implemented by a class.
606 *
607 * @param cls The class you want to inspect.
608 * @param outCount On return, contains the length of the returned array.
609 * If outCount is NULL, the length is not returned.
610 *
611 * @return An array of pointers of type Method describing the instance methods
612 * implemented by the class—any instance methods implemented by superclasses are not included.
613 * The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
614 *
615 * If cls implements no instance methods, or cls is Nil, returns NULL and *outCount is 0.
616 *
617 * @note To get the class methods of a class, use \c class_copyMethodList(object_getClass(cls), &count).
618 * @note To get the implementations of methods that may be implemented by superclasses,
619 * use \c class_getInstanceMethod or \c class_getClassMethod.
620 */
621 OBJC_EXPORT Method _Nonnull * _Nullable
622 class_copyMethodList(Class _Nullable cls, unsigned int * _Nullable outCount)
623 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
624
625 /**
626 * Returns a Boolean value that indicates whether a class conforms to a given protocol.
627 *
628 * @param cls The class you want to inspect.
629 * @param protocol A protocol.
630 *
631 * @return YES if cls conforms to protocol, otherwise NO.
632 *
633 * @note You should usually use NSObject's conformsToProtocol: method instead of this function.
634 */
635 OBJC_EXPORT BOOL
636 class_conformsToProtocol(Class _Nullable cls, Protocol * _Nullable protocol)
637 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
638
639 /**
640 * Describes the protocols adopted by a class.
641 *
642 * @param cls The class you want to inspect.
643 * @param outCount On return, contains the length of the returned array.
644 * If outCount is NULL, the length is not returned.
645 *
646 * @return An array of pointers of type Protocol* describing the protocols adopted
647 * by the class. Any protocols adopted by superclasses or other protocols are not included.
648 * The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
649 *
650 * If cls adopts no protocols, or cls is Nil, returns NULL and *outCount is 0.
651 */
652 OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable
653 class_copyProtocolList(Class _Nullable cls, unsigned int * _Nullable outCount)
654 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
655
656 /**
657 * Returns a property with a given name of a given class.
658 *
659 * @param cls The class you want to inspect.
660 * @param name The name of the property you want to inspect.
661 *
662 * @return A pointer of type \c objc_property_t describing the property, or
663 * \c NULL if the class does not declare a property with that name,
664 * or \c NULL if \e cls is \c Nil.
665 */
666 OBJC_EXPORT objc_property_t _Nullable
667 class_getProperty(Class _Nullable cls, const char * _Nonnull name)
668 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
669
670 /**
671 * Describes the properties declared by a class.
672 *
673 * @param cls The class you want to inspect.
674 * @param outCount On return, contains the length of the returned array.
675 * If \e outCount is \c NULL, the length is not returned.
676 *
677 * @return An array of pointers of type \c objc_property_t describing the properties
678 * declared by the class. Any properties declared by superclasses are not included.
679 * The array contains \c *outCount pointers followed by a \c NULL terminator. You must free the array with \c free().
680 *
681 * If \e cls declares no properties, or \e cls is \c Nil, returns \c NULL and \c *outCount is \c 0.
682 */
683 OBJC_EXPORT objc_property_t _Nonnull * _Nullable
684 class_copyPropertyList(Class _Nullable cls, unsigned int * _Nullable outCount)
685 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
686
687 /**
688 * Returns a description of the \c Ivar layout for a given class.
689 *
690 * @param cls The class to inspect.
691 *
692 * @return A description of the \c Ivar layout for \e cls.
693 */
694 OBJC_EXPORT const uint8_t * _Nullable
695 class_getIvarLayout(Class _Nullable cls)
696 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
697
698 /**
699 * Returns a description of the layout of weak Ivars for a given class.
700 *
701 * @param cls The class to inspect.
702 *
703 * @return A description of the layout of the weak \c Ivars for \e cls.
704 */
705 OBJC_EXPORT const uint8_t * _Nullable
706 class_getWeakIvarLayout(Class _Nullable cls)
707 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
708
709 /**
710 * Adds a new method to a class with a given name and implementation.
711 *
712 * @param cls The class to which to add a method.
713 * @param name A selector that specifies the name of the method being added.
714 * @param imp A function which is the implementation of the new method. The function must take at least two arguments—self and _cmd.
715 * @param types An array of characters that describe the types of the arguments to the method.
716 *
717 * @return YES if the method was added successfully, otherwise NO
718 * (for example, the class already contains a method implementation with that name).
719 *
720 * @note class_addMethod will add an override of a superclass's implementation,
721 * but will not replace an existing implementation in this class.
722 * To change an existing implementation, use method_setImplementation.
723 */
724 OBJC_EXPORT BOOL
725 class_addMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp,
726 const char * _Nullable types)
727 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
728
729 /**
730 * Replaces the implementation of a method for a given class.
731 *
732 * @param cls The class you want to modify.
733 * @param name A selector that identifies the method whose implementation you want to replace.
734 * @param imp The new implementation for the method identified by name for the class identified by cls.
735 * @param types An array of characters that describe the types of the arguments to the method.
736 * Since the function must take at least two arguments—self and _cmd, the second and third characters
737 * must be “@:” (the first character is the return type).
738 *
739 * @return The previous implementation of the method identified by \e name for the class identified by \e cls.
740 *
741 * @note This function behaves in two different ways:
742 * - If the method identified by \e name does not yet exist, it is added as if \c class_addMethod were called.
743 * The type encoding specified by \e types is used as given.
744 * - If the method identified by \e name does exist, its \c IMP is replaced as if \c method_setImplementation were called.
745 * The type encoding specified by \e types is ignored.
746 */
747 OBJC_EXPORT IMP _Nullable
748 class_replaceMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp,
749 const char * _Nullable types)
750 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
751
752 /**
753 * Adds a new instance variable to a class.
754 *
755 * @return YES if the instance variable was added successfully, otherwise NO
756 * (for example, the class already contains an instance variable with that name).
757 *
758 * @note This function may only be called after objc_allocateClassPair and before objc_registerClassPair.
759 * Adding an instance variable to an existing class is not supported.
760 * @note The class must not be a metaclass. Adding an instance variable to a metaclass is not supported.
761 * @note The instance variable's minimum alignment in bytes is 1<<align. The minimum alignment of an instance
762 * variable depends on the ivar's type and the machine architecture.
763 * For variables of any pointer type, pass log2(sizeof(pointer_type)).
764 */
765 OBJC_EXPORT BOOL
766 class_addIvar(Class _Nullable cls, const char * _Nonnull name, size_t size,
767 uint8_t alignment, const char * _Nullable types)
768 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
769
770 /**
771 * Adds a protocol to a class.
772 *
773 * @param cls The class to modify.
774 * @param protocol The protocol to add to \e cls.
775 *
776 * @return \c YES if the method was added successfully, otherwise \c NO
777 * (for example, the class already conforms to that protocol).
778 */
779 OBJC_EXPORT BOOL
780 class_addProtocol(Class _Nullable cls, Protocol * _Nonnull protocol)
781 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
782
783 /**
784 * Adds a property to a class.
785 *
786 * @param cls The class to modify.
787 * @param name The name of the property.
788 * @param attributes An array of property attributes.
789 * @param attributeCount The number of attributes in \e attributes.
790 *
791 * @return \c YES if the property was added successfully, otherwise \c NO
792 * (for example, the class already has that property).
793 */
794 OBJC_EXPORT BOOL
795 class_addProperty(Class _Nullable cls, const char * _Nonnull name,
796 const objc_property_attribute_t * _Nullable attributes,
797 unsigned int attributeCount)
798 OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
799
800 /**
801 * Replace a property of a class.
802 *
803 * @param cls The class to modify.
804 * @param name The name of the property.
805 * @param attributes An array of property attributes.
806 * @param attributeCount The number of attributes in \e attributes.
807 */
808 OBJC_EXPORT void
809 class_replaceProperty(Class _Nullable cls, const char * _Nonnull name,
810 const objc_property_attribute_t * _Nullable attributes,
811 unsigned int attributeCount)
812 OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
813
814 /**
815 * Sets the Ivar layout for a given class.
816 *
817 * @param cls The class to modify.
818 * @param layout The layout of the \c Ivars for \e cls.
819 */
820 OBJC_EXPORT void
821 class_setIvarLayout(Class _Nullable cls, const uint8_t * _Nullable layout)
822 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
823
824 /**
825 * Sets the layout for weak Ivars for a given class.
826 *
827 * @param cls The class to modify.
828 * @param layout The layout of the weak Ivars for \e cls.
829 */
830 OBJC_EXPORT void
831 class_setWeakIvarLayout(Class _Nullable cls, const uint8_t * _Nullable layout)
832 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
833
834 /**
835 * Used by CoreFoundation's toll-free bridging.
836 * Return the id of the named class.
837 *
838 * @return The id of the named class, or an uninitialized class
839 * structure that will be used for the class when and if it does
840 * get loaded.
841 *
842 * @warning Do not call this function yourself.
843 */
844 OBJC_EXPORT Class _Nonnull
845 objc_getFutureClass(const char * _Nonnull name)
846 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0)
847 OBJC_ARC_UNAVAILABLE;
848
849
850 /* Instantiating Classes */
851
852 /**
853 * Creates an instance of a class, allocating memory for the class in the
854 * default malloc memory zone.
855 *
856 * @param cls The class that you wish to allocate an instance of.
857 * @param extraBytes An integer indicating the number of extra bytes to allocate.
858 * The additional bytes can be used to store additional instance variables beyond
859 * those defined in the class definition.
860 *
861 * @return An instance of the class \e cls.
862 */
863 OBJC_EXPORT id _Nullable
864 class_createInstance(Class _Nullable cls, size_t extraBytes)
865 OBJC_RETURNS_RETAINED
866 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
867
868 /**
869 * Creates an instance of a class at the specific location provided.
870 *
871 * @param cls The class that you wish to allocate an instance of.
872 * @param bytes The location at which to allocate an instance of \e cls.
873 * Must point to at least \c class_getInstanceSize(cls) bytes of well-aligned,
874 * zero-filled memory.
875 *
876 * @return \e bytes on success, \c nil otherwise. (For example, \e cls or \e bytes
877 * might be \c nil)
878 *
879 * @see class_createInstance
880 */
881 OBJC_EXPORT id _Nullable
882 objc_constructInstance(Class _Nullable cls, void * _Nullable bytes)
883 OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0, 2.0)
884 OBJC_ARC_UNAVAILABLE;
885
886 /**
887 * Destroys an instance of a class without freeing memory and removes any
888 * associated references this instance might have had.
889 *
890 * @param obj The class instance to destroy.
891 *
892 * @return \e obj. Does nothing if \e obj is nil.
893 *
894 * @note CF and other clients do call this under GC.
895 */
896 OBJC_EXPORT void * _Nullable objc_destructInstance(id _Nullable obj)
897 OBJC_AVAILABLE(10.6, 3.0, 9.0, 1.0, 2.0)
898 OBJC_ARC_UNAVAILABLE;
899
900
901 /* Adding Classes */
902
903 /**
904 * Creates a new class and metaclass.
905 *
906 * @param superclass The class to use as the new class's superclass, or \c Nil to create a new root class.
907 * @param name The string to use as the new class's name. The string will be copied.
908 * @param extraBytes The number of bytes to allocate for indexed ivars at the end of
909 * the class and metaclass objects. This should usually be \c 0.
910 *
911 * @return The new class, or Nil if the class could not be created (for example, the desired name is already in use).
912 *
913 * @note You can get a pointer to the new metaclass by calling \c object_getClass(newClass).
914 * @note To create a new class, start by calling \c objc_allocateClassPair.
915 * Then set the class's attributes with functions like \c class_addMethod and \c class_addIvar.
916 * When you are done building the class, call \c objc_registerClassPair. The new class is now ready for use.
917 * @note Instance methods and instance variables should be added to the class itself.
918 * Class methods should be added to the metaclass.
919 */
920 OBJC_EXPORT Class _Nullable
921 objc_allocateClassPair(Class _Nullable superclass, const char * _Nonnull name,
922 size_t extraBytes)
923 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
924
925 /**
926 * Registers a class that was allocated using \c objc_allocateClassPair.
927 *
928 * @param cls The class you want to register.
929 */
930 OBJC_EXPORT void
931 objc_registerClassPair(Class _Nonnull cls)
932 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
933
934 /**
935 * Used by Foundation's Key-Value Observing.
936 *
937 * @warning Do not call this function yourself.
938 */
939 OBJC_EXPORT Class _Nonnull
940 objc_duplicateClass(Class _Nonnull original, const char * _Nonnull name,
941 size_t extraBytes)
942 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
943
944 /**
945 * Destroy a class and its associated metaclass.
946 *
947 * @param cls The class to be destroyed. It must have been allocated with
948 * \c objc_allocateClassPair
949 *
950 * @warning Do not call if instances of this class or a subclass exist.
951 */
952 OBJC_EXPORT void
953 objc_disposeClassPair(Class _Nonnull cls)
954 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
955
956
957 /* Working with Methods */
958
959 /**
960 * Returns the name of a method.
961 *
962 * @param m The method to inspect.
963 *
964 * @return A pointer of type SEL.
965 *
966 * @note To get the method name as a C string, call \c sel_getName(method_getName(method)).
967 */
968 OBJC_EXPORT SEL _Nonnull
969 method_getName(Method _Nonnull m)
970 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
971
972 /**
973 * Returns the implementation of a method.
974 *
975 * @param m The method to inspect.
976 *
977 * @return A function pointer of type IMP.
978 */
979 OBJC_EXPORT IMP _Nonnull
980 method_getImplementation(Method _Nonnull m)
981 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
982
983 /**
984 * Returns a string describing a method's parameter and return types.
985 *
986 * @param m The method to inspect.
987 *
988 * @return A C string. The string may be \c NULL.
989 */
990 OBJC_EXPORT const char * _Nullable
991 method_getTypeEncoding(Method _Nonnull m)
992 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
993
994 /**
995 * Returns the number of arguments accepted by a method.
996 *
997 * @param m A pointer to a \c Method data structure. Pass the method in question.
998 *
999 * @return An integer containing the number of arguments accepted by the given method.
1000 */
1001 OBJC_EXPORT unsigned int
1002 method_getNumberOfArguments(Method _Nonnull m)
1003 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
1004
1005 /**
1006 * Returns a string describing a method's return type.
1007 *
1008 * @param m The method to inspect.
1009 *
1010 * @return A C string describing the return type. You must free the string with \c free().
1011 */
1012 OBJC_EXPORT char * _Nonnull
1013 method_copyReturnType(Method _Nonnull m)
1014 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1015
1016 /**
1017 * Returns a string describing a single parameter type of a method.
1018 *
1019 * @param m The method to inspect.
1020 * @param index The index of the parameter to inspect.
1021 *
1022 * @return A C string describing the type of the parameter at index \e index, or \c NULL
1023 * if method has no parameter index \e index. You must free the string with \c free().
1024 */
1025 OBJC_EXPORT char * _Nullable
1026 method_copyArgumentType(Method _Nonnull m, unsigned int index)
1027 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1028
1029 /**
1030 * Returns by reference a string describing a method's return type.
1031 *
1032 * @param m The method you want to inquire about.
1033 * @param dst The reference string to store the description.
1034 * @param dst_len The maximum number of characters that can be stored in \e dst.
1035 *
1036 * @note The method's return type string is copied to \e dst.
1037 * \e dst is filled as if \c strncpy(dst, parameter_type, dst_len) were called.
1038 */
1039 OBJC_EXPORT void
1040 method_getReturnType(Method _Nonnull m, char * _Nonnull dst, size_t dst_len)
1041 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1042
1043 /**
1044 * Returns by reference a string describing a single parameter type of a method.
1045 *
1046 * @param m The method you want to inquire about.
1047 * @param index The index of the parameter you want to inquire about.
1048 * @param dst The reference string to store the description.
1049 * @param dst_len The maximum number of characters that can be stored in \e dst.
1050 *
1051 * @note The parameter type string is copied to \e dst. \e dst is filled as if \c strncpy(dst, parameter_type, dst_len)
1052 * were called. If the method contains no parameter with that index, \e dst is filled as
1053 * if \c strncpy(dst, "", dst_len) were called.
1054 */
1055 OBJC_EXPORT void
1056 method_getArgumentType(Method _Nonnull m, unsigned int index,
1057 char * _Nullable dst, size_t dst_len)
1058 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1059
1060 OBJC_EXPORT struct objc_method_description * _Nonnull
1061 method_getDescription(Method _Nonnull m)
1062 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1063
1064 /**
1065 * Sets the implementation of a method.
1066 *
1067 * @param m The method for which to set an implementation.
1068 * @param imp The implemention to set to this method.
1069 *
1070 * @return The previous implementation of the method.
1071 */
1072 OBJC_EXPORT IMP _Nonnull
1073 method_setImplementation(Method _Nonnull m, IMP _Nonnull imp)
1074 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1075
1076 /**
1077 * Exchanges the implementations of two methods.
1078 *
1079 * @param m1 Method to exchange with second method.
1080 * @param m2 Method to exchange with first method.
1081 *
1082 * @note This is an atomic version of the following:
1083 * \code
1084 * IMP imp1 = method_getImplementation(m1);
1085 * IMP imp2 = method_getImplementation(m2);
1086 * method_setImplementation(m1, imp2);
1087 * method_setImplementation(m2, imp1);
1088 * \endcode
1089 */
1090 OBJC_EXPORT void
1091 method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2)
1092 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1093
1094
1095 /* Working with Instance Variables */
1096
1097 /**
1098 * Returns the name of an instance variable.
1099 *
1100 * @param v The instance variable you want to enquire about.
1101 *
1102 * @return A C string containing the instance variable's name.
1103 */
1104 OBJC_EXPORT const char * _Nullable
1105 ivar_getName(Ivar _Nonnull v)
1106 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1107
1108 /**
1109 * Returns the type string of an instance variable.
1110 *
1111 * @param v The instance variable you want to enquire about.
1112 *
1113 * @return A C string containing the instance variable's type encoding.
1114 *
1115 * @note For possible values, see Objective-C Runtime Programming Guide > Type Encodings.
1116 */
1117 OBJC_EXPORT const char * _Nullable
1118 ivar_getTypeEncoding(Ivar _Nonnull v)
1119 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1120
1121 /**
1122 * Returns the offset of an instance variable.
1123 *
1124 * @param v The instance variable you want to enquire about.
1125 *
1126 * @return The offset of \e v.
1127 *
1128 * @note For instance variables of type \c id or other object types, call \c object_getIvar
1129 * and \c object_setIvar instead of using this offset to access the instance variable data directly.
1130 */
1131 OBJC_EXPORT ptrdiff_t
1132 ivar_getOffset(Ivar _Nonnull v)
1133 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1134
1135
1136 /* Working with Properties */
1137
1138 /**
1139 * Returns the name of a property.
1140 *
1141 * @param property The property you want to inquire about.
1142 *
1143 * @return A C string containing the property's name.
1144 */
1145 OBJC_EXPORT const char * _Nonnull
1146 property_getName(objc_property_t _Nonnull property)
1147 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1148
1149 /**
1150 * Returns the attribute string of a property.
1151 *
1152 * @param property A property.
1153 *
1154 * @return A C string containing the property's attributes.
1155 *
1156 * @note The format of the attribute string is described in Declared Properties in Objective-C Runtime Programming Guide.
1157 */
1158 OBJC_EXPORT const char * _Nullable
1159 property_getAttributes(objc_property_t _Nonnull property)
1160 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1161
1162 /**
1163 * Returns an array of property attributes for a property.
1164 *
1165 * @param property The property whose attributes you want copied.
1166 * @param outCount The number of attributes returned in the array.
1167 *
1168 * @return An array of property attributes; must be free'd() by the caller.
1169 */
1170 OBJC_EXPORT objc_property_attribute_t * _Nullable
1171 property_copyAttributeList(objc_property_t _Nonnull property,
1172 unsigned int * _Nullable outCount)
1173 OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
1174
1175 /**
1176 * Returns the value of a property attribute given the attribute name.
1177 *
1178 * @param property The property whose attribute value you are interested in.
1179 * @param attributeName C string representing the attribute name.
1180 *
1181 * @return The value string of the attribute \e attributeName if it exists in
1182 * \e property, \c nil otherwise.
1183 */
1184 OBJC_EXPORT char * _Nullable
1185 property_copyAttributeValue(objc_property_t _Nonnull property,
1186 const char * _Nonnull attributeName)
1187 OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
1188
1189
1190 /* Working with Protocols */
1191
1192 /**
1193 * Returns a specified protocol.
1194 *
1195 * @param name The name of a protocol.
1196 *
1197 * @return The protocol named \e name, or \c NULL if no protocol named \e name could be found.
1198 *
1199 * @note This function acquires the runtime lock.
1200 */
1201 OBJC_EXPORT Protocol * _Nullable
1202 objc_getProtocol(const char * _Nonnull name)
1203 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1204
1205 /**
1206 * Returns an array of all the protocols known to the runtime.
1207 *
1208 * @param outCount Upon return, contains the number of protocols in the returned array.
1209 *
1210 * @return A C array of all the protocols known to the runtime. The array contains \c *outCount
1211 * pointers followed by a \c NULL terminator. You must free the list with \c free().
1212 *
1213 * @note This function acquires the runtime lock.
1214 */
1215 OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable
1216 objc_copyProtocolList(unsigned int * _Nullable outCount)
1217 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1218
1219 /**
1220 * Returns a Boolean value that indicates whether one protocol conforms to another protocol.
1221 *
1222 * @param proto A protocol.
1223 * @param other A protocol.
1224 *
1225 * @return \c YES if \e proto conforms to \e other, otherwise \c NO.
1226 *
1227 * @note One protocol can incorporate other protocols using the same syntax
1228 * that classes use to adopt a protocol:
1229 * \code
1230 * @protocol ProtocolName < protocol list >
1231 * \endcode
1232 * All the protocols listed between angle brackets are considered part of the ProtocolName protocol.
1233 */
1234 OBJC_EXPORT BOOL
1235 protocol_conformsToProtocol(Protocol * _Nullable proto,
1236 Protocol * _Nullable other)
1237 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1238
1239 /**
1240 * Returns a Boolean value that indicates whether two protocols are equal.
1241 *
1242 * @param proto A protocol.
1243 * @param other A protocol.
1244 *
1245 * @return \c YES if \e proto is the same as \e other, otherwise \c NO.
1246 */
1247 OBJC_EXPORT BOOL
1248 protocol_isEqual(Protocol * _Nullable proto, Protocol * _Nullable other)
1249 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1250
1251 /**
1252 * Returns the name of a protocol.
1253 *
1254 * @param proto A protocol.
1255 *
1256 * @return The name of the protocol \e p as a C string.
1257 */
1258 OBJC_EXPORT const char * _Nonnull
1259 protocol_getName(Protocol * _Nonnull proto)
1260 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1261
1262 /**
1263 * Returns a method description structure for a specified method of a given protocol.
1264 *
1265 * @param proto A protocol.
1266 * @param aSel A selector.
1267 * @param isRequiredMethod A Boolean value that indicates whether aSel is a required method.
1268 * @param isInstanceMethod A Boolean value that indicates whether aSel is an instance method.
1269 *
1270 * @return An \c objc_method_description structure that describes the method specified by \e aSel,
1271 * \e isRequiredMethod, and \e isInstanceMethod for the protocol \e p.
1272 * If the protocol does not contain the specified method, returns an \c objc_method_description structure
1273 * with the value \c {NULL, \c NULL}.
1274 *
1275 * @note This function recursively searches any protocols that this protocol conforms to.
1276 */
1277 OBJC_EXPORT struct objc_method_description
1278 protocol_getMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull aSel,
1279 BOOL isRequiredMethod, BOOL isInstanceMethod)
1280 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1281
1282 /**
1283 * Returns an array of method descriptions of methods meeting a given specification for a given protocol.
1284 *
1285 * @param proto A protocol.
1286 * @param isRequiredMethod A Boolean value that indicates whether returned methods should
1287 * be required methods (pass YES to specify required methods).
1288 * @param isInstanceMethod A Boolean value that indicates whether returned methods should
1289 * be instance methods (pass YES to specify instance methods).
1290 * @param outCount Upon return, contains the number of method description structures in the returned array.
1291 *
1292 * @return A C array of \c objc_method_description structures containing the names and types of \e p's methods
1293 * specified by \e isRequiredMethod and \e isInstanceMethod. The array contains \c *outCount pointers followed
1294 * by a \c NULL terminator. You must free the list with \c free().
1295 * If the protocol declares no methods that meet the specification, \c NULL is returned and \c *outCount is 0.
1296 *
1297 * @note Methods in other protocols adopted by this protocol are not included.
1298 */
1299 OBJC_EXPORT struct objc_method_description * _Nullable
1300 protocol_copyMethodDescriptionList(Protocol * _Nonnull proto,
1301 BOOL isRequiredMethod,
1302 BOOL isInstanceMethod,
1303 unsigned int * _Nullable outCount)
1304 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1305
1306 /**
1307 * Returns the specified property of a given protocol.
1308 *
1309 * @param proto A protocol.
1310 * @param name The name of a property.
1311 * @param isRequiredProperty \c YES searches for a required property, \c NO searches for an optional property.
1312 * @param isInstanceProperty \c YES searches for an instance property, \c NO searches for a class property.
1313 *
1314 * @return The property specified by \e name, \e isRequiredProperty, and \e isInstanceProperty for \e proto,
1315 * or \c NULL if none of \e proto's properties meets the specification.
1316 */
1317 OBJC_EXPORT objc_property_t _Nullable
1318 protocol_getProperty(Protocol * _Nonnull proto,
1319 const char * _Nonnull name,
1320 BOOL isRequiredProperty, BOOL isInstanceProperty)
1321 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1322
1323 /**
1324 * Returns an array of the required instance properties declared by a protocol.
1325 *
1326 * @note Identical to
1327 * \code
1328 * protocol_copyPropertyList2(proto, outCount, YES, YES);
1329 * \endcode
1330 */
1331 OBJC_EXPORT objc_property_t _Nonnull * _Nullable
1332 protocol_copyPropertyList(Protocol * _Nonnull proto,
1333 unsigned int * _Nullable outCount)
1334 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1335
1336 /**
1337 * Returns an array of properties declared by a protocol.
1338 *
1339 * @param proto A protocol.
1340 * @param outCount Upon return, contains the number of elements in the returned array.
1341 * @param isRequiredProperty \c YES returns required properties, \c NO returns optional properties.
1342 * @param isInstanceProperty \c YES returns instance properties, \c NO returns class properties.
1343 *
1344 * @return A C array of pointers of type \c objc_property_t describing the properties declared by \e proto.
1345 * Any properties declared by other protocols adopted by this protocol are not included. The array contains
1346 * \c *outCount pointers followed by a \c NULL terminator. You must free the array with \c free().
1347 * If the protocol declares no matching properties, \c NULL is returned and \c *outCount is \c 0.
1348 */
1349 OBJC_EXPORT objc_property_t _Nonnull * _Nullable
1350 protocol_copyPropertyList2(Protocol * _Nonnull proto,
1351 unsigned int * _Nullable outCount,
1352 BOOL isRequiredProperty, BOOL isInstanceProperty)
1353 OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
1354
1355 /**
1356 * Returns an array of the protocols adopted by a protocol.
1357 *
1358 * @param proto A protocol.
1359 * @param outCount Upon return, contains the number of elements in the returned array.
1360 *
1361 * @return A C array of protocols adopted by \e proto. The array contains \e *outCount pointers
1362 * followed by a \c NULL terminator. You must free the array with \c free().
1363 * If the protocol adopts no other protocols, \c NULL is returned and \c *outCount is \c 0.
1364 */
1365 OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable
1366 protocol_copyProtocolList(Protocol * _Nonnull proto,
1367 unsigned int * _Nullable outCount)
1368 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1369
1370 /**
1371 * Creates a new protocol instance that cannot be used until registered with
1372 * \c objc_registerProtocol()
1373 *
1374 * @param name The name of the protocol to create.
1375 *
1376 * @return The Protocol instance on success, \c nil if a protocol
1377 * with the same name already exists.
1378 * @note There is no dispose method for this.
1379 */
1380 OBJC_EXPORT Protocol * _Nullable
1381 objc_allocateProtocol(const char * _Nonnull name)
1382 OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
1383
1384 /**
1385 * Registers a newly constructed protocol with the runtime. The protocol
1386 * will be ready for use and is immutable after this.
1387 *
1388 * @param proto The protocol you want to register.
1389 */
1390 OBJC_EXPORT void
1391 objc_registerProtocol(Protocol * _Nonnull proto)
1392 OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
1393
1394 /**
1395 * Adds a method to a protocol. The protocol must be under construction.
1396 *
1397 * @param proto The protocol to add a method to.
1398 * @param name The name of the method to add.
1399 * @param types A C string that represents the method signature.
1400 * @param isRequiredMethod YES if the method is not an optional method.
1401 * @param isInstanceMethod YES if the method is an instance method.
1402 */
1403 OBJC_EXPORT void
1404 protocol_addMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull name,
1405 const char * _Nullable types,
1406 BOOL isRequiredMethod, BOOL isInstanceMethod)
1407 OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
1408
1409 /**
1410 * Adds an incorporated protocol to another protocol. The protocol being
1411 * added to must still be under construction, while the additional protocol
1412 * must be already constructed.
1413 *
1414 * @param proto The protocol you want to add to, it must be under construction.
1415 * @param addition The protocol you want to incorporate into \e proto, it must be registered.
1416 */
1417 OBJC_EXPORT void
1418 protocol_addProtocol(Protocol * _Nonnull proto, Protocol * _Nonnull addition)
1419 OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
1420
1421 /**
1422 * Adds a property to a protocol. The protocol must be under construction.
1423 *
1424 * @param proto The protocol to add a property to.
1425 * @param name The name of the property.
1426 * @param attributes An array of property attributes.
1427 * @param attributeCount The number of attributes in \e attributes.
1428 * @param isRequiredProperty YES if the property (accessor methods) is not optional.
1429 * @param isInstanceProperty YES if the property (accessor methods) are instance methods.
1430 * This is the only case allowed fo a property, as a result, setting this to NO will
1431 * not add the property to the protocol at all.
1432 */
1433 OBJC_EXPORT void
1434 protocol_addProperty(Protocol * _Nonnull proto, const char * _Nonnull name,
1435 const objc_property_attribute_t * _Nullable attributes,
1436 unsigned int attributeCount,
1437 BOOL isRequiredProperty, BOOL isInstanceProperty)
1438 OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
1439
1440
1441 /* Working with Libraries */
1442
1443 /**
1444 * Returns the names of all the loaded Objective-C frameworks and dynamic
1445 * libraries.
1446 *
1447 * @param outCount The number of names returned.
1448 *
1449 * @return An array of C strings of names. Must be free()'d by caller.
1450 */
1451 OBJC_EXPORT const char * _Nonnull * _Nonnull
1452 objc_copyImageNames(unsigned int * _Nullable outCount)
1453 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1454
1455 /**
1456 * Returns the dynamic library name a class originated from.
1457 *
1458 * @param cls The class you are inquiring about.
1459 *
1460 * @return The name of the library containing this class.
1461 */
1462 OBJC_EXPORT const char * _Nullable
1463 class_getImageName(Class _Nullable cls)
1464 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1465
1466 /**
1467 * Returns the names of all the classes within a library.
1468 *
1469 * @param image The library or framework you are inquiring about.
1470 * @param outCount The number of class names returned.
1471 *
1472 * @return An array of C strings representing the class names.
1473 */
1474 OBJC_EXPORT const char * _Nonnull * _Nullable
1475 objc_copyClassNamesForImage(const char * _Nonnull image,
1476 unsigned int * _Nullable outCount)
1477 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1478
1479
1480 /* Working with Selectors */
1481
1482 /**
1483 * Returns the name of the method specified by a given selector.
1484 *
1485 * @param sel A pointer of type \c SEL. Pass the selector whose name you wish to determine.
1486 *
1487 * @return A C string indicating the name of the selector.
1488 */
1489 OBJC_EXPORT const char * _Nonnull
1490 sel_getName(SEL _Nonnull sel)
1491 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
1492
1493
1494 /**
1495 * Registers a method with the Objective-C runtime system, maps the method
1496 * name to a selector, and returns the selector value.
1497 *
1498 * @param str A pointer to a C string. Pass the name of the method you wish to register.
1499 *
1500 * @return A pointer of type SEL specifying the selector for the named method.
1501 *
1502 * @note You must register a method name with the Objective-C runtime system to obtain the
1503 * method’s selector before you can add the method to a class definition. If the method name
1504 * has already been registered, this function simply returns the selector.
1505 */
1506 OBJC_EXPORT SEL _Nonnull
1507 sel_registerName(const char * _Nonnull str)
1508 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
1509
1510 /**
1511 * Returns a Boolean value that indicates whether two selectors are equal.
1512 *
1513 * @param lhs The selector to compare with rhs.
1514 * @param rhs The selector to compare with lhs.
1515 *
1516 * @return \c YES if \e lhs and \e rhs are equal, otherwise \c NO.
1517 *
1518 * @note sel_isEqual is equivalent to ==.
1519 */
1520 OBJC_EXPORT BOOL
1521 sel_isEqual(SEL _Nonnull lhs, SEL _Nonnull rhs)
1522 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1523
1524
1525 /* Objective-C Language Features */
1526
1527 /**
1528 * This function is inserted by the compiler when a mutation
1529 * is detected during a foreach iteration. It gets called
1530 * when a mutation occurs, and the enumerationMutationHandler
1531 * is enacted if it is set up. A fatal error occurs if a handler is not set up.
1532 *
1533 * @param obj The object being mutated.
1534 *
1535 */
1536 OBJC_EXPORT void
1537 objc_enumerationMutation(id _Nonnull obj)
1538 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1539
1540 /**
1541 * Sets the current mutation handler.
1542 *
1543 * @param handler Function pointer to the new mutation handler.
1544 */
1545 OBJC_EXPORT void
1546 objc_setEnumerationMutationHandler(void (*_Nullable handler)(id _Nonnull ))
1547 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1548
1549 /**
1550 * Set the function to be called by objc_msgForward.
1551 *
1552 * @param fwd Function to be jumped to by objc_msgForward.
1553 * @param fwd_stret Function to be jumped to by objc_msgForward_stret.
1554 *
1555 * @see message.h::_objc_msgForward
1556 */
1557 OBJC_EXPORT void
1558 objc_setForwardHandler(void * _Nonnull fwd, void * _Nonnull fwd_stret)
1559 OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
1560
1561 /**
1562 * Creates a pointer to a function that will call the block
1563 * when the method is called.
1564 *
1565 * @param block The block that implements this method. Its signature should
1566 * be: method_return_type ^(id self, method_args...).
1567 * The selector is not available as a parameter to this block.
1568 * The block is copied with \c Block_copy().
1569 *
1570 * @return The IMP that calls this block. Must be disposed of with
1571 * \c imp_removeBlock.
1572 */
1573 OBJC_EXPORT IMP _Nonnull
1574 imp_implementationWithBlock(id _Nonnull block)
1575 OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
1576
1577 /**
1578 * Return the block associated with an IMP that was created using
1579 * \c imp_implementationWithBlock.
1580 *
1581 * @param anImp The IMP that calls this block.
1582 *
1583 * @return The block called by \e anImp.
1584 */
1585 OBJC_EXPORT id _Nullable
1586 imp_getBlock(IMP _Nonnull anImp)
1587 OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
1588
1589 /**
1590 * Disassociates a block from an IMP that was created using
1591 * \c imp_implementationWithBlock and releases the copy of the
1592 * block that was created.
1593 *
1594 * @param anImp An IMP that was created using \c imp_implementationWithBlock.
1595 *
1596 * @return YES if the block was released successfully, NO otherwise.
1597 * (For example, the block might not have been used to create an IMP previously).
1598 */
1599 OBJC_EXPORT BOOL
1600 imp_removeBlock(IMP _Nonnull anImp)
1601 OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
1602
1603 /**
1604 * This loads the object referenced by a weak pointer and returns it, after
1605 * retaining and autoreleasing the object to ensure that it stays alive
1606 * long enough for the caller to use it. This function would be used
1607 * anywhere a __weak variable is used in an expression.
1608 *
1609 * @param location The weak pointer address
1610 *
1611 * @return The object pointed to by \e location, or \c nil if \e *location is \c nil.
1612 */
1613 OBJC_EXPORT id _Nullable
1614 objc_loadWeak(id _Nullable * _Nonnull location)
1615 OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0);
1616
1617 /**
1618 * This function stores a new value into a __weak variable. It would
1619 * be used anywhere a __weak variable is the target of an assignment.
1620 *
1621 * @param location The address of the weak pointer itself
1622 * @param obj The new object this weak ptr should now point to
1623 *
1624 * @return The value stored into \e location, i.e. \e obj
1625 */
1626 OBJC_EXPORT id _Nullable
1627 objc_storeWeak(id _Nullable * _Nonnull location, id _Nullable obj)
1628 OBJC_AVAILABLE(10.7, 5.0, 9.0, 1.0, 2.0);
1629
1630
1631 /* Associative References */
1632
1633 /**
1634 * Policies related to associative references.
1635 * These are options to objc_setAssociatedObject()
1636 */
1637 typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {
1638 OBJC_ASSOCIATION_ASSIGN = 0, /**< Specifies a weak reference to the associated object. */
1639 OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object.
1640 * The association is not made atomically. */
1641 OBJC_ASSOCIATION_COPY_NONATOMIC = 3, /**< Specifies that the associated object is copied.
1642 * The association is not made atomically. */
1643 OBJC_ASSOCIATION_RETAIN = 01401, /**< Specifies a strong reference to the associated object.
1644 * The association is made atomically. */
1645 OBJC_ASSOCIATION_COPY = 01403 /**< Specifies that the associated object is copied.
1646 * The association is made atomically. */
1647 };
1648
1649 /**
1650 * Sets an associated value for a given object using a given key and association policy.
1651 *
1652 * @param object The source object for the association.
1653 * @param key The key for the association.
1654 * @param value The value to associate with the key key for object. Pass nil to clear an existing association.
1655 * @param policy The policy for the association. For possible values, see “Associative Object Behaviors.”
1656 *
1657 * @see objc_setAssociatedObject
1658 * @see objc_removeAssociatedObjects
1659 */
1660 OBJC_EXPORT void
1661 objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key,
1662 id _Nullable value, objc_AssociationPolicy policy)
1663 OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);
1664
1665 /**
1666 * Returns the value associated with a given object for a given key.
1667 *
1668 * @param object The source object for the association.
1669 * @param key The key for the association.
1670 *
1671 * @return The value associated with the key \e key for \e object.
1672 *
1673 * @see objc_setAssociatedObject
1674 */
1675 OBJC_EXPORT id _Nullable
1676 objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key)
1677 OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);
1678
1679 /**
1680 * Removes all associations for a given object.
1681 *
1682 * @param object An object that maintains associated objects.
1683 *
1684 * @note The main purpose of this function is to make it easy to return an object
1685 * to a "pristine state”. You should not use this function for general removal of
1686 * associations from objects, since it also removes associations that other clients
1687 * may have added to the object. Typically you should use \c objc_setAssociatedObject
1688 * with a nil value to clear an association.
1689 *
1690 * @see objc_setAssociatedObject
1691 * @see objc_getAssociatedObject
1692 */
1693 OBJC_EXPORT void
1694 objc_removeAssociatedObjects(id _Nonnull object)
1695 OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);
1696
1697
1698 /* Hooks for Swift */
1699
1700 /**
1701 * Function type for a hook that intercepts class_getImageName().
1702 *
1703 * @param cls The class whose image name is being looked up.
1704 * @param outImageName On return, the result of the image name lookup.
1705 * @return YES if an image name for this class was found, NO otherwise.
1706 *
1707 * @see class_getImageName
1708 * @see objc_setHook_getImageName
1709 */
1710 typedef BOOL (*objc_hook_getImageName)(Class _Nonnull cls, const char * _Nullable * _Nonnull outImageName);
1711
1712 /**
1713 * Install a hook for class_getImageName().
1714 *
1715 * @param newValue The hook function to install.
1716 * @param outOldValue The address of a function pointer variable. On return,
1717 * the old hook function is stored in the variable.
1718 *
1719 * @note The store to *outOldValue is thread-safe: the variable will be
1720 * updated before class_getImageName() calls your new hook to read it,
1721 * even if your new hook is called from another thread before this
1722 * setter completes.
1723 * @note The first hook in the chain is the native implementation of
1724 * class_getImageName(). Your hook should call the previous hook for
1725 * classes that you do not recognize.
1726 *
1727 * @see class_getImageName
1728 * @see objc_hook_getImageName
1729 */
1730 OBJC_EXPORT void objc_setHook_getImageName(objc_hook_getImageName _Nonnull newValue,
1731 objc_hook_getImageName _Nullable * _Nonnull outOldValue)
1732 OBJC_AVAILABLE(10.14, 12.0, 12.0, 5.0, 3.0);
1733
1734 /**
1735 * Function type for a hook that assists objc_getClass() and related functions.
1736 *
1737 * @param name The class name to look up.
1738 * @param outClass On return, the result of the class lookup.
1739 * @return YES if a class with this name was found, NO otherwise.
1740 *
1741 * @see objc_getClass
1742 * @see objc_setHook_getClass
1743 */
1744 typedef BOOL (*objc_hook_getClass)(const char * _Nonnull name, Class _Nullable * _Nonnull outClass);
1745
1746 /**
1747 * Install a hook for objc_getClass() and related functions.
1748 *
1749 * @param newValue The hook function to install.
1750 * @param outOldValue The address of a function pointer variable. On return,
1751 * the old hook function is stored in the variable.
1752 *
1753 * @note The store to *outOldValue is thread-safe: the variable will be
1754 * updated before objc_getClass() calls your new hook to read it,
1755 * even if your new hook is called from another thread before this
1756 * setter completes.
1757 * @note Your hook should call the previous hook for class names
1758 * that you do not recognize.
1759 *
1760 * @see objc_getClass
1761 * @see objc_hook_getClass
1762 */
1763 #if !(TARGET_OS_OSX && __i386__)
1764 #define OBJC_GETCLASSHOOK_DEFINED 1
1765 OBJC_EXPORT void objc_setHook_getClass(objc_hook_getClass _Nonnull newValue,
1766 objc_hook_getClass _Nullable * _Nonnull outOldValue)
1767 OBJC_AVAILABLE(10.14.4, 12.2, 12.2, 5.2, 3.2);
1768 #endif
1769
1770 /**
1771 * Function type for a function that is called when an image is loaded.
1772 *
1773 * @param header The newly loaded header.
1774 */
1775 struct mach_header;
1776 typedef void (*objc_func_loadImage)(const struct mach_header * _Nonnull header);
1777
1778 /**
1779 * Add a function to be called when a new image is loaded. The function is
1780 * called after ObjC has scanned and fixed up the image. It is called
1781 * BEFORE +load methods are invoked.
1782 *
1783 * When adding a new function, that function is immediately called with all
1784 * images that are currently loaded. It is then called as needed for images
1785 * that are loaded afterwards.
1786 *
1787 * Note: the function is called with ObjC's internal runtime lock held.
1788 * Be VERY careful with what the function does to avoid deadlocks or
1789 * poor performance.
1790 *
1791 * @param func The function to add.
1792 */
1793 #define OBJC_ADDLOADIMAGEFUNC_DEFINED 1
1794 OBJC_EXPORT void objc_addLoadImageFunc(objc_func_loadImage _Nonnull func)
1795 OBJC_AVAILABLE(10.15, 13.0, 13.0, 6.0, 4.0);
1796
1797 /**
1798 * Function type for a hook that provides a name for lazily named classes.
1799 *
1800 * @param cls The class to generate a name for.
1801 * @return The name of the class, or NULL if the name isn't known or can't me generated.
1802 *
1803 * @see objc_setHook_lazyClassNamer
1804 */
1805 typedef const char * _Nullable (*objc_hook_lazyClassNamer)(_Nonnull Class cls);
1806
1807 /**
1808 * Install a hook to provide a name for lazily-named classes.
1809 *
1810 * @param newValue The hook function to install.
1811 * @param outOldValue The address of a function pointer variable. On return,
1812 * the old hook function is stored in the variable.
1813 *
1814 * @note The store to *outOldValue is thread-safe: the variable will be
1815 * updated before objc_getClass() calls your new hook to read it,
1816 * even if your new hook is called from another thread before this
1817 * setter completes.
1818 * @note Your hook must call the previous hook for class names
1819 * that you do not recognize.
1820 */
1821 #if !(TARGET_OS_OSX && __i386__)
1822 #define OBJC_SETHOOK_LAZYCLASSNAMER_DEFINED 1
1823 OBJC_EXPORT
1824 void objc_setHook_lazyClassNamer(_Nonnull objc_hook_lazyClassNamer newValue,
1825 _Nonnull objc_hook_lazyClassNamer * _Nonnull oldOutValue)
1826 OBJC_AVAILABLE(10.16, 14.0, 14.0, 7.0, 5.0);
1827 #endif
1828
1829 /**
1830 * Callback from Objective-C to Swift to perform Swift class initialization.
1831 */
1832 #if !(TARGET_OS_OSX && __i386__)
1833 typedef Class _Nullable
1834 (*_objc_swiftMetadataInitializer)(Class _Nonnull cls, void * _Nullable arg);
1835 #endif
1836
1837
1838 /**
1839 * Perform Objective-C initialization of a Swift class.
1840 * Do not call this function. It is provided for the Swift runtime's use only
1841 * and will change without notice or mercy.
1842 */
1843 #if !(TARGET_OS_OSX && __i386__)
1844 #define OBJC_REALIZECLASSFROMSWIFT_DEFINED 1
1845 OBJC_EXPORT Class _Nullable
1846 _objc_realizeClassFromSwift(Class _Nullable cls, void * _Nullable previously)
1847 OBJC_AVAILABLE(10.14.4, 12.2, 12.2, 5.2, 3.2);
1848 #endif
1849
1850
1851 #define _C_ID '@'
1852 #define _C_CLASS '#'
1853 #define _C_SEL ':'
1854 #define _C_CHR 'c'
1855 #define _C_UCHR 'C'
1856 #define _C_SHT 's'
1857 #define _C_USHT 'S'
1858 #define _C_INT 'i'
1859 #define _C_UINT 'I'
1860 #define _C_LNG 'l'
1861 #define _C_ULNG 'L'
1862 #define _C_LNG_LNG 'q'
1863 #define _C_ULNG_LNG 'Q'
1864 #define _C_FLT 'f'
1865 #define _C_DBL 'd'
1866 #define _C_BFLD 'b'
1867 #define _C_BOOL 'B'
1868 #define _C_VOID 'v'
1869 #define _C_UNDEF '?'
1870 #define _C_PTR '^'
1871 #define _C_CHARPTR '*'
1872 #define _C_ATOM '%'
1873 #define _C_ARY_B '['
1874 #define _C_ARY_E ']'
1875 #define _C_UNION_B '('
1876 #define _C_UNION_E ')'
1877 #define _C_STRUCT_B '{'
1878 #define _C_STRUCT_E '}'
1879 #define _C_VECTOR '!'
1880 #define _C_CONST 'r'
1881
1882
1883 /* Obsolete types */
1884
1885 #if !__OBJC2__
1886
1887 #define CLS_GETINFO(cls,infomask) ((cls)->info & (infomask))
1888 #define CLS_SETINFO(cls,infomask) ((cls)->info |= (infomask))
1889
1890 // class is not a metaclass
1891 #define CLS_CLASS 0x1
1892 // class is a metaclass
1893 #define CLS_META 0x2
1894 // class's +initialize method has completed
1895 #define CLS_INITIALIZED 0x4
1896 // class is posing
1897 #define CLS_POSING 0x8
1898 // unused
1899 #define CLS_MAPPED 0x10
1900 // class and subclasses need cache flush during image loading
1901 #define CLS_FLUSH_CACHE 0x20
1902 // method cache should grow when full
1903 #define CLS_GROW_CACHE 0x40
1904 // unused
1905 #define CLS_NEED_BIND 0x80
1906 // methodLists is array of method lists
1907 #define CLS_METHOD_ARRAY 0x100
1908 // the JavaBridge constructs classes with these markers
1909 #define CLS_JAVA_HYBRID 0x200
1910 #define CLS_JAVA_CLASS 0x400
1911 // thread-safe +initialize
1912 #define CLS_INITIALIZING 0x800
1913 // bundle unloading
1914 #define CLS_FROM_BUNDLE 0x1000
1915 // C++ ivar support
1916 #define CLS_HAS_CXX_STRUCTORS 0x2000
1917 // Lazy method list arrays
1918 #define CLS_NO_METHOD_ARRAY 0x4000
1919 // +load implementation
1920 #define CLS_HAS_LOAD_METHOD 0x8000
1921 // objc_allocateClassPair API
1922 #define CLS_CONSTRUCTING 0x10000
1923 // class compiled with bigger class structure
1924 #define CLS_EXT 0x20000
1925
1926
1927 struct objc_method_description_list {
1928 int count;
1929 struct objc_method_description list[1];
1930 };
1931
1932
1933 struct objc_protocol_list {
1934 struct objc_protocol_list * _Nullable next;
1935 long count;
1936 __unsafe_unretained Protocol * _Nullable list[1];
1937 };
1938
1939
1940 struct objc_category {
1941 char * _Nonnull category_name OBJC2_UNAVAILABLE;
1942 char * _Nonnull class_name OBJC2_UNAVAILABLE;
1943 struct objc_method_list * _Nullable instance_methods OBJC2_UNAVAILABLE;
1944 struct objc_method_list * _Nullable class_methods OBJC2_UNAVAILABLE;
1945 struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE;
1946 } OBJC2_UNAVAILABLE;
1947
1948
1949 struct objc_ivar {
1950 char * _Nullable ivar_name OBJC2_UNAVAILABLE;
1951 char * _Nullable ivar_type OBJC2_UNAVAILABLE;
1952 int ivar_offset OBJC2_UNAVAILABLE;
1953 #ifdef __LP64__
1954 int space OBJC2_UNAVAILABLE;
1955 #endif
1956 } OBJC2_UNAVAILABLE;
1957
1958 struct objc_ivar_list {
1959 int ivar_count OBJC2_UNAVAILABLE;
1960 #ifdef __LP64__
1961 int space OBJC2_UNAVAILABLE;
1962 #endif
1963 /* variable length structure */
1964 struct objc_ivar ivar_list[1] OBJC2_UNAVAILABLE;
1965 } OBJC2_UNAVAILABLE;
1966
1967
1968 struct objc_method {
1969 SEL _Nonnull method_name OBJC2_UNAVAILABLE;
1970 char * _Nullable method_types OBJC2_UNAVAILABLE;
1971 IMP _Nonnull method_imp OBJC2_UNAVAILABLE;
1972 } OBJC2_UNAVAILABLE;
1973
1974 struct objc_method_list {
1975 struct objc_method_list * _Nullable obsolete OBJC2_UNAVAILABLE;
1976
1977 int method_count OBJC2_UNAVAILABLE;
1978 #ifdef __LP64__
1979 int space OBJC2_UNAVAILABLE;
1980 #endif
1981 /* variable length structure */
1982 struct objc_method method_list[1] OBJC2_UNAVAILABLE;
1983 } OBJC2_UNAVAILABLE;
1984
1985
1986 typedef struct objc_symtab *Symtab OBJC2_UNAVAILABLE;
1987
1988 struct objc_symtab {
1989 unsigned long sel_ref_cnt OBJC2_UNAVAILABLE;
1990 SEL _Nonnull * _Nullable refs OBJC2_UNAVAILABLE;
1991 unsigned short cls_def_cnt OBJC2_UNAVAILABLE;
1992 unsigned short cat_def_cnt OBJC2_UNAVAILABLE;
1993 void * _Nullable defs[1] /* variable size */ OBJC2_UNAVAILABLE;
1994 } OBJC2_UNAVAILABLE;
1995
1996
1997 typedef struct objc_cache *Cache OBJC2_UNAVAILABLE;
1998
1999 #define CACHE_BUCKET_NAME(B) ((B)->method_name)
2000 #define CACHE_BUCKET_IMP(B) ((B)->method_imp)
2001 #define CACHE_BUCKET_VALID(B) (B)
2002 #ifndef __LP64__
2003 #define CACHE_HASH(sel, mask) (((uintptr_t)(sel)>>2) & (mask))
2004 #else
2005 #define CACHE_HASH(sel, mask) (((unsigned int)((uintptr_t)(sel)>>3)) & (mask))
2006 #endif
2007 struct objc_cache {
2008 unsigned int mask /* total = mask + 1 */ OBJC2_UNAVAILABLE;
2009 unsigned int occupied OBJC2_UNAVAILABLE;
2010 Method _Nullable buckets[1] OBJC2_UNAVAILABLE;
2011 };
2012
2013
2014 typedef struct objc_module *Module OBJC2_UNAVAILABLE;
2015
2016 struct objc_module {
2017 unsigned long version OBJC2_UNAVAILABLE;
2018 unsigned long size OBJC2_UNAVAILABLE;
2019 const char * _Nullable name OBJC2_UNAVAILABLE;
2020 Symtab _Nullable symtab OBJC2_UNAVAILABLE;
2021 } OBJC2_UNAVAILABLE;
2022
2023 #else
2024
2025 struct objc_method_list;
2026
2027 #endif
2028
2029
2030 /* Obsolete functions */
2031
2032 OBJC_EXPORT IMP _Nullable
2033 class_lookupMethod(Class _Nullable cls, SEL _Nonnull sel)
2034 __OSX_DEPRECATED(10.0, 10.5, "use class_getMethodImplementation instead")
2035 __IOS_DEPRECATED(2.0, 2.0, "use class_getMethodImplementation instead")
2036 __TVOS_DEPRECATED(9.0, 9.0, "use class_getMethodImplementation instead")
2037 __WATCHOS_DEPRECATED(1.0, 1.0, "use class_getMethodImplementation instead")
2038 #ifndef __APPLE_BLEACH_SDK__
2039 __BRIDGEOS_DEPRECATED(2.0, 2.0, "use class_getMethodImplementation instead")
2040 #endif
2041 ;
2042 OBJC_EXPORT BOOL
2043 class_respondsToMethod(Class _Nullable cls, SEL _Nonnull sel)
2044 __OSX_DEPRECATED(10.0, 10.5, "use class_respondsToSelector instead")
2045 __IOS_DEPRECATED(2.0, 2.0, "use class_respondsToSelector instead")
2046 __TVOS_DEPRECATED(9.0, 9.0, "use class_respondsToSelector instead")
2047 __WATCHOS_DEPRECATED(1.0, 1.0, "use class_respondsToSelector instead")
2048 #ifndef __APPLE_BLEACH_SDK__
2049 __BRIDGEOS_DEPRECATED(2.0, 2.0, "use class_respondsToSelector instead")
2050 #endif
2051 ;
2052
2053 OBJC_EXPORT void
2054 _objc_flush_caches(Class _Nullable cls)
2055 __OSX_DEPRECATED(10.0, 10.5, "not recommended")
2056 __IOS_DEPRECATED(2.0, 2.0, "not recommended")
2057 __TVOS_DEPRECATED(9.0, 9.0, "not recommended")
2058 __WATCHOS_DEPRECATED(1.0, 1.0, "not recommended")
2059 #ifndef __APPLE_BLEACH_SDK__
2060 __BRIDGEOS_DEPRECATED(2.0, 2.0, "not recommended")
2061 #endif
2062 ;
2063
2064 OBJC_EXPORT id _Nullable
2065 object_copyFromZone(id _Nullable anObject, size_t nBytes, void * _Nullable z)
2066 OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.0, 10.5, "use object_copy instead");
2067
2068 OBJC_EXPORT id _Nullable
2069 object_realloc(id _Nullable anObject, size_t nBytes)
2070 OBJC2_UNAVAILABLE;
2071
2072 OBJC_EXPORT id _Nullable
2073 object_reallocFromZone(id _Nullable anObject, size_t nBytes, void * _Nullable z)
2074 OBJC2_UNAVAILABLE;
2075
2076 #define OBSOLETE_OBJC_GETCLASSES 1
2077 OBJC_EXPORT void * _Nonnull
2078 objc_getClasses(void)
2079 OBJC2_UNAVAILABLE;
2080
2081 OBJC_EXPORT void
2082 objc_addClass(Class _Nonnull myClass)
2083 OBJC2_UNAVAILABLE;
2084
2085 OBJC_EXPORT void
2086 objc_setClassHandler(int (* _Nullable )(const char * _Nonnull))
2087 OBJC2_UNAVAILABLE;
2088
2089 OBJC_EXPORT void
2090 objc_setMultithreaded(BOOL flag)
2091 OBJC2_UNAVAILABLE;
2092
2093 OBJC_EXPORT id _Nullable
2094 class_createInstanceFromZone(Class _Nullable, size_t idxIvars,
2095 void * _Nullable z)
2096 OBJC_OSX_DEPRECATED_OTHERS_UNAVAILABLE(10.0, 10.5, "use class_createInstance instead");
2097
2098 OBJC_EXPORT void
2099 class_addMethods(Class _Nullable, struct objc_method_list * _Nonnull)
2100 OBJC2_UNAVAILABLE;
2101
2102 OBJC_EXPORT void
2103 class_removeMethods(Class _Nullable, struct objc_method_list * _Nonnull)
2104 OBJC2_UNAVAILABLE;
2105
2106 OBJC_EXPORT void
2107 _objc_resolve_categories_for_class(Class _Nonnull cls)
2108 OBJC2_UNAVAILABLE;
2109
2110 OBJC_EXPORT Class _Nonnull
2111 class_poseAs(Class _Nonnull imposter, Class _Nonnull original)
2112 OBJC2_UNAVAILABLE;
2113
2114 OBJC_EXPORT unsigned int
2115 method_getSizeOfArguments(Method _Nonnull m)
2116 OBJC2_UNAVAILABLE;
2117
2118 OBJC_EXPORT unsigned
2119 method_getArgumentInfo(struct objc_method * _Nonnull m, int arg,
2120 const char * _Nullable * _Nonnull type,
2121 int * _Nonnull offset)
2122 UNAVAILABLE_ATTRIBUTE // This function was accidentally deleted in 10.9.
2123 OBJC2_UNAVAILABLE;
2124
2125 OBJC_EXPORT Class _Nullable
2126 objc_getOrigClass(const char * _Nonnull name)
2127 OBJC2_UNAVAILABLE;
2128
2129 #define OBJC_NEXT_METHOD_LIST 1
2130 OBJC_EXPORT struct objc_method_list * _Nullable
2131 class_nextMethodList(Class _Nullable, void * _Nullable * _Nullable)
2132 OBJC2_UNAVAILABLE;
2133 // usage for nextMethodList
2134 //
2135 // void *iterator = 0;
2136 // struct objc_method_list *mlist;
2137 // while ( mlist = class_nextMethodList( cls, &iterator ) )
2138 // ;
2139
2140 OBJC_EXPORT id _Nullable
2141 (* _Nonnull _alloc)(Class _Nullable, size_t)
2142 OBJC2_UNAVAILABLE;
2143
2144 OBJC_EXPORT id _Nullable
2145 (* _Nonnull _copy)(id _Nullable, size_t)
2146 OBJC2_UNAVAILABLE;
2147
2148 OBJC_EXPORT id _Nullable
2149 (* _Nonnull _realloc)(id _Nullable, size_t)
2150 OBJC2_UNAVAILABLE;
2151
2152 OBJC_EXPORT id _Nullable
2153 (* _Nonnull _dealloc)(id _Nullable)
2154 OBJC2_UNAVAILABLE;
2155
2156 OBJC_EXPORT id _Nullable
2157 (* _Nonnull _zoneAlloc)(Class _Nullable, size_t, void * _Nullable)
2158 OBJC2_UNAVAILABLE;
2159
2160 OBJC_EXPORT id _Nullable
2161 (* _Nonnull _zoneRealloc)(id _Nullable, size_t, void * _Nullable)
2162 OBJC2_UNAVAILABLE;
2163
2164 OBJC_EXPORT id _Nullable
2165 (* _Nonnull _zoneCopy)(id _Nullable, size_t, void * _Nullable)
2166 OBJC2_UNAVAILABLE;
2167
2168 OBJC_EXPORT void
2169 (* _Nonnull _error)(id _Nullable, const char * _Nonnull, va_list)
2170 OBJC2_UNAVAILABLE;
2171
2172 #endif