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