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/runtime.h"
37 #include "objc/message.h" /* For objc_msg_lookup(), objc_msg_lookup_super(). */
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 */
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. */
55 #if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
56 #define INVISIBLE_STRUCT_RETURN 1
58 #define INVISIBLE_STRUCT_RETURN 0
61 /* The uninstalled dispatch table. */
62 struct sarray
*__objc_uninstalled_dtable
= 0; /* !T:MUTEX */
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
;
72 /* Send +initialize to class. */
73 static void __objc_send_initialize (Class
);
75 static void __objc_install_dispatch_table_for_class (Class
);
77 /* Forward declare some functions. */
78 static void __objc_init_install_dtable (id
, SEL
);
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
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
);
98 /* Given a selector, return the proper forwarding implementation. */
101 __objc_get_forward_imp (id rcv
, SEL sel
)
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
107 if (__objc_msg_forward2
)
110 if ((result
= __objc_msg_forward2 (rcv
, sel
)) != NULL
)
113 if (__objc_msg_forward
)
116 if ((result
= __objc_msg_forward (sel
)) != NULL
)
120 /* In all other cases, use the default forwarding functions built
121 using __builtin_apply and friends. */
123 const char *t
= sel
->sel_types
;
125 if (t
&& (*t
== '[' || *t
== '(' || *t
== '{')
126 #ifdef OBJC_MAX_STRUCT_BY_VALUE
127 && objc_sizeof_type (t
) > OBJC_MAX_STRUCT_BY_VALUE
130 return (IMP
)__objc_block_forward
;
131 else if (t
&& (*t
== 'f' || *t
== 'd'))
132 return (IMP
)__objc_double_forward
;
134 return (IMP
)__objc_word_forward
;
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
;
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.
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. */
155 __objc_resolve_class_method (Class
class, SEL sel
)
157 /* We need to lookup +resolveClassMethod:. */
158 BOOL (*resolveMethodIMP
) (id
, SEL
, SEL
);
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
);
168 if (resolveMethodIMP
&& resolveMethodIMP ((id
)class, selector_resolveClassMethod
, sel
))
170 /* +resolveClassMethod: returned YES. Look the method up again.
171 We already know the dtable is installed. */
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
);
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.
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. */
194 __objc_resolve_instance_method (Class
class, SEL sel
)
196 /* We need to lookup +resolveInstanceMethod:. */
197 BOOL (*resolveMethodIMP
) (id
, SEL
, SEL
);
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)
205 /* Try again after installing the dtable. */
206 if (class->class_pointer
->dtable
== __objc_uninstalled_dtable
)
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
);
213 resolveMethodIMP
= sarray_get_safe (class->class_pointer
->dtable
,
214 (size_t) selector_resolveInstanceMethod
->sel_id
);
217 if (resolveMethodIMP
&& resolveMethodIMP ((id
)class, selector_resolveInstanceMethod
, sel
))
219 /* +resolveInstanceMethod: returned YES. Look the method up
220 again. We already know the dtable is installed. */
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
);
231 /* Temporary definition until we include objc/runtime.h. */
232 objc_EXPORT Class
objc_lookupClass (const char *name
);
234 /* Given a class and selector, return the selector's
238 get_imp (Class
class, SEL sel
)
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
248 void *res
= sarray_get_safe (class->dtable
, (size_t) sel
->sel_id
);
251 /* Not a valid method. */
252 if (class->dtable
== __objc_uninstalled_dtable
)
254 /* The dispatch table needs to be installed. */
255 objc_mutex_lock (__objc_runtime_mutex
);
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
261 if (class->dtable
== __objc_uninstalled_dtable
)
263 __objc_install_dispatch_table_for_class (class);
266 objc_mutex_unlock (__objc_runtime_mutex
);
267 /* Call ourselves with the installed dispatch table and get
269 res
= get_imp (class, sel
);
273 /* The dispatch table has been installed. */
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
);
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. */
286 /* Try going through the +resolveClassMethod: or
287 +resolveInstanceMethod: process. */
288 if (CLS_ISMETA (class))
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
);
297 res
= __objc_resolve_class_method (realClass
, sel
);
300 res
= __objc_resolve_instance_method (class, sel
);
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
);
319 /* The new name of get_imp(). */
321 class_getMethodImplementation (Class class_
, SEL selector
)
323 if (class_
== Nil
|| selector
== NULL
)
326 /* get_imp is inlined, so we're good. */
327 return get_imp (class_
, selector
);
330 /* Given a method, return its implementation. This has been replaced
331 by method_getImplementation() in the modern API. */
333 method_get_imp (struct objc_method
* method
)
335 return (method
!= (struct objc_method
*)0) ? method
->method_imp
: (IMP
)0;
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. */
343 __objc_responds_to (id object
, SEL sel
)
347 /* Install dispatch table if need be. */
348 if (object
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
350 objc_mutex_lock (__objc_runtime_mutex
);
351 if (object
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
353 __objc_install_dispatch_table_for_class (object
->class_pointer
);
355 objc_mutex_unlock (__objc_runtime_mutex
);
358 /* Get the method from the dispatch table. */
359 res
= sarray_get_safe (object
->class_pointer
->dtable
, (size_t) sel
->sel_id
);
364 class_respondsToSelector (Class class_
, SEL selector
)
368 if (class_
== Nil
|| selector
== NULL
)
371 /* Install dispatch table if need be. */
372 if (class_
->dtable
== __objc_uninstalled_dtable
)
374 objc_mutex_lock (__objc_runtime_mutex
);
375 if (class_
->dtable
== __objc_uninstalled_dtable
)
377 __objc_install_dispatch_table_for_class (class_
);
379 objc_mutex_unlock (__objc_runtime_mutex
);
382 /* Get the method from the dispatch table. */
383 res
= sarray_get_safe (class_
->dtable
, (size_t) selector
->sel_id
);
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
392 objc_msg_lookup (id receiver
, SEL op
)
397 result
= sarray_get_safe (receiver
->class_pointer
->dtable
,
401 /* Not a valid method. */
402 if (receiver
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
404 /* The dispatch table needs to be installed. This
405 happens on the very first method call to the
407 __objc_init_install_dtable (receiver
, op
);
409 /* Get real method for this in newly installed
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). */
419 result
= sarray_get_safe (receiver
->class_pointer
->dtable
,
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
);
428 result
= __objc_resolve_instance_method (receiver
->class_pointer
,
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
);
443 return (IMP
)nil_method
;
447 objc_msg_lookup_super (struct objc_super
*super
, SEL sel
)
450 return get_imp (super
->super_class
, sel
);
452 return (IMP
)nil_method
;
455 /* Temporarily defined here until objc_msg_sendv() goes away. */
456 char *method_get_first_argument (struct objc_method
*,
459 char *method_get_next_argument (arglist_t argframe
,
461 int method_get_sizeof_arguments (struct objc_method
*);
464 class_get_instance_method (Class
class, SEL op
);
467 objc_msg_sendv (id object
, SEL op
, arglist_t arg_frame
)
469 struct objc_method
*m
= class_get_instance_method (object
->class_pointer
, op
);
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
,
475 method_get_sizeof_arguments (m
));
479 __objc_init_dispatch_tables ()
481 __objc_uninstalled_dtable
= sarray_new (200, 0);
483 /* TODO: It would be cool to register typed selectors here. */
484 selector_resolveClassMethod
= sel_registerName ("resolveClassMethod:");
485 selector_resolveInstanceMethod
=sel_registerName ("resolveInstanceMethod:");
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. */
492 __objc_init_install_dtable (id receiver
, SEL op
__attribute__ ((__unused__
)))
494 objc_mutex_lock (__objc_runtime_mutex
);
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
)
500 objc_mutex_unlock (__objc_runtime_mutex
);
504 if (CLS_ISCLASS (receiver
->class_pointer
))
506 /* receiver is an ordinary object. */
507 assert (CLS_ISCLASS (receiver
->class_pointer
));
509 /* Install instance methods table. */
510 __objc_install_dispatch_table_for_class (receiver
->class_pointer
);
512 /* Call +initialize -- this will in turn install the factory
513 dispatch table if not already done. :-) */
514 __objc_send_initialize (receiver
->class_pointer
);
518 /* receiver is a class object. */
519 assert (CLS_ISCLASS ((Class
)receiver
));
520 assert (CLS_ISMETA (receiver
->class_pointer
));
522 /* Install real dtable for factory methods. */
523 __objc_install_dispatch_table_for_class (receiver
->class_pointer
);
525 __objc_send_initialize ((Class
)receiver
);
527 objc_mutex_unlock (__objc_runtime_mutex
);
530 /* Install dummy table for class which causes the first message to
531 that class (or instances hereof) to be initialized properly. */
533 __objc_install_premature_dtable (Class
class)
535 assert (__objc_uninstalled_dtable
);
536 class->dtable
= __objc_uninstalled_dtable
;
539 /* Send +initialize to class if not already done. */
541 __objc_send_initialize (Class
class)
543 /* This *must* be a class object. */
544 assert (CLS_ISCLASS (class));
545 assert (! CLS_ISMETA (class));
547 if (! CLS_ISINITIALIZED (class))
549 CLS_SETINITIALIZED (class);
550 CLS_SETINITIALIZED (class->class_pointer
);
552 /* Create the garbage collector type memory description. */
553 __objc_generate_gc_type_description (class);
555 if (class->super_class
)
556 __objc_send_initialize (class->super_class
);
559 SEL op
= sel_registerName ("initialize");
561 struct objc_method_list
* method_list
= class->class_pointer
->methods
;
566 struct objc_method
* method
;
568 for (i
= 0; i
< method_list
->method_count
; i
++)
570 method
= &(method_list
->method_list
[i
]);
571 if (method
->method_name
572 && method
->method_name
->sel_id
== op
->sel_id
)
574 imp
= method
->method_imp
;
582 method_list
= method_list
->method_next
;
585 (*imp
) ((id
) class, op
);
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. */
598 __objc_install_methods_in_dtable (Class
class, struct objc_method_list
* method_list
)
605 if (method_list
->method_next
)
606 __objc_install_methods_in_dtable (class, method_list
->method_next
);
608 for (i
= 0; i
< method_list
->method_count
; i
++)
610 struct objc_method
* method
= &(method_list
->method_list
[i
]);
611 sarray_at_put_safe (class->dtable
,
612 (sidx
) method
->method_name
->sel_id
,
617 /* Assumes that __objc_runtime_mutex is locked down. */
619 __objc_install_dispatch_table_for_class (Class
class)
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 ();
628 super
= class->super_class
;
630 if (super
!= 0 && (super
->dtable
== __objc_uninstalled_dtable
))
631 __objc_install_dispatch_table_for_class (super
);
633 /* Allocate dtable if necessary. */
636 objc_mutex_lock (__objc_runtime_mutex
);
637 class->dtable
= sarray_new (__objc_selector_max_index
, 0);
638 objc_mutex_unlock (__objc_runtime_mutex
);
641 class->dtable
= sarray_lazy_copy (super
->dtable
);
643 __objc_install_methods_in_dtable (class, class->methods
);
647 __objc_update_dispatch_table_for_class (Class
class)
652 /* Not yet installed -- skip it. */
653 if (class->dtable
== __objc_uninstalled_dtable
)
656 objc_mutex_lock (__objc_runtime_mutex
);
659 __objc_install_premature_dtable (class); /* someone might require it... */
660 sarray_free (arr
); /* release memory */
662 /* Could have been lazy... */
663 __objc_install_dispatch_table_for_class (class);
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
);
669 objc_mutex_unlock (__objc_runtime_mutex
);
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.
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. */
680 class_add_method_list (Class
class, struct objc_method_list
* list
)
682 /* Passing of a linked list is not allowed. Do multiple calls. */
683 assert (! list
->method_next
);
685 __objc_register_selectors_from_list(list
);
687 /* Add the methods to the class's method list. */
688 list
->method_next
= class->methods
;
689 class->methods
= list
;
691 /* Update the dispatch table of class. */
692 __objc_update_dispatch_table_for_class (class);
696 class_get_instance_method (Class
class, SEL op
)
698 return search_for_method_in_hierarchy (class, op
);
702 class_get_class_method (MetaClass
class, SEL op
)
704 return search_for_method_in_hierarchy (class, op
);
708 class_getInstanceMethod (Class class_
, SEL selector
)
710 struct objc_method
*m
;
712 if (class_
== Nil
|| selector
== NULL
)
715 m
= search_for_method_in_hierarchy (class_
, selector
);
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
);
728 class_getClassMethod (Class class_
, SEL selector
)
730 struct objc_method
*m
;
732 if (class_
== Nil
|| selector
== NULL
)
735 m
= search_for_method_in_hierarchy (class_
->class_pointer
,
740 /* Try going through +resolveClassMethod:, and do the search again
742 if (__objc_resolve_class_method (class_
, selector
))
743 return search_for_method_in_hierarchy (class_
->class_pointer
,
750 class_addMethod (Class class_
, SEL selector
, IMP implementation
,
751 const char *method_types
)
753 struct objc_method_list
*method_list
;
754 struct objc_method
*method
;
755 const char *method_name
;
757 if (class_
== Nil
|| selector
== NULL
|| implementation
== NULL
758 || method_types
== NULL
|| (strcmp (method_types
, "") == 0))
761 method_name
= sel_getName (selector
);
762 if (method_name
== NULL
)
765 method_list
= (struct objc_method_list
*)objc_calloc (1, sizeof (struct objc_method_list
));
766 method_list
->method_count
= 1;
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
);
772 method
->method_types
= objc_malloc (strlen (method_types
) + 1);
773 strcpy ((char *)method
->method_types
, method_types
);
775 method
->method_imp
= implementation
;
777 if (CLS_IS_IN_CONSTRUCTION (class_
))
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
782 method_list
->method_next
= class_
->methods
;
783 class_
->methods
= method_list
;
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
);
796 /* Temporarily, until we include objc/runtime.h. */
798 method_setImplementation (struct objc_method
* method
, IMP implementation
);
801 class_replaceMethod (Class class_
, SEL selector
, IMP implementation
,
802 const char *method_types
)
804 struct objc_method
* method
;
806 if (class_
== Nil
|| selector
== NULL
|| implementation
== NULL
807 || method_types
== NULL
)
810 method
= search_for_method_in_hierarchy (class_
, selector
);
814 return method_setImplementation (method
, implementation
);
818 class_addMethod (class_
, selector
, implementation
, method_types
);
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
)
829 struct objc_method
* method
= NULL
;
832 if (! sel_is_mapped (sel
))
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
);
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. */
849 search_for_method_in_list (struct objc_method_list
* list
, SEL op
)
851 struct objc_method_list
* method_list
= list
;
853 if (! sel_is_mapped (op
))
856 /* If not found then we'll search the list. */
861 /* Search the method list. */
862 for (i
= 0; i
< method_list
->method_count
; ++i
)
864 struct objc_method
* method
= &method_list
->method_list
[i
];
866 if (method
->method_name
)
867 if (method
->method_name
->sel_id
== op
->sel_id
)
871 /* The method wasn't found. Follow the link to the next list of
873 method_list
= method_list
->method_next
;
879 static retval_t
__objc_forward (id object
, SEL sel
, arglist_t args
);
881 /* Forwarding pointers/integers through the normal registers. */
883 __objc_word_forward (id rcv
, SEL op
, ...)
887 args
= __builtin_apply_args ();
888 res
= __objc_forward (rcv
, op
, args
);
890 __builtin_return (res
);
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. */
901 __objc_double_forward (id rcv
, SEL op
, ...)
905 args
= __builtin_apply_args ();
906 res
= __objc_forward (rcv
, op
, args
);
907 __builtin_return (res
);
910 #if INVISIBLE_STRUCT_RETURN
915 __objc_block_forward (id rcv
, SEL op
, ...)
919 args
= __builtin_apply_args ();
920 res
= __objc_forward (rcv
, op
, args
);
922 __builtin_return (res
);
924 #if INVISIBLE_STRUCT_RETURN
925 return (__big
) {{0, 0, 0, 0, 0, 0, 0, 0}};
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
936 __objc_forward (id object
, SEL sel
, arglist_t args
)
939 static SEL frwd_sel
= 0; /* !T:SAFE2 */
942 /* First try if the object understands forward::. */
944 frwd_sel
= sel_get_any_uid ("forward::");
946 if (__objc_responds_to (object
, frwd_sel
))
948 imp
= get_imp (object
->class_pointer
, frwd_sel
);
949 return (*imp
) (object
, frwd_sel
, sel
, args
);
952 /* If the object recognizes the doesNotRecognize: method then we're
954 err_sel
= sel_get_any_uid ("doesNotRecognize:");
955 if (__objc_responds_to (object
, err_sel
))
957 imp
= get_imp (object
->class_pointer
, err_sel
);
958 return (*imp
) (object
, err_sel
, sel
);
961 /* The object doesn't recognize the method. Check for responding to
962 error:. If it does then sent it. */
964 char msg
[256 + strlen ((const char *) sel_getName (sel
))
965 + strlen ((const char *) object
->class_pointer
->name
)];
967 sprintf (msg
, "(%s) %s does not recognize %s",
968 (CLS_ISMETA (object
->class_pointer
)
971 object
->class_pointer
->name
, sel_getName (sel
));
973 /* TODO: support for error: is surely deprecated ? */
974 err_sel
= sel_get_any_uid ("error:");
975 if (__objc_responds_to (object
, err_sel
))
977 imp
= get_imp (object
->class_pointer
, err_sel
);
978 return (*imp
) (object
, sel_get_any_uid ("error:"), msg
);
981 /* The object doesn't respond to doesNotRecognize: or error:;
982 Therefore, a default action is taken. */
983 _objc_abort ("%s\n", msg
);
990 __objc_print_dtable_stats ()
994 objc_mutex_lock (__objc_runtime_mutex
);
997 printf ("memory usage: (%s)\n", "2-level sparse arrays");
999 printf ("memory usage: (%s)\n", "3-level sparse arrays");
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
);
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");
1016 objc_mutex_unlock (__objc_runtime_mutex
);
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. */
1023 objc_get_uninstalled_dtable (void)
1025 return __objc_uninstalled_dtable
;