| 1 | /* GNU Objective C Runtime message lookup
|
|---|
| 2 | Copyright (C) 1993, 1995, 1996, 1997, 1998,
|
|---|
| 3 | 2001, 2002 Free Software Foundation, Inc.
|
|---|
| 4 | Contributed by Kresten Krab Thorup
|
|---|
| 5 |
|
|---|
| 6 | This file is part of GNU CC.
|
|---|
| 7 |
|
|---|
| 8 | GNU CC 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 2, or (at your option) any later version.
|
|---|
| 11 |
|
|---|
| 12 | GNU CC 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 | You should have received a copy of the GNU General Public License along with
|
|---|
| 18 | GNU CC; see the file COPYING. If not, write to the Free Software
|
|---|
| 19 | Foundation, 59 Temple Place - Suite 330,
|
|---|
| 20 | Boston, MA 02111-1307, USA. */
|
|---|
| 21 |
|
|---|
| 22 | /* As a special exception, if you link this library with files compiled with
|
|---|
| 23 | GCC to produce an executable, this does not cause the resulting executable
|
|---|
| 24 | to be covered by the GNU General Public License. This exception does not
|
|---|
| 25 | however invalidate any other reasons why the executable file might be
|
|---|
| 26 | covered by the GNU General Public License. */
|
|---|
| 27 |
|
|---|
| 28 | #include "tconfig.h"
|
|---|
| 29 | #include "runtime.h"
|
|---|
| 30 | #include "sarray.h"
|
|---|
| 31 | #include "encoding.h"
|
|---|
| 32 | #include "runtime-info.h"
|
|---|
| 33 |
|
|---|
| 34 | /* this is how we hack STRUCT_VALUE to be 1 or 0 */
|
|---|
| 35 | #define gen_rtx(args...) 1
|
|---|
| 36 | #define gen_rtx_MEM(args...) 1
|
|---|
| 37 | #define gen_rtx_REG(args...) 1
|
|---|
| 38 | #define rtx int
|
|---|
| 39 |
|
|---|
| 40 | #if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
|
|---|
| 41 | #define INVISIBLE_STRUCT_RETURN 1
|
|---|
| 42 | #else
|
|---|
| 43 | #define INVISIBLE_STRUCT_RETURN 0
|
|---|
| 44 | #endif
|
|---|
| 45 |
|
|---|
| 46 | /* The uninstalled dispatch table */
|
|---|
| 47 | struct sarray *__objc_uninstalled_dtable = 0; /* !T:MUTEX */
|
|---|
| 48 |
|
|---|
| 49 | /* Hook for method forwarding. If it is set, is invoked to return a
|
|---|
| 50 | function that performs the real forwarding. Otherwise the libgcc
|
|---|
| 51 | based functions (__builtin_apply and friends) are used. */
|
|---|
| 52 | IMP (*__objc_msg_forward) (SEL) = NULL;
|
|---|
| 53 |
|
|---|
| 54 | /* Send +initialize to class */
|
|---|
| 55 | static void __objc_send_initialize (Class);
|
|---|
| 56 |
|
|---|
| 57 | static void __objc_install_dispatch_table_for_class (Class);
|
|---|
| 58 |
|
|---|
| 59 | /* Forward declare some functions */
|
|---|
| 60 | static void __objc_init_install_dtable (id, SEL);
|
|---|
| 61 |
|
|---|
| 62 | /* Various forwarding functions that are used based upon the
|
|---|
| 63 | return type for the selector.
|
|---|
| 64 | __objc_block_forward for structures.
|
|---|
| 65 | __objc_double_forward for floats/doubles.
|
|---|
| 66 | __objc_word_forward for pointers or types that fit in registers.
|
|---|
| 67 | */
|
|---|
| 68 | static double __objc_double_forward (id, SEL, ...);
|
|---|
| 69 | static id __objc_word_forward (id, SEL, ...);
|
|---|
| 70 | typedef struct { id many[8]; } __big;
|
|---|
| 71 | #if INVISIBLE_STRUCT_RETURN
|
|---|
| 72 | static __big
|
|---|
| 73 | #else
|
|---|
| 74 | static id
|
|---|
| 75 | #endif
|
|---|
| 76 | __objc_block_forward (id, SEL, ...);
|
|---|
| 77 | static Method_t search_for_method_in_hierarchy (Class class, SEL sel);
|
|---|
| 78 | Method_t search_for_method_in_list (MethodList_t list, SEL op);
|
|---|
| 79 | id nil_method (id, SEL);
|
|---|
| 80 |
|
|---|
| 81 | /* Given a selector, return the proper forwarding implementation. */
|
|---|
| 82 | __inline__
|
|---|
| 83 | IMP
|
|---|
| 84 | __objc_get_forward_imp (SEL sel)
|
|---|
| 85 | {
|
|---|
| 86 | /* If a custom forwarding hook was registered, try getting a forwarding
|
|---|
| 87 | * function from it. */
|
|---|
| 88 | if (__objc_msg_forward)
|
|---|
| 89 | {
|
|---|
| 90 | IMP result;
|
|---|
| 91 | if ((result = __objc_msg_forward (sel)) != NULL)
|
|---|
| 92 | return result;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /* In all other cases, use the default forwarding functions built using
|
|---|
| 96 | * __builtin_apply and friends. */
|
|---|
| 97 | {
|
|---|
| 98 | const char *t = sel->sel_types;
|
|---|
| 99 |
|
|---|
| 100 | if (t && (*t == '[' || *t == '(' || *t == '{')
|
|---|
| 101 | #ifdef OBJC_MAX_STRUCT_BY_VALUE
|
|---|
| 102 | && objc_sizeof_type (t) > OBJC_MAX_STRUCT_BY_VALUE
|
|---|
| 103 | #endif
|
|---|
| 104 | )
|
|---|
| 105 | return (IMP)__objc_block_forward;
|
|---|
| 106 | else if (t && (*t == 'f' || *t == 'd'))
|
|---|
| 107 | return (IMP)__objc_double_forward;
|
|---|
| 108 | else
|
|---|
| 109 | return (IMP)__objc_word_forward;
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /* Given a class and selector, return the selector's implementation. */
|
|---|
| 114 | __inline__
|
|---|
| 115 | IMP
|
|---|
| 116 | get_imp (Class class, SEL sel)
|
|---|
| 117 | {
|
|---|
| 118 | void *res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
|
|---|
| 119 | if (res == 0)
|
|---|
| 120 | {
|
|---|
| 121 | /* Not a valid method */
|
|---|
| 122 | if (class->dtable == __objc_uninstalled_dtable)
|
|---|
| 123 | {
|
|---|
| 124 | /* The dispatch table needs to be installed. */
|
|---|
| 125 | objc_mutex_lock (__objc_runtime_mutex);
|
|---|
| 126 | __objc_install_dispatch_table_for_class (class);
|
|---|
| 127 | objc_mutex_unlock (__objc_runtime_mutex);
|
|---|
| 128 | /* Call ourselves with the installed dispatch table
|
|---|
| 129 | and get the real method */
|
|---|
| 130 | res = get_imp (class, sel);
|
|---|
| 131 | }
|
|---|
| 132 | else
|
|---|
| 133 | {
|
|---|
| 134 | /* The dispatch table has been installed so the
|
|---|
| 135 | method just doesn't exist for the class.
|
|---|
| 136 | Return the forwarding implementation. */
|
|---|
| 137 | res = __objc_get_forward_imp (sel);
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | return res;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /* Query if an object can respond to a selector, returns YES if the
|
|---|
| 144 | object implements the selector otherwise NO. Does not check if the
|
|---|
| 145 | method can be forwarded. */
|
|---|
| 146 | __inline__
|
|---|
| 147 | BOOL
|
|---|
| 148 | __objc_responds_to (id object, SEL sel)
|
|---|
| 149 | {
|
|---|
| 150 | void *res;
|
|---|
| 151 |
|
|---|
| 152 | /* Install dispatch table if need be */
|
|---|
| 153 | if (object->class_pointer->dtable == __objc_uninstalled_dtable)
|
|---|
| 154 | {
|
|---|
| 155 | objc_mutex_lock (__objc_runtime_mutex);
|
|---|
| 156 | __objc_install_dispatch_table_for_class (object->class_pointer);
|
|---|
| 157 | objc_mutex_unlock (__objc_runtime_mutex);
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /* Get the method from the dispatch table */
|
|---|
| 161 | res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
|
|---|
| 162 | return (res != 0);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /* This is the lookup function. All entries in the table are either a
|
|---|
| 166 | valid method *or* zero. If zero then either the dispatch table
|
|---|
| 167 | needs to be installed or it doesn't exist and forwarding is attempted. */
|
|---|
| 168 | __inline__
|
|---|
| 169 | IMP
|
|---|
| 170 | objc_msg_lookup (id receiver, SEL op)
|
|---|
| 171 | {
|
|---|
| 172 | IMP result;
|
|---|
| 173 | if (receiver)
|
|---|
| 174 | {
|
|---|
| 175 | result = sarray_get_safe (receiver->class_pointer->dtable,
|
|---|
| 176 | (sidx)op->sel_id);
|
|---|
| 177 | if (result == 0)
|
|---|
| 178 | {
|
|---|
| 179 | /* Not a valid method */
|
|---|
| 180 | if (receiver->class_pointer->dtable == __objc_uninstalled_dtable)
|
|---|
| 181 | {
|
|---|
| 182 | /* The dispatch table needs to be installed.
|
|---|
| 183 | This happens on the very first method call to the class. */
|
|---|
| 184 | __objc_init_install_dtable (receiver, op);
|
|---|
| 185 |
|
|---|
| 186 | /* Get real method for this in newly installed dtable */
|
|---|
| 187 | result = get_imp (receiver->class_pointer, op);
|
|---|
| 188 | }
|
|---|
| 189 | else
|
|---|
| 190 | {
|
|---|
| 191 | /* The dispatch table has been installed so the
|
|---|
| 192 | method just doesn't exist for the class.
|
|---|
| 193 | Attempt to forward the method. */
|
|---|
| 194 | result = __objc_get_forward_imp (op);
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 | return result;
|
|---|
| 198 | }
|
|---|
| 199 | else
|
|---|
| 200 | return (IMP)nil_method;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | IMP
|
|---|
| 204 | objc_msg_lookup_super (Super_t super, SEL sel)
|
|---|
| 205 | {
|
|---|
| 206 | if (super->self)
|
|---|
| 207 | return get_imp (super->class, sel);
|
|---|
| 208 | else
|
|---|
| 209 | return (IMP)nil_method;
|
|---|
| 210 | }
|
|---|
|
|---|