dereferences_param: mark more parameters as dereferenced
[smatch.git] / symbol.c
blob515b96f22792e22067c82bbd6951657dda88308d
1 /*
2 * Symbol lookup and handling.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
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"
38 #include "target.h"
41 * Secondary symbol list for stuff that needs to be output because it
42 * was used.
44 struct symbol_list *translation_unit_used_list = NULL;
47 * If the symbol is an inline symbol, add it to the list of symbols to parse
49 void access_symbol(struct symbol *sym)
51 if (sym->ctype.modifiers & MOD_INLINE) {
52 if (!sym->accessed) {
53 add_symbol(&translation_unit_used_list, sym);
54 sym->accessed = 1;
59 struct symbol *lookup_symbol(struct ident *ident, enum namespace ns)
61 struct symbol *sym;
63 for (sym = ident->symbols; sym; sym = sym->next_id) {
64 if (sym->namespace & ns) {
65 sym->used = 1;
66 return sym;
69 return NULL;
72 struct context *alloc_context(void)
74 return __alloc_context(0);
77 struct symbol *alloc_symbol(struct position pos, int type)
79 struct symbol *sym = __alloc_symbol(0);
80 sym->type = type;
81 sym->pos = pos;
82 sym->endpos.type = 0;
83 return sym;
86 struct struct_union_info {
87 unsigned long max_align;
88 unsigned long bit_size;
89 int align_size;
93 * Unions are fairly easy to lay out ;)
95 static void lay_out_union(struct symbol *sym, struct struct_union_info *info)
97 examine_symbol_type(sym);
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;
105 if (sym->bit_size > info->bit_size)
106 info->bit_size = sym->bit_size;
108 sym->offset = 0;
111 static int bitfield_base_size(struct symbol *sym)
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;
121 * Structures are a bit more interesting to lay out
123 static void lay_out_struct(struct symbol *sym, struct struct_union_info *info)
125 unsigned long bit_size, align_bit_mask;
126 int base_size;
128 examine_symbol_type(sym);
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;
136 bit_size = info->bit_size;
137 base_size = sym->bit_size;
140 * Unsized arrays cause us to not align the resulting
141 * structure size
143 if (base_size < 0) {
144 info->align_size = 0;
145 base_size = 0;
148 align_bit_mask = bytes_to_bits(sym->ctype.alignment) - 1;
151 * Bitfields have some very special rules..
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);
159 if (width > room) {
160 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
161 bit_offset = 0;
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);
169 return;
173 * Otherwise, just align it right and add it up..
175 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
176 sym->offset = bits_to_bytes(bit_size);
178 info->bit_size = bit_size + base_size;
179 // warning (sym->pos, "regular: offset=%d", sym->offset);
182 static struct symbol * examine_struct_union_type(struct symbol *sym, int advance)
184 struct struct_union_info info = {
185 .max_align = 1,
186 .bit_size = 0,
187 .align_size = 1
189 unsigned long bit_size, bit_align;
190 void (*fn)(struct symbol *, struct struct_union_info *);
191 struct symbol *member;
193 fn = advance ? lay_out_struct : lay_out_union;
194 FOR_EACH_PTR(sym->symbol_list, member) {
195 if (member->ctype.base_type == &autotype_ctype) {
196 sparse_error(member->pos, "member '%s' has __auto_type", show_ident(member->ident));
197 member->ctype.base_type = &incomplete_ctype;
199 fn(member, &info);
200 } END_FOR_EACH_PTR(member);
202 if (!sym->ctype.alignment)
203 sym->ctype.alignment = info.max_align;
204 bit_size = info.bit_size;
205 if (info.align_size) {
206 bit_align = bytes_to_bits(sym->ctype.alignment)-1;
207 bit_size = (bit_size + bit_align) & ~bit_align;
209 sym->bit_size = bit_size;
210 return sym;
213 static struct symbol *examine_base_type(struct symbol *sym)
215 struct symbol *base_type;
217 if (sym->ctype.base_type == &autotype_ctype) {
218 struct symbol *type = evaluate_expression(sym->initializer);
219 if (!type)
220 type = &bad_ctype;
221 if (is_bitfield_type(type)) {
222 warning(sym->pos, "__auto_type on bitfield");
223 if (type->type == SYM_NODE)
224 type = type->ctype.base_type;
225 type = type->ctype.base_type;
227 sym->ctype.base_type = type;
230 /* Check the base type */
231 base_type = examine_symbol_type(sym->ctype.base_type);
232 if (!base_type || base_type->type == SYM_PTR)
233 return base_type;
234 combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
235 sym->ctype.modifiers |= base_type->ctype.modifiers & MOD_PTRINHERIT;
236 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
237 (struct ptr_list **)&sym->ctype.contexts);
238 if (base_type->type == SYM_NODE) {
239 base_type = base_type->ctype.base_type;
240 sym->ctype.base_type = base_type;
241 sym->rank = base_type->rank;
243 return base_type;
246 static struct symbol * examine_array_type(struct symbol *sym)
248 struct symbol *base_type = examine_base_type(sym);
249 unsigned long bit_size = -1, alignment;
250 struct expression *array_size = sym->array_size;
252 if (!base_type)
253 return sym;
255 if (array_size) {
256 bit_size = array_element_offset(base_type->bit_size,
257 get_expression_value_silent(array_size));
258 if (array_size->type != EXPR_VALUE) {
259 if (Wvla)
260 warning(array_size->pos, "Variable length array is used.");
261 bit_size = -1;
264 alignment = base_type->ctype.alignment;
265 if (!sym->ctype.alignment)
266 sym->ctype.alignment = alignment;
267 sym->bit_size = bit_size;
268 return sym;
271 static struct symbol *examine_bitfield_type(struct symbol *sym)
273 struct symbol *base_type = examine_base_type(sym);
274 unsigned long alignment, modifiers;
276 if (!base_type)
277 return sym;
278 if (sym->bit_size > base_type->bit_size) {
279 sparse_error(sym->pos, "bitfield '%s' is wider (%d) than its type (%s)",
280 show_ident(sym->ident), sym->bit_size, show_typename(base_type));
281 sym->bit_size = -1;
284 alignment = base_type->ctype.alignment;
285 if (!sym->ctype.alignment)
286 sym->ctype.alignment = alignment;
287 modifiers = base_type->ctype.modifiers;
289 /* Bitfields are unsigned, unless the base type was explicitly signed */
290 if (!(modifiers & MOD_EXPLICITLY_SIGNED))
291 modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
292 sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
293 return sym;
297 * "typeof" will have to merge the types together
299 void merge_type(struct symbol *sym, struct symbol *base_type)
301 combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
302 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
303 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
304 (struct ptr_list **)&sym->ctype.contexts);
305 sym->ctype.base_type = base_type->ctype.base_type;
306 if (sym->ctype.base_type->type == SYM_NODE)
307 merge_type(sym, sym->ctype.base_type);
310 static int count_array_initializer(struct symbol *t, struct expression *expr)
312 int nr = 0;
313 int is_char = 0;
316 * Arrays of character types are special; they can be initialized by
317 * string literal _or_ by string literal in braces. The latter means
318 * that with T x[] = {<string literal>} number of elements in x depends
319 * on T - if it's a character type, we get the length of string literal
320 * (including NUL), otherwise we have one element here.
322 if (t->ctype.base_type == &int_type && t->rank == -2)
323 is_char = 1;
325 switch (expr->type) {
326 case EXPR_INITIALIZER: {
327 struct expression *entry;
328 int count = 0;
329 int str_len = 0;
330 FOR_EACH_PTR(expr->expr_list, entry) {
331 count++;
332 switch (entry->type) {
333 case EXPR_INDEX:
334 if (entry->idx_to >= nr)
335 nr = entry->idx_to+1;
336 break;
337 case EXPR_PREOP: {
338 struct expression *e = entry;
339 if (is_char) {
340 while (e && e->type == EXPR_PREOP && e->op == '(')
341 e = e->unop;
342 if (e && e->type == EXPR_STRING) {
343 entry = e;
344 case EXPR_STRING:
345 if (is_char)
346 str_len = entry->string->length;
352 default:
353 nr++;
355 } END_FOR_EACH_PTR(entry);
356 if (count == 1 && str_len)
357 nr = str_len;
358 break;
360 case EXPR_PREOP:
361 if (is_char) {
362 struct expression *e = expr;
363 while (e && e->type == EXPR_PREOP && e->op == '(')
364 e = e->unop;
365 if (e && e->type == EXPR_STRING) {
366 expr = e;
367 case EXPR_STRING:
368 if (is_char)
369 nr = expr->string->length;
372 break;
373 default:
374 break;
376 return nr;
379 static struct expression *get_symbol_initializer(struct symbol *sym)
381 do {
382 if (sym->initializer)
383 return sym->initializer;
384 } while ((sym = sym->same_symbol) != NULL);
385 return NULL;
388 static unsigned int implicit_array_size(struct symbol *node, unsigned int count)
390 struct symbol *arr_ori = node->ctype.base_type;
391 struct symbol *arr_new = alloc_symbol(node->pos, SYM_ARRAY);
392 struct symbol *elem_type = arr_ori->ctype.base_type;
393 struct expression *size = alloc_const_expression(node->pos, count);
394 unsigned int bit_size = array_element_offset(elem_type->bit_size, count);
396 *arr_new = *arr_ori;
397 arr_new->bit_size = bit_size;
398 arr_new->array_size = size;
399 node->array_size = size;
400 node->ctype.base_type = arr_new;
402 return bit_size;
405 static struct symbol * examine_node_type(struct symbol *sym)
407 struct symbol *base_type = examine_base_type(sym);
408 int bit_size;
409 unsigned long alignment;
411 /* SYM_NODE - figure out what the type of the node was.. */
412 bit_size = 0;
413 alignment = 0;
414 if (!base_type)
415 return sym;
417 bit_size = base_type->bit_size;
418 alignment = base_type->ctype.alignment;
420 /* Pick up signedness information into the node */
421 sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
423 if (!sym->ctype.alignment)
424 sym->ctype.alignment = alignment;
426 /* Unsized array? The size might come from the initializer.. */
427 if (bit_size < 0 && base_type->type == SYM_ARRAY) {
428 struct expression *initializer = get_symbol_initializer(sym);
429 if (initializer) {
430 struct symbol *node_type = base_type->ctype.base_type;
431 int count = count_array_initializer(node_type, initializer);
433 if (node_type && node_type->bit_size >= 0)
434 bit_size = implicit_array_size(sym, count);
438 sym->bit_size = bit_size;
439 sym->rank = base_type->rank;
440 return sym;
443 static struct symbol *examine_enum_type(struct symbol *sym)
445 struct symbol *base_type = examine_base_type(sym);
447 sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
448 sym->bit_size = bits_in_enum;
449 if (base_type->bit_size > sym->bit_size)
450 sym->bit_size = base_type->bit_size;
451 sym->ctype.alignment = enum_alignment;
452 if (base_type->ctype.alignment > sym->ctype.alignment)
453 sym->ctype.alignment = base_type->ctype.alignment;
454 return sym;
457 static struct symbol *examine_pointer_type(struct symbol *sym)
460 * Since pointers to incomplete types can be used,
461 * for example in a struct-declaration-list,
462 * the base type must *not* be examined here.
463 * It thus means that it needs to be done later,
464 * when the base type of the pointer is looked at.
466 if (!sym->bit_size)
467 sym->bit_size = bits_in_pointer;
468 if (!sym->ctype.alignment)
469 sym->ctype.alignment = pointer_alignment;
470 return sym;
473 static struct symbol *examine_typeof(struct symbol *sym)
475 struct symbol *base = evaluate_expression(sym->initializer);
476 unsigned long mod = 0;
478 if (!base)
479 base = &bad_ctype;
480 if (base->type == SYM_NODE) {
481 mod |= base->ctype.modifiers & MOD_TYPEOF;
482 base = base->ctype.base_type;
484 if (base->type == SYM_BITFIELD)
485 warning(base->pos, "typeof applied to bitfield type");
486 sym->type = SYM_NODE;
487 sym->ctype.modifiers = mod;
488 sym->ctype.base_type = base;
489 return examine_node_type(sym);
493 * Fill in type size and alignment information for
494 * regular SYM_TYPE things.
496 struct symbol *examine_symbol_type(struct symbol * sym)
498 if (!sym)
499 return sym;
501 /* Already done? */
502 if (sym->examined)
503 return sym;
504 sym->examined = 1;
506 switch (sym->type) {
507 case SYM_FN:
508 case SYM_NODE:
509 return examine_node_type(sym);
510 case SYM_ARRAY:
511 return examine_array_type(sym);
512 case SYM_STRUCT:
513 return examine_struct_union_type(sym, 1);
514 case SYM_UNION:
515 return examine_struct_union_type(sym, 0);
516 case SYM_PTR:
517 return examine_pointer_type(sym);
518 case SYM_ENUM:
519 return examine_enum_type(sym);
520 case SYM_BITFIELD:
521 return examine_bitfield_type(sym);
522 case SYM_BASETYPE:
523 /* Size and alignment had better already be set up */
524 return sym;
525 case SYM_TYPEOF:
526 return examine_typeof(sym);
527 case SYM_PREPROCESSOR:
528 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
529 return NULL;
530 case SYM_UNINITIALIZED:
531 // sparse_error(sym->pos, "ctype on uninitialized symbol '%s'", show_typename(sym));
532 return NULL;
533 case SYM_RESTRICT:
534 examine_base_type(sym);
535 return sym;
536 case SYM_FOULED:
537 examine_base_type(sym);
538 return sym;
539 default:
540 // sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
541 break;
543 return sym;
546 const char* get_type_name(enum type type)
548 const char *type_lookup[] = {
549 [SYM_UNINITIALIZED] = "uninitialized",
550 [SYM_PREPROCESSOR] = "preprocessor",
551 [SYM_BASETYPE] = "basetype",
552 [SYM_NODE] = "node",
553 [SYM_PTR] = "pointer",
554 [SYM_FN] = "function",
555 [SYM_ARRAY] = "array",
556 [SYM_STRUCT] = "struct",
557 [SYM_UNION] = "union",
558 [SYM_ENUM] = "enum",
559 [SYM_TYPEOF] = "typeof",
560 [SYM_BITFIELD] = "bitfield",
561 [SYM_LABEL] = "label",
562 [SYM_RESTRICT] = "restrict",
563 [SYM_FOULED] = "fouled",
564 [SYM_KEYWORD] = "keyword",
565 [SYM_BAD] = "bad"};
567 if (type <= SYM_BAD)
568 return type_lookup[type];
569 else
570 return NULL;
573 struct symbol *examine_pointer_target(struct symbol *sym)
575 return examine_base_type(sym);
578 static struct symbol_list *restr, *fouled;
580 void create_fouled(struct symbol *type)
582 if (type->bit_size < bits_in_int) {
583 struct symbol *new = alloc_symbol(type->pos, type->type);
584 *new = *type;
585 new->bit_size = bits_in_int;
586 new->rank = 0;
587 new->type = SYM_FOULED;
588 new->ctype.base_type = type;
589 add_symbol(&restr, type);
590 add_symbol(&fouled, new);
594 struct symbol *befoul(struct symbol *type)
596 struct symbol *t1, *t2;
597 while (type->type == SYM_NODE)
598 type = type->ctype.base_type;
599 PREPARE_PTR_LIST(restr, t1);
600 PREPARE_PTR_LIST(fouled, t2);
601 for (;;) {
602 if (t1 == type)
603 return t2;
604 if (!t1)
605 break;
606 NEXT_PTR_LIST(t1);
607 NEXT_PTR_LIST(t2);
609 FINISH_PTR_LIST(t2);
610 FINISH_PTR_LIST(t1);
611 return NULL;
614 static void inherit_declaration(struct symbol *sym, struct symbol *prev)
616 unsigned long mods = prev->ctype.modifiers;
618 // inherit function attributes
619 sym->ctype.modifiers |= mods & MOD_FUN_ATTR;
622 void check_declaration(struct symbol *sym)
624 int warned = 0;
625 struct symbol *next = sym;
627 while ((next = next->next_id) != NULL) {
628 if (next->namespace != sym->namespace)
629 continue;
630 if (sym->scope == next->scope) {
631 sym->same_symbol = next;
632 inherit_declaration(sym, next);
633 return;
635 /* Extern in block level matches a TOPLEVEL non-static symbol */
636 if (sym->ctype.modifiers & MOD_EXTERN) {
637 if ((next->ctype.modifiers & (MOD_TOPLEVEL|MOD_STATIC)) == MOD_TOPLEVEL) {
638 sym->same_symbol = next;
639 return;
643 if (!Wshadow || warned)
644 continue;
645 if (get_sym_type(next) == SYM_FN)
646 continue;
647 warned = 1;
648 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
649 info(next->pos, "originally declared here");
653 static void inherit_static(struct symbol *sym)
655 struct symbol *prev;
657 // only 'plain' symbols are concerned
658 if (sym->ctype.modifiers & (MOD_STATIC|MOD_EXTERN))
659 return;
661 for (prev = sym->next_id; prev; prev = prev->next_id) {
662 if (prev->namespace != NS_SYMBOL)
663 continue;
664 if (prev->scope != file_scope)
665 continue;
667 sym->ctype.modifiers |= prev->ctype.modifiers & MOD_STATIC;
669 // previous declarations are already converted
670 return;
674 void bind_symbol_with_scope(struct symbol *sym, struct ident *ident, enum namespace ns, struct scope *scope)
676 if (sym->bound) {
677 sparse_error(sym->pos, "internal error: symbol type already bound");
678 return;
680 if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
681 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
682 return;
684 sym->namespace = ns;
685 sym->next_id = ident->symbols;
686 ident->symbols = sym;
687 if (sym->ident && sym->ident != ident)
688 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
689 sym->ident = ident;
690 sym->bound = 1;
692 if (ns == NS_SYMBOL && toplevel(scope)) {
693 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
695 inherit_static(sym);
697 scope = global_scope;
698 if (sym->ctype.modifiers & MOD_STATIC ||
699 is_extern_inline(sym)) {
700 scope = file_scope;
701 mod = MOD_TOPLEVEL;
703 sym->ctype.modifiers |= mod;
705 bind_scope(sym, scope);
708 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
710 struct scope *scope = block_scope;;
712 if (ns == NS_MACRO)
713 scope = file_scope;
714 if (ns == NS_LABEL)
715 scope = function_scope;
716 bind_symbol_with_scope(sym, ident, ns, scope);
719 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
721 struct ident *ident = built_in_ident(name);
722 struct symbol *sym = lookup_symbol(ident, namespace);
724 if (sym && sym->type != type)
725 die("symbol %s created with different types: %d old %d", name,
726 type, sym->type);
728 if (!sym) {
729 struct token *token = built_in_token(stream, ident);
731 sym = alloc_symbol(token->pos, type);
732 bind_symbol(sym, token->ident, namespace);
734 return sym;
739 * Abstract types
741 struct symbol int_type,
742 fp_type;
745 * C types (i.e. actual instances that the abstract types
746 * can map onto)
748 struct symbol bool_ctype, void_ctype, type_ctype,
749 char_ctype, schar_ctype, uchar_ctype,
750 short_ctype, sshort_ctype, ushort_ctype,
751 int_ctype, sint_ctype, uint_ctype,
752 long_ctype, slong_ctype, ulong_ctype,
753 llong_ctype, sllong_ctype, ullong_ctype,
754 int128_ctype, sint128_ctype, uint128_ctype,
755 float_ctype, double_ctype, ldouble_ctype,
756 string_ctype, ptr_ctype, lazy_ptr_ctype,
757 incomplete_ctype, label_ctype, bad_ctype,
758 null_ctype;
759 struct symbol autotype_ctype;
760 struct symbol int_ptr_ctype, uint_ptr_ctype;
761 struct symbol long_ptr_ctype, ulong_ptr_ctype;
762 struct symbol llong_ptr_ctype, ullong_ptr_ctype;
763 struct symbol float32_ctype, float32x_ctype;
764 struct symbol float64_ctype, float64x_ctype;
765 struct symbol float128_ctype;
766 struct symbol const_void_ctype, const_char_ctype;
767 struct symbol const_ptr_ctype, const_string_ctype;
769 struct symbol zero_int;
771 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
772 #define __IDENT(n,str,res) \
773 struct ident n = __INIT_IDENT(str,res)
775 #include "ident-list.h"
777 void init_symbols(void)
779 int stream = init_stream("builtin", -1, includepath);
781 #define __IDENT(n,str,res) \
782 hash_ident(&n)
783 #include "ident-list.h"
785 init_parser(stream);
788 #ifdef __CHAR_UNSIGNED__
789 #define CHAR_SIGNEDNESS MOD_UNSIGNED
790 #else
791 #define CHAR_SIGNEDNESS MOD_SIGNED
792 #endif
793 // For fix-sized types
794 static int bits_in_type32 = 32;
795 static int bits_in_type64 = 64;
796 static int bits_in_type128 = 128;
798 #define T_BASETYPE SYM_BASETYPE, 0, 0, NULL, NULL, NULL
799 #define T_INT(R, S, M) SYM_BASETYPE, M, R, &bits_in_##S, &max_int_alignment, &int_type
800 #define T__INT(R, S) T_INT(R, S, MOD_SIGNED)
801 #define T_SINT(R, S) T_INT(R, S, MOD_ESIGNED)
802 #define T_UINT(R,S) T_INT(R, S, MOD_UNSIGNED)
803 #define T_FLOAT_(R,S,A) SYM_BASETYPE, 0, R, &bits_in_##S, A, &fp_type
804 #define T_FLOAT(R, S) T_FLOAT_(R, S, &max_fp_alignment)
805 #define T_PTR(B) SYM_PTR, 0, 0, &bits_in_pointer, &pointer_alignment, B
806 #define T_NODE(M,B,S,A) SYM_NODE, M, 0, S, A, B
807 #define T_CONST(B,S,A) T_NODE(MOD_CONST, B, S, A)
809 static const struct ctype_declare {
810 struct symbol *ptr;
811 enum type type;
812 unsigned long modifiers;
813 int rank;
814 int *bit_size;
815 int *maxalign;
816 struct symbol *base_type;
817 } ctype_declaration[] = {
818 { &bool_ctype, T_INT(-3, bool, MOD_UNSIGNED) },
819 { &void_ctype, T_BASETYPE },
820 { &type_ctype, T_BASETYPE },
821 { &incomplete_ctype, T_BASETYPE },
822 { &autotype_ctype, T_BASETYPE },
823 { &bad_ctype, T_BASETYPE },
825 { &char_ctype, T_INT(-2, char, CHAR_SIGNEDNESS) },
826 { &schar_ctype, T_SINT(-2, char) },
827 { &uchar_ctype, T_UINT(-2, char) },
828 { &short_ctype, T__INT(-1, short) },
829 { &sshort_ctype, T_SINT(-1, short) },
830 { &ushort_ctype, T_UINT(-1, short) },
831 { &int_ctype, T__INT( 0, int) },
832 { &sint_ctype, T_SINT( 0, int) },
833 { &uint_ctype, T_UINT( 0, int) },
834 { &long_ctype, T__INT( 1, long) },
835 { &slong_ctype, T_SINT( 1, long) },
836 { &ulong_ctype, T_UINT( 1, long) },
837 { &llong_ctype, T__INT( 2, longlong) },
838 { &sllong_ctype, T_SINT( 2, longlong) },
839 { &ullong_ctype, T_UINT( 2, longlong) },
840 { &int128_ctype, T__INT( 3, type128) },
841 { &sint128_ctype, T_SINT( 3, type128) },
842 { &uint128_ctype, T_UINT( 3, type128) },
844 { &float_ctype, T_FLOAT(-1, float) },
845 { &double_ctype, T_FLOAT( 0, double) },
846 { &ldouble_ctype, T_FLOAT( 1, longdouble) },
848 { &float32_ctype, T_FLOAT(-1, type32) },
849 { &float32x_ctype, T_FLOAT(-1, double) },
850 { &float64_ctype, T_FLOAT( 0, type64) },
851 { &float64x_ctype, T_FLOAT( 1, longdouble) },
852 { &float128_ctype, T_FLOAT_(2, type128, &max_alignment) },
854 { &string_ctype, T_PTR(&char_ctype) },
855 { &ptr_ctype, T_PTR(&void_ctype) },
856 { &null_ctype, T_PTR(&void_ctype) },
857 { &label_ctype, T_PTR(&void_ctype) },
858 { &lazy_ptr_ctype, T_PTR(&void_ctype) },
859 { &int_ptr_ctype, T_PTR(&int_ctype) },
860 { &uint_ptr_ctype, T_PTR(&uint_ctype) },
861 { &long_ptr_ctype, T_PTR(&long_ctype) },
862 { &ulong_ptr_ctype, T_PTR(&ulong_ctype) },
863 { &llong_ptr_ctype, T_PTR(&llong_ctype) },
864 { &ullong_ptr_ctype, T_PTR(&ullong_ctype) },
865 { &const_ptr_ctype, T_PTR(&const_void_ctype) },
866 { &const_string_ctype, T_PTR(&const_char_ctype) },
868 { &const_void_ctype, T_CONST(&void_ctype, NULL, NULL) },
869 { &const_char_ctype, T_CONST(&char_ctype, &bits_in_char, &max_int_alignment)},
870 { NULL, }
873 void init_ctype(void)
875 const struct ctype_declare *ctype;
877 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
878 struct symbol *sym = ctype->ptr;
879 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
880 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
881 unsigned long alignment = bits_to_bytes(bit_size);
883 if (alignment > maxalign)
884 alignment = maxalign;
885 sym->type = ctype->type;
886 sym->rank = ctype->rank;
887 sym->bit_size = bit_size;
888 sym->ctype.alignment = alignment;
889 sym->ctype.base_type = ctype->base_type;
890 sym->ctype.modifiers = ctype->modifiers;
892 if (sym->type == SYM_NODE) {
893 struct symbol *base = sym->ctype.base_type;
894 sym->rank = base->rank;
895 if (!ctype->bit_size)
896 sym->bit_size = base->bit_size;
897 if (!ctype->maxalign)
898 sym->ctype.alignment = base->ctype.alignment;
902 // and now some adjustments
903 if (funsigned_char) {
904 char_ctype.ctype.modifiers |= MOD_UNSIGNED;
905 char_ctype.ctype.modifiers &= ~MOD_SIGNED;