]> git.ipfire.org Git - thirdparty/gcc.git/blame - libobjc/sendmsg.c
In libobjc/: 2010-12-11 Nicola Pero <nicola.pero@meta-innovation.com>
[thirdparty/gcc.git] / libobjc / sendmsg.c
CommitLineData
88e17b57 1/* GNU Objective C Runtime message lookup
0fc39d8a 2 Copyright (C) 1993, 1995, 1996, 1997, 1998,
ad9eef11 3 2001, 2002, 2004, 2009, 2010 Free Software Foundation, Inc.
88e17b57
BE
4 Contributed by Kresten Krab Thorup
5
38709cad 6This file is part of GCC.
88e17b57 7
38709cad 8GCC is free software; you can redistribute it and/or modify it under the
88e17b57 9terms of the GNU General Public License as published by the Free Software
748086b7 10Foundation; either version 3, or (at your option) any later version.
88e17b57 11
38709cad 12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
88e17b57
BE
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15details.
16
748086b7
JJ
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24<http://www.gnu.org/licenses/>. */
88e17b57 25
88e17b57 26
4977bab6 27/* FIXME: This file has no business including tm.h. */
435317e2
AP
28/* FIXME: This should be using libffi instead of __builtin_apply
29 and friends. */
4977bab6 30
6dead247 31#include "objc-private/common.h"
7b869986 32#include "objc-private/error.h"
bce1b489 33#include "tconfig.h"
4977bab6
ZW
34#include "coretypes.h"
35#include "tm.h"
a19fac96
NP
36#include "objc/objc-api.h"
37#include "objc/thr.h"
a19fac96 38#include "objc-private/runtime.h"
5d3b14bd 39#include "objc-private/sarray.h"
348a3445 40#include "objc/encoding.h"
88e17b57 41#include "runtime-info.h"
5d3b14bd 42#include <assert.h> /* For assert */
5be9cdc1 43#include <string.h> /* For strlen */
88e17b57 44
6c5c7efd
NP
45/* Temporarily while we include objc/objc-api.h instead of objc-private/module-abi-8.h. */
46#define _CLS_IN_CONSTRUCTION 0x10L
47#define CLS_IS_IN_CONSTRUCTION(cls) __CLS_ISINFO(cls, _CLS_IN_CONSTRUCTION)
48
435317e2 49/* This is how we hack STRUCT_VALUE to be 1 or 0. */
88e17b57
BE
50#define gen_rtx(args...) 1
51#define gen_rtx_MEM(args...) 1
68b61df9 52#define gen_rtx_REG(args...) 1
c24aadf3
KT
53/* Alread defined in gcc/coretypes.h. So prevent double definition warning. */
54#undef rtx
88e17b57
BE
55#define rtx int
56
40165636 57#if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
88e17b57
BE
58#define INVISIBLE_STRUCT_RETURN 1
59#else
60#define INVISIBLE_STRUCT_RETURN 0
61#endif
62
63/* The uninstalled dispatch table */
40165636 64struct sarray *__objc_uninstalled_dtable = 0; /* !T:MUTEX */
88e17b57 65
80ae8e8a
AR
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
71 * used.
72 */
40165636 73IMP (*__objc_msg_forward) (SEL) = NULL;
80ae8e8a 74IMP (*__objc_msg_forward2) (id, SEL) = NULL;
68b61df9 75
88e17b57 76/* Send +initialize to class */
40165636 77static void __objc_send_initialize (Class);
88e17b57
BE
78
79static void __objc_install_dispatch_table_for_class (Class);
80
81/* Forward declare some functions */
40165636 82static void __objc_init_install_dtable (id, SEL);
88e17b57
BE
83
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.
80ae8e8a 88 __objc_word_forward for pointers or types that fit in registers. */
40165636
RB
89static double __objc_double_forward (id, SEL, ...);
90static id __objc_word_forward (id, SEL, ...);
88e17b57
BE
91typedef struct { id many[8]; } __big;
92#if INVISIBLE_STRUCT_RETURN
93static __big
94#else
95static id
96#endif
40165636 97__objc_block_forward (id, SEL, ...);
ad9eef11
NP
98static struct objc_method * search_for_method_in_hierarchy (Class class, SEL sel);
99struct objc_method * search_for_method_in_list (struct objc_method_list * list, SEL op);
faaa30fe 100id nil_method (id, SEL);
40165636
RB
101
102/* Given a selector, return the proper forwarding implementation. */
435317e2 103inline
40165636 104IMP
80ae8e8a 105__objc_get_forward_imp (id rcv, SEL sel)
40165636 106{
bd8d449d 107 /* If a custom forwarding hook was registered, try getting a forwarding
80ae8e8a
AR
108 function from it. There are two forward routine hooks, one that
109 takes the receiver as an argument and one that does not. */
8972bcd8
AR
110 if (__objc_msg_forward2)
111 {
112 IMP result;
113 if ((result = __objc_msg_forward2 (rcv, sel)) != NULL)
114 return result;
115 }
40165636
RB
116 if (__objc_msg_forward)
117 {
118 IMP result;
bd8d449d 119 if ((result = __objc_msg_forward (sel)) != NULL)
40165636
RB
120 return result;
121 }
bd8d449d
NP
122
123 /* In all other cases, use the default forwarding functions built using
80ae8e8a 124 __builtin_apply and friends. */
40165636
RB
125 {
126 const char *t = sel->sel_types;
127
128 if (t && (*t == '[' || *t == '(' || *t == '{')
129#ifdef OBJC_MAX_STRUCT_BY_VALUE
130 && objc_sizeof_type (t) > OBJC_MAX_STRUCT_BY_VALUE
131#endif
132 )
133 return (IMP)__objc_block_forward;
134 else if (t && (*t == 'f' || *t == 'd'))
135 return (IMP)__objc_double_forward;
136 else
137 return (IMP)__objc_word_forward;
138 }
139}
88e17b57 140
e97cfd97
NP
141/* Selectors for +resolveClassMethod: and +resolveInstanceMethod:.
142 These are set up at startup. */
143static SEL selector_resolveClassMethod = NULL;
144static SEL selector_resolveInstanceMethod = NULL;
145
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.
151
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. */
156static inline
157IMP
158__objc_resolve_class_method (Class class, SEL sel)
159{
160 /* We need to lookup +resolveClassMethod:. */
161 BOOL (*resolveMethodIMP) (id, SEL, SEL);
162
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);
170
171 if (resolveMethodIMP && resolveMethodIMP ((id)class, selector_resolveClassMethod, sel))
172 {
173 /* +resolveClassMethod: returned YES. Look the method up again.
174 We already know the dtable is installed. */
175
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);
180 }
181
182 return NULL;
183}
184
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.
190
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. */
195static inline
196IMP
197__objc_resolve_instance_method (Class class, SEL sel)
198{
199 /* We need to lookup +resolveInstanceMethod:. */
200 BOOL (*resolveMethodIMP) (id, SEL, SEL);
201
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)
207 {
208 /* Try again after installing the dtable. */
209 if (class->class_pointer->dtable == __objc_uninstalled_dtable)
210 {
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);
215 }
216 resolveMethodIMP = sarray_get_safe (class->class_pointer->dtable,
217 (size_t) selector_resolveInstanceMethod->sel_id);
218 }
219
220 if (resolveMethodIMP && resolveMethodIMP ((id)class, selector_resolveInstanceMethod, sel))
221 {
222 /* +resolveInstanceMethod: returned YES. Look the method up
223 again. We already know the dtable is installed. */
224
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);
229 }
230
231 return NULL;
232}
233
234/* Temporary definition until we include objc/runtime.h. */
235objc_EXPORT Class objc_lookupClass (const char *name);
236
88e17b57 237/* Given a class and selector, return the selector's implementation. */
bd74d88b 238inline
88e17b57
BE
239IMP
240get_imp (Class class, SEL sel)
241{
c19f8e35
NP
242 /* In a vanilla implementation we would first check if the dispatch
243 table is installed. Here instead, to get more speed in the
244 standard case (that the dispatch table is installed) we first try
245 to get the imp using brute force. Only if that fails, we do what
246 we should have been doing from the very beginning, that is, check
247 if the dispatch table needs to be installed, install it if it's
248 not installed, and retrieve the imp from the table if it's
249 installed. */
40165636 250 void *res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
88e17b57
BE
251 if (res == 0)
252 {
253 /* Not a valid method */
40165636 254 if (class->dtable == __objc_uninstalled_dtable)
88e17b57
BE
255 {
256 /* The dispatch table needs to be installed. */
40165636 257 objc_mutex_lock (__objc_runtime_mutex);
c19f8e35
NP
258
259 /* Double-checked locking pattern: Check
260 __objc_uninstalled_dtable again in case another thread
261 installed the dtable while we were waiting for the lock
262 to be released. */
263 if (class->dtable == __objc_uninstalled_dtable)
264 {
265 __objc_install_dispatch_table_for_class (class);
266 }
267
40165636 268 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
269 /* Call ourselves with the installed dispatch table
270 and get the real method */
40165636 271 res = get_imp (class, sel);
88e17b57
BE
272 }
273 else
274 {
c19f8e35
NP
275 /* The dispatch table has been installed. */
276
277 /* Get the method from the dispatch table (we try to get it
278 again in case another thread has installed the dtable just
279 after we invoked sarray_get_safe, but before we checked
280 class->dtable == __objc_uninstalled_dtable).
281 */
282 res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
283 if (res == 0)
284 {
285 /* The dispatch table has been installed, and the method
286 is not in the dispatch table. So the method just
e97cfd97
NP
287 doesn't exist for the class. */
288
289 /* Try going through the +resolveClassMethod: or
290 +resolveInstanceMethod: process. */
291 if (CLS_ISMETA (class))
292 {
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);
299 if (realClass)
300 res = __objc_resolve_class_method (realClass, sel);
301 }
302 else
303 res = __objc_resolve_instance_method (class, sel);
304
305 if (res == 0)
306 {
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);
315 }
c19f8e35 316 }
88e17b57
BE
317 }
318 }
319 return res;
320}
321
ad9eef11
NP
322/* The new name of get_imp(). */
323IMP
324class_getMethodImplementation (Class class_, SEL selector)
325{
326 if (class_ == Nil || selector == NULL)
327 return NULL;
328
329 /* get_imp is inlined, so we're good. */
330 return get_imp (class_, selector);
331}
332
cf3822f1
NP
333/* Given a method, return its implementation. */
334IMP
ad9eef11 335method_get_imp (struct objc_method * method)
cf3822f1 336{
ad9eef11 337 return (method != (struct objc_method *)0) ? method->method_imp : (IMP)0;
cf3822f1
NP
338}
339
88e17b57
BE
340/* Query if an object can respond to a selector, returns YES if the
341object implements the selector otherwise NO. Does not check if the
342method can be forwarded. */
435317e2 343inline
88e17b57
BE
344BOOL
345__objc_responds_to (id object, SEL sel)
346{
40165636 347 void *res;
88e17b57
BE
348
349 /* Install dispatch table if need be */
350 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
351 {
40165636 352 objc_mutex_lock (__objc_runtime_mutex);
c19f8e35
NP
353 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
354 {
355 __objc_install_dispatch_table_for_class (object->class_pointer);
356 }
40165636 357 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
358 }
359
360 /* Get the method from the dispatch table */
361 res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
362 return (res != 0);
363}
364
ad9eef11
NP
365BOOL
366class_respondsToSelector (Class class_, SEL selector)
367{
368 void *res;
369
370 if (class_ == Nil || selector == NULL)
371 return NO;
372
373 /* Install dispatch table if need be */
374 if (class_->dtable == __objc_uninstalled_dtable)
375 {
376 objc_mutex_lock (__objc_runtime_mutex);
377 if (class_->dtable == __objc_uninstalled_dtable)
378 {
379 __objc_install_dispatch_table_for_class (class_);
380 }
381 objc_mutex_unlock (__objc_runtime_mutex);
382 }
383
384 /* Get the method from the dispatch table */
385 res = sarray_get_safe (class_->dtable, (size_t) selector->sel_id);
386 return (res != 0);
387}
388
88e17b57
BE
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. */
7116b6ea 392
88e17b57 393IMP
40165636 394objc_msg_lookup (id receiver, SEL op)
88e17b57
BE
395{
396 IMP result;
40165636 397 if (receiver)
88e17b57
BE
398 {
399 result = sarray_get_safe (receiver->class_pointer->dtable,
400 (sidx)op->sel_id);
401 if (result == 0)
402 {
403 /* Not a valid method */
40165636 404 if (receiver->class_pointer->dtable == __objc_uninstalled_dtable)
88e17b57
BE
405 {
406 /* The dispatch table needs to be installed.
407 This happens on the very first method call to the class. */
40165636 408 __objc_init_install_dtable (receiver, op);
88e17b57
BE
409
410 /* Get real method for this in newly installed dtable */
40165636 411 result = get_imp (receiver->class_pointer, op);
88e17b57
BE
412 }
413 else
414 {
c19f8e35
NP
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 */
420 result = sarray_get_safe (receiver->class_pointer->dtable,
421 (sidx)op->sel_id);
422 if (result == 0)
423 {
e97cfd97
NP
424 /* Try going through the +resolveClassMethod: or
425 +resolveInstanceMethod: process. */
426 if (CLS_ISMETA (receiver->class_pointer))
427 result = __objc_resolve_class_method ((Class)receiver, op);
428 else
429 result = __objc_resolve_instance_method (receiver->class_pointer,
430 op);
431
432 if (result == 0)
433 {
434 /* If the method still just doesn't exist for
435 the class, attempt to forward the method. */
436 result = __objc_get_forward_imp (receiver, op);
437 }
c19f8e35 438 }
88e17b57
BE
439 }
440 }
441 return result;
442 }
443 else
faaa30fe 444 return (IMP)nil_method;
88e17b57
BE
445}
446
447IMP
448objc_msg_lookup_super (Super_t super, SEL sel)
449{
450 if (super->self)
451 return get_imp (super->class, sel);
452 else
faaa30fe 453 return (IMP)nil_method;
88e17b57
BE
454}
455
40165636 456int method_get_sizeof_arguments (Method *);
88e17b57
BE
457
458retval_t
40165636 459objc_msg_sendv (id object, SEL op, arglist_t arg_frame)
88e17b57 460{
40165636 461 Method *m = class_get_instance_method (object->class_pointer, op);
88e17b57 462 const char *type;
40165636
RB
463 *((id *) method_get_first_argument (m, arg_frame, &type)) = object;
464 *((SEL *) method_get_next_argument (arg_frame, &type)) = op;
465 return __builtin_apply ((apply_t) m->method_imp,
466 arg_frame,
467 method_get_sizeof_arguments (m));
88e17b57
BE
468}
469
470void
40165636 471__objc_init_dispatch_tables ()
88e17b57 472{
40165636 473 __objc_uninstalled_dtable = sarray_new (200, 0);
e97cfd97
NP
474
475 /* TODO: It would be cool to register typed selectors here. */
476 selector_resolveClassMethod = sel_register_name ("resolveClassMethod:");
477 selector_resolveInstanceMethod =sel_register_name ("resolveInstanceMethod:");
88e17b57
BE
478}
479
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. */
483static void
40165636 484__objc_init_install_dtable (id receiver, SEL op __attribute__ ((__unused__)))
88e17b57 485{
c19f8e35
NP
486 objc_mutex_lock (__objc_runtime_mutex);
487
88e17b57
BE
488 /* This may happen, if the programmer has taken the address of a
489 method before the dtable was initialized... too bad for him! */
40165636 490 if (receiver->class_pointer->dtable != __objc_uninstalled_dtable)
c19f8e35
NP
491 {
492 objc_mutex_unlock (__objc_runtime_mutex);
493 return;
494 }
495
40165636 496 if (CLS_ISCLASS (receiver->class_pointer))
88e17b57
BE
497 {
498 /* receiver is an ordinary object */
40165636 499 assert (CLS_ISCLASS (receiver->class_pointer));
88e17b57
BE
500
501 /* install instance methods table */
502 __objc_install_dispatch_table_for_class (receiver->class_pointer);
503
504 /* call +initialize -- this will in turn install the factory
505 dispatch table if not already done :-) */
40165636 506 __objc_send_initialize (receiver->class_pointer);
88e17b57
BE
507 }
508 else
509 {
510 /* receiver is a class object */
40165636
RB
511 assert (CLS_ISCLASS ((Class)receiver));
512 assert (CLS_ISMETA (receiver->class_pointer));
88e17b57
BE
513
514 /* Install real dtable for factory methods */
515 __objc_install_dispatch_table_for_class (receiver->class_pointer);
516
40165636 517 __objc_send_initialize ((Class)receiver);
88e17b57 518 }
40165636 519 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
520}
521
522/* Install dummy table for class which causes the first message to
523 that class (or instances hereof) to be initialized properly */
524void
40165636 525__objc_install_premature_dtable (Class class)
88e17b57 526{
40165636 527 assert (__objc_uninstalled_dtable);
88e17b57
BE
528 class->dtable = __objc_uninstalled_dtable;
529}
530
531/* Send +initialize to class if not already done */
532static void
40165636 533__objc_send_initialize (Class class)
88e17b57
BE
534{
535 /* This *must* be a class object */
40165636
RB
536 assert (CLS_ISCLASS (class));
537 assert (! CLS_ISMETA (class));
88e17b57 538
40165636 539 if (! CLS_ISINITIALIZED (class))
88e17b57 540 {
40165636
RB
541 CLS_SETINITIALIZED (class);
542 CLS_SETINITIALIZED (class->class_pointer);
88e17b57
BE
543
544 /* Create the garbage collector type memory description */
545 __objc_generate_gc_type_description (class);
546
40165636
RB
547 if (class->super_class)
548 __objc_send_initialize (class->super_class);
88e17b57
BE
549
550 {
551 SEL op = sel_register_name ("initialize");
552 IMP imp = 0;
ad9eef11 553 struct objc_method_list * method_list = class->class_pointer->methods;
88e17b57
BE
554
555 while (method_list) {
556 int i;
ad9eef11 557 struct objc_method * method;
88e17b57 558
40165636 559 for (i = 0; i < method_list->method_count; i++) {
88e17b57
BE
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;
564 break;
565 }
566 }
567
568 if (imp)
569 break;
570
571 method_list = method_list->method_next;
572
573 }
574 if (imp)
40165636 575 (*imp) ((id) class, op);
88e17b57
BE
576
577 }
578 }
579}
580
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. */
587static void
ad9eef11 588__objc_install_methods_in_dtable (Class class, struct objc_method_list * method_list)
88e17b57
BE
589{
590 int i;
591
40165636 592 if (! method_list)
88e17b57
BE
593 return;
594
595 if (method_list->method_next)
596 __objc_install_methods_in_dtable (class, method_list->method_next);
597
598 for (i = 0; i < method_list->method_count; i++)
599 {
ad9eef11 600 struct objc_method * method = &(method_list->method_list[i]);
88e17b57
BE
601 sarray_at_put_safe (class->dtable,
602 (sidx) method->method_name->sel_id,
603 method->method_imp);
604 }
605}
606
607/* Assumes that __objc_runtime_mutex is locked down. */
608static void
609__objc_install_dispatch_table_for_class (Class class)
610{
611 Class super;
612
613 /* If the class has not yet had its class links resolved, we must
614 re-compute all class links */
40165636
RB
615 if (! CLS_ISRESOLV (class))
616 __objc_resolve_class_links ();
88e17b57
BE
617
618 super = class->super_class;
619
620 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
621 __objc_install_dispatch_table_for_class (super);
622
623 /* Allocate dtable if necessary */
624 if (super == 0)
625 {
40165636 626 objc_mutex_lock (__objc_runtime_mutex);
88e17b57 627 class->dtable = sarray_new (__objc_selector_max_index, 0);
40165636 628 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
629 }
630 else
631 class->dtable = sarray_lazy_copy (super->dtable);
632
633 __objc_install_methods_in_dtable (class, class->methods);
634}
635
636void
637__objc_update_dispatch_table_for_class (Class class)
638{
639 Class next;
640 struct sarray *arr;
641
642 /* not yet installed -- skip it */
643 if (class->dtable == __objc_uninstalled_dtable)
644 return;
645
40165636 646 objc_mutex_lock (__objc_runtime_mutex);
88e17b57
BE
647
648 arr = class->dtable;
649 __objc_install_premature_dtable (class); /* someone might require it... */
650 sarray_free (arr); /* release memory */
651
652 /* could have been lazy... */
653 __objc_install_dispatch_table_for_class (class);
654
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);
658
40165636 659 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
660}
661
662
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.
666
667 This one is only called for categories. Class objects have their
668 methods installed right away, and their selectors are made into
435317e2 669 SEL's by the function __objc_register_selectors_from_class. */
88e17b57 670void
ad9eef11 671class_add_method_list (Class class, struct objc_method_list * list)
88e17b57 672{
88e17b57 673 /* Passing of a linked list is not allowed. Do multiple calls. */
40165636 674 assert (! list->method_next);
88e17b57 675
435317e2 676 __objc_register_selectors_from_list(list);
88e17b57
BE
677
678 /* Add the methods to the class's method list. */
679 list->method_next = class->methods;
680 class->methods = list;
681
682 /* Update the dispatch table of class */
683 __objc_update_dispatch_table_for_class (class);
684}
685
ad9eef11 686struct objc_method *
40165636 687class_get_instance_method (Class class, SEL op)
88e17b57 688{
40165636 689 return search_for_method_in_hierarchy (class, op);
88e17b57
BE
690}
691
ad9eef11 692struct objc_method *
40165636 693class_get_class_method (MetaClass class, SEL op)
88e17b57 694{
40165636 695 return search_for_method_in_hierarchy (class, op);
88e17b57
BE
696}
697
ad9eef11
NP
698struct objc_method *
699class_getInstanceMethod (Class class_, SEL selector)
700{
e97cfd97
NP
701 struct objc_method *m;
702
ad9eef11
NP
703 if (class_ == Nil || selector == NULL)
704 return NULL;
705
e97cfd97
NP
706 m = search_for_method_in_hierarchy (class_, selector);
707 if (m)
708 return m;
709
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);
714
715 return NULL;
ad9eef11
NP
716}
717
718struct objc_method *
719class_getClassMethod (Class class_, SEL selector)
720{
e97cfd97
NP
721 struct objc_method *m;
722
ad9eef11
NP
723 if (class_ == Nil || selector == NULL)
724 return NULL;
725
e97cfd97
NP
726 m = search_for_method_in_hierarchy (class_->class_pointer,
727 selector);
728 if (m)
729 return m;
730
731 /* Try going through +resolveClassMethod:, and do the search again
732 if successful. */
733 if (__objc_resolve_class_method (class_, selector))
734 return search_for_method_in_hierarchy (class_->class_pointer,
735 selector);
736
737 return NULL;
ad9eef11 738}
88e17b57 739
6c5c7efd
NP
740BOOL
741class_addMethod (Class class_, SEL selector, IMP implementation,
742 const char *method_types)
743{
744 struct objc_method_list *method_list;
745 struct objc_method *method;
746 const char *method_name;
747
748 if (class_ == Nil || selector == NULL || implementation == NULL
749 || method_types == NULL || (strcmp (method_types, "") == 0))
750 return NO;
751
752 method_name = sel_get_name (selector);
753 if (method_name == NULL)
754 return NO;
755
756 method_list = (struct objc_method_list *)objc_calloc (1, sizeof (struct objc_method_list));
757 method_list->method_count = 1;
758
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);
762
763 method->method_types = objc_malloc (strlen (method_types) + 1);
764 strcpy ((char *)method->method_types, method_types);
765
766 method->method_imp = implementation;
767
768 if (CLS_IS_IN_CONSTRUCTION (class_))
769 {
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
772 (if ever). */
773 method_list->method_next = class_->methods;
774 class_->methods = method_list;
775 }
776 else
777 {
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);
782 }
783
784 return YES;
785}
786
787/* Temporarily, until we include objc/runtime.h. */
788extern IMP
789method_setImplementation (struct objc_method * method, IMP implementation);
790
791IMP
792class_replaceMethod (Class class_, SEL selector, IMP implementation,
793 const char *method_types)
794{
795 struct objc_method * method;
796
797 if (class_ == Nil || selector == NULL || implementation == NULL
798 || method_types == NULL)
799 return NULL;
800
801 method = search_for_method_in_hierarchy (class_, selector);
802
803 if (method)
804 {
805 return method_setImplementation (method, implementation);
806 }
807 else
808 {
809 class_addMethod (class_, selector, implementation, method_types);
810 return NULL;
811 }
812}
813
88e17b57
BE
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
816 otherwise. */
ad9eef11 817static struct objc_method *
88e17b57
BE
818search_for_method_in_hierarchy (Class cls, SEL sel)
819{
ad9eef11 820 struct objc_method * method = NULL;
88e17b57
BE
821 Class class;
822
823 if (! sel_is_mapped (sel))
824 return NULL;
825
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);
830
831 return method;
832}
833
834
835
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. */
ad9eef11
NP
839struct objc_method *
840search_for_method_in_list (struct objc_method_list * list, SEL op)
88e17b57 841{
ad9eef11 842 struct objc_method_list * method_list = list;
88e17b57
BE
843
844 if (! sel_is_mapped (op))
845 return NULL;
846
847 /* If not found then we'll search the list. */
848 while (method_list)
849 {
850 int i;
851
852 /* Search the method list. */
853 for (i = 0; i < method_list->method_count; ++i)
854 {
ad9eef11 855 struct objc_method * method = &method_list->method_list[i];
88e17b57
BE
856
857 if (method->method_name)
858 if (method->method_name->sel_id == op->sel_id)
859 return method;
860 }
861
862 /* The method wasn't found. Follow the link to the next list of
863 methods. */
864 method_list = method_list->method_next;
865 }
866
867 return NULL;
868}
869
870static retval_t __objc_forward (id object, SEL sel, arglist_t args);
871
872/* Forwarding pointers/integers through the normal registers */
873static id
874__objc_word_forward (id rcv, SEL op, ...)
875{
876 void *args, *res;
877
878 args = __builtin_apply_args ();
879 res = __objc_forward (rcv, op, args);
880 if (res)
881 __builtin_return (res);
882 else
883 return res;
884}
885
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 */
891static double
892__objc_double_forward (id rcv, SEL op, ...)
893{
894 void *args, *res;
895
896 args = __builtin_apply_args ();
897 res = __objc_forward (rcv, op, args);
898 __builtin_return (res);
899}
900
901#if INVISIBLE_STRUCT_RETURN
902static __big
903#else
904static id
905#endif
906__objc_block_forward (id rcv, SEL op, ...)
907{
908 void *args, *res;
909
910 args = __builtin_apply_args ();
911 res = __objc_forward (rcv, op, args);
912 if (res)
913 __builtin_return (res);
914 else
915#if INVISIBLE_STRUCT_RETURN
916 return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
917#else
918 return nil;
919#endif
920}
921
922
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. */
925static retval_t
926__objc_forward (id object, SEL sel, arglist_t args)
927{
928 IMP imp;
929 static SEL frwd_sel = 0; /* !T:SAFE2 */
930 SEL err_sel;
931
932 /* first try if the object understands forward:: */
40165636
RB
933 if (! frwd_sel)
934 frwd_sel = sel_get_any_uid ("forward::");
88e17b57
BE
935
936 if (__objc_responds_to (object, frwd_sel))
937 {
40165636
RB
938 imp = get_imp (object->class_pointer, frwd_sel);
939 return (*imp) (object, frwd_sel, sel, args);
88e17b57
BE
940 }
941
942 /* If the object recognizes the doesNotRecognize: method then we're going
943 to send it. */
944 err_sel = sel_get_any_uid ("doesNotRecognize:");
945 if (__objc_responds_to (object, err_sel))
946 {
947 imp = get_imp (object->class_pointer, err_sel);
948 return (*imp) (object, err_sel, sel);
949 }
950
951 /* The object doesn't recognize the method. Check for responding to
952 error:. If it does then sent it. */
953 {
40165636
RB
954 char msg[256 + strlen ((const char *) sel_get_name (sel))
955 + strlen ((const char *) object->class_pointer->name)];
88e17b57
BE
956
957 sprintf (msg, "(%s) %s does not recognize %s",
40165636 958 (CLS_ISMETA (object->class_pointer)
88e17b57
BE
959 ? "class"
960 : "instance" ),
961 object->class_pointer->name, sel_get_name (sel));
962
7b869986 963 /* TODO: support for error: is surely deprecated ? */
88e17b57
BE
964 err_sel = sel_get_any_uid ("error:");
965 if (__objc_responds_to (object, err_sel))
966 {
967 imp = get_imp (object->class_pointer, err_sel);
968 return (*imp) (object, sel_get_any_uid ("error:"), msg);
969 }
970
971 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
972 a default action is taken. */
7b869986 973 _objc_abort ("%s\n", msg);
88e17b57
BE
974
975 return 0;
976 }
977}
978
979void
40165636 980__objc_print_dtable_stats ()
88e17b57
BE
981{
982 int total = 0;
983
40165636 984 objc_mutex_lock (__objc_runtime_mutex);
88e17b57 985
88e17b57 986#ifdef OBJC_SPARSE2
40165636 987 printf ("memory usage: (%s)\n", "2-level sparse arrays");
88e17b57 988#else
40165636 989 printf ("memory usage: (%s)\n", "3-level sparse arrays");
88e17b57 990#endif
88e17b57 991
40165636 992 printf ("arrays: %d = %ld bytes\n", narrays,
b15b7ef8 993 (long) ((size_t) narrays * sizeof (struct sarray)));
40165636
RB
994 total += narrays * sizeof (struct sarray);
995 printf ("buckets: %d = %ld bytes\n", nbuckets,
b15b7ef8 996 (long) ((size_t) nbuckets * sizeof (struct sbucket)));
40165636
RB
997 total += nbuckets * sizeof (struct sbucket);
998
999 printf ("idxtables: %d = %ld bytes\n",
b15b7ef8 1000 idxsize, (long) ((size_t) idxsize * sizeof (void *)));
40165636
RB
1001 total += idxsize * sizeof (void *);
1002 printf ("-----------------------------------\n");
1003 printf ("total: %d bytes\n", total);
1004 printf ("===================================\n");
1005
1006 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
1007}
1008
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. */
7116b6ea 1012
40165636
RB
1013struct sarray *
1014objc_get_uninstalled_dtable ()
88e17b57
BE
1015{
1016 return __objc_uninstalled_dtable;
1017}