]> gcc.gnu.org Git - gcc.git/blame - gcc/objc/init.c
More system.h cutover patches:
[gcc.git] / gcc / objc / init.c
CommitLineData
c72fc2d9 1/* GNU Objective C Runtime initialization
241365d3 2 Copyright (C) 1993, 1995, 1996, 1997 Free Software Foundation, Inc.
4426bf3f 3 Contributed by Kresten Krab Thorup
506b0f9e 4 +load support contributed by Ovidiu Predescu <ovidiu@net-community.com>
c72fc2d9
TW
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify it under the
4426bf3f
RK
9terms of the GNU General Public License as published by the Free Software
10Foundation; either version 2, or (at your option) any later version.
c72fc2d9
TW
11
12GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
4426bf3f
RK
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15details.
c72fc2d9
TW
16
17You should have received a copy of the GNU General Public License along with
4426bf3f 18GNU CC; see the file COPYING. If not, write to the Free Software
84c09f78 19Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
c72fc2d9
TW
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"
28
29/* The version number of this runtime. This must match the number
30 defined in gcc (objc-act.c) */
f0a52291 31#define OBJC_VERSION 8
c72fc2d9
TW
32#define PROTOCOL_VERSION 2
33
34/* This list contains all modules currently loaded into the runtime */
aef85e6a 35static struct objc_list* __objc_module_list = 0; /* !T:MUTEX */
c72fc2d9
TW
36
37/* This list contains all proto_list's not yet assigned class links */
aef85e6a 38static struct objc_list* unclaimed_proto_list = 0; /* !T:MUTEX */
c72fc2d9 39
4426bf3f 40/* List of unresolved static instances. */
aef85e6a
RK
41static struct objc_list *uninitialized_statics = 0; /* !T:MUTEX */
42
43/* Global runtime "write" mutex. */
506b0f9e 44objc_mutex_t __objc_runtime_mutex = 0;
aef85e6a
RK
45
46/* Number of threads that are alive. */
47int __objc_runtime_threads_alive = 1; /* !T:MUTEX */
4426bf3f 48
c72fc2d9 49/* Check compiler vs runtime version */
4426bf3f 50static void init_check_module_version (Module_t);
c72fc2d9
TW
51
52/* Assign isa links to protos */
53static void __objc_init_protocols (struct objc_protocol_list* protos);
54
55/* Add protocol to class */
4426bf3f 56static void __objc_class_add_protocols (Class, struct objc_protocol_list*);
c72fc2d9 57
d8a55d09
RK
58/* This is a hook which is called by __objc_exec_class every time a class
59 or a category is loaded into the runtime. This may e.g. help a
60 dynamic loader determine the classes that have been loaded when
61 an object file is dynamically linked in */
aef85e6a 62void (*_objc_load_callback)(Class class, Category* category) = 0; /* !T:SAFE */
d8a55d09 63
c72fc2d9 64/* Is all categories/classes resolved? */
aef85e6a 65BOOL __objc_dangling_categories = NO; /* !T:UNUSED */
c72fc2d9 66
a39d31bc
KKT
67extern SEL
68__sel_register_typed_name (const char *name, const char *types,
018086d1 69 struct objc_selector *orig, BOOL is_const);
a39d31bc 70
506b0f9e
RK
71/* Sends +load to all classes and categories in certain situations. */
72static void objc_send_load (void);
73
74/* Inserts all the classes defined in module in a tree of classes that
75 resembles the class hierarchy. This tree is traversed in preorder and the
76 classes in its nodes receive the +load message if these methods were not
77 executed before. The algorithm ensures that when the +load method of a class
78 is executed all the superclasses have been already received the +load
79 message. */
80static void __objc_create_classes_tree (Module_t module);
81
82static void __objc_call_callback (Module_t module);
83
84/* A special version that works only before the classes are completely
85 installed in the runtime. */
86static BOOL class_is_subclass_of_class (Class class, Class superclass);
87
88typedef struct objc_class_tree {
89 Class class;
90 struct objc_list *subclasses; /* `head' is pointer to an objc_class_tree */
91} objc_class_tree;
92
93/* This is a linked list of objc_class_tree trees. The head of these trees
94 are root classes (their super class is Nil). These different trees
95 represent different class hierarchies. */
96static struct objc_list *__objc_class_tree_list = NULL;
97
98/* Keeps the +load methods who have been already executed. This hash should
99 not be destroyed during the execution of the program. */
100static cache_ptr __objc_load_methods = NULL;
101
102/* Creates a tree of classes whose topmost class is directly inherited from
103 `upper' and the bottom class in this tree is `bottom_class'. The classes
104 in this tree are super classes of `bottom_class'. `subclasses' member
105 of each tree node point to the next subclass tree node. */
106static objc_class_tree *
107create_tree_of_subclasses_inherited_from (Class bottom_class, Class upper)
108{
109 Class superclass = bottom_class->super_class ?
110 objc_lookup_class ((char*)bottom_class->super_class)
111 : Nil;
112
113 objc_class_tree *tree, *prev;
114
115 DEBUG_PRINTF ("create_tree_of_subclasses_inherited_from:");
116 DEBUG_PRINTF ("bottom_class = %s, upper = %s\n",
117 (bottom_class ? bottom_class->name : NULL),
118 (upper ? upper->name : NULL));
119
120 tree = prev = objc_calloc (1, sizeof (objc_class_tree));
121 prev->class = bottom_class;
122
123 while (superclass != upper)
124 {
125 tree = objc_calloc (1, sizeof (objc_class_tree));
126 tree->class = superclass;
127 tree->subclasses = list_cons (prev, tree->subclasses);
128 superclass = (superclass->super_class ?
129 objc_lookup_class ((char*)superclass->super_class)
130 : Nil);
131 prev = tree;
132 }
133
134 return tree;
135}
136
137/* Insert the `class' into the proper place in the `tree' class hierarchy. This
138 function returns a new tree if the class has been successfully inserted into
139 the tree or NULL if the class is not part of the classes hierarchy described
140 by `tree'. This function is private to objc_tree_insert_class(), you should
141 not call it directly. */
142static objc_class_tree *
143__objc_tree_insert_class (objc_class_tree *tree, Class class)
144{
145 DEBUG_PRINTF ("__objc_tree_insert_class: tree = %x, class = %s\n",
146 tree, class->name);
147
148 if (tree == NULL)
149 return create_tree_of_subclasses_inherited_from (class, NULL);
150 else if (class == tree->class)
151 {
152 /* `class' has been already inserted */
153 DEBUG_PRINTF ("1. class %s was previously inserted\n", class->name);
154 return tree;
155 }
156 else if ((class->super_class ?
157 objc_lookup_class ((char*)class->super_class)
158 : Nil)
159 == tree->class)
160 {
161 /* If class is a direct subclass of tree->class then add class to the
162 list of subclasses. First check to see if it wasn't already
163 inserted. */
164 struct objc_list *list = tree->subclasses;
165 objc_class_tree *node;
166
167 while (list)
168 {
169 /* Class has been already inserted; do nothing just return
170 the tree. */
171 if (((objc_class_tree*)list->head)->class == class)
172 {
173 DEBUG_PRINTF ("2. class %s was previously inserted\n",
174 class->name);
175 return tree;
176 }
177 list = list->tail;
178 }
179
180 /* Create a new node class and insert it into the list of subclasses */
181 node = objc_calloc (1, sizeof (objc_class_tree));
182 node->class = class;
183 tree->subclasses = list_cons (node, tree->subclasses);
184 DEBUG_PRINTF ("3. class %s inserted\n", class->name);
185 return tree;
186 }
187 else
188 {
189 /* The class is not a direct subclass of tree->class. Search for class's
190 superclasses in the list of subclasses. */
191 struct objc_list *subclasses = tree->subclasses;
192
193 /* Precondition: the class must be a subclass of tree->class; otherwise
194 return NULL to indicate our caller that it must take the next tree. */
195 if (!class_is_subclass_of_class (class, tree->class))
196 return NULL;
197
198 for (; subclasses != NULL; subclasses = subclasses->tail)
199 {
200 Class aClass = ((objc_class_tree*)(subclasses->head))->class;
201
202 if (class_is_subclass_of_class (class, aClass))
203 {
204 /* If we found one of class's superclasses we insert the class
205 into its subtree and return the original tree since nothing
206 has been changed. */
207 subclasses->head
208 = __objc_tree_insert_class (subclasses->head, class);
209 DEBUG_PRINTF ("4. class %s inserted\n", class->name);
210 return tree;
211 }
212 }
213
214 /* We haven't found a subclass of `class' in the `subclasses' list.
215 Create a new tree of classes whose topmost class is a direct subclass
216 of tree->class. */
217 {
218 objc_class_tree *new_tree
219 = create_tree_of_subclasses_inherited_from (class, tree->class);
220 tree->subclasses = list_cons (new_tree, tree->subclasses);
221 DEBUG_PRINTF ("5. class %s inserted\n", class->name);
222 return tree;
223 }
224 }
225}
226
227/* This function inserts `class' in the right tree hierarchy classes. */
228static void
229objc_tree_insert_class (Class class)
230{
231 struct objc_list *list_node;
232 objc_class_tree *tree;
233
234 list_node = __objc_class_tree_list;
235 while (list_node)
236 {
237 tree = __objc_tree_insert_class (list_node->head, class);
238 if (tree)
239 {
240 list_node->head = tree;
241 break;
242 }
243 else
244 list_node = list_node->tail;
245 }
246
247 /* If the list was finished but the class hasn't been inserted, insert it
248 here. */
249 if (!list_node)
250 {
251 __objc_class_tree_list = list_cons (NULL, __objc_class_tree_list);
252 __objc_class_tree_list->head = __objc_tree_insert_class (NULL, class);
253 }
254}
255
256/* Traverse tree in preorder. Used to send +load. */
257static void
258objc_preorder_traverse (objc_class_tree *tree,
259 int level,
260 void (*function)(objc_class_tree*, int))
261{
262 struct objc_list *node;
263
264 (*function) (tree, level);
265 for (node = tree->subclasses; node; node = node->tail)
266 objc_preorder_traverse (node->head, level + 1, function);
267}
268
269/* Traverse tree in postorder. Used to destroy a tree. */
270static void
271objc_postorder_traverse (objc_class_tree *tree,
272 int level,
273 void (*function)(objc_class_tree*, int))
274{
275 struct objc_list *node;
276
277 for (node = tree->subclasses; node; node = node->tail)
278 objc_postorder_traverse (node->head, level + 1, function);
279 (*function) (tree, level);
280}
281
282/* Used to print a tree class hierarchy. */
283static void
284__objc_tree_print (objc_class_tree *tree, int level)
285{
286 int i;
287
288 for (i = 0; i < level; i++)
289 printf (" ");
290 printf ("%s\n", tree->class->name);
291}
292
293/* Walks on a linked list of methods in the reverse order and executes all
294 the methods corresponding to `op' selector. Walking in the reverse order
295 assures the +load of class is executed first and then +load of categories
296 because of the way in which categories are added to the class methods. */
297static void
298__objc_send_message_in_list (MethodList_t method_list, Class class, SEL op)
299{
300 int i;
301
302 if (!method_list)
303 return;
304
305 /* First execute the `op' message in the following method lists */
306 __objc_send_message_in_list (method_list->method_next, class, op);
307
308 /* Search the method list. */
309 for (i = 0; i < method_list->method_count; i++)
310 {
311 Method_t mth = &method_list->method_list[i];
312
313 if (mth->method_name && sel_eq (mth->method_name, op)
314 && !hash_is_key_in_hash (__objc_load_methods, mth->method_name))
315 {
316 /* The method was found and wasn't previously executed. */
317 (*mth->method_imp) ((id)class, mth->method_name);
318
319 /* Add this method into the +load hash table */
320 hash_add (&__objc_load_methods, mth->method_imp, mth->method_imp);
321
322 DEBUG_PRINTF ("sending +load in class: %s\n", class->name);
323
324 break;
325 }
326 }
327}
328
329static void
330__objc_send_load (objc_class_tree *tree, int level)
331{
332 static SEL load_sel = 0;
333 Class class = tree->class;
334 MethodList_t method_list = class->class_pointer->methods;
335
336 if (!load_sel)
337 load_sel = sel_register_name ("load");
338
339 __objc_send_message_in_list (method_list, class, load_sel);
340}
341
342static void
343__objc_destroy_class_tree_node (objc_class_tree *tree, int level)
344{
345 objc_free (tree);
346}
347
348/* This is used to check if the relationship between two classes before the
349 runtime completely installs the classes. */
350static BOOL
351class_is_subclass_of_class (Class class, Class superclass)
352{
353 for (; class != Nil;)
354 {
355 if (class == superclass)
356 return YES;
357 class = (class->super_class ?
358 objc_lookup_class ((char*)class->super_class)
359 : Nil);
360 }
361
362 return NO;
363}
f0a52291
RK
364
365/* This list contains all the classes in the runtime system for whom their
366 superclasses are not yet know to the runtime. */
367static struct objc_list* unresolved_classes = 0;
368
506b0f9e
RK
369/* Static function used to reference the Object and NXConstantString classes.
370 */
f0a52291
RK
371static void
372__objc_force_linking (void)
373{
374 extern void __objc_linking (void);
375 __objc_linking ();
376
377 /* Call the function to avoid compiler warning */
378 __objc_force_linking ();
379}
380
4426bf3f
RK
381/* Run through the statics list, removing modules as soon as all its statics
382 have been initialized. */
383static void
384objc_init_statics ()
385{
386 struct objc_list **cell = &uninitialized_statics;
387 struct objc_static_instances **statics_in_module;
388
aef85e6a
RK
389 objc_mutex_lock(__objc_runtime_mutex);
390
4426bf3f
RK
391 while (*cell)
392 {
393 int module_initialized = 1;
394
395 for (statics_in_module = (*cell)->head;
396 *statics_in_module; statics_in_module++)
397 {
398 struct objc_static_instances *statics = *statics_in_module;
df27f225 399 Class class = objc_lookup_class (statics->class_name);
4426bf3f
RK
400
401 if (!class)
402 module_initialized = 0;
403 /* Actually, the static's class_pointer will be NULL when we
404 haven't been here before. However, the comparison is to be
405 reminded of taking into account class posing and to think about
406 possible semantics... */
407 else if (class != statics->instances[0]->class_pointer)
408 {
409 id *inst;
410
411 for (inst = &statics->instances[0]; *inst; inst++)
412 {
413 (*inst)->class_pointer = class;
414
415 /* ??? Make sure the object will not be freed. With
416 refcounting, invoke `-retain'. Without refcounting, do
417 nothing and hope that `-free' will never be invoked. */
418
419 /* ??? Send the object an `-initStatic' or something to
420 that effect now or later on? What are the semantics of
421 statically allocated instances, besides the trivial
422 NXConstantString, anyway? */
423 }
424 }
425 }
426 if (module_initialized)
427 {
428 /* Remove this module from the uninitialized list. */
429 struct objc_list *this = *cell;
430 *cell = this->tail;
9d46282b 431 objc_free(this);
4426bf3f
RK
432 }
433 else
434 cell = &(*cell)->tail;
435 }
aef85e6a
RK
436
437 objc_mutex_unlock(__objc_runtime_mutex);
4426bf3f 438} /* objc_init_statics */
a39d31bc 439
c72fc2d9
TW
440/* This function is called by constructor functions generated for each
441 module compiled. (_GLOBAL_$I$...) The purpose of this function is to
442 gather the module pointers so that they may be processed by the
443 initialization routines as soon as possible */
444
445void
446__objc_exec_class (Module_t module)
447{
4426bf3f
RK
448 /* Have we processed any constructors previously? This flag is used to
449 indicate that some global data structures need to be built. */
c72fc2d9
TW
450 static BOOL previous_constructors = 0;
451
452 static struct objc_list* unclaimed_categories = 0;
453
4426bf3f 454 /* The symbol table (defined in objc-api.h) generated by gcc */
c72fc2d9
TW
455 Symtab_t symtab = module->symtab;
456
f0a52291
RK
457 /* The statics in this module */
458 struct objc_static_instances **statics
459 = symtab->defs[symtab->cls_def_cnt + symtab->cat_def_cnt];
460
c72fc2d9
TW
461 /* Entry used to traverse hash lists */
462 struct objc_list** cell;
463
464 /* The table of selector references for this module */
a39d31bc 465 SEL selectors = symtab->refs;
c72fc2d9
TW
466
467 /* dummy counter */
468 int i;
469
470 DEBUG_PRINTF ("received module: %s\n", module->name);
4426bf3f 471
c72fc2d9
TW
472 /* check gcc version */
473 init_check_module_version(module);
474
475 /* On the first call of this routine, initialize some data structures. */
476 if (!previous_constructors)
477 {
aef85e6a
RK
478 /* Initialize thread-safe system */
479 __objc_init_thread_system();
480 __objc_runtime_threads_alive = 1;
481 __objc_runtime_mutex = objc_mutex_allocate();
482
c72fc2d9
TW
483 __objc_init_selector_tables();
484 __objc_init_class_tables();
485 __objc_init_dispatch_tables();
506b0f9e
RK
486 __objc_class_tree_list = list_cons (NULL, __objc_class_tree_list);
487 __objc_load_methods
488 = hash_new (128, (hash_func_type)hash_ptr, compare_ptrs);
c72fc2d9
TW
489 previous_constructors = 1;
490 }
491
492 /* Save the module pointer for later processing. (not currently used) */
aef85e6a 493 objc_mutex_lock(__objc_runtime_mutex);
c72fc2d9
TW
494 __objc_module_list = list_cons(module, __objc_module_list);
495
a39d31bc
KKT
496 /* Replace referenced selectors from names to SEL's. */
497 if (selectors)
498 {
499 for (i = 0; selectors[i].sel_id; ++i)
500 {
501 const char *name, *type;
502 name = (char*)selectors[i].sel_id;
503 type = (char*)selectors[i].sel_types;
018086d1
RK
504 /* Constructors are constant static data so we can safely store
505 pointers to them in the runtime structures. is_const == YES */
a39d31bc 506 __sel_register_typed_name (name, type,
018086d1
RK
507 (struct objc_selector*)&(selectors[i]),
508 YES);
a39d31bc
KKT
509 }
510 }
511
c72fc2d9
TW
512 /* Parse the classes in the load module and gather selector information. */
513 DEBUG_PRINTF ("gathering selectors from module: %s\n", module->name);
514 for (i = 0; i < symtab->cls_def_cnt; ++i)
515 {
4426bf3f 516 Class class = (Class) symtab->defs[i];
f0a52291 517 const char* superclass = (char*)class->super_class;
c72fc2d9
TW
518
519 /* Make sure we have what we think. */
520 assert (CLS_ISCLASS(class));
521 assert (CLS_ISMETA(class->class_pointer));
522 DEBUG_PRINTF ("phase 1, processing class: %s\n", class->name);
523
506b0f9e
RK
524 /* Initialize the subclass list to be NULL.
525 In some cases it isn't and this crashes the program. */
526 class->subclass_list = NULL;
527
c72fc2d9
TW
528 /* Store the class in the class table and assign class numbers. */
529 __objc_add_class_to_hash (class);
530
531 /* Register all of the selectors in the class and meta class. */
532 __objc_register_selectors_from_class (class);
4426bf3f 533 __objc_register_selectors_from_class ((Class) class->class_pointer);
c72fc2d9
TW
534
535 /* Install the fake dispatch tables */
536 __objc_install_premature_dtable(class);
537 __objc_install_premature_dtable(class->class_pointer);
538
506b0f9e
RK
539 /* Register the instance methods as class methods, this is
540 only done for root classes. */
541 __objc_register_instance_methods_to_class(class);
542
c72fc2d9
TW
543 if (class->protocols)
544 __objc_init_protocols (class->protocols);
d8a55d09 545
f0a52291
RK
546 /* Check to see if the superclass is known in this point. If it's not
547 add the class to the unresolved_classes list. */
548 if (superclass && !objc_lookup_class (superclass))
549 unresolved_classes = list_cons (class, unresolved_classes);
c72fc2d9
TW
550 }
551
c72fc2d9
TW
552 /* Process category information from the module. */
553 for (i = 0; i < symtab->cat_def_cnt; ++i)
554 {
555 Category_t category = symtab->defs[i + symtab->cls_def_cnt];
4426bf3f 556 Class class = objc_lookup_class (category->class_name);
c72fc2d9
TW
557
558 /* If the class for the category exists then append its methods. */
559 if (class)
560 {
561
562 DEBUG_PRINTF ("processing categories from (module,object): %s, %s\n",
563 module->name,
564 class->name);
565
566 /* Do instance methods. */
567 if (category->instance_methods)
568 class_add_method_list (class, category->instance_methods);
569
570 /* Do class methods. */
571 if (category->class_methods)
4426bf3f 572 class_add_method_list ((Class) class->class_pointer,
c72fc2d9
TW
573 category->class_methods);
574
575 if (category->protocols)
576 {
577 __objc_init_protocols (category->protocols);
578 __objc_class_add_protocols (class, category->protocols);
579 }
580
506b0f9e
RK
581 /* Register the instance methods as class methods, this is
582 only done for root classes. */
583 __objc_register_instance_methods_to_class(class);
c72fc2d9
TW
584 }
585 else
586 {
587 /* The object to which the category methods belong can't be found.
588 Save the information. */
589 unclaimed_categories = list_cons(category, unclaimed_categories);
590 }
591 }
592
f0a52291
RK
593 if (statics)
594 uninitialized_statics = list_cons (statics, uninitialized_statics);
4426bf3f
RK
595 if (uninitialized_statics)
596 objc_init_statics ();
597
c72fc2d9
TW
598 /* Scan the unclaimed category hash. Attempt to attach any unclaimed
599 categories to objects. */
600 for (cell = &unclaimed_categories;
601 *cell;
a39d31bc 602 ({ if (*cell) cell = &(*cell)->tail; }))
c72fc2d9
TW
603 {
604 Category_t category = (*cell)->head;
4426bf3f 605 Class class = objc_lookup_class (category->class_name);
c72fc2d9
TW
606
607 if (class)
608 {
609 DEBUG_PRINTF ("attaching stored categories to object: %s\n",
610 class->name);
611
612 list_remove_head (cell);
613
614 if (category->instance_methods)
615 class_add_method_list (class, category->instance_methods);
616
617 if (category->class_methods)
4426bf3f 618 class_add_method_list ((Class) class->class_pointer,
c72fc2d9 619 category->class_methods);
506b0f9e 620
c72fc2d9
TW
621 if (category->protocols)
622 {
623 __objc_init_protocols (category->protocols);
624 __objc_class_add_protocols (class, category->protocols);
625 }
506b0f9e
RK
626
627 /* Register the instance methods as class methods, this is
628 only done for root classes. */
629 __objc_register_instance_methods_to_class(class);
c72fc2d9
TW
630 }
631 }
632
633 if (unclaimed_proto_list && objc_lookup_class ("Protocol"))
634 {
635 list_mapcar (unclaimed_proto_list,(void(*)(void*))__objc_init_protocols);
636 list_free (unclaimed_proto_list);
637 unclaimed_proto_list = 0;
638 }
639
f0a52291
RK
640 objc_send_load ();
641
aef85e6a 642 objc_mutex_unlock(__objc_runtime_mutex);
c72fc2d9
TW
643}
644
506b0f9e 645static void objc_send_load (void)
f0a52291
RK
646{
647 if (!__objc_module_list)
648 return;
649
650 /* Try to find out if all the classes loaded so far also have their
651 superclasses known to the runtime. We suppose that the objects that are
652 allocated in the +load method are in general of a class declared in the
653 same module. */
654 if (unresolved_classes)
655 {
656 Class class = unresolved_classes->head;
657
658 while (objc_lookup_class ((char*)class->super_class))
659 {
660 list_remove_head (&unresolved_classes);
661 if (unresolved_classes)
662 class = unresolved_classes->head;
663 else
664 break;
665 }
666
667 /*
506b0f9e 668 * If we still have classes for whom we don't have yet their super
f0a52291
RK
669 * classes known to the runtime we don't send the +load messages.
670 */
671 if (unresolved_classes)
672 return;
673 }
674
506b0f9e
RK
675 /* Special check to allow creating and sending messages to constant strings
676 in +load methods. If these classes are not yet known, even if all the
677 other classes are known, delay sending of +load. */
678 if (!objc_lookup_class ("NXConstantString") ||
679 !objc_lookup_class ("Object"))
f0a52291
RK
680 return;
681
682 /* Iterate over all modules in the __objc_module_list and call on them the
506b0f9e
RK
683 __objc_create_classes_tree function. This function creates a tree of
684 classes that resembles the class hierarchy. */
685 list_mapcar (__objc_module_list, (void(*)(void*))__objc_create_classes_tree);
686
687 while (__objc_class_tree_list)
688 {
689#ifdef DEBUG
690 objc_preorder_traverse (__objc_class_tree_list->head,
691 0, __objc_tree_print);
692#endif
693 objc_preorder_traverse (__objc_class_tree_list->head,
694 0, __objc_send_load);
695 objc_postorder_traverse (__objc_class_tree_list->head,
696 0, __objc_destroy_class_tree_node);
697 list_remove_head (&__objc_class_tree_list);
698 }
699
700 list_mapcar (__objc_module_list, (void(*)(void*))__objc_call_callback);
f0a52291
RK
701 list_free (__objc_module_list);
702 __objc_module_list = NULL;
703}
704
705static void
506b0f9e 706__objc_create_classes_tree (Module_t module)
f0a52291 707{
506b0f9e 708 /* The runtime mutex is locked in this point */
f0a52291 709
506b0f9e
RK
710 Symtab_t symtab = module->symtab;
711 int i;
f0a52291 712
506b0f9e
RK
713 /* Iterate thru classes defined in this module and insert them in the classes
714 tree hierarchy. */
715 for (i = 0; i < symtab->cls_def_cnt; i++)
716 {
717 Class class = (Class) symtab->defs[i];
718
719 objc_tree_insert_class (class);
f0a52291
RK
720 }
721}
722
723static void
506b0f9e 724__objc_call_callback (Module_t module)
f0a52291
RK
725{
726 /* The runtime mutex is locked in this point */
727
728 Symtab_t symtab = module->symtab;
f0a52291
RK
729 int i;
730
506b0f9e
RK
731 /* Iterate thru classes defined in this module and call the callback for
732 each one. */
f0a52291
RK
733 for (i = 0; i < symtab->cls_def_cnt; i++)
734 {
735 Class class = (Class) symtab->defs[i];
f0a52291
RK
736
737 /* Call the _objc_load_callback for this class. */
738 if (_objc_load_callback)
739 _objc_load_callback(class, 0);
740 }
741
742 /* Call the _objc_load_callback for categories. Don't register the instance
743 methods as class methods for categories to root classes since they were
744 already added in the class. */
745 for (i = 0; i < symtab->cat_def_cnt; i++)
746 {
747 Category_t category = symtab->defs[i + symtab->cls_def_cnt];
748 Class class = objc_lookup_class (category->class_name);
749
750 if (_objc_load_callback)
751 _objc_load_callback(class, category);
752 }
753}
754
c72fc2d9
TW
755/* Sanity check the version of gcc used to compile `module'*/
756static void init_check_module_version(Module_t module)
757{
758 if ((module->version != OBJC_VERSION) || (module->size != sizeof (Module)))
759 {
241365d3
RK
760 int code;
761
c72fc2d9 762 if(module->version > OBJC_VERSION)
241365d3 763 code = OBJC_ERR_OBJC_VERSION;
c72fc2d9 764 else if (module->version < OBJC_VERSION)
241365d3 765 code = OBJC_ERR_GCC_VERSION;
c72fc2d9 766 else
241365d3
RK
767 code = OBJC_ERR_MODULE_SIZE;
768
769 objc_error(nil, code, "Module %s version %d doesn't match runtime %d\n",
770 module->name, (int)module->version, OBJC_VERSION);
c72fc2d9
TW
771 }
772}
773
774static void
775__objc_init_protocols (struct objc_protocol_list* protos)
776{
777 int i;
4426bf3f 778 static Class proto_class = 0;
c72fc2d9
TW
779
780 if (! protos)
781 return;
782
aef85e6a
RK
783 objc_mutex_lock(__objc_runtime_mutex);
784
eba92c95
RS
785 if (!proto_class)
786 proto_class = objc_lookup_class("Protocol");
c72fc2d9 787
eba92c95 788 if (!proto_class)
c72fc2d9
TW
789 {
790 unclaimed_proto_list = list_cons (protos, unclaimed_proto_list);
8c55cd78 791 objc_mutex_unlock(__objc_runtime_mutex);
c72fc2d9
TW
792 return;
793 }
794
2acb0388 795#if 0
c72fc2d9 796 assert (protos->next == 0); /* only single ones allowed */
2acb0388 797#endif
c72fc2d9
TW
798
799 for(i = 0; i < protos->count; i++)
800 {
eba92c95
RS
801 struct objc_protocol* aProto = protos->list[i];
802 if (((size_t)aProto->class_pointer) == PROTOCOL_VERSION)
803 {
804 /* assign class pointer */
805 aProto->class_pointer = proto_class;
806
807 /* init super protocols */
808 __objc_init_protocols (aProto->protocol_list);
809 }
810 else if (protos->list[i]->class_pointer != proto_class)
c72fc2d9 811 {
241365d3
RK
812 objc_error(nil, OBJC_ERR_PROTOCOL_VERSION,
813 "Version %d doesn't match runtime protocol version %d\n",
814 (int)((char*)protos->list[i]->class_pointer-(char*)0),
815 PROTOCOL_VERSION);
c72fc2d9
TW
816 }
817 }
aef85e6a
RK
818
819 objc_mutex_unlock(__objc_runtime_mutex);
c72fc2d9
TW
820}
821
4426bf3f 822static void __objc_class_add_protocols (Class class,
c72fc2d9
TW
823 struct objc_protocol_list* protos)
824{
c72fc2d9
TW
825 /* Well... */
826 if (! protos)
827 return;
828
829 /* Add it... */
830 protos->next = class->protocols;
831 class->protocols = protos;
832}
This page took 0.518687 seconds and 5 git commands to generate.