1 /* GNU Objective-C Runtime API.
2 Copyright (C) 1993, 1995 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC 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
11 GNU CC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* As a special exception, if you link this library with files compiled
21 with GCC to produce an executable, this does not cause the resulting
22 executable to be covered by the GNU General Public License. This
23 exception does not however invalidate any other reasons why the
24 executable file might be covered by the GNU General Public License. */
26 #ifndef __objc_api_INCLUDE_GNU
27 #define __objc_api_INCLUDE_GNU
29 #include "objc/objc.h"
30 #include "objc/hash.h"
33 /* For functions which return Method_t */
34 #define METHOD_NULL (Method_t)0
35 /* Boolean typedefs */
37 ** Method descriptor returned by introspective Object methods.
38 ** This is really just the first part of the more complete objc_method
39 ** structure defined below and used internally by the runtime.
41 struct objc_method_description
43 SEL name
; /* this is a selector, not a string */
44 char *types
; /* type encoding */
47 /* Filer types used to describe Ivars and Methods. */
65 #define _C_CHARPTR '*'
69 #define _C_UNION_B '('
70 #define _C_UNION_E ')'
71 #define _C_STRUCT_B '{'
72 #define _C_STRUCT_E '}'
77 ** Set this variable nonzero to print a line describing each
78 ** message that is sent. (this is currently disabled)
80 extern BOOL objc_trace
;
84 ** Whereas a Module (defined further down) is the root (typically) of a file,
85 ** a Symtab is the root of the class and category definitions within the
88 ** A Symtab contains a variable length array of pointers to classes and
89 ** categories defined in the module.
91 typedef struct objc_symtab
{
92 unsigned long sel_ref_cnt
; /* Unknown. */
93 SEL refs
; /* Unknown. */
94 unsigned short cls_def_cnt
; /* Number of classes compiled
95 (defined) in the module. */
96 unsigned short cat_def_cnt
; /* Number of categories
97 compiled (defined) in the
99 void *defs
[1]; /* Variable array of pointers.
100 cls_def_cnt of type Class
101 followed by cat_def_cnt of
106 /* For every class which happens to have statically allocated instances in
107 this module, one OBJC_STATIC_INSTANCES is allocated by the compiler.
108 INSTANCES is NULL terminated and points to all statically allocated
109 instances of this class. */
110 struct objc_static_instances
117 ** The compiler generates one of these structures for each module that
118 ** composes the executable (eg main.m).
120 ** This data structure is the root of the definition tree for the module.
122 ** A collect program runs between ld stages and creates a ObjC ctor array.
123 ** That array holds a pointer to each module structure of the executable.
125 typedef struct objc_module
{
126 unsigned long version
; /* Compiler revision. */
127 unsigned long size
; /* sizeof(Module). */
128 const char* name
; /* Name of the file where the
129 module was generated. The
130 name includes the path. */
132 /* Pointer to a NULL terminated array of objc_static_instances. */
133 struct objc_static_instances
**statics
;
135 Symtab_t symtab
; /* Pointer to the Symtab of
136 the module. The Symtab
137 holds an array of pointers to
138 the classes and categories
139 defined in the module. */
144 ** The compiler generates one of these structures for a class that has
145 ** instance variables defined in its specification.
147 typedef struct objc_ivar
* Ivar_t
;
148 typedef struct objc_ivar_list
{
149 int ivar_count
; /* Number of structures (Ivar)
150 contained in the list. One
151 structure per instance
152 variable defined in the
155 const char* ivar_name
; /* Name of the instance
156 variable as entered in the
158 const char* ivar_type
; /* Description of the Ivar's
161 int ivar_offset
; /* Byte offset from the base
162 address of the instance
163 structure to the variable. */
165 } ivar_list
[1]; /* Variable length
167 } IvarList
, *IvarList_t
;
171 ** The compiler generates one (or more) of these structures for a class that
172 ** has methods defined in its specification.
174 ** The implementation of a class can be broken into separate pieces in a file
175 ** and categories can break them across modules. To handle this problem is a
176 ** singly linked list of methods.
178 typedef struct objc_method Method
;
179 typedef Method
* Method_t
;
180 typedef struct objc_method_list
{
181 struct objc_method_list
* method_next
; /* This variable is used to link
182 a method list to another. It
183 is a singly linked list. */
184 int method_count
; /* Number of methods defined in
187 SEL method_name
; /* This variable is the method's
189 The unique integer passed to
190 objc_msg_send is a char* too.
191 It is compared against
192 method_name using strcmp. */
193 const char* method_types
; /* Description of the method's
194 parameter list. Useful for
196 IMP method_imp
; /* Address of the method in the
198 } method_list
[1]; /* Variable length
200 } MethodList
, *MethodList_t
;
202 struct objc_protocol_list
{
203 struct objc_protocol_list
*next
;
209 ** This is used to assure consistent access to the info field of
212 #ifndef HOST_BITS_PER_LONG
213 #define HOST_BITS_PER_LONG (sizeof(long)*8)
216 #define __CLS_INFO(cls) ((cls)->info)
217 #define __CLS_ISINFO(cls, mask) ((__CLS_INFO(cls)&mask)==mask)
218 #define __CLS_SETINFO(cls, mask) (__CLS_INFO(cls) |= mask)
220 /* The structure is of type MetaClass */
221 #define _CLS_META 0x2L
222 #define CLS_ISMETA(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_META))
225 /* The structure is of type Class */
226 #define _CLS_CLASS 0x1L
227 #define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS))
230 ** The class is initialized within the runtime. This means that
231 ** it has had correct super and sublinks assigned
233 #define _CLS_RESOLV 0x8L
234 #define CLS_ISRESOLV(cls) __CLS_ISINFO(cls, _CLS_RESOLV)
235 #define CLS_SETRESOLV(cls) __CLS_SETINFO(cls, _CLS_RESOLV)
238 ** The class has been send a +initialize message or a such is not
239 ** defined for this class
241 #define _CLS_INITIALIZED 0x04L
242 #define CLS_ISINITIALIZED(cls) __CLS_ISINFO(cls, _CLS_INITIALIZED)
243 #define CLS_SETINITIALIZED(cls) __CLS_SETINFO(cls, _CLS_INITIALIZED)
246 ** The class number of this class. This must be the same for both the
247 ** class and it's meta class object
249 #define CLS_GETNUMBER(cls) (__CLS_INFO(cls) >> (HOST_BITS_PER_LONG/2))
250 #define CLS_SETNUMBER(cls, num) \
251 ({ (cls)->info <<= (HOST_BITS_PER_LONG/2); \
252 (cls)->info >>= (HOST_BITS_PER_LONG/2); \
253 __CLS_SETINFO(cls, (((unsigned long)num) << (HOST_BITS_PER_LONG/2))); })
256 ** The compiler generates one of these structures for each category. A class
257 ** may have many categories and contain both instance and factory methods.
259 typedef struct objc_category
{
260 const char* category_name
; /* Name of the category. Name
261 contained in the () of the
262 category definition. */
263 const char* class_name
; /* Name of the class to which
264 the category belongs. */
265 MethodList_t instance_methods
; /* Linked list of instance
266 methods defined in the
267 category. NULL indicates no
268 instance methods defined. */
269 MethodList_t class_methods
; /* Linked list of factory
270 methods defined in the
271 category. NULL indicates no
272 class methods defined. */
273 struct objc_protocol_list
*protocols
; /* List of Protocols
275 } Category
, *Category_t
;
278 ** Structure used when a message is send to a class's super class. The
279 ** compiler generates one of these structures and passes it to
282 typedef struct objc_super
{
283 id self
; /* Id of the object sending
285 Class
class; /* Object's super class. */
288 IMP
objc_msg_lookup_super(Super_t super
, SEL sel
);
290 retval_t
objc_msg_sendv(id
, SEL
, arglist_t
);
295 ** This is a hook which is called by objc_lookup_class and
296 ** objc_get_class if the runtime is not able to find the class.
297 ** This may e.g. try to load in the class using dynamic loading.
298 ** The function is guaranteed to be passed a non-NULL name string.
300 extern Class (*_objc_lookup_class
)(const char *name
);
303 ** This is a hook which is called by __objc_exec_class every time a class
304 ** or a category is loaded into the runtime. This may e.g. help a
305 ** dynamic loader determine the classes that have been loaded when
306 ** an object file is dynamically linked in.
308 extern void (*_objc_load_callback
)(Class
class, Category
* category
);
310 extern id (*_objc_object_alloc
)(Class
class);
312 extern id (*_objc_object_copy
)(id object
);
314 extern id (*_objc_object_dispose
)(id object
);
316 Method_t
class_get_class_method(MetaClass
class, SEL aSel
);
318 Method_t
class_get_instance_method(Class
class, SEL aSel
);
320 Class
class_pose_as(Class impostor
, Class superclass
);
322 Class
objc_get_class(const char *name
);
324 Class
objc_lookup_class(const char *name
);
326 Class
objc_next_class(void **enum_state
);
328 const char *sel_get_name(SEL selector
);
330 const char *sel_get_type(SEL selector
);
332 SEL
sel_get_uid(const char *name
);
334 SEL
sel_get_any_uid(const char *name
);
336 SEL
sel_get_typed_uid(const char *name
, const char*);
338 SEL
sel_register_name(const char *name
);
340 SEL
sel_register_typed_name(const char *name
, const char*type
);
343 BOOL
sel_is_mapped (SEL aSel
);
345 extern id
class_create_instance(Class
class);
347 static inline const char *
348 class_get_class_name(Class
class)
350 return CLS_ISCLASS(class)?class->name
:((class==Nil
)?"Nil":0);
354 class_get_instance_size(Class
class)
356 return CLS_ISCLASS(class)?class->instance_size
:0;
359 static inline MetaClass
360 class_get_meta_class(Class
class)
362 return CLS_ISCLASS(class)?class->class_pointer
:Nil
;
366 class_get_super_class(Class
class)
368 return CLS_ISCLASS(class)?class->super_class
:Nil
;
372 class_get_version(Class
class)
374 return CLS_ISCLASS(class)?class->version
:-1;
378 class_is_class(Class
class)
380 return CLS_ISCLASS(class);
384 class_is_meta_class(Class
class)
386 return CLS_ISMETA(class);
391 class_set_version(Class
class, long version
)
393 if (CLS_ISCLASS(class))
394 class->version
= version
;
398 method_get_imp(Method_t method
)
400 return (method
!=METHOD_NULL
)?method
->method_imp
:(IMP
)0;
403 IMP
get_imp (Class
class, SEL sel
);
405 id
object_copy(id object
);
407 id
object_dispose(id object
);
410 object_get_class(id object
)
412 return ((object
!=nil
)
413 ? (CLS_ISCLASS(object
->class_pointer
)
414 ? object
->class_pointer
415 : (CLS_ISMETA(object
->class_pointer
)
421 static inline const char *
422 object_get_class_name(id object
)
424 return ((object
!=nil
)?(CLS_ISCLASS(object
->class_pointer
)
425 ?object
->class_pointer
->name
426 :((Class
)object
)->name
)
430 static inline MetaClass
431 object_get_meta_class(id object
)
433 return ((object
!=nil
)?(CLS_ISCLASS(object
->class_pointer
)
434 ?object
->class_pointer
->class_pointer
435 :(CLS_ISMETA(object
->class_pointer
)
436 ?object
->class_pointer
442 object_get_super_class
445 return ((object
!=nil
)?(CLS_ISCLASS(object
->class_pointer
)
446 ?object
->class_pointer
->super_class
447 :(CLS_ISMETA(object
->class_pointer
)
448 ?((Class
)object
)->super_class
454 object_is_class(id object
)
456 return CLS_ISCLASS((Class
)object
);
460 object_is_instance(id object
)
462 return (object
!=nil
)&&CLS_ISCLASS(object
->class_pointer
);
466 object_is_meta_class(id object
)
468 return CLS_ISMETA((Class
)object
);
471 #endif /* not __objc_api_INCLUDE_GNU */