OSDN Git Service

Replace use of __objc_xcalloc, __objc_xrealloc, and __objc_xmalloc
[pf3gnuchains/gcc-fork.git] / gcc / objc / selector.c
1 /* GNU Objective C Runtime selector related functions
2    Copyright (C) 1993, 1995, 1996 Free Software Foundation, Inc.
3    Contributed by Kresten Krab Thorup
4
5 This file is part of GNU CC.
6
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.
10
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
14 details.
15
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.  */
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"
27 #include "objc/sarray.h"
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 */
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 */
37
38 static void register_selectors_from_list(MethodList_t);
39
40 /* Number of selectors stored in each of the above tables */
41 int __objc_selector_max_index = 0;              /* !T:MUTEX */
42
43 void __objc_init_selector_tables()
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.  */
55 void
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    */
76 static void
77 register_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 
84         = sel_register_typed_name ((const char*)method->method_name, 
85                                      method->method_types);
86       i += 1;
87     }
88 }
89
90
91 /* Returns YES iff t1 and t2 have same method types, but we ignore
92    the argframe layout */
93 BOOL
94 sel_types_match (const char* t1, const char* t2)
95 {
96   if (!t1 || !t2)
97     return NO;
98   while (*t1 && *t2)
99     {
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);
108       if (!*t1 && !*t2)
109         return YES;
110       if (*t1 != *t2)
111         return NO;
112       t1++;
113       t2++;
114     }
115   return NO;
116 }
117
118 /* return selector representing name */
119 SEL
120 sel_get_typed_uid (const char *name, const char *types)
121 {
122   struct objc_list *l;
123   sidx i;
124
125   objc_mutex_lock(__objc_runtime_mutex);
126
127   i = (sidx) hash_value_for_key (__objc_selector_hash, name);
128   if (i == 0)
129     {
130       objc_mutex_unlock(__objc_runtime_mutex);
131       return 0;
132     }
133
134   for (l = (struct objc_list*)sarray_get (__objc_selector_array, i);
135        l; l = l->tail)
136     {
137       SEL s = (SEL)l->head;
138       if (types == 0 || s->sel_types == 0)
139         {
140           if (s->sel_types == types)
141             {
142               objc_mutex_unlock(__objc_runtime_mutex);
143               return s;
144             }
145         }
146       else if (sel_types_match (s->sel_types, types))
147         {
148           objc_mutex_unlock(__objc_runtime_mutex);
149           return s;
150         }
151     }
152
153   objc_mutex_unlock(__objc_runtime_mutex);
154   return 0;
155 }
156
157 /* Return selector representing name; prefer a selector with non-NULL type */
158 SEL
159 sel_get_any_typed_uid (const char *name)
160 {
161   struct objc_list *l;
162   sidx i;
163   SEL s;
164
165   objc_mutex_lock(__objc_runtime_mutex);
166
167   i = (sidx) hash_value_for_key (__objc_selector_hash, name);
168   if (i == 0)
169     {
170       objc_mutex_unlock(__objc_runtime_mutex);
171       return 0;
172     }
173
174   for (l = (struct objc_list*)sarray_get (__objc_selector_array, i);
175        l; l = l->tail)
176     {
177       s = (SEL) l->head;
178       if (s->sel_types)
179         {
180             objc_mutex_unlock(__objc_runtime_mutex);
181             return s;
182         }
183     }
184
185   objc_mutex_unlock(__objc_runtime_mutex);
186   return s;
187 }
188
189 /* return selector representing name */
190 SEL
191 sel_get_any_uid (const char *name)
192 {
193   struct objc_list *l;
194   sidx i;
195
196   objc_mutex_lock(__objc_runtime_mutex);
197
198   i = (sidx) hash_value_for_key (__objc_selector_hash, name);
199   if (soffset_decode (i) == 0)
200     {
201       objc_mutex_unlock(__objc_runtime_mutex);
202       return 0;
203     }
204
205   l = (struct objc_list*)sarray_get (__objc_selector_array, i);
206   objc_mutex_unlock(__objc_runtime_mutex);
207
208   if (l == 0)
209     return 0;
210
211   return (SEL)l->head;
212 }
213
214 /* return selector representing name */
215 SEL
216 sel_get_uid (const char *name)
217 {
218   return sel_register_typed_name (name, 0);
219 }
220
221 /* Get name of selector.  If selector is unknown, the empty string "" 
222    is returned */ 
223 const char*
224 sel_get_name (SEL selector)
225 {
226   const char *ret;
227
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);
232   else
233     ret = 0;
234   objc_mutex_unlock(__objc_runtime_mutex);
235   return ret;
236 }
237
238 BOOL
239 sel_is_mapped (SEL selector)
240 {
241   unsigned int idx = soffset_decode ((sidx)selector->sel_id);
242   return ((idx > 0) && (idx <= __objc_selector_max_index));
243 }
244
245
246 const char*
247 sel_get_type (SEL selector)
248 {
249   if (selector)
250     return selector->sel_types;
251   else
252     return 0;
253 }
254
255 /* The uninstalled dispatch table */
256 extern struct sarray* __objc_uninstalled_dtable;
257
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. */
261 /* is_const parameter tells us if the name and types parameters
262    are really constant or not.  If YES then they are constant and
263    we can just store the pointers.  If NO then we need to copy
264    name and types because the pointers may disappear later on. */
265 SEL
266 __sel_register_typed_name (const char *name, const char *types, 
267                            struct objc_selector *orig, BOOL is_const)
268 {
269   struct objc_selector* j;
270   sidx i;
271   struct objc_list *l;
272
273   i = (sidx) hash_value_for_key (__objc_selector_hash, name);
274   if (soffset_decode (i) != 0)
275     {
276       for (l = (struct objc_list*)sarray_get (__objc_selector_array, i);
277            l; l = l->tail)
278         {
279           SEL s = (SEL)l->head;
280           if (types == 0 || s->sel_types == 0)
281             {
282               if (s->sel_types == types)
283                 {
284                   if (orig)
285                     {
286                       orig->sel_id = (void*)i;
287                       return orig;
288                     }
289                   else
290                     return s;
291                 }
292             }
293           else if (!strcmp (s->sel_types, types))
294             {
295               if (orig)
296                 {
297                   orig->sel_id = (void*)i;
298                   return orig;
299                 }
300               else
301                 return s;
302             }
303         }
304       if (orig)
305         j = orig;
306       else
307         j = objc_malloc (sizeof (struct objc_selector));
308
309       j->sel_id = (void*)i;
310       /* Can we use the pointer or must copy types?  Don't copy if NULL */
311       if ((is_const) || (types == 0))
312         j->sel_types = (const char*)types;
313       else {
314         j->sel_types = (char *) objc_malloc(strlen(types)+1);
315         strcpy(j->sel_types, types);
316       }
317       l = (struct objc_list*)sarray_get (__objc_selector_array, i);
318     }
319   else
320     {
321       __objc_selector_max_index += 1;
322       i = soffset_encode(__objc_selector_max_index);
323       if (orig)
324         j = orig;
325       else
326         j = objc_malloc (sizeof (struct objc_selector));
327         
328       j->sel_id = (void*)i;
329       /* Can we use the pointer or must copy types?  Don't copy if NULL */
330       if ((is_const) || (types == 0))
331         j->sel_types = (const char*)types;
332       else {
333         j->sel_types = (char *) objc_malloc(strlen(types)+1);
334         strcpy(j->sel_types, types);
335       }
336       l = 0;
337     }
338
339   DEBUG_PRINTF ("Record selector %s[%s] as: %ld\n", name, types, 
340                 soffset_decode (i));
341   
342   {
343     int is_new = (l == 0);
344     char *new_name;
345
346     /* Can we use the pointer or must copy name?  Don't copy if NULL */
347     if ((is_const) || (name == 0))
348       new_name = name;
349     else {
350       new_name = (char *) objc_malloc(strlen(name)+1);
351       strcpy(new_name, name);
352     }
353
354     l = list_cons ((void*)j, l);
355     sarray_at_put_safe (__objc_selector_names, i, (void *) new_name);
356     sarray_at_put_safe (__objc_selector_array, i, (void *) l);
357     if (is_new)
358       hash_add (&__objc_selector_hash, (void *) new_name, (void *) i);
359   }
360
361   sarray_realloc(__objc_uninstalled_dtable, __objc_selector_max_index+1);
362
363   return (SEL) j;
364 }
365
366 SEL
367 sel_register_name (const char *name)
368 {
369   SEL ret;
370     
371   objc_mutex_lock(__objc_runtime_mutex);
372   /* Assume that name is not constant static memory and needs to be
373      copied before put into a runtime structure.  is_const == NO */
374   ret = __sel_register_typed_name (name, 0, 0, NO);
375   objc_mutex_unlock(__objc_runtime_mutex);
376   
377   return ret;
378 }
379
380 SEL
381 sel_register_typed_name (const char *name, const char *type)
382 {
383   SEL ret;
384     
385   objc_mutex_lock(__objc_runtime_mutex);
386   /* Assume that name and type are not constant static memory and need to
387      be copied before put into a runtime structure.  is_const == NO */
388   ret = __sel_register_typed_name (name, type, 0, NO);
389   objc_mutex_unlock(__objc_runtime_mutex);
390   
391   return ret;
392 }
393