OSDN Git Service

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