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