1 /*
   2  * Symbol lookup and handling.
   3  *
   4  * Copyright (C) 2003 Transmeta Corp.
   5  *               2003-2004 Linus Torvalds
   6  *
   7  * Permission is hereby granted, free of charge, to any person obtaining a copy
   8  * of this software and associated documentation files (the "Software"), to deal
   9  * in the Software without restriction, including without limitation the rights
  10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11  * copies of the Software, and to permit persons to whom the Software is
  12  * furnished to do so, subject to the following conditions:
  13  *
  14  * The above copyright notice and this permission notice shall be included in
  15  * all copies or substantial portions of the Software.
  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);
  75 }
  76 
  77 struct symbol *alloc_symbol(struct position pos, int type)
  78 {
  79         struct symbol *sym = __alloc_symbol(0);
  80         sym->type = type;
  81         sym->pos = pos;
  82         sym->endpos.type = 0;
  83         return sym;
  84 }
  85 
  86 struct struct_union_info {
  87         unsigned long max_align;
  88         unsigned long bit_size;
  89         int align_size;
  90 };
  91 
  92 /*
  93  * Unions are fairly easy to lay out ;)
  94  */
  95 static void lay_out_union(struct symbol *sym, struct struct_union_info *info)
  96 {
  97         examine_symbol_type(sym);
  98 
  99         // Unnamed bitfields do not affect alignment.
 100         if (sym->ident || !is_bitfield_type(sym)) {
 101                 if (sym->ctype.alignment > info->max_align)
 102                         info->max_align = sym->ctype.alignment;
 103         }
 104 
 105         if (sym->bit_size > info->bit_size)
 106                 info->bit_size = sym->bit_size;
 107 
 108         sym->offset = 0;
 109 }
 110 
 111 static int bitfield_base_size(struct symbol *sym)
 112 {
 113         if (sym->type == SYM_NODE)
 114                 sym = sym->ctype.base_type;
 115         if (sym->type == SYM_BITFIELD)
 116                 sym = sym->ctype.base_type;
 117         return sym->bit_size;
 118 }
 119 
 120 /*
 121  * Structures are a bit more interesting to lay out
 122  */
 123 static void lay_out_struct(struct symbol *sym, struct struct_union_info *info)
 124 {
 125         unsigned long bit_size, align_bit_mask;
 126         int base_size;
 127 
 128         examine_symbol_type(sym);
 129 
 130         // Unnamed bitfields do not affect alignment.
 131         if (sym->ident || !is_bitfield_type(sym)) {
 132                 if (sym->ctype.alignment > info->max_align)
 133                         info->max_align = sym->ctype.alignment;
 134         }
 135 
 136         bit_size = info->bit_size;
 137         base_size = sym->bit_size; 
 138 
 139         /*
 140          * Unsized arrays cause us to not align the resulting
 141          * structure size
 142          */
 143         if (base_size < 0) {
 144                 info->align_size = 0;
 145                 base_size = 0;
 146         }
 147 
 148         align_bit_mask = bytes_to_bits(sym->ctype.alignment) - 1;
 149 
 150         /*
 151          * Bitfields have some very special rules..
 152          */
 153         if (is_bitfield_type (sym)) {
 154                 unsigned long bit_offset = bit_size & align_bit_mask;
 155                 int room = bitfield_base_size(sym) - bit_offset;
 156                 // Zero-width fields just fill up the unit.
 157                 int width = base_size ? : (bit_offset ? room : 0);
 158 
 159                 if (width > room) {
 160                         bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
 161                         bit_offset = 0;
 162                 }
 163                 sym->offset = bits_to_bytes(bit_size - bit_offset);
 164                 sym->bit_offset = bit_offset;
 165                 sym->ctype.base_type->bit_offset = bit_offset;
 166                 info->bit_size = bit_size + width;
 167                 // warning (sym->pos, "bitfield: offset=%d:%d  size=:%d", sym->offset, sym->bit_offset, width);
 168 
 169                 return;
 170         }
 171 
 172         /*
 173          * Otherwise, just align it right and add it up..
 174          */
 175         bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
 176         sym->offset = bits_to_bytes(bit_size);
 177 
 178         info->bit_size = bit_size + base_size;
 179         // warning (sym->pos, "regular: offset=%d", sym->offset);
 180 }
 181 
 182 static struct symbol * examine_struct_union_type(struct symbol *sym, int advance)
 183 {
 184         struct struct_union_info info = {
 185                 .max_align = 1,
 186                 .bit_size = 0,
 187                 .align_size = 1
 188         };
 189         unsigned long bit_size, bit_align;
 190         void (*fn)(struct symbol *, struct struct_union_info *);
 191         struct symbol *member;
 192 
 193         fn = advance ? lay_out_struct : lay_out_union;
 194         FOR_EACH_PTR(sym->symbol_list, member) {
 195                 fn(member, &info);
 196         } END_FOR_EACH_PTR(member);
 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) {       
 238                 bit_size = array_element_offset(base_type->bit_size,
 239                                                 get_expression_value_silent(array_size));
 240                 if (array_size->type != EXPR_VALUE) {
 241                         if (Wvla)
 242                                 warning(array_size->pos, "Variable length array is used.");
 243                         bit_size = -1;
 244                 }
 245         }
 246         alignment = base_type->ctype.alignment;
 247         if (!sym->ctype.alignment)
 248                 sym->ctype.alignment = alignment;
 249         sym->bit_size = bit_size;
 250         return sym;
 251 }
 252 
 253 static struct symbol *examine_bitfield_type(struct symbol *sym)
 254 {
 255         struct symbol *base_type = examine_base_type(sym);
 256         unsigned long bit_size, alignment, modifiers;
 257 
 258         if (!base_type)
 259                 return sym;
 260         bit_size = base_type->bit_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          */
 302         if (t->ctype.base_type == &int_type && t->ctype.modifiers & MOD_CHAR)
 303                 is_char = 1;
 304 
 305         switch (expr->type) {
 306         case EXPR_INITIALIZER: {
 307                 struct expression *entry;
 308                 int count = 0;
 309                 int str_len = 0;
 310                 FOR_EACH_PTR(expr->expr_list, entry) {
 311                         count++;
 312                         switch (entry->type) {
 313                         case EXPR_INDEX:
 314                                 if (entry->idx_to >= nr)
 315                                         nr = entry->idx_to+1;
 316                                 break;
 317                         case EXPR_PREOP: {
 318                                 struct expression *e = entry;
 319                                 if (is_char) {
 320                                         while (e && e->type == EXPR_PREOP && e->op == '(')
 321                                                 e = e->unop;
 322                                         if (e && e->type == EXPR_STRING) {
 323                                                 entry = e;
 324                         case EXPR_STRING:
 325                                                 if (is_char)
 326                                                         str_len = entry->string->length;
 327                                         }
 328 
 329 
 330                                 }
 331                         }
 332                         default:
 333                                 nr++;
 334                         }
 335                 } END_FOR_EACH_PTR(entry);
 336                 if (count == 1 && str_len)
 337                         nr = str_len;
 338                 break;
 339         }
 340         case EXPR_PREOP:
 341                 if (is_char) { 
 342                         struct expression *e = expr;
 343                         while (e && e->type == EXPR_PREOP && e->op == '(')
 344                                 e = e->unop;
 345                         if (e && e->type == EXPR_STRING) {
 346                                 expr = e;
 347         case EXPR_STRING:
 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 }
 435 
 436 static struct symbol *examine_pointer_type(struct symbol *sym)
 437 {
 438         /*
 439          * We need to set the pointer size first, and
 440          * examine the thing we point to only afterwards.
 441          * That's because this pointer type may end up
 442          * being needed for the base type size evaluation.
 443          */
 444         if (!sym->bit_size)
 445                 sym->bit_size = bits_in_pointer;
 446         if (!sym->ctype.alignment)
 447                 sym->ctype.alignment = pointer_alignment;
 448         return sym;
 449 }
 450 
 451 /*
 452  * Fill in type size and alignment information for
 453  * regular SYM_TYPE things.
 454  */
 455 struct symbol *examine_symbol_type(struct symbol * sym)
 456 {
 457         if (!sym)
 458                 return sym;
 459 
 460         /* Already done? */
 461         if (sym->examined)
 462                 return sym;
 463         sym->examined = 1;
 464 
 465         switch (sym->type) {
 466         case SYM_FN:
 467         case SYM_NODE:
 468                 return examine_node_type(sym);
 469         case SYM_ARRAY:
 470                 return examine_array_type(sym);
 471         case SYM_STRUCT:
 472                 return examine_struct_union_type(sym, 1);
 473         case SYM_UNION:
 474                 return examine_struct_union_type(sym, 0);
 475         case SYM_PTR:
 476                 return examine_pointer_type(sym);
 477         case SYM_ENUM:
 478                 return examine_enum_type(sym);
 479         case SYM_BITFIELD:
 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",
 529         [SYM_NODE] = "node",
 530         [SYM_PTR] = "pointer",
 531         [SYM_FN] = "function",
 532         [SYM_ARRAY] = "array",
 533         [SYM_STRUCT] = "struct",
 534         [SYM_UNION] = "union",
 535         [SYM_ENUM] = "enum",
 536         [SYM_TYPEDEF] = "typedef",
 537         [SYM_TYPEOF] = "typeof",
 538         [SYM_MEMBER] = "member",
 539         [SYM_BITFIELD] = "bitfield",
 540         [SYM_LABEL] = "label",
 541         [SYM_RESTRICT] = "restrict",
 542         [SYM_FOULED] = "fouled",
 543         [SYM_KEYWORD] = "keyword",
 544         [SYM_BAD] = "bad"};
 545 
 546         if (type <= SYM_BAD)
 547                 return type_lookup[type];
 548         else
 549                 return NULL;
 550 }
 551 
 552 struct symbol *examine_pointer_target(struct symbol *sym)
 553 {
 554         return examine_base_type(sym);
 555 }
 556 
 557 static struct symbol_list *restr, *fouled;
 558 
 559 void create_fouled(struct symbol *type)
 560 {
 561         if (type->bit_size < bits_in_int) {
 562                 struct symbol *new = alloc_symbol(type->pos, type->type);
 563                 *new = *type;
 564                 new->bit_size = bits_in_int;
 565                 new->type = SYM_FOULED;
 566                 new->ctype.base_type = type;
 567                 add_symbol(&restr, type);
 568                 add_symbol(&fouled, new);
 569         }
 570 }
 571 
 572 struct symbol *befoul(struct symbol *type)
 573 {
 574         struct symbol *t1, *t2;
 575         while (type->type == SYM_NODE)
 576                 type = type->ctype.base_type;
 577         PREPARE_PTR_LIST(restr, t1);
 578         PREPARE_PTR_LIST(fouled, t2);
 579         for (;;) {
 580                 if (t1 == type)
 581                         return t2;
 582                 if (!t1)
 583                         break;
 584                 NEXT_PTR_LIST(t1);
 585                 NEXT_PTR_LIST(t2);
 586         }
 587         FINISH_PTR_LIST(t2);
 588         FINISH_PTR_LIST(t1);
 589         return NULL;
 590 }
 591 
 592 void check_declaration(struct symbol *sym)
 593 {
 594         int warned = 0;
 595         struct symbol *next = sym;
 596 
 597         while ((next = next->next_id) != NULL) {
 598                 if (next->namespace != sym->namespace)
 599                         continue;
 600                 if (sym->scope == next->scope) {
 601                         sym->same_symbol = next;
 602                         return;
 603                 }
 604                 /* Extern in block level matches a TOPLEVEL non-static symbol */
 605                 if (sym->ctype.modifiers & MOD_EXTERN) {
 606                         if ((next->ctype.modifiers & (MOD_TOPLEVEL|MOD_STATIC)) == MOD_TOPLEVEL) {
 607                                 sym->same_symbol = next;
 608                                 return;
 609                         }
 610                 }
 611 
 612                 if (!Wshadow || warned)
 613                         continue;
 614                 if (get_sym_type(next) == SYM_FN)
 615                         continue;
 616                 warned = 1;
 617                 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
 618                 info(next->pos, "originally declared here");
 619         }
 620 }
 621 
 622 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
 623 {
 624         struct scope *scope;
 625         if (sym->bound) {
 626                 sparse_error(sym->pos, "internal error: symbol type already bound");
 627                 return;
 628         }
 629         if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
 630                 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
 631                 return;
 632         }
 633         sym->namespace = ns;
 634         sym->next_id = ident->symbols;
 635         ident->symbols = sym;
 636         if (sym->ident && sym->ident != ident)
 637                 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
 638         sym->ident = ident;
 639         sym->bound = 1;
 640 
 641         scope = block_scope;
 642         if (ns == NS_SYMBOL && toplevel(scope)) {
 643                 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
 644 
 645                 scope = global_scope;
 646                 if (sym->ctype.modifiers & MOD_STATIC ||
 647                     is_extern_inline(sym)) {
 648                         scope = file_scope;
 649                         mod = MOD_TOPLEVEL;
 650                 }
 651                 sym->ctype.modifiers |= mod;
 652         }
 653         if (ns == NS_MACRO)
 654                 scope = file_scope;
 655         if (ns == NS_LABEL)
 656                 scope = function_scope;
 657         bind_scope(sym, scope);
 658 }
 659 
 660 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
 661 {
 662         struct ident *ident = built_in_ident(name);
 663         struct symbol *sym = lookup_symbol(ident, namespace);
 664 
 665         if (sym && sym->type != type)
 666                 die("symbol %s created with different types: %d old %d", name,
 667                                 type, sym->type);
 668 
 669         if (!sym) {
 670                 struct token *token = built_in_token(stream, ident);
 671 
 672                 sym = alloc_symbol(token->pos, type);
 673                 bind_symbol(sym, token->ident, namespace);
 674         }
 675         return sym;
 676 }
 677 
 678 
 679 /*
 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 },
 758         { &uchar_ctype,         SYM_BASETYPE, MOD_UNSIGNED | 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 }