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
6 This file is part of GCC.
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.
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
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.
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/>. */
27 /* FIXME: This file has no business including tm.h. */
28 /* FIXME: This should be using libffi instead of __builtin_apply
31 #include "objc-private/common.h"
32 #include "objc-private/error.h"
34 #include "coretypes.h"
36 #include "objc/objc-api.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 */
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)
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. */
57 #if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
58 #define INVISIBLE_STRUCT_RETURN 1
60 #define INVISIBLE_STRUCT_RETURN 0
63 /* The uninstalled dispatch table. */
64 struct sarray
*__objc_uninstalled_dtable
= 0; /* !T:MUTEX */
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
;
74 /* Send +initialize to class. */
75 static void __objc_send_initialize (Class
);
77 static void __objc_install_dispatch_table_for_class (Class
);
79 /* Forward declare some functions. */
80 static void __objc_init_install_dtable (id
, SEL
);
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
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
);
100 /* Given a selector, return the proper forwarding implementation. */
103 __objc_get_forward_imp (id rcv
, SEL sel
)
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
109 if (__objc_msg_forward2
)
112 if ((result
= __objc_msg_forward2 (rcv
, sel
)) != NULL
)
115 if (__objc_msg_forward
)
118 if ((result
= __objc_msg_forward (sel
)) != NULL
)
122 /* In all other cases, use the default forwarding functions built
123 using __builtin_apply and friends. */
125 const char *t
= sel
->sel_types
;
127 if (t
&& (*t
== '[' || *t
== '(' || *t
== '{')
128 #ifdef OBJC_MAX_STRUCT_BY_VALUE
129 && objc_sizeof_type (t
) > OBJC_MAX_STRUCT_BY_VALUE
132 return (IMP
)__objc_block_forward
;
133 else if (t
&& (*t
== 'f' || *t
== 'd'))
134 return (IMP
)__objc_double_forward
;
136 return (IMP
)__objc_word_forward
;
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
;
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.
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. */
157 __objc_resolve_class_method (Class
class, SEL sel
)
159 /* We need to lookup +resolveClassMethod:. */
160 BOOL (*resolveMethodIMP
) (id
, SEL
, SEL
);
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
);
170 if (resolveMethodIMP
&& resolveMethodIMP ((id
)class, selector_resolveClassMethod
, sel
))
172 /* +resolveClassMethod: returned YES. Look the method up again.
173 We already know the dtable is installed. */
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
);
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.
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. */
196 __objc_resolve_instance_method (Class
class, SEL sel
)
198 /* We need to lookup +resolveInstanceMethod:. */
199 BOOL (*resolveMethodIMP
) (id
, SEL
, SEL
);
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)
207 /* Try again after installing the dtable. */
208 if (class->class_pointer
->dtable
== __objc_uninstalled_dtable
)
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
);
215 resolveMethodIMP
= sarray_get_safe (class->class_pointer
->dtable
,
216 (size_t) selector_resolveInstanceMethod
->sel_id
);
219 if (resolveMethodIMP
&& resolveMethodIMP ((id
)class, selector_resolveInstanceMethod
, sel
))
221 /* +resolveInstanceMethod: returned YES. Look the method up
222 again. We already know the dtable is installed. */
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
);
233 /* Temporary definition until we include objc/runtime.h. */
234 objc_EXPORT Class
objc_lookupClass (const char *name
);
236 /* Given a class and selector, return the selector's
240 get_imp (Class
class, SEL sel
)
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
250 void *res
= sarray_get_safe (class->dtable
, (size_t) sel
->sel_id
);
253 /* Not a valid method. */
254 if (class->dtable
== __objc_uninstalled_dtable
)
256 /* The dispatch table needs to be installed. */
257 objc_mutex_lock (__objc_runtime_mutex
);
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
263 if (class->dtable
== __objc_uninstalled_dtable
)
265 __objc_install_dispatch_table_for_class (class);
268 objc_mutex_unlock (__objc_runtime_mutex
);
269 /* Call ourselves with the installed dispatch table and get
271 res
= get_imp (class, sel
);
275 /* The dispatch table has been installed. */
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
);
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. */
288 /* Try going through the +resolveClassMethod: or
289 +resolveInstanceMethod: process. */
290 if (CLS_ISMETA (class))
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
);
299 res
= __objc_resolve_class_method (realClass
, sel
);
302 res
= __objc_resolve_instance_method (class, sel
);
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
);
321 /* The new name of get_imp(). */
323 class_getMethodImplementation (Class class_
, SEL selector
)
325 if (class_
== Nil
|| selector
== NULL
)
328 /* get_imp is inlined, so we're good. */
329 return get_imp (class_
, selector
);
332 /* Given a method, return its implementation. */
334 method_get_imp (struct objc_method
* method
)
336 return (method
!= (struct objc_method
*)0) ? method
->method_imp
: (IMP
)0;
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. */
344 __objc_responds_to (id object
, SEL sel
)
348 /* Install dispatch table if need be. */
349 if (object
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
351 objc_mutex_lock (__objc_runtime_mutex
);
352 if (object
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
354 __objc_install_dispatch_table_for_class (object
->class_pointer
);
356 objc_mutex_unlock (__objc_runtime_mutex
);
359 /* Get the method from the dispatch table. */
360 res
= sarray_get_safe (object
->class_pointer
->dtable
, (size_t) sel
->sel_id
);
365 class_respondsToSelector (Class class_
, SEL selector
)
369 if (class_
== Nil
|| selector
== NULL
)
372 /* Install dispatch table if need be. */
373 if (class_
->dtable
== __objc_uninstalled_dtable
)
375 objc_mutex_lock (__objc_runtime_mutex
);
376 if (class_
->dtable
== __objc_uninstalled_dtable
)
378 __objc_install_dispatch_table_for_class (class_
);
380 objc_mutex_unlock (__objc_runtime_mutex
);
383 /* Get the method from the dispatch table. */
384 res
= sarray_get_safe (class_
->dtable
, (size_t) selector
->sel_id
);
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
393 objc_msg_lookup (id receiver
, SEL op
)
398 result
= sarray_get_safe (receiver
->class_pointer
->dtable
,
402 /* Not a valid method. */
403 if (receiver
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
405 /* The dispatch table needs to be installed. This
406 happens on the very first method call to the
408 __objc_init_install_dtable (receiver
, op
);
410 /* Get real method for this in newly installed
412 result
= get_imp (receiver
->class_pointer
, op
);
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
,
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
);
429 result
= __objc_resolve_instance_method (receiver
->class_pointer
,
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
);
444 return (IMP
)nil_method
;
448 objc_msg_lookup_super (Super_t super
, SEL sel
)
451 return get_imp (super
->class, sel
);
453 return (IMP
)nil_method
;
456 int method_get_sizeof_arguments (Method
*);
459 objc_msg_sendv (id object
, SEL op
, arglist_t arg_frame
)
461 Method
*m
= class_get_instance_method (object
->class_pointer
, op
);
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
,
467 method_get_sizeof_arguments (m
));
471 __objc_init_dispatch_tables ()
473 __objc_uninstalled_dtable
= sarray_new (200, 0);
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:");
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. */
484 __objc_init_install_dtable (id receiver
, SEL op
__attribute__ ((__unused__
)))
486 objc_mutex_lock (__objc_runtime_mutex
);
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
)
492 objc_mutex_unlock (__objc_runtime_mutex
);
496 if (CLS_ISCLASS (receiver
->class_pointer
))
498 /* receiver is an ordinary object. */
499 assert (CLS_ISCLASS (receiver
->class_pointer
));
501 /* Install instance methods table. */
502 __objc_install_dispatch_table_for_class (receiver
->class_pointer
);
504 /* Call +initialize -- this will in turn install the factory
505 dispatch table if not already done. :-) */
506 __objc_send_initialize (receiver
->class_pointer
);
510 /* receiver is a class object. */
511 assert (CLS_ISCLASS ((Class
)receiver
));
512 assert (CLS_ISMETA (receiver
->class_pointer
));
514 /* Install real dtable for factory methods. */
515 __objc_install_dispatch_table_for_class (receiver
->class_pointer
);
517 __objc_send_initialize ((Class
)receiver
);
519 objc_mutex_unlock (__objc_runtime_mutex
);
522 /* Install dummy table for class which causes the first message to
523 that class (or instances hereof) to be initialized properly. */
525 __objc_install_premature_dtable (Class
class)
527 assert (__objc_uninstalled_dtable
);
528 class->dtable
= __objc_uninstalled_dtable
;
531 /* Send +initialize to class if not already done. */
533 __objc_send_initialize (Class
class)
535 /* This *must* be a class object. */
536 assert (CLS_ISCLASS (class));
537 assert (! CLS_ISMETA (class));
539 if (! CLS_ISINITIALIZED (class))
541 CLS_SETINITIALIZED (class);
542 CLS_SETINITIALIZED (class->class_pointer
);
544 /* Create the garbage collector type memory description. */
545 __objc_generate_gc_type_description (class);
547 if (class->super_class
)
548 __objc_send_initialize (class->super_class
);
551 SEL op
= sel_register_name ("initialize");
553 struct objc_method_list
* method_list
= class->class_pointer
->methods
;
558 struct objc_method
* method
;
560 for (i
= 0; i
< method_list
->method_count
; i
++)
562 method
= &(method_list
->method_list
[i
]);
563 if (method
->method_name
564 && method
->method_name
->sel_id
== op
->sel_id
)
566 imp
= method
->method_imp
;
574 method_list
= method_list
->method_next
;
577 (*imp
) ((id
) class, op
);
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. */
590 __objc_install_methods_in_dtable (Class
class, struct objc_method_list
* method_list
)
597 if (method_list
->method_next
)
598 __objc_install_methods_in_dtable (class, method_list
->method_next
);
600 for (i
= 0; i
< method_list
->method_count
; i
++)
602 struct objc_method
* method
= &(method_list
->method_list
[i
]);
603 sarray_at_put_safe (class->dtable
,
604 (sidx
) method
->method_name
->sel_id
,
609 /* Assumes that __objc_runtime_mutex is locked down. */
611 __objc_install_dispatch_table_for_class (Class
class)
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 ();
620 super
= class->super_class
;
622 if (super
!= 0 && (super
->dtable
== __objc_uninstalled_dtable
))
623 __objc_install_dispatch_table_for_class (super
);
625 /* Allocate dtable if necessary. */
628 objc_mutex_lock (__objc_runtime_mutex
);
629 class->dtable
= sarray_new (__objc_selector_max_index
, 0);
630 objc_mutex_unlock (__objc_runtime_mutex
);
633 class->dtable
= sarray_lazy_copy (super
->dtable
);
635 __objc_install_methods_in_dtable (class, class->methods
);
639 __objc_update_dispatch_table_for_class (Class
class)
644 /* Not yet installed -- skip it. */
645 if (class->dtable
== __objc_uninstalled_dtable
)
648 objc_mutex_lock (__objc_runtime_mutex
);
651 __objc_install_premature_dtable (class); /* someone might require it... */
652 sarray_free (arr
); /* release memory */
654 /* Could have been lazy... */
655 __objc_install_dispatch_table_for_class (class);
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
);
661 objc_mutex_unlock (__objc_runtime_mutex
);
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.
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. */
672 class_add_method_list (Class
class, struct objc_method_list
* list
)
674 /* Passing of a linked list is not allowed. Do multiple calls. */
675 assert (! list
->method_next
);
677 __objc_register_selectors_from_list(list
);
679 /* Add the methods to the class's method list. */
680 list
->method_next
= class->methods
;
681 class->methods
= list
;
683 /* Update the dispatch table of class. */
684 __objc_update_dispatch_table_for_class (class);
688 class_get_instance_method (Class
class, SEL op
)
690 return search_for_method_in_hierarchy (class, op
);
694 class_get_class_method (MetaClass
class, SEL op
)
696 return search_for_method_in_hierarchy (class, op
);
700 class_getInstanceMethod (Class class_
, SEL selector
)
702 struct objc_method
*m
;
704 if (class_
== Nil
|| selector
== NULL
)
707 m
= search_for_method_in_hierarchy (class_
, selector
);
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
);
720 class_getClassMethod (Class class_
, SEL selector
)
722 struct objc_method
*m
;
724 if (class_
== Nil
|| selector
== NULL
)
727 m
= search_for_method_in_hierarchy (class_
->class_pointer
,
732 /* Try going through +resolveClassMethod:, and do the search again
734 if (__objc_resolve_class_method (class_
, selector
))
735 return search_for_method_in_hierarchy (class_
->class_pointer
,
742 class_addMethod (Class class_
, SEL selector
, IMP implementation
,
743 const char *method_types
)
745 struct objc_method_list
*method_list
;
746 struct objc_method
*method
;
747 const char *method_name
;
749 if (class_
== Nil
|| selector
== NULL
|| implementation
== NULL
750 || method_types
== NULL
|| (strcmp (method_types
, "") == 0))
753 method_name
= sel_get_name (selector
);
754 if (method_name
== NULL
)
757 method_list
= (struct objc_method_list
*)objc_calloc (1, sizeof (struct objc_method_list
));
758 method_list
->method_count
= 1;
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
);
764 method
->method_types
= objc_malloc (strlen (method_types
) + 1);
765 strcpy ((char *)method
->method_types
, method_types
);
767 method
->method_imp
= implementation
;
769 if (CLS_IS_IN_CONSTRUCTION (class_
))
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
774 method_list
->method_next
= class_
->methods
;
775 class_
->methods
= method_list
;
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
);
788 /* Temporarily, until we include objc/runtime.h. */
790 method_setImplementation (struct objc_method
* method
, IMP implementation
);
793 class_replaceMethod (Class class_
, SEL selector
, IMP implementation
,
794 const char *method_types
)
796 struct objc_method
* method
;
798 if (class_
== Nil
|| selector
== NULL
|| implementation
== NULL
799 || method_types
== NULL
)
802 method
= search_for_method_in_hierarchy (class_
, selector
);
806 return method_setImplementation (method
, implementation
);
810 class_addMethod (class_
, selector
, implementation
, method_types
);
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
)
821 struct objc_method
* method
= NULL
;
824 if (! sel_is_mapped (sel
))
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
);
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. */
841 search_for_method_in_list (struct objc_method_list
* list
, SEL op
)
843 struct objc_method_list
* method_list
= list
;
845 if (! sel_is_mapped (op
))
848 /* If not found then we'll search the list. */
853 /* Search the method list. */
854 for (i
= 0; i
< method_list
->method_count
; ++i
)
856 struct objc_method
* method
= &method_list
->method_list
[i
];
858 if (method
->method_name
)
859 if (method
->method_name
->sel_id
== op
->sel_id
)
863 /* The method wasn't found. Follow the link to the next list of
865 method_list
= method_list
->method_next
;
871 static retval_t
__objc_forward (id object
, SEL sel
, arglist_t args
);
873 /* Forwarding pointers/integers through the normal registers. */
875 __objc_word_forward (id rcv
, SEL op
, ...)
879 args
= __builtin_apply_args ();
880 res
= __objc_forward (rcv
, op
, args
);
882 __builtin_return (res
);
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. */
893 __objc_double_forward (id rcv
, SEL op
, ...)
897 args
= __builtin_apply_args ();
898 res
= __objc_forward (rcv
, op
, args
);
899 __builtin_return (res
);
902 #if INVISIBLE_STRUCT_RETURN
907 __objc_block_forward (id rcv
, SEL op
, ...)
911 args
= __builtin_apply_args ();
912 res
= __objc_forward (rcv
, op
, args
);
914 __builtin_return (res
);
916 #if INVISIBLE_STRUCT_RETURN
917 return (__big
) {{0, 0, 0, 0, 0, 0, 0, 0}};
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
928 __objc_forward (id object
, SEL sel
, arglist_t args
)
931 static SEL frwd_sel
= 0; /* !T:SAFE2 */
934 /* First try if the object understands forward::. */
936 frwd_sel
= sel_get_any_uid ("forward::");
938 if (__objc_responds_to (object
, frwd_sel
))
940 imp
= get_imp (object
->class_pointer
, frwd_sel
);
941 return (*imp
) (object
, frwd_sel
, sel
, args
);
944 /* If the object recognizes the doesNotRecognize: method then we're
946 err_sel
= sel_get_any_uid ("doesNotRecognize:");
947 if (__objc_responds_to (object
, err_sel
))
949 imp
= get_imp (object
->class_pointer
, err_sel
);
950 return (*imp
) (object
, err_sel
, sel
);
953 /* The object doesn't recognize the method. Check for responding to
954 error:. If it does then sent it. */
956 char msg
[256 + strlen ((const char *) sel_get_name (sel
))
957 + strlen ((const char *) object
->class_pointer
->name
)];
959 sprintf (msg
, "(%s) %s does not recognize %s",
960 (CLS_ISMETA (object
->class_pointer
)
963 object
->class_pointer
->name
, sel_get_name (sel
));
965 /* TODO: support for error: is surely deprecated ? */
966 err_sel
= sel_get_any_uid ("error:");
967 if (__objc_responds_to (object
, err_sel
))
969 imp
= get_imp (object
->class_pointer
, err_sel
);
970 return (*imp
) (object
, sel_get_any_uid ("error:"), msg
);
973 /* The object doesn't respond to doesNotRecognize: or error:;
974 Therefore, a default action is taken. */
975 _objc_abort ("%s\n", msg
);
982 __objc_print_dtable_stats ()
986 objc_mutex_lock (__objc_runtime_mutex
);
989 printf ("memory usage: (%s)\n", "2-level sparse arrays");
991 printf ("memory usage: (%s)\n", "3-level sparse arrays");
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
);
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");
1008 objc_mutex_unlock (__objc_runtime_mutex
);
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. */
1015 objc_get_uninstalled_dtable ()
1017 return __objc_uninstalled_dtable
;