OSDN Git Service

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