encoding.c
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:21k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1. /* Encoding of types for Objective C.
  2.    Copyright (C) 1993, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
  3.    Contributed by Kresten Krab Thorup
  4.    Bitfield support by Ovidiu Predescu
  5. This file is part of GNU CC.
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10. GNU CC is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GNU CC; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA.  */
  18. /* As a special exception, if you link this library with files
  19.    compiled with GCC to produce an executable, this does not cause
  20.    the resulting executable to be covered by the GNU General Public License.
  21.    This exception does not however invalidate any other reasons why
  22.    the executable file might be covered by the GNU General Public License.  */
  23. #include "objc-api.h"
  24. #include "encoding.h"
  25. #include "compiler-info.h"
  26. #ifndef BITS_PER_UNIT
  27. #error cc1obj -print-objc-runtime-info must report BITS_PER_UNIT
  28. #endif
  29. #define MAX(X, Y)                    
  30.   ({ typeof(X) __x = (X), __y = (Y); 
  31.      (__x > __y ? __x : __y); })
  32. #define MIN(X, Y)                    
  33.   ({ typeof(X) __x = (X), __y = (Y); 
  34.      (__x < __y ? __x : __y); })
  35. #define ROUND(V, A) 
  36.   ({ typeof(V) __v=(V); typeof(A) __a=(A); 
  37.      __a*((__v+__a-1)/__a); })
  38. #if 0
  39. /* Various hacks for objc_layout_record. These are used by the target
  40.    macros. */
  41. #define TREE_CODE(TYPE) *TYPE
  42. #define TREE_TYPE(TREE) TREE
  43. #define RECORD_TYPE     _C_STRUCT_B
  44. #define UNION_TYPE      _C_UNION_B
  45. #define QUAL_UNION_TYPE _C_UNION_B
  46. #define ARRAY_TYPE      _C_ARY_B
  47. #define TYPE_FIELDS(TYPE)     objc_skip_typespec (TYPE)
  48. #define DECL_MODE(TYPE)         *(TYPE)
  49. #define DFmode          _C_DBL
  50. #define get_inner_array_type(TYPE)      ((TYPE) + 1)
  51. #endif
  52. static inline int
  53. atoi (const char* str)
  54. {
  55.   int res = 0;
  56.   
  57.   while (isDigit (*str))
  58.     res *= 10, res += (*str++ - '0');
  59.   return res;
  60. }
  61. /*
  62.   return the size of an object specified by type 
  63. */
  64. int
  65. objc_sizeof_type (const char* type)
  66. {
  67.   /* Skip the variable name if any */
  68.   if (*type == '"')
  69.     {
  70.       for (type++; *type++ != '"';)
  71. /* do nothing */;
  72.     }
  73.   switch(*type) {
  74.   case _C_ID:
  75.     return sizeof(id);
  76.     break;
  77.   case _C_CLASS:
  78.     return sizeof(Class);
  79.     break;
  80.   case _C_SEL:
  81.     return sizeof(SEL);
  82.     break;
  83.   case _C_CHR:
  84.     return sizeof(char);
  85.     break;
  86.     
  87.   case _C_UCHR:
  88.     return sizeof(unsigned char);
  89.     break;
  90.   case _C_SHT:
  91.     return sizeof(short);
  92.     break;
  93.   case _C_USHT:
  94.     return sizeof(unsigned short);
  95.     break;
  96.   case _C_INT:
  97.     return sizeof(int);
  98.     break;
  99.   case _C_UINT:
  100.     return sizeof(unsigned int);
  101.     break;
  102.   case _C_LNG:
  103.     return sizeof(long);
  104.     break;
  105.   case _C_ULNG:
  106.     return sizeof(unsigned long);
  107.     break;
  108.   case _C_LNG_LNG:
  109.     return sizeof(long long);
  110.     break;
  111.   case _C_ULNG_LNG:
  112.     return sizeof(unsigned long long);
  113.     break;
  114.   case _C_FLT:
  115.     return sizeof(float);
  116.     break;
  117.   case _C_DBL:
  118.     return sizeof(double);
  119.     break;
  120.   case _C_VOID:
  121.     return sizeof(void);
  122.     break;
  123.   case _C_PTR:
  124.   case _C_ATOM:
  125.   case _C_CHARPTR:
  126.     return sizeof(char*);
  127.     break;
  128.   case _C_ARY_B:
  129.     {
  130.       int len = atoi(type+1);
  131.       while (isDigit (*++type));
  132.       return len * objc_aligned_size (type);
  133.     }
  134.     break; 
  135.   case _C_BFLD:
  136.     {
  137.       /* The new encoding of bitfields is: b 'position' 'type' 'size' */
  138.       int position, size;
  139.       int startByte, endByte;
  140.       position = atoi (type + 1);
  141.       while (isDigit (*++type));
  142.       size = atoi (type + 1);
  143.       startByte = position / BITS_PER_UNIT;
  144.       endByte = (position + size) / BITS_PER_UNIT;
  145.       return endByte - startByte;
  146.     }
  147.   case _C_STRUCT_B:
  148.     {
  149.       struct objc_struct_layout layout;
  150.       unsigned int size;
  151.       objc_layout_structure (type, &layout);
  152.       while (objc_layout_structure_next_member (&layout))
  153.         /* do nothing */ ;
  154.       objc_layout_finish_structure (&layout, &size, NULL);
  155.       return size;
  156.     }
  157.   case _C_UNION_B:
  158.     {
  159.       int max_size = 0;
  160.       while (*type != _C_UNION_E && *type++ != '=') /* do nothing */;
  161.       while (*type != _C_UNION_E)
  162. {
  163.   /* Skip the variable name if any */
  164.   if (*type == '"')
  165.     {
  166.       for (type++; *type++ != '"';)
  167. /* do nothing */;
  168.     }
  169.   max_size = MAX (max_size, objc_sizeof_type (type));
  170.   type = objc_skip_typespec (type);
  171. }
  172.       return max_size;
  173.     }
  174.     
  175.   default:
  176.     {
  177.       objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %sn", type);
  178.       return 0;
  179.     }
  180.   }
  181. }
  182. /*
  183.   Return the alignment of an object specified by type 
  184. */
  185. int
  186. objc_alignof_type(const char* type)
  187. {
  188.   /* Skip the variable name if any */
  189.   if (*type == '"')
  190.     {
  191.       for (type++; *type++ != '"';)
  192. /* do nothing */;
  193.     }
  194.   switch(*type) {
  195.   case _C_ID:
  196.     return __alignof__(id);
  197.     break;
  198.   case _C_CLASS:
  199.     return __alignof__(Class);
  200.     break;
  201.     
  202.   case _C_SEL:
  203.     return __alignof__(SEL);
  204.     break;
  205.   case _C_CHR:
  206.     return __alignof__(char);
  207.     break;
  208.     
  209.   case _C_UCHR:
  210.     return __alignof__(unsigned char);
  211.     break;
  212.   case _C_SHT:
  213.     return __alignof__(short);
  214.     break;
  215.   case _C_USHT:
  216.     return __alignof__(unsigned short);
  217.     break;
  218.   case _C_INT:
  219.     return __alignof__(int);
  220.     break;
  221.   case _C_UINT:
  222.     return __alignof__(unsigned int);
  223.     break;
  224.   case _C_LNG:
  225.     return __alignof__(long);
  226.     break;
  227.   case _C_ULNG:
  228.     return __alignof__(unsigned long);
  229.     break;
  230.   case _C_LNG_LNG:
  231.     return __alignof__(long long);
  232.     break;
  233.   case _C_ULNG_LNG:
  234.     return __alignof__(unsigned long long);
  235.     break;
  236.   case _C_FLT:
  237.     return __alignof__(float);
  238.     break;
  239.   case _C_DBL:
  240. #if 0
  241.     return __alignof__(double);
  242. #else
  243.     return 4;
  244. #endif
  245.     break;
  246.   case _C_PTR:
  247.   case _C_ATOM:
  248.   case _C_CHARPTR:
  249.     return __alignof__(char*);
  250.     break;
  251.   case _C_ARY_B:
  252.     while (isDigit (*++type)) /* do nothing */;
  253.     return objc_alignof_type (type);
  254.   case _C_STRUCT_B:
  255.     {
  256.       struct objc_struct_layout layout;
  257.       unsigned int align;
  258.       objc_layout_structure (type, &layout);
  259.       while (objc_layout_structure_next_member (&layout))
  260.         /* do nothing */;
  261.       objc_layout_finish_structure (&layout, NULL, &align);
  262.       return align;
  263.     }
  264.   case _C_UNION_B:
  265.     {
  266.       int maxalign = 0;
  267.       while (*type != _C_UNION_E && *type++ != '=') /* do nothing */;
  268.       while (*type != _C_UNION_E)
  269. {
  270.   /* Skip the variable name if any */
  271.   if (*type == '"')
  272.     {
  273.       for (type++; *type++ != '"';)
  274. /* do nothing */;
  275.     }
  276.   maxalign = MAX (maxalign, objc_alignof_type (type));
  277.   type = objc_skip_typespec (type);
  278. }
  279.       return maxalign;
  280.     }
  281.   default:
  282.     {
  283.       objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %sn", type);
  284.       return 0;
  285.     }
  286.   }
  287. }
  288. /*
  289.   The aligned size if the size rounded up to the nearest alignment.
  290. */
  291. int
  292. objc_aligned_size (const char* type)
  293. {
  294.   int size, align;
  295.   /* Skip the variable name */
  296.   if (*type == '"')
  297.     {
  298.       for (type++; *type++ != '"';)
  299. /* do nothing */;
  300.     }
  301.   size = objc_sizeof_type (type);
  302.   align = objc_alignof_type (type);
  303.   return ROUND (size, align);
  304. }
  305. /*
  306.   The size rounded up to the nearest integral of the wordsize, taken
  307.   to be the size of a void*.
  308. */
  309. int 
  310. objc_promoted_size (const char* type)
  311. {
  312.   int size, wordsize;
  313.   /* Skip the variable name */
  314.   if (*type == '"')
  315.     {
  316.       for (type++; *type++ != '"';)
  317. /* do nothing */;
  318.     }
  319.   size = objc_sizeof_type (type);
  320.   wordsize = sizeof (void*);
  321.   return ROUND (size, wordsize);
  322. }
  323. /*
  324.   Skip type qualifiers.  These may eventually precede typespecs
  325.   occurring in method prototype encodings.
  326. */
  327. inline const char*
  328. objc_skip_type_qualifiers (const char* type)
  329. {
  330.   while (*type == _C_CONST
  331.  || *type == _C_IN 
  332.  || *type == _C_INOUT
  333.  || *type == _C_OUT 
  334.  || *type == _C_BYCOPY
  335.          || *type == _C_BYREF
  336.  || *type == _C_ONEWAY
  337.  || *type == _C_GCINVISIBLE)
  338.     {
  339.       type += 1;
  340.     }
  341.   return type;
  342. }
  343.   
  344. /*
  345.   Skip one typespec element.  If the typespec is prepended by type
  346.   qualifiers, these are skipped as well.
  347. */
  348. const char* 
  349. objc_skip_typespec (const char* type)
  350. {
  351.   /* Skip the variable name if any */
  352.   if (*type == '"')
  353.     {
  354.       for (type++; *type++ != '"';)
  355. /* do nothing */;
  356.     }
  357.   type = objc_skip_type_qualifiers (type);
  358.   
  359.   switch (*type) {
  360.   case _C_ID:
  361.     /* An id may be annotated by the actual type if it is known
  362.        with the @"ClassName" syntax */
  363.     if (*++type != '"')
  364.       return type;
  365.     else
  366.       {
  367. while (*++type != '"') /* do nothing */;
  368. return type + 1;
  369.       }
  370.     /* The following are one character type codes */
  371.   case _C_CLASS:
  372.   case _C_SEL:
  373.   case _C_CHR:
  374.   case _C_UCHR:
  375.   case _C_CHARPTR:
  376.   case _C_ATOM:
  377.   case _C_SHT:
  378.   case _C_USHT:
  379.   case _C_INT:
  380.   case _C_UINT:
  381.   case _C_LNG:
  382.   case _C_ULNG:
  383.   case _C_LNG_LNG:
  384.   case _C_ULNG_LNG:
  385.   case _C_FLT:
  386.   case _C_DBL:
  387.   case _C_VOID:
  388.   case _C_UNDEF:
  389.     return ++type;
  390.     break;
  391.   case _C_ARY_B:
  392.     /* skip digits, typespec and closing ']' */
  393.     
  394.     while(isDigit (*++type));
  395.     type = objc_skip_typespec(type);
  396.     if (*type == _C_ARY_E)
  397.       return ++type;
  398.     else
  399.       {
  400. objc_error(nil, OBJC_ERR_BAD_TYPE, "bad array type %sn", type);
  401. return 0;
  402.       }
  403.   case _C_BFLD:
  404.     /* The new encoding of bitfields is: b 'position' 'type' 'size' */
  405.     while (isDigit (*++type)); /* skip position */
  406.     while (isDigit (*++type)); /* skip type and size */
  407.     return type;
  408.   case _C_STRUCT_B:
  409.     /* skip name, and elements until closing '}'  */
  410.     
  411.     while (*type != _C_STRUCT_E && *type++ != '=');
  412.     while (*type != _C_STRUCT_E) { type = objc_skip_typespec (type); }
  413.     return ++type;
  414.   case _C_UNION_B:
  415.     /* skip name, and elements until closing ')'  */
  416.     
  417.     while (*type != _C_UNION_E && *type++ != '=');
  418.     while (*type != _C_UNION_E) { type = objc_skip_typespec (type); }
  419.     return ++type;
  420.   case _C_PTR:
  421.     /* Just skip the following typespec */
  422.     
  423.     return objc_skip_typespec (++type);
  424.     
  425.   default:
  426.     {
  427.       objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %sn", type);
  428.       return 0;
  429.     }
  430.   }
  431. }
  432. /*
  433.   Skip an offset as part of a method encoding.  This is prepended by a
  434.   '+' if the argument is passed in registers.
  435. */
  436. inline const char* 
  437. objc_skip_offset (const char* type)
  438. {
  439.   if (*type == '+') type++;
  440.   while (isDigit(*++type));
  441.   return type;
  442. }
  443. /*
  444.   Skip an argument specification of a method encoding.
  445. */
  446. const char*
  447. objc_skip_argspec (const char* type)
  448. {
  449.   type = objc_skip_typespec (type);
  450.   type = objc_skip_offset (type);
  451.   return type;
  452. }
  453. /*
  454.   Return the number of arguments that the method MTH expects.
  455.   Note that all methods need two implicit arguments `self' and
  456.   `_cmd'. 
  457. */
  458. int
  459. method_get_number_of_arguments (struct objc_method* mth)
  460. {
  461.   int i = 0;
  462.   const char* type = mth->method_types;
  463.   while (*type)
  464.     {
  465.       type = objc_skip_argspec (type);
  466.       i += 1;
  467.     }
  468.   return i - 1;
  469. }
  470. /*
  471.   Return the size of the argument block needed on the stack to invoke
  472.   the method MTH.  This may be zero, if all arguments are passed in
  473.   registers.
  474. */
  475. int
  476. method_get_sizeof_arguments (struct objc_method* mth)
  477. {
  478.   const char* type = objc_skip_typespec (mth->method_types);
  479.   return atoi (type);
  480. }
  481. /*
  482.   Return a pointer to the next argument of ARGFRAME.  type points to
  483.   the last argument.  Typical use of this look like:
  484.   {
  485.     char *datum, *type; 
  486.     for (datum = method_get_first_argument (method, argframe, &type);
  487.          datum; datum = method_get_next_argument (argframe, &type))
  488.       {
  489.         unsigned flags = objc_get_type_qualifiers (type);
  490.         type = objc_skip_type_qualifiers (type);
  491. if (*type != _C_PTR)
  492.           [portal encodeData: datum ofType: type];
  493. else
  494.   {
  495.     if ((flags & _F_IN) == _F_IN)
  496.               [portal encodeData: *(char**)datum ofType: ++type];
  497.   }
  498.       }
  499.   }
  500. */  
  501. char*
  502. method_get_next_argument (arglist_t argframe,
  503.   const char **type)
  504. {
  505.   const char *t = objc_skip_argspec (*type);
  506.   if (*t == '')
  507.     return 0;
  508.   *type = t;
  509.   t = objc_skip_typespec (t);
  510.   if (*t == '+')
  511.     return argframe->arg_regs + atoi (++t);
  512.   else
  513.     return argframe->arg_ptr + atoi (t);
  514. }
  515. /*
  516.   Return a pointer to the value of the first argument of the method 
  517.   described in M with the given argumentframe ARGFRAME.  The type
  518.   is returned in TYPE.  type must be passed to successive calls of 
  519.   method_get_next_argument.
  520. */
  521. char*
  522. method_get_first_argument (struct objc_method* m,
  523.    arglist_t argframe, 
  524.    const char** type)
  525. {
  526.   *type = m->method_types;
  527.   return method_get_next_argument (argframe, type);
  528. }
  529. /*
  530.    Return a pointer to the ARGth argument of the method
  531.    M from the frame ARGFRAME.  The type of the argument
  532.    is returned in the value-result argument TYPE 
  533. */
  534. char*
  535. method_get_nth_argument (struct objc_method* m,
  536.  arglist_t argframe, int arg, 
  537.  const char **type)
  538. {
  539.   const char* t = objc_skip_argspec (m->method_types);
  540.   if (arg > method_get_number_of_arguments (m))
  541.     return 0;
  542.   while (arg--)
  543.     t = objc_skip_argspec (t);
  544.   
  545.   *type = t;
  546.   t = objc_skip_typespec (t);
  547.   if (*t == '+')
  548.     return argframe->arg_regs + atoi (++t);
  549.   else
  550.     return argframe->arg_ptr + atoi (t);
  551. }
  552. unsigned
  553. objc_get_type_qualifiers (const char* type)
  554. {
  555.   unsigned res = 0;
  556.   BOOL flag = YES;
  557.   while (flag)
  558.     switch (*type++)
  559.       {
  560.       case _C_CONST: res |= _F_CONST; break;
  561.       case _C_IN: res |= _F_IN; break;
  562.       case _C_INOUT: res |= _F_INOUT; break;
  563.       case _C_OUT: res |= _F_OUT; break;
  564.       case _C_BYCOPY: res |= _F_BYCOPY; break;
  565.       case _C_BYREF:  res |= _F_BYREF; break;
  566.       case _C_ONEWAY: res |= _F_ONEWAY; break;
  567.       case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break;
  568.       default: flag = NO;
  569.     }
  570.   return res;
  571. }
  572. /* The following three functions can be used to determine how a
  573.    structure is laid out by the compiler. For example:
  574.   struct objc_struct_layout layout;
  575.   int i;
  576.   objc_layout_structure (type, &layout);
  577.   while (objc_layout_structure_next_member (&layout))
  578.     {
  579.       int position, align;
  580.       const char *type;
  581.       objc_layout_structure_get_info (&layout, &position, &align, &type);
  582.       printf ("element %d has offset %d, alignment %dn",
  583.               i++, position, align);
  584.     }
  585.   These functions are used by objc_sizeof_type and objc_alignof_type
  586.   functions to compute the size and alignment of structures. The
  587.   previous method of computing the size and alignment of a structure
  588.   was not working on some architectures, particulary on AIX, and in
  589.   the presence of bitfields inside the structure. */
  590. void
  591. objc_layout_structure (const char *type,
  592.                            struct objc_struct_layout *layout)
  593. {
  594.   const char *ntype;
  595.   if (*type++ != _C_STRUCT_B)
  596.     {
  597.       objc_error(nil, OBJC_ERR_BAD_TYPE,
  598.                  "record type expected in objc_layout_structure, got %sn",
  599.                  type);
  600.     }
  601.   layout->original_type = type;
  602.   /* Skip "<name>=" if any. Avoid embedded structures and unions. */
  603.   ntype = type;
  604.   while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B
  605.          && *ntype++ != '=')
  606.     /* do nothing */;
  607.   /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
  608.   if (*(ntype - 1) == '=')
  609.     type = ntype;
  610.   layout->type = type;
  611.   layout->prev_type = NULL;
  612.   layout->record_size = 0;
  613.   layout->record_align = BITS_PER_UNIT;
  614. #ifdef STRUCTURE_SIZE_BOUNDARY
  615.   layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY);
  616. #endif
  617. }
  618. BOOL
  619. objc_layout_structure_next_member (struct objc_struct_layout *layout)
  620. {
  621.   register int known_align = layout->record_size;
  622.   register int desired_align = 0;
  623.   /* The following are used only if the field is a bitfield */
  624.   register const char *bfld_type;
  625.   register int bfld_type_size, bfld_type_align, bfld_field_size;
  626.   /* The current type without the type qualifiers */
  627.   const char *type;
  628. #if 1
  629.   if (layout->prev_type == NULL)
  630.     {
  631.       layout->prev_type = layout->type;
  632.       layout->type = objc_skip_typespec (layout->prev_type);
  633.       return YES;
  634.     }
  635. #endif
  636.   /* Add the size of the previous field to the size of the record.  */
  637.   if (layout->prev_type)
  638.     {
  639.       type = objc_skip_type_qualifiers (layout->prev_type);
  640.       if (*type != _C_BFLD)
  641.         layout->record_size += objc_sizeof_type (type) * BITS_PER_UNIT;
  642.       else {
  643.         desired_align = 1;
  644.         /* Get the bitfield's type */
  645.         for (bfld_type = type + 1;
  646.              isDigit (*bfld_type);
  647.              bfld_type++)
  648.           /* do nothing */;
  649.         bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT;
  650.         bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
  651.         bfld_field_size = atoi (objc_skip_typespec (bfld_type));
  652.         layout->record_size += bfld_field_size;
  653.       }
  654.     }
  655.   if (*layout->type == _C_STRUCT_E)
  656.     return NO;
  657.   /* Skip the variable name if any */
  658.   if (*layout->type == '"')
  659.     {
  660.       for (layout->type++; *layout->type++ != '"';)
  661.         /* do nothing */;
  662.     }
  663.   type = objc_skip_type_qualifiers (layout->type);
  664.   if (*type != _C_BFLD)
  665.     desired_align = objc_alignof_type(type) * BITS_PER_UNIT;
  666.   else
  667.     {
  668.       desired_align = 1;
  669.       /* Skip the bitfield's offset */
  670.       for (bfld_type = type + 1; isDigit (*bfld_type); bfld_type++)
  671.         /* do nothing */;
  672.       bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT;
  673.       bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
  674.       bfld_field_size = atoi (objc_skip_typespec (bfld_type));
  675.     }
  676. #ifdef BIGGEST_FIELD_ALIGNMENT
  677.   desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
  678. #endif
  679. #ifdef ADJUST_FIELD_ALIGN
  680.   desired_align = ADJUST_FIELD_ALIGN (type, desired_align);
  681. #endif
  682.   /* Record must have at least as much alignment as any field.
  683.      Otherwise, the alignment of the field within the record
  684.      is meaningless.  */
  685. #ifndef PCC_BITFIELD_TYPE_MATTERS
  686.   layout->record_align = MAX (layout->record_align, desired_align);
  687. #else
  688.   if (*type == _C_BFLD)
  689.     {
  690.       /* For these machines, a zero-length field does not
  691.          affect the alignment of the structure as a whole.
  692.          It does, however, affect the alignment of the next field
  693.          within the structure.  */
  694.       if (bfld_field_size)
  695.         layout->record_align = MAX (layout->record_align, desired_align);
  696.       else
  697.         desired_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
  698.       /* A named bit field of declared type `int'
  699.          forces the entire structure to have `int' alignment.
  700.          Q1: How is encoded this thing and how to check for it?
  701.          Q2: How to determine maximum_field_alignment at runtime? */
  702. /*   if (DECL_NAME (field) != 0) */
  703.       {
  704.         int type_align = bfld_type_align;
  705. #if 0
  706.         if (maximum_field_alignment != 0)
  707.           type_align = MIN (type_align, maximum_field_alignment);
  708.         else if (DECL_PACKED (field))
  709.           type_align = MIN (type_align, BITS_PER_UNIT);
  710. #endif
  711.         layout->record_align = MAX (layout->record_align, type_align);
  712.       }
  713.     }
  714.   else
  715.     layout->record_align = MAX (layout->record_align, desired_align);
  716. #endif
  717.   /* Does this field automatically have alignment it needs
  718.      by virtue of the fields that precede it and the record's
  719.      own alignment?  */
  720.   if (*type == _C_BFLD)
  721.     layout->record_size = atoi (type + 1);
  722.   else if (layout->record_size % desired_align != 0)
  723.     {
  724.       /* No, we need to skip space before this field.
  725.          Bump the cumulative size to multiple of field alignment.  */
  726.       layout->record_size = ROUND (layout->record_size, desired_align);
  727.     }
  728.   
  729.   /* Jump to the next field in record. */
  730.   layout->prev_type = layout->type;
  731.   layout->type = objc_skip_typespec (layout->type);      /* skip component */
  732.   return YES;
  733. }
  734. void objc_layout_finish_structure (struct objc_struct_layout *layout,
  735.                                    unsigned int *size,
  736.                                    unsigned int *align)
  737. {
  738.   if (layout->type && *layout->type == _C_STRUCT_E)
  739.     {
  740.       /* Work out the alignment of the record as one expression and store
  741.          in the record type.  Round it up to a multiple of the record's
  742.          alignment. */
  743. #ifdef ROUND_TYPE_ALIGN
  744.       layout->record_align = ROUND_TYPE_ALIGN (layout->original_type,
  745.                                                1,
  746.                                                layout->record_align);
  747. #else
  748.       layout->record_align = MAX (1, layout->record_align);
  749. #endif
  750. #ifdef ROUND_TYPE_SIZE
  751.       layout->record_size = ROUND_TYPE_SIZE (layout->original_type,
  752.                                              layout->record_size,
  753.                                              layout->record_align);
  754. #else
  755.       /* Round the size up to be a multiple of the required alignment */
  756.       layout->record_size = ROUND (layout->record_size, layout->record_align);
  757. #endif
  758.       layout->type = NULL;
  759.     }
  760.   if (size)
  761.     *size = layout->record_size / BITS_PER_UNIT;
  762.   if (align)
  763.     *align = layout->record_align / BITS_PER_UNIT;
  764. }
  765. void objc_layout_structure_get_info (struct objc_struct_layout *layout,
  766.                                      unsigned int *offset,
  767.                                      unsigned int *align,
  768.                                      const char **type)
  769. {
  770.   if (offset)
  771.     *offset = layout->record_size / BITS_PER_UNIT;
  772.   if (align)
  773.     *align = layout->record_align / BITS_PER_UNIT;
  774.   if (type)
  775.     *type = layout->prev_type;
  776. }