1 /* 2 * Copyright (c) 2003 - 2009, Nils R. Weller 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #ifndef TYPE_H 28 #define TYPE_H 29 30 31 struct decl; 32 struct type; 33 struct vreg; 34 struct token; 35 struct stack_block; 36 struct function; 37 struct initializer; 38 39 #include <stddef.h> 40 41 #ifndef PREPROCESSOR 42 # include "features.h" 43 #endif 44 45 /* For structure definitions */ 46 struct ty_struct { 47 char *tag; 48 char *dummytag; 49 struct scope *scope; 50 struct scope *parentscope; 51 52 /* number of members */ 53 int nmemb; 54 55 /* e.g. __attribute__((packed)) */ 56 struct attrib *attrib; 57 int unnamed; 58 int incomplete; 59 int printed; 60 int is_union; 61 int alignment; 62 unsigned int references; 63 size_t size; 64 struct decl *flexible; 65 struct ty_struct *next; 66 }; 67 68 69 /* 70 * An array of ty_bit in ``struct type'' is used to describe a bitfield 71 */ 72 struct ty_bit { 73 char *name; 74 int numbits; 75 76 /* 77 * 09/04/08: New offsets for the new bitfield storage layout. 78 * byte_offset indicates the offset from the surrounding 79 * bitfield storage unit 80 * bit_offset indicates the bitfield offset in the first byte 81 * which carries this bitfield 82 * 83 * (I.e. base offset*8 + byte_offset*8 + bit_offset gets us 84 * to the start of the bitfield) 85 */ 86 int byte_offset; 87 int bit_offset; 88 89 /* 90 * Absolute offset within struct. Needed to calculate offset in 91 * partial storage unit easily (e.g. a 3 byte partial storage 92 * unit starting at byte 1) 93 */ 94 int absolute_byte_offset; 95 96 /* 97 * 10/02/08: Base storage unit declaration. The offset within 98 * this storage unit is the difference between byte/bit_offset 99 * and bitfield_storage_unit->offset. 100 * 101 * Note that this unit has type ``array of N chars'', but it 102 * need not be a full storage unit because it does NOT 103 * include non-bitfield members! E.g. in 104 * 105 * struct foo { char x; int bf:24; }; 106 * 107 * ... the bf storage unit declaration is of type char[3] and 108 * begins after x. This storage unit chopping is done to 109 * simplify initializer handling. 110 */ 111 struct decl *bitfield_storage_unit; 112 /* 113 * 09/08/08: Number of shift bits needed to encode or decode 114 * a bitfield member to/from a bitfield storage unit 115 * !!!!!!!!! DANGER!!!!!!!!!!!!!!!!!! DO NOT CHANGE FROM INT 116 * TO OTHER TYPE! (Passed to const_from_value()) 117 */ 118 int shiftbits; 119 /* 120 * Bitmask token which covers the range of this bitfield. NOTE 121 * that this doesn't take the bitfield location in its storage 122 * unit into account! So if we have a bitfield with 3 bits, 123 * then the bitmask token will be 0x7, REGARDLESS of whether 124 * the bitfield is stored in the high or low bits of a storage 125 * unit 126 */ 127 struct token *bitmask_tok; 128 struct token *bitmask_tok_with_shiftbits; 129 /* 130 * Inverted bitmask token. Unlike bitmask_tok, this DOES take 131 * the storage unit into account. If a bitfield with 3 bits 132 * is stored in the upper 4 bits of a byte, then the inverted 133 * bitmask is 0xf, i.e. it covers the LOWER 4 bits (and by 134 * ANDing the byte with the mask, the bitfield part is set to 0) 135 */ 136 struct token *bitmask_inv_tok; 137 struct token *shifttok; 138 struct token *shifttok_signext_left; 139 struct token *shifttok_signext_right; 140 141 }; 142 143 /* 144 * Data structure to store string literal information. This is not 145 * used by ``struct type'', but only by the ``struct token'' data 146 * member, initializers, etc 147 */ 148 struct ty_string { 149 char *str; /* The string literal */ 150 unsigned long count; /* .str%lu the `%lu' in asm output */ 151 size_t size; /* Size of array (including null) */ 152 struct type *ty; 153 struct ty_string *next; 154 }; 155 156 struct num; 157 158 struct ty_float { 159 struct num *num; 160 unsigned long count; 161 struct ty_float *next; 162 struct ty_float *prev; 163 164 /* 165 * 02/19/08: New flag which means ``this constant is no longer 166 * needed''. In particular, the point is to avoid printing 167 * float constants that were used in constant (initializer) 168 * expressions, since those are not accessed in the asm code 169 */ 170 int inactive; 171 }; 172 173 struct ty_llong { 174 struct num *num; 175 unsigned long count; 176 int loaded; 177 struct ty_llong *next; 178 }; 179 180 /* For enum definitions */ 181 struct ty_enum { 182 char *tag; 183 struct { 184 char *name; 185 struct decl *dec; 186 struct token *value; 187 } *members; 188 int nmemb; 189 }; 190 191 /* For function declarations */ 192 #define FDTYPE_KR 1 193 #define FDTYPE_ISO 2 194 195 struct ty_func { 196 char *name; 197 struct scope *scope; 198 struct decl *lastarg; 199 int nargs; /* number of arguments */ 200 int was_just_declared; 201 struct type *ret; /* return type */ 202 203 /* 204 * 04/11/08: Paramter name list for K&R functions. This is 205 * just used to resolve the parameter types as soon as 206 * those are encountered, and can be ignored (deleted) 207 * afterwards 208 */ 209 struct ntab *ntab; 210 char *asmname; /* assembler name */ 211 int type; /* K&R or ISO declaration */ 212 int variadic; /* Variadic function? */ 213 }; 214 215 216 #define TN_ARRAY_OF (1) 217 #define TN_POINTER_TO (1 << 1) 218 #define TN_FUNCTION (1 << 2) 219 #define TN_VARARRAY_OF (1 << 3) 220 221 222 /* 223 * This structure describes the properties of a declarator, i.e. 224 * ``pointer-to'', ``array-of-N'', ``function pointer'', etc 225 */ 226 struct type_node { 227 /* Designates array of/pointer to/function */ 228 int type; 229 230 /* If this is an array, arrayarg specifies the number of elements */ 231 #if REMOVE_ARRARG 232 int have_array_size; 233 struct expr *variable_arrarg; 234 #else 235 struct expr *arrarg; 236 #endif 237 /* 238 * Number of elements for string constants, and after parsing 239 * arrays with constant size 240 */ 241 size_t arrarg_const; 242 243 /* 244 * If this is a pointer, ptrarg specifies whether the pointer is 245 * qualified (TOK_KEY_VOLATILE/CONST/RESTRICT) or not (0) 246 */ 247 int ptrarg; 248 249 /* 250 * If this is a function, tfunc specifies its arguments. 251 * This is a null pointer in case of ``T f(void)'' 252 */ 253 struct ty_func *tfunc; 254 255 /* Next node in list */ 256 struct type_node *next; 257 258 /* Previous node in list */ 259 struct type_node *prev; 260 }; 261 262 263 struct type { 264 int line; /* line in source file */ 265 char *file; /* name of source file */ 266 char *name; 267 int is_func; /* is function declaration? */ 268 int is_def; /* is struct/func definition? */ 269 270 /* Structure/union/enum-specific data */ 271 int incomplete; /* is incomplete */ 272 struct ty_struct *tstruc; 273 struct ty_enum *tenum; 274 275 /* 276 * tbit is used for bitfield members. A bitfield is designated by 277 * a struct member of type (u)int/bool with tbit != NULL. That is, 278 * struct ty_struc *t = lookup_struct("tag", SCOPE_NESTED); 279 * if (t != NULL) { 280 * struct type *ty = t->members[0].dtype; 281 * if (ty->code == TY_INT || 282 * ty->code == TY_UINT || 283 * ty->code == TY_BOOL) { 284 * if (ty->tbit != NULL) { 285 * fist member is bitfield! 286 * } 287 * } 288 * } 289 * The same applies to unions 290 */ 291 struct ty_bit *tbit; 292 293 /* General data */ 294 int code; /* integer, double, etc */ 295 int implicit;/* implicit declaration? */ 296 #define IMPLICIT_FDECL (1) 297 #define IMPLICIT_INT (1 << 1) 298 int sign; /* signed or unsigned */ 299 int storage; /* storage duration */ 300 301 #if 0 302 int is_const; /* const? */ 303 int is_volatile; /* volatile? */ 304 int is_restrict; /* restrict? */ 305 int is_inline; /* inline? */ 306 int is_vla; /* VLA? (any component) */ 307 #endif 308 309 int flags; 310 #define FLAGS_CONST (1) 311 #define FLAGS_VOLATILE (1 << 1) 312 #define FLAGS_RESTRICT (1 << 2) 313 #define FLAGS_INLINE (1 << 3) 314 #define FLAGS_VLA (1 << 4) 315 #define FLAGS_THREAD (1 << 5) 316 #define FLAGS_FUNCNAME (1 << 6) 317 #define FLAGS_EXTERN_INLINE (1 << 7) 318 /* 319 * 02/10/09: Indicates that the variable was renamed using __asm__(). 320 * This requires a literal interpretation of the name on OSX 321 */ 322 #define FLAGS_ASM_RENAMED (1 << 8) 323 324 #define IS_CONST(flags) (flags & FLAGS_CONST) 325 #define IS_VOLATILE(flags) (flags & FLAGS_VOLATILE) 326 #define IS_RESTRICT(flags) (flags & FLAGS_RESTRICT) 327 #define IS_INLINE(flags) (flags & FLAGS_INLINE) 328 #define IS_VLA(flags) (flags & FLAGS_VLA) 329 #define IS_THREAD(flags) (flags & FLAGS_THREAD) 330 #define IS_FUNCNAME(flags) (flags & FLAGS_FUNCNAME) 331 #define IS_EXTERN_INLINE(flags) (flags & FLAGS_EXTERN_INLINE) 332 #define IS_ASM_RENAMED(flags) (flags & FLAGS_ASM_RENAMED) 333 334 struct stack_block *vla_addr; 335 336 struct attrib *attributes; /* GNU C __attribute__()s */ 337 struct attrib *attributes_tail; 338 unsigned int fastattr; /* quick attribute check flags */ 339 340 /* 341 * Declarator list. This is not used in 342 * structure/... definitions! 343 */ 344 struct type_node *tlist; 345 346 struct type_node *tlist_tail; 347 348 }; 349 350 351 #define CMPTY_SIGN 1 352 #define CMPTY_CONST (1 << 2) 353 #define CMPTY_ALL (CMPTY_SIGN|CMPTY_CONST) 354 #define CMPTY_ARRAYPTR (1 << 3) 355 #define CMPTY_TENTDEC (1 << 4) 356 357 struct ty_struct *alloc_ty_struct(void); 358 struct ty_llong *alloc_ty_llong(void); 359 struct ty_string *alloc_ty_string(void); 360 struct ty_string *make_ty_string(const char *, size_t); 361 unsigned long ty_string_count(void); 362 struct ty_bit *alloc_ty_bit(void); 363 struct ty_enum *alloc_ty_enum(void); 364 struct ty_func *alloc_ty_func(void); 365 struct type_node *alloc_type_node(void); 366 struct type *alloc_type(void); 367 int compare_types(struct type *dest, struct type *src, int flag); 368 int check_init_type(struct type *dest, struct expr *init); 369 void copy_type(struct type *dest, const struct type *src, int full); 370 struct type_node * 371 copy_tlist(struct type_node **dest, const struct type_node *tlist); 372 struct type *make_basic_type(int code); 373 struct type *make_void_ptr_type(void); 374 struct type *make_array_type(int size); 375 void append_typelist(struct type *t, 376 int type, void *type_arg, struct ty_func *tf, 377 struct token *tok); 378 379 char *type_to_text(struct type *t); 380 int is_arithmetic_type(struct type *t); 381 int is_scalar_type(struct type *t); 382 int is_floating_type(struct type *t); 383 int is_array_type(struct type *t); 384 int is_integral_type(struct type *t); 385 int is_basic_agg_type(struct type *t); 386 int is_arr_of_ptr(struct type *t); 387 int is_nullptr_const(struct token *, struct type *); 388 int type_without_sign(int code); 389 struct token *const_from_type(struct type *ty, int from_alignment, 390 int extype, struct token *tok); 391 struct token *fp_const_from_ascii(const char *asc, int type); 392 struct token *const_from_value(void *value, struct type *ty); 393 int check_types_assign( 394 struct token *t, 395 struct type *left, 396 struct vreg *right, 397 int to_const_ok, 398 int silent); 399 struct type *addrofify_type(struct type *t); 400 void functype_to_rettype(struct type *t); 401 struct type *dup_type(struct type *t); 402 403 int is_transparent_union(struct type *); 404 struct type *get_transparent_union_type( 405 struct token *, 406 struct type *left, struct vreg *right); 407 void init_to_array_size(struct type *ty, 408 struct initializer *init); 409 int func_returns_void(struct function *); 410 int is_modifyable(struct type *ty); 411 struct type *func_to_return_type(struct type *); 412 void ppcify_constant(struct token *tok); 413 414 #endif 415 416