xref: /netbsd-src/external/gpl3/gcc.old/dist/libobjc/encoding.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /* Encoding of types for Objective C.
2    Copyright (C) 1993-2013 Free Software Foundation, Inc.
3    Contributed by Kresten Krab Thorup
4    Bitfield support by Ovidiu Predescu
5 
6 This file is part of GCC.
7 
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12 
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21 
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
25 <http://www.gnu.org/licenses/>.  */
26 
27 /* FIXME: This file has no business including tm.h.  */
28 
29 /* FIXME: This file contains functions that will abort the entire
30    program if they fail.  Is that really needed ?  */
31 
32 #include "objc-private/common.h"
33 #include "objc-private/error.h"
34 #include "tconfig.h"
35 #include "coretypes.h"
36 #include "tm.h"
37 #include "objc/runtime.h"
38 #include "objc-private/module-abi-8.h" /* For struct objc_method */
39 #include <stdlib.h>
40 #include <ctype.h>
41 #include <string.h>                    /* For memcpy.  */
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 /* Replace TARGET_VSX, TARGET_ALTIVEC, and TARGET_64BIT with constants based on
115    the current switches, rather than looking in the options structure.  */
116 #ifdef _ARCH_PPC
117 #undef TARGET_VSX
118 #undef TARGET_ALTIVEC
119 #undef TARGET_64BIT
120 
121 #ifdef __VSX__
122 #define TARGET_VSX 1
123 #else
124 #define TARGET_VSX 0
125 #endif
126 
127 #ifdef __ALTIVEC__
128 #define TARGET_ALTIVEC 1
129 #else
130 #define TARGET_ALTIVEC 0
131 #endif
132 
133 #ifdef _ARCH_PPC64
134 #define TARGET_64BIT 1
135 #else
136 #define TARGET_64BIT 0
137 #endif
138 #endif
139 
140 /* Furthermore, some (powerpc) targets also use TARGET_ALIGN_NATURAL
141  in their alignment macros. Currently[4.5/6], rs6000.h points this
142  to a static variable, initialized by target overrides. This is reset
143  in linux64.h but not in darwin64.h.  The macro is not used by *86*.  */
144 
145 #if __MACH__
146 # if __LP64__
147 #  undef TARGET_ALIGN_NATURAL
148 #  define TARGET_ALIGN_NATURAL 1
149 # endif
150 
151 /* On Darwin32, we need to recurse until we find the starting stuct type.  */
152 static int
153 _darwin_rs6000_special_round_type_align (const char *struc, int comp, int spec)
154 {
155   const char *_stp , *_fields = TYPE_FIELDS (struc);
156   if (!_fields)
157     return MAX (comp, spec);
158   _stp = strip_array_types (_fields);
159   if (TYPE_MODE(_stp) == _C_COMPLEX)
160    _stp++;
161   switch (TYPE_MODE(_stp))
162     {
163       case RECORD_TYPE:
164       case UNION_TYPE:
165 	return MAX (MAX (comp, spec), objc_alignof_type (_stp) * BITS_PER_UNIT);
166 	break;
167       case DFmode:
168       case _C_LNG_LNG:
169       case _C_ULNG_LNG:
170 	return MAX (MAX (comp, spec), 64);
171 	break;
172 
173       default:
174 	return MAX (comp, spec);
175 	break;
176     }
177 }
178 
179 /* See comment below.  */
180 #define darwin_rs6000_special_round_type_align(S,C,S2)			\
181   (_darwin_rs6000_special_round_type_align ((char*)(S), (int)(C), (int)(S2)))
182 #endif
183 
184 /*  FIXME: while this file has no business including tm.h, this
185     definitely has no business defining this macro but it
186     is only way around without really rewritting this file,
187     should look after the branch of 3.4 to fix this.   */
188 #define rs6000_special_round_type_align(STRUCT, COMPUTED, SPECIFIED)	\
189   ({ const char *_fields = TYPE_FIELDS (STRUCT);			\
190   ((_fields != 0							\
191     && TYPE_MODE (strip_array_types (TREE_TYPE (_fields))) == DFmode)	\
192    ? MAX (MAX (COMPUTED, SPECIFIED), 64)				\
193    : MAX (COMPUTED, SPECIFIED));})
194 
195 #define rs6000_special_adjust_field_align_p(FIELD, COMPUTED) \
196  (TARGET_ALTIVEC && TREE_CODE (TREE_TYPE (FIELD)) == VECTOR_TYPE)
197 
198 /* Skip a variable name, enclosed in quotes (").  */
199 static inline
200 const char *
201 objc_skip_variable_name (const char *type)
202 {
203   /* Skip the variable name if any.  */
204   if (*type == '"')
205     {
206       /* FIXME: How do we know we won't read beyond the end of the
207 	 string.  Here and in the rest of the file!  */
208       /* Skip '"'.  */
209       type++;
210       /* Skip to the next '"'.  */
211       while (*type != '"')
212 	type++;
213       /* Skip '"'.  */
214       type++;
215     }
216 
217   return type;
218 }
219 
220 int
221 objc_sizeof_type (const char *type)
222 {
223   type = objc_skip_variable_name (type);
224 
225   switch (*type) {
226   case _C_BOOL:
227     return sizeof (_Bool);
228     break;
229 
230   case _C_ID:
231     return sizeof (id);
232     break;
233 
234   case _C_CLASS:
235     return sizeof (Class);
236     break;
237 
238   case _C_SEL:
239     return sizeof (SEL);
240     break;
241 
242   case _C_CHR:
243     return sizeof (char);
244     break;
245 
246   case _C_UCHR:
247     return sizeof (unsigned char);
248     break;
249 
250   case _C_SHT:
251     return sizeof (short);
252     break;
253 
254   case _C_USHT:
255     return sizeof (unsigned short);
256     break;
257 
258   case _C_INT:
259     return sizeof (int);
260     break;
261 
262   case _C_UINT:
263     return sizeof (unsigned int);
264     break;
265 
266   case _C_LNG:
267     return sizeof (long);
268     break;
269 
270   case _C_ULNG:
271     return sizeof (unsigned long);
272     break;
273 
274   case _C_LNG_LNG:
275     return sizeof (long long);
276     break;
277 
278   case _C_ULNG_LNG:
279     return sizeof (unsigned long long);
280     break;
281 
282   case _C_FLT:
283     return sizeof (float);
284     break;
285 
286   case _C_DBL:
287     return sizeof (double);
288     break;
289 
290   case _C_LNG_DBL:
291     return sizeof (long double);
292     break;
293 
294   case _C_VOID:
295     return sizeof (void);
296     break;
297 
298   case _C_PTR:
299   case _C_ATOM:
300   case _C_CHARPTR:
301     return sizeof (char *);
302     break;
303 
304   case _C_ARY_B:
305     {
306       int len = atoi (type + 1);
307       while (isdigit ((unsigned char)*++type))
308 	;
309       return len * objc_aligned_size (type);
310     }
311     break;
312 
313   case _C_VECTOR:
314     {
315       /* Skip the '!'.  */
316       type++;
317       /* Skip the '['.  */
318       type++;
319 
320       /* The size in bytes is the following number.  */
321       int size = atoi (type);
322       return size;
323     }
324     break;
325 
326   case _C_BFLD:
327     {
328       /* The GNU encoding of bitfields is: b 'position' 'type'
329 	 'size'.  */
330       int position, size;
331       int startByte, endByte;
332 
333       position = atoi (type + 1);
334       while (isdigit ((unsigned char)*++type))
335 	;
336       size = atoi (type + 1);
337 
338       startByte = position / BITS_PER_UNIT;
339       endByte = (position + size) / BITS_PER_UNIT;
340       return endByte - startByte;
341     }
342 
343   case _C_UNION_B:
344   case _C_STRUCT_B:
345     {
346       struct objc_struct_layout layout;
347       unsigned int size;
348 
349       objc_layout_structure (type, &layout);
350       while (objc_layout_structure_next_member (&layout))
351         /* do nothing */ ;
352       objc_layout_finish_structure (&layout, &size, NULL);
353 
354       return size;
355     }
356 
357   case _C_COMPLEX:
358     {
359       type++; /* Skip after the 'j'. */
360       switch (*type)
361         {
362 	    case _C_CHR:
363 	      return sizeof (_Complex char);
364 	      break;
365 
366 	    case _C_UCHR:
367 	      return sizeof (_Complex unsigned char);
368 	      break;
369 
370 	    case _C_SHT:
371 	      return sizeof (_Complex short);
372 	      break;
373 
374 	    case _C_USHT:
375 	      return sizeof (_Complex unsigned short);
376 	      break;
377 
378 	    case _C_INT:
379 	      return sizeof (_Complex int);
380 	      break;
381 
382 	    case _C_UINT:
383 	      return sizeof (_Complex unsigned int);
384 	      break;
385 
386 	    case _C_LNG:
387 	      return sizeof (_Complex long);
388 	      break;
389 
390 	    case _C_ULNG:
391 	      return sizeof (_Complex unsigned long);
392 	      break;
393 
394 	    case _C_LNG_LNG:
395 	      return sizeof (_Complex long long);
396 	      break;
397 
398 	    case _C_ULNG_LNG:
399 	      return sizeof (_Complex unsigned long long);
400 	      break;
401 
402 	    case _C_FLT:
403 	      return sizeof (_Complex float);
404 	      break;
405 
406 	    case _C_DBL:
407 	      return sizeof (_Complex double);
408 	      break;
409 
410 	    case _C_LNG_DBL:
411 	      return sizeof (_Complex long double);
412 	      break;
413 
414 	    default:
415 	      {
416 		/* FIXME: Is this so bad that we have to abort the
417 		   entire program ?  (it applies to all the other
418 		   _objc_abort calls in this file).
419 		*/
420 		_objc_abort ("unknown complex type %s\n", type);
421 		return 0;
422 	      }
423 	}
424     }
425 
426   default:
427     {
428       _objc_abort ("unknown type %s\n", type);
429       return 0;
430     }
431   }
432 }
433 
434 int
435 objc_alignof_type (const char *type)
436 {
437   type = objc_skip_variable_name (type);
438 
439   switch (*type) {
440   case _C_BOOL:
441     return __alignof__ (_Bool);
442     break;
443 
444   case _C_ID:
445     return __alignof__ (id);
446     break;
447 
448   case _C_CLASS:
449     return __alignof__ (Class);
450     break;
451 
452   case _C_SEL:
453     return __alignof__ (SEL);
454     break;
455 
456   case _C_CHR:
457     return __alignof__ (char);
458     break;
459 
460   case _C_UCHR:
461     return __alignof__ (unsigned char);
462     break;
463 
464   case _C_SHT:
465     return __alignof__ (short);
466     break;
467 
468   case _C_USHT:
469     return __alignof__ (unsigned short);
470     break;
471 
472   case _C_INT:
473     return __alignof__ (int);
474     break;
475 
476   case _C_UINT:
477     return __alignof__ (unsigned int);
478     break;
479 
480   case _C_LNG:
481     return __alignof__ (long);
482     break;
483 
484   case _C_ULNG:
485     return __alignof__ (unsigned long);
486     break;
487 
488   case _C_LNG_LNG:
489     return __alignof__ (long long);
490     break;
491 
492   case _C_ULNG_LNG:
493     return __alignof__ (unsigned long long);
494     break;
495 
496   case _C_FLT:
497     return __alignof__ (float);
498     break;
499 
500   case _C_DBL:
501     return __alignof__ (double);
502     break;
503 
504   case _C_LNG_DBL:
505     return __alignof__ (long double);
506     break;
507 
508   case _C_PTR:
509   case _C_ATOM:
510   case _C_CHARPTR:
511     return __alignof__ (char *);
512     break;
513 
514   case _C_ARY_B:
515     while (isdigit ((unsigned char)*++type))
516       /* do nothing */;
517     return objc_alignof_type (type);
518 
519   case _C_VECTOR:
520     {
521       /* Skip the '!'.  */
522       type++;
523       /* Skip the '['.  */
524       type++;
525 
526       /* Skip the size.  */
527       while (isdigit ((unsigned char)*type))
528 	type++;
529 
530       /* Skip the ','.  */
531       type++;
532 
533       /* The alignment in bytes is the following number.  */
534       return atoi (type);
535     }
536   case _C_STRUCT_B:
537   case _C_UNION_B:
538     {
539       struct objc_struct_layout layout;
540       unsigned int align;
541 
542       objc_layout_structure (type, &layout);
543       while (objc_layout_structure_next_member (&layout))
544         /* do nothing */;
545       objc_layout_finish_structure (&layout, NULL, &align);
546 
547       return align;
548     }
549 
550 
551   case _C_COMPLEX:
552     {
553       type++; /* Skip after the 'j'. */
554       switch (*type)
555         {
556 	    case _C_CHR:
557 	      return __alignof__ (_Complex char);
558 	      break;
559 
560 	    case _C_UCHR:
561 	      return __alignof__ (_Complex unsigned char);
562 	      break;
563 
564 	    case _C_SHT:
565 	      return __alignof__ (_Complex short);
566 	      break;
567 
568 	    case _C_USHT:
569 	      return __alignof__ (_Complex unsigned short);
570 	      break;
571 
572 	    case _C_INT:
573 	      return __alignof__ (_Complex int);
574 	      break;
575 
576 	    case _C_UINT:
577 	      return __alignof__ (_Complex unsigned int);
578 	      break;
579 
580 	    case _C_LNG:
581 	      return __alignof__ (_Complex long);
582 	      break;
583 
584 	    case _C_ULNG:
585 	      return __alignof__ (_Complex unsigned long);
586 	      break;
587 
588 	    case _C_LNG_LNG:
589 	      return __alignof__ (_Complex long long);
590 	      break;
591 
592 	    case _C_ULNG_LNG:
593 	      return __alignof__ (_Complex unsigned long long);
594 	      break;
595 
596 	    case _C_FLT:
597 	      return __alignof__ (_Complex float);
598 	      break;
599 
600 	    case _C_DBL:
601 	      return __alignof__ (_Complex double);
602 	      break;
603 
604 	    case _C_LNG_DBL:
605 	      return __alignof__ (_Complex long double);
606 	      break;
607 
608 	    default:
609 	      {
610 		_objc_abort ("unknown complex type %s\n", type);
611 		return 0;
612 	      }
613 	}
614     }
615 
616   default:
617     {
618       _objc_abort ("unknown type %s\n", type);
619       return 0;
620     }
621   }
622 }
623 
624 int
625 objc_aligned_size (const char *type)
626 {
627   int size, align;
628 
629   type = objc_skip_variable_name (type);
630   size = objc_sizeof_type (type);
631   align = objc_alignof_type (type);
632 
633   return ROUND (size, align);
634 }
635 
636 int
637 objc_promoted_size (const char *type)
638 {
639   int size, wordsize;
640 
641   type = objc_skip_variable_name (type);
642   size = objc_sizeof_type (type);
643   wordsize = sizeof (void *);
644 
645   return ROUND (size, wordsize);
646 }
647 
648 /*
649   Skip type qualifiers.  These may eventually precede typespecs
650   occurring in method prototype encodings.
651 */
652 
653 const char *
654 objc_skip_type_qualifiers (const char *type)
655 {
656   while (*type == _C_CONST
657 	 || *type == _C_IN
658 	 || *type == _C_INOUT
659 	 || *type == _C_OUT
660 	 || *type == _C_BYCOPY
661          || *type == _C_BYREF
662 	 || *type == _C_ONEWAY
663 	 || *type == _C_GCINVISIBLE)
664     {
665       type += 1;
666     }
667   return type;
668 }
669 
670 const char *
671 objc_skip_typespec (const char *type)
672 {
673   type = objc_skip_variable_name (type);
674   type = objc_skip_type_qualifiers (type);
675 
676   switch (*type) {
677 
678   case _C_ID:
679     /* An id may be annotated by the actual type if it is known
680        with the @"ClassName" syntax */
681 
682     if (*++type != '"')
683       return type;
684     else
685       {
686 	while (*++type != '"')
687 	  /* do nothing */;
688 	return type + 1;
689       }
690 
691     /* The following are one character type codes */
692   case _C_CLASS:
693   case _C_SEL:
694   case _C_CHR:
695   case _C_UCHR:
696   case _C_CHARPTR:
697   case _C_ATOM:
698   case _C_SHT:
699   case _C_USHT:
700   case _C_INT:
701   case _C_UINT:
702   case _C_LNG:
703   case _C_BOOL:
704   case _C_ULNG:
705   case _C_LNG_LNG:
706   case _C_ULNG_LNG:
707   case _C_FLT:
708   case _C_DBL:
709   case _C_LNG_DBL:
710   case _C_VOID:
711   case _C_UNDEF:
712     return ++type;
713     break;
714 
715   case _C_COMPLEX:
716     return type + 2;
717     break;
718 
719   case _C_ARY_B:
720     /* skip digits, typespec and closing ']' */
721     while (isdigit ((unsigned char)*++type))
722       ;
723     type = objc_skip_typespec (type);
724     if (*type == _C_ARY_E)
725       return ++type;
726     else
727       {
728 	_objc_abort ("bad array type %s\n", type);
729 	return 0;
730       }
731 
732   case _C_VECTOR:
733     /* Skip '!' */
734     type++;
735     /* Skip '[' */
736     type++;
737     /* Skip digits (size) */
738     while (isdigit ((unsigned char)*type))
739       type++;
740     /* Skip ',' */
741     type++;
742     /* Skip digits (alignment) */
743     while (isdigit ((unsigned char)*type))
744       type++;
745     /* Skip typespec.  */
746     type = objc_skip_typespec (type);
747     /* Skip closing ']'.  */
748     if (*type == _C_ARY_E)
749       return ++type;
750     else
751       {
752 	_objc_abort ("bad vector type %s\n", type);
753 	return 0;
754       }
755 
756   case _C_BFLD:
757     /* The GNU encoding of bitfields is: b 'position' 'type'
758        'size'.  */
759     while (isdigit ((unsigned char)*++type))
760       ;	/* skip position */
761     while (isdigit ((unsigned char)*++type))
762       ;	/* skip type and size */
763     return type;
764 
765   case _C_STRUCT_B:
766     /* skip name, and elements until closing '}'  */
767 
768     while (*type != _C_STRUCT_E && *type++ != '=')
769       ;
770     while (*type != _C_STRUCT_E)
771       {
772 	type = objc_skip_typespec (type);
773       }
774     return ++type;
775 
776   case _C_UNION_B:
777     /* skip name, and elements until closing ')'  */
778 
779     while (*type != _C_UNION_E && *type++ != '=')
780       ;
781     while (*type != _C_UNION_E)
782       {
783 	type = objc_skip_typespec (type);
784       }
785     return ++type;
786 
787   case _C_PTR:
788     /* Just skip the following typespec */
789 
790     return objc_skip_typespec (++type);
791 
792   default:
793     {
794       _objc_abort ("unknown type %s\n", type);
795       return 0;
796     }
797   }
798 }
799 
800 /*
801   Skip an offset as part of a method encoding.  This is prepended by a
802   '+' if the argument is passed in registers.
803 */
804 const char *
805 objc_skip_offset (const char *type)
806 {
807   /* The offset is prepended by a '+' if the argument is passed in
808      registers.  PS: The compiler stopped generating this '+' in
809      version 3.4.  */
810   if (*type == '+')
811     type++;
812 
813   /* Some people claim that on some platforms, where the stack grows
814      backwards, the compiler generates negative offsets (??).  Skip a
815      '-' for such a negative offset.  */
816   if (*type == '-')
817     type++;
818 
819   /* Skip the digits that represent the offset.  */
820   while (isdigit ((unsigned char) *type))
821     type++;
822 
823   return type;
824 }
825 
826 const char *
827 objc_skip_argspec (const char *type)
828 {
829   type = objc_skip_typespec (type);
830   type = objc_skip_offset (type);
831   return type;
832 }
833 
834 char *
835 method_copyReturnType (struct objc_method *method)
836 {
837   if (method == NULL)
838     return 0;
839   else
840     {
841       char *returnValue;
842       size_t returnValueSize;
843 
844       /* Determine returnValueSize.  */
845       {
846 	/* Find the end of the first argument.  We want to return the
847 	   first argument spec, plus 1 byte for the \0 at the end.  */
848 	const char *type = method->method_types;
849 	if (*type == '\0')
850 	  return NULL;
851 	type = objc_skip_argspec (type);
852 	returnValueSize = type - method->method_types + 1;
853       }
854 
855       /* Copy the first argument into returnValue.  */
856       returnValue = malloc (sizeof (char) * returnValueSize);
857       memcpy (returnValue, method->method_types, returnValueSize);
858       returnValue[returnValueSize - 1] = '\0';
859 
860       return returnValue;
861     }
862 }
863 
864 char *
865 method_copyArgumentType (struct objc_method * method, unsigned int argumentNumber)
866 {
867   if (method == NULL)
868     return 0;
869   else
870     {
871       char *returnValue;
872       const char *returnValueStart;
873       size_t returnValueSize;
874 
875       /* Determine returnValueStart and returnValueSize.  */
876       {
877 	const char *type = method->method_types;
878 
879 	/* Skip the first argument (return type).  */
880 	type = objc_skip_argspec (type);
881 
882 	/* Now keep skipping arguments until we get to
883 	   argumentNumber.  */
884 	while (argumentNumber > 0)
885 	  {
886 	    /* We are supposed to skip an argument, but the string is
887 	       finished.  This means we were asked for a non-existing
888 	       argument.  */
889 	    if (*type == '\0')
890 	      return NULL;
891 
892 	    type = objc_skip_argspec (type);
893 	    argumentNumber--;
894 	  }
895 
896 	/* If the argument does not exist, return NULL.  */
897 	if (*type == '\0')
898 	  return NULL;
899 
900 	returnValueStart = type;
901 	type = objc_skip_argspec (type);
902 	returnValueSize = type - returnValueStart + 1;
903       }
904 
905       /* Copy the argument into returnValue.  */
906       returnValue = malloc (sizeof (char) * returnValueSize);
907       memcpy (returnValue, returnValueStart, returnValueSize);
908       returnValue[returnValueSize - 1] = '\0';
909 
910       return returnValue;
911     }
912 }
913 
914 void method_getReturnType (struct objc_method * method, char *returnValue,
915 			   size_t returnValueSize)
916 {
917   if (returnValue == NULL  ||  returnValueSize == 0)
918     return;
919 
920   /* Zero the string; we'll then write the argument type at the
921      beginning of it, if needed.  */
922   memset (returnValue, 0, returnValueSize);
923 
924   if (method == NULL)
925     return;
926   else
927     {
928       size_t argumentTypeSize;
929 
930       /* Determine argumentTypeSize.  */
931       {
932 	/* Find the end of the first argument.  We want to return the
933 	   first argument spec.  */
934 	const char *type = method->method_types;
935 	if (*type == '\0')
936 	  return;
937 	type = objc_skip_argspec (type);
938 	argumentTypeSize = type - method->method_types;
939 	if (argumentTypeSize > returnValueSize)
940 	  argumentTypeSize = returnValueSize;
941       }
942       /* Copy the argument at the beginning of the string.  */
943       memcpy (returnValue, method->method_types, argumentTypeSize);
944     }
945 }
946 
947 void method_getArgumentType (struct objc_method * method, unsigned int argumentNumber,
948 			     char *returnValue, size_t returnValueSize)
949 {
950   if (returnValue == NULL  ||  returnValueSize == 0)
951     return;
952 
953   /* Zero the string; we'll then write the argument type at the
954      beginning of it, if needed.  */
955   memset (returnValue, 0, returnValueSize);
956 
957   if (method == NULL)
958     return;
959   else
960     {
961       const char *returnValueStart;
962       size_t argumentTypeSize;
963 
964       /* Determine returnValueStart and argumentTypeSize.  */
965       {
966 	const char *type = method->method_types;
967 
968 	/* Skip the first argument (return type).  */
969 	type = objc_skip_argspec (type);
970 
971 	/* Now keep skipping arguments until we get to
972 	   argumentNumber.  */
973 	while (argumentNumber > 0)
974 	  {
975 	    /* We are supposed to skip an argument, but the string is
976 	       finished.  This means we were asked for a non-existing
977 	       argument.  */
978 	    if (*type == '\0')
979 	      return;
980 
981 	    type = objc_skip_argspec (type);
982 	    argumentNumber--;
983 	  }
984 
985 	/* If the argument does not exist, it's game over.  */
986 	if (*type == '\0')
987 	  return;
988 
989 	returnValueStart = type;
990 	type = objc_skip_argspec (type);
991 	argumentTypeSize = type - returnValueStart;
992 	if (argumentTypeSize > returnValueSize)
993 	  argumentTypeSize = returnValueSize;
994       }
995       /* Copy the argument at the beginning of the string.  */
996       memcpy (returnValue, returnValueStart, argumentTypeSize);
997     }
998 }
999 
1000 unsigned int
1001 method_getNumberOfArguments (struct objc_method *method)
1002 {
1003   if (method == NULL)
1004     return 0;
1005   else
1006     {
1007       unsigned int i = 0;
1008       const char *type = method->method_types;
1009       while (*type)
1010 	{
1011 	  type = objc_skip_argspec (type);
1012 	  i += 1;
1013 	}
1014 
1015       if (i == 0)
1016 	{
1017 	  /* This could only happen if method_types is invalid; in
1018 	     that case, return 0.  */
1019 	  return 0;
1020 	}
1021       else
1022 	{
1023 	  /* Remove the return type.  */
1024 	  return (i - 1);
1025 	}
1026     }
1027 }
1028 
1029 unsigned
1030 objc_get_type_qualifiers (const char *type)
1031 {
1032   unsigned res = 0;
1033   BOOL flag = YES;
1034 
1035   while (flag)
1036     switch (*type++)
1037       {
1038       case _C_CONST:       res |= _F_CONST; break;
1039       case _C_IN:          res |= _F_IN; break;
1040       case _C_INOUT:       res |= _F_INOUT; break;
1041       case _C_OUT:         res |= _F_OUT; break;
1042       case _C_BYCOPY:      res |= _F_BYCOPY; break;
1043       case _C_BYREF:       res |= _F_BYREF; break;
1044       case _C_ONEWAY:      res |= _F_ONEWAY; break;
1045       case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break;
1046       default: flag = NO;
1047     }
1048 
1049   return res;
1050 }
1051 
1052 /* The following three functions can be used to determine how a
1053    structure is laid out by the compiler. For example:
1054 
1055   struct objc_struct_layout layout;
1056   int i;
1057 
1058   objc_layout_structure (type, &layout);
1059   while (objc_layout_structure_next_member (&layout))
1060     {
1061       int position, align;
1062       const char *type;
1063 
1064       objc_layout_structure_get_info (&layout, &position, &align, &type);
1065       printf ("element %d has offset %d, alignment %d\n",
1066               i++, position, align);
1067     }
1068 
1069   These functions are used by objc_sizeof_type and objc_alignof_type
1070   functions to compute the size and alignment of structures. The
1071   previous method of computing the size and alignment of a structure
1072   was not working on some architectures, particulary on AIX, and in
1073   the presence of bitfields inside the structure.  */
1074 void
1075 objc_layout_structure (const char *type,
1076 		       struct objc_struct_layout *layout)
1077 {
1078   const char *ntype;
1079 
1080   if (*type != _C_UNION_B && *type != _C_STRUCT_B)
1081     {
1082       _objc_abort ("record (or union) type expected in objc_layout_structure, got %s\n",
1083 		   type);
1084     }
1085 
1086   type ++;
1087   layout->original_type = type;
1088 
1089   /* Skip "<name>=" if any. Avoid embedded structures and unions. */
1090   ntype = type;
1091   while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B
1092          && *ntype++ != '=')
1093     /* do nothing */;
1094 
1095   /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
1096   if (*(ntype - 1) == '=')
1097     type = ntype;
1098 
1099   layout->type = type;
1100   layout->prev_type = NULL;
1101   layout->record_size = 0;
1102   layout->record_align = BITS_PER_UNIT;
1103 
1104   layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY);
1105 }
1106 
1107 BOOL
1108 objc_layout_structure_next_member (struct objc_struct_layout *layout)
1109 {
1110   register int desired_align = 0;
1111 
1112   /* The following are used only if the field is a bitfield */
1113   register const char *bfld_type = 0;
1114   register int bfld_type_align = 0, bfld_field_size = 0;
1115 
1116   /* The current type without the type qualifiers */
1117   const char *type;
1118   BOOL unionp = layout->original_type[-1] == _C_UNION_B;
1119 
1120   /* Add the size of the previous field to the size of the record.  */
1121   if (layout->prev_type)
1122     {
1123       type = objc_skip_type_qualifiers (layout->prev_type);
1124       if (unionp)
1125         layout->record_size = MAX (layout->record_size,
1126 				   objc_sizeof_type (type) * BITS_PER_UNIT);
1127 
1128       else if (*type != _C_BFLD)
1129         layout->record_size += objc_sizeof_type (type) * BITS_PER_UNIT;
1130       else {
1131         /* Get the bitfield's type */
1132         for (bfld_type = type + 1;
1133              isdigit ((unsigned char)*bfld_type);
1134              bfld_type++)
1135           /* do nothing */;
1136 
1137         bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
1138         bfld_field_size = atoi (objc_skip_typespec (bfld_type));
1139         layout->record_size += bfld_field_size;
1140       }
1141     }
1142 
1143   if ((unionp && *layout->type == _C_UNION_E)
1144       || (!unionp && *layout->type == _C_STRUCT_E))
1145     return NO;
1146 
1147   /* Skip the variable name if any */
1148   layout->type = objc_skip_variable_name (layout->type);
1149   type = objc_skip_type_qualifiers (layout->type);
1150 
1151   if (*type != _C_BFLD)
1152     desired_align = objc_alignof_type (type) * BITS_PER_UNIT;
1153   else
1154     {
1155       desired_align = 1;
1156       /* Skip the bitfield's offset */
1157       for (bfld_type = type + 1;
1158            isdigit ((unsigned char) *bfld_type);
1159            bfld_type++)
1160         /* do nothing */;
1161 
1162       bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
1163       bfld_field_size = atoi (objc_skip_typespec (bfld_type));
1164     }
1165 
1166   /* The following won't work for vectors.  */
1167 #ifdef BIGGEST_FIELD_ALIGNMENT
1168   desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
1169 #endif
1170 #ifdef ADJUST_FIELD_ALIGN
1171   desired_align = ADJUST_FIELD_ALIGN (type, desired_align);
1172 #endif
1173 
1174   /* Record must have at least as much alignment as any field.
1175      Otherwise, the alignment of the field within the record
1176      is meaningless.  */
1177 #ifndef PCC_BITFIELD_TYPE_MATTERS
1178   layout->record_align = MAX (layout->record_align, desired_align);
1179 #else	/* PCC_BITFIELD_TYPE_MATTERS */
1180   if (*type == _C_BFLD)
1181     {
1182       /* For these machines, a zero-length field does not
1183          affect the alignment of the structure as a whole.
1184          It does, however, affect the alignment of the next field
1185          within the structure.  */
1186       if (bfld_field_size)
1187         layout->record_align = MAX (layout->record_align, desired_align);
1188       else
1189         desired_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
1190 
1191       /* A named bit field of declared type `int'
1192          forces the entire structure to have `int' alignment.
1193          Q1: How is encoded this thing and how to check for it?
1194          Q2: How to determine maximum_field_alignment at runtime? */
1195 
1196 /*	  if (DECL_NAME (field) != 0) */
1197       {
1198         int type_align = bfld_type_align;
1199 #if 0
1200         if (maximum_field_alignment != 0)
1201           type_align = MIN (type_align, maximum_field_alignment);
1202         else if (DECL_PACKED (field))
1203           type_align = MIN (type_align, BITS_PER_UNIT);
1204 #endif
1205 
1206         layout->record_align = MAX (layout->record_align, type_align);
1207       }
1208     }
1209   else
1210     layout->record_align = MAX (layout->record_align, desired_align);
1211 #endif	/* PCC_BITFIELD_TYPE_MATTERS */
1212 
1213   /* Does this field automatically have alignment it needs
1214      by virtue of the fields that precede it and the record's
1215      own alignment?  */
1216 
1217   if (*type == _C_BFLD)
1218     layout->record_size = atoi (type + 1);
1219   else if (layout->record_size % desired_align != 0)
1220     {
1221       /* No, we need to skip space before this field.
1222          Bump the cumulative size to multiple of field alignment.  */
1223       layout->record_size = ROUND (layout->record_size, desired_align);
1224     }
1225 
1226   /* Jump to the next field in record. */
1227 
1228   layout->prev_type = layout->type;
1229   layout->type = objc_skip_typespec (layout->type);      /* skip component */
1230 
1231   return YES;
1232 }
1233 
1234 void objc_layout_finish_structure (struct objc_struct_layout *layout,
1235                                    unsigned int *size,
1236                                    unsigned int *align)
1237 {
1238   BOOL unionp = layout->original_type[-1] == _C_UNION_B;
1239   if (layout->type
1240       && ((!unionp && *layout->type == _C_STRUCT_E)
1241        	  || (unionp && *layout->type == _C_UNION_E)))
1242     {
1243       /* Work out the alignment of the record as one expression and store
1244          in the record type.  Round it up to a multiple of the record's
1245          alignment. */
1246 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
1247       layout->record_align = ROUND_TYPE_ALIGN (layout->original_type-1,
1248                                                1,
1249                                                layout->record_align);
1250 #else
1251       layout->record_align = MAX (1, layout->record_align);
1252 #endif
1253 
1254 #ifdef ROUND_TYPE_SIZE
1255       layout->record_size = ROUND_TYPE_SIZE (layout->original_type,
1256                                              layout->record_size,
1257                                              layout->record_align);
1258 #else
1259       /* Round the size up to be a multiple of the required alignment */
1260       layout->record_size = ROUND (layout->record_size, layout->record_align);
1261 #endif
1262 
1263       layout->type = NULL;
1264     }
1265   if (size)
1266     *size = layout->record_size / BITS_PER_UNIT;
1267   if (align)
1268     *align = layout->record_align / BITS_PER_UNIT;
1269 }
1270 
1271 void objc_layout_structure_get_info (struct objc_struct_layout *layout,
1272                                      unsigned int *offset,
1273                                      unsigned int *align,
1274                                      const char **type)
1275 {
1276   if (offset)
1277     *offset = layout->record_size / BITS_PER_UNIT;
1278   if (align)
1279     *align = layout->record_align / BITS_PER_UNIT;
1280   if (type)
1281     *type = layout->prev_type;
1282 }
1283