sendmsg.c 38 KB

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