Daily bump.
[official-gcc.git] / libobjc / encoding.c
blob73339086086bb0812eb2ade0be4094f8e7004b3c
1 /* Encoding of types for Objective C.
2 Copyright (C) 1993-2015 Free Software Foundation, Inc.
3 Contributed by Kresten Krab Thorup
4 Bitfield support by Ovidiu Predescu
6 This file is part of GCC.
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.
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.
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.
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/>. */
27 /* FIXME: This file has no business including tm.h. */
29 /* FIXME: This file contains functions that will abort the entire
30 program if they fail. Is that really needed ? */
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. */
43 #undef MAX
44 #define MAX(X, Y) \
45 ({ typeof (X) __x = (X), __y = (Y); \
46 (__x > __y ? __x : __y); })
48 #undef MIN
49 #define MIN(X, Y) \
50 ({ typeof (X) __x = (X), __y = (Y); \
51 (__x < __y ? __x : __y); })
53 #undef ROUND
54 #define ROUND(V, A) \
55 ({ typeof (V) __v = (V); typeof (A) __a = (A); \
56 __a * ((__v+__a - 1)/__a); })
59 /* Various hacks for objc_layout_record. These are used by the target
60 macros. */
62 #define TREE_CODE(TYPE) *(TYPE)
63 #define TREE_TYPE(TREE) (TREE)
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
70 #define REAL_TYPE _C_DBL
72 #define VECTOR_TYPE _C_VECTOR
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;})
80 #define DECL_MODE(TYPE) *(TYPE)
81 #define TYPE_MODE(TYPE) *(TYPE)
83 #define DFmode _C_DBL
85 #define strip_array_types(TYPE) ({const char *_field = (TYPE); \
86 while (*_field == _C_ARY_B)\
88 while (isdigit ((unsigned char)*++_field))\
91 _field;})
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;}))
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;
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)
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
121 #ifdef __VSX__
122 #define TARGET_VSX 1
123 #else
124 #define TARGET_VSX 0
125 #endif
127 #ifdef __ALTIVEC__
128 #define TARGET_ALTIVEC 1
129 #else
130 #define TARGET_ALTIVEC 0
131 #endif
133 #ifdef _ARCH_PPC64
134 #define TARGET_64BIT 1
135 #else
136 #define TARGET_64BIT 0
137 #endif
138 #endif
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*. */
145 #if __MACH__
146 # if __LP64__
147 # undef TARGET_ALIGN_NATURAL
148 # define TARGET_ALIGN_NATURAL 1
149 # endif
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)
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))
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;
173 default:
174 return MAX (comp, spec);
175 break;
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
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));})
195 #define rs6000_special_adjust_field_align_p(FIELD, COMPUTED) 0
197 /* Skip a variable name, enclosed in quotes ("). */
198 static inline
199 const char *
200 objc_skip_variable_name (const char *type)
202 /* Skip the variable name if any. */
203 if (*type == '"')
205 /* FIXME: How do we know we won't read beyond the end of the
206 string. Here and in the rest of the file! */
207 /* Skip '"'. */
208 type++;
209 /* Skip to the next '"'. */
210 while (*type != '"')
211 type++;
212 /* Skip '"'. */
213 type++;
216 return type;
220 objc_sizeof_type (const char *type)
222 type = objc_skip_variable_name (type);
224 switch (*type) {
225 case _C_BOOL:
226 return sizeof (_Bool);
227 break;
229 case _C_ID:
230 return sizeof (id);
231 break;
233 case _C_CLASS:
234 return sizeof (Class);
235 break;
237 case _C_SEL:
238 return sizeof (SEL);
239 break;
241 case _C_CHR:
242 return sizeof (char);
243 break;
245 case _C_UCHR:
246 return sizeof (unsigned char);
247 break;
249 case _C_SHT:
250 return sizeof (short);
251 break;
253 case _C_USHT:
254 return sizeof (unsigned short);
255 break;
257 case _C_INT:
258 return sizeof (int);
259 break;
261 case _C_UINT:
262 return sizeof (unsigned int);
263 break;
265 case _C_LNG:
266 return sizeof (long);
267 break;
269 case _C_ULNG:
270 return sizeof (unsigned long);
271 break;
273 case _C_LNG_LNG:
274 return sizeof (long long);
275 break;
277 case _C_ULNG_LNG:
278 return sizeof (unsigned long long);
279 break;
281 case _C_FLT:
282 return sizeof (float);
283 break;
285 case _C_DBL:
286 return sizeof (double);
287 break;
289 case _C_LNG_DBL:
290 return sizeof (long double);
291 break;
293 case _C_VOID:
294 return sizeof (void);
295 break;
297 case _C_PTR:
298 case _C_ATOM:
299 case _C_CHARPTR:
300 return sizeof (char *);
301 break;
303 case _C_ARY_B:
305 int len = atoi (type + 1);
306 while (isdigit ((unsigned char)*++type))
308 return len * objc_aligned_size (type);
310 break;
312 case _C_VECTOR:
314 /* Skip the '!'. */
315 type++;
316 /* Skip the '['. */
317 type++;
319 /* The size in bytes is the following number. */
320 int size = atoi (type);
321 return size;
323 break;
325 case _C_BFLD:
327 /* The GNU encoding of bitfields is: b 'position' 'type'
328 'size'. */
329 int position, size;
330 int startByte, endByte;
332 position = atoi (type + 1);
333 while (isdigit ((unsigned char)*++type))
335 size = atoi (type + 1);
337 startByte = position / BITS_PER_UNIT;
338 endByte = (position + size) / BITS_PER_UNIT;
339 return endByte - startByte;
342 case _C_UNION_B:
343 case _C_STRUCT_B:
345 struct objc_struct_layout layout;
346 unsigned int size;
348 objc_layout_structure (type, &layout);
349 while (objc_layout_structure_next_member (&layout))
350 /* do nothing */ ;
351 objc_layout_finish_structure (&layout, &size, NULL);
353 return size;
356 case _C_COMPLEX:
358 type++; /* Skip after the 'j'. */
359 switch (*type)
361 case _C_CHR:
362 return sizeof (_Complex char);
363 break;
365 case _C_UCHR:
366 return sizeof (_Complex unsigned char);
367 break;
369 case _C_SHT:
370 return sizeof (_Complex short);
371 break;
373 case _C_USHT:
374 return sizeof (_Complex unsigned short);
375 break;
377 case _C_INT:
378 return sizeof (_Complex int);
379 break;
381 case _C_UINT:
382 return sizeof (_Complex unsigned int);
383 break;
385 case _C_LNG:
386 return sizeof (_Complex long);
387 break;
389 case _C_ULNG:
390 return sizeof (_Complex unsigned long);
391 break;
393 case _C_LNG_LNG:
394 return sizeof (_Complex long long);
395 break;
397 case _C_ULNG_LNG:
398 return sizeof (_Complex unsigned long long);
399 break;
401 case _C_FLT:
402 return sizeof (_Complex float);
403 break;
405 case _C_DBL:
406 return sizeof (_Complex double);
407 break;
409 case _C_LNG_DBL:
410 return sizeof (_Complex long double);
411 break;
413 default:
415 /* FIXME: Is this so bad that we have to abort the
416 entire program ? (it applies to all the other
417 _objc_abort calls in this file).
419 _objc_abort ("unknown complex type %s\n", type);
420 return 0;
425 default:
427 _objc_abort ("unknown type %s\n", type);
428 return 0;
434 objc_alignof_type (const char *type)
436 type = objc_skip_variable_name (type);
438 switch (*type) {
439 case _C_BOOL:
440 return __alignof__ (_Bool);
441 break;
443 case _C_ID:
444 return __alignof__ (id);
445 break;
447 case _C_CLASS:
448 return __alignof__ (Class);
449 break;
451 case _C_SEL:
452 return __alignof__ (SEL);
453 break;
455 case _C_CHR:
456 return __alignof__ (char);
457 break;
459 case _C_UCHR:
460 return __alignof__ (unsigned char);
461 break;
463 case _C_SHT:
464 return __alignof__ (short);
465 break;
467 case _C_USHT:
468 return __alignof__ (unsigned short);
469 break;
471 case _C_INT:
472 return __alignof__ (int);
473 break;
475 case _C_UINT:
476 return __alignof__ (unsigned int);
477 break;
479 case _C_LNG:
480 return __alignof__ (long);
481 break;
483 case _C_ULNG:
484 return __alignof__ (unsigned long);
485 break;
487 case _C_LNG_LNG:
488 return __alignof__ (long long);
489 break;
491 case _C_ULNG_LNG:
492 return __alignof__ (unsigned long long);
493 break;
495 case _C_FLT:
496 return __alignof__ (float);
497 break;
499 case _C_DBL:
500 return __alignof__ (double);
501 break;
503 case _C_LNG_DBL:
504 return __alignof__ (long double);
505 break;
507 case _C_PTR:
508 case _C_ATOM:
509 case _C_CHARPTR:
510 return __alignof__ (char *);
511 break;
513 case _C_ARY_B:
514 while (isdigit ((unsigned char)*++type))
515 /* do nothing */;
516 return objc_alignof_type (type);
518 case _C_VECTOR:
520 /* Skip the '!'. */
521 type++;
522 /* Skip the '['. */
523 type++;
525 /* Skip the size. */
526 while (isdigit ((unsigned char)*type))
527 type++;
529 /* Skip the ','. */
530 type++;
532 /* The alignment in bytes is the following number. */
533 return atoi (type);
535 case _C_STRUCT_B:
536 case _C_UNION_B:
538 struct objc_struct_layout layout;
539 unsigned int align;
541 objc_layout_structure (type, &layout);
542 while (objc_layout_structure_next_member (&layout))
543 /* do nothing */;
544 objc_layout_finish_structure (&layout, NULL, &align);
546 return align;
550 case _C_COMPLEX:
552 type++; /* Skip after the 'j'. */
553 switch (*type)
555 case _C_CHR:
556 return __alignof__ (_Complex char);
557 break;
559 case _C_UCHR:
560 return __alignof__ (_Complex unsigned char);
561 break;
563 case _C_SHT:
564 return __alignof__ (_Complex short);
565 break;
567 case _C_USHT:
568 return __alignof__ (_Complex unsigned short);
569 break;
571 case _C_INT:
572 return __alignof__ (_Complex int);
573 break;
575 case _C_UINT:
576 return __alignof__ (_Complex unsigned int);
577 break;
579 case _C_LNG:
580 return __alignof__ (_Complex long);
581 break;
583 case _C_ULNG:
584 return __alignof__ (_Complex unsigned long);
585 break;
587 case _C_LNG_LNG:
588 return __alignof__ (_Complex long long);
589 break;
591 case _C_ULNG_LNG:
592 return __alignof__ (_Complex unsigned long long);
593 break;
595 case _C_FLT:
596 return __alignof__ (_Complex float);
597 break;
599 case _C_DBL:
600 return __alignof__ (_Complex double);
601 break;
603 case _C_LNG_DBL:
604 return __alignof__ (_Complex long double);
605 break;
607 default:
609 _objc_abort ("unknown complex type %s\n", type);
610 return 0;
615 default:
617 _objc_abort ("unknown type %s\n", type);
618 return 0;
624 objc_aligned_size (const char *type)
626 int size, align;
628 type = objc_skip_variable_name (type);
629 size = objc_sizeof_type (type);
630 align = objc_alignof_type (type);
632 return ROUND (size, align);
636 objc_promoted_size (const char *type)
638 int size, wordsize;
640 type = objc_skip_variable_name (type);
641 size = objc_sizeof_type (type);
642 wordsize = sizeof (void *);
644 return ROUND (size, wordsize);
647 inline
648 const char *
649 objc_skip_type_qualifiers (const char *type)
651 while (*type == _C_CONST
652 || *type == _C_IN
653 || *type == _C_INOUT
654 || *type == _C_OUT
655 || *type == _C_BYCOPY
656 || *type == _C_BYREF
657 || *type == _C_ONEWAY
658 || *type == _C_GCINVISIBLE)
660 type += 1;
662 return type;
665 inline
666 const char *
667 objc_skip_typespec (const char *type)
669 type = objc_skip_variable_name (type);
670 type = objc_skip_type_qualifiers (type);
672 switch (*type) {
674 case _C_ID:
675 /* An id may be annotated by the actual type if it is known
676 with the @"ClassName" syntax */
678 if (*++type != '"')
679 return type;
680 else
682 while (*++type != '"')
683 /* do nothing */;
684 return type + 1;
687 /* The following are one character type codes */
688 case _C_CLASS:
689 case _C_SEL:
690 case _C_CHR:
691 case _C_UCHR:
692 case _C_CHARPTR:
693 case _C_ATOM:
694 case _C_SHT:
695 case _C_USHT:
696 case _C_INT:
697 case _C_UINT:
698 case _C_LNG:
699 case _C_BOOL:
700 case _C_ULNG:
701 case _C_LNG_LNG:
702 case _C_ULNG_LNG:
703 case _C_FLT:
704 case _C_DBL:
705 case _C_LNG_DBL:
706 case _C_VOID:
707 case _C_UNDEF:
708 return ++type;
709 break;
711 case _C_COMPLEX:
712 return type + 2;
713 break;
715 case _C_ARY_B:
716 /* skip digits, typespec and closing ']' */
717 while (isdigit ((unsigned char)*++type))
719 type = objc_skip_typespec (type);
720 if (*type == _C_ARY_E)
721 return ++type;
722 else
724 _objc_abort ("bad array type %s\n", type);
725 return 0;
728 case _C_VECTOR:
729 /* Skip '!' */
730 type++;
731 /* Skip '[' */
732 type++;
733 /* Skip digits (size) */
734 while (isdigit ((unsigned char)*type))
735 type++;
736 /* Skip ',' */
737 type++;
738 /* Skip digits (alignment) */
739 while (isdigit ((unsigned char)*type))
740 type++;
741 /* Skip typespec. */
742 type = objc_skip_typespec (type);
743 /* Skip closing ']'. */
744 if (*type == _C_ARY_E)
745 return ++type;
746 else
748 _objc_abort ("bad vector type %s\n", type);
749 return 0;
752 case _C_BFLD:
753 /* The GNU encoding of bitfields is: b 'position' 'type'
754 'size'. */
755 while (isdigit ((unsigned char)*++type))
756 ; /* skip position */
757 while (isdigit ((unsigned char)*++type))
758 ; /* skip type and size */
759 return type;
761 case _C_STRUCT_B:
762 /* skip name, and elements until closing '}' */
764 while (*type != _C_STRUCT_E && *type++ != '=')
766 while (*type != _C_STRUCT_E)
768 type = objc_skip_typespec (type);
770 return ++type;
772 case _C_UNION_B:
773 /* skip name, and elements until closing ')' */
775 while (*type != _C_UNION_E && *type++ != '=')
777 while (*type != _C_UNION_E)
779 type = objc_skip_typespec (type);
781 return ++type;
783 case _C_PTR:
784 /* Just skip the following typespec */
786 return objc_skip_typespec (++type);
788 default:
790 _objc_abort ("unknown type %s\n", type);
791 return 0;
796 inline
797 const char *
798 objc_skip_offset (const char *type)
800 /* The offset is prepended by a '+' if the argument is passed in
801 registers. PS: The compiler stopped generating this '+' in
802 version 3.4. */
803 if (*type == '+')
804 type++;
806 /* Some people claim that on some platforms, where the stack grows
807 backwards, the compiler generates negative offsets (??). Skip a
808 '-' for such a negative offset. */
809 if (*type == '-')
810 type++;
812 /* Skip the digits that represent the offset. */
813 while (isdigit ((unsigned char) *type))
814 type++;
816 return type;
819 const char *
820 objc_skip_argspec (const char *type)
822 type = objc_skip_typespec (type);
823 type = objc_skip_offset (type);
824 return type;
827 char *
828 method_copyReturnType (struct objc_method *method)
830 if (method == NULL)
831 return 0;
832 else
834 char *returnValue;
835 size_t returnValueSize;
837 /* Determine returnValueSize. */
839 /* Find the end of the first argument. We want to return the
840 first argument spec, plus 1 byte for the \0 at the end. */
841 const char *type = method->method_types;
842 if (*type == '\0')
843 return NULL;
844 type = objc_skip_argspec (type);
845 returnValueSize = type - method->method_types + 1;
848 /* Copy the first argument into returnValue. */
849 returnValue = malloc (sizeof (char) * returnValueSize);
850 memcpy (returnValue, method->method_types, returnValueSize);
851 returnValue[returnValueSize - 1] = '\0';
853 return returnValue;
857 char *
858 method_copyArgumentType (struct objc_method * method, unsigned int argumentNumber)
860 if (method == NULL)
861 return 0;
862 else
864 char *returnValue;
865 const char *returnValueStart;
866 size_t returnValueSize;
868 /* Determine returnValueStart and returnValueSize. */
870 const char *type = method->method_types;
872 /* Skip the first argument (return type). */
873 type = objc_skip_argspec (type);
875 /* Now keep skipping arguments until we get to
876 argumentNumber. */
877 while (argumentNumber > 0)
879 /* We are supposed to skip an argument, but the string is
880 finished. This means we were asked for a non-existing
881 argument. */
882 if (*type == '\0')
883 return NULL;
885 type = objc_skip_argspec (type);
886 argumentNumber--;
889 /* If the argument does not exist, return NULL. */
890 if (*type == '\0')
891 return NULL;
893 returnValueStart = type;
894 type = objc_skip_argspec (type);
895 returnValueSize = type - returnValueStart + 1;
898 /* Copy the argument into returnValue. */
899 returnValue = malloc (sizeof (char) * returnValueSize);
900 memcpy (returnValue, returnValueStart, returnValueSize);
901 returnValue[returnValueSize - 1] = '\0';
903 return returnValue;
907 void method_getReturnType (struct objc_method * method, char *returnValue,
908 size_t returnValueSize)
910 if (returnValue == NULL || returnValueSize == 0)
911 return;
913 /* Zero the string; we'll then write the argument type at the
914 beginning of it, if needed. */
915 memset (returnValue, 0, returnValueSize);
917 if (method == NULL)
918 return;
919 else
921 size_t argumentTypeSize;
923 /* Determine argumentTypeSize. */
925 /* Find the end of the first argument. We want to return the
926 first argument spec. */
927 const char *type = method->method_types;
928 if (*type == '\0')
929 return;
930 type = objc_skip_argspec (type);
931 argumentTypeSize = type - method->method_types;
932 if (argumentTypeSize > returnValueSize)
933 argumentTypeSize = returnValueSize;
935 /* Copy the argument at the beginning of the string. */
936 memcpy (returnValue, method->method_types, argumentTypeSize);
940 void method_getArgumentType (struct objc_method * method, unsigned int argumentNumber,
941 char *returnValue, size_t returnValueSize)
943 if (returnValue == NULL || returnValueSize == 0)
944 return;
946 /* Zero the string; we'll then write the argument type at the
947 beginning of it, if needed. */
948 memset (returnValue, 0, returnValueSize);
950 if (method == NULL)
951 return;
952 else
954 const char *returnValueStart;
955 size_t argumentTypeSize;
957 /* Determine returnValueStart and argumentTypeSize. */
959 const char *type = method->method_types;
961 /* Skip the first argument (return type). */
962 type = objc_skip_argspec (type);
964 /* Now keep skipping arguments until we get to
965 argumentNumber. */
966 while (argumentNumber > 0)
968 /* We are supposed to skip an argument, but the string is
969 finished. This means we were asked for a non-existing
970 argument. */
971 if (*type == '\0')
972 return;
974 type = objc_skip_argspec (type);
975 argumentNumber--;
978 /* If the argument does not exist, it's game over. */
979 if (*type == '\0')
980 return;
982 returnValueStart = type;
983 type = objc_skip_argspec (type);
984 argumentTypeSize = type - returnValueStart;
985 if (argumentTypeSize > returnValueSize)
986 argumentTypeSize = returnValueSize;
988 /* Copy the argument at the beginning of the string. */
989 memcpy (returnValue, returnValueStart, argumentTypeSize);
993 unsigned int
994 method_getNumberOfArguments (struct objc_method *method)
996 if (method == NULL)
997 return 0;
998 else
1000 unsigned int i = 0;
1001 const char *type = method->method_types;
1002 while (*type)
1004 type = objc_skip_argspec (type);
1005 i += 1;
1008 if (i == 0)
1010 /* This could only happen if method_types is invalid; in
1011 that case, return 0. */
1012 return 0;
1014 else
1016 /* Remove the return type. */
1017 return (i - 1);
1022 unsigned
1023 objc_get_type_qualifiers (const char *type)
1025 unsigned res = 0;
1026 BOOL flag = YES;
1028 while (flag)
1029 switch (*type++)
1031 case _C_CONST: res |= _F_CONST; break;
1032 case _C_IN: res |= _F_IN; break;
1033 case _C_INOUT: res |= _F_INOUT; break;
1034 case _C_OUT: res |= _F_OUT; break;
1035 case _C_BYCOPY: res |= _F_BYCOPY; break;
1036 case _C_BYREF: res |= _F_BYREF; break;
1037 case _C_ONEWAY: res |= _F_ONEWAY; break;
1038 case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break;
1039 default: flag = NO;
1042 return res;
1045 /* The following three functions can be used to determine how a
1046 structure is laid out by the compiler. For example:
1048 struct objc_struct_layout layout;
1049 int i;
1051 objc_layout_structure (type, &layout);
1052 while (objc_layout_structure_next_member (&layout))
1054 int position, align;
1055 const char *type;
1057 objc_layout_structure_get_info (&layout, &position, &align, &type);
1058 printf ("element %d has offset %d, alignment %d\n",
1059 i++, position, align);
1062 These functions are used by objc_sizeof_type and objc_alignof_type
1063 functions to compute the size and alignment of structures. The
1064 previous method of computing the size and alignment of a structure
1065 was not working on some architectures, particularly on AIX, and in
1066 the presence of bitfields inside the structure. */
1067 void
1068 objc_layout_structure (const char *type,
1069 struct objc_struct_layout *layout)
1071 const char *ntype;
1073 if (*type != _C_UNION_B && *type != _C_STRUCT_B)
1075 _objc_abort ("record (or union) type expected in objc_layout_structure, got %s\n",
1076 type);
1079 type ++;
1080 layout->original_type = type;
1082 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
1083 ntype = type;
1084 while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B
1085 && *ntype++ != '=')
1086 /* do nothing */;
1088 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
1089 if (*(ntype - 1) == '=')
1090 type = ntype;
1092 layout->type = type;
1093 layout->prev_type = NULL;
1094 layout->record_size = 0;
1095 layout->record_align = BITS_PER_UNIT;
1097 layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY);
1100 BOOL
1101 objc_layout_structure_next_member (struct objc_struct_layout *layout)
1103 register int desired_align = 0;
1105 /* The following are used only if the field is a bitfield */
1106 register const char *bfld_type = 0;
1107 register int bfld_type_align = 0, bfld_field_size = 0;
1109 /* The current type without the type qualifiers */
1110 const char *type;
1111 BOOL unionp = layout->original_type[-1] == _C_UNION_B;
1113 /* Add the size of the previous field to the size of the record. */
1114 if (layout->prev_type)
1116 type = objc_skip_type_qualifiers (layout->prev_type);
1117 if (unionp)
1118 layout->record_size = MAX (layout->record_size,
1119 objc_sizeof_type (type) * BITS_PER_UNIT);
1121 else if (*type != _C_BFLD)
1122 layout->record_size += objc_sizeof_type (type) * BITS_PER_UNIT;
1123 else {
1124 /* Get the bitfield's type */
1125 for (bfld_type = type + 1;
1126 isdigit ((unsigned char)*bfld_type);
1127 bfld_type++)
1128 /* do nothing */;
1130 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
1131 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
1132 layout->record_size += bfld_field_size;
1136 if ((unionp && *layout->type == _C_UNION_E)
1137 || (!unionp && *layout->type == _C_STRUCT_E))
1138 return NO;
1140 /* Skip the variable name if any */
1141 layout->type = objc_skip_variable_name (layout->type);
1142 type = objc_skip_type_qualifiers (layout->type);
1144 if (*type != _C_BFLD)
1145 desired_align = objc_alignof_type (type) * BITS_PER_UNIT;
1146 else
1148 desired_align = 1;
1149 /* Skip the bitfield's offset */
1150 for (bfld_type = type + 1;
1151 isdigit ((unsigned char) *bfld_type);
1152 bfld_type++)
1153 /* do nothing */;
1155 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
1156 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
1159 /* The following won't work for vectors. */
1160 #ifdef BIGGEST_FIELD_ALIGNMENT
1161 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
1162 #endif
1163 #ifdef ADJUST_FIELD_ALIGN
1164 desired_align = ADJUST_FIELD_ALIGN (type, desired_align);
1165 #endif
1167 /* Record must have at least as much alignment as any field.
1168 Otherwise, the alignment of the field within the record
1169 is meaningless. */
1170 #ifndef PCC_BITFIELD_TYPE_MATTERS
1171 layout->record_align = MAX (layout->record_align, desired_align);
1172 #else /* PCC_BITFIELD_TYPE_MATTERS */
1173 if (*type == _C_BFLD)
1175 /* For these machines, a zero-length field does not
1176 affect the alignment of the structure as a whole.
1177 It does, however, affect the alignment of the next field
1178 within the structure. */
1179 if (bfld_field_size)
1180 layout->record_align = MAX (layout->record_align, desired_align);
1181 else
1182 desired_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
1184 /* A named bit field of declared type `int'
1185 forces the entire structure to have `int' alignment.
1186 Q1: How is encoded this thing and how to check for it?
1187 Q2: How to determine maximum_field_alignment at runtime? */
1189 /* if (DECL_NAME (field) != 0) */
1191 int type_align = bfld_type_align;
1192 #if 0
1193 if (maximum_field_alignment != 0)
1194 type_align = MIN (type_align, maximum_field_alignment);
1195 else if (DECL_PACKED (field))
1196 type_align = MIN (type_align, BITS_PER_UNIT);
1197 #endif
1199 layout->record_align = MAX (layout->record_align, type_align);
1202 else
1203 layout->record_align = MAX (layout->record_align, desired_align);
1204 #endif /* PCC_BITFIELD_TYPE_MATTERS */
1206 /* Does this field automatically have alignment it needs
1207 by virtue of the fields that precede it and the record's
1208 own alignment? */
1210 if (*type == _C_BFLD)
1211 layout->record_size = atoi (type + 1);
1212 else if (layout->record_size % desired_align != 0)
1214 /* No, we need to skip space before this field.
1215 Bump the cumulative size to multiple of field alignment. */
1216 layout->record_size = ROUND (layout->record_size, desired_align);
1219 /* Jump to the next field in record. */
1221 layout->prev_type = layout->type;
1222 layout->type = objc_skip_typespec (layout->type); /* skip component */
1224 return YES;
1227 void objc_layout_finish_structure (struct objc_struct_layout *layout,
1228 unsigned int *size,
1229 unsigned int *align)
1231 BOOL unionp = layout->original_type[-1] == _C_UNION_B;
1232 if (layout->type
1233 && ((!unionp && *layout->type == _C_STRUCT_E)
1234 || (unionp && *layout->type == _C_UNION_E)))
1236 /* Work out the alignment of the record as one expression and store
1237 in the record type. Round it up to a multiple of the record's
1238 alignment. */
1239 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
1240 layout->record_align = ROUND_TYPE_ALIGN (layout->original_type-1,
1242 layout->record_align);
1243 #else
1244 layout->record_align = MAX (1, layout->record_align);
1245 #endif
1247 #ifdef ROUND_TYPE_SIZE
1248 layout->record_size = ROUND_TYPE_SIZE (layout->original_type,
1249 layout->record_size,
1250 layout->record_align);
1251 #else
1252 /* Round the size up to be a multiple of the required alignment */
1253 layout->record_size = ROUND (layout->record_size, layout->record_align);
1254 #endif
1256 layout->type = NULL;
1258 if (size)
1259 *size = layout->record_size / BITS_PER_UNIT;
1260 if (align)
1261 *align = layout->record_align / BITS_PER_UNIT;
1264 void objc_layout_structure_get_info (struct objc_struct_layout *layout,
1265 unsigned int *offset,
1266 unsigned int *align,
1267 const char **type)
1269 if (offset)
1270 *offset = layout->record_size / BITS_PER_UNIT;
1271 if (align)
1272 *align = layout->record_align / BITS_PER_UNIT;
1273 if (type)
1274 *type = layout->prev_type;