1 /* GNU Objective C Runtime selector related functions
2 Copyright (C) 1993 Free Software Foundation, Inc.
4 Author: Kresten Krab Thorup
6 This file is part of GNU CC.
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.
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
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. */
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. */
28 #include "objc/sarray.h"
31 /* Initial selector hash table size. Value doesnt matter much */
32 #define SELECTOR_HASH_SIZE 128
34 /* Tables mapping selector names to uid and opposite */
35 static struct sarray* __objc_selector_array = 0; /* uid -> sel */
36 static struct sarray* __objc_selector_names = 0; /* uid -> name */
37 static cache_ptr __objc_selector_hash = 0; /* name -> uid */
39 static void register_selectors_from_list(MethodList_t);
41 /* Number of selectors stored in each of the above tables */
42 int __objc_selector_max_index = 0;
44 void __objc_init_selector_tables()
46 __objc_selector_array = sarray_new (SELECTOR_HASH_SIZE, 0);
47 __objc_selector_names = sarray_new (SELECTOR_HASH_SIZE, 0);
49 = hash_new (SELECTOR_HASH_SIZE,
50 (hash_func_type) hash_string,
51 (compare_func_type) compare_strings);
54 /* This routine is given a class and records all of the methods in its class
55 structure in the record table. */
57 __objc_register_selectors_from_class (Class* class)
59 MethodList_t method_list;
61 method_list = class->methods;
64 register_selectors_from_list (method_list);
65 method_list = method_list->method_next;
70 /* This routine is given a list of methods and records each of the methods in
71 the record table. This is the routine that does the actual recording
74 This one is only called for Class objects. For categories,
75 class_add_method_list is called.
78 register_selectors_from_list (MethodList_t method_list)
81 while (i < method_list->method_count)
83 Method_t method = &method_list->method_list[i];
85 = sel_register_typed_name ((const char*)method->method_name,
86 method->method_types);
92 /* Returns YES iff t1 and t2 have same method types, but we ignore
93 the argframe layout */
95 sel_types_match (const char* t1, const char* t2)
101 if (*t1 == '+') t1++;
102 if (*t2 == '+') t2++;
103 while (isdigit(*t1)) t1++;
104 while (isdigit(*t2)) t2++;
105 /* xxx Remove these next two lines when qualifiers are put in
106 all selectors, not just Protocol selectors. */
107 t1 = objc_skip_type_qualifiers(t1);
108 t2 = objc_skip_type_qualifiers(t2);
119 /* return selector representing name */
121 sel_get_typed_uid (const char *name, const char *types)
126 i = (sidx) hash_value_for_key (__objc_selector_hash, name);
130 for (l = (struct objc_list*)sarray_get (__objc_selector_array, i);
133 SEL s = (SEL)l->head;
134 if (types == 0 || s->sel_types == 0)
136 if (s->sel_types == types)
141 else if (sel_types_match (s->sel_types, types))
150 /* return selector representing name */
152 sel_get_any_uid (const char *name)
157 i = (sidx) hash_value_for_key (__objc_selector_hash, name);
158 if (soffset_decode (i) == 0)
161 l = (struct objc_list*)sarray_get (__objc_selector_array, i);
168 /* return selector representing name */
170 sel_get_uid (const char *name)
172 return sel_register_typed_name (name, 0);
175 /* Get name of selector. If selector is unknown, the empty string ""
178 sel_get_name (SEL selector)
180 if ((soffset_decode((sidx)selector->sel_id) > 0)
181 && (soffset_decode((sidx)selector->sel_id) <= __objc_selector_max_index))
182 return sarray_get (__objc_selector_names, (sidx) selector->sel_id);
188 sel_is_mapped (SEL selector)
190 unsigned int idx = soffset_decode ((sidx)selector->sel_id);
191 return ((idx > 0) && (idx <= __objc_selector_max_index));
196 sel_get_type (SEL selector)
199 return selector->sel_types;
204 /* The uninstalled dispatch table */
205 extern struct sarray* __objc_uninstalled_dtable;
207 /* Store the passed selector name in the selector record and return its
208 selector value (value returned by sel_get_uid). */
210 __sel_register_typed_name (const char *name, const char *types,
211 struct objc_selector *orig)
213 struct objc_selector* j;
217 i = (sidx) hash_value_for_key (__objc_selector_hash, name);
218 if (soffset_decode (i) != 0)
220 for (l = (struct objc_list*)sarray_get (__objc_selector_array, i);
223 SEL s = (SEL)l->head;
224 if (types == 0 || s->sel_types == 0)
226 if (s->sel_types == types)
230 orig->sel_id = (void*)i;
237 else if (!strcmp (s->sel_types, types))
241 orig->sel_id = (void*)i;
251 j = __objc_xmalloc (sizeof (struct objc_selector));
253 j->sel_id = (void*)i;
254 j->sel_types = (const char*)types;
255 l = (struct objc_list*)sarray_get (__objc_selector_array, i);
259 __objc_selector_max_index += 1;
260 i = soffset_encode(__objc_selector_max_index);
264 j = __objc_xmalloc (sizeof (struct objc_selector));
266 j->sel_id = (void*)i;
267 j->sel_types = (const char*)types;
271 DEBUG_PRINTF ("Record selector %s[%s] as: %ld\n", name, types,
275 int is_new = (l == 0);
276 l = list_cons ((void*)j, l);
277 sarray_at_put_safe (__objc_selector_names, i, (void *) name);
278 sarray_at_put_safe (__objc_selector_array, i, (void *) l);
280 hash_add (&__objc_selector_hash, (void *) name, (void *) i);
283 sarray_realloc(__objc_uninstalled_dtable, __objc_selector_max_index+1);
289 sel_register_name (const char *name)
291 return __sel_register_typed_name (name, 0, 0);
295 sel_register_typed_name (const char *name, const char *type)
297 return __sel_register_typed_name (name, type, 0);