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.
5 This file is part of GCC.
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
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.
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.
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/>. */
27 #ifndef __objc_api_INCLUDE_GNU
28 #define __objc_api_INCLUDE_GNU
31 #ifndef GNU_LIBOBJC_COMPILING_LIBOBJC_ITSELF
32 # include "deprecated/hash.h"
35 #include "objc-decls.h"
41 #endif /* __cplusplus */
43 #include "deprecated/METHOD_NULL.h"
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
50 SEL name
; /* this is a selector, not a string */
51 char *types
; /* type encoding */
54 /* Filer types used to describe Ivars and Methods. */
66 #define _C_LNG_LNG 'q'
67 #define _C_ULNG_LNG 'Q'
70 #define _C_LNG_DBL 'D'
76 #define _C_CHARPTR '*'
79 #define _C_UNION_B '('
80 #define _C_UNION_E ')'
81 #define _C_STRUCT_B '{'
82 #define _C_STRUCT_E '}'
84 #define _C_COMPLEX 'j'
86 /* The following one is never generated by the compiler. You can
87 treat it as equivalent to "*".
92 #include "deprecated/objc_error.h"
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
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
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
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. */
133 ** The compiler generates one of these structures for each module that
134 ** composes the executable (eg main.m).
136 ** This data structure is the root of the definition tree for the module.
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.
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
147 const char* name
; /* Name of the file used to compile the
148 module - not set by modern compilers for
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
158 ** The compiler generates one of these structures for a class that has
159 ** instance variables defined in its specification.
161 typedef struct objc_ivar
{
162 const char* ivar_name
; /* Name of the instance
163 variable as entered in the
165 const char* ivar_type
; /* Description of the Ivar's
168 int ivar_offset
; /* Byte offset from the base
169 address of the instance
170 structure to the variable. */
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
179 struct objc_ivar ivar_list
[1]; /* Variable length
181 } IvarList
, *IvarList_t
;
185 ** The compiler generates one (or more) of these structures for a class that
186 ** has methods defined in its specification.
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.
192 typedef struct objc_method
{
193 SEL method_name
; /* This variable is the method's
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
202 IMP method_imp
; /* Address of the method in the
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
212 Method method_list
[1]; /* Variable length
214 } MethodList
, *MethodList_t
;
216 struct objc_protocol_list
{
217 struct objc_protocol_list
*next
;
223 ** This is used to assure consistent access to the info field of
226 #ifndef HOST_BITS_PER_LONG
227 #define HOST_BITS_PER_LONG (sizeof(long)*8)
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)
234 /* The structure is of type MetaClass */
235 #define _CLS_META 0x2L
236 #define CLS_ISMETA(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_META))
239 /* The structure is of type Class */
240 #define _CLS_CLASS 0x1L
241 #define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS))
244 ** The class is initialized within the runtime. This means that
245 ** it has had correct super and sublinks assigned
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)
252 ** The class has been send a +initialize message or a such is not
253 ** defined for this class
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)
260 ** The class number of this class. This must be the same for both the
261 ** class and its meta class object
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))); })
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.
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
289 } Category
, *Category_t
;
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. */
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.
304 objc_EXPORT
Class (*_objc_lookup_class
)(const char *name
);
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.
312 objc_EXPORT
void (*_objc_load_callback
)(Class _class
, Category
* category
);
315 ** Hook functions for allocating, copying and disposing of instances
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
);
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.
327 objc_malloc(size_t size
);
329 /* FIXME: Shouldn't the following be called objc_malloc_atomic ? The
330 GC function is GC_malloc_atomic() which makes sense.
333 objc_atomic_malloc(size_t size
);
336 objc_realloc(void *mem
, size_t size
);
339 objc_calloc(size_t nelem
, size_t size
);
342 objc_free(void *mem
);
344 #include "deprecated/objc_valloc.h"
345 #include "deprecated/objc_malloc.h"
347 #include "deprecated/objc_unexpected_exception.h"
349 objc_EXPORT Method_t
class_get_class_method(MetaClass _class
, SEL aSel
);
351 objc_EXPORT Method_t
class_get_instance_method(Class _class
, SEL aSel
);
353 objc_EXPORT Class
class_pose_as(Class impostor
, Class superclass
);
355 objc_EXPORT Class
objc_get_class(const char *name
);
357 objc_EXPORT Class
objc_lookup_class(const char *name
);
359 objc_EXPORT Class
objc_next_class(void **enum_state
);
361 objc_EXPORT
const char *sel_get_name(SEL selector
);
363 objc_EXPORT
const char *sel_get_type(SEL selector
);
365 objc_EXPORT SEL
sel_get_uid(const char *name
);
367 objc_EXPORT SEL
sel_get_any_uid(const char *name
);
369 objc_EXPORT SEL
sel_get_any_typed_uid(const char *name
);
371 objc_EXPORT SEL
sel_get_typed_uid(const char *name
, const char*);
373 objc_EXPORT SEL
sel_register_name(const char *name
);
375 objc_EXPORT SEL
sel_register_typed_name(const char *name
, const char*type
);
378 objc_EXPORT BOOL
sel_is_mapped (SEL aSel
);
380 extern id
class_create_instance(Class _class
);
382 static inline const char *
383 class_get_class_name(Class _class
)
385 return CLS_ISCLASS(_class
)?_class
->name
:((_class
==Nil
)?"Nil":0);
389 class_get_instance_size(Class _class
)
391 return CLS_ISCLASS(_class
)?_class
->instance_size
:0;
394 static inline MetaClass
395 class_get_meta_class(Class _class
)
397 return CLS_ISCLASS(_class
)?_class
->class_pointer
:Nil
;
401 class_get_super_class(Class _class
)
403 return CLS_ISCLASS(_class
)?_class
->super_class
:Nil
;
407 class_get_version(Class _class
)
409 return CLS_ISCLASS(_class
)?_class
->version
:-1;
413 class_is_class(Class _class
)
415 return CLS_ISCLASS(_class
);
419 class_is_meta_class(Class _class
)
421 return CLS_ISMETA(_class
);
426 class_set_version(Class _class
, long version
)
428 if (CLS_ISCLASS(_class
))
429 _class
->version
= version
;
433 class_get_gc_object_type (Class _class
)
435 return CLS_ISCLASS(_class
) ? _class
->gc_object_type
: NULL
;
438 /* Mark the instance variable as innaccessible to the garbage collector */
439 extern void class_ivar_set_gcinvisible (Class _class
,
440 const char* ivarname
,
443 objc_EXPORT IMP
method_get_imp(Method_t method
);
445 objc_EXPORT IMP
get_imp (Class _class
, SEL sel
);
447 objc_EXPORT id
object_copy(id object
);
449 objc_EXPORT id
object_dispose(id object
);
452 object_get_class(id object
)
454 return ((object
!=nil
)
455 ? (CLS_ISCLASS(object
->class_pointer
)
456 ? object
->class_pointer
457 : (CLS_ISMETA(object
->class_pointer
)
463 static inline const char *
464 object_get_class_name(id object
)
466 return ((object
!=nil
)?(CLS_ISCLASS(object
->class_pointer
)
467 ?object
->class_pointer
->name
468 :((Class
)object
)->name
)
472 static inline MetaClass
473 object_get_meta_class(id object
)
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
484 object_get_super_class
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
496 object_is_class (id object
)
498 return ((object
!= nil
) && CLS_ISMETA (object
->class_pointer
));
502 object_is_instance (id object
)
504 return ((object
!= nil
) && CLS_ISCLASS (object
->class_pointer
));
508 object_is_meta_class (id object
)
510 return ((object
!= nil
)
511 && !object_is_instance (object
)
512 && !object_is_class (object
));
515 objc_EXPORT
struct sarray
*
516 objc_get_uninstalled_dtable(void);
520 #endif /* __cplusplus */
522 #endif /* not __objc_api_INCLUDE_GNU */