Ticket #34232: objc-api.h

File objc-api.h, 20.2 KB (added by jmstephensjr@…, 12 years ago)

from /opt/local/include/objc/

Line 
1/* GNU Objective-C Runtime API.
2   Copyright (C) 1993, 1995, 1996, 1997 Free Software Foundation, Inc.
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU CC is distributed in the hope that it will be useful, but WITHOUT
12ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING.  If not, write to
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, 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#ifdef __cplusplus
31extern "C" {
32#endif
33
34#include "objc/objc.h"
35#include "objc/hash.h"
36#include "objc/thr.h"
37#include <stdio.h>
38#include <stdarg.h>
39
40#include "objc/externvar.h"
41
42/* For functions which return Method_t */
43#define METHOD_NULL     (Method_t)0
44                                                /* Boolean typedefs */
45/*
46** Method descriptor returned by introspective Object methods.
47** This is really just the first part of the more complete objc_method
48** structure defined below and used internally by the runtime.
49*/
50struct objc_method_description
51{
52    SEL name;                   /* this is a selector, not a string */
53    char *types;                /* type encoding */
54};
55
56/* Filer types used to describe Ivars and Methods.  */
57#define _C_ID       '@'
58#define _C_CLASS    '#'
59#define _C_SEL      ':'
60#define _C_CHR      'c'
61#define _C_UCHR     'C'
62#define _C_SHT      's'
63#define _C_USHT     'S'
64#define _C_INT      'i'
65#define _C_UINT     'I'
66#define _C_LNG      'l'
67#define _C_ULNG     'L'
68#define _C_LNG_LNG  'q'
69#define _C_ULNG_LNG 'Q'
70#define _C_FLT      'f'
71#define _C_DBL      'd'
72#define _C_LNG_DBL  'X'
73#define _C_BFLD     'b'
74#define _C_VOID     'v'
75#define _C_UNDEF    '?'
76#define _C_PTR      '^'
77#define _C_CHARPTR  '*'
78#define _C_ATOM     '%'
79#define _C_ARY_B    '['
80#define _C_ARY_E    ']'
81#define _C_UNION_B  '('
82#define _C_UNION_E  ')'
83#define _C_STRUCT_B '{'
84#define _C_STRUCT_E '}'
85
86
87/*
88** Error handling
89**
90** Call objc_error() or objc_verror() to record an error; this error
91** routine will generally exit the program but not necessarily if the
92** user has installed his own error handler.
93**
94** Call objc_set_error_handler to assign your own function for
95** handling errors.  The function should return YES if it is ok
96** to continue execution, or return NO or just abort if the
97** program should be stopped.  The default error handler is just to
98** print a message on stderr.
99**
100** The error handler function should be of type objc_error_handler
101** The first parameter is an object instance of relevance.
102** The second parameter is an error code.
103** The third parameter is a format string in the printf style.
104** The fourth parameter is a variable list of arguments.
105*/
106extern void objc_error (id object, int code, const char* fmt, ...);
107extern void objc_verror (id object, int code, const char* fmt, va_list ap);
108typedef BOOL (*objc_error_handler) (id, int code, const char *fmt, va_list ap);
109objc_error_handler objc_set_error_handler (objc_error_handler func);
110
111/*
112** Error codes
113** These are used by the runtime library, and your
114** error handling may use them to determine if the error is
115** hard or soft thus whether execution can continue or abort.
116*/
117#define OBJC_ERR_UNKNOWN 0             /* Generic error */
118
119#define OBJC_ERR_OBJC_VERSION 1        /* Incorrect runtime version */
120#define OBJC_ERR_GCC_VERSION 2         /* Incorrect compiler version */
121#define OBJC_ERR_MODULE_SIZE 3         /* Bad module size */
122#define OBJC_ERR_PROTOCOL_VERSION 4    /* Incorrect protocol version */
123
124#define OBJC_ERR_MEMORY 10             /* Out of memory */
125
126#define OBJC_ERR_RECURSE_ROOT 20       /* Attempt to archive the root
127                                          object more than once. */
128#define OBJC_ERR_BAD_DATA 21           /* Didn't read expected data */
129#define OBJC_ERR_BAD_KEY 22            /* Bad key for object */
130#define OBJC_ERR_BAD_CLASS 23          /* Unknown class */
131#define OBJC_ERR_BAD_TYPE 24           /* Bad type specification */
132#define OBJC_ERR_NO_READ 25            /* Cannot read stream */
133#define OBJC_ERR_NO_WRITE 26           /* Cannot write stream */
134#define OBJC_ERR_STREAM_VERSION 27     /* Incorrect stream version */
135#define OBJC_ERR_BAD_OPCODE 28         /* Bad opcode */
136
137#define OBJC_ERR_UNIMPLEMENTED 30      /* Method is not implemented */
138
139#define OBJC_ERR_BAD_STATE 40          /* Bad thread state */
140
141
142#ifndef __cplusplus
143/* For every class which happens to have statically allocated instances in
144   this module, one OBJC_STATIC_INSTANCES is allocated by the compiler.
145   INSTANCES is NULL terminated and points to all statically allocated
146   instances of this class.  */
147struct objc_static_instances
148{
149  char *class_name;
150  id instances[0];
151};
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*/
162typedef 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*/
189typedef 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*/
209typedef struct objc_ivar* Ivar_t;
210typedef struct objc_ivar_list {
211  int   ivar_count;                             /* Number of structures (Ivar)
212                                                  contained in the list.  One
213                                                  structure per instance
214                                                  variable defined in the
215                                                  class. */
216  struct objc_ivar {
217    const char* ivar_name;                      /* Name of the instance
218                                                  variable as entered in the
219                                                  class definition. */
220    const char* ivar_type;                      /* Description of the Ivar's
221                                                  type.  Useful for
222                                                  debuggers. */
223    int        ivar_offset;                    /* Byte offset from the base
224                                                  address of the instance
225                                                  structure to the variable. */
226
227  } 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*/
240struct 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};
253
254typedef struct objc_method Method;
255
256typedef Method* Method_t;
257typedef struct objc_method_list {
258  struct objc_method_list*  method_next;     /* This variable is used to link
259                                                a method list to another.  It
260                                                is a singly linked list. */
261  int            method_count;               /* Number of methods defined in
262                                                this structure. */
263  struct objc_method method_list[1];         /* Variable length
264                                                structure. */
265} MethodList, *MethodList_t;
266
267struct objc_protocol_list {
268  struct objc_protocol_list *next;
269  int count;
270  Protocol *list[1];
271};
272
273/*
274** This is used to assure consistent access to the info field of
275** classes
276*/
277#ifndef HOST_BITS_PER_LONG
278#define HOST_BITS_PER_LONG  (sizeof (long) * 8)
279#endif
280
281#define __CLS_INFO(cls) ((cls)->info)
282#define __CLS_ISINFO(cls, mask) ((__CLS_INFO (cls) & mask) == mask)
283#define __CLS_SETINFO(cls, mask) (__CLS_INFO (cls) |= mask)
284
285/* The structure is of type MetaClass */
286#define _CLS_META 0x2L
287#define CLS_ISMETA(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_META))
288
289
290/* The structure is of type Class */
291#define _CLS_CLASS 0x1L
292#define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS))
293
294/*
295** The class is initialized within the runtime.  This means that
296** it has had correct super and sublinks assigned
297*/
298#define _CLS_RESOLV 0x8L
299#define CLS_ISRESOLV(cls) __CLS_ISINFO(cls, _CLS_RESOLV)
300#define CLS_SETRESOLV(cls) __CLS_SETINFO(cls, _CLS_RESOLV)
301
302/*
303** The class has been send a +initialize message or a such is not
304** defined for this class
305*/
306#define _CLS_INITIALIZED 0x04L
307#define CLS_ISINITIALIZED(cls) __CLS_ISINFO(cls, _CLS_INITIALIZED)
308#define CLS_SETINITIALIZED(cls) __CLS_SETINFO(cls, _CLS_INITIALIZED)
309
310/*
311** The class number of this class.  This must be the same for both the
312** class and its meta class object
313*/
314#define CLS_GETNUMBER(cls) (__CLS_INFO(cls) >> (HOST_BITS_PER_LONG/2))
315#define CLS_SETNUMBER(cls, num) \
316  ({ (cls)->info <<= (HOST_BITS_PER_LONG/2); \
317     (cls)->info >>= (HOST_BITS_PER_LONG/2); \
318     __CLS_SETINFO(cls, (((unsigned long)num) << (HOST_BITS_PER_LONG/2))); })
319
320/*
321** The compiler generates one of these structures for each category.  A class
322** may have many categories and contain both instance and factory methods. 
323*/
324typedef struct objc_category {
325  const char*   category_name;                /* Name of the category.  Name
326                                                contained in the () of the
327                                                category definition. */
328  const char*   class_name;                   /* Name of the class to which
329                                                the category belongs. */
330  MethodList_t  instance_methods;             /* Linked list of instance
331                                                methods defined in the
332                                                category. NULL indicates no
333                                                instance methods defined. */
334  MethodList_t  class_methods;                /* Linked list of factory
335                                                methods defined in the
336                                                category.  NULL indicates no
337                                                class methods defined. */
338  struct objc_protocol_list *protocols;       /* List of Protocols
339                                                 conformed to */
340} Category, *Category_t;
341
342/*
343** Structure used when a message is send to a class's super class.  The
344** compiler generates one of these structures and passes it to
345** objc_msg_super.
346*/
347typedef struct objc_super {
348  id      self;                           /* Id of the object sending
349                                                the message. */
350  Class class_type;                              /* Object's super class. */
351} Super, *Super_t;
352
353IMP objc_msg_lookup_super(Super_t super, SEL sel);
354
355retval_t objc_msg_sendv(id, SEL, arglist_t);
356
357
358/*
359** This is a hook which is called by objc_lookup_class and
360** objc_get_class if the runtime is not able to find the class.
361** This may e.g. try to load in the class using dynamic loading.
362** The function is guaranteed to be passed a non-NULL name string.
363*/
364externobjcvar Class (*_objc_lookup_class) (const char *name);
365
366/*
367** This is a hook which is called by __objc_exec_class every time a class
368** or a category is loaded into the runtime.  This may e.g. help a
369** dynamic loader determine the classes that have been loaded when
370** an object file is dynamically linked in.
371*/
372externobjcvar void (*_objc_load_callback) (Class class_type, Category* category);
373
374/*
375** Hook functions for allocating, copying and disposing of instances
376*/
377externobjcvar id (*_objc_object_alloc) (Class class_type);
378externobjcvar id (*_objc_object_copy) (id object);
379externobjcvar id (*_objc_object_dispose) (id object);
380
381/*
382** Standard functions for memory allocation and disposal.
383** Users should use these functions in their ObjC programs so
384** that they work properly with garbage collectors as well as
385** can take advantage of the exception/error handling available.
386*/
387void *objc_malloc (size_t size);
388
389void *objc_atomic_malloc (size_t size);
390
391void *objc_valloc (size_t size);
392
393void *objc_realloc (void *mem, size_t size);
394
395void *objc_calloc (size_t nelem, size_t size);
396
397void objc_free (void *mem);
398
399/*
400** Hook functions for memory allocation and disposal.
401** This makes it easy to substitute garbage collection systems
402** such as Boehm's GC by assigning these function pointers
403** to the GC's allocation routines.  By default these point
404** to the ANSI standard malloc, realloc, free, etc.
405**
406** Users should call the normal objc routines above for
407** memory allocation and disposal within their programs.
408*/
409externobjcvar void *(*_objc_malloc) (size_t);
410externobjcvar void *(*_objc_atomic_malloc) (size_t);
411externobjcvar void *(*_objc_valloc) (size_t);
412externobjcvar void *(*_objc_realloc) (void *, size_t);
413externobjcvar void *(*_objc_calloc) (size_t, size_t);
414externobjcvar void (*_objc_free) (void *);
415
416Method_t class_get_class_method (MetaClass class_type, SEL aSel);
417
418Method_t class_get_instance_method (Class class_type, SEL aSel);
419
420Class class_pose_as (Class impostor, Class superclass);
421
422Class objc_get_class (const char *name);
423
424Class objc_lookup_class (const char *name);
425
426Class objc_next_class (void **enum_state);
427
428const char *sel_get_name (SEL selector);
429
430const char *sel_get_type (SEL selector);
431
432SEL sel_get_uid (const char *name);
433
434SEL sel_get_any_uid (const char *name);
435
436SEL sel_get_any_typed_uid (const char *name);
437
438SEL sel_get_typed_uid (const char *name, const char*);
439
440SEL sel_register_name (const char *name);
441
442SEL sel_register_typed_name (const char *name, const char*type);
443
444
445BOOL sel_is_mapped (SEL aSel);
446
447extern id class_create_instance (Class class_type);
448
449static inline const char *
450class_get_class_name (Class class_type)
451{
452  return CLS_ISCLASS(class_type)?class_type->name:((class_type==Nil)?"Nil":0);
453}
454
455static inline long
456class_get_instance_size (Class class_type)
457{
458  return CLS_ISCLASS (class_type) ? class_type->instance_size : 0;
459}
460
461static inline MetaClass
462class_get_meta_class (Class class_type)
463{
464  return CLS_ISCLASS (class_type) ? class_type->class_pointer : Nil;
465}
466
467static inline Class
468class_get_super_class (Class class_type)
469{
470  return CLS_ISCLASS (class_type) ? class_type->super_class : Nil;
471}
472
473static inline int
474class_get_version (Class class_type)
475{
476  return CLS_ISCLASS (class_type) ? class_type->version : -1;
477}
478
479static inline BOOL
480class_is_class (Class class_type)
481{
482  return CLS_ISCLASS (class_type);
483}
484
485static inline BOOL
486class_is_meta_class (Class class_type)
487{
488  return CLS_ISMETA (class_type);
489}
490
491
492static inline void
493class_set_version (Class class_type, long version)
494{
495  if (CLS_ISCLASS (class_type))
496    class_type->version = version;
497}
498
499static inline void *
500class_get_gc_object_type (Class class_type)
501{
502  return CLS_ISCLASS(class_type) ? class_type->gc_object_type : NULL;
503}
504
505/* Mark the instance variable as innaccessible to the garbage collector */
506extern void class_ivar_set_gcinvisible (Class class_type,
507                                        const char* ivarname,
508                                        BOOL gcInvisible);
509
510static inline IMP
511method_get_imp (Method_t method)
512{
513  return (method != METHOD_NULL) ? method->method_imp : (IMP) 0;
514}
515
516IMP get_imp (Class class_type, SEL sel);
517
518/* Redefine on NeXTSTEP so as not to conflict with system function */
519#ifdef __NeXT__
520#define object_copy     gnu_object_copy
521#define object_dispose  gnu_object_dispose
522#endif
523
524id object_copy (id object);
525
526id object_dispose (id object);
527
528static inline Class
529object_get_class (id object)
530{
531  return ((object != nil)
532          ? (CLS_ISCLASS (object->class_pointer)
533             ? object->class_pointer
534             : (CLS_ISMETA (object->class_pointer)
535                ? (Class) object
536                : Nil))
537          : Nil);
538}
539
540static inline const char *
541object_get_class_name (id object)
542{
543  return ((object!=nil) ? (CLS_ISCLASS (object->class_pointer)
544                         ? object->class_pointer->name
545                         : ((Class) object)->name)
546                       :"Nil");
547}
548
549static inline MetaClass
550object_get_meta_class(id object)
551{
552  return ((object!=nil) ? (CLS_ISCLASS (object->class_pointer)
553                           ? object->class_pointer->class_pointer
554                           : (CLS_ISMETA(object->class_pointer)
555                              ? object->class_pointer
556                              :Nil))
557          : Nil);
558}
559
560static inline Class
561object_get_super_class (id object)
562{
563  return ((object!=nil) ? (CLS_ISCLASS (object->class_pointer)
564                           ? object->class_pointer->super_class
565                           : (CLS_ISMETA (object->class_pointer)
566                           ?((Class) object)->super_class
567                              : Nil))
568          : Nil);
569}
570
571static inline BOOL
572object_is_class (id object)
573{
574  return CLS_ISCLASS ((Class) object);
575}
576
577static inline BOOL
578object_is_instance(id object)
579{
580  return (object != nil) && CLS_ISCLASS (object->class_pointer);
581}
582
583static inline BOOL
584object_is_meta_class (id object)
585{
586  return CLS_ISMETA ((Class) object);
587}
588
589struct sarray* objc_get_uninstalled_dtable (void);
590
591#ifdef __cplusplus
592}
593#endif
594
595#endif /* not __objc_api_INCLUDE_GNU */
596
597
598