2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libobjc / objc / objc-api.h
blobb50fa1bda907ddb304bb4c69f4e8d5f0a75288cc
1 /* GNU Objective-C Runtime API.
2 Copyright (C) 1993, 1995, 1996, 1997, 2002 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, 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/objc.h"
31 #include "objc/hash.h"
32 #include "objc/thr.h"
33 #include <stdio.h>
34 #include <stdarg.h>
36 /* For functions which return Method_t */
37 #define METHOD_NULL (Method_t)0
38 /* Boolean typedefs */
40 ** Method descriptor returned by introspective Object methods.
41 ** This is really just the first part of the more complete objc_method
42 ** structure defined below and used internally by the runtime.
44 struct objc_method_description
46 SEL name; /* this is a selector, not a string */
47 char *types; /* type encoding */
50 /* Filer types used to describe Ivars and Methods. */
51 #define _C_ID '@'
52 #define _C_CLASS '#'
53 #define _C_SEL ':'
54 #define _C_CHR 'c'
55 #define _C_UCHR 'C'
56 #define _C_SHT 's'
57 #define _C_USHT 'S'
58 #define _C_INT 'i'
59 #define _C_UINT 'I'
60 #define _C_LNG 'l'
61 #define _C_ULNG 'L'
62 #define _C_LNG_LNG 'q'
63 #define _C_ULNG_LNG 'Q'
64 #define _C_FLT 'f'
65 #define _C_DBL 'd'
66 #define _C_BFLD 'b'
67 #define _C_VOID 'v'
68 #define _C_UNDEF '?'
69 #define _C_PTR '^'
70 #define _C_CHARPTR '*'
71 #define _C_ATOM '%'
72 #define _C_ARY_B '['
73 #define _C_ARY_E ']'
74 #define _C_UNION_B '('
75 #define _C_UNION_E ')'
76 #define _C_STRUCT_B '{'
77 #define _C_STRUCT_E '}'
78 #define _C_VECTOR '!'
82 ** Error handling
84 ** Call objc_error() or objc_verror() to record an error; this error
85 ** routine will generally exit the program but not necessarily if the
86 ** user has installed his own error handler.
88 ** Call objc_set_error_handler to assign your own function for
89 ** handling errors. The function should return YES if it is ok
90 ** to continue execution, or return NO or just abort if the
91 ** program should be stopped. The default error handler is just to
92 ** print a message on stderr.
94 ** The error handler function should be of type objc_error_handler
95 ** The first parameter is an object instance of relevance.
96 ** The second parameter is an error code.
97 ** The third parameter is a format string in the printf style.
98 ** The fourth parameter is a variable list of arguments.
100 extern void objc_error(id object, int code, const char* fmt, ...);
101 extern void objc_verror(id object, int code, const char* fmt, va_list ap);
102 typedef BOOL (*objc_error_handler)(id, int code, const char *fmt, va_list ap);
103 objc_error_handler objc_set_error_handler(objc_error_handler func);
106 ** Error codes
107 ** These are used by the runtime library, and your
108 ** error handling may use them to determine if the error is
109 ** hard or soft thus whether execution can continue or abort.
111 #define OBJC_ERR_UNKNOWN 0 /* Generic error */
113 #define OBJC_ERR_OBJC_VERSION 1 /* Incorrect runtime version */
114 #define OBJC_ERR_GCC_VERSION 2 /* Incorrect compiler version */
115 #define OBJC_ERR_MODULE_SIZE 3 /* Bad module size */
116 #define OBJC_ERR_PROTOCOL_VERSION 4 /* Incorrect protocol version */
118 #define OBJC_ERR_MEMORY 10 /* Out of memory */
120 #define OBJC_ERR_RECURSE_ROOT 20 /* Attempt to archive the root
121 object more than once. */
122 #define OBJC_ERR_BAD_DATA 21 /* Didn't read expected data */
123 #define OBJC_ERR_BAD_KEY 22 /* Bad key for object */
124 #define OBJC_ERR_BAD_CLASS 23 /* Unknown class */
125 #define OBJC_ERR_BAD_TYPE 24 /* Bad type specification */
126 #define OBJC_ERR_NO_READ 25 /* Cannot read stream */
127 #define OBJC_ERR_NO_WRITE 26 /* Cannot write stream */
128 #define OBJC_ERR_STREAM_VERSION 27 /* Incorrect stream version */
129 #define OBJC_ERR_BAD_OPCODE 28 /* Bad opcode */
131 #define OBJC_ERR_UNIMPLEMENTED 30 /* Method is not implemented */
133 #define OBJC_ERR_BAD_STATE 40 /* Bad thread state */
136 ** Set this variable nonzero to print a line describing each
137 ** message that is sent. (this is currently disabled)
139 extern BOOL objc_trace;
142 /* For every class which happens to have statically allocated instances in
143 this module, one OBJC_STATIC_INSTANCES is allocated by the compiler.
144 INSTANCES is NULL terminated and points to all statically allocated
145 instances of this class. */
146 struct objc_static_instances
148 char *class_name;
149 id instances[0];
153 ** Whereas a Module (defined further down) is the root (typically) of a file,
154 ** a Symtab is the root of the class and category definitions within the
155 ** module.
157 ** A Symtab contains a variable length array of pointers to classes and
158 ** categories defined in the module.
160 typedef struct objc_symtab {
161 unsigned long sel_ref_cnt; /* Unknown. */
162 SEL refs; /* Unknown. */
163 unsigned short cls_def_cnt; /* Number of classes compiled
164 (defined) in the module. */
165 unsigned short cat_def_cnt; /* Number of categories
166 compiled (defined) in the
167 module. */
169 void *defs[1]; /* Variable array of pointers.
170 cls_def_cnt of type Class
171 followed by cat_def_cnt of
172 type Category_t, followed
173 by a NULL terminated array
174 of objc_static_instances. */
175 } Symtab, *Symtab_t;
179 ** The compiler generates one of these structures for each module that
180 ** composes the executable (eg main.m).
182 ** This data structure is the root of the definition tree for the module.
184 ** A collect program runs between ld stages and creates a ObjC ctor array.
185 ** That array holds a pointer to each module structure of the executable.
187 typedef struct objc_module {
188 unsigned long version; /* Compiler revision. */
189 unsigned long size; /* sizeof(Module). */
190 const char* name; /* Name of the file where the
191 module was generated. The
192 name includes the path. */
194 Symtab_t symtab; /* Pointer to the Symtab of
195 the module. The Symtab
196 holds an array of
197 pointers to
198 the classes and categories
199 defined in the module. */
200 } Module, *Module_t;
204 ** The compiler generates one of these structures for a class that has
205 ** instance variables defined in its specification.
207 typedef struct objc_ivar* Ivar_t;
208 typedef struct objc_ivar_list {
209 int ivar_count; /* Number of structures (Ivar)
210 contained in the list. One
211 structure per instance
212 variable defined in the
213 class. */
214 struct objc_ivar {
215 const char* ivar_name; /* Name of the instance
216 variable as entered in the
217 class definition. */
218 const char* ivar_type; /* Description of the Ivar's
219 type. Useful for
220 debuggers. */
221 int ivar_offset; /* Byte offset from the base
222 address of the instance
223 structure to the variable. */
225 } ivar_list[1]; /* Variable length
226 structure. */
227 } IvarList, *IvarList_t;
231 ** The compiler generates one (or more) of these structures for a class that
232 ** has methods defined in its specification.
234 ** The implementation of a class can be broken into separate pieces in a file
235 ** and categories can break them across modules. To handle this problem is a
236 ** singly linked list of methods.
238 typedef struct objc_method Method;
239 typedef Method* Method_t;
240 typedef struct objc_method_list {
241 struct objc_method_list* method_next; /* This variable is used to link
242 a method list to another. It
243 is a singly linked list. */
244 int method_count; /* Number of methods defined in
245 this structure. */
246 struct objc_method {
247 SEL method_name; /* This variable is the method's
248 name. It is a char*.
249 The unique integer passed to
250 objc_msg_send is a char* too.
251 It is compared against
252 method_name using strcmp. */
253 const char* method_types; /* Description of the method's
254 parameter list. Useful for
255 debuggers. */
256 IMP method_imp; /* Address of the method in the
257 executable. */
258 } method_list[1]; /* Variable length
259 structure. */
260 } MethodList, *MethodList_t;
262 struct objc_protocol_list {
263 struct objc_protocol_list *next;
264 size_t count;
265 Protocol *list[1];
269 ** This is used to assure consistent access to the info field of
270 ** classes
272 #ifndef HOST_BITS_PER_LONG
273 #define HOST_BITS_PER_LONG (sizeof(long)*8)
274 #endif
276 #define __CLS_INFO(cls) ((cls)->info)
277 #define __CLS_ISINFO(cls, mask) ((__CLS_INFO(cls)&mask)==mask)
278 #define __CLS_SETINFO(cls, mask) (__CLS_INFO(cls) |= mask)
280 /* The structure is of type MetaClass */
281 #define _CLS_META 0x2L
282 #define CLS_ISMETA(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_META))
285 /* The structure is of type Class */
286 #define _CLS_CLASS 0x1L
287 #define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS))
290 ** The class is initialized within the runtime. This means that
291 ** it has had correct super and sublinks assigned
293 #define _CLS_RESOLV 0x8L
294 #define CLS_ISRESOLV(cls) __CLS_ISINFO(cls, _CLS_RESOLV)
295 #define CLS_SETRESOLV(cls) __CLS_SETINFO(cls, _CLS_RESOLV)
298 ** The class has been send a +initialize message or a such is not
299 ** defined for this class
301 #define _CLS_INITIALIZED 0x04L
302 #define CLS_ISINITIALIZED(cls) __CLS_ISINFO(cls, _CLS_INITIALIZED)
303 #define CLS_SETINITIALIZED(cls) __CLS_SETINFO(cls, _CLS_INITIALIZED)
306 ** The class number of this class. This must be the same for both the
307 ** class and its meta class object
309 #define CLS_GETNUMBER(cls) (__CLS_INFO(cls) >> (HOST_BITS_PER_LONG/2))
310 #define CLS_SETNUMBER(cls, num) \
311 ({ (cls)->info <<= (HOST_BITS_PER_LONG/2); \
312 (cls)->info >>= (HOST_BITS_PER_LONG/2); \
313 __CLS_SETINFO(cls, (((unsigned long)num) << (HOST_BITS_PER_LONG/2))); })
316 ** The compiler generates one of these structures for each category. A class
317 ** may have many categories and contain both instance and factory methods.
319 typedef struct objc_category {
320 const char* category_name; /* Name of the category. Name
321 contained in the () of the
322 category definition. */
323 const char* class_name; /* Name of the class to which
324 the category belongs. */
325 MethodList_t instance_methods; /* Linked list of instance
326 methods defined in the
327 category. NULL indicates no
328 instance methods defined. */
329 MethodList_t class_methods; /* Linked list of factory
330 methods defined in the
331 category. NULL indicates no
332 class methods defined. */
333 struct objc_protocol_list *protocols; /* List of Protocols
334 conformed to */
335 } Category, *Category_t;
338 ** Structure used when a message is send to a class's super class. The
339 ** compiler generates one of these structures and passes it to
340 ** objc_msg_super.
342 typedef struct objc_super {
343 id self; /* Id of the object sending
344 the message. */
345 Class class; /* Object's super class. */
346 } Super, *Super_t;
348 IMP objc_msg_lookup_super(Super_t super, SEL sel);
350 retval_t objc_msg_sendv(id, SEL, arglist_t);
355 ** This is a hook which is called by objc_lookup_class and
356 ** objc_get_class if the runtime is not able to find the class.
357 ** This may e.g. try to load in the class using dynamic loading.
358 ** The function is guaranteed to be passed a non-NULL name string.
360 extern Class (*_objc_lookup_class)(const char *name);
363 ** This is a hook which is called by __objc_exec_class every time a class
364 ** or a category is loaded into the runtime. This may e.g. help a
365 ** dynamic loader determine the classes that have been loaded when
366 ** an object file is dynamically linked in.
368 extern void (*_objc_load_callback)(Class class, Category* category);
371 ** Hook functions for allocating, copying and disposing of instances
373 extern id (*_objc_object_alloc)(Class class);
374 extern id (*_objc_object_copy)(id object);
375 extern id (*_objc_object_dispose)(id object);
378 ** Standard functions for memory allocation and disposal.
379 ** Users should use these functions in their ObjC programs so
380 ** that they work properly with garbage collectors as well as
381 ** can take advantage of the exception/error handling available.
383 void *
384 objc_malloc(size_t size);
386 void *
387 objc_atomic_malloc(size_t size);
389 void *
390 objc_valloc(size_t size);
392 void *
393 objc_realloc(void *mem, size_t size);
395 void *
396 objc_calloc(size_t nelem, size_t size);
398 void
399 objc_free(void *mem);
402 ** Hook functions for memory allocation and disposal.
403 ** This makes it easy to substitute garbage collection systems
404 ** such as Boehm's GC by assigning these function pointers
405 ** to the GC's allocation routines. By default these point
406 ** to the ANSI standard malloc, realloc, free, etc.
408 ** Users should call the normal objc routines above for
409 ** memory allocation and disposal within their programs.
411 extern void *(*_objc_malloc)(size_t);
412 extern void *(*_objc_atomic_malloc)(size_t);
413 extern void *(*_objc_valloc)(size_t);
414 extern void *(*_objc_realloc)(void *, size_t);
415 extern void *(*_objc_calloc)(size_t, size_t);
416 extern void (*_objc_free)(void *);
419 ** Hook for method forwarding. This makes it easy to substitute a
420 ** library, such as ffcall, that implements closures, thereby avoiding
421 ** gcc's __builtin_apply problems.
423 extern IMP (*__objc_msg_forward)(SEL);
425 Method_t class_get_class_method(MetaClass class, SEL aSel);
427 Method_t class_get_instance_method(Class class, SEL aSel);
429 Class class_pose_as(Class impostor, Class superclass);
431 Class objc_get_class(const char *name);
433 Class objc_lookup_class(const char *name);
435 Class objc_next_class(void **enum_state);
437 const char *sel_get_name(SEL selector);
439 const char *sel_get_type(SEL selector);
441 SEL sel_get_uid(const char *name);
443 SEL sel_get_any_uid(const char *name);
445 SEL sel_get_any_typed_uid(const char *name);
447 SEL sel_get_typed_uid(const char *name, const char*);
449 SEL sel_register_name(const char *name);
451 SEL sel_register_typed_name(const char *name, const char*type);
454 BOOL sel_is_mapped (SEL aSel);
456 extern id class_create_instance(Class class);
458 static inline const char *
459 class_get_class_name(Class class)
461 return CLS_ISCLASS(class)?class->name:((class==Nil)?"Nil":0);
464 static inline long
465 class_get_instance_size(Class class)
467 return CLS_ISCLASS(class)?class->instance_size:0;
470 static inline MetaClass
471 class_get_meta_class(Class class)
473 return CLS_ISCLASS(class)?class->class_pointer:Nil;
476 static inline Class
477 class_get_super_class(Class class)
479 return CLS_ISCLASS(class)?class->super_class:Nil;
482 static inline int
483 class_get_version(Class class)
485 return CLS_ISCLASS(class)?class->version:-1;
488 static inline BOOL
489 class_is_class(Class class)
491 return CLS_ISCLASS(class);
494 static inline BOOL
495 class_is_meta_class(Class class)
497 return CLS_ISMETA(class);
501 static inline void
502 class_set_version(Class class, long version)
504 if (CLS_ISCLASS(class))
505 class->version = version;
508 static inline void *
509 class_get_gc_object_type (Class class)
511 return CLS_ISCLASS(class) ? class->gc_object_type : NULL;
514 /* Mark the instance variable as innaccessible to the garbage collector */
515 extern void class_ivar_set_gcinvisible (Class class,
516 const char* ivarname,
517 BOOL gcInvisible);
519 static inline IMP
520 method_get_imp(Method_t method)
522 return (method!=METHOD_NULL)?method->method_imp:(IMP)0;
525 IMP get_imp (Class class, SEL sel);
527 /* Redefine on NeXTSTEP so as not to conflict with system function */
528 #ifdef __NeXT__
529 #define object_copy gnu_object_copy
530 #define object_dispose gnu_object_dispose
531 #endif
533 id object_copy(id object);
535 id object_dispose(id object);
537 static inline Class
538 object_get_class(id object)
540 return ((object!=nil)
541 ? (CLS_ISCLASS(object->class_pointer)
542 ? object->class_pointer
543 : (CLS_ISMETA(object->class_pointer)
544 ? (Class)object
545 : Nil))
546 : Nil);
549 static inline const char *
550 object_get_class_name(id object)
552 return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
553 ?object->class_pointer->name
554 :((Class)object)->name)
555 :"Nil");
558 static inline MetaClass
559 object_get_meta_class(id object)
561 return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
562 ?object->class_pointer->class_pointer
563 :(CLS_ISMETA(object->class_pointer)
564 ?object->class_pointer
565 :Nil))
566 :Nil);
569 static inline Class
570 object_get_super_class
571 (id object)
573 return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
574 ?object->class_pointer->super_class
575 :(CLS_ISMETA(object->class_pointer)
576 ?((Class)object)->super_class
577 :Nil))
578 :Nil);
581 static inline BOOL
582 object_is_class (id object)
584 return ((object != nil) && CLS_ISMETA (object->class_pointer));
587 static inline BOOL
588 object_is_instance (id object)
590 return ((object != nil) && CLS_ISCLASS (object->class_pointer));
593 static inline BOOL
594 object_is_meta_class (id object)
596 return ((object != nil)
597 && !object_is_instance (object)
598 && !object_is_class (object));
601 struct sarray*
602 objc_get_uninstalled_dtable(void);
604 #endif /* not __objc_api_INCLUDE_GNU */