1998-09-21 03:22:07 +02:00
|
|
|
/* GNU Objective C Runtime message lookup
|
2001-01-12 01:30:53 +01:00
|
|
|
Copyright (C) 1993, 1995, 1996, 1997, 1998,
|
2010-10-13 00:00:01 +02:00
|
|
|
2001, 2002, 2004, 2009, 2010 Free Software Foundation, Inc.
|
1998-09-21 03:22:07 +02:00
|
|
|
Contributed by Kresten Krab Thorup
|
|
|
|
|
hash.c, [...]: Replace "GNU CC" with "GCC".
* hash.c, init.c, libobjc.def, libobjc_entry.c, linking.m,
makefile.dos, misc.c, nil_method.c, objects.c, sarray.c,
selector.c, sendmsg.c, thr-dce.c, thr-decosf1.c, thr-irix.c,
thr-mach.c, thr-objc.c, thr-os2.c, thr-posix.c, thr-pthreads.c,
thr-rtems.c, thr-single.c, thr-solaris.c, thr-vxworks.c,
thr-win32.c, thr.c: Replace "GNU CC" with "GCC".
From-SVN: r67134
2003-05-23 22:25:39 +02:00
|
|
|
This file is part of GCC.
|
1998-09-21 03:22:07 +02:00
|
|
|
|
hash.c, [...]: Replace "GNU CC" with "GCC".
* hash.c, init.c, libobjc.def, libobjc_entry.c, linking.m,
makefile.dos, misc.c, nil_method.c, objects.c, sarray.c,
selector.c, sendmsg.c, thr-dce.c, thr-decosf1.c, thr-irix.c,
thr-mach.c, thr-objc.c, thr-os2.c, thr-posix.c, thr-pthreads.c,
thr-rtems.c, thr-single.c, thr-solaris.c, thr-vxworks.c,
thr-win32.c, thr.c: Replace "GNU CC" with "GCC".
From-SVN: r67134
2003-05-23 22:25:39 +02:00
|
|
|
GCC is free software; you can redistribute it and/or modify it under the
|
1998-09-21 03:22:07 +02:00
|
|
|
terms of the GNU General Public License as published by the Free Software
|
2009-04-09 17:00:19 +02:00
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
1998-09-21 03:22:07 +02:00
|
|
|
|
hash.c, [...]: Replace "GNU CC" with "GCC".
* hash.c, init.c, libobjc.def, libobjc_entry.c, linking.m,
makefile.dos, misc.c, nil_method.c, objects.c, sarray.c,
selector.c, sendmsg.c, thr-dce.c, thr-decosf1.c, thr-irix.c,
thr-mach.c, thr-objc.c, thr-os2.c, thr-posix.c, thr-pthreads.c,
thr-rtems.c, thr-single.c, thr-solaris.c, thr-vxworks.c,
thr-win32.c, thr.c: Replace "GNU CC" with "GCC".
From-SVN: r67134
2003-05-23 22:25:39 +02:00
|
|
|
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
|
1998-09-21 03:22:07 +02:00
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
2009-04-09 17:00:19 +02:00
|
|
|
Under Section 7 of GPL version 3, you are granted additional
|
|
|
|
permissions described in the GCC Runtime Library Exception, version
|
|
|
|
3.1, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License and
|
|
|
|
a copy of the GCC Runtime Library Exception along with this program;
|
|
|
|
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
|
2002-12-16 19:23:00 +01:00
|
|
|
/* FIXME: This file has no business including tm.h. */
|
2004-05-25 21:10:54 +02:00
|
|
|
/* FIXME: This should be using libffi instead of __builtin_apply
|
|
|
|
and friends. */
|
2002-12-16 19:23:00 +01:00
|
|
|
|
2010-09-12 00:47:14 +02:00
|
|
|
#include "objc-private/common.h"
|
2010-09-12 02:43:15 +02:00
|
|
|
#include "objc-private/error.h"
|
1998-09-30 04:13:15 +02:00
|
|
|
#include "tconfig.h"
|
2002-12-16 19:23:00 +01:00
|
|
|
#include "coretypes.h"
|
|
|
|
#include "tm.h"
|
2010-12-16 23:52:48 +01:00
|
|
|
#include "objc/runtime.h"
|
2010-12-19 02:29:09 +01:00
|
|
|
#include "objc/message.h" /* For objc_msg_lookup(), objc_msg_lookup_super(). */
|
2010-09-11 14:58:27 +02:00
|
|
|
#include "objc/thr.h"
|
2010-12-16 23:52:48 +01:00
|
|
|
#include "objc-private/module-abi-8.h"
|
2010-09-11 14:58:27 +02:00
|
|
|
#include "objc-private/runtime.h"
|
2010-09-18 11:34:58 +02:00
|
|
|
#include "objc-private/sarray.h"
|
2010-12-16 23:52:48 +01:00
|
|
|
#include "objc-private/selector.h" /* For sel_is_mapped() */
|
1998-09-21 03:22:07 +02:00
|
|
|
#include "runtime-info.h"
|
2010-09-18 11:34:58 +02:00
|
|
|
#include <assert.h> /* For assert */
|
2010-09-17 18:35:15 +02:00
|
|
|
#include <string.h> /* For strlen */
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-10-16 23:26:59 +02:00
|
|
|
/* Temporarily while we include objc/objc-api.h instead of objc-private/module-abi-8.h. */
|
|
|
|
#define _CLS_IN_CONSTRUCTION 0x10L
|
|
|
|
#define CLS_IS_IN_CONSTRUCTION(cls) __CLS_ISINFO(cls, _CLS_IN_CONSTRUCTION)
|
|
|
|
|
2004-05-25 21:10:54 +02:00
|
|
|
/* This is how we hack STRUCT_VALUE to be 1 or 0. */
|
1998-09-21 03:22:07 +02:00
|
|
|
#define gen_rtx(args...) 1
|
|
|
|
#define gen_rtx_MEM(args...) 1
|
2001-01-03 09:49:02 +01:00
|
|
|
#define gen_rtx_REG(args...) 1
|
2010-12-16 23:52:48 +01:00
|
|
|
/* Already defined in gcc/coretypes.h. So prevent double definition warning. */
|
2008-06-10 14:37:51 +02:00
|
|
|
#undef rtx
|
1998-09-21 03:22:07 +02:00
|
|
|
#define rtx int
|
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
#if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
|
1998-09-21 03:22:07 +02:00
|
|
|
#define INVISIBLE_STRUCT_RETURN 1
|
|
|
|
#else
|
|
|
|
#define INVISIBLE_STRUCT_RETURN 0
|
|
|
|
#endif
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* The uninstalled dispatch table. */
|
2002-07-02 21:43:03 +02:00
|
|
|
struct sarray *__objc_uninstalled_dtable = 0; /* !T:MUTEX */
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Two hooks for method forwarding. If either is set, it is invoked to
|
|
|
|
* return a function that performs the real forwarding. If both are
|
|
|
|
* set, the result of __objc_msg_forward2 will be preferred over that
|
|
|
|
* of __objc_msg_forward. If both return NULL or are unset, the
|
|
|
|
* libgcc based functions (__builtin_apply and friends) are used. */
|
2002-07-02 21:43:03 +02:00
|
|
|
IMP (*__objc_msg_forward) (SEL) = NULL;
|
2007-04-09 20:04:50 +02:00
|
|
|
IMP (*__objc_msg_forward2) (id, SEL) = NULL;
|
2001-01-03 09:49:02 +01:00
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Send +initialize to class. */
|
2002-07-02 21:43:03 +02:00
|
|
|
static void __objc_send_initialize (Class);
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
static void __objc_install_dispatch_table_for_class (Class);
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Forward declare some functions. */
|
2002-07-02 21:43:03 +02:00
|
|
|
static void __objc_init_install_dtable (id, SEL);
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
/* Various forwarding functions that are used based upon the
|
|
|
|
return type for the selector.
|
|
|
|
__objc_block_forward for structures.
|
|
|
|
__objc_double_forward for floats/doubles.
|
2010-12-15 01:47:16 +01:00
|
|
|
__objc_word_forward for pointers or types that fit in registers. */
|
2002-07-02 21:43:03 +02:00
|
|
|
static double __objc_double_forward (id, SEL, ...);
|
|
|
|
static id __objc_word_forward (id, SEL, ...);
|
1998-09-21 03:22:07 +02:00
|
|
|
typedef struct { id many[8]; } __big;
|
|
|
|
#if INVISIBLE_STRUCT_RETURN
|
|
|
|
static __big
|
|
|
|
#else
|
|
|
|
static id
|
|
|
|
#endif
|
2002-07-02 21:43:03 +02:00
|
|
|
__objc_block_forward (id, SEL, ...);
|
2010-10-13 00:00:01 +02:00
|
|
|
static struct objc_method * search_for_method_in_hierarchy (Class class, SEL sel);
|
|
|
|
struct objc_method * search_for_method_in_list (struct objc_method_list * list, SEL op);
|
2002-09-12 19:29:58 +02:00
|
|
|
id nil_method (id, SEL);
|
2002-07-02 21:43:03 +02:00
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Given a selector, return the proper forwarding implementation. */
|
2004-05-25 21:10:54 +02:00
|
|
|
inline
|
2002-07-02 21:43:03 +02:00
|
|
|
IMP
|
2007-04-09 20:04:50 +02:00
|
|
|
__objc_get_forward_imp (id rcv, SEL sel)
|
2002-07-02 21:43:03 +02:00
|
|
|
{
|
2010-12-15 01:47:16 +01:00
|
|
|
/* If a custom forwarding hook was registered, try getting a
|
|
|
|
forwarding function from it. There are two forward routine hooks,
|
|
|
|
one that takes the receiver as an argument and one that does
|
|
|
|
not. */
|
2007-04-22 06:44:37 +02:00
|
|
|
if (__objc_msg_forward2)
|
|
|
|
{
|
|
|
|
IMP result;
|
|
|
|
if ((result = __objc_msg_forward2 (rcv, sel)) != NULL)
|
|
|
|
return result;
|
|
|
|
}
|
2002-07-02 21:43:03 +02:00
|
|
|
if (__objc_msg_forward)
|
|
|
|
{
|
|
|
|
IMP result;
|
2002-06-17 19:41:11 +02:00
|
|
|
if ((result = __objc_msg_forward (sel)) != NULL)
|
2002-07-02 21:43:03 +02:00
|
|
|
return result;
|
|
|
|
}
|
2002-06-17 19:41:11 +02:00
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* In all other cases, use the default forwarding functions built
|
|
|
|
using __builtin_apply and friends. */
|
2002-07-02 21:43:03 +02:00
|
|
|
{
|
|
|
|
const char *t = sel->sel_types;
|
2010-12-15 01:47:16 +01:00
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
if (t && (*t == '[' || *t == '(' || *t == '{')
|
|
|
|
#ifdef OBJC_MAX_STRUCT_BY_VALUE
|
|
|
|
&& objc_sizeof_type (t) > OBJC_MAX_STRUCT_BY_VALUE
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
return (IMP)__objc_block_forward;
|
|
|
|
else if (t && (*t == 'f' || *t == 'd'))
|
|
|
|
return (IMP)__objc_double_forward;
|
|
|
|
else
|
|
|
|
return (IMP)__objc_word_forward;
|
|
|
|
}
|
|
|
|
}
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-12-11 20:43:21 +01:00
|
|
|
/* Selectors for +resolveClassMethod: and +resolveInstanceMethod:.
|
|
|
|
These are set up at startup. */
|
|
|
|
static SEL selector_resolveClassMethod = NULL;
|
|
|
|
static SEL selector_resolveInstanceMethod = NULL;
|
|
|
|
|
|
|
|
/* Internal routines use to resolve a class method using
|
|
|
|
+resolveClassMethod:. 'class' is always a non-Nil class (*not* a
|
|
|
|
meta-class), and 'sel' is the selector that we are trying to
|
|
|
|
resolve. This must be called when class is not Nil, and the
|
|
|
|
dispatch table for class methods has already been installed.
|
|
|
|
|
|
|
|
This routine tries to call +resolveClassMethod: to give an
|
|
|
|
opportunity to resolve the method. If +resolveClassMethod: returns
|
|
|
|
YES, it tries looking up the method again, and if found, it returns
|
|
|
|
it. Else, it returns NULL. */
|
|
|
|
static inline
|
|
|
|
IMP
|
|
|
|
__objc_resolve_class_method (Class class, SEL sel)
|
|
|
|
{
|
|
|
|
/* We need to lookup +resolveClassMethod:. */
|
|
|
|
BOOL (*resolveMethodIMP) (id, SEL, SEL);
|
|
|
|
|
|
|
|
/* The dispatch table for class methods is already installed and we
|
|
|
|
don't want any forwarding to happen when looking up this method,
|
|
|
|
so we just look it up directly. Note that if 'sel' is precisely
|
|
|
|
+resolveClassMethod:, this would look it up yet again and find
|
|
|
|
nothing. That's no problem and there's no recursion. */
|
|
|
|
resolveMethodIMP = (BOOL (*) (id, SEL, SEL))sarray_get_safe
|
|
|
|
(class->class_pointer->dtable, (size_t) selector_resolveClassMethod->sel_id);
|
|
|
|
|
|
|
|
if (resolveMethodIMP && resolveMethodIMP ((id)class, selector_resolveClassMethod, sel))
|
|
|
|
{
|
|
|
|
/* +resolveClassMethod: returned YES. Look the method up again.
|
|
|
|
We already know the dtable is installed. */
|
|
|
|
|
|
|
|
/* TODO: There is the case where +resolveClassMethod: is buggy
|
|
|
|
and returned YES without actually adding the method. We
|
|
|
|
could maybe print an error message. */
|
|
|
|
return sarray_get_safe (class->class_pointer->dtable, (size_t) sel->sel_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Internal routines use to resolve a instance method using
|
|
|
|
+resolveInstanceMethod:. 'class' is always a non-Nil class, and
|
|
|
|
'sel' is the selector that we are trying to resolve. This must be
|
|
|
|
called when class is not Nil, and the dispatch table for instance
|
|
|
|
methods has already been installed.
|
|
|
|
|
|
|
|
This routine tries to call +resolveInstanceMethod: to give an
|
|
|
|
opportunity to resolve the method. If +resolveInstanceMethod:
|
|
|
|
returns YES, it tries looking up the method again, and if found, it
|
|
|
|
returns it. Else, it returns NULL. */
|
|
|
|
static inline
|
|
|
|
IMP
|
|
|
|
__objc_resolve_instance_method (Class class, SEL sel)
|
|
|
|
{
|
|
|
|
/* We need to lookup +resolveInstanceMethod:. */
|
|
|
|
BOOL (*resolveMethodIMP) (id, SEL, SEL);
|
|
|
|
|
|
|
|
/* The dispatch table for class methods may not be already installed
|
|
|
|
so we have to install it if needed. */
|
|
|
|
resolveMethodIMP = sarray_get_safe (class->class_pointer->dtable,
|
|
|
|
(size_t) selector_resolveInstanceMethod->sel_id);
|
|
|
|
if (resolveMethodIMP == 0)
|
|
|
|
{
|
|
|
|
/* Try again after installing the dtable. */
|
|
|
|
if (class->class_pointer->dtable == __objc_uninstalled_dtable)
|
|
|
|
{
|
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
|
|
|
if (class->class_pointer->dtable == __objc_uninstalled_dtable)
|
|
|
|
__objc_install_dispatch_table_for_class (class->class_pointer);
|
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
|
|
|
}
|
|
|
|
resolveMethodIMP = sarray_get_safe (class->class_pointer->dtable,
|
|
|
|
(size_t) selector_resolveInstanceMethod->sel_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resolveMethodIMP && resolveMethodIMP ((id)class, selector_resolveInstanceMethod, sel))
|
|
|
|
{
|
|
|
|
/* +resolveInstanceMethod: returned YES. Look the method up
|
|
|
|
again. We already know the dtable is installed. */
|
|
|
|
|
|
|
|
/* TODO: There is the case where +resolveInstanceMethod: is
|
|
|
|
buggy and returned YES without actually adding the method.
|
|
|
|
We could maybe print an error message. */
|
|
|
|
return sarray_get_safe (class->dtable, (size_t) sel->sel_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Temporary definition until we include objc/runtime.h. */
|
|
|
|
objc_EXPORT Class objc_lookupClass (const char *name);
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Given a class and selector, return the selector's
|
|
|
|
implementation. */
|
2010-09-26 21:03:27 +02:00
|
|
|
inline
|
1998-09-21 03:22:07 +02:00
|
|
|
IMP
|
|
|
|
get_imp (Class class, SEL sel)
|
|
|
|
{
|
2003-07-10 11:24:51 +02:00
|
|
|
/* In a vanilla implementation we would first check if the dispatch
|
|
|
|
table is installed. Here instead, to get more speed in the
|
|
|
|
standard case (that the dispatch table is installed) we first try
|
|
|
|
to get the imp using brute force. Only if that fails, we do what
|
|
|
|
we should have been doing from the very beginning, that is, check
|
|
|
|
if the dispatch table needs to be installed, install it if it's
|
|
|
|
not installed, and retrieve the imp from the table if it's
|
|
|
|
installed. */
|
2002-07-02 21:43:03 +02:00
|
|
|
void *res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
|
1998-09-21 03:22:07 +02:00
|
|
|
if (res == 0)
|
|
|
|
{
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Not a valid method. */
|
2002-07-02 21:43:03 +02:00
|
|
|
if (class->dtable == __objc_uninstalled_dtable)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2010-12-15 01:47:16 +01:00
|
|
|
/* The dispatch table needs to be installed. */
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
2003-07-10 11:24:51 +02:00
|
|
|
|
|
|
|
/* Double-checked locking pattern: Check
|
|
|
|
__objc_uninstalled_dtable again in case another thread
|
|
|
|
installed the dtable while we were waiting for the lock
|
|
|
|
to be released. */
|
|
|
|
if (class->dtable == __objc_uninstalled_dtable)
|
|
|
|
{
|
|
|
|
__objc_install_dispatch_table_for_class (class);
|
|
|
|
}
|
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Call ourselves with the installed dispatch table and get
|
|
|
|
the real method. */
|
2002-07-02 21:43:03 +02:00
|
|
|
res = get_imp (class, sel);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-07-10 11:24:51 +02:00
|
|
|
/* The dispatch table has been installed. */
|
|
|
|
|
|
|
|
/* Get the method from the dispatch table (we try to get it
|
|
|
|
again in case another thread has installed the dtable just
|
|
|
|
after we invoked sarray_get_safe, but before we checked
|
2010-12-15 01:47:16 +01:00
|
|
|
class->dtable == __objc_uninstalled_dtable). */
|
2003-07-10 11:24:51 +02:00
|
|
|
res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
|
|
|
|
if (res == 0)
|
|
|
|
{
|
|
|
|
/* The dispatch table has been installed, and the method
|
|
|
|
is not in the dispatch table. So the method just
|
2010-12-11 20:43:21 +01:00
|
|
|
doesn't exist for the class. */
|
|
|
|
|
|
|
|
/* Try going through the +resolveClassMethod: or
|
|
|
|
+resolveInstanceMethod: process. */
|
|
|
|
if (CLS_ISMETA (class))
|
|
|
|
{
|
|
|
|
/* We have the meta class, but we need to invoke the
|
|
|
|
+resolveClassMethod: method on the class. So, we
|
|
|
|
need to obtain the class from the meta class,
|
|
|
|
which we do using the fact that both the class
|
|
|
|
and the meta-class have the same name. */
|
|
|
|
Class realClass = objc_lookupClass (class->name);
|
|
|
|
if (realClass)
|
|
|
|
res = __objc_resolve_class_method (realClass, sel);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res = __objc_resolve_instance_method (class, sel);
|
|
|
|
|
|
|
|
if (res == 0)
|
|
|
|
{
|
|
|
|
/* If that fails, then return the forwarding
|
2010-12-15 01:47:16 +01:00
|
|
|
implementation. We don't know the receiver (only
|
|
|
|
its class), so we have to pass 'nil' as the first
|
2010-12-11 20:43:21 +01:00
|
|
|
argument. Passing the class as first argument is
|
2010-12-15 01:47:16 +01:00
|
|
|
wrong because the class is not the receiver; it
|
|
|
|
can result in us calling a class method when we
|
|
|
|
want an instance method of the same name. */
|
2010-12-11 20:43:21 +01:00
|
|
|
res = __objc_get_forward_imp (nil, sel);
|
|
|
|
}
|
2003-07-10 11:24:51 +02:00
|
|
|
}
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-10-13 00:00:01 +02:00
|
|
|
/* The new name of get_imp(). */
|
|
|
|
IMP
|
|
|
|
class_getMethodImplementation (Class class_, SEL selector)
|
|
|
|
{
|
|
|
|
if (class_ == Nil || selector == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* get_imp is inlined, so we're good. */
|
|
|
|
return get_imp (class_, selector);
|
|
|
|
}
|
|
|
|
|
2010-10-10 00:22:23 +02:00
|
|
|
/* Given a method, return its implementation. */
|
|
|
|
IMP
|
2010-10-13 00:00:01 +02:00
|
|
|
method_get_imp (struct objc_method * method)
|
2010-10-10 00:22:23 +02:00
|
|
|
{
|
2010-10-13 00:00:01 +02:00
|
|
|
return (method != (struct objc_method *)0) ? method->method_imp : (IMP)0;
|
2010-10-10 00:22:23 +02:00
|
|
|
}
|
|
|
|
|
1998-09-21 03:22:07 +02:00
|
|
|
/* Query if an object can respond to a selector, returns YES if the
|
2010-12-15 01:47:16 +01:00
|
|
|
object implements the selector otherwise NO. Does not check if the
|
|
|
|
method can be forwarded. */
|
2004-05-25 21:10:54 +02:00
|
|
|
inline
|
1998-09-21 03:22:07 +02:00
|
|
|
BOOL
|
|
|
|
__objc_responds_to (id object, SEL sel)
|
|
|
|
{
|
2002-07-02 21:43:03 +02:00
|
|
|
void *res;
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Install dispatch table if need be. */
|
1998-09-21 03:22:07 +02:00
|
|
|
if (object->class_pointer->dtable == __objc_uninstalled_dtable)
|
|
|
|
{
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
2003-07-10 11:24:51 +02:00
|
|
|
if (object->class_pointer->dtable == __objc_uninstalled_dtable)
|
|
|
|
{
|
|
|
|
__objc_install_dispatch_table_for_class (object->class_pointer);
|
|
|
|
}
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Get the method from the dispatch table. */
|
1998-09-21 03:22:07 +02:00
|
|
|
res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
|
|
|
|
return (res != 0);
|
|
|
|
}
|
|
|
|
|
2010-10-13 00:00:01 +02:00
|
|
|
BOOL
|
|
|
|
class_respondsToSelector (Class class_, SEL selector)
|
|
|
|
{
|
|
|
|
void *res;
|
|
|
|
|
|
|
|
if (class_ == Nil || selector == NULL)
|
|
|
|
return NO;
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Install dispatch table if need be. */
|
2010-10-13 00:00:01 +02:00
|
|
|
if (class_->dtable == __objc_uninstalled_dtable)
|
|
|
|
{
|
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
|
|
|
if (class_->dtable == __objc_uninstalled_dtable)
|
|
|
|
{
|
|
|
|
__objc_install_dispatch_table_for_class (class_);
|
|
|
|
}
|
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
|
|
|
}
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Get the method from the dispatch table. */
|
2010-10-13 00:00:01 +02:00
|
|
|
res = sarray_get_safe (class_->dtable, (size_t) selector->sel_id);
|
|
|
|
return (res != 0);
|
|
|
|
}
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* This is the lookup function. All entries in the table are either a
|
1998-09-21 03:22:07 +02:00
|
|
|
valid method *or* zero. If zero then either the dispatch table
|
2010-12-15 01:47:16 +01:00
|
|
|
needs to be installed or it doesn't exist and forwarding is
|
|
|
|
attempted. */
|
1998-09-21 03:22:07 +02:00
|
|
|
IMP
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_msg_lookup (id receiver, SEL op)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
|
|
|
IMP result;
|
2002-07-02 21:43:03 +02:00
|
|
|
if (receiver)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
|
|
|
result = sarray_get_safe (receiver->class_pointer->dtable,
|
|
|
|
(sidx)op->sel_id);
|
|
|
|
if (result == 0)
|
|
|
|
{
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Not a valid method. */
|
2002-07-02 21:43:03 +02:00
|
|
|
if (receiver->class_pointer->dtable == __objc_uninstalled_dtable)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2010-12-15 01:47:16 +01:00
|
|
|
/* The dispatch table needs to be installed. This
|
|
|
|
happens on the very first method call to the
|
|
|
|
class. */
|
2002-07-02 21:43:03 +02:00
|
|
|
__objc_init_install_dtable (receiver, op);
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Get real method for this in newly installed
|
|
|
|
dtable. */
|
2002-07-02 21:43:03 +02:00
|
|
|
result = get_imp (receiver->class_pointer, op);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-07-10 11:24:51 +02:00
|
|
|
/* The dispatch table has been installed. Check again
|
|
|
|
if the method exists (just in case the dispatch table
|
|
|
|
has been installed by another thread after we did the
|
2010-12-15 01:47:16 +01:00
|
|
|
previous check that the method exists). */
|
2003-07-10 11:24:51 +02:00
|
|
|
result = sarray_get_safe (receiver->class_pointer->dtable,
|
|
|
|
(sidx)op->sel_id);
|
|
|
|
if (result == 0)
|
|
|
|
{
|
2010-12-11 20:43:21 +01:00
|
|
|
/* Try going through the +resolveClassMethod: or
|
|
|
|
+resolveInstanceMethod: process. */
|
|
|
|
if (CLS_ISMETA (receiver->class_pointer))
|
|
|
|
result = __objc_resolve_class_method ((Class)receiver, op);
|
|
|
|
else
|
|
|
|
result = __objc_resolve_instance_method (receiver->class_pointer,
|
|
|
|
op);
|
|
|
|
|
|
|
|
if (result == 0)
|
|
|
|
{
|
|
|
|
/* If the method still just doesn't exist for
|
2010-12-15 01:47:16 +01:00
|
|
|
the class, attempt to forward the method. */
|
2010-12-11 20:43:21 +01:00
|
|
|
result = __objc_get_forward_imp (receiver, op);
|
|
|
|
}
|
2003-07-10 11:24:51 +02:00
|
|
|
}
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
2002-09-12 19:29:58 +02:00
|
|
|
return (IMP)nil_method;
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IMP
|
2010-12-15 20:47:18 +01:00
|
|
|
objc_msg_lookup_super (struct objc_super *super, SEL sel)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
|
|
|
if (super->self)
|
2010-12-16 23:52:48 +01:00
|
|
|
return get_imp (super->super_class, sel);
|
1998-09-21 03:22:07 +02:00
|
|
|
else
|
2002-09-12 19:29:58 +02:00
|
|
|
return (IMP)nil_method;
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
2010-12-16 23:52:48 +01:00
|
|
|
/* Temporarily defined here until objc_msg_sendv() goes away. */
|
|
|
|
char *method_get_first_argument (struct objc_method *,
|
|
|
|
arglist_t argframe,
|
|
|
|
const char **type);
|
|
|
|
char *method_get_next_argument (arglist_t argframe,
|
|
|
|
const char **type);
|
|
|
|
int method_get_sizeof_arguments (struct objc_method *);
|
|
|
|
|
|
|
|
struct objc_method *
|
|
|
|
class_get_instance_method (Class class, SEL op);
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
retval_t
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_msg_sendv (id object, SEL op, arglist_t arg_frame)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2010-12-16 23:52:48 +01:00
|
|
|
struct objc_method *m = class_get_instance_method (object->class_pointer, op);
|
1998-09-21 03:22:07 +02:00
|
|
|
const char *type;
|
2002-07-02 21:43:03 +02:00
|
|
|
*((id *) method_get_first_argument (m, arg_frame, &type)) = object;
|
|
|
|
*((SEL *) method_get_next_argument (arg_frame, &type)) = op;
|
|
|
|
return __builtin_apply ((apply_t) m->method_imp,
|
|
|
|
arg_frame,
|
|
|
|
method_get_sizeof_arguments (m));
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-07-02 21:43:03 +02:00
|
|
|
__objc_init_dispatch_tables ()
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2002-07-02 21:43:03 +02:00
|
|
|
__objc_uninstalled_dtable = sarray_new (200, 0);
|
2010-12-11 20:43:21 +01:00
|
|
|
|
|
|
|
/* TODO: It would be cool to register typed selectors here. */
|
2010-12-16 23:52:48 +01:00
|
|
|
selector_resolveClassMethod = sel_registerName ("resolveClassMethod:");
|
|
|
|
selector_resolveInstanceMethod =sel_registerName ("resolveInstanceMethod:");
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* This function is called by objc_msg_lookup when the dispatch table
|
|
|
|
needs to be installed; thus it is called once for each class,
|
|
|
|
namely when the very first message is sent to it. */
|
1998-09-21 03:22:07 +02:00
|
|
|
static void
|
2002-07-02 21:43:03 +02:00
|
|
|
__objc_init_install_dtable (id receiver, SEL op __attribute__ ((__unused__)))
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2003-07-10 11:24:51 +02:00
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* This may happen, if the programmer has taken the address of a
|
|
|
|
method before the dtable was initialized... too bad for him! */
|
2002-07-02 21:43:03 +02:00
|
|
|
if (receiver->class_pointer->dtable != __objc_uninstalled_dtable)
|
2003-07-10 11:24:51 +02:00
|
|
|
{
|
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
if (CLS_ISCLASS (receiver->class_pointer))
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2010-12-15 01:47:16 +01:00
|
|
|
/* receiver is an ordinary object. */
|
2002-07-02 21:43:03 +02:00
|
|
|
assert (CLS_ISCLASS (receiver->class_pointer));
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Install instance methods table. */
|
1998-09-21 03:22:07 +02:00
|
|
|
__objc_install_dispatch_table_for_class (receiver->class_pointer);
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Call +initialize -- this will in turn install the factory
|
|
|
|
dispatch table if not already done. :-) */
|
2002-07-02 21:43:03 +02:00
|
|
|
__objc_send_initialize (receiver->class_pointer);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-15 01:47:16 +01:00
|
|
|
/* receiver is a class object. */
|
2002-07-02 21:43:03 +02:00
|
|
|
assert (CLS_ISCLASS ((Class)receiver));
|
|
|
|
assert (CLS_ISMETA (receiver->class_pointer));
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Install real dtable for factory methods. */
|
1998-09-21 03:22:07 +02:00
|
|
|
__objc_install_dispatch_table_for_class (receiver->class_pointer);
|
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
__objc_send_initialize ((Class)receiver);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Install dummy table for class which causes the first message to
|
2010-12-15 01:47:16 +01:00
|
|
|
that class (or instances hereof) to be initialized properly. */
|
1998-09-21 03:22:07 +02:00
|
|
|
void
|
2002-07-02 21:43:03 +02:00
|
|
|
__objc_install_premature_dtable (Class class)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2002-07-02 21:43:03 +02:00
|
|
|
assert (__objc_uninstalled_dtable);
|
1998-09-21 03:22:07 +02:00
|
|
|
class->dtable = __objc_uninstalled_dtable;
|
|
|
|
}
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Send +initialize to class if not already done. */
|
1998-09-21 03:22:07 +02:00
|
|
|
static void
|
2002-07-02 21:43:03 +02:00
|
|
|
__objc_send_initialize (Class class)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2010-12-15 01:47:16 +01:00
|
|
|
/* This *must* be a class object. */
|
2002-07-02 21:43:03 +02:00
|
|
|
assert (CLS_ISCLASS (class));
|
|
|
|
assert (! CLS_ISMETA (class));
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
if (! CLS_ISINITIALIZED (class))
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2002-07-02 21:43:03 +02:00
|
|
|
CLS_SETINITIALIZED (class);
|
|
|
|
CLS_SETINITIALIZED (class->class_pointer);
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Create the garbage collector type memory description. */
|
1998-09-21 03:22:07 +02:00
|
|
|
__objc_generate_gc_type_description (class);
|
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
if (class->super_class)
|
|
|
|
__objc_send_initialize (class->super_class);
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
{
|
2010-12-16 23:52:48 +01:00
|
|
|
SEL op = sel_registerName ("initialize");
|
2010-12-15 01:47:16 +01:00
|
|
|
IMP imp = 0;
|
2010-10-13 00:00:01 +02:00
|
|
|
struct objc_method_list * method_list = class->class_pointer->methods;
|
2010-12-15 01:47:16 +01:00
|
|
|
|
|
|
|
while (method_list)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct objc_method * method;
|
|
|
|
|
|
|
|
for (i = 0; i < method_list->method_count; i++)
|
|
|
|
{
|
|
|
|
method = &(method_list->method_list[i]);
|
|
|
|
if (method->method_name
|
|
|
|
&& method->method_name->sel_id == op->sel_id)
|
|
|
|
{
|
|
|
|
imp = method->method_imp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (imp)
|
|
|
|
break;
|
|
|
|
|
|
|
|
method_list = method_list->method_next;
|
|
|
|
}
|
1998-09-21 03:22:07 +02:00
|
|
|
if (imp)
|
2010-12-15 01:47:16 +01:00
|
|
|
(*imp) ((id) class, op);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Walk on the methods list of class and install the methods in the
|
|
|
|
reverse order of the lists. Since methods added by categories are
|
|
|
|
before the methods of class in the methods list, this allows
|
|
|
|
categories to substitute methods declared in class. However if
|
|
|
|
more than one category replaces the same method nothing is
|
|
|
|
guaranteed about what method will be used. Assumes that
|
|
|
|
__objc_runtime_mutex is locked down. */
|
1998-09-21 03:22:07 +02:00
|
|
|
static void
|
2010-10-13 00:00:01 +02:00
|
|
|
__objc_install_methods_in_dtable (Class class, struct objc_method_list * method_list)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
|
|
|
int i;
|
2010-12-15 01:47:16 +01:00
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
if (! method_list)
|
1998-09-21 03:22:07 +02:00
|
|
|
return;
|
2010-12-15 01:47:16 +01:00
|
|
|
|
1998-09-21 03:22:07 +02:00
|
|
|
if (method_list->method_next)
|
|
|
|
__objc_install_methods_in_dtable (class, method_list->method_next);
|
2010-12-15 01:47:16 +01:00
|
|
|
|
1998-09-21 03:22:07 +02:00
|
|
|
for (i = 0; i < method_list->method_count; i++)
|
|
|
|
{
|
2010-10-13 00:00:01 +02:00
|
|
|
struct objc_method * method = &(method_list->method_list[i]);
|
1998-09-21 03:22:07 +02:00
|
|
|
sarray_at_put_safe (class->dtable,
|
|
|
|
(sidx) method->method_name->sel_id,
|
|
|
|
method->method_imp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Assumes that __objc_runtime_mutex is locked down. */
|
1998-09-21 03:22:07 +02:00
|
|
|
static void
|
|
|
|
__objc_install_dispatch_table_for_class (Class class)
|
|
|
|
{
|
|
|
|
Class super;
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* If the class has not yet had its class links resolved, we must
|
|
|
|
re-compute all class links. */
|
2002-07-02 21:43:03 +02:00
|
|
|
if (! CLS_ISRESOLV (class))
|
|
|
|
__objc_resolve_class_links ();
|
2010-12-15 01:47:16 +01:00
|
|
|
|
1998-09-21 03:22:07 +02:00
|
|
|
super = class->super_class;
|
|
|
|
|
|
|
|
if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
|
|
|
|
__objc_install_dispatch_table_for_class (super);
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Allocate dtable if necessary. */
|
1998-09-21 03:22:07 +02:00
|
|
|
if (super == 0)
|
|
|
|
{
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
class->dtable = sarray_new (__objc_selector_max_index, 0);
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
class->dtable = sarray_lazy_copy (super->dtable);
|
|
|
|
|
|
|
|
__objc_install_methods_in_dtable (class, class->methods);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
__objc_update_dispatch_table_for_class (Class class)
|
|
|
|
{
|
|
|
|
Class next;
|
|
|
|
struct sarray *arr;
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Not yet installed -- skip it. */
|
1998-09-21 03:22:07 +02:00
|
|
|
if (class->dtable == __objc_uninstalled_dtable)
|
|
|
|
return;
|
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
arr = class->dtable;
|
|
|
|
__objc_install_premature_dtable (class); /* someone might require it... */
|
|
|
|
sarray_free (arr); /* release memory */
|
2010-12-15 01:47:16 +01:00
|
|
|
|
|
|
|
/* Could have been lazy... */
|
1998-09-21 03:22:07 +02:00
|
|
|
__objc_install_dispatch_table_for_class (class);
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
if (class->subclass_list) /* Traverse subclasses. */
|
1998-09-21 03:22:07 +02:00
|
|
|
for (next = class->subclass_list; next; next = next->sibling_class)
|
|
|
|
__objc_update_dispatch_table_for_class (next);
|
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function adds a method list to a class. This function is
|
|
|
|
typically called by another function specific to the run-time. As
|
|
|
|
such this function does not worry about thread safe issues.
|
|
|
|
|
|
|
|
This one is only called for categories. Class objects have their
|
|
|
|
methods installed right away, and their selectors are made into
|
2010-12-15 01:47:16 +01:00
|
|
|
SEL's by the function __objc_register_selectors_from_class. */
|
1998-09-21 03:22:07 +02:00
|
|
|
void
|
2010-10-13 00:00:01 +02:00
|
|
|
class_add_method_list (Class class, struct objc_method_list * list)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
|
|
|
/* Passing of a linked list is not allowed. Do multiple calls. */
|
2002-07-02 21:43:03 +02:00
|
|
|
assert (! list->method_next);
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2004-05-25 21:10:54 +02:00
|
|
|
__objc_register_selectors_from_list(list);
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
/* Add the methods to the class's method list. */
|
|
|
|
list->method_next = class->methods;
|
|
|
|
class->methods = list;
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Update the dispatch table of class. */
|
1998-09-21 03:22:07 +02:00
|
|
|
__objc_update_dispatch_table_for_class (class);
|
|
|
|
}
|
|
|
|
|
2010-10-13 00:00:01 +02:00
|
|
|
struct objc_method *
|
2002-07-02 21:43:03 +02:00
|
|
|
class_get_instance_method (Class class, SEL op)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2002-07-02 21:43:03 +02:00
|
|
|
return search_for_method_in_hierarchy (class, op);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
2010-10-13 00:00:01 +02:00
|
|
|
struct objc_method *
|
2002-07-02 21:43:03 +02:00
|
|
|
class_get_class_method (MetaClass class, SEL op)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2002-07-02 21:43:03 +02:00
|
|
|
return search_for_method_in_hierarchy (class, op);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
2010-10-13 00:00:01 +02:00
|
|
|
struct objc_method *
|
|
|
|
class_getInstanceMethod (Class class_, SEL selector)
|
|
|
|
{
|
2010-12-11 20:43:21 +01:00
|
|
|
struct objc_method *m;
|
|
|
|
|
2010-10-13 00:00:01 +02:00
|
|
|
if (class_ == Nil || selector == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2010-12-11 20:43:21 +01:00
|
|
|
m = search_for_method_in_hierarchy (class_, selector);
|
|
|
|
if (m)
|
|
|
|
return m;
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Try going through +resolveInstanceMethod:, and do the search
|
|
|
|
again if successful. */
|
2010-12-11 20:43:21 +01:00
|
|
|
if (__objc_resolve_instance_method (class_, selector))
|
|
|
|
return search_for_method_in_hierarchy (class_, selector);
|
|
|
|
|
|
|
|
return NULL;
|
2010-10-13 00:00:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct objc_method *
|
|
|
|
class_getClassMethod (Class class_, SEL selector)
|
|
|
|
{
|
2010-12-11 20:43:21 +01:00
|
|
|
struct objc_method *m;
|
|
|
|
|
2010-10-13 00:00:01 +02:00
|
|
|
if (class_ == Nil || selector == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2010-12-11 20:43:21 +01:00
|
|
|
m = search_for_method_in_hierarchy (class_->class_pointer,
|
|
|
|
selector);
|
|
|
|
if (m)
|
|
|
|
return m;
|
|
|
|
|
|
|
|
/* Try going through +resolveClassMethod:, and do the search again
|
|
|
|
if successful. */
|
|
|
|
if (__objc_resolve_class_method (class_, selector))
|
|
|
|
return search_for_method_in_hierarchy (class_->class_pointer,
|
|
|
|
selector);
|
|
|
|
|
|
|
|
return NULL;
|
2010-10-13 00:00:01 +02:00
|
|
|
}
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-10-16 23:26:59 +02:00
|
|
|
BOOL
|
|
|
|
class_addMethod (Class class_, SEL selector, IMP implementation,
|
|
|
|
const char *method_types)
|
|
|
|
{
|
|
|
|
struct objc_method_list *method_list;
|
|
|
|
struct objc_method *method;
|
|
|
|
const char *method_name;
|
|
|
|
|
|
|
|
if (class_ == Nil || selector == NULL || implementation == NULL
|
|
|
|
|| method_types == NULL || (strcmp (method_types, "") == 0))
|
|
|
|
return NO;
|
|
|
|
|
2010-12-16 23:52:48 +01:00
|
|
|
method_name = sel_getName (selector);
|
2010-10-16 23:26:59 +02:00
|
|
|
if (method_name == NULL)
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
method_list = (struct objc_method_list *)objc_calloc (1, sizeof (struct objc_method_list));
|
|
|
|
method_list->method_count = 1;
|
|
|
|
|
|
|
|
method = &(method_list->method_list[0]);
|
|
|
|
method->method_name = objc_malloc (strlen (method_name) + 1);
|
|
|
|
strcpy ((char *)method->method_name, method_name);
|
|
|
|
|
|
|
|
method->method_types = objc_malloc (strlen (method_types) + 1);
|
|
|
|
strcpy ((char *)method->method_types, method_types);
|
|
|
|
|
|
|
|
method->method_imp = implementation;
|
|
|
|
|
|
|
|
if (CLS_IS_IN_CONSTRUCTION (class_))
|
|
|
|
{
|
|
|
|
/* We only need to add the method to the list. It will be
|
|
|
|
registered with the runtime when the class pair is registered
|
|
|
|
(if ever). */
|
|
|
|
method_list->method_next = class_->methods;
|
|
|
|
class_->methods = method_list;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Add the method to a live class. */
|
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
|
|
|
class_add_method_list (class_, method_list);
|
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Temporarily, until we include objc/runtime.h. */
|
|
|
|
extern IMP
|
|
|
|
method_setImplementation (struct objc_method * method, IMP implementation);
|
|
|
|
|
|
|
|
IMP
|
|
|
|
class_replaceMethod (Class class_, SEL selector, IMP implementation,
|
|
|
|
const char *method_types)
|
|
|
|
{
|
|
|
|
struct objc_method * method;
|
|
|
|
|
|
|
|
if (class_ == Nil || selector == NULL || implementation == NULL
|
|
|
|
|| method_types == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
method = search_for_method_in_hierarchy (class_, selector);
|
|
|
|
|
|
|
|
if (method)
|
|
|
|
{
|
|
|
|
return method_setImplementation (method, implementation);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
class_addMethod (class_, selector, implementation, method_types);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Search for a method starting from the current class up its
|
|
|
|
hierarchy. Return a pointer to the method's method structure if
|
|
|
|
found. NULL otherwise. */
|
2010-10-13 00:00:01 +02:00
|
|
|
static struct objc_method *
|
1998-09-21 03:22:07 +02:00
|
|
|
search_for_method_in_hierarchy (Class cls, SEL sel)
|
|
|
|
{
|
2010-10-13 00:00:01 +02:00
|
|
|
struct objc_method * method = NULL;
|
1998-09-21 03:22:07 +02:00
|
|
|
Class class;
|
|
|
|
|
|
|
|
if (! sel_is_mapped (sel))
|
|
|
|
return NULL;
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Scan the method list of the class. If the method isn't found in
|
|
|
|
the list then step to its super class. */
|
1998-09-21 03:22:07 +02:00
|
|
|
for (class = cls; ((! method) && class); class = class->super_class)
|
|
|
|
method = search_for_method_in_list (class->methods, sel);
|
|
|
|
|
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Given a linked list of method and a method's name. Search for the
|
|
|
|
named method's method structure. Return a pointer to the method's
|
|
|
|
method structure if found. NULL otherwise. */
|
2010-10-13 00:00:01 +02:00
|
|
|
struct objc_method *
|
|
|
|
search_for_method_in_list (struct objc_method_list * list, SEL op)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2010-10-13 00:00:01 +02:00
|
|
|
struct objc_method_list * method_list = list;
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
if (! sel_is_mapped (op))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* If not found then we'll search the list. */
|
|
|
|
while (method_list)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Search the method list. */
|
|
|
|
for (i = 0; i < method_list->method_count; ++i)
|
|
|
|
{
|
2010-10-13 00:00:01 +02:00
|
|
|
struct objc_method * method = &method_list->method_list[i];
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
if (method->method_name)
|
|
|
|
if (method->method_name->sel_id == op->sel_id)
|
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The method wasn't found. Follow the link to the next list of
|
|
|
|
methods. */
|
|
|
|
method_list = method_list->method_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static retval_t __objc_forward (id object, SEL sel, arglist_t args);
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Forwarding pointers/integers through the normal registers. */
|
1998-09-21 03:22:07 +02:00
|
|
|
static id
|
|
|
|
__objc_word_forward (id rcv, SEL op, ...)
|
|
|
|
{
|
|
|
|
void *args, *res;
|
|
|
|
|
|
|
|
args = __builtin_apply_args ();
|
|
|
|
res = __objc_forward (rcv, op, args);
|
|
|
|
if (res)
|
|
|
|
__builtin_return (res);
|
|
|
|
else
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Specific routine for forwarding floats/double because of
|
2010-12-15 01:47:16 +01:00
|
|
|
architectural differences on some processors. i386s for example
|
|
|
|
which uses a floating point stack versus general registers for
|
|
|
|
floating point numbers. This forward routine makes sure that GCC
|
|
|
|
restores the proper return values. */
|
1998-09-21 03:22:07 +02:00
|
|
|
static double
|
|
|
|
__objc_double_forward (id rcv, SEL op, ...)
|
|
|
|
{
|
|
|
|
void *args, *res;
|
|
|
|
|
|
|
|
args = __builtin_apply_args ();
|
|
|
|
res = __objc_forward (rcv, op, args);
|
|
|
|
__builtin_return (res);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if INVISIBLE_STRUCT_RETURN
|
|
|
|
static __big
|
|
|
|
#else
|
|
|
|
static id
|
|
|
|
#endif
|
|
|
|
__objc_block_forward (id rcv, SEL op, ...)
|
|
|
|
{
|
|
|
|
void *args, *res;
|
|
|
|
|
|
|
|
args = __builtin_apply_args ();
|
|
|
|
res = __objc_forward (rcv, op, args);
|
|
|
|
if (res)
|
|
|
|
__builtin_return (res);
|
|
|
|
else
|
|
|
|
#if INVISIBLE_STRUCT_RETURN
|
|
|
|
return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
|
|
|
|
#else
|
|
|
|
return nil;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* This function is installed in the dispatch table for all methods
|
|
|
|
which are not implemented. Thus, it is called when a selector is
|
|
|
|
not recognized. */
|
1998-09-21 03:22:07 +02:00
|
|
|
static retval_t
|
|
|
|
__objc_forward (id object, SEL sel, arglist_t args)
|
|
|
|
{
|
|
|
|
IMP imp;
|
|
|
|
static SEL frwd_sel = 0; /* !T:SAFE2 */
|
|
|
|
SEL err_sel;
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* First try if the object understands forward::. */
|
2002-07-02 21:43:03 +02:00
|
|
|
if (! frwd_sel)
|
|
|
|
frwd_sel = sel_get_any_uid ("forward::");
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
if (__objc_responds_to (object, frwd_sel))
|
|
|
|
{
|
2002-07-02 21:43:03 +02:00
|
|
|
imp = get_imp (object->class_pointer, frwd_sel);
|
|
|
|
return (*imp) (object, frwd_sel, sel, args);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* If the object recognizes the doesNotRecognize: method then we're
|
|
|
|
going to send it. */
|
1998-09-21 03:22:07 +02:00
|
|
|
err_sel = sel_get_any_uid ("doesNotRecognize:");
|
|
|
|
if (__objc_responds_to (object, err_sel))
|
|
|
|
{
|
|
|
|
imp = get_imp (object->class_pointer, err_sel);
|
|
|
|
return (*imp) (object, err_sel, sel);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The object doesn't recognize the method. Check for responding to
|
2010-12-15 01:47:16 +01:00
|
|
|
error:. If it does then sent it. */
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2010-12-16 23:52:48 +01:00
|
|
|
char msg[256 + strlen ((const char *) sel_getName (sel))
|
2002-07-02 21:43:03 +02:00
|
|
|
+ strlen ((const char *) object->class_pointer->name)];
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
sprintf (msg, "(%s) %s does not recognize %s",
|
2002-07-02 21:43:03 +02:00
|
|
|
(CLS_ISMETA (object->class_pointer)
|
1998-09-21 03:22:07 +02:00
|
|
|
? "class"
|
|
|
|
: "instance" ),
|
2010-12-16 23:52:48 +01:00
|
|
|
object->class_pointer->name, sel_getName (sel));
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-09-12 02:43:15 +02:00
|
|
|
/* TODO: support for error: is surely deprecated ? */
|
1998-09-21 03:22:07 +02:00
|
|
|
err_sel = sel_get_any_uid ("error:");
|
|
|
|
if (__objc_responds_to (object, err_sel))
|
|
|
|
{
|
|
|
|
imp = get_imp (object->class_pointer, err_sel);
|
|
|
|
return (*imp) (object, sel_get_any_uid ("error:"), msg);
|
|
|
|
}
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* The object doesn't respond to doesNotRecognize: or error:;
|
|
|
|
Therefore, a default action is taken. */
|
2010-09-12 02:43:15 +02:00
|
|
|
_objc_abort ("%s\n", msg);
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-07-02 21:43:03 +02:00
|
|
|
__objc_print_dtable_stats ()
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
|
|
|
int total = 0;
|
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
|
|
|
|
#ifdef OBJC_SPARSE2
|
2002-07-02 21:43:03 +02:00
|
|
|
printf ("memory usage: (%s)\n", "2-level sparse arrays");
|
1998-09-21 03:22:07 +02:00
|
|
|
#else
|
2002-07-02 21:43:03 +02:00
|
|
|
printf ("memory usage: (%s)\n", "3-level sparse arrays");
|
1998-09-21 03:22:07 +02:00
|
|
|
#endif
|
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
printf ("arrays: %d = %ld bytes\n", narrays,
|
2008-11-21 13:07:43 +01:00
|
|
|
(long) ((size_t) narrays * sizeof (struct sarray)));
|
2002-07-02 21:43:03 +02:00
|
|
|
total += narrays * sizeof (struct sarray);
|
|
|
|
printf ("buckets: %d = %ld bytes\n", nbuckets,
|
2008-11-21 13:07:43 +01:00
|
|
|
(long) ((size_t) nbuckets * sizeof (struct sbucket)));
|
2002-07-02 21:43:03 +02:00
|
|
|
total += nbuckets * sizeof (struct sbucket);
|
|
|
|
|
|
|
|
printf ("idxtables: %d = %ld bytes\n",
|
2008-11-21 13:07:43 +01:00
|
|
|
idxsize, (long) ((size_t) idxsize * sizeof (void *)));
|
2002-07-02 21:43:03 +02:00
|
|
|
total += idxsize * sizeof (void *);
|
|
|
|
printf ("-----------------------------------\n");
|
|
|
|
printf ("total: %d bytes\n", total);
|
|
|
|
printf ("===================================\n");
|
|
|
|
|
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
|
2010-12-15 01:47:16 +01:00
|
|
|
/* Returns the uninstalled dispatch table indicator. If a class'
|
|
|
|
dispatch table points to __objc_uninstalled_dtable then that means
|
|
|
|
it needs its dispatch table to be installed. */
|
2002-07-02 21:43:03 +02:00
|
|
|
struct sarray *
|
2010-12-16 23:52:48 +01:00
|
|
|
objc_get_uninstalled_dtable (void)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
|
|
|
return __objc_uninstalled_dtable;
|
|
|
|
}
|