]>
Commit | Line | Data |
---|---|---|
88e17b57 | 1 | /* GNU Objective C Runtime selector related functions |
748086b7 | 2 | Copyright (C) 1993, 1995, 1996, 1997, 2002, 2004, 2009 Free Software Foundation, Inc. |
88e17b57 BE |
3 | Contributed by Kresten Krab Thorup |
4 | ||
38709cad | 5 | This file is part of GCC. |
88e17b57 | 6 | |
38709cad | 7 | GCC is free software; you can redistribute it and/or modify it under the |
88e17b57 | 8 | terms of the GNU General Public License as published by the Free Software |
748086b7 | 9 | Foundation; either version 3, or (at your option) any later version. |
88e17b57 | 10 | |
38709cad | 11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
88e17b57 BE |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
13 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | |
14 | details. | |
15 | ||
748086b7 JJ |
16 | Under Section 7 of GPL version 3, you are granted additional |
17 | permissions described in the GCC Runtime Library Exception, version | |
18 | 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | You should have received a copy of the GNU General Public License and | |
21 | a copy of the GCC Runtime Library Exception along with this program; | |
22 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | <http://www.gnu.org/licenses/>. */ | |
88e17b57 | 24 | |
6dead247 | 25 | #include "objc-private/common.h" |
9ecfa8de | 26 | #include "objc/runtime.h" |
a19fac96 | 27 | #include "objc/thr.h" |
5be9cdc1 | 28 | #include "objc-private/hash.h" |
9ecfa8de NP |
29 | #include "objc-private/objc-list.h" |
30 | #include "objc-private/module-abi-8.h" | |
a19fac96 | 31 | #include "objc-private/runtime.h" |
5d3b14bd | 32 | #include "objc-private/sarray.h" |
1af5b8f5 | 33 | #include "objc-private/selector.h" |
5750872c | 34 | #include <stdlib.h> /* For malloc. */ |
88e17b57 | 35 | |
d4645ada | 36 | /* Initial selector hash table size. Value doesn't matter much. */ |
88e17b57 BE |
37 | #define SELECTOR_HASH_SIZE 128 |
38 | ||
d4645ada | 39 | /* Tables mapping selector names to uid and opposite. */ |
40165636 RB |
40 | static struct sarray *__objc_selector_array = 0; /* uid -> sel !T:MUTEX */ |
41 | static struct sarray *__objc_selector_names = 0; /* uid -> name !T:MUTEX */ | |
88e17b57 BE |
42 | static cache_ptr __objc_selector_hash = 0; /* name -> uid !T:MUTEX */ |
43 | ||
d4645ada | 44 | /* Number of selectors stored in each of the above tables. */ |
b62cc13a | 45 | unsigned int __objc_selector_max_index = 0; /* !T:MUTEX */ |
88e17b57 | 46 | |
600cbba2 NP |
47 | /* Forward-declare an internal function. */ |
48 | static SEL | |
49 | __sel_register_typed_name (const char *name, const char *types, | |
50 | struct objc_selector *orig, BOOL is_const); | |
51 | ||
64cbe55e | 52 | void __objc_init_selector_tables (void) |
88e17b57 BE |
53 | { |
54 | __objc_selector_array = sarray_new (SELECTOR_HASH_SIZE, 0); | |
55 | __objc_selector_names = sarray_new (SELECTOR_HASH_SIZE, 0); | |
56 | __objc_selector_hash | |
270a1283 DA |
57 | = objc_hash_new (SELECTOR_HASH_SIZE, |
58 | (hash_func_type) objc_hash_string, | |
59 | (compare_func_type) objc_compare_strings); | |
88e17b57 BE |
60 | } |
61 | ||
600cbba2 NP |
62 | /* Register a bunch of selectors from the table of selectors in a |
63 | module. 'selectors' should not be NULL. The list is terminated by | |
64 | a selectors with a NULL sel_id. The selectors are assumed to | |
65 | contain the 'name' in the sel_id field; this is replaced with the | |
66 | final selector id after they are registered. */ | |
67 | void | |
68 | __objc_register_selectors_from_module (struct objc_selector *selectors) | |
69 | { | |
70 | int i; | |
71 | ||
72 | for (i = 0; selectors[i].sel_id; ++i) | |
73 | { | |
74 | const char *name, *type; | |
75 | name = (char *) selectors[i].sel_id; | |
76 | type = (char *) selectors[i].sel_types; | |
77 | /* Constructors are constant static data and we can safely store | |
78 | pointers to them in the runtime structures, so we set | |
79 | is_const == YES. */ | |
80 | __sel_register_typed_name (name, type, (struct objc_selector *) &(selectors[i]), | |
81 | /* is_const */ YES); | |
82 | } | |
83 | } | |
84 | ||
d4645ada NP |
85 | /* This routine is given a class and records all of the methods in its |
86 | class structure in the record table. */ | |
88e17b57 BE |
87 | void |
88 | __objc_register_selectors_from_class (Class class) | |
89 | { | |
9ecfa8de | 90 | struct objc_method_list * method_list; |
88e17b57 BE |
91 | |
92 | method_list = class->methods; | |
93 | while (method_list) | |
94 | { | |
435317e2 | 95 | __objc_register_selectors_from_list (method_list); |
88e17b57 BE |
96 | method_list = method_list->method_next; |
97 | } | |
98 | } | |
99 | ||
100 | ||
d4645ada NP |
101 | /* This routine is given a list of methods and records each of the |
102 | methods in the record table. This is the routine that does the | |
103 | actual recording work. | |
88e17b57 | 104 | |
435317e2 | 105 | The name and type pointers in the method list must be permanent and |
d4645ada | 106 | immutable. */ |
435317e2 | 107 | void |
9ecfa8de | 108 | __objc_register_selectors_from_list (struct objc_method_list *method_list) |
88e17b57 BE |
109 | { |
110 | int i = 0; | |
435317e2 AP |
111 | |
112 | objc_mutex_lock (__objc_runtime_mutex); | |
88e17b57 BE |
113 | while (i < method_list->method_count) |
114 | { | |
9ecfa8de | 115 | Method method = &method_list->method_list[i]; |
435317e2 AP |
116 | if (method->method_name) |
117 | { | |
118 | method->method_name | |
119 | = __sel_register_typed_name ((const char *) method->method_name, | |
120 | method->method_types, 0, YES); | |
121 | } | |
88e17b57 BE |
122 | i += 1; |
123 | } | |
435317e2 | 124 | objc_mutex_unlock (__objc_runtime_mutex); |
88e17b57 BE |
125 | } |
126 | ||
f7185d47 NP |
127 | /* The same as __objc_register_selectors_from_list, but works on a |
128 | struct objc_method_description_list* instead of a struct | |
129 | objc_method_list*. This is only used for protocols, which have | |
d4645ada | 130 | lists of method descriptions, not methods. */ |
f7185d47 NP |
131 | void |
132 | __objc_register_selectors_from_description_list | |
133 | (struct objc_method_description_list *method_list) | |
134 | { | |
135 | int i = 0; | |
136 | ||
137 | objc_mutex_lock (__objc_runtime_mutex); | |
138 | while (i < method_list->count) | |
139 | { | |
140 | struct objc_method_description *method = &method_list->list[i]; | |
141 | if (method->name) | |
142 | { | |
143 | method->name | |
144 | = __sel_register_typed_name ((const char *) method->name, | |
145 | method->types, 0, YES); | |
146 | } | |
147 | i += 1; | |
148 | } | |
149 | objc_mutex_unlock (__objc_runtime_mutex); | |
150 | } | |
88e17b57 | 151 | |
d4645ada | 152 | /* Register instance methods as class methods for root classes. */ |
40165636 | 153 | void __objc_register_instance_methods_to_class (Class class) |
88e17b57 | 154 | { |
9ecfa8de NP |
155 | struct objc_method_list *method_list; |
156 | struct objc_method_list *class_method_list; | |
88e17b57 | 157 | int max_methods_no = 16; |
9ecfa8de NP |
158 | struct objc_method_list *new_list; |
159 | Method curr_method; | |
88e17b57 BE |
160 | |
161 | /* Only if a root class. */ | |
40165636 | 162 | if (class->super_class) |
88e17b57 BE |
163 | return; |
164 | ||
d4645ada | 165 | /* Allocate a method list to hold the new class methods. */ |
40165636 | 166 | new_list = objc_calloc (sizeof (struct objc_method_list) |
d4645ada | 167 | + sizeof (struct objc_method[max_methods_no]), 1); |
88e17b57 BE |
168 | method_list = class->methods; |
169 | class_method_list = class->class_pointer->methods; | |
170 | curr_method = &new_list->method_list[0]; | |
d4645ada NP |
171 | |
172 | /* Iterate through the method lists for the class. */ | |
88e17b57 BE |
173 | while (method_list) |
174 | { | |
175 | int i; | |
d4645ada NP |
176 | |
177 | /* Iterate through the methods from this method list. */ | |
88e17b57 BE |
178 | for (i = 0; i < method_list->method_count; i++) |
179 | { | |
9ecfa8de | 180 | Method mth = &method_list->method_list[i]; |
88e17b57 | 181 | if (mth->method_name |
40165636 | 182 | && ! search_for_method_in_list (class_method_list, |
88e17b57 BE |
183 | mth->method_name)) |
184 | { | |
d4645ada NP |
185 | /* This instance method isn't a class method. Add it |
186 | into the new_list. */ | |
88e17b57 | 187 | *curr_method = *mth; |
d4645ada NP |
188 | |
189 | /* Reallocate the method list if necessary. */ | |
40165636 | 190 | if (++new_list->method_count == max_methods_no) |
88e17b57 | 191 | new_list = |
40165636 RB |
192 | objc_realloc (new_list, sizeof (struct objc_method_list) |
193 | + sizeof (struct | |
d4645ada | 194 | objc_method[max_methods_no += 16])); |
88e17b57 BE |
195 | curr_method = &new_list->method_list[new_list->method_count]; |
196 | } | |
197 | } | |
198 | ||
199 | method_list = method_list->method_next; | |
200 | } | |
201 | ||
d4645ada NP |
202 | /* If we created any new class methods then attach the method list |
203 | to the class. */ | |
88e17b57 BE |
204 | if (new_list->method_count) |
205 | { | |
206 | new_list = | |
40165636 | 207 | objc_realloc (new_list, sizeof (struct objc_method_list) |
d4645ada | 208 | + sizeof (struct objc_method[new_list->method_count])); |
88e17b57 BE |
209 | new_list->method_next = class->class_pointer->methods; |
210 | class->class_pointer->methods = new_list; | |
211 | } | |
5af0e6ae AF |
212 | else |
213 | objc_free(new_list); | |
d4645ada NP |
214 | |
215 | __objc_update_dispatch_table_for_class (class->class_pointer); | |
88e17b57 BE |
216 | } |
217 | ||
debfbfee NP |
218 | BOOL |
219 | sel_isEqual (SEL s1, SEL s2) | |
220 | { | |
221 | if (s1 == 0 || s2 == 0) | |
222 | return s1 == s2; | |
223 | else | |
224 | return s1->sel_id == s2->sel_id; | |
225 | } | |
88e17b57 | 226 | |
d4645ada NP |
227 | /* Return YES iff t1 and t2 have same method types. Ignore the |
228 | argframe layout. */ | |
88e17b57 | 229 | BOOL |
40165636 | 230 | sel_types_match (const char *t1, const char *t2) |
88e17b57 | 231 | { |
40165636 | 232 | if (! t1 || ! t2) |
88e17b57 BE |
233 | return NO; |
234 | while (*t1 && *t2) | |
235 | { | |
236 | if (*t1 == '+') t1++; | |
237 | if (*t2 == '+') t2++; | |
40165636 RB |
238 | while (isdigit ((unsigned char) *t1)) t1++; |
239 | while (isdigit ((unsigned char) *t2)) t2++; | |
88e17b57 | 240 | /* xxx Remove these next two lines when qualifiers are put in |
d4645ada | 241 | all selectors, not just Protocol selectors. */ |
40165636 RB |
242 | t1 = objc_skip_type_qualifiers (t1); |
243 | t2 = objc_skip_type_qualifiers (t2); | |
244 | if (! *t1 && ! *t2) | |
88e17b57 BE |
245 | return YES; |
246 | if (*t1 != *t2) | |
247 | return NO; | |
248 | t1++; | |
249 | t2++; | |
250 | } | |
251 | return NO; | |
252 | } | |
253 | ||
5750872c NP |
254 | /* Return selector representing name. In the Modern API, you'd |
255 | normally use sel_registerTypedName() for this, which does the same | |
256 | but would register the selector with the runtime if not registered | |
257 | yet (if you only want to check for selectors without registering, | |
258 | use sel_copyTypedSelectorList()). */ | |
88e17b57 BE |
259 | SEL |
260 | sel_get_typed_uid (const char *name, const char *types) | |
261 | { | |
262 | struct objc_list *l; | |
263 | sidx i; | |
264 | ||
40165636 | 265 | objc_mutex_lock (__objc_runtime_mutex); |
88e17b57 | 266 | |
270a1283 | 267 | i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name); |
88e17b57 BE |
268 | if (i == 0) |
269 | { | |
40165636 | 270 | objc_mutex_unlock (__objc_runtime_mutex); |
88e17b57 BE |
271 | return 0; |
272 | } | |
273 | ||
40165636 | 274 | for (l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i); |
88e17b57 BE |
275 | l; l = l->tail) |
276 | { | |
40165636 | 277 | SEL s = (SEL) l->head; |
88e17b57 BE |
278 | if (types == 0 || s->sel_types == 0) |
279 | { | |
280 | if (s->sel_types == types) | |
281 | { | |
40165636 | 282 | objc_mutex_unlock (__objc_runtime_mutex); |
88e17b57 BE |
283 | return s; |
284 | } | |
285 | } | |
286 | else if (sel_types_match (s->sel_types, types)) | |
287 | { | |
40165636 | 288 | objc_mutex_unlock (__objc_runtime_mutex); |
88e17b57 BE |
289 | return s; |
290 | } | |
291 | } | |
292 | ||
40165636 | 293 | objc_mutex_unlock (__objc_runtime_mutex); |
88e17b57 BE |
294 | return 0; |
295 | } | |
296 | ||
d4645ada | 297 | /* Return selector representing name; prefer a selector with non-NULL |
5750872c NP |
298 | type. In the Modern API, sel_getTypedSelector() is similar but |
299 | returns NULL if a typed selector couldn't be found. */ | |
88e17b57 BE |
300 | SEL |
301 | sel_get_any_typed_uid (const char *name) | |
302 | { | |
303 | struct objc_list *l; | |
304 | sidx i; | |
305 | SEL s = NULL; | |
306 | ||
40165636 | 307 | objc_mutex_lock (__objc_runtime_mutex); |
88e17b57 | 308 | |
270a1283 | 309 | i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name); |
88e17b57 BE |
310 | if (i == 0) |
311 | { | |
40165636 | 312 | objc_mutex_unlock (__objc_runtime_mutex); |
88e17b57 BE |
313 | return 0; |
314 | } | |
315 | ||
40165636 | 316 | for (l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i); |
88e17b57 BE |
317 | l; l = l->tail) |
318 | { | |
319 | s = (SEL) l->head; | |
320 | if (s->sel_types) | |
321 | { | |
48d69c57 NP |
322 | objc_mutex_unlock (__objc_runtime_mutex); |
323 | return s; | |
88e17b57 BE |
324 | } |
325 | } | |
326 | ||
40165636 | 327 | objc_mutex_unlock (__objc_runtime_mutex); |
88e17b57 BE |
328 | return s; |
329 | } | |
330 | ||
d4645ada | 331 | /* Return selector representing name. */ |
88e17b57 BE |
332 | SEL |
333 | sel_get_any_uid (const char *name) | |
334 | { | |
335 | struct objc_list *l; | |
336 | sidx i; | |
337 | ||
40165636 | 338 | objc_mutex_lock (__objc_runtime_mutex); |
88e17b57 | 339 | |
270a1283 | 340 | i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name); |
88e17b57 BE |
341 | if (soffset_decode (i) == 0) |
342 | { | |
40165636 | 343 | objc_mutex_unlock (__objc_runtime_mutex); |
88e17b57 BE |
344 | return 0; |
345 | } | |
346 | ||
40165636 RB |
347 | l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i); |
348 | objc_mutex_unlock (__objc_runtime_mutex); | |
88e17b57 BE |
349 | |
350 | if (l == 0) | |
351 | return 0; | |
352 | ||
40165636 | 353 | return (SEL) l->head; |
88e17b57 BE |
354 | } |
355 | ||
5750872c NP |
356 | SEL |
357 | sel_getTypedSelector (const char *name) | |
358 | { | |
359 | sidx i; | |
360 | objc_mutex_lock (__objc_runtime_mutex); | |
361 | ||
362 | /* Look for a typed selector. */ | |
363 | i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name); | |
364 | if (i != 0) | |
365 | { | |
366 | struct objc_list *l; | |
367 | ||
368 | for (l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i); | |
369 | l; l = l->tail) | |
370 | { | |
371 | SEL s = (SEL) l->head; | |
372 | if (s->sel_types) | |
373 | { | |
374 | objc_mutex_unlock (__objc_runtime_mutex); | |
375 | return s; | |
376 | } | |
377 | } | |
378 | } | |
379 | ||
380 | /* No typed selector found. Return NULL. */ | |
381 | objc_mutex_unlock (__objc_runtime_mutex); | |
382 | return 0; | |
383 | } | |
384 | ||
385 | SEL * | |
386 | sel_copyTypedSelectorList (const char *name, unsigned int *numberOfReturnedSelectors) | |
387 | { | |
388 | unsigned int count = 0; | |
389 | SEL *returnValue = NULL; | |
390 | sidx i; | |
391 | ||
392 | if (name == NULL) | |
393 | { | |
394 | if (numberOfReturnedSelectors) | |
395 | *numberOfReturnedSelectors = 0; | |
396 | return NULL; | |
397 | } | |
398 | ||
399 | objc_mutex_lock (__objc_runtime_mutex); | |
400 | ||
401 | /* Count how many selectors we have. */ | |
402 | i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name); | |
403 | if (i != 0) | |
404 | { | |
405 | struct objc_list *selector_list = NULL; | |
406 | selector_list = (struct objc_list *) sarray_get_safe (__objc_selector_array, i); | |
407 | ||
408 | /* Count how many selectors we have. */ | |
409 | { | |
410 | struct objc_list *l; | |
411 | for (l = selector_list; l; l = l->tail) | |
412 | count++; | |
413 | } | |
414 | ||
415 | if (count != 0) | |
416 | { | |
417 | /* Allocate enough memory to hold them. */ | |
418 | returnValue = (SEL *)(malloc (sizeof (SEL) * (count + 1))); | |
419 | ||
420 | /* Copy the selectors. */ | |
421 | { | |
422 | unsigned int j; | |
423 | for (j = 0; j < count; j++) | |
424 | { | |
425 | returnValue[j] = (SEL)(selector_list->head); | |
426 | selector_list = selector_list->tail; | |
427 | } | |
428 | returnValue[j] = NULL; | |
429 | } | |
430 | } | |
431 | } | |
432 | ||
433 | objc_mutex_unlock (__objc_runtime_mutex); | |
434 | ||
435 | if (numberOfReturnedSelectors) | |
436 | *numberOfReturnedSelectors = count; | |
437 | ||
438 | return returnValue; | |
439 | } | |
440 | ||
d4645ada NP |
441 | /* Get the name of a selector. If the selector is unknown, the empty |
442 | string "" is returned. */ | |
bc18535a | 443 | const char *sel_getName (SEL selector) |
88e17b57 BE |
444 | { |
445 | const char *ret; | |
446 | ||
524660d2 NP |
447 | if (selector == NULL) |
448 | return "<null selector>"; | |
d4645ada | 449 | |
40165636 RB |
450 | objc_mutex_lock (__objc_runtime_mutex); |
451 | if ((soffset_decode ((sidx)selector->sel_id) > 0) | |
452 | && (soffset_decode ((sidx)selector->sel_id) <= __objc_selector_max_index)) | |
88e17b57 BE |
453 | ret = sarray_get_safe (__objc_selector_names, (sidx) selector->sel_id); |
454 | else | |
455 | ret = 0; | |
40165636 | 456 | objc_mutex_unlock (__objc_runtime_mutex); |
88e17b57 BE |
457 | return ret; |
458 | } | |
459 | ||
bc18535a NP |
460 | /* Traditional GNU Objective-C Runtime API. */ |
461 | const char *sel_get_name (SEL selector) | |
462 | { | |
524660d2 NP |
463 | if (selector == NULL) |
464 | return 0; | |
465 | ||
bc18535a NP |
466 | return sel_getName (selector); |
467 | } | |
468 | ||
88e17b57 BE |
469 | BOOL |
470 | sel_is_mapped (SEL selector) | |
471 | { | |
472 | unsigned int idx = soffset_decode ((sidx)selector->sel_id); | |
473 | return ((idx > 0) && (idx <= __objc_selector_max_index)); | |
474 | } | |
475 | ||
5750872c | 476 | const char *sel_getTypeEncoding (SEL selector) |
88e17b57 BE |
477 | { |
478 | if (selector) | |
479 | return selector->sel_types; | |
480 | else | |
481 | return 0; | |
482 | } | |
483 | ||
bc18535a NP |
484 | /* Traditional GNU Objective-C Runtime API. */ |
485 | const char *sel_get_type (SEL selector) | |
486 | { | |
5750872c | 487 | return sel_getTypeEncoding (selector); |
bc18535a NP |
488 | } |
489 | ||
d4645ada | 490 | /* The uninstalled dispatch table. */ |
40165636 | 491 | extern struct sarray *__objc_uninstalled_dtable; |
88e17b57 | 492 | |
435317e2 | 493 | /* __sel_register_typed_name allocates lots of struct objc_selector:s |
d4645ada NP |
494 | of 8 (16, if pointers are 64 bits) bytes at startup. To reduce the |
495 | number of malloc calls and memory lost to malloc overhead, we | |
496 | allocate objc_selector:s in blocks here. This is only called from | |
497 | __sel_register_typed_name, and __sel_register_typed_name may only | |
498 | be called when __objc_runtime_mutex is locked. | |
499 | ||
500 | Note that the objc_selector:s allocated from | |
501 | __sel_register_typed_name are never freed. | |
502 | ||
503 | 62 because 62 * sizeof (struct objc_selector) = 496 (992). This | |
504 | should let malloc add some overhead and use a nice, round 512 | |
505 | (1024) byte chunk. */ | |
435317e2 AP |
506 | #define SELECTOR_POOL_SIZE 62 |
507 | static struct objc_selector *selector_pool; | |
508 | static int selector_pool_left; | |
509 | ||
510 | static struct objc_selector * | |
511 | pool_alloc_selector(void) | |
512 | { | |
513 | if (!selector_pool_left) | |
514 | { | |
515 | selector_pool = objc_malloc (sizeof (struct objc_selector) | |
516 | * SELECTOR_POOL_SIZE); | |
517 | selector_pool_left = SELECTOR_POOL_SIZE; | |
518 | } | |
519 | return &selector_pool[--selector_pool_left]; | |
520 | } | |
521 | ||
d4645ada NP |
522 | /* Store the passed selector name in the selector record and return |
523 | its selector value (value returned by sel_get_uid). Assume that | |
524 | the calling function has locked down __objc_runtime_mutex. The | |
600cbba2 | 525 | 'is_const' parameter tells us if the name and types parameters are |
d4645ada NP |
526 | really constant or not. If YES then they are constant and we can |
527 | just store the pointers. If NO then we need to copy name and types | |
600cbba2 NP |
528 | because the pointers may disappear later on. If the 'orig' |
529 | parameter is not NULL, then we are registering a selector from a | |
530 | module, and 'orig' is that selector. In this case, we can put the | |
531 | selector in the tables if needed, and orig->sel_id is updated with | |
532 | the selector ID of the registered selector, and 'orig' is | |
533 | returned. */ | |
534 | static SEL | |
88e17b57 BE |
535 | __sel_register_typed_name (const char *name, const char *types, |
536 | struct objc_selector *orig, BOOL is_const) | |
537 | { | |
40165636 | 538 | struct objc_selector *j; |
88e17b57 BE |
539 | sidx i; |
540 | struct objc_list *l; | |
541 | ||
270a1283 | 542 | i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name); |
88e17b57 BE |
543 | if (soffset_decode (i) != 0) |
544 | { | |
c75534d1 NP |
545 | /* There are already selectors with that name. Examine them to |
546 | see if the one we're registering already exists. */ | |
547 | for (l = (struct objc_list *)sarray_get_safe (__objc_selector_array, i); | |
88e17b57 BE |
548 | l; l = l->tail) |
549 | { | |
c75534d1 | 550 | SEL s = (SEL)l->head; |
88e17b57 BE |
551 | if (types == 0 || s->sel_types == 0) |
552 | { | |
553 | if (s->sel_types == types) | |
554 | { | |
555 | if (orig) | |
556 | { | |
c75534d1 | 557 | orig->sel_id = (void *)i; |
88e17b57 BE |
558 | return orig; |
559 | } | |
560 | else | |
561 | return s; | |
562 | } | |
563 | } | |
40165636 | 564 | else if (! strcmp (s->sel_types, types)) |
88e17b57 BE |
565 | { |
566 | if (orig) | |
567 | { | |
c75534d1 | 568 | orig->sel_id = (void *)i; |
88e17b57 BE |
569 | return orig; |
570 | } | |
571 | else | |
572 | return s; | |
573 | } | |
574 | } | |
c75534d1 NP |
575 | /* A selector with this specific name/type combination does not |
576 | exist yet. We need to register it. */ | |
88e17b57 BE |
577 | if (orig) |
578 | j = orig; | |
579 | else | |
435317e2 | 580 | j = pool_alloc_selector (); |
d4645ada | 581 | |
c75534d1 NP |
582 | j->sel_id = (void *)i; |
583 | /* Can we use the pointer or must we copy types ? Don't copy if | |
d4645ada | 584 | NULL. */ |
88e17b57 | 585 | if ((is_const) || (types == 0)) |
c75534d1 | 586 | j->sel_types = types; |
d4645ada NP |
587 | else |
588 | { | |
c75534d1 NP |
589 | j->sel_types = (char *)objc_malloc (strlen (types) + 1); |
590 | strcpy ((char *)j->sel_types, types); | |
d4645ada | 591 | } |
c75534d1 | 592 | l = (struct objc_list *)sarray_get_safe (__objc_selector_array, i); |
88e17b57 BE |
593 | } |
594 | else | |
595 | { | |
c75534d1 NP |
596 | /* There are no other selectors with this name registered in the |
597 | runtime tables. */ | |
598 | const char *new_name; | |
599 | ||
600 | /* Determine i. */ | |
88e17b57 | 601 | __objc_selector_max_index += 1; |
40165636 | 602 | i = soffset_encode (__objc_selector_max_index); |
c75534d1 NP |
603 | |
604 | /* Prepare the selector. */ | |
88e17b57 BE |
605 | if (orig) |
606 | j = orig; | |
607 | else | |
435317e2 | 608 | j = pool_alloc_selector (); |
d4645ada | 609 | |
c75534d1 NP |
610 | j->sel_id = (void *)i; |
611 | /* Can we use the pointer or must we copy types ? Don't copy if | |
d4645ada | 612 | NULL. */ |
c75534d1 NP |
613 | if (is_const || (types == 0)) |
614 | j->sel_types = types; | |
615 | else | |
616 | { | |
617 | j->sel_types = (char *)objc_malloc (strlen (types) + 1); | |
618 | strcpy ((char *)j->sel_types, types); | |
619 | } | |
620 | ||
621 | /* Since this is the first selector with this name, we need to | |
622 | register the correspondence between 'i' (the sel_id) and | |
623 | 'name' (the actual string) in __objc_selector_names and | |
624 | __objc_selector_hash. */ | |
625 | ||
626 | /* Can we use the pointer or must we copy name ? Don't copy if | |
627 | NULL. (FIXME: Can the name really be NULL here ?) */ | |
628 | if (is_const || (name == 0)) | |
629 | new_name = name; | |
d4645ada NP |
630 | else |
631 | { | |
c75534d1 NP |
632 | new_name = (char *)objc_malloc (strlen (name) + 1); |
633 | strcpy ((char *)new_name, name); | |
d4645ada | 634 | } |
c75534d1 NP |
635 | |
636 | /* This maps the sel_id to the name. */ | |
637 | sarray_at_put_safe (__objc_selector_names, i, (void *)new_name); | |
638 | ||
639 | /* This maps the name to the sel_id. */ | |
640 | objc_hash_add (&__objc_selector_hash, (void *)new_name, (void *)i); | |
641 | ||
88e17b57 BE |
642 | l = 0; |
643 | } | |
644 | ||
645 | DEBUG_PRINTF ("Record selector %s[%s] as: %ld\n", name, types, | |
c75534d1 NP |
646 | (long)soffset_decode (i)); |
647 | ||
648 | /* Now add the selector to the list of selectors with that id. */ | |
649 | l = list_cons ((void *)j, l); | |
650 | sarray_at_put_safe (__objc_selector_array, i, (void *)l); | |
651 | ||
40165636 | 652 | sarray_realloc (__objc_uninstalled_dtable, __objc_selector_max_index + 1); |
d4645ada | 653 | |
c75534d1 | 654 | return (SEL)j; |
88e17b57 BE |
655 | } |
656 | ||
657 | SEL | |
bc18535a | 658 | sel_registerName (const char *name) |
88e17b57 BE |
659 | { |
660 | SEL ret; | |
661 | ||
40165636 | 662 | objc_mutex_lock (__objc_runtime_mutex); |
88e17b57 | 663 | /* Assume that name is not constant static memory and needs to be |
d4645ada | 664 | copied before put into a runtime structure. is_const == NO. */ |
88e17b57 | 665 | ret = __sel_register_typed_name (name, 0, 0, NO); |
40165636 | 666 | objc_mutex_unlock (__objc_runtime_mutex); |
88e17b57 BE |
667 | |
668 | return ret; | |
669 | } | |
670 | ||
bc18535a | 671 | /* Traditional GNU Objective-C Runtime API. */ |
88e17b57 | 672 | SEL |
bc18535a NP |
673 | sel_register_name (const char *name) |
674 | { | |
675 | return sel_registerName (name); | |
676 | } | |
677 | ||
678 | SEL | |
679 | sel_registerTypedName (const char *name, const char *type) | |
88e17b57 BE |
680 | { |
681 | SEL ret; | |
435317e2 | 682 | |
40165636 | 683 | objc_mutex_lock (__objc_runtime_mutex); |
d4645ada NP |
684 | /* Assume that name and type are not constant static memory and need |
685 | to be copied before put into a runtime structure. is_const == | |
686 | NO. */ | |
88e17b57 | 687 | ret = __sel_register_typed_name (name, type, 0, NO); |
40165636 | 688 | objc_mutex_unlock (__objc_runtime_mutex); |
88e17b57 BE |
689 | |
690 | return ret; | |
691 | } | |
bc18535a NP |
692 | |
693 | SEL | |
694 | sel_register_typed_name (const char *name, const char *type) | |
695 | { | |
696 | return sel_registerTypedName (name, type); | |
697 | } | |
698 | ||
d4645ada | 699 | /* Return the selector representing name. */ |
bc18535a NP |
700 | SEL |
701 | sel_getUid (const char *name) | |
702 | { | |
703 | return sel_registerTypedName (name, 0); | |
704 | } | |
705 | ||
706 | /* Traditional GNU Objective-C Runtime API. */ | |
707 | SEL | |
708 | sel_get_uid (const char *name) | |
709 | { | |
710 | return sel_getUid (name); | |
711 | } |