return_to_param: delete debug code
[smatch.git] / symbol.c
blobabbcc53347a2e5596c6d5080e7c428b3b137484f
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 bool is_wstring_expr(struct expression *expr)
312 while (expr) {
313 switch (expr->type) {
314 case EXPR_STRING:
315 return 1;
316 case EXPR_INITIALIZER:
317 if (expression_list_size(expr->expr_list) != 1)
318 return 0;
319 expr = first_expression(expr->expr_list);
320 break;
321 case EXPR_PREOP:
322 if (expr->op == '(') {
323 expr = expr->unop;
324 break;
326 default:
327 return 0;
330 return 0;
333 static int count_array_initializer(struct symbol *t, struct expression *expr)
335 int nr = 0;
336 int is_char = 0;
339 * Arrays of character types are special; they can be initialized by
340 * string literal _or_ by string literal in braces. The latter means
341 * that with T x[] = {<string literal>} number of elements in x depends
342 * on T - if it's a character type, we get the length of string literal
343 * (including NUL), otherwise we have one element here.
345 if (t->ctype.base_type == &int_type && t->rank == -2)
346 is_char = 1;
347 else if (t == wchar_ctype && is_wstring_expr(expr))
348 is_char = 1;
350 switch (expr->type) {
351 case EXPR_INITIALIZER: {
352 struct expression *entry;
353 int count = 0;
354 int str_len = 0;
355 FOR_EACH_PTR(expr->expr_list, entry) {
356 count++;
357 switch (entry->type) {
358 case EXPR_INDEX:
359 if (entry->idx_to >= nr)
360 nr = entry->idx_to+1;
361 break;
362 case EXPR_PREOP: {
363 struct expression *e = entry;
364 if (is_char) {
365 while (e && e->type == EXPR_PREOP && e->op == '(')
366 e = e->unop;
367 if (e && e->type == EXPR_STRING) {
368 entry = e;
369 case EXPR_STRING:
370 if (is_char)
371 str_len = entry->string->length;
377 default:
378 nr++;
380 } END_FOR_EACH_PTR(entry);
381 if (count == 1 && str_len)
382 nr = str_len;
383 break;
385 case EXPR_PREOP:
386 if (is_char) {
387 struct expression *e = expr;
388 while (e && e->type == EXPR_PREOP && e->op == '(')
389 e = e->unop;
390 if (e && e->type == EXPR_STRING) {
391 expr = e;
392 case EXPR_STRING:
393 if (is_char)
394 nr = expr->string->length;
397 break;
398 default:
399 break;
401 return nr;
404 static struct expression *get_symbol_initializer(struct symbol *sym)
406 do {
407 if (sym->initializer)
408 return sym->initializer;
409 } while ((sym = sym->same_symbol) != NULL);
410 return NULL;
413 static unsigned int implicit_array_size(struct symbol *node, unsigned int count)
415 struct symbol *arr_ori = node->ctype.base_type;
416 struct symbol *arr_new = alloc_symbol(node->pos, SYM_ARRAY);
417 struct symbol *elem_type = arr_ori->ctype.base_type;
418 struct expression *size = alloc_const_expression(node->pos, count);
419 unsigned int bit_size = array_element_offset(elem_type->bit_size, count);
421 *arr_new = *arr_ori;
422 arr_new->bit_size = bit_size;
423 arr_new->array_size = size;
424 node->array_size = size;
425 node->ctype.base_type = arr_new;
427 return bit_size;
430 static struct symbol * examine_node_type(struct symbol *sym)
432 struct symbol *base_type = examine_base_type(sym);
433 int bit_size;
434 unsigned long alignment;
436 /* SYM_NODE - figure out what the type of the node was.. */
437 bit_size = 0;
438 alignment = 0;
439 if (!base_type)
440 return sym;
442 bit_size = base_type->bit_size;
443 alignment = base_type->ctype.alignment;
445 /* Pick up signedness information into the node */
446 sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
448 if (!sym->ctype.alignment)
449 sym->ctype.alignment = alignment;
451 /* Unsized array? The size might come from the initializer.. */
452 if (bit_size < 0 && base_type->type == SYM_ARRAY) {
453 struct expression *initializer = get_symbol_initializer(sym);
454 if (initializer) {
455 struct symbol *node_type = base_type->ctype.base_type;
456 int count = count_array_initializer(node_type, initializer);
458 if (node_type && node_type->bit_size >= 0)
459 bit_size = implicit_array_size(sym, count);
463 sym->bit_size = bit_size;
464 sym->rank = base_type->rank;
465 return sym;
468 static struct symbol *examine_enum_type(struct symbol *sym)
470 struct symbol *base_type = examine_base_type(sym);
472 sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
473 sym->bit_size = bits_in_enum;
474 if (base_type->bit_size > sym->bit_size)
475 sym->bit_size = base_type->bit_size;
476 sym->ctype.alignment = enum_alignment;
477 if (base_type->ctype.alignment > sym->ctype.alignment)
478 sym->ctype.alignment = base_type->ctype.alignment;
479 return sym;
482 static struct symbol *examine_pointer_type(struct symbol *sym)
485 * Since pointers to incomplete types can be used,
486 * for example in a struct-declaration-list,
487 * the base type must *not* be examined here.
488 * It thus means that it needs to be done later,
489 * when the base type of the pointer is looked at.
491 if (!sym->bit_size)
492 sym->bit_size = bits_in_pointer;
493 if (!sym->ctype.alignment)
494 sym->ctype.alignment = pointer_alignment;
495 return sym;
498 static struct symbol *examine_typeof(struct symbol *sym)
500 struct symbol *base = evaluate_expression(sym->initializer);
501 unsigned long mod = 0;
503 if (!base)
504 base = &bad_ctype;
505 if (base->type == SYM_NODE) {
506 mod |= base->ctype.modifiers & MOD_TYPEOF;
507 base = base->ctype.base_type;
509 if (base->type == SYM_BITFIELD)
510 warning(base->pos, "typeof applied to bitfield type");
511 sym->type = SYM_NODE;
512 sym->ctype.modifiers = mod;
513 sym->ctype.base_type = base;
514 return examine_node_type(sym);
518 * Fill in type size and alignment information for
519 * regular SYM_TYPE things.
521 struct symbol *examine_symbol_type(struct symbol * sym)
523 if (!sym)
524 return sym;
526 /* Already done? */
527 if (sym->examined)
528 return sym;
529 sym->examined = 1;
531 switch (sym->type) {
532 case SYM_FN:
533 case SYM_NODE:
534 return examine_node_type(sym);
535 case SYM_ARRAY:
536 return examine_array_type(sym);
537 case SYM_STRUCT:
538 return examine_struct_union_type(sym, 1);
539 case SYM_UNION:
540 return examine_struct_union_type(sym, 0);
541 case SYM_PTR:
542 return examine_pointer_type(sym);
543 case SYM_ENUM:
544 return examine_enum_type(sym);
545 case SYM_BITFIELD:
546 return examine_bitfield_type(sym);
547 case SYM_BASETYPE:
548 /* Size and alignment had better already be set up */
549 return sym;
550 case SYM_TYPEOF:
551 return examine_typeof(sym);
552 case SYM_PREPROCESSOR:
553 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
554 return NULL;
555 case SYM_UNINITIALIZED:
556 // sparse_error(sym->pos, "ctype on uninitialized symbol '%s'", show_typename(sym));
557 return NULL;
558 case SYM_RESTRICT:
559 examine_base_type(sym);
560 return sym;
561 case SYM_FOULED:
562 examine_base_type(sym);
563 return sym;
564 default:
565 // sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
566 break;
568 return sym;
571 const char* get_type_name(enum type type)
573 const char *type_lookup[] = {
574 [SYM_UNINITIALIZED] = "uninitialized",
575 [SYM_PREPROCESSOR] = "preprocessor",
576 [SYM_BASETYPE] = "basetype",
577 [SYM_NODE] = "node",
578 [SYM_PTR] = "pointer",
579 [SYM_FN] = "function",
580 [SYM_ARRAY] = "array",
581 [SYM_STRUCT] = "struct",
582 [SYM_UNION] = "union",
583 [SYM_ENUM] = "enum",
584 [SYM_TYPEOF] = "typeof",
585 [SYM_BITFIELD] = "bitfield",
586 [SYM_LABEL] = "label",
587 [SYM_RESTRICT] = "restrict",
588 [SYM_FOULED] = "fouled",
589 [SYM_KEYWORD] = "keyword",
590 [SYM_BAD] = "bad"};
592 if (type <= SYM_BAD)
593 return type_lookup[type];
594 else
595 return NULL;
598 struct symbol *examine_pointer_target(struct symbol *sym)
600 return examine_base_type(sym);
603 static struct symbol_list *restr, *fouled;
605 void create_fouled(struct symbol *type)
607 if (type->bit_size < bits_in_int) {
608 struct symbol *new = alloc_symbol(type->pos, type->type);
609 *new = *type;
610 new->bit_size = bits_in_int;
611 new->rank = 0;
612 new->type = SYM_FOULED;
613 new->ctype.base_type = type;
614 add_symbol(&restr, type);
615 add_symbol(&fouled, new);
619 struct symbol *befoul(struct symbol *type)
621 struct symbol *t1, *t2;
622 while (type->type == SYM_NODE)
623 type = type->ctype.base_type;
624 PREPARE_PTR_LIST(restr, t1);
625 PREPARE_PTR_LIST(fouled, t2);
626 for (;;) {
627 if (t1 == type)
628 return t2;
629 if (!t1)
630 break;
631 NEXT_PTR_LIST(t1);
632 NEXT_PTR_LIST(t2);
634 FINISH_PTR_LIST(t2);
635 FINISH_PTR_LIST(t1);
636 return NULL;
639 static void inherit_declaration(struct symbol *sym, struct symbol *prev)
641 unsigned long mods = prev->ctype.modifiers;
643 // inherit function attributes
644 sym->ctype.modifiers |= mods & MOD_FUN_ATTR;
647 void check_declaration(struct symbol *sym)
649 int warned = 0;
650 struct symbol *next = sym;
652 while ((next = next->next_id) != NULL) {
653 if (next->namespace != sym->namespace)
654 continue;
655 if (sym->scope == next->scope) {
656 sym->same_symbol = next;
657 inherit_declaration(sym, next);
658 return;
660 /* Extern in block level matches a TOPLEVEL non-static symbol */
661 if (sym->ctype.modifiers & MOD_EXTERN) {
662 if ((next->ctype.modifiers & (MOD_TOPLEVEL|MOD_STATIC)) == MOD_TOPLEVEL) {
663 sym->same_symbol = next;
664 return;
668 if (!Wshadow || warned)
669 continue;
670 if (get_sym_type(next) == SYM_FN)
671 continue;
672 warned = 1;
673 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
674 info(next->pos, "originally declared here");
678 static void inherit_static(struct symbol *sym)
680 struct symbol *prev;
682 // only 'plain' symbols are concerned
683 if (sym->ctype.modifiers & (MOD_STATIC|MOD_EXTERN))
684 return;
686 for (prev = sym->next_id; prev; prev = prev->next_id) {
687 if (prev->namespace != NS_SYMBOL)
688 continue;
689 if (prev->scope != file_scope)
690 continue;
692 sym->ctype.modifiers |= prev->ctype.modifiers & MOD_STATIC;
694 // previous declarations are already converted
695 return;
699 void bind_symbol_with_scope(struct symbol *sym, struct ident *ident, enum namespace ns, struct scope *scope)
701 if (sym->bound) {
702 sparse_error(sym->pos, "internal error: symbol type already bound");
703 return;
705 if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
706 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
707 return;
709 sym->namespace = ns;
710 sym->next_id = ident->symbols;
711 ident->symbols = sym;
712 if (sym->ident && sym->ident != ident)
713 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
714 sym->ident = ident;
715 sym->bound = 1;
717 if (ns == NS_SYMBOL && toplevel(scope)) {
718 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
720 inherit_static(sym);
722 scope = global_scope;
723 if (sym->ctype.modifiers & MOD_STATIC ||
724 is_extern_inline(sym)) {
725 scope = file_scope;
726 mod = MOD_TOPLEVEL;
728 sym->ctype.modifiers |= mod;
730 bind_scope(sym, scope);
733 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
735 struct scope *scope = block_scope;;
737 if (ns == NS_MACRO)
738 scope = file_scope;
739 if (ns == NS_LABEL)
740 scope = function_scope;
741 bind_symbol_with_scope(sym, ident, ns, scope);
744 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
746 struct ident *ident = built_in_ident(name);
747 struct symbol *sym = lookup_symbol(ident, namespace);
749 if (sym && sym->type != type)
750 die("symbol %s created with different types: %d old %d", name,
751 type, sym->type);
753 if (!sym) {
754 struct token *token = built_in_token(stream, ident);
756 sym = alloc_symbol(token->pos, type);
757 bind_symbol(sym, token->ident, namespace);
759 return sym;
764 * Abstract types
766 struct symbol int_type,
767 fp_type;
770 * C types (i.e. actual instances that the abstract types
771 * can map onto)
773 struct symbol bool_ctype, void_ctype, type_ctype,
774 char_ctype, schar_ctype, uchar_ctype,
775 short_ctype, sshort_ctype, ushort_ctype,
776 int_ctype, sint_ctype, uint_ctype,
777 long_ctype, slong_ctype, ulong_ctype,
778 llong_ctype, sllong_ctype, ullong_ctype,
779 int128_ctype, sint128_ctype, uint128_ctype,
780 float_ctype, double_ctype, ldouble_ctype,
781 string_ctype, ptr_ctype, lazy_ptr_ctype,
782 incomplete_ctype, label_ctype, bad_ctype,
783 null_ctype;
784 struct symbol autotype_ctype;
785 struct symbol int_ptr_ctype, uint_ptr_ctype;
786 struct symbol long_ptr_ctype, ulong_ptr_ctype;
787 struct symbol llong_ptr_ctype, ullong_ptr_ctype;
788 struct symbol float32_ctype, float32x_ctype;
789 struct symbol float64_ctype, float64x_ctype;
790 struct symbol float128_ctype;
791 struct symbol const_void_ctype, const_char_ctype;
792 struct symbol const_ptr_ctype, const_string_ctype;
794 struct symbol zero_int;
796 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
797 #define __IDENT(n,str,res) \
798 struct ident n = __INIT_IDENT(str,res)
800 #include "ident-list.h"
802 void init_symbols(void)
804 int stream = init_stream(NULL, "builtin", -1, includepath);
806 #define __IDENT(n,str,res) \
807 hash_ident(&n)
808 #include "ident-list.h"
810 init_parser(stream);
813 #ifdef __CHAR_UNSIGNED__
814 #define CHAR_SIGNEDNESS MOD_UNSIGNED
815 #else
816 #define CHAR_SIGNEDNESS MOD_SIGNED
817 #endif
818 // For fix-sized types
819 static int bits_in_type32 = 32;
820 static int bits_in_type64 = 64;
821 static int bits_in_type128 = 128;
823 #define T_BASETYPE SYM_BASETYPE, 0, 0, NULL, NULL, NULL
824 #define T_INT(R, S, M) SYM_BASETYPE, M, R, &bits_in_##S, &max_int_alignment, &int_type
825 #define T__INT(R, S) T_INT(R, S, MOD_SIGNED)
826 #define T_SINT(R, S) T_INT(R, S, MOD_ESIGNED)
827 #define T_UINT(R,S) T_INT(R, S, MOD_UNSIGNED)
828 #define T_FLOAT_(R,S,A) SYM_BASETYPE, 0, R, &bits_in_##S, A, &fp_type
829 #define T_FLOAT(R, S) T_FLOAT_(R, S, &max_fp_alignment)
830 #define T_PTR(B) SYM_PTR, 0, 0, &bits_in_pointer, &pointer_alignment, B
831 #define T_NODE(M,B,S,A) SYM_NODE, M, 0, S, A, B
832 #define T_CONST(B,S,A) T_NODE(MOD_CONST, B, S, A)
834 static const struct ctype_declare {
835 struct symbol *ptr;
836 enum type type;
837 unsigned long modifiers;
838 int rank;
839 int *bit_size;
840 int *maxalign;
841 struct symbol *base_type;
842 } ctype_declaration[] = {
843 { &bool_ctype, T_INT(-3, bool, MOD_UNSIGNED) },
844 { &void_ctype, T_BASETYPE },
845 { &type_ctype, T_BASETYPE },
846 { &incomplete_ctype, T_BASETYPE },
847 { &autotype_ctype, T_BASETYPE },
848 { &bad_ctype, T_BASETYPE },
850 { &char_ctype, T_INT(-2, char, CHAR_SIGNEDNESS) },
851 { &schar_ctype, T_SINT(-2, char) },
852 { &uchar_ctype, T_UINT(-2, char) },
853 { &short_ctype, T__INT(-1, short) },
854 { &sshort_ctype, T_SINT(-1, short) },
855 { &ushort_ctype, T_UINT(-1, short) },
856 { &int_ctype, T__INT( 0, int) },
857 { &sint_ctype, T_SINT( 0, int) },
858 { &uint_ctype, T_UINT( 0, int) },
859 { &long_ctype, T__INT( 1, long) },
860 { &slong_ctype, T_SINT( 1, long) },
861 { &ulong_ctype, T_UINT( 1, long) },
862 { &llong_ctype, T__INT( 2, longlong) },
863 { &sllong_ctype, T_SINT( 2, longlong) },
864 { &ullong_ctype, T_UINT( 2, longlong) },
865 { &int128_ctype, T__INT( 3, type128) },
866 { &sint128_ctype, T_SINT( 3, type128) },
867 { &uint128_ctype, T_UINT( 3, type128) },
869 { &float_ctype, T_FLOAT(-1, float) },
870 { &double_ctype, T_FLOAT( 0, double) },
871 { &ldouble_ctype, T_FLOAT( 1, longdouble) },
873 { &float32_ctype, T_FLOAT(-1, type32) },
874 { &float32x_ctype, T_FLOAT(-1, double) },
875 { &float64_ctype, T_FLOAT( 0, type64) },
876 { &float64x_ctype, T_FLOAT( 1, longdouble) },
877 { &float128_ctype, T_FLOAT_(2, type128, &max_alignment) },
879 { &string_ctype, T_PTR(&char_ctype) },
880 { &ptr_ctype, T_PTR(&void_ctype) },
881 { &null_ctype, T_PTR(&void_ctype) },
882 { &label_ctype, T_PTR(&void_ctype) },
883 { &lazy_ptr_ctype, T_PTR(&void_ctype) },
884 { &int_ptr_ctype, T_PTR(&int_ctype) },
885 { &uint_ptr_ctype, T_PTR(&uint_ctype) },
886 { &long_ptr_ctype, T_PTR(&long_ctype) },
887 { &ulong_ptr_ctype, T_PTR(&ulong_ctype) },
888 { &llong_ptr_ctype, T_PTR(&llong_ctype) },
889 { &ullong_ptr_ctype, T_PTR(&ullong_ctype) },
890 { &const_ptr_ctype, T_PTR(&const_void_ctype) },
891 { &const_string_ctype, T_PTR(&const_char_ctype) },
893 { &const_void_ctype, T_CONST(&void_ctype, NULL, NULL) },
894 { &const_char_ctype, T_CONST(&char_ctype, &bits_in_char, &max_int_alignment)},
895 { NULL, }
898 void init_ctype(void)
900 const struct ctype_declare *ctype;
902 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
903 struct symbol *sym = ctype->ptr;
904 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
905 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
906 unsigned long alignment = bits_to_bytes(bit_size);
908 if (alignment > maxalign)
909 alignment = maxalign;
910 sym->type = ctype->type;
911 sym->rank = ctype->rank;
912 sym->bit_size = bit_size;
913 sym->ctype.alignment = alignment;
914 sym->ctype.base_type = ctype->base_type;
915 sym->ctype.modifiers = ctype->modifiers;
917 if (sym->type == SYM_NODE) {
918 struct symbol *base = sym->ctype.base_type;
919 sym->rank = base->rank;
920 if (!ctype->bit_size)
921 sym->bit_size = base->bit_size;
922 if (!ctype->maxalign)
923 sym->ctype.alignment = base->ctype.alignment;
927 // and now some adjustments
928 if (funsigned_char) {
929 char_ctype.ctype.modifiers |= MOD_UNSIGNED;
930 char_ctype.ctype.modifiers &= ~MOD_SIGNED;
933 if (!ptrdiff_ctype)
934 ptrdiff_ctype = ssize_t_ctype;
935 if (!intptr_ctype)
936 intptr_ctype = ssize_t_ctype;
937 if (!uintptr_ctype)
938 uintptr_ctype = size_t_ctype;