]> git.ipfire.org Git - thirdparty/gcc.git/blob - libobjc/sendmsg.c
In libobjc/: 2010-12-19 Nicola Pero <nicola.pero@meta-innovation.com>
[thirdparty/gcc.git] / libobjc / sendmsg.c
1 /* GNU Objective C Runtime message lookup
2 Copyright (C) 1993, 1995, 1996, 1997, 1998,
3 2001, 2002, 2004, 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Kresten Krab Thorup
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 3, or (at your option) any later version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
25
26
27 /* FIXME: This file has no business including tm.h. */
28 /* FIXME: This should be using libffi instead of __builtin_apply
29 and friends. */
30
31 #include "objc-private/common.h"
32 #include "objc-private/error.h"
33 #include "tconfig.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "objc/runtime.h"
37 #include "objc/message.h" /* For objc_msg_lookup(), objc_msg_lookup_super(). */
38 #include "objc/thr.h"
39 #include "objc-private/module-abi-8.h"
40 #include "objc-private/runtime.h"
41 #include "objc-private/sarray.h"
42 #include "objc-private/selector.h" /* For sel_is_mapped() */
43 #include "runtime-info.h"
44 #include <assert.h> /* For assert */
45 #include <string.h> /* For strlen */
46
47 /* This is how we hack STRUCT_VALUE to be 1 or 0. */
48 #define gen_rtx(args...) 1
49 #define gen_rtx_MEM(args...) 1
50 #define gen_rtx_REG(args...) 1
51 /* Already defined in gcc/coretypes.h. So prevent double definition warning. */
52 #undef rtx
53 #define rtx int
54
55 #if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
56 #define INVISIBLE_STRUCT_RETURN 1
57 #else
58 #define INVISIBLE_STRUCT_RETURN 0
59 #endif
60
61 /* The uninstalled dispatch table. */
62 struct sarray *__objc_uninstalled_dtable = 0; /* !T:MUTEX */
63
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. */
69 IMP (*__objc_msg_forward) (SEL) = NULL;
70 IMP (*__objc_msg_forward2) (id, SEL) = NULL;
71
72 /* Send +initialize to class. */
73 static void __objc_send_initialize (Class);
74
75 static void __objc_install_dispatch_table_for_class (Class);
76
77 /* Forward declare some functions. */
78 static void __objc_init_install_dtable (id, SEL);
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.
84 __objc_word_forward for pointers or types that fit in registers. */
85 static double __objc_double_forward (id, SEL, ...);
86 static id __objc_word_forward (id, SEL, ...);
87 typedef struct { id many[8]; } __big;
88 #if INVISIBLE_STRUCT_RETURN
89 static __big
90 #else
91 static id
92 #endif
93 __objc_block_forward (id, SEL, ...);
94 static struct objc_method * search_for_method_in_hierarchy (Class class, SEL sel);
95 struct objc_method * search_for_method_in_list (struct objc_method_list * list, SEL op);
96 id nil_method (id, SEL);
97
98 /* Given a selector, return the proper forwarding implementation. */
99 inline
100 IMP
101 __objc_get_forward_imp (id rcv, SEL sel)
102 {
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. */
107 if (__objc_msg_forward2)
108 {
109 IMP result;
110 if ((result = __objc_msg_forward2 (rcv, sel)) != NULL)
111 return result;
112 }
113 if (__objc_msg_forward)
114 {
115 IMP result;
116 if ((result = __objc_msg_forward (sel)) != NULL)
117 return result;
118 }
119
120 /* In all other cases, use the default forwarding functions built
121 using __builtin_apply and friends. */
122 {
123 const char *t = sel->sel_types;
124
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 }
137
138 /* Selectors for +resolveClassMethod: and +resolveInstanceMethod:.
139 These are set up at startup. */
140 static SEL selector_resolveClassMethod = NULL;
141 static 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. */
153 static inline
154 IMP
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. */
192 static inline
193 IMP
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
231 /* Given a class and selector, return the selector's
232 implementation. */
233 inline
234 IMP
235 get_imp (Class class, SEL sel)
236 {
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. */
245 void *res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
246 if (res == 0)
247 {
248 /* Not a valid method. */
249 if (class->dtable == __objc_uninstalled_dtable)
250 {
251 /* The dispatch table needs to be installed. */
252 objc_mutex_lock (__objc_runtime_mutex);
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
263 objc_mutex_unlock (__objc_runtime_mutex);
264 /* Call ourselves with the installed dispatch table and get
265 the real method. */
266 res = get_imp (class, sel);
267 }
268 else
269 {
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
275 class->dtable == __objc_uninstalled_dtable). */
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
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. */
292 Class realClass = objc_lookUpClass (class->name);
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
302 implementation. We don't know the receiver (only
303 its class), so we have to pass 'nil' as the first
304 argument. Passing the class as first argument is
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. */
308 res = __objc_get_forward_imp (nil, sel);
309 }
310 }
311 }
312 }
313 return res;
314 }
315
316 /* The new name of get_imp(). */
317 IMP
318 class_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
327 /* Given a method, return its implementation. This has been replaced
328 by method_getImplementation() in the modern API. */
329 IMP
330 method_get_imp (struct objc_method * method)
331 {
332 return (method != (struct objc_method *)0) ? method->method_imp : (IMP)0;
333 }
334
335 /* Query if an object can respond to a selector, returns YES if the
336 object implements the selector otherwise NO. Does not check if the
337 method can be forwarded. */
338 inline
339 BOOL
340 __objc_responds_to (id object, SEL sel)
341 {
342 void *res;
343
344 /* Install dispatch table if need be. */
345 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
346 {
347 objc_mutex_lock (__objc_runtime_mutex);
348 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
349 {
350 __objc_install_dispatch_table_for_class (object->class_pointer);
351 }
352 objc_mutex_unlock (__objc_runtime_mutex);
353 }
354
355 /* Get the method from the dispatch table. */
356 res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
357 return (res != 0);
358 }
359
360 BOOL
361 class_respondsToSelector (Class class_, SEL selector)
362 {
363 void *res;
364
365 if (class_ == Nil || selector == NULL)
366 return NO;
367
368 /* Install dispatch table if need be. */
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
379 /* Get the method from the dispatch table. */
380 res = sarray_get_safe (class_->dtable, (size_t) selector->sel_id);
381 return (res != 0);
382 }
383
384 /* This is the lookup function. All entries in the table are either a
385 valid method *or* zero. If zero then either the dispatch table
386 needs to be installed or it doesn't exist and forwarding is
387 attempted. */
388 IMP
389 objc_msg_lookup (id receiver, SEL op)
390 {
391 IMP result;
392 if (receiver)
393 {
394 result = sarray_get_safe (receiver->class_pointer->dtable,
395 (sidx)op->sel_id);
396 if (result == 0)
397 {
398 /* Not a valid method. */
399 if (receiver->class_pointer->dtable == __objc_uninstalled_dtable)
400 {
401 /* The dispatch table needs to be installed. This
402 happens on the very first method call to the
403 class. */
404 __objc_init_install_dtable (receiver, op);
405
406 /* Get real method for this in newly installed
407 dtable. */
408 result = get_imp (receiver->class_pointer, op);
409 }
410 else
411 {
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
415 previous check that the method exists). */
416 result = sarray_get_safe (receiver->class_pointer->dtable,
417 (sidx)op->sel_id);
418 if (result == 0)
419 {
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
431 the class, attempt to forward the method. */
432 result = __objc_get_forward_imp (receiver, op);
433 }
434 }
435 }
436 }
437 return result;
438 }
439 else
440 return (IMP)nil_method;
441 }
442
443 IMP
444 objc_msg_lookup_super (struct objc_super *super, SEL sel)
445 {
446 if (super->self)
447 return get_imp (super->super_class, sel);
448 else
449 return (IMP)nil_method;
450 }
451
452 /* Temporarily defined here until objc_msg_sendv() goes away. */
453 char *method_get_first_argument (struct objc_method *,
454 arglist_t argframe,
455 const char **type);
456 char *method_get_next_argument (arglist_t argframe,
457 const char **type);
458 int method_get_sizeof_arguments (struct objc_method *);
459
460 struct objc_method *
461 class_get_instance_method (Class class, SEL op);
462
463 retval_t
464 objc_msg_sendv (id object, SEL op, arglist_t arg_frame)
465 {
466 struct objc_method *m = class_get_instance_method (object->class_pointer, op);
467 const char *type;
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));
473 }
474
475 void
476 __objc_init_dispatch_tables ()
477 {
478 __objc_uninstalled_dtable = sarray_new (200, 0);
479
480 /* TODO: It would be cool to register typed selectors here. */
481 selector_resolveClassMethod = sel_registerName ("resolveClassMethod:");
482 selector_resolveInstanceMethod =sel_registerName ("resolveInstanceMethod:");
483 }
484
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. */
488 static void
489 __objc_init_install_dtable (id receiver, SEL op __attribute__ ((__unused__)))
490 {
491 objc_mutex_lock (__objc_runtime_mutex);
492
493 /* This may happen, if the programmer has taken the address of a
494 method before the dtable was initialized... too bad for him! */
495 if (receiver->class_pointer->dtable != __objc_uninstalled_dtable)
496 {
497 objc_mutex_unlock (__objc_runtime_mutex);
498 return;
499 }
500
501 if (CLS_ISCLASS (receiver->class_pointer))
502 {
503 /* receiver is an ordinary object. */
504 assert (CLS_ISCLASS (receiver->class_pointer));
505
506 /* Install instance methods table. */
507 __objc_install_dispatch_table_for_class (receiver->class_pointer);
508
509 /* Call +initialize -- this will in turn install the factory
510 dispatch table if not already done. :-) */
511 __objc_send_initialize (receiver->class_pointer);
512 }
513 else
514 {
515 /* receiver is a class object. */
516 assert (CLS_ISCLASS ((Class)receiver));
517 assert (CLS_ISMETA (receiver->class_pointer));
518
519 /* Install real dtable for factory methods. */
520 __objc_install_dispatch_table_for_class (receiver->class_pointer);
521
522 __objc_send_initialize ((Class)receiver);
523 }
524 objc_mutex_unlock (__objc_runtime_mutex);
525 }
526
527 /* Install dummy table for class which causes the first message to
528 that class (or instances hereof) to be initialized properly. */
529 void
530 __objc_install_premature_dtable (Class class)
531 {
532 assert (__objc_uninstalled_dtable);
533 class->dtable = __objc_uninstalled_dtable;
534 }
535
536 /* Send +initialize to class if not already done. */
537 static void
538 __objc_send_initialize (Class class)
539 {
540 /* This *must* be a class object. */
541 assert (CLS_ISCLASS (class));
542 assert (! CLS_ISMETA (class));
543
544 if (! CLS_ISINITIALIZED (class))
545 {
546 CLS_SETINITIALIZED (class);
547 CLS_SETINITIALIZED (class->class_pointer);
548
549 /* Create the garbage collector type memory description. */
550 __objc_generate_gc_type_description (class);
551
552 if (class->super_class)
553 __objc_send_initialize (class->super_class);
554
555 {
556 SEL op = sel_registerName ("initialize");
557 IMP imp = 0;
558 struct objc_method_list * method_list = class->class_pointer->methods;
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 }
581 if (imp)
582 (*imp) ((id) class, op);
583 }
584 }
585 }
586
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. */
594 static void
595 __objc_install_methods_in_dtable (Class class, struct objc_method_list * method_list)
596 {
597 int i;
598
599 if (! method_list)
600 return;
601
602 if (method_list->method_next)
603 __objc_install_methods_in_dtable (class, method_list->method_next);
604
605 for (i = 0; i < method_list->method_count; i++)
606 {
607 struct objc_method * method = &(method_list->method_list[i]);
608 sarray_at_put_safe (class->dtable,
609 (sidx) method->method_name->sel_id,
610 method->method_imp);
611 }
612 }
613
614 /* Assumes that __objc_runtime_mutex is locked down. */
615 static void
616 __objc_install_dispatch_table_for_class (Class class)
617 {
618 Class super;
619
620 /* If the class has not yet had its class links resolved, we must
621 re-compute all class links. */
622 if (! CLS_ISRESOLV (class))
623 __objc_resolve_class_links ();
624
625 super = class->super_class;
626
627 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
628 __objc_install_dispatch_table_for_class (super);
629
630 /* Allocate dtable if necessary. */
631 if (super == 0)
632 {
633 objc_mutex_lock (__objc_runtime_mutex);
634 class->dtable = sarray_new (__objc_selector_max_index, 0);
635 objc_mutex_unlock (__objc_runtime_mutex);
636 }
637 else
638 class->dtable = sarray_lazy_copy (super->dtable);
639
640 __objc_install_methods_in_dtable (class, class->methods);
641 }
642
643 void
644 __objc_update_dispatch_table_for_class (Class class)
645 {
646 Class next;
647 struct sarray *arr;
648
649 /* Not yet installed -- skip it. */
650 if (class->dtable == __objc_uninstalled_dtable)
651 return;
652
653 objc_mutex_lock (__objc_runtime_mutex);
654
655 arr = class->dtable;
656 __objc_install_premature_dtable (class); /* someone might require it... */
657 sarray_free (arr); /* release memory */
658
659 /* Could have been lazy... */
660 __objc_install_dispatch_table_for_class (class);
661
662 if (class->subclass_list) /* Traverse subclasses. */
663 for (next = class->subclass_list; next; next = next->sibling_class)
664 __objc_update_dispatch_table_for_class (next);
665
666 objc_mutex_unlock (__objc_runtime_mutex);
667 }
668
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
675 SEL's by the function __objc_register_selectors_from_class. */
676 void
677 class_add_method_list (Class class, struct objc_method_list * list)
678 {
679 /* Passing of a linked list is not allowed. Do multiple calls. */
680 assert (! list->method_next);
681
682 __objc_register_selectors_from_list(list);
683
684 /* Add the methods to the class's method list. */
685 list->method_next = class->methods;
686 class->methods = list;
687
688 /* Update the dispatch table of class. */
689 __objc_update_dispatch_table_for_class (class);
690 }
691
692 struct objc_method *
693 class_get_instance_method (Class class, SEL op)
694 {
695 return search_for_method_in_hierarchy (class, op);
696 }
697
698 struct objc_method *
699 class_get_class_method (MetaClass class, SEL op)
700 {
701 return search_for_method_in_hierarchy (class, op);
702 }
703
704 struct objc_method *
705 class_getInstanceMethod (Class class_, SEL selector)
706 {
707 struct objc_method *m;
708
709 if (class_ == Nil || selector == NULL)
710 return NULL;
711
712 m = search_for_method_in_hierarchy (class_, selector);
713 if (m)
714 return m;
715
716 /* Try going through +resolveInstanceMethod:, and do the search
717 again if successful. */
718 if (__objc_resolve_instance_method (class_, selector))
719 return search_for_method_in_hierarchy (class_, selector);
720
721 return NULL;
722 }
723
724 struct objc_method *
725 class_getClassMethod (Class class_, SEL selector)
726 {
727 struct objc_method *m;
728
729 if (class_ == Nil || selector == NULL)
730 return NULL;
731
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;
744 }
745
746 BOOL
747 class_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
758 method_name = sel_getName (selector);
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
793 IMP
794 class_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
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. */
819 static struct objc_method *
820 search_for_method_in_hierarchy (Class cls, SEL sel)
821 {
822 struct objc_method * method = NULL;
823 Class class;
824
825 if (! sel_is_mapped (sel))
826 return NULL;
827
828 /* Scan the method list of the class. If the method isn't found in
829 the list then step to its super class. */
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
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. */
841 struct objc_method *
842 search_for_method_in_list (struct objc_method_list * list, SEL op)
843 {
844 struct objc_method_list * method_list = list;
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 {
857 struct objc_method * method = &method_list->method_list[i];
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
872 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
873
874 /* Forwarding pointers/integers through the normal registers. */
875 static 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
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. */
893 static 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
904 static __big
905 #else
906 static 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
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. */
928 static 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
935 /* First try if the object understands forward::. */
936 if (! frwd_sel)
937 frwd_sel = sel_get_any_uid ("forward::");
938
939 if (__objc_responds_to (object, frwd_sel))
940 {
941 imp = get_imp (object->class_pointer, frwd_sel);
942 return (*imp) (object, frwd_sel, sel, args);
943 }
944
945 /* If the object recognizes the doesNotRecognize: method then we're
946 going to send it. */
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
955 error:. If it does then sent it. */
956 {
957 char msg[256 + strlen ((const char *) sel_getName (sel))
958 + strlen ((const char *) object->class_pointer->name)];
959
960 sprintf (msg, "(%s) %s does not recognize %s",
961 (CLS_ISMETA (object->class_pointer)
962 ? "class"
963 : "instance" ),
964 object->class_pointer->name, sel_getName (sel));
965
966 /* TODO: support for error: is surely deprecated ? */
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
974 /* The object doesn't respond to doesNotRecognize: or error:;
975 Therefore, a default action is taken. */
976 _objc_abort ("%s\n", msg);
977
978 return 0;
979 }
980 }
981
982 void
983 __objc_print_dtable_stats ()
984 {
985 int total = 0;
986
987 objc_mutex_lock (__objc_runtime_mutex);
988
989 #ifdef OBJC_SPARSE2
990 printf ("memory usage: (%s)\n", "2-level sparse arrays");
991 #else
992 printf ("memory usage: (%s)\n", "3-level sparse arrays");
993 #endif
994
995 printf ("arrays: %d = %ld bytes\n", narrays,
996 (long) ((size_t) narrays * sizeof (struct sarray)));
997 total += narrays * sizeof (struct sarray);
998 printf ("buckets: %d = %ld bytes\n", nbuckets,
999 (long) ((size_t) nbuckets * sizeof (struct sbucket)));
1000 total += nbuckets * sizeof (struct sbucket);
1001
1002 printf ("idxtables: %d = %ld bytes\n",
1003 idxsize, (long) ((size_t) idxsize * sizeof (void *)));
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);
1010 }
1011
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. */
1015 struct sarray *
1016 objc_get_uninstalled_dtable (void)
1017 {
1018 return __objc_uninstalled_dtable;
1019 }