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