| 1 | /* GNU Objective C Runtime message lookup
|
|---|
| 2 | Copyright (C) 1993, 1995, 1996, 1997, 1998,
|
|---|
| 3 | 2001 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 (__objc_msg_forward)
|
|---|
| 87 | {
|
|---|
| 88 | IMP result;
|
|---|
| 89 | if ((result = __objc_msg_forward (sel)))
|
|---|
| 90 | return result;
|
|---|
| 91 | }
|
|---|
| 92 | else
|
|---|
| 93 | {
|
|---|
| 94 | const char *t = sel->sel_types;
|
|---|
| 95 |
|
|---|
| 96 | if (t && (*t == '[' || *t == '(' || *t == '{')
|
|---|
| 97 | #ifdef OBJC_MAX_STRUCT_BY_VALUE
|
|---|
| 98 | && objc_sizeof_type(t) > OBJC_MAX_STRUCT_BY_VALUE
|
|---|
| 99 | #endif
|
|---|
| 100 | )
|
|---|
| 101 | return (IMP)__objc_block_forward;
|
|---|
| 102 | else if (t && (*t == 'f' || *t == 'd'))
|
|---|
| 103 | return (IMP)__objc_double_forward;
|
|---|
| 104 | else
|
|---|
| 105 | return (IMP)__objc_word_forward;
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /* Given a class and selector, return the selector's implementation. */
|
|---|
| 110 | __inline__
|
|---|
| 111 | IMP
|
|---|
| 112 | get_imp (Class class, SEL sel)
|
|---|
| 113 | {
|
|---|
| 114 | void* res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
|
|---|
| 115 | if (res == 0)
|
|---|
| 116 | {
|
|---|
| 117 | /* Not a valid method */
|
|---|
| 118 | if(class->dtable == __objc_uninstalled_dtable)
|
|---|
| 119 | {
|
|---|
| 120 | /* The dispatch table needs to be installed. */
|
|---|
| 121 | objc_mutex_lock(__objc_runtime_mutex);
|
|---|
| 122 | __objc_install_dispatch_table_for_class (class);
|
|---|
| 123 | objc_mutex_unlock(__objc_runtime_mutex);
|
|---|
| 124 | /* Call ourselves with the installed dispatch table
|
|---|
| 125 | and get the real method */
|
|---|
| 126 | res = get_imp(class, sel);
|
|---|
| 127 | }
|
|---|
| 128 | else
|
|---|
| 129 | {
|
|---|
| 130 | /* The dispatch table has been installed so the
|
|---|
| 131 | method just doesn't exist for the class.
|
|---|
| 132 | Return the forwarding implementation. */
|
|---|
| 133 | res = __objc_get_forward_imp(sel);
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | return res;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /* Query if an object can respond to a selector, returns YES if the
|
|---|
| 140 | object implements the selector otherwise NO. Does not check if the
|
|---|
| 141 | method can be forwarded. */
|
|---|
| 142 | __inline__
|
|---|
| 143 | BOOL
|
|---|
| 144 | __objc_responds_to (id object, SEL sel)
|
|---|
| 145 | {
|
|---|
| 146 | void* res;
|
|---|
| 147 |
|
|---|
| 148 | /* Install dispatch table if need be */
|
|---|
| 149 | if (object->class_pointer->dtable == __objc_uninstalled_dtable)
|
|---|
| 150 | {
|
|---|
| 151 | objc_mutex_lock(__objc_runtime_mutex);
|
|---|
| 152 | __objc_install_dispatch_table_for_class (object->class_pointer);
|
|---|
| 153 | objc_mutex_unlock(__objc_runtime_mutex);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /* Get the method from the dispatch table */
|
|---|
| 157 | res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
|
|---|
| 158 | return (res != 0);
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /* This is the lookup function. All entries in the table are either a
|
|---|
| 162 | valid method *or* zero. If zero then either the dispatch table
|
|---|
| 163 | needs to be installed or it doesn't exist and forwarding is attempted. */
|
|---|
| 164 | __inline__
|
|---|
| 165 | IMP
|
|---|
| 166 | objc_msg_lookup(id receiver, SEL op)
|
|---|
| 167 | {
|
|---|
| 168 | IMP result;
|
|---|
| 169 | if(receiver)
|
|---|
| 170 | {
|
|---|
| 171 | result = sarray_get_safe (receiver->class_pointer->dtable,
|
|---|
| 172 | (sidx)op->sel_id);
|
|---|
| 173 | if (result == 0)
|
|---|
| 174 | {
|
|---|
| 175 | /* Not a valid method */
|
|---|
| 176 | if(receiver->class_pointer->dtable == __objc_uninstalled_dtable)
|
|---|
| 177 | {
|
|---|
| 178 | /* The dispatch table needs to be installed.
|
|---|
| 179 | This happens on the very first method call to the class. */
|
|---|
| 180 | __objc_init_install_dtable(receiver, op);
|
|---|
| 181 |
|
|---|
| 182 | /* Get real method for this in newly installed dtable */
|
|---|
| 183 | result = get_imp(receiver->class_pointer, op);
|
|---|
| 184 | }
|
|---|
| 185 | else
|
|---|
| 186 | {
|
|---|
| 187 | /* The dispatch table has been installed so the
|
|---|
| 188 | method just doesn't exist for the class.
|
|---|
| 189 | Attempt to forward the method. */
|
|---|
| 190 | result = __objc_get_forward_imp(op);
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|
| 193 | return result;
|
|---|
| 194 | }
|
|---|
| 195 | else
|
|---|
| 196 | return nil_method;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | IMP
|
|---|
| 200 | objc_msg_lookup_super (Super_t super, SEL sel)
|
|---|
| 201 | {
|
|---|
| 202 | if (super->self)
|
|---|
| 203 | return get_imp (super->class, sel);
|
|---|
| 204 | else
|
|---|
| 205 | return nil_method;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | int method_get_sizeof_arguments (Method*);
|
|---|
| 209 |
|
|---|
| 210 | retval_t
|
|---|
| 211 | objc_msg_sendv(id object, SEL op, arglist_t arg_frame)
|
|---|
| 212 | {
|
|---|
| 213 | Method* m = class_get_instance_method(object->class_pointer, op);
|
|---|
| 214 | const char *type;
|
|---|
| 215 | *((id*)method_get_first_argument (m, arg_frame, &type)) = object;
|
|---|
| 216 | *((SEL*)method_get_next_argument (arg_frame, &type)) = op;
|
|---|
| 217 | return __builtin_apply((apply_t)m->method_imp,
|
|---|
| 218 | arg_frame,
|
|---|
| 219 | method_get_sizeof_arguments (m));
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | void
|
|---|
| 223 | __objc_init_dispatch_tables()
|
|---|
| 224 | {
|
|---|
| 225 | __objc_uninstalled_dtable
|
|---|
| 226 | = sarray_new(200, 0);
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | /* This function is called by objc_msg_lookup when the
|
|---|
| 230 | dispatch table needs to be installed; thus it is called once
|
|---|
| 231 | for each class, namely when the very first message is sent to it. */
|
|---|
| 232 | static void
|
|---|
| 233 | __objc_init_install_dtable(id receiver, SEL op)
|
|---|
| 234 | {
|
|---|
| 235 | /* This may happen, if the programmer has taken the address of a
|
|---|
| 236 | method before the dtable was initialized... too bad for him! */
|
|---|
| 237 | if(receiver->class_pointer->dtable != __objc_uninstalled_dtable)
|
|---|
| 238 | return;
|
|---|
| 239 |
|
|---|
| 240 | objc_mutex_lock(__objc_runtime_mutex);
|
|---|
| 241 |
|
|---|
| 242 | if(CLS_ISCLASS(receiver->class_pointer))
|
|---|
| 243 | {
|
|---|
| 244 | /* receiver is an ordinary object */
|
|---|
| 245 | assert(CLS_ISCLASS(receiver->class_pointer));
|
|---|
| 246 |
|
|---|
| 247 | /* install instance methods table */
|
|---|
| 248 | __objc_install_dispatch_table_for_class (receiver->class_pointer);
|
|---|
| 249 |
|
|---|
| 250 | /* call +initialize -- this will in turn install the factory
|
|---|
| 251 | dispatch table if not already done :-) */
|
|---|
| 252 | __objc_send_initialize(receiver->class_pointer);
|
|---|
| 253 | }
|
|---|
| 254 | else
|
|---|
| 255 | {
|
|---|
| 256 | /* receiver is a class object */
|
|---|
| 257 | assert(CLS_ISCLASS((Class)receiver));
|
|---|
| 258 | assert(CLS_ISMETA(receiver->class_pointer));
|
|---|
| 259 |
|
|---|
| 260 | /* Install real dtable for factory methods */
|
|---|
| 261 | __objc_install_dispatch_table_for_class (receiver->class_pointer);
|
|---|
| 262 |
|
|---|
| 263 | __objc_send_initialize((Class)receiver);
|
|---|
| 264 | }
|
|---|
| 265 | objc_mutex_unlock(__objc_runtime_mutex);
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | /* Install dummy table for class which causes the first message to
|
|---|
| 269 | that class (or instances hereof) to be initialized properly */
|
|---|
| 270 | void
|
|---|
| 271 | __objc_install_premature_dtable(Class class)
|
|---|
| 272 | {
|
|---|
| 273 | assert(__objc_uninstalled_dtable);
|
|---|
| 274 | class->dtable = __objc_uninstalled_dtable;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | /* Send +initialize to class if not already done */
|
|---|
| 278 | static void
|
|---|
| 279 | __objc_send_initialize(Class class)
|
|---|
| 280 | {
|
|---|
| 281 | /* This *must* be a class object */
|
|---|
| 282 | assert(CLS_ISCLASS(class));
|
|---|
| 283 | assert(!CLS_ISMETA(class));
|
|---|
| 284 |
|
|---|
| 285 | if (!CLS_ISINITIALIZED(class))
|
|---|
| 286 | {
|
|---|
| 287 | CLS_SETINITIALIZED(class);
|
|---|
| 288 | CLS_SETINITIALIZED(class->class_pointer);
|
|---|
| 289 |
|
|---|
| 290 | /* Create the garbage collector type memory description */
|
|---|
| 291 | __objc_generate_gc_type_description (class);
|
|---|
| 292 |
|
|---|
| 293 | if(class->super_class)
|
|---|
| 294 | __objc_send_initialize(class->super_class);
|
|---|
| 295 |
|
|---|
| 296 | {
|
|---|
| 297 | SEL op = sel_register_name ("initialize");
|
|---|
| 298 | IMP imp = 0;
|
|---|
| 299 | MethodList_t method_list = class->class_pointer->methods;
|
|---|
| 300 |
|
|---|
| 301 | while (method_list) {
|
|---|
| 302 | int i;
|
|---|
| 303 | Method_t method;
|
|---|
| 304 |
|
|---|
| 305 | for (i = 0; i< method_list->method_count; i++) {
|
|---|
| 306 | method = &(method_list->method_list[i]);
|
|---|
| 307 | if (method->method_name
|
|---|
| 308 | && method->method_name->sel_id == op->sel_id) {
|
|---|
| 309 | imp = method->method_imp;
|
|---|
| 310 | break;
|
|---|
| 311 | }
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | if (imp)
|
|---|
| 315 | break;
|
|---|
| 316 |
|
|---|
| 317 | method_list = method_list->method_next;
|
|---|
| 318 |
|
|---|
| 319 | }
|
|---|
| 320 | if (imp)
|
|---|
| 321 | (*imp)((id)class, op);
|
|---|
| 322 |
|
|---|
| 323 | }
|
|---|
| 324 | }
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | /* Walk on the methods list of class and install the methods in the reverse
|
|---|
| 328 | order of the lists. Since methods added by categories are before the methods
|
|---|
| 329 | of class in the methods list, this allows categories to substitute methods
|
|---|
| 330 | declared in class. However if more than one category replaces the same
|
|---|
| 331 | method nothing is guaranteed about what method will be used.
|
|---|
| 332 | Assumes that __objc_runtime_mutex is locked down. */
|
|---|
| 333 | static void
|
|---|
| 334 | __objc_install_methods_in_dtable (Class class, MethodList_t method_list)
|
|---|
| 335 | {
|
|---|
| 336 | int i;
|
|---|
| 337 |
|
|---|
| 338 | if (!method_list)
|
|---|
| 339 | return;
|
|---|
| 340 |
|
|---|
| 341 | if (method_list->method_next)
|
|---|
| 342 | __objc_install_methods_in_dtable (class, method_list->method_next);
|
|---|
| 343 |
|
|---|
| 344 | for (i = 0; i < method_list->method_count; i++)
|
|---|
| 345 | {
|
|---|
| 346 | Method_t method = &(method_list->method_list[i]);
|
|---|
| 347 | sarray_at_put_safe (class->dtable,
|
|---|
| 348 | (sidx) method->method_name->sel_id,
|
|---|
| 349 | method->method_imp);
|
|---|
| 350 | }
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | /* Assumes that __objc_runtime_mutex is locked down. */
|
|---|
| 354 | static void
|
|---|
| 355 | __objc_install_dispatch_table_for_class (Class class)
|
|---|
| 356 | {
|
|---|
| 357 | Class super;
|
|---|
| 358 |
|
|---|
| 359 | /* If the class has not yet had its class links resolved, we must
|
|---|
| 360 | re-compute all class links */
|
|---|
| 361 | if(!CLS_ISRESOLV(class))
|
|---|
| 362 | __objc_resolve_class_links();
|
|---|
| 363 |
|
|---|
| 364 | super = class->super_class;
|
|---|
| 365 |
|
|---|
| 366 | if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
|
|---|
| 367 | __objc_install_dispatch_table_for_class (super);
|
|---|
| 368 |
|
|---|
| 369 | /* Allocate dtable if necessary */
|
|---|
| 370 | if (super == 0)
|
|---|
| 371 | {
|
|---|
| 372 | objc_mutex_lock(__objc_runtime_mutex);
|
|---|
| 373 | class->dtable = sarray_new (__objc_selector_max_index, 0);
|
|---|
| 374 | objc_mutex_unlock(__objc_runtime_mutex);
|
|---|
| 375 | }
|
|---|
| 376 | else
|
|---|
| 377 | class->dtable = sarray_lazy_copy (super->dtable);
|
|---|
| 378 |
|
|---|
| 379 | __objc_install_methods_in_dtable (class, class->methods);
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | void
|
|---|
| 383 | __objc_update_dispatch_table_for_class (Class class)
|
|---|
| 384 | {
|
|---|
| 385 | Class next;
|
|---|
| 386 | struct sarray *arr;
|
|---|
| 387 |
|
|---|
| 388 | /* not yet installed -- skip it */
|
|---|
| 389 | if (class->dtable == __objc_uninstalled_dtable)
|
|---|
| 390 | return;
|
|---|
| 391 |
|
|---|
| 392 | objc_mutex_lock(__objc_runtime_mutex);
|
|---|
| 393 |
|
|---|
| 394 | arr = class->dtable;
|
|---|
| 395 | __objc_install_premature_dtable (class); /* someone might require it... */
|
|---|
| 396 | sarray_free (arr); /* release memory */
|
|---|
| 397 |
|
|---|
| 398 | /* could have been lazy... */
|
|---|
| 399 | __objc_install_dispatch_table_for_class (class);
|
|---|
| 400 |
|
|---|
| 401 | if (class->subclass_list) /* Traverse subclasses */
|
|---|
| 402 | for (next = class->subclass_list; next; next = next->sibling_class)
|
|---|
| 403 | __objc_update_dispatch_table_for_class (next);
|
|---|
| 404 |
|
|---|
| 405 | objc_mutex_unlock(__objc_runtime_mutex);
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 | /* This function adds a method list to a class. This function is
|
|---|
| 410 | typically called by another function specific to the run-time. As
|
|---|
| 411 | such this function does not worry about thread safe issues.
|
|---|
| 412 |
|
|---|
| 413 | This one is only called for categories. Class objects have their
|
|---|
| 414 | methods installed right away, and their selectors are made into
|
|---|
| 415 | SEL's by the function __objc_register_selectors_from_class. */
|
|---|
| 416 | void
|
|---|
| 417 | class_add_method_list (Class class, MethodList_t list)
|
|---|
| 418 | {
|
|---|
| 419 | int i;
|
|---|
| 420 |
|
|---|
| 421 | /* Passing of a linked list is not allowed. Do multiple calls. */
|
|---|
| 422 | assert (!list->method_next);
|
|---|
| 423 |
|
|---|
| 424 | /* Check for duplicates. */
|
|---|
| 425 | for (i = 0; i < list->method_count; ++i)
|
|---|
| 426 | {
|
|---|
| 427 | Method_t method = &list->method_list[i];
|
|---|
| 428 |
|
|---|
| 429 | if (method->method_name) /* Sometimes these are NULL */
|
|---|
| 430 | {
|
|---|
| 431 | /* This is where selector names are transmogrified to SEL's */
|
|---|
| 432 | method->method_name =
|
|---|
| 433 | sel_register_typed_name ((const char*)method->method_name,
|
|---|
| 434 | method->method_types);
|
|---|
| 435 | }
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | /* Add the methods to the class's method list. */
|
|---|
| 439 | list->method_next = class->methods;
|
|---|
| 440 | class->methods = list;
|
|---|
| 441 |
|
|---|
| 442 | /* Update the dispatch table of class */
|
|---|
| 443 | __objc_update_dispatch_table_for_class (class);
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | Method_t
|
|---|
| 447 | class_get_instance_method(Class class, SEL op)
|
|---|
| 448 | {
|
|---|
| 449 | return search_for_method_in_hierarchy(class, op);
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | Method_t
|
|---|
| 453 | class_get_class_method(MetaClass class, SEL op)
|
|---|
| 454 | {
|
|---|
| 455 | return search_for_method_in_hierarchy(class, op);
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 | /* Search for a method starting from the current class up its hierarchy.
|
|---|
| 460 | Return a pointer to the method's method structure if found. NULL
|
|---|
| 461 | otherwise. */
|
|---|
| 462 |
|
|---|
| 463 | static Method_t
|
|---|
| 464 | search_for_method_in_hierarchy (Class cls, SEL sel)
|
|---|
| 465 | {
|
|---|
| 466 | Method_t method = NULL;
|
|---|
| 467 | Class class;
|
|---|
| 468 |
|
|---|
| 469 | if (! sel_is_mapped (sel))
|
|---|
| 470 | return NULL;
|
|---|
| 471 |
|
|---|
| 472 | /* Scan the method list of the class. If the method isn't found in the
|
|---|
| 473 | list then step to its super class. */
|
|---|
| 474 | for (class = cls; ((! method) && class); class = class->super_class)
|
|---|
| 475 | method = search_for_method_in_list (class->methods, sel);
|
|---|
| 476 |
|
|---|
| 477 | return method;
|
|---|
| 478 | }
|
|---|
| 479 |
|
|---|
| 480 |
|
|---|
| 481 |
|
|---|
| 482 | /* Given a linked list of method and a method's name. Search for the named
|
|---|
| 483 | method's method structure. Return a pointer to the method's method
|
|---|
| 484 | structure if found. NULL otherwise. */
|
|---|
| 485 | Method_t
|
|---|
| 486 | search_for_method_in_list (MethodList_t list, SEL op)
|
|---|
| 487 | {
|
|---|
| 488 | MethodList_t method_list = list;
|
|---|
| 489 |
|
|---|
| 490 | if (! sel_is_mapped (op))
|
|---|
| 491 | return NULL;
|
|---|
| 492 |
|
|---|
| 493 | /* If not found then we'll search the list. */
|
|---|
| 494 | while (method_list)
|
|---|
| 495 | {
|
|---|
| 496 | int i;
|
|---|
| 497 |
|
|---|
| 498 | /* Search the method list. */
|
|---|
| 499 | for (i = 0; i < method_list->method_count; ++i)
|
|---|
| 500 | {
|
|---|
| 501 | Method_t method = &method_list->method_list[i];
|
|---|
| 502 |
|
|---|
| 503 | if (method->method_name)
|
|---|
| 504 | if (method->method_name->sel_id == op->sel_id)
|
|---|
| 505 | return method;
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|
| 508 | /* The method wasn't found. Follow the link to the next list of
|
|---|
| 509 | methods. */
|
|---|
| 510 | method_list = method_list->method_next;
|
|---|
| 511 | }
|
|---|
| 512 |
|
|---|
| 513 | return NULL;
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | static retval_t __objc_forward (id object, SEL sel, arglist_t args);
|
|---|
| 517 |
|
|---|
| 518 | /* Forwarding pointers/integers through the normal registers */
|
|---|
| 519 | static id
|
|---|
| 520 | __objc_word_forward (id rcv, SEL op, ...)
|
|---|
| 521 | {
|
|---|
| 522 | void *args, *res;
|
|---|
| 523 |
|
|---|
| 524 | args = __builtin_apply_args ();
|
|---|
| 525 | res = __objc_forward (rcv, op, args);
|
|---|
| 526 | if (res)
|
|---|
| 527 | __builtin_return (res);
|
|---|
| 528 | else
|
|---|
| 529 | return res;
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | /* Specific routine for forwarding floats/double because of
|
|---|
| 533 | architectural differences on some processors. i386s for
|
|---|
| 534 | example which uses a floating point stack versus general
|
|---|
| 535 | registers for floating point numbers. This forward routine
|
|---|
| 536 | makes sure that GCC restores the proper return values */
|
|---|
| 537 | static double
|
|---|
| 538 | __objc_double_forward (id rcv, SEL op, ...)
|
|---|
| 539 | {
|
|---|
| 540 | void *args, *res;
|
|---|
| 541 |
|
|---|
| 542 | args = __builtin_apply_args ();
|
|---|
| 543 | res = __objc_forward (rcv, op, args);
|
|---|
| 544 | __builtin_return (res);
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | #if INVISIBLE_STRUCT_RETURN
|
|---|
| 548 | static __big
|
|---|
| 549 | #else
|
|---|
| 550 | static id
|
|---|
| 551 | #endif
|
|---|
| 552 | __objc_block_forward (id rcv, SEL op, ...)
|
|---|
| 553 | {
|
|---|
| 554 | void *args, *res;
|
|---|
| 555 |
|
|---|
| 556 | args = __builtin_apply_args ();
|
|---|
| 557 | res = __objc_forward (rcv, op, args);
|
|---|
| 558 | if (res)
|
|---|
| 559 | __builtin_return (res);
|
|---|
| 560 | else
|
|---|
| 561 | #if INVISIBLE_STRUCT_RETURN
|
|---|
| 562 | return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
|
|---|
| 563 | #else
|
|---|
| 564 | return nil;
|
|---|
| 565 | #endif
|
|---|
| 566 | }
|
|---|
| 567 |
|
|---|
| 568 |
|
|---|
| 569 | /* This function is installed in the dispatch table for all methods which are
|
|---|
| 570 | not implemented. Thus, it is called when a selector is not recognized. */
|
|---|
| 571 | static retval_t
|
|---|
| 572 | __objc_forward (id object, SEL sel, arglist_t args)
|
|---|
| 573 | {
|
|---|
| 574 | IMP imp;
|
|---|
| 575 | static SEL frwd_sel = 0; /* !T:SAFE2 */
|
|---|
| 576 | SEL err_sel;
|
|---|
| 577 |
|
|---|
| 578 | /* first try if the object understands forward:: */
|
|---|
| 579 | if (!frwd_sel)
|
|---|
| 580 | frwd_sel = sel_get_any_uid("forward::");
|
|---|
| 581 |
|
|---|
| 582 | if (__objc_responds_to (object, frwd_sel))
|
|---|
| 583 | {
|
|---|
| 584 | imp = get_imp(object->class_pointer, frwd_sel);
|
|---|
| 585 | return (*imp)(object, frwd_sel, sel, args);
|
|---|
| 586 | }
|
|---|
| 587 |
|
|---|
| 588 | /* If the object recognizes the doesNotRecognize: method then we're going
|
|---|
| 589 | to send it. */
|
|---|
| 590 | err_sel = sel_get_any_uid ("doesNotRecognize:");
|
|---|
| 591 | if (__objc_responds_to (object, err_sel))
|
|---|
| 592 | {
|
|---|
| 593 | imp = get_imp (object->class_pointer, err_sel);
|
|---|
| 594 | return (*imp) (object, err_sel, sel);
|
|---|
| 595 | }
|
|---|
| 596 |
|
|---|
| 597 | /* The object doesn't recognize the method. Check for responding to
|
|---|
| 598 | error:. If it does then sent it. */
|
|---|
| 599 | {
|
|---|
| 600 | char msg[256 + strlen ((const char*)sel_get_name (sel))
|
|---|
| 601 | + strlen ((const char*)object->class_pointer->name)];
|
|---|
| 602 |
|
|---|
| 603 | sprintf (msg, "(%s) %s does not recognize %s",
|
|---|
| 604 | (CLS_ISMETA(object->class_pointer)
|
|---|
| 605 | ? "class"
|
|---|
| 606 | : "instance" ),
|
|---|
| 607 | object->class_pointer->name, sel_get_name (sel));
|
|---|
| 608 |
|
|---|
| 609 | err_sel = sel_get_any_uid ("error:");
|
|---|
| 610 | if (__objc_responds_to (object, err_sel))
|
|---|
| 611 | {
|
|---|
| 612 | imp = get_imp (object->class_pointer, err_sel);
|
|---|
| 613 | return (*imp) (object, sel_get_any_uid ("error:"), msg);
|
|---|
| 614 | }
|
|---|
| 615 |
|
|---|
| 616 | /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
|
|---|
| 617 | a default action is taken. */
|
|---|
| 618 | objc_error (object, OBJC_ERR_UNIMPLEMENTED, "%s\n", msg);
|
|---|
| 619 |
|
|---|
| 620 | return 0;
|
|---|
| 621 | }
|
|---|
| 622 | }
|
|---|
| 623 |
|
|---|
| 624 | void
|
|---|
| 625 | __objc_print_dtable_stats()
|
|---|
| 626 | {
|
|---|
| 627 | int total = 0;
|
|---|
| 628 |
|
|---|
| 629 | objc_mutex_lock(__objc_runtime_mutex);
|
|---|
| 630 |
|
|---|
| 631 | #ifdef OBJC_SPARSE2
|
|---|
| 632 | printf("memory usage: (%s)\n", "2-level sparse arrays");
|
|---|
| 633 | #else
|
|---|
| 634 | printf("memory usage: (%s)\n", "3-level sparse arrays");
|
|---|
| 635 | #endif
|
|---|
| 636 |
|
|---|
| 637 | printf("arrays: %d = %ld bytes\n", narrays,
|
|---|
| 638 | (long)narrays*sizeof(struct sarray));
|
|---|
| 639 | total += narrays*sizeof(struct sarray);
|
|---|
| 640 | printf("buckets: %d = %ld bytes\n", nbuckets,
|
|---|
| 641 | (long)nbuckets*sizeof(struct sbucket));
|
|---|
| 642 | total += nbuckets*sizeof(struct sbucket);
|
|---|
| 643 |
|
|---|
| 644 | printf("idxtables: %d = %ld bytes\n", idxsize, (long)idxsize*sizeof(void*));
|
|---|
| 645 | total += idxsize*sizeof(void*);
|
|---|
| 646 | printf("-----------------------------------\n");
|
|---|
| 647 | printf("total: %d bytes\n", total);
|
|---|
| 648 | printf("===================================\n");
|
|---|
| 649 |
|
|---|
| 650 | objc_mutex_unlock(__objc_runtime_mutex);
|
|---|
| 651 | }
|
|---|
| 652 |
|
|---|
| 653 | /* Returns the uninstalled dispatch table indicator.
|
|---|
| 654 | If a class' dispatch table points to __objc_uninstalled_dtable
|
|---|
| 655 | then that means it needs its dispatch table to be installed. */
|
|---|
| 656 | __inline__
|
|---|
| 657 | struct sarray*
|
|---|
| 658 | objc_get_uninstalled_dtable()
|
|---|
| 659 | {
|
|---|
| 660 | return __objc_uninstalled_dtable;
|
|---|
| 661 | }
|
|---|