📄 encoding.c
字号:
/* Encoding of types for Objective C. Copyright (C) 1993, 1995, 1996, 1997, 1998, 2000, 2002, 2004 Free Software Foundation, Inc. Contributed by Kresten Krab Thorup Bitfield support by Ovidiu PredescuThis file is part of GCC.GCC is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GCC is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GCC; see the file COPYING. If not, write tothe Free Software Foundation, 51 Franklin Street, Fifth Floor,Boston, MA 02110-1301, USA. *//* As a special exception, if you link this library with files compiled with GCC to produce an executable, this does not cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License. *//* FIXME: This file has no business including tm.h. */#include "tconfig.h"#include "coretypes.h"#include "tm.h"#include "objc/objc-api.h"#include "objc/encoding.h"#include <stdlib.h>#undef MAX#define MAX(X, Y) \ ({ typeof (X) __x = (X), __y = (Y); \ (__x > __y ? __x : __y); })#undef MIN#define MIN(X, Y) \ ({ typeof (X) __x = (X), __y = (Y); \ (__x < __y ? __x : __y); })#undef ROUND#define ROUND(V, A) \ ({ typeof (V) __v = (V); typeof (A) __a = (A); \ __a * ((__v+__a - 1)/__a); })/* Various hacks for objc_layout_record. These are used by the target macros. */#define TREE_CODE(TYPE) *(TYPE)#define TREE_TYPE(TREE) (TREE)#define RECORD_TYPE _C_STRUCT_B#define UNION_TYPE _C_UNION_B#define QUAL_UNION_TYPE _C_UNION_B#define ARRAY_TYPE _C_ARY_B#define REAL_TYPE _C_DBL#define VECTOR_TYPE _C_VECTOR#define TYPE_FIELDS(TYPE) objc_skip_typespec (TYPE)#define DECL_MODE(TYPE) *(TYPE)#define TYPE_MODE(TYPE) *(TYPE)#define DFmode _C_DBL#define get_inner_array_type(TYPE) ((TYPE) + 1)/* Some ports (eg ARM) allow the structure size boundary to be selected at compile-time. We override the normal definition with one that has a constant value for this compilation. */#ifndef BITS_PER_UNIT#define BITS_PER_UNIT 8#endif#undef STRUCTURE_SIZE_BOUNDARY#define STRUCTURE_SIZE_BOUNDARY (BITS_PER_UNIT * sizeof (struct{char a;}))/* Some ROUND_TYPE_ALIGN macros use TARGET_foo, and consequently target_flags. Define a dummy entry here to so we don't die. We have to rename it because target_flags may already have been declared extern. */#define target_flags not_target_flagsstatic int __attribute__ ((__unused__)) not_target_flags = 0;/* Some ROUND_TYPE_ALIGN use ALTIVEC_VECTOR_MODE (rs6000 darwin). Define a dummy ALTIVEC_VECTOR_MODE so it will not die. */#undef ALTIVEC_VECTOR_MODE#define ALTIVEC_VECTOR_MODE(MODE) (0)/* FIXME: while this file has no business including tm.h, this definitely has no business defining this macro but it is only way around without really rewritting this file, should look after the branch of 3.4 to fix this. */#define rs6000_special_round_type_align(STRUCT, COMPUTED, SPECIFIED) \ ((TYPE_FIELDS (STRUCT) != 0 \ && DECL_MODE (TYPE_FIELDS (STRUCT)) == DFmode) \ ? MAX (MAX (COMPUTED, SPECIFIED), 64) \ : MAX (COMPUTED, SPECIFIED))/* return the size of an object specified by type*/intobjc_sizeof_type (const char *type){ /* Skip the variable name if any */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } switch (*type) { case _C_ID: return sizeof (id); break; case _C_CLASS: return sizeof (Class); break; case _C_SEL: return sizeof (SEL); break; case _C_CHR: return sizeof (char); break; case _C_UCHR: return sizeof (unsigned char); break; case _C_SHT: return sizeof (short); break; case _C_USHT: return sizeof (unsigned short); break; case _C_INT: return sizeof (int); break; case _C_UINT: return sizeof (unsigned int); break; case _C_LNG: return sizeof (long); break; case _C_ULNG: return sizeof (unsigned long); break; case _C_LNG_LNG: return sizeof (long long); break; case _C_ULNG_LNG: return sizeof (unsigned long long); break; case _C_FLT: return sizeof (float); break; case _C_DBL: return sizeof (double); break; case _C_VOID: return sizeof (void); break; case _C_PTR: case _C_ATOM: case _C_CHARPTR: return sizeof (char *); break; case _C_ARY_B: { int len = atoi (type + 1); while (isdigit ((unsigned char)*++type)) ; return len * objc_aligned_size (type); } break; case _C_BFLD: { /* The new encoding of bitfields is: b 'position' 'type' 'size' */ int position, size; int startByte, endByte; position = atoi (type + 1); while (isdigit ((unsigned char)*++type)) ; size = atoi (type + 1); startByte = position / BITS_PER_UNIT; endByte = (position + size) / BITS_PER_UNIT; return endByte - startByte; } case _C_STRUCT_B: { struct objc_struct_layout layout; unsigned int size; objc_layout_structure (type, &layout); while (objc_layout_structure_next_member (&layout)) /* do nothing */ ; objc_layout_finish_structure (&layout, &size, NULL); return size; } case _C_UNION_B: { int max_size = 0; while (*type != _C_UNION_E && *type++ != '=') /* do nothing */; while (*type != _C_UNION_E) { /* Skip the variable name if any */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } max_size = MAX (max_size, objc_sizeof_type (type)); type = objc_skip_typespec (type); } return max_size; } default: { objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type); return 0; } }}/* Return the alignment of an object specified by type*/intobjc_alignof_type (const char *type){ /* Skip the variable name if any */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } switch (*type) { case _C_ID: return __alignof__ (id); break; case _C_CLASS: return __alignof__ (Class); break; case _C_SEL: return __alignof__ (SEL); break; case _C_CHR: return __alignof__ (char); break; case _C_UCHR: return __alignof__ (unsigned char); break; case _C_SHT: return __alignof__ (short); break; case _C_USHT: return __alignof__ (unsigned short); break; case _C_INT: return __alignof__ (int); break; case _C_UINT: return __alignof__ (unsigned int); break; case _C_LNG: return __alignof__ (long); break; case _C_ULNG: return __alignof__ (unsigned long); break; case _C_LNG_LNG: return __alignof__ (long long); break; case _C_ULNG_LNG: return __alignof__ (unsigned long long); break; case _C_FLT: return __alignof__ (float); break; case _C_DBL: return __alignof__ (double); break; case _C_PTR: case _C_ATOM: case _C_CHARPTR: return __alignof__ (char *); break; case _C_ARY_B: while (isdigit ((unsigned char)*++type)) /* do nothing */; return objc_alignof_type (type); case _C_STRUCT_B: { struct objc_struct_layout layout; unsigned int align; objc_layout_structure (type, &layout); while (objc_layout_structure_next_member (&layout)) /* do nothing */; objc_layout_finish_structure (&layout, NULL, &align); return align; } case _C_UNION_B: { int maxalign = 0; while (*type != _C_UNION_E && *type++ != '=') /* do nothing */; while (*type != _C_UNION_E) { /* Skip the variable name if any */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } maxalign = MAX (maxalign, objc_alignof_type (type)); type = objc_skip_typespec (type); } return maxalign; } default: { objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type); return 0; } }}/* The aligned size if the size rounded up to the nearest alignment.*/intobjc_aligned_size (const char *type){ int size, align; /* Skip the variable name */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } size = objc_sizeof_type (type); align = objc_alignof_type (type); return ROUND (size, align);}/* The size rounded up to the nearest integral of the wordsize, taken to be the size of a void *.*/intobjc_promoted_size (const char *type){ int size, wordsize; /* Skip the variable name */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } size = objc_sizeof_type (type); wordsize = sizeof (void *); return ROUND (size, wordsize);}/* Skip type qualifiers. These may eventually precede typespecs occurring in method prototype encodings.*/inline const char *objc_skip_type_qualifiers (const char *type){ while (*type == _C_CONST || *type == _C_IN || *type == _C_INOUT || *type == _C_OUT || *type == _C_BYCOPY || *type == _C_BYREF || *type == _C_ONEWAY || *type == _C_GCINVISIBLE) { type += 1; } return type;}/* Skip one typespec element. If the typespec is prepended by type qualifiers, these are skipped as well.*/const char *objc_skip_typespec (const char *type){ /* Skip the variable name if any */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } type = objc_skip_type_qualifiers (type); switch (*type) { case _C_ID: /* An id may be annotated by the actual type if it is known with the @"ClassName" syntax */ if (*++type != '"') return type; else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -