ivars.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* GNU Objective C Runtime ivar related functions.
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. Contributed by Nicola Pero
  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. #include "objc-private/common.h"
  20. #include "objc/runtime.h"
  21. #include "objc-private/module-abi-8.h" /* For runtime structures */
  22. #include "objc/thr.h"
  23. #include "objc-private/runtime.h" /* the kitchen sink */
  24. #include <string.h> /* For strcmp. */
  25. #include <stdlib.h> /* For malloc. */
  26. struct objc_ivar *
  27. class_getInstanceVariable (Class class_, const char *name)
  28. {
  29. if (class_ != Nil && name != NULL && ! CLS_IS_IN_CONSTRUCTION (class_))
  30. {
  31. while (class_ != Nil)
  32. {
  33. struct objc_ivar_list *ivars = class_->ivars;
  34. if (ivars != NULL)
  35. {
  36. int i;
  37. for (i = 0; i < ivars->ivar_count; i++)
  38. {
  39. struct objc_ivar *ivar = &(ivars->ivar_list[i]);
  40. if (!strcmp (ivar->ivar_name, name))
  41. return ivar;
  42. }
  43. }
  44. class_ = class_getSuperclass (class_);
  45. }
  46. }
  47. return NULL;
  48. }
  49. struct objc_ivar *
  50. class_getClassVariable (Class class_, const char *name)
  51. {
  52. if (class_ == Nil)
  53. return NULL;
  54. /* Logically, since a class is an instance of its meta-class, and
  55. since its class methods are the instance methods of the
  56. meta-class, class variables should be instance variables of the
  57. meta-class. That is different from the normal use of having
  58. 'static' variables in the class implementation file, because
  59. every class would have its own variables.
  60. Anyway, it is all speculative at this stage, but if we get class
  61. variables in Objective-C, it is conceivable that this
  62. implementation should work. */
  63. return class_getInstanceVariable (class_->class_pointer, name);
  64. }
  65. void *
  66. object_getIndexedIvars (id object)
  67. {
  68. if (object == nil)
  69. return NULL;
  70. else
  71. return (void *)(((char *)object)
  72. + object->class_pointer->instance_size);
  73. }
  74. struct objc_ivar *
  75. object_getInstanceVariable (id object, const char *name, void **returnValue)
  76. {
  77. if (object == nil || name == NULL)
  78. return NULL;
  79. else
  80. {
  81. struct objc_ivar * variable = class_getInstanceVariable (object->class_pointer, name);
  82. if (variable != NULL && returnValue != NULL)
  83. {
  84. char *location = (char *)object + variable->ivar_offset;
  85. *returnValue = *((id *)location);
  86. }
  87. return variable;
  88. }
  89. }
  90. struct objc_ivar *
  91. object_setInstanceVariable (id object, const char *name, void *newValue)
  92. {
  93. if (object == nil || name == NULL)
  94. return NULL;
  95. else
  96. {
  97. struct objc_ivar * variable = class_getInstanceVariable (object->class_pointer, name);
  98. if (variable != NULL)
  99. {
  100. char *location = (char *)object + variable->ivar_offset;
  101. *((id *)location) = (id)newValue;
  102. }
  103. return variable;
  104. }
  105. }
  106. id object_getIvar (id object, struct objc_ivar * variable)
  107. {
  108. if (object == nil || variable == NULL)
  109. return nil;
  110. else
  111. {
  112. char *location = (char *)object + variable->ivar_offset;
  113. return *((id *)location);
  114. }
  115. }
  116. void object_setIvar (id object, struct objc_ivar * variable, id value)
  117. {
  118. if (object == nil || variable == NULL)
  119. return;
  120. else
  121. {
  122. char *location = (char *)object + variable->ivar_offset;
  123. *((id *)location) = value;
  124. }
  125. }
  126. const char * ivar_getName (struct objc_ivar * variable)
  127. {
  128. if (variable == NULL)
  129. return NULL;
  130. return variable->ivar_name;
  131. }
  132. ptrdiff_t ivar_getOffset (struct objc_ivar * variable)
  133. {
  134. if (variable == NULL)
  135. return 0;
  136. return (ptrdiff_t)(variable->ivar_offset);
  137. }
  138. const char * ivar_getTypeEncoding (struct objc_ivar * variable)
  139. {
  140. if (variable == NULL)
  141. return NULL;
  142. return variable->ivar_type;
  143. }
  144. struct objc_ivar ** class_copyIvarList (Class class_, unsigned int *numberOfReturnedIvars)
  145. {
  146. unsigned int count = 0;
  147. struct objc_ivar **returnValue = NULL;
  148. struct objc_ivar_list* ivar_list;
  149. if (class_ == Nil || CLS_IS_IN_CONSTRUCTION (class_) || !class_->ivars)
  150. {
  151. if (numberOfReturnedIvars)
  152. *numberOfReturnedIvars = 0;
  153. return NULL;
  154. }
  155. /* Count how many ivars we have. */
  156. ivar_list = class_->ivars;
  157. count = ivar_list->ivar_count;
  158. if (count != 0)
  159. {
  160. unsigned int i = 0;
  161. /* Allocate enough memory to hold them. */
  162. returnValue = (struct objc_ivar **)(malloc (sizeof (struct objc_ivar *) * (count + 1)));
  163. /* Copy the ivars. */
  164. for (i = 0; i < count; i++)
  165. returnValue[i] = &(ivar_list->ivar_list[i]);
  166. returnValue[i] = NULL;
  167. }
  168. if (numberOfReturnedIvars)
  169. *numberOfReturnedIvars = count;
  170. return returnValue;
  171. }
  172. BOOL
  173. class_addIvar (Class class_, const char * ivar_name, size_t size,
  174. unsigned char log_2_of_alignment, const char *type)
  175. {
  176. struct objc_ivar_list *ivars;
  177. if (class_ == Nil
  178. || (! CLS_IS_IN_CONSTRUCTION (class_))
  179. || ivar_name == NULL
  180. || (strcmp (ivar_name, "") == 0)
  181. || size == 0
  182. || type == NULL)
  183. return NO;
  184. /* Check if the class has an instance variable with that name
  185. already. */
  186. ivars = class_->ivars;
  187. if (ivars != NULL)
  188. {
  189. int i;
  190. for (i = 0; i < ivars->ivar_count; i++)
  191. {
  192. struct objc_ivar *ivar = &(ivars->ivar_list[i]);
  193. if (strcmp (ivar->ivar_name, ivar_name) == 0)
  194. return NO;
  195. }
  196. }
  197. /* Ok, no direct ivars. Check superclasses. */
  198. if (class_getInstanceVariable (objc_getClass ((char *)(class_->super_class)),
  199. ivar_name))
  200. return NO;
  201. /* Good. Create space for the new instance variable. */
  202. if (ivars)
  203. {
  204. int ivar_count = ivars->ivar_count + 1;
  205. int new_size = sizeof (struct objc_ivar_list)
  206. + (ivar_count - 1) * sizeof (struct objc_ivar);
  207. ivars = (struct objc_ivar_list*) objc_realloc (ivars, new_size);
  208. ivars->ivar_count = ivar_count;
  209. class_->ivars = ivars;
  210. }
  211. else
  212. {
  213. int new_size = sizeof (struct objc_ivar_list);
  214. ivars = (struct objc_ivar_list*) objc_malloc (new_size);
  215. ivars->ivar_count = 1;
  216. class_->ivars = ivars;
  217. }
  218. /* Now ivars is set to a list of instance variables of the right
  219. size. */
  220. {
  221. struct objc_ivar *ivar = &(ivars->ivar_list[ivars->ivar_count - 1]);
  222. unsigned int alignment = 1 << log_2_of_alignment;
  223. int misalignment;
  224. ivar->ivar_name = objc_malloc (strlen (ivar_name) + 1);
  225. strcpy ((char *)ivar->ivar_name, ivar_name);
  226. ivar->ivar_type = objc_malloc (strlen (type) + 1);
  227. strcpy ((char *)ivar->ivar_type, type);
  228. /* The new instance variable is placed at the end of the existing
  229. instance_size, at the first byte that is aligned with
  230. alignment. */
  231. misalignment = class_->instance_size % alignment;
  232. if (misalignment == 0)
  233. ivar->ivar_offset = class_->instance_size;
  234. else
  235. ivar->ivar_offset = class_->instance_size - misalignment + alignment;
  236. class_->instance_size = ivar->ivar_offset + size;
  237. }
  238. return YES;
  239. }
  240. const char *
  241. property_getName (struct objc_property * property __attribute__ ((__unused__)))
  242. {
  243. if (property == NULL)
  244. return NULL;
  245. /* TODO: New ABI. */
  246. /* The current ABI does not have any information on properties. */
  247. return NULL;
  248. }
  249. const char *
  250. property_getAttributes (struct objc_property * property __attribute__ ((__unused__)))
  251. {
  252. if (property == NULL)
  253. return NULL;
  254. /* TODO: New ABI. */
  255. /* The current ABI does not have any information on properties. */
  256. return NULL;
  257. }
  258. struct objc_property *
  259. class_getProperty (Class class_ __attribute__ ((__unused__)),
  260. const char *propertyName __attribute__ ((__unused__)))
  261. {
  262. if (class_ == NULL || propertyName == NULL)
  263. return NULL;
  264. /* TODO: New ABI. */
  265. /* The current ABI does not have any information on class properties. */
  266. return NULL;
  267. }
  268. struct objc_property **
  269. class_copyPropertyList (Class class_ __attribute__ ((__unused__)),
  270. unsigned int *numberOfReturnedProperties __attribute__ ((__unused__)))
  271. {
  272. if (class_ == Nil)
  273. {
  274. if (numberOfReturnedProperties)
  275. *numberOfReturnedProperties = 0;
  276. return NULL;
  277. }
  278. /* TODO: New ABI. */
  279. /* The current ABI does not have any information on class properties. */
  280. if (numberOfReturnedProperties)
  281. *numberOfReturnedProperties = 0;
  282. return NULL;
  283. }
  284. const char *
  285. class_getIvarLayout (Class class_ __attribute__ ((__unused__)))
  286. {
  287. return NULL;
  288. }
  289. const char *
  290. class_getWeakIvarLayout (Class class_ __attribute__ ((__unused__)))
  291. {
  292. return NULL;
  293. }
  294. void
  295. class_setIvarLayout (Class class_ __attribute__ ((__unused__)),
  296. const char *layout __attribute__ ((__unused__)))
  297. {
  298. return;
  299. }
  300. void
  301. class_setWeakIvarLayout (Class class_ __attribute__ ((__unused__)),
  302. const char *layout __attribute__ ((__unused__)))
  303. {
  304. return;
  305. }