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
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2, or (at your option) any later version.
11 GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 You should have received a copy of the GNU General Public License along with
17 GNU CC; see the file COPYING. If not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* As a special exception, if you link this library with files compiled with
21 GCC to produce an executable, this does not cause the resulting executable
22 to be covered by the GNU General Public License. This exception does not
23 however invalidate any other reasons why the executable file might be
24 covered by the GNU General Public License. */
32 /* Initial selector hash table size. Value doesn't matter much */
33 #define SELECTOR_HASH_SIZE 128
35 static void register_selectors_from_list(MethodList_t);
37 void __objc_init_selector_tables()
39 __objc_selector_array = sarray_new (SELECTOR_HASH_SIZE);
40 __objc_selector_names = sarray_new (SELECTOR_HASH_SIZE);
41 __objc_selector_hash = strhash_new(SELECTOR_HASH_SIZE);
44 /* This routine is given a class and records all of the methods in its class
45 structure in the record table. */
46 void __objc_register_selectors_from_class (Class class)
48 register MethodList_t method_list;
50 method_list = class->methods;
52 register_selectors_from_list (method_list);
53 method_list = method_list->method_next;
58 /* This routine is given a list of methods and records each of the methods in
59 the record table. This is the routine that does the actual recording
62 This one is only called for Class objects. For categories,
63 class_add_method_list is called.
65 static void register_selectors_from_list (MethodList_t method_list)
68 while (i < method_list->method_count)
70 Method_t method = &method_list->method_list[i];
72 = sel_register_typed_name ((const char*)method->method_name,
73 method->method_types);
79 /* Register instance methods as class methods for root classes */
80 void __objc_register_instance_methods_to_class(Class class)
82 MethodList_t method_list;
83 MethodList_t class_method_list;
84 int max_methods_no = 16;
85 MethodList_t new_list;
88 /* Only if a root class. */
89 if(class->super_class)
92 /* Allocate a method list to hold the new class methods */
94 OBJC_CALLOC_UNCOLLECTABLE(sizeof(struct objc_method_list) +
95 sizeof(struct objc_method[max_methods_no]));
97 method_list = class->methods;
98 class_method_list = class->class_pointer->methods;
99 curr_method = &new_list->method_list[0];
101 /* Iterate through the method lists for the class */
106 /* Iterate through the methods from this method list */
107 for (i = 0; i < method_list->method_count; i++)
109 Method_t mth = &method_list->method_list[i];
111 && !search_for_method_in_list (class_method_list,
114 /* This instance method isn't a class method.
115 Add it into the new_list. */
118 /* Reallocate the method list if necessary */
119 if(++new_list->method_count == max_methods_no)
121 objc_realloc(new_list, sizeof(struct objc_method_list)
123 objc_method[max_methods_no += 16]));
124 curr_method = &new_list->method_list[new_list->method_count];
128 method_list = method_list->method_next;
131 /* If we created any new class methods
132 then attach the method list to the class */
133 if (new_list->method_count)
136 objc_realloc(new_list, sizeof(struct objc_method_list)
137 + sizeof(struct objc_method[new_list->method_count]));
138 new_list->method_next = class->class_pointer->methods;
139 class->class_pointer->methods = new_list;
142 __objc_update_dispatch_table_for_class (class->class_pointer);
146 /* Returns YES iff t1 and t2 have same method types, but we ignore
147 the argframe layout */
148 BOOL sel_types_match (const char* t1, const char* t2)
154 if (*t1 == '+') t1++;
155 if (*t2 == '+') t2++;
156 while (isdigit((int)*t1)) t1++;
157 while (isdigit((int)*t2)) t2++;
158 /* xxx Remove these next two lines when qualifiers are put in
159 all selectors, not just Protocol selectors. */
160 t1 = objc_skip_type_qualifiers(t1);
161 t2 = objc_skip_type_qualifiers(t2);
172 /* return selector representing name */
173 SEL sel_get_typed_uid (const char *name, const char *types)
180 i = (sidx) hash_value_for_key (__objc_selector_hash, name);
187 for (l = (struct objc_list*)sarray_get_safe (__objc_selector_array, i);
190 SEL s = (SEL)l->head;
191 if (types == 0 || s->sel_types == 0)
193 if (s->sel_types == types)
199 else if (sel_types_match (s->sel_types, types))
210 /* Return selector representing name; prefer a selector with non-NULL type */
211 SEL sel_get_any_typed_uid (const char *name)
219 i = (sidx) hash_value_for_key (__objc_selector_hash, name);
226 for (l = (struct objc_list*)sarray_get_safe (__objc_selector_array, i);
241 /* return selector representing name */
242 SEL sel_get_any_uid (const char *name)
249 i = (sidx) hash_value_for_key (__objc_selector_hash, name);
255 l = (struct objc_list*)sarray_get_safe (__objc_selector_array, i);
264 /* return selector representing name */
265 SEL sel_get_uid (const char *name)
267 return sel_register_typed_name (name, 0);
270 /* Get name of selector. If selector is unknown, the empty string ""
272 const char* sel_get_name (SEL selector)
277 ret = ((selector->sel_id > 0)
278 && ((unsigned int)selector->sel_id <= __objc_selector_max_index))
279 ? sarray_get_safe (__objc_selector_names, (sidx) selector->sel_id)
285 BOOL sel_is_mapped (SEL selector)
287 register unsigned int idx = (unsigned int)selector->sel_id;
288 return ((idx > 0) && (idx <= __objc_selector_max_index));
292 const char* sel_get_type (SEL selector)
294 return selector ? selector->sel_types : 0;
297 /* Store the passed selector name in the selector record and return its
298 selector value (value returned by sel_get_uid).
299 Assumes that the calling function has locked down __objc_runtime_mutex. */
300 /* is_const parameter tells us if the name and types parameters
301 are really constant or not. If YES then they are constant and
302 we can just store the pointers. If NO then we need to copy
303 name and types because the pointers may disappear later on. */
305 __sel_register_typed_name (const char *name, const char *types,
306 struct objc_selector *orig, BOOL is_const)
308 struct objc_selector* j;
312 i = (sidx) hash_value_for_key (__objc_selector_hash, name);
314 for (l = (struct objc_list*)sarray_get_safe (__objc_selector_array, i);
316 SEL s = (SEL)l->head;
317 if (types == 0 || s->sel_types == 0) {
318 if (s->sel_types == types) {
320 orig->sel_id = (void*)i;
327 else if (!strcmp (s->sel_types, types)) {
329 orig->sel_id = (void*)i;
339 j = OBJC_MALLOC_UNCOLLECTABLE(sizeof(struct objc_selector));
341 j->sel_id = (void*)i;
342 /* Can we use the pointer or must copy types? Don't copy if NULL */
343 if ((is_const) || (types == 0)) {
344 j->sel_types = (const char*)types;
347 j->sel_types = (char *)OBJC_MALLOC_UNCOLLECTABLE(strlen(types) + 1);
348 strcpy((char *)j->sel_types, types);
350 l = (struct objc_list*)sarray_get_safe (__objc_selector_array, i);
353 __objc_selector_max_index += 1;
354 i = __objc_selector_max_index;
359 j = OBJC_MALLOC_UNCOLLECTABLE (sizeof (struct objc_selector));
361 j->sel_id = (void*)i;
362 /* Can we use the pointer or must copy types? Don't copy if NULL */
363 if ((is_const) || (types == 0))
364 j->sel_types = (const char*)types;
366 j->sel_types = (char *)OBJC_MALLOC_UNCOLLECTABLE(strlen(types)+1);
368 strcpy((char *)j->sel_types, types);
373 DEBUG_PRINTF ("Record selector %s[%s] as: %ld\n",
374 name ? name : "<NULL>", types ? types : "<NULL>",
378 int is_new = (l == 0);
379 const char *new_name;
381 /* Can we use the pointer or must copy name? Don't copy if NULL */
382 if ((is_const) || (name == 0)) {
386 new_name = (char *)OBJC_MALLOC_UNCOLLECTABLE(strlen(name) + 1);
387 strcpy((char *)new_name, name);
390 l = list_cons ((void*)j, l);
391 sarray_at_put_safe (__objc_selector_names, i, (void *) new_name);
392 sarray_at_put_safe (__objc_selector_array, i, (void *) l);
394 hash_add (&__objc_selector_hash, (void *) new_name, (void *) i);
397 //hh:sarray_realloc(__objc_uninstalled_dtable, __objc_selector_max_index + 1);
402 SEL sel_register_name (const char *name)
407 /* Assume that name is not constant static memory and needs to be
408 copied before put into a runtime structure. is_const == NO */
409 ret = __sel_register_typed_name (name, 0, 0, NO);
415 SEL sel_register_typed_name (const char *name, const char *type)
420 /* Assume that name and type are not constant static memory and need to
421 be copied before put into a runtime structure. is_const == NO */
422 ret = __sel_register_typed_name (name, type, 0, NO);