OSDN Git Service

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