]>
Commit | Line | Data |
---|---|---|
c72fc2d9 | 1 | /* GNU Objective-C Runtime API. |
e35ec852 | 2 | Copyright (C) 1993, 1995 Free Software Foundation, Inc. |
c72fc2d9 TW |
3 | |
4 | This file is part of GNU CC. | |
5 | ||
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 | |
9 | later version. | |
10 | ||
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. | |
15 | ||
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 | |
84c09f78 RK |
18 | the Free Software Foundation, 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
c72fc2d9 TW |
20 | |
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. */ | |
26 | ||
27 | #ifndef __objc_api_INCLUDE_GNU | |
28 | #define __objc_api_INCLUDE_GNU | |
29 | ||
36be30f1 KKT |
30 | #include "objc/objc.h" |
31 | #include "objc/hash.h" | |
d0b57512 | 32 | #include <stdio.h> |
c72fc2d9 | 33 | |
991d3e71 KKT |
34 | /* For functions which return Method_t */ |
35 | #define METHOD_NULL (Method_t)0 | |
36 | /* Boolean typedefs */ | |
37 | /* | |
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. | |
41 | */ | |
42 | struct objc_method_description | |
43 | { | |
44 | SEL name; /* this is a selector, not a string */ | |
45 | char *types; /* type encoding */ | |
46 | }; | |
47 | ||
991d3e71 KKT |
48 | /* Filer types used to describe Ivars and Methods. */ |
49 | #define _C_ID '@' | |
50 | #define _C_CLASS '#' | |
51 | #define _C_SEL ':' | |
52 | #define _C_CHR 'c' | |
53 | #define _C_UCHR 'C' | |
54 | #define _C_SHT 's' | |
55 | #define _C_USHT 'S' | |
56 | #define _C_INT 'i' | |
57 | #define _C_UINT 'I' | |
58 | #define _C_LNG 'l' | |
59 | #define _C_ULNG 'L' | |
60 | #define _C_FLT 'f' | |
61 | #define _C_DBL 'd' | |
62 | #define _C_BFLD 'b' | |
63 | #define _C_VOID 'v' | |
64 | #define _C_UNDEF '?' | |
65 | #define _C_PTR '^' | |
66 | #define _C_CHARPTR '*' | |
67 | #define _C_ATOM '%' | |
68 | #define _C_ARY_B '[' | |
69 | #define _C_ARY_E ']' | |
70 | #define _C_UNION_B '(' | |
71 | #define _C_UNION_E ')' | |
72 | #define _C_STRUCT_B '{' | |
73 | #define _C_STRUCT_E '}' | |
74 | ||
75 | ||
76 | ||
77 | /* | |
78 | ** Set this variable nonzero to print a line describing each | |
79 | ** message that is sent. (this is currently disabled) | |
80 | */ | |
81 | extern BOOL objc_trace; | |
82 | ||
83 | ||
84 | /* | |
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 | |
87 | ** module. | |
88 | ** | |
89 | ** A Symtab contains a variable length array of pointers to classes and | |
90 | ** categories defined in the module. | |
91 | */ | |
92 | typedef struct objc_symtab { | |
93 | unsigned long sel_ref_cnt; /* Unknown. */ | |
a39d31bc | 94 | SEL refs; /* Unknown. */ |
991d3e71 KKT |
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 | |
99 | module. */ | |
100 | void *defs[1]; /* Variable array of pointers. | |
e35ec852 | 101 | cls_def_cnt of type Class |
991d3e71 KKT |
102 | followed by cat_def_cnt of |
103 | type Category_t. */ | |
104 | } Symtab, *Symtab_t; | |
105 | ||
106 | ||
e35ec852 RK |
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 | |
112 | { | |
113 | char *class_name; | |
114 | id instances[0]; | |
115 | }; | |
116 | ||
991d3e71 KKT |
117 | /* |
118 | ** The compiler generates one of these structures for each module that | |
119 | ** composes the executable (eg main.m). | |
120 | ** | |
121 | ** This data structure is the root of the definition tree for the module. | |
122 | ** | |
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. | |
125 | */ | |
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. */ | |
e35ec852 RK |
132 | |
133 | /* Pointer to a NULL terminated array of objc_static_instances. */ | |
134 | struct objc_static_instances **statics; | |
135 | ||
991d3e71 KKT |
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. */ | |
141 | } Module, *Module_t; | |
142 | ||
143 | ||
144 | /* | |
145 | ** The compiler generates one of these structures for a class that has | |
146 | ** instance variables defined in its specification. | |
147 | */ | |
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 | |
154 | class. */ | |
155 | struct objc_ivar { | |
156 | const char* ivar_name; /* Name of the instance | |
157 | variable as entered in the | |
158 | class definition. */ | |
159 | const char* ivar_type; /* Description of the Ivar's | |
160 | type. Useful for | |
161 | debuggers. */ | |
a39d31bc | 162 | int ivar_offset; /* Byte offset from the base |
991d3e71 KKT |
163 | address of the instance |
164 | structure to the variable. */ | |
165 | ||
166 | } ivar_list[1]; /* Variable length | |
167 | structure. */ | |
168 | } IvarList, *IvarList_t; | |
169 | ||
170 | ||
171 | /* | |
172 | ** The compiler generates one (or more) of these structures for a class that | |
173 | ** has methods defined in its specification. | |
174 | ** | |
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. | |
178 | */ | |
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. */ | |
a39d31bc | 185 | int method_count; /* Number of methods defined in |
991d3e71 KKT |
186 | this structure. */ |
187 | struct objc_method { | |
188 | SEL method_name; /* This variable is the method's | |
189 | name. It is a char*. | |
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 | |
196 | debuggers. */ | |
197 | IMP method_imp; /* Address of the method in the | |
198 | executable. */ | |
199 | } method_list[1]; /* Variable length | |
200 | structure. */ | |
201 | } MethodList, *MethodList_t; | |
202 | ||
203 | struct objc_protocol_list { | |
204 | struct objc_protocol_list *next; | |
205 | int count; | |
206 | Protocol *list[1]; | |
207 | }; | |
208 | ||
209 | /* | |
210 | ** This is used to assure consistent access to the info field of | |
211 | ** classes | |
212 | */ | |
213 | #ifndef HOST_BITS_PER_LONG | |
214 | #define HOST_BITS_PER_LONG (sizeof(long)*8) | |
215 | #endif | |
216 | ||
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) | |
220 | ||
e35ec852 | 221 | /* The structure is of type MetaClass */ |
991d3e71 KKT |
222 | #define _CLS_META 0x2L |
223 | #define CLS_ISMETA(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_META)) | |
224 | ||
225 | ||
e35ec852 | 226 | /* The structure is of type Class */ |
991d3e71 KKT |
227 | #define _CLS_CLASS 0x1L |
228 | #define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS)) | |
229 | ||
230 | /* | |
231 | ** The class is initialized within the runtime. This means that | |
232 | ** it has had correct super and sublinks assigned | |
233 | */ | |
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) | |
237 | ||
238 | /* | |
239 | ** The class has been send a +initialize message or a such is not | |
240 | ** defined for this class | |
241 | */ | |
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) | |
245 | ||
246 | /* | |
247 | ** The class number of this class. This must be the same for both the | |
248 | ** class and it's meta class object | |
249 | */ | |
250 | #define CLS_GETNUMBER(cls) (__CLS_INFO(cls) >> (HOST_BITS_PER_LONG/2)) | |
251 | #define CLS_SETNUMBER(cls, num) \ | |
62b9a712 KKT |
252 | ({ (cls)->info <<= (HOST_BITS_PER_LONG/2); \ |
253 | (cls)->info >>= (HOST_BITS_PER_LONG/2); \ | |
991d3e71 KKT |
254 | __CLS_SETINFO(cls, (((unsigned long)num) << (HOST_BITS_PER_LONG/2))); }) |
255 | ||
256 | /* | |
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. | |
259 | */ | |
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 | |
275 | conformed to */ | |
276 | } Category, *Category_t; | |
277 | ||
278 | /* | |
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 | |
281 | ** objc_msg_super. | |
282 | */ | |
283 | typedef struct objc_super { | |
284 | id self; /* Id of the object sending | |
285 | the message. */ | |
e35ec852 | 286 | Class class; /* Object's super class. */ |
991d3e71 KKT |
287 | } Super, *Super_t; |
288 | ||
289 | IMP objc_msg_lookup_super(Super_t super, SEL sel); | |
290 | ||
62b9a712 | 291 | retval_t objc_msg_sendv(id, SEL, arglist_t); |
991d3e71 KKT |
292 | |
293 | ||
294 | ||
c72fc2d9 TW |
295 | /* |
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. | |
300 | */ | |
e35ec852 | 301 | extern Class (*_objc_lookup_class)(const char *name); |
c72fc2d9 | 302 | |
a4137a04 RK |
303 | /* |
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. | |
308 | */ | |
309 | extern void (*_objc_load_callback)(Class class, Category* category); | |
310 | ||
e35ec852 | 311 | extern id (*_objc_object_alloc)(Class class); |
c72fc2d9 | 312 | |
c72fc2d9 TW |
313 | extern id (*_objc_object_copy)(id object); |
314 | ||
c72fc2d9 TW |
315 | extern id (*_objc_object_dispose)(id object); |
316 | ||
e35ec852 | 317 | Method_t class_get_class_method(MetaClass class, SEL aSel); |
c72fc2d9 | 318 | |
e35ec852 | 319 | Method_t class_get_instance_method(Class class, SEL aSel); |
c72fc2d9 | 320 | |
e35ec852 | 321 | Class class_pose_as(Class impostor, Class superclass); |
c72fc2d9 | 322 | |
e35ec852 | 323 | Class objc_get_class(const char *name); |
c72fc2d9 | 324 | |
e35ec852 | 325 | Class objc_lookup_class(const char *name); |
c72fc2d9 | 326 | |
e35ec852 | 327 | Class objc_next_class(void **enum_state); |
a39d31bc | 328 | |
c72fc2d9 TW |
329 | const char *sel_get_name(SEL selector); |
330 | ||
a39d31bc KKT |
331 | const char *sel_get_type(SEL selector); |
332 | ||
c72fc2d9 TW |
333 | SEL sel_get_uid(const char *name); |
334 | ||
a39d31bc KKT |
335 | SEL sel_get_any_uid(const char *name); |
336 | ||
682d0171 RK |
337 | SEL sel_get_any_typed_uid(const char *name); |
338 | ||
a39d31bc KKT |
339 | SEL sel_get_typed_uid(const char *name, const char*); |
340 | ||
c72fc2d9 TW |
341 | SEL sel_register_name(const char *name); |
342 | ||
a39d31bc KKT |
343 | SEL sel_register_typed_name(const char *name, const char*type); |
344 | ||
345 | ||
c72fc2d9 TW |
346 | BOOL sel_is_mapped (SEL aSel); |
347 | ||
e35ec852 | 348 | extern id class_create_instance(Class class); |
c72fc2d9 | 349 | |
c72fc2d9 | 350 | static inline const char * |
e35ec852 | 351 | class_get_class_name(Class class) |
c72fc2d9 TW |
352 | { |
353 | return CLS_ISCLASS(class)?class->name:((class==Nil)?"Nil":0); | |
354 | } | |
355 | ||
c72fc2d9 | 356 | static inline long |
e35ec852 | 357 | class_get_instance_size(Class class) |
c72fc2d9 TW |
358 | { |
359 | return CLS_ISCLASS(class)?class->instance_size:0; | |
360 | } | |
361 | ||
e35ec852 RK |
362 | static inline MetaClass |
363 | class_get_meta_class(Class class) | |
c72fc2d9 TW |
364 | { |
365 | return CLS_ISCLASS(class)?class->class_pointer:Nil; | |
366 | } | |
367 | ||
e35ec852 RK |
368 | static inline Class |
369 | class_get_super_class(Class class) | |
c72fc2d9 TW |
370 | { |
371 | return CLS_ISCLASS(class)?class->super_class:Nil; | |
372 | } | |
373 | ||
c72fc2d9 | 374 | static inline int |
e35ec852 | 375 | class_get_version(Class class) |
c72fc2d9 TW |
376 | { |
377 | return CLS_ISCLASS(class)?class->version:-1; | |
378 | } | |
379 | ||
c72fc2d9 | 380 | static inline BOOL |
e35ec852 | 381 | class_is_class(Class class) |
c72fc2d9 TW |
382 | { |
383 | return CLS_ISCLASS(class); | |
384 | } | |
385 | ||
c72fc2d9 | 386 | static inline BOOL |
e35ec852 | 387 | class_is_meta_class(Class class) |
c72fc2d9 TW |
388 | { |
389 | return CLS_ISMETA(class); | |
390 | } | |
391 | ||
392 | ||
c72fc2d9 | 393 | static inline void |
e35ec852 | 394 | class_set_version(Class class, long version) |
c72fc2d9 TW |
395 | { |
396 | if (CLS_ISCLASS(class)) | |
397 | class->version = version; | |
398 | } | |
399 | ||
c72fc2d9 TW |
400 | static inline IMP |
401 | method_get_imp(Method_t method) | |
402 | { | |
403 | return (method!=METHOD_NULL)?method->method_imp:(IMP)0; | |
404 | } | |
405 | ||
e35ec852 | 406 | IMP get_imp (Class class, SEL sel); |
c72fc2d9 | 407 | |
991d3e71 | 408 | id object_copy(id object); |
c72fc2d9 | 409 | |
991d3e71 | 410 | id object_dispose(id object); |
c72fc2d9 | 411 | |
e35ec852 | 412 | static inline Class |
c72fc2d9 TW |
413 | object_get_class(id object) |
414 | { | |
415 | return ((object!=nil) | |
416 | ? (CLS_ISCLASS(object->class_pointer) | |
417 | ? object->class_pointer | |
418 | : (CLS_ISMETA(object->class_pointer) | |
e35ec852 | 419 | ? (Class)object |
c72fc2d9 TW |
420 | : Nil)) |
421 | : Nil); | |
422 | } | |
423 | ||
c72fc2d9 TW |
424 | static inline const char * |
425 | object_get_class_name(id object) | |
426 | { | |
427 | return ((object!=nil)?(CLS_ISCLASS(object->class_pointer) | |
428 | ?object->class_pointer->name | |
e35ec852 | 429 | :((Class)object)->name) |
c72fc2d9 TW |
430 | :"Nil"); |
431 | } | |
432 | ||
e35ec852 | 433 | static inline MetaClass |
c72fc2d9 TW |
434 | object_get_meta_class(id object) |
435 | { | |
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 | |
440 | :Nil)) | |
441 | :Nil); | |
442 | } | |
443 | ||
e35ec852 | 444 | static inline Class |
a1d7405c KKT |
445 | object_get_super_class |
446 | (id object) | |
c72fc2d9 TW |
447 | { |
448 | return ((object!=nil)?(CLS_ISCLASS(object->class_pointer) | |
449 | ?object->class_pointer->super_class | |
450 | :(CLS_ISMETA(object->class_pointer) | |
e35ec852 | 451 | ?((Class)object)->super_class |
c72fc2d9 TW |
452 | :Nil)) |
453 | :Nil); | |
454 | } | |
455 | ||
c72fc2d9 TW |
456 | static inline BOOL |
457 | object_is_class(id object) | |
458 | { | |
e35ec852 | 459 | return CLS_ISCLASS((Class)object); |
c72fc2d9 TW |
460 | } |
461 | ||
c72fc2d9 TW |
462 | static inline BOOL |
463 | object_is_instance(id object) | |
464 | { | |
465 | return (object!=nil)&&CLS_ISCLASS(object->class_pointer); | |
466 | } | |
467 | ||
c72fc2d9 TW |
468 | static inline BOOL |
469 | object_is_meta_class(id object) | |
470 | { | |
e35ec852 | 471 | return CLS_ISMETA((Class)object); |
c72fc2d9 TW |
472 | } |
473 | ||
991d3e71 | 474 | #endif /* not __objc_api_INCLUDE_GNU */ |
a1d7405c | 475 | |
36be30f1 | 476 | |
36be30f1 | 477 |