In libobjc/: 2010-10-11 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / libobjc / objc / runtime.h
blobb15c522235d422613c396684f32b90a0adbb972c
1 /* GNU Objective-C Runtime API - Modern API
2 Copyright (C) 2010 Free Software Foundation, Inc.
3 Contributed by Nicola Pero <nicola.pero@meta-innovation.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #ifndef __objc_runtime_INCLUDE_GNU
27 #define __objc_runtime_INCLUDE_GNU
30 This file declares the "modern" GNU Objective-C Runtime API.
31 Include this file to use it.
33 This API is replacing the "traditional" GNU Objective-C Runtime API
34 (declared in objc/objc-api.h) which is the one supported by older
35 versions of the GNU Objective-C Runtime. The "modern" API is very
36 similar to the API used by the modern Apple/NeXT runtime.
38 Because the two APIs have some conflicting definitions (in
39 particular, Method and Category are defined differently) you should
40 include either objc/objc-api.h (to use the traditional GNU
41 Objective-C Runtime API) or objc/runtime.h (to use the modern GNU
42 Objective-C Runtime API), but not both.
44 #ifdef __objc_api_INCLUDE_GNU
45 # error You can not include both objc/objc-api.h and objc/runtime.h. Include objc/objc-api.h for the traditional GNU Objective-C Runtime API and objc/runtime.h for the modern one.
46 #endif
48 /* TODO: This file is incomplete. */
50 #include "objc.h"
52 /* An 'Ivar' represents an instance variable. It holds information
53 about the name, type and offset of the instance variable. */
54 typedef struct objc_ivar *Ivar;
56 /* A 'Property' represents a property. It holds information about the
57 name of the property, and its attributes.
59 Compatibility Note: the Apple/NeXT runtime defines this as
60 objc_property_t, so we define it that way as well, but obviously
61 Property is the right name. */
62 typedef struct objc_property *Property;
63 typedef struct objc_property *objc_property_t;
65 /* A 'Method' represents a method. It holds information about the
66 name, types and the IMP of the method. */
67 typedef struct objc_method *Method;
69 /* A 'Category' represents a category. It holds information about the
70 name of the category, the class it belongs to, and the methods,
71 protocols and such like provided by the category. */
72 typedef struct objc_category *Category;
74 /* 'Protocol' is defined in objc/objc.h (which is included by this
75 file). */
77 /* Method descriptor returned by introspective Object methods. At the
78 moment, this is really just the first part of the more complete
79 objc_method structure used internally by the runtime. (PS: In the
80 GNU Objective-C Runtime, selectors already include a type, so an
81 objc_method_description does not add much to a SEL. But in other
82 runtimes, that is not the case, which is why
83 objc_method_description exists). */
84 struct objc_method_description
86 SEL name; /* Selector (name and signature) */
87 char *types; /* Type encoding */
90 /* The following are used in encode strings to describe the type of
91 Ivars and Methods. */
92 #define _C_ID '@'
93 #define _C_CLASS '#'
94 #define _C_SEL ':'
95 #define _C_CHR 'c'
96 #define _C_UCHR 'C'
97 #define _C_SHT 's'
98 #define _C_USHT 'S'
99 #define _C_INT 'i'
100 #define _C_UINT 'I'
101 #define _C_LNG 'l'
102 #define _C_ULNG 'L'
103 #define _C_LNG_LNG 'q'
104 #define _C_ULNG_LNG 'Q'
105 #define _C_FLT 'f'
106 #define _C_DBL 'd'
107 #define _C_LNG_DBL 'D'
108 #define _C_BFLD 'b'
109 #define _C_BOOL 'B'
110 #define _C_VOID 'v'
111 #define _C_UNDEF '?'
112 #define _C_PTR '^'
113 #define _C_CHARPTR '*'
114 #define _C_ARY_B '['
115 #define _C_ARY_E ']'
116 #define _C_UNION_B '('
117 #define _C_UNION_E ')'
118 #define _C_STRUCT_B '{'
119 #define _C_STRUCT_E '}'
120 #define _C_VECTOR '!'
121 #define _C_COMPLEX 'j'
123 /* _C_ATOM is never generated by the compiler. You can treat it as
124 equivalent to "*". */
125 #define _C_ATOM '%'
127 /* The following are used in encode strings to describe some
128 qualifiers of method and ivar types. */
129 #define _C_CONST 'r'
130 #define _C_IN 'n'
131 #define _C_INOUT 'N'
132 #define _C_OUT 'o'
133 #define _C_BYCOPY 'O'
134 #define _C_BYREF 'R'
135 #define _C_ONEWAY 'V'
136 #define _C_GCINVISIBLE '|'
138 /* The same when used as flags. */
139 #define _F_CONST 0x01
140 #define _F_IN 0x01
141 #define _F_OUT 0x02
142 #define _F_INOUT 0x03
143 #define _F_BYCOPY 0x04
144 #define _F_BYREF 0x08
145 #define _F_ONEWAY 0x10
146 #define _F_GCINVISIBLE 0x20
149 /** Implementation: the following functions are defined inline. */
151 /* Return the class of 'object', or Nil if the object is nil. If
152 'object' is a class, the meta class is returned; if 'object' is a
153 meta class, the root meta class is returned (note that this is
154 different from the traditional GNU Objective-C Runtime API function
155 object_get_class(), which for a meta class would return the meta
156 class itself). This function is inline, so it is really fast and
157 should be used instead of accessing object->class_pointer
158 directly. */
159 static inline Class
160 object_getClass (id object)
162 if (object != nil)
163 return object->class_pointer;
164 else
165 return Nil;
169 /** Implementation: the following functions are in selector.c. */
171 /* Return the name of a given selector. */
172 objc_EXPORT const char *sel_getName (SEL selector);
174 /* Return the type of a given selector.
176 Compatibility Note: the Apple/NeXT runtime has untyped selectors,
177 so it does not have this function, which is specific to the GNU
178 Runtime. */
179 objc_EXPORT const char *sel_getType (SEL selector);
181 /* This is the same as sel_registerName (). Please use
182 sel_registerName () instead. */
183 objc_EXPORT SEL sel_getUid (const char *name);
185 /* Register a selector with a given name (but unspecified types). If
186 you know the types, it is better to call sel_registerTypedName().
187 If a selector with this name already exists, it is returned. */
188 objc_EXPORT SEL sel_registerName (const char *name);
190 /* Register a selector with a given name and types. If a selector
191 with this name and types already exists, it is returned.
193 Compatibility Note: the Apple/NeXT runtime has untyped selectors,
194 so it does not have this function, which is specific to the GNU
195 Runtime. */
196 objc_EXPORT SEL set_registerTypedName (const char *name, const char *type);
198 /* Return YES if first_selector is the same as second_selector, and NO
199 if not. */
200 objc_EXPORT BOOL sel_isEqual (SEL first_selector, SEL second_selector);
203 /** Implementation: the following functions are in objects.c. */
205 /* Create an instance of class 'class_', adding extraBytes to the size
206 of the returned object. This method allocates the appropriate
207 amount of memory for the instance, initializes it to zero, then
208 calls all the C++ constructors on appropriate C++ instance
209 variables of the instance (if any) (TODO: The C++ constructors bit
210 is not implemented yet). */
211 objc_EXPORT id class_createInstance (Class class_, size_t extraBytes);
213 /* Copy an object and return the copy. extraBytes should be identical
214 to the extraBytes parameter that was passed when creating the
215 original object. */
216 objc_EXPORT id object_copy (id object, size_t extraBytes);
218 /* Dispose of an object. This method calls the appropriate C++
219 destructors on appropriate C++ instance variables of the instance
220 (if any) (TODO: This is not implemented yet), then frees the memory
221 for the instance. */
222 objc_EXPORT id object_dispose (id object);
224 /* Return the name of the class of 'object'. If 'object' is 'nil',
225 returns "Nil". */
226 objc_EXPORT const char * object_getClassName (id object);
228 /* Change the class of object to be class_. Return the previous class
229 of object. This is currently not really thread-safe. */
230 objc_EXPORT Class object_setClass (id object, Class class_);
233 /** Implementation: the following functions are in ivars.c. */
235 /* Return an instance variable given the class and the instance
236 variable name. This is an expensive function to call, so try to
237 reuse the returned Ivar if you can. */
238 objc_EXPORT Ivar class_getInstanceVariable (Class class_, const char *name);
240 /* If the object was created in class_createInstance() with some
241 extraBytes, returns a pointer to them. If it was not, then the
242 returned pointer may make no sense. */
243 objc_EXPORT void * object_getIndexedIvars (id object);
245 /* Get the value of an instance variable of type 'id'. The function
246 returns the instance variable. To get the value of the instance
247 variable, you should pass as 'returnValue' a pointer to an 'id';
248 the value will be copied there. Note that 'returnValue' is really
249 a 'void *', not a 'void **'. This function really works only with
250 instance variables of type 'id'; for other types of instance
251 variables, access directly the data at (char *)object +
252 ivar_getOffset (ivar). */
253 objc_EXPORT Ivar object_getInstanceVariable (id object, const char *name, void **returnValue);
255 /* Set the value of an instance variable. The value to set is passed
256 in 'newValue' (which really is an 'id', not a 'void *'). The
257 function returns the instance variable. This function really works
258 only with instance variables of type 'id'; for other types of
259 instance variables, access directly the data at (char *)object +
260 ivar_getOffset (ivar). */
261 objc_EXPORT Ivar object_setInstanceVariable (id object, const char *name, void *newValue);
263 /* Get the value of an instance variable of type 'id' of the object
264 'object'. This is faster than object_getInstanceVariable if you
265 already have the instance variable because it avoids the expensive
266 call to class_getInstanceVariable that is done by
267 object_getInstanceVariable. */
268 objc_EXPORT id object_getIvar (id object, Ivar variable);
270 /* Set the value of an instance variable of type 'id' of the object
271 'object'. This is faster than object_setInstanceVariable if you
272 already have the instance variable because it avoids the expensive
273 call to class_getInstanceVariable that is done by
274 object_setInstanceVariable. */
275 objc_EXPORT void object_setIvar (id object, Ivar variable, id value);
277 /* Return the name of the instance variable. */
278 objc_EXPORT const char * ivar_getName (Ivar variable);
280 /* Return the offset of the instance variable from the start of the
281 object data. */
282 objc_EXPORT ptrdiff_t ivar_getOffset (Ivar variable);
284 /* Return the type encoding of the variable. */
285 objc_EXPORT const char * ivar_getTypeEncoding (Ivar variable);
288 /** Implementation: the following functions are in class.c. */
290 /* Compatibility Note: The Apple/NeXT runtime does not have
291 objc_get_unknown_class_handler and
292 objc_setGetUnknownClassHandler(). They provide functionality that
293 the traditional GNU Objective-C Runtime API used to provide via the
294 _objc_lookup_class hook. */
296 /* An 'objc_get_unknown_class_handler' function is used by
297 objc_getClass() to get a class that is currently unknown to the
298 compiler. You could use it for example to have the class loaded by
299 dynamically loading a library. 'class_name' is the name of the
300 class. The function should return the Class object if it manages to
301 load the class, and Nil if not. */
302 typedef Class (*objc_get_unknown_class_handler)(const char *class_name);
304 /* Sets a new handler function for getting unknown classes (to be used
305 by objc_getClass () and related), and returns the previous one.
306 This function is not safe to call in a multi-threaded environment
307 because other threads may be trying to use the get unknown class
308 handler while you change it! */
309 objc_get_unknown_class_handler
310 objc_setGetUnknownClassHandler (objc_get_unknown_class_handler new_handler);
313 /* Return the class with name 'name', if it is already registered with
314 the runtime. If it is not registered, and
315 objc_setGetUnknownClassHandler() has been called to set a handler
316 for unknown classes, the handler is called to give it a chance to
317 load the class in some other way. If the class is not known to the
318 runtime and the handler is not set or returns Nil, objc_getClass()
319 returns Nil. */
320 objc_EXPORT Class objc_getClass (const char *name);
322 /* Return the class with name 'name', if it is already registered with
323 the runtime. Return Nil if not. This function does not call the
324 objc_get_unknown_class_handler function if the class is not
325 found. */
326 objc_EXPORT Class objc_lookupClass (const char *name);
328 /* Return the meta class associated to the class with name 'name', if
329 it is already registered with the runtime. First, it finds the
330 class using objc_getClass(). Then, it returns the associated meta
331 class. If the class could not be found using objc_getClass(),
332 returns Nil. */
333 objc_EXPORT Class objc_getMetaClass (const char *name);
335 /* This is identical to objc_getClass(), but if the class is not found,
336 it aborts the process instead of returning Nil. */
337 objc_EXPORT Class objc_getRequiredClass (const char *name);
339 /* If 'returnValue' is NULL, 'objc_getClassList' returns the number of
340 classes currently registered with the runtime. If 'returnValue' is
341 not NULL, it should be a (Class *) pointer to an area of memory
342 which can contain up to 'maxNumberOfClassesToReturn' Class records.
343 'objc_getClassList' will fill the area pointed to by 'returnValue'
344 with all the Classes registered with the runtime (or up to
345 maxNumberOfClassesToReturn if there are more than
346 maxNumberOfClassesToReturn). The function return value is the
347 number of classes actually returned in 'returnValue'. */
348 objc_EXPORT int objc_getClassList (Class *returnValue, int maxNumberOfClassesToReturn);
350 /* Compatibility Note: The Apple/NeXT runtime also has
352 Class objc_getFutureClass (const char *name);
353 void objc_setFutureClass (Class class_, const char *name);
355 the documentation is unclear on what they are supposed to do, and
356 the GNU Objective-C Runtime currently does not provide them. */
359 /* TODO: Add all the other functions in the API. */
362 /** Implementation: the following functions are in objc-foreach.c. */
364 /* 'objc_enumerationMutation()' is called when a collection is
365 mutated while being "fast enumerated". That is a hard error, and
366 objc_enumerationMutation is called to deal with it. 'collection'
367 is the collection object that was mutated during an enumeration.
369 objc_enumerationMutation() will invoke the mutation handler if any
370 is set. Then, it will abort the program.
372 Compatibility note: the Apple runtime will not abort the program
373 after calling the mutation handler. */
374 objc_EXPORT void objc_enumerationMutation (id collection);
376 /* 'objc_set_enumeration_mutation_handler' can be used to set a
377 function that will be called (instead of aborting) when a fast
378 enumeration is mutated during enumeration. The handler will be
379 called with the 'collection' being mutated as the only argument and
380 it should not return; it should either exit the program, or could
381 throw an exception. The recommended implementation is to throw an
382 exception - the user can then use exception handlers to deal with
385 This function is not thread safe (other threads may be trying to
386 invoke the enumeration mutation handler while you are changing it!)
387 and should be called during during the program initialization
388 before threads are started. It is mostly reserved for "Foundation"
389 libraries; in the case of GNUstep, GNUstep Base may be using this
390 function to improve the standard enumeration mutation handling.
391 You probably shouldn't use this function unless you are writing
392 your own Foundation library. */
393 objc_EXPORT void objc_setEnumerationMutationHandler (void (*handler)(id));
395 /* This structure (used during fast enumeration) is automatically
396 defined by the compiler (it is as if this definition was always
397 included in all Objective-C files). Note that it is usually
398 defined again with the name of NSFastEnumeration by "Foundation"
399 libraries such as GNUstep Base. And if NSFastEnumeration is
400 defined, the compiler will use it instead of
401 __objcFastEnumerationState when doing fast enumeration. */
403 struct __objcFastEnumerationState
405 unsigned long state;
406 id *itemsPtr;
407 unsigned long *mutationsPtr;
408 unsigned long extra[5];
413 /** Implementation: the following functions are in encoding.c. */
415 /* Traditional GNU Objective-C Runtime functions that are currently
416 used to implement method forwarding.
419 /* Return the size of a variable which has the specified 'type'
420 encoding. */
421 int objc_sizeof_type (const char *type);
423 /* Return the align of a variable which has the specified 'type'
424 encoding. */
425 int objc_alignof_type (const char *type);
427 /* Return the aligned size of a variable which has the specified
428 'type' encoding. The aligned size is the size rounded up to the
429 nearest alignment. */
430 int objc_aligned_size (const char *type);
432 /* Return the promoted size of a variable which has the specified
433 'type' encoding. This is the size rounded up to the nearest
434 integral of the wordsize, taken to be the size of a void *. */
435 int objc_promoted_size (const char *type);
438 /* The following functions are used when parsing the type encoding of
439 methods, to skip over parts that are ignored. They take as
440 argument a pointer to a location inside the type encoding of a
441 method (which is a string) and return a new pointer, pointing to a
442 new location inside the string after having skipped the unwanted
443 information. */
445 /* Skip some type qualifiers (_C_CONST, _C_IN, etc). These may
446 eventually precede typespecs occurring in method prototype
447 encodings. */
448 const char *objc_skip_type_qualifiers (const char *type);
450 /* Skip one typespec element (_C_CLASS, _C_SEL, etc). If the typespec
451 is prepended by type qualifiers, these are skipped as well. */
452 const char *objc_skip_typespec (const char *type);
454 /* Skip an offset. */
455 const char *objc_skip_offset (const char *type);
457 /* Skip an argument specification (ie, skipping a typespec, which may
458 include qualifiers, and an offset too). */
459 const char *objc_skip_argspec (const char *type);
461 /* Read type qualifiers (_C_CONST, _C_IN, etc) from string 'type'
462 (stopping at the first non-type qualifier found) and return an
463 unsigned int which is the logical OR of all the corresponding flags
464 (_F_CONST, _F_IN etc). */
465 unsigned objc_get_type_qualifiers (const char *type);
468 /* Note that the following functions work for very simple structures,
469 but get easily confused by more complicated ones (for example,
470 containing vectors). A better solution is required.
473 /* The following three functions can be used to determine how a
474 structure is laid out by the compiler. For example:
476 struct objc_struct_layout layout;
477 int i;
479 objc_layout_structure (type, &layout);
480 while (objc_layout_structure_next_member (&layout))
482 int position, align;
483 const char *type;
485 objc_layout_structure_get_info (&layout, &position, &align, &type);
486 printf ("element %d has offset %d, alignment %d\n",
487 i++, position, align);
490 These functions are used by objc_sizeof_type and objc_alignof_type
491 functions to compute the size and alignment of structures. The
492 previous method of computing the size and alignment of a structure
493 was not working on some architectures, particulary on AIX, and in
494 the presence of bitfields inside the structure. */
495 struct objc_struct_layout
497 const char *original_type;
498 const char *type;
499 const char *prev_type;
500 unsigned int record_size;
501 unsigned int record_align;
504 void objc_layout_structure (const char *type,
505 struct objc_struct_layout *layout);
506 BOOL objc_layout_structure_next_member (struct objc_struct_layout *layout);
507 void objc_layout_finish_structure (struct objc_struct_layout *layout,
508 unsigned int *size,
509 unsigned int *align);
510 void objc_layout_structure_get_info (struct objc_struct_layout *layout,
511 unsigned int *offset,
512 unsigned int *align,
513 const char **type);
515 #endif