Wolfgang Thallergcc16 merge934c3885 months agoHistory
1164 lines · 37.5 KB · C
RawDownload
/* GNU Objective C Runtime message lookup    Copyright (C) 1993-2026 Free Software Foundation, Inc.   Contributed by Kresten Krab Thorup This file is part of GCC. GCC is free software; you can redistribute it and/or modify it under theterms of the GNU General Public License as published by the Free SoftwareFoundation; either version 3, or (at your option) any later version. GCC is distributed in the hope that it will be useful, but WITHOUT ANYWARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESSFOR A PARTICULAR PURPOSE.  See the GNU General Public License for moredetails. Under Section 7 of GPL version 3, you are granted additionalpermissions described in the GCC Runtime Library Exception, version3.1, as published by the Free Software Foundation. You should have received a copy of the GNU General Public License anda 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/>.  */ /* Uncommented the following line to enable debug logging.  Use this   only while debugging the runtime.  *//* #define DEBUG 1 */ /* FIXME: This should be using libffi instead of __builtin_apply   and friends.  */ #include "objc-private/common.h"#include "objc-private/error.h"#include "tconfig.h"#include "coretypes.h"#include "objc/runtime.h"#include "objc/message.h"          /* For objc_msg_lookup(), objc_msg_lookup_super().  */#include "objc/thr.h"#include "objc-private/module-abi-8.h"#include "objc-private/runtime.h"#include "objc-private/hash.h"#include "objc-private/sarray.h"#include "objc-private/selector.h" /* For sel_is_mapped() */#include "runtime-info.h"#include <assert.h> /* For assert */#include <string.h> /* For strlen */ #define INVISIBLE_STRUCT_RETURN 1 /* The uninstalled dispatch table.  If a class' dispatch table points   to __objc_uninstalled_dtable then that means it needs its dispatch   table to be installed.  */struct sarray *__objc_uninstalled_dtable = 0;   /* !T:MUTEX */ /* 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.  */IMP (*__objc_msg_forward) (SEL) = NULL;IMP (*__objc_msg_forward2) (id, SEL) = NULL; /* Send +initialize to class.  */static void __objc_send_initialize (Class); /* Forward declare some functions */static void __objc_install_dtable_for_class (Class cls);static void __objc_prepare_dtable_for_class (Class cls);static void __objc_install_prepared_dtable_for_class (Class cls); static struct sarray *__objc_prepared_dtable_for_class (Class cls);static IMP __objc_get_prepared_imp (Class cls,SEL sel);   /* 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.   __objc_word_forward for pointers or types that fit in registers.  */static double __objc_double_forward (id, SEL, ...);static id __objc_word_forward (id, SEL, ...);typedef struct { id many[8]; } __big;#if INVISIBLE_STRUCT_RETURN static __big #elsestatic id#endif__objc_block_forward (id, SEL, ...);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);id nil_method (id, SEL); /* Make sure this inline function is exported regardless of GNU89 or C99   inlining semantics as it is part of the libobjc ABI.  */extern IMP __objc_get_forward_imp (id, SEL); /* Given a selector, return the proper forwarding implementation.  */inlineIMP__objc_get_forward_imp (id rcv, SEL sel){  /* 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.  */  if (__objc_msg_forward2)    {      IMP result;      if ((result = __objc_msg_forward2 (rcv, sel)) != NULL)       return result;    }  if (__objc_msg_forward)    {      IMP result;      if ((result = __objc_msg_forward (sel)) != NULL) 	return result;    }   /* In all other cases, use the default forwarding functions built     using __builtin_apply and friends.  */    {      const char *t = sel->sel_types;            if (t && (*t == '[' || *t == '(' || *t == '{')#ifdef OBJC_MAX_STRUCT_BY_VALUE          && objc_sizeof_type (t) > OBJC_MAX_STRUCT_BY_VALUE#endif          )        return (IMP)(void*)__objc_block_forward;      else if (t && (*t == 'f' || *t == 'd'))        return (IMP)(void*)__objc_double_forward;      else        return (IMP)(void*)__objc_word_forward;    }} /* 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 inlineIMP__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 inlineIMP__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_dtable_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;} /* Given a CLASS and selector, return the implementation corresponding   to the method of the selector.    If CLASS is a class, the instance method is returned.   If CLASS is a meta class, the class method is returned.    Since this requires the dispatch table to be installed, this function   will implicitly invoke +initialize for CLASS if it hasn't been   invoked yet.  This also insures that +initialize has been invoked   when the returned implementation is called directly.    The forwarding hooks require the receiver as an argument (if they are to   perform dynamic lookup in proxy objects etc), so this function has a   receiver argument to be used with those hooks.  */static inlineIMPget_implementation (id receiver, Class class, SEL sel){  void *res;   if (class->dtable == __objc_uninstalled_dtable)    {      /* The dispatch table needs to be installed.  */      objc_mutex_lock (__objc_runtime_mutex);       /* 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_dtable_for_class (class);       /* If the dispatch table is not yet installed, we are still in	 the process of executing +initialize.  But the implementation	 pointer should be available in the prepared ispatch table if	 it exists at all.  */      if (class->dtable == __objc_uninstalled_dtable)	{	  assert (__objc_prepared_dtable_for_class (class) != 0);	  res = __objc_get_prepared_imp (class, sel);	}      else	res = 0;       objc_mutex_unlock (__objc_runtime_mutex);      /* Call ourselves with the installed dispatch table and get the	 real method.  */      if (!res)	res = get_implementation (receiver, class, sel);    }  else    {      /* The dispatch table has been installed.  */      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 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)	    res = __objc_get_forward_imp (receiver, sel);	}    }  return res;} /* Make sure this inline function is exported regardless of GNU89 or C99   inlining semantics as it is part of the libobjc ABI.  */extern IMP get_imp (Class, SEL); inlineIMPget_imp (Class class, SEL sel){  /* 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.  */  void *res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);  if (res == 0)    {      res = get_implementation(nil, class, sel);    }  return res;} /* The new name of get_imp().  */IMPclass_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);} /* Given a method, return its implementation.  This has been replaced   by method_getImplementation() in the modern API.  */IMPmethod_get_imp (struct objc_method * method){  return (method != (struct objc_method *)0) ? method->method_imp : (IMP)0;} /* Query if an object can respond to a selector, returns YES if the   object implements the selector otherwise NO.  Does not check if the   method can be forwarded.  Since this requires the dispatch table to   installed, this function will implicitly invoke +initialize for the   class of OBJECT if it hasn't been invoked yet.  */inlineBOOL__objc_responds_to (id object, SEL sel){  void *res;  struct sarray *dtable;   /* Install dispatch table if need be */  dtable = object->class_pointer->dtable;  if (dtable == __objc_uninstalled_dtable)    {      objc_mutex_lock (__objc_runtime_mutex);      if (object->class_pointer->dtable == __objc_uninstalled_dtable)        __objc_install_dtable_for_class (object->class_pointer);       /* If the dispatch table is not yet installed, we are still in         the process of executing +initialize.  Yet the dispatch table         should be available.  */      if (object->class_pointer->dtable == __objc_uninstalled_dtable)        {          dtable = __objc_prepared_dtable_for_class (object->class_pointer);          assert (dtable);        }      else        dtable = object->class_pointer->dtable;       objc_mutex_unlock (__objc_runtime_mutex);    }   /* Get the method from the dispatch table.  */  res = sarray_get_safe (dtable, (size_t) sel->sel_id);  return (res != 0) ? YES : NO;} BOOLclass_respondsToSelector (Class class_, SEL selector){  struct sarray *dtable;  void *res;   if (class_ == Nil  ||  selector == NULL)    return NO;   /* Install dispatch table if need be.  */  dtable = class_->dtable;  if (dtable == __objc_uninstalled_dtable)    {      objc_mutex_lock (__objc_runtime_mutex);      if (class_->dtable == __objc_uninstalled_dtable)	__objc_install_dtable_for_class (class_);       /* If the dispatch table is not yet installed,         we are still in the process of executing +initialize.         Yet the dispatch table should be available.  */      if (class_->dtable == __objc_uninstalled_dtable)        {          dtable = __objc_prepared_dtable_for_class (class_);          assert (dtable);        }      else        dtable = class_->dtable;       objc_mutex_unlock (__objc_runtime_mutex);    }   /* Get the method from the dispatch table.  */  res = sarray_get_safe (dtable, (size_t) selector->sel_id);  return (res != 0) ? YES : NO;} /* This is the lookup function.  All entries in the table are either a   valid method *or* zero.  If zero then either the dispatch table   needs to be installed or it doesn't exist and forwarding is   attempted.  */IMPobjc_msg_lookup (id receiver, SEL op){  IMP result;  if (receiver)    {      /* First try a quick lookup assuming the dispatch table exists.  */      result = sarray_get_safe (receiver->class_pointer->dtable, 				(sidx)op->sel_id);      if (result == 0)	{	  /* Not found ... call get_implementation () to install the             dispatch table and call +initialize as required,             providing the method implementation or a forwarding             function.  */	  result = get_implementation (receiver, receiver->class_pointer, op);	}      return result;    }  else    return (IMP)nil_method;} IMPobjc_msg_lookup_super (struct objc_super *super, SEL sel){  if (super->self)    return get_imp (super->super_class, sel);  else    return (IMP)nil_method;} void__objc_init_dispatch_tables (){  __objc_uninstalled_dtable = sarray_new (200, 0);   /* TODO: It would be cool to register typed selectors here.  */  selector_resolveClassMethod = sel_registerName ("resolveClassMethod:");  selector_resolveInstanceMethod = sel_registerName ("resolveInstanceMethod:");}  /* Install dummy table for class which causes the first message to   that class (or instances hereof) to be initialized properly.  */void__objc_install_premature_dtable (Class class){  assert (__objc_uninstalled_dtable);  class->dtable = __objc_uninstalled_dtable;}    /* Send +initialize to class if not already done.  */static void__objc_send_initialize (Class class){  /* This *must* be a class object.  */  assert (CLS_ISCLASS (class));  assert (! CLS_ISMETA (class));   /* class_add_method_list/__objc_update_dispatch_table_for_class may     have reset the dispatch table.  The canonical way to insure that     we send +initialize just once, is this flag.  */  if (! CLS_ISINITIALIZED (class))    {      DEBUG_PRINTF ("+initialize: need to initialize class '%s'\n", class->name);

Lines 1–500 of 1164.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t