In libobjc/: 2010-12-15 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / libobjc / objc / runtime.h
blob94f5bf7f10aea8fc83d16d840debdcb9550b8722
1 /* GNU Objective-C Runtime API - Modern API
2 Copyright (C) 2010 Free Software Foundation, Inc.
3 Contributed by Nicola Pero <nicola.pero@meta-innovation.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #ifndef __objc_runtime_INCLUDE_GNU
27 #define __objc_runtime_INCLUDE_GNU
30 This file declares the "modern" GNU Objective-C Runtime API.
31 Include this file to use it.
33 This API is replacing the "traditional" GNU Objective-C Runtime API
34 (declared in objc/objc-api.h) which is the one supported by older
35 versions of the GNU Objective-C Runtime. The "modern" API is very
36 similar to the API used by the modern Apple/NeXT runtime.
38 Because the two APIs have some conflicting definitions (in
39 particular, Method and Category are defined differently) you should
40 include either objc/objc-api.h (to use the traditional GNU
41 Objective-C Runtime API) or objc/runtime.h (to use the modern GNU
42 Objective-C Runtime API), but not both.
44 #ifdef __objc_api_INCLUDE_GNU
45 # error You can not include both objc/objc-api.h and objc/runtime.h. Include objc/objc-api.h for the traditional GNU Objective-C Runtime API and objc/runtime.h for the modern one.
46 #endif
48 /* TODO: This file is incomplete. */
50 #include "objc.h"
51 #include "objc-decls.h"
53 #ifdef __cplusplus
54 extern "C" {
55 #endif /* __cplusplus */
57 /* An 'Ivar' represents an instance variable. It holds information
58 about the name, type and offset of the instance variable. */
59 typedef struct objc_ivar *Ivar;
61 /* A 'Property' represents a property. It holds information about the
62 name of the property, and its attributes.
64 Compatibility Note: the Apple/NeXT runtime defines this as
65 objc_property_t, so we define it that way as well, but obviously
66 Property is the right name. */
67 typedef struct objc_property *Property;
68 typedef struct objc_property *objc_property_t;
70 /* A 'Method' represents a method. It holds information about the
71 name, types and the IMP of the method. */
72 typedef struct objc_method *Method;
74 /* A 'Category' represents a category. It holds information about the
75 name of the category, the class it belongs to, and the methods,
76 protocols and such like provided by the category. */
77 typedef struct objc_category *Category;
79 /* 'Protocol' is defined in objc/objc.h (which is included by this
80 file). */
82 /* Method descriptor returned by introspective Object methods. At the
83 moment, this is really just the first part of the more complete
84 objc_method structure used internally by the runtime. (PS: In the
85 GNU Objective-C Runtime, selectors already include a type, so an
86 objc_method_description does not add much to a SEL. But in other
87 runtimes, that is not the case, which is why
88 objc_method_description exists). */
89 struct objc_method_description
91 SEL name; /* Selector (name and signature) */
92 char *types; /* Type encoding */
95 /* The following are used in encode strings to describe the type of
96 Ivars and Methods. */
97 #define _C_ID '@'
98 #define _C_CLASS '#'
99 #define _C_SEL ':'
100 #define _C_CHR 'c'
101 #define _C_UCHR 'C'
102 #define _C_SHT 's'
103 #define _C_USHT 'S'
104 #define _C_INT 'i'
105 #define _C_UINT 'I'
106 #define _C_LNG 'l'
107 #define _C_ULNG 'L'
108 #define _C_LNG_LNG 'q'
109 #define _C_ULNG_LNG 'Q'
110 #define _C_FLT 'f'
111 #define _C_DBL 'd'
112 #define _C_LNG_DBL 'D'
113 #define _C_BFLD 'b'
114 #define _C_BOOL 'B'
115 #define _C_VOID 'v'
116 #define _C_UNDEF '?'
117 #define _C_PTR '^'
118 #define _C_CHARPTR '*'
119 #define _C_ARY_B '['
120 #define _C_ARY_E ']'
121 #define _C_UNION_B '('
122 #define _C_UNION_E ')'
123 #define _C_STRUCT_B '{'
124 #define _C_STRUCT_E '}'
125 #define _C_VECTOR '!'
126 #define _C_COMPLEX 'j'
128 /* _C_ATOM is never generated by the compiler. You can treat it as
129 equivalent to "*". */
130 #define _C_ATOM '%'
132 /* The following are used in encode strings to describe some
133 qualifiers of method and ivar types. */
134 #define _C_CONST 'r'
135 #define _C_IN 'n'
136 #define _C_INOUT 'N'
137 #define _C_OUT 'o'
138 #define _C_BYCOPY 'O'
139 #define _C_BYREF 'R'
140 #define _C_ONEWAY 'V'
141 #define _C_GCINVISIBLE '|'
143 /* The same when used as flags. */
144 #define _F_CONST 0x01
145 #define _F_IN 0x01
146 #define _F_OUT 0x02
147 #define _F_INOUT 0x03
148 #define _F_BYCOPY 0x04
149 #define _F_BYREF 0x08
150 #define _F_ONEWAY 0x10
151 #define _F_GCINVISIBLE 0x20
154 /** Implementation: the following functions are defined inline. */
156 /* Return the class of 'object', or Nil if the object is nil. If
157 'object' is a class, the meta class is returned; if 'object' is a
158 meta class, the root meta class is returned (note that this is
159 different from the traditional GNU Objective-C Runtime API function
160 object_get_class(), which for a meta class would return the meta
161 class itself). This function is inline, so it is really fast and
162 should be used instead of accessing object->class_pointer
163 directly. */
164 static inline Class
165 object_getClass (id object)
167 if (object != nil)
168 return object->class_pointer;
169 else
170 return Nil;
174 /** Implementation: the following functions are in selector.c. */
176 /* Return the name of a given selector. If 'selector' is NULL, return
177 "<null selector>". */
178 objc_EXPORT const char *sel_getName (SEL selector);
180 /* Return the type of a given selector.
182 Compatibility Note: the Apple/NeXT runtime has untyped selectors,
183 so it does not have this function, which is specific to the GNU
184 Runtime. */
185 objc_EXPORT const char *sel_getType (SEL selector);
187 /* This is the same as sel_registerName (). Please use
188 sel_registerName () instead. */
189 objc_EXPORT SEL sel_getUid (const char *name);
191 /* Register a selector with a given name (but unspecified types). If
192 you know the types, it is better to call sel_registerTypedName().
193 If a selector with this name already exists, it is returned. */
194 objc_EXPORT SEL sel_registerName (const char *name);
196 /* Register a selector with a given name and types. If a selector
197 with this name and types already exists, it is returned.
199 Compatibility Note: the Apple/NeXT runtime has untyped selectors,
200 so it does not have this function, which is specific to the GNU
201 Runtime. */
202 objc_EXPORT SEL sel_registerTypedName (const char *name, const char *type);
204 /* Return YES if first_selector is the same as second_selector, and NO
205 if not. */
206 objc_EXPORT BOOL sel_isEqual (SEL first_selector, SEL second_selector);
209 /** Implementation: the following functions are in objects.c. */
211 /* Create an instance of class 'class_', adding extraBytes to the size
212 of the returned object. This method allocates the appropriate
213 amount of memory for the instance, initializes it to zero, then
214 calls all the C++ constructors on appropriate C++ instance
215 variables of the instance (if any) (TODO: The C++ constructors bit
216 is not implemented yet). */
217 objc_EXPORT id class_createInstance (Class class_, size_t extraBytes);
219 /* Copy an object and return the copy. extraBytes should be identical
220 to the extraBytes parameter that was passed when creating the
221 original object. */
222 objc_EXPORT id object_copy (id object, size_t extraBytes);
224 /* Dispose of an object. This method calls the appropriate C++
225 destructors on appropriate C++ instance variables of the instance
226 (if any) (TODO: This is not implemented yet), then frees the memory
227 for the instance. */
228 objc_EXPORT id object_dispose (id object);
230 /* Return the name of the class of 'object'. If 'object' is 'nil',
231 returns "Nil". */
232 objc_EXPORT const char * object_getClassName (id object);
234 /* Change the class of object to be class_. Return the previous class
235 of object. This is currently not really thread-safe. */
236 objc_EXPORT Class object_setClass (id object, Class class_);
239 /** Implementation: the following functions are in ivars.c. */
241 /* Return an instance variable given the class and the instance
242 variable name. This is an expensive function to call, so try to
243 reuse the returned Ivar if you can. */
244 objc_EXPORT Ivar class_getInstanceVariable (Class class_, const char *name);
246 /* Return a class variable given the class and the class variable
247 name. This is an expensive function to call, so try to reuse the
248 returned Ivar if you can.
250 This function always returns NULL since class variables are
251 currently unavailable in Objective-C. */
252 objc_EXPORT Ivar class_getClassVariable (Class class_, const char *name);
254 /* If the object was created in class_createInstance() with some
255 extraBytes, returns a pointer to them. If it was not, then the
256 returned pointer may make no sense. */
257 objc_EXPORT void * object_getIndexedIvars (id object);
259 /* Get the value of an instance variable of type 'id'. The function
260 returns the instance variable. To get the value of the instance
261 variable, you should pass as 'returnValue' a pointer to an 'id';
262 the value will be copied there. Note that 'returnValue' is really
263 a 'void *', not a 'void **'. This function really works only with
264 instance variables of type 'id'; for other types of instance
265 variables, access directly the data at (char *)object +
266 ivar_getOffset (ivar). */
267 objc_EXPORT Ivar object_getInstanceVariable (id object, const char *name, void **returnValue);
269 /* Set the value of an instance variable. The value to set is passed
270 in 'newValue' (which really is an 'id', not a 'void *'). The
271 function returns the instance variable. This function really works
272 only with instance variables of type 'id'; for other types of
273 instance variables, access directly the data at (char *)object +
274 ivar_getOffset (ivar). */
275 objc_EXPORT Ivar object_setInstanceVariable (id object, const char *name, void *newValue);
277 /* Get the value of an instance variable of type 'id' of the object
278 'object'. This is faster than object_getInstanceVariable if you
279 already have the instance variable because it avoids the expensive
280 call to class_getInstanceVariable that is done by
281 object_getInstanceVariable. */
282 objc_EXPORT id object_getIvar (id object, Ivar variable);
284 /* Set the value of an instance variable of type 'id' of the object
285 'object'. This is faster than object_setInstanceVariable if you
286 already have the instance variable because it avoids the expensive
287 call to class_getInstanceVariable that is done by
288 object_setInstanceVariable. */
289 objc_EXPORT void object_setIvar (id object, Ivar variable, id value);
291 /* Return the name of the instance variable. Return NULL if
292 'variable' is NULL. */
293 objc_EXPORT const char * ivar_getName (Ivar variable);
295 /* Return the offset of the instance variable from the start of the
296 object data. Return 0 if 'variable' is NULL. */
297 objc_EXPORT ptrdiff_t ivar_getOffset (Ivar variable);
299 /* Return the type encoding of the variable. Return NULL if
300 'variable' is NULL. */
301 objc_EXPORT const char * ivar_getTypeEncoding (Ivar variable);
303 /* Return all the instance variables of the class. The return value
304 of the function is a pointer to an area, allocated with malloc(),
305 that contains all the instance variables of the class. It does not
306 include instance variables of superclasses. The list is terminated
307 by NULL. Optionally, if you pass a non-NULL
308 'numberOfReturnedIvars' pointer, the unsigned int that it points to
309 will be filled with the number of instance variables returned.
310 Return NULL for classes still in construction (ie, allocated using
311 objc_allocatedClassPair() but not yet registered with the runtime
312 using objc_registerClassPair()). */
313 objc_EXPORT Ivar * class_copyIvarList (Class class_, unsigned int *numberOfReturnedIvars);
315 /* Add an instance variable with name 'ivar_name' to class 'class_',
316 where 'class_' is a class in construction that has been created
317 using objc_allocateClassPair() and has not been registered with the
318 runtime using objc_registerClassPair() yet. You can not add
319 instance variables to classes already registered with the runtime.
320 'size' is the size of the instance variable, 'alignment' the
321 alignment, and 'type' the type encoding of the variable type. You
322 can use sizeof(), __alignof__() and @encode() to determine the
323 right 'size', 'alignment' and 'type' for your instance variable.
324 For example, to add an instance variable name "my_variable" and of
325 type 'id', you can use:
327 class_addIvar (class, "my_variable", sizeof (id), __alignof__ (id),
328 @encode (id));
330 Return YES if the variable was added, and NO if not. In
331 particular, return NO if 'class_' is Nil, or a meta-class or a
332 class not in construction. Return Nil also if 'ivar_name' or
333 'type' is NULL, or 'size' is 0.
335 objc_EXPORT BOOL class_addIvar (Class class_, const char * ivar_name, size_t size,
336 unsigned char alignment, const char *type);
338 /* Return the name of the property. Return NULL if 'property' is
339 NULL. */
340 objc_EXPORT const char * property_getName (Property property);
342 /* Return the attributes of the property as a string. Return NULL if
343 'property' is NULL. */
344 objc_EXPORT const char * property_getAttributes (Property property);
346 /* Return the property with name 'propertyName' of the class 'class_'.
347 This function returns NULL if the required property can not be
348 found. Return NULL if 'class_' or 'propertyName' is NULL.
350 Note that the traditional ABI does not store the list of properties
351 of a class in a compiled module, so the traditional ABI will always
352 return NULL. */
353 objc_EXPORT Property class_getProperty (Class class_, const char *propertyName);
355 /* Return all the properties of the class. The return value
356 of the function is a pointer to an area, allocated with malloc(),
357 that contains all the properties of the class. It does not
358 include properties of superclasses. The list is terminated
359 by NULL. Optionally, if you pass a non-NULL
360 'numberOfReturnedIvars' pointer, the unsigned int that it points to
361 will be filled with the number of properties returned.
363 Note that the traditional ABI does not store the list of properties
364 of a class in a compiled module, so the traditional ABI will always
365 return an empty list. */
366 objc_EXPORT Property * class_copyPropertyList
367 (Class class_, unsigned int *numberOfReturnedProperties);
369 /* Return the ivar layout for class 'class_'.
371 At the moment this function always returns NULL. */
372 objc_EXPORT const char * class_getIvarLayout (Class class_);
374 /* Return the weak ivar layout for class 'class_'.
376 At the moment this function always returns NULL. */
377 objc_EXPORT const char * class_getWeakIvarLayout (Class class_);
379 /* Set the ivar layout for class 'class_'.
381 At the moment, this function does nothing. */
382 objc_EXPORT void class_setIvarLayout (Class class_, const char *layout);
384 /* Set the weak ivar layout for class 'class_'.
386 At the moment, this function does nothing. With the GNU runtime,
387 you should use class_ivar_set_gcinvisible () to hide variables from
388 the Garbage Collector. */
389 objc_EXPORT void class_setWeakIvarLayout (Class class_, const char *layout);
392 /** Implementation: the following functions are in class.c. */
394 /* Compatibility Note: The Apple/NeXT runtime does not have
395 objc_get_unknown_class_handler and
396 objc_setGetUnknownClassHandler(). They provide functionality that
397 the traditional GNU Objective-C Runtime API used to provide via the
398 _objc_lookup_class hook. */
400 /* An 'objc_get_unknown_class_handler' function is used by
401 objc_getClass() to get a class that is currently unknown to the
402 compiler. You could use it for example to have the class loaded by
403 dynamically loading a library. 'class_name' is the name of the
404 class. The function should return the Class object if it manages to
405 load the class, and Nil if not. */
406 typedef Class (*objc_get_unknown_class_handler)(const char *class_name);
408 /* Sets a new handler function for getting unknown classes (to be used
409 by objc_getClass () and related), and returns the previous one.
410 This function is not safe to call in a multi-threaded environment
411 because other threads may be trying to use the get unknown class
412 handler while you change it! */
413 objc_get_unknown_class_handler
414 objc_setGetUnknownClassHandler (objc_get_unknown_class_handler new_handler);
416 /* Return the class with name 'name', if it is already registered with
417 the runtime. If it is not registered, and
418 objc_setGetUnknownClassHandler() has been called to set a handler
419 for unknown classes, the handler is called to give it a chance to
420 load the class in some other way. If the class is not known to the
421 runtime and the handler is not set or returns Nil, objc_getClass()
422 returns Nil. */
423 objc_EXPORT Class objc_getClass (const char *name);
425 /* Return the class with name 'name', if it is already registered with
426 the runtime. Return Nil if not. This function does not call the
427 objc_get_unknown_class_handler function if the class is not
428 found. */
429 objc_EXPORT Class objc_lookupClass (const char *name);
431 /* Return the meta class associated to the class with name 'name', if
432 it is already registered with the runtime. First, it finds the
433 class using objc_getClass(). Then, it returns the associated meta
434 class. If the class could not be found using objc_getClass(),
435 returns Nil. */
436 objc_EXPORT Class objc_getMetaClass (const char *name);
438 /* This is identical to objc_getClass(), but if the class is not found,
439 it aborts the process instead of returning Nil. */
440 objc_EXPORT Class objc_getRequiredClass (const char *name);
442 /* If 'returnValue' is NULL, 'objc_getClassList' returns the number of
443 classes currently registered with the runtime. If 'returnValue' is
444 not NULL, it should be a (Class *) pointer to an area of memory
445 which can contain up to 'maxNumberOfClassesToReturn' Class records.
446 'objc_getClassList' will fill the area pointed to by 'returnValue'
447 with all the Classes registered with the runtime (or up to
448 maxNumberOfClassesToReturn if there are more than
449 maxNumberOfClassesToReturn). The function return value is the
450 number of classes actually returned in 'returnValue'. */
451 objc_EXPORT int objc_getClassList (Class *returnValue, int maxNumberOfClassesToReturn);
453 /* Compatibility Note: The Apple/NeXT runtime also has
455 Class objc_getFutureClass (const char *name);
456 void objc_setFutureClass (Class class_, const char *name);
458 the documentation is unclear on what they are supposed to do, and
459 the GNU Objective-C Runtime currently does not provide them. */
461 /* Return the name of the class 'class_', or the string "nil" if the
462 class_ is Nil. */
463 objc_EXPORT const char * class_getName (Class class_);
465 /* Return YES if 'class_' is a meta class, and NO if not. If 'class_'
466 is Nil, return NO. */
467 objc_EXPORT BOOL class_isMetaClass (Class class_);
469 /* Return the superclass of 'class_'. If 'class_' is Nil, or it is a
470 root class, return Nil. If 'class_' is a class being constructed,
471 that is, a class returned by objc_allocateClassPair() but before it
472 has been registered with the runtime using
473 objc_registerClassPair(), return Nil. */
474 objc_EXPORT Class class_getSuperclass (Class class_);
476 /* Return the 'version' number of the class, which is an integer that
477 can be used to track changes in the class API, methods and
478 variables. If class_ is Nil, return 0. If class_ is not Nil, the
479 version is 0 unless class_setVersion() has been called to set a
480 different one.
482 Please note that internally the version is a long, but the API only
483 allows you to set and retrieve int values. */
484 objc_EXPORT int class_getVersion (Class class_);
486 /* Set the 'version' number of the class, which is an integer that can
487 be used to track changes in the class API, methods and variables.
488 If 'class_' is Nil, does nothing.
490 This is typically used internally by "Foundation" libraries such as
491 GNUstep Base to support serialization / deserialization of objects
492 that work across changes in the classes. If you are using such a
493 library, you probably want to use their versioning API, which may
494 be based on this one, but is integrated with the rest of the
495 library.
497 Please note that internally the version is a long, but the API only
498 allows you to set and retrieve int values. */
499 objc_EXPORT void class_setVersion (Class class_, int version);
501 /* Return the size in bytes (a byte is the size of a char) of an
502 instance of the class. If class_ is Nil, return 0; else it return
503 a non-zero number (since the 'isa' instance variable is required
504 for all classes). */
505 objc_EXPORT size_t class_getInstanceSize (Class class_);
507 /* Change the implementation of the method. It also searches all
508 classes for any class implementing the method, and replaces the
509 existing implementation with the new one. For that to work,
510 'method' must be a method returned by class_getInstanceMethod() or
511 class_getClassMethod() as the matching is done by comparing the
512 pointers; in that case, only the implementation in the class is
513 modified. Return the previous implementation that has been
514 replaced. If method or implementation is NULL, do nothing and
515 return NULL. */
516 objc_EXPORT IMP
517 method_setImplementation (Method method, IMP implementation);
519 /* Swap the implementation of two methods in a single, atomic
520 operation. This is equivalent to getting the implementation of
521 each method and then calling method_setImplementation() on the
522 other one. For this to work, the two methods must have been
523 returned by class_getInstanceMethod() or class_getClassMethod().
524 If 'method_a' or 'method_b' is NULL, do nothing. */
525 objc_EXPORT void
526 method_exchangeImplementations (Method method_a, Method method_b);
528 /* Create a new class/meta-class pair. This function is called to
529 create a new class at runtime. The class is created with
530 superclass 'superclass' (use 'Nil' to create a new root class) and
531 name 'class_name'. 'extraBytes' can be used to specify some extra
532 space for indexed variables to be added at the end of the class and
533 meta-class objects (it is recommended that you set extraBytes to
534 0). Once you have created the class, it is not usable yet. You
535 need to add any instance variables (by using class_addIvar()), any
536 instance methods (by using class_addMethod()) and any class methods
537 (by using class_addMethod() on the meta-class, as in
538 class_addMethod (object_getClass (class), method)) that are
539 required, and then you need to call objc_registerClassPair() to
540 activate the class. If you need to create a hierarchy of classes,
541 you need to create and register them one at a time. You can not
542 create a new class using another class in construction as
543 superclass. Return Nil if 'class-name' is NULL or if a class with
544 that name already exists or 'superclass' is a class still in
545 construction.
547 Implementation Note: in the GNU runtime, allocating a class pair
548 only creates the structures for the class pair, but does not
549 register anything with the runtime. The class is registered with
550 the runtime only when objc_registerClassPair() is called. In
551 particular, if a class is in construction, objc_getClass() will not
552 find it, the superclass will not know about it,
553 class_getSuperclass() will return Nil and another thread may
554 allocate a class pair with the same name; the conflict will only be
555 detected when the classes are registered with the runtime.
557 objc_EXPORT Class
558 objc_allocateClassPair (Class super_class, const char *class_name,
559 size_t extraBytes);
561 /* Register a class pair that was created with
562 objc_allocateClassPair(). After you register a class, you can no
563 longer make changes to its instance variables, but you can start
564 creating instances of it. Do nothing if 'class_' is NULL or if it
565 is not a class allocated by objc_allocateClassPair() and still in
566 construction. */
567 objc_EXPORT void
568 objc_registerClassPair (Class class_);
570 /* Dispose of a class pair created using objc_allocateClassPair().
571 Call this function if you started creating a new class with
572 objc_allocateClassPair() but then want to abort the process. You
573 should not access 'class_' after calling this method. Note that if
574 'class_' has already been registered with the runtime via
575 objc_registerClassPair(), this function does nothing; you can only
576 dispose of class pairs that are still being constructed. Do
577 nothing if class is 'Nil' or if 'class_' is not a class being
578 constructed. */
579 objc_EXPORT void
580 objc_disposeClassPair (Class class_);
582 /* Compatibility Note: The Apple/NeXT runtime has the function
583 objc_duplicateClass () but it's undocumented. The GNU runtime does
584 not have it. */
587 /** Implementation: the following functions are in sendmsg.c. */
589 /* Return the instance method with selector 'selector' of class
590 'class_', or NULL if the class (or one of its superclasses) does
591 not implement the method. Return NULL if class_ is Nil or selector
592 is NULL. Calling this function may trigger a call to
593 +resolveInstanceMethod:, but does not return a forwarding
594 function. */
595 objc_EXPORT Method class_getInstanceMethod (Class class_, SEL selector);
597 /* Return the class method with selector 'selector' of class 'class_',
598 or NULL if the class (or one of its superclasses) does not
599 implement the method. Return NULL if class_ is Nil or selector is
600 NULL. Calling this function may trigger a call to
601 +resolveClassMethod:, but does not return a forwarding
602 function. */
603 objc_EXPORT Method class_getClassMethod (Class class_, SEL selector);
605 /* Return the IMP (pointer to the function implementing a method) for
606 the instance method with selector 'selector' in class 'class_'.
607 This is the same routine that is used while messaging, and should
608 be very fast. Note that you most likely would need to cast the
609 return function pointer to a function pointer with the appropriate
610 arguments and return type before calling it. To get a class
611 method, you can pass the meta-class as the class_ argument (ie, use
612 class_getMethodImplementation (object_getClass (class_),
613 selector)). Return NULL if class_ is Nil or selector is NULL.
614 This function first looks for an existing method; if it is not
615 found, it calls +resolveClassMethod: or +resolveInstanceMethod:
616 (depending on whether a class or instance method is being looked
617 up) if it is implemented. If the method returns YES, then it tries
618 the look up again (the assumption being that +resolveClassMethod:
619 or resolveInstanceMethod: will add the method using
620 class_addMethod()). If it is still not found, it returns a
621 forwarding function. */
622 objc_EXPORT IMP class_getMethodImplementation (Class class_, SEL selector);
624 /* Compatibility Note: the Apple/NeXT runtime has the function
625 class_getMethodImplementation_stret () which currently does not
626 exist on the GNU runtime because the messaging implementation is
627 different. */
629 /* Return YES if class 'class_' has an instance method implementing
630 selector 'selector', and NO if not. Return NO if class_ is Nil or
631 selector is NULL. If you need to check a class method, use the
632 meta-class as the class_ argument (ie, use class_respondsToSelector
633 (object_getClass (class_), selector)). */
634 objc_EXPORT BOOL class_respondsToSelector (Class class_, SEL selector);
636 /* Add a method to a class. Use this function to add a new method to
637 a class (potentially overriding a method with the same selector in
638 the superclass); if you want to modify an existing method, use
639 method_setImplementation() instead (or class_replaceMethod ()).
640 This method adds an instance method to 'class_'; to add a class
641 method, get the meta class first, then add the method to the meta
642 class, that is, use
644 class_addMethod (object_getClass (class_), selector,
645 implementation, type);
647 Return YES if the method was added, and NO if not. Do nothing if
648 one of the arguments is NULL. */
649 objc_EXPORT BOOL class_addMethod (Class class_, SEL selector, IMP implementation,
650 const char *method_types);
652 /* Replace a method in a class. If the class already have a method
653 with this 'selector', find it and use method_setImplementation() to
654 replace the implementation with 'implementation' (method_types is
655 ignored in that case). If the class does not already have a method
656 with this 'selector', call 'class_addMethod() to add it.
658 Return the previous implementation of the method, or NULL if none
659 was found. Return NULL if any of the arguments is NULL. */
660 objc_EXPORT IMP class_replaceMethod (Class class_, SEL selector, IMP implementation,
661 const char *method_types);
664 /** Implementation: the following functions are in methods.c. */
666 /* Return the selector for method 'method'. Return NULL if 'method'
667 is NULL.
669 This function is misnamed; it should be called
670 'method_getSelector'. To get the actual name, get the selector,
671 then the name from the selector (ie, use sel_getName
672 (method_getName (method))). */
673 objc_EXPORT SEL method_getName (Method method);
675 /* Return the IMP of the method. Return NULL if 'method' is NULL. */
676 objc_EXPORT IMP method_getImplementation (Method method);
678 /* Return the type encoding of the method. Return NULL if 'method' is
679 NULL. */
680 objc_EXPORT const char * method_getTypeEncoding (Method method);
682 /* Return a method description for the method. Return NULL if
683 'method' is NULL. */
684 objc_EXPORT struct objc_method_description * method_getDescription (Method method);
686 /* Return all the instance methods of the class. The return value of
687 the function is a pointer to an area, allocated with malloc(), that
688 contains all the instance methods of the class. It does not
689 include instance methods of superclasses. The list is terminated
690 by NULL. Optionally, if you pass a non-NULL
691 'numberOfReturnedMethods' pointer, the unsigned int that it points
692 to will be filled with the number of instance methods returned. To
693 get the list of class methods, pass the meta-class in the 'class_'
694 argument, (ie, use class_copyMethodList (object_getClass (class_),
695 &numberOfReturnedMethods)). */
696 objc_EXPORT Method * class_copyMethodList (Class class_, unsigned int *numberOfReturnedMethods);
699 /** Implementation: the following functions are in encoding.c. */
701 /* Return the number of arguments that the method 'method' expects.
702 Note that all methods need two implicit arguments ('self' for the
703 receiver, and '_cmd' for the selector). Return 0 if 'method' is
704 NULL. */
705 objc_EXPORT unsigned int method_getNumberOfArguments (Method method);
707 /* Return the string encoding for the return type of method 'method'.
708 The string is a standard zero-terminated string in an area of
709 memory allocated with malloc(); you should free it with free() when
710 you finish using it. Return an empty string if method is NULL. */
711 objc_EXPORT char * method_copyReturnType (Method method);
713 /* Return the string encoding for the argument type of method
714 'method', argument number 'argumentNumber' ('argumentNumber' is 0
715 for self, 1 for _cmd, and 2 or more for the additional arguments if
716 any). The string is a standard zero-terminated string in an area
717 of memory allocated with malloc(); you should free it with free()
718 when you finish using it. Return an empty string if method is NULL
719 or if 'argumentNumber' refers to a non-existing argument. */
720 objc_EXPORT char * method_copyArgumentType (Method method, unsigned int argumentNumber);
722 /* Return the string encoding for the return type of method 'method'.
723 The string is returned by copying it into the supplied
724 'returnValue' string, which is of size 'returnValueSize'. No more
725 than 'returnValueSize' characters are copied; if the encoding is
726 smaller than 'returnValueSize', the rest of 'returnValue' is filled
727 with zeros. If it is bigger, it is truncated (and would not be
728 zero-terminated). You should supply a big enough
729 'returnValueSize'. If the method is NULL, returnValue is set to a
730 string of zeros. */
731 objc_EXPORT void method_getReturnType (Method method, char *returnValue,
732 size_t returnValueSize);
734 /* Return the string encoding for the argument type of method
735 'method', argument number 'argumentNumber' ('argumentNumber' is 0
736 for self, 1 for _cmd, and 2 or more for the additional arguments if
737 any). The string is returned by copying it into the supplied
738 'returnValue' string, which is of size 'returnValueSize'. No more
739 than 'returnValueSize' characters are copied; if the encoding is
740 smaller than 'returnValueSize', the rest of 'returnValue' is filled
741 with zeros. If it is bigger, it is truncated (and would not be
742 zero-terminated). You should supply a big enough
743 'returnValueSize'. If the method is NULL, returnValue is set to a
744 string of zeros. */
745 objc_EXPORT void method_getArgumentType (Method method, unsigned int argumentNumber,
746 char *returnValue, size_t returnValueSize);
749 /** Implementation: the following functions are in protocols.c. */
751 /* Return the protocol with name 'name', or nil if it the protocol is
752 not known to the runtime. */
753 objc_EXPORT Protocol *objc_getProtocol (const char *name);
755 /* Return all the protocols known to the runtime. The return value of
756 the function is a pointer to an area, allocated with malloc(), that
757 contains all the protocols known to the runtime; the list is
758 terminated by NULL. You should free this area using free() once
759 you no longer need it. Optionally, if you pass a non-NULL
760 'numberOfReturnedProtocols' pointer, the unsigned int that it
761 points to will be filled with the number of protocols returned. If
762 there are no protocols known to the runtime, NULL is returned. */
763 objc_EXPORT Protocol **objc_copyProtocolList (unsigned int *numberOfReturnedProtocols);
765 /* Add a protocol to a class, and return YES if it was done
766 succesfully, and NO if not. At the moment, NO should only happen
767 if class_ or protocol are nil, if the protocol is not a Protocol
768 object or if the class already conforms to the protocol. */
769 objc_EXPORT BOOL class_addProtocol (Class class_, Protocol *protocol);
771 /* Return YES if the class 'class_' conforms to Protocol 'protocol',
772 and NO if not. */
773 objc_EXPORT BOOL class_conformsToProtocol (Class class_, Protocol *protocol);
775 /* Return all the protocols that the class conforms to. The return
776 value of the function is a pointer to an area, allocated with
777 malloc(), that contains all the protocols formally adopted by the
778 class. It does not include protocols adopted by superclasses. The
779 list is terminated by NULL. Optionally, if you pass a non-NULL
780 'numberOfReturnedProtocols' pointer, the unsigned int that it
781 points to will be filled with the number of protocols returned. */
782 objc_EXPORT Protocol **class_copyProtocolList (Class class_, unsigned int *numberOfReturnedProtocols);
784 /* Return YES if protocol 'protocol' conforms to protocol
785 'anotherProtocol', and NO if not. Note that if one of the two
786 protocols is nil, it returns NO. */
787 objc_EXPORT BOOL protocol_conformsToProtocol (Protocol *protocol, Protocol *anotherProtocol);
789 /* Return YES if protocol 'protocol' is the same as protocol
790 'anotherProtocol', and 'NO' if not. Note that it returns YES if
791 the two protocols are both nil. */
792 objc_EXPORT BOOL protocol_isEqual (Protocol *protocol, Protocol *anotherProtocol);
794 /* Return the name of protocol 'protocol'. If 'protocol' is nil or is
795 not a Protocol, return NULL. */
796 objc_EXPORT const char *protocol_getName (Protocol *protocol);
798 /* Return the method description for the method with selector
799 'selector' in protocol 'protocol'; if 'requiredMethod' is YES, the
800 function searches the list of required methods; if NO, the list of
801 optional methods. If 'instanceMethod' is YES, the function search
802 for an instance method; if NO, for a class method. If there is no
803 matching method, an objc_method_description structure with both
804 name and types set to NULL is returned. This function will only
805 find methods that are directly declared in the protocol itself, not
806 in other protocols that this protocol adopts.
808 Note that the traditional ABI does not store the list of optional
809 methods of a protocol in a compiled module, so the traditional ABI
810 will always return (NULL, NULL) when requiredMethod == NO. */
811 objc_EXPORT struct objc_method_description protocol_getMethodDescription (Protocol *protocol,
812 SEL selector,
813 BOOL requiredMethod,
814 BOOL instanceMethod);
816 /* Return the method descriptions of all the methods of the protocol.
817 The return value of the function is a pointer to an area, allocated
818 with malloc(), that contains all the method descriptions of the
819 methods of the protocol. It does not recursively include methods
820 of the protocols adopted by this protocol. The list is terminated
821 by a NULL objc_method_description (one with both fields set to
822 NULL). Optionally, if you pass a non-NULL
823 'numberOfReturnedMethods' pointer, the unsigned int that it points
824 to will be filled with the number of properties returned.
826 Note that the traditional ABI does not store the list of optional
827 methods of a protocol in a compiled module, so the traditional ABI
828 will always return an empty list if requiredMethod is set to
829 NO. */
830 objc_EXPORT struct objc_method_description *protocol_copyMethodDescriptionList (Protocol *protocol,
831 BOOL requiredMethod,
832 BOOL instanceMethod,
833 unsigned int *numberOfReturnedMethods);
835 /* Return the property with name 'propertyName' of the protocol
836 'protocol'. If 'requiredProperty' is YES, the function searches
837 the list of required properties; if NO, the list of optional
838 properties. If 'instanceProperty' is YES, the function searches
839 the list of instance properties; if NO, the list of class
840 properties. At the moment, optional properties and class
841 properties are not part of the Objective-C language, so both
842 'requiredProperty' and 'instanceProperty' should be set to YES.
843 This function returns NULL if the required property can not be
844 found.
846 Note that the traditional ABI does not store the list of properties
847 of a protocol in a compiled module, so the traditional ABI will
848 always return NULL. */
849 objc_EXPORT Property protocol_getProperty (Protocol *protocol, const char *propertyName,
850 BOOL requiredProperty, BOOL instanceProperty);
852 /* Return all the properties of the protocol. The return value of the
853 function is a pointer to an area, allocated with malloc(), that
854 contains all the properties of the protocol. It does not
855 recursively include properties of the protocols adopted by this
856 protocol. The list is terminated by NULL. Optionally, if you pass
857 a non-NULL 'numberOfReturnedProperties' pointer, the unsigned int
858 that it points to will be filled with the number of properties
859 returned.
861 Note that the traditional ABI does not store the list of properties
862 of a protocol in a compiled module, so the traditional ABI will
863 always return NULL and store 0 in numberOfReturnedProperties. */
864 objc_EXPORT Property *protocol_copyPropertyList (Protocol *protocol, unsigned int *numberOfReturnedProperties);
866 /* Return all the protocols that the protocol conforms to. The return
867 value of the function is a pointer to an area, allocated with
868 malloc(), that contains all the protocols formally adopted by the
869 protocol. It does not recursively include protocols adopted by the
870 protocols adopted by this protocol. The list is terminated by
871 NULL. Optionally, if you pass a non-NULL
872 'numberOfReturnedProtocols' pointer, the unsigned int that it
873 points to will be filled with the number of protocols returned. */
874 objc_EXPORT Protocol **protocol_copyProtocolList (Protocol *protocol, unsigned int *numberOfReturnedProtocols);
877 /* TODO: Add all the other functions in the API. */
880 /** Implementation: the following functions are in objc-foreach.c. */
882 /* 'objc_enumerationMutation()' is called when a collection is
883 mutated while being "fast enumerated". That is a hard error, and
884 objc_enumerationMutation is called to deal with it. 'collection'
885 is the collection object that was mutated during an enumeration.
887 objc_enumerationMutation() will invoke the mutation handler if any
888 is set. Then, it will abort the program.
890 Compatibility note: the Apple runtime will not abort the program
891 after calling the mutation handler. */
892 objc_EXPORT void objc_enumerationMutation (id collection);
894 /* 'objc_set_enumeration_mutation_handler' can be used to set a
895 function that will be called (instead of aborting) when a fast
896 enumeration is mutated during enumeration. The handler will be
897 called with the 'collection' being mutated as the only argument and
898 it should not return; it should either exit the program, or could
899 throw an exception. The recommended implementation is to throw an
900 exception - the user can then use exception handlers to deal with
903 This function is not thread safe (other threads may be trying to
904 invoke the enumeration mutation handler while you are changing it!)
905 and should be called during during the program initialization
906 before threads are started. It is mostly reserved for "Foundation"
907 libraries; in the case of GNUstep, GNUstep Base may be using this
908 function to improve the standard enumeration mutation handling.
909 You probably shouldn't use this function unless you are writing
910 your own Foundation library. */
911 objc_EXPORT void objc_setEnumerationMutationHandler (void (*handler)(id));
913 /* This structure (used during fast enumeration) is automatically
914 defined by the compiler (it is as if this definition was always
915 included in all Objective-C files). Note that it is usually
916 defined again with the name of NSFastEnumeration by "Foundation"
917 libraries such as GNUstep Base. And if NSFastEnumeration is
918 defined, the compiler will use it instead of
919 __objcFastEnumerationState when doing fast enumeration. */
921 struct __objcFastEnumerationState
923 unsigned long state;
924 id *itemsPtr;
925 unsigned long *mutationsPtr;
926 unsigned long extra[5];
931 /** Implementation: the following functions are in memory.c. */
933 /* Traditional GNU Objective-C Runtime functions that are used for
934 memory allocation and disposal. These functions are used in the
935 same way as you use malloc, realloc, calloc and free and make sure
936 that memory allocation works properly with the garbage
937 collector.
939 Compatibility Note: these functions are not available with the
940 Apple/NeXT runtime. */
942 objc_EXPORT void *objc_malloc(size_t size);
944 /* FIXME: Shouldn't the following be called objc_malloc_atomic ? The
945 GC function is GC_malloc_atomic() which makes sense.
947 objc_EXPORT void *objc_atomic_malloc(size_t size);
949 objc_EXPORT void *objc_realloc(void *mem, size_t size);
951 objc_EXPORT void *objc_calloc(size_t nelem, size_t size);
953 objc_EXPORT void objc_free(void *mem);
956 /** Implementation: the following functions are in encoding.c. */
958 /* Traditional GNU Objective-C Runtime functions that are currently
959 used to implement method forwarding.
961 Compatibility Note: these functions are not available with the
962 Apple/NeXT runtime. */
964 /* Return the size of a variable which has the specified 'type'
965 encoding. */
966 int objc_sizeof_type (const char *type);
968 /* Return the align of a variable which has the specified 'type'
969 encoding. */
970 int objc_alignof_type (const char *type);
972 /* Return the aligned size of a variable which has the specified
973 'type' encoding. The aligned size is the size rounded up to the
974 nearest alignment. */
975 int objc_aligned_size (const char *type);
977 /* Return the promoted size of a variable which has the specified
978 'type' encoding. This is the size rounded up to the nearest
979 integral of the wordsize, taken to be the size of a void *. */
980 int objc_promoted_size (const char *type);
983 /* The following functions are used when parsing the type encoding of
984 methods, to skip over parts that are ignored. They take as
985 argument a pointer to a location inside the type encoding of a
986 method (which is a string) and return a new pointer, pointing to a
987 new location inside the string after having skipped the unwanted
988 information. */
990 /* Skip some type qualifiers (_C_CONST, _C_IN, etc). These may
991 eventually precede typespecs occurring in method prototype
992 encodings. */
993 const char *objc_skip_type_qualifiers (const char *type);
995 /* Skip one typespec element (_C_CLASS, _C_SEL, etc). If the typespec
996 is prepended by type qualifiers, these are skipped as well. */
997 const char *objc_skip_typespec (const char *type);
999 /* Skip an offset. */
1000 const char *objc_skip_offset (const char *type);
1002 /* Skip an argument specification (ie, skipping a typespec, which may
1003 include qualifiers, and an offset too). */
1004 const char *objc_skip_argspec (const char *type);
1006 /* Read type qualifiers (_C_CONST, _C_IN, etc) from string 'type'
1007 (stopping at the first non-type qualifier found) and return an
1008 unsigned int which is the logical OR of all the corresponding flags
1009 (_F_CONST, _F_IN etc). */
1010 unsigned objc_get_type_qualifiers (const char *type);
1013 /* Note that the following functions work for very simple structures,
1014 but get easily confused by more complicated ones (for example,
1015 containing vectors). A better solution is required.
1018 /* The following three functions can be used to determine how a
1019 structure is laid out by the compiler. For example:
1021 struct objc_struct_layout layout;
1022 int i;
1024 objc_layout_structure (type, &layout);
1025 while (objc_layout_structure_next_member (&layout))
1027 int position, align;
1028 const char *type;
1030 objc_layout_structure_get_info (&layout, &position, &align, &type);
1031 printf ("element %d has offset %d, alignment %d\n",
1032 i++, position, align);
1035 These functions are used by objc_sizeof_type and objc_alignof_type
1036 functions to compute the size and alignment of structures. The
1037 previous method of computing the size and alignment of a structure
1038 was not working on some architectures, particulary on AIX, and in
1039 the presence of bitfields inside the structure. */
1040 struct objc_struct_layout
1042 const char *original_type;
1043 const char *type;
1044 const char *prev_type;
1045 unsigned int record_size;
1046 unsigned int record_align;
1049 void objc_layout_structure (const char *type,
1050 struct objc_struct_layout *layout);
1051 BOOL objc_layout_structure_next_member (struct objc_struct_layout *layout);
1052 void objc_layout_finish_structure (struct objc_struct_layout *layout,
1053 unsigned int *size,
1054 unsigned int *align);
1055 void objc_layout_structure_get_info (struct objc_struct_layout *layout,
1056 unsigned int *offset,
1057 unsigned int *align,
1058 const char **type);
1060 #ifdef __cplusplus
1062 #endif /* __cplusplus */
1064 #endif