Print this page
new smatch


  16  *
  17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23  * THE SOFTWARE.
  24  */
  25 #include <stdlib.h>
  26 #include <stdio.h>
  27 #include <string.h>
  28 
  29 #include "lib.h"
  30 #include "allocate.h"
  31 #include "token.h"
  32 #include "parse.h"
  33 #include "symbol.h"
  34 #include "scope.h"
  35 #include "expression.h"

  36 
  37 #include "target.h"
  38 
  39 /*
  40  * Secondary symbol list for stuff that needs to be output because it
  41  * was used. 
  42  */
  43 struct symbol_list *translation_unit_used_list = NULL;
  44 
  45 /*
  46  * If the symbol is an inline symbol, add it to the list of symbols to parse
  47  */
  48 void access_symbol(struct symbol *sym)
  49 {
  50         if (sym->ctype.modifiers & MOD_INLINE) {
  51                 if (!(sym->ctype.modifiers & MOD_ACCESSED)) {
  52                         add_symbol(&translation_unit_used_list, sym);
  53                         sym->ctype.modifiers |= MOD_ACCESSED;
  54                 }
  55         }
  56 }
  57 
  58 struct symbol *lookup_symbol(struct ident *ident, enum namespace ns)
  59 {
  60         struct symbol *sym;
  61 
  62         for (sym = ident->symbols; sym; sym = sym->next_id) {
  63                 if (sym->namespace & ns) {
  64                         sym->used = 1;
  65                         return sym;
  66                 }
  67         }
  68         return NULL;
  69 }
  70 
  71 struct context *alloc_context(void)
  72 {
  73         return __alloc_context(0);


 196 
 197         if (!sym->ctype.alignment)
 198                 sym->ctype.alignment = info.max_align;
 199         bit_size = info.bit_size;
 200         if (info.align_size) {
 201                 bit_align = bytes_to_bits(sym->ctype.alignment)-1;
 202                 bit_size = (bit_size + bit_align) & ~bit_align;
 203         }
 204         sym->bit_size = bit_size;
 205         return sym;
 206 }
 207 
 208 static struct symbol *examine_base_type(struct symbol *sym)
 209 {
 210         struct symbol *base_type;
 211 
 212         /* Check the base type */
 213         base_type = examine_symbol_type(sym->ctype.base_type);
 214         if (!base_type || base_type->type == SYM_PTR)
 215                 return base_type;
 216         sym->ctype.as |= base_type->ctype.as;
 217         sym->ctype.modifiers |= base_type->ctype.modifiers & MOD_PTRINHERIT;
 218         concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
 219                         (struct ptr_list **)&sym->ctype.contexts);
 220         if (base_type->type == SYM_NODE) {
 221                 base_type = base_type->ctype.base_type;
 222                 sym->ctype.base_type = base_type;
 223         }
 224         return base_type;
 225 }
 226 
 227 static struct symbol * examine_array_type(struct symbol *sym)
 228 {
 229         struct symbol *base_type = examine_base_type(sym);
 230         unsigned long bit_size = -1, alignment;
 231         struct expression *array_size = sym->array_size;
 232 
 233         if (!base_type)
 234                 return sym;
 235 
 236         if (array_size) {       


 260         if (sym->bit_size > bit_size)
 261                 warning(sym->pos, "impossible field-width, %d, for this type",  sym->bit_size);
 262 
 263         alignment = base_type->ctype.alignment;
 264         if (!sym->ctype.alignment)
 265                 sym->ctype.alignment = alignment;
 266         modifiers = base_type->ctype.modifiers;
 267 
 268         /* Bitfields are unsigned, unless the base type was explicitly signed */
 269         if (!(modifiers & MOD_EXPLICITLY_SIGNED))
 270                 modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
 271         sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
 272         return sym;
 273 }
 274 
 275 /*
 276  * "typeof" will have to merge the types together
 277  */
 278 void merge_type(struct symbol *sym, struct symbol *base_type)
 279 {
 280         sym->ctype.as |= base_type->ctype.as;
 281         sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
 282         concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
 283                         (struct ptr_list **)&sym->ctype.contexts);
 284         sym->ctype.base_type = base_type->ctype.base_type;
 285         if (sym->ctype.base_type->type == SYM_NODE)
 286                 merge_type(sym, sym->ctype.base_type);
 287 }
 288 
 289 static int count_array_initializer(struct symbol *t, struct expression *expr)
 290 {
 291         int nr = 0;
 292         int is_char = 0;
 293 
 294         /*
 295          * Arrays of character types are special; they can be initialized by
 296          * string literal _or_ by string literal in braces.  The latter means
 297          * that with T x[] = {<string literal>} number of elements in x depends
 298          * on T - if it's a character type, we get the length of string literal
 299          * (including NUL), otherwise we have one element here.
 300          */


 347                                 if (is_char)
 348                                         nr = expr->string->length;
 349                         }
 350                 }
 351                 break;
 352         default:
 353                 break;
 354         }
 355         return nr;
 356 }
 357 
 358 static struct expression *get_symbol_initializer(struct symbol *sym)
 359 {
 360         do {
 361                 if (sym->initializer)
 362                         return sym->initializer;
 363         } while ((sym = sym->same_symbol) != NULL);
 364         return NULL;
 365 }
 366 

















 367 static struct symbol * examine_node_type(struct symbol *sym)
 368 {
 369         struct symbol *base_type = examine_base_type(sym);
 370         int bit_size;
 371         unsigned long alignment;
 372 
 373         /* SYM_NODE - figure out what the type of the node was.. */
 374         bit_size = 0;
 375         alignment = 0;
 376         if (!base_type)
 377                 return sym;
 378 
 379         bit_size = base_type->bit_size;
 380         alignment = base_type->ctype.alignment;
 381 
 382         /* Pick up signedness information into the node */
 383         sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
 384 
 385         if (!sym->ctype.alignment)
 386                 sym->ctype.alignment = alignment;
 387 
 388         /* Unsized array? The size might come from the initializer.. */
 389         if (bit_size < 0 && base_type->type == SYM_ARRAY) {
 390                 struct expression *initializer = get_symbol_initializer(sym);
 391                 if (initializer) {
 392                         struct symbol *node_type = base_type->ctype.base_type;
 393                         int count = count_array_initializer(node_type, initializer);
 394 
 395                         if (node_type && node_type->bit_size >= 0)
 396                                 bit_size = array_element_offset(node_type->bit_size, count);
 397                 }
 398         }
 399         
 400         sym->bit_size = bit_size;
 401         return sym;
 402 }
 403 
 404 static struct symbol *examine_enum_type(struct symbol *sym)
 405 {
 406         struct symbol *base_type = examine_base_type(sym);
 407 
 408         sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
 409         sym->bit_size = bits_in_enum;
 410         if (base_type->bit_size > sym->bit_size)
 411                 sym->bit_size = base_type->bit_size;
 412         sym->ctype.alignment = enum_alignment;
 413         if (base_type->ctype.alignment > sym->ctype.alignment)
 414                 sym->ctype.alignment = base_type->ctype.alignment;
 415         return sym;
 416 }


 462                 return examine_bitfield_type(sym);
 463         case SYM_BASETYPE:
 464                 /* Size and alignment had better already be set up */
 465                 return sym;
 466         case SYM_TYPEOF: {
 467                 struct symbol *base = evaluate_expression(sym->initializer);
 468                 if (base) {
 469                         unsigned long mod = 0;
 470 
 471                         if (is_bitfield_type(base))
 472                                 warning(base->pos, "typeof applied to bitfield type");
 473                         if (base->type == SYM_NODE) {
 474                                 mod |= base->ctype.modifiers & MOD_TYPEOF;
 475                                 base = base->ctype.base_type;
 476                         }
 477                         sym->type = SYM_NODE;
 478                         sym->ctype.modifiers = mod;
 479                         sym->ctype.base_type = base;
 480                         return examine_node_type(sym);
 481                 }
 482                 break;


 483         }
 484         case SYM_PREPROCESSOR:
 485                 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
 486                 return NULL;
 487         case SYM_UNINITIALIZED:
 488 //              sparse_error(sym->pos, "ctype on uninitialized symbol %p", sym);
 489                 return NULL;
 490         case SYM_RESTRICT:
 491                 examine_base_type(sym);
 492                 return sym;
 493         case SYM_FOULED:
 494                 examine_base_type(sym);
 495                 return sym;
 496         default:
 497 //              sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
 498                 break;
 499         }
 500         return sym;
 501 }
 502 
 503 const char* get_type_name(enum type type)
 504 {
 505         const char *type_lookup[] = {
 506         [SYM_UNINITIALIZED] = "uninitialized",
 507         [SYM_PREPROCESSOR] = "preprocessor",
 508         [SYM_BASETYPE] = "basetype",


 660  * Abstract types
 661  */
 662 struct symbol   int_type,
 663                 fp_type;
 664 
 665 /*
 666  * C types (i.e. actual instances that the abstract types
 667  * can map onto)
 668  */
 669 struct symbol   bool_ctype, void_ctype, type_ctype,
 670                 char_ctype, schar_ctype, uchar_ctype,
 671                 short_ctype, sshort_ctype, ushort_ctype,
 672                 int_ctype, sint_ctype, uint_ctype,
 673                 long_ctype, slong_ctype, ulong_ctype,
 674                 llong_ctype, sllong_ctype, ullong_ctype,
 675                 lllong_ctype, slllong_ctype, ulllong_ctype,
 676                 float_ctype, double_ctype, ldouble_ctype,
 677                 string_ctype, ptr_ctype, lazy_ptr_ctype,
 678                 incomplete_ctype, label_ctype, bad_ctype,
 679                 null_ctype;








 680 
 681 struct symbol   zero_int;
 682 
 683 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
 684 #define __IDENT(n,str,res) \
 685         struct ident n  = __INIT_IDENT(str,res)
 686 
 687 #include "ident-list.h"
 688 
 689 void init_symbols(void)
 690 {
 691         int stream = init_stream("builtin", -1, includepath);
 692 
 693 #define __IDENT(n,str,res) \
 694         hash_ident(&n)
 695 #include "ident-list.h"
 696 
 697         init_parser(stream);
 698         init_builtins(stream);
 699 }
 700 
 701 #ifdef __CHAR_UNSIGNED__
 702 #define CHAR_SIGNEDNESS MOD_UNSIGNED
 703 #else
 704 #define CHAR_SIGNEDNESS MOD_SIGNED
 705 #endif




 706 
 707 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
 708 #define MOD_LL (MOD_LONG | MOD_LONGLONG)
 709 #define MOD_LLL MOD_LONGLONGLONG
 710 static const struct ctype_declare {
 711         struct symbol *ptr;
 712         enum type type;
 713         unsigned long modifiers;
 714         int *bit_size;
 715         int *maxalign;
 716         struct symbol *base_type;
 717 } ctype_declaration[] = {
 718         { &bool_ctype,          SYM_BASETYPE, MOD_UNSIGNED,             &bits_in_bool,       &max_int_alignment, &int_type },
 719         { &void_ctype,          SYM_BASETYPE, 0,                        NULL,            NULL,               NULL },
 720         { &type_ctype,          SYM_BASETYPE, MOD_TYPE,                 NULL,                    NULL,               NULL },
 721         { &incomplete_ctype,SYM_BASETYPE, 0,                            NULL,                    NULL,               NULL },
 722         { &bad_ctype,           SYM_BASETYPE, 0,                        NULL,                    NULL,               NULL },
 723 
 724         { &char_ctype,          SYM_BASETYPE, CHAR_SIGNEDNESS | MOD_CHAR,    &bits_in_char,          &max_int_alignment, &int_type },
 725         { &schar_ctype,         SYM_BASETYPE, MOD_ESIGNED | MOD_CHAR,   &bits_in_char,       &max_int_alignment, &int_type },


 727         { &short_ctype,         SYM_BASETYPE, MOD_SIGNED | MOD_SHORT,   &bits_in_short,      &max_int_alignment, &int_type },
 728         { &sshort_ctype,    SYM_BASETYPE, MOD_ESIGNED | MOD_SHORT,  &bits_in_short,          &max_int_alignment, &int_type },
 729         { &ushort_ctype,    SYM_BASETYPE, MOD_UNSIGNED | MOD_SHORT, &bits_in_short,          &max_int_alignment, &int_type },
 730         { &int_ctype,           SYM_BASETYPE, MOD_SIGNED,               &bits_in_int,        &max_int_alignment, &int_type },
 731         { &sint_ctype,          SYM_BASETYPE, MOD_ESIGNED,              &bits_in_int,        &max_int_alignment, &int_type },
 732         { &uint_ctype,          SYM_BASETYPE, MOD_UNSIGNED,             &bits_in_int,        &max_int_alignment, &int_type },
 733         { &long_ctype,          SYM_BASETYPE, MOD_SIGNED | MOD_LONG,    &bits_in_long,       &max_int_alignment, &int_type },
 734         { &slong_ctype,         SYM_BASETYPE, MOD_ESIGNED | MOD_LONG,   &bits_in_long,       &max_int_alignment, &int_type },
 735         { &ulong_ctype,         SYM_BASETYPE, MOD_UNSIGNED | MOD_LONG,  &bits_in_long,       &max_int_alignment, &int_type },
 736         { &llong_ctype,         SYM_BASETYPE, MOD_SIGNED | MOD_LL,      &bits_in_longlong,       &max_int_alignment, &int_type },
 737         { &sllong_ctype,    SYM_BASETYPE, MOD_ESIGNED | MOD_LL,         &bits_in_longlong,       &max_int_alignment, &int_type },
 738         { &ullong_ctype,    SYM_BASETYPE, MOD_UNSIGNED | MOD_LL,    &bits_in_longlong,       &max_int_alignment, &int_type },
 739         { &lllong_ctype,    SYM_BASETYPE, MOD_SIGNED | MOD_LLL,         &bits_in_longlonglong,   &max_int_alignment, &int_type },
 740         { &slllong_ctype,   SYM_BASETYPE, MOD_ESIGNED | MOD_LLL,    &bits_in_longlonglong,   &max_int_alignment, &int_type },
 741         { &ulllong_ctype,   SYM_BASETYPE, MOD_UNSIGNED | MOD_LLL,   &bits_in_longlonglong,   &max_int_alignment, &int_type },
 742 
 743         { &float_ctype,         SYM_BASETYPE,  0,                       &bits_in_float,          &max_fp_alignment,  &fp_type },
 744         { &double_ctype,    SYM_BASETYPE, MOD_LONG,             &bits_in_double,         &max_fp_alignment,  &fp_type },
 745         { &ldouble_ctype,   SYM_BASETYPE, MOD_LONG | MOD_LONGLONG,  &bits_in_longdouble,     &max_fp_alignment,  &fp_type },
 746 






 747         { &string_ctype,    SYM_PTR,          0,                        &bits_in_pointer,        &pointer_alignment, &char_ctype },
 748         { &ptr_ctype,           SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &void_ctype },
 749         { &null_ctype,          SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &void_ctype },
 750         { &label_ctype,         SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &void_ctype },
 751         { &lazy_ptr_ctype,  SYM_PTR,          0,                        &bits_in_pointer,        &pointer_alignment, &void_ctype },











 752         { NULL, }
 753 };
 754 #undef MOD_LLL
 755 #undef MOD_LL
 756 #undef MOD_ESIGNED
 757 
 758 void init_ctype(void)
 759 {
 760         const struct ctype_declare *ctype;
 761 
 762         for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
 763                 struct symbol *sym = ctype->ptr;
 764                 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
 765                 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
 766                 unsigned long alignment = bits_to_bytes(bit_size);
 767 
 768                 if (alignment > maxalign)
 769                         alignment = maxalign;
 770                 sym->type = ctype->type;
 771                 sym->bit_size = bit_size;
 772                 sym->ctype.alignment = alignment;
 773                 sym->ctype.base_type = ctype->base_type;
 774                 sym->ctype.modifiers = ctype->modifiers;
 775         }






 776 }


  16  *
  17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23  * THE SOFTWARE.
  24  */
  25 #include <stdlib.h>
  26 #include <stdio.h>
  27 #include <string.h>
  28 
  29 #include "lib.h"
  30 #include "allocate.h"
  31 #include "token.h"
  32 #include "parse.h"
  33 #include "symbol.h"
  34 #include "scope.h"
  35 #include "expression.h"
  36 #include "evaluate.h"
  37 
  38 #include "target.h"
  39 
  40 /*
  41  * Secondary symbol list for stuff that needs to be output because it
  42  * was used. 
  43  */
  44 struct symbol_list *translation_unit_used_list = NULL;
  45 
  46 /*
  47  * If the symbol is an inline symbol, add it to the list of symbols to parse
  48  */
  49 void access_symbol(struct symbol *sym)
  50 {
  51         if (sym->ctype.modifiers & MOD_INLINE) {
  52                 if (!sym->accessed) {
  53                         add_symbol(&translation_unit_used_list, sym);
  54                         sym->accessed = 1;
  55                 }
  56         }
  57 }
  58 
  59 struct symbol *lookup_symbol(struct ident *ident, enum namespace ns)
  60 {
  61         struct symbol *sym;
  62 
  63         for (sym = ident->symbols; sym; sym = sym->next_id) {
  64                 if (sym->namespace & ns) {
  65                         sym->used = 1;
  66                         return sym;
  67                 }
  68         }
  69         return NULL;
  70 }
  71 
  72 struct context *alloc_context(void)
  73 {
  74         return __alloc_context(0);


 197 
 198         if (!sym->ctype.alignment)
 199                 sym->ctype.alignment = info.max_align;
 200         bit_size = info.bit_size;
 201         if (info.align_size) {
 202                 bit_align = bytes_to_bits(sym->ctype.alignment)-1;
 203                 bit_size = (bit_size + bit_align) & ~bit_align;
 204         }
 205         sym->bit_size = bit_size;
 206         return sym;
 207 }
 208 
 209 static struct symbol *examine_base_type(struct symbol *sym)
 210 {
 211         struct symbol *base_type;
 212 
 213         /* Check the base type */
 214         base_type = examine_symbol_type(sym->ctype.base_type);
 215         if (!base_type || base_type->type == SYM_PTR)
 216                 return base_type;
 217         combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
 218         sym->ctype.modifiers |= base_type->ctype.modifiers & MOD_PTRINHERIT;
 219         concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
 220                         (struct ptr_list **)&sym->ctype.contexts);
 221         if (base_type->type == SYM_NODE) {
 222                 base_type = base_type->ctype.base_type;
 223                 sym->ctype.base_type = base_type;
 224         }
 225         return base_type;
 226 }
 227 
 228 static struct symbol * examine_array_type(struct symbol *sym)
 229 {
 230         struct symbol *base_type = examine_base_type(sym);
 231         unsigned long bit_size = -1, alignment;
 232         struct expression *array_size = sym->array_size;
 233 
 234         if (!base_type)
 235                 return sym;
 236 
 237         if (array_size) {       


 261         if (sym->bit_size > bit_size)
 262                 warning(sym->pos, "impossible field-width, %d, for this type",  sym->bit_size);
 263 
 264         alignment = base_type->ctype.alignment;
 265         if (!sym->ctype.alignment)
 266                 sym->ctype.alignment = alignment;
 267         modifiers = base_type->ctype.modifiers;
 268 
 269         /* Bitfields are unsigned, unless the base type was explicitly signed */
 270         if (!(modifiers & MOD_EXPLICITLY_SIGNED))
 271                 modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
 272         sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
 273         return sym;
 274 }
 275 
 276 /*
 277  * "typeof" will have to merge the types together
 278  */
 279 void merge_type(struct symbol *sym, struct symbol *base_type)
 280 {
 281         combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
 282         sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
 283         concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
 284                         (struct ptr_list **)&sym->ctype.contexts);
 285         sym->ctype.base_type = base_type->ctype.base_type;
 286         if (sym->ctype.base_type->type == SYM_NODE)
 287                 merge_type(sym, sym->ctype.base_type);
 288 }
 289 
 290 static int count_array_initializer(struct symbol *t, struct expression *expr)
 291 {
 292         int nr = 0;
 293         int is_char = 0;
 294 
 295         /*
 296          * Arrays of character types are special; they can be initialized by
 297          * string literal _or_ by string literal in braces.  The latter means
 298          * that with T x[] = {<string literal>} number of elements in x depends
 299          * on T - if it's a character type, we get the length of string literal
 300          * (including NUL), otherwise we have one element here.
 301          */


 348                                 if (is_char)
 349                                         nr = expr->string->length;
 350                         }
 351                 }
 352                 break;
 353         default:
 354                 break;
 355         }
 356         return nr;
 357 }
 358 
 359 static struct expression *get_symbol_initializer(struct symbol *sym)
 360 {
 361         do {
 362                 if (sym->initializer)
 363                         return sym->initializer;
 364         } while ((sym = sym->same_symbol) != NULL);
 365         return NULL;
 366 }
 367 
 368 static unsigned int implicit_array_size(struct symbol *node, unsigned int count)
 369 {
 370         struct symbol *arr_ori = node->ctype.base_type;
 371         struct symbol *arr_new = alloc_symbol(node->pos, SYM_ARRAY);
 372         struct symbol *elem_type = arr_ori->ctype.base_type;
 373         struct expression *size = alloc_const_expression(node->pos, count);
 374         unsigned int bit_size = array_element_offset(elem_type->bit_size, count);
 375 
 376         *arr_new = *arr_ori;
 377         arr_new->bit_size = bit_size;
 378         arr_new->array_size = size;
 379         node->array_size = size;
 380         node->ctype.base_type = arr_new;
 381 
 382         return bit_size;
 383 }
 384 
 385 static struct symbol * examine_node_type(struct symbol *sym)
 386 {
 387         struct symbol *base_type = examine_base_type(sym);
 388         int bit_size;
 389         unsigned long alignment;
 390 
 391         /* SYM_NODE - figure out what the type of the node was.. */
 392         bit_size = 0;
 393         alignment = 0;
 394         if (!base_type)
 395                 return sym;
 396 
 397         bit_size = base_type->bit_size;
 398         alignment = base_type->ctype.alignment;
 399 
 400         /* Pick up signedness information into the node */
 401         sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
 402 
 403         if (!sym->ctype.alignment)
 404                 sym->ctype.alignment = alignment;
 405 
 406         /* Unsized array? The size might come from the initializer.. */
 407         if (bit_size < 0 && base_type->type == SYM_ARRAY) {
 408                 struct expression *initializer = get_symbol_initializer(sym);
 409                 if (initializer) {
 410                         struct symbol *node_type = base_type->ctype.base_type;
 411                         int count = count_array_initializer(node_type, initializer);
 412 
 413                         if (node_type && node_type->bit_size >= 0)
 414                                 bit_size = implicit_array_size(sym, count);
 415                 }
 416         }
 417         
 418         sym->bit_size = bit_size;
 419         return sym;
 420 }
 421 
 422 static struct symbol *examine_enum_type(struct symbol *sym)
 423 {
 424         struct symbol *base_type = examine_base_type(sym);
 425 
 426         sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
 427         sym->bit_size = bits_in_enum;
 428         if (base_type->bit_size > sym->bit_size)
 429                 sym->bit_size = base_type->bit_size;
 430         sym->ctype.alignment = enum_alignment;
 431         if (base_type->ctype.alignment > sym->ctype.alignment)
 432                 sym->ctype.alignment = base_type->ctype.alignment;
 433         return sym;
 434 }


 480                 return examine_bitfield_type(sym);
 481         case SYM_BASETYPE:
 482                 /* Size and alignment had better already be set up */
 483                 return sym;
 484         case SYM_TYPEOF: {
 485                 struct symbol *base = evaluate_expression(sym->initializer);
 486                 if (base) {
 487                         unsigned long mod = 0;
 488 
 489                         if (is_bitfield_type(base))
 490                                 warning(base->pos, "typeof applied to bitfield type");
 491                         if (base->type == SYM_NODE) {
 492                                 mod |= base->ctype.modifiers & MOD_TYPEOF;
 493                                 base = base->ctype.base_type;
 494                         }
 495                         sym->type = SYM_NODE;
 496                         sym->ctype.modifiers = mod;
 497                         sym->ctype.base_type = base;
 498                         return examine_node_type(sym);
 499                 }
 500                 sym->type = SYM_NODE;
 501                 sym->ctype.base_type = &bad_ctype;
 502                 return sym;
 503         }
 504         case SYM_PREPROCESSOR:
 505                 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
 506                 return NULL;
 507         case SYM_UNINITIALIZED:
 508 //              sparse_error(sym->pos, "ctype on uninitialized symbol '%s'", show_typename(sym));
 509                 return NULL;
 510         case SYM_RESTRICT:
 511                 examine_base_type(sym);
 512                 return sym;
 513         case SYM_FOULED:
 514                 examine_base_type(sym);
 515                 return sym;
 516         default:
 517 //              sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
 518                 break;
 519         }
 520         return sym;
 521 }
 522 
 523 const char* get_type_name(enum type type)
 524 {
 525         const char *type_lookup[] = {
 526         [SYM_UNINITIALIZED] = "uninitialized",
 527         [SYM_PREPROCESSOR] = "preprocessor",
 528         [SYM_BASETYPE] = "basetype",


 680  * Abstract types
 681  */
 682 struct symbol   int_type,
 683                 fp_type;
 684 
 685 /*
 686  * C types (i.e. actual instances that the abstract types
 687  * can map onto)
 688  */
 689 struct symbol   bool_ctype, void_ctype, type_ctype,
 690                 char_ctype, schar_ctype, uchar_ctype,
 691                 short_ctype, sshort_ctype, ushort_ctype,
 692                 int_ctype, sint_ctype, uint_ctype,
 693                 long_ctype, slong_ctype, ulong_ctype,
 694                 llong_ctype, sllong_ctype, ullong_ctype,
 695                 lllong_ctype, slllong_ctype, ulllong_ctype,
 696                 float_ctype, double_ctype, ldouble_ctype,
 697                 string_ctype, ptr_ctype, lazy_ptr_ctype,
 698                 incomplete_ctype, label_ctype, bad_ctype,
 699                 null_ctype;
 700 struct symbol   int_ptr_ctype, uint_ptr_ctype;
 701 struct symbol   long_ptr_ctype, ulong_ptr_ctype;
 702 struct symbol   llong_ptr_ctype, ullong_ptr_ctype;
 703 struct symbol   float32_ctype, float32x_ctype;
 704 struct symbol   float64_ctype, float64x_ctype;
 705 struct symbol   float128_ctype;
 706 struct symbol   const_void_ctype, const_char_ctype;
 707 struct symbol   const_ptr_ctype, const_string_ctype;
 708 
 709 struct symbol   zero_int;
 710 
 711 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
 712 #define __IDENT(n,str,res) \
 713         struct ident n  = __INIT_IDENT(str,res)
 714 
 715 #include "ident-list.h"
 716 
 717 void init_symbols(void)
 718 {
 719         int stream = init_stream("builtin", -1, includepath);
 720 
 721 #define __IDENT(n,str,res) \
 722         hash_ident(&n)
 723 #include "ident-list.h"
 724 
 725         init_parser(stream);
 726         init_builtins(stream);
 727 }
 728 
 729 #ifdef __CHAR_UNSIGNED__
 730 #define CHAR_SIGNEDNESS MOD_UNSIGNED
 731 #else
 732 #define CHAR_SIGNEDNESS MOD_SIGNED
 733 #endif
 734 // For fix-sized types
 735 static int bits_in_type32 = 32;
 736 static int bits_in_type64 = 64;
 737 static int bits_in_type128 = 128;
 738 
 739 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
 740 #define MOD_LL (MOD_LONG | MOD_LONGLONG)
 741 #define MOD_LLL MOD_LONGLONGLONG
 742 static const struct ctype_declare {
 743         struct symbol *ptr;
 744         enum type type;
 745         unsigned long modifiers;
 746         int *bit_size;
 747         int *maxalign;
 748         struct symbol *base_type;
 749 } ctype_declaration[] = {
 750         { &bool_ctype,          SYM_BASETYPE, MOD_UNSIGNED,             &bits_in_bool,       &max_int_alignment, &int_type },
 751         { &void_ctype,          SYM_BASETYPE, 0,                        NULL,            NULL,               NULL },
 752         { &type_ctype,          SYM_BASETYPE, MOD_TYPE,                 NULL,                    NULL,               NULL },
 753         { &incomplete_ctype,SYM_BASETYPE, 0,                            NULL,                    NULL,               NULL },
 754         { &bad_ctype,           SYM_BASETYPE, 0,                        NULL,                    NULL,               NULL },
 755 
 756         { &char_ctype,          SYM_BASETYPE, CHAR_SIGNEDNESS | MOD_CHAR,    &bits_in_char,          &max_int_alignment, &int_type },
 757         { &schar_ctype,         SYM_BASETYPE, MOD_ESIGNED | MOD_CHAR,   &bits_in_char,       &max_int_alignment, &int_type },


 759         { &short_ctype,         SYM_BASETYPE, MOD_SIGNED | MOD_SHORT,   &bits_in_short,      &max_int_alignment, &int_type },
 760         { &sshort_ctype,    SYM_BASETYPE, MOD_ESIGNED | MOD_SHORT,  &bits_in_short,          &max_int_alignment, &int_type },
 761         { &ushort_ctype,    SYM_BASETYPE, MOD_UNSIGNED | MOD_SHORT, &bits_in_short,          &max_int_alignment, &int_type },
 762         { &int_ctype,           SYM_BASETYPE, MOD_SIGNED,               &bits_in_int,        &max_int_alignment, &int_type },
 763         { &sint_ctype,          SYM_BASETYPE, MOD_ESIGNED,              &bits_in_int,        &max_int_alignment, &int_type },
 764         { &uint_ctype,          SYM_BASETYPE, MOD_UNSIGNED,             &bits_in_int,        &max_int_alignment, &int_type },
 765         { &long_ctype,          SYM_BASETYPE, MOD_SIGNED | MOD_LONG,    &bits_in_long,       &max_int_alignment, &int_type },
 766         { &slong_ctype,         SYM_BASETYPE, MOD_ESIGNED | MOD_LONG,   &bits_in_long,       &max_int_alignment, &int_type },
 767         { &ulong_ctype,         SYM_BASETYPE, MOD_UNSIGNED | MOD_LONG,  &bits_in_long,       &max_int_alignment, &int_type },
 768         { &llong_ctype,         SYM_BASETYPE, MOD_SIGNED | MOD_LL,      &bits_in_longlong,       &max_int_alignment, &int_type },
 769         { &sllong_ctype,    SYM_BASETYPE, MOD_ESIGNED | MOD_LL,         &bits_in_longlong,       &max_int_alignment, &int_type },
 770         { &ullong_ctype,    SYM_BASETYPE, MOD_UNSIGNED | MOD_LL,    &bits_in_longlong,       &max_int_alignment, &int_type },
 771         { &lllong_ctype,    SYM_BASETYPE, MOD_SIGNED | MOD_LLL,         &bits_in_longlonglong,   &max_int_alignment, &int_type },
 772         { &slllong_ctype,   SYM_BASETYPE, MOD_ESIGNED | MOD_LLL,    &bits_in_longlonglong,   &max_int_alignment, &int_type },
 773         { &ulllong_ctype,   SYM_BASETYPE, MOD_UNSIGNED | MOD_LLL,   &bits_in_longlonglong,   &max_int_alignment, &int_type },
 774 
 775         { &float_ctype,         SYM_BASETYPE,  0,                       &bits_in_float,          &max_fp_alignment,  &fp_type },
 776         { &double_ctype,    SYM_BASETYPE, MOD_LONG,             &bits_in_double,         &max_fp_alignment,  &fp_type },
 777         { &ldouble_ctype,   SYM_BASETYPE, MOD_LONG | MOD_LONGLONG,  &bits_in_longdouble,     &max_fp_alignment,  &fp_type },
 778 
 779         { &float32_ctype,   SYM_BASETYPE,  0,                           &bits_in_type32,          &max_fp_alignment, &fp_type },
 780         { &float32x_ctype,  SYM_BASETYPE, MOD_LONG,             &bits_in_double,         &max_fp_alignment,  &fp_type },
 781         { &float64_ctype,   SYM_BASETYPE,  0,                           &bits_in_type64,          &max_fp_alignment, &fp_type },
 782         { &float64x_ctype,  SYM_BASETYPE, MOD_LONG | MOD_LONGLONG,  &bits_in_longdouble,     &max_fp_alignment,  &fp_type },
 783         { &float128_ctype,  SYM_BASETYPE,  0,                           &bits_in_type128,         &max_alignment,    &fp_type },
 784 
 785         { &string_ctype,    SYM_PTR,          0,                        &bits_in_pointer,        &pointer_alignment, &char_ctype },
 786         { &ptr_ctype,           SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &void_ctype },
 787         { &null_ctype,          SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &void_ctype },
 788         { &label_ctype,         SYM_PTR,      0,                        &bits_in_pointer,        &pointer_alignment, &void_ctype },
 789         { &lazy_ptr_ctype,  SYM_PTR,          0,                        &bits_in_pointer,        &pointer_alignment, &void_ctype },
 790         { &int_ptr_ctype,   SYM_PTR,          0,                        &bits_in_pointer,        &pointer_alignment, &int_ctype },
 791         { &uint_ptr_ctype,  SYM_PTR,          0,                        &bits_in_pointer,        &pointer_alignment, &uint_ctype },
 792         { &long_ptr_ctype,  SYM_PTR,          0,                        &bits_in_pointer,        &pointer_alignment, &long_ctype },
 793         { &ulong_ptr_ctype, SYM_PTR,          0,                        &bits_in_pointer,        &pointer_alignment, &ulong_ctype },
 794         { &llong_ptr_ctype, SYM_PTR,          0,                        &bits_in_pointer,        &pointer_alignment, &llong_ctype },
 795         { &ullong_ptr_ctype,SYM_PTR,          0,                        &bits_in_pointer,        &pointer_alignment, &ullong_ctype },
 796 
 797         { &const_void_ctype, SYM_NODE,        MOD_CONST,                NULL, NULL, &void_ctype },
 798         { &const_char_ctype, SYM_NODE,        MOD_CONST,                &bits_in_char, &max_int_alignment, &char_ctype },
 799         { &const_ptr_ctype, SYM_PTR,          0,                        &bits_in_pointer,        &pointer_alignment, &const_void_ctype },
 800         { &const_string_ctype,SYM_PTR,        0,                        &bits_in_pointer,        &pointer_alignment, &const_char_ctype },
 801         { NULL, }
 802 };
 803 #undef MOD_LLL
 804 #undef MOD_LL
 805 #undef MOD_ESIGNED
 806 
 807 void init_ctype(void)
 808 {
 809         const struct ctype_declare *ctype;
 810 
 811         for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
 812                 struct symbol *sym = ctype->ptr;
 813                 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
 814                 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
 815                 unsigned long alignment = bits_to_bytes(bit_size);
 816 
 817                 if (alignment > maxalign)
 818                         alignment = maxalign;
 819                 sym->type = ctype->type;
 820                 sym->bit_size = bit_size;
 821                 sym->ctype.alignment = alignment;
 822                 sym->ctype.base_type = ctype->base_type;
 823                 sym->ctype.modifiers = ctype->modifiers;
 824         }
 825 
 826         // and now some adjustments
 827         if (funsigned_char) {
 828                 char_ctype.ctype.modifiers |= MOD_UNSIGNED;
 829                 char_ctype.ctype.modifiers &= ~MOD_SIGNED;
 830         }
 831 }