OSDN Git Service

2005-12-11 Andrew Pinski <pinskia@physics.uc.edu>
[pf3gnuchains/gcc-fork.git] / libobjc / encoding.c
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
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 2, 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 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.  */
23
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.  */
29
30 /* FIXME: This file has no business including tm.h.  */
31
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>
38
39 #undef  MAX
40 #define MAX(X, Y)                    \
41   ({ typeof (X) __x = (X), __y = (Y); \
42      (__x > __y ? __x : __y); })
43
44 #undef  MIN
45 #define MIN(X, Y)                    \
46   ({ typeof (X) __x = (X), __y = (Y); \
47      (__x < __y ? __x : __y); })
48
49 #undef  ROUND
50 #define ROUND(V, A) \
51   ({ typeof (V) __v = (V); typeof (A) __a = (A); \
52      __a * ((__v+__a - 1)/__a); })
53
54
55 /* Various hacks for objc_layout_record. These are used by the target
56    macros. */
57
58 #define TREE_CODE(TYPE) *(TYPE)
59 #define TREE_TYPE(TREE) (TREE)
60
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
65
66 #define REAL_TYPE       _C_DBL
67
68 #define VECTOR_TYPE     _C_VECTOR
69
70 #define TYPE_FIELDS(TYPE)     objc_skip_typespec (TYPE)
71
72 #define DECL_MODE(TYPE) *(TYPE)
73 #define TYPE_MODE(TYPE) *(TYPE)
74
75 #define DFmode          _C_DBL
76
77 #define get_inner_array_type(TYPE)      ((TYPE) + 1)
78
79 /* Some ports (eg ARM) allow the structure size boundary to be
80    selected at compile-time.  We override the normal definition with
81    one that has a constant value for this compilation.  */
82 #ifndef BITS_PER_UNIT
83 #define BITS_PER_UNIT 8
84 #endif
85 #undef  STRUCTURE_SIZE_BOUNDARY
86 #define STRUCTURE_SIZE_BOUNDARY (BITS_PER_UNIT * sizeof (struct{char a;}))
87
88 /* Some ROUND_TYPE_ALIGN macros use TARGET_foo, and consequently
89    target_flags.  Define a dummy entry here to so we don't die.
90    We have to rename it because target_flags may already have been
91    declared extern.  */
92 #define target_flags not_target_flags
93 static int __attribute__ ((__unused__)) not_target_flags = 0;
94
95 /* Some ROUND_TYPE_ALIGN use ALTIVEC_VECTOR_MODE (rs6000 darwin).
96    Define a dummy ALTIVEC_VECTOR_MODE so it will not die.  */
97 #undef ALTIVEC_VECTOR_MODE
98 #define ALTIVEC_VECTOR_MODE(MODE) (0)
99
100
101 /*  FIXME: while this file has no business including tm.h, this
102     definitely has no business defining this macro but it
103     is only way around without really rewritting this file,
104     should look after the branch of 3.4 to fix this.  */
105 #define rs6000_special_round_type_align(STRUCT, COMPUTED, SPECIFIED)    \
106   ((TYPE_FIELDS (STRUCT) != 0                                           \
107     && DECL_MODE (TYPE_FIELDS (STRUCT)) == DFmode)                      \
108    ? MAX (MAX (COMPUTED, SPECIFIED), 64)                                \
109    : MAX (COMPUTED, SPECIFIED))
110
111 /*
112   return the size of an object specified by type
113 */
114
115 int
116 objc_sizeof_type (const char *type)
117 {
118   /* Skip the variable name if any */
119   if (*type == '"')
120     {
121       for (type++; *type++ != '"';)
122         /* do nothing */;
123     }
124
125   switch (*type) {
126   case _C_BOOL:
127     return sizeof (_Bool);
128     break;
129
130   case _C_ID:
131     return sizeof (id);
132     break;
133
134   case _C_CLASS:
135     return sizeof (Class);
136     break;
137
138   case _C_SEL:
139     return sizeof (SEL);
140     break;
141
142   case _C_CHR:
143     return sizeof (char);
144     break;
145
146   case _C_UCHR:
147     return sizeof (unsigned char);
148     break;
149
150   case _C_SHT:
151     return sizeof (short);
152     break;
153
154   case _C_USHT:
155     return sizeof (unsigned short);
156     break;
157
158   case _C_INT:
159     return sizeof (int);
160     break;
161
162   case _C_UINT:
163     return sizeof (unsigned int);
164     break;
165
166   case _C_LNG:
167     return sizeof (long);
168     break;
169
170   case _C_ULNG:
171     return sizeof (unsigned long);
172     break;
173
174   case _C_LNG_LNG:
175     return sizeof (long long);
176     break;
177
178   case _C_ULNG_LNG:
179     return sizeof (unsigned long long);
180     break;
181
182   case _C_FLT:
183     return sizeof (float);
184     break;
185
186   case _C_DBL:
187     return sizeof (double);
188     break;
189
190   case _C_VOID:
191     return sizeof (void);
192     break;
193
194   case _C_PTR:
195   case _C_ATOM:
196   case _C_CHARPTR:
197     return sizeof (char *);
198     break;
199
200   case _C_ARY_B:
201     {
202       int len = atoi (type + 1);
203       while (isdigit ((unsigned char)*++type))
204         ;
205       return len * objc_aligned_size (type);
206     }
207     break;
208
209   case _C_BFLD:
210     {
211       /* The new encoding of bitfields is: b 'position' 'type' 'size' */
212       int position, size;
213       int startByte, endByte;
214
215       position = atoi (type + 1);
216       while (isdigit ((unsigned char)*++type))
217         ;
218       size = atoi (type + 1);
219
220       startByte = position / BITS_PER_UNIT;
221       endByte = (position + size) / BITS_PER_UNIT;
222       return endByte - startByte;
223     }
224
225   case _C_STRUCT_B:
226     {
227       struct objc_struct_layout layout;
228       unsigned int size;
229
230       objc_layout_structure (type, &layout);
231       while (objc_layout_structure_next_member (&layout))
232         /* do nothing */ ;
233       objc_layout_finish_structure (&layout, &size, NULL);
234
235       return size;
236     }
237
238   case _C_UNION_B:
239     {
240       int max_size = 0;
241       while (*type != _C_UNION_E && *type++ != '=')
242         /* do nothing */;
243       while (*type != _C_UNION_E)
244         {
245           /* Skip the variable name if any */
246           if (*type == '"')
247             {
248               for (type++; *type++ != '"';)
249                 /* do nothing */;
250             }
251           max_size = MAX (max_size, objc_sizeof_type (type));
252           type = objc_skip_typespec (type);
253         }
254       return max_size;
255     }
256
257   default:
258     {
259       objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
260       return 0;
261     }
262   }
263 }
264
265
266 /*
267   Return the alignment of an object specified by type
268 */
269
270 int
271 objc_alignof_type (const char *type)
272 {
273   /* Skip the variable name if any */
274   if (*type == '"')
275     {
276       for (type++; *type++ != '"';)
277         /* do nothing */;
278     }
279   switch (*type) {
280   case _C_BOOL:
281     return __alignof__ (_Bool);
282     break;
283
284   case _C_ID:
285     return __alignof__ (id);
286     break;
287
288   case _C_CLASS:
289     return __alignof__ (Class);
290     break;
291
292   case _C_SEL:
293     return __alignof__ (SEL);
294     break;
295
296   case _C_CHR:
297     return __alignof__ (char);
298     break;
299
300   case _C_UCHR:
301     return __alignof__ (unsigned char);
302     break;
303
304   case _C_SHT:
305     return __alignof__ (short);
306     break;
307
308   case _C_USHT:
309     return __alignof__ (unsigned short);
310     break;
311
312   case _C_INT:
313     return __alignof__ (int);
314     break;
315
316   case _C_UINT:
317     return __alignof__ (unsigned int);
318     break;
319
320   case _C_LNG:
321     return __alignof__ (long);
322     break;
323
324   case _C_ULNG:
325     return __alignof__ (unsigned long);
326     break;
327
328   case _C_LNG_LNG:
329     return __alignof__ (long long);
330     break;
331
332   case _C_ULNG_LNG:
333     return __alignof__ (unsigned long long);
334     break;
335
336   case _C_FLT:
337     return __alignof__ (float);
338     break;
339
340   case _C_DBL:
341     return __alignof__ (double);
342     break;
343
344   case _C_PTR:
345   case _C_ATOM:
346   case _C_CHARPTR:
347     return __alignof__ (char *);
348     break;
349
350   case _C_ARY_B:
351     while (isdigit ((unsigned char)*++type))
352       /* do nothing */;
353     return objc_alignof_type (type);
354
355   case _C_STRUCT_B:
356     {
357       struct objc_struct_layout layout;
358       unsigned int align;
359
360       objc_layout_structure (type, &layout);
361       while (objc_layout_structure_next_member (&layout))
362         /* do nothing */;
363       objc_layout_finish_structure (&layout, NULL, &align);
364
365       return align;
366     }
367
368   case _C_UNION_B:
369     {
370       int maxalign = 0;
371       while (*type != _C_UNION_E && *type++ != '=')
372         /* do nothing */;
373       while (*type != _C_UNION_E)
374         {
375           /* Skip the variable name if any */
376           if (*type == '"')
377             {
378               for (type++; *type++ != '"';)
379                 /* do nothing */;
380             }
381           maxalign = MAX (maxalign, objc_alignof_type (type));
382           type = objc_skip_typespec (type);
383         }
384       return maxalign;
385     }
386
387   default:
388     {
389       objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
390       return 0;
391     }
392   }
393 }
394
395 /*
396   The aligned size if the size rounded up to the nearest alignment.
397 */
398
399 int
400 objc_aligned_size (const char *type)
401 {
402   int size, align;
403
404   /* Skip the variable name */
405   if (*type == '"')
406     {
407       for (type++; *type++ != '"';)
408         /* do nothing */;
409     }
410
411   size = objc_sizeof_type (type);
412   align = objc_alignof_type (type);
413
414   return ROUND (size, align);
415 }
416
417 /*
418   The size rounded up to the nearest integral of the wordsize, taken
419   to be the size of a void *.
420 */
421
422 int
423 objc_promoted_size (const char *type)
424 {
425   int size, wordsize;
426
427   /* Skip the variable name */
428   if (*type == '"')
429     {
430       for (type++; *type++ != '"';)
431         /* do nothing */;
432     }
433
434   size = objc_sizeof_type (type);
435   wordsize = sizeof (void *);
436
437   return ROUND (size, wordsize);
438 }
439
440 /*
441   Skip type qualifiers.  These may eventually precede typespecs
442   occurring in method prototype encodings.
443 */
444
445 inline const char *
446 objc_skip_type_qualifiers (const char *type)
447 {
448   while (*type == _C_CONST
449          || *type == _C_IN
450          || *type == _C_INOUT
451          || *type == _C_OUT
452          || *type == _C_BYCOPY
453          || *type == _C_BYREF
454          || *type == _C_ONEWAY
455          || *type == _C_GCINVISIBLE)
456     {
457       type += 1;
458     }
459   return type;
460 }
461
462
463 /*
464   Skip one typespec element.  If the typespec is prepended by type
465   qualifiers, these are skipped as well.
466 */
467
468 const char *
469 objc_skip_typespec (const char *type)
470 {
471   /* Skip the variable name if any */
472   if (*type == '"')
473     {
474       for (type++; *type++ != '"';)
475         /* do nothing */;
476     }
477
478   type = objc_skip_type_qualifiers (type);
479
480   switch (*type) {
481
482   case _C_ID:
483     /* An id may be annotated by the actual type if it is known
484        with the @"ClassName" syntax */
485
486     if (*++type != '"')
487       return type;
488     else
489       {
490         while (*++type != '"')
491           /* do nothing */;
492         return type + 1;
493       }
494
495     /* The following are one character type codes */
496   case _C_CLASS:
497   case _C_SEL:
498   case _C_CHR:
499   case _C_UCHR:
500   case _C_CHARPTR:
501   case _C_ATOM:
502   case _C_SHT:
503   case _C_USHT:
504   case _C_INT:
505   case _C_UINT:
506   case _C_LNG:
507   case _C_BOOL:
508   case _C_ULNG:
509   case _C_LNG_LNG:
510   case _C_ULNG_LNG:
511   case _C_FLT:
512   case _C_DBL:
513   case _C_VOID:
514   case _C_UNDEF:
515     return ++type;
516     break;
517
518   case _C_ARY_B:
519     /* skip digits, typespec and closing ']' */
520
521     while (isdigit ((unsigned char)*++type))
522       ;
523     type = objc_skip_typespec (type);
524     if (*type == _C_ARY_E)
525       return ++type;
526     else
527       {
528         objc_error (nil, OBJC_ERR_BAD_TYPE, "bad array type %s\n", type);
529         return 0;
530       }
531
532   case _C_BFLD:
533     /* The new encoding of bitfields is: b 'position' 'type' 'size' */
534     while (isdigit ((unsigned char)*++type))
535       ; /* skip position */
536     while (isdigit ((unsigned char)*++type))
537       ; /* skip type and size */
538     return type;
539
540   case _C_STRUCT_B:
541     /* skip name, and elements until closing '}'  */
542
543     while (*type != _C_STRUCT_E && *type++ != '=')
544       ;
545     while (*type != _C_STRUCT_E)
546       {
547         type = objc_skip_typespec (type);
548       }
549     return ++type;
550
551   case _C_UNION_B:
552     /* skip name, and elements until closing ')'  */
553
554     while (*type != _C_UNION_E && *type++ != '=')
555       ;
556     while (*type != _C_UNION_E)
557       {
558         type = objc_skip_typespec (type);
559       }
560     return ++type;
561
562   case _C_PTR:
563     /* Just skip the following typespec */
564
565     return objc_skip_typespec (++type);
566
567   default:
568     {
569       objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
570       return 0;
571     }
572   }
573 }
574
575 /*
576   Skip an offset as part of a method encoding.  This is prepended by a
577   '+' if the argument is passed in registers.
578 */
579 inline const char *
580 objc_skip_offset (const char *type)
581 {
582   if (*type == '+')
583     type++;
584   while (isdigit ((unsigned char) *++type))
585     ;
586   return type;
587 }
588
589 /*
590   Skip an argument specification of a method encoding.
591 */
592 const char *
593 objc_skip_argspec (const char *type)
594 {
595   type = objc_skip_typespec (type);
596   type = objc_skip_offset (type);
597   return type;
598 }
599
600 /*
601   Return the number of arguments that the method MTH expects.
602   Note that all methods need two implicit arguments `self' and
603   `_cmd'.
604 */
605 int
606 method_get_number_of_arguments (struct objc_method *mth)
607 {
608   int i = 0;
609   const char *type = mth->method_types;
610   while (*type)
611     {
612       type = objc_skip_argspec (type);
613       i += 1;
614     }
615   return i - 1;
616 }
617
618 /*
619   Return the size of the argument block needed on the stack to invoke
620   the method MTH.  This may be zero, if all arguments are passed in
621   registers.
622 */
623
624 int
625 method_get_sizeof_arguments (struct objc_method *mth)
626 {
627   const char *type = objc_skip_typespec (mth->method_types);
628   return atoi (type);
629 }
630
631 /*
632   Return a pointer to the next argument of ARGFRAME.  type points to
633   the last argument.  Typical use of this look like:
634
635   {
636     char *datum, *type;
637     for (datum = method_get_first_argument (method, argframe, &type);
638          datum; datum = method_get_next_argument (argframe, &type))
639       {
640         unsigned flags = objc_get_type_qualifiers (type);
641         type = objc_skip_type_qualifiers (type);
642         if (*type != _C_PTR)
643           [portal encodeData: datum ofType: type];
644         else
645           {
646             if ((flags & _F_IN) == _F_IN)
647               [portal encodeData: *(char **) datum ofType: ++type];
648           }
649       }
650   }
651 */
652
653 char *
654 method_get_next_argument (arglist_t argframe, const char **type)
655 {
656   const char *t = objc_skip_argspec (*type);
657
658   if (*t == '\0')
659     return 0;
660
661   *type = t;
662   t = objc_skip_typespec (t);
663
664   if (*t == '+')
665     return argframe->arg_regs + atoi (++t);
666   else
667     return argframe->arg_ptr + atoi (t);
668 }
669
670 /*
671   Return a pointer to the value of the first argument of the method
672   described in M with the given argumentframe ARGFRAME.  The type
673   is returned in TYPE.  type must be passed to successive calls of
674   method_get_next_argument.
675 */
676 char *
677 method_get_first_argument (struct objc_method *m,
678                            arglist_t argframe,
679                            const char **type)
680 {
681   *type = m->method_types;
682   return method_get_next_argument (argframe, type);
683 }
684
685 /*
686    Return a pointer to the ARGth argument of the method
687    M from the frame ARGFRAME.  The type of the argument
688    is returned in the value-result argument TYPE
689 */
690
691 char *
692 method_get_nth_argument (struct objc_method *m,
693                          arglist_t argframe, int arg,
694                          const char **type)
695 {
696   const char *t = objc_skip_argspec (m->method_types);
697
698   if (arg > method_get_number_of_arguments (m))
699     return 0;
700
701   while (arg--)
702     t = objc_skip_argspec (t);
703
704   *type = t;
705   t = objc_skip_typespec (t);
706
707   if (*t == '+')
708     return argframe->arg_regs + atoi (++t);
709   else
710     return argframe->arg_ptr + atoi (t);
711 }
712
713 unsigned
714 objc_get_type_qualifiers (const char *type)
715 {
716   unsigned res = 0;
717   BOOL flag = YES;
718
719   while (flag)
720     switch (*type++)
721       {
722       case _C_CONST:    res |= _F_CONST; break;
723       case _C_IN:       res |= _F_IN; break;
724       case _C_INOUT:    res |= _F_INOUT; break;
725       case _C_OUT:      res |= _F_OUT; break;
726       case _C_BYCOPY:   res |= _F_BYCOPY; break;
727       case _C_BYREF:  res |= _F_BYREF; break;
728       case _C_ONEWAY:   res |= _F_ONEWAY; break;
729       case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break;
730       default: flag = NO;
731     }
732
733   return res;
734 }
735
736
737 /* The following three functions can be used to determine how a
738    structure is laid out by the compiler. For example:
739
740   struct objc_struct_layout layout;
741   int i;
742
743   objc_layout_structure (type, &layout);
744   while (objc_layout_structure_next_member (&layout))
745     {
746       int position, align;
747       const char *type;
748
749       objc_layout_structure_get_info (&layout, &position, &align, &type);
750       printf ("element %d has offset %d, alignment %d\n",
751               i++, position, align);
752     }
753
754   These functions are used by objc_sizeof_type and objc_alignof_type
755   functions to compute the size and alignment of structures. The
756   previous method of computing the size and alignment of a structure
757   was not working on some architectures, particulary on AIX, and in
758   the presence of bitfields inside the structure. */
759 void
760 objc_layout_structure (const char *type,
761                            struct objc_struct_layout *layout)
762 {
763   const char *ntype;
764
765   if (*type++ != _C_STRUCT_B)
766     {
767       objc_error (nil, OBJC_ERR_BAD_TYPE,
768                  "record type expected in objc_layout_structure, got %s\n",
769                  type);
770     }
771
772   layout->original_type = type;
773
774   /* Skip "<name>=" if any. Avoid embedded structures and unions. */
775   ntype = type;
776   while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B
777          && *ntype++ != '=')
778     /* do nothing */;
779
780   /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
781   if (*(ntype - 1) == '=')
782     type = ntype;
783
784   layout->type = type;
785   layout->prev_type = NULL;
786   layout->record_size = 0;
787   layout->record_align = BITS_PER_UNIT;
788
789   layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY);
790 }
791
792
793 BOOL
794 objc_layout_structure_next_member (struct objc_struct_layout *layout)
795 {
796   register int desired_align = 0;
797
798   /* The following are used only if the field is a bitfield */
799   register const char *bfld_type = 0;
800   register int bfld_type_size, bfld_type_align = 0, bfld_field_size = 0;
801
802   /* The current type without the type qualifiers */
803   const char *type;
804
805   /* Add the size of the previous field to the size of the record.  */
806   if (layout->prev_type)
807     {
808       type = objc_skip_type_qualifiers (layout->prev_type);
809
810       if (*type != _C_BFLD)
811         layout->record_size += objc_sizeof_type (type) * BITS_PER_UNIT;
812       else {
813         /* Get the bitfield's type */
814         for (bfld_type = type + 1;
815              isdigit ((unsigned char)*bfld_type);
816              bfld_type++)
817           /* do nothing */;
818
819         bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT;
820         bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
821         bfld_field_size = atoi (objc_skip_typespec (bfld_type));
822         layout->record_size += bfld_field_size;
823       }
824     }
825
826   if (*layout->type == _C_STRUCT_E)
827     return NO;
828
829   /* Skip the variable name if any */
830   if (*layout->type == '"')
831     {
832       for (layout->type++; *layout->type++ != '"';)
833         /* do nothing */;
834     }
835
836   type = objc_skip_type_qualifiers (layout->type);
837
838   if (*type != _C_BFLD)
839     desired_align = objc_alignof_type (type) * BITS_PER_UNIT;
840   else
841     {
842       desired_align = 1;
843       /* Skip the bitfield's offset */
844       for (bfld_type = type + 1;
845            isdigit ((unsigned char) *bfld_type);
846            bfld_type++)
847         /* do nothing */;
848
849       bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT;
850       bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
851       bfld_field_size = atoi (objc_skip_typespec (bfld_type));
852     }
853
854 #ifdef BIGGEST_FIELD_ALIGNMENT
855   desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
856 #endif
857 #ifdef ADJUST_FIELD_ALIGN
858   desired_align = ADJUST_FIELD_ALIGN (type, desired_align);
859 #endif
860
861   /* Record must have at least as much alignment as any field.
862      Otherwise, the alignment of the field within the record
863      is meaningless.  */
864 #ifndef PCC_BITFIELD_TYPE_MATTERS
865   layout->record_align = MAX (layout->record_align, desired_align);
866 #else   /* PCC_BITFIELD_TYPE_MATTERS */
867   if (*type == _C_BFLD)
868     {
869       /* For these machines, a zero-length field does not
870          affect the alignment of the structure as a whole.
871          It does, however, affect the alignment of the next field
872          within the structure.  */
873       if (bfld_field_size)
874         layout->record_align = MAX (layout->record_align, desired_align);
875       else
876         desired_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
877
878       /* A named bit field of declared type `int'
879          forces the entire structure to have `int' alignment.
880          Q1: How is encoded this thing and how to check for it?
881          Q2: How to determine maximum_field_alignment at runtime? */
882
883 /*        if (DECL_NAME (field) != 0) */
884       {
885         int type_align = bfld_type_align;
886 #if 0
887         if (maximum_field_alignment != 0)
888           type_align = MIN (type_align, maximum_field_alignment);
889         else if (DECL_PACKED (field))
890           type_align = MIN (type_align, BITS_PER_UNIT);
891 #endif
892
893         layout->record_align = MAX (layout->record_align, type_align);
894       }
895     }
896   else
897     layout->record_align = MAX (layout->record_align, desired_align);
898 #endif  /* PCC_BITFIELD_TYPE_MATTERS */
899
900   /* Does this field automatically have alignment it needs
901      by virtue of the fields that precede it and the record's
902      own alignment?  */
903
904   if (*type == _C_BFLD)
905     layout->record_size = atoi (type + 1);
906   else if (layout->record_size % desired_align != 0)
907     {
908       /* No, we need to skip space before this field.
909          Bump the cumulative size to multiple of field alignment.  */
910       layout->record_size = ROUND (layout->record_size, desired_align);
911     }
912
913   /* Jump to the next field in record. */
914
915   layout->prev_type = layout->type;
916   layout->type = objc_skip_typespec (layout->type);      /* skip component */
917
918   return YES;
919 }
920
921
922 void objc_layout_finish_structure (struct objc_struct_layout *layout,
923                                    unsigned int *size,
924                                    unsigned int *align)
925 {
926   if (layout->type && *layout->type == _C_STRUCT_E)
927     {
928       /* Work out the alignment of the record as one expression and store
929          in the record type.  Round it up to a multiple of the record's
930          alignment. */
931
932 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
933       layout->record_align = ROUND_TYPE_ALIGN (layout->original_type,
934                                                1,
935                                                layout->record_align);
936 #else
937       layout->record_align = MAX (1, layout->record_align);
938 #endif
939
940 #ifdef ROUND_TYPE_SIZE
941       layout->record_size = ROUND_TYPE_SIZE (layout->original_type,
942                                              layout->record_size,
943                                              layout->record_align);
944 #else
945       /* Round the size up to be a multiple of the required alignment */
946       layout->record_size = ROUND (layout->record_size, layout->record_align);
947 #endif
948
949       layout->type = NULL;
950     }
951   if (size)
952     *size = layout->record_size / BITS_PER_UNIT;
953   if (align)
954     *align = layout->record_align / BITS_PER_UNIT;
955 }
956
957
958 void objc_layout_structure_get_info (struct objc_struct_layout *layout,
959                                      unsigned int *offset,
960                                      unsigned int *align,
961                                      const char **type)
962 {
963   if (offset)
964     *offset = layout->record_size / BITS_PER_UNIT;
965   if (align)
966     *align = layout->record_align / BITS_PER_UNIT;
967   if (type)
968     *type = layout->prev_type;
969 }