|
|
1.1 root 1: /* GNU Objective C Runtime class related functions
2: Copyright (C) 1993 Free Software Foundation, Inc.
3:
4: Author: Kresten Krab Thorup, Dennis Glatting
5:
6: This file is part of GNU CC.
7:
8: GNU CC is free software; you can redistribute it and/or modify it under the
9: terms of the GNU General Public License as published by the Free Software
10: Foundation; either version 2, or (at your option) any later version.
11:
12: GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
13: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14: FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15: details.
16:
17: You should have received a copy of the GNU General Public License along with
18: GNU CC; see the file COPYING. If not, write to the Free Software
19: Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20:
21: /* As a special exception, if you link this library with files compiled with
22: GCC to produce an executable, this does not cause the resulting executable
23: to be covered by the GNU General Public License. This exception does not
24: however invalidate any other reasons why the executable file might be
25: covered by the GNU General Public License. */
26:
27: #include "runtime.h" /* the kitchen sink */
1.1.1.2 root 28: #include "sarray.h"
1.1 root 29:
30: /* The table of classname->class. Used for objc_lookup_class and friends */
31: static cache_ptr __objc_class_hash = 0;
32:
33: /* This is a hook which is called by objc_get_class and
34: objc_lookup_class if the runtime is not able to find the class.
35: This may e.g. try to load in the class using dynamic loading */
36: Class* (*_objc_lookup_class)(const char* name) = 0;
37:
38:
39: /* True when class links has been resolved */
40: BOOL __objc_class_links_resolved = NO;
41:
42:
43: /* Initial number of buckets size of class hash table. */
44: #define CLASS_HASH_SIZE 32
45:
46: void __objc_init_class_tables()
47: {
48: /* Allocate the class hash table */
49:
50: if(__objc_class_hash)
51: return;
52:
53: __objc_class_hash
54: = hash_new (CLASS_HASH_SIZE,
55: (hash_func_type) hash_string,
56: (compare_func_type) compare_strings);
57: }
58:
59: /* This function adds a class to the class hash table, and assigns the
60: class a number, unless it's already known */
61: void
62: __objc_add_class_to_hash(Class* class)
63: {
64: Class* h_class;
65:
66: /* make sure the table is there */
67: assert(__objc_class_hash);
68:
69: /* make sure it's not a meta class */
70: assert(CLS_ISCLASS(class));
71:
72: /* Check to see if the class is already in the hash table. */
73: h_class = hash_value_for_key (__objc_class_hash, class->name);
74: if (!h_class)
75: {
76: /* The class isn't in the hash table. Add the class and assign a class
77: number. */
78: static unsigned int class_number = 1;
79:
80: CLS_SETNUMBER(class, class_number);
81: CLS_SETNUMBER(class->class_pointer, class_number);
82:
83: ++class_number;
84: hash_add (&__objc_class_hash, class->name, class);
85: }
86: }
87:
88: /* Get the class object for the class named NAME. If NAME does not
89: identify a known class, the hook _objc_lookup_class is called. If
90: this fails, nil is returned */
91: Class* objc_lookup_class (const char* name)
92: {
93: Class* class;
94:
95: /* Make sure the class hash table exists. */
96: assert (__objc_class_hash);
97:
98: class = hash_value_for_key (__objc_class_hash, name);
99:
100: if (class)
101: return class;
102:
103: if (_objc_lookup_class)
104: return (*_objc_lookup_class)(name);
105: else
106: return 0;
107: }
108:
109: /* Get the class object for the class named NAME. If NAME does not
110: identify a known class, the hook _objc_lookup_class is called. If
111: this fails, an error message is issued and the system aborts */
112: Class*
113: objc_get_class (const char *name)
114: {
115: Class* class;
116:
117: /* Make sure the class hash table exists. */
118: assert (__objc_class_hash);
119:
120: class = hash_value_for_key (__objc_class_hash, name);
121:
122: if (class)
123: return class;
124:
125: if (_objc_lookup_class)
126: class = (*_objc_lookup_class)(name);
127:
128: if(class)
129: return class;
130:
131: fprintf(stderr, "objc runtime: cannot find class %s\n", name);
132: abort();
133: }
134:
1.1.1.3 ! root 135: MetaClass*
! 136: objc_get_meta_class(const char *name)
! 137: {
! 138: return objc_get_class(name)->class_pointer;
! 139: }
! 140:
! 141: /* This function provides a way to enumerate all the classes in the
! 142: executable. Pass *ENUM_STATE == NULL to start the enumeration. The
! 143: function will return 0 when there are no more classes.
! 144: For example:
! 145: id class;
! 146: void *es = NULL;
! 147: while ((class = objc_next_class(&es)))
! 148: ... do something with class;
! 149: */
! 150: Class*
! 151: objc_next_class(void **enum_state)
! 152: {
! 153: /* make sure the table is there */
! 154: assert(__objc_class_hash);
! 155:
! 156: *(node_ptr*)enum_state =
! 157: hash_next(__objc_class_hash, *(node_ptr*)enum_state);
! 158: if (*(node_ptr*)enum_state)
! 159: return (*(node_ptr*)enum_state)->value;
! 160: return (Class*)0;
! 161: }
1.1 root 162:
163: /* Resolve super/subclass links for all classes. The only thing we
164: can be sure of is that the class_pointer for class objects point
165: to the right meta class objects */
166: void __objc_resolve_class_links()
167: {
168: node_ptr node;
169: Class* object_class = objc_get_class ("Object");
170:
171: assert(object_class);
172:
173: /* Assign subclass links */
174: for (node = hash_next (__objc_class_hash, NULL); node;
175: node = hash_next (__objc_class_hash, node))
176: {
177: Class* class1 = node->value;
178:
179: /* Make sure we have what we think we have. */
180: assert (CLS_ISCLASS(class1));
181: assert (CLS_ISMETA(class1->class_pointer));
182:
183: /* The class_pointer of all meta classes point to Object's meta class. */
184: class1->class_pointer->class_pointer = object_class->class_pointer;
185:
186: if (!(CLS_ISRESOLV(class1)))
187: {
188: CLS_SETRESOLV(class1);
189: CLS_SETRESOLV(class1->class_pointer);
190:
191: if(class1->super_class)
192: {
193: Class* a_super_class
194: = objc_get_class ((char *) class1->super_class);
195:
196: assert (a_super_class);
197:
198: DEBUG_PRINTF ("making class connections for: %s\n",
199: class1->name);
200:
201: /* assign subclass links for superclass */
202: class1->sibling_class = a_super_class->subclass_list;
203: a_super_class->subclass_list = class1;
204:
205: /* Assign subclass links for meta class of superclass */
206: if (a_super_class->class_pointer)
207: {
208: class1->class_pointer->sibling_class
209: = a_super_class->class_pointer->subclass_list;
210: a_super_class->class_pointer->subclass_list
211: = class1->class_pointer;
212: }
213: }
214: else /* a root class, make its meta object */
215: /* be a subclass of Object */
216: {
217: class1->class_pointer->sibling_class
218: = object_class->subclass_list;
219: object_class->subclass_list = class1->class_pointer;
220: }
221: }
222: }
223:
224: /* Assign superclass links */
225: for (node = hash_next (__objc_class_hash, NULL); node;
226: node = hash_next (__objc_class_hash, node))
227: {
228: Class* class1 = node->value;
229: Class* sub_class;
230: for (sub_class = class1->subclass_list; sub_class;
231: sub_class = sub_class->sibling_class)
232: {
233: sub_class->super_class = class1;
234: if(CLS_ISCLASS(sub_class))
235: sub_class->class_pointer->super_class = class1->class_pointer;
236: }
237: }
238: }
239:
240:
1.1.1.2 root 241:
242: #define CLASSOF(c) ((c)->class_pointer)
243:
1.1 root 244: Class*
245: class_pose_as (Class* impostor, Class* super_class)
246: {
1.1.1.3 ! root 247: node_ptr node;
! 248: Class* class1;
! 249:
1.1.1.2 root 250: if (!CLS_ISRESOLV (impostor))
251: __objc_resolve_class_links ();
1.1 root 252:
1.1.1.2 root 253: /* preconditions */
254: assert (impostor);
255: assert (super_class);
256: assert (impostor->super_class == super_class);
257: assert (CLS_ISCLASS (impostor));
258: assert (CLS_ISCLASS (super_class));
1.1 root 259: assert (impostor->instance_size == super_class->instance_size);
260:
261: {
1.1.1.2 root 262: Class **subclass = &(super_class->subclass_list);
1.1 root 263:
1.1.1.2 root 264: /* move subclasses of super_class to impostor */
265: while (*subclass)
1.1 root 266: {
1.1.1.2 root 267: Class *nextSub = (*subclass)->sibling_class;
268:
1.1.1.3 ! root 269: if (*subclass != impostor)
1.1.1.2 root 270: {
271: Class *sub = *subclass;
272:
273: /* classes */
274: sub->sibling_class = impostor->subclass_list;
275: sub->super_class = impostor;
276: impostor->subclass_list = sub;
1.1.1.3 ! root 277:
! 278: /* It will happen that SUB is not a class object if it is
! 279: the top of the meta class hierachy chain. (root
! 280: meta-class objects inherit theit class object) If that is
! 281: the case... dont mess with the meta-meta class. */
! 282: if (CLS_ISCLASS (sub))
! 283: {
! 284: /* meta classes */
! 285: CLASSOF (sub)->sibling_class = CLASSOF (impostor)->subclass_list;
! 286: CLASSOF (sub)->super_class = CLASSOF (impostor);
! 287: CLASSOF (impostor)->subclass_list = CLASSOF (sub);
! 288: }
1.1.1.2 root 289: }
290:
291: *subclass = nextSub;
1.1 root 292: }
293:
1.1.1.2 root 294: /* set subclasses of superclass to be impostor only */
295: super_class->subclass_list = impostor;
296: CLASSOF (super_class)->subclass_list = CLASSOF (impostor);
297:
298: /* set impostor to have no sibling classes */
299: impostor->sibling_class = 0;
300: CLASSOF (impostor)->sibling_class = 0;
301: }
302:
1.1.1.3 ! root 303: /* check relationship of impostor and super_class is kept. */
1.1.1.2 root 304: assert (impostor->super_class == super_class);
305: assert (CLASSOF (impostor)->super_class == CLASSOF (super_class));
306:
1.1.1.3 ! root 307: /* This is how to update the lookup table. Regardless of
! 308: what the keys of the hashtable is, change all values that are
! 309: suprecalss into impostor. */
1.1.1.2 root 310:
1.1.1.3 ! root 311: for (node = hash_next (__objc_class_hash, NULL); node;
! 312: node = hash_next (__objc_class_hash, node))
! 313: {
! 314: class1 = (Class*)node->value;
! 315: if (class1 == super_class)
! 316: {
! 317: node->value = impostor; /* change hash table value */
! 318: }
! 319: }
1.1 root 320:
1.1.1.2 root 321: /* next, we update the dispatch tables... */
1.1.1.3 ! root 322: __objc_update_dispatch_table_for_class (CLASSOF (impostor));
! 323: __objc_update_dispatch_table_for_class (impostor);
1.1.1.2 root 324:
325: return impostor;
326: }
327:
1.1.1.3 ! root 328:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.