1 /* GNU Objective-C Runtime API.
2 Copyright (C) 1993 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 */
49 /* Filer types used to describe Ivars and Methods. */
67 #define _C_CHARPTR '*'
71 #define _C_UNION_B '('
72 #define _C_UNION_E ')'
73 #define _C_STRUCT_B '{'
74 #define _C_STRUCT_E '}'
79 ** Set this variable nonzero to print a line describing each
80 ** message that is sent. (this is currently disabled)
82 extern BOOL objc_trace
;
86 ** Whereas a Module (defined further down) is the root (typically) of a file,
87 ** a Symtab is the root of the class and category definitions within the
90 ** A Symtab contains a variable length array of pointers to classes and
91 ** categories defined in the module.
93 typedef struct objc_symtab
{
94 unsigned long sel_ref_cnt
; /* Unknown. */
95 SEL
*refs
; /* Unknown. */
96 unsigned short cls_def_cnt
; /* Number of classes compiled
97 (defined) in the module. */
98 unsigned short cat_def_cnt
; /* Number of categories
99 compiled (defined) in the
101 void *defs
[1]; /* Variable array of pointers.
102 cls_def_cnt of type Class*
103 followed by cat_def_cnt of
109 ** The compiler generates one of these structures for each module that
110 ** composes the executable (eg main.m).
112 ** This data structure is the root of the definition tree for the module.
114 ** A collect program runs between ld stages and creates a ObjC ctor array.
115 ** That array holds a pointer to each module structure of the executable.
117 typedef struct objc_module
{
118 unsigned long version
; /* Compiler revision. */
119 unsigned long size
; /* sizeof(Module). */
120 const char* name
; /* Name of the file where the
121 module was generated. The
122 name includes the path. */
123 Symtab_t symtab
; /* Pointer to the Symtab of
124 the module. The Symtab
125 holds an array of pointers to
126 the classes and categories
127 defined in the module. */
132 ** The compiler generates one of these structures for a class that has
133 ** instance variables defined in its specification.
135 typedef struct objc_ivar
* Ivar_t
;
136 typedef struct objc_ivar_list
{
137 int ivar_count
; /* Number of structures (Ivar)
138 contained in the list. One
139 structure per instance
140 variable defined in the
143 const char* ivar_name
; /* Name of the instance
144 variable as entered in the
146 const char* ivar_type
; /* Description of the Ivar's
149 int ivar_offset
; /* Byte offset from the base
150 address of the instance
151 structure to the variable. */
153 } ivar_list
[1]; /* Variable length
155 } IvarList
, *IvarList_t
;
159 ** The compiler generates one (or more) of these structures for a class that
160 ** has methods defined in its specification.
162 ** The implementation of a class can be broken into separate pieces in a file
163 ** and categories can break them across modules. To handle this problem is a
164 ** singly linked list of methods.
166 typedef struct objc_method Method
;
167 typedef Method
* Method_t
;
168 typedef struct objc_method_list
{
169 struct objc_method_list
* method_next
; /* This variable is used to link
170 a method list to another. It
171 is a singly linked list. */
172 int method_count
; /* Number of methods defined in
175 SEL method_name
; /* This variable is the method's
177 The unique integer passed to
178 objc_msg_send is a char* too.
179 It is compared against
180 method_name using strcmp. */
181 const char* method_types
; /* Description of the method's
182 parameter list. Useful for
184 IMP method_imp
; /* Address of the method in the
186 } method_list
[1]; /* Variable length
188 } MethodList
, *MethodList_t
;
190 struct objc_protocol_list
{
191 struct objc_protocol_list
*next
;
197 ** This is used to assure consistent access to the info field of
200 #ifndef HOST_BITS_PER_LONG
201 #define HOST_BITS_PER_LONG (sizeof(long)*8)
204 #define __CLS_INFO(cls) ((cls)->info)
205 #define __CLS_ISINFO(cls, mask) ((__CLS_INFO(cls)&mask)==mask)
206 #define __CLS_SETINFO(cls, mask) (__CLS_INFO(cls) |= mask)
208 /* The structure is of type MetaClass* */
209 #define _CLS_META 0x2L
210 #define CLS_ISMETA(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_META))
213 /* The structure is of type Class* */
214 #define _CLS_CLASS 0x1L
215 #define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS))
218 ** The class is initialized within the runtime. This means that
219 ** it has had correct super and sublinks assigned
221 #define _CLS_RESOLV 0x8L
222 #define CLS_ISRESOLV(cls) __CLS_ISINFO(cls, _CLS_RESOLV)
223 #define CLS_SETRESOLV(cls) __CLS_SETINFO(cls, _CLS_RESOLV)
226 ** The class has been send a +initialize message or a such is not
227 ** defined for this class
229 #define _CLS_INITIALIZED 0x04L
230 #define CLS_ISINITIALIZED(cls) __CLS_ISINFO(cls, _CLS_INITIALIZED)
231 #define CLS_SETINITIALIZED(cls) __CLS_SETINFO(cls, _CLS_INITIALIZED)
234 ** The class number of this class. This must be the same for both the
235 ** class and it's meta class object
237 #define CLS_GETNUMBER(cls) (__CLS_INFO(cls) >> (HOST_BITS_PER_LONG/2))
238 #define CLS_SETNUMBER(cls, num) \
239 ({ assert(CLS_GETNUMBER(cls)==0); \
240 __CLS_SETINFO(cls, (((unsigned long)num) << (HOST_BITS_PER_LONG/2))); })
243 ** The compiler generates one of these structures for each category. A class
244 ** may have many categories and contain both instance and factory methods.
246 typedef struct objc_category
{
247 const char* category_name
; /* Name of the category. Name
248 contained in the () of the
249 category definition. */
250 const char* class_name
; /* Name of the class to which
251 the category belongs. */
252 MethodList_t instance_methods
; /* Linked list of instance
253 methods defined in the
254 category. NULL indicates no
255 instance methods defined. */
256 MethodList_t class_methods
; /* Linked list of factory
257 methods defined in the
258 category. NULL indicates no
259 class methods defined. */
260 struct objc_protocol_list
*protocols
; /* List of Protocols
262 } Category
, *Category_t
;
265 ** Structure used when a message is send to a class's super class. The
266 ** compiler generates one of these structures and passes it to
269 typedef struct objc_super
{
270 id self
; /* Id of the object sending
272 Class
* class; /* Object's super class. */
275 IMP
objc_msg_lookup_super(Super_t super
, SEL sel
);
277 retval_t
objc_msg_sendv(id
, SEL
, size_t, arglist_t
);
281 static const ARGSIZE
= 96; /* for `method_get_argsize()' */
284 ** This is a hook which is called by objc_lookup_class and
285 ** objc_get_class if the runtime is not able to find the class.
286 ** This may e.g. try to load in the class using dynamic loading.
287 ** The function is guaranteed to be passed a non-NULL name string.
289 extern Class
* (*_objc_lookup_class
)(const char *name
);
291 extern id (*_objc_object_alloc
)(Class
* class);
293 extern id (*_objc_object_copy
)(id object
);
295 extern id (*_objc_object_dispose
)(id object
);
297 Method_t
class_get_class_method(MetaClass
* class, SEL aSel
);
299 Method_t
class_get_instance_method(Class
* class, SEL aSel
);
301 Class
* class_pose_as(Class
* impostor
, Class
* superclass
);
303 Class
* objc_get_class(const char *name
);
305 Class
* objc_lookup_class(const char *name
);
307 const char *sel_get_name(SEL selector
);
309 SEL
sel_get_uid(const char *name
);
311 SEL
sel_register_name(const char *name
);
313 BOOL
sel_is_mapped (SEL aSel
);
315 extern id
class_create_instance(Class
* class);
317 static inline const char *
318 class_get_class_name(Class
* class)
320 return CLS_ISCLASS(class)?class->name
:((class==Nil
)?"Nil":0);
324 class_get_instance_size(Class
* class)
326 return CLS_ISCLASS(class)?class->instance_size
:0;
329 static inline MetaClass
*
330 class_get_meta_class(Class
* class)
332 return CLS_ISCLASS(class)?class->class_pointer
:Nil
;
336 class_get_super_class(Class
* class)
338 return CLS_ISCLASS(class)?class->super_class
:Nil
;
342 class_get_version(Class
* class)
344 return CLS_ISCLASS(class)?class->version
:-1;
348 class_is_class(Class
* class)
350 return CLS_ISCLASS(class);
354 class_is_meta_class(Class
* class)
356 return CLS_ISMETA(class);
361 class_set_version(Class
* class, long version
)
363 if (CLS_ISCLASS(class))
364 class->version
= version
;
367 static inline unsigned int
368 method_get_argsize(Method_t method
)
370 return ARGSIZE
; /* This was a magic number (96)... */
374 method_get_imp(Method_t method
)
376 return (method
!=METHOD_NULL
)?method
->method_imp
:(IMP
)0;
379 IMP
get_imp (Class
* class, SEL sel
);
381 id
object_copy(id object
);
383 id
object_dispose(id object
);
386 object_get_class(id object
)
388 return ((object
!=nil
)
389 ? (CLS_ISCLASS(object
->class_pointer
)
390 ? object
->class_pointer
391 : (CLS_ISMETA(object
->class_pointer
)
397 static inline const char *
398 object_get_class_name(id object
)
400 return ((object
!=nil
)?(CLS_ISCLASS(object
->class_pointer
)
401 ?object
->class_pointer
->name
402 :((Class
*)object
)->name
)
406 static inline MetaClass
*
407 object_get_meta_class(id object
)
409 return ((object
!=nil
)?(CLS_ISCLASS(object
->class_pointer
)
410 ?object
->class_pointer
->class_pointer
411 :(CLS_ISMETA(object
->class_pointer
)
412 ?object
->class_pointer
418 object_get_super_class
421 return ((object
!=nil
)?(CLS_ISCLASS(object
->class_pointer
)
422 ?object
->class_pointer
->super_class
423 :(CLS_ISMETA(object
->class_pointer
)
424 ?((Class
*)object
)->super_class
430 object_is_class(id object
)
432 return CLS_ISCLASS((Class
*)object
);
436 object_is_instance(id object
)
438 return (object
!=nil
)&&CLS_ISCLASS(object
->class_pointer
);
442 object_is_meta_class(id object
)
444 return CLS_ISMETA((Class
*)object
);
447 #endif /* not __objc_api_INCLUDE_GNU */