home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 6 / FreshFish_September1994.bin / bbs / gnu / gcc-2.6.0-src.lha / GNU / src / amiga / gcc-2.6.0 / objc / selector.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-11  |  6.7 KB  |  271 lines

  1. /* GNU Objective C Runtime selector related functions
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4. Author: Kresten Krab Thorup
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is free software; you can redistribute it and/or modify it under the
  9.    terms of the GNU General Public License as published by the Free Software
  10.    Foundation; either version 2, or (at your option) any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
  13.    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14.    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  15.    details.
  16.  
  17. You should have received a copy of the GNU General Public License along with
  18.    GNU CC; see the file COPYING.  If not, write to the Free Software
  19.    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* As a special exception, if you link this library with files compiled with
  22.    GCC to produce an executable, this does not cause the resulting executable
  23.    to be covered by the GNU General Public License. This exception does not
  24.    however invalidate any other reasons why the executable file might be
  25.    covered by the GNU General Public License.  */
  26.  
  27. #include "runtime.h"
  28. #include "objc/sarray.h"
  29.  
  30. /* Initial selector hash table size. Value doesnt matter much */
  31. #define SELECTOR_HASH_SIZE 128
  32.  
  33. /* Tables mapping selector names to uid and opposite */
  34. static struct sarray* __objc_selector_array = 0; /* uid -> sel */
  35. static struct sarray* __objc_selector_names = 0; /* uid -> name */
  36. static cache_ptr      __objc_selector_hash  = 0; /* name -> uid */
  37.  
  38. static void register_selectors_from_list(MethodList_t);
  39.  
  40. /* Number of selectors stored in each of the above tables */
  41. int __objc_selector_max_index = 0;
  42.  
  43. void __objc_init_selector_tables()
  44. {
  45.   __objc_selector_array = sarray_new (SELECTOR_HASH_SIZE, 0);
  46.   __objc_selector_names = sarray_new (SELECTOR_HASH_SIZE, 0);
  47.   __objc_selector_hash
  48.     = hash_new (SELECTOR_HASH_SIZE,
  49.         (hash_func_type) hash_string,
  50.         (compare_func_type) compare_strings);
  51. }  
  52.  
  53. /* This routine is given a class and records all of the methods in its class
  54.    structure in the record table.  */
  55. void
  56. __objc_register_selectors_from_class (Class* class)
  57. {
  58.   MethodList_t method_list;
  59.  
  60.   method_list = class->methods;
  61.   while (method_list)
  62.     {
  63.       register_selectors_from_list (method_list);
  64.       method_list = method_list->method_next;
  65.     }
  66. }
  67.  
  68.  
  69. /* This routine is given a list of methods and records each of the methods in
  70.    the record table.  This is the routine that does the actual recording
  71.    work.
  72.  
  73.    This one is only called for Class objects.  For categories,
  74.    class_add_method_list is called.
  75.    */
  76. static void
  77. register_selectors_from_list (MethodList_t method_list)
  78. {
  79.   int i = 0;
  80.   while (i < method_list->method_count)
  81.     {
  82.       Method_t method = &method_list->method_list[i];
  83.       method->method_name 
  84.     = sel_register_typed_name ((const char*)method->method_name, 
  85.                      method->method_types);
  86.       i += 1;
  87.     }
  88. }
  89.  
  90. /* return selector representing name */
  91. SEL
  92. sel_get_typed_uid (const char *name, const char *types)
  93. {
  94.   struct objc_list *l;
  95.   sidx i;
  96.  
  97.   i = (sidx) hash_value_for_key (__objc_selector_hash, name);
  98.   if (i == 0)
  99.     return 0;
  100.  
  101.   for (l = (struct objc_list*)sarray_get (__objc_selector_array, i);
  102.        l; l = l->tail)
  103.     {
  104.       SEL s = (SEL)l->head;
  105.       if (types == 0 || s->sel_types == 0)
  106.     {
  107.       if (s->sel_types == types)
  108.         {
  109.           return s;
  110.         }
  111.     }
  112.       else if (! strcmp (s->sel_types, types))
  113.     {
  114.       return s;
  115.     }
  116.     }
  117.  
  118.   return 0;
  119. }
  120.  
  121. /* return selector representing name */
  122. SEL
  123. sel_get_any_uid (const char *name)
  124. {
  125.   struct objc_list *l;
  126.   sidx i;
  127.  
  128.   i = (sidx) hash_value_for_key (__objc_selector_hash, name);
  129.   if (soffset_decode (i) == 0)
  130.     return 0;
  131.  
  132.   l = (struct objc_list*)sarray_get (__objc_selector_array, i);
  133.   if (l == 0)
  134.     return 0;
  135.  
  136.   return (SEL)l->head;
  137. }
  138.  
  139. /* return selector representing name */
  140. SEL
  141. sel_get_uid (const char *name)
  142. {
  143.   return sel_register_typed_name (name, 0);
  144. }
  145.  
  146. /* Get name of selector.  If selector is unknown, the empty string "" 
  147.    is returned */ 
  148. const char*
  149. sel_get_name (SEL selector)
  150. {
  151.   if ((soffset_decode((sidx)selector->sel_id) > 0)
  152.       && (soffset_decode((sidx)selector->sel_id) <= __objc_selector_max_index))
  153.     return sarray_get (__objc_selector_array, (sidx) selector->sel_id);
  154.   else
  155.     return 0;
  156. }
  157.  
  158. BOOL
  159. sel_is_mapped (SEL selector)
  160. {
  161.   unsigned int idx = soffset_decode ((sidx)selector->sel_id);
  162.   return ((idx > 0) && (idx <= __objc_selector_max_index));
  163. }
  164.  
  165.  
  166. const char*
  167. sel_get_type (SEL selector)
  168. {
  169.   if (selector)
  170.     return selector->sel_types;
  171.   else
  172.     return 0;
  173. }
  174.  
  175. /* The uninstalled dispatch table */
  176. extern struct sarray* __objc_uninstalled_dtable;
  177.  
  178. /* Store the passed selector name in the selector record and return its
  179.    selector value (value returned by sel_get_uid). */
  180. SEL
  181. __sel_register_typed_name (const char *name, const char *types, 
  182.                struct objc_selector *orig)
  183. {
  184.   struct objc_selector* j;
  185.   sidx i;
  186.   struct objc_list *l;
  187.  
  188.   i = (sidx) hash_value_for_key (__objc_selector_hash, name);
  189.   if (soffset_decode (i) != 0)
  190.     {
  191.       for (l = (struct objc_list*)sarray_get (__objc_selector_array, i);
  192.        l; l = l->tail)
  193.     {
  194.       SEL s = (SEL)l->head;
  195.       if (types == 0 || s->sel_types == 0)
  196.         {
  197.           if (s->sel_types == types)
  198.         {
  199.           if (orig)
  200.             {
  201.               orig->sel_id = (void*)i;
  202.               return orig;
  203.             }
  204.           else
  205.             return s;
  206.         }
  207.         }
  208.       else if (strcmp (s->sel_types, types))
  209.         {
  210.           if (orig)
  211.         {
  212.           orig->sel_id = (void*)i;
  213.           return orig;
  214.         }
  215.           else
  216.         return s;
  217.         }
  218.     }
  219.       if (orig)
  220.     j = orig;
  221.       else
  222.     j = __objc_xmalloc (sizeof (struct objc_selector));
  223.  
  224.       j->sel_id = (void*)i;
  225.       j->sel_types = (const char*)types;
  226.       l = (struct objc_list*)sarray_get (__objc_selector_array, i);
  227.     }
  228.   else
  229.     {
  230.       __objc_selector_max_index += 1;
  231.       i = soffset_encode(__objc_selector_max_index);
  232.       if (orig)
  233.     j = orig;
  234.       else
  235.     j = __objc_xmalloc (sizeof (struct objc_selector));
  236.     
  237.       j->sel_id = (void*)i;
  238.       j->sel_types = (const char*)types;
  239.       l = 0;
  240.     }
  241.  
  242.   DEBUG_PRINTF ("Record selector %s[%s] as: %ld\n", name, types, 
  243.         soffset_decode (i));
  244.   
  245.   {
  246.     int is_new = (l == 0);
  247.     l = list_cons ((void*)j, l);
  248.     sarray_at_put_safe (__objc_selector_names, i, (void *) name);
  249.     sarray_at_put_safe (__objc_selector_array, i, (void *) l);
  250.     if (is_new)
  251.       hash_add (&__objc_selector_hash, (void *) name, (void *) i);
  252.   }
  253.  
  254.   sarray_realloc(__objc_uninstalled_dtable, __objc_selector_max_index+1);
  255.  
  256.   return (SEL) j;
  257. }
  258.  
  259. SEL
  260. sel_register_name (const char *name)
  261. {
  262.   return __sel_register_typed_name (name, 0, 0);
  263. }
  264.  
  265. SEL
  266. sel_register_typed_name (const char *name, const char *type)
  267. {
  268.   return __sel_register_typed_name (name, type, 0);
  269. }
  270.  
  271.