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