encoding.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /* Encoding of types for Objective C.
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. Contributed by Kresten Krab Thorup
  4. Bitfield support by Ovidiu Predescu
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. GCC is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. /* FIXME: This file has no business including tm.h. */
  22. /* FIXME: This file contains functions that will abort the entire
  23. program if they fail. Is that really needed ? */
  24. #include "objc-private/common.h"
  25. #include "objc-private/error.h"
  26. #include "tconfig.h"
  27. #include "coretypes.h"
  28. #include "tm.h"
  29. #include "objc/runtime.h"
  30. #include "objc-private/module-abi-8.h" /* For struct objc_method */
  31. #include <stdlib.h>
  32. #include <ctype.h>
  33. #include <string.h> /* For memcpy. */
  34. #undef MAX
  35. #define MAX(X, Y) \
  36. ({ typeof (X) __x = (X), __y = (Y); \
  37. (__x > __y ? __x : __y); })
  38. #undef MIN
  39. #define MIN(X, Y) \
  40. ({ typeof (X) __x = (X), __y = (Y); \
  41. (__x < __y ? __x : __y); })
  42. #undef ROUND
  43. #define ROUND(V, A) \
  44. ({ typeof (V) __v = (V); typeof (A) __a = (A); \
  45. __a * ((__v+__a - 1)/__a); })
  46. /* Various hacks for objc_layout_record. These are used by the target
  47. macros. */
  48. #define TREE_CODE(TYPE) *(TYPE)
  49. #define TREE_TYPE(TREE) (TREE)
  50. #define RECORD_TYPE _C_STRUCT_B
  51. #define UNION_TYPE _C_UNION_B
  52. #define QUAL_UNION_TYPE _C_UNION_B
  53. #define ARRAY_TYPE _C_ARY_B
  54. #define REAL_TYPE _C_DBL
  55. #define VECTOR_TYPE _C_VECTOR
  56. #define TYPE_FIELDS(TYPE) ({const char *_field = (TYPE)+1; \
  57. while (*_field != _C_STRUCT_E && *_field != _C_STRUCT_B \
  58. && *_field != _C_UNION_B && *_field++ != '=') \
  59. /* do nothing */; \
  60. _field;})
  61. #define DECL_MODE(TYPE) *(TYPE)
  62. #define TYPE_MODE(TYPE) *(TYPE)
  63. #define DFmode _C_DBL
  64. #define strip_array_types(TYPE) ({const char *_field = (TYPE); \
  65. while (*_field == _C_ARY_B)\
  66. {\
  67. while (isdigit ((unsigned char)*++_field))\
  68. ;\
  69. }\
  70. _field;})
  71. /* Some ports (eg ARM) allow the structure size boundary to be
  72. selected at compile-time. We override the normal definition with
  73. one that has a constant value for this compilation. */
  74. #ifndef BITS_PER_UNIT
  75. #define BITS_PER_UNIT 8
  76. #endif
  77. #undef STRUCTURE_SIZE_BOUNDARY
  78. #define STRUCTURE_SIZE_BOUNDARY (BITS_PER_UNIT * sizeof (struct{char a;}))
  79. /* Some ROUND_TYPE_ALIGN macros use TARGET_foo, and consequently
  80. target_flags. Define a dummy entry here to so we don't die.
  81. We have to rename it because target_flags may already have been
  82. declared extern. */
  83. #define target_flags not_target_flags
  84. static int __attribute__ ((__unused__)) not_target_flags = 0;
  85. /* Some ROUND_TYPE_ALIGN use ALTIVEC_VECTOR_MODE (rs6000 darwin).
  86. Define a dummy ALTIVEC_VECTOR_MODE so it will not die. */
  87. #undef ALTIVEC_VECTOR_MODE
  88. #define ALTIVEC_VECTOR_MODE(MODE) (0)
  89. /* Replace TARGET_VSX, TARGET_ALTIVEC, and TARGET_64BIT with constants based on
  90. the current switches, rather than looking in the options structure. */
  91. #ifdef _ARCH_PPC
  92. #undef TARGET_VSX
  93. #undef TARGET_ALTIVEC
  94. #undef TARGET_64BIT
  95. #ifdef __VSX__
  96. #define TARGET_VSX 1
  97. #else
  98. #define TARGET_VSX 0
  99. #endif
  100. #ifdef __ALTIVEC__
  101. #define TARGET_ALTIVEC 1
  102. #else
  103. #define TARGET_ALTIVEC 0
  104. #endif
  105. #ifdef _ARCH_PPC64
  106. #define TARGET_64BIT 1
  107. #else
  108. #define TARGET_64BIT 0
  109. #endif
  110. #endif
  111. /* Furthermore, some (powerpc) targets also use TARGET_ALIGN_NATURAL
  112. in their alignment macros. Currently[4.5/6], rs6000.h points this
  113. to a static variable, initialized by target overrides. This is reset
  114. in linux64.h but not in darwin64.h. The macro is not used by *86*. */
  115. #if __MACH__
  116. # if __LP64__
  117. # undef TARGET_ALIGN_NATURAL
  118. # define TARGET_ALIGN_NATURAL 1
  119. # endif
  120. /* On Darwin32, we need to recurse until we find the starting stuct type. */
  121. static int
  122. _darwin_rs6000_special_round_type_align (const char *struc, int comp, int spec)
  123. {
  124. const char *_stp , *_fields = TYPE_FIELDS (struc);
  125. if (!_fields)
  126. return MAX (comp, spec);
  127. _stp = strip_array_types (_fields);
  128. if (TYPE_MODE(_stp) == _C_COMPLEX)
  129. _stp++;
  130. switch (TYPE_MODE(_stp))
  131. {
  132. case RECORD_TYPE:
  133. case UNION_TYPE:
  134. return MAX (MAX (comp, spec), objc_alignof_type (_stp) * BITS_PER_UNIT);
  135. break;
  136. case DFmode:
  137. case _C_LNG_LNG:
  138. case _C_ULNG_LNG:
  139. return MAX (MAX (comp, spec), 64);
  140. break;
  141. default:
  142. return MAX (comp, spec);
  143. break;
  144. }
  145. }
  146. /* See comment below. */
  147. #define darwin_rs6000_special_round_type_align(S,C,S2) \
  148. (_darwin_rs6000_special_round_type_align ((char*)(S), (int)(C), (int)(S2)))
  149. #endif
  150. /* FIXME: while this file has no business including tm.h, this
  151. definitely has no business defining this macro but it
  152. is only way around without really rewritting this file,
  153. should look after the branch of 3.4 to fix this. */
  154. #define rs6000_special_round_type_align(STRUCT, COMPUTED, SPECIFIED) \
  155. ({ const char *_fields = TYPE_FIELDS (STRUCT); \
  156. ((_fields != 0 \
  157. && TYPE_MODE (strip_array_types (TREE_TYPE (_fields))) == DFmode) \
  158. ? MAX (MAX (COMPUTED, SPECIFIED), 64) \
  159. : MAX (COMPUTED, SPECIFIED));})
  160. #define rs6000_special_adjust_field_align_p(FIELD, COMPUTED) 0
  161. /* Skip a variable name, enclosed in quotes ("). */
  162. static inline
  163. const char *
  164. objc_skip_variable_name (const char *type)
  165. {
  166. /* Skip the variable name if any. */
  167. if (*type == '"')
  168. {
  169. /* FIXME: How do we know we won't read beyond the end of the
  170. string. Here and in the rest of the file! */
  171. /* Skip '"'. */
  172. type++;
  173. /* Skip to the next '"'. */
  174. while (*type != '"')
  175. type++;
  176. /* Skip '"'. */
  177. type++;
  178. }
  179. return type;
  180. }
  181. int
  182. objc_sizeof_type (const char *type)
  183. {
  184. type = objc_skip_variable_name (type);
  185. switch (*type) {
  186. case _C_BOOL:
  187. return sizeof (_Bool);
  188. break;
  189. case _C_ID:
  190. return sizeof (id);
  191. break;
  192. case _C_CLASS:
  193. return sizeof (Class);
  194. break;
  195. case _C_SEL:
  196. return sizeof (SEL);
  197. break;
  198. case _C_CHR:
  199. return sizeof (char);
  200. break;
  201. case _C_UCHR:
  202. return sizeof (unsigned char);
  203. break;
  204. case _C_SHT:
  205. return sizeof (short);
  206. break;
  207. case _C_USHT:
  208. return sizeof (unsigned short);
  209. break;
  210. case _C_INT:
  211. return sizeof (int);
  212. break;
  213. case _C_UINT:
  214. return sizeof (unsigned int);
  215. break;
  216. case _C_LNG:
  217. return sizeof (long);
  218. break;
  219. case _C_ULNG:
  220. return sizeof (unsigned long);
  221. break;
  222. case _C_LNG_LNG:
  223. return sizeof (long long);
  224. break;
  225. case _C_ULNG_LNG:
  226. return sizeof (unsigned long long);
  227. break;
  228. case _C_FLT:
  229. return sizeof (float);
  230. break;
  231. case _C_DBL:
  232. return sizeof (double);
  233. break;
  234. case _C_LNG_DBL:
  235. return sizeof (long double);
  236. break;
  237. case _C_VOID:
  238. return sizeof (void);
  239. break;
  240. case _C_PTR:
  241. case _C_ATOM:
  242. case _C_CHARPTR:
  243. return sizeof (char *);
  244. break;
  245. case _C_ARY_B:
  246. {
  247. int len = atoi (type + 1);
  248. while (isdigit ((unsigned char)*++type))
  249. ;
  250. return len * objc_aligned_size (type);
  251. }
  252. break;
  253. case _C_VECTOR:
  254. {
  255. /* Skip the '!'. */
  256. type++;
  257. /* Skip the '['. */
  258. type++;
  259. /* The size in bytes is the following number. */
  260. int size = atoi (type);
  261. return size;
  262. }
  263. break;
  264. case _C_BFLD:
  265. {
  266. /* The GNU encoding of bitfields is: b 'position' 'type'
  267. 'size'. */
  268. int position, size;
  269. int startByte, endByte;
  270. position = atoi (type + 1);
  271. while (isdigit ((unsigned char)*++type))
  272. ;
  273. size = atoi (type + 1);
  274. startByte = position / BITS_PER_UNIT;
  275. endByte = (position + size) / BITS_PER_UNIT;
  276. return endByte - startByte;
  277. }
  278. case _C_UNION_B:
  279. case _C_STRUCT_B:
  280. {
  281. struct objc_struct_layout layout;
  282. unsigned int size;
  283. objc_layout_structure (type, &layout);
  284. while (objc_layout_structure_next_member (&layout))
  285. /* do nothing */ ;
  286. objc_layout_finish_structure (&layout, &size, NULL);
  287. return size;
  288. }
  289. case _C_COMPLEX:
  290. {
  291. type++; /* Skip after the 'j'. */
  292. switch (*type)
  293. {
  294. case _C_CHR:
  295. return sizeof (_Complex char);
  296. break;
  297. case _C_UCHR:
  298. return sizeof (_Complex unsigned char);
  299. break;
  300. case _C_SHT:
  301. return sizeof (_Complex short);
  302. break;
  303. case _C_USHT:
  304. return sizeof (_Complex unsigned short);
  305. break;
  306. case _C_INT:
  307. return sizeof (_Complex int);
  308. break;
  309. case _C_UINT:
  310. return sizeof (_Complex unsigned int);
  311. break;
  312. case _C_LNG:
  313. return sizeof (_Complex long);
  314. break;
  315. case _C_ULNG:
  316. return sizeof (_Complex unsigned long);
  317. break;
  318. case _C_LNG_LNG:
  319. return sizeof (_Complex long long);
  320. break;
  321. case _C_ULNG_LNG:
  322. return sizeof (_Complex unsigned long long);
  323. break;
  324. case _C_FLT:
  325. return sizeof (_Complex float);
  326. break;
  327. case _C_DBL:
  328. return sizeof (_Complex double);
  329. break;
  330. case _C_LNG_DBL:
  331. return sizeof (_Complex long double);
  332. break;
  333. default:
  334. {
  335. /* FIXME: Is this so bad that we have to abort the
  336. entire program ? (it applies to all the other
  337. _objc_abort calls in this file).
  338. */
  339. _objc_abort ("unknown complex type %s\n", type);
  340. return 0;
  341. }
  342. }
  343. }
  344. default:
  345. {
  346. _objc_abort ("unknown type %s\n", type);
  347. return 0;
  348. }
  349. }
  350. }
  351. int
  352. objc_alignof_type (const char *type)
  353. {
  354. type = objc_skip_variable_name (type);
  355. switch (*type) {
  356. case _C_BOOL:
  357. return __alignof__ (_Bool);
  358. break;
  359. case _C_ID:
  360. return __alignof__ (id);
  361. break;
  362. case _C_CLASS:
  363. return __alignof__ (Class);
  364. break;
  365. case _C_SEL:
  366. return __alignof__ (SEL);
  367. break;
  368. case _C_CHR:
  369. return __alignof__ (char);
  370. break;
  371. case _C_UCHR:
  372. return __alignof__ (unsigned char);
  373. break;
  374. case _C_SHT:
  375. return __alignof__ (short);
  376. break;
  377. case _C_USHT:
  378. return __alignof__ (unsigned short);
  379. break;
  380. case _C_INT:
  381. return __alignof__ (int);
  382. break;
  383. case _C_UINT:
  384. return __alignof__ (unsigned int);
  385. break;
  386. case _C_LNG:
  387. return __alignof__ (long);
  388. break;
  389. case _C_ULNG:
  390. return __alignof__ (unsigned long);
  391. break;
  392. case _C_LNG_LNG:
  393. return __alignof__ (long long);
  394. break;
  395. case _C_ULNG_LNG:
  396. return __alignof__ (unsigned long long);
  397. break;
  398. case _C_FLT:
  399. return __alignof__ (float);
  400. break;
  401. case _C_DBL:
  402. return __alignof__ (double);
  403. break;
  404. case _C_LNG_DBL:
  405. return __alignof__ (long double);
  406. break;
  407. case _C_PTR:
  408. case _C_ATOM:
  409. case _C_CHARPTR:
  410. return __alignof__ (char *);
  411. break;
  412. case _C_ARY_B:
  413. while (isdigit ((unsigned char)*++type))
  414. /* do nothing */;
  415. return objc_alignof_type (type);
  416. case _C_VECTOR:
  417. {
  418. /* Skip the '!'. */
  419. type++;
  420. /* Skip the '['. */
  421. type++;
  422. /* Skip the size. */
  423. while (isdigit ((unsigned char)*type))
  424. type++;
  425. /* Skip the ','. */
  426. type++;
  427. /* The alignment in bytes is the following number. */
  428. return atoi (type);
  429. }
  430. case _C_STRUCT_B:
  431. case _C_UNION_B:
  432. {
  433. struct objc_struct_layout layout;
  434. unsigned int align;
  435. objc_layout_structure (type, &layout);
  436. while (objc_layout_structure_next_member (&layout))
  437. /* do nothing */;
  438. objc_layout_finish_structure (&layout, NULL, &align);
  439. return align;
  440. }
  441. case _C_COMPLEX:
  442. {
  443. type++; /* Skip after the 'j'. */
  444. switch (*type)
  445. {
  446. case _C_CHR:
  447. return __alignof__ (_Complex char);
  448. break;
  449. case _C_UCHR:
  450. return __alignof__ (_Complex unsigned char);
  451. break;
  452. case _C_SHT:
  453. return __alignof__ (_Complex short);
  454. break;
  455. case _C_USHT:
  456. return __alignof__ (_Complex unsigned short);
  457. break;
  458. case _C_INT:
  459. return __alignof__ (_Complex int);
  460. break;
  461. case _C_UINT:
  462. return __alignof__ (_Complex unsigned int);
  463. break;
  464. case _C_LNG:
  465. return __alignof__ (_Complex long);
  466. break;
  467. case _C_ULNG:
  468. return __alignof__ (_Complex unsigned long);
  469. break;
  470. case _C_LNG_LNG:
  471. return __alignof__ (_Complex long long);
  472. break;
  473. case _C_ULNG_LNG:
  474. return __alignof__ (_Complex unsigned long long);
  475. break;
  476. case _C_FLT:
  477. return __alignof__ (_Complex float);
  478. break;
  479. case _C_DBL:
  480. return __alignof__ (_Complex double);
  481. break;
  482. case _C_LNG_DBL:
  483. return __alignof__ (_Complex long double);
  484. break;
  485. default:
  486. {
  487. _objc_abort ("unknown complex type %s\n", type);
  488. return 0;
  489. }
  490. }
  491. }
  492. default:
  493. {
  494. _objc_abort ("unknown type %s\n", type);
  495. return 0;
  496. }
  497. }
  498. }
  499. int
  500. objc_aligned_size (const char *type)
  501. {
  502. int size, align;
  503. type = objc_skip_variable_name (type);
  504. size = objc_sizeof_type (type);
  505. align = objc_alignof_type (type);
  506. return ROUND (size, align);
  507. }
  508. int
  509. objc_promoted_size (const char *type)
  510. {
  511. int size, wordsize;
  512. type = objc_skip_variable_name (type);
  513. size = objc_sizeof_type (type);
  514. wordsize = sizeof (void *);
  515. return ROUND (size, wordsize);
  516. }
  517. inline
  518. const char *
  519. objc_skip_type_qualifiers (const char *type)
  520. {
  521. while (*type == _C_CONST
  522. || *type == _C_IN
  523. || *type == _C_INOUT
  524. || *type == _C_OUT
  525. || *type == _C_BYCOPY
  526. || *type == _C_BYREF
  527. || *type == _C_ONEWAY
  528. || *type == _C_GCINVISIBLE)
  529. {
  530. type += 1;
  531. }
  532. return type;
  533. }
  534. inline
  535. const char *
  536. objc_skip_typespec (const char *type)
  537. {
  538. type = objc_skip_variable_name (type);
  539. type = objc_skip_type_qualifiers (type);
  540. switch (*type) {
  541. case _C_ID:
  542. /* An id may be annotated by the actual type if it is known
  543. with the @"ClassName" syntax */
  544. if (*++type != '"')
  545. return type;
  546. else
  547. {
  548. while (*++type != '"')
  549. /* do nothing */;
  550. return type + 1;
  551. }
  552. /* The following are one character type codes */
  553. case _C_CLASS:
  554. case _C_SEL:
  555. case _C_CHR:
  556. case _C_UCHR:
  557. case _C_CHARPTR:
  558. case _C_ATOM:
  559. case _C_SHT:
  560. case _C_USHT:
  561. case _C_INT:
  562. case _C_UINT:
  563. case _C_LNG:
  564. case _C_BOOL:
  565. case _C_ULNG:
  566. case _C_LNG_LNG:
  567. case _C_ULNG_LNG:
  568. case _C_FLT:
  569. case _C_DBL:
  570. case _C_LNG_DBL:
  571. case _C_VOID:
  572. case _C_UNDEF:
  573. return ++type;
  574. break;
  575. case _C_COMPLEX:
  576. return type + 2;
  577. break;
  578. case _C_ARY_B:
  579. /* skip digits, typespec and closing ']' */
  580. while (isdigit ((unsigned char)*++type))
  581. ;
  582. type = objc_skip_typespec (type);
  583. if (*type == _C_ARY_E)
  584. return ++type;
  585. else
  586. {
  587. _objc_abort ("bad array type %s\n", type);
  588. return 0;
  589. }
  590. case _C_VECTOR:
  591. /* Skip '!' */
  592. type++;
  593. /* Skip '[' */
  594. type++;
  595. /* Skip digits (size) */
  596. while (isdigit ((unsigned char)*type))
  597. type++;
  598. /* Skip ',' */
  599. type++;
  600. /* Skip digits (alignment) */
  601. while (isdigit ((unsigned char)*type))
  602. type++;
  603. /* Skip typespec. */
  604. type = objc_skip_typespec (type);
  605. /* Skip closing ']'. */
  606. if (*type == _C_ARY_E)
  607. return ++type;
  608. else
  609. {
  610. _objc_abort ("bad vector type %s\n", type);
  611. return 0;
  612. }
  613. case _C_BFLD:
  614. /* The GNU encoding of bitfields is: b 'position' 'type'
  615. 'size'. */
  616. while (isdigit ((unsigned char)*++type))
  617. ; /* skip position */
  618. while (isdigit ((unsigned char)*++type))
  619. ; /* skip type and size */
  620. return type;
  621. case _C_STRUCT_B:
  622. /* skip name, and elements until closing '}' */
  623. while (*type != _C_STRUCT_E && *type++ != '=')
  624. ;
  625. while (*type != _C_STRUCT_E)
  626. {
  627. type = objc_skip_typespec (type);
  628. }
  629. return ++type;
  630. case _C_UNION_B:
  631. /* skip name, and elements until closing ')' */
  632. while (*type != _C_UNION_E && *type++ != '=')
  633. ;
  634. while (*type != _C_UNION_E)
  635. {
  636. type = objc_skip_typespec (type);
  637. }
  638. return ++type;
  639. case _C_PTR:
  640. /* Just skip the following typespec */
  641. return objc_skip_typespec (++type);
  642. default:
  643. {
  644. _objc_abort ("unknown type %s\n", type);
  645. return 0;
  646. }
  647. }
  648. }
  649. inline
  650. const char *
  651. objc_skip_offset (const char *type)
  652. {
  653. /* The offset is prepended by a '+' if the argument is passed in
  654. registers. PS: The compiler stopped generating this '+' in
  655. version 3.4. */
  656. if (*type == '+')
  657. type++;
  658. /* Some people claim that on some platforms, where the stack grows
  659. backwards, the compiler generates negative offsets (??). Skip a
  660. '-' for such a negative offset. */
  661. if (*type == '-')
  662. type++;
  663. /* Skip the digits that represent the offset. */
  664. while (isdigit ((unsigned char) *type))
  665. type++;
  666. return type;
  667. }
  668. const char *
  669. objc_skip_argspec (const char *type)
  670. {
  671. type = objc_skip_typespec (type);
  672. type = objc_skip_offset (type);
  673. return type;
  674. }
  675. char *
  676. method_copyReturnType (struct objc_method *method)
  677. {
  678. if (method == NULL)
  679. return 0;
  680. else
  681. {
  682. char *returnValue;
  683. size_t returnValueSize;
  684. /* Determine returnValueSize. */
  685. {
  686. /* Find the end of the first argument. We want to return the
  687. first argument spec, plus 1 byte for the \0 at the end. */
  688. const char *type = method->method_types;
  689. if (*type == '\0')
  690. return NULL;
  691. type = objc_skip_argspec (type);
  692. returnValueSize = type - method->method_types + 1;
  693. }
  694. /* Copy the first argument into returnValue. */
  695. returnValue = malloc (sizeof (char) * returnValueSize);
  696. memcpy (returnValue, method->method_types, returnValueSize);
  697. returnValue[returnValueSize - 1] = '\0';
  698. return returnValue;
  699. }
  700. }
  701. char *
  702. method_copyArgumentType (struct objc_method * method, unsigned int argumentNumber)
  703. {
  704. if (method == NULL)
  705. return 0;
  706. else
  707. {
  708. char *returnValue;
  709. const char *returnValueStart;
  710. size_t returnValueSize;
  711. /* Determine returnValueStart and returnValueSize. */
  712. {
  713. const char *type = method->method_types;
  714. /* Skip the first argument (return type). */
  715. type = objc_skip_argspec (type);
  716. /* Now keep skipping arguments until we get to
  717. argumentNumber. */
  718. while (argumentNumber > 0)
  719. {
  720. /* We are supposed to skip an argument, but the string is
  721. finished. This means we were asked for a non-existing
  722. argument. */
  723. if (*type == '\0')
  724. return NULL;
  725. type = objc_skip_argspec (type);
  726. argumentNumber--;
  727. }
  728. /* If the argument does not exist, return NULL. */
  729. if (*type == '\0')
  730. return NULL;
  731. returnValueStart = type;
  732. type = objc_skip_argspec (type);
  733. returnValueSize = type - returnValueStart + 1;
  734. }
  735. /* Copy the argument into returnValue. */
  736. returnValue = malloc (sizeof (char) * returnValueSize);
  737. memcpy (returnValue, returnValueStart, returnValueSize);
  738. returnValue[returnValueSize - 1] = '\0';
  739. return returnValue;
  740. }
  741. }
  742. void method_getReturnType (struct objc_method * method, char *returnValue,
  743. size_t returnValueSize)
  744. {
  745. if (returnValue == NULL || returnValueSize == 0)
  746. return;
  747. /* Zero the string; we'll then write the argument type at the
  748. beginning of it, if needed. */
  749. memset (returnValue, 0, returnValueSize);
  750. if (method == NULL)
  751. return;
  752. else
  753. {
  754. size_t argumentTypeSize;
  755. /* Determine argumentTypeSize. */
  756. {
  757. /* Find the end of the first argument. We want to return the
  758. first argument spec. */
  759. const char *type = method->method_types;
  760. if (*type == '\0')
  761. return;
  762. type = objc_skip_argspec (type);
  763. argumentTypeSize = type - method->method_types;
  764. if (argumentTypeSize > returnValueSize)
  765. argumentTypeSize = returnValueSize;
  766. }
  767. /* Copy the argument at the beginning of the string. */
  768. memcpy (returnValue, method->method_types, argumentTypeSize);
  769. }
  770. }
  771. void method_getArgumentType (struct objc_method * method, unsigned int argumentNumber,
  772. char *returnValue, size_t returnValueSize)
  773. {
  774. if (returnValue == NULL || returnValueSize == 0)
  775. return;
  776. /* Zero the string; we'll then write the argument type at the
  777. beginning of it, if needed. */
  778. memset (returnValue, 0, returnValueSize);
  779. if (method == NULL)
  780. return;
  781. else
  782. {
  783. const char *returnValueStart;
  784. size_t argumentTypeSize;
  785. /* Determine returnValueStart and argumentTypeSize. */
  786. {
  787. const char *type = method->method_types;
  788. /* Skip the first argument (return type). */
  789. type = objc_skip_argspec (type);
  790. /* Now keep skipping arguments until we get to
  791. argumentNumber. */
  792. while (argumentNumber > 0)
  793. {
  794. /* We are supposed to skip an argument, but the string is
  795. finished. This means we were asked for a non-existing
  796. argument. */
  797. if (*type == '\0')
  798. return;
  799. type = objc_skip_argspec (type);
  800. argumentNumber--;
  801. }
  802. /* If the argument does not exist, it's game over. */
  803. if (*type == '\0')
  804. return;
  805. returnValueStart = type;
  806. type = objc_skip_argspec (type);
  807. argumentTypeSize = type - returnValueStart;
  808. if (argumentTypeSize > returnValueSize)
  809. argumentTypeSize = returnValueSize;
  810. }
  811. /* Copy the argument at the beginning of the string. */
  812. memcpy (returnValue, returnValueStart, argumentTypeSize);
  813. }
  814. }
  815. unsigned int
  816. method_getNumberOfArguments (struct objc_method *method)
  817. {
  818. if (method == NULL)
  819. return 0;
  820. else
  821. {
  822. unsigned int i = 0;
  823. const char *type = method->method_types;
  824. while (*type)
  825. {
  826. type = objc_skip_argspec (type);
  827. i += 1;
  828. }
  829. if (i == 0)
  830. {
  831. /* This could only happen if method_types is invalid; in
  832. that case, return 0. */
  833. return 0;
  834. }
  835. else
  836. {
  837. /* Remove the return type. */
  838. return (i - 1);
  839. }
  840. }
  841. }
  842. unsigned
  843. objc_get_type_qualifiers (const char *type)
  844. {
  845. unsigned res = 0;
  846. BOOL flag = YES;
  847. while (flag)
  848. switch (*type++)
  849. {
  850. case _C_CONST: res |= _F_CONST; break;
  851. case _C_IN: res |= _F_IN; break;
  852. case _C_INOUT: res |= _F_INOUT; break;
  853. case _C_OUT: res |= _F_OUT; break;
  854. case _C_BYCOPY: res |= _F_BYCOPY; break;
  855. case _C_BYREF: res |= _F_BYREF; break;
  856. case _C_ONEWAY: res |= _F_ONEWAY; break;
  857. case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break;
  858. default: flag = NO;
  859. }
  860. return res;
  861. }
  862. /* The following three functions can be used to determine how a
  863. structure is laid out by the compiler. For example:
  864. struct objc_struct_layout layout;
  865. int i;
  866. objc_layout_structure (type, &layout);
  867. while (objc_layout_structure_next_member (&layout))
  868. {
  869. int position, align;
  870. const char *type;
  871. objc_layout_structure_get_info (&layout, &position, &align, &type);
  872. printf ("element %d has offset %d, alignment %d\n",
  873. i++, position, align);
  874. }
  875. These functions are used by objc_sizeof_type and objc_alignof_type
  876. functions to compute the size and alignment of structures. The
  877. previous method of computing the size and alignment of a structure
  878. was not working on some architectures, particularly on AIX, and in
  879. the presence of bitfields inside the structure. */
  880. void
  881. objc_layout_structure (const char *type,
  882. struct objc_struct_layout *layout)
  883. {
  884. const char *ntype;
  885. if (*type != _C_UNION_B && *type != _C_STRUCT_B)
  886. {
  887. _objc_abort ("record (or union) type expected in objc_layout_structure, got %s\n",
  888. type);
  889. }
  890. type ++;
  891. layout->original_type = type;
  892. /* Skip "<name>=" if any. Avoid embedded structures and unions. */
  893. ntype = type;
  894. while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B
  895. && *ntype++ != '=')
  896. /* do nothing */;
  897. /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
  898. if (*(ntype - 1) == '=')
  899. type = ntype;
  900. layout->type = type;
  901. layout->prev_type = NULL;
  902. layout->record_size = 0;
  903. layout->record_align = BITS_PER_UNIT;
  904. layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY);
  905. }
  906. BOOL
  907. objc_layout_structure_next_member (struct objc_struct_layout *layout)
  908. {
  909. register int desired_align = 0;
  910. /* The following are used only if the field is a bitfield */
  911. register const char *bfld_type = 0;
  912. register int bfld_type_align = 0, bfld_field_size = 0;
  913. /* The current type without the type qualifiers */
  914. const char *type;
  915. BOOL unionp = layout->original_type[-1] == _C_UNION_B;
  916. /* Add the size of the previous field to the size of the record. */
  917. if (layout->prev_type)
  918. {
  919. type = objc_skip_type_qualifiers (layout->prev_type);
  920. if (unionp)
  921. layout->record_size = MAX (layout->record_size,
  922. objc_sizeof_type (type) * BITS_PER_UNIT);
  923. else if (*type != _C_BFLD)
  924. layout->record_size += objc_sizeof_type (type) * BITS_PER_UNIT;
  925. else {
  926. /* Get the bitfield's type */
  927. for (bfld_type = type + 1;
  928. isdigit ((unsigned char)*bfld_type);
  929. bfld_type++)
  930. /* do nothing */;
  931. bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
  932. bfld_field_size = atoi (objc_skip_typespec (bfld_type));
  933. layout->record_size += bfld_field_size;
  934. }
  935. }
  936. if ((unionp && *layout->type == _C_UNION_E)
  937. || (!unionp && *layout->type == _C_STRUCT_E))
  938. return NO;
  939. /* Skip the variable name if any */
  940. layout->type = objc_skip_variable_name (layout->type);
  941. type = objc_skip_type_qualifiers (layout->type);
  942. if (*type != _C_BFLD)
  943. desired_align = objc_alignof_type (type) * BITS_PER_UNIT;
  944. else
  945. {
  946. desired_align = 1;
  947. /* Skip the bitfield's offset */
  948. for (bfld_type = type + 1;
  949. isdigit ((unsigned char) *bfld_type);
  950. bfld_type++)
  951. /* do nothing */;
  952. bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
  953. bfld_field_size = atoi (objc_skip_typespec (bfld_type));
  954. }
  955. /* The following won't work for vectors. */
  956. #ifdef BIGGEST_FIELD_ALIGNMENT
  957. desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
  958. #endif
  959. #ifdef ADJUST_FIELD_ALIGN
  960. desired_align = ADJUST_FIELD_ALIGN (type, desired_align);
  961. #endif
  962. /* Record must have at least as much alignment as any field.
  963. Otherwise, the alignment of the field within the record
  964. is meaningless. */
  965. #ifndef PCC_BITFIELD_TYPE_MATTERS
  966. layout->record_align = MAX (layout->record_align, desired_align);
  967. #else /* PCC_BITFIELD_TYPE_MATTERS */
  968. if (*type == _C_BFLD)
  969. {
  970. /* For these machines, a zero-length field does not
  971. affect the alignment of the structure as a whole.
  972. It does, however, affect the alignment of the next field
  973. within the structure. */
  974. if (bfld_field_size)
  975. layout->record_align = MAX (layout->record_align, desired_align);
  976. else
  977. desired_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
  978. /* A named bit field of declared type `int'
  979. forces the entire structure to have `int' alignment.
  980. Q1: How is encoded this thing and how to check for it?
  981. Q2: How to determine maximum_field_alignment at runtime? */
  982. /* if (DECL_NAME (field) != 0) */
  983. {
  984. int type_align = bfld_type_align;
  985. #if 0
  986. if (maximum_field_alignment != 0)
  987. type_align = MIN (type_align, maximum_field_alignment);
  988. else if (DECL_PACKED (field))
  989. type_align = MIN (type_align, BITS_PER_UNIT);
  990. #endif
  991. layout->record_align = MAX (layout->record_align, type_align);
  992. }
  993. }
  994. else
  995. layout->record_align = MAX (layout->record_align, desired_align);
  996. #endif /* PCC_BITFIELD_TYPE_MATTERS */
  997. /* Does this field automatically have alignment it needs
  998. by virtue of the fields that precede it and the record's
  999. own alignment? */
  1000. if (*type == _C_BFLD)
  1001. layout->record_size = atoi (type + 1);
  1002. else if (layout->record_size % desired_align != 0)
  1003. {
  1004. /* No, we need to skip space before this field.
  1005. Bump the cumulative size to multiple of field alignment. */
  1006. layout->record_size = ROUND (layout->record_size, desired_align);
  1007. }
  1008. /* Jump to the next field in record. */
  1009. layout->prev_type = layout->type;
  1010. layout->type = objc_skip_typespec (layout->type); /* skip component */
  1011. return YES;
  1012. }
  1013. void objc_layout_finish_structure (struct objc_struct_layout *layout,
  1014. unsigned int *size,
  1015. unsigned int *align)
  1016. {
  1017. BOOL unionp = layout->original_type[-1] == _C_UNION_B;
  1018. if (layout->type
  1019. && ((!unionp && *layout->type == _C_STRUCT_E)
  1020. || (unionp && *layout->type == _C_UNION_E)))
  1021. {
  1022. /* Work out the alignment of the record as one expression and store
  1023. in the record type. Round it up to a multiple of the record's
  1024. alignment. */
  1025. #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
  1026. layout->record_align = ROUND_TYPE_ALIGN (layout->original_type-1,
  1027. 1,
  1028. layout->record_align);
  1029. #else
  1030. layout->record_align = MAX (1, layout->record_align);
  1031. #endif
  1032. #ifdef ROUND_TYPE_SIZE
  1033. layout->record_size = ROUND_TYPE_SIZE (layout->original_type,
  1034. layout->record_size,
  1035. layout->record_align);
  1036. #else
  1037. /* Round the size up to be a multiple of the required alignment */
  1038. layout->record_size = ROUND (layout->record_size, layout->record_align);
  1039. #endif
  1040. layout->type = NULL;
  1041. }
  1042. if (size)
  1043. *size = layout->record_size / BITS_PER_UNIT;
  1044. if (align)
  1045. *align = layout->record_align / BITS_PER_UNIT;
  1046. }
  1047. void objc_layout_structure_get_info (struct objc_struct_layout *layout,
  1048. unsigned int *offset,
  1049. unsigned int *align,
  1050. const char **type)
  1051. {
  1052. if (offset)
  1053. *offset = layout->record_size / BITS_PER_UNIT;
  1054. if (align)
  1055. *align = layout->record_align / BITS_PER_UNIT;
  1056. if (type)
  1057. *type = layout->prev_type;
  1058. }