OSDN Git Service

Add NIOS2 support. Code from SourceyG++.
[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 /* Set this variable nonzero to print a line describing each
137    message that is sent.  (this is currently disabled)  */
138 extern BOOL objc_trace;
139
140
141 /* For every class which happens to have statically allocated instances in
142    this module, one OBJC_STATIC_INSTANCES is allocated by the compiler.
143    INSTANCES is NULL terminated and points to all statically allocated
144    instances of this class.  */
145 struct objc_static_instances
146 {
147   char *class_name;
148 #ifdef __cplusplus
149   id instances[1];
150 #else
151   id instances[0];
152 #endif
153 };
154
155 /* Whereas a Module (defined further down) is the root (typically) of a file,
156    a Symtab is the root of the class and category definitions within the
157    module.  
158    
159    A Symtab contains a variable length array of pointers to classes and
160    categories  defined in the module.   */
161 typedef struct objc_symtab {
162   unsigned long sel_ref_cnt;                     /* Unknown. */
163   SEL        refs;                              /* Unknown. */
164   unsigned short cls_def_cnt;                   /* Number of classes compiled
165                                                   (defined) in the module. */
166   unsigned short cat_def_cnt;                   /* Number of categories 
167                                                   compiled (defined) in the 
168                                                   module. */
169
170   void      *defs[1];                           /* Variable array of pointers.
171                                                   cls_def_cnt of type Class 
172                                                   followed by cat_def_cnt of
173                                                   type Category_t, followed
174                                                   by a NULL terminated array
175                                                   of objc_static_instances. */
176 } Symtab,   *Symtab_t;
177
178
179 /*
180 ** The compiler generates one of these structures for each module that
181 ** composes the executable (eg main.m).  
182 ** 
183 ** This data structure is the root of the definition tree for the module.  
184 ** 
185 ** A collect program runs between ld stages and creates a ObjC ctor array. 
186 ** That array holds a pointer to each module structure of the executable. 
187 */
188 typedef struct objc_module {
189   unsigned long version;                        /* Compiler revision. */
190   unsigned long size;                           /* sizeof(Module). */
191   const char* name;                             /* Name of the file where the 
192                                                   module was generated.   The 
193                                                   name includes the path. */
194
195   Symtab_t    symtab;                           /* Pointer to the Symtab of
196                                                   the module.  The Symtab
197                                                   holds an array of 
198                                                   pointers to 
199                                                   the classes and categories 
200                                                   defined in the module. */
201 } Module, *Module_t;
202
203
204 /*
205 ** The compiler generates one of these structures for a class that has
206 ** instance variables defined in its specification. 
207 */
208 typedef struct objc_ivar {
209     const char* ivar_name;                      /* Name of the instance
210                                                   variable as entered in the
211                                                   class definition. */
212     const char* ivar_type;                      /* Description of the Ivar's
213                                                   type.  Useful for 
214                                                   debuggers. */
215     int        ivar_offset;                    /* Byte offset from the base 
216                                                   address of the instance 
217                                                   structure to the variable. */
218 } *Ivar_t;
219
220 typedef struct objc_ivar_list {
221   int   ivar_count;                             /* Number of structures (Ivar) 
222                                                   contained in the list.  One
223                                                   structure per instance 
224                                                   variable defined in the
225                                                   class. */
226   struct objc_ivar ivar_list[1];               /* Variable length 
227                                                   structure. */
228 } IvarList, *IvarList_t;
229
230
231 /*
232 ** The compiler generates one (or more) of these structures for a class that
233 ** has methods defined in its specification. 
234 ** 
235 ** The implementation of a class can be broken into separate pieces in a file
236 ** and categories can break them across modules. To handle this problem is a
237 ** singly linked list of methods. 
238 */
239 typedef struct objc_method {
240   SEL         method_name;                  /* This variable is the method's 
241                                                name.  It is a char*. 
242                                                The unique integer passed to 
243                                                objc_msg_send is a char* too.  
244                                                It is compared against 
245                                                method_name using strcmp. */
246   const char* method_types;                 /* Description of the method's
247                                                parameter list.  Useful for
248                                                debuggers. */
249   IMP         method_imp;                   /* Address of the method in the 
250                                                executable. */
251 } Method, *Method_t;
252
253 typedef struct objc_method_list {
254   struct objc_method_list*  method_next;    /* This variable is used to link 
255                                                a method list to another.  It 
256                                                is a singly linked list. */
257   int            method_count;              /* Number of methods defined in 
258                                                this structure. */
259   Method method_list[1];                    /* Variable length 
260                                                structure. */
261 } MethodList, *MethodList_t;
262
263 struct objc_protocol_list {
264   struct objc_protocol_list *next;
265   size_t count;
266   Protocol *list[1];
267 };
268
269 /*
270 ** This is used to assure consistent access to the info field of 
271 ** classes
272 */
273 #ifndef HOST_BITS_PER_LONG
274 #define HOST_BITS_PER_LONG  (sizeof(long)*8)
275 #endif 
276
277 #define __CLS_INFO(cls) ((cls)->info)
278 #define __CLS_ISINFO(cls, mask) ((__CLS_INFO(cls)&mask)==mask)
279 #define __CLS_SETINFO(cls, mask) (__CLS_INFO(cls) |= mask)
280
281 /* The structure is of type MetaClass */
282 #define _CLS_META 0x2L
283 #define CLS_ISMETA(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_META))
284
285
286 /* The structure is of type Class */
287 #define _CLS_CLASS 0x1L
288 #define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS))
289
290 /*
291 ** The class is initialized within the runtime.  This means that 
292 ** it has had correct super and sublinks assigned
293 */
294 #define _CLS_RESOLV 0x8L
295 #define CLS_ISRESOLV(cls) __CLS_ISINFO(cls, _CLS_RESOLV)
296 #define CLS_SETRESOLV(cls) __CLS_SETINFO(cls, _CLS_RESOLV)
297
298 /*
299 ** The class has been send a +initialize message or a such is not 
300 ** defined for this class
301 */
302 #define _CLS_INITIALIZED 0x04L
303 #define CLS_ISINITIALIZED(cls) __CLS_ISINFO(cls, _CLS_INITIALIZED)
304 #define CLS_SETINITIALIZED(cls) __CLS_SETINFO(cls, _CLS_INITIALIZED)
305
306 /*
307 ** The class number of this class.  This must be the same for both the 
308 ** class and its meta class object
309 */
310 #define CLS_GETNUMBER(cls) (__CLS_INFO(cls) >> (HOST_BITS_PER_LONG/2))
311 #define CLS_SETNUMBER(cls, num) \
312   ({ (cls)->info <<= (HOST_BITS_PER_LONG/2); \
313      (cls)->info >>= (HOST_BITS_PER_LONG/2); \
314      __CLS_SETINFO(cls, (((unsigned long)num) << (HOST_BITS_PER_LONG/2))); })
315
316 /*
317 ** The compiler generates one of these structures for each category.  A class
318 ** may have many categories and contain both instance and factory methods.  
319 */
320 typedef struct objc_category {
321   const char*   category_name;                /* Name of the category.  Name
322                                                 contained in the () of the
323                                                 category definition. */
324   const char*   class_name;                   /* Name of the class to which
325                                                 the category belongs. */
326   MethodList_t  instance_methods;             /* Linked list of instance
327                                                 methods defined in the 
328                                                 category. NULL indicates no
329                                                 instance methods defined. */
330   MethodList_t  class_methods;                /* Linked list of factory 
331                                                 methods defined in the
332                                                 category.  NULL indicates no
333                                                 class methods defined. */
334   struct objc_protocol_list *protocols;       /* List of Protocols 
335                                                  conformed to */
336 } Category, *Category_t;
337
338 /*
339 ** Structure used when a message is send to a class's super class.  The
340 ** compiler generates one of these structures and passes it to
341 ** objc_msg_super.
342 */
343 typedef struct objc_super {
344   id      self;                           /* Id of the object sending
345                                                 the message. */
346 #ifdef __cplusplus
347   Class super_class;
348 #else
349   Class class;                              /* Object's super class. */
350 #endif
351 } Super, *Super_t;
352
353 IMP objc_msg_lookup_super(Super_t super, SEL sel);
354
355 retval_t objc_msg_sendv(id, SEL, arglist_t);
356
357
358
359 /*
360 ** This is a hook which is called by objc_lookup_class and
361 ** objc_get_class if the runtime is not able to find the class.
362 ** This may e.g. try to load in the class using dynamic loading.
363 ** The function is guaranteed to be passed a non-NULL name string.
364 */
365 objc_EXPORT Class (*_objc_lookup_class)(const char *name);
366
367 /*
368 ** This is a hook which is called by __objc_exec_class every time a class
369 ** or a category is loaded into the runtime.  This may e.g. help a
370 ** dynamic loader determine the classes that have been loaded when
371 ** an object file is dynamically linked in.
372 */
373 objc_EXPORT void (*_objc_load_callback)(Class _class, Category* category);
374
375 /*
376 ** Hook functions for allocating, copying and disposing of instances
377 */
378 objc_EXPORT id (*_objc_object_alloc)(Class _class);
379 objc_EXPORT id (*_objc_object_copy)(id object);
380 objc_EXPORT id (*_objc_object_dispose)(id object);
381
382 /*
383 ** Standard functions for memory allocation and disposal.
384 ** Users should use these functions in their ObjC programs so
385 ** that they work properly with garbage collectors as well as
386 ** can take advantage of the exception/error handling available.
387 */
388 void *
389 objc_malloc(size_t size);
390
391 void *
392 objc_atomic_malloc(size_t size);
393
394 void *
395 objc_valloc(size_t size);
396
397 void *
398 objc_realloc(void *mem, size_t size);
399
400 void *
401 objc_calloc(size_t nelem, size_t size);
402
403 void
404 objc_free(void *mem);
405
406 /*
407 ** Hook functions for memory allocation and disposal.
408 ** This makes it easy to substitute garbage collection systems
409 ** such as Boehm's GC by assigning these function pointers
410 ** to the GC's allocation routines.  By default these point
411 ** to the ANSI standard malloc, realloc, free, etc.
412 **
413 ** Users should call the normal objc routines above for
414 ** memory allocation and disposal within their programs.
415 */
416 objc_EXPORT void *(*_objc_malloc)(size_t);
417 objc_EXPORT void *(*_objc_atomic_malloc)(size_t);
418 objc_EXPORT void *(*_objc_valloc)(size_t);
419 objc_EXPORT void *(*_objc_realloc)(void *, size_t);
420 objc_EXPORT void *(*_objc_calloc)(size_t, size_t);
421 objc_EXPORT void (*_objc_free)(void *);
422
423 /*
424 **  Hooks for method forwarding. This makes it easy to substitute a
425 **  library, such as ffcall, that implements closures, thereby avoiding
426 **  gcc's __builtin_apply problems.  __objc_msg_forward2's result will
427 **  be preferred over that of __objc_msg_forward if both are set and
428 **  return non-NULL.
429 */
430 objc_EXPORT IMP (*__objc_msg_forward)(SEL);
431 objc_EXPORT IMP (*__objc_msg_forward2)(id, SEL);
432
433 /*
434 ** Hook for uncaught exceptions.  This hook is called when an exception
435 ** is thrown and no valid exception handler is in place.  The function
436 ** is expected never to return.  If the function returns the result is
437 ** currently undefined.
438 */
439 objc_EXPORT void (*_objc_unexpected_exception)(id);
440
441
442 Method_t class_get_class_method(MetaClass _class, SEL aSel);
443
444 Method_t class_get_instance_method(Class _class, SEL aSel);
445
446 Class class_pose_as(Class impostor, Class superclass);
447
448 Class objc_get_class(const char *name);
449
450 Class objc_lookup_class(const char *name);
451
452 Class objc_next_class(void **enum_state);
453
454 const char *sel_get_name(SEL selector);
455
456 const char *sel_get_type(SEL selector);
457
458 SEL sel_get_uid(const char *name);
459
460 SEL sel_get_any_uid(const char *name);
461
462 SEL sel_get_any_typed_uid(const char *name);
463
464 SEL sel_get_typed_uid(const char *name, const char*);
465
466 SEL sel_register_name(const char *name);
467
468 SEL sel_register_typed_name(const char *name, const char*type);
469
470
471 BOOL sel_is_mapped (SEL aSel);
472
473 extern id class_create_instance(Class _class);
474
475 static inline const char *
476 class_get_class_name(Class _class)
477 {
478   return CLS_ISCLASS(_class)?_class->name:((_class==Nil)?"Nil":0);
479 }
480
481 static inline long
482 class_get_instance_size(Class _class)
483 {
484   return CLS_ISCLASS(_class)?_class->instance_size:0;
485 }
486
487 static inline MetaClass
488 class_get_meta_class(Class _class)
489 {
490   return CLS_ISCLASS(_class)?_class->class_pointer:Nil;
491 }
492
493 static inline Class
494 class_get_super_class(Class _class)
495 {
496   return CLS_ISCLASS(_class)?_class->super_class:Nil;
497 }
498
499 static inline int
500 class_get_version(Class _class)
501 {
502   return CLS_ISCLASS(_class)?_class->version:-1;
503 }
504
505 static inline BOOL
506 class_is_class(Class _class)
507 {
508   return CLS_ISCLASS(_class);
509 }
510
511 static inline BOOL
512 class_is_meta_class(Class _class)
513 {
514   return CLS_ISMETA(_class);
515 }
516
517
518 static inline void
519 class_set_version(Class _class, long version)
520 {
521   if (CLS_ISCLASS(_class))
522     _class->version = version;
523 }
524
525 static inline void *
526 class_get_gc_object_type (Class _class)
527 {
528   return CLS_ISCLASS(_class) ? _class->gc_object_type : NULL;
529 }
530
531 /* Mark the instance variable as innaccessible to the garbage collector */
532 extern void class_ivar_set_gcinvisible (Class _class,
533                                         const char* ivarname,
534                                         BOOL gcInvisible);
535
536 static inline IMP
537 method_get_imp(Method_t method)
538 {
539   return (method!=METHOD_NULL)?method->method_imp:(IMP)0;
540 }
541
542 IMP get_imp (Class _class, SEL sel);
543
544 /* Redefine on NeXTSTEP so as not to conflict with system function */
545 #ifdef __NeXT__
546 #define object_copy     gnu_object_copy
547 #define object_dispose  gnu_object_dispose
548 #endif
549
550 id object_copy(id object);
551
552 id object_dispose(id object);
553
554 static inline Class
555 object_get_class(id object)
556 {
557   return ((object!=nil)
558           ? (CLS_ISCLASS(object->class_pointer)
559              ? object->class_pointer
560              : (CLS_ISMETA(object->class_pointer)
561                 ? (Class)object
562                 : Nil))
563           : Nil);
564 }
565
566 static inline const char *
567 object_get_class_name(id object)
568 {
569   return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
570                          ?object->class_pointer->name
571                          :((Class)object)->name)
572                        :"Nil");
573 }
574
575 static inline MetaClass
576 object_get_meta_class(id object)
577 {
578   return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
579                          ?object->class_pointer->class_pointer
580                          :(CLS_ISMETA(object->class_pointer)
581                            ?object->class_pointer
582                            :Nil))
583                        :Nil);
584 }
585
586 static inline Class
587 object_get_super_class
588 (id object)
589 {
590   return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
591                          ?object->class_pointer->super_class
592                          :(CLS_ISMETA(object->class_pointer)
593                            ?((Class)object)->super_class
594                            :Nil))
595                        :Nil);
596 }
597
598 static inline BOOL
599 object_is_class (id object)
600 {
601   return ((object != nil)  &&  CLS_ISMETA (object->class_pointer));
602 }
603  
604 static inline BOOL
605 object_is_instance (id object)
606 {
607   return ((object != nil)  &&  CLS_ISCLASS (object->class_pointer));
608 }
609
610 static inline BOOL
611 object_is_meta_class (id object)
612 {
613   return ((object != nil)
614           &&  !object_is_instance (object)  
615           &&  !object_is_class (object));
616 }
617
618 struct sarray* 
619 objc_get_uninstalled_dtable(void);
620
621 #ifdef __cplusplus
622 }
623 #endif /* __cplusplus */
624
625 #endif /* not __objc_api_INCLUDE_GNU */
626
627
628