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>
7 This file is part of GCC.
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.
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
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.
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/>. */
27 #include "objc-private/common.h"
28 #include "objc-private/error.h"
29 #include "objc/runtime.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() */
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
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
50 static struct objc_list
*__objc_module_list
= 0; /* !T:MUTEX */
52 /* This list contains all proto_list's not yet assigned class
54 static struct objc_list
*unclaimed_proto_list
= 0; /* !T:MUTEX */
56 /* List of unresolved static instances. */
57 static struct objc_list
*uninitialized_statics
= 0; /* !T:MUTEX */
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.
66 Protected by __objc_runtime_mutex. */
67 static cache_ptr duplicate_classes
= NULL
;
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;
82 /* Number of threads that are alive. */
83 int __objc_runtime_threads_alive
= 1; /* !T:MUTEX */
85 /* Check compiler vs runtime version. */
86 static void init_check_module_version (struct objc_module
*);
88 /* Assign isa links to protos. */
89 static void __objc_init_protocols (struct objc_protocol_list
*protos
);
91 /* Assign isa link to a protocol, and register it. */
92 static void __objc_init_protocol (struct objc_protocol
*protocol
);
94 /* Add protocol to class. */
95 static void __objc_class_add_protocols (Class
, struct objc_protocol_list
*);
97 /* Load callback hook. */
98 void (*_objc_load_callback
) (Class
class, struct objc_category
*category
) = 0; /* !T:SAFE */
100 /* Are all categories/classes resolved ? */
101 BOOL __objc_dangling_categories
= NO
; /* !T:UNUSED */
103 /* Sends +load to all classes and categories in certain
105 static void objc_send_load (void);
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
);
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
);
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
);
123 typedef struct objc_class_tree
126 struct objc_list
*subclasses
; /* `head' is a pointer to an
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
;
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
;
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
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.
152 Unfortunately, the tree might also contain classes which had been
153 loaded previously, and whose class links have already been
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)
161 char *super_class_name
;
163 /* If the class links have been resolved, use the resolved
165 if (CLS_ISRESOLV (class))
166 return class->super_class
;
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
;
173 /* Return Nil for a root class. */
174 if (super_class_name
== NULL
)
177 /* Lookup the superclass of non-root classes. */
178 return objc_getClass (super_class_name
);
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
)
191 objc_class_tree
*tree
, *prev
;
193 if (bottom_class
->super_class
)
194 superclass
= objc_getClass ((char *) bottom_class
->super_class
);
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
));
203 tree
= prev
= objc_calloc (1, sizeof (objc_class_tree
));
204 prev
->class = bottom_class
;
206 while (superclass
!= upper
)
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
);
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
224 static objc_class_tree
*
225 __objc_tree_insert_class (objc_class_tree
*tree
, Class
class)
227 DEBUG_PRINTF ("__objc_tree_insert_class: tree = %p, class = %s\n",
231 return create_tree_of_subclasses_inherited_from (class, NULL
);
232 else if (class == tree
->class)
234 /* `class' has been already inserted. */
235 DEBUG_PRINTF ("1. class %s was previously inserted\n", class->name
);
238 else if (class_superclass_of_class (class) == tree
->class)
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
243 struct objc_list
*list
= tree
->subclasses
;
244 objc_class_tree
*node
;
248 /* Class has been already inserted; do nothing just return
250 if (((objc_class_tree
*) list
->head
)->class == class)
252 DEBUG_PRINTF ("2. class %s was previously inserted\n",
259 /* Create a new node class and insert it into the list of
261 node
= objc_calloc (1, sizeof (objc_class_tree
));
263 tree
->subclasses
= list_cons (node
, tree
->subclasses
);
264 DEBUG_PRINTF ("3. class %s inserted\n", class->name
);
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
;
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))
279 for (; subclasses
!= NULL
; subclasses
= subclasses
->tail
)
281 Class aClass
= ((objc_class_tree
*) (subclasses
->head
))->class;
283 if (class_is_subclass_of_class (class, aClass
))
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. */
289 = __objc_tree_insert_class (subclasses
->head
, class);
290 DEBUG_PRINTF ("4. class %s inserted\n", class->name
);
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. */
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
);
308 /* This function inserts `class' in the right tree hierarchy classes. */
310 objc_tree_insert_class (Class
class)
312 struct objc_list
*list_node
;
313 objc_class_tree
*tree
;
315 list_node
= __objc_class_tree_list
;
318 tree
= __objc_tree_insert_class (list_node
->head
, class);
321 list_node
->head
= tree
;
325 list_node
= list_node
->tail
;
328 /* If the list was finished but the class hasn't been inserted,
332 __objc_class_tree_list
= list_cons (NULL
, __objc_class_tree_list
);
333 __objc_class_tree_list
->head
= __objc_tree_insert_class (NULL
, class);
337 /* Traverse tree in preorder. Used to send +load. */
339 objc_preorder_traverse (objc_class_tree
*tree
,
341 void (*function
) (objc_class_tree
*, int))
343 struct objc_list
*node
;
345 (*function
) (tree
, level
);
346 for (node
= tree
->subclasses
; node
; node
= node
->tail
)
347 objc_preorder_traverse (node
->head
, level
+ 1, function
);
350 /* Traverse tree in postorder. Used to destroy a tree. */
352 objc_postorder_traverse (objc_class_tree
*tree
,
354 void (*function
) (objc_class_tree
*, int))
356 struct objc_list
*node
;
358 for (node
= tree
->subclasses
; node
; node
= node
->tail
)
359 objc_postorder_traverse (node
->head
, level
+ 1, function
);
360 (*function
) (tree
, level
);
363 /* Used to print a tree class hierarchy. */
366 __objc_tree_print (objc_class_tree
*tree
, int level
)
370 for (i
= 0; i
< level
; i
++)
372 printf ("%s\n", tree
->class->name
);
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. */
383 __objc_send_load_using_method_list (struct objc_method_list
*method_list
, Class
class)
385 static SEL load_selector
= 0;
391 /* This needs no lock protection because we are called with the
392 objc_runtime_mutex locked. */
394 load_selector
= sel_registerName ("load");
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
399 __objc_send_load_using_method_list (method_list
->method_next
, class);
401 /* Search the method list. */
402 for (i
= 0; i
< method_list
->method_count
; i
++)
404 struct objc_method
*mth
= &method_list
->method_list
[i
];
406 /* We are searching for +load methods that we haven't executed
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
))
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
,
417 DEBUG_PRINTF ("sending +load in class: %s\n", class->name
);
420 (*mth
->method_imp
) ((id
)class, mth
->method_name
);
427 /* This function needs to be called with the objc_runtime_mutex
430 __objc_send_load (objc_class_tree
*tree
,
431 int level
__attribute__ ((__unused__
)))
433 Class
class = tree
->class;
434 struct objc_method_list
*method_list
= class->class_pointer
->methods
;
436 __objc_send_load_using_method_list (method_list
, class);
440 __objc_destroy_class_tree_node (objc_class_tree
*tree
,
441 int level
__attribute__ ((__unused__
)))
446 /* This is used to check if the relationship between two classes
447 before the runtime completely installs the classes. */
449 class_is_subclass_of_class (Class
class, Class superclass
)
451 for (; class != Nil
;)
453 if (class == superclass
)
455 class = class_superclass_of_class (class);
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;
465 /* Extern function used to reference the Object class. */
466 extern void __objc_force_linking (void);
469 __objc_force_linking (void)
471 extern void __objc_linking (void);
475 /* Run through the statics list, removing modules as soon as all its
476 statics have been initialized. */
478 objc_init_statics (void)
480 struct objc_list
**cell
= &uninitialized_statics
;
481 struct objc_static_instances
**statics_in_module
;
483 objc_mutex_lock (__objc_runtime_mutex
);
487 int module_initialized
= 1;
489 for (statics_in_module
= (*cell
)->head
;
490 *statics_in_module
; statics_in_module
++)
492 struct objc_static_instances
*statics
= *statics_in_module
;
493 Class
class = objc_getClass (statics
->class_name
);
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;
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. */
518 if (strcmp (statics
->class_name
, "Protocol") == 0)
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
524 for (inst
= &statics
->instances
[0]; *inst
; inst
++)
525 __objc_init_protocol ((struct objc_protocol
*)*inst
);
529 /* Other static instances (typically constant
530 strings) are easier as we just fix up their class
532 for (inst
= &statics
->instances
[0]; *inst
; inst
++)
533 (*inst
)->class_pointer
= class;
537 if (module_initialized
)
539 /* Remove this module from the uninitialized list. */
540 struct objc_list
*this = *cell
;
545 cell
= &(*cell
)->tail
;
548 objc_mutex_unlock (__objc_runtime_mutex
);
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. */
556 __objc_exec_class (struct objc_module
*module
)
558 /* Have we processed any constructors previously? This flag is used
559 to indicate that some global data structures need to be
561 static BOOL previous_constructors
= 0;
563 static struct objc_list
*unclaimed_categories
= 0;
565 /* The symbol table (defined in objc-private/module-abi-8.h)
567 struct objc_symtab
*symtab
= module
->symtab
;
569 /* The statics in this module. */
570 struct objc_static_instances
**statics
571 = symtab
->defs
[symtab
->cls_def_cnt
+ symtab
->cat_def_cnt
];
573 /* Entry used to traverse hash lists. */
574 struct objc_list
**cell
;
576 /* The table of selector references for this module. */
577 struct objc_selector
*selectors
= symtab
->refs
;
581 DEBUG_PRINTF ("received module: %s\n", module
->name
);
583 /* Check gcc version. */
584 init_check_module_version (module
);
586 /* On the first call of this routine, initialize some data
588 if (! previous_constructors
)
590 /* Initialize thread-safe system. */
591 __objc_init_thread_system ();
592 __objc_runtime_threads_alive
= 1;
593 __objc_runtime_mutex
= objc_mutex_allocate ();
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
,
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
,
605 __objc_protocols_init ();
606 __objc_accessors_init ();
608 previous_constructors
= 1;
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
);
616 /* Replace referenced selectors from names to SELs. */
618 __objc_register_selectors_from_module (selectors
);
620 /* Parse the classes in the load module and gather selector
622 DEBUG_PRINTF ("gathering selectors from module: %s\n", module
->name
);
623 for (i
= 0; i
< symtab
->cls_def_cnt
; ++i
)
625 Class
class = (Class
) symtab
->defs
[i
];
626 const char *superclass
= (char *) class->super_class
;
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
);
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
;
637 if (__objc_init_class (class))
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
);
646 /* Process category information from the module. */
647 for (i
= 0; i
< symtab
->cat_def_cnt
; ++i
)
649 struct objc_category
*category
= symtab
->defs
[i
+ symtab
->cls_def_cnt
];
650 Class
class = objc_getClass (category
->class_name
);
652 /* If the class for the category exists then append its
656 DEBUG_PRINTF ("processing categories from (module,object): %s, %s\n",
660 /* Do instance methods. */
661 if (category
->instance_methods
)
662 class_add_method_list (class, category
->instance_methods
);
664 /* Do class methods. */
665 if (category
->class_methods
)
666 class_add_method_list ((Class
) class->class_pointer
,
667 category
->class_methods
);
669 if (category
->protocols
)
671 __objc_init_protocols (category
->protocols
);
672 __objc_class_add_protocols (class, category
->protocols
);
675 /* Register the instance methods as class methods, this is
676 only done for root classes. */
677 __objc_register_instance_methods_to_class (class);
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
);
688 uninitialized_statics
= list_cons (statics
, uninitialized_statics
);
689 if (uninitialized_statics
)
690 objc_init_statics ();
692 /* Scan the unclaimed category hash. Attempt to attach any
693 unclaimed categories to objects. */
694 for (cell
= &unclaimed_categories
; *cell
; )
696 struct objc_category
*category
= (*cell
)->head
;
697 Class
class = objc_getClass (category
->class_name
);
701 DEBUG_PRINTF ("attaching stored categories to object: %s\n",
704 list_remove_head (cell
);
706 if (category
->instance_methods
)
707 class_add_method_list (class, category
->instance_methods
);
709 if (category
->class_methods
)
710 class_add_method_list ((Class
) class->class_pointer
,
711 category
->class_methods
);
713 if (category
->protocols
)
715 __objc_init_protocols (category
->protocols
);
716 __objc_class_add_protocols (class, category
->protocols
);
719 /* Register the instance methods as class methods, this is
720 only done for root classes. */
721 __objc_register_instance_methods_to_class (class);
724 cell
= &(*cell
)->tail
;
727 if (unclaimed_proto_list
&& objc_getClass ("Protocol"))
729 list_mapcar (unclaimed_proto_list
,
730 (void (*) (void *))__objc_init_protocols
);
731 list_free (unclaimed_proto_list
);
732 unclaimed_proto_list
= 0;
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 ();
745 objc_mutex_unlock (__objc_runtime_mutex
);
748 /* This function needs to be called with the objc_runtime_mutex
751 objc_send_load (void)
753 if (!__objc_module_list
)
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
)
762 Class
class = unresolved_classes
->head
;
764 while (objc_getClass ((char *) class->super_class
))
766 list_remove_head (&unresolved_classes
);
767 if (unresolved_classes
)
768 class = unresolved_classes
->head
;
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
)
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"))
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
);
791 while (__objc_class_tree_list
)
794 objc_preorder_traverse (__objc_class_tree_list
->head
,
795 0, __objc_tree_print
);
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
);
804 /* For each module, call the _objc_load_callback if any is
806 list_mapcar (__objc_module_list
, (void (*) (void *)) __objc_call_load_callback
);
808 /* Empty the list of modules. */
809 list_free (__objc_module_list
);
810 __objc_module_list
= NULL
;
814 __objc_create_classes_tree (struct objc_module
*module
)
816 /* The runtime mutex is locked at this point */
817 struct objc_symtab
*symtab
= module
->symtab
;
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
++)
824 Class
class = (Class
) symtab
->defs
[i
];
826 if (!objc_hash_is_key_in_hash (duplicate_classes
, class))
827 objc_tree_insert_class (class);
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
)
840 struct objc_category
*category
= symtab
->defs
[i
+ symtab
->cls_def_cnt
];
841 Class
class = objc_getClass (category
->class_name
);
843 /* If the class for the category exists then append its
846 objc_tree_insert_class (class);
851 __objc_call_load_callback (struct objc_module
*module
)
853 if (_objc_load_callback
)
855 /* The runtime mutex is locked at this point. */
856 struct objc_symtab
*symtab
= module
->symtab
;
859 /* Iterate thru classes defined in this module and call the callback
861 for (i
= 0; i
< symtab
->cls_def_cnt
; i
++)
863 Class
class = (Class
) symtab
->defs
[i
];
865 if (!objc_hash_is_key_in_hash (duplicate_classes
, class))
867 /* Call the _objc_load_callback for this class. */
868 _objc_load_callback (class, 0);
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
++)
877 struct objc_category
*category
= symtab
->defs
[i
+ symtab
->cls_def_cnt
];
878 Class
class = objc_getClass (category
->class_name
);
880 _objc_load_callback (class, category
);
885 /* Sanity check the version of gcc used to compile `module'. */
887 init_check_module_version (struct objc_module
*module
)
889 if ((module
->version
!= OBJC_VERSION
) || (module
->size
!= sizeof (struct objc_module
)))
891 _objc_abort ("Module %s version %d doesn't match runtime %d\n",
892 module
->name
, (int)module
->version
, OBJC_VERSION
);
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
901 __objc_init_class (Class
class)
903 /* Store the class in the class table and assign class numbers. */
904 if (__objc_add_class_to_hash (class))
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
);
910 /* Install the fake dispatch tables. */
911 __objc_install_premature_dtable (class);
912 __objc_install_premature_dtable (class->class_pointer
);
914 /* Register the instance methods as class methods, this is only
915 done for root classes. */
916 __objc_register_instance_methods_to_class (class);
918 if (class->protocols
)
919 __objc_init_protocols (class->protocols
);
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);
932 /* __objc_init_protocol must be called with __objc_runtime_mutex
933 already locked, and the "Protocol" class already registered. */
935 __objc_init_protocol (struct objc_protocol
*protocol
)
937 static Class proto_class
= 0;
940 proto_class
= objc_getClass ("Protocol");
942 if (((size_t)protocol
->class_pointer
) == PROTOCOL_VERSION
)
944 /* Assign class pointer. */
945 protocol
->class_pointer
= proto_class
;
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
953 if (protocol
->class_methods
)
954 __objc_register_selectors_from_description_list (protocol
->class_methods
);
956 if (protocol
->instance_methods
)
957 __objc_register_selectors_from_description_list (protocol
->instance_methods
);
959 /* Register the protocol in the hashtable or protocols by
961 __objc_protocols_add_protocol (protocol
->protocol_name
, protocol
);
963 /* Init super protocols. */
964 __objc_init_protocols (protocol
->protocol_list
);
966 else if (protocol
->class_pointer
!= proto_class
)
968 _objc_abort ("Version %d doesn't match runtime protocol version %d\n",
969 (int) ((char *) protocol
->class_pointer
976 __objc_init_protocols (struct objc_protocol_list
*protos
)
979 static Class proto_class
= 0;
984 objc_mutex_lock (__objc_runtime_mutex
);
987 proto_class
= objc_getClass ("Protocol");
991 unclaimed_proto_list
= list_cons (protos
, unclaimed_proto_list
);
992 objc_mutex_unlock (__objc_runtime_mutex
);
997 assert (protos
->next
== 0); /* Only single ones allowed. */
1000 for (i
= 0; i
< protos
->count
; i
++)
1002 struct objc_protocol
*aProto
= protos
->list
[i
];
1003 __objc_init_protocol (aProto
);
1006 objc_mutex_unlock (__objc_runtime_mutex
);
1010 __objc_class_add_protocols (Class
class, struct objc_protocol_list
*protos
)
1015 protos
->next
= class->protocols
;
1016 class->protocols
= protos
;