d19355e3270330b1d81d315ff34da3d36692fe0b
[gcc.git] / libobjc / init.c
1 /* GNU Objective C Runtime initialization
2 Copyright (C) 1993, 1995, 1996, 1997, 2002, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Kresten Krab Thorup
5 +load support contributed by Ovidiu Predescu <ovidiu@net-community.com>
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under the
10 terms of the GNU General Public License as published by the Free Software
11 Foundation; either version 3, or (at your option) any later version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
26
27 #include "objc-private/common.h"
28 #include "objc-private/error.h"
29 #include "objc/runtime.h"
30 #include "objc/thr.h"
31 #include "objc-private/hash.h"
32 #include "objc-private/objc-list.h"
33 #include "objc-private/module-abi-8.h"
34 #include "objc-private/runtime.h" /* For __objc_resolve_class_links(). */
35 #include "objc-private/selector.h" /* For __sel_register_typed_name(). */
36 #include "objc-private/objc-sync.h" /* For __objc_sync_init() */
37 #include "objc-private/protocols.h" /* For __objc_protocols_init(),
38 __objc_protocols_add_protocol()
39 __objc_protocols_register_selectors() */
40 #include "objc-private/accessors.h" /* For __objc_accessors_init() */
41
42 /* The version number of this runtime. This must match the number
43 defined in gcc (objc-act.c). */
44 #define OBJC_VERSION 8
45 #define PROTOCOL_VERSION 2
46
47 /* This list contains modules currently loaded into the runtime and
48 for which the +load method (and the load callback, if any) has not
49 been called yet. */
50 static struct objc_list *__objc_module_list = 0; /* !T:MUTEX */
51
52 /* This list contains all proto_list's not yet assigned class
53 links. */
54 static struct objc_list *unclaimed_proto_list = 0; /* !T:MUTEX */
55
56 /* List of unresolved static instances. */
57 static struct objc_list *uninitialized_statics = 0; /* !T:MUTEX */
58
59 /* List of duplicated classes found while loading modules. If we find
60 a class twice, we ignore it the second time. On some platforms,
61 where the order in which modules are loaded is well defined, this
62 allows you to replace a class in a shared library by linking in a
63 new implementation which is loaded in in the right order, and which
64 overrides the existing one.
65
66 Protected by __objc_runtime_mutex. */
67 static cache_ptr duplicate_classes = NULL;
68
69 /* Global runtime "write" mutex. Having a single mutex prevents
70 deadlocks, but reduces concurrency. To improve concurrency, some
71 groups of functions in the runtime have their own separate mutex
72 (eg, __class_table_lock in class.c); to avoid deadlocks, these
73 routines must make sure that they never acquire any other lock
74 while holding their own local lock. Ie, they should lock, execute
75 some C code that does not perform any calls to other runtime
76 functions which may potentially lock different locks, then unlock.
77 If they need to perform any calls to other runtime functions that
78 may potentially lock other locks, then they should use the global
79 __objc_runtime_mutex. */
80 objc_mutex_t __objc_runtime_mutex = 0;
81
82 /* Number of threads that are alive. */
83 int __objc_runtime_threads_alive = 1; /* !T:MUTEX */
84
85 /* Check compiler vs runtime version. */
86 static void init_check_module_version (struct objc_module *);
87
88 /* Assign isa links to protos. */
89 static void __objc_init_protocols (struct objc_protocol_list *protos);
90
91 /* Assign isa link to a protocol, and register it. */
92 static void __objc_init_protocol (struct objc_protocol *protocol);
93
94 /* Add protocol to class. */
95 static void __objc_class_add_protocols (Class, struct objc_protocol_list *);
96
97 /* Load callback hook. */
98 void (*_objc_load_callback) (Class class, struct objc_category *category) = 0; /* !T:SAFE */
99
100 /* Are all categories/classes resolved ? */
101 BOOL __objc_dangling_categories = NO; /* !T:UNUSED */
102
103 /* Sends +load to all classes and categories in certain
104 situations. */
105 static void objc_send_load (void);
106
107 /* Inserts all the classes defined in module in a tree of classes that
108 resembles the class hierarchy. This tree is traversed in preorder
109 and the classes in its nodes receive the +load message if these
110 methods were not executed before. The algorithm ensures that when
111 the +load method of a class is executed all the superclasses have
112 been already received the +load message. */
113 static void __objc_create_classes_tree (struct objc_module *module);
114
115 /* Calls the _objc_load_callback for each class and category in the
116 module (if _objc_load_callback is not NULL). */
117 static void __objc_call_load_callback (struct objc_module *module);
118
119 /* A special version that works only before the classes are completely
120 installed in the runtime. */
121 static BOOL class_is_subclass_of_class (Class class, Class superclass);
122
123 typedef struct objc_class_tree
124 {
125 Class class;
126 struct objc_list *subclasses; /* `head' is a pointer to an
127 objc_class_tree. */
128 } objc_class_tree;
129
130 /* This is a linked list of objc_class_tree trees. The head of these
131 trees are root classes (their super class is Nil). These different
132 trees represent different class hierarchies. */
133 static struct objc_list *__objc_class_tree_list = NULL;
134
135 /* Keeps the +load methods who have been already executed. This hash
136 should not be destroyed during the execution of the program. */
137 static cache_ptr __objc_load_methods = NULL;
138
139 /* This function is used when building the class tree used to send
140 ordinately the +load message to all classes needing it. The tree
141 is really needed so that superclasses will get the message before
142 subclasses.
143
144 This tree will contain classes which are being loaded (or have just
145 being loaded), and whose super_class pointers have not yet been
146 resolved. This implies that their super_class pointers point to a
147 string with the name of the superclass; when the first message is
148 sent to the class (/an object of that class) the class links will
149 be resolved, which will replace the super_class pointers with
150 pointers to the actual superclasses.
151
152 Unfortunately, the tree might also contain classes which had been
153 loaded previously, and whose class links have already been
154 resolved.
155
156 This function returns the superclass of a class in both cases, and
157 can be used to build the determine the class relationships while
158 building the tree. */
159 static Class class_superclass_of_class (Class class)
160 {
161 char *super_class_name;
162
163 /* If the class links have been resolved, use the resolved
164 links. */
165 if (CLS_ISRESOLV (class))
166 return class->super_class;
167
168 /* Else, 'class' has not yet been resolved. This means that its
169 super_class pointer is really the name of the super class (rather
170 than a pointer to the actual superclass). */
171 super_class_name = (char *)class->super_class;
172
173 /* Return Nil for a root class. */
174 if (super_class_name == NULL)
175 return Nil;
176
177 /* Lookup the superclass of non-root classes. */
178 return objc_getClass (super_class_name);
179 }
180
181
182 /* Creates a tree of classes whose topmost class is directly inherited
183 from `upper' and the bottom class in this tree is
184 `bottom_class'. The classes in this tree are super classes of
185 `bottom_class'. `subclasses' member of each tree node point to the
186 next subclass tree node. */
187 static objc_class_tree *
188 create_tree_of_subclasses_inherited_from (Class bottom_class, Class upper)
189 {
190 Class superclass;
191 objc_class_tree *tree, *prev;
192
193 if (bottom_class->super_class)
194 superclass = objc_getClass ((char *) bottom_class->super_class);
195 else
196 superclass = Nil;
197
198 DEBUG_PRINTF ("create_tree_of_subclasses_inherited_from:");
199 DEBUG_PRINTF ("bottom_class = %s, upper = %s\n",
200 (bottom_class ? bottom_class->name : NULL),
201 (upper ? upper->name : NULL));
202
203 tree = prev = objc_calloc (1, sizeof (objc_class_tree));
204 prev->class = bottom_class;
205
206 while (superclass != upper)
207 {
208 tree = objc_calloc (1, sizeof (objc_class_tree));
209 tree->class = superclass;
210 tree->subclasses = list_cons (prev, tree->subclasses);
211 superclass = class_superclass_of_class (superclass);
212 prev = tree;
213 }
214
215 return tree;
216 }
217
218 /* Insert the `class' into the proper place in the `tree' class
219 hierarchy. This function returns a new tree if the class has been
220 successfully inserted into the tree or NULL if the class is not
221 part of the classes hierarchy described by `tree'. This function is
222 private to objc_tree_insert_class (), you should not call it
223 directly. */
224 static objc_class_tree *
225 __objc_tree_insert_class (objc_class_tree *tree, Class class)
226 {
227 DEBUG_PRINTF ("__objc_tree_insert_class: tree = %p, class = %s\n",
228 tree, class->name);
229
230 if (tree == NULL)
231 return create_tree_of_subclasses_inherited_from (class, NULL);
232 else if (class == tree->class)
233 {
234 /* `class' has been already inserted. */
235 DEBUG_PRINTF ("1. class %s was previously inserted\n", class->name);
236 return tree;
237 }
238 else if (class_superclass_of_class (class) == tree->class)
239 {
240 /* If class is a direct subclass of tree->class then add class
241 to the list of subclasses. First check to see if it wasn't
242 already inserted. */
243 struct objc_list *list = tree->subclasses;
244 objc_class_tree *node;
245
246 while (list)
247 {
248 /* Class has been already inserted; do nothing just return
249 the tree. */
250 if (((objc_class_tree *) list->head)->class == class)
251 {
252 DEBUG_PRINTF ("2. class %s was previously inserted\n",
253 class->name);
254 return tree;
255 }
256 list = list->tail;
257 }
258
259 /* Create a new node class and insert it into the list of
260 subclasses. */
261 node = objc_calloc (1, sizeof (objc_class_tree));
262 node->class = class;
263 tree->subclasses = list_cons (node, tree->subclasses);
264 DEBUG_PRINTF ("3. class %s inserted\n", class->name);
265 return tree;
266 }
267 else
268 {
269 /* The class is not a direct subclass of tree->class. Search
270 for class's superclasses in the list of subclasses. */
271 struct objc_list *subclasses = tree->subclasses;
272
273 /* Precondition: the class must be a subclass of tree->class;
274 otherwise return NULL to indicate our caller that it must
275 take the next tree. */
276 if (! class_is_subclass_of_class (class, tree->class))
277 return NULL;
278
279 for (; subclasses != NULL; subclasses = subclasses->tail)
280 {
281 Class aClass = ((objc_class_tree *) (subclasses->head))->class;
282
283 if (class_is_subclass_of_class (class, aClass))
284 {
285 /* If we found one of class's superclasses we insert the
286 class into its subtree and return the original tree
287 since nothing has been changed. */
288 subclasses->head
289 = __objc_tree_insert_class (subclasses->head, class);
290 DEBUG_PRINTF ("4. class %s inserted\n", class->name);
291 return tree;
292 }
293 }
294
295 /* We haven't found a subclass of `class' in the `subclasses'
296 list. Create a new tree of classes whose topmost class is a
297 direct subclass of tree->class. */
298 {
299 objc_class_tree *new_tree
300 = create_tree_of_subclasses_inherited_from (class, tree->class);
301 tree->subclasses = list_cons (new_tree, tree->subclasses);
302 DEBUG_PRINTF ("5. class %s inserted\n", class->name);
303 return tree;
304 }
305 }
306 }
307
308 /* This function inserts `class' in the right tree hierarchy classes. */
309 static void
310 objc_tree_insert_class (Class class)
311 {
312 struct objc_list *list_node;
313 objc_class_tree *tree;
314
315 list_node = __objc_class_tree_list;
316 while (list_node)
317 {
318 tree = __objc_tree_insert_class (list_node->head, class);
319 if (tree)
320 {
321 list_node->head = tree;
322 break;
323 }
324 else
325 list_node = list_node->tail;
326 }
327
328 /* If the list was finished but the class hasn't been inserted,
329 insert it here. */
330 if (! list_node)
331 {
332 __objc_class_tree_list = list_cons (NULL, __objc_class_tree_list);
333 __objc_class_tree_list->head = __objc_tree_insert_class (NULL, class);
334 }
335 }
336
337 /* Traverse tree in preorder. Used to send +load. */
338 static void
339 objc_preorder_traverse (objc_class_tree *tree,
340 int level,
341 void (*function) (objc_class_tree *, int))
342 {
343 struct objc_list *node;
344
345 (*function) (tree, level);
346 for (node = tree->subclasses; node; node = node->tail)
347 objc_preorder_traverse (node->head, level + 1, function);
348 }
349
350 /* Traverse tree in postorder. Used to destroy a tree. */
351 static void
352 objc_postorder_traverse (objc_class_tree *tree,
353 int level,
354 void (*function) (objc_class_tree *, int))
355 {
356 struct objc_list *node;
357
358 for (node = tree->subclasses; node; node = node->tail)
359 objc_postorder_traverse (node->head, level + 1, function);
360 (*function) (tree, level);
361 }
362
363 /* Used to print a tree class hierarchy. */
364 #ifdef DEBUG
365 static void
366 __objc_tree_print (objc_class_tree *tree, int level)
367 {
368 int i;
369
370 for (i = 0; i < level; i++)
371 printf (" ");
372 printf ("%s\n", tree->class->name);
373 }
374 #endif
375
376 /* Walks on a linked list of methods in the reverse order and executes
377 all the methods corresponding to the `+load' selector. Walking in
378 the reverse order assures the +load of class is executed first and
379 then +load of categories because of the way in which categories are
380 added to the class methods. This function needs to be called with
381 the objc_runtime_mutex locked. */
382 static void
383 __objc_send_load_using_method_list (struct objc_method_list *method_list, Class class)
384 {
385 static SEL load_selector = 0;
386 int i;
387
388 if (!method_list)
389 return;
390
391 /* This needs no lock protection because we are called with the
392 objc_runtime_mutex locked. */
393 if (!load_selector)
394 load_selector = sel_registerName ("load");
395
396 /* method_list is a linked list of method lists; since we're
397 executing in reverse order, we need to do the next list before we
398 do this one. */
399 __objc_send_load_using_method_list (method_list->method_next, class);
400
401 /* Search the method list. */
402 for (i = 0; i < method_list->method_count; i++)
403 {
404 struct objc_method *mth = &method_list->method_list[i];
405
406 /* We are searching for +load methods that we haven't executed
407 yet. */
408 if (mth->method_name && sel_eq (mth->method_name, load_selector)
409 && ! objc_hash_is_key_in_hash (__objc_load_methods, mth->method_imp))
410 {
411 /* Add this method into the +load hash table, so we won't
412 execute it again next time. */
413 objc_hash_add (&__objc_load_methods,
414 mth->method_imp,
415 mth->method_imp);
416
417 DEBUG_PRINTF ("sending +load in class: %s\n", class->name);
418
419 /* Call +load. */
420 (*mth->method_imp) ((id)class, mth->method_name);
421
422 break;
423 }
424 }
425 }
426
427 /* This function needs to be called with the objc_runtime_mutex
428 locked. */
429 static void
430 __objc_send_load (objc_class_tree *tree,
431 int level __attribute__ ((__unused__)))
432 {
433 Class class = tree->class;
434 struct objc_method_list *method_list = class->class_pointer->methods;
435
436 __objc_send_load_using_method_list (method_list, class);
437 }
438
439 static void
440 __objc_destroy_class_tree_node (objc_class_tree *tree,
441 int level __attribute__ ((__unused__)))
442 {
443 objc_free (tree);
444 }
445
446 /* This is used to check if the relationship between two classes
447 before the runtime completely installs the classes. */
448 static BOOL
449 class_is_subclass_of_class (Class class, Class superclass)
450 {
451 for (; class != Nil;)
452 {
453 if (class == superclass)
454 return YES;
455 class = class_superclass_of_class (class);
456 }
457
458 return NO;
459 }
460
461 /* This list contains all the classes in the runtime system for whom
462 their superclasses are not yet known to the runtime. */
463 static struct objc_list *unresolved_classes = 0;
464
465 /* Extern function used to reference the Object class. */
466 extern void __objc_force_linking (void);
467
468 void
469 __objc_force_linking (void)
470 {
471 extern void __objc_linking (void);
472 __objc_linking ();
473 }
474
475 /* Run through the statics list, removing modules as soon as all its
476 statics have been initialized. */
477 static void
478 objc_init_statics (void)
479 {
480 struct objc_list **cell = &uninitialized_statics;
481 struct objc_static_instances **statics_in_module;
482
483 objc_mutex_lock (__objc_runtime_mutex);
484
485 while (*cell)
486 {
487 int module_initialized = 1;
488
489 for (statics_in_module = (*cell)->head;
490 *statics_in_module; statics_in_module++)
491 {
492 struct objc_static_instances *statics = *statics_in_module;
493 Class class = objc_getClass (statics->class_name);
494
495 if (! class)
496 {
497 /* It is unfortunate that this will cause all the
498 statics initialization to be done again (eg, if we
499 already initialized constant strings, and are now
500 initializing protocols, setting module_initialized to
501 0 would cause constant strings to be initialized
502 again). It would be good to be able to track if we
503 have already initialized some of them. */
504 module_initialized = 0;
505 }
506 else
507 {
508 /* Note that if this is a list of Protocol objects, some
509 of them may have been initialized already (because
510 they were attached to classes or categories, and the
511 class/category loading code automatically fixes them
512 up), and some of them may not. We really need to go
513 through the whole list to be sure! Protocols are
514 also special because we want to register them and
515 register all their selectors. */
516 id *inst;
517
518 if (strcmp (statics->class_name, "Protocol") == 0)
519 {
520 /* Protocols are special, because not only we want
521 to fix up their class pointers, but we also want
522 to register them and their selectors with the
523 runtime. */
524 for (inst = &statics->instances[0]; *inst; inst++)
525 __objc_init_protocol ((struct objc_protocol *)*inst);
526 }
527 else
528 {
529 /* Other static instances (typically constant
530 strings) are easier as we just fix up their class
531 pointers. */
532 for (inst = &statics->instances[0]; *inst; inst++)
533 (*inst)->class_pointer = class;
534 }
535 }
536 }
537 if (module_initialized)
538 {
539 /* Remove this module from the uninitialized list. */
540 struct objc_list *this = *cell;
541 *cell = this->tail;
542 objc_free (this);
543 }
544 else
545 cell = &(*cell)->tail;
546 }
547
548 objc_mutex_unlock (__objc_runtime_mutex);
549 }
550
551 /* This function is called by constructor functions generated for each
552 module compiled. (_GLOBAL_$I$...) The purpose of this function is
553 to gather the module pointers so that they may be processed by the
554 initialization routines as soon as possible. */
555 void
556 __objc_exec_class (struct objc_module *module)
557 {
558 /* Have we processed any constructors previously? This flag is used
559 to indicate that some global data structures need to be
560 built. */
561 static BOOL previous_constructors = 0;
562
563 static struct objc_list *unclaimed_categories = 0;
564
565 /* The symbol table (defined in objc-private/module-abi-8.h)
566 generated by gcc. */
567 struct objc_symtab *symtab = module->symtab;
568
569 /* The statics in this module. */
570 struct objc_static_instances **statics
571 = symtab->defs[symtab->cls_def_cnt + symtab->cat_def_cnt];
572
573 /* Entry used to traverse hash lists. */
574 struct objc_list **cell;
575
576 /* The table of selector references for this module. */
577 struct objc_selector *selectors = symtab->refs;
578
579 int i;
580
581 DEBUG_PRINTF ("received module: %s\n", module->name);
582
583 /* Check gcc version. */
584 init_check_module_version (module);
585
586 /* On the first call of this routine, initialize some data
587 structures. */
588 if (! previous_constructors)
589 {
590 /* Initialize thread-safe system. */
591 __objc_init_thread_system ();
592 __objc_runtime_threads_alive = 1;
593 __objc_runtime_mutex = objc_mutex_allocate ();
594
595 __objc_init_selector_tables ();
596 __objc_init_class_tables ();
597 __objc_init_dispatch_tables ();
598 duplicate_classes = objc_hash_new (8,
599 (hash_func_type)objc_hash_ptr,
600 objc_compare_ptrs);
601 __objc_class_tree_list = list_cons (NULL, __objc_class_tree_list);
602 __objc_load_methods = objc_hash_new (128,
603 (hash_func_type)objc_hash_ptr,
604 objc_compare_ptrs);
605 __objc_protocols_init ();
606 __objc_accessors_init ();
607 __objc_sync_init ();
608 previous_constructors = 1;
609 }
610
611 /* Save the module pointer so that later we remember to call +load
612 on all classes and categories on it. */
613 objc_mutex_lock (__objc_runtime_mutex);
614 __objc_module_list = list_cons (module, __objc_module_list);
615
616 /* Replace referenced selectors from names to SELs. */
617 if (selectors)
618 __objc_register_selectors_from_module (selectors);
619
620 /* Parse the classes in the load module and gather selector
621 information. */
622 DEBUG_PRINTF ("gathering selectors from module: %s\n", module->name);
623 for (i = 0; i < symtab->cls_def_cnt; ++i)
624 {
625 Class class = (Class) symtab->defs[i];
626 const char *superclass = (char *) class->super_class;
627
628 /* Make sure we have what we think. */
629 assert (CLS_ISCLASS (class));
630 assert (CLS_ISMETA (class->class_pointer));
631 DEBUG_PRINTF ("phase 1, processing class: %s\n", class->name);
632
633 /* Initialize the subclass list to be NULL. In some cases it
634 isn't and this crashes the program. */
635 class->subclass_list = NULL;
636
637 if (__objc_init_class (class))
638 {
639 /* Check to see if the superclass is known in this point. If
640 it's not add the class to the unresolved_classes list. */
641 if (superclass && ! objc_getClass (superclass))
642 unresolved_classes = list_cons (class, unresolved_classes);
643 }
644 }
645
646 /* Process category information from the module. */
647 for (i = 0; i < symtab->cat_def_cnt; ++i)
648 {
649 struct objc_category *category = symtab->defs[i + symtab->cls_def_cnt];
650 Class class = objc_getClass (category->class_name);
651
652 /* If the class for the category exists then append its
653 methods. */
654 if (class)
655 {
656 DEBUG_PRINTF ("processing categories from (module,object): %s, %s\n",
657 module->name,
658 class->name);
659
660 /* Do instance methods. */
661 if (category->instance_methods)
662 class_add_method_list (class, category->instance_methods);
663
664 /* Do class methods. */
665 if (category->class_methods)
666 class_add_method_list ((Class) class->class_pointer,
667 category->class_methods);
668
669 if (category->protocols)
670 {
671 __objc_init_protocols (category->protocols);
672 __objc_class_add_protocols (class, category->protocols);
673 }
674
675 /* Register the instance methods as class methods, this is
676 only done for root classes. */
677 __objc_register_instance_methods_to_class (class);
678 }
679 else
680 {
681 /* The object to which the category methods belong can't be
682 found. Save the information. */
683 unclaimed_categories = list_cons (category, unclaimed_categories);
684 }
685 }
686
687 if (statics)
688 uninitialized_statics = list_cons (statics, uninitialized_statics);
689 if (uninitialized_statics)
690 objc_init_statics ();
691
692 /* Scan the unclaimed category hash. Attempt to attach any
693 unclaimed categories to objects. */
694 for (cell = &unclaimed_categories; *cell; )
695 {
696 struct objc_category *category = (*cell)->head;
697 Class class = objc_getClass (category->class_name);
698
699 if (class)
700 {
701 DEBUG_PRINTF ("attaching stored categories to object: %s\n",
702 class->name);
703
704 list_remove_head (cell);
705
706 if (category->instance_methods)
707 class_add_method_list (class, category->instance_methods);
708
709 if (category->class_methods)
710 class_add_method_list ((Class) class->class_pointer,
711 category->class_methods);
712
713 if (category->protocols)
714 {
715 __objc_init_protocols (category->protocols);
716 __objc_class_add_protocols (class, category->protocols);
717 }
718
719 /* Register the instance methods as class methods, this is
720 only done for root classes. */
721 __objc_register_instance_methods_to_class (class);
722 }
723 else
724 cell = &(*cell)->tail;
725 }
726
727 if (unclaimed_proto_list && objc_getClass ("Protocol"))
728 {
729 list_mapcar (unclaimed_proto_list,
730 (void (*) (void *))__objc_init_protocols);
731 list_free (unclaimed_proto_list);
732 unclaimed_proto_list = 0;
733 }
734
735 objc_send_load ();
736
737 /* Check if there are no unresolved classes (ie, classes whose
738 superclass has not been loaded yet) and that the 'Object' class,
739 used as the class of classes, exist. If so, it is worth
740 "resolving the class links" at this point, which will setup all
741 the class/superclass pointers. */
742 if (!unresolved_classes && objc_getClass ("Object"))
743 __objc_resolve_class_links ();
744
745 objc_mutex_unlock (__objc_runtime_mutex);
746 }
747
748 /* This function needs to be called with the objc_runtime_mutex
749 locked. */
750 static void
751 objc_send_load (void)
752 {
753 if (!__objc_module_list)
754 return;
755
756 /* Try to find out if all the classes loaded so far also have their
757 superclasses known to the runtime. We suppose that the objects
758 that are allocated in the +load method are in general of a class
759 declared in the same module. */
760 if (unresolved_classes)
761 {
762 Class class = unresolved_classes->head;
763
764 while (objc_getClass ((char *) class->super_class))
765 {
766 list_remove_head (&unresolved_classes);
767 if (unresolved_classes)
768 class = unresolved_classes->head;
769 else
770 break;
771 }
772
773 /* If we still have classes for whom we don't have yet their
774 super classes known to the runtime we don't send the +load
775 messages (and call the load callback) yet. */
776 if (unresolved_classes)
777 return;
778 }
779
780 /* Special check. If 'Object', which is used by meta-classes, has
781 not been loaded yet, delay sending of +load. */
782 if (! objc_getClass ("Object"))
783 return;
784
785 /* Iterate over all modules in the __objc_module_list and call on
786 them the __objc_create_classes_tree function. This function
787 creates a tree of classes that resembles the class hierarchy. */
788 list_mapcar (__objc_module_list,
789 (void (*) (void *)) __objc_create_classes_tree);
790
791 while (__objc_class_tree_list)
792 {
793 #ifdef DEBUG
794 objc_preorder_traverse (__objc_class_tree_list->head,
795 0, __objc_tree_print);
796 #endif
797 objc_preorder_traverse (__objc_class_tree_list->head,
798 0, __objc_send_load);
799 objc_postorder_traverse (__objc_class_tree_list->head,
800 0, __objc_destroy_class_tree_node);
801 list_remove_head (&__objc_class_tree_list);
802 }
803
804 /* For each module, call the _objc_load_callback if any is
805 defined. */
806 list_mapcar (__objc_module_list, (void (*) (void *)) __objc_call_load_callback);
807
808 /* Empty the list of modules. */
809 list_free (__objc_module_list);
810 __objc_module_list = NULL;
811 }
812
813 static void
814 __objc_create_classes_tree (struct objc_module *module)
815 {
816 /* The runtime mutex is locked at this point */
817 struct objc_symtab *symtab = module->symtab;
818 int i;
819
820 /* Iterate thru classes defined in this module and insert them in
821 the classes tree hierarchy. */
822 for (i = 0; i < symtab->cls_def_cnt; i++)
823 {
824 Class class = (Class) symtab->defs[i];
825
826 if (!objc_hash_is_key_in_hash (duplicate_classes, class))
827 objc_tree_insert_class (class);
828 }
829
830 /* Now iterate over "claimed" categories too (ie, categories that
831 extend a class that has already been loaded by the runtime), and
832 insert them in the classes tree hiearchy too. Otherwise, if you
833 add a category, its +load method would not be called if the class
834 is already loaded in the runtime. It the category is
835 "unclaimed", ie, we haven't loaded the main class yet, postpone
836 sending +load as we want to execute +load from the class before
837 we execute the one from the category. */
838 for (i = 0; i < symtab->cat_def_cnt; ++i)
839 {
840 struct objc_category *category = symtab->defs[i + symtab->cls_def_cnt];
841 Class class = objc_getClass (category->class_name);
842
843 /* If the class for the category exists then append its
844 methods. */
845 if (class)
846 objc_tree_insert_class (class);
847 }
848 }
849
850 static void
851 __objc_call_load_callback (struct objc_module *module)
852 {
853 if (_objc_load_callback)
854 {
855 /* The runtime mutex is locked at this point. */
856 struct objc_symtab *symtab = module->symtab;
857 int i;
858
859 /* Iterate thru classes defined in this module and call the callback
860 for each one. */
861 for (i = 0; i < symtab->cls_def_cnt; i++)
862 {
863 Class class = (Class) symtab->defs[i];
864
865 if (!objc_hash_is_key_in_hash (duplicate_classes, class))
866 {
867 /* Call the _objc_load_callback for this class. */
868 _objc_load_callback (class, 0);
869 }
870 }
871
872 /* Call the _objc_load_callback for categories. Don't register
873 the instance methods as class methods for categories to root
874 classes since they were already added in the class. */
875 for (i = 0; i < symtab->cat_def_cnt; i++)
876 {
877 struct objc_category *category = symtab->defs[i + symtab->cls_def_cnt];
878 Class class = objc_getClass (category->class_name);
879
880 _objc_load_callback (class, category);
881 }
882 }
883 }
884
885 /* Sanity check the version of gcc used to compile `module'. */
886 static void
887 init_check_module_version (struct objc_module *module)
888 {
889 if ((module->version != OBJC_VERSION) || (module->size != sizeof (struct objc_module)))
890 {
891 _objc_abort ("Module %s version %d doesn't match runtime %d\n",
892 module->name, (int)module->version, OBJC_VERSION);
893 }
894 }
895
896 /* __objc_init_class must be called with __objc_runtime_mutex already
897 locked. Return YES if the class could be setup; return NO if the
898 class could not be setup because a class with the same name already
899 exists. */
900 BOOL
901 __objc_init_class (Class class)
902 {
903 /* Store the class in the class table and assign class numbers. */
904 if (__objc_add_class_to_hash (class))
905 {
906 /* Register all of the selectors in the class and meta class. */
907 __objc_register_selectors_from_class (class);
908 __objc_register_selectors_from_class ((Class) class->class_pointer);
909
910 /* Install the fake dispatch tables. */
911 __objc_install_premature_dtable (class);
912 __objc_install_premature_dtable (class->class_pointer);
913
914 /* Register the instance methods as class methods, this is only
915 done for root classes. */
916 __objc_register_instance_methods_to_class (class);
917
918 if (class->protocols)
919 __objc_init_protocols (class->protocols);
920
921 return YES;
922 }
923 else
924 {
925 /* The module contains a duplicate class. Remember it so that
926 we will ignore it later. */
927 objc_hash_add (&duplicate_classes, class, class);
928 return NO;
929 }
930 }
931
932 /* __objc_init_protocol must be called with __objc_runtime_mutex
933 already locked, and the "Protocol" class already registered. */
934 static void
935 __objc_init_protocol (struct objc_protocol *protocol)
936 {
937 static Class proto_class = 0;
938
939 if (! proto_class)
940 proto_class = objc_getClass ("Protocol");
941
942 if (((size_t)protocol->class_pointer) == PROTOCOL_VERSION)
943 {
944 /* Assign class pointer. */
945 protocol->class_pointer = proto_class;
946
947 /* Register all the selectors in the protocol with the runtime.
948 This both registers the selectors with the right types, and
949 it also fixes up the 'struct objc_method' structures inside
950 the protocol so that each method_name (a char * as compiled
951 by the compiler) is replaced with the appropriate runtime
952 SEL. */
953 if (protocol->class_methods)
954 __objc_register_selectors_from_description_list (protocol->class_methods);
955
956 if (protocol->instance_methods)
957 __objc_register_selectors_from_description_list (protocol->instance_methods);
958
959 /* Register the protocol in the hashtable or protocols by
960 name. */
961 __objc_protocols_add_protocol (protocol->protocol_name, protocol);
962
963 /* Init super protocols. */
964 __objc_init_protocols (protocol->protocol_list);
965 }
966 else if (protocol->class_pointer != proto_class)
967 {
968 _objc_abort ("Version %d doesn't match runtime protocol version %d\n",
969 (int) ((char *) protocol->class_pointer
970 - (char *) 0),
971 PROTOCOL_VERSION);
972 }
973 }
974
975 static void
976 __objc_init_protocols (struct objc_protocol_list *protos)
977 {
978 size_t i;
979 static Class proto_class = 0;
980
981 if (! protos)
982 return;
983
984 objc_mutex_lock (__objc_runtime_mutex);
985
986 if (! proto_class)
987 proto_class = objc_getClass ("Protocol");
988
989 if (! proto_class)
990 {
991 unclaimed_proto_list = list_cons (protos, unclaimed_proto_list);
992 objc_mutex_unlock (__objc_runtime_mutex);
993 return;
994 }
995
996 #if 0
997 assert (protos->next == 0); /* Only single ones allowed. */
998 #endif
999
1000 for (i = 0; i < protos->count; i++)
1001 {
1002 struct objc_protocol *aProto = protos->list[i];
1003 __objc_init_protocol (aProto);
1004 }
1005
1006 objc_mutex_unlock (__objc_runtime_mutex);
1007 }
1008
1009 static void
1010 __objc_class_add_protocols (Class class, struct objc_protocol_list *protos)
1011 {
1012 if (! protos)
1013 return;
1014
1015 protos->next = class->protocols;
1016 class->protocols = protos;
1017 }