OSDN Git Service

Daily bump.
[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)           ({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;})
75
76 #define DECL_MODE(TYPE) *(TYPE)
77 #define TYPE_MODE(TYPE) *(TYPE)
78
79 #define DFmode          _C_DBL
80
81 #define strip_array_types(TYPE)      ({const char *_field = (TYPE); \
82   while (*_field == _C_ARY_B)\
83     {\
84       while (isdigit ((unsigned char)*++_field))\
85         ;\
86     }\
87     _field;})
88
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;}))
97
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;
104
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)
109
110
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)
125
126 /*
127   return the size of an object specified by type
128 */
129
130 int
131 objc_sizeof_type (const char *type)
132 {
133   /* Skip the variable name if any */
134   if (*type == '"')
135     {
136       for (type++; *type++ != '"';)
137         /* do nothing */;
138     }
139
140   switch (*type) {
141   case _C_BOOL:
142     return sizeof (_Bool);
143     break;
144
145   case _C_ID:
146     return sizeof (id);
147     break;
148
149   case _C_CLASS:
150     return sizeof (Class);
151     break;
152
153   case _C_SEL:
154     return sizeof (SEL);
155     break;
156
157   case _C_CHR:
158     return sizeof (char);
159     break;
160
161   case _C_UCHR:
162     return sizeof (unsigned char);
163     break;
164
165   case _C_SHT:
166     return sizeof (short);
167     break;
168
169   case _C_USHT:
170     return sizeof (unsigned short);
171     break;
172
173   case _C_INT:
174     return sizeof (int);
175     break;
176
177   case _C_UINT:
178     return sizeof (unsigned int);
179     break;
180
181   case _C_LNG:
182     return sizeof (long);
183     break;
184
185   case _C_ULNG:
186     return sizeof (unsigned long);
187     break;
188
189   case _C_LNG_LNG:
190     return sizeof (long long);
191     break;
192
193   case _C_ULNG_LNG:
194     return sizeof (unsigned long long);
195     break;
196
197   case _C_FLT:
198     return sizeof (float);
199     break;
200
201   case _C_DBL:
202     return sizeof (double);
203     break;
204
205   case _C_VOID:
206     return sizeof (void);
207     break;
208
209   case _C_PTR:
210   case _C_ATOM:
211   case _C_CHARPTR:
212     return sizeof (char *);
213     break;
214
215   case _C_ARY_B:
216     {
217       int len = atoi (type + 1);
218       while (isdigit ((unsigned char)*++type))
219         ;
220       return len * objc_aligned_size (type);
221     }
222     break;
223
224   case _C_BFLD:
225     {
226       /* The new encoding of bitfields is: b 'position' 'type' 'size' */
227       int position, size;
228       int startByte, endByte;
229
230       position = atoi (type + 1);
231       while (isdigit ((unsigned char)*++type))
232         ;
233       size = atoi (type + 1);
234
235       startByte = position / BITS_PER_UNIT;
236       endByte = (position + size) / BITS_PER_UNIT;
237       return endByte - startByte;
238     }
239
240   case _C_UNION_B:
241   case _C_STRUCT_B:
242     {
243       struct objc_struct_layout layout;
244       unsigned int size;
245
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);
250
251       return size;
252     }
253     
254   case _C_COMPLEX:
255     {
256       type++; /* Skip after the 'j'. */
257       switch (*type)
258         {
259             case _C_CHR:
260               return sizeof (_Complex char);
261               break;
262
263             case _C_UCHR:
264               return sizeof (_Complex unsigned char);
265               break;
266
267             case _C_SHT:
268               return sizeof (_Complex short);
269               break;
270
271             case _C_USHT:
272               return sizeof (_Complex unsigned short);
273               break;
274
275             case _C_INT:
276               return sizeof (_Complex int);
277               break;
278
279             case _C_UINT:
280               return sizeof (_Complex unsigned int);
281               break;
282
283             case _C_LNG:
284               return sizeof (_Complex long);
285               break;
286
287             case _C_ULNG:
288               return sizeof (_Complex unsigned long);
289               break;
290
291             case _C_LNG_LNG:
292               return sizeof (_Complex long long);
293               break;
294
295             case _C_ULNG_LNG:
296               return sizeof (_Complex unsigned long long);
297               break;
298
299             case _C_FLT:
300               return sizeof (_Complex float);
301               break;
302
303             case _C_DBL:
304               return sizeof (_Complex double);
305               break;
306             
307             default:
308               {
309                 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown complex type %s\n",
310                             type);
311                 return 0;
312               }
313         }
314     }
315
316   default:
317     {
318       objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
319       return 0;
320     }
321   }
322 }
323
324
325 /*
326   Return the alignment of an object specified by type
327 */
328
329 int
330 objc_alignof_type (const char *type)
331 {
332   /* Skip the variable name if any */
333   if (*type == '"')
334     {
335       for (type++; *type++ != '"';)
336         /* do nothing */;
337     }
338   switch (*type) {
339   case _C_BOOL:
340     return __alignof__ (_Bool);
341     break;
342
343   case _C_ID:
344     return __alignof__ (id);
345     break;
346
347   case _C_CLASS:
348     return __alignof__ (Class);
349     break;
350
351   case _C_SEL:
352     return __alignof__ (SEL);
353     break;
354
355   case _C_CHR:
356     return __alignof__ (char);
357     break;
358
359   case _C_UCHR:
360     return __alignof__ (unsigned char);
361     break;
362
363   case _C_SHT:
364     return __alignof__ (short);
365     break;
366
367   case _C_USHT:
368     return __alignof__ (unsigned short);
369     break;
370
371   case _C_INT:
372     return __alignof__ (int);
373     break;
374
375   case _C_UINT:
376     return __alignof__ (unsigned int);
377     break;
378
379   case _C_LNG:
380     return __alignof__ (long);
381     break;
382
383   case _C_ULNG:
384     return __alignof__ (unsigned long);
385     break;
386
387   case _C_LNG_LNG:
388     return __alignof__ (long long);
389     break;
390
391   case _C_ULNG_LNG:
392     return __alignof__ (unsigned long long);
393     break;
394
395   case _C_FLT:
396     return __alignof__ (float);
397     break;
398
399   case _C_DBL:
400     return __alignof__ (double);
401     break;
402
403   case _C_PTR:
404   case _C_ATOM:
405   case _C_CHARPTR:
406     return __alignof__ (char *);
407     break;
408
409   case _C_ARY_B:
410     while (isdigit ((unsigned char)*++type))
411       /* do nothing */;
412     return objc_alignof_type (type);
413
414   case _C_STRUCT_B:
415   case _C_UNION_B:
416     {
417       struct objc_struct_layout layout;
418       unsigned int align;
419
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);
424
425       return align;
426     }
427     
428     
429   case _C_COMPLEX:
430     {
431       type++; /* Skip after the 'j'. */
432       switch (*type)
433         {
434             case _C_CHR:
435               return __alignof__ (_Complex char);
436               break;
437
438             case _C_UCHR:
439               return __alignof__ (_Complex unsigned char);
440               break;
441
442             case _C_SHT:
443               return __alignof__ (_Complex short);
444               break;
445
446             case _C_USHT:
447               return __alignof__ (_Complex unsigned short);
448               break;
449
450             case _C_INT:
451               return __alignof__ (_Complex int);
452               break;
453
454             case _C_UINT:
455               return __alignof__ (_Complex unsigned int);
456               break;
457
458             case _C_LNG:
459               return __alignof__ (_Complex long);
460               break;
461
462             case _C_ULNG:
463               return __alignof__ (_Complex unsigned long);
464               break;
465
466             case _C_LNG_LNG:
467               return __alignof__ (_Complex long long);
468               break;
469
470             case _C_ULNG_LNG:
471               return __alignof__ (_Complex unsigned long long);
472               break;
473
474             case _C_FLT:
475               return __alignof__ (_Complex float);
476               break;
477
478             case _C_DBL:
479               return __alignof__ (_Complex double);
480               break;
481             
482             default:
483               {
484                 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown complex type %s\n",
485                             type);
486                 return 0;
487               }
488         }
489     }
490
491   default:
492     {
493       objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
494       return 0;
495     }
496   }
497 }
498
499 /*
500   The aligned size if the size rounded up to the nearest alignment.
501 */
502
503 int
504 objc_aligned_size (const char *type)
505 {
506   int size, align;
507
508   /* Skip the variable name */
509   if (*type == '"')
510     {
511       for (type++; *type++ != '"';)
512         /* do nothing */;
513     }
514
515   size = objc_sizeof_type (type);
516   align = objc_alignof_type (type);
517
518   return ROUND (size, align);
519 }
520
521 /*
522   The size rounded up to the nearest integral of the wordsize, taken
523   to be the size of a void *.
524 */
525
526 int
527 objc_promoted_size (const char *type)
528 {
529   int size, wordsize;
530
531   /* Skip the variable name */
532   if (*type == '"')
533     {
534       for (type++; *type++ != '"';)
535         /* do nothing */;
536     }
537
538   size = objc_sizeof_type (type);
539   wordsize = sizeof (void *);
540
541   return ROUND (size, wordsize);
542 }
543
544 /*
545   Skip type qualifiers.  These may eventually precede typespecs
546   occurring in method prototype encodings.
547 */
548
549 inline const char *
550 objc_skip_type_qualifiers (const char *type)
551 {
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)
560     {
561       type += 1;
562     }
563   return type;
564 }
565
566
567 /*
568   Skip one typespec element.  If the typespec is prepended by type
569   qualifiers, these are skipped as well.
570 */
571
572 const char *
573 objc_skip_typespec (const char *type)
574 {
575   /* Skip the variable name if any */
576   if (*type == '"')
577     {
578       for (type++; *type++ != '"';)
579         /* do nothing */;
580     }
581
582   type = objc_skip_type_qualifiers (type);
583
584   switch (*type) {
585
586   case _C_ID:
587     /* An id may be annotated by the actual type if it is known
588        with the @"ClassName" syntax */
589
590     if (*++type != '"')
591       return type;
592     else
593       {
594         while (*++type != '"')
595           /* do nothing */;
596         return type + 1;
597       }
598
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;
621     
622   case _C_COMPLEX:
623     return type + 2;
624     break;
625
626   case _C_ARY_B:
627     /* skip digits, typespec and closing ']' */
628
629     while (isdigit ((unsigned char)*++type))
630       ;
631     type = objc_skip_typespec (type);
632     if (*type == _C_ARY_E)
633       return ++type;
634     else
635       {
636         objc_error (nil, OBJC_ERR_BAD_TYPE, "bad array type %s\n", type);
637         return 0;
638       }
639
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;
647
648   case _C_STRUCT_B:
649     /* skip name, and elements until closing '}'  */
650
651     while (*type != _C_STRUCT_E && *type++ != '=')
652       ;
653     while (*type != _C_STRUCT_E)
654       {
655         type = objc_skip_typespec (type);
656       }
657     return ++type;
658
659   case _C_UNION_B:
660     /* skip name, and elements until closing ')'  */
661
662     while (*type != _C_UNION_E && *type++ != '=')
663       ;
664     while (*type != _C_UNION_E)
665       {
666         type = objc_skip_typespec (type);
667       }
668     return ++type;
669
670   case _C_PTR:
671     /* Just skip the following typespec */
672
673     return objc_skip_typespec (++type);
674
675   default:
676     {
677       objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
678       return 0;
679     }
680   }
681 }
682
683 /*
684   Skip an offset as part of a method encoding.  This is prepended by a
685   '+' if the argument is passed in registers.
686 */
687 inline const char *
688 objc_skip_offset (const char *type)
689 {
690   if (*type == '+')
691     type++;
692   while (isdigit ((unsigned char) *++type))
693     ;
694   return type;
695 }
696
697 /*
698   Skip an argument specification of a method encoding.
699 */
700 const char *
701 objc_skip_argspec (const char *type)
702 {
703   type = objc_skip_typespec (type);
704   type = objc_skip_offset (type);
705   return type;
706 }
707
708 /*
709   Return the number of arguments that the method MTH expects.
710   Note that all methods need two implicit arguments `self' and
711   `_cmd'.
712 */
713 int
714 method_get_number_of_arguments (struct objc_method *mth)
715 {
716   int i = 0;
717   const char *type = mth->method_types;
718   while (*type)
719     {
720       type = objc_skip_argspec (type);
721       i += 1;
722     }
723   return i - 1;
724 }
725
726 /*
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.
730 */
731
732 int
733 method_get_sizeof_arguments (struct objc_method *mth)
734 {
735   const char *type = objc_skip_typespec (mth->method_types);
736   return atoi (type);
737 }
738
739 /*
740   Return a pointer to the next argument of ARGFRAME.  type points to
741   the last argument.  Typical use of this look like:
742
743   {
744     char *datum, *type;
745     for (datum = method_get_first_argument (method, argframe, &type);
746          datum; datum = method_get_next_argument (argframe, &type))
747       {
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
753           {
754             if ((flags & _F_IN) == _F_IN)
755               [portal encodeData: *(char **) datum ofType: ++type];
756           }
757       }
758   }
759 */
760
761 char *
762 method_get_next_argument (arglist_t argframe, const char **type)
763 {
764   const char *t = objc_skip_argspec (*type);
765
766   if (*t == '\0')
767     return 0;
768
769   *type = t;
770   t = objc_skip_typespec (t);
771
772   if (*t == '+')
773     return argframe->arg_regs + atoi (++t);
774   else
775     return argframe->arg_ptr + atoi (t);
776 }
777
778 /*
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.
783 */
784 char *
785 method_get_first_argument (struct objc_method *m,
786                            arglist_t argframe,
787                            const char **type)
788 {
789   *type = m->method_types;
790   return method_get_next_argument (argframe, type);
791 }
792
793 /*
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
797 */
798
799 char *
800 method_get_nth_argument (struct objc_method *m,
801                          arglist_t argframe, int arg,
802                          const char **type)
803 {
804   const char *t = objc_skip_argspec (m->method_types);
805
806   if (arg > method_get_number_of_arguments (m))
807     return 0;
808
809   while (arg--)
810     t = objc_skip_argspec (t);
811
812   *type = t;
813   t = objc_skip_typespec (t);
814
815   if (*t == '+')
816     return argframe->arg_regs + atoi (++t);
817   else
818     return argframe->arg_ptr + atoi (t);
819 }
820
821 unsigned
822 objc_get_type_qualifiers (const char *type)
823 {
824   unsigned res = 0;
825   BOOL flag = YES;
826
827   while (flag)
828     switch (*type++)
829       {
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;
839     }
840
841   return res;
842 }
843
844
845 /* The following three functions can be used to determine how a
846    structure is laid out by the compiler. For example:
847
848   struct objc_struct_layout layout;
849   int i;
850
851   objc_layout_structure (type, &layout);
852   while (objc_layout_structure_next_member (&layout))
853     {
854       int position, align;
855       const char *type;
856
857       objc_layout_structure_get_info (&layout, &position, &align, &type);
858       printf ("element %d has offset %d, alignment %d\n",
859               i++, position, align);
860     }
861
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)
870 {
871   const char *ntype;
872
873   if (*type != _C_UNION_B && *type != _C_STRUCT_B)
874     {
875       objc_error (nil, OBJC_ERR_BAD_TYPE,
876                  "record (or union) type expected in objc_layout_structure, got %s\n",
877                  type);
878     }
879
880   type ++;
881   layout->original_type = type;
882
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 */;
888
889   /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
890   if (*(ntype - 1) == '=')
891     type = ntype;
892
893   layout->type = type;
894   layout->prev_type = NULL;
895   layout->record_size = 0;
896   layout->record_align = BITS_PER_UNIT;
897
898   layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY);
899 }
900
901
902 BOOL
903 objc_layout_structure_next_member (struct objc_struct_layout *layout)
904 {
905   register int desired_align = 0;
906
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;
910
911   /* The current type without the type qualifiers */
912   const char *type;
913   BOOL unionp = layout->original_type[-1] == _C_UNION_B;
914
915   /* Add the size of the previous field to the size of the record.  */
916   if (layout->prev_type)
917     {
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);
922
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 */;
931
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;
936       }
937     }
938
939   if ((unionp && *layout->type == _C_UNION_E)
940       || (!unionp && *layout->type == _C_STRUCT_E))
941     return NO;
942
943   /* Skip the variable name if any */
944   if (*layout->type == '"')
945     {
946       for (layout->type++; *layout->type++ != '"';)
947         /* do nothing */;
948     }
949
950   type = objc_skip_type_qualifiers (layout->type);
951
952   if (*type != _C_BFLD)
953     desired_align = objc_alignof_type (type) * BITS_PER_UNIT;
954   else
955     {
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 */;
962
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));
966     }
967
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
974
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)
982     {
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;
991
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? */
996
997 /*        if (DECL_NAME (field) != 0) */
998       {
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
1006
1007         layout->record_align = MAX (layout->record_align, type_align);
1008       }
1009     }
1010   else
1011     layout->record_align = MAX (layout->record_align, desired_align);
1012 #endif  /* PCC_BITFIELD_TYPE_MATTERS */
1013
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?  */
1017
1018   if (*type == _C_BFLD)
1019     layout->record_size = atoi (type + 1);
1020   else if (layout->record_size % desired_align != 0)
1021     {
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);
1025     }
1026
1027   /* Jump to the next field in record. */
1028
1029   layout->prev_type = layout->type;
1030   layout->type = objc_skip_typespec (layout->type);      /* skip component */
1031
1032   return YES;
1033 }
1034
1035
1036 void objc_layout_finish_structure (struct objc_struct_layout *layout,
1037                                    unsigned int *size,
1038                                    unsigned int *align)
1039 {
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)))
1044     {
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,
1050                                                1,
1051                                                layout->record_align);
1052 #else
1053       layout->record_align = MAX (1, layout->record_align);
1054 #endif
1055
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
1064
1065       layout->type = NULL;
1066     }
1067   if (size)
1068     *size = layout->record_size / BITS_PER_UNIT;
1069   if (align)
1070     *align = layout->record_align / BITS_PER_UNIT;
1071 }
1072
1073
1074 void objc_layout_structure_get_info (struct objc_struct_layout *layout,
1075                                      unsigned int *offset,
1076                                      unsigned int *align,
1077                                      const char **type)
1078 {
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;
1085 }