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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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. */
27 #ifndef __objc_api_INCLUDE_GNU
28 #define __objc_api_INCLUDE_GNU
30 #include "objc/objc.h"
31 #include "objc/hash.h"
34 /* For functions which return Method_t */
35 #define METHOD_NULL (Method_t)0
36 /* Boolean typedefs */
38 ** Method descriptor returned by introspective Object methods.
39 ** This is really just the first part of the more complete objc_method
40 ** structure defined below and used internally by the runtime.
42 struct objc_method_description
44 SEL name
; /* this is a selector, not a string */
45 char *types
; /* type encoding */
48 /* Filer types used to describe Ivars and Methods. */
66 #define _C_CHARPTR '*'
70 #define _C_UNION_B '('
71 #define _C_UNION_E ')'
72 #define _C_STRUCT_B '{'
73 #define _C_STRUCT_E '}'
78 ** Set this variable nonzero to print a line describing each
79 ** message that is sent. (this is currently disabled)
81 extern BOOL objc_trace
;
85 ** Whereas a Module (defined further down) is the root (typically) of a file,
86 ** a Symtab is the root of the class and category definitions within the
89 ** A Symtab contains a variable length array of pointers to classes and
90 ** categories defined in the module.
92 typedef struct objc_symtab
{
93 unsigned long sel_ref_cnt
; /* Unknown. */
94 SEL refs
; /* Unknown. */
95 unsigned short cls_def_cnt
; /* Number of classes compiled
96 (defined) in the module. */
97 unsigned short cat_def_cnt
; /* Number of categories
98 compiled (defined) in the
100 void *defs
[1]; /* Variable array of pointers.
101 cls_def_cnt of type Class
102 followed by cat_def_cnt of
107 /* For every class which happens to have statically allocated instances in
108 this module, one OBJC_STATIC_INSTANCES is allocated by the compiler.
109 INSTANCES is NULL terminated and points to all statically allocated
110 instances of this class. */
111 struct objc_static_instances
118 ** The compiler generates one of these structures for each module that
119 ** composes the executable (eg main.m).
121 ** This data structure is the root of the definition tree for the module.
123 ** A collect program runs between ld stages and creates a ObjC ctor array.
124 ** That array holds a pointer to each module structure of the executable.
126 typedef struct objc_module
{
127 unsigned long version
; /* Compiler revision. */
128 unsigned long size
; /* sizeof(Module). */
129 const char* name
; /* Name of the file where the
130 module was generated. The
131 name includes the path. */
133 /* Pointer to a NULL terminated array of objc_static_instances. */
134 struct objc_static_instances
**statics
;
136 Symtab_t symtab
; /* Pointer to the Symtab of
137 the module. The Symtab
138 holds an array of pointers to
139 the classes and categories
140 defined in the module. */
145 ** The compiler generates one of these structures for a class that has
146 ** instance variables defined in its specification.
148 typedef struct objc_ivar
* Ivar_t
;
149 typedef struct objc_ivar_list
{
150 int ivar_count
; /* Number of structures (Ivar)
151 contained in the list. One
152 structure per instance
153 variable defined in the
156 const char* ivar_name
; /* Name of the instance
157 variable as entered in the
159 const char* ivar_type
; /* Description of the Ivar's
162 int ivar_offset
; /* Byte offset from the base
163 address of the instance
164 structure to the variable. */
166 } ivar_list
[1]; /* Variable length
168 } IvarList
, *IvarList_t
;
172 ** The compiler generates one (or more) of these structures for a class that
173 ** has methods defined in its specification.
175 ** The implementation of a class can be broken into separate pieces in a file
176 ** and categories can break them across modules. To handle this problem is a
177 ** singly linked list of methods.
179 typedef struct objc_method Method
;
180 typedef Method
* Method_t
;
181 typedef struct objc_method_list
{
182 struct objc_method_list
* method_next
; /* This variable is used to link
183 a method list to another. It
184 is a singly linked list. */
185 int method_count
; /* Number of methods defined in
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
199 } method_list
[1]; /* Variable length
201 } MethodList
, *MethodList_t
;
203 struct objc_protocol_list
{
204 struct objc_protocol_list
*next
;
210 ** This is used to assure consistent access to the info field of
213 #ifndef HOST_BITS_PER_LONG
214 #define HOST_BITS_PER_LONG (sizeof(long)*8)
217 #define __CLS_INFO(cls) ((cls)->info)
218 #define __CLS_ISINFO(cls, mask) ((__CLS_INFO(cls)&mask)==mask)
219 #define __CLS_SETINFO(cls, mask) (__CLS_INFO(cls) |= mask)
221 /* The structure is of type MetaClass */
222 #define _CLS_META 0x2L
223 #define CLS_ISMETA(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_META))
226 /* The structure is of type Class */
227 #define _CLS_CLASS 0x1L
228 #define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS))
231 ** The class is initialized within the runtime. This means that
232 ** it has had correct super and sublinks assigned
234 #define _CLS_RESOLV 0x8L
235 #define CLS_ISRESOLV(cls) __CLS_ISINFO(cls, _CLS_RESOLV)
236 #define CLS_SETRESOLV(cls) __CLS_SETINFO(cls, _CLS_RESOLV)
239 ** The class has been send a +initialize message or a such is not
240 ** defined for this class
242 #define _CLS_INITIALIZED 0x04L
243 #define CLS_ISINITIALIZED(cls) __CLS_ISINFO(cls, _CLS_INITIALIZED)
244 #define CLS_SETINITIALIZED(cls) __CLS_SETINFO(cls, _CLS_INITIALIZED)
247 ** The class number of this class. This must be the same for both the
248 ** class and it's meta class object
250 #define CLS_GETNUMBER(cls) (__CLS_INFO(cls) >> (HOST_BITS_PER_LONG/2))
251 #define CLS_SETNUMBER(cls, num) \
252 ({ (cls)->info <<= (HOST_BITS_PER_LONG/2); \
253 (cls)->info >>= (HOST_BITS_PER_LONG/2); \
254 __CLS_SETINFO(cls, (((unsigned long)num) << (HOST_BITS_PER_LONG/2))); })
257 ** The compiler generates one of these structures for each category. A class
258 ** may have many categories and contain both instance and factory methods.
260 typedef struct objc_category
{
261 const char* category_name
; /* Name of the category. Name
262 contained in the () of the
263 category definition. */
264 const char* class_name
; /* Name of the class to which
265 the category belongs. */
266 MethodList_t instance_methods
; /* Linked list of instance
267 methods defined in the
268 category. NULL indicates no
269 instance methods defined. */
270 MethodList_t class_methods
; /* Linked list of factory
271 methods defined in the
272 category. NULL indicates no
273 class methods defined. */
274 struct objc_protocol_list
*protocols
; /* List of Protocols
276 } Category
, *Category_t
;
279 ** Structure used when a message is send to a class's super class. The
280 ** compiler generates one of these structures and passes it to
283 typedef struct objc_super
{
284 id self
; /* Id of the object sending
286 Class
class; /* Object's super class. */
289 IMP
objc_msg_lookup_super(Super_t super
, SEL sel
);
291 retval_t
objc_msg_sendv(id
, SEL
, arglist_t
);
296 ** This is a hook which is called by objc_lookup_class and
297 ** objc_get_class if the runtime is not able to find the class.
298 ** This may e.g. try to load in the class using dynamic loading.
299 ** The function is guaranteed to be passed a non-NULL name string.
301 extern Class (*_objc_lookup_class
)(const char *name
);
304 ** This is a hook which is called by __objc_exec_class every time a class
305 ** or a category is loaded into the runtime. This may e.g. help a
306 ** dynamic loader determine the classes that have been loaded when
307 ** an object file is dynamically linked in.
309 extern void (*_objc_load_callback
)(Class
class, Category
* category
);
311 extern id (*_objc_object_alloc
)(Class
class);
313 extern id (*_objc_object_copy
)(id object
);
315 extern id (*_objc_object_dispose
)(id object
);
317 Method_t
class_get_class_method(MetaClass
class, SEL aSel
);
319 Method_t
class_get_instance_method(Class
class, SEL aSel
);
321 Class
class_pose_as(Class impostor
, Class superclass
);
323 Class
objc_get_class(const char *name
);
325 Class
objc_lookup_class(const char *name
);
327 Class
objc_next_class(void **enum_state
);
329 const char *sel_get_name(SEL selector
);
331 const char *sel_get_type(SEL selector
);
333 SEL
sel_get_uid(const char *name
);
335 SEL
sel_get_any_uid(const char *name
);
337 SEL
sel_get_any_typed_uid(const char *name
);
339 SEL
sel_get_typed_uid(const char *name
, const char*);
341 SEL
sel_register_name(const char *name
);
343 SEL
sel_register_typed_name(const char *name
, const char*type
);
346 BOOL
sel_is_mapped (SEL aSel
);
348 extern id
class_create_instance(Class
class);
350 static inline const char *
351 class_get_class_name(Class
class)
353 return CLS_ISCLASS(class)?class->name
:((class==Nil
)?"Nil":0);
357 class_get_instance_size(Class
class)
359 return CLS_ISCLASS(class)?class->instance_size
:0;
362 static inline MetaClass
363 class_get_meta_class(Class
class)
365 return CLS_ISCLASS(class)?class->class_pointer
:Nil
;
369 class_get_super_class(Class
class)
371 return CLS_ISCLASS(class)?class->super_class
:Nil
;
375 class_get_version(Class
class)
377 return CLS_ISCLASS(class)?class->version
:-1;
381 class_is_class(Class
class)
383 return CLS_ISCLASS(class);
387 class_is_meta_class(Class
class)
389 return CLS_ISMETA(class);
394 class_set_version(Class
class, long version
)
396 if (CLS_ISCLASS(class))
397 class->version
= version
;
401 method_get_imp(Method_t method
)
403 return (method
!=METHOD_NULL
)?method
->method_imp
:(IMP
)0;
406 IMP
get_imp (Class
class, SEL sel
);
408 id
object_copy(id object
);
410 id
object_dispose(id object
);
413 object_get_class(id object
)
415 return ((object
!=nil
)
416 ? (CLS_ISCLASS(object
->class_pointer
)
417 ? object
->class_pointer
418 : (CLS_ISMETA(object
->class_pointer
)
424 static inline const char *
425 object_get_class_name(id object
)
427 return ((object
!=nil
)?(CLS_ISCLASS(object
->class_pointer
)
428 ?object
->class_pointer
->name
429 :((Class
)object
)->name
)
433 static inline MetaClass
434 object_get_meta_class(id object
)
436 return ((object
!=nil
)?(CLS_ISCLASS(object
->class_pointer
)
437 ?object
->class_pointer
->class_pointer
438 :(CLS_ISMETA(object
->class_pointer
)
439 ?object
->class_pointer
445 object_get_super_class
448 return ((object
!=nil
)?(CLS_ISCLASS(object
->class_pointer
)
449 ?object
->class_pointer
->super_class
450 :(CLS_ISMETA(object
->class_pointer
)
451 ?((Class
)object
)->super_class
457 object_is_class(id object
)
459 return CLS_ISCLASS((Class
)object
);
463 object_is_instance(id object
)
465 return (object
!=nil
)&&CLS_ISCLASS(object
->class_pointer
);
469 object_is_meta_class(id object
)
471 return CLS_ISMETA((Class
)object
);
474 #endif /* not __objc_api_INCLUDE_GNU */