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 /* For functions which return Method_t */
44 #define METHOD_NULL (Method_t)0
45 /* Boolean typedefs */
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 struct objc_method_description
51 SEL name
; /* this is a selector, not a string */
52 char *types
; /* type encoding */
55 /* Filer types used to describe Ivars and Methods. */
67 #define _C_LNG_LNG 'q'
68 #define _C_ULNG_LNG 'Q'
76 #define _C_CHARPTR '*'
80 #define _C_UNION_B '('
81 #define _C_UNION_E ')'
82 #define _C_STRUCT_B '{'
83 #define _C_STRUCT_E '}'
85 #define _C_COMPLEX 'j'
87 #include "deprecated/objc_error.h"
89 /* For every class which happens to have statically allocated instances in
90 this module, one OBJC_STATIC_INSTANCES is allocated by the compiler.
91 INSTANCES is NULL terminated and points to all statically allocated
92 instances of this class. */
93 struct objc_static_instances
103 /* Whereas a Module (defined further down) is the root (typically) of a file,
104 a Symtab is the root of the class and category definitions within the
107 A Symtab contains a variable length array of pointers to classes and
108 categories defined in the module. */
109 typedef struct objc_symtab
{
110 unsigned long sel_ref_cnt
; /* Unknown. */
111 SEL refs
; /* Unknown. */
112 unsigned short cls_def_cnt
; /* Number of classes compiled
113 (defined) in the module. */
114 unsigned short cat_def_cnt
; /* Number of categories
115 compiled (defined) in the
118 void *defs
[1]; /* Variable array of pointers.
119 cls_def_cnt of type Class
120 followed by cat_def_cnt of
121 type Category_t, followed
122 by a NULL terminated array
123 of objc_static_instances. */
128 ** The compiler generates one of these structures for each module that
129 ** composes the executable (eg main.m).
131 ** This data structure is the root of the definition tree for the module.
133 ** A collect program runs between ld stages and creates a ObjC ctor array.
134 ** That array holds a pointer to each module structure of the executable.
136 typedef struct objc_module
{
137 unsigned long version
; /* Version of the Module data structure. */
138 unsigned long size
; /* sizeof(Module) according to the compiler -
139 only used to sanity check that it matches
140 sizeof(Module) according to the
142 const char* name
; /* Name of the file used to compile the
143 module - not set by modern compilers for
145 Symtab_t symtab
; /* Pointer to the Symtab of the module. The
146 Symtab holds an array of pointers to the
147 classes and categories defined in the
153 ** The compiler generates one of these structures for a class that has
154 ** instance variables defined in its specification.
156 typedef struct objc_ivar
{
157 const char* ivar_name
; /* Name of the instance
158 variable as entered in the
160 const char* ivar_type
; /* Description of the Ivar's
163 int ivar_offset
; /* Byte offset from the base
164 address of the instance
165 structure to the variable. */
168 typedef struct objc_ivar_list
{
169 int ivar_count
; /* Number of structures (Ivar)
170 contained in the list. One
171 structure per instance
172 variable defined in the
174 struct objc_ivar ivar_list
[1]; /* Variable length
176 } IvarList
, *IvarList_t
;
180 ** The compiler generates one (or more) of these structures for a class that
181 ** has methods defined in its specification.
183 ** The implementation of a class can be broken into separate pieces in a file
184 ** and categories can break them across modules. To handle this problem is a
185 ** singly linked list of methods.
187 typedef struct objc_method
{
188 SEL method_name
; /* This variable is the method's
190 The unique integer passed to
191 objc_msg_send is a char* too.
192 It is compared against
193 method_name using strcmp. */
194 const char* method_types
; /* Description of the method's
195 parameter list. Useful for
197 IMP method_imp
; /* Address of the method in the
201 typedef struct objc_method_list
{
202 struct objc_method_list
* method_next
; /* This variable is used to link
203 a method list to another. It
204 is a singly linked list. */
205 int method_count
; /* Number of methods defined in
207 Method method_list
[1]; /* Variable length
209 } MethodList
, *MethodList_t
;
211 struct objc_protocol_list
{
212 struct objc_protocol_list
*next
;
218 ** This is used to assure consistent access to the info field of
221 #ifndef HOST_BITS_PER_LONG
222 #define HOST_BITS_PER_LONG (sizeof(long)*8)
225 #define __CLS_INFO(cls) ((cls)->info)
226 #define __CLS_ISINFO(cls, mask) ((__CLS_INFO(cls)&mask)==mask)
227 #define __CLS_SETINFO(cls, mask) (__CLS_INFO(cls) |= mask)
229 /* The structure is of type MetaClass */
230 #define _CLS_META 0x2L
231 #define CLS_ISMETA(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_META))
234 /* The structure is of type Class */
235 #define _CLS_CLASS 0x1L
236 #define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS))
239 ** The class is initialized within the runtime. This means that
240 ** it has had correct super and sublinks assigned
242 #define _CLS_RESOLV 0x8L
243 #define CLS_ISRESOLV(cls) __CLS_ISINFO(cls, _CLS_RESOLV)
244 #define CLS_SETRESOLV(cls) __CLS_SETINFO(cls, _CLS_RESOLV)
247 ** The class has been send a +initialize message or a such is not
248 ** defined for this class
250 #define _CLS_INITIALIZED 0x04L
251 #define CLS_ISINITIALIZED(cls) __CLS_ISINFO(cls, _CLS_INITIALIZED)
252 #define CLS_SETINITIALIZED(cls) __CLS_SETINFO(cls, _CLS_INITIALIZED)
255 ** The class number of this class. This must be the same for both the
256 ** class and its meta class object
258 #define CLS_GETNUMBER(cls) (__CLS_INFO(cls) >> (HOST_BITS_PER_LONG/2))
259 #define CLS_SETNUMBER(cls, num) \
260 ({ (cls)->info <<= (HOST_BITS_PER_LONG/2); \
261 (cls)->info >>= (HOST_BITS_PER_LONG/2); \
262 __CLS_SETINFO(cls, (((unsigned long)num) << (HOST_BITS_PER_LONG/2))); })
265 ** The compiler generates one of these structures for each category. A class
266 ** may have many categories and contain both instance and factory methods.
268 typedef struct objc_category
{
269 const char* category_name
; /* Name of the category. Name
270 contained in the () of the
271 category definition. */
272 const char* class_name
; /* Name of the class to which
273 the category belongs. */
274 MethodList_t instance_methods
; /* Linked list of instance
275 methods defined in the
276 category. NULL indicates no
277 instance methods defined. */
278 MethodList_t class_methods
; /* Linked list of factory
279 methods defined in the
280 category. NULL indicates no
281 class methods defined. */
282 struct objc_protocol_list
*protocols
; /* List of Protocols
284 } Category
, *Category_t
;
287 ** Structure used when a message is send to a class's super class. The
288 ** compiler generates one of these structures and passes it to
291 typedef struct objc_super
{
292 id self
; /* Id of the object sending
297 Class
class; /* Object's super class. */
301 IMP
objc_msg_lookup_super(Super_t super
, SEL sel
);
303 retval_t
objc_msg_sendv(id
, SEL
, arglist_t
);
308 ** This is a hook which is called by objc_lookup_class and
309 ** objc_get_class if the runtime is not able to find the class.
310 ** This may e.g. try to load in the class using dynamic loading.
311 ** The function is guaranteed to be passed a non-NULL name string.
313 objc_EXPORT
Class (*_objc_lookup_class
)(const char *name
);
316 ** This is a hook which is called by __objc_exec_class every time a class
317 ** or a category is loaded into the runtime. This may e.g. help a
318 ** dynamic loader determine the classes that have been loaded when
319 ** an object file is dynamically linked in.
321 objc_EXPORT
void (*_objc_load_callback
)(Class _class
, Category
* category
);
324 ** Hook functions for allocating, copying and disposing of instances
326 objc_EXPORT
id (*_objc_object_alloc
)(Class _class
);
327 objc_EXPORT
id (*_objc_object_copy
)(id object
);
328 objc_EXPORT
id (*_objc_object_dispose
)(id object
);
331 Standard functions for memory allocation and disposal. Users should
332 use these functions in their ObjC programs so that they work so that
333 they work properly with garbage collectors.
336 objc_malloc(size_t size
);
338 /* FIXME: Shouldn't the following be called objc_malloc_atomic ? The
339 GC function is GC_malloc_atomic() which makes sense.
342 objc_atomic_malloc(size_t size
);
345 objc_realloc(void *mem
, size_t size
);
348 objc_calloc(size_t nelem
, size_t size
);
351 objc_free(void *mem
);
353 #include "deprecated/objc_valloc.h"
354 #include "deprecated/objc_malloc.h"
357 ** Hooks for method forwarding. This makes it easy to substitute a
358 ** library, such as ffcall, that implements closures, thereby avoiding
359 ** gcc's __builtin_apply problems. __objc_msg_forward2's result will
360 ** be preferred over that of __objc_msg_forward if both are set and
363 objc_EXPORT
IMP (*__objc_msg_forward
)(SEL
);
364 objc_EXPORT
IMP (*__objc_msg_forward2
)(id
, SEL
);
366 #include "deprecated/objc_unexpected_exception.h"
368 Method_t
class_get_class_method(MetaClass _class
, SEL aSel
);
370 Method_t
class_get_instance_method(Class _class
, SEL aSel
);
372 Class
class_pose_as(Class impostor
, Class superclass
);
374 Class
objc_get_class(const char *name
);
376 Class
objc_lookup_class(const char *name
);
378 Class
objc_next_class(void **enum_state
);
380 const char *sel_get_name(SEL selector
);
382 const char *sel_get_type(SEL selector
);
384 SEL
sel_get_uid(const char *name
);
386 SEL
sel_get_any_uid(const char *name
);
388 SEL
sel_get_any_typed_uid(const char *name
);
390 SEL
sel_get_typed_uid(const char *name
, const char*);
392 SEL
sel_register_name(const char *name
);
394 SEL
sel_register_typed_name(const char *name
, const char*type
);
397 BOOL
sel_is_mapped (SEL aSel
);
399 extern id
class_create_instance(Class _class
);
401 static inline const char *
402 class_get_class_name(Class _class
)
404 return CLS_ISCLASS(_class
)?_class
->name
:((_class
==Nil
)?"Nil":0);
408 class_get_instance_size(Class _class
)
410 return CLS_ISCLASS(_class
)?_class
->instance_size
:0;
413 static inline MetaClass
414 class_get_meta_class(Class _class
)
416 return CLS_ISCLASS(_class
)?_class
->class_pointer
:Nil
;
420 class_get_super_class(Class _class
)
422 return CLS_ISCLASS(_class
)?_class
->super_class
:Nil
;
426 class_get_version(Class _class
)
428 return CLS_ISCLASS(_class
)?_class
->version
:-1;
432 class_is_class(Class _class
)
434 return CLS_ISCLASS(_class
);
438 class_is_meta_class(Class _class
)
440 return CLS_ISMETA(_class
);
445 class_set_version(Class _class
, long version
)
447 if (CLS_ISCLASS(_class
))
448 _class
->version
= version
;
452 class_get_gc_object_type (Class _class
)
454 return CLS_ISCLASS(_class
) ? _class
->gc_object_type
: NULL
;
457 /* Mark the instance variable as innaccessible to the garbage collector */
458 extern void class_ivar_set_gcinvisible (Class _class
,
459 const char* ivarname
,
463 method_get_imp(Method_t method
)
465 return (method
!=METHOD_NULL
)?method
->method_imp
:(IMP
)0;
468 IMP
get_imp (Class _class
, SEL sel
);
470 id
object_copy(id object
);
472 id
object_dispose(id object
);
475 object_get_class(id object
)
477 return ((object
!=nil
)
478 ? (CLS_ISCLASS(object
->class_pointer
)
479 ? object
->class_pointer
480 : (CLS_ISMETA(object
->class_pointer
)
486 static inline const char *
487 object_get_class_name(id object
)
489 return ((object
!=nil
)?(CLS_ISCLASS(object
->class_pointer
)
490 ?object
->class_pointer
->name
491 :((Class
)object
)->name
)
495 static inline MetaClass
496 object_get_meta_class(id object
)
498 return ((object
!=nil
)?(CLS_ISCLASS(object
->class_pointer
)
499 ?object
->class_pointer
->class_pointer
500 :(CLS_ISMETA(object
->class_pointer
)
501 ?object
->class_pointer
507 object_get_super_class
510 return ((object
!=nil
)?(CLS_ISCLASS(object
->class_pointer
)
511 ?object
->class_pointer
->super_class
512 :(CLS_ISMETA(object
->class_pointer
)
513 ?((Class
)object
)->super_class
519 object_is_class (id object
)
521 return ((object
!= nil
) && CLS_ISMETA (object
->class_pointer
));
525 object_is_instance (id object
)
527 return ((object
!= nil
) && CLS_ISCLASS (object
->class_pointer
));
531 object_is_meta_class (id object
)
533 return ((object
!= nil
)
534 && !object_is_instance (object
)
535 && !object_is_class (object
));
539 objc_get_uninstalled_dtable(void);
543 #endif /* __cplusplus */
545 #endif /* not __objc_api_INCLUDE_GNU */