Daily bump.
[official-gcc.git] / libobjc / encoding.c
blobf3ce9f7b975a0b5b82680f930cd0318d5908d7cd
1 /* Encoding of types for Objective C.
2 Copyright (C) 1993-2021 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 #undef DFmode
85 #define DFmode _C_DBL
87 #define strip_array_types(TYPE) ({const char *_field = (TYPE); \
88 while (*_field == _C_ARY_B)\
90 while (isdigit ((unsigned char)*++_field))\
93 _field;})
95 /* Some ports (eg ARM) allow the structure size boundary to be
96 selected at compile-time. We override the normal definition with
97 one that has a constant value for this compilation. */
98 #undef STRUCTURE_SIZE_BOUNDARY
99 #define STRUCTURE_SIZE_BOUNDARY (__CHAR_BIT__ * sizeof (struct{char a;}))
101 /* Some ROUND_TYPE_ALIGN macros use TARGET_foo, and consequently
102 target_flags. Define a dummy entry here to so we don't die.
103 We have to rename it because target_flags may already have been
104 declared extern. */
105 #define target_flags not_target_flags
106 static int __attribute__ ((__unused__)) not_target_flags = 0;
108 /* Some ROUND_TYPE_ALIGN use ALTIVEC_VECTOR_MODE (rs6000 darwin).
109 Define a dummy ALTIVEC_VECTOR_MODE so it will not die. */
110 #undef ALTIVEC_VECTOR_MODE
111 #define ALTIVEC_VECTOR_MODE(MODE) (0)
113 /* Replace TARGET_VSX, TARGET_ALTIVEC, and TARGET_64BIT with constants based on
114 the current switches, rather than looking in the options structure. */
115 #ifdef _ARCH_PPC
116 #undef TARGET_VSX
117 #undef TARGET_ALTIVEC
118 #undef TARGET_64BIT
120 #ifdef __VSX__
121 #define TARGET_VSX 1
122 #else
123 #define TARGET_VSX 0
124 #endif
126 #ifdef __ALTIVEC__
127 #define TARGET_ALTIVEC 1
128 #else
129 #define TARGET_ALTIVEC 0
130 #endif
132 #ifdef _ARCH_PPC64
133 #define TARGET_64BIT 1
134 #else
135 #define TARGET_64BIT 0
136 #endif
137 #endif
139 /* Furthermore, some (powerpc) targets also use TARGET_ALIGN_NATURAL
140 in their alignment macros. Currently[4.5/6], rs6000.h points this
141 to a static variable, initialized by target overrides. This is reset
142 in linux64.h but not in darwin64.h. The macro is not used by *86*. */
144 #if __MACH__
145 # if __LP64__
146 # undef TARGET_ALIGN_NATURAL
147 # define TARGET_ALIGN_NATURAL 1
148 # endif
149 /* On Darwin32, we need to recurse until we find the starting stuct type. */
150 static int
151 _darwin_rs6000_special_round_type_align (const char *struc, int comp, int spec)
153 const char *_stp , *_fields = TYPE_FIELDS (struc);
154 if (!_fields)
155 return MAX (comp, spec);
156 _stp = strip_array_types (_fields);
157 if (TYPE_MODE(_stp) == _C_COMPLEX)
158 _stp++;
159 switch (TYPE_MODE(_stp))
161 case RECORD_TYPE:
162 case UNION_TYPE:
163 return MAX (MAX (comp, spec), objc_alignof_type (_stp) * __CHAR_BIT__);
164 break;
165 case DFmode:
166 case _C_LNG_LNG:
167 case _C_ULNG_LNG:
168 return MAX (MAX (comp, spec), 64);
169 break;
171 default:
172 return MAX (comp, spec);
173 break;
177 /* See comment below. */
178 #define darwin_rs6000_special_round_type_align(S,C,S2) \
179 (_darwin_rs6000_special_round_type_align ((char*)(S), (int)(C), (int)(S2)))
180 #endif
182 /* FIXME: while this file has no business including tm.h, this
183 definitely has no business defining this macro but it
184 is only way around without really rewritting this file,
185 should look after the branch of 3.4 to fix this. */
186 #define rs6000_special_round_type_align(STRUCT, COMPUTED, SPECIFIED) \
187 ({ const char *_fields = TYPE_FIELDS (STRUCT); \
188 ((_fields != 0 \
189 && TYPE_MODE (strip_array_types (TREE_TYPE (_fields))) == DFmode) \
190 ? MAX (MAX (COMPUTED, SPECIFIED), 64) \
191 : MAX (COMPUTED, SPECIFIED));})
193 #define rs6000_special_adjust_field_align_p(FIELD, COMPUTED) 0
195 /* Skip a variable name, enclosed in quotes ("). */
196 static inline
197 const char *
198 objc_skip_variable_name (const char *type)
200 /* Skip the variable name if any. */
201 if (*type == '"')
203 /* FIXME: How do we know we won't read beyond the end of the
204 string. Here and in the rest of the file! */
205 /* Skip '"'. */
206 type++;
207 /* Skip to the next '"'. */
208 while (*type != '"')
209 type++;
210 /* Skip '"'. */
211 type++;
214 return type;
218 objc_sizeof_type (const char *type)
220 type = objc_skip_variable_name (type);
222 switch (*type) {
223 case _C_BOOL:
224 return sizeof (_Bool);
225 break;
227 case _C_ID:
228 return sizeof (id);
229 break;
231 case _C_CLASS:
232 return sizeof (Class);
233 break;
235 case _C_SEL:
236 return sizeof (SEL);
237 break;
239 case _C_CHR:
240 return sizeof (char);
241 break;
243 case _C_UCHR:
244 return sizeof (unsigned char);
245 break;
247 case _C_SHT:
248 return sizeof (short);
249 break;
251 case _C_USHT:
252 return sizeof (unsigned short);
253 break;
255 case _C_INT:
256 return sizeof (int);
257 break;
259 case _C_UINT:
260 return sizeof (unsigned int);
261 break;
263 case _C_LNG:
264 return sizeof (long);
265 break;
267 case _C_ULNG:
268 return sizeof (unsigned long);
269 break;
271 case _C_LNG_LNG:
272 return sizeof (long long);
273 break;
275 case _C_ULNG_LNG:
276 return sizeof (unsigned long long);
277 break;
279 case _C_FLT:
280 return sizeof (float);
281 break;
283 case _C_DBL:
284 return sizeof (double);
285 break;
287 case _C_LNG_DBL:
288 return sizeof (long double);
289 break;
291 case _C_VOID:
292 return sizeof (void);
293 break;
295 case _C_PTR:
296 case _C_ATOM:
297 case _C_CHARPTR:
298 return sizeof (char *);
299 break;
301 case _C_ARY_B:
303 int len = atoi (type + 1);
304 while (isdigit ((unsigned char)*++type))
306 return len * objc_aligned_size (type);
308 break;
310 case _C_VECTOR:
312 /* Skip the '!'. */
313 type++;
314 /* Skip the '['. */
315 type++;
317 /* The size in bytes is the following number. */
318 int size = atoi (type);
319 return size;
321 break;
323 case _C_BFLD:
325 /* The GNU encoding of bitfields is: b 'position' 'type'
326 'size'. */
327 int position, size;
328 int startByte, endByte;
330 position = atoi (type + 1);
331 while (isdigit ((unsigned char)*++type))
333 size = atoi (type + 1);
335 startByte = position / __CHAR_BIT__;
336 endByte = (position + size) / __CHAR_BIT__;
337 return endByte - startByte;
340 case _C_UNION_B:
341 case _C_STRUCT_B:
343 struct objc_struct_layout layout;
344 unsigned int size;
346 objc_layout_structure (type, &layout);
347 while (objc_layout_structure_next_member (&layout))
348 /* do nothing */ ;
349 objc_layout_finish_structure (&layout, &size, NULL);
351 return size;
354 case _C_COMPLEX:
356 type++; /* Skip after the 'j'. */
357 switch (*type)
359 case _C_CHR:
360 return sizeof (_Complex char);
361 break;
363 case _C_UCHR:
364 return sizeof (_Complex unsigned char);
365 break;
367 case _C_SHT:
368 return sizeof (_Complex short);
369 break;
371 case _C_USHT:
372 return sizeof (_Complex unsigned short);
373 break;
375 case _C_INT:
376 return sizeof (_Complex int);
377 break;
379 case _C_UINT:
380 return sizeof (_Complex unsigned int);
381 break;
383 case _C_LNG:
384 return sizeof (_Complex long);
385 break;
387 case _C_ULNG:
388 return sizeof (_Complex unsigned long);
389 break;
391 case _C_LNG_LNG:
392 return sizeof (_Complex long long);
393 break;
395 case _C_ULNG_LNG:
396 return sizeof (_Complex unsigned long long);
397 break;
399 case _C_FLT:
400 return sizeof (_Complex float);
401 break;
403 case _C_DBL:
404 return sizeof (_Complex double);
405 break;
407 case _C_LNG_DBL:
408 return sizeof (_Complex long double);
409 break;
411 default:
413 /* FIXME: Is this so bad that we have to abort the
414 entire program ? (it applies to all the other
415 _objc_abort calls in this file).
417 _objc_abort ("unknown complex type %s\n", type);
418 return 0;
423 default:
425 _objc_abort ("unknown type %s\n", type);
426 return 0;
432 objc_alignof_type (const char *type)
434 type = objc_skip_variable_name (type);
436 switch (*type) {
437 case _C_BOOL:
438 return __alignof__ (_Bool);
439 break;
441 case _C_ID:
442 return __alignof__ (id);
443 break;
445 case _C_CLASS:
446 return __alignof__ (Class);
447 break;
449 case _C_SEL:
450 return __alignof__ (SEL);
451 break;
453 case _C_CHR:
454 return __alignof__ (char);
455 break;
457 case _C_UCHR:
458 return __alignof__ (unsigned char);
459 break;
461 case _C_SHT:
462 return __alignof__ (short);
463 break;
465 case _C_USHT:
466 return __alignof__ (unsigned short);
467 break;
469 case _C_INT:
470 return __alignof__ (int);
471 break;
473 case _C_UINT:
474 return __alignof__ (unsigned int);
475 break;
477 case _C_LNG:
478 return __alignof__ (long);
479 break;
481 case _C_ULNG:
482 return __alignof__ (unsigned long);
483 break;
485 case _C_LNG_LNG:
486 return __alignof__ (long long);
487 break;
489 case _C_ULNG_LNG:
490 return __alignof__ (unsigned long long);
491 break;
493 case _C_FLT:
494 return __alignof__ (float);
495 break;
497 case _C_DBL:
498 return __alignof__ (double);
499 break;
501 case _C_LNG_DBL:
502 return __alignof__ (long double);
503 break;
505 case _C_PTR:
506 case _C_ATOM:
507 case _C_CHARPTR:
508 return __alignof__ (char *);
509 break;
511 case _C_ARY_B:
512 while (isdigit ((unsigned char)*++type))
513 /* do nothing */;
514 return objc_alignof_type (type);
516 case _C_VECTOR:
518 /* Skip the '!'. */
519 type++;
520 /* Skip the '['. */
521 type++;
523 /* Skip the size. */
524 while (isdigit ((unsigned char)*type))
525 type++;
527 /* Skip the ','. */
528 type++;
530 /* The alignment in bytes is the following number. */
531 return atoi (type);
533 case _C_STRUCT_B:
534 case _C_UNION_B:
536 struct objc_struct_layout layout;
537 unsigned int align;
539 objc_layout_structure (type, &layout);
540 while (objc_layout_structure_next_member (&layout))
541 /* do nothing */;
542 objc_layout_finish_structure (&layout, NULL, &align);
544 return align;
548 case _C_COMPLEX:
550 type++; /* Skip after the 'j'. */
551 switch (*type)
553 case _C_CHR:
554 return __alignof__ (_Complex char);
555 break;
557 case _C_UCHR:
558 return __alignof__ (_Complex unsigned char);
559 break;
561 case _C_SHT:
562 return __alignof__ (_Complex short);
563 break;
565 case _C_USHT:
566 return __alignof__ (_Complex unsigned short);
567 break;
569 case _C_INT:
570 return __alignof__ (_Complex int);
571 break;
573 case _C_UINT:
574 return __alignof__ (_Complex unsigned int);
575 break;
577 case _C_LNG:
578 return __alignof__ (_Complex long);
579 break;
581 case _C_ULNG:
582 return __alignof__ (_Complex unsigned long);
583 break;
585 case _C_LNG_LNG:
586 return __alignof__ (_Complex long long);
587 break;
589 case _C_ULNG_LNG:
590 return __alignof__ (_Complex unsigned long long);
591 break;
593 case _C_FLT:
594 return __alignof__ (_Complex float);
595 break;
597 case _C_DBL:
598 return __alignof__ (_Complex double);
599 break;
601 case _C_LNG_DBL:
602 return __alignof__ (_Complex long double);
603 break;
605 default:
607 _objc_abort ("unknown complex type %s\n", type);
608 return 0;
613 default:
615 _objc_abort ("unknown type %s\n", type);
616 return 0;
622 objc_aligned_size (const char *type)
624 int size, align;
626 type = objc_skip_variable_name (type);
627 size = objc_sizeof_type (type);
628 align = objc_alignof_type (type);
630 return ROUND (size, align);
634 objc_promoted_size (const char *type)
636 int size, wordsize;
638 type = objc_skip_variable_name (type);
639 size = objc_sizeof_type (type);
640 wordsize = sizeof (void *);
642 return ROUND (size, wordsize);
645 inline
646 const char *
647 objc_skip_type_qualifiers (const char *type)
649 while (*type == _C_CONST
650 || *type == _C_IN
651 || *type == _C_INOUT
652 || *type == _C_OUT
653 || *type == _C_BYCOPY
654 || *type == _C_BYREF
655 || *type == _C_ONEWAY
656 || *type == _C_GCINVISIBLE)
658 type += 1;
660 return type;
663 inline
664 const char *
665 objc_skip_typespec (const char *type)
667 type = objc_skip_variable_name (type);
668 type = objc_skip_type_qualifiers (type);
670 switch (*type) {
672 case _C_ID:
673 /* An id may be annotated by the actual type if it is known
674 with the @"ClassName" syntax */
676 if (*++type != '"')
677 return type;
678 else
680 while (*++type != '"')
681 /* do nothing */;
682 return type + 1;
685 /* The following are one character type codes */
686 case _C_CLASS:
687 case _C_SEL:
688 case _C_CHR:
689 case _C_UCHR:
690 case _C_CHARPTR:
691 case _C_ATOM:
692 case _C_SHT:
693 case _C_USHT:
694 case _C_INT:
695 case _C_UINT:
696 case _C_LNG:
697 case _C_BOOL:
698 case _C_ULNG:
699 case _C_LNG_LNG:
700 case _C_ULNG_LNG:
701 case _C_FLT:
702 case _C_DBL:
703 case _C_LNG_DBL:
704 case _C_VOID:
705 case _C_UNDEF:
706 return ++type;
707 break;
709 case _C_COMPLEX:
710 return type + 2;
711 break;
713 case _C_ARY_B:
714 /* skip digits, typespec and closing ']' */
715 while (isdigit ((unsigned char)*++type))
717 type = objc_skip_typespec (type);
718 if (*type == _C_ARY_E)
719 return ++type;
720 else
722 _objc_abort ("bad array type %s\n", type);
723 return 0;
726 case _C_VECTOR:
727 /* Skip '!' */
728 type++;
729 /* Skip '[' */
730 type++;
731 /* Skip digits (size) */
732 while (isdigit ((unsigned char)*type))
733 type++;
734 /* Skip ',' */
735 type++;
736 /* Skip digits (alignment) */
737 while (isdigit ((unsigned char)*type))
738 type++;
739 /* Skip typespec. */
740 type = objc_skip_typespec (type);
741 /* Skip closing ']'. */
742 if (*type == _C_ARY_E)
743 return ++type;
744 else
746 _objc_abort ("bad vector type %s\n", type);
747 return 0;
750 case _C_BFLD:
751 /* The GNU encoding of bitfields is: b 'position' 'type'
752 'size'. */
753 while (isdigit ((unsigned char)*++type))
754 ; /* skip position */
755 while (isdigit ((unsigned char)*++type))
756 ; /* skip type and size */
757 return type;
759 case _C_STRUCT_B:
760 /* skip name, and elements until closing '}' */
762 while (*type != _C_STRUCT_E && *type++ != '=')
764 while (*type != _C_STRUCT_E)
766 type = objc_skip_typespec (type);
768 return ++type;
770 case _C_UNION_B:
771 /* skip name, and elements until closing ')' */
773 while (*type != _C_UNION_E && *type++ != '=')
775 while (*type != _C_UNION_E)
777 type = objc_skip_typespec (type);
779 return ++type;
781 case _C_PTR:
782 /* Just skip the following typespec */
784 return objc_skip_typespec (++type);
786 default:
788 _objc_abort ("unknown type %s\n", type);
789 return 0;
794 inline
795 const char *
796 objc_skip_offset (const char *type)
798 /* The offset is prepended by a '+' if the argument is passed in
799 registers. PS: The compiler stopped generating this '+' in
800 version 3.4. */
801 if (*type == '+')
802 type++;
804 /* Some people claim that on some platforms, where the stack grows
805 backwards, the compiler generates negative offsets (??). Skip a
806 '-' for such a negative offset. */
807 if (*type == '-')
808 type++;
810 /* Skip the digits that represent the offset. */
811 while (isdigit ((unsigned char) *type))
812 type++;
814 return type;
817 const char *
818 objc_skip_argspec (const char *type)
820 type = objc_skip_typespec (type);
821 type = objc_skip_offset (type);
822 return type;
825 char *
826 method_copyReturnType (struct objc_method *method)
828 if (method == NULL)
829 return 0;
830 else
832 char *returnValue;
833 size_t returnValueSize;
835 /* Determine returnValueSize. */
837 /* Find the end of the first argument. We want to return the
838 first argument spec, plus 1 byte for the \0 at the end. */
839 const char *type = method->method_types;
840 if (*type == '\0')
841 return NULL;
842 type = objc_skip_argspec (type);
843 returnValueSize = type - method->method_types + 1;
846 /* Copy the first argument into returnValue. */
847 returnValue = malloc (sizeof (char) * returnValueSize);
848 memcpy (returnValue, method->method_types, returnValueSize);
849 returnValue[returnValueSize - 1] = '\0';
851 return returnValue;
855 char *
856 method_copyArgumentType (struct objc_method * method, unsigned int argumentNumber)
858 if (method == NULL)
859 return 0;
860 else
862 char *returnValue;
863 const char *returnValueStart;
864 size_t returnValueSize;
866 /* Determine returnValueStart and returnValueSize. */
868 const char *type = method->method_types;
870 /* Skip the first argument (return type). */
871 type = objc_skip_argspec (type);
873 /* Now keep skipping arguments until we get to
874 argumentNumber. */
875 while (argumentNumber > 0)
877 /* We are supposed to skip an argument, but the string is
878 finished. This means we were asked for a non-existing
879 argument. */
880 if (*type == '\0')
881 return NULL;
883 type = objc_skip_argspec (type);
884 argumentNumber--;
887 /* If the argument does not exist, return NULL. */
888 if (*type == '\0')
889 return NULL;
891 returnValueStart = type;
892 type = objc_skip_argspec (type);
893 returnValueSize = type - returnValueStart + 1;
896 /* Copy the argument into returnValue. */
897 returnValue = malloc (sizeof (char) * returnValueSize);
898 memcpy (returnValue, returnValueStart, returnValueSize);
899 returnValue[returnValueSize - 1] = '\0';
901 return returnValue;
905 void method_getReturnType (struct objc_method * method, char *returnValue,
906 size_t returnValueSize)
908 if (returnValue == NULL || returnValueSize == 0)
909 return;
911 /* Zero the string; we'll then write the argument type at the
912 beginning of it, if needed. */
913 memset (returnValue, 0, returnValueSize);
915 if (method == NULL)
916 return;
917 else
919 size_t argumentTypeSize;
921 /* Determine argumentTypeSize. */
923 /* Find the end of the first argument. We want to return the
924 first argument spec. */
925 const char *type = method->method_types;
926 if (*type == '\0')
927 return;
928 type = objc_skip_argspec (type);
929 argumentTypeSize = type - method->method_types;
930 if (argumentTypeSize > returnValueSize)
931 argumentTypeSize = returnValueSize;
933 /* Copy the argument at the beginning of the string. */
934 memcpy (returnValue, method->method_types, argumentTypeSize);
938 void method_getArgumentType (struct objc_method * method, unsigned int argumentNumber,
939 char *returnValue, size_t returnValueSize)
941 if (returnValue == NULL || returnValueSize == 0)
942 return;
944 /* Zero the string; we'll then write the argument type at the
945 beginning of it, if needed. */
946 memset (returnValue, 0, returnValueSize);
948 if (method == NULL)
949 return;
950 else
952 const char *returnValueStart;
953 size_t argumentTypeSize;
955 /* Determine returnValueStart and argumentTypeSize. */
957 const char *type = method->method_types;
959 /* Skip the first argument (return type). */
960 type = objc_skip_argspec (type);
962 /* Now keep skipping arguments until we get to
963 argumentNumber. */
964 while (argumentNumber > 0)
966 /* We are supposed to skip an argument, but the string is
967 finished. This means we were asked for a non-existing
968 argument. */
969 if (*type == '\0')
970 return;
972 type = objc_skip_argspec (type);
973 argumentNumber--;
976 /* If the argument does not exist, it's game over. */
977 if (*type == '\0')
978 return;
980 returnValueStart = type;
981 type = objc_skip_argspec (type);
982 argumentTypeSize = type - returnValueStart;
983 if (argumentTypeSize > returnValueSize)
984 argumentTypeSize = returnValueSize;
986 /* Copy the argument at the beginning of the string. */
987 memcpy (returnValue, returnValueStart, argumentTypeSize);
991 unsigned int
992 method_getNumberOfArguments (struct objc_method *method)
994 if (method == NULL)
995 return 0;
996 else
998 unsigned int i = 0;
999 const char *type = method->method_types;
1000 while (*type)
1002 type = objc_skip_argspec (type);
1003 i += 1;
1006 if (i == 0)
1008 /* This could only happen if method_types is invalid; in
1009 that case, return 0. */
1010 return 0;
1012 else
1014 /* Remove the return type. */
1015 return (i - 1);
1020 unsigned
1021 objc_get_type_qualifiers (const char *type)
1023 unsigned res = 0;
1024 BOOL flag = YES;
1026 while (flag)
1027 switch (*type++)
1029 case _C_CONST: res |= _F_CONST; break;
1030 case _C_IN: res |= _F_IN; break;
1031 case _C_INOUT: res |= _F_INOUT; break;
1032 case _C_OUT: res |= _F_OUT; break;
1033 case _C_BYCOPY: res |= _F_BYCOPY; break;
1034 case _C_BYREF: res |= _F_BYREF; break;
1035 case _C_ONEWAY: res |= _F_ONEWAY; break;
1036 case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break;
1037 default: flag = NO;
1040 return res;
1043 /* The following three functions can be used to determine how a
1044 structure is laid out by the compiler. For example:
1046 struct objc_struct_layout layout;
1047 int i;
1049 objc_layout_structure (type, &layout);
1050 while (objc_layout_structure_next_member (&layout))
1052 int position, align;
1053 const char *type;
1055 objc_layout_structure_get_info (&layout, &position, &align, &type);
1056 printf ("element %d has offset %d, alignment %d\n",
1057 i++, position, align);
1060 These functions are used by objc_sizeof_type and objc_alignof_type
1061 functions to compute the size and alignment of structures. The
1062 previous method of computing the size and alignment of a structure
1063 was not working on some architectures, particularly on AIX, and in
1064 the presence of bitfields inside the structure. */
1065 void
1066 objc_layout_structure (const char *type,
1067 struct objc_struct_layout *layout)
1069 const char *ntype;
1071 if (*type != _C_UNION_B && *type != _C_STRUCT_B)
1073 _objc_abort ("record (or union) type expected in objc_layout_structure, got %s\n",
1074 type);
1077 type ++;
1078 layout->original_type = type;
1080 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
1081 ntype = type;
1082 while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B
1083 && *ntype++ != '=')
1084 /* do nothing */;
1086 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
1087 if (*(ntype - 1) == '=')
1088 type = ntype;
1090 layout->type = type;
1091 layout->prev_type = NULL;
1092 layout->record_size = 0;
1093 layout->record_align = __CHAR_BIT__;
1095 layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY);
1098 BOOL
1099 objc_layout_structure_next_member (struct objc_struct_layout *layout)
1101 register int desired_align = 0;
1103 /* The following are used only if the field is a bitfield */
1104 register const char *bfld_type = 0;
1105 register int bfld_type_align = 0, bfld_field_size = 0;
1107 /* The current type without the type qualifiers */
1108 const char *type;
1109 BOOL unionp = layout->original_type[-1] == _C_UNION_B;
1111 /* Add the size of the previous field to the size of the record. */
1112 if (layout->prev_type)
1114 type = objc_skip_type_qualifiers (layout->prev_type);
1115 if (unionp)
1116 layout->record_size = MAX (layout->record_size,
1117 objc_sizeof_type (type) * __CHAR_BIT__);
1119 else if (*type != _C_BFLD)
1120 layout->record_size += objc_sizeof_type (type) * __CHAR_BIT__;
1121 else {
1122 /* Get the bitfield's type */
1123 for (bfld_type = type + 1;
1124 isdigit ((unsigned char)*bfld_type);
1125 bfld_type++)
1126 /* do nothing */;
1128 bfld_type_align = objc_alignof_type (bfld_type) * __CHAR_BIT__;
1129 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
1130 layout->record_size += bfld_field_size;
1134 if ((unionp && *layout->type == _C_UNION_E)
1135 || (!unionp && *layout->type == _C_STRUCT_E))
1136 return NO;
1138 /* Skip the variable name if any */
1139 layout->type = objc_skip_variable_name (layout->type);
1140 type = objc_skip_type_qualifiers (layout->type);
1142 if (*type != _C_BFLD)
1143 desired_align = objc_alignof_type (type) * __CHAR_BIT__;
1144 else
1146 desired_align = 1;
1147 /* Skip the bitfield's offset */
1148 for (bfld_type = type + 1;
1149 isdigit ((unsigned char) *bfld_type);
1150 bfld_type++)
1151 /* do nothing */;
1153 bfld_type_align = objc_alignof_type (bfld_type) * __CHAR_BIT__;
1154 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
1157 /* The following won't work for vectors. */
1158 #ifdef BIGGEST_FIELD_ALIGNMENT
1159 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
1160 #endif
1161 #ifdef ADJUST_FIELD_ALIGN
1162 desired_align = ADJUST_FIELD_ALIGN (type, type, desired_align);
1163 #endif
1165 /* Record must have at least as much alignment as any field.
1166 Otherwise, the alignment of the field within the record
1167 is meaningless. */
1168 #ifndef HAVE_BITFIELD_TYPE_MATTERS
1169 layout->record_align = MAX (layout->record_align, desired_align);
1170 #else /* PCC_BITFIELD_TYPE_MATTERS */
1171 if (*type == _C_BFLD)
1173 /* For these machines, a zero-length field does not
1174 affect the alignment of the structure as a whole.
1175 It does, however, affect the alignment of the next field
1176 within the structure. */
1177 if (bfld_field_size)
1178 layout->record_align = MAX (layout->record_align, desired_align);
1179 else
1180 desired_align = objc_alignof_type (bfld_type) * __CHAR_BIT__;
1182 /* A named bit field of declared type `int'
1183 forces the entire structure to have `int' alignment.
1184 Q1: How is encoded this thing and how to check for it?
1185 Q2: How to determine maximum_field_alignment at runtime? */
1187 /* if (DECL_NAME (field) != 0) */
1189 int type_align = bfld_type_align;
1190 #if 0
1191 if (maximum_field_alignment != 0)
1192 type_align = MIN (type_align, maximum_field_alignment);
1193 else if (DECL_PACKED (field))
1194 type_align = MIN (type_align, __CHAR_BIT__);
1195 #endif
1197 layout->record_align = MAX (layout->record_align, type_align);
1200 else
1201 layout->record_align = MAX (layout->record_align, desired_align);
1202 #endif /* PCC_BITFIELD_TYPE_MATTERS */
1204 /* Does this field automatically have alignment it needs
1205 by virtue of the fields that precede it and the record's
1206 own alignment? */
1208 if (*type == _C_BFLD)
1209 layout->record_size = atoi (type + 1);
1210 else if (layout->record_size % desired_align != 0)
1212 /* No, we need to skip space before this field.
1213 Bump the cumulative size to multiple of field alignment. */
1214 layout->record_size = ROUND (layout->record_size, desired_align);
1217 /* Jump to the next field in record. */
1219 layout->prev_type = layout->type;
1220 layout->type = objc_skip_typespec (layout->type); /* skip component */
1222 return YES;
1225 void objc_layout_finish_structure (struct objc_struct_layout *layout,
1226 unsigned int *size,
1227 unsigned int *align)
1229 BOOL unionp = layout->original_type[-1] == _C_UNION_B;
1230 if (layout->type
1231 && ((!unionp && *layout->type == _C_STRUCT_E)
1232 || (unionp && *layout->type == _C_UNION_E)))
1234 /* Work out the alignment of the record as one expression and store
1235 in the record type. Round it up to a multiple of the record's
1236 alignment. */
1237 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
1238 layout->record_align = ROUND_TYPE_ALIGN (layout->original_type-1,
1240 layout->record_align);
1241 #else
1242 layout->record_align = MAX (1, layout->record_align);
1243 #endif
1245 /* Round the size up to be a multiple of the required alignment */
1246 layout->record_size = ROUND (layout->record_size, layout->record_align);
1248 layout->type = NULL;
1250 if (size)
1251 *size = layout->record_size / __CHAR_BIT__;
1252 if (align)
1253 *align = layout->record_align / __CHAR_BIT__;
1256 void objc_layout_structure_get_info (struct objc_struct_layout *layout,
1257 unsigned int *offset,
1258 unsigned int *align,
1259 const char **type)
1261 if (offset)
1262 *offset = layout->record_size / __CHAR_BIT__;
1263 if (align)
1264 *align = layout->record_align / __CHAR_BIT__;
1265 if (type)
1266 *type = layout->prev_type;