* g++.dg/tree-ssa-pr43411.C: Rename function to be inlined and
[official-gcc.git] / libobjc / objc / runtime.h
blob6e1f1ec36e816499a75d9fa27526008e7cadd821
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 #include "objc.h"
49 #include "objc-decls.h"
51 #ifdef __cplusplus
52 extern "C" {
53 #endif /* __cplusplus */
55 /* An 'Ivar' represents an instance variable. It holds information
56 about the name, type and offset of the instance variable. */
57 typedef struct objc_ivar *Ivar;
59 /* A 'Property' represents a property. It holds information about the
60 name of the property, and its attributes.
62 Compatibility Note: the Apple/NeXT runtime defines this as
63 objc_property_t, so we define it that way as well, but obviously
64 Property is the right name. */
65 typedef struct objc_property *Property;
66 typedef struct objc_property *objc_property_t;
68 /* A 'Method' represents a method. It holds information about the
69 name, types and the IMP of the method. */
70 typedef struct objc_method *Method;
72 /* A 'Category' represents a category. It holds information about the
73 name of the category, the class it belongs to, and the methods,
74 protocols and such like provided by the category. */
75 typedef struct objc_category *Category;
77 /* 'Protocol' is defined in objc/objc.h (which is included by this
78 file). */
80 /* Method descriptor returned by introspective Object methods. At the
81 moment, this is really just the first part of the more complete
82 objc_method structure used internally by the runtime. (PS: In the
83 GNU Objective-C Runtime, selectors already include a type, so an
84 objc_method_description does not add much to a SEL. But in other
85 runtimes, that is not the case, which is why
86 objc_method_description exists). */
87 struct objc_method_description
89 SEL name; /* Selector (name and signature) */
90 char *types; /* Type encoding */
93 /* The following are used in encode strings to describe the type of
94 Ivars and Methods. */
95 #define _C_ID '@'
96 #define _C_CLASS '#'
97 #define _C_SEL ':'
98 #define _C_CHR 'c'
99 #define _C_UCHR 'C'
100 #define _C_SHT 's'
101 #define _C_USHT 'S'
102 #define _C_INT 'i'
103 #define _C_UINT 'I'
104 #define _C_LNG 'l'
105 #define _C_ULNG 'L'
106 #define _C_LNG_LNG 'q'
107 #define _C_ULNG_LNG 'Q'
108 #define _C_FLT 'f'
109 #define _C_DBL 'd'
110 #define _C_LNG_DBL 'D'
111 #define _C_BFLD 'b'
112 #define _C_BOOL 'B'
113 #define _C_VOID 'v'
114 #define _C_UNDEF '?'
115 #define _C_PTR '^'
116 #define _C_CHARPTR '*'
117 #define _C_ARY_B '['
118 #define _C_ARY_E ']'
119 #define _C_UNION_B '('
120 #define _C_UNION_E ')'
121 #define _C_STRUCT_B '{'
122 #define _C_STRUCT_E '}'
123 #define _C_VECTOR '!'
124 #define _C_COMPLEX 'j'
126 /* _C_ATOM is never generated by the compiler. You can treat it as
127 equivalent to "*". */
128 #define _C_ATOM '%'
130 /* The following are used in encode strings to describe some
131 qualifiers of method and ivar types. */
132 #define _C_CONST 'r'
133 #define _C_IN 'n'
134 #define _C_INOUT 'N'
135 #define _C_OUT 'o'
136 #define _C_BYCOPY 'O'
137 #define _C_BYREF 'R'
138 #define _C_ONEWAY 'V'
139 #define _C_GCINVISIBLE '|'
141 /* The same when used as flags. */
142 #define _F_CONST 0x01
143 #define _F_IN 0x01
144 #define _F_OUT 0x02
145 #define _F_INOUT 0x03
146 #define _F_BYCOPY 0x04
147 #define _F_BYREF 0x08
148 #define _F_ONEWAY 0x10
149 #define _F_GCINVISIBLE 0x20
152 /** Implementation: the following functions are defined inline. */
154 /* Return the class of 'object', or Nil if the object is nil. If
155 'object' is a class, the meta class is returned; if 'object' is a
156 meta class, the root meta class is returned (note that this is
157 different from the traditional GNU Objective-C Runtime API function
158 object_get_class(), which for a meta class would return the meta
159 class itself). This function is inline, so it is really fast and
160 should be used instead of accessing object->class_pointer
161 directly. */
162 static inline Class
163 object_getClass (id object)
165 if (object != nil)
166 return object->class_pointer;
167 else
168 return Nil;
172 /** Implementation: the following functions are in selector.c. */
174 /* Return the name of a given selector. If 'selector' is NULL, return
175 "<null selector>". */
176 objc_EXPORT const char *sel_getName (SEL selector);
178 /* Return the type of a given selector. Return NULL if selector is
179 NULL.
181 Compatibility Note: the Apple/NeXT runtime has untyped selectors,
182 so it does not have this function, which is specific to the GNU
183 Runtime. */
184 objc_EXPORT const char *sel_getTypeEncoding (SEL selector);
186 /* This is the same as sel_registerName (). Please use
187 sel_registerName () instead. */
188 objc_EXPORT SEL sel_getUid (const char *name);
190 /* Register a selector with a given name (but unspecified types). If
191 you know the types, it is better to call sel_registerTypedName().
192 If a selector with this name and no types already exists, it is
193 returned. Note that this function should really be called
194 'objc_registerSelector'. Return NULL if 'name' is NULL. */
195 objc_EXPORT SEL sel_registerName (const char *name);
197 /* Register a selector with a given name and types. If a selector
198 with this name and types already exists, it is returned. Note that
199 this function should really be called 'objc_registerTypedSelector',
200 and it's called 'sel_registerTypedName' only for consistency with
201 'sel_registerName'. Return NULL if 'name' is NULL.
203 Compatibility Note: the Apple/NeXT runtime has untyped selectors,
204 so it does not have this function, which is specific to the GNU
205 Runtime. */
206 objc_EXPORT SEL sel_registerTypedName (const char *name, const char *type);
208 /* Return YES if first_selector is the same as second_selector, and NO
209 if not. */
210 objc_EXPORT BOOL sel_isEqual (SEL first_selector, SEL second_selector);
212 /* Return all the selectors with the supplied name. In the GNU
213 runtime, selectors are typed and there may be multiple selectors
214 with the same name but a different type. The return value of the
215 function is a pointer to an area, allocated with malloc(), that
216 contains all the selectors with the supplier name known to the
217 runtime. The list is terminated by NULL. Optionally, if you pass
218 a non-NULL 'numberOfReturnedSelectors' pointer, the unsigned int
219 that it points to will be filled with the number of selectors
220 returned.
222 Compatibility Note: the Apple/NeXT runtime has untyped selectors,
223 so it does not have this function, which is specific to the GNU
224 Runtime. */
225 objc_EXPORT SEL * sel_copyTypedSelectorList (const char *name,
226 unsigned int *numberOfReturnedSelectors);
228 /* Return a selector with name 'name' and a non-zero type encoding, if
229 there is a single selector with a type, and with that name,
230 registered with the runtime. If there is no such selector, or if
231 there are multiple selectors with the same name but conflicting
232 types, NULL is returned. Return NULL if 'name' is NULL.
234 This is useful if you have the name of the selector, and would
235 really like to get a selector for it that includes the type
236 encoding. Unfortunately, if the program contains multiple selector
237 with the same name but different types, sel_getTypedSelector can
238 not possibly know which one you need, and so will return NULL.
240 Compatibility Note: the Apple/NeXT runtime has untyped selectors,
241 so it does not have this function, which is specific to the GNU
242 Runtime. */
243 objc_EXPORT SEL sel_getTypedSelector (const char *name);
246 /** Implementation: the following functions are in objects.c. */
248 /* Create an instance of class 'class_', adding extraBytes to the size
249 of the returned object. This method allocates the appropriate
250 amount of memory for the instance, initializes it to zero, then
251 calls all the C++ constructors on appropriate C++ instance
252 variables of the instance (if any) (TODO: The C++ constructors bit
253 is not implemented yet). */
254 objc_EXPORT id class_createInstance (Class class_, size_t extraBytes);
256 /* Copy an object and return the copy. extraBytes should be identical
257 to the extraBytes parameter that was passed when creating the
258 original object. */
259 objc_EXPORT id object_copy (id object, size_t extraBytes);
261 /* Dispose of an object. This method calls the appropriate C++
262 destructors on appropriate C++ instance variables of the instance
263 (if any) (TODO: This is not implemented yet), then frees the memory
264 for the instance. */
265 objc_EXPORT id object_dispose (id object);
267 /* Return the name of the class of 'object'. If 'object' is 'nil',
268 returns "Nil". */
269 objc_EXPORT const char * object_getClassName (id object);
271 /* Change the class of object to be class_. Return the previous class
272 of object. This is currently not really thread-safe. */
273 objc_EXPORT Class object_setClass (id object, Class class_);
276 /** Implementation: the following functions are in ivars.c. */
278 /* Return an instance variable given the class and the instance
279 variable name. This is an expensive function to call, so try to
280 reuse the returned Ivar if you can. */
281 objc_EXPORT Ivar class_getInstanceVariable (Class class_, const char *name);
283 /* Return a class variable given the class and the class variable
284 name. This is an expensive function to call, so try to reuse the
285 returned Ivar if you can.
287 This function always returns NULL since class variables are
288 currently unavailable in Objective-C. */
289 objc_EXPORT Ivar class_getClassVariable (Class class_, const char *name);
291 /* If the object was created in class_createInstance() with some
292 extraBytes, returns a pointer to them. If it was not, then the
293 returned pointer may make no sense. */
294 objc_EXPORT void * object_getIndexedIvars (id object);
296 /* Get the value of an instance variable of type 'id'. The function
297 returns the instance variable. To get the value of the instance
298 variable, you should pass as 'returnValue' a pointer to an 'id';
299 the value will be copied there. Note that 'returnValue' is really
300 a 'void *', not a 'void **'. This function really works only with
301 instance variables of type 'id'; for other types of instance
302 variables, access directly the data at (char *)object +
303 ivar_getOffset (ivar). */
304 objc_EXPORT Ivar object_getInstanceVariable (id object, const char *name, void **returnValue);
306 /* Set the value of an instance variable. The value to set is passed
307 in 'newValue' (which really is an 'id', not a 'void *'). The
308 function returns the instance variable. This function really works
309 only with instance variables of type 'id'; for other types of
310 instance variables, access directly the data at (char *)object +
311 ivar_getOffset (ivar). */
312 objc_EXPORT Ivar object_setInstanceVariable (id object, const char *name, void *newValue);
314 /* Get the value of an instance variable of type 'id' of the object
315 'object'. This is faster than object_getInstanceVariable if you
316 already have the instance variable because it avoids the expensive
317 call to class_getInstanceVariable that is done by
318 object_getInstanceVariable. */
319 objc_EXPORT id object_getIvar (id object, Ivar variable);
321 /* Set the value of an instance variable of type 'id' of the object
322 'object'. This is faster than object_setInstanceVariable if you
323 already have the instance variable because it avoids the expensive
324 call to class_getInstanceVariable that is done by
325 object_setInstanceVariable. */
326 objc_EXPORT void object_setIvar (id object, Ivar variable, id value);
328 /* Return the name of the instance variable. Return NULL if
329 'variable' is NULL. */
330 objc_EXPORT const char * ivar_getName (Ivar variable);
332 /* Return the offset of the instance variable from the start of the
333 object data. Return 0 if 'variable' is NULL. */
334 objc_EXPORT ptrdiff_t ivar_getOffset (Ivar variable);
336 /* Return the type encoding of the variable. Return NULL if
337 'variable' is NULL. */
338 objc_EXPORT const char * ivar_getTypeEncoding (Ivar variable);
340 /* Return all the instance variables of the class. The return value
341 of the function is a pointer to an area, allocated with malloc(),
342 that contains all the instance variables of the class. It does not
343 include instance variables of superclasses. The list is terminated
344 by NULL. Optionally, if you pass a non-NULL
345 'numberOfReturnedIvars' pointer, the unsigned int that it points to
346 will be filled with the number of instance variables returned.
347 Return NULL for classes still in construction (ie, allocated using
348 objc_allocatedClassPair() but not yet registered with the runtime
349 using objc_registerClassPair()). */
350 objc_EXPORT Ivar * class_copyIvarList (Class class_, unsigned int *numberOfReturnedIvars);
352 /* Add an instance variable with name 'ivar_name' to class 'class_',
353 where 'class_' is a class in construction that has been created
354 using objc_allocateClassPair() and has not been registered with the
355 runtime using objc_registerClassPair() yet. You can not add
356 instance variables to classes already registered with the runtime.
357 'size' is the size of the instance variable, 'log_2_of_alignment'
358 the alignment as a power of 2 (so 0 means alignment to a 1 byte
359 boundary, 1 means alignment to a 2 byte boundary, 2 means alignment
360 to a 4 byte boundary, etc), and 'type' the type encoding of the
361 variable type. You can use sizeof(), log2(__alignof__()) and
362 @encode() to determine the right 'size', 'alignment' and 'type' for
363 your instance variable. For example, to add an instance variable
364 name "my_variable" and of type 'id', you can use:
366 class_addIvar (class, "my_variable", sizeof (id), log2 ( __alignof__ (id)),
367 @encode (id));
369 Return YES if the variable was added, and NO if not. In
370 particular, return NO if 'class_' is Nil, or a meta-class or a
371 class not in construction. Return Nil also if 'ivar_name' or
372 'type' is NULL, or 'size' is 0.
374 objc_EXPORT BOOL class_addIvar (Class class_, const char * ivar_name, size_t size,
375 unsigned char log_2_of_alignment, const char *type);
377 /* Return the name of the property. Return NULL if 'property' is
378 NULL. */
379 objc_EXPORT const char * property_getName (Property property);
381 /* Return the attributes of the property as a string. Return NULL if
382 'property' is NULL. */
383 objc_EXPORT const char * property_getAttributes (Property property);
385 /* Return the property with name 'propertyName' of the class 'class_'.
386 This function returns NULL if the required property can not be
387 found. Return NULL if 'class_' or 'propertyName' is NULL.
389 Note that the traditional ABI does not store the list of properties
390 of a class in a compiled module, so the traditional ABI will always
391 return NULL. */
392 objc_EXPORT Property class_getProperty (Class class_, const char *propertyName);
394 /* Return all the properties of the class. The return value
395 of the function is a pointer to an area, allocated with malloc(),
396 that contains all the properties of the class. It does not
397 include properties of superclasses. The list is terminated
398 by NULL. Optionally, if you pass a non-NULL
399 'numberOfReturnedIvars' pointer, the unsigned int that it points to
400 will be filled with the number of properties returned.
402 Note that the traditional ABI does not store the list of properties
403 of a class in a compiled module, so the traditional ABI will always
404 return an empty list. */
405 objc_EXPORT Property * class_copyPropertyList
406 (Class class_, unsigned int *numberOfReturnedProperties);
408 /* Return the ivar layout for class 'class_'.
410 At the moment this function always returns NULL. */
411 objc_EXPORT const char * class_getIvarLayout (Class class_);
413 /* Return the weak ivar layout for class 'class_'.
415 At the moment this function always returns NULL. */
416 objc_EXPORT const char * class_getWeakIvarLayout (Class class_);
418 /* Set the ivar layout for class 'class_'.
420 At the moment, this function does nothing. */
421 objc_EXPORT void class_setIvarLayout (Class class_, const char *layout);
423 /* Set the weak ivar layout for class 'class_'.
425 At the moment, this function does nothing. With the GNU runtime,
426 you should use class_ivar_set_gcinvisible () to hide variables from
427 the Garbage Collector. */
428 objc_EXPORT void class_setWeakIvarLayout (Class class_, const char *layout);
431 /** Implementation: the following functions are in class.c. */
433 /* Compatibility Note: The Apple/NeXT runtime does not have
434 objc_get_unknown_class_handler and
435 objc_setGetUnknownClassHandler(). They provide functionality that
436 the traditional GNU Objective-C Runtime API used to provide via the
437 _objc_lookup_class hook. */
439 /* An 'objc_get_unknown_class_handler' function is used by
440 objc_getClass() to get a class that is currently unknown to the
441 compiler. You could use it for example to have the class loaded by
442 dynamically loading a library. 'class_name' is the name of the
443 class. The function should return the Class object if it manages to
444 load the class, and Nil if not. */
445 typedef Class (*objc_get_unknown_class_handler)(const char *class_name);
447 /* Sets a new handler function for getting unknown classes (to be used
448 by objc_getClass () and related), and returns the previous one.
449 This function is not safe to call in a multi-threaded environment
450 because other threads may be trying to use the get unknown class
451 handler while you change it! */
452 objc_EXPORT
453 objc_get_unknown_class_handler
454 objc_setGetUnknownClassHandler (objc_get_unknown_class_handler new_handler);
456 /* Return the class with name 'name', if it is already registered with
457 the runtime. If it is not registered, and
458 objc_setGetUnknownClassHandler() has been called to set a handler
459 for unknown classes, the handler is called to give it a chance to
460 load the class in some other way. If the class is not known to the
461 runtime and the handler is not set or returns Nil, objc_getClass()
462 returns Nil. */
463 objc_EXPORT Class objc_getClass (const char *name);
465 /* Return the class with name 'name', if it is already registered with
466 the runtime. Return Nil if not. This function does not call the
467 objc_get_unknown_class_handler function if the class is not
468 found. */
469 objc_EXPORT Class objc_lookUpClass (const char *name);
471 /* Return the meta class associated to the class with name 'name', if
472 it is already registered with the runtime. First, it finds the
473 class using objc_getClass(). Then, it returns the associated meta
474 class. If the class could not be found using objc_getClass(),
475 returns Nil. */
476 objc_EXPORT Class objc_getMetaClass (const char *name);
478 /* This is identical to objc_getClass(), but if the class is not found,
479 it aborts the process instead of returning Nil. */
480 objc_EXPORT Class objc_getRequiredClass (const char *name);
482 /* If 'returnValue' is NULL, 'objc_getClassList' returns the number of
483 classes currently registered with the runtime. If 'returnValue' is
484 not NULL, it should be a (Class *) pointer to an area of memory
485 which can contain up to 'maxNumberOfClassesToReturn' Class records.
486 'objc_getClassList' will fill the area pointed to by 'returnValue'
487 with all the Classes registered with the runtime (or up to
488 maxNumberOfClassesToReturn if there are more than
489 maxNumberOfClassesToReturn). The function return value is the
490 number of classes actually returned in 'returnValue'. */
491 objc_EXPORT int objc_getClassList (Class *returnValue, int maxNumberOfClassesToReturn);
493 /* Compatibility Note: The Apple/NeXT runtime also has
495 Class objc_getFutureClass (const char *name);
496 void objc_setFutureClass (Class class_, const char *name);
498 the documentation is unclear on what they are supposed to do, and
499 the GNU Objective-C Runtime currently does not provide them. */
501 /* Return the name of the class 'class_', or the string "nil" if the
502 class_ is Nil. */
503 objc_EXPORT const char * class_getName (Class class_);
505 /* Return YES if 'class_' is a meta class, and NO if not. If 'class_'
506 is Nil, return NO. */
507 objc_EXPORT BOOL class_isMetaClass (Class class_);
509 /* Return the superclass of 'class_'. If 'class_' is Nil, or it is a
510 root class, return Nil. If 'class_' is a class being constructed,
511 that is, a class returned by objc_allocateClassPair() but before it
512 has been registered with the runtime using
513 objc_registerClassPair(), return Nil. */
514 objc_EXPORT Class class_getSuperclass (Class class_);
516 /* Return the 'version' number of the class, which is an integer that
517 can be used to track changes in the class API, methods and
518 variables. If class_ is Nil, return 0. If class_ is not Nil, the
519 version is 0 unless class_setVersion() has been called to set a
520 different one.
522 Please note that internally the version is a long, but the API only
523 allows you to set and retrieve int values. */
524 objc_EXPORT int class_getVersion (Class class_);
526 /* Set the 'version' number of the class, which is an integer that can
527 be used to track changes in the class API, methods and variables.
528 If 'class_' is Nil, does nothing.
530 This is typically used internally by "Foundation" libraries such as
531 GNUstep Base to support serialization / deserialization of objects
532 that work across changes in the classes. If you are using such a
533 library, you probably want to use their versioning API, which may
534 be based on this one, but is integrated with the rest of the
535 library.
537 Please note that internally the version is a long, but the API only
538 allows you to set and retrieve int values. */
539 objc_EXPORT void class_setVersion (Class class_, int version);
541 /* Return the size in bytes (a byte is the size of a char) of an
542 instance of the class. If class_ is Nil, return 0; else it return
543 a non-zero number (since the 'isa' instance variable is required
544 for all classes). */
545 objc_EXPORT size_t class_getInstanceSize (Class class_);
547 /* Change the implementation of the method. It also searches all
548 classes for any class implementing the method, and replaces the
549 existing implementation with the new one. For that to work,
550 'method' must be a method returned by class_getInstanceMethod() or
551 class_getClassMethod() as the matching is done by comparing the
552 pointers; in that case, only the implementation in the class is
553 modified. Return the previous implementation that has been
554 replaced. If method or implementation is NULL, do nothing and
555 return NULL. */
556 objc_EXPORT IMP
557 method_setImplementation (Method method, IMP implementation);
559 /* Swap the implementation of two methods in a single, atomic
560 operation. This is equivalent to getting the implementation of
561 each method and then calling method_setImplementation() on the
562 other one. For this to work, the two methods must have been
563 returned by class_getInstanceMethod() or class_getClassMethod().
564 If 'method_a' or 'method_b' is NULL, do nothing. */
565 objc_EXPORT void
566 method_exchangeImplementations (Method method_a, Method method_b);
568 /* Create a new class/meta-class pair. This function is called to
569 create a new class at runtime. The class is created with
570 superclass 'superclass' (use 'Nil' to create a new root class) and
571 name 'class_name'. 'extraBytes' can be used to specify some extra
572 space for indexed variables to be added at the end of the class and
573 meta-class objects (it is recommended that you set extraBytes to
574 0). Once you have created the class, it is not usable yet. You
575 need to add any instance variables (by using class_addIvar()), any
576 instance methods (by using class_addMethod()) and any class methods
577 (by using class_addMethod() on the meta-class, as in
578 class_addMethod (object_getClass (class), method)) that are
579 required, and then you need to call objc_registerClassPair() to
580 activate the class. If you need to create a hierarchy of classes,
581 you need to create and register them one at a time. You can not
582 create a new class using another class in construction as
583 superclass. Return Nil if 'class-name' is NULL or if a class with
584 that name already exists or 'superclass' is a class still in
585 construction.
587 Implementation Note: in the GNU runtime, allocating a class pair
588 only creates the structures for the class pair, but does not
589 register anything with the runtime. The class is registered with
590 the runtime only when objc_registerClassPair() is called. In
591 particular, if a class is in construction, objc_getClass() will not
592 find it, the superclass will not know about it,
593 class_getSuperclass() will return Nil and another thread may
594 allocate a class pair with the same name; the conflict will only be
595 detected when the classes are registered with the runtime.
597 objc_EXPORT Class
598 objc_allocateClassPair (Class super_class, const char *class_name,
599 size_t extraBytes);
601 /* Register a class pair that was created with
602 objc_allocateClassPair(). After you register a class, you can no
603 longer make changes to its instance variables, but you can start
604 creating instances of it. Do nothing if 'class_' is NULL or if it
605 is not a class allocated by objc_allocateClassPair() and still in
606 construction. */
607 objc_EXPORT void
608 objc_registerClassPair (Class class_);
610 /* Dispose of a class pair created using objc_allocateClassPair().
611 Call this function if you started creating a new class with
612 objc_allocateClassPair() but then want to abort the process. You
613 should not access 'class_' after calling this method. Note that if
614 'class_' has already been registered with the runtime via
615 objc_registerClassPair(), this function does nothing; you can only
616 dispose of class pairs that are still being constructed. Do
617 nothing if class is 'Nil' or if 'class_' is not a class being
618 constructed. */
619 objc_EXPORT void
620 objc_disposeClassPair (Class class_);
622 /* Compatibility Note: The Apple/NeXT runtime has the function
623 objc_duplicateClass () but it's undocumented. The GNU runtime does
624 not have it. */
627 /** Implementation: the following functions are in sendmsg.c. */
629 /* Return the instance method with selector 'selector' of class
630 'class_', or NULL if the class (or one of its superclasses) does
631 not implement the method. Return NULL if class_ is Nil or selector
632 is NULL. Calling this function may trigger a call to
633 +resolveInstanceMethod:, but does not return a forwarding
634 function. */
635 objc_EXPORT Method class_getInstanceMethod (Class class_, SEL selector);
637 /* Return the class method with selector 'selector' of class 'class_',
638 or NULL if the class (or one of its superclasses) does not
639 implement the method. Return NULL if class_ is Nil or selector is
640 NULL. Calling this function may trigger a call to
641 +resolveClassMethod:, but does not return a forwarding
642 function. */
643 objc_EXPORT Method class_getClassMethod (Class class_, SEL selector);
645 /* Return the IMP (pointer to the function implementing a method) for
646 the instance method with selector 'selector' in class 'class_'.
647 This is the same routine that is used while messaging, and should
648 be very fast. Note that you most likely would need to cast the
649 return function pointer to a function pointer with the appropriate
650 arguments and return type before calling it. To get a class
651 method, you can pass the meta-class as the class_ argument (ie, use
652 class_getMethodImplementation (object_getClass (class_),
653 selector)). Return NULL if class_ is Nil or selector is NULL.
654 This function first looks for an existing method; if it is not
655 found, it calls +resolveClassMethod: or +resolveInstanceMethod:
656 (depending on whether a class or instance method is being looked
657 up) if it is implemented. If the method returns YES, then it tries
658 the look up again (the assumption being that +resolveClassMethod:
659 or resolveInstanceMethod: will add the method using
660 class_addMethod()). If it is still not found, it returns a
661 forwarding function. */
662 objc_EXPORT IMP class_getMethodImplementation (Class class_, SEL selector);
664 /* Compatibility Note: the Apple/NeXT runtime has the function
665 class_getMethodImplementation_stret () which currently does not
666 exist on the GNU runtime because the messaging implementation is
667 different. */
669 /* Return YES if class 'class_' has an instance method implementing
670 selector 'selector', and NO if not. Return NO if class_ is Nil or
671 selector is NULL. If you need to check a class method, use the
672 meta-class as the class_ argument (ie, use class_respondsToSelector
673 (object_getClass (class_), selector)). */
674 objc_EXPORT BOOL class_respondsToSelector (Class class_, SEL selector);
676 /* Add a method to a class. Use this function to add a new method to
677 a class (potentially overriding a method with the same selector in
678 the superclass); if you want to modify an existing method, use
679 method_setImplementation() instead (or class_replaceMethod ()).
680 This method adds an instance method to 'class_'; to add a class
681 method, get the meta class first, then add the method to the meta
682 class, that is, use
684 class_addMethod (object_getClass (class_), selector,
685 implementation, type);
687 Return YES if the method was added, and NO if not. Do nothing if
688 one of the arguments is NULL. */
689 objc_EXPORT BOOL class_addMethod (Class class_, SEL selector, IMP implementation,
690 const char *method_types);
692 /* Replace a method in a class. If the class already have a method
693 with this 'selector', find it and use method_setImplementation() to
694 replace the implementation with 'implementation' (method_types is
695 ignored in that case). If the class does not already have a method
696 with this 'selector', call 'class_addMethod() to add it.
698 Return the previous implementation of the method, or NULL if none
699 was found. Return NULL if any of the arguments is NULL. */
700 objc_EXPORT IMP class_replaceMethod (Class class_, SEL selector, IMP implementation,
701 const char *method_types);
704 /** Implementation: the following functions are in methods.c. */
706 /* Return the selector for method 'method'. Return NULL if 'method'
707 is NULL.
709 This function is misnamed; it should be called
710 'method_getSelector'. To get the actual name, get the selector,
711 then the name from the selector (ie, use sel_getName
712 (method_getName (method))). */
713 objc_EXPORT SEL method_getName (Method method);
715 /* Return the IMP of the method. Return NULL if 'method' is NULL. */
716 objc_EXPORT IMP method_getImplementation (Method method);
718 /* Return the type encoding of the method. Return NULL if 'method' is
719 NULL. */
720 objc_EXPORT const char * method_getTypeEncoding (Method method);
722 /* Return a method description for the method. Return NULL if
723 'method' is NULL. */
724 objc_EXPORT struct objc_method_description * method_getDescription (Method method);
726 /* Return all the instance methods of the class. The return value of
727 the function is a pointer to an area, allocated with malloc(), that
728 contains all the instance methods of the class. It does not
729 include instance methods of superclasses. The list is terminated
730 by NULL. Optionally, if you pass a non-NULL
731 'numberOfReturnedMethods' pointer, the unsigned int that it points
732 to will be filled with the number of instance methods returned. To
733 get the list of class methods, pass the meta-class in the 'class_'
734 argument, (ie, use class_copyMethodList (object_getClass (class_),
735 &numberOfReturnedMethods)). */
736 objc_EXPORT Method * class_copyMethodList (Class class_, unsigned int *numberOfReturnedMethods);
739 /** Implementation: the following functions are in encoding.c. */
741 /* Return the number of arguments that the method 'method' expects.
742 Note that all methods need two implicit arguments ('self' for the
743 receiver, and '_cmd' for the selector). Return 0 if 'method' is
744 NULL. */
745 objc_EXPORT unsigned int method_getNumberOfArguments (Method method);
747 /* Return the string encoding for the return type of method 'method'.
748 The string is a standard zero-terminated string in an area of
749 memory allocated with malloc(); you should free it with free() when
750 you finish using it. Return an empty string if method is NULL. */
751 objc_EXPORT char * method_copyReturnType (Method method);
753 /* Return the string encoding for the argument type of method
754 'method', argument number 'argumentNumber' ('argumentNumber' is 0
755 for self, 1 for _cmd, and 2 or more for the additional arguments if
756 any). The string is a standard zero-terminated string in an area
757 of memory allocated with malloc(); you should free it with free()
758 when you finish using it. Return an empty string if method is NULL
759 or if 'argumentNumber' refers to a non-existing argument. */
760 objc_EXPORT char * method_copyArgumentType (Method method, unsigned int argumentNumber);
762 /* Return the string encoding for the return type of method 'method'.
763 The string is returned by copying it into the supplied
764 'returnValue' string, which is of size 'returnValueSize'. No more
765 than 'returnValueSize' characters are copied; if the encoding is
766 smaller than 'returnValueSize', the rest of 'returnValue' is filled
767 with zeros. If it is bigger, it is truncated (and would not be
768 zero-terminated). You should supply a big enough
769 'returnValueSize'. If the method is NULL, returnValue is set to a
770 string of zeros. */
771 objc_EXPORT void method_getReturnType (Method method, char *returnValue,
772 size_t returnValueSize);
774 /* Return the string encoding for the argument type of method
775 'method', argument number 'argumentNumber' ('argumentNumber' is 0
776 for self, 1 for _cmd, and 2 or more for the additional arguments if
777 any). The string is returned by copying it into the supplied
778 'returnValue' string, which is of size 'returnValueSize'. No more
779 than 'returnValueSize' characters are copied; if the encoding is
780 smaller than 'returnValueSize', the rest of 'returnValue' is filled
781 with zeros. If it is bigger, it is truncated (and would not be
782 zero-terminated). You should supply a big enough
783 'returnValueSize'. If the method is NULL, returnValue is set to a
784 string of zeros. */
785 objc_EXPORT void method_getArgumentType (Method method, unsigned int argumentNumber,
786 char *returnValue, size_t returnValueSize);
789 /** Implementation: the following functions are in protocols.c. */
791 /* Return the protocol with name 'name', or nil if it the protocol is
792 not known to the runtime. */
793 objc_EXPORT Protocol *objc_getProtocol (const char *name);
795 /* Return all the protocols known to the runtime. The return value of
796 the function is a pointer to an area, allocated with malloc(), that
797 contains all the protocols known to the runtime; the list is
798 terminated by NULL. You should free this area using free() once
799 you no longer need it. Optionally, if you pass a non-NULL
800 'numberOfReturnedProtocols' pointer, the unsigned int that it
801 points to will be filled with the number of protocols returned. If
802 there are no protocols known to the runtime, NULL is returned. */
803 objc_EXPORT Protocol **objc_copyProtocolList (unsigned int *numberOfReturnedProtocols);
805 /* Add a protocol to a class, and return YES if it was done
806 succesfully, and NO if not. At the moment, NO should only happen
807 if class_ or protocol are nil, if the protocol is not a Protocol
808 object or if the class already conforms to the protocol. */
809 objc_EXPORT BOOL class_addProtocol (Class class_, Protocol *protocol);
811 /* Return YES if the class 'class_' conforms to Protocol 'protocol',
812 and NO if not. This function does not check superclasses; if you
813 want to check for superclasses (in the way that [NSObject
814 +conformsToProtocol:] does) you need to iterate over the class
815 hierarchy using class_getSuperclass(), and call
816 class_conformsToProtocol() for each of them. */
817 objc_EXPORT BOOL class_conformsToProtocol (Class class_, Protocol *protocol);
819 /* Return all the protocols that the class conforms to. The return
820 value of the function is a pointer to an area, allocated with
821 malloc(), that contains all the protocols formally adopted by the
822 class. It does not include protocols adopted by superclasses. The
823 list is terminated by NULL. Optionally, if you pass a non-NULL
824 'numberOfReturnedProtocols' pointer, the unsigned int that it
825 points to will be filled with the number of protocols returned.
826 This function does not return protocols that superclasses conform
827 to. */
828 objc_EXPORT Protocol **class_copyProtocolList (Class class_, unsigned int *numberOfReturnedProtocols);
830 /* Return YES if protocol 'protocol' conforms to protocol
831 'anotherProtocol', and NO if not. Note that if one of the two
832 protocols is nil, it returns NO. */
833 objc_EXPORT BOOL protocol_conformsToProtocol (Protocol *protocol, Protocol *anotherProtocol);
835 /* Return YES if protocol 'protocol' is the same as protocol
836 'anotherProtocol', and 'NO' if not. Note that it returns YES if
837 the two protocols are both nil. */
838 objc_EXPORT BOOL protocol_isEqual (Protocol *protocol, Protocol *anotherProtocol);
840 /* Return the name of protocol 'protocol'. If 'protocol' is nil or is
841 not a Protocol, return NULL. */
842 objc_EXPORT const char *protocol_getName (Protocol *protocol);
844 /* Return the method description for the method with selector
845 'selector' in protocol 'protocol'; if 'requiredMethod' is YES, the
846 function searches the list of required methods; if NO, the list of
847 optional methods. If 'instanceMethod' is YES, the function search
848 for an instance method; if NO, for a class method. If there is no
849 matching method, an objc_method_description structure with both
850 name and types set to NULL is returned. This function will only
851 find methods that are directly declared in the protocol itself, not
852 in other protocols that this protocol adopts.
854 Note that the traditional ABI does not store the list of optional
855 methods of a protocol in a compiled module, so the traditional ABI
856 will always return (NULL, NULL) when requiredMethod == NO. */
857 objc_EXPORT struct objc_method_description protocol_getMethodDescription (Protocol *protocol,
858 SEL selector,
859 BOOL requiredMethod,
860 BOOL instanceMethod);
862 /* Return the method descriptions of all the methods of the protocol.
863 The return value of the function is a pointer to an area, allocated
864 with malloc(), that contains all the method descriptions of the
865 methods of the protocol. It does not recursively include methods
866 of the protocols adopted by this protocol. The list is terminated
867 by a NULL objc_method_description (one with both fields set to
868 NULL). Optionally, if you pass a non-NULL
869 'numberOfReturnedMethods' pointer, the unsigned int that it points
870 to will be filled with the number of properties returned.
872 Note that the traditional ABI does not store the list of optional
873 methods of a protocol in a compiled module, so the traditional ABI
874 will always return an empty list if requiredMethod is set to
875 NO. */
876 objc_EXPORT struct objc_method_description *protocol_copyMethodDescriptionList (Protocol *protocol,
877 BOOL requiredMethod,
878 BOOL instanceMethod,
879 unsigned int *numberOfReturnedMethods);
881 /* Return the property with name 'propertyName' of the protocol
882 'protocol'. If 'requiredProperty' is YES, the function searches
883 the list of required properties; if NO, the list of optional
884 properties. If 'instanceProperty' is YES, the function searches
885 the list of instance properties; if NO, the list of class
886 properties. At the moment, optional properties and class
887 properties are not part of the Objective-C language, so both
888 'requiredProperty' and 'instanceProperty' should be set to YES.
889 This function returns NULL if the required property can not be
890 found.
892 Note that the traditional ABI does not store the list of properties
893 of a protocol in a compiled module, so the traditional ABI will
894 always return NULL. */
895 objc_EXPORT Property protocol_getProperty (Protocol *protocol, const char *propertyName,
896 BOOL requiredProperty, BOOL instanceProperty);
898 /* Return all the properties of the protocol. The return value of the
899 function is a pointer to an area, allocated with malloc(), that
900 contains all the properties of the protocol. It does not
901 recursively include properties of the protocols adopted by this
902 protocol. The list is terminated by NULL. Optionally, if you pass
903 a non-NULL 'numberOfReturnedProperties' pointer, the unsigned int
904 that it points to will be filled with the number of properties
905 returned.
907 Note that the traditional ABI does not store the list of properties
908 of a protocol in a compiled module, so the traditional ABI will
909 always return NULL and store 0 in numberOfReturnedProperties. */
910 objc_EXPORT Property *protocol_copyPropertyList (Protocol *protocol, unsigned int *numberOfReturnedProperties);
912 /* Return all the protocols that the protocol conforms to. The return
913 value of the function is a pointer to an area, allocated with
914 malloc(), that contains all the protocols formally adopted by the
915 protocol. It does not recursively include protocols adopted by the
916 protocols adopted by this protocol. The list is terminated by
917 NULL. Optionally, if you pass a non-NULL
918 'numberOfReturnedProtocols' pointer, the unsigned int that it
919 points to will be filled with the number of protocols returned. */
920 objc_EXPORT Protocol **protocol_copyProtocolList (Protocol *protocol, unsigned int *numberOfReturnedProtocols);
923 /** Implementation: the following hook is in init.c. */
925 /* This is a hook which is called by __objc_exec_class every time a
926 class or a category is loaded into the runtime. This may e.g. help
927 a dynamic loader determine the classes that have been loaded when
928 an object file is dynamically linked in. */
929 objc_EXPORT void (*_objc_load_callback)(Class _class, struct objc_category *category);
932 /** Implementation: the following functions are in objc-foreach.c. */
934 /* 'objc_enumerationMutation()' is called when a collection is
935 mutated while being "fast enumerated". That is a hard error, and
936 objc_enumerationMutation is called to deal with it. 'collection'
937 is the collection object that was mutated during an enumeration.
939 objc_enumerationMutation() will invoke the mutation handler if any
940 is set. Then, it will abort the program.
942 Compatibility note: the Apple runtime will not abort the program
943 after calling the mutation handler. */
944 objc_EXPORT void objc_enumerationMutation (id collection);
946 /* 'objc_set_enumeration_mutation_handler' can be used to set a
947 function that will be called (instead of aborting) when a fast
948 enumeration is mutated during enumeration. The handler will be
949 called with the 'collection' being mutated as the only argument and
950 it should not return; it should either exit the program, or could
951 throw an exception. The recommended implementation is to throw an
952 exception - the user can then use exception handlers to deal with
955 This function is not thread safe (other threads may be trying to
956 invoke the enumeration mutation handler while you are changing it!)
957 and should be called during during the program initialization
958 before threads are started. It is mostly reserved for "Foundation"
959 libraries; in the case of GNUstep, GNUstep Base may be using this
960 function to improve the standard enumeration mutation handling.
961 You probably shouldn't use this function unless you are writing
962 your own Foundation library. */
963 objc_EXPORT void objc_setEnumerationMutationHandler (void (*handler)(id));
965 /* This structure (used during fast enumeration) is automatically
966 defined by the compiler (it is as if this definition was always
967 included in all Objective-C files). Note that it is usually
968 defined again with the name of NSFastEnumeration by "Foundation"
969 libraries such as GNUstep Base. And if NSFastEnumeration is
970 defined, the compiler will use it instead of
971 __objcFastEnumerationState when doing fast enumeration. */
973 struct __objcFastEnumerationState
975 unsigned long state;
976 id *itemsPtr;
977 unsigned long *mutationsPtr;
978 unsigned long extra[5];
983 /* Compatibility Note: The Apple/NeXT runtime has the functions
984 objc_copyImageNames (), class_getImageName () and
985 objc_copyClassNamesForImage () but they are undocumented. The GNU
986 runtime does not have them at the moment. */
988 /* Compatibility Note: The Apple/NeXT runtime has the functions
989 objc_setAssociatedObject (), objc_getAssociatedObject (),
990 objc_removeAssociatedObjects () and the objc_AssociationPolicy type
991 and related enum. The GNU runtime does not have them yet.
992 TODO: Implement them. */
994 /* Compatibility Note: The Apple/NeXT runtime has the function
995 objc_setForwardHandler (). The GNU runtime does not have it
996 because messaging (and, in particular, forwarding) works in a
997 different (incompatible) way with the GNU runtime. If you need to
998 customize message forwarding at the Objective-C runtime level (that
999 is, if you are implementing your own "Foundation" library such as
1000 GNUstep Base on top of the Objective-C runtime), in objc/message.h
1001 there are hooks (that work in the framework of the GNU runtime) to
1002 do so. */
1005 /** Implementation: the following functions are in memory.c. */
1007 /* Traditional GNU Objective-C Runtime functions that are used for
1008 memory allocation and disposal. These functions are used in the
1009 same way as you use malloc, realloc, calloc and free and make sure
1010 that memory allocation works properly with the garbage
1011 collector.
1013 Compatibility Note: these functions are not available with the
1014 Apple/NeXT runtime. */
1016 objc_EXPORT void *objc_malloc(size_t size);
1018 /* FIXME: Shouldn't the following be called objc_malloc_atomic ? The
1019 GC function is GC_malloc_atomic() which makes sense.
1021 objc_EXPORT void *objc_atomic_malloc(size_t size);
1023 objc_EXPORT void *objc_realloc(void *mem, size_t size);
1025 objc_EXPORT void *objc_calloc(size_t nelem, size_t size);
1027 objc_EXPORT void objc_free(void *mem);
1030 /** Implementation: the following functions are in gc.c. */
1032 /* The GNU Objective-C Runtime has a different implementation of
1033 garbage collection.
1035 Compatibility Note: these functions are not available with the
1036 Apple/NeXT runtime. */
1038 /* Mark the instance variable as inaccessible to the garbage
1039 collector. */
1040 objc_EXPORT void class_ivar_set_gcinvisible (Class _class,
1041 const char* ivarname,
1042 BOOL gcInvisible);
1045 /** Implementation: the following functions are in encoding.c. */
1047 /* Traditional GNU Objective-C Runtime functions that are currently
1048 used to implement method forwarding.
1050 Compatibility Note: these functions are not available with the
1051 Apple/NeXT runtime. */
1053 /* Return the size of a variable which has the specified 'type'
1054 encoding. */
1055 objc_EXPORT int objc_sizeof_type (const char *type);
1057 /* Return the align of a variable which has the specified 'type'
1058 encoding. */
1059 objc_EXPORT int objc_alignof_type (const char *type);
1061 /* Return the aligned size of a variable which has the specified
1062 'type' encoding. The aligned size is the size rounded up to the
1063 nearest alignment. */
1064 objc_EXPORT int objc_aligned_size (const char *type);
1066 /* Return the promoted size of a variable which has the specified
1067 'type' encoding. This is the size rounded up to the nearest
1068 integral of the wordsize, taken to be the size of a void *. */
1069 objc_EXPORT int objc_promoted_size (const char *type);
1072 /* The following functions are used when parsing the type encoding of
1073 methods, to skip over parts that are ignored. They take as
1074 argument a pointer to a location inside the type encoding of a
1075 method (which is a string) and return a new pointer, pointing to a
1076 new location inside the string after having skipped the unwanted
1077 information. */
1079 /* Skip some type qualifiers (_C_CONST, _C_IN, etc). These may
1080 eventually precede typespecs occurring in method prototype
1081 encodings. */
1082 objc_EXPORT const char *objc_skip_type_qualifiers (const char *type);
1084 /* Skip one typespec element (_C_CLASS, _C_SEL, etc). If the typespec
1085 is prepended by type qualifiers, these are skipped as well. */
1086 objc_EXPORT const char *objc_skip_typespec (const char *type);
1088 /* Skip an offset. */
1089 objc_EXPORT const char *objc_skip_offset (const char *type);
1091 /* Skip an argument specification (ie, skipping a typespec, which may
1092 include qualifiers, and an offset too). */
1093 objc_EXPORT const char *objc_skip_argspec (const char *type);
1095 /* Read type qualifiers (_C_CONST, _C_IN, etc) from string 'type'
1096 (stopping at the first non-type qualifier found) and return an
1097 unsigned int which is the logical OR of all the corresponding flags
1098 (_F_CONST, _F_IN etc). */
1099 objc_EXPORT unsigned objc_get_type_qualifiers (const char *type);
1102 /* Note that the following functions work for very simple structures,
1103 but get easily confused by more complicated ones (for example,
1104 containing vectors). A better solution is required. These
1105 functions are likely to change in the next GCC release. */
1107 /* The following three functions can be used to determine how a
1108 structure is laid out by the compiler. For example:
1110 struct objc_struct_layout layout;
1111 int i;
1113 objc_layout_structure (type, &layout);
1114 while (objc_layout_structure_next_member (&layout))
1116 int position, align;
1117 const char *type;
1119 objc_layout_structure_get_info (&layout, &position, &align, &type);
1120 printf ("element %d has offset %d, alignment %d\n",
1121 i++, position, align);
1124 These functions are used by objc_sizeof_type and objc_alignof_type
1125 functions to compute the size and alignment of structures. The
1126 previous method of computing the size and alignment of a structure
1127 was not working on some architectures, particulary on AIX, and in
1128 the presence of bitfields inside the structure. */
1129 struct objc_struct_layout
1131 const char *original_type;
1132 const char *type;
1133 const char *prev_type;
1134 unsigned int record_size;
1135 unsigned int record_align;
1138 objc_EXPORT void objc_layout_structure (const char *type,
1139 struct objc_struct_layout *layout);
1140 objc_EXPORT BOOL objc_layout_structure_next_member (struct objc_struct_layout *layout);
1141 objc_EXPORT void objc_layout_finish_structure (struct objc_struct_layout *layout,
1142 unsigned int *size,
1143 unsigned int *align);
1144 objc_EXPORT void objc_layout_structure_get_info (struct objc_struct_layout *layout,
1145 unsigned int *offset,
1146 unsigned int *align,
1147 const char **type);
1149 #ifdef __cplusplus
1151 #endif /* __cplusplus */
1153 #endif