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