2005-12-14 Paolo Bonzini <bonzini@gnu.org>
[official-gcc.git] / libobjc / objc / objc-api.h
blobe393c3952190ea45356a0f5954388013a3f385d8
1 /* GNU Objective-C Runtime API.
2 Copyright (C) 1993, 1995, 1996, 1997, 2002, 2004 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 /* As a special exception, if you link this library with files compiled
22 with GCC to produce an executable, this does not cause the resulting
23 executable to be covered by the GNU General Public License. This
24 exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
27 #ifndef __objc_api_INCLUDE_GNU
28 #define __objc_api_INCLUDE_GNU
30 #include "objc.h"
31 #include "hash.h"
32 #include "thr.h"
33 #include "objc-decls.h"
34 #include <stdio.h>
35 #include <stdarg.h>
37 #ifdef __cplusplus
38 extern "C" {
39 #endif /* __cplusplus */
41 /* For functions which return Method_t */
42 #define METHOD_NULL (Method_t)0
43 /* Boolean typedefs */
45 ** Method descriptor returned by introspective Object methods.
46 ** This is really just the first part of the more complete objc_method
47 ** structure defined below and used internally by the runtime.
49 struct objc_method_description
51 SEL name; /* this is a selector, not a string */
52 char *types; /* type encoding */
55 /* Filer types used to describe Ivars and Methods. */
56 #define _C_ID '@'
57 #define _C_CLASS '#'
58 #define _C_SEL ':'
59 #define _C_CHR 'c'
60 #define _C_UCHR 'C'
61 #define _C_SHT 's'
62 #define _C_USHT 'S'
63 #define _C_INT 'i'
64 #define _C_UINT 'I'
65 #define _C_LNG 'l'
66 #define _C_ULNG 'L'
67 #define _C_LNG_LNG 'q'
68 #define _C_ULNG_LNG 'Q'
69 #define _C_FLT 'f'
70 #define _C_DBL 'd'
71 #define _C_BFLD 'b'
72 #define _C_BOOL 'B'
73 #define _C_VOID 'v'
74 #define _C_UNDEF '?'
75 #define _C_PTR '^'
76 #define _C_CHARPTR '*'
77 #define _C_ATOM '%'
78 #define _C_ARY_B '['
79 #define _C_ARY_E ']'
80 #define _C_UNION_B '('
81 #define _C_UNION_E ')'
82 #define _C_STRUCT_B '{'
83 #define _C_STRUCT_E '}'
84 #define _C_VECTOR '!'
88 ** Error handling
90 ** Call objc_error() or objc_verror() to record an error; this error
91 ** routine will generally exit the program but not necessarily if the
92 ** user has installed his own error handler.
94 ** Call objc_set_error_handler to assign your own function for
95 ** handling errors. The function should return YES if it is ok
96 ** to continue execution, or return NO or just abort if the
97 ** program should be stopped. The default error handler is just to
98 ** print a message on stderr.
100 ** The error handler function should be of type objc_error_handler
101 ** The first parameter is an object instance of relevance.
102 ** The second parameter is an error code.
103 ** The third parameter is a format string in the printf style.
104 ** The fourth parameter is a variable list of arguments.
106 extern void objc_error(id object, int code, const char* fmt, ...);
107 extern void objc_verror(id object, int code, const char* fmt, va_list ap);
108 typedef BOOL (*objc_error_handler)(id, int code, const char *fmt, va_list ap);
109 extern objc_error_handler objc_set_error_handler(objc_error_handler func);
112 ** Error codes
113 ** These are used by the runtime library, and your
114 ** error handling may use them to determine if the error is
115 ** hard or soft thus whether execution can continue or abort.
117 #define OBJC_ERR_UNKNOWN 0 /* Generic error */
119 #define OBJC_ERR_OBJC_VERSION 1 /* Incorrect runtime version */
120 #define OBJC_ERR_GCC_VERSION 2 /* Incorrect compiler version */
121 #define OBJC_ERR_MODULE_SIZE 3 /* Bad module size */
122 #define OBJC_ERR_PROTOCOL_VERSION 4 /* Incorrect protocol version */
124 #define OBJC_ERR_MEMORY 10 /* Out of memory */
126 #define OBJC_ERR_RECURSE_ROOT 20 /* Attempt to archive the root
127 object more than once. */
128 #define OBJC_ERR_BAD_DATA 21 /* Didn't read expected data */
129 #define OBJC_ERR_BAD_KEY 22 /* Bad key for object */
130 #define OBJC_ERR_BAD_CLASS 23 /* Unknown class */
131 #define OBJC_ERR_BAD_TYPE 24 /* Bad type specification */
132 #define OBJC_ERR_NO_READ 25 /* Cannot read stream */
133 #define OBJC_ERR_NO_WRITE 26 /* Cannot write stream */
134 #define OBJC_ERR_STREAM_VERSION 27 /* Incorrect stream version */
135 #define OBJC_ERR_BAD_OPCODE 28 /* Bad opcode */
137 #define OBJC_ERR_UNIMPLEMENTED 30 /* Method is not implemented */
139 #define OBJC_ERR_BAD_STATE 40 /* Bad thread state */
142 ** Set this variable nonzero to print a line describing each
143 ** message that is sent. (this is currently disabled)
145 extern BOOL objc_trace;
148 /* For every class which happens to have statically allocated instances in
149 this module, one OBJC_STATIC_INSTANCES is allocated by the compiler.
150 INSTANCES is NULL terminated and points to all statically allocated
151 instances of this class. */
152 struct objc_static_instances
154 char *class_name;
155 #ifdef __cplusplus
156 id instances[1];
157 #else
158 id instances[0];
159 #endif
163 ** Whereas a Module (defined further down) is the root (typically) of a file,
164 ** a Symtab is the root of the class and category definitions within the
165 ** module.
167 ** A Symtab contains a variable length array of pointers to classes and
168 ** categories defined in the module.
170 typedef struct objc_symtab {
171 unsigned long sel_ref_cnt; /* Unknown. */
172 SEL refs; /* Unknown. */
173 unsigned short cls_def_cnt; /* Number of classes compiled
174 (defined) in the module. */
175 unsigned short cat_def_cnt; /* Number of categories
176 compiled (defined) in the
177 module. */
179 void *defs[1]; /* Variable array of pointers.
180 cls_def_cnt of type Class
181 followed by cat_def_cnt of
182 type Category_t, followed
183 by a NULL terminated array
184 of objc_static_instances. */
185 } Symtab, *Symtab_t;
189 ** The compiler generates one of these structures for each module that
190 ** composes the executable (eg main.m).
192 ** This data structure is the root of the definition tree for the module.
194 ** A collect program runs between ld stages and creates a ObjC ctor array.
195 ** That array holds a pointer to each module structure of the executable.
197 typedef struct objc_module {
198 unsigned long version; /* Compiler revision. */
199 unsigned long size; /* sizeof(Module). */
200 const char* name; /* Name of the file where the
201 module was generated. The
202 name includes the path. */
204 Symtab_t symtab; /* Pointer to the Symtab of
205 the module. The Symtab
206 holds an array of
207 pointers to
208 the classes and categories
209 defined in the module. */
210 } Module, *Module_t;
214 ** The compiler generates one of these structures for a class that has
215 ** instance variables defined in its specification.
217 typedef struct objc_ivar {
218 const char* ivar_name; /* Name of the instance
219 variable as entered in the
220 class definition. */
221 const char* ivar_type; /* Description of the Ivar's
222 type. Useful for
223 debuggers. */
224 int ivar_offset; /* Byte offset from the base
225 address of the instance
226 structure to the variable. */
227 } *Ivar_t;
229 typedef struct objc_ivar_list {
230 int ivar_count; /* Number of structures (Ivar)
231 contained in the list. One
232 structure per instance
233 variable defined in the
234 class. */
235 struct objc_ivar ivar_list[1]; /* Variable length
236 structure. */
237 } IvarList, *IvarList_t;
241 ** The compiler generates one (or more) of these structures for a class that
242 ** has methods defined in its specification.
244 ** The implementation of a class can be broken into separate pieces in a file
245 ** and categories can break them across modules. To handle this problem is a
246 ** singly linked list of methods.
248 typedef struct objc_method {
249 SEL method_name; /* This variable is the method's
250 name. It is a char*.
251 The unique integer passed to
252 objc_msg_send is a char* too.
253 It is compared against
254 method_name using strcmp. */
255 const char* method_types; /* Description of the method's
256 parameter list. Useful for
257 debuggers. */
258 IMP method_imp; /* Address of the method in the
259 executable. */
260 } Method, *Method_t;
262 typedef struct objc_method_list {
263 struct objc_method_list* method_next; /* This variable is used to link
264 a method list to another. It
265 is a singly linked list. */
266 int method_count; /* Number of methods defined in
267 this structure. */
268 Method method_list[1]; /* Variable length
269 structure. */
270 } MethodList, *MethodList_t;
272 struct objc_protocol_list {
273 struct objc_protocol_list *next;
274 size_t count;
275 Protocol *list[1];
279 ** This is used to assure consistent access to the info field of
280 ** classes
282 #ifndef HOST_BITS_PER_LONG
283 #define HOST_BITS_PER_LONG (sizeof(long)*8)
284 #endif
286 #define __CLS_INFO(cls) ((cls)->info)
287 #define __CLS_ISINFO(cls, mask) ((__CLS_INFO(cls)&mask)==mask)
288 #define __CLS_SETINFO(cls, mask) (__CLS_INFO(cls) |= mask)
290 /* The structure is of type MetaClass */
291 #define _CLS_META 0x2L
292 #define CLS_ISMETA(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_META))
295 /* The structure is of type Class */
296 #define _CLS_CLASS 0x1L
297 #define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS))
300 ** The class is initialized within the runtime. This means that
301 ** it has had correct super and sublinks assigned
303 #define _CLS_RESOLV 0x8L
304 #define CLS_ISRESOLV(cls) __CLS_ISINFO(cls, _CLS_RESOLV)
305 #define CLS_SETRESOLV(cls) __CLS_SETINFO(cls, _CLS_RESOLV)
308 ** The class has been send a +initialize message or a such is not
309 ** defined for this class
311 #define _CLS_INITIALIZED 0x04L
312 #define CLS_ISINITIALIZED(cls) __CLS_ISINFO(cls, _CLS_INITIALIZED)
313 #define CLS_SETINITIALIZED(cls) __CLS_SETINFO(cls, _CLS_INITIALIZED)
316 ** The class number of this class. This must be the same for both the
317 ** class and its meta class object
319 #define CLS_GETNUMBER(cls) (__CLS_INFO(cls) >> (HOST_BITS_PER_LONG/2))
320 #define CLS_SETNUMBER(cls, num) \
321 ({ (cls)->info <<= (HOST_BITS_PER_LONG/2); \
322 (cls)->info >>= (HOST_BITS_PER_LONG/2); \
323 __CLS_SETINFO(cls, (((unsigned long)num) << (HOST_BITS_PER_LONG/2))); })
326 ** The compiler generates one of these structures for each category. A class
327 ** may have many categories and contain both instance and factory methods.
329 typedef struct objc_category {
330 const char* category_name; /* Name of the category. Name
331 contained in the () of the
332 category definition. */
333 const char* class_name; /* Name of the class to which
334 the category belongs. */
335 MethodList_t instance_methods; /* Linked list of instance
336 methods defined in the
337 category. NULL indicates no
338 instance methods defined. */
339 MethodList_t class_methods; /* Linked list of factory
340 methods defined in the
341 category. NULL indicates no
342 class methods defined. */
343 struct objc_protocol_list *protocols; /* List of Protocols
344 conformed to */
345 } Category, *Category_t;
348 ** Structure used when a message is send to a class's super class. The
349 ** compiler generates one of these structures and passes it to
350 ** objc_msg_super.
352 typedef struct objc_super {
353 id self; /* Id of the object sending
354 the message. */
355 #ifdef __cplusplus
356 Class super_class;
357 #else
358 Class class; /* Object's super class. */
359 #endif
360 } Super, *Super_t;
362 IMP objc_msg_lookup_super(Super_t super, SEL sel);
364 retval_t objc_msg_sendv(id, SEL, arglist_t);
369 ** This is a hook which is called by objc_lookup_class and
370 ** objc_get_class if the runtime is not able to find the class.
371 ** This may e.g. try to load in the class using dynamic loading.
372 ** The function is guaranteed to be passed a non-NULL name string.
374 objc_EXPORT Class (*_objc_lookup_class)(const char *name);
377 ** This is a hook which is called by __objc_exec_class every time a class
378 ** or a category is loaded into the runtime. This may e.g. help a
379 ** dynamic loader determine the classes that have been loaded when
380 ** an object file is dynamically linked in.
382 objc_EXPORT void (*_objc_load_callback)(Class _class, Category* category);
385 ** Hook functions for allocating, copying and disposing of instances
387 objc_EXPORT id (*_objc_object_alloc)(Class _class);
388 objc_EXPORT id (*_objc_object_copy)(id object);
389 objc_EXPORT id (*_objc_object_dispose)(id object);
392 ** Standard functions for memory allocation and disposal.
393 ** Users should use these functions in their ObjC programs so
394 ** that they work properly with garbage collectors as well as
395 ** can take advantage of the exception/error handling available.
397 void *
398 objc_malloc(size_t size);
400 void *
401 objc_atomic_malloc(size_t size);
403 void *
404 objc_valloc(size_t size);
406 void *
407 objc_realloc(void *mem, size_t size);
409 void *
410 objc_calloc(size_t nelem, size_t size);
412 void
413 objc_free(void *mem);
416 ** Hook functions for memory allocation and disposal.
417 ** This makes it easy to substitute garbage collection systems
418 ** such as Boehm's GC by assigning these function pointers
419 ** to the GC's allocation routines. By default these point
420 ** to the ANSI standard malloc, realloc, free, etc.
422 ** Users should call the normal objc routines above for
423 ** memory allocation and disposal within their programs.
425 objc_EXPORT void *(*_objc_malloc)(size_t);
426 objc_EXPORT void *(*_objc_atomic_malloc)(size_t);
427 objc_EXPORT void *(*_objc_valloc)(size_t);
428 objc_EXPORT void *(*_objc_realloc)(void *, size_t);
429 objc_EXPORT void *(*_objc_calloc)(size_t, size_t);
430 objc_EXPORT void (*_objc_free)(void *);
433 ** Hook for method forwarding. This makes it easy to substitute a
434 ** library, such as ffcall, that implements closures, thereby avoiding
435 ** gcc's __builtin_apply problems.
437 objc_EXPORT IMP (*__objc_msg_forward)(SEL);
439 Method_t class_get_class_method(MetaClass _class, SEL aSel);
441 Method_t class_get_instance_method(Class _class, SEL aSel);
443 Class class_pose_as(Class impostor, Class superclass);
445 Class objc_get_class(const char *name);
447 Class objc_lookup_class(const char *name);
449 Class objc_next_class(void **enum_state);
451 const char *sel_get_name(SEL selector);
453 const char *sel_get_type(SEL selector);
455 SEL sel_get_uid(const char *name);
457 SEL sel_get_any_uid(const char *name);
459 SEL sel_get_any_typed_uid(const char *name);
461 SEL sel_get_typed_uid(const char *name, const char*);
463 SEL sel_register_name(const char *name);
465 SEL sel_register_typed_name(const char *name, const char*type);
468 BOOL sel_is_mapped (SEL aSel);
470 extern id class_create_instance(Class _class);
472 static inline const char *
473 class_get_class_name(Class _class)
475 return CLS_ISCLASS(_class)?_class->name:((_class==Nil)?"Nil":0);
478 static inline long
479 class_get_instance_size(Class _class)
481 return CLS_ISCLASS(_class)?_class->instance_size:0;
484 static inline MetaClass
485 class_get_meta_class(Class _class)
487 return CLS_ISCLASS(_class)?_class->class_pointer:Nil;
490 static inline Class
491 class_get_super_class(Class _class)
493 return CLS_ISCLASS(_class)?_class->super_class:Nil;
496 static inline int
497 class_get_version(Class _class)
499 return CLS_ISCLASS(_class)?_class->version:-1;
502 static inline BOOL
503 class_is_class(Class _class)
505 return CLS_ISCLASS(_class);
508 static inline BOOL
509 class_is_meta_class(Class _class)
511 return CLS_ISMETA(_class);
515 static inline void
516 class_set_version(Class _class, long version)
518 if (CLS_ISCLASS(_class))
519 _class->version = version;
522 static inline void *
523 class_get_gc_object_type (Class _class)
525 return CLS_ISCLASS(_class) ? _class->gc_object_type : NULL;
528 /* Mark the instance variable as innaccessible to the garbage collector */
529 extern void class_ivar_set_gcinvisible (Class _class,
530 const char* ivarname,
531 BOOL gcInvisible);
533 static inline IMP
534 method_get_imp(Method_t method)
536 return (method!=METHOD_NULL)?method->method_imp:(IMP)0;
539 IMP get_imp (Class _class, SEL sel);
541 /* Redefine on NeXTSTEP so as not to conflict with system function */
542 #ifdef __NeXT__
543 #define object_copy gnu_object_copy
544 #define object_dispose gnu_object_dispose
545 #endif
547 id object_copy(id object);
549 id object_dispose(id object);
551 static inline Class
552 object_get_class(id object)
554 return ((object!=nil)
555 ? (CLS_ISCLASS(object->class_pointer)
556 ? object->class_pointer
557 : (CLS_ISMETA(object->class_pointer)
558 ? (Class)object
559 : Nil))
560 : Nil);
563 static inline const char *
564 object_get_class_name(id object)
566 return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
567 ?object->class_pointer->name
568 :((Class)object)->name)
569 :"Nil");
572 static inline MetaClass
573 object_get_meta_class(id object)
575 return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
576 ?object->class_pointer->class_pointer
577 :(CLS_ISMETA(object->class_pointer)
578 ?object->class_pointer
579 :Nil))
580 :Nil);
583 static inline Class
584 object_get_super_class
585 (id object)
587 return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
588 ?object->class_pointer->super_class
589 :(CLS_ISMETA(object->class_pointer)
590 ?((Class)object)->super_class
591 :Nil))
592 :Nil);
595 static inline BOOL
596 object_is_class (id object)
598 return ((object != nil) && CLS_ISMETA (object->class_pointer));
601 static inline BOOL
602 object_is_instance (id object)
604 return ((object != nil) && CLS_ISCLASS (object->class_pointer));
607 static inline BOOL
608 object_is_meta_class (id object)
610 return ((object != nil)
611 && !object_is_instance (object)
612 && !object_is_class (object));
615 struct sarray*
616 objc_get_uninstalled_dtable(void);
618 #ifdef __cplusplus
620 #endif /* __cplusplus */
622 #endif /* not __objc_api_INCLUDE_GNU */