Revert r135493 & r135463
[official-gcc.git] / libobjc / encoding.c
blobe65634103aa41c55ff2b903e29ac74513d2a17a4
1 /* Encoding of types for Objective C.
2 Copyright (C) 1993, 1995, 1996, 1997, 1998, 2000, 2002, 2004
3 Free Software Foundation, Inc.
4 Contributed by Kresten Krab Thorup
5 Bitfield support by Ovidiu Predescu
7 This file is part of GCC.
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 2, or (at your option)
12 any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 /* As a special exception, if you link this library with files
25 compiled with GCC to produce an executable, this does not cause
26 the resulting executable to be covered by the GNU General Public License.
27 This exception does not however invalidate any other reasons why
28 the executable file might be covered by the GNU General Public License. */
30 /* FIXME: This file has no business including tm.h. */
32 #include "tconfig.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "objc/objc-api.h"
36 #include "objc/encoding.h"
37 #include <stdlib.h>
39 #undef MAX
40 #define MAX(X, Y) \
41 ({ typeof (X) __x = (X), __y = (Y); \
42 (__x > __y ? __x : __y); })
44 #undef MIN
45 #define MIN(X, Y) \
46 ({ typeof (X) __x = (X), __y = (Y); \
47 (__x < __y ? __x : __y); })
49 #undef ROUND
50 #define ROUND(V, A) \
51 ({ typeof (V) __v = (V); typeof (A) __a = (A); \
52 __a * ((__v+__a - 1)/__a); })
55 /* Various hacks for objc_layout_record. These are used by the target
56 macros. */
58 #define TREE_CODE(TYPE) *(TYPE)
59 #define TREE_TYPE(TREE) (TREE)
61 #define RECORD_TYPE _C_STRUCT_B
62 #define UNION_TYPE _C_UNION_B
63 #define QUAL_UNION_TYPE _C_UNION_B
64 #define ARRAY_TYPE _C_ARY_B
66 #define REAL_TYPE _C_DBL
68 #define VECTOR_TYPE _C_VECTOR
70 #define TYPE_FIELDS(TYPE) ({const char *_field = (TYPE)+1; \
71 while (*_field != _C_STRUCT_E && *_field != _C_STRUCT_B \
72 && *_field != _C_UNION_B && *_field++ != '=') \
73 /* do nothing */; \
74 _field;})
76 #define DECL_MODE(TYPE) *(TYPE)
77 #define TYPE_MODE(TYPE) *(TYPE)
79 #define DFmode _C_DBL
81 #define get_inner_array_type(TYPE) ({const char *_field = (TYPE); \
82 while (*_field == _C_ARY_B)\
84 while (isdigit ((unsigned char)*++_field))\
87 _field;})
89 /* Some ports (eg ARM) allow the structure size boundary to be
90 selected at compile-time. We override the normal definition with
91 one that has a constant value for this compilation. */
92 #ifndef BITS_PER_UNIT
93 #define BITS_PER_UNIT 8
94 #endif
95 #undef STRUCTURE_SIZE_BOUNDARY
96 #define STRUCTURE_SIZE_BOUNDARY (BITS_PER_UNIT * sizeof (struct{char a;}))
98 /* Some ROUND_TYPE_ALIGN macros use TARGET_foo, and consequently
99 target_flags. Define a dummy entry here to so we don't die.
100 We have to rename it because target_flags may already have been
101 declared extern. */
102 #define target_flags not_target_flags
103 static int __attribute__ ((__unused__)) not_target_flags = 0;
105 /* Some ROUND_TYPE_ALIGN use ALTIVEC_VECTOR_MODE (rs6000 darwin).
106 Define a dummy ALTIVEC_VECTOR_MODE so it will not die. */
107 #undef ALTIVEC_VECTOR_MODE
108 #define ALTIVEC_VECTOR_MODE(MODE) (0)
111 /* FIXME: while this file has no business including tm.h, this
112 definitely has no business defining this macro but it
113 is only way around without really rewritting this file,
114 should look after the branch of 3.4 to fix this. */
115 #define rs6000_special_round_type_align(STRUCT, COMPUTED, SPECIFIED) \
116 ({ const char *_fields = TYPE_FIELDS (STRUCT); \
117 ((_fields != 0 \
118 && TYPE_MODE (TREE_CODE (TREE_TYPE (_fields)) == ARRAY_TYPE \
119 ? get_inner_array_type (_fields) \
120 : TREE_TYPE (_fields)) == DFmode) \
121 ? MAX (MAX (COMPUTED, SPECIFIED), 64) \
122 : MAX (COMPUTED, SPECIFIED));})
123 /* FIXME: The word 'fixme' is insufficient to explain the wrong-ness
124 of this next macro definition. */
125 #define darwin_rs6000_special_round_type_align(S,C,S2) \
126 rs6000_special_round_type_align(S,C,S2)
129 return the size of an object specified by type
133 objc_sizeof_type (const char *type)
135 /* Skip the variable name if any */
136 if (*type == '"')
138 for (type++; *type++ != '"';)
139 /* do nothing */;
142 switch (*type) {
143 case _C_BOOL:
144 return sizeof (_Bool);
145 break;
147 case _C_ID:
148 return sizeof (id);
149 break;
151 case _C_CLASS:
152 return sizeof (Class);
153 break;
155 case _C_SEL:
156 return sizeof (SEL);
157 break;
159 case _C_CHR:
160 return sizeof (char);
161 break;
163 case _C_UCHR:
164 return sizeof (unsigned char);
165 break;
167 case _C_SHT:
168 return sizeof (short);
169 break;
171 case _C_USHT:
172 return sizeof (unsigned short);
173 break;
175 case _C_INT:
176 return sizeof (int);
177 break;
179 case _C_UINT:
180 return sizeof (unsigned int);
181 break;
183 case _C_LNG:
184 return sizeof (long);
185 break;
187 case _C_ULNG:
188 return sizeof (unsigned long);
189 break;
191 case _C_LNG_LNG:
192 return sizeof (long long);
193 break;
195 case _C_ULNG_LNG:
196 return sizeof (unsigned long long);
197 break;
199 case _C_FLT:
200 return sizeof (float);
201 break;
203 case _C_DBL:
204 return sizeof (double);
205 break;
207 case _C_VOID:
208 return sizeof (void);
209 break;
211 case _C_PTR:
212 case _C_ATOM:
213 case _C_CHARPTR:
214 return sizeof (char *);
215 break;
217 case _C_ARY_B:
219 int len = atoi (type + 1);
220 while (isdigit ((unsigned char)*++type))
222 return len * objc_aligned_size (type);
224 break;
226 case _C_BFLD:
228 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
229 int position, size;
230 int startByte, endByte;
232 position = atoi (type + 1);
233 while (isdigit ((unsigned char)*++type))
235 size = atoi (type + 1);
237 startByte = position / BITS_PER_UNIT;
238 endByte = (position + size) / BITS_PER_UNIT;
239 return endByte - startByte;
242 case _C_UNION_B:
243 case _C_STRUCT_B:
245 struct objc_struct_layout layout;
246 unsigned int size;
248 objc_layout_structure (type, &layout);
249 while (objc_layout_structure_next_member (&layout))
250 /* do nothing */ ;
251 objc_layout_finish_structure (&layout, &size, NULL);
253 return size;
256 case _C_COMPLEX:
258 type++; /* Skip after the 'j'. */
259 switch (*type)
261 case _C_CHR:
262 return sizeof (_Complex char);
263 break;
265 case _C_UCHR:
266 return sizeof (_Complex unsigned char);
267 break;
269 case _C_SHT:
270 return sizeof (_Complex short);
271 break;
273 case _C_USHT:
274 return sizeof (_Complex unsigned short);
275 break;
277 case _C_INT:
278 return sizeof (_Complex int);
279 break;
281 case _C_UINT:
282 return sizeof (_Complex unsigned int);
283 break;
285 case _C_LNG:
286 return sizeof (_Complex long);
287 break;
289 case _C_ULNG:
290 return sizeof (_Complex unsigned long);
291 break;
293 case _C_LNG_LNG:
294 return sizeof (_Complex long long);
295 break;
297 case _C_ULNG_LNG:
298 return sizeof (_Complex unsigned long long);
299 break;
301 case _C_FLT:
302 return sizeof (_Complex float);
303 break;
305 case _C_DBL:
306 return sizeof (_Complex double);
307 break;
309 default:
311 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown complex type %s\n",
312 type);
313 return 0;
318 default:
320 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
321 return 0;
328 Return the alignment of an object specified by type
332 objc_alignof_type (const char *type)
334 /* Skip the variable name if any */
335 if (*type == '"')
337 for (type++; *type++ != '"';)
338 /* do nothing */;
340 switch (*type) {
341 case _C_BOOL:
342 return __alignof__ (_Bool);
343 break;
345 case _C_ID:
346 return __alignof__ (id);
347 break;
349 case _C_CLASS:
350 return __alignof__ (Class);
351 break;
353 case _C_SEL:
354 return __alignof__ (SEL);
355 break;
357 case _C_CHR:
358 return __alignof__ (char);
359 break;
361 case _C_UCHR:
362 return __alignof__ (unsigned char);
363 break;
365 case _C_SHT:
366 return __alignof__ (short);
367 break;
369 case _C_USHT:
370 return __alignof__ (unsigned short);
371 break;
373 case _C_INT:
374 return __alignof__ (int);
375 break;
377 case _C_UINT:
378 return __alignof__ (unsigned int);
379 break;
381 case _C_LNG:
382 return __alignof__ (long);
383 break;
385 case _C_ULNG:
386 return __alignof__ (unsigned long);
387 break;
389 case _C_LNG_LNG:
390 return __alignof__ (long long);
391 break;
393 case _C_ULNG_LNG:
394 return __alignof__ (unsigned long long);
395 break;
397 case _C_FLT:
398 return __alignof__ (float);
399 break;
401 case _C_DBL:
402 return __alignof__ (double);
403 break;
405 case _C_PTR:
406 case _C_ATOM:
407 case _C_CHARPTR:
408 return __alignof__ (char *);
409 break;
411 case _C_ARY_B:
412 while (isdigit ((unsigned char)*++type))
413 /* do nothing */;
414 return objc_alignof_type (type);
416 case _C_STRUCT_B:
417 case _C_UNION_B:
419 struct objc_struct_layout layout;
420 unsigned int align;
422 objc_layout_structure (type, &layout);
423 while (objc_layout_structure_next_member (&layout))
424 /* do nothing */;
425 objc_layout_finish_structure (&layout, NULL, &align);
427 return align;
431 case _C_COMPLEX:
433 type++; /* Skip after the 'j'. */
434 switch (*type)
436 case _C_CHR:
437 return __alignof__ (_Complex char);
438 break;
440 case _C_UCHR:
441 return __alignof__ (_Complex unsigned char);
442 break;
444 case _C_SHT:
445 return __alignof__ (_Complex short);
446 break;
448 case _C_USHT:
449 return __alignof__ (_Complex unsigned short);
450 break;
452 case _C_INT:
453 return __alignof__ (_Complex int);
454 break;
456 case _C_UINT:
457 return __alignof__ (_Complex unsigned int);
458 break;
460 case _C_LNG:
461 return __alignof__ (_Complex long);
462 break;
464 case _C_ULNG:
465 return __alignof__ (_Complex unsigned long);
466 break;
468 case _C_LNG_LNG:
469 return __alignof__ (_Complex long long);
470 break;
472 case _C_ULNG_LNG:
473 return __alignof__ (_Complex unsigned long long);
474 break;
476 case _C_FLT:
477 return __alignof__ (_Complex float);
478 break;
480 case _C_DBL:
481 return __alignof__ (_Complex double);
482 break;
484 default:
486 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown complex type %s\n",
487 type);
488 return 0;
493 default:
495 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
496 return 0;
502 The aligned size if the size rounded up to the nearest alignment.
506 objc_aligned_size (const char *type)
508 int size, align;
510 /* Skip the variable name */
511 if (*type == '"')
513 for (type++; *type++ != '"';)
514 /* do nothing */;
517 size = objc_sizeof_type (type);
518 align = objc_alignof_type (type);
520 return ROUND (size, align);
524 The size rounded up to the nearest integral of the wordsize, taken
525 to be the size of a void *.
529 objc_promoted_size (const char *type)
531 int size, wordsize;
533 /* Skip the variable name */
534 if (*type == '"')
536 for (type++; *type++ != '"';)
537 /* do nothing */;
540 size = objc_sizeof_type (type);
541 wordsize = sizeof (void *);
543 return ROUND (size, wordsize);
547 Skip type qualifiers. These may eventually precede typespecs
548 occurring in method prototype encodings.
551 inline const char *
552 objc_skip_type_qualifiers (const char *type)
554 while (*type == _C_CONST
555 || *type == _C_IN
556 || *type == _C_INOUT
557 || *type == _C_OUT
558 || *type == _C_BYCOPY
559 || *type == _C_BYREF
560 || *type == _C_ONEWAY
561 || *type == _C_GCINVISIBLE)
563 type += 1;
565 return type;
570 Skip one typespec element. If the typespec is prepended by type
571 qualifiers, these are skipped as well.
574 const char *
575 objc_skip_typespec (const char *type)
577 /* Skip the variable name if any */
578 if (*type == '"')
580 for (type++; *type++ != '"';)
581 /* do nothing */;
584 type = objc_skip_type_qualifiers (type);
586 switch (*type) {
588 case _C_ID:
589 /* An id may be annotated by the actual type if it is known
590 with the @"ClassName" syntax */
592 if (*++type != '"')
593 return type;
594 else
596 while (*++type != '"')
597 /* do nothing */;
598 return type + 1;
601 /* The following are one character type codes */
602 case _C_CLASS:
603 case _C_SEL:
604 case _C_CHR:
605 case _C_UCHR:
606 case _C_CHARPTR:
607 case _C_ATOM:
608 case _C_SHT:
609 case _C_USHT:
610 case _C_INT:
611 case _C_UINT:
612 case _C_LNG:
613 case _C_BOOL:
614 case _C_ULNG:
615 case _C_LNG_LNG:
616 case _C_ULNG_LNG:
617 case _C_FLT:
618 case _C_DBL:
619 case _C_VOID:
620 case _C_UNDEF:
621 return ++type;
622 break;
624 case _C_COMPLEX:
625 return type + 2;
626 break;
628 case _C_ARY_B:
629 /* skip digits, typespec and closing ']' */
631 while (isdigit ((unsigned char)*++type))
633 type = objc_skip_typespec (type);
634 if (*type == _C_ARY_E)
635 return ++type;
636 else
638 objc_error (nil, OBJC_ERR_BAD_TYPE, "bad array type %s\n", type);
639 return 0;
642 case _C_BFLD:
643 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
644 while (isdigit ((unsigned char)*++type))
645 ; /* skip position */
646 while (isdigit ((unsigned char)*++type))
647 ; /* skip type and size */
648 return type;
650 case _C_STRUCT_B:
651 /* skip name, and elements until closing '}' */
653 while (*type != _C_STRUCT_E && *type++ != '=')
655 while (*type != _C_STRUCT_E)
657 type = objc_skip_typespec (type);
659 return ++type;
661 case _C_UNION_B:
662 /* skip name, and elements until closing ')' */
664 while (*type != _C_UNION_E && *type++ != '=')
666 while (*type != _C_UNION_E)
668 type = objc_skip_typespec (type);
670 return ++type;
672 case _C_PTR:
673 /* Just skip the following typespec */
675 return objc_skip_typespec (++type);
677 default:
679 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
680 return 0;
686 Skip an offset as part of a method encoding. This is prepended by a
687 '+' if the argument is passed in registers.
689 inline const char *
690 objc_skip_offset (const char *type)
692 if (*type == '+')
693 type++;
694 while (isdigit ((unsigned char) *++type))
696 return type;
700 Skip an argument specification of a method encoding.
702 const char *
703 objc_skip_argspec (const char *type)
705 type = objc_skip_typespec (type);
706 type = objc_skip_offset (type);
707 return type;
711 Return the number of arguments that the method MTH expects.
712 Note that all methods need two implicit arguments `self' and
713 `_cmd'.
716 method_get_number_of_arguments (struct objc_method *mth)
718 int i = 0;
719 const char *type = mth->method_types;
720 while (*type)
722 type = objc_skip_argspec (type);
723 i += 1;
725 return i - 1;
729 Return the size of the argument block needed on the stack to invoke
730 the method MTH. This may be zero, if all arguments are passed in
731 registers.
735 method_get_sizeof_arguments (struct objc_method *mth)
737 const char *type = objc_skip_typespec (mth->method_types);
738 return atoi (type);
742 Return a pointer to the next argument of ARGFRAME. type points to
743 the last argument. Typical use of this look like:
746 char *datum, *type;
747 for (datum = method_get_first_argument (method, argframe, &type);
748 datum; datum = method_get_next_argument (argframe, &type))
750 unsigned flags = objc_get_type_qualifiers (type);
751 type = objc_skip_type_qualifiers (type);
752 if (*type != _C_PTR)
753 [portal encodeData: datum ofType: type];
754 else
756 if ((flags & _F_IN) == _F_IN)
757 [portal encodeData: *(char **) datum ofType: ++type];
763 char *
764 method_get_next_argument (arglist_t argframe, const char **type)
766 const char *t = objc_skip_argspec (*type);
768 if (*t == '\0')
769 return 0;
771 *type = t;
772 t = objc_skip_typespec (t);
774 if (*t == '+')
775 return argframe->arg_regs + atoi (++t);
776 else
777 return argframe->arg_ptr + atoi (t);
781 Return a pointer to the value of the first argument of the method
782 described in M with the given argumentframe ARGFRAME. The type
783 is returned in TYPE. type must be passed to successive calls of
784 method_get_next_argument.
786 char *
787 method_get_first_argument (struct objc_method *m,
788 arglist_t argframe,
789 const char **type)
791 *type = m->method_types;
792 return method_get_next_argument (argframe, type);
796 Return a pointer to the ARGth argument of the method
797 M from the frame ARGFRAME. The type of the argument
798 is returned in the value-result argument TYPE
801 char *
802 method_get_nth_argument (struct objc_method *m,
803 arglist_t argframe, int arg,
804 const char **type)
806 const char *t = objc_skip_argspec (m->method_types);
808 if (arg > method_get_number_of_arguments (m))
809 return 0;
811 while (arg--)
812 t = objc_skip_argspec (t);
814 *type = t;
815 t = objc_skip_typespec (t);
817 if (*t == '+')
818 return argframe->arg_regs + atoi (++t);
819 else
820 return argframe->arg_ptr + atoi (t);
823 unsigned
824 objc_get_type_qualifiers (const char *type)
826 unsigned res = 0;
827 BOOL flag = YES;
829 while (flag)
830 switch (*type++)
832 case _C_CONST: res |= _F_CONST; break;
833 case _C_IN: res |= _F_IN; break;
834 case _C_INOUT: res |= _F_INOUT; break;
835 case _C_OUT: res |= _F_OUT; break;
836 case _C_BYCOPY: res |= _F_BYCOPY; break;
837 case _C_BYREF: res |= _F_BYREF; break;
838 case _C_ONEWAY: res |= _F_ONEWAY; break;
839 case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break;
840 default: flag = NO;
843 return res;
847 /* The following three functions can be used to determine how a
848 structure is laid out by the compiler. For example:
850 struct objc_struct_layout layout;
851 int i;
853 objc_layout_structure (type, &layout);
854 while (objc_layout_structure_next_member (&layout))
856 int position, align;
857 const char *type;
859 objc_layout_structure_get_info (&layout, &position, &align, &type);
860 printf ("element %d has offset %d, alignment %d\n",
861 i++, position, align);
864 These functions are used by objc_sizeof_type and objc_alignof_type
865 functions to compute the size and alignment of structures. The
866 previous method of computing the size and alignment of a structure
867 was not working on some architectures, particulary on AIX, and in
868 the presence of bitfields inside the structure. */
869 void
870 objc_layout_structure (const char *type,
871 struct objc_struct_layout *layout)
873 const char *ntype;
875 if (*type != _C_UNION_B && *type != _C_STRUCT_B)
877 objc_error (nil, OBJC_ERR_BAD_TYPE,
878 "record (or union) type expected in objc_layout_structure, got %s\n",
879 type);
882 type ++;
883 layout->original_type = type;
885 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
886 ntype = type;
887 while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B
888 && *ntype++ != '=')
889 /* do nothing */;
891 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
892 if (*(ntype - 1) == '=')
893 type = ntype;
895 layout->type = type;
896 layout->prev_type = NULL;
897 layout->record_size = 0;
898 layout->record_align = BITS_PER_UNIT;
900 layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY);
904 BOOL
905 objc_layout_structure_next_member (struct objc_struct_layout *layout)
907 register int desired_align = 0;
909 /* The following are used only if the field is a bitfield */
910 register const char *bfld_type = 0;
911 register int bfld_type_size, 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;
917 /* Add the size of the previous field to the size of the record. */
918 if (layout->prev_type)
920 type = objc_skip_type_qualifiers (layout->prev_type);
921 if (unionp)
922 layout->record_size = MAX (layout->record_size,
923 objc_sizeof_type (type) * BITS_PER_UNIT);
925 else if (*type != _C_BFLD)
926 layout->record_size += objc_sizeof_type (type) * BITS_PER_UNIT;
927 else {
928 /* Get the bitfield's type */
929 for (bfld_type = type + 1;
930 isdigit ((unsigned char)*bfld_type);
931 bfld_type++)
932 /* do nothing */;
934 bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT;
935 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
936 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
937 layout->record_size += bfld_field_size;
941 if ((unionp && *layout->type == _C_UNION_E)
942 || (!unionp && *layout->type == _C_STRUCT_E))
943 return NO;
945 /* Skip the variable name if any */
946 if (*layout->type == '"')
948 for (layout->type++; *layout->type++ != '"';)
949 /* do nothing */;
952 type = objc_skip_type_qualifiers (layout->type);
954 if (*type != _C_BFLD)
955 desired_align = objc_alignof_type (type) * BITS_PER_UNIT;
956 else
958 desired_align = 1;
959 /* Skip the bitfield's offset */
960 for (bfld_type = type + 1;
961 isdigit ((unsigned char) *bfld_type);
962 bfld_type++)
963 /* do nothing */;
965 bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT;
966 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
967 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
970 #ifdef BIGGEST_FIELD_ALIGNMENT
971 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
972 #endif
973 #ifdef ADJUST_FIELD_ALIGN
974 desired_align = ADJUST_FIELD_ALIGN (type, desired_align);
975 #endif
977 /* Record must have at least as much alignment as any field.
978 Otherwise, the alignment of the field within the record
979 is meaningless. */
980 #ifndef PCC_BITFIELD_TYPE_MATTERS
981 layout->record_align = MAX (layout->record_align, desired_align);
982 #else /* PCC_BITFIELD_TYPE_MATTERS */
983 if (*type == _C_BFLD)
985 /* For these machines, a zero-length field does not
986 affect the alignment of the structure as a whole.
987 It does, however, affect the alignment of the next field
988 within the structure. */
989 if (bfld_field_size)
990 layout->record_align = MAX (layout->record_align, desired_align);
991 else
992 desired_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
994 /* A named bit field of declared type `int'
995 forces the entire structure to have `int' alignment.
996 Q1: How is encoded this thing and how to check for it?
997 Q2: How to determine maximum_field_alignment at runtime? */
999 /* if (DECL_NAME (field) != 0) */
1001 int type_align = bfld_type_align;
1002 #if 0
1003 if (maximum_field_alignment != 0)
1004 type_align = MIN (type_align, maximum_field_alignment);
1005 else if (DECL_PACKED (field))
1006 type_align = MIN (type_align, BITS_PER_UNIT);
1007 #endif
1009 layout->record_align = MAX (layout->record_align, type_align);
1012 else
1013 layout->record_align = MAX (layout->record_align, desired_align);
1014 #endif /* PCC_BITFIELD_TYPE_MATTERS */
1016 /* Does this field automatically have alignment it needs
1017 by virtue of the fields that precede it and the record's
1018 own alignment? */
1020 if (*type == _C_BFLD)
1021 layout->record_size = atoi (type + 1);
1022 else if (layout->record_size % desired_align != 0)
1024 /* No, we need to skip space before this field.
1025 Bump the cumulative size to multiple of field alignment. */
1026 layout->record_size = ROUND (layout->record_size, desired_align);
1029 /* Jump to the next field in record. */
1031 layout->prev_type = layout->type;
1032 layout->type = objc_skip_typespec (layout->type); /* skip component */
1034 return YES;
1038 void objc_layout_finish_structure (struct objc_struct_layout *layout,
1039 unsigned int *size,
1040 unsigned int *align)
1042 BOOL unionp = layout->original_type[-1] == _C_UNION_B;
1043 if (layout->type
1044 && ((!unionp && *layout->type == _C_STRUCT_E)
1045 || (unionp && *layout->type == _C_UNION_E)))
1047 /* Work out the alignment of the record as one expression and store
1048 in the record type. Round it up to a multiple of the record's
1049 alignment. */
1050 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
1051 layout->record_align = ROUND_TYPE_ALIGN (layout->original_type-1,
1053 layout->record_align);
1054 #else
1055 layout->record_align = MAX (1, layout->record_align);
1056 #endif
1058 #ifdef ROUND_TYPE_SIZE
1059 layout->record_size = ROUND_TYPE_SIZE (layout->original_type,
1060 layout->record_size,
1061 layout->record_align);
1062 #else
1063 /* Round the size up to be a multiple of the required alignment */
1064 layout->record_size = ROUND (layout->record_size, layout->record_align);
1065 #endif
1067 layout->type = NULL;
1069 if (size)
1070 *size = layout->record_size / BITS_PER_UNIT;
1071 if (align)
1072 *align = layout->record_align / BITS_PER_UNIT;
1076 void objc_layout_structure_get_info (struct objc_struct_layout *layout,
1077 unsigned int *offset,
1078 unsigned int *align,
1079 const char **type)
1081 if (offset)
1082 *offset = layout->record_size / BITS_PER_UNIT;
1083 if (align)
1084 *align = layout->record_align / BITS_PER_UNIT;
1085 if (type)
1086 *type = layout->prev_type;