OSDN Git Service

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