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