OSDN Git Service

PR ada/60703
[pf3gnuchains/gcc-fork.git] / libobjc / selector.c
index f8a7f14..55ca706 100644 (file)
@@ -1,5 +1,6 @@
 /* GNU Objective C Runtime selector related functions
-   Copyright (C) 1993, 1995, 1996, 1997, 2002, 2004, 2009 Free Software Foundation, Inc.
+   Copyright (C) 1993, 1995, 1996, 1997, 2002, 2004, 2009, 2010
+   Free Software Foundation, Inc.
    Contributed by Kresten Krab Thorup
 
 This file is part of GCC.
@@ -226,7 +227,7 @@ sel_isEqual (SEL s1, SEL s2)
 
 /* Return YES iff t1 and t2 have same method types.  Ignore the
    argframe layout.  */
-BOOL
+static BOOL
 sel_types_match (const char *t1, const char *t2)
 {
   if (! t1 || ! t2)
@@ -251,83 +252,6 @@ sel_types_match (const char *t1, const char *t2)
   return NO;
 }
 
-/* Return selector representing name.  In the Modern API, you'd
-   normally use sel_registerTypedName() for this, which does the same
-   but would register the selector with the runtime if not registered
-   yet (if you only want to check for selectors without registering,
-   use sel_copyTypedSelectorList()).  */
-SEL
-sel_get_typed_uid (const char *name, const char *types)
-{
-  struct objc_list *l;
-  sidx i;
-
-  objc_mutex_lock (__objc_runtime_mutex);
-
-  i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name);
-  if (i == 0)
-    {
-      objc_mutex_unlock (__objc_runtime_mutex);
-      return 0;
-    }
-
-  for (l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
-       l; l = l->tail)
-    {
-      SEL s = (SEL) l->head;
-      if (types == 0 || s->sel_types == 0)
-       {
-         if (s->sel_types == types)
-           {
-             objc_mutex_unlock (__objc_runtime_mutex);
-             return s;
-           }
-       }
-      else if (sel_types_match (s->sel_types, types))
-       {
-         objc_mutex_unlock (__objc_runtime_mutex);
-         return s;
-       }
-    }
-
-  objc_mutex_unlock (__objc_runtime_mutex);
-  return 0;
-}
-
-/* Return selector representing name; prefer a selector with non-NULL
-   type.  In the Modern API, sel_getTypedSelector() is similar but
-   returns NULL if a typed selector couldn't be found.  */
-SEL
-sel_get_any_typed_uid (const char *name)
-{
-  struct objc_list *l;
-  sidx i;
-  SEL s = NULL;
-
-  objc_mutex_lock (__objc_runtime_mutex);
-
-  i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name);
-  if (i == 0)
-    {
-      objc_mutex_unlock (__objc_runtime_mutex);
-      return 0;
-    }
-
-  for (l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
-       l; l = l->tail)
-    {
-      s = (SEL) l->head;
-      if (s->sel_types)
-       {
-         objc_mutex_unlock (__objc_runtime_mutex);
-         return s;
-       }
-    }
-
-  objc_mutex_unlock (__objc_runtime_mutex);
-  return s;
-}
-
 /* Return selector representing name.  */
 SEL
 sel_get_any_uid (const char *name)
@@ -357,13 +281,18 @@ SEL
 sel_getTypedSelector (const char *name)
 {
   sidx i;
-  objc_mutex_lock (__objc_runtime_mutex);
 
+  if (name == NULL)
+    return NULL;
+  
+  objc_mutex_lock (__objc_runtime_mutex);
+  
   /* Look for a typed selector.  */
   i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name);
   if (i != 0)
     {
       struct objc_list *l;
+      SEL returnValue = NULL;
 
       for (l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
           l; l = l->tail)
@@ -371,10 +300,40 @@ sel_getTypedSelector (const char *name)
          SEL s = (SEL) l->head;
          if (s->sel_types)
            {
-             objc_mutex_unlock (__objc_runtime_mutex);
-             return s;
+             if (returnValue == NULL)
+               {
+                 /* First typed selector that we find.  Keep it in
+                    returnValue, but keep checking as we want to
+                    detect conflicts.  */
+                 returnValue = s;
+               }
+             else
+               {
+                 /* We had already found a typed selectors, so we
+                    have multiple ones.  Double-check that they have
+                    different types, just in case for some reason we
+                    got duplicates with the same types.  If so, it's
+                    OK, we'll ignore the duplicate.  */
+                 if (returnValue->sel_types == s->sel_types)
+                   continue;
+                 else if (sel_types_match (returnValue->sel_types, s->sel_types))
+                   continue;
+                 else
+                   {
+                     /* The types of the two selectors are different;
+                        it's a conflict.  Too bad.  Return NULL.  */
+                     objc_mutex_unlock (__objc_runtime_mutex);
+                     return NULL;
+                   }
+               }
            }
        }
+
+      if (returnValue != NULL)
+       {
+         objc_mutex_unlock (__objc_runtime_mutex);
+         return returnValue;
+       }
     }
 
   /* No typed selector found.  Return NULL.  */
@@ -457,15 +416,6 @@ const char *sel_getName (SEL selector)
   return ret;
 }
 
-/* Traditional GNU Objective-C Runtime API.  */
-const char *sel_get_name (SEL selector)
-{
-  if (selector == NULL)
-    return 0;
-
-  return sel_getName (selector);
-}
-
 BOOL
 sel_is_mapped (SEL selector)
 {
@@ -481,12 +431,6 @@ const char *sel_getTypeEncoding (SEL selector)
     return 0;
 }
 
-/* Traditional GNU Objective-C Runtime API.  */
-const char *sel_get_type (SEL selector)
-{
-  return sel_getTypeEncoding (selector);
-}
-
 /* The uninstalled dispatch table.  */
 extern struct sarray *__objc_uninstalled_dtable;
 
@@ -561,7 +505,7 @@ __sel_register_typed_name (const char *name, const char *types,
                    return s;
                }
            }
-         else if (! strcmp (s->sel_types, types))
+         else if (sel_types_match (s->sel_types, types))
            {
              if (orig)
                {
@@ -658,6 +602,9 @@ SEL
 sel_registerName (const char *name)
 {
   SEL ret;
+
+  if (name == NULL)
+    return NULL;
     
   objc_mutex_lock (__objc_runtime_mutex);
   /* Assume that name is not constant static memory and needs to be
@@ -668,18 +615,14 @@ sel_registerName (const char *name)
   return ret;
 }
 
-/* Traditional GNU Objective-C Runtime API.  */
-SEL
-sel_register_name (const char *name)
-{
-  return sel_registerName (name);
-}
-
 SEL
 sel_registerTypedName (const char *name, const char *type)
 {
   SEL ret;
 
+  if (name == NULL)
+    return NULL;
+
   objc_mutex_lock (__objc_runtime_mutex);
   /* Assume that name and type are not constant static memory and need
      to be copied before put into a runtime structure.  is_const ==
@@ -690,22 +633,9 @@ sel_registerTypedName (const char *name, const char *type)
   return ret;
 }
 
-SEL
-sel_register_typed_name (const char *name, const char *type)
-{
-  return sel_registerTypedName (name, type);
-}
-
 /* Return the selector representing name.  */
 SEL
 sel_getUid (const char *name)
 {
   return sel_registerTypedName (name, 0);
 }
-
-/* Traditional GNU Objective-C Runtime API.  */
-SEL
-sel_get_uid (const char *name)
-{
-  return sel_getUid (name);
-}