]> gcc.gnu.org Git - gcc.git/blame - libobjc/selector.c
re PR c++/8856 (g++ accepts invalid conversion-function-id)
[gcc.git] / libobjc / selector.c
CommitLineData
88e17b57 1/* GNU Objective C Runtime selector related functions
b62cc13a 2 Copyright (C) 1993, 1995, 1996, 1997, 2002 Free Software Foundation, Inc.
88e17b57
BE
3 Contributed by Kresten Krab Thorup
4
38709cad 5This file is part of GCC.
88e17b57 6
38709cad 7GCC is free software; you can redistribute it and/or modify it under the
88e17b57
BE
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2, or (at your option) any later version.
10
38709cad 11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
88e17b57
BE
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14details.
15
16You should have received a copy of the GNU General Public License along with
38709cad 17GCC; see the file COPYING. If not, write to the Free Software
88e17b57
BE
18Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
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. */
25
26#include "runtime.h"
bce1b489 27#include "sarray.h"
88e17b57
BE
28#include "encoding.h"
29
30/* Initial selector hash table size. Value doesn't matter much */
31#define SELECTOR_HASH_SIZE 128
32
33/* Tables mapping selector names to uid and opposite */
40165636
RB
34static struct sarray *__objc_selector_array = 0; /* uid -> sel !T:MUTEX */
35static struct sarray *__objc_selector_names = 0; /* uid -> name !T:MUTEX */
88e17b57
BE
36static cache_ptr __objc_selector_hash = 0; /* name -> uid !T:MUTEX */
37
40165636 38static void register_selectors_from_list (MethodList_t);
88e17b57
BE
39
40/* Number of selectors stored in each of the above tables */
b62cc13a 41unsigned int __objc_selector_max_index = 0; /* !T:MUTEX */
88e17b57 42
40165636 43void __objc_init_selector_tables ()
88e17b57
BE
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. */
55void
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 */
76static void
77register_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
40165636
RB
84 = sel_register_typed_name ((const char *) method->method_name,
85 method->method_types);
88e17b57
BE
86 i += 1;
87 }
88}
89
90
91/* Register instance methods as class methods for root classes */
40165636 92void __objc_register_instance_methods_to_class (Class class)
88e17b57
BE
93{
94 MethodList_t method_list;
95 MethodList_t class_method_list;
96 int max_methods_no = 16;
97 MethodList_t new_list;
98 Method_t curr_method;
99
100 /* Only if a root class. */
40165636 101 if (class->super_class)
88e17b57
BE
102 return;
103
104 /* Allocate a method list to hold the new class methods */
40165636
RB
105 new_list = objc_calloc (sizeof (struct objc_method_list)
106 + sizeof (struct objc_method[max_methods_no]), 1);
88e17b57
BE
107 method_list = class->methods;
108 class_method_list = class->class_pointer->methods;
109 curr_method = &new_list->method_list[0];
110
111 /* Iterate through the method lists for the class */
112 while (method_list)
113 {
114 int i;
115
116 /* Iterate through the methods from this method list */
117 for (i = 0; i < method_list->method_count; i++)
118 {
119 Method_t mth = &method_list->method_list[i];
120 if (mth->method_name
40165636 121 && ! search_for_method_in_list (class_method_list,
88e17b57
BE
122 mth->method_name))
123 {
124 /* This instance method isn't a class method.
125 Add it into the new_list. */
126 *curr_method = *mth;
127
128 /* Reallocate the method list if necessary */
40165636 129 if (++new_list->method_count == max_methods_no)
88e17b57 130 new_list =
40165636
RB
131 objc_realloc (new_list, sizeof (struct objc_method_list)
132 + sizeof (struct
88e17b57
BE
133 objc_method[max_methods_no += 16]));
134 curr_method = &new_list->method_list[new_list->method_count];
135 }
136 }
137
138 method_list = method_list->method_next;
139 }
140
141 /* If we created any new class methods
142 then attach the method list to the class */
143 if (new_list->method_count)
144 {
145 new_list =
40165636
RB
146 objc_realloc (new_list, sizeof (struct objc_method_list)
147 + sizeof (struct objc_method[new_list->method_count]));
88e17b57
BE
148 new_list->method_next = class->class_pointer->methods;
149 class->class_pointer->methods = new_list;
150 }
151
152 __objc_update_dispatch_table_for_class (class->class_pointer);
153}
154
155
156/* Returns YES iff t1 and t2 have same method types, but we ignore
157 the argframe layout */
158BOOL
40165636 159sel_types_match (const char *t1, const char *t2)
88e17b57 160{
40165636 161 if (! t1 || ! t2)
88e17b57
BE
162 return NO;
163 while (*t1 && *t2)
164 {
165 if (*t1 == '+') t1++;
166 if (*t2 == '+') t2++;
40165636
RB
167 while (isdigit ((unsigned char) *t1)) t1++;
168 while (isdigit ((unsigned char) *t2)) t2++;
88e17b57
BE
169 /* xxx Remove these next two lines when qualifiers are put in
170 all selectors, not just Protocol selectors. */
40165636
RB
171 t1 = objc_skip_type_qualifiers (t1);
172 t2 = objc_skip_type_qualifiers (t2);
173 if (! *t1 && ! *t2)
88e17b57
BE
174 return YES;
175 if (*t1 != *t2)
176 return NO;
177 t1++;
178 t2++;
179 }
180 return NO;
181}
182
183/* return selector representing name */
184SEL
185sel_get_typed_uid (const char *name, const char *types)
186{
187 struct objc_list *l;
188 sidx i;
189
40165636 190 objc_mutex_lock (__objc_runtime_mutex);
88e17b57
BE
191
192 i = (sidx) hash_value_for_key (__objc_selector_hash, name);
193 if (i == 0)
194 {
40165636 195 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
196 return 0;
197 }
198
40165636 199 for (l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
88e17b57
BE
200 l; l = l->tail)
201 {
40165636 202 SEL s = (SEL) l->head;
88e17b57
BE
203 if (types == 0 || s->sel_types == 0)
204 {
205 if (s->sel_types == types)
206 {
40165636 207 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
208 return s;
209 }
210 }
211 else if (sel_types_match (s->sel_types, types))
212 {
40165636 213 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
214 return s;
215 }
216 }
217
40165636 218 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
219 return 0;
220}
221
222/* Return selector representing name; prefer a selector with non-NULL type */
223SEL
224sel_get_any_typed_uid (const char *name)
225{
226 struct objc_list *l;
227 sidx i;
228 SEL s = NULL;
229
40165636 230 objc_mutex_lock (__objc_runtime_mutex);
88e17b57
BE
231
232 i = (sidx) hash_value_for_key (__objc_selector_hash, name);
233 if (i == 0)
234 {
40165636 235 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
236 return 0;
237 }
238
40165636 239 for (l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
88e17b57
BE
240 l; l = l->tail)
241 {
242 s = (SEL) l->head;
243 if (s->sel_types)
244 {
40165636 245 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
246 return s;
247 }
248 }
249
40165636 250 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
251 return s;
252}
253
254/* return selector representing name */
255SEL
256sel_get_any_uid (const char *name)
257{
258 struct objc_list *l;
259 sidx i;
260
40165636 261 objc_mutex_lock (__objc_runtime_mutex);
88e17b57
BE
262
263 i = (sidx) hash_value_for_key (__objc_selector_hash, name);
264 if (soffset_decode (i) == 0)
265 {
40165636 266 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
267 return 0;
268 }
269
40165636
RB
270 l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
271 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
272
273 if (l == 0)
274 return 0;
275
40165636 276 return (SEL) l->head;
88e17b57
BE
277}
278
279/* return selector representing name */
280SEL
281sel_get_uid (const char *name)
282{
283 return sel_register_typed_name (name, 0);
284}
285
286/* Get name of selector. If selector is unknown, the empty string ""
287 is returned */
40165636 288const char *sel_get_name (SEL selector)
88e17b57
BE
289{
290 const char *ret;
291
40165636
RB
292 objc_mutex_lock (__objc_runtime_mutex);
293 if ((soffset_decode ((sidx)selector->sel_id) > 0)
294 && (soffset_decode ((sidx)selector->sel_id) <= __objc_selector_max_index))
88e17b57
BE
295 ret = sarray_get_safe (__objc_selector_names, (sidx) selector->sel_id);
296 else
297 ret = 0;
40165636 298 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
299 return ret;
300}
301
302BOOL
303sel_is_mapped (SEL selector)
304{
305 unsigned int idx = soffset_decode ((sidx)selector->sel_id);
306 return ((idx > 0) && (idx <= __objc_selector_max_index));
307}
308
309
40165636 310const char *sel_get_type (SEL selector)
88e17b57
BE
311{
312 if (selector)
313 return selector->sel_types;
314 else
315 return 0;
316}
317
318/* The uninstalled dispatch table */
40165636 319extern struct sarray *__objc_uninstalled_dtable;
88e17b57
BE
320
321/* Store the passed selector name in the selector record and return its
322 selector value (value returned by sel_get_uid).
323 Assumes that the calling function has locked down __objc_runtime_mutex. */
324/* is_const parameter tells us if the name and types parameters
325 are really constant or not. If YES then they are constant and
326 we can just store the pointers. If NO then we need to copy
327 name and types because the pointers may disappear later on. */
328SEL
329__sel_register_typed_name (const char *name, const char *types,
330 struct objc_selector *orig, BOOL is_const)
331{
40165636 332 struct objc_selector *j;
88e17b57
BE
333 sidx i;
334 struct objc_list *l;
335
336 i = (sidx) hash_value_for_key (__objc_selector_hash, name);
337 if (soffset_decode (i) != 0)
338 {
40165636 339 for (l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
88e17b57
BE
340 l; l = l->tail)
341 {
40165636 342 SEL s = (SEL) l->head;
88e17b57
BE
343 if (types == 0 || s->sel_types == 0)
344 {
345 if (s->sel_types == types)
346 {
347 if (orig)
348 {
40165636 349 orig->sel_id = (void *) i;
88e17b57
BE
350 return orig;
351 }
352 else
353 return s;
354 }
355 }
40165636 356 else if (! strcmp (s->sel_types, types))
88e17b57
BE
357 {
358 if (orig)
359 {
40165636 360 orig->sel_id = (void *) i;
88e17b57
BE
361 return orig;
362 }
363 else
364 return s;
365 }
366 }
367 if (orig)
368 j = orig;
369 else
370 j = objc_malloc (sizeof (struct objc_selector));
371
40165636 372 j->sel_id = (void *) i;
88e17b57
BE
373 /* Can we use the pointer or must copy types? Don't copy if NULL */
374 if ((is_const) || (types == 0))
40165636 375 j->sel_types = (const char *) types;
88e17b57 376 else {
40165636
RB
377 j->sel_types = (char *) objc_malloc (strlen (types) + 1);
378 strcpy ((char *) j->sel_types, types);
88e17b57 379 }
40165636 380 l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
88e17b57
BE
381 }
382 else
383 {
384 __objc_selector_max_index += 1;
40165636 385 i = soffset_encode (__objc_selector_max_index);
88e17b57
BE
386 if (orig)
387 j = orig;
388 else
389 j = objc_malloc (sizeof (struct objc_selector));
390
40165636 391 j->sel_id = (void *) i;
88e17b57
BE
392 /* Can we use the pointer or must copy types? Don't copy if NULL */
393 if ((is_const) || (types == 0))
40165636 394 j->sel_types = (const char *) types;
88e17b57 395 else {
40165636
RB
396 j->sel_types = (char *) objc_malloc (strlen (types) + 1);
397 strcpy ((char *) j->sel_types, types);
88e17b57
BE
398 }
399 l = 0;
400 }
401
402 DEBUG_PRINTF ("Record selector %s[%s] as: %ld\n", name, types,
403 soffset_decode (i));
404
405 {
406 int is_new = (l == 0);
407 const char *new_name;
408
409 /* Can we use the pointer or must copy name? Don't copy if NULL */
410 if ((is_const) || (name == 0))
411 new_name = name;
412 else {
40165636
RB
413 new_name = (char *) objc_malloc (strlen (name) + 1);
414 strcpy ((char *) new_name, name);
88e17b57
BE
415 }
416
40165636 417 l = list_cons ((void *) j, l);
88e17b57
BE
418 sarray_at_put_safe (__objc_selector_names, i, (void *) new_name);
419 sarray_at_put_safe (__objc_selector_array, i, (void *) l);
420 if (is_new)
421 hash_add (&__objc_selector_hash, (void *) new_name, (void *) i);
422 }
423
40165636 424 sarray_realloc (__objc_uninstalled_dtable, __objc_selector_max_index + 1);
88e17b57
BE
425
426 return (SEL) j;
427}
428
429SEL
430sel_register_name (const char *name)
431{
432 SEL ret;
433
40165636 434 objc_mutex_lock (__objc_runtime_mutex);
88e17b57
BE
435 /* Assume that name is not constant static memory and needs to be
436 copied before put into a runtime structure. is_const == NO */
437 ret = __sel_register_typed_name (name, 0, 0, NO);
40165636 438 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
439
440 return ret;
441}
442
443SEL
444sel_register_typed_name (const char *name, const char *type)
445{
446 SEL ret;
447
40165636 448 objc_mutex_lock (__objc_runtime_mutex);
88e17b57
BE
449 /* Assume that name and type are not constant static memory and need to
450 be copied before put into a runtime structure. is_const == NO */
451 ret = __sel_register_typed_name (name, type, 0, NO);
40165636 452 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
453
454 return ret;
455}
This page took 0.423738 seconds and 5 git commands to generate.