]> git.ipfire.org Git - thirdparty/gcc.git/blob - libobjc/objc/runtime.h
In gcc/testsuite/:
[thirdparty/gcc.git] / libobjc / objc / runtime.h
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>
4
5 This file is part of GCC.
6
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.
11
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.
16
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.
20
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/>. */
25
26 #ifndef __objc_runtime_INCLUDE_GNU
27 #define __objc_runtime_INCLUDE_GNU
28
29 /*
30 This file declares the "modern" GNU Objective-C Runtime API.
31 Include this file to use it.
32
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.
37
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.
43 */
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
47
48 /* TODO: This file is incomplete. */
49
50 #include "objc.h"
51 #include "objc-decls.h"
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif /* __cplusplus */
56
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;
60
61 /* A 'Property' represents a property. It holds information about the
62 name of the property, and its attributes.
63
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;
69
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;
73
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;
78
79 /* 'Protocol' is defined in objc/objc.h (which is included by this
80 file). */
81
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
90 {
91 SEL name; /* Selector (name and signature) */
92 char *types; /* Type encoding */
93 };
94
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'
127
128 /* _C_ATOM is never generated by the compiler. You can treat it as
129 equivalent to "*". */
130 #define _C_ATOM '%'
131
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 '|'
142
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
152
153
154 /** Implementation: the following functions are defined inline. */
155
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)
166 {
167 if (object != nil)
168 return object->class_pointer;
169 else
170 return Nil;
171 }
172
173
174 /** Implementation: the following functions are in selector.c. */
175
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);
179
180 /* Return the type of a given selector.
181
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);
186
187 /* This is the same as sel_registerName (). Please use
188 sel_registerName () instead. */
189 objc_EXPORT SEL sel_getUid (const char *name);
190
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);
195
196 /* Register a selector with a given name and types. If a selector
197 with this name and types already exists, it is returned.
198
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);
203
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);
207
208
209 /** Implementation: the following functions are in objects.c. */
210
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);
218
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);
223
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);
229
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);
233
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_);
237
238
239 /** Implementation: the following functions are in ivars.c. */
240
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);
245
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.
249
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);
253
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);
258
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);
268
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);
276
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);
283
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);
290
291 /* Return the name of the instance variable. Return NULL if
292 'variable' is NULL. */
293 objc_EXPORT const char * ivar_getName (Ivar variable);
294
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);
298
299 /* Return the type encoding of the variable. Return NULL if
300 'variable' is NULL. */
301 objc_EXPORT const char * ivar_getTypeEncoding (Ivar variable);
302
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);
314
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:
326
327 class_addIvar (class, "my_variable", sizeof (id), __alignof__ (id),
328 @encode (id));
329
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.
334 */
335 objc_EXPORT BOOL class_addIvar (Class class_, const char * ivar_name, size_t size,
336 unsigned char alignment, const char *type);
337
338 /* Return the name of the property. Return NULL if 'property' is
339 NULL. */
340 objc_EXPORT const char * property_getName (Property property);
341
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);
345
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.
349
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);
354
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.
362
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);
368
369 /* Return the ivar layout for class 'class_'.
370
371 At the moment this function always returns NULL. */
372 objc_EXPORT const char * class_getIvarLayout (Class class_);
373
374 /* Return the weak ivar layout for class 'class_'.
375
376 At the moment this function always returns NULL. */
377 objc_EXPORT const char * class_getWeakIvarLayout (Class class_);
378
379 /* Set the ivar layout for class 'class_'.
380
381 At the moment, this function does nothing. */
382 objc_EXPORT void class_setIvarLayout (Class class_, const char *layout);
383
384 /* Set the weak ivar layout for class 'class_'.
385
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);
390
391
392 /** Implementation: the following functions are in class.c. */
393
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. */
399
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);
407
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_EXPORT
414 objc_get_unknown_class_handler
415 objc_setGetUnknownClassHandler (objc_get_unknown_class_handler new_handler);
416
417 /* Return the class with name 'name', if it is already registered with
418 the runtime. If it is not registered, and
419 objc_setGetUnknownClassHandler() has been called to set a handler
420 for unknown classes, the handler is called to give it a chance to
421 load the class in some other way. If the class is not known to the
422 runtime and the handler is not set or returns Nil, objc_getClass()
423 returns Nil. */
424 objc_EXPORT Class objc_getClass (const char *name);
425
426 /* Return the class with name 'name', if it is already registered with
427 the runtime. Return Nil if not. This function does not call the
428 objc_get_unknown_class_handler function if the class is not
429 found. */
430 objc_EXPORT Class objc_lookUpClass (const char *name);
431
432 /* Return the meta class associated to the class with name 'name', if
433 it is already registered with the runtime. First, it finds the
434 class using objc_getClass(). Then, it returns the associated meta
435 class. If the class could not be found using objc_getClass(),
436 returns Nil. */
437 objc_EXPORT Class objc_getMetaClass (const char *name);
438
439 /* This is identical to objc_getClass(), but if the class is not found,
440 it aborts the process instead of returning Nil. */
441 objc_EXPORT Class objc_getRequiredClass (const char *name);
442
443 /* If 'returnValue' is NULL, 'objc_getClassList' returns the number of
444 classes currently registered with the runtime. If 'returnValue' is
445 not NULL, it should be a (Class *) pointer to an area of memory
446 which can contain up to 'maxNumberOfClassesToReturn' Class records.
447 'objc_getClassList' will fill the area pointed to by 'returnValue'
448 with all the Classes registered with the runtime (or up to
449 maxNumberOfClassesToReturn if there are more than
450 maxNumberOfClassesToReturn). The function return value is the
451 number of classes actually returned in 'returnValue'. */
452 objc_EXPORT int objc_getClassList (Class *returnValue, int maxNumberOfClassesToReturn);
453
454 /* Compatibility Note: The Apple/NeXT runtime also has
455
456 Class objc_getFutureClass (const char *name);
457 void objc_setFutureClass (Class class_, const char *name);
458
459 the documentation is unclear on what they are supposed to do, and
460 the GNU Objective-C Runtime currently does not provide them. */
461
462 /* Return the name of the class 'class_', or the string "nil" if the
463 class_ is Nil. */
464 objc_EXPORT const char * class_getName (Class class_);
465
466 /* Return YES if 'class_' is a meta class, and NO if not. If 'class_'
467 is Nil, return NO. */
468 objc_EXPORT BOOL class_isMetaClass (Class class_);
469
470 /* Return the superclass of 'class_'. If 'class_' is Nil, or it is a
471 root class, return Nil. If 'class_' is a class being constructed,
472 that is, a class returned by objc_allocateClassPair() but before it
473 has been registered with the runtime using
474 objc_registerClassPair(), return Nil. */
475 objc_EXPORT Class class_getSuperclass (Class class_);
476
477 /* Return the 'version' number of the class, which is an integer that
478 can be used to track changes in the class API, methods and
479 variables. If class_ is Nil, return 0. If class_ is not Nil, the
480 version is 0 unless class_setVersion() has been called to set a
481 different one.
482
483 Please note that internally the version is a long, but the API only
484 allows you to set and retrieve int values. */
485 objc_EXPORT int class_getVersion (Class class_);
486
487 /* Set the 'version' number of the class, which is an integer that can
488 be used to track changes in the class API, methods and variables.
489 If 'class_' is Nil, does nothing.
490
491 This is typically used internally by "Foundation" libraries such as
492 GNUstep Base to support serialization / deserialization of objects
493 that work across changes in the classes. If you are using such a
494 library, you probably want to use their versioning API, which may
495 be based on this one, but is integrated with the rest of the
496 library.
497
498 Please note that internally the version is a long, but the API only
499 allows you to set and retrieve int values. */
500 objc_EXPORT void class_setVersion (Class class_, int version);
501
502 /* Return the size in bytes (a byte is the size of a char) of an
503 instance of the class. If class_ is Nil, return 0; else it return
504 a non-zero number (since the 'isa' instance variable is required
505 for all classes). */
506 objc_EXPORT size_t class_getInstanceSize (Class class_);
507
508 /* Change the implementation of the method. It also searches all
509 classes for any class implementing the method, and replaces the
510 existing implementation with the new one. For that to work,
511 'method' must be a method returned by class_getInstanceMethod() or
512 class_getClassMethod() as the matching is done by comparing the
513 pointers; in that case, only the implementation in the class is
514 modified. Return the previous implementation that has been
515 replaced. If method or implementation is NULL, do nothing and
516 return NULL. */
517 objc_EXPORT IMP
518 method_setImplementation (Method method, IMP implementation);
519
520 /* Swap the implementation of two methods in a single, atomic
521 operation. This is equivalent to getting the implementation of
522 each method and then calling method_setImplementation() on the
523 other one. For this to work, the two methods must have been
524 returned by class_getInstanceMethod() or class_getClassMethod().
525 If 'method_a' or 'method_b' is NULL, do nothing. */
526 objc_EXPORT void
527 method_exchangeImplementations (Method method_a, Method method_b);
528
529 /* Create a new class/meta-class pair. This function is called to
530 create a new class at runtime. The class is created with
531 superclass 'superclass' (use 'Nil' to create a new root class) and
532 name 'class_name'. 'extraBytes' can be used to specify some extra
533 space for indexed variables to be added at the end of the class and
534 meta-class objects (it is recommended that you set extraBytes to
535 0). Once you have created the class, it is not usable yet. You
536 need to add any instance variables (by using class_addIvar()), any
537 instance methods (by using class_addMethod()) and any class methods
538 (by using class_addMethod() on the meta-class, as in
539 class_addMethod (object_getClass (class), method)) that are
540 required, and then you need to call objc_registerClassPair() to
541 activate the class. If you need to create a hierarchy of classes,
542 you need to create and register them one at a time. You can not
543 create a new class using another class in construction as
544 superclass. Return Nil if 'class-name' is NULL or if a class with
545 that name already exists or 'superclass' is a class still in
546 construction.
547
548 Implementation Note: in the GNU runtime, allocating a class pair
549 only creates the structures for the class pair, but does not
550 register anything with the runtime. The class is registered with
551 the runtime only when objc_registerClassPair() is called. In
552 particular, if a class is in construction, objc_getClass() will not
553 find it, the superclass will not know about it,
554 class_getSuperclass() will return Nil and another thread may
555 allocate a class pair with the same name; the conflict will only be
556 detected when the classes are registered with the runtime.
557 */
558 objc_EXPORT Class
559 objc_allocateClassPair (Class super_class, const char *class_name,
560 size_t extraBytes);
561
562 /* Register a class pair that was created with
563 objc_allocateClassPair(). After you register a class, you can no
564 longer make changes to its instance variables, but you can start
565 creating instances of it. Do nothing if 'class_' is NULL or if it
566 is not a class allocated by objc_allocateClassPair() and still in
567 construction. */
568 objc_EXPORT void
569 objc_registerClassPair (Class class_);
570
571 /* Dispose of a class pair created using objc_allocateClassPair().
572 Call this function if you started creating a new class with
573 objc_allocateClassPair() but then want to abort the process. You
574 should not access 'class_' after calling this method. Note that if
575 'class_' has already been registered with the runtime via
576 objc_registerClassPair(), this function does nothing; you can only
577 dispose of class pairs that are still being constructed. Do
578 nothing if class is 'Nil' or if 'class_' is not a class being
579 constructed. */
580 objc_EXPORT void
581 objc_disposeClassPair (Class class_);
582
583 /* Compatibility Note: The Apple/NeXT runtime has the function
584 objc_duplicateClass () but it's undocumented. The GNU runtime does
585 not have it. */
586
587
588 /** Implementation: the following functions are in sendmsg.c. */
589
590 /* Return the instance method with selector 'selector' of class
591 'class_', or NULL if the class (or one of its superclasses) does
592 not implement the method. Return NULL if class_ is Nil or selector
593 is NULL. Calling this function may trigger a call to
594 +resolveInstanceMethod:, but does not return a forwarding
595 function. */
596 objc_EXPORT Method class_getInstanceMethod (Class class_, SEL selector);
597
598 /* Return the class method with selector 'selector' of class 'class_',
599 or NULL if the class (or one of its superclasses) does not
600 implement the method. Return NULL if class_ is Nil or selector is
601 NULL. Calling this function may trigger a call to
602 +resolveClassMethod:, but does not return a forwarding
603 function. */
604 objc_EXPORT Method class_getClassMethod (Class class_, SEL selector);
605
606 /* Return the IMP (pointer to the function implementing a method) for
607 the instance method with selector 'selector' in class 'class_'.
608 This is the same routine that is used while messaging, and should
609 be very fast. Note that you most likely would need to cast the
610 return function pointer to a function pointer with the appropriate
611 arguments and return type before calling it. To get a class
612 method, you can pass the meta-class as the class_ argument (ie, use
613 class_getMethodImplementation (object_getClass (class_),
614 selector)). Return NULL if class_ is Nil or selector is NULL.
615 This function first looks for an existing method; if it is not
616 found, it calls +resolveClassMethod: or +resolveInstanceMethod:
617 (depending on whether a class or instance method is being looked
618 up) if it is implemented. If the method returns YES, then it tries
619 the look up again (the assumption being that +resolveClassMethod:
620 or resolveInstanceMethod: will add the method using
621 class_addMethod()). If it is still not found, it returns a
622 forwarding function. */
623 objc_EXPORT IMP class_getMethodImplementation (Class class_, SEL selector);
624
625 /* Compatibility Note: the Apple/NeXT runtime has the function
626 class_getMethodImplementation_stret () which currently does not
627 exist on the GNU runtime because the messaging implementation is
628 different. */
629
630 /* Return YES if class 'class_' has an instance method implementing
631 selector 'selector', and NO if not. Return NO if class_ is Nil or
632 selector is NULL. If you need to check a class method, use the
633 meta-class as the class_ argument (ie, use class_respondsToSelector
634 (object_getClass (class_), selector)). */
635 objc_EXPORT BOOL class_respondsToSelector (Class class_, SEL selector);
636
637 /* Add a method to a class. Use this function to add a new method to
638 a class (potentially overriding a method with the same selector in
639 the superclass); if you want to modify an existing method, use
640 method_setImplementation() instead (or class_replaceMethod ()).
641 This method adds an instance method to 'class_'; to add a class
642 method, get the meta class first, then add the method to the meta
643 class, that is, use
644
645 class_addMethod (object_getClass (class_), selector,
646 implementation, type);
647
648 Return YES if the method was added, and NO if not. Do nothing if
649 one of the arguments is NULL. */
650 objc_EXPORT BOOL class_addMethod (Class class_, SEL selector, IMP implementation,
651 const char *method_types);
652
653 /* Replace a method in a class. If the class already have a method
654 with this 'selector', find it and use method_setImplementation() to
655 replace the implementation with 'implementation' (method_types is
656 ignored in that case). If the class does not already have a method
657 with this 'selector', call 'class_addMethod() to add it.
658
659 Return the previous implementation of the method, or NULL if none
660 was found. Return NULL if any of the arguments is NULL. */
661 objc_EXPORT IMP class_replaceMethod (Class class_, SEL selector, IMP implementation,
662 const char *method_types);
663
664
665 /** Implementation: the following functions are in methods.c. */
666
667 /* Return the selector for method 'method'. Return NULL if 'method'
668 is NULL.
669
670 This function is misnamed; it should be called
671 'method_getSelector'. To get the actual name, get the selector,
672 then the name from the selector (ie, use sel_getName
673 (method_getName (method))). */
674 objc_EXPORT SEL method_getName (Method method);
675
676 /* Return the IMP of the method. Return NULL if 'method' is NULL. */
677 objc_EXPORT IMP method_getImplementation (Method method);
678
679 /* Return the type encoding of the method. Return NULL if 'method' is
680 NULL. */
681 objc_EXPORT const char * method_getTypeEncoding (Method method);
682
683 /* Return a method description for the method. Return NULL if
684 'method' is NULL. */
685 objc_EXPORT struct objc_method_description * method_getDescription (Method method);
686
687 /* Return all the instance methods of the class. The return value of
688 the function is a pointer to an area, allocated with malloc(), that
689 contains all the instance methods of the class. It does not
690 include instance methods of superclasses. The list is terminated
691 by NULL. Optionally, if you pass a non-NULL
692 'numberOfReturnedMethods' pointer, the unsigned int that it points
693 to will be filled with the number of instance methods returned. To
694 get the list of class methods, pass the meta-class in the 'class_'
695 argument, (ie, use class_copyMethodList (object_getClass (class_),
696 &numberOfReturnedMethods)). */
697 objc_EXPORT Method * class_copyMethodList (Class class_, unsigned int *numberOfReturnedMethods);
698
699
700 /** Implementation: the following functions are in encoding.c. */
701
702 /* Return the number of arguments that the method 'method' expects.
703 Note that all methods need two implicit arguments ('self' for the
704 receiver, and '_cmd' for the selector). Return 0 if 'method' is
705 NULL. */
706 objc_EXPORT unsigned int method_getNumberOfArguments (Method method);
707
708 /* Return the string encoding for the return type of method 'method'.
709 The string is a standard zero-terminated string in an area of
710 memory allocated with malloc(); you should free it with free() when
711 you finish using it. Return an empty string if method is NULL. */
712 objc_EXPORT char * method_copyReturnType (Method method);
713
714 /* Return the string encoding for the argument type of method
715 'method', argument number 'argumentNumber' ('argumentNumber' is 0
716 for self, 1 for _cmd, and 2 or more for the additional arguments if
717 any). The string is a standard zero-terminated string in an area
718 of memory allocated with malloc(); you should free it with free()
719 when you finish using it. Return an empty string if method is NULL
720 or if 'argumentNumber' refers to a non-existing argument. */
721 objc_EXPORT char * method_copyArgumentType (Method method, unsigned int argumentNumber);
722
723 /* Return the string encoding for the return type of method 'method'.
724 The string is returned by copying it into the supplied
725 'returnValue' string, which is of size 'returnValueSize'. No more
726 than 'returnValueSize' characters are copied; if the encoding is
727 smaller than 'returnValueSize', the rest of 'returnValue' is filled
728 with zeros. If it is bigger, it is truncated (and would not be
729 zero-terminated). You should supply a big enough
730 'returnValueSize'. If the method is NULL, returnValue is set to a
731 string of zeros. */
732 objc_EXPORT void method_getReturnType (Method method, char *returnValue,
733 size_t returnValueSize);
734
735 /* Return the string encoding for the argument type of method
736 'method', argument number 'argumentNumber' ('argumentNumber' is 0
737 for self, 1 for _cmd, and 2 or more for the additional arguments if
738 any). The string is returned by copying it into the supplied
739 'returnValue' string, which is of size 'returnValueSize'. No more
740 than 'returnValueSize' characters are copied; if the encoding is
741 smaller than 'returnValueSize', the rest of 'returnValue' is filled
742 with zeros. If it is bigger, it is truncated (and would not be
743 zero-terminated). You should supply a big enough
744 'returnValueSize'. If the method is NULL, returnValue is set to a
745 string of zeros. */
746 objc_EXPORT void method_getArgumentType (Method method, unsigned int argumentNumber,
747 char *returnValue, size_t returnValueSize);
748
749
750 /** Implementation: the following functions are in protocols.c. */
751
752 /* Return the protocol with name 'name', or nil if it the protocol is
753 not known to the runtime. */
754 objc_EXPORT Protocol *objc_getProtocol (const char *name);
755
756 /* Return all the protocols known to the runtime. The return value of
757 the function is a pointer to an area, allocated with malloc(), that
758 contains all the protocols known to the runtime; the list is
759 terminated by NULL. You should free this area using free() once
760 you no longer need it. Optionally, if you pass a non-NULL
761 'numberOfReturnedProtocols' pointer, the unsigned int that it
762 points to will be filled with the number of protocols returned. If
763 there are no protocols known to the runtime, NULL is returned. */
764 objc_EXPORT Protocol **objc_copyProtocolList (unsigned int *numberOfReturnedProtocols);
765
766 /* Add a protocol to a class, and return YES if it was done
767 succesfully, and NO if not. At the moment, NO should only happen
768 if class_ or protocol are nil, if the protocol is not a Protocol
769 object or if the class already conforms to the protocol. */
770 objc_EXPORT BOOL class_addProtocol (Class class_, Protocol *protocol);
771
772 /* Return YES if the class 'class_' conforms to Protocol 'protocol',
773 and NO if not. */
774 objc_EXPORT BOOL class_conformsToProtocol (Class class_, Protocol *protocol);
775
776 /* Return all the protocols that the class conforms to. The return
777 value of the function is a pointer to an area, allocated with
778 malloc(), that contains all the protocols formally adopted by the
779 class. It does not include protocols adopted by superclasses. The
780 list is terminated by NULL. Optionally, if you pass a non-NULL
781 'numberOfReturnedProtocols' pointer, the unsigned int that it
782 points to will be filled with the number of protocols returned. */
783 objc_EXPORT Protocol **class_copyProtocolList (Class class_, unsigned int *numberOfReturnedProtocols);
784
785 /* Return YES if protocol 'protocol' conforms to protocol
786 'anotherProtocol', and NO if not. Note that if one of the two
787 protocols is nil, it returns NO. */
788 objc_EXPORT BOOL protocol_conformsToProtocol (Protocol *protocol, Protocol *anotherProtocol);
789
790 /* Return YES if protocol 'protocol' is the same as protocol
791 'anotherProtocol', and 'NO' if not. Note that it returns YES if
792 the two protocols are both nil. */
793 objc_EXPORT BOOL protocol_isEqual (Protocol *protocol, Protocol *anotherProtocol);
794
795 /* Return the name of protocol 'protocol'. If 'protocol' is nil or is
796 not a Protocol, return NULL. */
797 objc_EXPORT const char *protocol_getName (Protocol *protocol);
798
799 /* Return the method description for the method with selector
800 'selector' in protocol 'protocol'; if 'requiredMethod' is YES, the
801 function searches the list of required methods; if NO, the list of
802 optional methods. If 'instanceMethod' is YES, the function search
803 for an instance method; if NO, for a class method. If there is no
804 matching method, an objc_method_description structure with both
805 name and types set to NULL is returned. This function will only
806 find methods that are directly declared in the protocol itself, not
807 in other protocols that this protocol adopts.
808
809 Note that the traditional ABI does not store the list of optional
810 methods of a protocol in a compiled module, so the traditional ABI
811 will always return (NULL, NULL) when requiredMethod == NO. */
812 objc_EXPORT struct objc_method_description protocol_getMethodDescription (Protocol *protocol,
813 SEL selector,
814 BOOL requiredMethod,
815 BOOL instanceMethod);
816
817 /* Return the method descriptions of all the methods of the protocol.
818 The return value of the function is a pointer to an area, allocated
819 with malloc(), that contains all the method descriptions of the
820 methods of the protocol. It does not recursively include methods
821 of the protocols adopted by this protocol. The list is terminated
822 by a NULL objc_method_description (one with both fields set to
823 NULL). Optionally, if you pass a non-NULL
824 'numberOfReturnedMethods' pointer, the unsigned int that it points
825 to will be filled with the number of properties returned.
826
827 Note that the traditional ABI does not store the list of optional
828 methods of a protocol in a compiled module, so the traditional ABI
829 will always return an empty list if requiredMethod is set to
830 NO. */
831 objc_EXPORT struct objc_method_description *protocol_copyMethodDescriptionList (Protocol *protocol,
832 BOOL requiredMethod,
833 BOOL instanceMethod,
834 unsigned int *numberOfReturnedMethods);
835
836 /* Return the property with name 'propertyName' of the protocol
837 'protocol'. If 'requiredProperty' is YES, the function searches
838 the list of required properties; if NO, the list of optional
839 properties. If 'instanceProperty' is YES, the function searches
840 the list of instance properties; if NO, the list of class
841 properties. At the moment, optional properties and class
842 properties are not part of the Objective-C language, so both
843 'requiredProperty' and 'instanceProperty' should be set to YES.
844 This function returns NULL if the required property can not be
845 found.
846
847 Note that the traditional ABI does not store the list of properties
848 of a protocol in a compiled module, so the traditional ABI will
849 always return NULL. */
850 objc_EXPORT Property protocol_getProperty (Protocol *protocol, const char *propertyName,
851 BOOL requiredProperty, BOOL instanceProperty);
852
853 /* Return all the properties of the protocol. The return value of the
854 function is a pointer to an area, allocated with malloc(), that
855 contains all the properties of the protocol. It does not
856 recursively include properties of the protocols adopted by this
857 protocol. The list is terminated by NULL. Optionally, if you pass
858 a non-NULL 'numberOfReturnedProperties' pointer, the unsigned int
859 that it points to will be filled with the number of properties
860 returned.
861
862 Note that the traditional ABI does not store the list of properties
863 of a protocol in a compiled module, so the traditional ABI will
864 always return NULL and store 0 in numberOfReturnedProperties. */
865 objc_EXPORT Property *protocol_copyPropertyList (Protocol *protocol, unsigned int *numberOfReturnedProperties);
866
867 /* Return all the protocols that the protocol conforms to. The return
868 value of the function is a pointer to an area, allocated with
869 malloc(), that contains all the protocols formally adopted by the
870 protocol. It does not recursively include protocols adopted by the
871 protocols adopted by this protocol. The list is terminated by
872 NULL. Optionally, if you pass a non-NULL
873 'numberOfReturnedProtocols' pointer, the unsigned int that it
874 points to will be filled with the number of protocols returned. */
875 objc_EXPORT Protocol **protocol_copyProtocolList (Protocol *protocol, unsigned int *numberOfReturnedProtocols);
876
877
878 /* TODO: Add all the other functions in the API. */
879
880 /** Implementation: the following hook is in init.c. */
881
882 /* This is a hook which is called by __objc_exec_class every time a
883 class or a category is loaded into the runtime. This may e.g. help
884 a dynamic loader determine the classes that have been loaded when
885 an object file is dynamically linked in. */
886 objc_EXPORT void (*_objc_load_callback)(Class _class, struct objc_category *category);
887
888
889 /** Implementation: the following functions are in objc-foreach.c. */
890
891 /* 'objc_enumerationMutation()' is called when a collection is
892 mutated while being "fast enumerated". That is a hard error, and
893 objc_enumerationMutation is called to deal with it. 'collection'
894 is the collection object that was mutated during an enumeration.
895
896 objc_enumerationMutation() will invoke the mutation handler if any
897 is set. Then, it will abort the program.
898
899 Compatibility note: the Apple runtime will not abort the program
900 after calling the mutation handler. */
901 objc_EXPORT void objc_enumerationMutation (id collection);
902
903 /* 'objc_set_enumeration_mutation_handler' can be used to set a
904 function that will be called (instead of aborting) when a fast
905 enumeration is mutated during enumeration. The handler will be
906 called with the 'collection' being mutated as the only argument and
907 it should not return; it should either exit the program, or could
908 throw an exception. The recommended implementation is to throw an
909 exception - the user can then use exception handlers to deal with
910 it.
911
912 This function is not thread safe (other threads may be trying to
913 invoke the enumeration mutation handler while you are changing it!)
914 and should be called during during the program initialization
915 before threads are started. It is mostly reserved for "Foundation"
916 libraries; in the case of GNUstep, GNUstep Base may be using this
917 function to improve the standard enumeration mutation handling.
918 You probably shouldn't use this function unless you are writing
919 your own Foundation library. */
920 objc_EXPORT void objc_setEnumerationMutationHandler (void (*handler)(id));
921
922 /* This structure (used during fast enumeration) is automatically
923 defined by the compiler (it is as if this definition was always
924 included in all Objective-C files). Note that it is usually
925 defined again with the name of NSFastEnumeration by "Foundation"
926 libraries such as GNUstep Base. And if NSFastEnumeration is
927 defined, the compiler will use it instead of
928 __objcFastEnumerationState when doing fast enumeration. */
929 /*
930 struct __objcFastEnumerationState
931 {
932 unsigned long state;
933 id *itemsPtr;
934 unsigned long *mutationsPtr;
935 unsigned long extra[5];
936 };
937 */
938
939
940 /** Implementation: the following functions are in memory.c. */
941
942 /* Traditional GNU Objective-C Runtime functions that are used for
943 memory allocation and disposal. These functions are used in the
944 same way as you use malloc, realloc, calloc and free and make sure
945 that memory allocation works properly with the garbage
946 collector.
947
948 Compatibility Note: these functions are not available with the
949 Apple/NeXT runtime. */
950
951 objc_EXPORT void *objc_malloc(size_t size);
952
953 /* FIXME: Shouldn't the following be called objc_malloc_atomic ? The
954 GC function is GC_malloc_atomic() which makes sense.
955 */
956 objc_EXPORT void *objc_atomic_malloc(size_t size);
957
958 objc_EXPORT void *objc_realloc(void *mem, size_t size);
959
960 objc_EXPORT void *objc_calloc(size_t nelem, size_t size);
961
962 objc_EXPORT void objc_free(void *mem);
963
964
965 /** Implementation: the following functions are in gc.c. */
966
967 /* Mark the instance variable as inaccessible to the garbage
968 collector. */
969 objc_EXPORT void class_ivar_set_gcinvisible (Class _class,
970 const char* ivarname,
971 BOOL gcInvisible);
972
973
974 /** Implementation: the following functions are in encoding.c. */
975
976 /* Traditional GNU Objective-C Runtime functions that are currently
977 used to implement method forwarding.
978
979 Compatibility Note: these functions are not available with the
980 Apple/NeXT runtime. */
981
982 /* Return the size of a variable which has the specified 'type'
983 encoding. */
984 objc_EXPORT int objc_sizeof_type (const char *type);
985
986 /* Return the align of a variable which has the specified 'type'
987 encoding. */
988 objc_EXPORT int objc_alignof_type (const char *type);
989
990 /* Return the aligned size of a variable which has the specified
991 'type' encoding. The aligned size is the size rounded up to the
992 nearest alignment. */
993 objc_EXPORT int objc_aligned_size (const char *type);
994
995 /* Return the promoted size of a variable which has the specified
996 'type' encoding. This is the size rounded up to the nearest
997 integral of the wordsize, taken to be the size of a void *. */
998 objc_EXPORT int objc_promoted_size (const char *type);
999
1000
1001 /* The following functions are used when parsing the type encoding of
1002 methods, to skip over parts that are ignored. They take as
1003 argument a pointer to a location inside the type encoding of a
1004 method (which is a string) and return a new pointer, pointing to a
1005 new location inside the string after having skipped the unwanted
1006 information. */
1007
1008 /* Skip some type qualifiers (_C_CONST, _C_IN, etc). These may
1009 eventually precede typespecs occurring in method prototype
1010 encodings. */
1011 objc_EXPORT const char *objc_skip_type_qualifiers (const char *type);
1012
1013 /* Skip one typespec element (_C_CLASS, _C_SEL, etc). If the typespec
1014 is prepended by type qualifiers, these are skipped as well. */
1015 objc_EXPORT const char *objc_skip_typespec (const char *type);
1016
1017 /* Skip an offset. */
1018 objc_EXPORT const char *objc_skip_offset (const char *type);
1019
1020 /* Skip an argument specification (ie, skipping a typespec, which may
1021 include qualifiers, and an offset too). */
1022 objc_EXPORT const char *objc_skip_argspec (const char *type);
1023
1024 /* Read type qualifiers (_C_CONST, _C_IN, etc) from string 'type'
1025 (stopping at the first non-type qualifier found) and return an
1026 unsigned int which is the logical OR of all the corresponding flags
1027 (_F_CONST, _F_IN etc). */
1028 objc_EXPORT unsigned objc_get_type_qualifiers (const char *type);
1029
1030
1031 /* Note that the following functions work for very simple structures,
1032 but get easily confused by more complicated ones (for example,
1033 containing vectors). A better solution is required. These
1034 functions are likely to change in the next GCC release. */
1035
1036 /* The following three functions can be used to determine how a
1037 structure is laid out by the compiler. For example:
1038
1039 struct objc_struct_layout layout;
1040 int i;
1041
1042 objc_layout_structure (type, &layout);
1043 while (objc_layout_structure_next_member (&layout))
1044 {
1045 int position, align;
1046 const char *type;
1047
1048 objc_layout_structure_get_info (&layout, &position, &align, &type);
1049 printf ("element %d has offset %d, alignment %d\n",
1050 i++, position, align);
1051 }
1052
1053 These functions are used by objc_sizeof_type and objc_alignof_type
1054 functions to compute the size and alignment of structures. The
1055 previous method of computing the size and alignment of a structure
1056 was not working on some architectures, particulary on AIX, and in
1057 the presence of bitfields inside the structure. */
1058 struct objc_struct_layout
1059 {
1060 const char *original_type;
1061 const char *type;
1062 const char *prev_type;
1063 unsigned int record_size;
1064 unsigned int record_align;
1065 };
1066
1067 objc_EXPORT void objc_layout_structure (const char *type,
1068 struct objc_struct_layout *layout);
1069 objc_EXPORT BOOL objc_layout_structure_next_member (struct objc_struct_layout *layout);
1070 objc_EXPORT void objc_layout_finish_structure (struct objc_struct_layout *layout,
1071 unsigned int *size,
1072 unsigned int *align);
1073 objc_EXPORT void objc_layout_structure_get_info (struct objc_struct_layout *layout,
1074 unsigned int *offset,
1075 unsigned int *align,
1076 const char **type);
1077
1078 #ifdef __cplusplus
1079 }
1080 #endif /* __cplusplus */
1081
1082 #endif