OSDN Git Service

In libobjc/:
[pf3gnuchains/gcc-fork.git] / libobjc / init.c
index 9384f58..ff47f81 100644 (file)
@@ -1,5 +1,5 @@
 /* GNU Objective C Runtime initialization 
-   Copyright (C) 1993, 1995, 1996, 1997, 2002, 2009
+   Copyright (C) 1993, 1995, 1996, 1997, 2002, 2009, 2010
    Free Software Foundation, Inc.
    Contributed by Kresten Krab Thorup
    +load support contributed by Ovidiu Predescu <ovidiu@net-community.com>
@@ -26,24 +26,29 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
 #include "objc-private/common.h"
 #include "objc-private/error.h"
-#include "objc/objc.h"
-#include "objc/objc-api.h"
+#include "objc/runtime.h"
 #include "objc/thr.h"
 #include "objc-private/hash.h"
 #include "objc-private/objc-list.h" 
+#include "objc-private/module-abi-8.h" 
 #include "objc-private/runtime.h"
 #include "objc-private/objc-sync.h" /* For __objc_sync_init() */
-#include "objc-private/protocols.h" /* For __objc_protocols_init() and __objc_protocols_add_protocol() */
+#include "objc-private/protocols.h" /* For __objc_protocols_init(),
+                                      __objc_protocols_add_protocol()
+                                      __objc_protocols_register_selectors() */
+#include "objc-private/accessors.h" /* For __objc_accessors_init() */
 
-/* The version number of this runtime.  This must match the number 
+/* The version number of this runtime.  This must match the number
    defined in gcc (objc-act.c).  */
 #define OBJC_VERSION 8
 #define PROTOCOL_VERSION 2
 
-/* This list contains all modules currently loaded into the runtime.  */
+/* This list contains all modules currently loaded into the
+   runtime.  */
 static struct objc_list *__objc_module_list = 0;       /* !T:MUTEX */
 
-/* This list contains all proto_list's not yet assigned class links.  */
+/* This list contains all proto_list's not yet assigned class
+   links.  */
 static struct objc_list *unclaimed_proto_list = 0;     /* !T:MUTEX */
 
 /* List of unresolved static instances.  */
@@ -66,11 +71,14 @@ objc_mutex_t __objc_runtime_mutex = 0;
 int __objc_runtime_threads_alive = 1;                  /* !T:MUTEX */
 
 /* Check compiler vs runtime version.  */
-static void init_check_module_version (Module_t);
+static void init_check_module_version (struct objc_module *);
 
 /* Assign isa links to protos.  */
 static void __objc_init_protocols (struct objc_protocol_list *protos);
 
+/* Assign isa link to a protocol, and register it.  */
+static void __objc_init_protocol (struct objc_protocol *protocol);
+
 /* Add protocol to class.  */
 static void __objc_class_add_protocols (Class, struct objc_protocol_list *);
 
@@ -78,16 +86,19 @@ static void __objc_class_add_protocols (Class, struct objc_protocol_list *);
    class or a category is loaded into the runtime.  This may e.g. help
    a dynamic loader determine the classes that have been loaded when
    an object file is dynamically linked in.  */
-void (*_objc_load_callback) (Class class, Category *category); /* !T:SAFE */
+/* TODO: This needs to be declared in a public file with the new API.  */
+void (*_objc_load_callback) (Class class, struct objc_category *category); /* !T:SAFE */
 
-/* Is all categories/classes resolved?  */
+/* Are all categories/classes resolved?  */
 BOOL __objc_dangling_categories = NO;           /* !T:UNUSED */
 
+/* TODO: This needs to go into objc-private/selector.h.  */
 extern SEL
 __sel_register_typed_name (const char *name, const char *types, 
                           struct objc_selector *orig, BOOL is_const);
 
-/* Sends +load to all classes and categories in certain situations.  */
+/* Sends +load to all classes and categories in certain
+   situations.  */
 static void objc_send_load (void);
 
 /* Inserts all the classes defined in module in a tree of classes that
@@ -96,9 +107,9 @@ static void objc_send_load (void);
    methods were not executed before. The algorithm ensures that when
    the +load method of a class is executed all the superclasses have
    been already received the +load message.  */
-static void __objc_create_classes_tree (Module_t module);
+static void __objc_create_classes_tree (struct objc_module *module);
 
-static void __objc_call_callback (Module_t module);
+static void __objc_call_callback (struct objc_module *module);
 
 /* A special version that works only before the classes are completely
    installed in the runtime.  */
@@ -137,20 +148,19 @@ static cache_ptr __objc_load_methods = NULL;
 
    This function returns the superclass of a class in both cases, and
    can be used to build the determine the class relationships while
-   building the tree.
-*/
+   building the tree.  */
 static Class  class_superclass_of_class (Class class)
 {
   char *super_class_name;
 
   /* If the class links have been resolved, use the resolved
-   * links.  */
+     links.  */
   if (CLS_ISRESOLV (class))
     return class->super_class;
   
   /* Else, 'class' has not yet been resolved.  This means that its
-   * super_class pointer is really the name of the super class (rather
-   * than a pointer to the actual superclass).  */
+     super_class pointer is really the name of the super class (rather
+     than a pointer to the actual superclass).  */
   super_class_name = (char *)class->super_class;
 
   /* Return Nil for a root class.  */
@@ -158,7 +168,7 @@ static Class  class_superclass_of_class (Class class)
     return Nil;
 
   /* Lookup the superclass of non-root classes.  */
-  return objc_lookup_class (super_class_name);
+  return objc_getClass (super_class_name);
 }
 
 
@@ -167,16 +177,17 @@ static Class  class_superclass_of_class (Class class)
    `bottom_class'. The classes in this tree are super classes of
    `bottom_class'. `subclasses' member of each tree node point to the
    next subclass tree node.  */
-
 static objc_class_tree *
 create_tree_of_subclasses_inherited_from (Class bottom_class, Class upper)
 {
-  Class superclass = bottom_class->super_class ?
-                       objc_lookup_class ((char *) bottom_class->super_class)
-                     : Nil;
-                                       
+  Class superclass;
   objc_class_tree *tree, *prev;
 
+  if (bottom_class->super_class)
+    superclass = objc_getClass ((char *) bottom_class->super_class);
+  else
+    superclass = Nil;
+
   DEBUG_PRINTF ("create_tree_of_subclasses_inherited_from:");
   DEBUG_PRINTF ("bottom_class = %s, upper = %s\n",
                (bottom_class ? bottom_class->name : NULL),
@@ -203,7 +214,6 @@ create_tree_of_subclasses_inherited_from (Class bottom_class, Class upper)
    part of the classes hierarchy described by `tree'. This function is
    private to objc_tree_insert_class (), you should not call it
    directly.  */
-
 static objc_class_tree *
 __objc_tree_insert_class (objc_class_tree *tree, Class class)
 {
@@ -214,15 +224,15 @@ __objc_tree_insert_class (objc_class_tree *tree, Class class)
     return create_tree_of_subclasses_inherited_from (class, NULL);
   else if (class == tree->class)
     {
-      /* `class' has been already inserted */
+      /* `class' has been already inserted */
       DEBUG_PRINTF ("1. class %s was previously inserted\n", class->name);
       return tree;
     }
   else if (class_superclass_of_class (class) == tree->class)
     {
-      /* If class is a direct subclass of tree->class then add class to the
-        list of subclasses. First check to see if it wasn't already
-        inserted.  */
+      /* If class is a direct subclass of tree->class then add class
+        to the list of subclasses. First check to see if it wasn't
+        already inserted.  */
       struct objc_list *list = tree->subclasses;
       objc_class_tree *node;
 
@@ -239,7 +249,8 @@ __objc_tree_insert_class (objc_class_tree *tree, Class class)
          list = list->tail;
        }
 
-      /* Create a new node class and insert it into the list of subclasses */
+      /* Create a new node class and insert it into the list of
+        subclasses.  */
       node = objc_calloc (1, sizeof (objc_class_tree));
       node->class = class;
       tree->subclasses = list_cons (node, tree->subclasses);
@@ -248,8 +259,8 @@ __objc_tree_insert_class (objc_class_tree *tree, Class class)
     }
   else
     {
-      /* The class is not a direct subclass of tree->class. Search for
-         class's superclasses in the list of subclasses.  */
+      /* The class is not a direct subclass of tree->class.  Search
+         for class's superclasses in the list of subclasses.  */
       struct objc_list *subclasses = tree->subclasses;
 
       /* Precondition: the class must be a subclass of tree->class;
@@ -288,7 +299,6 @@ __objc_tree_insert_class (objc_class_tree *tree, Class class)
 }
 
 /* This function inserts `class' in the right tree hierarchy classes.  */
-
 static void
 objc_tree_insert_class (Class class)
 {
@@ -318,7 +328,6 @@ objc_tree_insert_class (Class class)
 }
 
 /* Traverse tree in preorder. Used to send +load.  */
-
 static void
 objc_preorder_traverse (objc_class_tree *tree,
                        int level,
@@ -332,7 +341,6 @@ objc_preorder_traverse (objc_class_tree *tree,
 }
 
 /* Traverse tree in postorder. Used to destroy a tree.  */
-
 static void
 objc_postorder_traverse (objc_class_tree *tree,
                         int level,
@@ -346,7 +354,6 @@ objc_postorder_traverse (objc_class_tree *tree,
 }
 
 /* Used to print a tree class hierarchy.  */
-
 #ifdef DEBUG
 static void
 __objc_tree_print (objc_class_tree *tree, int level)
@@ -364,33 +371,32 @@ __objc_tree_print (objc_class_tree *tree, int level)
    reverse order assures the +load of class is executed first and then
    +load of categories because of the way in which categories are
    added to the class methods.  */
-
 static void
-__objc_send_message_in_list (MethodList_t method_list, Class class, SEL op)
+__objc_send_message_in_list (struct objc_method_list *method_list, Class class, SEL op)
 {
   int i;
 
   if (! method_list)
     return;
 
-  /* First execute the `op' message in the following method lists */
+  /* First execute the `op' message in the following method lists */
   __objc_send_message_in_list (method_list->method_next, class, op);
 
   /* Search the method list.  */
   for (i = 0; i < method_list->method_count; i++)
     {
-      Method_t mth = &method_list->method_list[i];
+      struct objc_method *mth = &method_list->method_list[i];
 
       if (mth->method_name && sel_eq (mth->method_name, op)
          && ! objc_hash_is_key_in_hash (__objc_load_methods, mth->method_imp))
        {
-         /* Add this method into the +load hash table */
+         /* Add this method into the +load hash table */
          objc_hash_add (&__objc_load_methods,
                         mth->method_imp,
                         mth->method_imp);
-
+         
          DEBUG_PRINTF ("sending +load in class: %s\n", class->name);
-
+         
          /* The method was found and wasn't previously executed.  */
          (*mth->method_imp) ((id)class, mth->method_name);
 
@@ -405,10 +411,10 @@ __objc_send_load (objc_class_tree *tree,
 {
   static SEL load_sel = 0;
   Class class = tree->class;
-  MethodList_t method_list = class->class_pointer->methods;
+  struct objc_method_list *method_list = class->class_pointer->methods;
 
   if (! load_sel)
-    load_sel = sel_register_name ("load");
+    load_sel = sel_registerName ("load");
 
   __objc_send_message_in_list (method_list, class, load_sel);
 }
@@ -422,7 +428,6 @@ __objc_destroy_class_tree_node (objc_class_tree *tree,
 
 /* This is used to check if the relationship between two classes
    before the runtime completely installs the classes.  */
-
 static BOOL
 class_is_subclass_of_class (Class class, Class superclass)
 {
@@ -440,9 +445,7 @@ class_is_subclass_of_class (Class class, Class superclass)
    their superclasses are not yet known to the runtime.  */
 static struct objc_list *unresolved_classes = 0;
 
-/* Extern function used to reference the Object and NXConstantString
-   classes.  */
-
+/* Extern function used to reference the Object class.  */
 extern void __objc_force_linking (void);
 
 void
@@ -454,7 +457,6 @@ __objc_force_linking (void)
 
 /* Run through the statics list, removing modules as soon as all its
    statics have been initialized.  */
-
 static void
 objc_init_statics (void)
 {
@@ -471,30 +473,47 @@ objc_init_statics (void)
           *statics_in_module; statics_in_module++)
        {
          struct objc_static_instances *statics = *statics_in_module;
-         Class class = objc_lookup_class (statics->class_name);
+         Class class = objc_getClass (statics->class_name);
 
          if (! class)
-           module_initialized = 0;
-         /* Actually, the static's class_pointer will be NULL when we
-             haven't been here before.  However, the comparison is to be
-             reminded of taking into account class posing and to think about
-             possible semantics...  */
-         else if (class != statics->instances[0]->class_pointer)
            {
+             /* It is unfortunate that this will cause all the
+                statics initialization to be done again (eg, if we
+                already initialized constant strings, and are now
+                initializing protocols, setting module_initialized to
+                0 would cause constant strings to be initialized
+                again).  It would be good to be able to track if we
+                have already initialized some of them.  */
+             module_initialized = 0;
+           }
+         else
+           {
+             /* Note that if this is a list of Protocol objects, some
+                of them may have been initialized already (because
+                they were attached to classes or categories, and the
+                class/category loading code automatically fixes them
+                up), and some of them may not.  We really need to go
+                through the whole list to be sure!  Protocols are
+                also special because we want to register them and
+                register all their selectors.  */
              id *inst;
 
-             for (inst = &statics->instances[0]; *inst; inst++)
+             if (strcmp (statics->class_name, "Protocol") == 0)
                {
-                 (*inst)->class_pointer = class;
-
-                 /* ??? Make sure the object will not be freed.  With
-                     refcounting, invoke `-retain'.  Without refcounting, do
-                     nothing and hope that `-free' will never be invoked.  */
-
-                 /* ??? Send the object an `-initStatic' or something to
-                     that effect now or later on?  What are the semantics of
-                     statically allocated instances, besides the trivial
-                     NXConstantString, anyway?  */
+                 /* Protocols are special, because not only we want
+                    to fix up their class pointers, but we also want
+                    to register them and their selectors with the
+                    runtime.  */
+                 for (inst = &statics->instances[0]; *inst; inst++)
+                   __objc_init_protocol ((struct objc_protocol *)*inst);
+               }
+             else
+               {
+                 /* Other static instances (typically constant
+                    strings) are easier as we just fix up their class
+                    pointers.  */
+                 for (inst = &statics->instances[0]; *inst; inst++)              
+                   (*inst)->class_pointer = class;
                }
            }
        }
@@ -510,47 +529,48 @@ objc_init_statics (void)
     }
 
   objc_mutex_unlock (__objc_runtime_mutex);
-} /* objc_init_statics */
+}
 
 /* This function is called by constructor functions generated for each
    module compiled.  (_GLOBAL_$I$...) The purpose of this function is
    to gather the module pointers so that they may be processed by the
    initialization routines as soon as possible.  */
-
 void
-__objc_exec_class (Module_t module)
+__objc_exec_class (struct objc_module *module)
 {
-  /* Have we processed any constructors previously?  This flag is used to
-     indicate that some global data structures need to be built.  */
+  /* Have we processed any constructors previously?  This flag is used
+     to indicate that some global data structures need to be
+     built.  */
   static BOOL previous_constructors = 0;
 
   static struct objc_list *unclaimed_categories = 0;
 
-  /* The symbol table (defined in objc-api.h) generated by gcc */
-  Symtab_t symtab = module->symtab;
+  /* The symbol table (defined in objc-private/module-abi-8.h)
+     generated by gcc.  */
+  struct objc_symtab *symtab = module->symtab;
 
-  /* The statics in this module */
+  /* The statics in this module */
   struct objc_static_instances **statics
     = symtab->defs[symtab->cls_def_cnt + symtab->cat_def_cnt];
 
-  /* Entry used to traverse hash lists */
+  /* Entry used to traverse hash lists */
   struct objc_list **cell;
 
-  /* The table of selector references for this module */
+  /* The table of selector references for this module */
   SEL selectors = symtab->refs; 
 
-  /* dummy counter */
   int i;
 
   DEBUG_PRINTF ("received module: %s\n", module->name);
 
-  /* check gcc version */
+  /* Check gcc version.  */
   init_check_module_version (module);
 
-  /* On the first call of this routine, initialize some data structures.  */
+  /* On the first call of this routine, initialize some data
+     structures.  */
   if (! previous_constructors)
     {
-       /* Initialize thread-safe system */
+       /* Initialize thread-safe system */
       __objc_init_thread_system ();
       __objc_runtime_threads_alive = 1;
       __objc_runtime_mutex = objc_mutex_allocate ();
@@ -563,11 +583,13 @@ __objc_exec_class (Module_t module)
                                           (hash_func_type)objc_hash_ptr,
                                           objc_compare_ptrs);
       __objc_protocols_init ();
+      __objc_accessors_init ();
       __objc_sync_init ();
       previous_constructors = 1;
     }
 
-  /* Save the module pointer for later processing. (not currently used) */
+  /* Save the module pointer for later processing. (not currently
+     used).  */
   objc_mutex_lock (__objc_runtime_mutex);
   __objc_module_list = list_cons (module, __objc_module_list);
 
@@ -579,15 +601,17 @@ __objc_exec_class (Module_t module)
          const char *name, *type;
          name = (char *) selectors[i].sel_id;
          type = (char *) selectors[i].sel_types;
-         /* Constructors are constant static data so we can safely store
-            pointers to them in the runtime structures. is_const == YES */
+         /* Constructors are constant static data so we can safely
+            store pointers to them in the runtime
+            structures. is_const == YES.  */
          __sel_register_typed_name (name, type, 
                                     (struct objc_selector *) &(selectors[i]),
                                     YES);
        }
     }
 
-  /* Parse the classes in the load module and gather selector information.  */
+  /* Parse the classes in the load module and gather selector
+     information.  */
   DEBUG_PRINTF ("gathering selectors from module: %s\n", module->name);
   for (i = 0; i < symtab->cls_def_cnt; ++i)
     {
@@ -599,41 +623,26 @@ __objc_exec_class (Module_t module)
       assert (CLS_ISMETA (class->class_pointer));
       DEBUG_PRINTF ("phase 1, processing class: %s\n", class->name);
 
-      /* Initialize the subclass list to be NULL.
-        In some cases it isn't and this crashes the program.  */
+      /* Initialize the subclass list to be NULL.  In some cases it
+        isn't and this crashes the program.  */
       class->subclass_list = NULL;
 
-      /* Store the class in the class table and assign class numbers.  */
-      __objc_add_class_to_hash (class);
-
-      /* Register all of the selectors in the class and meta class.  */
-      __objc_register_selectors_from_class (class);
-      __objc_register_selectors_from_class ((Class) class->class_pointer);
-
-      /* Install the fake dispatch tables */
-      __objc_install_premature_dtable (class);
-      __objc_install_premature_dtable (class->class_pointer);
+      __objc_init_class (class);
 
-      /* Register the instance methods as class methods, this is
-        only done for root classes.  */
-      __objc_register_instance_methods_to_class (class);
-
-      if (class->protocols)
-       __objc_init_protocols (class->protocols);
-
-      /* Check to see if the superclass is known in this point. If it's not
-        add the class to the unresolved_classes list.  */
-      if (superclass && ! objc_lookup_class (superclass))
+      /* Check to see if the superclass is known in this point. If
+        it's not add the class to the unresolved_classes list.  */
+      if (superclass && ! objc_getClass (superclass))
        unresolved_classes = list_cons (class, unresolved_classes);
    }
 
   /* Process category information from the module.  */
   for (i = 0; i < symtab->cat_def_cnt; ++i)
     {
-      Category_t category = symtab->defs[i + symtab->cls_def_cnt];
-      Class class = objc_lookup_class (category->class_name);
+      struct objc_category *category = symtab->defs[i + symtab->cls_def_cnt];
+      Class class = objc_getClass (category->class_name);
       
-      /* If the class for the category exists then append its methods.  */
+      /* If the class for the category exists then append its
+        methods.  */
       if (class)
        {
 
@@ -662,8 +671,8 @@ __objc_exec_class (Module_t module)
        }
       else
        {
-         /* The object to which the category methods belong can't be found.
-            Save the information.  */
+         /* The object to which the category methods belong can't be
+            found.  Save the information.  */
          unclaimed_categories = list_cons (category, unclaimed_categories);
        }
     }
@@ -673,12 +682,12 @@ __objc_exec_class (Module_t module)
   if (uninitialized_statics)
     objc_init_statics ();
 
-  /* Scan the unclaimed category hash.  Attempt to attach any unclaimed
-     categories to objects.  */
+  /* Scan the unclaimed category hash.  Attempt to attach any
+     unclaimed categories to objects.  */
   for (cell = &unclaimed_categories; *cell; )
     {
-      Category_t category = (*cell)->head;
-      Class class = objc_lookup_class (category->class_name);
+      struct objc_category *category = (*cell)->head;
+      Class class = objc_getClass (category->class_name);
       
       if (class)
        {
@@ -708,7 +717,7 @@ __objc_exec_class (Module_t module)
        cell = &(*cell)->tail;
     }
   
-  if (unclaimed_proto_list && objc_lookup_class ("Protocol"))
+  if (unclaimed_proto_list && objc_getClass ("Protocol"))
     {
       list_mapcar (unclaimed_proto_list,
                   (void (*) (void *))__objc_init_protocols);
@@ -735,7 +744,7 @@ objc_send_load (void)
     {
       Class class = unresolved_classes->head;
 
-      while (objc_lookup_class ((char *) class->super_class))
+      while (objc_getClass ((char *) class->super_class))
        {
          list_remove_head (&unresolved_classes);
          if (unresolved_classes)
@@ -751,11 +760,9 @@ objc_send_load (void)
        return;
     }
 
-  /* Special check to allow creating and sending messages to constant
-     strings in +load methods. If these classes are not yet known,
-     even if all the other classes are known, delay sending of +load.  */
-  if (! objc_lookup_class ("NXConstantString") ||
-      ! objc_lookup_class ("Object"))
+  /* Special check.  If 'Object', which is used by meta-classes, has
+     not been loaded yet, delay sending of +load.  */
+  if (! objc_getClass ("Object"))
     return;
 
   /* Iterate over all modules in the __objc_module_list and call on
@@ -783,11 +790,10 @@ objc_send_load (void)
 }
 
 static void
-__objc_create_classes_tree (Module_t module)
+__objc_create_classes_tree (struct objc_module *module)
 {
-  /* The runtime mutex is locked in this point */
-
-  Symtab_t symtab = module->symtab;
+  /* The runtime mutex is locked at this point */
+  struct objc_symtab *symtab = module->symtab;
   int i;
 
   /* Iterate thru classes defined in this module and insert them in
@@ -801,11 +807,10 @@ __objc_create_classes_tree (Module_t module)
 }
 
 static void
-__objc_call_callback (Module_t module)
+__objc_call_callback (struct objc_module *module)
 {
-  /* The runtime mutex is locked in this point.  */
-
-  Symtab_t symtab = module->symtab;
+  /* The runtime mutex is locked at this point.  */
+  struct objc_symtab *symtab = module->symtab;
   int i;
 
   /* Iterate thru classes defined in this module and call the callback
@@ -824,26 +829,91 @@ __objc_call_callback (Module_t module)
      since they were already added in the class.  */
   for (i = 0; i < symtab->cat_def_cnt; i++)
     {
-      Category_t category = symtab->defs[i + symtab->cls_def_cnt];
-      Class class = objc_lookup_class (category->class_name);
-      
+      struct objc_category *category = symtab->defs[i + symtab->cls_def_cnt];
+      Class class = objc_getClass (category->class_name);
+
       if (_objc_load_callback)
        _objc_load_callback (class, category);
     }
 }
 
 /* Sanity check the version of gcc used to compile `module'.  */
-
 static void
-init_check_module_version (Module_t module)
+init_check_module_version (struct objc_module *module)
 {
-  if ((module->version != OBJC_VERSION) || (module->size != sizeof (Module)))
+  if ((module->version != OBJC_VERSION) || (module->size != sizeof (struct objc_module)))
     {
       _objc_abort ("Module %s version %d doesn't match runtime %d\n",
                   module->name, (int)module->version, OBJC_VERSION);
     }
 }
 
+/* __objc_init_class must be called with __objc_runtime_mutex already locked.  */
+void
+__objc_init_class (Class class)
+{
+  /* Store the class in the class table and assign class numbers.  */
+  __objc_add_class_to_hash (class);
+  
+  /* Register all of the selectors in the class and meta class.  */
+  __objc_register_selectors_from_class (class);
+  __objc_register_selectors_from_class ((Class) class->class_pointer);
+
+  /* Install the fake dispatch tables.  */
+  __objc_install_premature_dtable (class);
+  __objc_install_premature_dtable (class->class_pointer);
+
+  /* Register the instance methods as class methods, this is only done
+     for root classes.  */
+  __objc_register_instance_methods_to_class (class);
+
+  if (class->protocols)
+    __objc_init_protocols (class->protocols);
+}
+
+/* __objc_init_protocol must be called with __objc_runtime_mutex
+   already locked, and the "Protocol" class already registered.  */
+static void
+__objc_init_protocol (struct objc_protocol *protocol)
+{
+  static Class proto_class = 0;
+
+  if (! proto_class)
+    proto_class = objc_getClass ("Protocol");
+
+  if (((size_t)protocol->class_pointer) == PROTOCOL_VERSION)
+    {
+      /* Assign class pointer.  */
+      protocol->class_pointer = proto_class;
+      
+      /* Register all the selectors in the protocol with the runtime.
+        This both registers the selectors with the right types, and
+        it also fixes up the 'struct objc_method' structures inside
+        the protocol so that each method_name (a char * as compiled
+        by the compiler) is replaced with the appropriate runtime
+        SEL.  */
+      if (protocol->class_methods)
+       __objc_register_selectors_from_description_list (protocol->class_methods);
+
+      if (protocol->instance_methods)
+       __objc_register_selectors_from_description_list (protocol->instance_methods);
+
+      /* Register the protocol in the hashtable or protocols by
+        name.  */
+      __objc_protocols_add_protocol (protocol->protocol_name, protocol);
+      
+      /* Init super protocols.  */
+      __objc_init_protocols (protocol->protocol_list);
+    }
+  else if (protocol->class_pointer != proto_class)
+    {
+      _objc_abort ("Version %d doesn't match runtime protocol version %d\n",
+                  (int) ((char *) protocol->class_pointer
+                         - (char *) 0),
+                  PROTOCOL_VERSION);
+    }
+}
+
 static void
 __objc_init_protocols (struct objc_protocol_list *protos)
 {
@@ -856,7 +926,7 @@ __objc_init_protocols (struct objc_protocol_list *protos)
   objc_mutex_lock (__objc_runtime_mutex);
 
   if (! proto_class)
-    proto_class = objc_lookup_class ("Protocol");
+    proto_class = objc_getClass ("Protocol");
 
   if (! proto_class)
     {
@@ -866,31 +936,13 @@ __objc_init_protocols (struct objc_protocol_list *protos)
     }
 
 #if 0
-  assert (protos->next == 0);  /* only single ones allowed */
+  assert (protos->next == 0); /* Only single ones allowed.  */
 #endif
 
   for (i = 0; i < protos->count; i++)
     {
       struct objc_protocol *aProto = protos->list[i];
-      if (((size_t)aProto->class_pointer) == PROTOCOL_VERSION)
-       {
-         /* Assign class pointer */
-         aProto->class_pointer = proto_class;
-
-         /* Register the protocol in the hashtable or protocols by
-            name.  */
-         __objc_protocols_add_protocol (aProto->protocol_name, aProto);
-
-         /* Init super protocols */
-         __objc_init_protocols (aProto->protocol_list);
-       }
-      else if (protos->list[i]->class_pointer != proto_class)
-       {
-         _objc_abort ("Version %d doesn't match runtime protocol version %d\n",
-                      (int) ((char *) protos->list[i]->class_pointer
-                             - (char *) 0),
-                      PROTOCOL_VERSION);
-       }
+      __objc_init_protocol (aProto);
     }
 
   objc_mutex_unlock (__objc_runtime_mutex);
@@ -899,11 +951,9 @@ __objc_init_protocols (struct objc_protocol_list *protos)
 static void
 __objc_class_add_protocols (Class class, struct objc_protocol_list *protos)
 {
-  /* Well...  */
   if (! protos)
     return;
 
-  /* Add it...  */
   protos->next = class->protocols;
   class->protocols = protos;
 }