1 /* GNU Objective C Runtime initialization
2 Copyright (C) 1993, 1995, 1996, 1997, 2002, 2009
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/objc-api.h"
31 #include "objc-private/hash.h"
32 #include "objc-private/objc-list.h"
33 #include "objc-private/runtime.h"
34 #include "objc-private/objc-sync.h" /* For __objc_sync_init() */
35 #include "objc-private/protocols.h" /* For __objc_protocols_init(),
36 __objc_protocols_add_protocol()
37 __objc_protocols_register_selectors() */
39 /* The version number of this runtime. This must match the number
40 defined in gcc (objc-act.c). */
41 #define OBJC_VERSION 8
42 #define PROTOCOL_VERSION 2
44 /* This list contains all modules currently loaded into the runtime. */
45 static struct objc_list
*__objc_module_list
= 0; /* !T:MUTEX */
47 /* This list contains all proto_list's not yet assigned class links. */
48 static struct objc_list
*unclaimed_proto_list
= 0; /* !T:MUTEX */
50 /* List of unresolved static instances. */
51 static struct objc_list
*uninitialized_statics
= 0; /* !T:MUTEX */
53 /* Global runtime "write" mutex. Having a single mutex prevents
54 deadlocks, but reduces concurrency. To improve concurrency, some
55 groups of functions in the runtime have their own separate mutex
56 (eg, __class_table_lock in class.c); to avoid deadlocks, these
57 routines must make sure that they never acquire any other lock
58 while holding their own local lock. Ie, they should lock, execute
59 some C code that does not perform any calls to other runtime
60 functions which may potentially lock different locks, then unlock.
61 If they need to perform any calls to other runtime functions that
62 may potentially lock other locks, then they should use the global
63 __objc_runtime_mutex. */
64 objc_mutex_t __objc_runtime_mutex
= 0;
66 /* Number of threads that are alive. */
67 int __objc_runtime_threads_alive
= 1; /* !T:MUTEX */
69 /* Check compiler vs runtime version. */
70 static void init_check_module_version (Module_t
);
72 /* Assign isa links to protos. */
73 static void __objc_init_protocols (struct objc_protocol_list
*protos
);
75 /* Assign isa link to a protocol, and register it. */
76 static void __objc_init_protocol (struct objc_protocol
*protocol
);
78 /* Add protocol to class. */
79 static void __objc_class_add_protocols (Class
, struct objc_protocol_list
*);
81 /* This is a hook which is called by __objc_exec_class every time a
82 class or a category is loaded into the runtime. This may e.g. help
83 a dynamic loader determine the classes that have been loaded when
84 an object file is dynamically linked in. */
85 void (*_objc_load_callback
) (Class
class, Category
*category
); /* !T:SAFE */
87 /* Is all categories/classes resolved? */
88 BOOL __objc_dangling_categories
= NO
; /* !T:UNUSED */
91 __sel_register_typed_name (const char *name
, const char *types
,
92 struct objc_selector
*orig
, BOOL is_const
);
94 /* Sends +load to all classes and categories in certain situations. */
95 static void objc_send_load (void);
97 /* Inserts all the classes defined in module in a tree of classes that
98 resembles the class hierarchy. This tree is traversed in preorder
99 and the classes in its nodes receive the +load message if these
100 methods were not executed before. The algorithm ensures that when
101 the +load method of a class is executed all the superclasses have
102 been already received the +load message. */
103 static void __objc_create_classes_tree (Module_t module
);
105 static void __objc_call_callback (Module_t module
);
107 /* A special version that works only before the classes are completely
108 installed in the runtime. */
109 static BOOL
class_is_subclass_of_class (Class
class, Class superclass
);
111 typedef struct objc_class_tree
{
113 struct objc_list
*subclasses
; /* `head' is pointer to an objc_class_tree */
116 /* This is a linked list of objc_class_tree trees. The head of these
117 trees are root classes (their super class is Nil). These different
118 trees represent different class hierarchies. */
119 static struct objc_list
*__objc_class_tree_list
= NULL
;
121 /* Keeps the +load methods who have been already executed. This hash
122 should not be destroyed during the execution of the program. */
123 static cache_ptr __objc_load_methods
= NULL
;
125 /* This function is used when building the class tree used to send
126 ordinately the +load message to all classes needing it. The tree
127 is really needed so that superclasses will get the message before
130 This tree will contain classes which are being loaded (or have just
131 being loaded), and whose super_class pointers have not yet been
132 resolved. This implies that their super_class pointers point to a
133 string with the name of the superclass; when the first message is
134 sent to the class (/an object of that class) the class links will
135 be resolved, which will replace the super_class pointers with
136 pointers to the actual superclasses.
138 Unfortunately, the tree might also contain classes which had been
139 loaded previously, and whose class links have already been
142 This function returns the superclass of a class in both cases, and
143 can be used to build the determine the class relationships while
146 static Class
class_superclass_of_class (Class
class)
148 char *super_class_name
;
150 /* If the class links have been resolved, use the resolved
152 if (CLS_ISRESOLV (class))
153 return class->super_class
;
155 /* Else, 'class' has not yet been resolved. This means that its
156 * super_class pointer is really the name of the super class (rather
157 * than a pointer to the actual superclass). */
158 super_class_name
= (char *)class->super_class
;
160 /* Return Nil for a root class. */
161 if (super_class_name
== NULL
)
164 /* Lookup the superclass of non-root classes. */
165 return objc_lookup_class (super_class_name
);
169 /* Creates a tree of classes whose topmost class is directly inherited
170 from `upper' and the bottom class in this tree is
171 `bottom_class'. The classes in this tree are super classes of
172 `bottom_class'. `subclasses' member of each tree node point to the
173 next subclass tree node. */
175 static objc_class_tree
*
176 create_tree_of_subclasses_inherited_from (Class bottom_class
, Class upper
)
178 Class superclass
= bottom_class
->super_class
?
179 objc_lookup_class ((char *) bottom_class
->super_class
)
182 objc_class_tree
*tree
, *prev
;
184 DEBUG_PRINTF ("create_tree_of_subclasses_inherited_from:");
185 DEBUG_PRINTF ("bottom_class = %s, upper = %s\n",
186 (bottom_class
? bottom_class
->name
: NULL
),
187 (upper
? upper
->name
: NULL
));
189 tree
= prev
= objc_calloc (1, sizeof (objc_class_tree
));
190 prev
->class = bottom_class
;
192 while (superclass
!= upper
)
194 tree
= objc_calloc (1, sizeof (objc_class_tree
));
195 tree
->class = superclass
;
196 tree
->subclasses
= list_cons (prev
, tree
->subclasses
);
197 superclass
= class_superclass_of_class (superclass
);
204 /* Insert the `class' into the proper place in the `tree' class
205 hierarchy. This function returns a new tree if the class has been
206 successfully inserted into the tree or NULL if the class is not
207 part of the classes hierarchy described by `tree'. This function is
208 private to objc_tree_insert_class (), you should not call it
211 static objc_class_tree
*
212 __objc_tree_insert_class (objc_class_tree
*tree
, Class
class)
214 DEBUG_PRINTF ("__objc_tree_insert_class: tree = %x, class = %s\n",
218 return create_tree_of_subclasses_inherited_from (class, NULL
);
219 else if (class == tree
->class)
221 /* `class' has been already inserted */
222 DEBUG_PRINTF ("1. class %s was previously inserted\n", class->name
);
225 else if (class_superclass_of_class (class) == tree
->class)
227 /* If class is a direct subclass of tree->class then add class to the
228 list of subclasses. First check to see if it wasn't already
230 struct objc_list
*list
= tree
->subclasses
;
231 objc_class_tree
*node
;
235 /* Class has been already inserted; do nothing just return
237 if (((objc_class_tree
*) list
->head
)->class == class)
239 DEBUG_PRINTF ("2. class %s was previously inserted\n",
246 /* Create a new node class and insert it into the list of subclasses */
247 node
= objc_calloc (1, sizeof (objc_class_tree
));
249 tree
->subclasses
= list_cons (node
, tree
->subclasses
);
250 DEBUG_PRINTF ("3. class %s inserted\n", class->name
);
255 /* The class is not a direct subclass of tree->class. Search for
256 class's superclasses in the list of subclasses. */
257 struct objc_list
*subclasses
= tree
->subclasses
;
259 /* Precondition: the class must be a subclass of tree->class;
260 otherwise return NULL to indicate our caller that it must
261 take the next tree. */
262 if (! class_is_subclass_of_class (class, tree
->class))
265 for (; subclasses
!= NULL
; subclasses
= subclasses
->tail
)
267 Class aClass
= ((objc_class_tree
*) (subclasses
->head
))->class;
269 if (class_is_subclass_of_class (class, aClass
))
271 /* If we found one of class's superclasses we insert the
272 class into its subtree and return the original tree
273 since nothing has been changed. */
275 = __objc_tree_insert_class (subclasses
->head
, class);
276 DEBUG_PRINTF ("4. class %s inserted\n", class->name
);
281 /* We haven't found a subclass of `class' in the `subclasses'
282 list. Create a new tree of classes whose topmost class is a
283 direct subclass of tree->class. */
285 objc_class_tree
*new_tree
286 = create_tree_of_subclasses_inherited_from (class, tree
->class);
287 tree
->subclasses
= list_cons (new_tree
, tree
->subclasses
);
288 DEBUG_PRINTF ("5. class %s inserted\n", class->name
);
294 /* This function inserts `class' in the right tree hierarchy classes. */
297 objc_tree_insert_class (Class
class)
299 struct objc_list
*list_node
;
300 objc_class_tree
*tree
;
302 list_node
= __objc_class_tree_list
;
305 tree
= __objc_tree_insert_class (list_node
->head
, class);
308 list_node
->head
= tree
;
312 list_node
= list_node
->tail
;
315 /* If the list was finished but the class hasn't been inserted,
319 __objc_class_tree_list
= list_cons (NULL
, __objc_class_tree_list
);
320 __objc_class_tree_list
->head
= __objc_tree_insert_class (NULL
, class);
324 /* Traverse tree in preorder. Used to send +load. */
327 objc_preorder_traverse (objc_class_tree
*tree
,
329 void (*function
) (objc_class_tree
*, int))
331 struct objc_list
*node
;
333 (*function
) (tree
, level
);
334 for (node
= tree
->subclasses
; node
; node
= node
->tail
)
335 objc_preorder_traverse (node
->head
, level
+ 1, function
);
338 /* Traverse tree in postorder. Used to destroy a tree. */
341 objc_postorder_traverse (objc_class_tree
*tree
,
343 void (*function
) (objc_class_tree
*, int))
345 struct objc_list
*node
;
347 for (node
= tree
->subclasses
; node
; node
= node
->tail
)
348 objc_postorder_traverse (node
->head
, level
+ 1, function
);
349 (*function
) (tree
, level
);
352 /* Used to print a tree class hierarchy. */
356 __objc_tree_print (objc_class_tree
*tree
, int level
)
360 for (i
= 0; i
< level
; i
++)
362 printf ("%s\n", tree
->class->name
);
366 /* Walks on a linked list of methods in the reverse order and executes
367 all the methods corresponding to `op' selector. Walking in the
368 reverse order assures the +load of class is executed first and then
369 +load of categories because of the way in which categories are
370 added to the class methods. */
373 __objc_send_message_in_list (MethodList_t method_list
, Class
class, SEL op
)
380 /* First execute the `op' message in the following method lists */
381 __objc_send_message_in_list (method_list
->method_next
, class, op
);
383 /* Search the method list. */
384 for (i
= 0; i
< method_list
->method_count
; i
++)
386 Method_t mth
= &method_list
->method_list
[i
];
388 if (mth
->method_name
&& sel_eq (mth
->method_name
, op
)
389 && ! objc_hash_is_key_in_hash (__objc_load_methods
, mth
->method_imp
))
391 /* Add this method into the +load hash table */
392 objc_hash_add (&__objc_load_methods
,
396 DEBUG_PRINTF ("sending +load in class: %s\n", class->name
);
398 /* The method was found and wasn't previously executed. */
399 (*mth
->method_imp
) ((id
)class, mth
->method_name
);
407 __objc_send_load (objc_class_tree
*tree
,
408 int level
__attribute__ ((__unused__
)))
410 static SEL load_sel
= 0;
411 Class
class = tree
->class;
412 MethodList_t method_list
= class->class_pointer
->methods
;
415 load_sel
= sel_register_name ("load");
417 __objc_send_message_in_list (method_list
, class, load_sel
);
421 __objc_destroy_class_tree_node (objc_class_tree
*tree
,
422 int level
__attribute__ ((__unused__
)))
427 /* This is used to check if the relationship between two classes
428 before the runtime completely installs the classes. */
431 class_is_subclass_of_class (Class
class, Class superclass
)
433 for (; class != Nil
;)
435 if (class == superclass
)
437 class = class_superclass_of_class (class);
443 /* This list contains all the classes in the runtime system for whom
444 their superclasses are not yet known to the runtime. */
445 static struct objc_list
*unresolved_classes
= 0;
447 /* Extern function used to reference the Object and NXConstantString
450 extern void __objc_force_linking (void);
453 __objc_force_linking (void)
455 extern void __objc_linking (void);
459 /* Run through the statics list, removing modules as soon as all its
460 statics have been initialized. */
463 objc_init_statics (void)
465 struct objc_list
**cell
= &uninitialized_statics
;
466 struct objc_static_instances
**statics_in_module
;
468 objc_mutex_lock (__objc_runtime_mutex
);
472 int module_initialized
= 1;
474 for (statics_in_module
= (*cell
)->head
;
475 *statics_in_module
; statics_in_module
++)
477 struct objc_static_instances
*statics
= *statics_in_module
;
478 Class
class = objc_lookup_class (statics
->class_name
);
482 /* It is unfortunate that this will cause all the
483 statics initialization to be done again (eg, if we
484 already initialized constant strings, and are now
485 initializing protocols, setting module_initialized to
486 0 would cause constant strings to be initialized
487 again). It would be good to be able to track if we
488 have already initialized some of them. */
489 module_initialized
= 0;
493 /* Note that if this is a list of Protocol objects, some
494 of them may have been initialized already (because
495 they were attached to classes or categories, and the
496 class/category loading code automatically fixes them
497 up), and some of them may not. We really need to go
498 through the whole list to be sure! Protocols are
499 also special because we want to register them and
500 register all their selectors. */
503 if (strcmp (statics
->class_name
, "Protocol") == 0)
505 /* Protocols are special, because not only we want
506 to fix up their class pointers, but we also want
507 to register them and their selectors with the
509 for (inst
= &statics
->instances
[0]; *inst
; inst
++)
510 __objc_init_protocol ((struct objc_protocol
*)*inst
);
514 /* Other static instances (typically constant strings) are
515 easier as we just fix up their class pointers. */
516 for (inst
= &statics
->instances
[0]; *inst
; inst
++)
517 (*inst
)->class_pointer
= class;
521 if (module_initialized
)
523 /* Remove this module from the uninitialized list. */
524 struct objc_list
*this = *cell
;
529 cell
= &(*cell
)->tail
;
532 objc_mutex_unlock (__objc_runtime_mutex
);
533 } /* objc_init_statics */
535 /* This function is called by constructor functions generated for each
536 module compiled. (_GLOBAL_$I$...) The purpose of this function is
537 to gather the module pointers so that they may be processed by the
538 initialization routines as soon as possible. */
541 __objc_exec_class (Module_t module
)
543 /* Have we processed any constructors previously? This flag is used to
544 indicate that some global data structures need to be built. */
545 static BOOL previous_constructors
= 0;
547 static struct objc_list
*unclaimed_categories
= 0;
549 /* The symbol table (defined in objc-api.h) generated by gcc */
550 Symtab_t symtab
= module
->symtab
;
552 /* The statics in this module */
553 struct objc_static_instances
**statics
554 = symtab
->defs
[symtab
->cls_def_cnt
+ symtab
->cat_def_cnt
];
556 /* Entry used to traverse hash lists */
557 struct objc_list
**cell
;
559 /* The table of selector references for this module */
560 SEL selectors
= symtab
->refs
;
565 DEBUG_PRINTF ("received module: %s\n", module
->name
);
567 /* check gcc version */
568 init_check_module_version (module
);
570 /* On the first call of this routine, initialize some data structures. */
571 if (! previous_constructors
)
573 /* Initialize thread-safe system */
574 __objc_init_thread_system ();
575 __objc_runtime_threads_alive
= 1;
576 __objc_runtime_mutex
= objc_mutex_allocate ();
578 __objc_init_selector_tables ();
579 __objc_init_class_tables ();
580 __objc_init_dispatch_tables ();
581 __objc_class_tree_list
= list_cons (NULL
, __objc_class_tree_list
);
582 __objc_load_methods
= objc_hash_new (128,
583 (hash_func_type
)objc_hash_ptr
,
585 __objc_protocols_init ();
587 previous_constructors
= 1;
590 /* Save the module pointer for later processing. (not currently used) */
591 objc_mutex_lock (__objc_runtime_mutex
);
592 __objc_module_list
= list_cons (module
, __objc_module_list
);
594 /* Replace referenced selectors from names to SEL's. */
597 for (i
= 0; selectors
[i
].sel_id
; ++i
)
599 const char *name
, *type
;
600 name
= (char *) selectors
[i
].sel_id
;
601 type
= (char *) selectors
[i
].sel_types
;
602 /* Constructors are constant static data so we can safely store
603 pointers to them in the runtime structures. is_const == YES */
604 __sel_register_typed_name (name
, type
,
605 (struct objc_selector
*) &(selectors
[i
]),
610 /* Parse the classes in the load module and gather selector information. */
611 DEBUG_PRINTF ("gathering selectors from module: %s\n", module
->name
);
612 for (i
= 0; i
< symtab
->cls_def_cnt
; ++i
)
614 Class
class = (Class
) symtab
->defs
[i
];
615 const char *superclass
= (char *) class->super_class
;
617 /* Make sure we have what we think. */
618 assert (CLS_ISCLASS (class));
619 assert (CLS_ISMETA (class->class_pointer
));
620 DEBUG_PRINTF ("phase 1, processing class: %s\n", class->name
);
622 /* Initialize the subclass list to be NULL.
623 In some cases it isn't and this crashes the program. */
624 class->subclass_list
= NULL
;
626 __objc_init_class (class);
628 /* Check to see if the superclass is known in this point. If it's not
629 add the class to the unresolved_classes list. */
630 if (superclass
&& ! objc_lookup_class (superclass
))
631 unresolved_classes
= list_cons (class, unresolved_classes
);
634 /* Process category information from the module. */
635 for (i
= 0; i
< symtab
->cat_def_cnt
; ++i
)
637 Category_t category
= symtab
->defs
[i
+ symtab
->cls_def_cnt
];
638 Class
class = objc_lookup_class (category
->class_name
);
640 /* If the class for the category exists then append its methods. */
644 DEBUG_PRINTF ("processing categories from (module,object): %s, %s\n",
648 /* Do instance methods. */
649 if (category
->instance_methods
)
650 class_add_method_list (class, category
->instance_methods
);
652 /* Do class methods. */
653 if (category
->class_methods
)
654 class_add_method_list ((Class
) class->class_pointer
,
655 category
->class_methods
);
657 if (category
->protocols
)
659 __objc_init_protocols (category
->protocols
);
660 __objc_class_add_protocols (class, category
->protocols
);
663 /* Register the instance methods as class methods, this is
664 only done for root classes. */
665 __objc_register_instance_methods_to_class (class);
669 /* The object to which the category methods belong can't be found.
670 Save the information. */
671 unclaimed_categories
= list_cons (category
, unclaimed_categories
);
676 uninitialized_statics
= list_cons (statics
, uninitialized_statics
);
677 if (uninitialized_statics
)
678 objc_init_statics ();
680 /* Scan the unclaimed category hash. Attempt to attach any unclaimed
681 categories to objects. */
682 for (cell
= &unclaimed_categories
; *cell
; )
684 Category_t category
= (*cell
)->head
;
685 Class
class = objc_lookup_class (category
->class_name
);
689 DEBUG_PRINTF ("attaching stored categories to object: %s\n",
692 list_remove_head (cell
);
694 if (category
->instance_methods
)
695 class_add_method_list (class, category
->instance_methods
);
697 if (category
->class_methods
)
698 class_add_method_list ((Class
) class->class_pointer
,
699 category
->class_methods
);
701 if (category
->protocols
)
703 __objc_init_protocols (category
->protocols
);
704 __objc_class_add_protocols (class, category
->protocols
);
707 /* Register the instance methods as class methods, this is
708 only done for root classes. */
709 __objc_register_instance_methods_to_class (class);
712 cell
= &(*cell
)->tail
;
715 if (unclaimed_proto_list
&& objc_lookup_class ("Protocol"))
717 list_mapcar (unclaimed_proto_list
,
718 (void (*) (void *))__objc_init_protocols
);
719 list_free (unclaimed_proto_list
);
720 unclaimed_proto_list
= 0;
725 objc_mutex_unlock (__objc_runtime_mutex
);
729 objc_send_load (void)
731 if (! __objc_module_list
)
734 /* Try to find out if all the classes loaded so far also have their
735 superclasses known to the runtime. We suppose that the objects
736 that are allocated in the +load method are in general of a class
737 declared in the same module. */
738 if (unresolved_classes
)
740 Class
class = unresolved_classes
->head
;
742 while (objc_lookup_class ((char *) class->super_class
))
744 list_remove_head (&unresolved_classes
);
745 if (unresolved_classes
)
746 class = unresolved_classes
->head
;
751 /* If we still have classes for whom we don't have yet their
752 super classes known to the runtime we don't send the +load
754 if (unresolved_classes
)
758 /* Special check to allow creating and sending messages to constant
759 strings in +load methods. If these classes are not yet known,
760 even if all the other classes are known, delay sending of +load. */
761 if (! objc_lookup_class ("NXConstantString") ||
762 ! objc_lookup_class ("Object"))
765 /* Iterate over all modules in the __objc_module_list and call on
766 them the __objc_create_classes_tree function. This function
767 creates a tree of classes that resembles the class hierarchy. */
768 list_mapcar (__objc_module_list
,
769 (void (*) (void *)) __objc_create_classes_tree
);
771 while (__objc_class_tree_list
)
774 objc_preorder_traverse (__objc_class_tree_list
->head
,
775 0, __objc_tree_print
);
777 objc_preorder_traverse (__objc_class_tree_list
->head
,
778 0, __objc_send_load
);
779 objc_postorder_traverse (__objc_class_tree_list
->head
,
780 0, __objc_destroy_class_tree_node
);
781 list_remove_head (&__objc_class_tree_list
);
784 list_mapcar (__objc_module_list
, (void (*) (void *)) __objc_call_callback
);
785 list_free (__objc_module_list
);
786 __objc_module_list
= NULL
;
790 __objc_create_classes_tree (Module_t module
)
792 /* The runtime mutex is locked in this point */
794 Symtab_t symtab
= module
->symtab
;
797 /* Iterate thru classes defined in this module and insert them in
798 the classes tree hierarchy. */
799 for (i
= 0; i
< symtab
->cls_def_cnt
; i
++)
801 Class
class = (Class
) symtab
->defs
[i
];
803 objc_tree_insert_class (class);
808 __objc_call_callback (Module_t module
)
810 /* The runtime mutex is locked in this point. */
812 Symtab_t symtab
= module
->symtab
;
815 /* Iterate thru classes defined in this module and call the callback
817 for (i
= 0; i
< symtab
->cls_def_cnt
; i
++)
819 Class
class = (Class
) symtab
->defs
[i
];
821 /* Call the _objc_load_callback for this class. */
822 if (_objc_load_callback
)
823 _objc_load_callback (class, 0);
826 /* Call the _objc_load_callback for categories. Don't register the
827 instance methods as class methods for categories to root classes
828 since they were already added in the class. */
829 for (i
= 0; i
< symtab
->cat_def_cnt
; i
++)
831 Category_t category
= symtab
->defs
[i
+ symtab
->cls_def_cnt
];
832 Class
class = objc_lookup_class (category
->class_name
);
834 if (_objc_load_callback
)
835 _objc_load_callback (class, category
);
839 /* Sanity check the version of gcc used to compile `module'. */
842 init_check_module_version (Module_t module
)
844 if ((module
->version
!= OBJC_VERSION
) || (module
->size
!= sizeof (Module
)))
846 _objc_abort ("Module %s version %d doesn't match runtime %d\n",
847 module
->name
, (int)module
->version
, OBJC_VERSION
);
851 /* __objc_init_class must be called with __objc_runtime_mutex already locked. */
853 __objc_init_class (Class
class)
855 /* Store the class in the class table and assign class numbers. */
856 __objc_add_class_to_hash (class);
858 /* Register all of the selectors in the class and meta class. */
859 __objc_register_selectors_from_class (class);
860 __objc_register_selectors_from_class ((Class
) class->class_pointer
);
862 /* Install the fake dispatch tables */
863 __objc_install_premature_dtable (class);
864 __objc_install_premature_dtable (class->class_pointer
);
866 /* Register the instance methods as class methods, this is only done
868 __objc_register_instance_methods_to_class (class);
870 if (class->protocols
)
871 __objc_init_protocols (class->protocols
);
874 /* __objc_init_protocol must be called with __objc_runtime_mutex
875 already locked, and the "Protocol" class already registered. */
877 __objc_init_protocol (struct objc_protocol
*protocol
)
879 static Class proto_class
= 0;
882 proto_class
= objc_get_class ("Protocol");
884 if (((size_t)protocol
->class_pointer
) == PROTOCOL_VERSION
)
886 /* Assign class pointer */
887 protocol
->class_pointer
= proto_class
;
889 /* Register all the selectors in the protocol with the runtime.
890 This both registers the selectors with the right types, and
891 it also fixes up the 'struct objc_method' structures inside
892 the protocol so that each method_name (a char * as compiled
893 by the compiler) is replaced with the appropriate runtime
895 if (protocol
->class_methods
)
896 __objc_register_selectors_from_description_list (protocol
->class_methods
);
898 if (protocol
->instance_methods
)
899 __objc_register_selectors_from_description_list (protocol
->instance_methods
);
901 /* Register the protocol in the hashtable or protocols by
903 __objc_protocols_add_protocol (protocol
->protocol_name
, protocol
);
905 /* Init super protocols */
906 __objc_init_protocols (protocol
->protocol_list
);
908 else if (protocol
->class_pointer
!= proto_class
)
910 _objc_abort ("Version %d doesn't match runtime protocol version %d\n",
911 (int) ((char *) protocol
->class_pointer
918 __objc_init_protocols (struct objc_protocol_list
*protos
)
921 static Class proto_class
= 0;
926 objc_mutex_lock (__objc_runtime_mutex
);
929 proto_class
= objc_lookup_class ("Protocol");
933 unclaimed_proto_list
= list_cons (protos
, unclaimed_proto_list
);
934 objc_mutex_unlock (__objc_runtime_mutex
);
939 assert (protos
->next
== 0); /* only single ones allowed */
942 for (i
= 0; i
< protos
->count
; i
++)
944 struct objc_protocol
*aProto
= protos
->list
[i
];
945 __objc_init_protocol (aProto
);
948 objc_mutex_unlock (__objc_runtime_mutex
);
952 __objc_class_add_protocols (Class
class, struct objc_protocol_list
*protos
)
959 protos
->next
= class->protocols
;
960 class->protocols
= protos
;