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
67 * to return a function that performs the real forwarding. If both
68 * are set, the result of __objc_msg_forward2 will be preferred over
69 * that of __objc_msg_forward. If both return NULL or are unset,
70 * the libgcc based functions (__builtin_apply and friends) are
73 IMP (*__objc_msg_forward
) (SEL
) = NULL
;
74 IMP (*__objc_msg_forward2
) (id
, SEL
) = NULL
;
76 /* Send +initialize to class */
77 static void __objc_send_initialize (Class
);
79 static void __objc_install_dispatch_table_for_class (Class
);
81 /* Forward declare some functions */
82 static void __objc_init_install_dtable (id
, SEL
);
84 /* Various forwarding functions that are used based upon the
85 return type for the selector.
86 __objc_block_forward for structures.
87 __objc_double_forward for floats/doubles.
88 __objc_word_forward for pointers or types that fit in registers. */
89 static double __objc_double_forward (id
, SEL
, ...);
90 static id
__objc_word_forward (id
, SEL
, ...);
91 typedef struct { id many
[8]; } __big
;
92 #if INVISIBLE_STRUCT_RETURN
97 __objc_block_forward (id
, SEL
, ...);
98 static struct objc_method
* search_for_method_in_hierarchy (Class
class, SEL sel
);
99 struct objc_method
* search_for_method_in_list (struct objc_method_list
* list
, SEL op
);
100 id
nil_method (id
, SEL
);
102 /* Given a selector, return the proper forwarding implementation. */
105 __objc_get_forward_imp (id rcv
, SEL sel
)
107 /* If a custom forwarding hook was registered, try getting a forwarding
108 function from it. There are two forward routine hooks, one that
109 takes the receiver as an argument and one that does not. */
110 if (__objc_msg_forward2
)
113 if ((result
= __objc_msg_forward2 (rcv
, sel
)) != NULL
)
116 if (__objc_msg_forward
)
119 if ((result
= __objc_msg_forward (sel
)) != NULL
)
123 /* In all other cases, use the default forwarding functions built using
124 __builtin_apply and friends. */
126 const char *t
= sel
->sel_types
;
128 if (t
&& (*t
== '[' || *t
== '(' || *t
== '{')
129 #ifdef OBJC_MAX_STRUCT_BY_VALUE
130 && objc_sizeof_type (t
) > OBJC_MAX_STRUCT_BY_VALUE
133 return (IMP
)__objc_block_forward
;
134 else if (t
&& (*t
== 'f' || *t
== 'd'))
135 return (IMP
)__objc_double_forward
;
137 return (IMP
)__objc_word_forward
;
141 /* Selectors for +resolveClassMethod: and +resolveInstanceMethod:.
142 These are set up at startup. */
143 static SEL selector_resolveClassMethod
= NULL
;
144 static SEL selector_resolveInstanceMethod
= NULL
;
146 /* Internal routines use to resolve a class method using
147 +resolveClassMethod:. 'class' is always a non-Nil class (*not* a
148 meta-class), and 'sel' is the selector that we are trying to
149 resolve. This must be called when class is not Nil, and the
150 dispatch table for class methods has already been installed.
152 This routine tries to call +resolveClassMethod: to give an
153 opportunity to resolve the method. If +resolveClassMethod: returns
154 YES, it tries looking up the method again, and if found, it returns
155 it. Else, it returns NULL. */
158 __objc_resolve_class_method (Class
class, SEL sel
)
160 /* We need to lookup +resolveClassMethod:. */
161 BOOL (*resolveMethodIMP
) (id
, SEL
, SEL
);
163 /* The dispatch table for class methods is already installed and we
164 don't want any forwarding to happen when looking up this method,
165 so we just look it up directly. Note that if 'sel' is precisely
166 +resolveClassMethod:, this would look it up yet again and find
167 nothing. That's no problem and there's no recursion. */
168 resolveMethodIMP
= (BOOL (*) (id
, SEL
, SEL
))sarray_get_safe
169 (class->class_pointer
->dtable
, (size_t) selector_resolveClassMethod
->sel_id
);
171 if (resolveMethodIMP
&& resolveMethodIMP ((id
)class, selector_resolveClassMethod
, sel
))
173 /* +resolveClassMethod: returned YES. Look the method up again.
174 We already know the dtable is installed. */
176 /* TODO: There is the case where +resolveClassMethod: is buggy
177 and returned YES without actually adding the method. We
178 could maybe print an error message. */
179 return sarray_get_safe (class->class_pointer
->dtable
, (size_t) sel
->sel_id
);
185 /* Internal routines use to resolve a instance method using
186 +resolveInstanceMethod:. 'class' is always a non-Nil class, and
187 'sel' is the selector that we are trying to resolve. This must be
188 called when class is not Nil, and the dispatch table for instance
189 methods has already been installed.
191 This routine tries to call +resolveInstanceMethod: to give an
192 opportunity to resolve the method. If +resolveInstanceMethod:
193 returns YES, it tries looking up the method again, and if found, it
194 returns it. Else, it returns NULL. */
197 __objc_resolve_instance_method (Class
class, SEL sel
)
199 /* We need to lookup +resolveInstanceMethod:. */
200 BOOL (*resolveMethodIMP
) (id
, SEL
, SEL
);
202 /* The dispatch table for class methods may not be already installed
203 so we have to install it if needed. */
204 resolveMethodIMP
= sarray_get_safe (class->class_pointer
->dtable
,
205 (size_t) selector_resolveInstanceMethod
->sel_id
);
206 if (resolveMethodIMP
== 0)
208 /* Try again after installing the dtable. */
209 if (class->class_pointer
->dtable
== __objc_uninstalled_dtable
)
211 objc_mutex_lock (__objc_runtime_mutex
);
212 if (class->class_pointer
->dtable
== __objc_uninstalled_dtable
)
213 __objc_install_dispatch_table_for_class (class->class_pointer
);
214 objc_mutex_unlock (__objc_runtime_mutex
);
216 resolveMethodIMP
= sarray_get_safe (class->class_pointer
->dtable
,
217 (size_t) selector_resolveInstanceMethod
->sel_id
);
220 if (resolveMethodIMP
&& resolveMethodIMP ((id
)class, selector_resolveInstanceMethod
, sel
))
222 /* +resolveInstanceMethod: returned YES. Look the method up
223 again. We already know the dtable is installed. */
225 /* TODO: There is the case where +resolveInstanceMethod: is
226 buggy and returned YES without actually adding the method.
227 We could maybe print an error message. */
228 return sarray_get_safe (class->dtable
, (size_t) sel
->sel_id
);
234 /* Temporary definition until we include objc/runtime.h. */
235 objc_EXPORT Class
objc_lookupClass (const char *name
);
237 /* Given a class and selector, return the selector's implementation. */
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
270 and get the real method */
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).
282 res
= sarray_get_safe (class->dtable
, (size_t) sel
->sel_id
);
285 /* The dispatch table has been installed, and the method
286 is not in the dispatch table. So the method just
287 doesn't exist for the class. */
289 /* Try going through the +resolveClassMethod: or
290 +resolveInstanceMethod: process. */
291 if (CLS_ISMETA (class))
293 /* We have the meta class, but we need to invoke the
294 +resolveClassMethod: method on the class. So, we
295 need to obtain the class from the meta class,
296 which we do using the fact that both the class
297 and the meta-class have the same name. */
298 Class realClass
= objc_lookupClass (class->name
);
300 res
= __objc_resolve_class_method (realClass
, sel
);
303 res
= __objc_resolve_instance_method (class, sel
);
307 /* If that fails, then return the forwarding
308 implementation. We don't know the receiver (only its
309 class), so we have to pass 'nil' as the first
310 argument. Passing the class as first argument is
311 wrong because the class is not the receiver; it can
312 result in us calling a class method when we want an
313 instance method of the same name. */
314 res
= __objc_get_forward_imp (nil
, sel
);
322 /* The new name of get_imp(). */
324 class_getMethodImplementation (Class class_
, SEL selector
)
326 if (class_
== Nil
|| selector
== NULL
)
329 /* get_imp is inlined, so we're good. */
330 return get_imp (class_
, selector
);
333 /* Given a method, return its implementation. */
335 method_get_imp (struct objc_method
* method
)
337 return (method
!= (struct objc_method
*)0) ? method
->method_imp
: (IMP
)0;
340 /* Query if an object can respond to a selector, returns YES if the
341 object implements the selector otherwise NO. Does not check if the
342 method can be forwarded. */
345 __objc_responds_to (id object
, SEL sel
)
349 /* Install dispatch table if need be */
350 if (object
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
352 objc_mutex_lock (__objc_runtime_mutex
);
353 if (object
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
355 __objc_install_dispatch_table_for_class (object
->class_pointer
);
357 objc_mutex_unlock (__objc_runtime_mutex
);
360 /* Get the method from the dispatch table */
361 res
= sarray_get_safe (object
->class_pointer
->dtable
, (size_t) sel
->sel_id
);
366 class_respondsToSelector (Class class_
, SEL selector
)
370 if (class_
== Nil
|| selector
== NULL
)
373 /* Install dispatch table if need be */
374 if (class_
->dtable
== __objc_uninstalled_dtable
)
376 objc_mutex_lock (__objc_runtime_mutex
);
377 if (class_
->dtable
== __objc_uninstalled_dtable
)
379 __objc_install_dispatch_table_for_class (class_
);
381 objc_mutex_unlock (__objc_runtime_mutex
);
384 /* Get the method from the dispatch table */
385 res
= sarray_get_safe (class_
->dtable
, (size_t) selector
->sel_id
);
389 /* This is the lookup function. All entries in the table are either a
390 valid method *or* zero. If zero then either the dispatch table
391 needs to be installed or it doesn't exist and forwarding is attempted. */
394 objc_msg_lookup (id receiver
, SEL op
)
399 result
= sarray_get_safe (receiver
->class_pointer
->dtable
,
403 /* Not a valid method */
404 if (receiver
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
406 /* The dispatch table needs to be installed.
407 This happens on the very first method call to the class. */
408 __objc_init_install_dtable (receiver
, op
);
410 /* Get real method for this in newly installed dtable */
411 result
= get_imp (receiver
->class_pointer
, op
);
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).
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
481 dispatch table needs to be installed; thus it is called once
482 for each class, 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
;
555 while (method_list
) {
557 struct objc_method
* method
;
559 for (i
= 0; i
< method_list
->method_count
; i
++) {
560 method
= &(method_list
->method_list
[i
]);
561 if (method
->method_name
562 && method
->method_name
->sel_id
== op
->sel_id
) {
563 imp
= method
->method_imp
;
571 method_list
= method_list
->method_next
;
575 (*imp
) ((id
) class, op
);
581 /* Walk on the methods list of class and install the methods in the reverse
582 order of the lists. Since methods added by categories are before the methods
583 of class in the methods list, this allows categories to substitute methods
584 declared in class. However if more than one category replaces the same
585 method nothing is guaranteed about what method will be used.
586 Assumes that __objc_runtime_mutex is locked down. */
588 __objc_install_methods_in_dtable (Class
class, struct objc_method_list
* method_list
)
595 if (method_list
->method_next
)
596 __objc_install_methods_in_dtable (class, method_list
->method_next
);
598 for (i
= 0; i
< method_list
->method_count
; i
++)
600 struct objc_method
* method
= &(method_list
->method_list
[i
]);
601 sarray_at_put_safe (class->dtable
,
602 (sidx
) method
->method_name
->sel_id
,
607 /* Assumes that __objc_runtime_mutex is locked down. */
609 __objc_install_dispatch_table_for_class (Class
class)
613 /* If the class has not yet had its class links resolved, we must
614 re-compute all class links */
615 if (! CLS_ISRESOLV (class))
616 __objc_resolve_class_links ();
618 super
= class->super_class
;
620 if (super
!= 0 && (super
->dtable
== __objc_uninstalled_dtable
))
621 __objc_install_dispatch_table_for_class (super
);
623 /* Allocate dtable if necessary */
626 objc_mutex_lock (__objc_runtime_mutex
);
627 class->dtable
= sarray_new (__objc_selector_max_index
, 0);
628 objc_mutex_unlock (__objc_runtime_mutex
);
631 class->dtable
= sarray_lazy_copy (super
->dtable
);
633 __objc_install_methods_in_dtable (class, class->methods
);
637 __objc_update_dispatch_table_for_class (Class
class)
642 /* not yet installed -- skip it */
643 if (class->dtable
== __objc_uninstalled_dtable
)
646 objc_mutex_lock (__objc_runtime_mutex
);
649 __objc_install_premature_dtable (class); /* someone might require it... */
650 sarray_free (arr
); /* release memory */
652 /* could have been lazy... */
653 __objc_install_dispatch_table_for_class (class);
655 if (class->subclass_list
) /* Traverse subclasses */
656 for (next
= class->subclass_list
; next
; next
= next
->sibling_class
)
657 __objc_update_dispatch_table_for_class (next
);
659 objc_mutex_unlock (__objc_runtime_mutex
);
663 /* This function adds a method list to a class. This function is
664 typically called by another function specific to the run-time. As
665 such this function does not worry about thread safe issues.
667 This one is only called for categories. Class objects have their
668 methods installed right away, and their selectors are made into
669 SEL's by the function __objc_register_selectors_from_class. */
671 class_add_method_list (Class
class, struct objc_method_list
* list
)
673 /* Passing of a linked list is not allowed. Do multiple calls. */
674 assert (! list
->method_next
);
676 __objc_register_selectors_from_list(list
);
678 /* Add the methods to the class's method list. */
679 list
->method_next
= class->methods
;
680 class->methods
= list
;
682 /* Update the dispatch table of class */
683 __objc_update_dispatch_table_for_class (class);
687 class_get_instance_method (Class
class, SEL op
)
689 return search_for_method_in_hierarchy (class, op
);
693 class_get_class_method (MetaClass
class, SEL op
)
695 return search_for_method_in_hierarchy (class, op
);
699 class_getInstanceMethod (Class class_
, SEL selector
)
701 struct objc_method
*m
;
703 if (class_
== Nil
|| selector
== NULL
)
706 m
= search_for_method_in_hierarchy (class_
, selector
);
710 /* Try going through +resolveInstanceMethod:, and do
711 the search again if successful. */
712 if (__objc_resolve_instance_method (class_
, selector
))
713 return search_for_method_in_hierarchy (class_
, selector
);
719 class_getClassMethod (Class class_
, SEL selector
)
721 struct objc_method
*m
;
723 if (class_
== Nil
|| selector
== NULL
)
726 m
= search_for_method_in_hierarchy (class_
->class_pointer
,
731 /* Try going through +resolveClassMethod:, and do the search again
733 if (__objc_resolve_class_method (class_
, selector
))
734 return search_for_method_in_hierarchy (class_
->class_pointer
,
741 class_addMethod (Class class_
, SEL selector
, IMP implementation
,
742 const char *method_types
)
744 struct objc_method_list
*method_list
;
745 struct objc_method
*method
;
746 const char *method_name
;
748 if (class_
== Nil
|| selector
== NULL
|| implementation
== NULL
749 || method_types
== NULL
|| (strcmp (method_types
, "") == 0))
752 method_name
= sel_get_name (selector
);
753 if (method_name
== NULL
)
756 method_list
= (struct objc_method_list
*)objc_calloc (1, sizeof (struct objc_method_list
));
757 method_list
->method_count
= 1;
759 method
= &(method_list
->method_list
[0]);
760 method
->method_name
= objc_malloc (strlen (method_name
) + 1);
761 strcpy ((char *)method
->method_name
, method_name
);
763 method
->method_types
= objc_malloc (strlen (method_types
) + 1);
764 strcpy ((char *)method
->method_types
, method_types
);
766 method
->method_imp
= implementation
;
768 if (CLS_IS_IN_CONSTRUCTION (class_
))
770 /* We only need to add the method to the list. It will be
771 registered with the runtime when the class pair is registered
773 method_list
->method_next
= class_
->methods
;
774 class_
->methods
= method_list
;
778 /* Add the method to a live class. */
779 objc_mutex_lock (__objc_runtime_mutex
);
780 class_add_method_list (class_
, method_list
);
781 objc_mutex_unlock (__objc_runtime_mutex
);
787 /* Temporarily, until we include objc/runtime.h. */
789 method_setImplementation (struct objc_method
* method
, IMP implementation
);
792 class_replaceMethod (Class class_
, SEL selector
, IMP implementation
,
793 const char *method_types
)
795 struct objc_method
* method
;
797 if (class_
== Nil
|| selector
== NULL
|| implementation
== NULL
798 || method_types
== NULL
)
801 method
= search_for_method_in_hierarchy (class_
, selector
);
805 return method_setImplementation (method
, implementation
);
809 class_addMethod (class_
, selector
, implementation
, method_types
);
814 /* Search for a method starting from the current class up its hierarchy.
815 Return a pointer to the method's method structure if found. NULL
817 static struct objc_method
*
818 search_for_method_in_hierarchy (Class cls
, SEL sel
)
820 struct objc_method
* method
= NULL
;
823 if (! sel_is_mapped (sel
))
826 /* Scan the method list of the class. If the method isn't found in the
827 list then step to its super class. */
828 for (class = cls
; ((! method
) && class); class = class->super_class
)
829 method
= search_for_method_in_list (class->methods
, sel
);
836 /* Given a linked list of method and a method's name. Search for the named
837 method's method structure. Return a pointer to the method's method
838 structure if found. NULL otherwise. */
840 search_for_method_in_list (struct objc_method_list
* list
, SEL op
)
842 struct objc_method_list
* method_list
= list
;
844 if (! sel_is_mapped (op
))
847 /* If not found then we'll search the list. */
852 /* Search the method list. */
853 for (i
= 0; i
< method_list
->method_count
; ++i
)
855 struct objc_method
* method
= &method_list
->method_list
[i
];
857 if (method
->method_name
)
858 if (method
->method_name
->sel_id
== op
->sel_id
)
862 /* The method wasn't found. Follow the link to the next list of
864 method_list
= method_list
->method_next
;
870 static retval_t
__objc_forward (id object
, SEL sel
, arglist_t args
);
872 /* Forwarding pointers/integers through the normal registers */
874 __objc_word_forward (id rcv
, SEL op
, ...)
878 args
= __builtin_apply_args ();
879 res
= __objc_forward (rcv
, op
, args
);
881 __builtin_return (res
);
886 /* Specific routine for forwarding floats/double because of
887 architectural differences on some processors. i386s for
888 example which uses a floating point stack versus general
889 registers for floating point numbers. This forward routine
890 makes sure that GCC restores the proper return values */
892 __objc_double_forward (id rcv
, SEL op
, ...)
896 args
= __builtin_apply_args ();
897 res
= __objc_forward (rcv
, op
, args
);
898 __builtin_return (res
);
901 #if INVISIBLE_STRUCT_RETURN
906 __objc_block_forward (id rcv
, SEL op
, ...)
910 args
= __builtin_apply_args ();
911 res
= __objc_forward (rcv
, op
, args
);
913 __builtin_return (res
);
915 #if INVISIBLE_STRUCT_RETURN
916 return (__big
) {{0, 0, 0, 0, 0, 0, 0, 0}};
923 /* This function is installed in the dispatch table for all methods which are
924 not implemented. Thus, it is called when a selector is not recognized. */
926 __objc_forward (id object
, SEL sel
, arglist_t args
)
929 static SEL frwd_sel
= 0; /* !T:SAFE2 */
932 /* first try if the object understands forward:: */
934 frwd_sel
= sel_get_any_uid ("forward::");
936 if (__objc_responds_to (object
, frwd_sel
))
938 imp
= get_imp (object
->class_pointer
, frwd_sel
);
939 return (*imp
) (object
, frwd_sel
, sel
, args
);
942 /* If the object recognizes the doesNotRecognize: method then we're going
944 err_sel
= sel_get_any_uid ("doesNotRecognize:");
945 if (__objc_responds_to (object
, err_sel
))
947 imp
= get_imp (object
->class_pointer
, err_sel
);
948 return (*imp
) (object
, err_sel
, sel
);
951 /* The object doesn't recognize the method. Check for responding to
952 error:. If it does then sent it. */
954 char msg
[256 + strlen ((const char *) sel_get_name (sel
))
955 + strlen ((const char *) object
->class_pointer
->name
)];
957 sprintf (msg
, "(%s) %s does not recognize %s",
958 (CLS_ISMETA (object
->class_pointer
)
961 object
->class_pointer
->name
, sel_get_name (sel
));
963 /* TODO: support for error: is surely deprecated ? */
964 err_sel
= sel_get_any_uid ("error:");
965 if (__objc_responds_to (object
, err_sel
))
967 imp
= get_imp (object
->class_pointer
, err_sel
);
968 return (*imp
) (object
, sel_get_any_uid ("error:"), msg
);
971 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
972 a default action is taken. */
973 _objc_abort ("%s\n", msg
);
980 __objc_print_dtable_stats ()
984 objc_mutex_lock (__objc_runtime_mutex
);
987 printf ("memory usage: (%s)\n", "2-level sparse arrays");
989 printf ("memory usage: (%s)\n", "3-level sparse arrays");
992 printf ("arrays: %d = %ld bytes\n", narrays
,
993 (long) ((size_t) narrays
* sizeof (struct sarray
)));
994 total
+= narrays
* sizeof (struct sarray
);
995 printf ("buckets: %d = %ld bytes\n", nbuckets
,
996 (long) ((size_t) nbuckets
* sizeof (struct sbucket
)));
997 total
+= nbuckets
* sizeof (struct sbucket
);
999 printf ("idxtables: %d = %ld bytes\n",
1000 idxsize
, (long) ((size_t) idxsize
* sizeof (void *)));
1001 total
+= idxsize
* sizeof (void *);
1002 printf ("-----------------------------------\n");
1003 printf ("total: %d bytes\n", total
);
1004 printf ("===================================\n");
1006 objc_mutex_unlock (__objc_runtime_mutex
);
1009 /* Returns the uninstalled dispatch table indicator.
1010 If a class' dispatch table points to __objc_uninstalled_dtable
1011 then that means it needs its dispatch table to be installed. */
1014 objc_get_uninstalled_dtable ()
1016 return __objc_uninstalled_dtable
;