2008-10-02 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / libobjc / encoding.c
blob93395ddaffce023c4902b45352c8561060d59aab
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 strip_array_types(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 (strip_array_types (TREE_TYPE (_fields))) == DFmode) \
119 ? MAX (MAX (COMPUTED, SPECIFIED), 64) \
120 : MAX (COMPUTED, SPECIFIED));})
121 /* FIXME: The word 'fixme' is insufficient to explain the wrong-ness
122 of this next macro definition. */
123 #define darwin_rs6000_special_round_type_align(S,C,S2) \
124 rs6000_special_round_type_align(S,C,S2)
127 return the size of an object specified by type
131 objc_sizeof_type (const char *type)
133 /* Skip the variable name if any */
134 if (*type == '"')
136 for (type++; *type++ != '"';)
137 /* do nothing */;
140 switch (*type) {
141 case _C_BOOL:
142 return sizeof (_Bool);
143 break;
145 case _C_ID:
146 return sizeof (id);
147 break;
149 case _C_CLASS:
150 return sizeof (Class);
151 break;
153 case _C_SEL:
154 return sizeof (SEL);
155 break;
157 case _C_CHR:
158 return sizeof (char);
159 break;
161 case _C_UCHR:
162 return sizeof (unsigned char);
163 break;
165 case _C_SHT:
166 return sizeof (short);
167 break;
169 case _C_USHT:
170 return sizeof (unsigned short);
171 break;
173 case _C_INT:
174 return sizeof (int);
175 break;
177 case _C_UINT:
178 return sizeof (unsigned int);
179 break;
181 case _C_LNG:
182 return sizeof (long);
183 break;
185 case _C_ULNG:
186 return sizeof (unsigned long);
187 break;
189 case _C_LNG_LNG:
190 return sizeof (long long);
191 break;
193 case _C_ULNG_LNG:
194 return sizeof (unsigned long long);
195 break;
197 case _C_FLT:
198 return sizeof (float);
199 break;
201 case _C_DBL:
202 return sizeof (double);
203 break;
205 case _C_VOID:
206 return sizeof (void);
207 break;
209 case _C_PTR:
210 case _C_ATOM:
211 case _C_CHARPTR:
212 return sizeof (char *);
213 break;
215 case _C_ARY_B:
217 int len = atoi (type + 1);
218 while (isdigit ((unsigned char)*++type))
220 return len * objc_aligned_size (type);
222 break;
224 case _C_BFLD:
226 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
227 int position, size;
228 int startByte, endByte;
230 position = atoi (type + 1);
231 while (isdigit ((unsigned char)*++type))
233 size = atoi (type + 1);
235 startByte = position / BITS_PER_UNIT;
236 endByte = (position + size) / BITS_PER_UNIT;
237 return endByte - startByte;
240 case _C_UNION_B:
241 case _C_STRUCT_B:
243 struct objc_struct_layout layout;
244 unsigned int size;
246 objc_layout_structure (type, &layout);
247 while (objc_layout_structure_next_member (&layout))
248 /* do nothing */ ;
249 objc_layout_finish_structure (&layout, &size, NULL);
251 return size;
254 case _C_COMPLEX:
256 type++; /* Skip after the 'j'. */
257 switch (*type)
259 case _C_CHR:
260 return sizeof (_Complex char);
261 break;
263 case _C_UCHR:
264 return sizeof (_Complex unsigned char);
265 break;
267 case _C_SHT:
268 return sizeof (_Complex short);
269 break;
271 case _C_USHT:
272 return sizeof (_Complex unsigned short);
273 break;
275 case _C_INT:
276 return sizeof (_Complex int);
277 break;
279 case _C_UINT:
280 return sizeof (_Complex unsigned int);
281 break;
283 case _C_LNG:
284 return sizeof (_Complex long);
285 break;
287 case _C_ULNG:
288 return sizeof (_Complex unsigned long);
289 break;
291 case _C_LNG_LNG:
292 return sizeof (_Complex long long);
293 break;
295 case _C_ULNG_LNG:
296 return sizeof (_Complex unsigned long long);
297 break;
299 case _C_FLT:
300 return sizeof (_Complex float);
301 break;
303 case _C_DBL:
304 return sizeof (_Complex double);
305 break;
307 default:
309 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown complex type %s\n",
310 type);
311 return 0;
316 default:
318 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
319 return 0;
326 Return the alignment of an object specified by type
330 objc_alignof_type (const char *type)
332 /* Skip the variable name if any */
333 if (*type == '"')
335 for (type++; *type++ != '"';)
336 /* do nothing */;
338 switch (*type) {
339 case _C_BOOL:
340 return __alignof__ (_Bool);
341 break;
343 case _C_ID:
344 return __alignof__ (id);
345 break;
347 case _C_CLASS:
348 return __alignof__ (Class);
349 break;
351 case _C_SEL:
352 return __alignof__ (SEL);
353 break;
355 case _C_CHR:
356 return __alignof__ (char);
357 break;
359 case _C_UCHR:
360 return __alignof__ (unsigned char);
361 break;
363 case _C_SHT:
364 return __alignof__ (short);
365 break;
367 case _C_USHT:
368 return __alignof__ (unsigned short);
369 break;
371 case _C_INT:
372 return __alignof__ (int);
373 break;
375 case _C_UINT:
376 return __alignof__ (unsigned int);
377 break;
379 case _C_LNG:
380 return __alignof__ (long);
381 break;
383 case _C_ULNG:
384 return __alignof__ (unsigned long);
385 break;
387 case _C_LNG_LNG:
388 return __alignof__ (long long);
389 break;
391 case _C_ULNG_LNG:
392 return __alignof__ (unsigned long long);
393 break;
395 case _C_FLT:
396 return __alignof__ (float);
397 break;
399 case _C_DBL:
400 return __alignof__ (double);
401 break;
403 case _C_PTR:
404 case _C_ATOM:
405 case _C_CHARPTR:
406 return __alignof__ (char *);
407 break;
409 case _C_ARY_B:
410 while (isdigit ((unsigned char)*++type))
411 /* do nothing */;
412 return objc_alignof_type (type);
414 case _C_STRUCT_B:
415 case _C_UNION_B:
417 struct objc_struct_layout layout;
418 unsigned int align;
420 objc_layout_structure (type, &layout);
421 while (objc_layout_structure_next_member (&layout))
422 /* do nothing */;
423 objc_layout_finish_structure (&layout, NULL, &align);
425 return align;
429 case _C_COMPLEX:
431 type++; /* Skip after the 'j'. */
432 switch (*type)
434 case _C_CHR:
435 return __alignof__ (_Complex char);
436 break;
438 case _C_UCHR:
439 return __alignof__ (_Complex unsigned char);
440 break;
442 case _C_SHT:
443 return __alignof__ (_Complex short);
444 break;
446 case _C_USHT:
447 return __alignof__ (_Complex unsigned short);
448 break;
450 case _C_INT:
451 return __alignof__ (_Complex int);
452 break;
454 case _C_UINT:
455 return __alignof__ (_Complex unsigned int);
456 break;
458 case _C_LNG:
459 return __alignof__ (_Complex long);
460 break;
462 case _C_ULNG:
463 return __alignof__ (_Complex unsigned long);
464 break;
466 case _C_LNG_LNG:
467 return __alignof__ (_Complex long long);
468 break;
470 case _C_ULNG_LNG:
471 return __alignof__ (_Complex unsigned long long);
472 break;
474 case _C_FLT:
475 return __alignof__ (_Complex float);
476 break;
478 case _C_DBL:
479 return __alignof__ (_Complex double);
480 break;
482 default:
484 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown complex type %s\n",
485 type);
486 return 0;
491 default:
493 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
494 return 0;
500 The aligned size if the size rounded up to the nearest alignment.
504 objc_aligned_size (const char *type)
506 int size, align;
508 /* Skip the variable name */
509 if (*type == '"')
511 for (type++; *type++ != '"';)
512 /* do nothing */;
515 size = objc_sizeof_type (type);
516 align = objc_alignof_type (type);
518 return ROUND (size, align);
522 The size rounded up to the nearest integral of the wordsize, taken
523 to be the size of a void *.
527 objc_promoted_size (const char *type)
529 int size, wordsize;
531 /* Skip the variable name */
532 if (*type == '"')
534 for (type++; *type++ != '"';)
535 /* do nothing */;
538 size = objc_sizeof_type (type);
539 wordsize = sizeof (void *);
541 return ROUND (size, wordsize);
545 Skip type qualifiers. These may eventually precede typespecs
546 occurring in method prototype encodings.
549 inline const char *
550 objc_skip_type_qualifiers (const char *type)
552 while (*type == _C_CONST
553 || *type == _C_IN
554 || *type == _C_INOUT
555 || *type == _C_OUT
556 || *type == _C_BYCOPY
557 || *type == _C_BYREF
558 || *type == _C_ONEWAY
559 || *type == _C_GCINVISIBLE)
561 type += 1;
563 return type;
568 Skip one typespec element. If the typespec is prepended by type
569 qualifiers, these are skipped as well.
572 const char *
573 objc_skip_typespec (const char *type)
575 /* Skip the variable name if any */
576 if (*type == '"')
578 for (type++; *type++ != '"';)
579 /* do nothing */;
582 type = objc_skip_type_qualifiers (type);
584 switch (*type) {
586 case _C_ID:
587 /* An id may be annotated by the actual type if it is known
588 with the @"ClassName" syntax */
590 if (*++type != '"')
591 return type;
592 else
594 while (*++type != '"')
595 /* do nothing */;
596 return type + 1;
599 /* The following are one character type codes */
600 case _C_CLASS:
601 case _C_SEL:
602 case _C_CHR:
603 case _C_UCHR:
604 case _C_CHARPTR:
605 case _C_ATOM:
606 case _C_SHT:
607 case _C_USHT:
608 case _C_INT:
609 case _C_UINT:
610 case _C_LNG:
611 case _C_BOOL:
612 case _C_ULNG:
613 case _C_LNG_LNG:
614 case _C_ULNG_LNG:
615 case _C_FLT:
616 case _C_DBL:
617 case _C_VOID:
618 case _C_UNDEF:
619 return ++type;
620 break;
622 case _C_COMPLEX:
623 return type + 2;
624 break;
626 case _C_ARY_B:
627 /* skip digits, typespec and closing ']' */
629 while (isdigit ((unsigned char)*++type))
631 type = objc_skip_typespec (type);
632 if (*type == _C_ARY_E)
633 return ++type;
634 else
636 objc_error (nil, OBJC_ERR_BAD_TYPE, "bad array type %s\n", type);
637 return 0;
640 case _C_BFLD:
641 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
642 while (isdigit ((unsigned char)*++type))
643 ; /* skip position */
644 while (isdigit ((unsigned char)*++type))
645 ; /* skip type and size */
646 return type;
648 case _C_STRUCT_B:
649 /* skip name, and elements until closing '}' */
651 while (*type != _C_STRUCT_E && *type++ != '=')
653 while (*type != _C_STRUCT_E)
655 type = objc_skip_typespec (type);
657 return ++type;
659 case _C_UNION_B:
660 /* skip name, and elements until closing ')' */
662 while (*type != _C_UNION_E && *type++ != '=')
664 while (*type != _C_UNION_E)
666 type = objc_skip_typespec (type);
668 return ++type;
670 case _C_PTR:
671 /* Just skip the following typespec */
673 return objc_skip_typespec (++type);
675 default:
677 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
678 return 0;
684 Skip an offset as part of a method encoding. This is prepended by a
685 '+' if the argument is passed in registers.
687 inline const char *
688 objc_skip_offset (const char *type)
690 if (*type == '+')
691 type++;
692 while (isdigit ((unsigned char) *++type))
694 return type;
698 Skip an argument specification of a method encoding.
700 const char *
701 objc_skip_argspec (const char *type)
703 type = objc_skip_typespec (type);
704 type = objc_skip_offset (type);
705 return type;
709 Return the number of arguments that the method MTH expects.
710 Note that all methods need two implicit arguments `self' and
711 `_cmd'.
714 method_get_number_of_arguments (struct objc_method *mth)
716 int i = 0;
717 const char *type = mth->method_types;
718 while (*type)
720 type = objc_skip_argspec (type);
721 i += 1;
723 return i - 1;
727 Return the size of the argument block needed on the stack to invoke
728 the method MTH. This may be zero, if all arguments are passed in
729 registers.
733 method_get_sizeof_arguments (struct objc_method *mth)
735 const char *type = objc_skip_typespec (mth->method_types);
736 return atoi (type);
740 Return a pointer to the next argument of ARGFRAME. type points to
741 the last argument. Typical use of this look like:
744 char *datum, *type;
745 for (datum = method_get_first_argument (method, argframe, &type);
746 datum; datum = method_get_next_argument (argframe, &type))
748 unsigned flags = objc_get_type_qualifiers (type);
749 type = objc_skip_type_qualifiers (type);
750 if (*type != _C_PTR)
751 [portal encodeData: datum ofType: type];
752 else
754 if ((flags & _F_IN) == _F_IN)
755 [portal encodeData: *(char **) datum ofType: ++type];
761 char *
762 method_get_next_argument (arglist_t argframe, const char **type)
764 const char *t = objc_skip_argspec (*type);
766 if (*t == '\0')
767 return 0;
769 *type = t;
770 t = objc_skip_typespec (t);
772 if (*t == '+')
773 return argframe->arg_regs + atoi (++t);
774 else
775 return argframe->arg_ptr + atoi (t);
779 Return a pointer to the value of the first argument of the method
780 described in M with the given argumentframe ARGFRAME. The type
781 is returned in TYPE. type must be passed to successive calls of
782 method_get_next_argument.
784 char *
785 method_get_first_argument (struct objc_method *m,
786 arglist_t argframe,
787 const char **type)
789 *type = m->method_types;
790 return method_get_next_argument (argframe, type);
794 Return a pointer to the ARGth argument of the method
795 M from the frame ARGFRAME. The type of the argument
796 is returned in the value-result argument TYPE
799 char *
800 method_get_nth_argument (struct objc_method *m,
801 arglist_t argframe, int arg,
802 const char **type)
804 const char *t = objc_skip_argspec (m->method_types);
806 if (arg > method_get_number_of_arguments (m))
807 return 0;
809 while (arg--)
810 t = objc_skip_argspec (t);
812 *type = t;
813 t = objc_skip_typespec (t);
815 if (*t == '+')
816 return argframe->arg_regs + atoi (++t);
817 else
818 return argframe->arg_ptr + atoi (t);
821 unsigned
822 objc_get_type_qualifiers (const char *type)
824 unsigned res = 0;
825 BOOL flag = YES;
827 while (flag)
828 switch (*type++)
830 case _C_CONST: res |= _F_CONST; break;
831 case _C_IN: res |= _F_IN; break;
832 case _C_INOUT: res |= _F_INOUT; break;
833 case _C_OUT: res |= _F_OUT; break;
834 case _C_BYCOPY: res |= _F_BYCOPY; break;
835 case _C_BYREF: res |= _F_BYREF; break;
836 case _C_ONEWAY: res |= _F_ONEWAY; break;
837 case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break;
838 default: flag = NO;
841 return res;
845 /* The following three functions can be used to determine how a
846 structure is laid out by the compiler. For example:
848 struct objc_struct_layout layout;
849 int i;
851 objc_layout_structure (type, &layout);
852 while (objc_layout_structure_next_member (&layout))
854 int position, align;
855 const char *type;
857 objc_layout_structure_get_info (&layout, &position, &align, &type);
858 printf ("element %d has offset %d, alignment %d\n",
859 i++, position, align);
862 These functions are used by objc_sizeof_type and objc_alignof_type
863 functions to compute the size and alignment of structures. The
864 previous method of computing the size and alignment of a structure
865 was not working on some architectures, particulary on AIX, and in
866 the presence of bitfields inside the structure. */
867 void
868 objc_layout_structure (const char *type,
869 struct objc_struct_layout *layout)
871 const char *ntype;
873 if (*type != _C_UNION_B && *type != _C_STRUCT_B)
875 objc_error (nil, OBJC_ERR_BAD_TYPE,
876 "record (or union) type expected in objc_layout_structure, got %s\n",
877 type);
880 type ++;
881 layout->original_type = type;
883 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
884 ntype = type;
885 while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B
886 && *ntype++ != '=')
887 /* do nothing */;
889 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
890 if (*(ntype - 1) == '=')
891 type = ntype;
893 layout->type = type;
894 layout->prev_type = NULL;
895 layout->record_size = 0;
896 layout->record_align = BITS_PER_UNIT;
898 layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY);
902 BOOL
903 objc_layout_structure_next_member (struct objc_struct_layout *layout)
905 register int desired_align = 0;
907 /* The following are used only if the field is a bitfield */
908 register const char *bfld_type = 0;
909 register int bfld_type_size, bfld_type_align = 0, bfld_field_size = 0;
911 /* The current type without the type qualifiers */
912 const char *type;
913 BOOL unionp = layout->original_type[-1] == _C_UNION_B;
915 /* Add the size of the previous field to the size of the record. */
916 if (layout->prev_type)
918 type = objc_skip_type_qualifiers (layout->prev_type);
919 if (unionp)
920 layout->record_size = MAX (layout->record_size,
921 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 */;
932 bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT;
933 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
934 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
935 layout->record_size += bfld_field_size;
939 if ((unionp && *layout->type == _C_UNION_E)
940 || (!unionp && *layout->type == _C_STRUCT_E))
941 return NO;
943 /* Skip the variable name if any */
944 if (*layout->type == '"')
946 for (layout->type++; *layout->type++ != '"';)
947 /* do nothing */;
950 type = objc_skip_type_qualifiers (layout->type);
952 if (*type != _C_BFLD)
953 desired_align = objc_alignof_type (type) * BITS_PER_UNIT;
954 else
956 desired_align = 1;
957 /* Skip the bitfield's offset */
958 for (bfld_type = type + 1;
959 isdigit ((unsigned char) *bfld_type);
960 bfld_type++)
961 /* do nothing */;
963 bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT;
964 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
965 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
968 #ifdef BIGGEST_FIELD_ALIGNMENT
969 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
970 #endif
971 #ifdef ADJUST_FIELD_ALIGN
972 desired_align = ADJUST_FIELD_ALIGN (type, desired_align);
973 #endif
975 /* Record must have at least as much alignment as any field.
976 Otherwise, the alignment of the field within the record
977 is meaningless. */
978 #ifndef PCC_BITFIELD_TYPE_MATTERS
979 layout->record_align = MAX (layout->record_align, desired_align);
980 #else /* PCC_BITFIELD_TYPE_MATTERS */
981 if (*type == _C_BFLD)
983 /* For these machines, a zero-length field does not
984 affect the alignment of the structure as a whole.
985 It does, however, affect the alignment of the next field
986 within the structure. */
987 if (bfld_field_size)
988 layout->record_align = MAX (layout->record_align, desired_align);
989 else
990 desired_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
992 /* A named bit field of declared type `int'
993 forces the entire structure to have `int' alignment.
994 Q1: How is encoded this thing and how to check for it?
995 Q2: How to determine maximum_field_alignment at runtime? */
997 /* if (DECL_NAME (field) != 0) */
999 int type_align = bfld_type_align;
1000 #if 0
1001 if (maximum_field_alignment != 0)
1002 type_align = MIN (type_align, maximum_field_alignment);
1003 else if (DECL_PACKED (field))
1004 type_align = MIN (type_align, BITS_PER_UNIT);
1005 #endif
1007 layout->record_align = MAX (layout->record_align, type_align);
1010 else
1011 layout->record_align = MAX (layout->record_align, desired_align);
1012 #endif /* PCC_BITFIELD_TYPE_MATTERS */
1014 /* Does this field automatically have alignment it needs
1015 by virtue of the fields that precede it and the record's
1016 own alignment? */
1018 if (*type == _C_BFLD)
1019 layout->record_size = atoi (type + 1);
1020 else if (layout->record_size % desired_align != 0)
1022 /* No, we need to skip space before this field.
1023 Bump the cumulative size to multiple of field alignment. */
1024 layout->record_size = ROUND (layout->record_size, desired_align);
1027 /* Jump to the next field in record. */
1029 layout->prev_type = layout->type;
1030 layout->type = objc_skip_typespec (layout->type); /* skip component */
1032 return YES;
1036 void objc_layout_finish_structure (struct objc_struct_layout *layout,
1037 unsigned int *size,
1038 unsigned int *align)
1040 BOOL unionp = layout->original_type[-1] == _C_UNION_B;
1041 if (layout->type
1042 && ((!unionp && *layout->type == _C_STRUCT_E)
1043 || (unionp && *layout->type == _C_UNION_E)))
1045 /* Work out the alignment of the record as one expression and store
1046 in the record type. Round it up to a multiple of the record's
1047 alignment. */
1048 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
1049 layout->record_align = ROUND_TYPE_ALIGN (layout->original_type-1,
1051 layout->record_align);
1052 #else
1053 layout->record_align = MAX (1, layout->record_align);
1054 #endif
1056 #ifdef ROUND_TYPE_SIZE
1057 layout->record_size = ROUND_TYPE_SIZE (layout->original_type,
1058 layout->record_size,
1059 layout->record_align);
1060 #else
1061 /* Round the size up to be a multiple of the required alignment */
1062 layout->record_size = ROUND (layout->record_size, layout->record_align);
1063 #endif
1065 layout->type = NULL;
1067 if (size)
1068 *size = layout->record_size / BITS_PER_UNIT;
1069 if (align)
1070 *align = layout->record_align / BITS_PER_UNIT;
1074 void objc_layout_structure_get_info (struct objc_struct_layout *layout,
1075 unsigned int *offset,
1076 unsigned int *align,
1077 const char **type)
1079 if (offset)
1080 *offset = layout->record_size / BITS_PER_UNIT;
1081 if (align)
1082 *align = layout->record_align / BITS_PER_UNIT;
1083 if (type)
1084 *type = layout->prev_type;