Move entry to proper file.
[official-gcc.git] / libobjc / objc / objc-api.h
blobe0e49e21fdfc89f86f424978b39802069951e5a4
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 '!'
85 #define _C_COMPLEX 'j'
89 ** Error handling
91 ** Call objc_error() or objc_verror() to record an error; this error
92 ** routine will generally exit the program but not necessarily if the
93 ** user has installed his own error handler.
95 ** Call objc_set_error_handler to assign your own function for
96 ** handling errors. The function should return YES if it is ok
97 ** to continue execution, or return NO or just abort if the
98 ** program should be stopped. The default error handler is just to
99 ** print a message on stderr.
101 ** The error handler function should be of type objc_error_handler
102 ** The first parameter is an object instance of relevance.
103 ** The second parameter is an error code.
104 ** The third parameter is a format string in the printf style.
105 ** The fourth parameter is a variable list of arguments.
107 extern void objc_error(id object, int code, const char* fmt, ...);
108 extern void objc_verror(id object, int code, const char* fmt, va_list ap);
109 typedef BOOL (*objc_error_handler)(id, int code, const char *fmt, va_list ap);
110 extern objc_error_handler objc_set_error_handler(objc_error_handler func);
113 ** Error codes
114 ** These are used by the runtime library, and your
115 ** error handling may use them to determine if the error is
116 ** hard or soft thus whether execution can continue or abort.
118 #define OBJC_ERR_UNKNOWN 0 /* Generic error */
120 #define OBJC_ERR_OBJC_VERSION 1 /* Incorrect runtime version */
121 #define OBJC_ERR_GCC_VERSION 2 /* Incorrect compiler version */
122 #define OBJC_ERR_MODULE_SIZE 3 /* Bad module size */
123 #define OBJC_ERR_PROTOCOL_VERSION 4 /* Incorrect protocol version */
125 #define OBJC_ERR_MEMORY 10 /* Out of memory */
127 #define OBJC_ERR_RECURSE_ROOT 20 /* Attempt to archive the root
128 object more than once. */
129 #define OBJC_ERR_BAD_DATA 21 /* Didn't read expected data */
130 #define OBJC_ERR_BAD_KEY 22 /* Bad key for object */
131 #define OBJC_ERR_BAD_CLASS 23 /* Unknown class */
132 #define OBJC_ERR_BAD_TYPE 24 /* Bad type specification */
133 #define OBJC_ERR_NO_READ 25 /* Cannot read stream */
134 #define OBJC_ERR_NO_WRITE 26 /* Cannot write stream */
135 #define OBJC_ERR_STREAM_VERSION 27 /* Incorrect stream version */
136 #define OBJC_ERR_BAD_OPCODE 28 /* Bad opcode */
138 #define OBJC_ERR_UNIMPLEMENTED 30 /* Method is not implemented */
140 #define OBJC_ERR_BAD_STATE 40 /* Bad thread state */
143 ** Set this variable nonzero to print a line describing each
144 ** message that is sent. (this is currently disabled)
146 extern BOOL objc_trace;
149 /* For every class which happens to have statically allocated instances in
150 this module, one OBJC_STATIC_INSTANCES is allocated by the compiler.
151 INSTANCES is NULL terminated and points to all statically allocated
152 instances of this class. */
153 struct objc_static_instances
155 char *class_name;
156 #ifdef __cplusplus
157 id instances[1];
158 #else
159 id instances[0];
160 #endif
164 ** Whereas a Module (defined further down) is the root (typically) of a file,
165 ** a Symtab is the root of the class and category definitions within the
166 ** module.
168 ** A Symtab contains a variable length array of pointers to classes and
169 ** categories defined in the module.
171 typedef struct objc_symtab {
172 unsigned long sel_ref_cnt; /* Unknown. */
173 SEL refs; /* Unknown. */
174 unsigned short cls_def_cnt; /* Number of classes compiled
175 (defined) in the module. */
176 unsigned short cat_def_cnt; /* Number of categories
177 compiled (defined) in the
178 module. */
180 void *defs[1]; /* Variable array of pointers.
181 cls_def_cnt of type Class
182 followed by cat_def_cnt of
183 type Category_t, followed
184 by a NULL terminated array
185 of objc_static_instances. */
186 } Symtab, *Symtab_t;
190 ** The compiler generates one of these structures for each module that
191 ** composes the executable (eg main.m).
193 ** This data structure is the root of the definition tree for the module.
195 ** A collect program runs between ld stages and creates a ObjC ctor array.
196 ** That array holds a pointer to each module structure of the executable.
198 typedef struct objc_module {
199 unsigned long version; /* Compiler revision. */
200 unsigned long size; /* sizeof(Module). */
201 const char* name; /* Name of the file where the
202 module was generated. The
203 name includes the path. */
205 Symtab_t symtab; /* Pointer to the Symtab of
206 the module. The Symtab
207 holds an array of
208 pointers to
209 the classes and categories
210 defined in the module. */
211 } Module, *Module_t;
215 ** The compiler generates one of these structures for a class that has
216 ** instance variables defined in its specification.
218 typedef struct objc_ivar {
219 const char* ivar_name; /* Name of the instance
220 variable as entered in the
221 class definition. */
222 const char* ivar_type; /* Description of the Ivar's
223 type. Useful for
224 debuggers. */
225 int ivar_offset; /* Byte offset from the base
226 address of the instance
227 structure to the variable. */
228 } *Ivar_t;
230 typedef struct objc_ivar_list {
231 int ivar_count; /* Number of structures (Ivar)
232 contained in the list. One
233 structure per instance
234 variable defined in the
235 class. */
236 struct objc_ivar ivar_list[1]; /* Variable length
237 structure. */
238 } IvarList, *IvarList_t;
242 ** The compiler generates one (or more) of these structures for a class that
243 ** has methods defined in its specification.
245 ** The implementation of a class can be broken into separate pieces in a file
246 ** and categories can break them across modules. To handle this problem is a
247 ** singly linked list of methods.
249 typedef struct objc_method {
250 SEL method_name; /* This variable is the method's
251 name. It is a char*.
252 The unique integer passed to
253 objc_msg_send is a char* too.
254 It is compared against
255 method_name using strcmp. */
256 const char* method_types; /* Description of the method's
257 parameter list. Useful for
258 debuggers. */
259 IMP method_imp; /* Address of the method in the
260 executable. */
261 } Method, *Method_t;
263 typedef struct objc_method_list {
264 struct objc_method_list* method_next; /* This variable is used to link
265 a method list to another. It
266 is a singly linked list. */
267 int method_count; /* Number of methods defined in
268 this structure. */
269 Method method_list[1]; /* Variable length
270 structure. */
271 } MethodList, *MethodList_t;
273 struct objc_protocol_list {
274 struct objc_protocol_list *next;
275 size_t count;
276 Protocol *list[1];
280 ** This is used to assure consistent access to the info field of
281 ** classes
283 #ifndef HOST_BITS_PER_LONG
284 #define HOST_BITS_PER_LONG (sizeof(long)*8)
285 #endif
287 #define __CLS_INFO(cls) ((cls)->info)
288 #define __CLS_ISINFO(cls, mask) ((__CLS_INFO(cls)&mask)==mask)
289 #define __CLS_SETINFO(cls, mask) (__CLS_INFO(cls) |= mask)
291 /* The structure is of type MetaClass */
292 #define _CLS_META 0x2L
293 #define CLS_ISMETA(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_META))
296 /* The structure is of type Class */
297 #define _CLS_CLASS 0x1L
298 #define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS))
301 ** The class is initialized within the runtime. This means that
302 ** it has had correct super and sublinks assigned
304 #define _CLS_RESOLV 0x8L
305 #define CLS_ISRESOLV(cls) __CLS_ISINFO(cls, _CLS_RESOLV)
306 #define CLS_SETRESOLV(cls) __CLS_SETINFO(cls, _CLS_RESOLV)
309 ** The class has been send a +initialize message or a such is not
310 ** defined for this class
312 #define _CLS_INITIALIZED 0x04L
313 #define CLS_ISINITIALIZED(cls) __CLS_ISINFO(cls, _CLS_INITIALIZED)
314 #define CLS_SETINITIALIZED(cls) __CLS_SETINFO(cls, _CLS_INITIALIZED)
317 ** The class number of this class. This must be the same for both the
318 ** class and its meta class object
320 #define CLS_GETNUMBER(cls) (__CLS_INFO(cls) >> (HOST_BITS_PER_LONG/2))
321 #define CLS_SETNUMBER(cls, num) \
322 ({ (cls)->info <<= (HOST_BITS_PER_LONG/2); \
323 (cls)->info >>= (HOST_BITS_PER_LONG/2); \
324 __CLS_SETINFO(cls, (((unsigned long)num) << (HOST_BITS_PER_LONG/2))); })
327 ** The compiler generates one of these structures for each category. A class
328 ** may have many categories and contain both instance and factory methods.
330 typedef struct objc_category {
331 const char* category_name; /* Name of the category. Name
332 contained in the () of the
333 category definition. */
334 const char* class_name; /* Name of the class to which
335 the category belongs. */
336 MethodList_t instance_methods; /* Linked list of instance
337 methods defined in the
338 category. NULL indicates no
339 instance methods defined. */
340 MethodList_t class_methods; /* Linked list of factory
341 methods defined in the
342 category. NULL indicates no
343 class methods defined. */
344 struct objc_protocol_list *protocols; /* List of Protocols
345 conformed to */
346 } Category, *Category_t;
349 ** Structure used when a message is send to a class's super class. The
350 ** compiler generates one of these structures and passes it to
351 ** objc_msg_super.
353 typedef struct objc_super {
354 id self; /* Id of the object sending
355 the message. */
356 #ifdef __cplusplus
357 Class super_class;
358 #else
359 Class class; /* Object's super class. */
360 #endif
361 } Super, *Super_t;
363 IMP objc_msg_lookup_super(Super_t super, SEL sel);
365 retval_t objc_msg_sendv(id, SEL, arglist_t);
370 ** This is a hook which is called by objc_lookup_class and
371 ** objc_get_class if the runtime is not able to find the class.
372 ** This may e.g. try to load in the class using dynamic loading.
373 ** The function is guaranteed to be passed a non-NULL name string.
375 objc_EXPORT Class (*_objc_lookup_class)(const char *name);
378 ** This is a hook which is called by __objc_exec_class every time a class
379 ** or a category is loaded into the runtime. This may e.g. help a
380 ** dynamic loader determine the classes that have been loaded when
381 ** an object file is dynamically linked in.
383 objc_EXPORT void (*_objc_load_callback)(Class _class, Category* category);
386 ** Hook functions for allocating, copying and disposing of instances
388 objc_EXPORT id (*_objc_object_alloc)(Class _class);
389 objc_EXPORT id (*_objc_object_copy)(id object);
390 objc_EXPORT id (*_objc_object_dispose)(id object);
393 ** Standard functions for memory allocation and disposal.
394 ** Users should use these functions in their ObjC programs so
395 ** that they work properly with garbage collectors as well as
396 ** can take advantage of the exception/error handling available.
398 void *
399 objc_malloc(size_t size);
401 void *
402 objc_atomic_malloc(size_t size);
404 void *
405 objc_valloc(size_t size);
407 void *
408 objc_realloc(void *mem, size_t size);
410 void *
411 objc_calloc(size_t nelem, size_t size);
413 void
414 objc_free(void *mem);
417 ** Hook functions for memory allocation and disposal.
418 ** This makes it easy to substitute garbage collection systems
419 ** such as Boehm's GC by assigning these function pointers
420 ** to the GC's allocation routines. By default these point
421 ** to the ANSI standard malloc, realloc, free, etc.
423 ** Users should call the normal objc routines above for
424 ** memory allocation and disposal within their programs.
426 objc_EXPORT void *(*_objc_malloc)(size_t);
427 objc_EXPORT void *(*_objc_atomic_malloc)(size_t);
428 objc_EXPORT void *(*_objc_valloc)(size_t);
429 objc_EXPORT void *(*_objc_realloc)(void *, size_t);
430 objc_EXPORT void *(*_objc_calloc)(size_t, size_t);
431 objc_EXPORT void (*_objc_free)(void *);
434 ** Hook for method forwarding. This makes it easy to substitute a
435 ** library, such as ffcall, that implements closures, thereby avoiding
436 ** gcc's __builtin_apply problems.
438 objc_EXPORT IMP (*__objc_msg_forward)(SEL);
440 Method_t class_get_class_method(MetaClass _class, SEL aSel);
442 Method_t class_get_instance_method(Class _class, SEL aSel);
444 Class class_pose_as(Class impostor, Class superclass);
446 Class objc_get_class(const char *name);
448 Class objc_lookup_class(const char *name);
450 Class objc_next_class(void **enum_state);
452 const char *sel_get_name(SEL selector);
454 const char *sel_get_type(SEL selector);
456 SEL sel_get_uid(const char *name);
458 SEL sel_get_any_uid(const char *name);
460 SEL sel_get_any_typed_uid(const char *name);
462 SEL sel_get_typed_uid(const char *name, const char*);
464 SEL sel_register_name(const char *name);
466 SEL sel_register_typed_name(const char *name, const char*type);
469 BOOL sel_is_mapped (SEL aSel);
471 extern id class_create_instance(Class _class);
473 static inline const char *
474 class_get_class_name(Class _class)
476 return CLS_ISCLASS(_class)?_class->name:((_class==Nil)?"Nil":0);
479 static inline long
480 class_get_instance_size(Class _class)
482 return CLS_ISCLASS(_class)?_class->instance_size:0;
485 static inline MetaClass
486 class_get_meta_class(Class _class)
488 return CLS_ISCLASS(_class)?_class->class_pointer:Nil;
491 static inline Class
492 class_get_super_class(Class _class)
494 return CLS_ISCLASS(_class)?_class->super_class:Nil;
497 static inline int
498 class_get_version(Class _class)
500 return CLS_ISCLASS(_class)?_class->version:-1;
503 static inline BOOL
504 class_is_class(Class _class)
506 return CLS_ISCLASS(_class);
509 static inline BOOL
510 class_is_meta_class(Class _class)
512 return CLS_ISMETA(_class);
516 static inline void
517 class_set_version(Class _class, long version)
519 if (CLS_ISCLASS(_class))
520 _class->version = version;
523 static inline void *
524 class_get_gc_object_type (Class _class)
526 return CLS_ISCLASS(_class) ? _class->gc_object_type : NULL;
529 /* Mark the instance variable as innaccessible to the garbage collector */
530 extern void class_ivar_set_gcinvisible (Class _class,
531 const char* ivarname,
532 BOOL gcInvisible);
534 static inline IMP
535 method_get_imp(Method_t method)
537 return (method!=METHOD_NULL)?method->method_imp:(IMP)0;
540 IMP get_imp (Class _class, SEL sel);
542 /* Redefine on NeXTSTEP so as not to conflict with system function */
543 #ifdef __NeXT__
544 #define object_copy gnu_object_copy
545 #define object_dispose gnu_object_dispose
546 #endif
548 id object_copy(id object);
550 id object_dispose(id object);
552 static inline Class
553 object_get_class(id object)
555 return ((object!=nil)
556 ? (CLS_ISCLASS(object->class_pointer)
557 ? object->class_pointer
558 : (CLS_ISMETA(object->class_pointer)
559 ? (Class)object
560 : Nil))
561 : Nil);
564 static inline const char *
565 object_get_class_name(id object)
567 return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
568 ?object->class_pointer->name
569 :((Class)object)->name)
570 :"Nil");
573 static inline MetaClass
574 object_get_meta_class(id object)
576 return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
577 ?object->class_pointer->class_pointer
578 :(CLS_ISMETA(object->class_pointer)
579 ?object->class_pointer
580 :Nil))
581 :Nil);
584 static inline Class
585 object_get_super_class
586 (id object)
588 return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
589 ?object->class_pointer->super_class
590 :(CLS_ISMETA(object->class_pointer)
591 ?((Class)object)->super_class
592 :Nil))
593 :Nil);
596 static inline BOOL
597 object_is_class (id object)
599 return ((object != nil) && CLS_ISMETA (object->class_pointer));
602 static inline BOOL
603 object_is_instance (id object)
605 return ((object != nil) && CLS_ISCLASS (object->class_pointer));
608 static inline BOOL
609 object_is_meta_class (id object)
611 return ((object != nil)
612 && !object_is_instance (object)
613 && !object_is_class (object));
616 struct sarray*
617 objc_get_uninstalled_dtable(void);
619 #ifdef __cplusplus
621 #endif /* __cplusplus */
623 #endif /* not __objc_api_INCLUDE_GNU */