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