]> git.ipfire.org Git - thirdparty/gcc.git/blob - libobjc/objc/objc-api.h
5f9043086b70efdd12389770eb687f3ec780a7e8
[thirdparty/gcc.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 #ifndef GNU_LIBOBJC_COMPILING_LIBOBJC_ITSELF
32 # include "deprecated/hash.h"
33 #endif
34 #include "thr.h"
35 #include "objc-decls.h"
36 #include <stdio.h>
37 #include <stdarg.h>
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif /* __cplusplus */
42
43 #include "deprecated/METHOD_NULL.h"
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 struct objc_method_description
49 {
50 SEL name; /* this is a selector, not a string */
51 char *types; /* type encoding */
52 };
53
54 /* Filer types used to describe Ivars and Methods. */
55 #define _C_ID '@'
56 #define _C_CLASS '#'
57 #define _C_SEL ':'
58 #define _C_CHR 'c'
59 #define _C_UCHR 'C'
60 #define _C_SHT 's'
61 #define _C_USHT 'S'
62 #define _C_INT 'i'
63 #define _C_UINT 'I'
64 #define _C_LNG 'l'
65 #define _C_ULNG 'L'
66 #define _C_LNG_LNG 'q'
67 #define _C_ULNG_LNG 'Q'
68 #define _C_FLT 'f'
69 #define _C_DBL 'd'
70 #define _C_LNG_DBL 'D'
71 #define _C_BFLD 'b'
72 #define _C_BOOL 'B'
73 #define _C_VOID 'v'
74 #define _C_UNDEF '?'
75 #define _C_PTR '^'
76 #define _C_CHARPTR '*'
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 '}'
83 #define _C_VECTOR '!'
84 #define _C_COMPLEX 'j'
85
86 /* The following one is never generated by the compiler. You can
87 treat it as equivalent to "*".
88 */
89 #define _C_ATOM '%'
90
91
92 #include "deprecated/objc_error.h"
93
94 /* For every class which happens to have statically allocated instances in
95 this module, one OBJC_STATIC_INSTANCES is allocated by the compiler.
96 INSTANCES is NULL terminated and points to all statically allocated
97 instances of this class. */
98 struct objc_static_instances
99 {
100 char *class_name;
101 #ifdef __cplusplus
102 id instances[1];
103 #else
104 id instances[0];
105 #endif
106 };
107
108 /* Whereas a Module (defined further down) is the root (typically) of a file,
109 a Symtab is the root of the class and category definitions within the
110 module.
111
112 A Symtab contains a variable length array of pointers to classes and
113 categories defined in the module. */
114 typedef struct objc_symtab {
115 unsigned long sel_ref_cnt; /* Unknown. */
116 SEL refs; /* Unknown. */
117 unsigned short cls_def_cnt; /* Number of classes compiled
118 (defined) in the module. */
119 unsigned short cat_def_cnt; /* Number of categories
120 compiled (defined) in the
121 module. */
122
123 void *defs[1]; /* Variable array of pointers.
124 cls_def_cnt of type Class
125 followed by cat_def_cnt of
126 type Category_t, followed
127 by a NULL terminated array
128 of objc_static_instances. */
129 } Symtab, *Symtab_t;
130
131
132 /*
133 ** The compiler generates one of these structures for each module that
134 ** composes the executable (eg main.m).
135 **
136 ** This data structure is the root of the definition tree for the module.
137 **
138 ** A collect program runs between ld stages and creates a ObjC ctor array.
139 ** That array holds a pointer to each module structure of the executable.
140 */
141 typedef struct objc_module {
142 unsigned long version; /* Version of the Module data structure. */
143 unsigned long size; /* sizeof(Module) according to the compiler -
144 only used to sanity check that it matches
145 sizeof(Module) according to the
146 runtime. */
147 const char* name; /* Name of the file used to compile the
148 module - not set by modern compilers for
149 security reasons. */
150 Symtab_t symtab; /* Pointer to the Symtab of the module. The
151 Symtab holds an array of pointers to the
152 classes and categories defined in the
153 module. */
154 } Module, *Module_t;
155
156
157 /*
158 ** The compiler generates one of these structures for a class that has
159 ** instance variables defined in its specification.
160 */
161 typedef struct objc_ivar {
162 const char* ivar_name; /* Name of the instance
163 variable as entered in the
164 class definition. */
165 const char* ivar_type; /* Description of the Ivar's
166 type. Useful for
167 debuggers. */
168 int ivar_offset; /* Byte offset from the base
169 address of the instance
170 structure to the variable. */
171 } *Ivar_t;
172
173 typedef struct objc_ivar_list {
174 int ivar_count; /* Number of structures (Ivar)
175 contained in the list. One
176 structure per instance
177 variable defined in the
178 class. */
179 struct objc_ivar ivar_list[1]; /* Variable length
180 structure. */
181 } IvarList, *IvarList_t;
182
183
184 /*
185 ** The compiler generates one (or more) of these structures for a class that
186 ** has methods defined in its specification.
187 **
188 ** The implementation of a class can be broken into separate pieces in a file
189 ** and categories can break them across modules. To handle this problem is a
190 ** singly linked list of methods.
191 */
192 typedef struct objc_method {
193 SEL method_name; /* This variable is the method's
194 name. It is a char*.
195 The unique integer passed to
196 objc_msg_send is a char* too.
197 It is compared against
198 method_name using strcmp. */
199 const char* method_types; /* Description of the method's
200 parameter list. Useful for
201 debuggers. */
202 IMP method_imp; /* Address of the method in the
203 executable. */
204 } Method, *Method_t;
205
206 typedef struct objc_method_list {
207 struct objc_method_list* method_next; /* This variable is used to link
208 a method list to another. It
209 is a singly linked list. */
210 int method_count; /* Number of methods defined in
211 this structure. */
212 Method method_list[1]; /* Variable length
213 structure. */
214 } MethodList, *MethodList_t;
215
216 struct objc_protocol_list {
217 struct objc_protocol_list *next;
218 size_t count;
219 Protocol *list[1];
220 };
221
222 /*
223 ** This is used to assure consistent access to the info field of
224 ** classes
225 */
226 #ifndef HOST_BITS_PER_LONG
227 #define HOST_BITS_PER_LONG (sizeof(long)*8)
228 #endif
229
230 #define __CLS_INFO(cls) ((cls)->info)
231 #define __CLS_ISINFO(cls, mask) ((__CLS_INFO(cls)&mask)==mask)
232 #define __CLS_SETINFO(cls, mask) (__CLS_INFO(cls) |= mask)
233
234 /* The structure is of type MetaClass */
235 #define _CLS_META 0x2L
236 #define CLS_ISMETA(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_META))
237
238
239 /* The structure is of type Class */
240 #define _CLS_CLASS 0x1L
241 #define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS))
242
243 /*
244 ** The class is initialized within the runtime. This means that
245 ** it has had correct super and sublinks assigned
246 */
247 #define _CLS_RESOLV 0x8L
248 #define CLS_ISRESOLV(cls) __CLS_ISINFO(cls, _CLS_RESOLV)
249 #define CLS_SETRESOLV(cls) __CLS_SETINFO(cls, _CLS_RESOLV)
250
251 /*
252 ** The class has been send a +initialize message or a such is not
253 ** defined for this class
254 */
255 #define _CLS_INITIALIZED 0x04L
256 #define CLS_ISINITIALIZED(cls) __CLS_ISINFO(cls, _CLS_INITIALIZED)
257 #define CLS_SETINITIALIZED(cls) __CLS_SETINFO(cls, _CLS_INITIALIZED)
258
259 /*
260 ** The class number of this class. This must be the same for both the
261 ** class and its meta class object
262 */
263 #define CLS_GETNUMBER(cls) (__CLS_INFO(cls) >> (HOST_BITS_PER_LONG/2))
264 #define CLS_SETNUMBER(cls, num) \
265 ({ (cls)->info <<= (HOST_BITS_PER_LONG/2); \
266 (cls)->info >>= (HOST_BITS_PER_LONG/2); \
267 __CLS_SETINFO(cls, (((unsigned long)num) << (HOST_BITS_PER_LONG/2))); })
268
269 /*
270 ** The compiler generates one of these structures for each category. A class
271 ** may have many categories and contain both instance and factory methods.
272 */
273 typedef struct objc_category {
274 const char* category_name; /* Name of the category. Name
275 contained in the () of the
276 category definition. */
277 const char* class_name; /* Name of the class to which
278 the category belongs. */
279 MethodList_t instance_methods; /* Linked list of instance
280 methods defined in the
281 category. NULL indicates no
282 instance methods defined. */
283 MethodList_t class_methods; /* Linked list of factory
284 methods defined in the
285 category. NULL indicates no
286 class methods defined. */
287 struct objc_protocol_list *protocols; /* List of Protocols
288 conformed to */
289 } Category, *Category_t;
290
291 /* We include message.h for compatibility with the old objc-api.h
292 which included the declarations currently in message.h. The
293 Apple/NeXT runtime does not do this and only include message.h in
294 objc-runtime.h. It does not matter that much since most of the
295 definitions in message.h are runtime-specific. */
296 #include "message.h"
297
298 /*
299 ** This is a hook which is called by objc_lookup_class and
300 ** objc_get_class if the runtime is not able to find the class.
301 ** This may e.g. try to load in the class using dynamic loading.
302 ** The function is guaranteed to be passed a non-NULL name string.
303 */
304 objc_EXPORT Class (*_objc_lookup_class)(const char *name);
305
306 /*
307 ** This is a hook which is called by __objc_exec_class every time a class
308 ** or a category is loaded into the runtime. This may e.g. help a
309 ** dynamic loader determine the classes that have been loaded when
310 ** an object file is dynamically linked in.
311 */
312 objc_EXPORT void (*_objc_load_callback)(Class _class, Category* category);
313
314 /*
315 ** Hook functions for allocating, copying and disposing of instances
316 */
317 objc_EXPORT id (*_objc_object_alloc)(Class _class);
318 objc_EXPORT id (*_objc_object_copy)(id object);
319 objc_EXPORT id (*_objc_object_dispose)(id object);
320
321 /*
322 Standard functions for memory allocation and disposal. Users should
323 use these functions in their ObjC programs so that they work so that
324 they work properly with garbage collectors.
325 */
326 objc_EXPORT void *
327 objc_malloc(size_t size);
328
329 /* FIXME: Shouldn't the following be called objc_malloc_atomic ? The
330 GC function is GC_malloc_atomic() which makes sense.
331 */
332 objc_EXPORT void *
333 objc_atomic_malloc(size_t size);
334
335 objc_EXPORT void *
336 objc_realloc(void *mem, size_t size);
337
338 objc_EXPORT void *
339 objc_calloc(size_t nelem, size_t size);
340
341 objc_EXPORT void
342 objc_free(void *mem);
343
344 #include "deprecated/objc_valloc.h"
345 #include "deprecated/objc_malloc.h"
346
347 #include "deprecated/objc_unexpected_exception.h"
348
349 objc_EXPORT Method_t class_get_class_method(MetaClass _class, SEL aSel);
350
351 objc_EXPORT Method_t class_get_instance_method(Class _class, SEL aSel);
352
353 objc_EXPORT Class class_pose_as(Class impostor, Class superclass);
354
355 objc_EXPORT Class objc_get_class(const char *name);
356
357 objc_EXPORT Class objc_lookup_class(const char *name);
358
359 objc_EXPORT Class objc_next_class(void **enum_state);
360
361 objc_EXPORT const char *sel_get_name(SEL selector);
362
363 objc_EXPORT const char *sel_get_type(SEL selector);
364
365 objc_EXPORT SEL sel_get_uid(const char *name);
366
367 objc_EXPORT SEL sel_get_any_uid(const char *name);
368
369 objc_EXPORT SEL sel_get_any_typed_uid(const char *name);
370
371 objc_EXPORT SEL sel_get_typed_uid(const char *name, const char*);
372
373 objc_EXPORT SEL sel_register_name(const char *name);
374
375 objc_EXPORT SEL sel_register_typed_name(const char *name, const char*type);
376
377
378 objc_EXPORT BOOL sel_is_mapped (SEL aSel);
379
380 extern id class_create_instance(Class _class);
381
382 static inline const char *
383 class_get_class_name(Class _class)
384 {
385 return CLS_ISCLASS(_class)?_class->name:((_class==Nil)?"Nil":0);
386 }
387
388 static inline long
389 class_get_instance_size(Class _class)
390 {
391 return CLS_ISCLASS(_class)?_class->instance_size:0;
392 }
393
394 static inline MetaClass
395 class_get_meta_class(Class _class)
396 {
397 return CLS_ISCLASS(_class)?_class->class_pointer:Nil;
398 }
399
400 static inline Class
401 class_get_super_class(Class _class)
402 {
403 return CLS_ISCLASS(_class)?_class->super_class:Nil;
404 }
405
406 static inline int
407 class_get_version(Class _class)
408 {
409 return CLS_ISCLASS(_class)?_class->version:-1;
410 }
411
412 static inline BOOL
413 class_is_class(Class _class)
414 {
415 return CLS_ISCLASS(_class);
416 }
417
418 static inline BOOL
419 class_is_meta_class(Class _class)
420 {
421 return CLS_ISMETA(_class);
422 }
423
424
425 static inline void
426 class_set_version(Class _class, long version)
427 {
428 if (CLS_ISCLASS(_class))
429 _class->version = version;
430 }
431
432 static inline void *
433 class_get_gc_object_type (Class _class)
434 {
435 return CLS_ISCLASS(_class) ? _class->gc_object_type : NULL;
436 }
437
438 /* Mark the instance variable as innaccessible to the garbage collector */
439 extern void class_ivar_set_gcinvisible (Class _class,
440 const char* ivarname,
441 BOOL gcInvisible);
442
443 objc_EXPORT IMP method_get_imp(Method_t method);
444
445 objc_EXPORT IMP get_imp (Class _class, SEL sel);
446
447 objc_EXPORT id object_copy(id object);
448
449 objc_EXPORT id object_dispose(id object);
450
451 static inline Class
452 object_get_class(id object)
453 {
454 return ((object!=nil)
455 ? (CLS_ISCLASS(object->class_pointer)
456 ? object->class_pointer
457 : (CLS_ISMETA(object->class_pointer)
458 ? (Class)object
459 : Nil))
460 : Nil);
461 }
462
463 static inline const char *
464 object_get_class_name(id object)
465 {
466 return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
467 ?object->class_pointer->name
468 :((Class)object)->name)
469 :"Nil");
470 }
471
472 static inline MetaClass
473 object_get_meta_class(id object)
474 {
475 return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
476 ?object->class_pointer->class_pointer
477 :(CLS_ISMETA(object->class_pointer)
478 ?object->class_pointer
479 :Nil))
480 :Nil);
481 }
482
483 static inline Class
484 object_get_super_class
485 (id object)
486 {
487 return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
488 ?object->class_pointer->super_class
489 :(CLS_ISMETA(object->class_pointer)
490 ?((Class)object)->super_class
491 :Nil))
492 :Nil);
493 }
494
495 static inline BOOL
496 object_is_class (id object)
497 {
498 return ((object != nil) && CLS_ISMETA (object->class_pointer));
499 }
500
501 static inline BOOL
502 object_is_instance (id object)
503 {
504 return ((object != nil) && CLS_ISCLASS (object->class_pointer));
505 }
506
507 static inline BOOL
508 object_is_meta_class (id object)
509 {
510 return ((object != nil)
511 && !object_is_instance (object)
512 && !object_is_class (object));
513 }
514
515 objc_EXPORT struct sarray*
516 objc_get_uninstalled_dtable(void);
517
518 #ifdef __cplusplus
519 }
520 #endif /* __cplusplus */
521
522 #endif /* not __objc_api_INCLUDE_GNU */
523
524
525