OSDN Git Service

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