OSDN Git Service

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