]> git.ipfire.org Git - thirdparty/gcc.git/blame - libobjc/sendmsg.c
In libobjc/: 2010-12-21 Nicola Pero <nicola.pero@meta-innovation.com>
[thirdparty/gcc.git] / libobjc / sendmsg.c
CommitLineData
88e17b57 1/* GNU Objective C Runtime message lookup
0fc39d8a 2 Copyright (C) 1993, 1995, 1996, 1997, 1998,
ad9eef11 3 2001, 2002, 2004, 2009, 2010 Free Software Foundation, Inc.
88e17b57
BE
4 Contributed by Kresten Krab Thorup
5
38709cad 6This file is part of GCC.
88e17b57 7
38709cad 8GCC is free software; you can redistribute it and/or modify it under the
88e17b57 9terms of the GNU General Public License as published by the Free Software
748086b7 10Foundation; either version 3, or (at your option) any later version.
88e17b57 11
38709cad 12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
88e17b57
BE
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15details.
16
748086b7
JJ
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24<http://www.gnu.org/licenses/>. */
88e17b57 25
88e17b57 26
4977bab6 27/* FIXME: This file has no business including tm.h. */
435317e2
AP
28/* FIXME: This should be using libffi instead of __builtin_apply
29 and friends. */
4977bab6 30
6dead247 31#include "objc-private/common.h"
7b869986 32#include "objc-private/error.h"
bce1b489 33#include "tconfig.h"
4977bab6
ZW
34#include "coretypes.h"
35#include "tm.h"
114dae43 36#include "objc/runtime.h"
5ec582f9 37#include "objc/message.h" /* For objc_msg_lookup(), objc_msg_lookup_super(). */
a19fac96 38#include "objc/thr.h"
114dae43 39#include "objc-private/module-abi-8.h"
a19fac96 40#include "objc-private/runtime.h"
5d3b14bd 41#include "objc-private/sarray.h"
114dae43 42#include "objc-private/selector.h" /* For sel_is_mapped() */
88e17b57 43#include "runtime-info.h"
5d3b14bd 44#include <assert.h> /* For assert */
5be9cdc1 45#include <string.h> /* For strlen */
88e17b57 46
435317e2 47/* This is how we hack STRUCT_VALUE to be 1 or 0. */
88e17b57
BE
48#define gen_rtx(args...) 1
49#define gen_rtx_MEM(args...) 1
68b61df9 50#define gen_rtx_REG(args...) 1
114dae43 51/* Already defined in gcc/coretypes.h. So prevent double definition warning. */
c24aadf3 52#undef rtx
88e17b57
BE
53#define rtx int
54
40165636 55#if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
88e17b57
BE
56#define INVISIBLE_STRUCT_RETURN 1
57#else
58#define INVISIBLE_STRUCT_RETURN 0
59#endif
60
b427203d 61/* The uninstalled dispatch table. */
40165636 62struct sarray *__objc_uninstalled_dtable = 0; /* !T:MUTEX */
88e17b57 63
b427203d
NP
64/* Two hooks for method forwarding. If either is set, it is invoked to
65 * return a function that performs the real forwarding. If both are
66 * set, the result of __objc_msg_forward2 will be preferred over that
67 * of __objc_msg_forward. If both return NULL or are unset, the
68 * libgcc based functions (__builtin_apply and friends) are used. */
40165636 69IMP (*__objc_msg_forward) (SEL) = NULL;
80ae8e8a 70IMP (*__objc_msg_forward2) (id, SEL) = NULL;
68b61df9 71
b427203d 72/* Send +initialize to class. */
40165636 73static void __objc_send_initialize (Class);
88e17b57
BE
74
75static void __objc_install_dispatch_table_for_class (Class);
76
b427203d 77/* Forward declare some functions. */
40165636 78static void __objc_init_install_dtable (id, SEL);
88e17b57
BE
79
80/* Various forwarding functions that are used based upon the
81 return type for the selector.
82 __objc_block_forward for structures.
83 __objc_double_forward for floats/doubles.
b427203d 84 __objc_word_forward for pointers or types that fit in registers. */
40165636
RB
85static double __objc_double_forward (id, SEL, ...);
86static id __objc_word_forward (id, SEL, ...);
88e17b57
BE
87typedef struct { id many[8]; } __big;
88#if INVISIBLE_STRUCT_RETURN
89static __big
90#else
91static id
92#endif
40165636 93__objc_block_forward (id, SEL, ...);
ad9eef11
NP
94static struct objc_method * search_for_method_in_hierarchy (Class class, SEL sel);
95struct objc_method * search_for_method_in_list (struct objc_method_list * list, SEL op);
faaa30fe 96id nil_method (id, SEL);
40165636 97
b427203d 98/* Given a selector, return the proper forwarding implementation. */
435317e2 99inline
40165636 100IMP
80ae8e8a 101__objc_get_forward_imp (id rcv, SEL sel)
40165636 102{
b427203d
NP
103 /* If a custom forwarding hook was registered, try getting a
104 forwarding function from it. There are two forward routine hooks,
105 one that takes the receiver as an argument and one that does
106 not. */
8972bcd8
AR
107 if (__objc_msg_forward2)
108 {
109 IMP result;
110 if ((result = __objc_msg_forward2 (rcv, sel)) != NULL)
111 return result;
112 }
40165636
RB
113 if (__objc_msg_forward)
114 {
115 IMP result;
bd8d449d 116 if ((result = __objc_msg_forward (sel)) != NULL)
40165636
RB
117 return result;
118 }
bd8d449d 119
b427203d
NP
120 /* In all other cases, use the default forwarding functions built
121 using __builtin_apply and friends. */
40165636
RB
122 {
123 const char *t = sel->sel_types;
b427203d 124
40165636
RB
125 if (t && (*t == '[' || *t == '(' || *t == '{')
126#ifdef OBJC_MAX_STRUCT_BY_VALUE
127 && objc_sizeof_type (t) > OBJC_MAX_STRUCT_BY_VALUE
128#endif
129 )
130 return (IMP)__objc_block_forward;
131 else if (t && (*t == 'f' || *t == 'd'))
132 return (IMP)__objc_double_forward;
133 else
134 return (IMP)__objc_word_forward;
135 }
136}
88e17b57 137
e97cfd97
NP
138/* Selectors for +resolveClassMethod: and +resolveInstanceMethod:.
139 These are set up at startup. */
140static SEL selector_resolveClassMethod = NULL;
141static SEL selector_resolveInstanceMethod = NULL;
142
143/* Internal routines use to resolve a class method using
144 +resolveClassMethod:. 'class' is always a non-Nil class (*not* a
145 meta-class), and 'sel' is the selector that we are trying to
146 resolve. This must be called when class is not Nil, and the
147 dispatch table for class methods has already been installed.
148
149 This routine tries to call +resolveClassMethod: to give an
150 opportunity to resolve the method. If +resolveClassMethod: returns
151 YES, it tries looking up the method again, and if found, it returns
152 it. Else, it returns NULL. */
153static inline
154IMP
155__objc_resolve_class_method (Class class, SEL sel)
156{
157 /* We need to lookup +resolveClassMethod:. */
158 BOOL (*resolveMethodIMP) (id, SEL, SEL);
159
160 /* The dispatch table for class methods is already installed and we
161 don't want any forwarding to happen when looking up this method,
162 so we just look it up directly. Note that if 'sel' is precisely
163 +resolveClassMethod:, this would look it up yet again and find
164 nothing. That's no problem and there's no recursion. */
165 resolveMethodIMP = (BOOL (*) (id, SEL, SEL))sarray_get_safe
166 (class->class_pointer->dtable, (size_t) selector_resolveClassMethod->sel_id);
167
168 if (resolveMethodIMP && resolveMethodIMP ((id)class, selector_resolveClassMethod, sel))
169 {
170 /* +resolveClassMethod: returned YES. Look the method up again.
171 We already know the dtable is installed. */
172
173 /* TODO: There is the case where +resolveClassMethod: is buggy
174 and returned YES without actually adding the method. We
175 could maybe print an error message. */
176 return sarray_get_safe (class->class_pointer->dtable, (size_t) sel->sel_id);
177 }
178
179 return NULL;
180}
181
182/* Internal routines use to resolve a instance method using
183 +resolveInstanceMethod:. 'class' is always a non-Nil class, and
184 'sel' is the selector that we are trying to resolve. This must be
185 called when class is not Nil, and the dispatch table for instance
186 methods has already been installed.
187
188 This routine tries to call +resolveInstanceMethod: to give an
189 opportunity to resolve the method. If +resolveInstanceMethod:
190 returns YES, it tries looking up the method again, and if found, it
191 returns it. Else, it returns NULL. */
192static inline
193IMP
194__objc_resolve_instance_method (Class class, SEL sel)
195{
196 /* We need to lookup +resolveInstanceMethod:. */
197 BOOL (*resolveMethodIMP) (id, SEL, SEL);
198
199 /* The dispatch table for class methods may not be already installed
200 so we have to install it if needed. */
201 resolveMethodIMP = sarray_get_safe (class->class_pointer->dtable,
202 (size_t) selector_resolveInstanceMethod->sel_id);
203 if (resolveMethodIMP == 0)
204 {
205 /* Try again after installing the dtable. */
206 if (class->class_pointer->dtable == __objc_uninstalled_dtable)
207 {
208 objc_mutex_lock (__objc_runtime_mutex);
209 if (class->class_pointer->dtable == __objc_uninstalled_dtable)
210 __objc_install_dispatch_table_for_class (class->class_pointer);
211 objc_mutex_unlock (__objc_runtime_mutex);
212 }
213 resolveMethodIMP = sarray_get_safe (class->class_pointer->dtable,
214 (size_t) selector_resolveInstanceMethod->sel_id);
215 }
216
217 if (resolveMethodIMP && resolveMethodIMP ((id)class, selector_resolveInstanceMethod, sel))
218 {
219 /* +resolveInstanceMethod: returned YES. Look the method up
220 again. We already know the dtable is installed. */
221
222 /* TODO: There is the case where +resolveInstanceMethod: is
223 buggy and returned YES without actually adding the method.
224 We could maybe print an error message. */
225 return sarray_get_safe (class->dtable, (size_t) sel->sel_id);
226 }
227
228 return NULL;
229}
230
b427203d
NP
231/* Given a class and selector, return the selector's
232 implementation. */
bd74d88b 233inline
88e17b57
BE
234IMP
235get_imp (Class class, SEL sel)
236{
c19f8e35
NP
237 /* In a vanilla implementation we would first check if the dispatch
238 table is installed. Here instead, to get more speed in the
239 standard case (that the dispatch table is installed) we first try
240 to get the imp using brute force. Only if that fails, we do what
241 we should have been doing from the very beginning, that is, check
242 if the dispatch table needs to be installed, install it if it's
243 not installed, and retrieve the imp from the table if it's
244 installed. */
40165636 245 void *res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
88e17b57
BE
246 if (res == 0)
247 {
b427203d 248 /* Not a valid method. */
40165636 249 if (class->dtable == __objc_uninstalled_dtable)
88e17b57 250 {
b427203d 251 /* The dispatch table needs to be installed. */
40165636 252 objc_mutex_lock (__objc_runtime_mutex);
c19f8e35
NP
253
254 /* Double-checked locking pattern: Check
255 __objc_uninstalled_dtable again in case another thread
256 installed the dtable while we were waiting for the lock
257 to be released. */
258 if (class->dtable == __objc_uninstalled_dtable)
259 {
260 __objc_install_dispatch_table_for_class (class);
261 }
262
40165636 263 objc_mutex_unlock (__objc_runtime_mutex);
b427203d
NP
264 /* Call ourselves with the installed dispatch table and get
265 the real method. */
40165636 266 res = get_imp (class, sel);
88e17b57
BE
267 }
268 else
269 {
c19f8e35
NP
270 /* The dispatch table has been installed. */
271
272 /* Get the method from the dispatch table (we try to get it
273 again in case another thread has installed the dtable just
274 after we invoked sarray_get_safe, but before we checked
b427203d 275 class->dtable == __objc_uninstalled_dtable). */
c19f8e35
NP
276 res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
277 if (res == 0)
278 {
279 /* The dispatch table has been installed, and the method
280 is not in the dispatch table. So the method just
e97cfd97
NP
281 doesn't exist for the class. */
282
283 /* Try going through the +resolveClassMethod: or
284 +resolveInstanceMethod: process. */
285 if (CLS_ISMETA (class))
286 {
287 /* We have the meta class, but we need to invoke the
288 +resolveClassMethod: method on the class. So, we
289 need to obtain the class from the meta class,
290 which we do using the fact that both the class
291 and the meta-class have the same name. */
6e45b376 292 Class realClass = objc_lookUpClass (class->name);
e97cfd97
NP
293 if (realClass)
294 res = __objc_resolve_class_method (realClass, sel);
295 }
296 else
297 res = __objc_resolve_instance_method (class, sel);
298
299 if (res == 0)
300 {
301 /* If that fails, then return the forwarding
b427203d
NP
302 implementation. We don't know the receiver (only
303 its class), so we have to pass 'nil' as the first
e97cfd97 304 argument. Passing the class as first argument is
b427203d
NP
305 wrong because the class is not the receiver; it
306 can result in us calling a class method when we
307 want an instance method of the same name. */
e97cfd97
NP
308 res = __objc_get_forward_imp (nil, sel);
309 }
c19f8e35 310 }
88e17b57
BE
311 }
312 }
313 return res;
314}
315
ad9eef11
NP
316/* The new name of get_imp(). */
317IMP
318class_getMethodImplementation (Class class_, SEL selector)
319{
320 if (class_ == Nil || selector == NULL)
321 return NULL;
322
323 /* get_imp is inlined, so we're good. */
324 return get_imp (class_, selector);
325}
326
fea78205
NP
327/* Given a method, return its implementation. This has been replaced
328 by method_getImplementation() in the modern API. */
cf3822f1 329IMP
ad9eef11 330method_get_imp (struct objc_method * method)
cf3822f1 331{
ad9eef11 332 return (method != (struct objc_method *)0) ? method->method_imp : (IMP)0;
cf3822f1
NP
333}
334
88e17b57 335/* Query if an object can respond to a selector, returns YES if the
b427203d
NP
336 object implements the selector otherwise NO. Does not check if the
337 method can be forwarded. */
435317e2 338inline
88e17b57
BE
339BOOL
340__objc_responds_to (id object, SEL sel)
341{
40165636 342 void *res;
88e17b57 343
b427203d 344 /* Install dispatch table if need be. */
88e17b57
BE
345 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
346 {
40165636 347 objc_mutex_lock (__objc_runtime_mutex);
c19f8e35
NP
348 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
349 {
350 __objc_install_dispatch_table_for_class (object->class_pointer);
351 }
40165636 352 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
353 }
354
b427203d 355 /* Get the method from the dispatch table. */
88e17b57
BE
356 res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
357 return (res != 0);
358}
359
ad9eef11
NP
360BOOL
361class_respondsToSelector (Class class_, SEL selector)
362{
363 void *res;
364
365 if (class_ == Nil || selector == NULL)
366 return NO;
367
b427203d 368 /* Install dispatch table if need be. */
ad9eef11
NP
369 if (class_->dtable == __objc_uninstalled_dtable)
370 {
371 objc_mutex_lock (__objc_runtime_mutex);
372 if (class_->dtable == __objc_uninstalled_dtable)
373 {
374 __objc_install_dispatch_table_for_class (class_);
375 }
376 objc_mutex_unlock (__objc_runtime_mutex);
377 }
378
b427203d 379 /* Get the method from the dispatch table. */
ad9eef11
NP
380 res = sarray_get_safe (class_->dtable, (size_t) selector->sel_id);
381 return (res != 0);
382}
383
b427203d 384/* This is the lookup function. All entries in the table are either a
88e17b57 385 valid method *or* zero. If zero then either the dispatch table
b427203d
NP
386 needs to be installed or it doesn't exist and forwarding is
387 attempted. */
88e17b57 388IMP
40165636 389objc_msg_lookup (id receiver, SEL op)
88e17b57
BE
390{
391 IMP result;
40165636 392 if (receiver)
88e17b57
BE
393 {
394 result = sarray_get_safe (receiver->class_pointer->dtable,
395 (sidx)op->sel_id);
396 if (result == 0)
397 {
b427203d 398 /* Not a valid method. */
40165636 399 if (receiver->class_pointer->dtable == __objc_uninstalled_dtable)
88e17b57 400 {
b427203d
NP
401 /* The dispatch table needs to be installed. This
402 happens on the very first method call to the
403 class. */
40165636 404 __objc_init_install_dtable (receiver, op);
88e17b57 405
b427203d
NP
406 /* Get real method for this in newly installed
407 dtable. */
40165636 408 result = get_imp (receiver->class_pointer, op);
88e17b57
BE
409 }
410 else
411 {
c19f8e35
NP
412 /* The dispatch table has been installed. Check again
413 if the method exists (just in case the dispatch table
414 has been installed by another thread after we did the
b427203d 415 previous check that the method exists). */
c19f8e35
NP
416 result = sarray_get_safe (receiver->class_pointer->dtable,
417 (sidx)op->sel_id);
418 if (result == 0)
419 {
e97cfd97
NP
420 /* Try going through the +resolveClassMethod: or
421 +resolveInstanceMethod: process. */
422 if (CLS_ISMETA (receiver->class_pointer))
423 result = __objc_resolve_class_method ((Class)receiver, op);
424 else
425 result = __objc_resolve_instance_method (receiver->class_pointer,
426 op);
427
428 if (result == 0)
429 {
430 /* If the method still just doesn't exist for
b427203d 431 the class, attempt to forward the method. */
e97cfd97
NP
432 result = __objc_get_forward_imp (receiver, op);
433 }
c19f8e35 434 }
88e17b57
BE
435 }
436 }
437 return result;
438 }
439 else
faaa30fe 440 return (IMP)nil_method;
88e17b57
BE
441}
442
443IMP
53f672ca 444objc_msg_lookup_super (struct objc_super *super, SEL sel)
88e17b57
BE
445{
446 if (super->self)
114dae43 447 return get_imp (super->super_class, sel);
88e17b57 448 else
faaa30fe 449 return (IMP)nil_method;
88e17b57
BE
450}
451
114dae43
NP
452/* Temporarily defined here until objc_msg_sendv() goes away. */
453char *method_get_first_argument (struct objc_method *,
454 arglist_t argframe,
455 const char **type);
456char *method_get_next_argument (arglist_t argframe,
457 const char **type);
458int method_get_sizeof_arguments (struct objc_method *);
459
460struct objc_method *
461class_get_instance_method (Class class, SEL op);
88e17b57
BE
462
463retval_t
40165636 464objc_msg_sendv (id object, SEL op, arglist_t arg_frame)
88e17b57 465{
114dae43 466 struct objc_method *m = class_get_instance_method (object->class_pointer, op);
88e17b57 467 const char *type;
40165636
RB
468 *((id *) method_get_first_argument (m, arg_frame, &type)) = object;
469 *((SEL *) method_get_next_argument (arg_frame, &type)) = op;
470 return __builtin_apply ((apply_t) m->method_imp,
471 arg_frame,
472 method_get_sizeof_arguments (m));
88e17b57
BE
473}
474
475void
40165636 476__objc_init_dispatch_tables ()
88e17b57 477{
40165636 478 __objc_uninstalled_dtable = sarray_new (200, 0);
e97cfd97
NP
479
480 /* TODO: It would be cool to register typed selectors here. */
114dae43
NP
481 selector_resolveClassMethod = sel_registerName ("resolveClassMethod:");
482 selector_resolveInstanceMethod =sel_registerName ("resolveInstanceMethod:");
88e17b57
BE
483}
484
b427203d
NP
485/* This function is called by objc_msg_lookup when the dispatch table
486 needs to be installed; thus it is called once for each class,
487 namely when the very first message is sent to it. */
88e17b57 488static void
40165636 489__objc_init_install_dtable (id receiver, SEL op __attribute__ ((__unused__)))
88e17b57 490{
c19f8e35
NP
491 objc_mutex_lock (__objc_runtime_mutex);
492
b427203d
NP
493 /* This may happen, if the programmer has taken the address of a
494 method before the dtable was initialized... too bad for him! */
40165636 495 if (receiver->class_pointer->dtable != __objc_uninstalled_dtable)
c19f8e35
NP
496 {
497 objc_mutex_unlock (__objc_runtime_mutex);
498 return;
499 }
500
40165636 501 if (CLS_ISCLASS (receiver->class_pointer))
88e17b57 502 {
b427203d 503 /* receiver is an ordinary object. */
40165636 504 assert (CLS_ISCLASS (receiver->class_pointer));
88e17b57 505
b427203d 506 /* Install instance methods table. */
88e17b57
BE
507 __objc_install_dispatch_table_for_class (receiver->class_pointer);
508
b427203d
NP
509 /* Call +initialize -- this will in turn install the factory
510 dispatch table if not already done. :-) */
40165636 511 __objc_send_initialize (receiver->class_pointer);
88e17b57
BE
512 }
513 else
514 {
b427203d 515 /* receiver is a class object. */
40165636
RB
516 assert (CLS_ISCLASS ((Class)receiver));
517 assert (CLS_ISMETA (receiver->class_pointer));
88e17b57 518
b427203d 519 /* Install real dtable for factory methods. */
88e17b57
BE
520 __objc_install_dispatch_table_for_class (receiver->class_pointer);
521
40165636 522 __objc_send_initialize ((Class)receiver);
88e17b57 523 }
40165636 524 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
525}
526
527/* Install dummy table for class which causes the first message to
b427203d 528 that class (or instances hereof) to be initialized properly. */
88e17b57 529void
40165636 530__objc_install_premature_dtable (Class class)
88e17b57 531{
40165636 532 assert (__objc_uninstalled_dtable);
88e17b57
BE
533 class->dtable = __objc_uninstalled_dtable;
534}
535
b427203d 536/* Send +initialize to class if not already done. */
88e17b57 537static void
40165636 538__objc_send_initialize (Class class)
88e17b57 539{
b427203d 540 /* This *must* be a class object. */
40165636
RB
541 assert (CLS_ISCLASS (class));
542 assert (! CLS_ISMETA (class));
88e17b57 543
40165636 544 if (! CLS_ISINITIALIZED (class))
88e17b57 545 {
40165636
RB
546 CLS_SETINITIALIZED (class);
547 CLS_SETINITIALIZED (class->class_pointer);
88e17b57 548
b427203d 549 /* Create the garbage collector type memory description. */
88e17b57
BE
550 __objc_generate_gc_type_description (class);
551
40165636
RB
552 if (class->super_class)
553 __objc_send_initialize (class->super_class);
88e17b57
BE
554
555 {
114dae43 556 SEL op = sel_registerName ("initialize");
b427203d 557 IMP imp = 0;
ad9eef11 558 struct objc_method_list * method_list = class->class_pointer->methods;
b427203d
NP
559
560 while (method_list)
561 {
562 int i;
563 struct objc_method * method;
564
565 for (i = 0; i < method_list->method_count; i++)
566 {
567 method = &(method_list->method_list[i]);
568 if (method->method_name
569 && method->method_name->sel_id == op->sel_id)
570 {
571 imp = method->method_imp;
572 break;
573 }
574 }
575
576 if (imp)
577 break;
578
579 method_list = method_list->method_next;
580 }
88e17b57 581 if (imp)
b427203d 582 (*imp) ((id) class, op);
88e17b57
BE
583 }
584 }
585}
586
b427203d
NP
587/* Walk on the methods list of class and install the methods in the
588 reverse order of the lists. Since methods added by categories are
589 before the methods of class in the methods list, this allows
590 categories to substitute methods declared in class. However if
591 more than one category replaces the same method nothing is
592 guaranteed about what method will be used. Assumes that
593 __objc_runtime_mutex is locked down. */
88e17b57 594static void
ad9eef11 595__objc_install_methods_in_dtable (Class class, struct objc_method_list * method_list)
88e17b57
BE
596{
597 int i;
b427203d 598
40165636 599 if (! method_list)
88e17b57 600 return;
b427203d 601
88e17b57
BE
602 if (method_list->method_next)
603 __objc_install_methods_in_dtable (class, method_list->method_next);
b427203d 604
88e17b57
BE
605 for (i = 0; i < method_list->method_count; i++)
606 {
ad9eef11 607 struct objc_method * method = &(method_list->method_list[i]);
88e17b57
BE
608 sarray_at_put_safe (class->dtable,
609 (sidx) method->method_name->sel_id,
610 method->method_imp);
611 }
612}
613
b427203d 614/* Assumes that __objc_runtime_mutex is locked down. */
88e17b57
BE
615static void
616__objc_install_dispatch_table_for_class (Class class)
617{
618 Class super;
619
b427203d
NP
620 /* If the class has not yet had its class links resolved, we must
621 re-compute all class links. */
40165636
RB
622 if (! CLS_ISRESOLV (class))
623 __objc_resolve_class_links ();
b427203d 624
88e17b57
BE
625 super = class->super_class;
626
627 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
628 __objc_install_dispatch_table_for_class (super);
629
b427203d 630 /* Allocate dtable if necessary. */
88e17b57
BE
631 if (super == 0)
632 {
40165636 633 objc_mutex_lock (__objc_runtime_mutex);
88e17b57 634 class->dtable = sarray_new (__objc_selector_max_index, 0);
40165636 635 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
636 }
637 else
638 class->dtable = sarray_lazy_copy (super->dtable);
639
640 __objc_install_methods_in_dtable (class, class->methods);
641}
642
643void
644__objc_update_dispatch_table_for_class (Class class)
645{
646 Class next;
647 struct sarray *arr;
648
b427203d 649 /* Not yet installed -- skip it. */
88e17b57
BE
650 if (class->dtable == __objc_uninstalled_dtable)
651 return;
652
40165636 653 objc_mutex_lock (__objc_runtime_mutex);
88e17b57
BE
654
655 arr = class->dtable;
656 __objc_install_premature_dtable (class); /* someone might require it... */
657 sarray_free (arr); /* release memory */
b427203d
NP
658
659 /* Could have been lazy... */
88e17b57
BE
660 __objc_install_dispatch_table_for_class (class);
661
b427203d 662 if (class->subclass_list) /* Traverse subclasses. */
88e17b57
BE
663 for (next = class->subclass_list; next; next = next->sibling_class)
664 __objc_update_dispatch_table_for_class (next);
665
40165636 666 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
667}
668
88e17b57
BE
669/* This function adds a method list to a class. This function is
670 typically called by another function specific to the run-time. As
671 such this function does not worry about thread safe issues.
672
673 This one is only called for categories. Class objects have their
674 methods installed right away, and their selectors are made into
b427203d 675 SEL's by the function __objc_register_selectors_from_class. */
88e17b57 676void
ad9eef11 677class_add_method_list (Class class, struct objc_method_list * list)
88e17b57 678{
88e17b57 679 /* Passing of a linked list is not allowed. Do multiple calls. */
40165636 680 assert (! list->method_next);
88e17b57 681
435317e2 682 __objc_register_selectors_from_list(list);
88e17b57
BE
683
684 /* Add the methods to the class's method list. */
685 list->method_next = class->methods;
686 class->methods = list;
687
b427203d 688 /* Update the dispatch table of class. */
88e17b57
BE
689 __objc_update_dispatch_table_for_class (class);
690}
691
ad9eef11 692struct objc_method *
40165636 693class_get_instance_method (Class class, SEL op)
88e17b57 694{
40165636 695 return search_for_method_in_hierarchy (class, op);
88e17b57
BE
696}
697
ad9eef11 698struct objc_method *
40165636 699class_get_class_method (MetaClass class, SEL op)
88e17b57 700{
40165636 701 return search_for_method_in_hierarchy (class, op);
88e17b57
BE
702}
703
ad9eef11
NP
704struct objc_method *
705class_getInstanceMethod (Class class_, SEL selector)
706{
e97cfd97
NP
707 struct objc_method *m;
708
ad9eef11
NP
709 if (class_ == Nil || selector == NULL)
710 return NULL;
711
e97cfd97
NP
712 m = search_for_method_in_hierarchy (class_, selector);
713 if (m)
714 return m;
715
b427203d
NP
716 /* Try going through +resolveInstanceMethod:, and do the search
717 again if successful. */
e97cfd97
NP
718 if (__objc_resolve_instance_method (class_, selector))
719 return search_for_method_in_hierarchy (class_, selector);
720
721 return NULL;
ad9eef11
NP
722}
723
724struct objc_method *
725class_getClassMethod (Class class_, SEL selector)
726{
e97cfd97
NP
727 struct objc_method *m;
728
ad9eef11
NP
729 if (class_ == Nil || selector == NULL)
730 return NULL;
731
e97cfd97
NP
732 m = search_for_method_in_hierarchy (class_->class_pointer,
733 selector);
734 if (m)
735 return m;
736
737 /* Try going through +resolveClassMethod:, and do the search again
738 if successful. */
739 if (__objc_resolve_class_method (class_, selector))
740 return search_for_method_in_hierarchy (class_->class_pointer,
741 selector);
742
743 return NULL;
ad9eef11 744}
88e17b57 745
6c5c7efd
NP
746BOOL
747class_addMethod (Class class_, SEL selector, IMP implementation,
748 const char *method_types)
749{
750 struct objc_method_list *method_list;
751 struct objc_method *method;
752 const char *method_name;
753
754 if (class_ == Nil || selector == NULL || implementation == NULL
755 || method_types == NULL || (strcmp (method_types, "") == 0))
756 return NO;
757
114dae43 758 method_name = sel_getName (selector);
6c5c7efd
NP
759 if (method_name == NULL)
760 return NO;
761
762 method_list = (struct objc_method_list *)objc_calloc (1, sizeof (struct objc_method_list));
763 method_list->method_count = 1;
764
765 method = &(method_list->method_list[0]);
766 method->method_name = objc_malloc (strlen (method_name) + 1);
767 strcpy ((char *)method->method_name, method_name);
768
769 method->method_types = objc_malloc (strlen (method_types) + 1);
770 strcpy ((char *)method->method_types, method_types);
771
772 method->method_imp = implementation;
773
774 if (CLS_IS_IN_CONSTRUCTION (class_))
775 {
776 /* We only need to add the method to the list. It will be
777 registered with the runtime when the class pair is registered
778 (if ever). */
779 method_list->method_next = class_->methods;
780 class_->methods = method_list;
781 }
782 else
783 {
784 /* Add the method to a live class. */
785 objc_mutex_lock (__objc_runtime_mutex);
786 class_add_method_list (class_, method_list);
787 objc_mutex_unlock (__objc_runtime_mutex);
788 }
789
790 return YES;
791}
792
6c5c7efd
NP
793IMP
794class_replaceMethod (Class class_, SEL selector, IMP implementation,
795 const char *method_types)
796{
797 struct objc_method * method;
798
799 if (class_ == Nil || selector == NULL || implementation == NULL
800 || method_types == NULL)
801 return NULL;
802
803 method = search_for_method_in_hierarchy (class_, selector);
804
805 if (method)
806 {
807 return method_setImplementation (method, implementation);
808 }
809 else
810 {
811 class_addMethod (class_, selector, implementation, method_types);
812 return NULL;
813 }
814}
815
b427203d
NP
816/* Search for a method starting from the current class up its
817 hierarchy. Return a pointer to the method's method structure if
818 found. NULL otherwise. */
ad9eef11 819static struct objc_method *
88e17b57
BE
820search_for_method_in_hierarchy (Class cls, SEL sel)
821{
ad9eef11 822 struct objc_method * method = NULL;
88e17b57
BE
823 Class class;
824
825 if (! sel_is_mapped (sel))
826 return NULL;
827
b427203d
NP
828 /* Scan the method list of the class. If the method isn't found in
829 the list then step to its super class. */
88e17b57
BE
830 for (class = cls; ((! method) && class); class = class->super_class)
831 method = search_for_method_in_list (class->methods, sel);
832
833 return method;
834}
835
836
837
b427203d
NP
838/* Given a linked list of method and a method's name. Search for the
839 named method's method structure. Return a pointer to the method's
840 method structure if found. NULL otherwise. */
ad9eef11
NP
841struct objc_method *
842search_for_method_in_list (struct objc_method_list * list, SEL op)
88e17b57 843{
ad9eef11 844 struct objc_method_list * method_list = list;
88e17b57
BE
845
846 if (! sel_is_mapped (op))
847 return NULL;
848
849 /* If not found then we'll search the list. */
850 while (method_list)
851 {
852 int i;
853
854 /* Search the method list. */
855 for (i = 0; i < method_list->method_count; ++i)
856 {
ad9eef11 857 struct objc_method * method = &method_list->method_list[i];
88e17b57
BE
858
859 if (method->method_name)
860 if (method->method_name->sel_id == op->sel_id)
861 return method;
862 }
863
864 /* The method wasn't found. Follow the link to the next list of
865 methods. */
866 method_list = method_list->method_next;
867 }
868
869 return NULL;
870}
871
872static retval_t __objc_forward (id object, SEL sel, arglist_t args);
873
b427203d 874/* Forwarding pointers/integers through the normal registers. */
88e17b57
BE
875static id
876__objc_word_forward (id rcv, SEL op, ...)
877{
878 void *args, *res;
879
880 args = __builtin_apply_args ();
881 res = __objc_forward (rcv, op, args);
882 if (res)
883 __builtin_return (res);
884 else
885 return res;
886}
887
888/* Specific routine for forwarding floats/double because of
b427203d
NP
889 architectural differences on some processors. i386s for example
890 which uses a floating point stack versus general registers for
891 floating point numbers. This forward routine makes sure that GCC
892 restores the proper return values. */
88e17b57
BE
893static double
894__objc_double_forward (id rcv, SEL op, ...)
895{
896 void *args, *res;
897
898 args = __builtin_apply_args ();
899 res = __objc_forward (rcv, op, args);
900 __builtin_return (res);
901}
902
903#if INVISIBLE_STRUCT_RETURN
904static __big
905#else
906static id
907#endif
908__objc_block_forward (id rcv, SEL op, ...)
909{
910 void *args, *res;
911
912 args = __builtin_apply_args ();
913 res = __objc_forward (rcv, op, args);
914 if (res)
915 __builtin_return (res);
916 else
917#if INVISIBLE_STRUCT_RETURN
918 return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
919#else
920 return nil;
921#endif
922}
923
924
b427203d
NP
925/* This function is installed in the dispatch table for all methods
926 which are not implemented. Thus, it is called when a selector is
927 not recognized. */
88e17b57
BE
928static retval_t
929__objc_forward (id object, SEL sel, arglist_t args)
930{
931 IMP imp;
932 static SEL frwd_sel = 0; /* !T:SAFE2 */
933 SEL err_sel;
934
b427203d 935 /* First try if the object understands forward::. */
40165636
RB
936 if (! frwd_sel)
937 frwd_sel = sel_get_any_uid ("forward::");
88e17b57
BE
938
939 if (__objc_responds_to (object, frwd_sel))
940 {
40165636
RB
941 imp = get_imp (object->class_pointer, frwd_sel);
942 return (*imp) (object, frwd_sel, sel, args);
88e17b57
BE
943 }
944
b427203d
NP
945 /* If the object recognizes the doesNotRecognize: method then we're
946 going to send it. */
88e17b57
BE
947 err_sel = sel_get_any_uid ("doesNotRecognize:");
948 if (__objc_responds_to (object, err_sel))
949 {
950 imp = get_imp (object->class_pointer, err_sel);
951 return (*imp) (object, err_sel, sel);
952 }
953
954 /* The object doesn't recognize the method. Check for responding to
b427203d 955 error:. If it does then sent it. */
88e17b57 956 {
114dae43 957 char msg[256 + strlen ((const char *) sel_getName (sel))
40165636 958 + strlen ((const char *) object->class_pointer->name)];
88e17b57
BE
959
960 sprintf (msg, "(%s) %s does not recognize %s",
40165636 961 (CLS_ISMETA (object->class_pointer)
88e17b57
BE
962 ? "class"
963 : "instance" ),
114dae43 964 object->class_pointer->name, sel_getName (sel));
88e17b57 965
7b869986 966 /* TODO: support for error: is surely deprecated ? */
88e17b57
BE
967 err_sel = sel_get_any_uid ("error:");
968 if (__objc_responds_to (object, err_sel))
969 {
970 imp = get_imp (object->class_pointer, err_sel);
971 return (*imp) (object, sel_get_any_uid ("error:"), msg);
972 }
973
b427203d
NP
974 /* The object doesn't respond to doesNotRecognize: or error:;
975 Therefore, a default action is taken. */
7b869986 976 _objc_abort ("%s\n", msg);
88e17b57
BE
977
978 return 0;
979 }
980}
981
982void
d9df3365 983__objc_print_dtable_stats (void)
88e17b57
BE
984{
985 int total = 0;
986
40165636 987 objc_mutex_lock (__objc_runtime_mutex);
88e17b57 988
88e17b57 989#ifdef OBJC_SPARSE2
40165636 990 printf ("memory usage: (%s)\n", "2-level sparse arrays");
88e17b57 991#else
40165636 992 printf ("memory usage: (%s)\n", "3-level sparse arrays");
88e17b57 993#endif
88e17b57 994
40165636 995 printf ("arrays: %d = %ld bytes\n", narrays,
b15b7ef8 996 (long) ((size_t) narrays * sizeof (struct sarray)));
40165636
RB
997 total += narrays * sizeof (struct sarray);
998 printf ("buckets: %d = %ld bytes\n", nbuckets,
b15b7ef8 999 (long) ((size_t) nbuckets * sizeof (struct sbucket)));
40165636
RB
1000 total += nbuckets * sizeof (struct sbucket);
1001
1002 printf ("idxtables: %d = %ld bytes\n",
b15b7ef8 1003 idxsize, (long) ((size_t) idxsize * sizeof (void *)));
40165636
RB
1004 total += idxsize * sizeof (void *);
1005 printf ("-----------------------------------\n");
1006 printf ("total: %d bytes\n", total);
1007 printf ("===================================\n");
1008
1009 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
1010}
1011
b427203d
NP
1012/* Returns the uninstalled dispatch table indicator. If a class'
1013 dispatch table points to __objc_uninstalled_dtable then that means
1014 it needs its dispatch table to be installed. */
40165636 1015struct sarray *
114dae43 1016objc_get_uninstalled_dtable (void)
88e17b57
BE
1017{
1018 return __objc_uninstalled_dtable;
1019}