1 /* GNU Objective C Runtime initialization
2 Copyright (C) 1993-2019 Free Software Foundation, Inc.
3 Contributed by Kresten Krab Thorup
4 +load support contributed by Ovidiu Predescu <ovidiu@net-community.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 3, or (at your option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* Uncommented the following line to enable debug logging. Use this
27 only while debugging the runtime. */
30 #include "objc-private/common.h"
31 #include "objc-private/error.h"
32 #include "objc/runtime.h"
34 #include "objc-private/hash.h"
35 #include "objc-private/objc-list.h"
36 #include "objc-private/module-abi-8.h"
37 #include "objc-private/runtime.h" /* For __objc_resolve_class_links(). */
38 #include "objc-private/selector.h" /* For __sel_register_typed_name(). */
39 #include "objc-private/objc-sync.h" /* For __objc_sync_init() */
40 #include "objc-private/protocols.h" /* For __objc_protocols_init(),
41 __objc_protocols_add_protocol()
42 __objc_protocols_register_selectors() */
43 #include "objc-private/accessors.h" /* For __objc_accessors_init() */
45 /* The version number of this runtime. This must match the number
46 defined in gcc (objc-act.c). */
47 #define OBJC_VERSION 8
48 #define PROTOCOL_VERSION 2
50 /* This list contains modules currently loaded into the runtime and
51 for which the +load method (and the load callback, if any) has not
53 static struct objc_list
*__objc_module_list
= 0; /* !T:MUTEX */
55 /* This list contains all proto_list's not yet assigned class
57 static struct objc_list
*unclaimed_proto_list
= 0; /* !T:MUTEX */
59 /* List of unresolved static instances. */
60 static struct objc_list
*uninitialized_statics
= 0; /* !T:MUTEX */
62 /* List of duplicated classes found while loading modules. If we find
63 a class twice, we ignore it the second time. On some platforms,
64 where the order in which modules are loaded is well defined, this
65 allows you to replace a class in a shared library by linking in a
66 new implementation which is loaded in in the right order, and which
67 overrides the existing one.
69 Protected by __objc_runtime_mutex. */
70 static cache_ptr duplicate_classes
= NULL
;
72 /* Global runtime "write" mutex. Having a single mutex prevents
73 deadlocks, but reduces concurrency. To improve concurrency, some
74 groups of functions in the runtime have their own separate mutex
75 (eg, __class_table_lock in class.c); to avoid deadlocks, these
76 routines must make sure that they never acquire any other lock
77 while holding their own local lock. Ie, they should lock, execute
78 some C code that does not perform any calls to other runtime
79 functions which may potentially lock different locks, then unlock.
80 If they need to perform any calls to other runtime functions that
81 may potentially lock other locks, then they should use the global
82 __objc_runtime_mutex. */
83 objc_mutex_t __objc_runtime_mutex
= 0;
85 /* Number of threads that are alive. */
86 int __objc_runtime_threads_alive
= 1; /* !T:MUTEX */
88 /* Check compiler vs runtime version. */
89 static void init_check_module_version (struct objc_module
*);
91 /* Assign isa links to protos. */
92 static void __objc_init_protocols (struct objc_protocol_list
*protos
);
94 /* Assign isa link to a protocol, and register it. */
95 static void __objc_init_protocol (struct objc_protocol
*protocol
);
97 /* Add protocol to class. */
98 static void __objc_class_add_protocols (Class
, struct objc_protocol_list
*);
100 /* Load callback hook. */
101 void (*_objc_load_callback
) (Class
class, struct objc_category
*category
) = 0; /* !T:SAFE */
103 /* Are all categories/classes resolved ? */
104 BOOL __objc_dangling_categories
= NO
; /* !T:UNUSED */
106 /* Sends +load to all classes and categories in certain
108 static void objc_send_load (void);
110 /* Inserts all the classes defined in module in a tree of classes that
111 resembles the class hierarchy. This tree is traversed in preorder
112 and the classes in its nodes receive the +load message if these
113 methods were not executed before. The algorithm ensures that when
114 the +load method of a class is executed all the superclasses have
115 been already received the +load message. */
116 static void __objc_create_classes_tree (struct objc_module
*module
);
118 /* Calls the _objc_load_callback for each class and category in the
119 module (if _objc_load_callback is not NULL). */
120 static void __objc_call_load_callback (struct objc_module
*module
);
122 /* A special version that works only before the classes are completely
123 installed in the runtime. */
124 static BOOL
class_is_subclass_of_class (Class
class, Class superclass
);
126 /* This is a node in the class tree hierarchy used to send +load
128 typedef struct objc_class_tree
130 /* The class corresponding to the node. */
133 /* This is a linked list of all the direct subclasses of this class.
134 'head' points to a subclass node; 'tail' points to the next
135 objc_list node (whose 'head' points to another subclass node,
137 struct objc_list
*subclasses
;
140 /* This is a linked list of objc_class_tree trees. The head of these
141 trees are root classes (their super class is Nil). These different
142 trees represent different class hierarchies. */
143 static struct objc_list
*__objc_class_tree_list
= NULL
;
145 /* Keeps the +load methods who have been already executed. This hash
146 should not be destroyed during the execution of the program. */
147 static cache_ptr __objc_load_methods
= NULL
;
149 /* This function is used when building the class tree used to send
150 ordinately the +load message to all classes needing it. The tree
151 is really needed so that superclasses will get the message before
154 This tree may contain classes which are being loaded (or have just
155 being loaded), and whose super_class pointers have not yet been
156 resolved. This implies that their super_class pointers point to a
157 string with the name of the superclass; when the first message is
158 sent to the class (/an object of that class) the class links will
159 be resolved, which will replace the super_class pointers with
160 pointers to the actual superclasses.
162 Unfortunately, the tree might also contain classes which had been
163 loaded previously, and whose class links have already been
166 This function returns the superclass of a class in both cases, and
167 can be used to build the determine the class relationships while
168 building the tree. */
169 static Class
class_superclass_of_class (Class
class)
171 char *super_class_name
;
173 /* If the class links have been resolved, use the resolved
175 if (CLS_ISRESOLV (class))
176 return class->super_class
;
178 /* Else, 'class' has not yet been resolved. This means that its
179 super_class pointer is really the name of the super class (rather
180 than a pointer to the actual superclass). */
181 super_class_name
= (char *)class->super_class
;
183 /* Return Nil for a root class. */
184 if (super_class_name
== NULL
)
187 /* Lookup the superclass of non-root classes. */
188 return objc_getClass (super_class_name
);
192 /* Creates a tree of classes whose topmost class is directly inherited
193 from `upper' and the bottom class in this tree is `bottom_class'.
194 If `upper' is Nil, creates a class hierarchy up to a root class.
195 The classes in this tree are super classes of `bottom_class'. The
196 `subclasses' member of each tree node point to the list of
197 subclasses for the node. */
198 static objc_class_tree
*
199 create_tree_of_subclasses_inherited_from (Class bottom_class
, Class upper
)
202 objc_class_tree
*tree
, *prev
;
204 DEBUG_PRINTF ("create_tree_of_subclasses_inherited_from:");
205 DEBUG_PRINTF (" bottom_class = %s, upper = %s\n",
206 (bottom_class
? bottom_class
->name
: NULL
),
207 (upper
? upper
->name
: NULL
));
209 superclass
= class_superclass_of_class (bottom_class
);
211 prev
= objc_calloc (1, sizeof (objc_class_tree
));
212 prev
->class = bottom_class
;
214 if (superclass
== upper
)
217 while (superclass
!= upper
)
219 tree
= objc_calloc (1, sizeof (objc_class_tree
));
220 tree
->class = superclass
;
221 tree
->subclasses
= list_cons (prev
, tree
->subclasses
);
222 superclass
= class_superclass_of_class (superclass
);
229 /* Insert the `class' into the proper place in the `tree' class
230 hierarchy. This function returns a new tree if the class has been
231 successfully inserted into the tree or NULL if the class is not
232 part of the classes hierarchy described by `tree'. This function
233 is private to objc_tree_insert_class (), you should not call it
235 static objc_class_tree
*
236 __objc_tree_insert_class (objc_class_tree
*tree
, Class
class)
238 DEBUG_PRINTF ("__objc_tree_insert_class: tree = %p (root: %s), class = %s\n",
239 tree
, ((tree
&& tree
->class) ? tree
->class->name
: "Nil"), class->name
);
242 return create_tree_of_subclasses_inherited_from (class, NULL
);
243 else if (class == tree
->class)
245 /* `class' has been already inserted. */
246 DEBUG_PRINTF (" 1. class %s was previously inserted\n", class->name
);
249 else if (class_superclass_of_class (class) == tree
->class)
251 /* If class is a direct subclass of tree->class then add class
252 to the list of subclasses. First check to see if it wasn't
254 struct objc_list
*list
= tree
->subclasses
;
255 objc_class_tree
*node
;
259 /* Class has been already inserted; do nothing just return
261 if (((objc_class_tree
*) list
->head
)->class == class)
263 DEBUG_PRINTF (" 2. class %s was previously inserted\n",
270 /* Create a new node class and insert it into the list of
272 node
= objc_calloc (1, sizeof (objc_class_tree
));
274 tree
->subclasses
= list_cons (node
, tree
->subclasses
);
275 DEBUG_PRINTF (" 3. class %s inserted\n", class->name
);
280 /* The class is not a direct subclass of tree->class. Search
281 for class's superclasses in the list of subclasses. */
282 struct objc_list
*subclasses
= tree
->subclasses
;
284 /* Precondition: the class must be a subclass of tree->class;
285 otherwise return NULL to indicate our caller that it must
286 take the next tree. */
287 if (! class_is_subclass_of_class (class, tree
->class))
290 for (; subclasses
!= NULL
; subclasses
= subclasses
->tail
)
292 Class aClass
= ((objc_class_tree
*) (subclasses
->head
))->class;
294 if (class_is_subclass_of_class (class, aClass
))
296 /* If we found one of class's superclasses we insert the
297 class into its subtree and return the original tree
298 since nothing has been changed. */
300 = __objc_tree_insert_class (subclasses
->head
, class);
301 DEBUG_PRINTF (" 4. class %s inserted\n", class->name
);
306 /* We haven't found a subclass of `class' in the `subclasses'
307 list. Create a new tree of classes whose topmost class is a
308 direct subclass of tree->class. */
310 objc_class_tree
*new_tree
311 = create_tree_of_subclasses_inherited_from (class, tree
->class);
312 tree
->subclasses
= list_cons (new_tree
, tree
->subclasses
);
313 DEBUG_PRINTF (" 5. class %s inserted\n", class->name
);
319 /* This function inserts `class' in the right tree hierarchy classes. */
321 objc_tree_insert_class (Class
class)
323 struct objc_list
*list_node
;
324 objc_class_tree
*tree
;
326 list_node
= __objc_class_tree_list
;
329 /* Try to insert the class in this class hierarchy. */
330 tree
= __objc_tree_insert_class (list_node
->head
, class);
333 list_node
->head
= tree
;
337 list_node
= list_node
->tail
;
340 /* If the list was finished but the class hasn't been inserted, we
341 don't have an existing class hierarchy that can accommodate it.
343 __objc_class_tree_list
= list_cons (NULL
, __objc_class_tree_list
);
344 __objc_class_tree_list
->head
= __objc_tree_insert_class (NULL
, class);
347 /* Traverse tree in preorder. Used to send +load. */
349 objc_preorder_traverse (objc_class_tree
*tree
,
351 void (*function
) (objc_class_tree
*, int))
353 struct objc_list
*node
;
355 (*function
) (tree
, level
);
356 for (node
= tree
->subclasses
; node
; node
= node
->tail
)
357 objc_preorder_traverse (node
->head
, level
+ 1, function
);
360 /* Traverse tree in postorder. Used to destroy a tree. */
362 objc_postorder_traverse (objc_class_tree
*tree
,
364 void (*function
) (objc_class_tree
*, int))
366 struct objc_list
*node
;
368 for (node
= tree
->subclasses
; node
; node
= node
->tail
)
369 objc_postorder_traverse (node
->head
, level
+ 1, function
);
370 (*function
) (tree
, level
);
373 /* Used to print a tree class hierarchy. */
376 __objc_tree_print (objc_class_tree
*tree
, int level
)
380 for (i
= 0; i
< level
; i
++)
382 printf ("%s\n", tree
->class->name
);
386 /* Walks on a linked list of methods in the reverse order and executes
387 all the methods corresponding to the `+load' selector. Walking in
388 the reverse order assures the +load of class is executed first and
389 then +load of categories because of the way in which categories are
390 added to the class methods. This function needs to be called with
391 the objc_runtime_mutex locked. */
393 __objc_send_load_using_method_list (struct objc_method_list
*method_list
, Class
class)
395 static SEL load_selector
= 0;
401 /* This needs no lock protection because we are called with the
402 objc_runtime_mutex locked. */
404 load_selector
= sel_registerName ("load");
406 /* method_list is a linked list of method lists; since we're
407 executing in reverse order, we need to do the next list before we
409 __objc_send_load_using_method_list (method_list
->method_next
, class);
411 /* Search the method list. */
412 for (i
= 0; i
< method_list
->method_count
; i
++)
414 struct objc_method
*mth
= &method_list
->method_list
[i
];
416 /* We are searching for +load methods that we haven't executed
418 if (mth
->method_name
&& sel_eq (mth
->method_name
, load_selector
)
419 && ! objc_hash_is_key_in_hash (__objc_load_methods
, mth
->method_imp
))
421 /* Add this method into the +load hash table, so we won't
422 execute it again next time. */
423 objc_hash_add (&__objc_load_methods
,
428 DEBUG_PRINTF (" begin of [%s +load]\n", class->name
);
429 (*mth
->method_imp
) ((id
)class, mth
->method_name
);
430 DEBUG_PRINTF (" end of [%s +load]\n", class->name
);
437 /* This function needs to be called with the objc_runtime_mutex
440 __objc_send_load (objc_class_tree
*tree
,
441 int level
__attribute__ ((__unused__
)))
443 Class
class = tree
->class;
444 struct objc_method_list
*method_list
= class->class_pointer
->methods
;
446 DEBUG_PRINTF ("+load: need to send load to class '%s'\n", class->name
);
447 __objc_send_load_using_method_list (method_list
, class);
451 __objc_destroy_class_tree_node (objc_class_tree
*tree
,
452 int level
__attribute__ ((__unused__
)))
457 /* This is used to check if the relationship between two classes
458 before the runtime completely installs the classes. */
460 class_is_subclass_of_class (Class
class, Class superclass
)
462 for (; class != Nil
;)
464 if (class == superclass
)
466 class = class_superclass_of_class (class);
472 /* This list contains all the classes in the runtime system for whom
473 their superclasses are not yet known to the runtime. */
474 static struct objc_list
*unresolved_classes
= 0;
476 /* Extern function used to reference the Object class. */
477 extern void __objc_force_linking (void);
480 __objc_force_linking (void)
482 extern void __objc_linking (void);
486 /* Run through the statics list, removing modules as soon as all its
487 statics have been initialized. */
489 objc_init_statics (void)
491 struct objc_list
**cell
= &uninitialized_statics
;
492 struct objc_static_instances
**statics_in_module
;
494 objc_mutex_lock (__objc_runtime_mutex
);
498 int module_initialized
= 1;
500 for (statics_in_module
= (*cell
)->head
;
501 *statics_in_module
; statics_in_module
++)
503 struct objc_static_instances
*statics
= *statics_in_module
;
504 Class
class = objc_getClass (statics
->class_name
);
508 /* It is unfortunate that this will cause all the
509 statics initialization to be done again (eg, if we
510 already initialized constant strings, and are now
511 initializing protocols, setting module_initialized to
512 0 would cause constant strings to be initialized
513 again). It would be good to be able to track if we
514 have already initialized some of them. */
515 module_initialized
= 0;
519 /* Note that if this is a list of Protocol objects, some
520 of them may have been initialized already (because
521 they were attached to classes or categories, and the
522 class/category loading code automatically fixes them
523 up), and some of them may not. We really need to go
524 through the whole list to be sure! Protocols are
525 also special because we want to register them and
526 register all their selectors. */
529 if (strcmp (statics
->class_name
, "Protocol") == 0)
531 /* Protocols are special, because not only we want
532 to fix up their class pointers, but we also want
533 to register them and their selectors with the
535 for (inst
= &statics
->instances
[0]; *inst
; inst
++)
536 __objc_init_protocol ((struct objc_protocol
*)*inst
);
540 /* Other static instances (typically constant
541 strings) are easier as we just fix up their class
543 for (inst
= &statics
->instances
[0]; *inst
; inst
++)
544 (*inst
)->class_pointer
= class;
548 if (module_initialized
)
550 /* Remove this module from the uninitialized list. */
551 struct objc_list
*this = *cell
;
556 cell
= &(*cell
)->tail
;
559 objc_mutex_unlock (__objc_runtime_mutex
);
562 /* This function is called by constructor functions generated for each
563 module compiled. (_GLOBAL_$I$...) The purpose of this function is
564 to gather the module pointers so that they may be processed by the
565 initialization routines as soon as possible. */
567 __objc_exec_class (struct objc_module
*module
)
569 /* Have we processed any constructors previously? This flag is used
570 to indicate that some global data structures need to be
572 static BOOL previous_constructors
= 0;
574 static struct objc_list
*unclaimed_categories
= 0;
576 /* The symbol table (defined in objc-private/module-abi-8.h)
578 struct objc_symtab
*symtab
= module
->symtab
;
580 /* The statics in this module. */
581 struct objc_static_instances
**statics
582 = symtab
->defs
[symtab
->cls_def_cnt
+ symtab
->cat_def_cnt
];
584 /* Entry used to traverse hash lists. */
585 struct objc_list
**cell
;
587 /* The table of selector references for this module. */
588 struct objc_selector
*selectors
= symtab
->refs
;
592 DEBUG_PRINTF ("\n__objc_exec_class (%p) - start processing module...\n", module
);
594 /* Check gcc version. */
595 init_check_module_version (module
);
597 /* On the first call of this routine, initialize some data
599 if (! previous_constructors
)
601 /* Initialize thread-safe system. */
602 __objc_init_thread_system ();
603 __objc_runtime_threads_alive
= 1;
604 __objc_runtime_mutex
= objc_mutex_allocate ();
606 __objc_init_selector_tables ();
607 __objc_init_class_tables ();
608 __objc_init_dispatch_tables ();
609 duplicate_classes
= objc_hash_new (8,
610 (hash_func_type
)objc_hash_ptr
,
612 __objc_load_methods
= objc_hash_new (128,
613 (hash_func_type
)objc_hash_ptr
,
615 __objc_protocols_init ();
616 __objc_accessors_init ();
618 previous_constructors
= 1;
621 /* Save the module pointer so that later we remember to call +load
622 on all classes and categories on it. */
623 objc_mutex_lock (__objc_runtime_mutex
);
624 __objc_module_list
= list_cons (module
, __objc_module_list
);
626 /* Replace referenced selectors from names to SELs. */
629 DEBUG_PRINTF (" registering selectors\n");
630 __objc_register_selectors_from_module (selectors
);
633 /* Parse the classes in the load module and gather selector
635 for (i
= 0; i
< symtab
->cls_def_cnt
; ++i
)
637 Class
class = (Class
) symtab
->defs
[i
];
638 const char *superclass
= (char *) class->super_class
;
640 /* Make sure we have what we think. */
641 assert (CLS_ISCLASS (class));
642 assert (CLS_ISMETA (class->class_pointer
));
643 DEBUG_PRINTF (" installing class '%s'\n", class->name
);
645 /* Workaround for a bug in clang: Clang may set flags other than
646 _CLS_CLASS and _CLS_META even when compiling for the
647 traditional ABI (version 8), confusing our runtime. Try to
648 wipe these flags out. */
649 if (CLS_ISCLASS (class))
650 __CLS_INFO (class) = _CLS_CLASS
;
652 __CLS_INFO (class) = _CLS_META
;
654 /* Initialize the subclass list to be NULL. In some cases it
655 isn't and this crashes the program. */
656 class->subclass_list
= NULL
;
658 if (__objc_init_class (class))
660 /* Check to see if the superclass is known in this point. If
661 it's not add the class to the unresolved_classes list. */
662 if (superclass
&& ! objc_getClass (superclass
))
663 unresolved_classes
= list_cons (class, unresolved_classes
);
667 /* Process category information from the module. */
668 for (i
= 0; i
< symtab
->cat_def_cnt
; ++i
)
670 struct objc_category
*category
= symtab
->defs
[i
+ symtab
->cls_def_cnt
];
671 Class
class = objc_getClass (category
->class_name
);
673 /* If the class for the category exists then append its
677 DEBUG_PRINTF (" installing category '%s (%s)'\n", category
->class_name
, category
->category_name
);
678 /* Do instance methods. */
679 if (category
->instance_methods
)
680 class_add_method_list (class, category
->instance_methods
);
682 /* Do class methods. */
683 if (category
->class_methods
)
684 class_add_method_list ((Class
) class->class_pointer
,
685 category
->class_methods
);
687 if (category
->protocols
)
689 __objc_init_protocols (category
->protocols
);
690 __objc_class_add_protocols (class, category
->protocols
);
693 /* Register the instance methods as class methods, this is
694 only done for root classes. */
695 __objc_register_instance_methods_to_class (class);
699 DEBUG_PRINTF (" delaying installation of category '%s (%s)'\n", category
->class_name
, category
->category_name
);
700 /* The object to which the category methods belong can't be
701 found. Save the information. */
702 unclaimed_categories
= list_cons (category
, unclaimed_categories
);
707 uninitialized_statics
= list_cons (statics
, uninitialized_statics
);
708 if (uninitialized_statics
)
709 objc_init_statics ();
711 /* Scan the unclaimed category hash. Attempt to attach any
712 unclaimed categories to objects. */
713 for (cell
= &unclaimed_categories
; *cell
; )
715 struct objc_category
*category
= (*cell
)->head
;
716 Class
class = objc_getClass (category
->class_name
);
720 DEBUG_PRINTF (" installing (delayed) category '%s (%s)'\n", category
->class_name
, category
->category_name
);
721 list_remove_head (cell
);
723 if (category
->instance_methods
)
724 class_add_method_list (class, category
->instance_methods
);
726 if (category
->class_methods
)
727 class_add_method_list ((Class
) class->class_pointer
,
728 category
->class_methods
);
730 if (category
->protocols
)
732 __objc_init_protocols (category
->protocols
);
733 __objc_class_add_protocols (class, category
->protocols
);
736 /* Register the instance methods as class methods, this is
737 only done for root classes. */
738 __objc_register_instance_methods_to_class (class);
741 cell
= &(*cell
)->tail
;
744 if (unclaimed_proto_list
&& objc_getClass ("Protocol"))
746 list_mapcar (unclaimed_proto_list
,
747 (void (*) (void *))__objc_init_protocols
);
748 list_free (unclaimed_proto_list
);
749 unclaimed_proto_list
= 0;
754 /* Check if there are no unresolved classes (ie, classes whose
755 superclass has not been loaded yet) and that the 'Object' class,
756 used as the class of classes, exist. If so, it is worth
757 "resolving the class links" at this point, which will setup all
758 the class/superclass pointers. */
759 if (!unresolved_classes
&& objc_getClass ("Object"))
761 DEBUG_PRINTF (" resolving class links\n");
762 __objc_resolve_class_links ();
765 objc_mutex_unlock (__objc_runtime_mutex
);
767 DEBUG_PRINTF ("__objc_exec_class (%p) - finished processing module...\n\n", module
);
770 /* This function needs to be called with the objc_runtime_mutex
773 objc_send_load (void)
775 if (!__objc_module_list
)
778 /* Try to find out if all the classes loaded so far also have their
779 superclasses known to the runtime. We suppose that the objects
780 that are allocated in the +load method are in general of a class
781 declared in the same module. */
782 if (unresolved_classes
)
784 Class
class = unresolved_classes
->head
;
786 while (objc_getClass ((char *) class->super_class
))
788 list_remove_head (&unresolved_classes
);
789 if (unresolved_classes
)
790 class = unresolved_classes
->head
;
795 /* If we still have classes for whom we don't have yet their
796 super classes known to the runtime we don't send the +load
797 messages (and call the load callback) yet. */
798 if (unresolved_classes
)
802 /* Special check. If 'Object', which is used by meta-classes, has
803 not been loaded yet, delay sending of +load. */
804 if (! objc_getClass ("Object"))
807 /* Iterate over all modules in the __objc_module_list and call on
808 them the __objc_create_classes_tree function. This function
809 creates a tree of classes that resembles the class hierarchy. */
810 list_mapcar (__objc_module_list
,
811 (void (*) (void *)) __objc_create_classes_tree
);
813 while (__objc_class_tree_list
)
816 objc_preorder_traverse (__objc_class_tree_list
->head
,
817 0, __objc_tree_print
);
819 objc_preorder_traverse (__objc_class_tree_list
->head
,
820 0, __objc_send_load
);
821 objc_postorder_traverse (__objc_class_tree_list
->head
,
822 0, __objc_destroy_class_tree_node
);
823 list_remove_head (&__objc_class_tree_list
);
826 /* For each module, call the _objc_load_callback if any is
828 list_mapcar (__objc_module_list
, (void (*) (void *)) __objc_call_load_callback
);
830 /* Empty the list of modules. */
831 list_free (__objc_module_list
);
832 __objc_module_list
= NULL
;
836 __objc_create_classes_tree (struct objc_module
*module
)
838 /* The runtime mutex is locked at this point */
839 struct objc_symtab
*symtab
= module
->symtab
;
842 /* Iterate through classes defined in this module and insert them in
843 the classes tree hierarchy. */
844 for (i
= 0; i
< symtab
->cls_def_cnt
; i
++)
846 Class
class = (Class
) symtab
->defs
[i
];
848 if (!objc_hash_is_key_in_hash (duplicate_classes
, class))
849 objc_tree_insert_class (class);
852 /* Now iterate over "claimed" categories too (ie, categories that
853 extend a class that has already been loaded by the runtime), and
854 insert them in the classes tree hiearchy too. Otherwise, if you
855 add a category, its +load method would not be called if the class
856 is already loaded in the runtime. It the category is
857 "unclaimed", ie, we haven't loaded the main class yet, postpone
858 sending +load as we want to execute +load from the class before
859 we execute the one from the category. */
860 for (i
= 0; i
< symtab
->cat_def_cnt
; ++i
)
862 struct objc_category
*category
= symtab
->defs
[i
+ symtab
->cls_def_cnt
];
863 Class
class = objc_getClass (category
->class_name
);
865 /* If the class for the category exists then append its
868 objc_tree_insert_class (class);
873 __objc_call_load_callback (struct objc_module
*module
)
875 if (_objc_load_callback
)
877 /* The runtime mutex is locked at this point. */
878 struct objc_symtab
*symtab
= module
->symtab
;
881 /* Iterate through classes defined in this module and call the callback
883 for (i
= 0; i
< symtab
->cls_def_cnt
; i
++)
885 Class
class = (Class
) symtab
->defs
[i
];
887 if (!objc_hash_is_key_in_hash (duplicate_classes
, class))
889 /* Call the _objc_load_callback for this class. */
890 DEBUG_PRINTF (" calling the load callback for class '%s'\n", class->name
);
891 _objc_load_callback (class, 0);
895 /* Call the _objc_load_callback for categories. Don't register
896 the instance methods as class methods for categories to root
897 classes since they were already added in the class. */
898 for (i
= 0; i
< symtab
->cat_def_cnt
; i
++)
900 struct objc_category
*category
= symtab
->defs
[i
+ symtab
->cls_def_cnt
];
901 Class
class = objc_getClass (category
->class_name
);
903 DEBUG_PRINTF (" calling the load callback for category '%s (%s)'\n",
904 category
->class_name
, category
->category_name
);
905 _objc_load_callback (class, category
);
910 /* Sanity check the version of gcc used to compile `module'. */
912 init_check_module_version (struct objc_module
*module
)
914 if ((module
->version
!= OBJC_VERSION
) || (module
->size
!= sizeof (struct objc_module
)))
916 _objc_abort ("Module %s version %d doesn't match runtime %d\n",
917 module
->name
, (int)module
->version
, OBJC_VERSION
);
921 /* __objc_init_class must be called with __objc_runtime_mutex already
922 locked. Return YES if the class could be setup; return NO if the
923 class could not be setup because a class with the same name already
926 __objc_init_class (Class
class)
928 /* Store the class in the class table and assign class numbers. */
929 if (__objc_add_class_to_hash (class))
931 /* Register all of the selectors in the class and meta class. */
932 __objc_register_selectors_from_class (class);
933 __objc_register_selectors_from_class ((Class
) class->class_pointer
);
935 /* Install the fake dispatch tables. */
936 __objc_install_premature_dtable (class);
937 __objc_install_premature_dtable (class->class_pointer
);
939 /* Register the instance methods as class methods, this is only
940 done for root classes. */
941 __objc_register_instance_methods_to_class (class);
943 if (class->protocols
)
944 __objc_init_protocols (class->protocols
);
950 /* The module contains a duplicate class. Remember it so that
951 we will ignore it later. */
952 DEBUG_PRINTF (" duplicate class '%s' - will be ignored\n", class->name
);
953 objc_hash_add (&duplicate_classes
, class, class);
958 /* __objc_init_protocol must be called with __objc_runtime_mutex
959 already locked, and the "Protocol" class already registered. */
961 __objc_init_protocol (struct objc_protocol
*protocol
)
963 static Class proto_class
= 0;
966 proto_class
= objc_getClass ("Protocol");
968 if (((size_t)protocol
->class_pointer
) == PROTOCOL_VERSION
)
970 /* Assign class pointer. */
971 protocol
->class_pointer
= proto_class
;
973 /* Register all the selectors in the protocol with the runtime.
974 This both registers the selectors with the right types, and
975 it also fixes up the 'struct objc_method' structures inside
976 the protocol so that each method_name (a char * as compiled
977 by the compiler) is replaced with the appropriate runtime
979 if (protocol
->class_methods
)
980 __objc_register_selectors_from_description_list (protocol
->class_methods
);
982 if (protocol
->instance_methods
)
983 __objc_register_selectors_from_description_list (protocol
->instance_methods
);
985 /* Register the protocol in the hashtable or protocols by
987 __objc_protocols_add_protocol (protocol
->protocol_name
, protocol
);
989 /* Init super protocols. */
990 __objc_init_protocols (protocol
->protocol_list
);
992 else if (protocol
->class_pointer
!= proto_class
)
994 _objc_abort ("Version %d doesn't match runtime protocol version %d\n",
995 (int) ((char *) protocol
->class_pointer
1002 __objc_init_protocols (struct objc_protocol_list
*protos
)
1005 static Class proto_class
= 0;
1010 objc_mutex_lock (__objc_runtime_mutex
);
1013 proto_class
= objc_getClass ("Protocol");
1017 unclaimed_proto_list
= list_cons (protos
, unclaimed_proto_list
);
1018 objc_mutex_unlock (__objc_runtime_mutex
);
1023 assert (protos
->next
== 0); /* Only single ones allowed. */
1026 for (i
= 0; i
< protos
->count
; i
++)
1028 struct objc_protocol
*aProto
= protos
->list
[i
];
1029 __objc_init_protocol (aProto
);
1032 objc_mutex_unlock (__objc_runtime_mutex
);
1036 __objc_class_add_protocols (Class
class, struct objc_protocol_list
*protos
)
1041 protos
->next
= class->protocols
;
1042 class->protocols
= protos
;