OSDN Git Service

Add -lisl to clooglibs.
[pf3gnuchains/gcc-fork.git] / libobjc / sendmsg.c
1 /* GNU Objective C Runtime message lookup 
2    Copyright (C) 1993, 1995, 1996, 1997, 1998,
3    2001, 2002, 2004, 2009, 2010 Free Software Foundation, Inc.
4    Contributed by Kresten Krab Thorup
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 3, or (at your option) any later version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15 details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 <http://www.gnu.org/licenses/>.  */
25
26 /* Uncommented the following line to enable debug logging.  Use this
27    only while debugging the runtime.  */
28 /* #define DEBUG 1 */
29
30 /* FIXME: This file has no business including tm.h.  */
31 /* FIXME: This should be using libffi instead of __builtin_apply
32    and friends.  */
33
34 #include "objc-private/common.h"
35 #include "objc-private/error.h"
36 #include "tconfig.h"
37 #include "coretypes.h"
38 #include "tm.h"
39 #include "objc/runtime.h"
40 #include "objc/message.h"          /* For objc_msg_lookup(), objc_msg_lookup_super().  */
41 #include "objc/thr.h"
42 #include "objc-private/module-abi-8.h"
43 #include "objc-private/runtime.h"
44 #include "objc-private/sarray.h"
45 #include "objc-private/selector.h" /* For sel_is_mapped() */
46 #include "runtime-info.h"
47 #include <assert.h> /* For assert */
48 #include <string.h> /* For strlen */
49
50 /* This is how we hack STRUCT_VALUE to be 1 or 0.   */
51 #define gen_rtx(args...) 1
52 #define gen_rtx_MEM(args...) 1
53 #define gen_rtx_REG(args...) 1
54 /* Already defined in gcc/coretypes.h. So prevent double definition warning.  */
55 #undef rtx
56 #define rtx int
57
58 #if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
59 #define INVISIBLE_STRUCT_RETURN 1
60 #else
61 #define INVISIBLE_STRUCT_RETURN 0
62 #endif
63
64 /* The uninstalled dispatch table.  */
65 struct sarray *__objc_uninstalled_dtable = 0;   /* !T:MUTEX */
66
67 /* Two hooks for method forwarding. If either is set, it is invoked to
68  * return a function that performs the real forwarding.  If both are
69  * set, the result of __objc_msg_forward2 will be preferred over that
70  * of __objc_msg_forward.  If both return NULL or are unset, the
71  * libgcc based functions (__builtin_apply and friends) are used.  */
72 IMP (*__objc_msg_forward) (SEL) = NULL;
73 IMP (*__objc_msg_forward2) (id, SEL) = NULL;
74
75 /* Send +initialize to class.  */
76 static void __objc_send_initialize (Class);
77
78 static void __objc_install_dispatch_table_for_class (Class);
79
80 /* Forward declare some functions.  */
81 static void __objc_init_install_dtable (id, SEL);
82
83 /* Various forwarding functions that are used based upon the
84    return type for the selector.
85    __objc_block_forward for structures.
86    __objc_double_forward for floats/doubles.
87    __objc_word_forward for pointers or types that fit in registers.  */
88 static double __objc_double_forward (id, SEL, ...);
89 static id __objc_word_forward (id, SEL, ...);
90 typedef struct { id many[8]; } __big;
91 #if INVISIBLE_STRUCT_RETURN 
92 static __big 
93 #else
94 static id
95 #endif
96 __objc_block_forward (id, SEL, ...);
97 static struct objc_method * search_for_method_in_hierarchy (Class class, SEL sel);
98 struct objc_method * search_for_method_in_list (struct objc_method_list * list, SEL op);
99 id nil_method (id, SEL);
100
101 /* Given a selector, return the proper forwarding implementation.  */
102 inline
103 IMP
104 __objc_get_forward_imp (id rcv, SEL sel)
105 {
106   /* If a custom forwarding hook was registered, try getting a
107      forwarding function from it. There are two forward routine hooks,
108      one that takes the receiver as an argument and one that does
109      not.  */
110   if (__objc_msg_forward2)
111     {
112       IMP result;
113       if ((result = __objc_msg_forward2 (rcv, sel)) != NULL)
114        return result;
115     }
116   if (__objc_msg_forward)
117     {
118       IMP result;
119       if ((result = __objc_msg_forward (sel)) != NULL) 
120         return result;
121     }
122
123   /* In all other cases, use the default forwarding functions built
124      using __builtin_apply and friends.  */
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 }
140
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;
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.  */
156 static inline
157 IMP
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.  */
195 static inline
196 IMP
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 /* Given a class and selector, return the selector's
235    implementation.  */
236 inline
237 IMP
238 get_imp (Class class, SEL sel)
239 {
240   /* In a vanilla implementation we would first check if the dispatch
241      table is installed.  Here instead, to get more speed in the
242      standard case (that the dispatch table is installed) we first try
243      to get the imp using brute force.  Only if that fails, we do what
244      we should have been doing from the very beginning, that is, check
245      if the dispatch table needs to be installed, install it if it's
246      not installed, and retrieve the imp from the table if it's
247      installed.  */
248   void *res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
249   if (res == 0)
250     {
251       /* Not a valid method.  */
252       if (class->dtable == __objc_uninstalled_dtable)
253         {
254           /* The dispatch table needs to be installed.  */
255           objc_mutex_lock (__objc_runtime_mutex);
256
257            /* Double-checked locking pattern: Check
258               __objc_uninstalled_dtable again in case another thread
259               installed the dtable while we were waiting for the lock
260               to be released.  */
261          if (class->dtable == __objc_uninstalled_dtable)
262            {
263              __objc_install_dispatch_table_for_class (class);
264            }
265
266           objc_mutex_unlock (__objc_runtime_mutex);
267           /* Call ourselves with the installed dispatch table and get
268              the real method.  */
269           res = get_imp (class, sel);
270         }
271       else
272         {
273           /* The dispatch table has been installed.  */
274
275          /* Get the method from the dispatch table (we try to get it
276             again in case another thread has installed the dtable just
277             after we invoked sarray_get_safe, but before we checked
278             class->dtable == __objc_uninstalled_dtable).  */
279           res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
280           if (res == 0)
281             {
282               /* The dispatch table has been installed, and the method
283                  is not in the dispatch table.  So the method just
284                  doesn't exist for the class.  */
285
286               /* Try going through the +resolveClassMethod: or
287                  +resolveInstanceMethod: process.  */
288               if (CLS_ISMETA (class))
289                 {
290                   /* We have the meta class, but we need to invoke the
291                      +resolveClassMethod: method on the class.  So, we
292                      need to obtain the class from the meta class,
293                      which we do using the fact that both the class
294                      and the meta-class have the same name.  */
295                   Class realClass = objc_lookUpClass (class->name);
296                   if (realClass)
297                     res = __objc_resolve_class_method (realClass, sel);
298                 }
299               else
300                 res = __objc_resolve_instance_method (class, sel);
301
302               if (res == 0)
303                 {
304                   /* If that fails, then return the forwarding
305                      implementation.  We don't know the receiver (only
306                      its class), so we have to pass 'nil' as the first
307                      argument.  Passing the class as first argument is
308                      wrong because the class is not the receiver; it
309                      can result in us calling a class method when we
310                      want an instance method of the same name.  */
311                   res = __objc_get_forward_imp (nil, sel);
312                 }
313             }
314         }
315     }
316   return res;
317 }
318
319 /* The new name of get_imp().  */
320 IMP
321 class_getMethodImplementation (Class class_, SEL selector)
322 {
323   if (class_ == Nil  ||  selector == NULL)
324     return NULL;
325
326   /* get_imp is inlined, so we're good.  */
327   return get_imp (class_, selector);
328 }
329
330 /* Given a method, return its implementation.  This has been replaced
331    by method_getImplementation() in the modern API.  */
332 IMP
333 method_get_imp (struct objc_method * method)
334 {
335   return (method != (struct objc_method *)0) ? method->method_imp : (IMP)0;
336 }
337
338 /* Query if an object can respond to a selector, returns YES if the
339    object implements the selector otherwise NO.  Does not check if the
340    method can be forwarded.  */
341 inline
342 BOOL
343 __objc_responds_to (id object, SEL sel)
344 {
345   void *res;
346
347   /* Install dispatch table if need be.  */
348   if (object->class_pointer->dtable == __objc_uninstalled_dtable)
349     {
350       objc_mutex_lock (__objc_runtime_mutex);
351       if (object->class_pointer->dtable == __objc_uninstalled_dtable)
352         {
353           __objc_install_dispatch_table_for_class (object->class_pointer);
354         }
355       objc_mutex_unlock (__objc_runtime_mutex);
356     }
357
358   /* Get the method from the dispatch table.  */
359   res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
360   return (res != 0);
361 }
362
363 BOOL
364 class_respondsToSelector (Class class_, SEL selector)
365 {
366   void *res;
367
368   if (class_ == Nil  ||  selector == NULL)
369     return NO;
370
371   /* Install dispatch table if need be.  */
372   if (class_->dtable == __objc_uninstalled_dtable)
373     {
374       objc_mutex_lock (__objc_runtime_mutex);
375       if (class_->dtable == __objc_uninstalled_dtable)
376         {
377           __objc_install_dispatch_table_for_class (class_);
378         }
379       objc_mutex_unlock (__objc_runtime_mutex);
380     }
381
382   /* Get the method from the dispatch table.  */
383   res = sarray_get_safe (class_->dtable, (size_t) selector->sel_id);
384   return (res != 0);
385 }
386
387 /* This is the lookup function.  All entries in the table are either a
388    valid method *or* zero.  If zero then either the dispatch table
389    needs to be installed or it doesn't exist and forwarding is
390    attempted.  */
391 IMP
392 objc_msg_lookup (id receiver, SEL op)
393 {
394   IMP result;
395   if (receiver)
396     {
397       result = sarray_get_safe (receiver->class_pointer->dtable, 
398                                 (sidx)op->sel_id);
399       if (result == 0)
400         {
401           /* Not a valid method.  */
402           if (receiver->class_pointer->dtable == __objc_uninstalled_dtable)
403             {
404               /* The dispatch table needs to be installed.  This
405                  happens on the very first method call to the
406                  class.  */
407               __objc_init_install_dtable (receiver, op);
408
409               /* Get real method for this in newly installed
410                  dtable.  */
411               result = get_imp (receiver->class_pointer, op);
412             }
413           else
414             {
415               /* The dispatch table has been installed.  Check again
416                  if the method exists (just in case the dispatch table
417                  has been installed by another thread after we did the
418                  previous check that the method exists).  */
419               result = sarray_get_safe (receiver->class_pointer->dtable,
420                                         (sidx)op->sel_id);
421               if (result == 0)
422                 {
423                   /* Try going through the +resolveClassMethod: or
424                      +resolveInstanceMethod: process.  */
425                   if (CLS_ISMETA (receiver->class_pointer))
426                     result = __objc_resolve_class_method ((Class)receiver, op);
427                   else
428                     result = __objc_resolve_instance_method (receiver->class_pointer,
429                                                              op);
430
431                   if (result == 0)
432                     {
433                       /* If the method still just doesn't exist for
434                          the class, attempt to forward the method.  */
435                       result = __objc_get_forward_imp (receiver, op);
436                     }
437                 }
438             }
439         }
440       return result;
441     }
442   else
443     return (IMP)nil_method;
444 }
445
446 IMP
447 objc_msg_lookup_super (struct objc_super *super, SEL sel)
448 {
449   if (super->self)
450     return get_imp (super->super_class, sel);
451   else
452     return (IMP)nil_method;
453 }
454
455 /* Temporarily defined here until objc_msg_sendv() goes away.  */
456 char *method_get_first_argument (struct objc_method *,
457                                  arglist_t argframe, 
458                                  const char **type);
459 char *method_get_next_argument (arglist_t argframe, 
460                                 const char **type);
461 int method_get_sizeof_arguments (struct objc_method *);
462
463 struct objc_method *
464 class_get_instance_method (Class class, SEL op);
465
466 retval_t
467 objc_msg_sendv (id object, SEL op, arglist_t arg_frame)
468 {
469   struct objc_method *m = class_get_instance_method (object->class_pointer, op);
470   const char *type;
471   *((id *) method_get_first_argument (m, arg_frame, &type)) = object;
472   *((SEL *) method_get_next_argument (arg_frame, &type)) = op;
473   return __builtin_apply ((apply_t) m->method_imp, 
474                           arg_frame,
475                           method_get_sizeof_arguments (m));
476 }
477
478 void
479 __objc_init_dispatch_tables ()
480 {
481   __objc_uninstalled_dtable = sarray_new (200, 0);
482
483   /* TODO: It would be cool to register typed selectors here.  */
484   selector_resolveClassMethod = sel_registerName ("resolveClassMethod:");
485   selector_resolveInstanceMethod  =sel_registerName ("resolveInstanceMethod:");
486 }
487
488 /* This function is called by objc_msg_lookup when the dispatch table
489    needs to be installed; thus it is called once for each class,
490    namely when the very first message is sent to it.  */
491 static void
492 __objc_init_install_dtable (id receiver, SEL op __attribute__ ((__unused__)))
493 {
494   objc_mutex_lock (__objc_runtime_mutex);
495   
496   /* This may happen, if the programmer has taken the address of a
497      method before the dtable was initialized... too bad for him!  */
498   if (receiver->class_pointer->dtable != __objc_uninstalled_dtable)
499     {
500       objc_mutex_unlock (__objc_runtime_mutex);
501       return;
502     }
503   
504   if (CLS_ISCLASS (receiver->class_pointer))
505     {
506       /* receiver is an ordinary object.  */
507       assert (CLS_ISCLASS (receiver->class_pointer));
508
509       /* Install instance methods table.  */
510       __objc_install_dispatch_table_for_class (receiver->class_pointer);
511
512       /* Call +initialize -- this will in turn install the factory
513          dispatch table if not already done. :-)  */
514       __objc_send_initialize (receiver->class_pointer);
515     }
516   else
517     {
518       /* receiver is a class object.  */
519       assert (CLS_ISCLASS ((Class)receiver));
520       assert (CLS_ISMETA (receiver->class_pointer));
521
522       /* Install real dtable for factory methods.  */
523       __objc_install_dispatch_table_for_class (receiver->class_pointer);
524
525       __objc_send_initialize ((Class)receiver);
526     }
527   objc_mutex_unlock (__objc_runtime_mutex);
528 }
529
530 /* Install dummy table for class which causes the first message to
531    that class (or instances hereof) to be initialized properly.  */
532 void
533 __objc_install_premature_dtable (Class class)
534 {
535   assert (__objc_uninstalled_dtable);
536   class->dtable = __objc_uninstalled_dtable;
537 }   
538
539 /* Send +initialize to class if not already done.  */
540 static void
541 __objc_send_initialize (Class class)
542 {
543   /* This *must* be a class object.  */
544   assert (CLS_ISCLASS (class));
545   assert (! CLS_ISMETA (class));
546
547   if (! CLS_ISINITIALIZED (class))
548     {
549       DEBUG_PRINTF ("+initialize: need to initialize class '%s'\n", class->name);
550       CLS_SETINITIALIZED (class);
551       CLS_SETINITIALIZED (class->class_pointer);
552
553       /* Create the garbage collector type memory description.  */
554       __objc_generate_gc_type_description (class);
555
556       if (class->super_class)
557         __objc_send_initialize (class->super_class);
558
559       {
560         SEL op = sel_registerName ("initialize");
561         IMP imp = 0;
562         struct objc_method_list * method_list = class->class_pointer->methods;
563         
564         while (method_list)
565           {
566             int i;
567             struct objc_method * method;
568             
569             for (i = 0; i < method_list->method_count; i++)
570               {
571                 method = &(method_list->method_list[i]);
572                 if (method->method_name
573                     && method->method_name->sel_id == op->sel_id)
574                   {
575                     imp = method->method_imp;
576                     break;
577                   }
578               }
579             
580             if (imp)
581               break;
582             
583             method_list = method_list->method_next;
584           }
585         if (imp)
586           {
587             DEBUG_PRINTF (" begin of [%s +initialize]\n", class->name);
588             (*imp) ((id) class, op);
589             DEBUG_PRINTF (" end of [%s +initialize]\n", class->name);
590           }
591 #ifdef DEBUG
592         else
593           {
594             DEBUG_PRINTF (" class '%s' has no +initialize method\n", class->name);          
595           }
596 #endif
597       }
598     }
599 }
600
601 /* Walk on the methods list of class and install the methods in the
602    reverse order of the lists.  Since methods added by categories are
603    before the methods of class in the methods list, this allows
604    categories to substitute methods declared in class.  However if
605    more than one category replaces the same method nothing is
606    guaranteed about what method will be used.  Assumes that
607    __objc_runtime_mutex is locked down.  */
608 static void
609 __objc_install_methods_in_dtable (Class class, struct objc_method_list * method_list)
610 {
611   int i;
612   
613   if (! method_list)
614     return;
615   
616   if (method_list->method_next)
617     __objc_install_methods_in_dtable (class, method_list->method_next);
618   
619   for (i = 0; i < method_list->method_count; i++)
620     {
621       struct objc_method * method = &(method_list->method_list[i]);
622       sarray_at_put_safe (class->dtable,
623                           (sidx) method->method_name->sel_id,
624                           method->method_imp);
625     }
626 }
627
628 /* Assumes that __objc_runtime_mutex is locked down.  */
629 static void
630 __objc_install_dispatch_table_for_class (Class class)
631 {
632   Class super;
633
634   /* If the class has not yet had its class links resolved, we must
635      re-compute all class links.  */
636   if (! CLS_ISRESOLV (class))
637     __objc_resolve_class_links ();
638
639   DEBUG_PRINTF ("__objc_install_dispatch_table_for_class (%s)\n", class->name);
640   
641   super = class->super_class;
642
643   if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
644     __objc_install_dispatch_table_for_class (super);
645
646   /* Allocate dtable if necessary.  */
647   if (super == 0)
648     {
649       objc_mutex_lock (__objc_runtime_mutex);
650       class->dtable = sarray_new (__objc_selector_max_index, 0);
651       objc_mutex_unlock (__objc_runtime_mutex);
652     }
653   else
654     class->dtable = sarray_lazy_copy (super->dtable);
655
656   __objc_install_methods_in_dtable (class, class->methods);
657 }
658
659 void
660 __objc_update_dispatch_table_for_class (Class class)
661 {
662   Class next;
663   struct sarray *arr;
664
665   /* Not yet installed -- skip it.  */
666   if (class->dtable == __objc_uninstalled_dtable) 
667     return;
668
669   DEBUG_PRINTF (" _objc_update_dispatch_table_for_class (%s)\n", class->name);
670
671   objc_mutex_lock (__objc_runtime_mutex);
672
673   arr = class->dtable;
674   __objc_install_premature_dtable (class); /* someone might require it... */
675   sarray_free (arr);                       /* release memory */
676   
677   /* Could have been lazy...  */
678   __objc_install_dispatch_table_for_class (class); 
679
680   if (class->subclass_list)     /* Traverse subclasses.  */
681     for (next = class->subclass_list; next; next = next->sibling_class)
682       __objc_update_dispatch_table_for_class (next);
683
684   objc_mutex_unlock (__objc_runtime_mutex);
685 }
686
687 /* This function adds a method list to a class.  This function is
688    typically called by another function specific to the run-time.  As
689    such this function does not worry about thread safe issues.
690
691    This one is only called for categories. Class objects have their
692    methods installed right away, and their selectors are made into
693    SEL's by the function __objc_register_selectors_from_class.  */
694 void
695 class_add_method_list (Class class, struct objc_method_list * list)
696 {
697   /* Passing of a linked list is not allowed.  Do multiple calls.  */
698   assert (! list->method_next);
699
700   __objc_register_selectors_from_list(list);
701
702   /* Add the methods to the class's method list.  */
703   list->method_next = class->methods;
704   class->methods = list;
705
706   /* Update the dispatch table of class.  */
707   __objc_update_dispatch_table_for_class (class);
708 }
709
710 struct objc_method *
711 class_get_instance_method (Class class, SEL op)
712 {
713   return search_for_method_in_hierarchy (class, op);
714 }
715
716 struct objc_method *
717 class_get_class_method (MetaClass class, SEL op)
718 {
719   return search_for_method_in_hierarchy (class, op);
720 }
721
722 struct objc_method *
723 class_getInstanceMethod (Class class_, SEL selector)
724 {
725   struct objc_method *m;
726
727   if (class_ == Nil  ||  selector == NULL)
728     return NULL;
729
730   m = search_for_method_in_hierarchy (class_, selector);
731   if (m)
732     return m;
733
734   /* Try going through +resolveInstanceMethod:, and do the search
735      again if successful.  */
736   if (__objc_resolve_instance_method (class_, selector))
737     return search_for_method_in_hierarchy (class_, selector);
738
739   return NULL;
740 }
741
742 struct objc_method *
743 class_getClassMethod (Class class_, SEL selector)
744 {
745   struct objc_method *m;
746
747   if (class_ == Nil  ||  selector == NULL)
748     return NULL;
749   
750   m = search_for_method_in_hierarchy (class_->class_pointer, 
751                                       selector);
752   if (m)
753     return m;
754
755   /* Try going through +resolveClassMethod:, and do the search again
756      if successful.  */
757   if (__objc_resolve_class_method (class_, selector))
758     return search_for_method_in_hierarchy (class_->class_pointer, 
759                                            selector);    
760
761   return NULL;
762 }
763
764 BOOL
765 class_addMethod (Class class_, SEL selector, IMP implementation,
766                  const char *method_types)
767 {
768   struct objc_method_list *method_list;
769   struct objc_method *method;
770   const char *method_name;
771
772   if (class_ == Nil  ||  selector == NULL  ||  implementation == NULL  
773       || method_types == NULL  || (strcmp (method_types, "") == 0))
774     return NO;
775
776   method_name = sel_getName (selector);
777   if (method_name == NULL)
778     return NO;
779
780   /* If the method already exists in the class, return NO.  It is fine
781      if the method already exists in the superclass; in that case, we
782      are overriding it.  */
783   if (CLS_IS_IN_CONSTRUCTION (class_))
784     {
785       /* The class only contains a list of methods; they have not been
786          registered yet, ie, the method_name of each of them is still
787          a string, not a selector.  Iterate manually over them to
788          check if we have already added the method.  */
789       struct objc_method_list * method_list = class_->methods;
790       while (method_list)
791         {
792           int i;
793           
794           /* Search the method list.  */
795           for (i = 0; i < method_list->method_count; ++i)
796             {
797               struct objc_method * method = &method_list->method_list[i];
798               
799               if (method->method_name
800                   && strcmp ((char *)method->method_name, method_name) == 0)
801                 return NO;
802             }
803           
804           /* The method wasn't found.  Follow the link to the next list of
805              methods.  */
806           method_list = method_list->method_next;
807         }
808       /* The method wasn't found.  It's a new one.  Go ahead and add
809          it.  */
810     }
811   else
812     {
813       /* Do the standard lookup.  This assumes the selectors are
814          mapped.  */
815       if (search_for_method_in_list (class_->methods, selector))
816         return NO;
817     }
818
819   method_list = (struct objc_method_list *)objc_calloc (1, sizeof (struct objc_method_list));
820   method_list->method_count = 1;
821
822   method = &(method_list->method_list[0]);
823   method->method_name = objc_malloc (strlen (method_name) + 1);
824   strcpy ((char *)method->method_name, method_name);
825
826   method->method_types = objc_malloc (strlen (method_types) + 1);
827   strcpy ((char *)method->method_types, method_types);
828   
829   method->method_imp = implementation;
830   
831   if (CLS_IS_IN_CONSTRUCTION (class_))
832     {
833       /* We only need to add the method to the list.  It will be
834          registered with the runtime when the class pair is registered
835          (if ever).  */
836       method_list->method_next = class_->methods;
837       class_->methods = method_list;
838     }
839   else
840     {
841       /* Add the method to a live class.  */
842       objc_mutex_lock (__objc_runtime_mutex);
843       class_add_method_list (class_, method_list);
844       objc_mutex_unlock (__objc_runtime_mutex);
845     }
846
847   return YES;
848 }
849
850 IMP
851 class_replaceMethod (Class class_, SEL selector, IMP implementation,
852                      const char *method_types)
853 {
854   struct objc_method * method;
855
856   if (class_ == Nil  ||  selector == NULL  ||  implementation == NULL
857       || method_types == NULL)
858     return NULL;
859
860   method = search_for_method_in_hierarchy (class_, selector);
861
862   if (method)
863     {
864       return method_setImplementation (method, implementation);
865     }
866   else
867     {
868       class_addMethod (class_, selector, implementation, method_types);
869       return NULL;
870     }
871 }
872
873 /* Search for a method starting from the current class up its
874    hierarchy.  Return a pointer to the method's method structure if
875    found.  NULL otherwise.  */
876 static struct objc_method *
877 search_for_method_in_hierarchy (Class cls, SEL sel)
878 {
879   struct objc_method * method = NULL;
880   Class class;
881
882   if (! sel_is_mapped (sel))
883     return NULL;
884
885   /* Scan the method list of the class.  If the method isn't found in
886      the list then step to its super class.  */
887   for (class = cls; ((! method) && class); class = class->super_class)
888     method = search_for_method_in_list (class->methods, sel);
889
890   return method;
891 }
892
893
894
895 /* Given a linked list of method and a method's name.  Search for the
896    named method's method structure.  Return a pointer to the method's
897    method structure if found.  NULL otherwise.  */  
898 struct objc_method *
899 search_for_method_in_list (struct objc_method_list * list, SEL op)
900 {
901   struct objc_method_list * method_list = list;
902
903   if (! sel_is_mapped (op))
904     return NULL;
905
906   /* If not found then we'll search the list.  */
907   while (method_list)
908     {
909       int i;
910
911       /* Search the method list.  */
912       for (i = 0; i < method_list->method_count; ++i)
913         {
914           struct objc_method * method = &method_list->method_list[i];
915
916           if (method->method_name)
917             if (method->method_name->sel_id == op->sel_id)
918               return method;
919         }
920
921       /* The method wasn't found.  Follow the link to the next list of
922          methods.  */
923       method_list = method_list->method_next;
924     }
925
926   return NULL;
927 }
928
929 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
930
931 /* Forwarding pointers/integers through the normal registers.  */
932 static id
933 __objc_word_forward (id rcv, SEL op, ...)
934 {
935   void *args, *res;
936
937   args = __builtin_apply_args ();
938   res = __objc_forward (rcv, op, args);
939   if (res)
940     __builtin_return (res);
941   else
942     return res;
943 }
944
945 /* Specific routine for forwarding floats/double because of
946    architectural differences on some processors.  i386s for example
947    which uses a floating point stack versus general registers for
948    floating point numbers.  This forward routine makes sure that GCC
949    restores the proper return values.  */
950 static double
951 __objc_double_forward (id rcv, SEL op, ...)
952 {
953   void *args, *res;
954
955   args = __builtin_apply_args ();
956   res = __objc_forward (rcv, op, args);
957   __builtin_return (res);
958 }
959
960 #if INVISIBLE_STRUCT_RETURN
961 static __big
962 #else
963 static id
964 #endif
965 __objc_block_forward (id rcv, SEL op, ...)
966 {
967   void *args, *res;
968
969   args = __builtin_apply_args ();
970   res = __objc_forward (rcv, op, args);
971   if (res)
972     __builtin_return (res);
973   else
974 #if INVISIBLE_STRUCT_RETURN
975     return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
976 #else
977     return nil;
978 #endif
979 }
980
981
982 /* This function is installed in the dispatch table for all methods
983    which are not implemented.  Thus, it is called when a selector is
984    not recognized.  */
985 static retval_t
986 __objc_forward (id object, SEL sel, arglist_t args)
987 {
988   IMP imp;
989   static SEL frwd_sel = 0;                      /* !T:SAFE2 */
990   SEL err_sel;
991
992   /* First try if the object understands forward::.  */
993   if (! frwd_sel)
994     frwd_sel = sel_get_any_uid ("forward::");
995
996   if (__objc_responds_to (object, frwd_sel))
997     {
998       imp = get_imp (object->class_pointer, frwd_sel);
999       return (*imp) (object, frwd_sel, sel, args);
1000     }
1001
1002   /* If the object recognizes the doesNotRecognize: method then we're
1003      going to send it.  */
1004   err_sel = sel_get_any_uid ("doesNotRecognize:");
1005   if (__objc_responds_to (object, err_sel))
1006     {
1007       imp = get_imp (object->class_pointer, err_sel);
1008       return (*imp) (object, err_sel, sel);
1009     }
1010   
1011   /* The object doesn't recognize the method.  Check for responding to
1012      error:.  If it does then sent it.  */
1013   {
1014     char msg[256 + strlen ((const char *) sel_getName (sel))
1015              + strlen ((const char *) object->class_pointer->name)];
1016
1017     sprintf (msg, "(%s) %s does not recognize %s",
1018              (CLS_ISMETA (object->class_pointer)
1019               ? "class"
1020               : "instance" ),
1021              object->class_pointer->name, sel_getName (sel));
1022
1023     /* TODO: support for error: is surely deprecated ? */
1024     err_sel = sel_get_any_uid ("error:");
1025     if (__objc_responds_to (object, err_sel))
1026       {
1027         imp = get_imp (object->class_pointer, err_sel);
1028         return (*imp) (object, sel_get_any_uid ("error:"), msg);
1029       }
1030
1031     /* The object doesn't respond to doesNotRecognize: or error:;
1032        Therefore, a default action is taken.  */
1033     _objc_abort ("%s\n", msg);
1034
1035     return 0;
1036   }
1037 }
1038
1039 void
1040 __objc_print_dtable_stats (void)
1041 {
1042   int total = 0;
1043
1044   objc_mutex_lock (__objc_runtime_mutex);
1045
1046 #ifdef OBJC_SPARSE2
1047   printf ("memory usage: (%s)\n", "2-level sparse arrays");
1048 #else
1049   printf ("memory usage: (%s)\n", "3-level sparse arrays");
1050 #endif
1051
1052   printf ("arrays: %d = %ld bytes\n", narrays, 
1053           (long) ((size_t) narrays * sizeof (struct sarray)));
1054   total += narrays * sizeof (struct sarray);
1055   printf ("buckets: %d = %ld bytes\n", nbuckets, 
1056           (long) ((size_t) nbuckets * sizeof (struct sbucket)));
1057   total += nbuckets * sizeof (struct sbucket);
1058
1059   printf ("idxtables: %d = %ld bytes\n",
1060           idxsize, (long) ((size_t) idxsize * sizeof (void *)));
1061   total += idxsize * sizeof (void *);
1062   printf ("-----------------------------------\n");
1063   printf ("total: %d bytes\n", total);
1064   printf ("===================================\n");
1065
1066   objc_mutex_unlock (__objc_runtime_mutex);
1067 }
1068
1069 /* Returns the uninstalled dispatch table indicator.  If a class'
1070    dispatch table points to __objc_uninstalled_dtable then that means
1071    it needs its dispatch table to be installed.  */
1072 struct sarray *
1073 objc_get_uninstalled_dtable (void)
1074 {
1075   return __objc_uninstalled_dtable;
1076 }