2015-08-12 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / libobjc / encoding.c
blobabb614540979b3ab5c731f0f84da1cbbf225c84d
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 "config.h"
33 #include "objc-private/common.h"
34 #include "objc-private/error.h"
35 #include "tconfig.h"
36 #include "coretypes.h"
37 #include "tm.h"
38 #include "objc/runtime.h"
39 #include "objc-private/module-abi-8.h" /* For struct objc_method */
40 #include <stdlib.h>
41 #include <ctype.h>
42 #include <string.h> /* For memcpy. */
44 #undef MAX
45 #define MAX(X, Y) \
46 ({ typeof (X) __x = (X), __y = (Y); \
47 (__x > __y ? __x : __y); })
49 #undef MIN
50 #define MIN(X, Y) \
51 ({ typeof (X) __x = (X), __y = (Y); \
52 (__x < __y ? __x : __y); })
54 #undef ROUND
55 #define ROUND(V, A) \
56 ({ typeof (V) __v = (V); typeof (A) __a = (A); \
57 __a * ((__v+__a - 1)/__a); })
60 /* Various hacks for objc_layout_record. These are used by the target
61 macros. */
63 #define TREE_CODE(TYPE) *(TYPE)
64 #define TREE_TYPE(TREE) (TREE)
66 #define RECORD_TYPE _C_STRUCT_B
67 #define UNION_TYPE _C_UNION_B
68 #define QUAL_UNION_TYPE _C_UNION_B
69 #define ARRAY_TYPE _C_ARY_B
71 #define REAL_TYPE _C_DBL
73 #define VECTOR_TYPE _C_VECTOR
75 #define TYPE_FIELDS(TYPE) ({const char *_field = (TYPE)+1; \
76 while (*_field != _C_STRUCT_E && *_field != _C_STRUCT_B \
77 && *_field != _C_UNION_B && *_field++ != '=') \
78 /* do nothing */; \
79 _field;})
81 #define DECL_MODE(TYPE) *(TYPE)
82 #define TYPE_MODE(TYPE) *(TYPE)
84 #define DFmode _C_DBL
86 #define strip_array_types(TYPE) ({const char *_field = (TYPE); \
87 while (*_field == _C_ARY_B)\
89 while (isdigit ((unsigned char)*++_field))\
92 _field;})
94 /* Some ports (eg ARM) allow the structure size boundary to be
95 selected at compile-time. We override the normal definition with
96 one that has a constant value for this compilation. */
97 #ifndef BITS_PER_UNIT
98 #define BITS_PER_UNIT 8
99 #endif
100 #undef STRUCTURE_SIZE_BOUNDARY
101 #define STRUCTURE_SIZE_BOUNDARY (BITS_PER_UNIT * sizeof (struct{char a;}))
103 /* Some ROUND_TYPE_ALIGN macros use TARGET_foo, and consequently
104 target_flags. Define a dummy entry here to so we don't die.
105 We have to rename it because target_flags may already have been
106 declared extern. */
107 #define target_flags not_target_flags
108 static int __attribute__ ((__unused__)) not_target_flags = 0;
110 /* Some ROUND_TYPE_ALIGN use ALTIVEC_VECTOR_MODE (rs6000 darwin).
111 Define a dummy ALTIVEC_VECTOR_MODE so it will not die. */
112 #undef ALTIVEC_VECTOR_MODE
113 #define ALTIVEC_VECTOR_MODE(MODE) (0)
115 /* Replace TARGET_VSX, TARGET_ALTIVEC, and TARGET_64BIT with constants based on
116 the current switches, rather than looking in the options structure. */
117 #ifdef _ARCH_PPC
118 #undef TARGET_VSX
119 #undef TARGET_ALTIVEC
120 #undef TARGET_64BIT
122 #ifdef __VSX__
123 #define TARGET_VSX 1
124 #else
125 #define TARGET_VSX 0
126 #endif
128 #ifdef __ALTIVEC__
129 #define TARGET_ALTIVEC 1
130 #else
131 #define TARGET_ALTIVEC 0
132 #endif
134 #ifdef _ARCH_PPC64
135 #define TARGET_64BIT 1
136 #else
137 #define TARGET_64BIT 0
138 #endif
139 #endif
141 /* Furthermore, some (powerpc) targets also use TARGET_ALIGN_NATURAL
142 in their alignment macros. Currently[4.5/6], rs6000.h points this
143 to a static variable, initialized by target overrides. This is reset
144 in linux64.h but not in darwin64.h. The macro is not used by *86*. */
146 #if __MACH__
147 # if __LP64__
148 # undef TARGET_ALIGN_NATURAL
149 # define TARGET_ALIGN_NATURAL 1
150 # endif
152 /* On Darwin32, we need to recurse until we find the starting stuct type. */
153 static int
154 _darwin_rs6000_special_round_type_align (const char *struc, int comp, int spec)
156 const char *_stp , *_fields = TYPE_FIELDS (struc);
157 if (!_fields)
158 return MAX (comp, spec);
159 _stp = strip_array_types (_fields);
160 if (TYPE_MODE(_stp) == _C_COMPLEX)
161 _stp++;
162 switch (TYPE_MODE(_stp))
164 case RECORD_TYPE:
165 case UNION_TYPE:
166 return MAX (MAX (comp, spec), objc_alignof_type (_stp) * BITS_PER_UNIT);
167 break;
168 case DFmode:
169 case _C_LNG_LNG:
170 case _C_ULNG_LNG:
171 return MAX (MAX (comp, spec), 64);
172 break;
174 default:
175 return MAX (comp, spec);
176 break;
180 /* See comment below. */
181 #define darwin_rs6000_special_round_type_align(S,C,S2) \
182 (_darwin_rs6000_special_round_type_align ((char*)(S), (int)(C), (int)(S2)))
183 #endif
185 /* FIXME: while this file has no business including tm.h, this
186 definitely has no business defining this macro but it
187 is only way around without really rewritting this file,
188 should look after the branch of 3.4 to fix this. */
189 #define rs6000_special_round_type_align(STRUCT, COMPUTED, SPECIFIED) \
190 ({ const char *_fields = TYPE_FIELDS (STRUCT); \
191 ((_fields != 0 \
192 && TYPE_MODE (strip_array_types (TREE_TYPE (_fields))) == DFmode) \
193 ? MAX (MAX (COMPUTED, SPECIFIED), 64) \
194 : MAX (COMPUTED, SPECIFIED));})
196 #define rs6000_special_adjust_field_align_p(FIELD, COMPUTED) 0
198 /* Skip a variable name, enclosed in quotes ("). */
199 static inline
200 const char *
201 objc_skip_variable_name (const char *type)
203 /* Skip the variable name if any. */
204 if (*type == '"')
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++;
217 return type;
221 objc_sizeof_type (const char *type)
223 type = objc_skip_variable_name (type);
225 switch (*type) {
226 case _C_BOOL:
227 return sizeof (_Bool);
228 break;
230 case _C_ID:
231 return sizeof (id);
232 break;
234 case _C_CLASS:
235 return sizeof (Class);
236 break;
238 case _C_SEL:
239 return sizeof (SEL);
240 break;
242 case _C_CHR:
243 return sizeof (char);
244 break;
246 case _C_UCHR:
247 return sizeof (unsigned char);
248 break;
250 case _C_SHT:
251 return sizeof (short);
252 break;
254 case _C_USHT:
255 return sizeof (unsigned short);
256 break;
258 case _C_INT:
259 return sizeof (int);
260 break;
262 case _C_UINT:
263 return sizeof (unsigned int);
264 break;
266 case _C_LNG:
267 return sizeof (long);
268 break;
270 case _C_ULNG:
271 return sizeof (unsigned long);
272 break;
274 case _C_LNG_LNG:
275 return sizeof (long long);
276 break;
278 case _C_ULNG_LNG:
279 return sizeof (unsigned long long);
280 break;
282 case _C_FLT:
283 return sizeof (float);
284 break;
286 case _C_DBL:
287 return sizeof (double);
288 break;
290 case _C_LNG_DBL:
291 return sizeof (long double);
292 break;
294 case _C_VOID:
295 return sizeof (void);
296 break;
298 case _C_PTR:
299 case _C_ATOM:
300 case _C_CHARPTR:
301 return sizeof (char *);
302 break;
304 case _C_ARY_B:
306 int len = atoi (type + 1);
307 while (isdigit ((unsigned char)*++type))
309 return len * objc_aligned_size (type);
311 break;
313 case _C_VECTOR:
315 /* Skip the '!'. */
316 type++;
317 /* Skip the '['. */
318 type++;
320 /* The size in bytes is the following number. */
321 int size = atoi (type);
322 return size;
324 break;
326 case _C_BFLD:
328 /* The GNU encoding of bitfields is: b 'position' 'type'
329 'size'. */
330 int position, size;
331 int startByte, endByte;
333 position = atoi (type + 1);
334 while (isdigit ((unsigned char)*++type))
336 size = atoi (type + 1);
338 startByte = position / BITS_PER_UNIT;
339 endByte = (position + size) / BITS_PER_UNIT;
340 return endByte - startByte;
343 case _C_UNION_B:
344 case _C_STRUCT_B:
346 struct objc_struct_layout layout;
347 unsigned int size;
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);
354 return size;
357 case _C_COMPLEX:
359 type++; /* Skip after the 'j'. */
360 switch (*type)
362 case _C_CHR:
363 return sizeof (_Complex char);
364 break;
366 case _C_UCHR:
367 return sizeof (_Complex unsigned char);
368 break;
370 case _C_SHT:
371 return sizeof (_Complex short);
372 break;
374 case _C_USHT:
375 return sizeof (_Complex unsigned short);
376 break;
378 case _C_INT:
379 return sizeof (_Complex int);
380 break;
382 case _C_UINT:
383 return sizeof (_Complex unsigned int);
384 break;
386 case _C_LNG:
387 return sizeof (_Complex long);
388 break;
390 case _C_ULNG:
391 return sizeof (_Complex unsigned long);
392 break;
394 case _C_LNG_LNG:
395 return sizeof (_Complex long long);
396 break;
398 case _C_ULNG_LNG:
399 return sizeof (_Complex unsigned long long);
400 break;
402 case _C_FLT:
403 return sizeof (_Complex float);
404 break;
406 case _C_DBL:
407 return sizeof (_Complex double);
408 break;
410 case _C_LNG_DBL:
411 return sizeof (_Complex long double);
412 break;
414 default:
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).
420 _objc_abort ("unknown complex type %s\n", type);
421 return 0;
426 default:
428 _objc_abort ("unknown type %s\n", type);
429 return 0;
435 objc_alignof_type (const char *type)
437 type = objc_skip_variable_name (type);
439 switch (*type) {
440 case _C_BOOL:
441 return __alignof__ (_Bool);
442 break;
444 case _C_ID:
445 return __alignof__ (id);
446 break;
448 case _C_CLASS:
449 return __alignof__ (Class);
450 break;
452 case _C_SEL:
453 return __alignof__ (SEL);
454 break;
456 case _C_CHR:
457 return __alignof__ (char);
458 break;
460 case _C_UCHR:
461 return __alignof__ (unsigned char);
462 break;
464 case _C_SHT:
465 return __alignof__ (short);
466 break;
468 case _C_USHT:
469 return __alignof__ (unsigned short);
470 break;
472 case _C_INT:
473 return __alignof__ (int);
474 break;
476 case _C_UINT:
477 return __alignof__ (unsigned int);
478 break;
480 case _C_LNG:
481 return __alignof__ (long);
482 break;
484 case _C_ULNG:
485 return __alignof__ (unsigned long);
486 break;
488 case _C_LNG_LNG:
489 return __alignof__ (long long);
490 break;
492 case _C_ULNG_LNG:
493 return __alignof__ (unsigned long long);
494 break;
496 case _C_FLT:
497 return __alignof__ (float);
498 break;
500 case _C_DBL:
501 return __alignof__ (double);
502 break;
504 case _C_LNG_DBL:
505 return __alignof__ (long double);
506 break;
508 case _C_PTR:
509 case _C_ATOM:
510 case _C_CHARPTR:
511 return __alignof__ (char *);
512 break;
514 case _C_ARY_B:
515 while (isdigit ((unsigned char)*++type))
516 /* do nothing */;
517 return objc_alignof_type (type);
519 case _C_VECTOR:
521 /* Skip the '!'. */
522 type++;
523 /* Skip the '['. */
524 type++;
526 /* Skip the size. */
527 while (isdigit ((unsigned char)*type))
528 type++;
530 /* Skip the ','. */
531 type++;
533 /* The alignment in bytes is the following number. */
534 return atoi (type);
536 case _C_STRUCT_B:
537 case _C_UNION_B:
539 struct objc_struct_layout layout;
540 unsigned int align;
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);
547 return align;
551 case _C_COMPLEX:
553 type++; /* Skip after the 'j'. */
554 switch (*type)
556 case _C_CHR:
557 return __alignof__ (_Complex char);
558 break;
560 case _C_UCHR:
561 return __alignof__ (_Complex unsigned char);
562 break;
564 case _C_SHT:
565 return __alignof__ (_Complex short);
566 break;
568 case _C_USHT:
569 return __alignof__ (_Complex unsigned short);
570 break;
572 case _C_INT:
573 return __alignof__ (_Complex int);
574 break;
576 case _C_UINT:
577 return __alignof__ (_Complex unsigned int);
578 break;
580 case _C_LNG:
581 return __alignof__ (_Complex long);
582 break;
584 case _C_ULNG:
585 return __alignof__ (_Complex unsigned long);
586 break;
588 case _C_LNG_LNG:
589 return __alignof__ (_Complex long long);
590 break;
592 case _C_ULNG_LNG:
593 return __alignof__ (_Complex unsigned long long);
594 break;
596 case _C_FLT:
597 return __alignof__ (_Complex float);
598 break;
600 case _C_DBL:
601 return __alignof__ (_Complex double);
602 break;
604 case _C_LNG_DBL:
605 return __alignof__ (_Complex long double);
606 break;
608 default:
610 _objc_abort ("unknown complex type %s\n", type);
611 return 0;
616 default:
618 _objc_abort ("unknown type %s\n", type);
619 return 0;
625 objc_aligned_size (const char *type)
627 int size, align;
629 type = objc_skip_variable_name (type);
630 size = objc_sizeof_type (type);
631 align = objc_alignof_type (type);
633 return ROUND (size, align);
637 objc_promoted_size (const char *type)
639 int size, wordsize;
641 type = objc_skip_variable_name (type);
642 size = objc_sizeof_type (type);
643 wordsize = sizeof (void *);
645 return ROUND (size, wordsize);
648 inline
649 const char *
650 objc_skip_type_qualifiers (const char *type)
652 while (*type == _C_CONST
653 || *type == _C_IN
654 || *type == _C_INOUT
655 || *type == _C_OUT
656 || *type == _C_BYCOPY
657 || *type == _C_BYREF
658 || *type == _C_ONEWAY
659 || *type == _C_GCINVISIBLE)
661 type += 1;
663 return type;
666 inline
667 const char *
668 objc_skip_typespec (const char *type)
670 type = objc_skip_variable_name (type);
671 type = objc_skip_type_qualifiers (type);
673 switch (*type) {
675 case _C_ID:
676 /* An id may be annotated by the actual type if it is known
677 with the @"ClassName" syntax */
679 if (*++type != '"')
680 return type;
681 else
683 while (*++type != '"')
684 /* do nothing */;
685 return type + 1;
688 /* The following are one character type codes */
689 case _C_CLASS:
690 case _C_SEL:
691 case _C_CHR:
692 case _C_UCHR:
693 case _C_CHARPTR:
694 case _C_ATOM:
695 case _C_SHT:
696 case _C_USHT:
697 case _C_INT:
698 case _C_UINT:
699 case _C_LNG:
700 case _C_BOOL:
701 case _C_ULNG:
702 case _C_LNG_LNG:
703 case _C_ULNG_LNG:
704 case _C_FLT:
705 case _C_DBL:
706 case _C_LNG_DBL:
707 case _C_VOID:
708 case _C_UNDEF:
709 return ++type;
710 break;
712 case _C_COMPLEX:
713 return type + 2;
714 break;
716 case _C_ARY_B:
717 /* skip digits, typespec and closing ']' */
718 while (isdigit ((unsigned char)*++type))
720 type = objc_skip_typespec (type);
721 if (*type == _C_ARY_E)
722 return ++type;
723 else
725 _objc_abort ("bad array type %s\n", type);
726 return 0;
729 case _C_VECTOR:
730 /* Skip '!' */
731 type++;
732 /* Skip '[' */
733 type++;
734 /* Skip digits (size) */
735 while (isdigit ((unsigned char)*type))
736 type++;
737 /* Skip ',' */
738 type++;
739 /* Skip digits (alignment) */
740 while (isdigit ((unsigned char)*type))
741 type++;
742 /* Skip typespec. */
743 type = objc_skip_typespec (type);
744 /* Skip closing ']'. */
745 if (*type == _C_ARY_E)
746 return ++type;
747 else
749 _objc_abort ("bad vector type %s\n", type);
750 return 0;
753 case _C_BFLD:
754 /* The GNU encoding of bitfields is: b 'position' 'type'
755 'size'. */
756 while (isdigit ((unsigned char)*++type))
757 ; /* skip position */
758 while (isdigit ((unsigned char)*++type))
759 ; /* skip type and size */
760 return type;
762 case _C_STRUCT_B:
763 /* skip name, and elements until closing '}' */
765 while (*type != _C_STRUCT_E && *type++ != '=')
767 while (*type != _C_STRUCT_E)
769 type = objc_skip_typespec (type);
771 return ++type;
773 case _C_UNION_B:
774 /* skip name, and elements until closing ')' */
776 while (*type != _C_UNION_E && *type++ != '=')
778 while (*type != _C_UNION_E)
780 type = objc_skip_typespec (type);
782 return ++type;
784 case _C_PTR:
785 /* Just skip the following typespec */
787 return objc_skip_typespec (++type);
789 default:
791 _objc_abort ("unknown type %s\n", type);
792 return 0;
797 inline
798 const char *
799 objc_skip_offset (const char *type)
801 /* The offset is prepended by a '+' if the argument is passed in
802 registers. PS: The compiler stopped generating this '+' in
803 version 3.4. */
804 if (*type == '+')
805 type++;
807 /* Some people claim that on some platforms, where the stack grows
808 backwards, the compiler generates negative offsets (??). Skip a
809 '-' for such a negative offset. */
810 if (*type == '-')
811 type++;
813 /* Skip the digits that represent the offset. */
814 while (isdigit ((unsigned char) *type))
815 type++;
817 return type;
820 const char *
821 objc_skip_argspec (const char *type)
823 type = objc_skip_typespec (type);
824 type = objc_skip_offset (type);
825 return type;
828 char *
829 method_copyReturnType (struct objc_method *method)
831 if (method == NULL)
832 return 0;
833 else
835 char *returnValue;
836 size_t returnValueSize;
838 /* Determine returnValueSize. */
840 /* Find the end of the first argument. We want to return the
841 first argument spec, plus 1 byte for the \0 at the end. */
842 const char *type = method->method_types;
843 if (*type == '\0')
844 return NULL;
845 type = objc_skip_argspec (type);
846 returnValueSize = type - method->method_types + 1;
849 /* Copy the first argument into returnValue. */
850 returnValue = malloc (sizeof (char) * returnValueSize);
851 memcpy (returnValue, method->method_types, returnValueSize);
852 returnValue[returnValueSize - 1] = '\0';
854 return returnValue;
858 char *
859 method_copyArgumentType (struct objc_method * method, unsigned int argumentNumber)
861 if (method == NULL)
862 return 0;
863 else
865 char *returnValue;
866 const char *returnValueStart;
867 size_t returnValueSize;
869 /* Determine returnValueStart and returnValueSize. */
871 const char *type = method->method_types;
873 /* Skip the first argument (return type). */
874 type = objc_skip_argspec (type);
876 /* Now keep skipping arguments until we get to
877 argumentNumber. */
878 while (argumentNumber > 0)
880 /* We are supposed to skip an argument, but the string is
881 finished. This means we were asked for a non-existing
882 argument. */
883 if (*type == '\0')
884 return NULL;
886 type = objc_skip_argspec (type);
887 argumentNumber--;
890 /* If the argument does not exist, return NULL. */
891 if (*type == '\0')
892 return NULL;
894 returnValueStart = type;
895 type = objc_skip_argspec (type);
896 returnValueSize = type - returnValueStart + 1;
899 /* Copy the argument into returnValue. */
900 returnValue = malloc (sizeof (char) * returnValueSize);
901 memcpy (returnValue, returnValueStart, returnValueSize);
902 returnValue[returnValueSize - 1] = '\0';
904 return returnValue;
908 void method_getReturnType (struct objc_method * method, char *returnValue,
909 size_t returnValueSize)
911 if (returnValue == NULL || returnValueSize == 0)
912 return;
914 /* Zero the string; we'll then write the argument type at the
915 beginning of it, if needed. */
916 memset (returnValue, 0, returnValueSize);
918 if (method == NULL)
919 return;
920 else
922 size_t argumentTypeSize;
924 /* Determine argumentTypeSize. */
926 /* Find the end of the first argument. We want to return the
927 first argument spec. */
928 const char *type = method->method_types;
929 if (*type == '\0')
930 return;
931 type = objc_skip_argspec (type);
932 argumentTypeSize = type - method->method_types;
933 if (argumentTypeSize > returnValueSize)
934 argumentTypeSize = returnValueSize;
936 /* Copy the argument at the beginning of the string. */
937 memcpy (returnValue, method->method_types, argumentTypeSize);
941 void method_getArgumentType (struct objc_method * method, unsigned int argumentNumber,
942 char *returnValue, size_t returnValueSize)
944 if (returnValue == NULL || returnValueSize == 0)
945 return;
947 /* Zero the string; we'll then write the argument type at the
948 beginning of it, if needed. */
949 memset (returnValue, 0, returnValueSize);
951 if (method == NULL)
952 return;
953 else
955 const char *returnValueStart;
956 size_t argumentTypeSize;
958 /* Determine returnValueStart and argumentTypeSize. */
960 const char *type = method->method_types;
962 /* Skip the first argument (return type). */
963 type = objc_skip_argspec (type);
965 /* Now keep skipping arguments until we get to
966 argumentNumber. */
967 while (argumentNumber > 0)
969 /* We are supposed to skip an argument, but the string is
970 finished. This means we were asked for a non-existing
971 argument. */
972 if (*type == '\0')
973 return;
975 type = objc_skip_argspec (type);
976 argumentNumber--;
979 /* If the argument does not exist, it's game over. */
980 if (*type == '\0')
981 return;
983 returnValueStart = type;
984 type = objc_skip_argspec (type);
985 argumentTypeSize = type - returnValueStart;
986 if (argumentTypeSize > returnValueSize)
987 argumentTypeSize = returnValueSize;
989 /* Copy the argument at the beginning of the string. */
990 memcpy (returnValue, returnValueStart, argumentTypeSize);
994 unsigned int
995 method_getNumberOfArguments (struct objc_method *method)
997 if (method == NULL)
998 return 0;
999 else
1001 unsigned int i = 0;
1002 const char *type = method->method_types;
1003 while (*type)
1005 type = objc_skip_argspec (type);
1006 i += 1;
1009 if (i == 0)
1011 /* This could only happen if method_types is invalid; in
1012 that case, return 0. */
1013 return 0;
1015 else
1017 /* Remove the return type. */
1018 return (i - 1);
1023 unsigned
1024 objc_get_type_qualifiers (const char *type)
1026 unsigned res = 0;
1027 BOOL flag = YES;
1029 while (flag)
1030 switch (*type++)
1032 case _C_CONST: res |= _F_CONST; break;
1033 case _C_IN: res |= _F_IN; break;
1034 case _C_INOUT: res |= _F_INOUT; break;
1035 case _C_OUT: res |= _F_OUT; break;
1036 case _C_BYCOPY: res |= _F_BYCOPY; break;
1037 case _C_BYREF: res |= _F_BYREF; break;
1038 case _C_ONEWAY: res |= _F_ONEWAY; break;
1039 case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break;
1040 default: flag = NO;
1043 return res;
1046 /* The following three functions can be used to determine how a
1047 structure is laid out by the compiler. For example:
1049 struct objc_struct_layout layout;
1050 int i;
1052 objc_layout_structure (type, &layout);
1053 while (objc_layout_structure_next_member (&layout))
1055 int position, align;
1056 const char *type;
1058 objc_layout_structure_get_info (&layout, &position, &align, &type);
1059 printf ("element %d has offset %d, alignment %d\n",
1060 i++, position, align);
1063 These functions are used by objc_sizeof_type and objc_alignof_type
1064 functions to compute the size and alignment of structures. The
1065 previous method of computing the size and alignment of a structure
1066 was not working on some architectures, particularly on AIX, and in
1067 the presence of bitfields inside the structure. */
1068 void
1069 objc_layout_structure (const char *type,
1070 struct objc_struct_layout *layout)
1072 const char *ntype;
1074 if (*type != _C_UNION_B && *type != _C_STRUCT_B)
1076 _objc_abort ("record (or union) type expected in objc_layout_structure, got %s\n",
1077 type);
1080 type ++;
1081 layout->original_type = type;
1083 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
1084 ntype = type;
1085 while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B
1086 && *ntype++ != '=')
1087 /* do nothing */;
1089 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
1090 if (*(ntype - 1) == '=')
1091 type = ntype;
1093 layout->type = type;
1094 layout->prev_type = NULL;
1095 layout->record_size = 0;
1096 layout->record_align = BITS_PER_UNIT;
1098 layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY);
1101 BOOL
1102 objc_layout_structure_next_member (struct objc_struct_layout *layout)
1104 register int desired_align = 0;
1106 /* The following are used only if the field is a bitfield */
1107 register const char *bfld_type = 0;
1108 register int bfld_type_align = 0, bfld_field_size = 0;
1110 /* The current type without the type qualifiers */
1111 const char *type;
1112 BOOL unionp = layout->original_type[-1] == _C_UNION_B;
1114 /* Add the size of the previous field to the size of the record. */
1115 if (layout->prev_type)
1117 type = objc_skip_type_qualifiers (layout->prev_type);
1118 if (unionp)
1119 layout->record_size = MAX (layout->record_size,
1120 objc_sizeof_type (type) * BITS_PER_UNIT);
1122 else if (*type != _C_BFLD)
1123 layout->record_size += objc_sizeof_type (type) * BITS_PER_UNIT;
1124 else {
1125 /* Get the bitfield's type */
1126 for (bfld_type = type + 1;
1127 isdigit ((unsigned char)*bfld_type);
1128 bfld_type++)
1129 /* do nothing */;
1131 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
1132 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
1133 layout->record_size += bfld_field_size;
1137 if ((unionp && *layout->type == _C_UNION_E)
1138 || (!unionp && *layout->type == _C_STRUCT_E))
1139 return NO;
1141 /* Skip the variable name if any */
1142 layout->type = objc_skip_variable_name (layout->type);
1143 type = objc_skip_type_qualifiers (layout->type);
1145 if (*type != _C_BFLD)
1146 desired_align = objc_alignof_type (type) * BITS_PER_UNIT;
1147 else
1149 desired_align = 1;
1150 /* Skip the bitfield's offset */
1151 for (bfld_type = type + 1;
1152 isdigit ((unsigned char) *bfld_type);
1153 bfld_type++)
1154 /* do nothing */;
1156 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
1157 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
1160 /* The following won't work for vectors. */
1161 #ifdef BIGGEST_FIELD_ALIGNMENT
1162 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
1163 #endif
1164 #ifdef ADJUST_FIELD_ALIGN
1165 desired_align = ADJUST_FIELD_ALIGN (type, desired_align);
1166 #endif
1168 /* Record must have at least as much alignment as any field.
1169 Otherwise, the alignment of the field within the record
1170 is meaningless. */
1171 #ifndef HAVE_BITFIELD_TYPE_MATTERS
1172 layout->record_align = MAX (layout->record_align, desired_align);
1173 #else /* PCC_BITFIELD_TYPE_MATTERS */
1174 if (*type == _C_BFLD)
1176 /* For these machines, a zero-length field does not
1177 affect the alignment of the structure as a whole.
1178 It does, however, affect the alignment of the next field
1179 within the structure. */
1180 if (bfld_field_size)
1181 layout->record_align = MAX (layout->record_align, desired_align);
1182 else
1183 desired_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
1185 /* A named bit field of declared type `int'
1186 forces the entire structure to have `int' alignment.
1187 Q1: How is encoded this thing and how to check for it?
1188 Q2: How to determine maximum_field_alignment at runtime? */
1190 /* if (DECL_NAME (field) != 0) */
1192 int type_align = bfld_type_align;
1193 #if 0
1194 if (maximum_field_alignment != 0)
1195 type_align = MIN (type_align, maximum_field_alignment);
1196 else if (DECL_PACKED (field))
1197 type_align = MIN (type_align, BITS_PER_UNIT);
1198 #endif
1200 layout->record_align = MAX (layout->record_align, type_align);
1203 else
1204 layout->record_align = MAX (layout->record_align, desired_align);
1205 #endif /* PCC_BITFIELD_TYPE_MATTERS */
1207 /* Does this field automatically have alignment it needs
1208 by virtue of the fields that precede it and the record's
1209 own alignment? */
1211 if (*type == _C_BFLD)
1212 layout->record_size = atoi (type + 1);
1213 else if (layout->record_size % desired_align != 0)
1215 /* No, we need to skip space before this field.
1216 Bump the cumulative size to multiple of field alignment. */
1217 layout->record_size = ROUND (layout->record_size, desired_align);
1220 /* Jump to the next field in record. */
1222 layout->prev_type = layout->type;
1223 layout->type = objc_skip_typespec (layout->type); /* skip component */
1225 return YES;
1228 void objc_layout_finish_structure (struct objc_struct_layout *layout,
1229 unsigned int *size,
1230 unsigned int *align)
1232 BOOL unionp = layout->original_type[-1] == _C_UNION_B;
1233 if (layout->type
1234 && ((!unionp && *layout->type == _C_STRUCT_E)
1235 || (unionp && *layout->type == _C_UNION_E)))
1237 /* Work out the alignment of the record as one expression and store
1238 in the record type. Round it up to a multiple of the record's
1239 alignment. */
1240 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
1241 layout->record_align = ROUND_TYPE_ALIGN (layout->original_type-1,
1243 layout->record_align);
1244 #else
1245 layout->record_align = MAX (1, layout->record_align);
1246 #endif
1248 #ifdef ROUND_TYPE_SIZE
1249 layout->record_size = ROUND_TYPE_SIZE (layout->original_type,
1250 layout->record_size,
1251 layout->record_align);
1252 #else
1253 /* Round the size up to be a multiple of the required alignment */
1254 layout->record_size = ROUND (layout->record_size, layout->record_align);
1255 #endif
1257 layout->type = NULL;
1259 if (size)
1260 *size = layout->record_size / BITS_PER_UNIT;
1261 if (align)
1262 *align = layout->record_align / BITS_PER_UNIT;
1265 void objc_layout_structure_get_info (struct objc_struct_layout *layout,
1266 unsigned int *offset,
1267 unsigned int *align,
1268 const char **type)
1270 if (offset)
1271 *offset = layout->record_size / BITS_PER_UNIT;
1272 if (align)
1273 *align = layout->record_align / BITS_PER_UNIT;
1274 if (type)
1275 *type = layout->prev_type;