runtime.h 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. /* GNU Objective-C Runtime API - Modern API
  2. Copyright (C) 2010-2019 Free Software Foundation, Inc.
  3. Contributed by Nicola Pero <nicola.pero@meta-innovation.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 3, or (at your option) any
  8. later version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  12. License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #ifndef __objc_runtime_INCLUDE_GNU
  21. #define __objc_runtime_INCLUDE_GNU
  22. /*
  23. This file declares the "modern" GNU Objective-C Runtime API.
  24. This API replaced the "traditional" GNU Objective-C Runtime API
  25. (which used to be declared in objc/objc-api.h) which is the one
  26. supported by older versions of the GNU Objective-C Runtime. The
  27. "modern" API is very similar to the API used by the modern
  28. Apple/NeXT runtime.
  29. */
  30. #include "objc.h"
  31. #include "objc-decls.h"
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif /* __cplusplus */
  35. /* An 'Ivar' represents an instance variable. It holds information
  36. about the name, type and offset of the instance variable. */
  37. typedef struct objc_ivar *Ivar;
  38. /* A 'Property' represents a property. It holds information about the
  39. name of the property, and its attributes.
  40. Compatibility Note: the Apple/NeXT runtime defines this as
  41. objc_property_t, so we define it that way as well, but obviously
  42. Property is the right name. */
  43. typedef struct objc_property *Property;
  44. typedef struct objc_property *objc_property_t;
  45. /* A 'Method' represents a method. It holds information about the
  46. name, types and the IMP of the method. */
  47. typedef struct objc_method *Method;
  48. /* A 'Category' represents a category. It holds information about the
  49. name of the category, the class it belongs to, and the methods,
  50. protocols and such like provided by the category. */
  51. typedef struct objc_category *Category;
  52. /* 'Protocol' is defined in objc/objc.h (which is included by this
  53. file). */
  54. /* Method descriptor returned by introspective Object methods. At the
  55. moment, this is really just the first part of the more complete
  56. objc_method structure used internally by the runtime. (PS: In the
  57. GNU Objective-C Runtime, selectors already include a type, so an
  58. objc_method_description does not add much to a SEL. But in other
  59. runtimes, that is not the case, which is why
  60. objc_method_description exists). */
  61. struct objc_method_description
  62. {
  63. SEL name; /* Selector (name and signature) */
  64. char *types; /* Type encoding */
  65. };
  66. /* The following are used in encode strings to describe the type of
  67. Ivars and Methods. */
  68. #define _C_ID '@'
  69. #define _C_CLASS '#'
  70. #define _C_SEL ':'
  71. #define _C_CHR 'c'
  72. #define _C_UCHR 'C'
  73. #define _C_SHT 's'
  74. #define _C_USHT 'S'
  75. #define _C_INT 'i'
  76. #define _C_UINT 'I'
  77. #define _C_LNG 'l'
  78. #define _C_ULNG 'L'
  79. #define _C_LNG_LNG 'q'
  80. #define _C_ULNG_LNG 'Q'
  81. #define _C_FLT 'f'
  82. #define _C_DBL 'd'
  83. #define _C_LNG_DBL 'D'
  84. #define _C_BFLD 'b'
  85. #define _C_BOOL 'B'
  86. #define _C_VOID 'v'
  87. #define _C_UNDEF '?'
  88. #define _C_PTR '^'
  89. #define _C_CHARPTR '*'
  90. #define _C_ARY_B '['
  91. #define _C_ARY_E ']'
  92. #define _C_UNION_B '('
  93. #define _C_UNION_E ')'
  94. #define _C_STRUCT_B '{'
  95. #define _C_STRUCT_E '}'
  96. #define _C_VECTOR '!'
  97. #define _C_COMPLEX 'j'
  98. /* _C_ATOM is never generated by the compiler. You can treat it as
  99. equivalent to "*". */
  100. #define _C_ATOM '%'
  101. /* The following are used in encode strings to describe some
  102. qualifiers of method and ivar types. */
  103. #define _C_CONST 'r'
  104. #define _C_IN 'n'
  105. #define _C_INOUT 'N'
  106. #define _C_OUT 'o'
  107. #define _C_BYCOPY 'O'
  108. #define _C_BYREF 'R'
  109. #define _C_ONEWAY 'V'
  110. #define _C_GCINVISIBLE '|'
  111. /* The same when used as flags. */
  112. #define _F_CONST 0x01
  113. #define _F_IN 0x01
  114. #define _F_OUT 0x02
  115. #define _F_INOUT 0x03
  116. #define _F_BYCOPY 0x04
  117. #define _F_BYREF 0x08
  118. #define _F_ONEWAY 0x10
  119. #define _F_GCINVISIBLE 0x20
  120. /** Implementation: the following functions are defined inline. */
  121. /* Return the class of 'object', or Nil if the object is nil. If
  122. 'object' is a class, the meta class is returned; if 'object' is a
  123. meta class, the root meta class is returned (note that this is
  124. different from the traditional GNU Objective-C Runtime API function
  125. object_get_class(), which for a meta class would return the meta
  126. class itself). This function is inline, so it is really fast and
  127. should be used instead of accessing object->class_pointer
  128. directly. */
  129. static inline Class
  130. object_getClass (id object)
  131. {
  132. if (object != nil)
  133. return object->class_pointer;
  134. else
  135. return Nil;
  136. }
  137. /** Implementation: the following functions are in selector.c. */
  138. /* Return the name of a given selector. If 'selector' is NULL, return
  139. "<null selector>". */
  140. objc_EXPORT const char *sel_getName (SEL selector);
  141. /* Return the type of a given selector. Return NULL if selector is
  142. NULL.
  143. Compatibility Note: the Apple/NeXT runtime has untyped selectors,
  144. so it does not have this function, which is specific to the GNU
  145. Runtime. */
  146. objc_EXPORT const char *sel_getTypeEncoding (SEL selector);
  147. /* This is the same as sel_registerName (). Please use
  148. sel_registerName () instead. */
  149. objc_EXPORT SEL sel_getUid (const char *name);
  150. /* Register a selector with a given name (but unspecified types). If
  151. you know the types, it is better to call sel_registerTypedName().
  152. If a selector with this name and no types already exists, it is
  153. returned. Note that this function should really be called
  154. 'objc_registerSelector'. Return NULL if 'name' is NULL. */
  155. objc_EXPORT SEL sel_registerName (const char *name);
  156. /* Register a selector with a given name and types. If a selector
  157. with this name and types already exists, it is returned. Note that
  158. this function should really be called 'objc_registerTypedSelector',
  159. and it's called 'sel_registerTypedName' only for consistency with
  160. 'sel_registerName'. Return NULL if 'name' is NULL.
  161. Compatibility Note: the Apple/NeXT runtime has untyped selectors,
  162. so it does not have this function, which is specific to the GNU
  163. Runtime. */
  164. objc_EXPORT SEL sel_registerTypedName (const char *name, const char *type);
  165. /* Return YES if first_selector is the same as second_selector, and NO
  166. if not. */
  167. objc_EXPORT BOOL sel_isEqual (SEL first_selector, SEL second_selector);
  168. /* Return all the selectors with the supplied name. In the GNU
  169. runtime, selectors are typed and there may be multiple selectors
  170. with the same name but a different type. The return value of the
  171. function is a pointer to an area, allocated with malloc(), that
  172. contains all the selectors with the supplier name known to the
  173. runtime. The list is terminated by NULL. Optionally, if you pass
  174. a non-NULL 'numberOfReturnedSelectors' pointer, the unsigned int
  175. that it points to will be filled with the number of selectors
  176. returned.
  177. Compatibility Note: the Apple/NeXT runtime has untyped selectors,
  178. so it does not have this function, which is specific to the GNU
  179. Runtime. */
  180. objc_EXPORT SEL * sel_copyTypedSelectorList (const char *name,
  181. unsigned int *numberOfReturnedSelectors);
  182. /* Return a selector with name 'name' and a non-zero type encoding, if
  183. there is a single selector with a type, and with that name,
  184. registered with the runtime. If there is no such selector, or if
  185. there are multiple selectors with the same name but conflicting
  186. types, NULL is returned. Return NULL if 'name' is NULL.
  187. This is useful if you have the name of the selector, and would
  188. really like to get a selector for it that includes the type
  189. encoding. Unfortunately, if the program contains multiple selector
  190. with the same name but different types, sel_getTypedSelector cannot
  191. possibly know which one you need, and so will return NULL.
  192. Compatibility Note: the Apple/NeXT runtime has untyped selectors,
  193. so it does not have this function, which is specific to the GNU
  194. Runtime. */
  195. objc_EXPORT SEL sel_getTypedSelector (const char *name);
  196. /** Implementation: the following functions are in objects.c. */
  197. /* Create an instance of class 'class_', adding extraBytes to the size
  198. of the returned object. This method allocates the appropriate
  199. amount of memory for the instance, initializes it to zero, then
  200. calls all the C++ constructors on appropriate C++ instance
  201. variables of the instance (if any) (TODO: The C++ constructors bit
  202. is not implemented yet). */
  203. objc_EXPORT id class_createInstance (Class class_, size_t extraBytes);
  204. /* Copy an object and return the copy. extraBytes should be identical
  205. to the extraBytes parameter that was passed when creating the
  206. original object. */
  207. objc_EXPORT id object_copy (id object, size_t extraBytes);
  208. /* Dispose of an object. This method calls the appropriate C++
  209. destructors on appropriate C++ instance variables of the instance
  210. (if any) (TODO: This is not implemented yet), then frees the memory
  211. for the instance. */
  212. objc_EXPORT id object_dispose (id object);
  213. /* Return the name of the class of 'object'. If 'object' is 'nil',
  214. returns "Nil". */
  215. objc_EXPORT const char * object_getClassName (id object);
  216. /* Change the class of object to be class_. Return the previous class
  217. of object. This is currently not really thread-safe. */
  218. objc_EXPORT Class object_setClass (id object, Class class_);
  219. /** Implementation: the following functions are in ivars.c. */
  220. /* Return an instance variable given the class and the instance
  221. variable name. This is an expensive function to call, so try to
  222. reuse the returned Ivar if you can. */
  223. objc_EXPORT Ivar class_getInstanceVariable (Class class_, const char *name);
  224. /* Return a class variable given the class and the class variable
  225. name. This is an expensive function to call, so try to reuse the
  226. returned Ivar if you can.
  227. This function always returns NULL since class variables are
  228. currently unavailable in Objective-C. */
  229. objc_EXPORT Ivar class_getClassVariable (Class class_, const char *name);
  230. /* If the object was created in class_createInstance() with some
  231. extraBytes, returns a pointer to them. If it was not, then the
  232. returned pointer may make no sense. */
  233. objc_EXPORT void * object_getIndexedIvars (id object);
  234. /* Get the value of an instance variable of type 'id'. The function
  235. returns the instance variable. To get the value of the instance
  236. variable, you should pass as 'returnValue' a pointer to an 'id';
  237. the value will be copied there. Note that 'returnValue' is really
  238. a 'void *', not a 'void **'. This function really works only with
  239. instance variables of type 'id'; for other types of instance
  240. variables, access directly the data at (char *)object +
  241. ivar_getOffset (ivar). */
  242. objc_EXPORT Ivar object_getInstanceVariable (id object, const char *name, void **returnValue);
  243. /* Set the value of an instance variable. The value to set is passed
  244. in 'newValue' (which really is an 'id', not a 'void *'). The
  245. function returns the instance variable. This function really works
  246. only with instance variables of type 'id'; for other types of
  247. instance variables, access directly the data at (char *)object +
  248. ivar_getOffset (ivar). */
  249. objc_EXPORT Ivar object_setInstanceVariable (id object, const char *name, void *newValue);
  250. /* Get the value of an instance variable of type 'id' of the object
  251. 'object'. This is faster than object_getInstanceVariable if you
  252. already have the instance variable because it avoids the expensive
  253. call to class_getInstanceVariable that is done by
  254. object_getInstanceVariable. */
  255. objc_EXPORT id object_getIvar (id object, Ivar variable);
  256. /* Set the value of an instance variable of type 'id' of the object
  257. 'object'. This is faster than object_setInstanceVariable if you
  258. already have the instance variable because it avoids the expensive
  259. call to class_getInstanceVariable that is done by
  260. object_setInstanceVariable. */
  261. objc_EXPORT void object_setIvar (id object, Ivar variable, id value);
  262. /* Return the name of the instance variable. Return NULL if
  263. 'variable' is NULL. */
  264. objc_EXPORT const char * ivar_getName (Ivar variable);
  265. /* Return the offset of the instance variable from the start of the
  266. object data. Return 0 if 'variable' is NULL. */
  267. objc_EXPORT ptrdiff_t ivar_getOffset (Ivar variable);
  268. /* Return the type encoding of the variable. Return NULL if
  269. 'variable' is NULL. */
  270. objc_EXPORT const char * ivar_getTypeEncoding (Ivar variable);
  271. /* Return all the instance variables of the class. The return value
  272. of the function is a pointer to an area, allocated with malloc(),
  273. that contains all the instance variables of the class. It does not
  274. include instance variables of superclasses. The list is terminated
  275. by NULL. Optionally, if you pass a non-NULL
  276. 'numberOfReturnedIvars' pointer, the unsigned int that it points to
  277. will be filled with the number of instance variables returned.
  278. Return NULL for classes still in construction (ie, allocated using
  279. objc_allocatedClassPair() but not yet registered with the runtime
  280. using objc_registerClassPair()). */
  281. objc_EXPORT Ivar * class_copyIvarList (Class class_, unsigned int *numberOfReturnedIvars);
  282. /* Add an instance variable with name 'ivar_name' to class 'class_',
  283. where 'class_' is a class in construction that has been created
  284. using objc_allocateClassPair() and has not been registered with the
  285. runtime using objc_registerClassPair() yet. You cannot add
  286. instance variables to classes already registered with the runtime.
  287. 'size' is the size of the instance variable, 'log_2_of_alignment'
  288. the alignment as a power of 2 (so 0 means alignment to a 1 byte
  289. boundary, 1 means alignment to a 2 byte boundary, 2 means alignment
  290. to a 4 byte boundary, etc), and 'type' the type encoding of the
  291. variable type. You can use sizeof(), log2(__alignof__()) and
  292. @encode() to determine the right 'size', 'alignment' and 'type' for
  293. your instance variable. For example, to add an instance variable
  294. name "my_variable" and of type 'id', you can use:
  295. class_addIvar (class, "my_variable", sizeof (id), log2 ( __alignof__ (id)),
  296. @encode (id));
  297. Return YES if the variable was added, and NO if not. In
  298. particular, return NO if 'class_' is Nil, or a meta-class or a
  299. class not in construction. Return Nil also if 'ivar_name' or
  300. 'type' is NULL, or 'size' is 0.
  301. */
  302. objc_EXPORT BOOL class_addIvar (Class class_, const char * ivar_name, size_t size,
  303. unsigned char log_2_of_alignment, const char *type);
  304. /* Return the name of the property. Return NULL if 'property' is
  305. NULL. */
  306. objc_EXPORT const char * property_getName (Property property);
  307. /* Return the attributes of the property as a string. Return NULL if
  308. 'property' is NULL. */
  309. objc_EXPORT const char * property_getAttributes (Property property);
  310. /* Return the property with name 'propertyName' of the class 'class_'.
  311. This function returns NULL if the required property cannot be
  312. found. Return NULL if 'class_' or 'propertyName' is NULL.
  313. Note that the traditional ABI does not store the list of properties
  314. of a class in a compiled module, so the traditional ABI will always
  315. return NULL. */
  316. objc_EXPORT Property class_getProperty (Class class_, const char *propertyName);
  317. /* Return all the properties of the class. The return value
  318. of the function is a pointer to an area, allocated with malloc(),
  319. that contains all the properties of the class. It does not
  320. include properties of superclasses. The list is terminated
  321. by NULL. Optionally, if you pass a non-NULL
  322. 'numberOfReturnedIvars' pointer, the unsigned int that it points to
  323. will be filled with the number of properties returned.
  324. Note that the traditional ABI does not store the list of properties
  325. of a class in a compiled module, so the traditional ABI will always
  326. return an empty list. */
  327. objc_EXPORT Property * class_copyPropertyList
  328. (Class class_, unsigned int *numberOfReturnedProperties);
  329. /* Return the ivar layout for class 'class_'.
  330. At the moment this function always returns NULL. */
  331. objc_EXPORT const char * class_getIvarLayout (Class class_);
  332. /* Return the weak ivar layout for class 'class_'.
  333. At the moment this function always returns NULL. */
  334. objc_EXPORT const char * class_getWeakIvarLayout (Class class_);
  335. /* Set the ivar layout for class 'class_'.
  336. At the moment, this function does nothing. */
  337. objc_EXPORT void class_setIvarLayout (Class class_, const char *layout);
  338. /* Set the weak ivar layout for class 'class_'.
  339. At the moment, this function does nothing. With the GNU runtime,
  340. you should use class_ivar_set_gcinvisible () to hide variables from
  341. the Garbage Collector. */
  342. objc_EXPORT void class_setWeakIvarLayout (Class class_, const char *layout);
  343. /** Implementation: the following functions are in class.c. */
  344. /* Compatibility Note: The Apple/NeXT runtime does not have
  345. objc_get_unknown_class_handler and
  346. objc_setGetUnknownClassHandler(). They provide functionality that
  347. the traditional GNU Objective-C Runtime API used to provide via the
  348. _objc_lookup_class hook. */
  349. /* An 'objc_get_unknown_class_handler' function is used by
  350. objc_getClass() to get a class that is currently unknown to the
  351. compiler. You could use it for example to have the class loaded by
  352. dynamically loading a library. 'class_name' is the name of the
  353. class. The function should return the Class object if it manages to
  354. load the class, and Nil if not. */
  355. typedef Class (*objc_get_unknown_class_handler)(const char *class_name);
  356. /* Sets a new handler function for getting unknown classes (to be used
  357. by objc_getClass () and related), and returns the previous one.
  358. This function is not safe to call in a multi-threaded environment
  359. because other threads may be trying to use the get unknown class
  360. handler while you change it! */
  361. objc_EXPORT
  362. objc_get_unknown_class_handler
  363. objc_setGetUnknownClassHandler (objc_get_unknown_class_handler new_handler);
  364. /* Return the class with name 'name', if it is already registered with
  365. the runtime. If it is not registered, and
  366. objc_setGetUnknownClassHandler() has been called to set a handler
  367. for unknown classes, the handler is called to give it a chance to
  368. load the class in some other way. If the class is not known to the
  369. runtime and the handler is not set or returns Nil, objc_getClass()
  370. returns Nil. */
  371. objc_EXPORT Class objc_getClass (const char *name);
  372. /* Return the class with name 'name', if it is already registered with
  373. the runtime. Return Nil if not. This function does not call the
  374. objc_get_unknown_class_handler function if the class is not
  375. found. */
  376. objc_EXPORT Class objc_lookUpClass (const char *name);
  377. /* Return the meta class associated to the class with name 'name', if
  378. it is already registered with the runtime. First, it finds the
  379. class using objc_getClass(). Then, it returns the associated meta
  380. class. If the class could not be found using objc_getClass(),
  381. returns Nil. */
  382. objc_EXPORT Class objc_getMetaClass (const char *name);
  383. /* This is identical to objc_getClass(), but if the class is not found,
  384. it aborts the process instead of returning Nil. */
  385. objc_EXPORT Class objc_getRequiredClass (const char *name);
  386. /* If 'returnValue' is NULL, 'objc_getClassList' returns the number of
  387. classes currently registered with the runtime. If 'returnValue' is
  388. not NULL, it should be a (Class *) pointer to an area of memory
  389. which can contain up to 'maxNumberOfClassesToReturn' Class records.
  390. 'objc_getClassList' will fill the area pointed to by 'returnValue'
  391. with all the Classes registered with the runtime (or up to
  392. maxNumberOfClassesToReturn if there are more than
  393. maxNumberOfClassesToReturn). The function return value is the
  394. number of classes actually returned in 'returnValue'. */
  395. objc_EXPORT int objc_getClassList (Class *returnValue, int maxNumberOfClassesToReturn);
  396. /* Compatibility Note: The Apple/NeXT runtime also has
  397. Class objc_getFutureClass (const char *name);
  398. void objc_setFutureClass (Class class_, const char *name);
  399. the documentation is unclear on what they are supposed to do, and
  400. the GNU Objective-C Runtime currently does not provide them. */
  401. /* Return the name of the class 'class_', or the string "nil" if the
  402. class_ is Nil. */
  403. objc_EXPORT const char * class_getName (Class class_);
  404. /* Return YES if 'class_' is a meta class, and NO if not. If 'class_'
  405. is Nil, return NO. */
  406. objc_EXPORT BOOL class_isMetaClass (Class class_);
  407. /* Return the superclass of 'class_'. If 'class_' is Nil, or it is a
  408. root class, return Nil. This function also works if 'class_' is a
  409. class being constructed, that is, a class returned by
  410. objc_allocateClassPair() but before it has been registered with the
  411. runtime using objc_registerClassPair(). */
  412. objc_EXPORT Class class_getSuperclass (Class class_);
  413. /* Return the 'version' number of the class, which is an integer that
  414. can be used to track changes in the class API, methods and
  415. variables. If class_ is Nil, return 0. If class_ is not Nil, the
  416. version is 0 unless class_setVersion() has been called to set a
  417. different one.
  418. Please note that internally the version is a long, but the API only
  419. allows you to set and retrieve int values. */
  420. objc_EXPORT int class_getVersion (Class class_);
  421. /* Set the 'version' number of the class, which is an integer that can
  422. be used to track changes in the class API, methods and variables.
  423. If 'class_' is Nil, does nothing.
  424. This is typically used internally by "Foundation" libraries such as
  425. GNUstep Base to support serialization / deserialization of objects
  426. that work across changes in the classes. If you are using such a
  427. library, you probably want to use their versioning API, which may
  428. be based on this one, but is integrated with the rest of the
  429. library.
  430. Please note that internally the version is a long, but the API only
  431. allows you to set and retrieve int values. */
  432. objc_EXPORT void class_setVersion (Class class_, int version);
  433. /* Return the size in bytes (a byte is the size of a char) of an
  434. instance of the class. If class_ is Nil, return 0; else it return
  435. a non-zero number (since the 'isa' instance variable is required
  436. for all classes). */
  437. objc_EXPORT size_t class_getInstanceSize (Class class_);
  438. /* Change the implementation of the method. It also searches all
  439. classes for any class implementing the method, and replaces the
  440. existing implementation with the new one. For that to work,
  441. 'method' must be a method returned by class_getInstanceMethod() or
  442. class_getClassMethod() as the matching is done by comparing the
  443. pointers; in that case, only the implementation in the class is
  444. modified. Return the previous implementation that has been
  445. replaced. If method or implementation is NULL, do nothing and
  446. return NULL. */
  447. objc_EXPORT IMP
  448. method_setImplementation (Method method, IMP implementation);
  449. /* Swap the implementation of two methods in a single, atomic
  450. operation. This is equivalent to getting the implementation of
  451. each method and then calling method_setImplementation() on the
  452. other one. For this to work, the two methods must have been
  453. returned by class_getInstanceMethod() or class_getClassMethod().
  454. If 'method_a' or 'method_b' is NULL, do nothing. */
  455. objc_EXPORT void
  456. method_exchangeImplementations (Method method_a, Method method_b);
  457. /* Create a new class/meta-class pair. This function is called to
  458. create a new class at runtime. The class is created with
  459. superclass 'superclass' (use 'Nil' to create a new root class) and
  460. name 'class_name'. 'extraBytes' can be used to specify some extra
  461. space for indexed variables to be added at the end of the class and
  462. meta-class objects (it is recommended that you set extraBytes to
  463. 0). Once you have created the class, it is not usable yet. You
  464. need to add any instance variables (by using class_addIvar()), any
  465. instance methods (by using class_addMethod()) and any class methods
  466. (by using class_addMethod() on the meta-class, as in
  467. class_addMethod (object_getClass (class), method)) that are
  468. required, and then you need to call objc_registerClassPair() to
  469. activate the class. If you need to create a hierarchy of classes,
  470. you need to create and register them one at a time. You cannot
  471. create a new class using another class in construction as
  472. superclass. Return Nil if 'class-name' is NULL or if a class with
  473. that name already exists or 'superclass' is a class still in
  474. construction.
  475. Implementation Note: in the GNU runtime, allocating a class pair
  476. only creates the structures for the class pair, but does not
  477. register anything with the runtime. The class is registered with
  478. the runtime only when objc_registerClassPair() is called. In
  479. particular, if a class is in construction, objc_getClass() will not
  480. find it, the superclass will not know about it,
  481. class_getSuperclass() will return Nil and another thread may
  482. allocate a class pair with the same name; the conflict will only be
  483. detected when the classes are registered with the runtime.
  484. */
  485. objc_EXPORT Class
  486. objc_allocateClassPair (Class super_class, const char *class_name,
  487. size_t extraBytes);
  488. /* Register a class pair that was created with
  489. objc_allocateClassPair(). After you register a class, you can no
  490. longer make changes to its instance variables, but you can start
  491. creating instances of it. Do nothing if 'class_' is NULL or if it
  492. is not a class allocated by objc_allocateClassPair() and still in
  493. construction. */
  494. objc_EXPORT void
  495. objc_registerClassPair (Class class_);
  496. /* Dispose of a class pair created using objc_allocateClassPair().
  497. Call this function if you started creating a new class with
  498. objc_allocateClassPair() but then want to abort the process. You
  499. should not access 'class_' after calling this method. Note that if
  500. 'class_' has already been registered with the runtime via
  501. objc_registerClassPair(), this function does nothing; you can only
  502. dispose of class pairs that are still being constructed. Do
  503. nothing if class is 'Nil' or if 'class_' is not a class being
  504. constructed. */
  505. objc_EXPORT void
  506. objc_disposeClassPair (Class class_);
  507. /* Compatibility Note: The Apple/NeXT runtime has the function
  508. objc_duplicateClass () but it's undocumented. The GNU runtime does
  509. not have it. */
  510. /** Implementation: the following functions are in sendmsg.c. */
  511. /* Return the instance method with selector 'selector' of class
  512. 'class_', or NULL if the class (or one of its superclasses) does
  513. not implement the method. Return NULL if class_ is Nil or selector
  514. is NULL. Calling this function may trigger a call to
  515. +resolveInstanceMethod:, but does not return a forwarding
  516. function. */
  517. objc_EXPORT Method class_getInstanceMethod (Class class_, SEL selector);
  518. /* Return the class method with selector 'selector' of class 'class_',
  519. or NULL if the class (or one of its superclasses) does not
  520. implement the method. Return NULL if class_ is Nil or selector is
  521. NULL. Calling this function may trigger a call to
  522. +resolveClassMethod:, but does not return a forwarding
  523. function. */
  524. objc_EXPORT Method class_getClassMethod (Class class_, SEL selector);
  525. /* Return the IMP (pointer to the function implementing a method) for
  526. the instance method with selector 'selector' in class 'class_'.
  527. This is the same routine that is used while messaging, and should
  528. be very fast. Note that you most likely would need to cast the
  529. return function pointer to a function pointer with the appropriate
  530. arguments and return type before calling it. To get a class
  531. method, you can pass the meta-class as the class_ argument (ie, use
  532. class_getMethodImplementation (object_getClass (class_),
  533. selector)). Return NULL if class_ is Nil or selector is NULL.
  534. This function first looks for an existing method; if it is not
  535. found, it calls +resolveClassMethod: or +resolveInstanceMethod:
  536. (depending on whether a class or instance method is being looked
  537. up) if it is implemented. If the method returns YES, then it tries
  538. the look up again (the assumption being that +resolveClassMethod:
  539. or resolveInstanceMethod: will add the method using
  540. class_addMethod()). If it is still not found, it returns a
  541. forwarding function. */
  542. objc_EXPORT IMP class_getMethodImplementation (Class class_, SEL selector);
  543. /* Compatibility Note: the Apple/NeXT runtime has the function
  544. class_getMethodImplementation_stret () which currently does not
  545. exist on the GNU runtime because the messaging implementation is
  546. different. */
  547. /* Return YES if class 'class_' has an instance method implementing
  548. selector 'selector', and NO if not. Return NO if class_ is Nil or
  549. selector is NULL. If you need to check a class method, use the
  550. meta-class as the class_ argument (ie, use class_respondsToSelector
  551. (object_getClass (class_), selector)). */
  552. objc_EXPORT BOOL class_respondsToSelector (Class class_, SEL selector);
  553. /* Add a method to a class. Use this function to add a new method to
  554. a class (potentially overriding a method with the same selector in
  555. the superclass); if you want to modify an existing method, use
  556. method_setImplementation() instead (or class_replaceMethod ()).
  557. This method adds an instance method to 'class_'; to add a class
  558. method, get the meta class first, then add the method to the meta
  559. class, that is, use
  560. class_addMethod (object_getClass (class_), selector,
  561. implementation, type);
  562. Return YES if the method was added, and NO if not. Do nothing if
  563. one of the arguments is NULL. */
  564. objc_EXPORT BOOL class_addMethod (Class class_, SEL selector, IMP implementation,
  565. const char *method_types);
  566. /* Replace a method in a class. If the class already have a method
  567. with this 'selector', find it and use method_setImplementation() to
  568. replace the implementation with 'implementation' (method_types is
  569. ignored in that case). If the class does not already have a method
  570. with this 'selector', call 'class_addMethod() to add it.
  571. Return the previous implementation of the method, or NULL if none
  572. was found. Return NULL if any of the arguments is NULL. */
  573. objc_EXPORT IMP class_replaceMethod (Class class_, SEL selector, IMP implementation,
  574. const char *method_types);
  575. /** Implementation: the following functions are in methods.c. */
  576. /* Return the selector for method 'method'. Return NULL if 'method'
  577. is NULL.
  578. This function is misnamed; it should be called
  579. 'method_getSelector'. To get the actual name, get the selector,
  580. then the name from the selector (ie, use sel_getName
  581. (method_getName (method))). */
  582. objc_EXPORT SEL method_getName (Method method);
  583. /* Return the IMP of the method. Return NULL if 'method' is NULL. */
  584. objc_EXPORT IMP method_getImplementation (Method method);
  585. /* Return the type encoding of the method. Return NULL if 'method' is
  586. NULL. */
  587. objc_EXPORT const char * method_getTypeEncoding (Method method);
  588. /* Return a method description for the method. Return NULL if
  589. 'method' is NULL. */
  590. objc_EXPORT struct objc_method_description * method_getDescription (Method method);
  591. /* Return all the instance methods of the class. The return value of
  592. the function is a pointer to an area, allocated with malloc(), that
  593. contains all the instance methods of the class. It does not
  594. include instance methods of superclasses. The list is terminated
  595. by NULL. Optionally, if you pass a non-NULL
  596. 'numberOfReturnedMethods' pointer, the unsigned int that it points
  597. to will be filled with the number of instance methods returned. To
  598. get the list of class methods, pass the meta-class in the 'class_'
  599. argument, (ie, use class_copyMethodList (object_getClass (class_),
  600. &numberOfReturnedMethods)). */
  601. objc_EXPORT Method * class_copyMethodList (Class class_, unsigned int *numberOfReturnedMethods);
  602. /** Implementation: the following functions are in encoding.c. */
  603. /* Return the number of arguments that the method 'method' expects.
  604. Note that all methods need two implicit arguments ('self' for the
  605. receiver, and '_cmd' for the selector). Return 0 if 'method' is
  606. NULL. */
  607. objc_EXPORT unsigned int method_getNumberOfArguments (Method method);
  608. /* Return the string encoding for the return type of method 'method'.
  609. The string is a standard zero-terminated string in an area of
  610. memory allocated with malloc(); you should free it with free() when
  611. you finish using it. Return an empty string if method is NULL. */
  612. objc_EXPORT char * method_copyReturnType (Method method);
  613. /* Return the string encoding for the argument type of method
  614. 'method', argument number 'argumentNumber' ('argumentNumber' is 0
  615. for self, 1 for _cmd, and 2 or more for the additional arguments if
  616. any). The string is a standard zero-terminated string in an area
  617. of memory allocated with malloc(); you should free it with free()
  618. when you finish using it. Return an empty string if method is NULL
  619. or if 'argumentNumber' refers to a non-existing argument. */
  620. objc_EXPORT char * method_copyArgumentType (Method method, unsigned int argumentNumber);
  621. /* Return the string encoding for the return type of method 'method'.
  622. The string is returned by copying it into the supplied
  623. 'returnValue' string, which is of size 'returnValueSize'. No more
  624. than 'returnValueSize' characters are copied; if the encoding is
  625. smaller than 'returnValueSize', the rest of 'returnValue' is filled
  626. with zeros. If it is bigger, it is truncated (and would not be
  627. zero-terminated). You should supply a big enough
  628. 'returnValueSize'. If the method is NULL, returnValue is set to a
  629. string of zeros. */
  630. objc_EXPORT void method_getReturnType (Method method, char *returnValue,
  631. size_t returnValueSize);
  632. /* Return the string encoding for the argument type of method
  633. 'method', argument number 'argumentNumber' ('argumentNumber' is 0
  634. for self, 1 for _cmd, and 2 or more for the additional arguments if
  635. any). The string is returned by copying it into the supplied
  636. 'returnValue' string, which is of size 'returnValueSize'. No more
  637. than 'returnValueSize' characters are copied; if the encoding is
  638. smaller than 'returnValueSize', the rest of 'returnValue' is filled
  639. with zeros. If it is bigger, it is truncated (and would not be
  640. zero-terminated). You should supply a big enough
  641. 'returnValueSize'. If the method is NULL, returnValue is set to a
  642. string of zeros. */
  643. objc_EXPORT void method_getArgumentType (Method method, unsigned int argumentNumber,
  644. char *returnValue, size_t returnValueSize);
  645. /** Implementation: the following functions are in protocols.c. */
  646. /* Return the protocol with name 'name', or nil if it the protocol is
  647. not known to the runtime. */
  648. objc_EXPORT Protocol *objc_getProtocol (const char *name);
  649. /* Return all the protocols known to the runtime. The return value of
  650. the function is a pointer to an area, allocated with malloc(), that
  651. contains all the protocols known to the runtime; the list is
  652. terminated by NULL. You should free this area using free() once
  653. you no longer need it. Optionally, if you pass a non-NULL
  654. 'numberOfReturnedProtocols' pointer, the unsigned int that it
  655. points to will be filled with the number of protocols returned. If
  656. there are no protocols known to the runtime, NULL is returned. */
  657. objc_EXPORT Protocol **objc_copyProtocolList (unsigned int *numberOfReturnedProtocols);
  658. /* Add a protocol to a class, and return YES if it was done
  659. successfully, and NO if not. At the moment, NO should only happen
  660. if class_ or protocol are nil, if the protocol is not a Protocol
  661. object or if the class already conforms to the protocol. */
  662. objc_EXPORT BOOL class_addProtocol (Class class_, Protocol *protocol);
  663. /* Return YES if the class 'class_' conforms to Protocol 'protocol',
  664. and NO if not. This function does not check superclasses; if you
  665. want to check for superclasses (in the way that [NSObject
  666. +conformsToProtocol:] does) you need to iterate over the class
  667. hierarchy using class_getSuperclass(), and call
  668. class_conformsToProtocol() for each of them. */
  669. objc_EXPORT BOOL class_conformsToProtocol (Class class_, Protocol *protocol);
  670. /* Return all the protocols that the class conforms to. The return
  671. value of the function is a pointer to an area, allocated with
  672. malloc(), that contains all the protocols formally adopted by the
  673. class. It does not include protocols adopted by superclasses. The
  674. list is terminated by NULL. Optionally, if you pass a non-NULL
  675. 'numberOfReturnedProtocols' pointer, the unsigned int that it
  676. points to will be filled with the number of protocols returned.
  677. This function does not return protocols that superclasses conform
  678. to. */
  679. objc_EXPORT Protocol **class_copyProtocolList (Class class_, unsigned int *numberOfReturnedProtocols);
  680. /* Return YES if protocol 'protocol' conforms to protocol
  681. 'anotherProtocol', and NO if not. Note that if one of the two
  682. protocols is nil, it returns NO. */
  683. objc_EXPORT BOOL protocol_conformsToProtocol (Protocol *protocol, Protocol *anotherProtocol);
  684. /* Return YES if protocol 'protocol' is the same as protocol
  685. 'anotherProtocol', and 'NO' if not. Note that it returns YES if
  686. the two protocols are both nil. */
  687. objc_EXPORT BOOL protocol_isEqual (Protocol *protocol, Protocol *anotherProtocol);
  688. /* Return the name of protocol 'protocol'. If 'protocol' is nil or is
  689. not a Protocol, return NULL. */
  690. objc_EXPORT const char *protocol_getName (Protocol *protocol);
  691. /* Return the method description for the method with selector
  692. 'selector' in protocol 'protocol'; if 'requiredMethod' is YES, the
  693. function searches the list of required methods; if NO, the list of
  694. optional methods. If 'instanceMethod' is YES, the function search
  695. for an instance method; if NO, for a class method. If there is no
  696. matching method, an objc_method_description structure with both
  697. name and types set to NULL is returned. This function will only
  698. find methods that are directly declared in the protocol itself, not
  699. in other protocols that this protocol adopts.
  700. Note that the traditional ABI does not store the list of optional
  701. methods of a protocol in a compiled module, so the traditional ABI
  702. will always return (NULL, NULL) when requiredMethod == NO. */
  703. objc_EXPORT struct objc_method_description protocol_getMethodDescription (Protocol *protocol,
  704. SEL selector,
  705. BOOL requiredMethod,
  706. BOOL instanceMethod);
  707. /* Return the method descriptions of all the methods of the protocol.
  708. The return value of the function is a pointer to an area, allocated
  709. with malloc(), that contains all the method descriptions of the
  710. methods of the protocol. It does not recursively include methods
  711. of the protocols adopted by this protocol. The list is terminated
  712. by a NULL objc_method_description (one with both fields set to
  713. NULL). Optionally, if you pass a non-NULL
  714. 'numberOfReturnedMethods' pointer, the unsigned int that it points
  715. to will be filled with the number of properties returned.
  716. Note that the traditional ABI does not store the list of optional
  717. methods of a protocol in a compiled module, so the traditional ABI
  718. will always return an empty list if requiredMethod is set to
  719. NO. */
  720. objc_EXPORT struct objc_method_description *protocol_copyMethodDescriptionList (Protocol *protocol,
  721. BOOL requiredMethod,
  722. BOOL instanceMethod,
  723. unsigned int *numberOfReturnedMethods);
  724. /* Return the property with name 'propertyName' of the protocol
  725. 'protocol'. If 'requiredProperty' is YES, the function searches
  726. the list of required properties; if NO, the list of optional
  727. properties. If 'instanceProperty' is YES, the function searches
  728. the list of instance properties; if NO, the list of class
  729. properties. At the moment, optional properties and class
  730. properties are not part of the Objective-C language, so both
  731. 'requiredProperty' and 'instanceProperty' should be set to YES.
  732. This function returns NULL if the required property cannot be
  733. found.
  734. Note that the traditional ABI does not store the list of properties
  735. of a protocol in a compiled module, so the traditional ABI will
  736. always return NULL. */
  737. objc_EXPORT Property protocol_getProperty (Protocol *protocol, const char *propertyName,
  738. BOOL requiredProperty, BOOL instanceProperty);
  739. /* Return all the properties of the protocol. The return value of the
  740. function is a pointer to an area, allocated with malloc(), that
  741. contains all the properties of the protocol. It does not
  742. recursively include properties of the protocols adopted by this
  743. protocol. The list is terminated by NULL. Optionally, if you pass
  744. a non-NULL 'numberOfReturnedProperties' pointer, the unsigned int
  745. that it points to will be filled with the number of properties
  746. returned.
  747. Note that the traditional ABI does not store the list of properties
  748. of a protocol in a compiled module, so the traditional ABI will
  749. always return NULL and store 0 in numberOfReturnedProperties. */
  750. objc_EXPORT Property *protocol_copyPropertyList (Protocol *protocol, unsigned int *numberOfReturnedProperties);
  751. /* Return all the protocols that the protocol conforms to. The return
  752. value of the function is a pointer to an area, allocated with
  753. malloc(), that contains all the protocols formally adopted by the
  754. protocol. It does not recursively include protocols adopted by the
  755. protocols adopted by this protocol. The list is terminated by
  756. NULL. Optionally, if you pass a non-NULL
  757. 'numberOfReturnedProtocols' pointer, the unsigned int that it
  758. points to will be filled with the number of protocols returned. */
  759. objc_EXPORT Protocol **protocol_copyProtocolList (Protocol *protocol, unsigned int *numberOfReturnedProtocols);
  760. /** Implementation: the following hook is in init.c. */
  761. /* This is a hook which is called by __objc_exec_class every time a
  762. class or a category is loaded into the runtime. This may e.g. help
  763. a dynamic loader determine the classes that have been loaded when
  764. an object file is dynamically linked in. */
  765. objc_EXPORT void (*_objc_load_callback)(Class _class, struct objc_category *category);
  766. /** Implementation: the following functions are in objc-foreach.c. */
  767. /* 'objc_enumerationMutation()' is called when a collection is
  768. mutated while being "fast enumerated". That is a hard error, and
  769. objc_enumerationMutation is called to deal with it. 'collection'
  770. is the collection object that was mutated during an enumeration.
  771. objc_enumerationMutation() will invoke the mutation handler if any
  772. is set. Then, it will abort the program.
  773. Compatibility note: the Apple runtime will not abort the program
  774. after calling the mutation handler. */
  775. objc_EXPORT void objc_enumerationMutation (id collection);
  776. /* 'objc_set_enumeration_mutation_handler' can be used to set a
  777. function that will be called (instead of aborting) when a fast
  778. enumeration is mutated during enumeration. The handler will be
  779. called with the 'collection' being mutated as the only argument and
  780. it should not return; it should either exit the program, or could
  781. throw an exception. The recommended implementation is to throw an
  782. exception - the user can then use exception handlers to deal with
  783. it.
  784. This function is not thread safe (other threads may be trying to
  785. invoke the enumeration mutation handler while you are changing it!)
  786. and should be called during during the program initialization
  787. before threads are started. It is mostly reserved for "Foundation"
  788. libraries; in the case of GNUstep, GNUstep Base may be using this
  789. function to improve the standard enumeration mutation handling.
  790. You probably shouldn't use this function unless you are writing
  791. your own Foundation library. */
  792. objc_EXPORT void objc_setEnumerationMutationHandler (void (*handler)(id));
  793. /* This structure (used during fast enumeration) is automatically
  794. defined by the compiler (it is as if this definition was always
  795. included in all Objective-C files). Note that it is usually
  796. defined again with the name of NSFastEnumeration by "Foundation"
  797. libraries such as GNUstep Base. And if NSFastEnumeration is
  798. defined, the compiler will use it instead of
  799. __objcFastEnumerationState when doing fast enumeration. */
  800. /*
  801. struct __objcFastEnumerationState
  802. {
  803. unsigned long state;
  804. id *itemsPtr;
  805. unsigned long *mutationsPtr;
  806. unsigned long extra[5];
  807. };
  808. */
  809. /* Compatibility Note: The Apple/NeXT runtime has the functions
  810. objc_copyImageNames (), class_getImageName () and
  811. objc_copyClassNamesForImage () but they are undocumented. The GNU
  812. runtime does not have them at the moment. */
  813. /* Compatibility Note: The Apple/NeXT runtime has the functions
  814. objc_setAssociatedObject (), objc_getAssociatedObject (),
  815. objc_removeAssociatedObjects () and the objc_AssociationPolicy type
  816. and related enum. The GNU runtime does not have them yet.
  817. TODO: Implement them. */
  818. /* Compatibility Note: The Apple/NeXT runtime has the function
  819. objc_setForwardHandler (). The GNU runtime does not have it
  820. because messaging (and, in particular, forwarding) works in a
  821. different (incompatible) way with the GNU runtime. If you need to
  822. customize message forwarding at the Objective-C runtime level (that
  823. is, if you are implementing your own "Foundation" library such as
  824. GNUstep Base on top of the Objective-C runtime), in objc/message.h
  825. there are hooks (that work in the framework of the GNU runtime) to
  826. do so. */
  827. /** Implementation: the following functions are in memory.c. */
  828. /* Traditional GNU Objective-C Runtime functions that are used for
  829. memory allocation and disposal. These functions are used in the
  830. same way as you use malloc, realloc, calloc and free and make sure
  831. that memory allocation works properly with the garbage
  832. collector.
  833. Compatibility Note: these functions are not available with the
  834. Apple/NeXT runtime. */
  835. objc_EXPORT void *objc_malloc(size_t size);
  836. /* FIXME: Shouldn't the following be called objc_malloc_atomic ? The
  837. GC function is GC_malloc_atomic() which makes sense.
  838. */
  839. objc_EXPORT void *objc_atomic_malloc(size_t size);
  840. objc_EXPORT void *objc_realloc(void *mem, size_t size);
  841. objc_EXPORT void *objc_calloc(size_t nelem, size_t size);
  842. objc_EXPORT void objc_free(void *mem);
  843. /** Implementation: the following functions are in gc.c. */
  844. /* The GNU Objective-C Runtime has a different implementation of
  845. garbage collection.
  846. Compatibility Note: these functions are not available with the
  847. Apple/NeXT runtime. */
  848. /* Mark the instance variable as inaccessible to the garbage
  849. collector. */
  850. objc_EXPORT void class_ivar_set_gcinvisible (Class _class,
  851. const char* ivarname,
  852. BOOL gcInvisible);
  853. /** Implementation: the following functions are in encoding.c. */
  854. /* Traditional GNU Objective-C Runtime functions that are currently
  855. used to implement method forwarding.
  856. Compatibility Note: these functions are not available with the
  857. Apple/NeXT runtime. */
  858. /* Return the size of a variable which has the specified 'type'
  859. encoding. */
  860. objc_EXPORT int objc_sizeof_type (const char *type);
  861. /* Return the align of a variable which has the specified 'type'
  862. encoding. */
  863. objc_EXPORT int objc_alignof_type (const char *type);
  864. /* Return the aligned size of a variable which has the specified
  865. 'type' encoding. The aligned size is the size rounded up to the
  866. nearest alignment. */
  867. objc_EXPORT int objc_aligned_size (const char *type);
  868. /* Return the promoted size of a variable which has the specified
  869. 'type' encoding. This is the size rounded up to the nearest
  870. integral of the wordsize, taken to be the size of a void *. */
  871. objc_EXPORT int objc_promoted_size (const char *type);
  872. /* The following functions are used when parsing the type encoding of
  873. methods, to skip over parts that are ignored. They take as
  874. argument a pointer to a location inside the type encoding of a
  875. method (which is a string) and return a new pointer, pointing to a
  876. new location inside the string after having skipped the unwanted
  877. information. */
  878. /* Skip some type qualifiers (_C_CONST, _C_IN, etc). These may
  879. eventually precede typespecs occurring in method prototype
  880. encodings. */
  881. objc_EXPORT const char *objc_skip_type_qualifiers (const char *type);
  882. /* Skip one typespec element (_C_CLASS, _C_SEL, etc). If the typespec
  883. is prepended by type qualifiers, these are skipped as well. */
  884. objc_EXPORT const char *objc_skip_typespec (const char *type);
  885. /* Skip an offset. */
  886. objc_EXPORT const char *objc_skip_offset (const char *type);
  887. /* Skip an argument specification (ie, skipping a typespec, which may
  888. include qualifiers, and an offset too). */
  889. objc_EXPORT const char *objc_skip_argspec (const char *type);
  890. /* Read type qualifiers (_C_CONST, _C_IN, etc) from string 'type'
  891. (stopping at the first non-type qualifier found) and return an
  892. unsigned int which is the logical OR of all the corresponding flags
  893. (_F_CONST, _F_IN etc). */
  894. objc_EXPORT unsigned objc_get_type_qualifiers (const char *type);
  895. /* Note that the following functions work for very simple structures,
  896. but get easily confused by more complicated ones (for example,
  897. containing vectors). A better solution is required. These
  898. functions are likely to change in the next GCC release. */
  899. /* The following three functions can be used to determine how a
  900. structure is laid out by the compiler. For example:
  901. struct objc_struct_layout layout;
  902. int i;
  903. objc_layout_structure (type, &layout);
  904. while (objc_layout_structure_next_member (&layout))
  905. {
  906. int position, align;
  907. const char *type;
  908. objc_layout_structure_get_info (&layout, &position, &align, &type);
  909. printf ("element %d has offset %d, alignment %d\n",
  910. i++, position, align);
  911. }
  912. These functions are used by objc_sizeof_type and objc_alignof_type
  913. functions to compute the size and alignment of structures. The
  914. previous method of computing the size and alignment of a structure
  915. was not working on some architectures, particularly on AIX, and in
  916. the presence of bitfields inside the structure. */
  917. struct objc_struct_layout
  918. {
  919. const char *original_type;
  920. const char *type;
  921. const char *prev_type;
  922. unsigned int record_size;
  923. unsigned int record_align;
  924. };
  925. objc_EXPORT void objc_layout_structure (const char *type,
  926. struct objc_struct_layout *layout);
  927. objc_EXPORT BOOL objc_layout_structure_next_member (struct objc_struct_layout *layout);
  928. objc_EXPORT void objc_layout_finish_structure (struct objc_struct_layout *layout,
  929. unsigned int *size,
  930. unsigned int *align);
  931. objc_EXPORT void objc_layout_structure_get_info (struct objc_struct_layout *layout,
  932. unsigned int *offset,
  933. unsigned int *align,
  934. const char **type);
  935. #ifdef __cplusplus
  936. }
  937. #endif /* __cplusplus */
  938. #endif