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

信息检索与抽取

开发平台:

Unix_Linux

  1. /* GNU Objective C Runtime selector related functions
  2.    Copyright (C) 1993, 1995, 1996, 1997 Free Software Foundation, Inc.
  3.    Contributed by Kresten Krab Thorup
  4. This file is part of GNU CC.
  5. GNU CC is free software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation; either version 2, or (at your option) any later version.
  8. GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  11. details.
  12. You should have received a copy of the GNU General Public License along with
  13. GNU CC; see the file COPYING.  If not, write to the Free Software
  14. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  15. /* As a special exception, if you link this library with files compiled with
  16.    GCC to produce an executable, this does not cause the resulting executable
  17.    to be covered by the GNU General Public License. This exception does not
  18.    however invalidate any other reasons why the executable file might be
  19.    covered by the GNU General Public License.  */
  20. #include "runtime.h"
  21. #include "sarray.h"
  22. #include "encoding.h"
  23. #include <string.h> /* strcpy */
  24. /* Initial selector hash table size. Value doesn't matter much */
  25. #define SELECTOR_HASH_SIZE 128
  26. /* Tables mapping selector names to uid and opposite */
  27. static struct sarray* __objc_selector_array = 0; /* uid -> sel  !T:MUTEX */
  28. static struct sarray* __objc_selector_names = 0; /* uid -> name !T:MUTEX */
  29. static cache_ptr      __objc_selector_hash  = 0; /* name -> uid !T:MUTEX */
  30. static void register_selectors_from_list(MethodList_t);
  31. /* Number of selectors stored in each of the above tables */
  32. externobjcvardef int __objc_selector_max_index = 0;  /* !T:MUTEX */
  33. void __objc_init_selector_tables()
  34. {
  35.   __objc_selector_array = sarray_new (SELECTOR_HASH_SIZE, 0);
  36.   __objc_selector_names = sarray_new (SELECTOR_HASH_SIZE, 0);
  37.   __objc_selector_hash
  38.     = hash_new (SELECTOR_HASH_SIZE,
  39. (hash_func_type) hash_string,
  40. (compare_func_type) compare_strings);
  41. }  
  42. /* This routine is given a class and records all of the methods in its class
  43.    structure in the record table.  */
  44. void
  45. __objc_register_selectors_from_class (Class class)
  46. {
  47.   MethodList_t method_list;
  48.   method_list = class->methods;
  49.   while (method_list)
  50.     {
  51.       register_selectors_from_list (method_list);
  52.       method_list = method_list->method_next;
  53.     }
  54. }
  55. /* This routine is given a list of methods and records each of the methods in
  56.    the record table.  This is the routine that does the actual recording
  57.    work.
  58.    This one is only called for Class objects.  For categories,
  59.    class_add_method_list is called.
  60.    */
  61. static void
  62. register_selectors_from_list (MethodList_t method_list)
  63. {
  64.   int i = 0;
  65.   while (i < method_list->method_count)
  66.     {
  67.       Method_t method = &method_list->method_list[i];
  68.       method->method_name 
  69. = sel_register_typed_name ((const char*)method->method_name, 
  70.      method->method_types);
  71.       i += 1;
  72.     }
  73. }
  74. /* Register instance methods as class methods for root classes */
  75. void __objc_register_instance_methods_to_class(Class class)
  76. {
  77.   MethodList_t method_list;
  78.   MethodList_t class_method_list;
  79.   int max_methods_no = 16;
  80.   MethodList_t new_list;
  81.   Method_t curr_method;
  82.   /* Only if a root class. */
  83.   if(class->super_class)
  84.     return;
  85.   /* Allocate a method list to hold the new class methods */
  86.   new_list = objc_calloc(sizeof(struct objc_method_list)
  87.     + sizeof(struct objc_method[max_methods_no]), 1);
  88.   method_list = class->methods;
  89.   class_method_list = class->class_pointer->methods;
  90.   curr_method = &new_list->method_list[0];
  91.   /* Iterate through the method lists for the class */
  92.   while (method_list)
  93.     {
  94.       int i;
  95.       /* Iterate through the methods from this method list */
  96.       for (i = 0; i < method_list->method_count; i++)
  97. {
  98.   Method_t mth = &method_list->method_list[i];
  99.   if (mth->method_name
  100.       && !search_for_method_in_list (class_method_list,
  101.       mth->method_name))
  102.     {
  103.       /* This instance method isn't a class method. 
  104.   Add it into the new_list. */
  105.       *curr_method = *mth;
  106.   
  107.       /* Reallocate the method list if necessary */
  108.       if(++new_list->method_count == max_methods_no)
  109. new_list =
  110.   objc_realloc(new_list, sizeof(struct objc_method_list)
  111. + sizeof(struct 
  112. objc_method[max_methods_no += 16]));
  113.       curr_method = &new_list->method_list[new_list->method_count];
  114.     }
  115. }
  116.       method_list = method_list->method_next;
  117.     }
  118.   /* If we created any new class methods
  119.      then attach the method list to the class */
  120.   if (new_list->method_count)
  121.     {
  122.       new_list =
  123.   objc_realloc(new_list, sizeof(struct objc_method_list)
  124.      + sizeof(struct objc_method[new_list->method_count]));
  125.       new_list->method_next = class->class_pointer->methods;
  126.       class->class_pointer->methods = new_list;
  127.     }
  128.     __objc_update_dispatch_table_for_class (class->class_pointer);
  129. }
  130. /* Returns YES iff t1 and t2 have same method types, but we ignore
  131.    the argframe layout */
  132. BOOL
  133. sel_types_match (const char* t1, const char* t2)
  134. {
  135.   if (!t1 || !t2)
  136.     return NO;
  137.   while (*t1 && *t2)
  138.     {
  139.       if (*t1 == '+') t1++;
  140.       if (*t2 == '+') t2++;
  141.       while (isDigit (*t1)) t1++;
  142.       while (isDigit (*t2)) t2++;
  143.       /* xxx Remove these next two lines when qualifiers are put in
  144.  all selectors, not just Protocol selectors. */
  145.       t1 = objc_skip_type_qualifiers(t1);
  146.       t2 = objc_skip_type_qualifiers(t2);
  147.       if (!*t1 && !*t2)
  148. return YES;
  149. #if 1
  150.       {
  151. /* Hack for Swarm Java -> Objective C selector construction. */
  152. char t1v = *t1;
  153. char t2v = *t2;
  154. t1v = isUpper (t1v) ? t1v - 'A' + 'a' : t1v;
  155. t2v = isUpper (t2v) ? t2v - 'A' + 'a' : t2v;
  156. if (t1v != t2v)
  157.   return NO;
  158.       }
  159. #else
  160.       if (*t1 != *t2)
  161. return NO;
  162. #endif
  163.       t1++;
  164.       t2++;
  165.     }
  166.   return NO;
  167. }
  168. /* return selector representing name */
  169. SEL
  170. sel_get_typed_uid (const char *name, const char *types)
  171. {
  172.   struct objc_list *l;
  173.   sidx i;
  174.   objc_mutex_lock(__objc_runtime_mutex);
  175.   i = (sidx) hash_value_for_key (__objc_selector_hash, name);
  176.   if (i == 0)
  177.     {
  178.       objc_mutex_unlock(__objc_runtime_mutex);
  179.       return 0;
  180.     }
  181.   for (l = (struct objc_list*)sarray_get_safe (__objc_selector_array, i);
  182.        l; l = l->tail)
  183.     {
  184.       SEL s = (SEL)l->head;
  185.       if (types == 0 || s->sel_types == 0)
  186. {
  187.   if (s->sel_types == types)
  188.     {
  189.       objc_mutex_unlock(__objc_runtime_mutex);
  190.       return s;
  191.     }
  192. }
  193.       else if (sel_types_match (s->sel_types, types))
  194. {
  195.   objc_mutex_unlock(__objc_runtime_mutex);
  196.   return s;
  197. }
  198.     }
  199.   objc_mutex_unlock(__objc_runtime_mutex);
  200.   return 0;
  201. }
  202. /* Return selector representing name; prefer a selector with non-NULL type */
  203. SEL
  204. sel_get_any_typed_uid (const char *name)
  205. {
  206.   struct objc_list *l;
  207.   sidx i;
  208.   SEL s = NULL;
  209.   objc_mutex_lock(__objc_runtime_mutex);
  210.   i = (sidx) hash_value_for_key (__objc_selector_hash, name);
  211.   if (i == 0)
  212.     {
  213.       objc_mutex_unlock(__objc_runtime_mutex);
  214.       return 0;
  215.     }
  216.   for (l = (struct objc_list*)sarray_get_safe (__objc_selector_array, i);
  217.        l; l = l->tail)
  218.     {
  219.       s = (SEL) l->head;
  220.       if (s->sel_types)
  221. {
  222.     objc_mutex_unlock(__objc_runtime_mutex);
  223.     return s;
  224. }
  225.     }
  226.   objc_mutex_unlock(__objc_runtime_mutex);
  227.   return s;
  228. }
  229. /* return selector representing name */
  230. SEL
  231. sel_get_any_uid (const char *name)
  232. {
  233.   struct objc_list *l;
  234.   sidx i;
  235.   objc_mutex_lock(__objc_runtime_mutex);
  236.   i = (sidx) hash_value_for_key (__objc_selector_hash, name);
  237.   if (soffset_decode (i) == 0)
  238.     {
  239.       objc_mutex_unlock(__objc_runtime_mutex);
  240.       return 0;
  241.     }
  242.   l = (struct objc_list*)sarray_get_safe (__objc_selector_array, i);
  243.   objc_mutex_unlock(__objc_runtime_mutex);
  244.   if (l == 0)
  245.     return 0;
  246.   return (SEL)l->head;
  247. }
  248. /* return selector representing name */
  249. SEL
  250. sel_get_uid (const char *name)
  251. {
  252.   return sel_register_typed_name (name, 0);
  253. }
  254. /* Get name of selector.  If selector is unknown, the empty string "" 
  255.    is returned */ 
  256. const char*
  257. sel_get_name (SEL selector)
  258. {
  259.   const char *ret;
  260.   objc_mutex_lock(__objc_runtime_mutex);
  261.   if ((soffset_decode((sidx)selector->sel_id) > 0)
  262.       && (soffset_decode((sidx)selector->sel_id) <= __objc_selector_max_index))
  263.     ret = sarray_get_safe (__objc_selector_names, (sidx) selector->sel_id);
  264.   else
  265.     ret = 0;
  266.   objc_mutex_unlock(__objc_runtime_mutex);
  267.   return ret;
  268. }
  269. BOOL
  270. sel_is_mapped (SEL selector)
  271. {
  272.   unsigned int idx = soffset_decode ((sidx)selector->sel_id);
  273.   return ((idx > 0) && (idx <= __objc_selector_max_index));
  274. }
  275. const char*
  276. sel_get_type (SEL selector)
  277. {
  278.   if (selector)
  279.     return selector->sel_types;
  280.   else
  281.     return 0;
  282. }
  283. /* The uninstalled dispatch table */
  284. externobjcvar struct sarray* __objc_uninstalled_dtable;
  285. /* Store the passed selector name in the selector record and return its
  286.    selector value (value returned by sel_get_uid).
  287.    Assumes that the calling function has locked down __objc_runtime_mutex. */
  288. /* is_const parameter tells us if the name and types parameters
  289.    are really constant or not.  If YES then they are constant and
  290.    we can just store the pointers.  If NO then we need to copy
  291.    name and types because the pointers may disappear later on. */
  292. SEL
  293. __sel_register_typed_name (const char *name, const char *types, 
  294.    struct objc_selector *orig, BOOL is_const)
  295. {
  296.   struct objc_selector* j;
  297.   sidx i;
  298.   struct objc_list *l;
  299.   i = (sidx) hash_value_for_key (__objc_selector_hash, name);
  300.   if (soffset_decode (i) != 0)
  301.     {
  302.       for (l = (struct objc_list*)sarray_get_safe (__objc_selector_array, i);
  303.    l; l = l->tail)
  304. {
  305.   SEL s = (SEL)l->head;
  306.   if (types == 0 || s->sel_types == 0)
  307.     {
  308.       if (s->sel_types == types)
  309. {
  310.   if (orig)
  311.     {
  312.       orig->sel_id = (void*)i;
  313.       return orig;
  314.     }
  315.   else
  316.     return s;
  317. }
  318.     }
  319.   else if (!strcmp (s->sel_types, types))
  320.     {
  321.       if (orig)
  322. {
  323.   orig->sel_id = (void*)i;
  324.   return orig;
  325. }
  326.       else
  327. return s;
  328.     }
  329. }
  330.       if (orig)
  331. j = orig;
  332.       else
  333. j = objc_malloc (sizeof (struct objc_selector));
  334.       j->sel_id = (void*)i;
  335.       /* Can we use the pointer or must copy types?  Don't copy if NULL */
  336.       if ((is_const) || (types == 0))
  337. j->sel_types = (const char*)types;
  338.       else {
  339. j->sel_types = (char *) objc_malloc(strlen(types)+1);
  340. strcpy((char *)j->sel_types, types);
  341.       }
  342.       l = (struct objc_list*)sarray_get_safe (__objc_selector_array, i);
  343.     }
  344.   else
  345.     {
  346.       __objc_selector_max_index += 1;
  347.       i = soffset_encode(__objc_selector_max_index);
  348.       if (orig)
  349. j = orig;
  350.       else
  351. j = objc_malloc (sizeof (struct objc_selector));
  352.       j->sel_id = (void*)i;
  353.       /* Can we use the pointer or must copy types?  Don't copy if NULL */
  354.       if ((is_const) || (types == 0))
  355. j->sel_types = (const char*)types;
  356.       else {
  357. j->sel_types = (char *) objc_malloc(strlen(types)+1);
  358. strcpy((char *)j->sel_types, types);
  359.       }
  360.       l = 0;
  361.     }
  362.   DEBUG_PRINTF ("Record selector %s[%s] as: %ldn", name, types ?: "<NULL>", 
  363. soffset_decode (i));
  364.   
  365.   {
  366.     int is_new = (l == 0);
  367.     const char *new_name;
  368.     /* Can we use the pointer or must copy name?  Don't copy if NULL */
  369.     if ((is_const) || (name == 0))
  370.       new_name = name;
  371.     else {
  372.       new_name = (char *) objc_malloc(strlen(name)+1);
  373.       strcpy((char *)new_name, name);
  374.     }
  375.     l = list_cons ((void*)j, l);
  376.     sarray_at_put_safe (__objc_selector_names, i, (void *) new_name);
  377.     sarray_at_put_safe (__objc_selector_array, i, (void *) l);
  378.     if (is_new)
  379.       hash_add (&__objc_selector_hash, (void *) new_name, (void *) i);
  380.   }
  381.   sarray_realloc(__objc_uninstalled_dtable, __objc_selector_max_index+1);
  382.   return (SEL) j;
  383. }
  384. SEL
  385. sel_register_name (const char *name)
  386. {
  387.   SEL ret;
  388.     
  389.   objc_mutex_lock(__objc_runtime_mutex);
  390.   /* Assume that name is not constant static memory and needs to be
  391.      copied before put into a runtime structure.  is_const == NO */
  392.   ret = __sel_register_typed_name (name, 0, 0, NO);
  393.   objc_mutex_unlock(__objc_runtime_mutex);
  394.   
  395.   return ret;
  396. }
  397. SEL
  398. sel_register_typed_name (const char *name, const char *type)
  399. {
  400.   SEL ret;
  401.     
  402.   objc_mutex_lock(__objc_runtime_mutex);
  403.   /* Assume that name and type are not constant static memory and need to
  404.      be copied before put into a runtime structure.  is_const == NO */
  405.   ret = __sel_register_typed_name (name, type, 0, NO);
  406.   objc_mutex_unlock(__objc_runtime_mutex);
  407.   
  408.   return ret;
  409. }