minor sigsev fix
[official-gcc.git] / libobjc / encoding.c
blobc743d865e17e0c7c1ab84d899f52a428f49f0b71
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
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 3, 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 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.
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/>. */
28 /* FIXME: This file has no business including tm.h. */
30 #include "tconfig.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "objc/objc-api.h"
34 #include "objc/encoding.h"
35 #include <stdlib.h>
37 #undef MAX
38 #define MAX(X, Y) \
39 ({ typeof (X) __x = (X), __y = (Y); \
40 (__x > __y ? __x : __y); })
42 #undef MIN
43 #define MIN(X, Y) \
44 ({ typeof (X) __x = (X), __y = (Y); \
45 (__x < __y ? __x : __y); })
47 #undef ROUND
48 #define ROUND(V, A) \
49 ({ typeof (V) __v = (V); typeof (A) __a = (A); \
50 __a * ((__v+__a - 1)/__a); })
53 /* Various hacks for objc_layout_record. These are used by the target
54 macros. */
56 #define TREE_CODE(TYPE) *(TYPE)
57 #define TREE_TYPE(TREE) (TREE)
59 #define RECORD_TYPE _C_STRUCT_B
60 #define UNION_TYPE _C_UNION_B
61 #define QUAL_UNION_TYPE _C_UNION_B
62 #define ARRAY_TYPE _C_ARY_B
64 #define REAL_TYPE _C_DBL
66 #define VECTOR_TYPE _C_VECTOR
68 #define TYPE_FIELDS(TYPE) ({const char *_field = (TYPE)+1; \
69 while (*_field != _C_STRUCT_E && *_field != _C_STRUCT_B \
70 && *_field != _C_UNION_B && *_field++ != '=') \
71 /* do nothing */; \
72 _field;})
74 #define DECL_MODE(TYPE) *(TYPE)
75 #define TYPE_MODE(TYPE) *(TYPE)
77 #define DFmode _C_DBL
79 #define strip_array_types(TYPE) ({const char *_field = (TYPE); \
80 while (*_field == _C_ARY_B)\
82 while (isdigit ((unsigned char)*++_field))\
85 _field;})
87 /* Some ports (eg ARM) allow the structure size boundary to be
88 selected at compile-time. We override the normal definition with
89 one that has a constant value for this compilation. */
90 #ifndef BITS_PER_UNIT
91 #define BITS_PER_UNIT 8
92 #endif
93 #undef STRUCTURE_SIZE_BOUNDARY
94 #define STRUCTURE_SIZE_BOUNDARY (BITS_PER_UNIT * sizeof (struct{char a;}))
96 /* Some ROUND_TYPE_ALIGN macros use TARGET_foo, and consequently
97 target_flags. Define a dummy entry here to so we don't die.
98 We have to rename it because target_flags may already have been
99 declared extern. */
100 #define target_flags not_target_flags
101 static int __attribute__ ((__unused__)) not_target_flags = 0;
103 /* Some ROUND_TYPE_ALIGN use ALTIVEC_VECTOR_MODE (rs6000 darwin).
104 Define a dummy ALTIVEC_VECTOR_MODE so it will not die. */
105 #undef ALTIVEC_VECTOR_MODE
106 #define ALTIVEC_VECTOR_MODE(MODE) (0)
108 /* Furthermore, some (powerpc) targets also use TARGET_ALIGN_NATURAL
109 in their alignment macros. Currently[4.5/6], rs6000.h points this
110 to a static variable, initialized by target overrides. This is reset
111 in linux64.h but not in darwin64.h. The macro is not used by *86*. */
113 #if __MACH__ && __LP64__
114 # undef TARGET_ALIGN_NATURAL
115 # define TARGET_ALIGN_NATURAL 1
116 #endif
118 /* FIXME: while this file has no business including tm.h, this
119 definitely has no business defining this macro but it
120 is only way around without really rewritting this file,
121 should look after the branch of 3.4 to fix this.
122 FIXME1: It's also out of date, darwin no longer has the same alignment
123 'special' as aix - this is probably the origin of the m32 breakage. */
124 #define rs6000_special_round_type_align(STRUCT, COMPUTED, SPECIFIED) \
125 ({ const char *_fields = TYPE_FIELDS (STRUCT); \
126 ((_fields != 0 \
127 && TYPE_MODE (strip_array_types (TREE_TYPE (_fields))) == DFmode) \
128 ? MAX (MAX (COMPUTED, SPECIFIED), 64) \
129 : MAX (COMPUTED, SPECIFIED));})
130 /* FIXME: The word 'fixme' is insufficient to explain the wrong-ness
131 of this next macro definition. */
132 #define darwin_rs6000_special_round_type_align(S,C,S2) \
133 rs6000_special_round_type_align(S,C,S2)
136 return the size of an object specified by type
140 objc_sizeof_type (const char *type)
142 /* Skip the variable name if any */
143 if (*type == '"')
145 for (type++; *type++ != '"';)
146 /* do nothing */;
149 switch (*type) {
150 case _C_BOOL:
151 return sizeof (_Bool);
152 break;
154 case _C_ID:
155 return sizeof (id);
156 break;
158 case _C_CLASS:
159 return sizeof (Class);
160 break;
162 case _C_SEL:
163 return sizeof (SEL);
164 break;
166 case _C_CHR:
167 return sizeof (char);
168 break;
170 case _C_UCHR:
171 return sizeof (unsigned char);
172 break;
174 case _C_SHT:
175 return sizeof (short);
176 break;
178 case _C_USHT:
179 return sizeof (unsigned short);
180 break;
182 case _C_INT:
183 return sizeof (int);
184 break;
186 case _C_UINT:
187 return sizeof (unsigned int);
188 break;
190 case _C_LNG:
191 return sizeof (long);
192 break;
194 case _C_ULNG:
195 return sizeof (unsigned long);
196 break;
198 case _C_LNG_LNG:
199 return sizeof (long long);
200 break;
202 case _C_ULNG_LNG:
203 return sizeof (unsigned long long);
204 break;
206 case _C_FLT:
207 return sizeof (float);
208 break;
210 case _C_DBL:
211 return sizeof (double);
212 break;
214 case _C_VOID:
215 return sizeof (void);
216 break;
218 case _C_PTR:
219 case _C_ATOM:
220 case _C_CHARPTR:
221 return sizeof (char *);
222 break;
224 case _C_ARY_B:
226 int len = atoi (type + 1);
227 while (isdigit ((unsigned char)*++type))
229 return len * objc_aligned_size (type);
231 break;
233 case _C_BFLD:
235 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
236 int position, size;
237 int startByte, endByte;
239 position = atoi (type + 1);
240 while (isdigit ((unsigned char)*++type))
242 size = atoi (type + 1);
244 startByte = position / BITS_PER_UNIT;
245 endByte = (position + size) / BITS_PER_UNIT;
246 return endByte - startByte;
249 case _C_UNION_B:
250 case _C_STRUCT_B:
252 struct objc_struct_layout layout;
253 unsigned int size;
255 objc_layout_structure (type, &layout);
256 while (objc_layout_structure_next_member (&layout))
257 /* do nothing */ ;
258 objc_layout_finish_structure (&layout, &size, NULL);
260 return size;
263 case _C_COMPLEX:
265 type++; /* Skip after the 'j'. */
266 switch (*type)
268 case _C_CHR:
269 return sizeof (_Complex char);
270 break;
272 case _C_UCHR:
273 return sizeof (_Complex unsigned char);
274 break;
276 case _C_SHT:
277 return sizeof (_Complex short);
278 break;
280 case _C_USHT:
281 return sizeof (_Complex unsigned short);
282 break;
284 case _C_INT:
285 return sizeof (_Complex int);
286 break;
288 case _C_UINT:
289 return sizeof (_Complex unsigned int);
290 break;
292 case _C_LNG:
293 return sizeof (_Complex long);
294 break;
296 case _C_ULNG:
297 return sizeof (_Complex unsigned long);
298 break;
300 case _C_LNG_LNG:
301 return sizeof (_Complex long long);
302 break;
304 case _C_ULNG_LNG:
305 return sizeof (_Complex unsigned long long);
306 break;
308 case _C_FLT:
309 return sizeof (_Complex float);
310 break;
312 case _C_DBL:
313 return sizeof (_Complex double);
314 break;
316 default:
318 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown complex type %s\n",
319 type);
320 return 0;
325 default:
327 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
328 return 0;
335 Return the alignment of an object specified by type
339 objc_alignof_type (const char *type)
341 /* Skip the variable name if any */
342 if (*type == '"')
344 for (type++; *type++ != '"';)
345 /* do nothing */;
347 switch (*type) {
348 case _C_BOOL:
349 return __alignof__ (_Bool);
350 break;
352 case _C_ID:
353 return __alignof__ (id);
354 break;
356 case _C_CLASS:
357 return __alignof__ (Class);
358 break;
360 case _C_SEL:
361 return __alignof__ (SEL);
362 break;
364 case _C_CHR:
365 return __alignof__ (char);
366 break;
368 case _C_UCHR:
369 return __alignof__ (unsigned char);
370 break;
372 case _C_SHT:
373 return __alignof__ (short);
374 break;
376 case _C_USHT:
377 return __alignof__ (unsigned short);
378 break;
380 case _C_INT:
381 return __alignof__ (int);
382 break;
384 case _C_UINT:
385 return __alignof__ (unsigned int);
386 break;
388 case _C_LNG:
389 return __alignof__ (long);
390 break;
392 case _C_ULNG:
393 return __alignof__ (unsigned long);
394 break;
396 case _C_LNG_LNG:
397 return __alignof__ (long long);
398 break;
400 case _C_ULNG_LNG:
401 return __alignof__ (unsigned long long);
402 break;
404 case _C_FLT:
405 return __alignof__ (float);
406 break;
408 case _C_DBL:
409 return __alignof__ (double);
410 break;
412 case _C_PTR:
413 case _C_ATOM:
414 case _C_CHARPTR:
415 return __alignof__ (char *);
416 break;
418 case _C_ARY_B:
419 while (isdigit ((unsigned char)*++type))
420 /* do nothing */;
421 return objc_alignof_type (type);
423 case _C_STRUCT_B:
424 case _C_UNION_B:
426 struct objc_struct_layout layout;
427 unsigned int align;
429 objc_layout_structure (type, &layout);
430 while (objc_layout_structure_next_member (&layout))
431 /* do nothing */;
432 objc_layout_finish_structure (&layout, NULL, &align);
434 return align;
438 case _C_COMPLEX:
440 type++; /* Skip after the 'j'. */
441 switch (*type)
443 case _C_CHR:
444 return __alignof__ (_Complex char);
445 break;
447 case _C_UCHR:
448 return __alignof__ (_Complex unsigned char);
449 break;
451 case _C_SHT:
452 return __alignof__ (_Complex short);
453 break;
455 case _C_USHT:
456 return __alignof__ (_Complex unsigned short);
457 break;
459 case _C_INT:
460 return __alignof__ (_Complex int);
461 break;
463 case _C_UINT:
464 return __alignof__ (_Complex unsigned int);
465 break;
467 case _C_LNG:
468 return __alignof__ (_Complex long);
469 break;
471 case _C_ULNG:
472 return __alignof__ (_Complex unsigned long);
473 break;
475 case _C_LNG_LNG:
476 return __alignof__ (_Complex long long);
477 break;
479 case _C_ULNG_LNG:
480 return __alignof__ (_Complex unsigned long long);
481 break;
483 case _C_FLT:
484 return __alignof__ (_Complex float);
485 break;
487 case _C_DBL:
488 return __alignof__ (_Complex double);
489 break;
491 default:
493 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown complex type %s\n",
494 type);
495 return 0;
500 default:
502 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
503 return 0;
509 The aligned size if the size rounded up to the nearest alignment.
513 objc_aligned_size (const char *type)
515 int size, align;
517 /* Skip the variable name */
518 if (*type == '"')
520 for (type++; *type++ != '"';)
521 /* do nothing */;
524 size = objc_sizeof_type (type);
525 align = objc_alignof_type (type);
527 return ROUND (size, align);
531 The size rounded up to the nearest integral of the wordsize, taken
532 to be the size of a void *.
536 objc_promoted_size (const char *type)
538 int size, wordsize;
540 /* Skip the variable name */
541 if (*type == '"')
543 for (type++; *type++ != '"';)
544 /* do nothing */;
547 size = objc_sizeof_type (type);
548 wordsize = sizeof (void *);
550 return ROUND (size, wordsize);
554 Skip type qualifiers. These may eventually precede typespecs
555 occurring in method prototype encodings.
558 inline const char *
559 objc_skip_type_qualifiers (const char *type)
561 while (*type == _C_CONST
562 || *type == _C_IN
563 || *type == _C_INOUT
564 || *type == _C_OUT
565 || *type == _C_BYCOPY
566 || *type == _C_BYREF
567 || *type == _C_ONEWAY
568 || *type == _C_GCINVISIBLE)
570 type += 1;
572 return type;
577 Skip one typespec element. If the typespec is prepended by type
578 qualifiers, these are skipped as well.
581 const char *
582 objc_skip_typespec (const char *type)
584 /* Skip the variable name if any */
585 if (*type == '"')
587 for (type++; *type++ != '"';)
588 /* do nothing */;
591 type = objc_skip_type_qualifiers (type);
593 switch (*type) {
595 case _C_ID:
596 /* An id may be annotated by the actual type if it is known
597 with the @"ClassName" syntax */
599 if (*++type != '"')
600 return type;
601 else
603 while (*++type != '"')
604 /* do nothing */;
605 return type + 1;
608 /* The following are one character type codes */
609 case _C_CLASS:
610 case _C_SEL:
611 case _C_CHR:
612 case _C_UCHR:
613 case _C_CHARPTR:
614 case _C_ATOM:
615 case _C_SHT:
616 case _C_USHT:
617 case _C_INT:
618 case _C_UINT:
619 case _C_LNG:
620 case _C_BOOL:
621 case _C_ULNG:
622 case _C_LNG_LNG:
623 case _C_ULNG_LNG:
624 case _C_FLT:
625 case _C_DBL:
626 case _C_VOID:
627 case _C_UNDEF:
628 return ++type;
629 break;
631 case _C_COMPLEX:
632 return type + 2;
633 break;
635 case _C_ARY_B:
636 /* skip digits, typespec and closing ']' */
638 while (isdigit ((unsigned char)*++type))
640 type = objc_skip_typespec (type);
641 if (*type == _C_ARY_E)
642 return ++type;
643 else
645 objc_error (nil, OBJC_ERR_BAD_TYPE, "bad array type %s\n", type);
646 return 0;
649 case _C_BFLD:
650 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
651 while (isdigit ((unsigned char)*++type))
652 ; /* skip position */
653 while (isdigit ((unsigned char)*++type))
654 ; /* skip type and size */
655 return type;
657 case _C_STRUCT_B:
658 /* skip name, and elements until closing '}' */
660 while (*type != _C_STRUCT_E && *type++ != '=')
662 while (*type != _C_STRUCT_E)
664 type = objc_skip_typespec (type);
666 return ++type;
668 case _C_UNION_B:
669 /* skip name, and elements until closing ')' */
671 while (*type != _C_UNION_E && *type++ != '=')
673 while (*type != _C_UNION_E)
675 type = objc_skip_typespec (type);
677 return ++type;
679 case _C_PTR:
680 /* Just skip the following typespec */
682 return objc_skip_typespec (++type);
684 default:
686 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
687 return 0;
693 Skip an offset as part of a method encoding. This is prepended by a
694 '+' if the argument is passed in registers.
696 inline const char *
697 objc_skip_offset (const char *type)
699 if (*type == '+')
700 type++;
701 while (isdigit ((unsigned char) *++type))
703 return type;
707 Skip an argument specification of a method encoding.
709 const char *
710 objc_skip_argspec (const char *type)
712 type = objc_skip_typespec (type);
713 type = objc_skip_offset (type);
714 return type;
718 Return the number of arguments that the method MTH expects.
719 Note that all methods need two implicit arguments `self' and
720 `_cmd'.
723 method_get_number_of_arguments (struct objc_method *mth)
725 int i = 0;
726 const char *type = mth->method_types;
727 while (*type)
729 type = objc_skip_argspec (type);
730 i += 1;
732 return i - 1;
736 Return the size of the argument block needed on the stack to invoke
737 the method MTH. This may be zero, if all arguments are passed in
738 registers.
742 method_get_sizeof_arguments (struct objc_method *mth)
744 const char *type = objc_skip_typespec (mth->method_types);
745 return atoi (type);
749 Return a pointer to the next argument of ARGFRAME. type points to
750 the last argument. Typical use of this look like:
753 char *datum, *type;
754 for (datum = method_get_first_argument (method, argframe, &type);
755 datum; datum = method_get_next_argument (argframe, &type))
757 unsigned flags = objc_get_type_qualifiers (type);
758 type = objc_skip_type_qualifiers (type);
759 if (*type != _C_PTR)
760 [portal encodeData: datum ofType: type];
761 else
763 if ((flags & _F_IN) == _F_IN)
764 [portal encodeData: *(char **) datum ofType: ++type];
770 char *
771 method_get_next_argument (arglist_t argframe, const char **type)
773 const char *t = objc_skip_argspec (*type);
775 if (*t == '\0')
776 return 0;
778 *type = t;
779 t = objc_skip_typespec (t);
781 if (*t == '+')
782 return argframe->arg_regs + atoi (++t);
783 else
784 return argframe->arg_ptr + atoi (t);
788 Return a pointer to the value of the first argument of the method
789 described in M with the given argumentframe ARGFRAME. The type
790 is returned in TYPE. type must be passed to successive calls of
791 method_get_next_argument.
793 char *
794 method_get_first_argument (struct objc_method *m,
795 arglist_t argframe,
796 const char **type)
798 *type = m->method_types;
799 return method_get_next_argument (argframe, type);
803 Return a pointer to the ARGth argument of the method
804 M from the frame ARGFRAME. The type of the argument
805 is returned in the value-result argument TYPE
808 char *
809 method_get_nth_argument (struct objc_method *m,
810 arglist_t argframe, int arg,
811 const char **type)
813 const char *t = objc_skip_argspec (m->method_types);
815 if (arg > method_get_number_of_arguments (m))
816 return 0;
818 while (arg--)
819 t = objc_skip_argspec (t);
821 *type = t;
822 t = objc_skip_typespec (t);
824 if (*t == '+')
825 return argframe->arg_regs + atoi (++t);
826 else
827 return argframe->arg_ptr + atoi (t);
830 unsigned
831 objc_get_type_qualifiers (const char *type)
833 unsigned res = 0;
834 BOOL flag = YES;
836 while (flag)
837 switch (*type++)
839 case _C_CONST: res |= _F_CONST; break;
840 case _C_IN: res |= _F_IN; break;
841 case _C_INOUT: res |= _F_INOUT; break;
842 case _C_OUT: res |= _F_OUT; break;
843 case _C_BYCOPY: res |= _F_BYCOPY; break;
844 case _C_BYREF: res |= _F_BYREF; break;
845 case _C_ONEWAY: res |= _F_ONEWAY; break;
846 case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break;
847 default: flag = NO;
850 return res;
854 /* The following three functions can be used to determine how a
855 structure is laid out by the compiler. For example:
857 struct objc_struct_layout layout;
858 int i;
860 objc_layout_structure (type, &layout);
861 while (objc_layout_structure_next_member (&layout))
863 int position, align;
864 const char *type;
866 objc_layout_structure_get_info (&layout, &position, &align, &type);
867 printf ("element %d has offset %d, alignment %d\n",
868 i++, position, align);
871 These functions are used by objc_sizeof_type and objc_alignof_type
872 functions to compute the size and alignment of structures. The
873 previous method of computing the size and alignment of a structure
874 was not working on some architectures, particulary on AIX, and in
875 the presence of bitfields inside the structure. */
876 void
877 objc_layout_structure (const char *type,
878 struct objc_struct_layout *layout)
880 const char *ntype;
882 if (*type != _C_UNION_B && *type != _C_STRUCT_B)
884 objc_error (nil, OBJC_ERR_BAD_TYPE,
885 "record (or union) type expected in objc_layout_structure, got %s\n",
886 type);
889 type ++;
890 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 */;
898 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
899 if (*(ntype - 1) == '=')
900 type = ntype;
902 layout->type = type;
903 layout->prev_type = NULL;
904 layout->record_size = 0;
905 layout->record_align = BITS_PER_UNIT;
907 layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY);
911 BOOL
912 objc_layout_structure_next_member (struct objc_struct_layout *layout)
914 register int desired_align = 0;
916 /* The following are used only if the field is a bitfield */
917 register const char *bfld_type = 0;
918 register int bfld_type_align = 0, bfld_field_size = 0;
920 /* The current type without the type qualifiers */
921 const char *type;
922 BOOL unionp = layout->original_type[-1] == _C_UNION_B;
924 /* Add the size of the previous field to the size of the record. */
925 if (layout->prev_type)
927 type = objc_skip_type_qualifiers (layout->prev_type);
928 if (unionp)
929 layout->record_size = MAX (layout->record_size,
930 objc_sizeof_type (type) * BITS_PER_UNIT);
932 else if (*type != _C_BFLD)
933 layout->record_size += objc_sizeof_type (type) * BITS_PER_UNIT;
934 else {
935 /* Get the bitfield's type */
936 for (bfld_type = type + 1;
937 isdigit ((unsigned char)*bfld_type);
938 bfld_type++)
939 /* do nothing */;
941 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
942 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
943 layout->record_size += bfld_field_size;
947 if ((unionp && *layout->type == _C_UNION_E)
948 || (!unionp && *layout->type == _C_STRUCT_E))
949 return NO;
951 /* Skip the variable name if any */
952 if (*layout->type == '"')
954 for (layout->type++; *layout->type++ != '"';)
955 /* do nothing */;
958 type = objc_skip_type_qualifiers (layout->type);
960 if (*type != _C_BFLD)
961 desired_align = objc_alignof_type (type) * BITS_PER_UNIT;
962 else
964 desired_align = 1;
965 /* Skip the bitfield's offset */
966 for (bfld_type = type + 1;
967 isdigit ((unsigned char) *bfld_type);
968 bfld_type++)
969 /* do nothing */;
971 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
972 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
975 #ifdef BIGGEST_FIELD_ALIGNMENT
976 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
977 #endif
978 #ifdef ADJUST_FIELD_ALIGN
979 desired_align = ADJUST_FIELD_ALIGN (type, desired_align);
980 #endif
982 /* Record must have at least as much alignment as any field.
983 Otherwise, the alignment of the field within the record
984 is meaningless. */
985 #ifndef PCC_BITFIELD_TYPE_MATTERS
986 layout->record_align = MAX (layout->record_align, desired_align);
987 #else /* PCC_BITFIELD_TYPE_MATTERS */
988 if (*type == _C_BFLD)
990 /* For these machines, a zero-length field does not
991 affect the alignment of the structure as a whole.
992 It does, however, affect the alignment of the next field
993 within the structure. */
994 if (bfld_field_size)
995 layout->record_align = MAX (layout->record_align, desired_align);
996 else
997 desired_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
999 /* A named bit field of declared type `int'
1000 forces the entire structure to have `int' alignment.
1001 Q1: How is encoded this thing and how to check for it?
1002 Q2: How to determine maximum_field_alignment at runtime? */
1004 /* if (DECL_NAME (field) != 0) */
1006 int type_align = bfld_type_align;
1007 #if 0
1008 if (maximum_field_alignment != 0)
1009 type_align = MIN (type_align, maximum_field_alignment);
1010 else if (DECL_PACKED (field))
1011 type_align = MIN (type_align, BITS_PER_UNIT);
1012 #endif
1014 layout->record_align = MAX (layout->record_align, type_align);
1017 else
1018 layout->record_align = MAX (layout->record_align, desired_align);
1019 #endif /* PCC_BITFIELD_TYPE_MATTERS */
1021 /* Does this field automatically have alignment it needs
1022 by virtue of the fields that precede it and the record's
1023 own alignment? */
1025 if (*type == _C_BFLD)
1026 layout->record_size = atoi (type + 1);
1027 else if (layout->record_size % desired_align != 0)
1029 /* No, we need to skip space before this field.
1030 Bump the cumulative size to multiple of field alignment. */
1031 layout->record_size = ROUND (layout->record_size, desired_align);
1034 /* Jump to the next field in record. */
1036 layout->prev_type = layout->type;
1037 layout->type = objc_skip_typespec (layout->type); /* skip component */
1039 return YES;
1043 void objc_layout_finish_structure (struct objc_struct_layout *layout,
1044 unsigned int *size,
1045 unsigned int *align)
1047 BOOL unionp = layout->original_type[-1] == _C_UNION_B;
1048 if (layout->type
1049 && ((!unionp && *layout->type == _C_STRUCT_E)
1050 || (unionp && *layout->type == _C_UNION_E)))
1052 /* Work out the alignment of the record as one expression and store
1053 in the record type. Round it up to a multiple of the record's
1054 alignment. */
1055 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
1056 layout->record_align = ROUND_TYPE_ALIGN (layout->original_type-1,
1058 layout->record_align);
1059 #else
1060 layout->record_align = MAX (1, layout->record_align);
1061 #endif
1063 #ifdef ROUND_TYPE_SIZE
1064 layout->record_size = ROUND_TYPE_SIZE (layout->original_type,
1065 layout->record_size,
1066 layout->record_align);
1067 #else
1068 /* Round the size up to be a multiple of the required alignment */
1069 layout->record_size = ROUND (layout->record_size, layout->record_align);
1070 #endif
1072 layout->type = NULL;
1074 if (size)
1075 *size = layout->record_size / BITS_PER_UNIT;
1076 if (align)
1077 *align = layout->record_align / BITS_PER_UNIT;
1081 void objc_layout_structure_get_info (struct objc_struct_layout *layout,
1082 unsigned int *offset,
1083 unsigned int *align,
1084 const char **type)
1086 if (offset)
1087 *offset = layout->record_size / BITS_PER_UNIT;
1088 if (align)
1089 *align = layout->record_align / BITS_PER_UNIT;
1090 if (type)
1091 *type = layout->prev_type;