sendmsg.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /* GNU Objective C Runtime message lookup
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. Contributed by Kresten Krab Thorup
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation; either version 3, or (at your option) any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  11. details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. /* Uncommented the following line to enable debug logging. Use this
  20. only while debugging the runtime. */
  21. /* #define DEBUG 1 */
  22. /* FIXME: This should be using libffi instead of __builtin_apply
  23. and friends. */
  24. #include "objc-private/common.h"
  25. #include "objc-private/error.h"
  26. #include "tconfig.h"
  27. #include "coretypes.h"
  28. #include "objc/runtime.h"
  29. #include "objc/message.h" /* For objc_msg_lookup(), objc_msg_lookup_super(). */
  30. #include "objc/thr.h"
  31. #include "objc-private/module-abi-8.h"
  32. #include "objc-private/runtime.h"
  33. #include "objc-private/hash.h"
  34. #include "objc-private/sarray.h"
  35. #include "objc-private/selector.h" /* For sel_is_mapped() */
  36. #include "runtime-info.h"
  37. #include <assert.h> /* For assert */
  38. #include <string.h> /* For strlen */
  39. #define INVISIBLE_STRUCT_RETURN 1
  40. /* The uninstalled dispatch table. If a class' dispatch table points
  41. to __objc_uninstalled_dtable then that means it needs its dispatch
  42. table to be installed. */
  43. struct sarray *__objc_uninstalled_dtable = 0; /* !T:MUTEX */
  44. /* Two hooks for method forwarding. If either is set, it is invoked to
  45. * return a function that performs the real forwarding. If both are
  46. * set, the result of __objc_msg_forward2 will be preferred over that
  47. * of __objc_msg_forward. If both return NULL or are unset, the
  48. * libgcc based functions (__builtin_apply and friends) are used. */
  49. IMP (*__objc_msg_forward) (SEL) = NULL;
  50. IMP (*__objc_msg_forward2) (id, SEL) = NULL;
  51. /* Send +initialize to class. */
  52. static void __objc_send_initialize (Class);
  53. /* Forward declare some functions */
  54. static void __objc_install_dtable_for_class (Class cls);
  55. static void __objc_prepare_dtable_for_class (Class cls);
  56. static void __objc_install_prepared_dtable_for_class (Class cls);
  57. static struct sarray *__objc_prepared_dtable_for_class (Class cls);
  58. static IMP __objc_get_prepared_imp (Class cls,SEL sel);
  59. /* Various forwarding functions that are used based upon the
  60. return type for the selector.
  61. __objc_block_forward for structures.
  62. __objc_double_forward for floats/doubles.
  63. __objc_word_forward for pointers or types that fit in registers. */
  64. static double __objc_double_forward (id, SEL, ...);
  65. static id __objc_word_forward (id, SEL, ...);
  66. typedef struct { id many[8]; } __big;
  67. #if INVISIBLE_STRUCT_RETURN
  68. static __big
  69. #else
  70. static id
  71. #endif
  72. __objc_block_forward (id, SEL, ...);
  73. static struct objc_method * search_for_method_in_hierarchy (Class class, SEL sel);
  74. struct objc_method * search_for_method_in_list (struct objc_method_list * list, SEL op);
  75. id nil_method (id, SEL);
  76. /* Make sure this inline function is exported regardless of GNU89 or C99
  77. inlining semantics as it is part of the libobjc ABI. */
  78. extern IMP __objc_get_forward_imp (id, SEL);
  79. /* Given a selector, return the proper forwarding implementation. */
  80. inline
  81. IMP
  82. __objc_get_forward_imp (id rcv, SEL sel)
  83. {
  84. /* If a custom forwarding hook was registered, try getting a
  85. forwarding function from it. There are two forward routine hooks,
  86. one that takes the receiver as an argument and one that does
  87. not. */
  88. if (__objc_msg_forward2)
  89. {
  90. IMP result;
  91. if ((result = __objc_msg_forward2 (rcv, sel)) != NULL)
  92. return result;
  93. }
  94. if (__objc_msg_forward)
  95. {
  96. IMP result;
  97. if ((result = __objc_msg_forward (sel)) != NULL)
  98. return result;
  99. }
  100. /* In all other cases, use the default forwarding functions built
  101. using __builtin_apply and friends. */
  102. {
  103. const char *t = sel->sel_types;
  104. if (t && (*t == '[' || *t == '(' || *t == '{')
  105. #ifdef OBJC_MAX_STRUCT_BY_VALUE
  106. && objc_sizeof_type (t) > OBJC_MAX_STRUCT_BY_VALUE
  107. #endif
  108. )
  109. return (IMP)__objc_block_forward;
  110. else if (t && (*t == 'f' || *t == 'd'))
  111. return (IMP)__objc_double_forward;
  112. else
  113. return (IMP)__objc_word_forward;
  114. }
  115. }
  116. /* Selectors for +resolveClassMethod: and +resolveInstanceMethod:.
  117. These are set up at startup. */
  118. static SEL selector_resolveClassMethod = NULL;
  119. static SEL selector_resolveInstanceMethod = NULL;
  120. /* Internal routines use to resolve a class method using
  121. +resolveClassMethod:. 'class' is always a non-Nil class (*not* a
  122. meta-class), and 'sel' is the selector that we are trying to
  123. resolve. This must be called when class is not Nil, and the
  124. dispatch table for class methods has already been installed.
  125. This routine tries to call +resolveClassMethod: to give an
  126. opportunity to resolve the method. If +resolveClassMethod: returns
  127. YES, it tries looking up the method again, and if found, it returns
  128. it. Else, it returns NULL. */
  129. static inline
  130. IMP
  131. __objc_resolve_class_method (Class class, SEL sel)
  132. {
  133. /* We need to lookup +resolveClassMethod:. */
  134. BOOL (*resolveMethodIMP) (id, SEL, SEL);
  135. /* The dispatch table for class methods is already installed and we
  136. don't want any forwarding to happen when looking up this method,
  137. so we just look it up directly. Note that if 'sel' is precisely
  138. +resolveClassMethod:, this would look it up yet again and find
  139. nothing. That's no problem and there's no recursion. */
  140. resolveMethodIMP = (BOOL (*) (id, SEL, SEL))sarray_get_safe
  141. (class->class_pointer->dtable, (size_t) selector_resolveClassMethod->sel_id);
  142. if (resolveMethodIMP && resolveMethodIMP ((id)class, selector_resolveClassMethod, sel))
  143. {
  144. /* +resolveClassMethod: returned YES. Look the method up again.
  145. We already know the dtable is installed. */
  146. /* TODO: There is the case where +resolveClassMethod: is buggy
  147. and returned YES without actually adding the method. We
  148. could maybe print an error message. */
  149. return sarray_get_safe (class->class_pointer->dtable, (size_t) sel->sel_id);
  150. }
  151. return NULL;
  152. }
  153. /* Internal routines use to resolve a instance method using
  154. +resolveInstanceMethod:. 'class' is always a non-Nil class, and
  155. 'sel' is the selector that we are trying to resolve. This must be
  156. called when class is not Nil, and the dispatch table for instance
  157. methods has already been installed.
  158. This routine tries to call +resolveInstanceMethod: to give an
  159. opportunity to resolve the method. If +resolveInstanceMethod:
  160. returns YES, it tries looking up the method again, and if found, it
  161. returns it. Else, it returns NULL. */
  162. static inline
  163. IMP
  164. __objc_resolve_instance_method (Class class, SEL sel)
  165. {
  166. /* We need to lookup +resolveInstanceMethod:. */
  167. BOOL (*resolveMethodIMP) (id, SEL, SEL);
  168. /* The dispatch table for class methods may not be already installed
  169. so we have to install it if needed. */
  170. resolveMethodIMP = sarray_get_safe (class->class_pointer->dtable,
  171. (size_t) selector_resolveInstanceMethod->sel_id);
  172. if (resolveMethodIMP == 0)
  173. {
  174. /* Try again after installing the dtable. */
  175. if (class->class_pointer->dtable == __objc_uninstalled_dtable)
  176. {
  177. objc_mutex_lock (__objc_runtime_mutex);
  178. if (class->class_pointer->dtable == __objc_uninstalled_dtable)
  179. __objc_install_dtable_for_class (class->class_pointer);
  180. objc_mutex_unlock (__objc_runtime_mutex);
  181. }
  182. resolveMethodIMP = sarray_get_safe (class->class_pointer->dtable,
  183. (size_t) selector_resolveInstanceMethod->sel_id);
  184. }
  185. if (resolveMethodIMP && resolveMethodIMP ((id)class, selector_resolveInstanceMethod, sel))
  186. {
  187. /* +resolveInstanceMethod: returned YES. Look the method up
  188. again. We already know the dtable is installed. */
  189. /* TODO: There is the case where +resolveInstanceMethod: is
  190. buggy and returned YES without actually adding the method.
  191. We could maybe print an error message. */
  192. return sarray_get_safe (class->dtable, (size_t) sel->sel_id);
  193. }
  194. return NULL;
  195. }
  196. /* Given a CLASS and selector, return the implementation corresponding
  197. to the method of the selector.
  198. If CLASS is a class, the instance method is returned.
  199. If CLASS is a meta class, the class method is returned.
  200. Since this requires the dispatch table to be installed, this function
  201. will implicitly invoke +initialize for CLASS if it hasn't been
  202. invoked yet. This also insures that +initialize has been invoked
  203. when the returned implementation is called directly.
  204. The forwarding hooks require the receiver as an argument (if they are to
  205. perform dynamic lookup in proxy objects etc), so this function has a
  206. receiver argument to be used with those hooks. */
  207. static inline
  208. IMP
  209. get_implementation (id receiver, Class class, SEL sel)
  210. {
  211. void *res;
  212. if (class->dtable == __objc_uninstalled_dtable)
  213. {
  214. /* The dispatch table needs to be installed. */
  215. objc_mutex_lock (__objc_runtime_mutex);
  216. /* Double-checked locking pattern: Check
  217. __objc_uninstalled_dtable again in case another thread
  218. installed the dtable while we were waiting for the lock to be
  219. released. */
  220. if (class->dtable == __objc_uninstalled_dtable)
  221. __objc_install_dtable_for_class (class);
  222. /* If the dispatch table is not yet installed, we are still in
  223. the process of executing +initialize. But the implementation
  224. pointer should be available in the prepared ispatch table if
  225. it exists at all. */
  226. if (class->dtable == __objc_uninstalled_dtable)
  227. {
  228. assert (__objc_prepared_dtable_for_class (class) != 0);
  229. res = __objc_get_prepared_imp (class, sel);
  230. }
  231. else
  232. res = 0;
  233. objc_mutex_unlock (__objc_runtime_mutex);
  234. /* Call ourselves with the installed dispatch table and get the
  235. real method. */
  236. if (!res)
  237. res = get_implementation (receiver, class, sel);
  238. }
  239. else
  240. {
  241. /* The dispatch table has been installed. */
  242. res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
  243. if (res == 0)
  244. {
  245. /* The dispatch table has been installed, and the method is
  246. not in the dispatch table. So the method just doesn't
  247. exist for the class. */
  248. /* Try going through the +resolveClassMethod: or
  249. +resolveInstanceMethod: process. */
  250. if (CLS_ISMETA (class))
  251. {
  252. /* We have the meta class, but we need to invoke the
  253. +resolveClassMethod: method on the class. So, we
  254. need to obtain the class from the meta class, which
  255. we do using the fact that both the class and the
  256. meta-class have the same name. */
  257. Class realClass = objc_lookUpClass (class->name);
  258. if (realClass)
  259. res = __objc_resolve_class_method (realClass, sel);
  260. }
  261. else
  262. res = __objc_resolve_instance_method (class, sel);
  263. if (res == 0)
  264. res = __objc_get_forward_imp (receiver, sel);
  265. }
  266. }
  267. return res;
  268. }
  269. /* Make sure this inline function is exported regardless of GNU89 or C99
  270. inlining semantics as it is part of the libobjc ABI. */
  271. extern IMP get_imp (Class, SEL);
  272. inline
  273. IMP
  274. get_imp (Class class, SEL sel)
  275. {
  276. /* In a vanilla implementation we would first check if the dispatch
  277. table is installed. Here instead, to get more speed in the
  278. standard case (that the dispatch table is installed) we first try
  279. to get the imp using brute force. Only if that fails, we do what
  280. we should have been doing from the very beginning, that is, check
  281. if the dispatch table needs to be installed, install it if it's
  282. not installed, and retrieve the imp from the table if it's
  283. installed. */
  284. void *res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
  285. if (res == 0)
  286. {
  287. res = get_implementation(nil, class, sel);
  288. }
  289. return res;
  290. }
  291. /* The new name of get_imp(). */
  292. IMP
  293. class_getMethodImplementation (Class class_, SEL selector)
  294. {
  295. if (class_ == Nil || selector == NULL)
  296. return NULL;
  297. /* get_imp is inlined, so we're good. */
  298. return get_imp (class_, selector);
  299. }
  300. /* Given a method, return its implementation. This has been replaced
  301. by method_getImplementation() in the modern API. */
  302. IMP
  303. method_get_imp (struct objc_method * method)
  304. {
  305. return (method != (struct objc_method *)0) ? method->method_imp : (IMP)0;
  306. }
  307. /* Query if an object can respond to a selector, returns YES if the
  308. object implements the selector otherwise NO. Does not check if the
  309. method can be forwarded. Since this requires the dispatch table to
  310. installed, this function will implicitly invoke +initialize for the
  311. class of OBJECT if it hasn't been invoked yet. */
  312. inline
  313. BOOL
  314. __objc_responds_to (id object, SEL sel)
  315. {
  316. void *res;
  317. struct sarray *dtable;
  318. /* Install dispatch table if need be */
  319. dtable = object->class_pointer->dtable;
  320. if (dtable == __objc_uninstalled_dtable)
  321. {
  322. objc_mutex_lock (__objc_runtime_mutex);
  323. if (object->class_pointer->dtable == __objc_uninstalled_dtable)
  324. __objc_install_dtable_for_class (object->class_pointer);
  325. /* If the dispatch table is not yet installed, we are still in
  326. the process of executing +initialize. Yet the dispatch table
  327. should be available. */
  328. if (object->class_pointer->dtable == __objc_uninstalled_dtable)
  329. {
  330. dtable = __objc_prepared_dtable_for_class (object->class_pointer);
  331. assert (dtable);
  332. }
  333. else
  334. dtable = object->class_pointer->dtable;
  335. objc_mutex_unlock (__objc_runtime_mutex);
  336. }
  337. /* Get the method from the dispatch table. */
  338. res = sarray_get_safe (dtable, (size_t) sel->sel_id);
  339. return (res != 0) ? YES : NO;
  340. }
  341. BOOL
  342. class_respondsToSelector (Class class_, SEL selector)
  343. {
  344. struct sarray *dtable;
  345. void *res;
  346. if (class_ == Nil || selector == NULL)
  347. return NO;
  348. /* Install dispatch table if need be. */
  349. dtable = class_->dtable;
  350. if (dtable == __objc_uninstalled_dtable)
  351. {
  352. objc_mutex_lock (__objc_runtime_mutex);
  353. if (class_->dtable == __objc_uninstalled_dtable)
  354. __objc_install_dtable_for_class (class_);
  355. /* If the dispatch table is not yet installed,
  356. we are still in the process of executing +initialize.
  357. Yet the dispatch table should be available. */
  358. if (class_->dtable == __objc_uninstalled_dtable)
  359. {
  360. dtable = __objc_prepared_dtable_for_class (class_);
  361. assert (dtable);
  362. }
  363. else
  364. dtable = class_->dtable;
  365. objc_mutex_unlock (__objc_runtime_mutex);
  366. }
  367. /* Get the method from the dispatch table. */
  368. res = sarray_get_safe (dtable, (size_t) selector->sel_id);
  369. return (res != 0) ? YES : NO;
  370. }
  371. /* This is the lookup function. All entries in the table are either a
  372. valid method *or* zero. If zero then either the dispatch table
  373. needs to be installed or it doesn't exist and forwarding is
  374. attempted. */
  375. IMP
  376. objc_msg_lookup (id receiver, SEL op)
  377. {
  378. IMP result;
  379. if (receiver)
  380. {
  381. /* First try a quick lookup assuming the dispatch table exists. */
  382. result = sarray_get_safe (receiver->class_pointer->dtable,
  383. (sidx)op->sel_id);
  384. if (result == 0)
  385. {
  386. /* Not found ... call get_implementation () to install the
  387. dispatch table and call +initialize as required,
  388. providing the method implementation or a forwarding
  389. function. */
  390. result = get_implementation (receiver, receiver->class_pointer, op);
  391. }
  392. return result;
  393. }
  394. else
  395. return (IMP)nil_method;
  396. }
  397. IMP
  398. objc_msg_lookup_super (struct objc_super *super, SEL sel)
  399. {
  400. if (super->self)
  401. return get_imp (super->super_class, sel);
  402. else
  403. return (IMP)nil_method;
  404. }
  405. void
  406. __objc_init_dispatch_tables ()
  407. {
  408. __objc_uninstalled_dtable = sarray_new (200, 0);
  409. /* TODO: It would be cool to register typed selectors here. */
  410. selector_resolveClassMethod = sel_registerName ("resolveClassMethod:");
  411. selector_resolveInstanceMethod = sel_registerName ("resolveInstanceMethod:");
  412. }
  413. /* Install dummy table for class which causes the first message to
  414. that class (or instances hereof) to be initialized properly. */
  415. void
  416. __objc_install_premature_dtable (Class class)
  417. {
  418. assert (__objc_uninstalled_dtable);
  419. class->dtable = __objc_uninstalled_dtable;
  420. }
  421. /* Send +initialize to class if not already done. */
  422. static void
  423. __objc_send_initialize (Class class)
  424. {
  425. /* This *must* be a class object. */
  426. assert (CLS_ISCLASS (class));
  427. assert (! CLS_ISMETA (class));
  428. /* class_add_method_list/__objc_update_dispatch_table_for_class may
  429. have reset the dispatch table. The canonical way to insure that
  430. we send +initialize just once, is this flag. */
  431. if (! CLS_ISINITIALIZED (class))
  432. {
  433. DEBUG_PRINTF ("+initialize: need to initialize class '%s'\n", class->name);
  434. CLS_SETINITIALIZED (class);
  435. CLS_SETINITIALIZED (class->class_pointer);
  436. /* Create the garbage collector type memory description. */
  437. __objc_generate_gc_type_description (class);
  438. if (class->super_class)
  439. __objc_send_initialize (class->super_class);
  440. {
  441. SEL op = sel_registerName ("initialize");
  442. struct objc_method *method = search_for_method_in_hierarchy (class->class_pointer,
  443. op);
  444. if (method)
  445. {
  446. DEBUG_PRINTF (" begin of [%s +initialize]\n", class->name);
  447. (*method->method_imp) ((id)class, op);
  448. DEBUG_PRINTF (" end of [%s +initialize]\n", class->name);
  449. }
  450. #ifdef DEBUG
  451. else
  452. {
  453. DEBUG_PRINTF (" class '%s' has no +initialize method\n", class->name);
  454. }
  455. #endif
  456. }
  457. }
  458. }
  459. /* Walk on the methods list of class and install the methods in the
  460. reverse order of the lists. Since methods added by categories are
  461. before the methods of class in the methods list, this allows
  462. categories to substitute methods declared in class. However if
  463. more than one category replaces the same method nothing is
  464. guaranteed about what method will be used. Assumes that
  465. __objc_runtime_mutex is locked down. */
  466. static void
  467. __objc_install_methods_in_dtable (struct sarray *dtable, struct objc_method_list * method_list)
  468. {
  469. int i;
  470. if (! method_list)
  471. return;
  472. if (method_list->method_next)
  473. __objc_install_methods_in_dtable (dtable, method_list->method_next);
  474. for (i = 0; i < method_list->method_count; i++)
  475. {
  476. struct objc_method * method = &(method_list->method_list[i]);
  477. sarray_at_put_safe (dtable,
  478. (sidx) method->method_name->sel_id,
  479. method->method_imp);
  480. }
  481. }
  482. void
  483. __objc_update_dispatch_table_for_class (Class class)
  484. {
  485. Class next;
  486. struct sarray *arr;
  487. DEBUG_PRINTF (" _objc_update_dtable_for_class (%s)\n", class->name);
  488. objc_mutex_lock (__objc_runtime_mutex);
  489. /* Not yet installed -- skip it unless in +initialize. */
  490. if (class->dtable == __objc_uninstalled_dtable)
  491. {
  492. if (__objc_prepared_dtable_for_class (class))
  493. {
  494. /* There is a prepared table so we must be initialising this
  495. class ... we must re-do the table preparation. */
  496. __objc_prepare_dtable_for_class (class);
  497. }
  498. objc_mutex_unlock (__objc_runtime_mutex);
  499. return;
  500. }
  501. arr = class->dtable;
  502. __objc_install_premature_dtable (class); /* someone might require it... */
  503. sarray_free (arr); /* release memory */
  504. /* Could have been lazy... */
  505. __objc_install_dtable_for_class (class);
  506. if (class->subclass_list) /* Traverse subclasses. */
  507. for (next = class->subclass_list; next; next = next->sibling_class)
  508. __objc_update_dispatch_table_for_class (next);
  509. objc_mutex_unlock (__objc_runtime_mutex);
  510. }
  511. /* This function adds a method list to a class. This function is
  512. typically called by another function specific to the run-time. As
  513. such this function does not worry about thread safe issues.
  514. This one is only called for categories. Class objects have their
  515. methods installed right away, and their selectors are made into
  516. SEL's by the function __objc_register_selectors_from_class. */
  517. void
  518. class_add_method_list (Class class, struct objc_method_list * list)
  519. {
  520. /* Passing of a linked list is not allowed. Do multiple calls. */
  521. assert (! list->method_next);
  522. __objc_register_selectors_from_list(list);
  523. /* Add the methods to the class's method list. */
  524. list->method_next = class->methods;
  525. class->methods = list;
  526. /* Update the dispatch table of class. */
  527. __objc_update_dispatch_table_for_class (class);
  528. }
  529. struct objc_method *
  530. class_getInstanceMethod (Class class_, SEL selector)
  531. {
  532. struct objc_method *m;
  533. if (class_ == Nil || selector == NULL)
  534. return NULL;
  535. m = search_for_method_in_hierarchy (class_, selector);
  536. if (m)
  537. return m;
  538. /* Try going through +resolveInstanceMethod:, and do the search
  539. again if successful. */
  540. if (__objc_resolve_instance_method (class_, selector))
  541. return search_for_method_in_hierarchy (class_, selector);
  542. return NULL;
  543. }
  544. struct objc_method *
  545. class_getClassMethod (Class class_, SEL selector)
  546. {
  547. struct objc_method *m;
  548. if (class_ == Nil || selector == NULL)
  549. return NULL;
  550. m = search_for_method_in_hierarchy (class_->class_pointer,
  551. selector);
  552. if (m)
  553. return m;
  554. /* Try going through +resolveClassMethod:, and do the search again
  555. if successful. */
  556. if (__objc_resolve_class_method (class_, selector))
  557. return search_for_method_in_hierarchy (class_->class_pointer,
  558. selector);
  559. return NULL;
  560. }
  561. BOOL
  562. class_addMethod (Class class_, SEL selector, IMP implementation,
  563. const char *method_types)
  564. {
  565. struct objc_method_list *method_list;
  566. struct objc_method *method;
  567. const char *method_name;
  568. if (class_ == Nil || selector == NULL || implementation == NULL
  569. || method_types == NULL || (strcmp (method_types, "") == 0))
  570. return NO;
  571. method_name = sel_getName (selector);
  572. if (method_name == NULL)
  573. return NO;
  574. /* If the method already exists in the class, return NO. It is fine
  575. if the method already exists in the superclass; in that case, we
  576. are overriding it. */
  577. if (CLS_IS_IN_CONSTRUCTION (class_))
  578. {
  579. /* The class only contains a list of methods; they have not been
  580. registered yet, ie, the method_name of each of them is still
  581. a string, not a selector. Iterate manually over them to
  582. check if we have already added the method. */
  583. struct objc_method_list * method_list = class_->methods;
  584. while (method_list)
  585. {
  586. int i;
  587. /* Search the method list. */
  588. for (i = 0; i < method_list->method_count; ++i)
  589. {
  590. struct objc_method * method = &method_list->method_list[i];
  591. if (method->method_name
  592. && strcmp ((char *)method->method_name, method_name) == 0)
  593. return NO;
  594. }
  595. /* The method wasn't found. Follow the link to the next list of
  596. methods. */
  597. method_list = method_list->method_next;
  598. }
  599. /* The method wasn't found. It's a new one. Go ahead and add
  600. it. */
  601. }
  602. else
  603. {
  604. /* Do the standard lookup. This assumes the selectors are
  605. mapped. */
  606. if (search_for_method_in_list (class_->methods, selector))
  607. return NO;
  608. }
  609. method_list = (struct objc_method_list *)objc_calloc (1, sizeof (struct objc_method_list));
  610. method_list->method_count = 1;
  611. method = &(method_list->method_list[0]);
  612. method->method_name = objc_malloc (strlen (method_name) + 1);
  613. strcpy ((char *)method->method_name, method_name);
  614. method->method_types = objc_malloc (strlen (method_types) + 1);
  615. strcpy ((char *)method->method_types, method_types);
  616. method->method_imp = implementation;
  617. if (CLS_IS_IN_CONSTRUCTION (class_))
  618. {
  619. /* We only need to add the method to the list. It will be
  620. registered with the runtime when the class pair is registered
  621. (if ever). */
  622. method_list->method_next = class_->methods;
  623. class_->methods = method_list;
  624. }
  625. else
  626. {
  627. /* Add the method to a live class. */
  628. objc_mutex_lock (__objc_runtime_mutex);
  629. class_add_method_list (class_, method_list);
  630. objc_mutex_unlock (__objc_runtime_mutex);
  631. }
  632. return YES;
  633. }
  634. IMP
  635. class_replaceMethod (Class class_, SEL selector, IMP implementation,
  636. const char *method_types)
  637. {
  638. struct objc_method * method;
  639. if (class_ == Nil || selector == NULL || implementation == NULL
  640. || method_types == NULL)
  641. return NULL;
  642. method = search_for_method_in_hierarchy (class_, selector);
  643. if (method)
  644. {
  645. return method_setImplementation (method, implementation);
  646. }
  647. else
  648. {
  649. class_addMethod (class_, selector, implementation, method_types);
  650. return NULL;
  651. }
  652. }
  653. /* Search for a method starting from the current class up its
  654. hierarchy. Return a pointer to the method's method structure if
  655. found. NULL otherwise. */
  656. static struct objc_method *
  657. search_for_method_in_hierarchy (Class cls, SEL sel)
  658. {
  659. struct objc_method * method = NULL;
  660. Class class;
  661. if (! sel_is_mapped (sel))
  662. return NULL;
  663. /* Scan the method list of the class. If the method isn't found in
  664. the list then step to its super class. */
  665. for (class = cls; ((! method) && class); class = class->super_class)
  666. method = search_for_method_in_list (class->methods, sel);
  667. return method;
  668. }
  669. /* Given a linked list of method and a method's name. Search for the
  670. named method's method structure. Return a pointer to the method's
  671. method structure if found. NULL otherwise. */
  672. struct objc_method *
  673. search_for_method_in_list (struct objc_method_list * list, SEL op)
  674. {
  675. struct objc_method_list * method_list = list;
  676. if (! sel_is_mapped (op))
  677. return NULL;
  678. /* If not found then we'll search the list. */
  679. while (method_list)
  680. {
  681. int i;
  682. /* Search the method list. */
  683. for (i = 0; i < method_list->method_count; ++i)
  684. {
  685. struct objc_method * method = &method_list->method_list[i];
  686. if (method->method_name)
  687. if (method->method_name->sel_id == op->sel_id)
  688. return method;
  689. }
  690. /* The method wasn't found. Follow the link to the next list of
  691. methods. */
  692. method_list = method_list->method_next;
  693. }
  694. return NULL;
  695. }
  696. typedef void * retval_t;
  697. typedef void * arglist_t;
  698. static retval_t __objc_forward (id object, SEL sel, arglist_t args);
  699. /* Forwarding pointers/integers through the normal registers. */
  700. static id
  701. __objc_word_forward (id rcv, SEL op, ...)
  702. {
  703. void *args, *res;
  704. args = __builtin_apply_args ();
  705. res = __objc_forward (rcv, op, args);
  706. if (res)
  707. __builtin_return (res);
  708. else
  709. return res;
  710. }
  711. /* Specific routine for forwarding floats/double because of
  712. architectural differences on some processors. i386s for example
  713. which uses a floating point stack versus general registers for
  714. floating point numbers. This forward routine makes sure that GCC
  715. restores the proper return values. */
  716. static double
  717. __objc_double_forward (id rcv, SEL op, ...)
  718. {
  719. void *args, *res;
  720. args = __builtin_apply_args ();
  721. res = __objc_forward (rcv, op, args);
  722. __builtin_return (res);
  723. }
  724. #if INVISIBLE_STRUCT_RETURN
  725. static __big
  726. #else
  727. static id
  728. #endif
  729. __objc_block_forward (id rcv, SEL op, ...)
  730. {
  731. void *args, *res;
  732. args = __builtin_apply_args ();
  733. res = __objc_forward (rcv, op, args);
  734. if (res)
  735. __builtin_return (res);
  736. else
  737. #if INVISIBLE_STRUCT_RETURN
  738. return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
  739. #else
  740. return nil;
  741. #endif
  742. }
  743. /* This function is called for methods which are not implemented,
  744. unless a custom forwarding routine has been installed. Please note
  745. that most serious users of libobjc (eg, GNUstep base) do install
  746. their own forwarding routines, and hence this is never actually
  747. used. But, if no custom forwarding routine is installed, this is
  748. called when a selector is not recognized. */
  749. static retval_t
  750. __objc_forward (id object, SEL sel, arglist_t args)
  751. {
  752. IMP imp;
  753. static SEL frwd_sel = 0; /* !T:SAFE2 */
  754. SEL err_sel;
  755. /* First try if the object understands forward::. */
  756. if (! frwd_sel)
  757. frwd_sel = sel_get_any_uid ("forward::");
  758. if (__objc_responds_to (object, frwd_sel))
  759. {
  760. imp = get_implementation (object, object->class_pointer, frwd_sel);
  761. return (*imp) (object, frwd_sel, sel, args);
  762. }
  763. /* If the object recognizes the doesNotRecognize: method then we're
  764. going to send it. */
  765. err_sel = sel_get_any_uid ("doesNotRecognize:");
  766. if (__objc_responds_to (object, err_sel))
  767. {
  768. imp = get_implementation (object, object->class_pointer, err_sel);
  769. return (*imp) (object, err_sel, sel);
  770. }
  771. /* The object doesn't recognize the method. Check for responding to
  772. error:. If it does then sent it. */
  773. {
  774. char msg[256 + strlen ((const char *) sel_getName (sel))
  775. + strlen ((const char *) object->class_pointer->name)];
  776. sprintf (msg, "(%s) %s does not recognize %s",
  777. (CLS_ISMETA (object->class_pointer)
  778. ? "class"
  779. : "instance" ),
  780. object->class_pointer->name, sel_getName (sel));
  781. /* The object doesn't respond to doesNotRecognize:. Therefore, a
  782. default action is taken. */
  783. _objc_abort ("%s\n", msg);
  784. return 0;
  785. }
  786. }
  787. void
  788. __objc_print_dtable_stats (void)
  789. {
  790. int total = 0;
  791. objc_mutex_lock (__objc_runtime_mutex);
  792. #ifdef OBJC_SPARSE2
  793. printf ("memory usage: (%s)\n", "2-level sparse arrays");
  794. #else
  795. printf ("memory usage: (%s)\n", "3-level sparse arrays");
  796. #endif
  797. printf ("arrays: %d = %ld bytes\n", narrays,
  798. (long) ((size_t) narrays * sizeof (struct sarray)));
  799. total += narrays * sizeof (struct sarray);
  800. printf ("buckets: %d = %ld bytes\n", nbuckets,
  801. (long) ((size_t) nbuckets * sizeof (struct sbucket)));
  802. total += nbuckets * sizeof (struct sbucket);
  803. printf ("idxtables: %d = %ld bytes\n",
  804. idxsize, (long) ((size_t) idxsize * sizeof (void *)));
  805. total += idxsize * sizeof (void *);
  806. printf ("-----------------------------------\n");
  807. printf ("total: %d bytes\n", total);
  808. printf ("===================================\n");
  809. objc_mutex_unlock (__objc_runtime_mutex);
  810. }
  811. static cache_ptr prepared_dtable_table = 0;
  812. /* This function is called by: objc_msg_lookup, get_imp and
  813. __objc_responds_to (and the dispatch table installation functions
  814. themselves) to install a dispatch table for a class.
  815. If CLS is a class, it installs instance methods.
  816. If CLS is a meta class, it installs class methods.
  817. In either case +initialize is invoked for the corresponding class.
  818. The implementation must insure that the dispatch table is not
  819. installed until +initialize completes. Otherwise it opens a
  820. potential race since the installation of the dispatch table is used
  821. as gate in regular method dispatch and we need to guarantee that
  822. +initialize is the first method invoked an that no other thread my
  823. dispatch messages to the class before +initialize completes. */
  824. static void
  825. __objc_install_dtable_for_class (Class cls)
  826. {
  827. /* If the class has not yet had its class links resolved, we must
  828. re-compute all class links. */
  829. if (! CLS_ISRESOLV (cls))
  830. __objc_resolve_class_links ();
  831. /* Make sure the super class has its dispatch table installed or is
  832. at least preparing. We do not need to send initialize for the
  833. super class since __objc_send_initialize will insure that. */
  834. if (cls->super_class
  835. && cls->super_class->dtable == __objc_uninstalled_dtable
  836. && !__objc_prepared_dtable_for_class (cls->super_class))
  837. {
  838. __objc_install_dtable_for_class (cls->super_class);
  839. /* The superclass initialisation may have also initialised the
  840. current class, in which case there is no more to do. */
  841. if (cls->dtable != __objc_uninstalled_dtable)
  842. return;
  843. }
  844. /* We have already been prepared but +initialize hasn't completed.
  845. The +initialize implementation is probably sending 'self'
  846. messages. We rely on _objc_get_prepared_imp to retrieve the
  847. implementation pointers. */
  848. if (__objc_prepared_dtable_for_class (cls))
  849. return;
  850. /* We have this function cache the implementation pointers for
  851. _objc_get_prepared_imp but the dispatch table won't be initilized
  852. until __objc_send_initialize completes. */
  853. __objc_prepare_dtable_for_class (cls);
  854. /* We may have already invoked +initialize but
  855. __objc_update_dispatch_table_for_class invoked by
  856. class_add_method_list may have reset dispatch table. */
  857. /* Call +initialize. If we are a real class, we are installing
  858. instance methods. If we are a meta class, we are installing
  859. class methods. The __objc_send_initialize itself will insure
  860. that the message is called only once per class. */
  861. if (CLS_ISCLASS (cls))
  862. __objc_send_initialize (cls);
  863. else
  864. {
  865. /* Retrieve the class from the meta class. */
  866. Class c = objc_getClass (cls->name);
  867. assert (CLS_ISMETA (cls));
  868. assert (c);
  869. __objc_send_initialize (c);
  870. }
  871. /* We install the dispatch table correctly when +initialize completed. */
  872. __objc_install_prepared_dtable_for_class (cls);
  873. }
  874. /* Builds the dispatch table for the class CLS and stores it in a
  875. place where it can be retrieved by __objc_get_prepared_imp until
  876. __objc_install_prepared_dtable_for_class installs it into the
  877. class. The dispatch table should not be installed into the class
  878. until +initialize has completed. */
  879. static void
  880. __objc_prepare_dtable_for_class (Class cls)
  881. {
  882. struct sarray *dtable;
  883. struct sarray *super_dtable;
  884. /* This table could be initialized in init.c. We cannot use the
  885. class name since the class maintains the instance methods and the
  886. meta class maintains the the class methods yet both share the
  887. same name. Classes should be unique in any program. */
  888. if (! prepared_dtable_table)
  889. prepared_dtable_table
  890. = objc_hash_new (32,
  891. (hash_func_type) objc_hash_ptr,
  892. (compare_func_type) objc_compare_ptrs);
  893. /* If the class has not yet had its class links resolved, we must
  894. re-compute all class links. */
  895. if (! CLS_ISRESOLV (cls))
  896. __objc_resolve_class_links ();
  897. assert (cls);
  898. assert (cls->dtable == __objc_uninstalled_dtable);
  899. /* If there is already a prepared dtable for this class, we must
  900. replace it with a new version (since there must have been methods
  901. added to or otherwise modified in the class while executing
  902. +initialize, and the table needs to be recomputed. */
  903. dtable = __objc_prepared_dtable_for_class (cls);
  904. if (dtable != 0)
  905. {
  906. objc_hash_remove (prepared_dtable_table, cls);
  907. sarray_free (dtable);
  908. }
  909. /* Now prepare the dtable for population. */
  910. assert (cls != cls->super_class);
  911. if (cls->super_class)
  912. {
  913. /* Inherit the method list from the super class. Yet the super
  914. class may still be initializing in the case when a class
  915. cluster sub class initializes its super classes. */
  916. if (cls->super_class->dtable == __objc_uninstalled_dtable)
  917. __objc_install_dtable_for_class (cls->super_class);
  918. super_dtable = cls->super_class->dtable;
  919. /* If the dispatch table is not yet installed, we are still in
  920. the process of executing +initialize. Yet the dispatch table
  921. should be available. */
  922. if (super_dtable == __objc_uninstalled_dtable)
  923. super_dtable = __objc_prepared_dtable_for_class (cls->super_class);
  924. assert (super_dtable);
  925. dtable = sarray_lazy_copy (super_dtable);
  926. }
  927. else
  928. dtable = sarray_new (__objc_selector_max_index, 0);
  929. __objc_install_methods_in_dtable (dtable, cls->methods);
  930. objc_hash_add (&prepared_dtable_table,
  931. cls,
  932. dtable);
  933. }
  934. /* This wrapper only exists to allow an easy replacement of the lookup
  935. implementation and it is expected that the compiler will optimize
  936. it away. */
  937. static struct sarray *
  938. __objc_prepared_dtable_for_class (Class cls)
  939. {
  940. struct sarray *dtable = 0;
  941. assert (cls);
  942. if (prepared_dtable_table)
  943. dtable = objc_hash_value_for_key (prepared_dtable_table, cls);
  944. /* dtable my be nil, since we call this to check whether we are
  945. currently preparing before we start preparing. */
  946. return dtable;
  947. }
  948. /* Helper function for messages sent to CLS or implementation pointers
  949. retrieved from CLS during +initialize before the dtable is
  950. installed. When a class implicitly initializes another class which
  951. in turn implicitly invokes methods in this class, before the
  952. implementation of +initialize of CLS completes, this returns the
  953. expected implementation. Forwarding remains the responsibility of
  954. objc_msg_lookup. This function should only be called under the
  955. global lock. */
  956. static IMP
  957. __objc_get_prepared_imp (Class cls,SEL sel)
  958. {
  959. struct sarray *dtable;
  960. IMP imp;
  961. assert (cls);
  962. assert (sel);
  963. assert (cls->dtable == __objc_uninstalled_dtable);
  964. dtable = __objc_prepared_dtable_for_class (cls);
  965. assert (dtable);
  966. assert (dtable != __objc_uninstalled_dtable);
  967. imp = sarray_get_safe (dtable, (size_t) sel->sel_id);
  968. /* imp may be Nil if the method does not exist and we may fallback
  969. to the forwarding implementation later. */
  970. return imp;
  971. }
  972. /* When this function is called +initialize should be completed. So
  973. now we are safe to install the dispatch table for the class so that
  974. they become available for other threads that may be waiting in the
  975. lock. */
  976. static void
  977. __objc_install_prepared_dtable_for_class (Class cls)
  978. {
  979. assert (cls);
  980. assert (cls->dtable == __objc_uninstalled_dtable);
  981. cls->dtable = __objc_prepared_dtable_for_class (cls);
  982. assert (cls->dtable);
  983. assert (cls->dtable != __objc_uninstalled_dtable);
  984. objc_hash_remove (prepared_dtable_table, cls);
  985. }