home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / objam01.lha / objam / objc / sendmsg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-10  |  11.4 KB  |  420 lines

  1. /*
  2. ** ObjectiveAmiga: Message lookup
  3. ** See GNU:lib/libobjam/ReadMe for details
  4. */
  5.  
  6.  
  7. #include "runtime.h"
  8.  
  9. /* The uninstalled dispatch table */
  10. struct sarray* __objc_uninstalled_dtable = 0;
  11.  
  12. /* Send +initialize to class */
  13. static void __objc_send_initialize(OCClass*);
  14.  
  15. static void __objc_install_dispatch_table_for_class (OCClass*);
  16.  
  17. /* Forward declare some functions */
  18. static void __objc_init_install_dtable(id, SEL);
  19. static id __objc_missing_method(id, SEL, ...);
  20. static Method_t search_for_method_in_hierarchy (OCClass* class, SEL sel);
  21. static Method_t search_for_method_in_list(MethodList_t list, SEL op);
  22. id nil_method(id, SEL, ...);
  23.  
  24. id
  25. nil_method(id receiver, SEL op, ...)
  26. {
  27.   return receiver;
  28. }
  29.  
  30. /* Given a class and selector, return the selector's implementation.  */
  31. __inline__ IMP
  32. get_imp (OCClass* class, SEL sel)
  33. {
  34.   void* res = sarray_get (class->dtable, (size_t) sel);
  35.   if(res == __objc_init_install_dtable)
  36.     __objc_install_dispatch_table_for_class (class);
  37.   return sarray_get (class->dtable, (size_t) sel);
  38. }
  39.  
  40. __inline__ BOOL
  41. __objc_responds_to (id object, SEL sel)
  42. {
  43.   return get_imp (object->class_pointer, sel) != __objc_missing_method;
  44. }
  45.  
  46. /* This is the lookup function.  All entries in the table are either a 
  47.    valid method *or* one of `__objc_missing_method' which calls
  48.    forward:: etc, or `__objc_init_install_dtable' which installs the
  49.    real dtable */
  50. __inline__ IMP
  51. objc_msg_lookup(id receiver, SEL op)
  52. {
  53.   if(receiver)
  54.     return sarray_get(receiver->class_pointer->dtable, (sidx)op);
  55.   else
  56.     return nil_method;
  57. }
  58.  
  59. IMP
  60. objc_msg_lookup_super (Super_t super, SEL sel)
  61. {
  62.   if (super->self)
  63.     return get_imp (super->class, sel);
  64.   else
  65.     return nil_method;
  66. }
  67.  
  68. retval_t
  69. objc_msg_sendv(id object, SEL op, arglist_t arg_frame)
  70. {
  71.   Method* m = class_get_instance_method(object->class_pointer, op);
  72.   const char *type;
  73.   *((id*)method_get_first_argument (m, arg_frame, &type)) = object;
  74.   *((SEL*)method_get_next_argument (arg_frame, &type)) = op;
  75.   return __builtin_apply((apply_t)m->method_imp, 
  76.              arg_frame,
  77.              method_get_sizeof_arguments (m));
  78. }
  79.  
  80. void __objc_init_dispatch_tables()
  81. {
  82.   __objc_uninstalled_dtable
  83.     = sarray_new(200, __objc_init_install_dtable);
  84. }
  85.  
  86. /* This one is a bit hairy.  This function is installed in the 
  87.    premature dispatch table, and thus called once for each class,
  88.    namely when the very first message is send to it.  */
  89.  
  90. static void __objc_init_install_dtable(id receiver, SEL op)
  91. {
  92.   __label__ allready_initialized;
  93.   IMP imp;
  94.   void* args;
  95.   void* result;
  96.  
  97.   /* This may happen, if the programmer has taken the address of a 
  98.      method before the dtable was initialized... too bad for him! */
  99.   if(receiver->class_pointer->dtable != __objc_uninstalled_dtable)
  100.     goto allready_initialized;
  101.  
  102.   if(CLS_ISCLASS(receiver->class_pointer))
  103.     {
  104.       /* receiver is an ordinary object */
  105.       assert(CLS_ISCLASS(receiver->class_pointer));
  106.  
  107.       /* install instance methods table */
  108.       __objc_install_dispatch_table_for_class (receiver->class_pointer);
  109.  
  110.       /* call +initialize -- this will in turn install the factory 
  111.      dispatch table if not already done :-) */
  112.       __objc_send_initialize(receiver->class_pointer);
  113.     }
  114.   else
  115.     {
  116.       /* receiver is a class object */
  117.       assert(CLS_ISCLASS((OCClass*)receiver));
  118.       assert(CLS_ISMETA(receiver->class_pointer));
  119.  
  120.       /* Install real dtable for factory methods */
  121.       __objc_install_dispatch_table_for_class (receiver->class_pointer);
  122.       
  123.       if(op != sel_get_uid ("initialize"))
  124.     __objc_send_initialize((OCClass*)receiver);
  125.       else
  126.     CLS_SETINITIALIZED((OCClass*)receiver);
  127.     }
  128.  
  129. allready_initialized:
  130.   
  131.   /* Get real method for this in newly installed dtable */
  132.   imp = get_imp(receiver->class_pointer, op);
  133.  
  134.   args = __builtin_apply_args();
  135.   result = __builtin_apply((apply_t)imp, args, 96);
  136.   __builtin_return (result);
  137.   
  138. }
  139.  
  140. /* Install dummy table for class which causes the first message to
  141.    that class (or instances hereof) to be initialized properly */
  142. void __objc_install_premature_dtable(OCClass* class)
  143. {
  144.   assert(__objc_uninstalled_dtable);
  145.   class->dtable = __objc_uninstalled_dtable;
  146. }   
  147.  
  148. /* Send +initialize to class if not already done */
  149. static void __objc_send_initialize(OCClass* class)
  150. {
  151.   Method_t m;
  152.  
  153.   /* This *must* be a class object */
  154.   assert(CLS_ISCLASS(class));
  155.   assert(!CLS_ISMETA(class));
  156.  
  157.   if (!CLS_ISINITIALIZED(class))
  158.     {
  159.       CLS_SETINITIALIZED(class);
  160.       CLS_SETINITIALIZED(class->class_pointer);
  161.       
  162.       if(class->super_class)
  163.     __objc_send_initialize(class->super_class);
  164.  
  165.       {
  166.     MethodList_t method_list = class->class_pointer->methods;
  167.     SEL op = sel_register_name ("initialize");
  168.  
  169.     /* If not found then we'll search the list.  */
  170.     while (method_list)
  171.       {
  172.         int i;
  173.  
  174.         /* Search the method list.  */
  175.         for (i = 0; i < method_list->method_count; ++i)
  176.           {
  177.         Method_t method = &method_list->method_list[i];
  178.         
  179.         
  180.         if (method->method_name == op)
  181.           (*method->method_imp)((id) class, op);
  182.           }
  183.  
  184.         /* The method wasn't found.  Follow the link to the next list of
  185.            methods.  */
  186.         method_list = method_list->method_next;
  187.       }
  188.       }
  189.     }
  190. }  
  191.  
  192. static void
  193. __objc_install_dispatch_table_for_class (OCClass* class)
  194. {
  195.   OCClass* super;
  196.   MethodList_t mlist;
  197.   int counter;
  198.  
  199.   /* If the class has not yet had it's class links resolved, we must 
  200.      re-compute all class links */
  201.   if(!CLS_ISRESOLV(class))
  202.     __objc_resolve_class_links();
  203.  
  204.   super = class->super_class;
  205.  
  206.   if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
  207.     __objc_install_dispatch_table_for_class (super);
  208.  
  209.   /* Allocate dtable if nessecary */
  210.   if (super == 0)
  211.     {
  212.       class->dtable = sarray_new (__objc_selector_max_index,
  213.                   __objc_missing_method);
  214.     }
  215.   else
  216.     class->dtable = sarray_lazy_copy (super->dtable);
  217.  
  218.   for (mlist = class->methods; mlist; mlist = mlist->method_next)
  219.     {
  220.       counter = mlist->method_count - 1;
  221.       while (counter >= 0)
  222.         {
  223.           Method_t method = &(mlist->method_list[counter]);
  224.       sarray_at_put_safe (class->dtable,
  225.                   (sidx) method->method_name,
  226.                   method->method_imp);
  227.           counter -= 1;
  228.         }
  229.     }
  230. }
  231.  
  232. void __objc_update_dispatch_table_for_class (OCClass* class)
  233. {
  234.   OCClass* next;
  235.   struct sarray* save;
  236.  
  237.   /* not yet installed -- skip it */
  238.   if (class->dtable == __objc_uninstalled_dtable) 
  239.     return;
  240.  
  241.   sarray_free (class->dtable);    /* release memory */
  242.   __objc_install_premature_dtable (class); /* someone might require it... */
  243.   __objc_install_dispatch_table_for_class (class); /* could have been lazy... */
  244.  
  245.   if (class->subclass_list)    /* Traverse subclasses */
  246.     for (next = class->subclass_list; next; next = next->sibling_class)
  247.       __objc_update_dispatch_table_for_class (next);
  248.  
  249. }
  250.  
  251.  
  252. /* This function adds a method list to a class.  This function is
  253.    typically called by another function specific to the run-time.  As
  254.    such this function does not worry about thread safe issued.
  255.  
  256.    This one is only called for categories. Class objects have their
  257.    methods installed rightaway, and their selectors are made into
  258.    SEL's by the function __objc_register_selectors_from_class. */ 
  259. void
  260. class_add_method_list (OCClass* class, MethodList_t list)
  261. {
  262.   int i;
  263.   static SEL initialize_sel = 0;
  264.   if (!initialize_sel)
  265.     initialize_sel = sel_register_name ("initialize");
  266.  
  267.   /* Passing of a linked list is not allowed.  Do multiple calls.  */
  268.   assert (!list->method_next);
  269.  
  270.   /* Check for duplicates.  */
  271.   for (i = 0; i < list->method_count; ++i)
  272.     {
  273.       Method_t method = &list->method_list[i];
  274.  
  275.       if (method->method_name)  /* Sometimes these are NULL */
  276.     {
  277.       /* This is where selector names are transmogriffed to SEL's */
  278.       method->method_name = sel_register_name ((char*)method->method_name);
  279.  
  280.       if (search_for_method_in_list (class->methods, method->method_name)
  281.           && method->method_name != initialize_sel)
  282.         {
  283.           /* Duplication. Print a error message an change the method name
  284.          to NULL. */
  285.           fprintf (stderr, "attempt to add a existing method: %s\n",
  286.                sel_get_name(method->method_name));
  287.           method->method_name = 0;
  288.         }
  289.     }
  290.     }
  291.  
  292.   /* Add the methods to the class's method list.  */
  293.   list->method_next = class->methods;
  294.   class->methods = list;
  295. }
  296.  
  297.  
  298. Method_t
  299. class_get_instance_method(OCClass* class, SEL op)
  300. {
  301.   return search_for_method_in_hierarchy(class, op);
  302. }
  303.  
  304. Method_t
  305. class_get_class_method(MetaClass* class, SEL op)
  306. {
  307.   return search_for_method_in_hierarchy(class, op);
  308. }
  309.  
  310.  
  311. /* Search for a method starting from the current class up its hierarchy.
  312.    Return a pointer to the method's method structure if found.  NULL
  313.    otherwise. */   
  314.  
  315. static Method_t
  316. search_for_method_in_hierarchy (OCClass* cls, SEL sel)
  317. {
  318.   Method_t method = NULL;
  319.   OCClass* class;
  320.  
  321.   if (! sel_is_mapped (sel))
  322.     return NULL;
  323.  
  324.   /* Scan the method list of the class.  If the method isn't found in the
  325.      list then step to its super class. */
  326.   for (class = cls; ((! method) && class); class = class->super_class)
  327.     method = search_for_method_in_list (class->methods, sel);
  328.  
  329.   return method;
  330. }
  331.  
  332.  
  333.  
  334. /* Given a linked list of method and a method's name.  Search for the named
  335.    method's method structure.  Return a pointer to the method's method
  336.    structure if found.  NULL otherwise. */  
  337. static Method_t
  338. search_for_method_in_list (MethodList_t list, SEL op)
  339. {
  340.   MethodList_t method_list = list;
  341.  
  342.   if (! sel_is_mapped (op))
  343.     return NULL;
  344.  
  345.   /* If not found then we'll search the list.  */
  346.   while (method_list)
  347.     {
  348.       int i;
  349.  
  350.       /* Search the method list.  */
  351.       for (i = 0; i < method_list->method_count; ++i)
  352.         {
  353.           Method_t method = &method_list->method_list[i];
  354.  
  355.           if (method->method_name)
  356.             if (method->method_name == op)
  357.               return method;
  358.         }
  359.  
  360.       /* The method wasn't found.  Follow the link to the next list of
  361.          methods.  */
  362.       method_list = method_list->method_next;
  363.     }
  364.  
  365.   return NULL;
  366. }
  367.  
  368.  
  369. /* This fuction is installed in the dispatch table for all methods which are
  370.    not implemented.  Thus, it is called when a selector is not recognized. */
  371. static id
  372. __objc_missing_method (id object, SEL sel, ...)
  373. {
  374.   IMP imp;
  375.   SEL frwd_sel;
  376.   SEL err_sel;
  377.  
  378.   /* first try if the object understands forward:: */
  379.   frwd_sel = sel_get_uid("forward::");
  380.   imp = get_imp(object->class_pointer, frwd_sel);
  381.   if(imp != __objc_missing_method)
  382.     {
  383.       void *result, *args = __builtin_apply_args();
  384.       result = (*imp)(object, frwd_sel, sel, args);
  385.       __builtin_return(result);
  386.     }
  387.  
  388.   /* If the object recognizes the doesNotRecognize: method then we're going
  389.      to send it. */
  390.   err_sel = sel_get_uid ("doesNotRecognize:");
  391.   imp = get_imp (object->class_pointer, err_sel);
  392.   if (imp != __objc_missing_method)
  393.     {
  394.       return (*imp) (object, err_sel, sel);
  395.     }
  396.   
  397.   /* The object doesn't recognize the method.  Check for responding to
  398.      error:.  If it does then sent it. */
  399.   {
  400.     char msg[256 + strlen ((char*)sel_get_name (sel))
  401.              + strlen ((char*)object->class_pointer->name)];
  402.  
  403.     sprintf (msg, "(%s) %s does not recognize %s",
  404.          (CLS_ISMETA(object->class_pointer)
  405.           ? "class"
  406.           : "instance" ),
  407.              object->class_pointer->name, sel_get_name (sel));
  408.  
  409.     err_sel = sel_get_uid ("error:");
  410.     imp = get_imp (object->class_pointer, err_sel);
  411.     if (imp != __objc_missing_method)
  412.       return (*imp) (object, sel_get_uid ("error:"), msg);
  413.  
  414.     /* The object doesn't respond to doesNotRecognize: or error:;  Therefore,
  415.        a default action is taken. */
  416.     fprintf (stderr, "fatal: %s\n", msg);
  417.     abort ();
  418.   }
  419. }
  420.