init.c
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:26k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

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