1 /* GNU Objective C Runtime selector related functions
2 Copyright (C) 1993, 1995, 1996 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. */
27 #include "objc/sarray.h"
30 /* Initial selector hash table size. Value doesn't matter much */
31 #define SELECTOR_HASH_SIZE 128
33 /* Tables mapping selector names to uid and opposite */
34 static struct sarray
* __objc_selector_array
= 0; /* uid -> sel !T:MUTEX */
35 static struct sarray
* __objc_selector_names
= 0; /* uid -> name !T:MUTEX */
36 static cache_ptr __objc_selector_hash
= 0; /* name -> uid !T:MUTEX */
38 static void register_selectors_from_list(MethodList_t
);
40 /* Number of selectors stored in each of the above tables */
41 int __objc_selector_max_index
= 0; /* !T:MUTEX */
43 void __objc_init_selector_tables()
45 __objc_selector_array
= sarray_new (SELECTOR_HASH_SIZE
, 0);
46 __objc_selector_names
= sarray_new (SELECTOR_HASH_SIZE
, 0);
48 = hash_new (SELECTOR_HASH_SIZE
,
49 (hash_func_type
) hash_string
,
50 (compare_func_type
) compare_strings
);
53 /* This routine is given a class and records all of the methods in its class
54 structure in the record table. */
56 __objc_register_selectors_from_class (Class
class)
58 MethodList_t method_list
;
60 method_list
= class->methods
;
63 register_selectors_from_list (method_list
);
64 method_list
= method_list
->method_next
;
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
73 This one is only called for Class objects. For categories,
74 class_add_method_list is called.
77 register_selectors_from_list (MethodList_t method_list
)
80 while (i
< method_list
->method_count
)
82 Method_t method
= &method_list
->method_list
[i
];
84 = sel_register_typed_name ((const char*)method
->method_name
,
85 method
->method_types
);
91 /* Returns YES iff t1 and t2 have same method types, but we ignore
92 the argframe layout */
94 sel_types_match (const char* t1
, const char* t2
)
100 if (*t1
== '+') t1
++;
101 if (*t2
== '+') t2
++;
102 while (isdigit(*t1
)) t1
++;
103 while (isdigit(*t2
)) t2
++;
104 /* xxx Remove these next two lines when qualifiers are put in
105 all selectors, not just Protocol selectors. */
106 t1
= objc_skip_type_qualifiers(t1
);
107 t2
= objc_skip_type_qualifiers(t2
);
118 /* return selector representing name */
120 sel_get_typed_uid (const char *name
, const char *types
)
125 objc_mutex_lock(__objc_runtime_mutex
);
127 i
= (sidx
) hash_value_for_key (__objc_selector_hash
, name
);
130 objc_mutex_unlock(__objc_runtime_mutex
);
134 for (l
= (struct objc_list
*)sarray_get (__objc_selector_array
, i
);
137 SEL s
= (SEL
)l
->head
;
138 if (types
== 0 || s
->sel_types
== 0)
140 if (s
->sel_types
== types
)
142 objc_mutex_unlock(__objc_runtime_mutex
);
146 else if (sel_types_match (s
->sel_types
, types
))
148 objc_mutex_unlock(__objc_runtime_mutex
);
153 objc_mutex_unlock(__objc_runtime_mutex
);
157 /* Return selector representing name; prefer a selector with non-NULL type */
159 sel_get_any_typed_uid (const char *name
)
165 objc_mutex_lock(__objc_runtime_mutex
);
167 i
= (sidx
) hash_value_for_key (__objc_selector_hash
, name
);
170 objc_mutex_unlock(__objc_runtime_mutex
);
174 for (l
= (struct objc_list
*)sarray_get (__objc_selector_array
, i
);
180 objc_mutex_unlock(__objc_runtime_mutex
);
185 objc_mutex_unlock(__objc_runtime_mutex
);
189 /* return selector representing name */
191 sel_get_any_uid (const char *name
)
196 objc_mutex_lock(__objc_runtime_mutex
);
198 i
= (sidx
) hash_value_for_key (__objc_selector_hash
, name
);
199 if (soffset_decode (i
) == 0)
201 objc_mutex_unlock(__objc_runtime_mutex
);
205 l
= (struct objc_list
*)sarray_get (__objc_selector_array
, i
);
206 objc_mutex_unlock(__objc_runtime_mutex
);
214 /* return selector representing name */
216 sel_get_uid (const char *name
)
218 return sel_register_typed_name (name
, 0);
221 /* Get name of selector. If selector is unknown, the empty string ""
224 sel_get_name (SEL selector
)
228 objc_mutex_lock(__objc_runtime_mutex
);
229 if ((soffset_decode((sidx
)selector
->sel_id
) > 0)
230 && (soffset_decode((sidx
)selector
->sel_id
) <= __objc_selector_max_index
))
231 ret
= sarray_get (__objc_selector_names
, (sidx
) selector
->sel_id
);
234 objc_mutex_unlock(__objc_runtime_mutex
);
239 sel_is_mapped (SEL selector
)
241 unsigned int idx
= soffset_decode ((sidx
)selector
->sel_id
);
242 return ((idx
> 0) && (idx
<= __objc_selector_max_index
));
247 sel_get_type (SEL selector
)
250 return selector
->sel_types
;
255 /* The uninstalled dispatch table */
256 extern struct sarray
* __objc_uninstalled_dtable
;
258 /* Store the passed selector name in the selector record and return its
259 selector value (value returned by sel_get_uid).
260 Assumes that the calling function has locked down __objc_runtime_mutex. */
262 __sel_register_typed_name (const char *name
, const char *types
,
263 struct objc_selector
*orig
)
265 struct objc_selector
* j
;
269 i
= (sidx
) hash_value_for_key (__objc_selector_hash
, name
);
270 if (soffset_decode (i
) != 0)
272 for (l
= (struct objc_list
*)sarray_get (__objc_selector_array
, i
);
275 SEL s
= (SEL
)l
->head
;
276 if (types
== 0 || s
->sel_types
== 0)
278 if (s
->sel_types
== types
)
282 orig
->sel_id
= (void*)i
;
289 else if (!strcmp (s
->sel_types
, types
))
293 orig
->sel_id
= (void*)i
;
303 j
= __objc_xmalloc (sizeof (struct objc_selector
));
305 j
->sel_id
= (void*)i
;
306 j
->sel_types
= (const char*)types
;
307 l
= (struct objc_list
*)sarray_get (__objc_selector_array
, i
);
311 __objc_selector_max_index
+= 1;
312 i
= soffset_encode(__objc_selector_max_index
);
316 j
= __objc_xmalloc (sizeof (struct objc_selector
));
318 j
->sel_id
= (void*)i
;
319 j
->sel_types
= (const char*)types
;
323 DEBUG_PRINTF ("Record selector %s[%s] as: %ld\n", name
, types
,
327 int is_new
= (l
== 0);
328 l
= list_cons ((void*)j
, l
);
329 sarray_at_put_safe (__objc_selector_names
, i
, (void *) name
);
330 sarray_at_put_safe (__objc_selector_array
, i
, (void *) l
);
332 hash_add (&__objc_selector_hash
, (void *) name
, (void *) i
);
335 sarray_realloc(__objc_uninstalled_dtable
, __objc_selector_max_index
+1);
341 sel_register_name (const char *name
)
345 objc_mutex_lock(__objc_runtime_mutex
);
346 ret
= __sel_register_typed_name (name
, 0, 0);
347 objc_mutex_unlock(__objc_runtime_mutex
);
353 sel_register_typed_name (const char *name
, const char *type
)
357 objc_mutex_lock(__objc_runtime_mutex
);
358 ret
= __sel_register_typed_name (name
, type
, 0);
359 objc_mutex_unlock(__objc_runtime_mutex
);