function_hooks: don't call db_return_states() early
[smatch.git] / symbol.c
blob4dbb10bd836f1c9a356c963c0527b7220645bbc0
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>
28 #include <assert.h>
30 #include "lib.h"
31 #include "allocate.h"
32 #include "token.h"
33 #include "parse.h"
34 #include "symbol.h"
35 #include "scope.h"
36 #include "expression.h"
37 #include "evaluate.h"
39 #include "target.h"
42 * Secondary symbol list for stuff that needs to be output because it
43 * was used.
45 struct symbol_list *translation_unit_used_list = NULL;
48 * If the symbol is an inline symbol, add it to the list of symbols to parse
50 void access_symbol(struct symbol *sym)
52 if (sym->ctype.modifiers & MOD_INLINE) {
53 if (!sym->accessed) {
54 add_symbol(&translation_unit_used_list, sym);
55 sym->accessed = 1;
60 struct symbol *lookup_symbol(struct ident *ident, enum namespace ns)
62 struct symbol *sym;
64 for (sym = ident->symbols; sym; sym = sym->next_id) {
65 if (sym->namespace & ns) {
66 sym->used = 1;
67 return sym;
70 return NULL;
73 struct symbol *lookup_macro_symbol(const char *macro)
75 struct ident *id;
77 id = built_in_ident(macro);
78 return lookup_symbol(id, NS_MACRO);
81 struct context *alloc_context(void)
83 return __alloc_context(0);
86 struct symbol *alloc_symbol(struct position pos, int type)
88 struct symbol *sym = __alloc_symbol(0);
89 sym->type = type;
90 sym->pos = pos;
91 sym->endpos.type = 0;
92 return sym;
95 struct struct_union_info {
96 unsigned long max_align;
97 unsigned long bit_size;
98 int align_size;
99 char has_flex_array;
100 bool packed;
101 struct symbol *flex_array;
105 * Unions are fairly easy to lay out ;)
107 static void lay_out_union(struct symbol *sym, struct struct_union_info *info)
109 if (sym->bit_size < 0 && is_array_type(sym))
110 sparse_error(sym->pos, "flexible array member '%s' in a union", show_ident(sym->ident));
112 if (sym->bit_size > info->bit_size)
113 info->bit_size = sym->bit_size;
115 sym->offset = 0;
118 static int bitfield_base_size(struct symbol *sym)
120 if (sym->type == SYM_NODE)
121 sym = sym->ctype.base_type;
122 if (sym->type == SYM_BITFIELD)
123 sym = sym->ctype.base_type;
124 return sym->bit_size;
128 * Structures are a bit more interesting to lay out
130 static void lay_out_struct(struct symbol *sym, struct struct_union_info *info)
132 unsigned long bit_size, align_bit_mask;
133 unsigned long alignment;
134 int base_size;
136 bit_size = info->bit_size;
137 base_size = sym->bit_size;
140 * If the member is unsized, either it's a flexible array or
141 * it's invalid and a warning has already been issued.
143 if (base_size < 0) {
144 if (!is_array_type(sym))
145 return;
146 base_size = 0;
147 info->flex_array = sym;
150 alignment = info->packed ? 1 : sym->ctype.alignment;
151 align_bit_mask = bytes_to_bits(alignment) - 1;
154 * Bitfields have some very special rules..
156 if (is_bitfield_type (sym)) {
157 unsigned long bit_offset = bit_size & align_bit_mask;
158 int room = bitfield_base_size(sym) - bit_offset;
159 // Zero-width fields just fill up the unit.
160 int width = base_size ? : (bit_offset ? room : 0);
162 if (width > room && !info->packed) {
163 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
164 bit_offset = 0;
166 sym->offset = bits_to_bytes(bit_size - bit_offset);
167 sym->bit_offset = bit_offset;
168 sym->ctype.base_type->bit_offset = bit_offset;
169 info->bit_size = bit_size + width;
170 // warning (sym->pos, "bitfield: offset=%d:%d size=:%d", sym->offset, sym->bit_offset, width);
172 if (info->packed && sym->type == SYM_NODE)
173 sym->packed = 1;
174 return;
178 * Otherwise, just align it right and add it up..
180 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
181 sym->offset = bits_to_bytes(bit_size);
183 info->bit_size = bit_size + base_size;
184 // warning (sym->pos, "regular: offset=%d", sym->offset);
188 // propagate properties of anonymous structs or unions into their members.
190 // :note: GCC seems to only propagate the qualifiers.
191 // :note: clang doesn't propagate anything at all.
192 static void examine_anonymous_member(struct symbol *sym)
194 unsigned long mod = sym->ctype.modifiers & MOD_QUALIFIER;
195 struct symbol *sub;
197 if (sym->type == SYM_NODE)
198 sym = sym->ctype.base_type;
199 if (sym->type != SYM_STRUCT && sym->type != SYM_UNION)
200 return;
202 FOR_EACH_PTR(sym->symbol_list, sub) {
203 assert(sub->type == SYM_NODE);
204 sub->ctype.modifiers |= mod;
206 // if nested, propagate all the way down
207 if (!sub->ident)
208 examine_anonymous_member(sub);
209 } END_FOR_EACH_PTR(sub);
212 static struct symbol * examine_struct_union_type(struct symbol *sym, int advance)
214 struct struct_union_info info = {
215 .packed = sym->packed,
216 .max_align = 1,
217 .bit_size = 0,
218 .align_size = 1
220 unsigned long bit_size, bit_align;
221 void (*fn)(struct symbol *, struct struct_union_info *);
222 struct symbol *member;
224 fn = advance ? lay_out_struct : lay_out_union;
225 FOR_EACH_PTR(sym->symbol_list, member) {
226 if (member->ctype.base_type == &autotype_ctype) {
227 sparse_error(member->pos, "member '%s' has __auto_type", show_ident(member->ident));
228 member->ctype.base_type = &incomplete_ctype;
230 if (info.flex_array)
231 sparse_error(info.flex_array->pos, "flexible array member '%s' is not last", show_ident(info.flex_array->ident));
232 examine_symbol_type(member);
233 if (!member->ident)
234 examine_anonymous_member(member);
236 if (member->ctype.alignment > info.max_align && !sym->packed) {
237 // Unnamed bitfields do not affect alignment.
238 if (member->ident || !is_bitfield_type(member))
239 info.max_align = member->ctype.alignment;
242 if (has_flexible_array(member))
243 info.has_flex_array = 1;
244 if (has_flexible_array(member) && Wflexible_array_nested)
245 warning(member->pos, "nested flexible array");
246 fn(member, &info);
247 } END_FOR_EACH_PTR(member);
249 if (!sym->ctype.alignment)
250 sym->ctype.alignment = info.max_align;
251 bit_size = info.bit_size;
252 if (info.align_size) {
253 bit_align = bytes_to_bits(sym->ctype.alignment)-1;
254 bit_size = (bit_size + bit_align) & ~bit_align;
256 if (info.flex_array) {
257 info.has_flex_array = 1;
259 if (info.has_flex_array && (!is_union_type(sym) || Wflexible_array_union))
260 sym->has_flex_array = 1;
261 sym->bit_size = bit_size;
262 return sym;
265 static struct symbol *examine_base_type(struct symbol *sym)
267 struct symbol *base_type;
269 if (sym->ctype.base_type == &autotype_ctype) {
270 struct symbol *type = evaluate_expression(sym->initializer);
271 if (!type)
272 type = &bad_ctype;
273 if (is_bitfield_type(type)) {
274 warning(sym->pos, "__auto_type on bitfield");
275 if (type->type == SYM_NODE)
276 type = type->ctype.base_type;
277 type = type->ctype.base_type;
279 sym->ctype.base_type = type;
282 /* Check the base type */
283 base_type = examine_symbol_type(sym->ctype.base_type);
284 if (!base_type || base_type->type == SYM_PTR)
285 return base_type;
286 combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
287 sym->ctype.modifiers |= base_type->ctype.modifiers & MOD_PTRINHERIT;
288 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
289 (struct ptr_list **)&sym->ctype.contexts);
290 if (base_type->type == SYM_NODE) {
291 base_type = base_type->ctype.base_type;
292 sym->ctype.base_type = base_type;
293 sym->rank = base_type->rank;
295 return base_type;
298 static struct symbol * examine_array_type(struct symbol *sym)
300 struct symbol *base_type = examine_base_type(sym);
301 unsigned long bit_size = -1, alignment;
302 struct expression *array_size = sym->array_size;
304 if (!base_type)
305 return sym;
307 if (array_size) {
308 bit_size = array_element_offset(base_type->bit_size,
309 get_expression_value_silent(array_size));
310 if (array_size->type != EXPR_VALUE) {
311 if (Wvla)
312 warning(array_size->pos, "Variable length array is used.");
313 bit_size = -1;
316 if (has_flexible_array(base_type) && Wflexible_array_array)
317 warning(sym->pos, "array of flexible structures");
318 alignment = base_type->ctype.alignment;
319 if (!sym->ctype.alignment)
320 sym->ctype.alignment = alignment;
321 sym->bit_size = bit_size;
322 return sym;
325 static struct symbol *examine_bitfield_type(struct symbol *sym)
327 struct symbol *base_type = examine_base_type(sym);
328 unsigned long alignment, modifiers;
330 if (!base_type)
331 return sym;
332 if (sym->bit_size > base_type->bit_size) {
333 sparse_error(sym->pos, "bitfield '%s' is wider (%d) than its type (%s)",
334 show_ident(sym->ident), sym->bit_size, show_typename(base_type));
335 sym->bit_size = -1;
338 alignment = base_type->ctype.alignment;
339 if (!sym->ctype.alignment)
340 sym->ctype.alignment = alignment;
341 modifiers = base_type->ctype.modifiers;
343 /* use -funsigned-bitfields to determine the sign if not explicit */
344 if (!(modifiers & MOD_EXPLICITLY_SIGNED) && funsigned_bitfields)
345 modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
346 sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
347 return sym;
351 * "typeof" will have to merge the types together
353 void merge_type(struct symbol *sym, struct symbol *base_type)
355 combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
356 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
357 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
358 (struct ptr_list **)&sym->ctype.contexts);
359 sym->ctype.base_type = base_type->ctype.base_type;
360 if (sym->ctype.base_type->type == SYM_NODE)
361 merge_type(sym, sym->ctype.base_type);
364 static bool is_wstring_expr(struct expression *expr)
366 while (expr) {
367 switch (expr->type) {
368 case EXPR_STRING:
369 return 1;
370 case EXPR_INITIALIZER:
371 if (expression_list_size(expr->expr_list) != 1)
372 return 0;
373 expr = first_expression(expr->expr_list);
374 break;
375 case EXPR_PREOP:
376 if (expr->op == '(') {
377 expr = expr->unop;
378 break;
380 default:
381 return 0;
384 return 0;
387 static int count_array_initializer(struct symbol *t, struct expression *expr)
389 int nr = 0;
390 int is_char = 0;
393 * Arrays of character types are special; they can be initialized by
394 * string literal _or_ by string literal in braces. The latter means
395 * that with T x[] = {<string literal>} number of elements in x depends
396 * on T - if it's a character type, we get the length of string literal
397 * (including NUL), otherwise we have one element here.
399 if (t->ctype.base_type == &int_type && t->rank == -2)
400 is_char = 1;
401 else if (t == wchar_ctype && is_wstring_expr(expr))
402 is_char = 1;
404 switch (expr->type) {
405 case EXPR_INITIALIZER: {
406 struct expression *entry;
407 int count = 0;
408 int str_len = 0;
409 FOR_EACH_PTR(expr->expr_list, entry) {
410 count++;
411 switch (entry->type) {
412 case EXPR_INDEX:
413 if (entry->idx_to >= nr)
414 nr = entry->idx_to+1;
415 break;
416 case EXPR_PREOP: {
417 struct expression *e = entry;
418 if (is_char) {
419 while (e && e->type == EXPR_PREOP && e->op == '(')
420 e = e->unop;
421 if (e && e->type == EXPR_STRING) {
422 entry = e;
423 case EXPR_STRING:
424 if (is_char)
425 str_len = entry->string->length;
431 default:
432 nr++;
434 } END_FOR_EACH_PTR(entry);
435 if (count == 1 && str_len)
436 nr = str_len;
437 break;
439 case EXPR_PREOP:
440 if (is_char) {
441 struct expression *e = expr;
442 while (e && e->type == EXPR_PREOP && e->op == '(')
443 e = e->unop;
444 if (e && e->type == EXPR_STRING) {
445 expr = e;
446 case EXPR_STRING:
447 if (is_char)
448 nr = expr->string->length;
451 break;
452 default:
453 break;
455 return nr;
458 static struct expression *get_symbol_initializer(struct symbol *sym)
460 do {
461 if (sym->initializer)
462 return sym->initializer;
463 } while ((sym = sym->same_symbol) != NULL);
464 return NULL;
467 static unsigned int implicit_array_size(struct symbol *node, unsigned int count)
469 struct symbol *arr_ori = node->ctype.base_type;
470 struct symbol *arr_new = alloc_symbol(node->pos, SYM_ARRAY);
471 struct symbol *elem_type = arr_ori->ctype.base_type;
472 struct expression *size = alloc_const_expression(node->pos, count);
473 unsigned int bit_size = array_element_offset(elem_type->bit_size, count);
475 *arr_new = *arr_ori;
476 arr_new->bit_size = bit_size;
477 arr_new->array_size = size;
478 node->array_size = size;
479 node->ctype.base_type = arr_new;
481 return bit_size;
484 static struct symbol * examine_node_type(struct symbol *sym)
486 struct symbol *base_type = examine_base_type(sym);
487 int bit_size;
488 unsigned long alignment;
490 /* SYM_NODE - figure out what the type of the node was.. */
491 bit_size = 0;
492 alignment = 0;
493 if (!base_type)
494 return sym;
496 bit_size = base_type->bit_size;
497 alignment = base_type->ctype.alignment;
499 /* Pick up signedness information into the node */
500 sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
502 if (!sym->ctype.alignment)
503 sym->ctype.alignment = alignment;
505 /* Unsized array? The size might come from the initializer.. */
506 if (bit_size < 0 && base_type->type == SYM_ARRAY) {
507 struct expression *initializer = get_symbol_initializer(sym);
508 if (initializer) {
509 struct symbol *node_type = base_type->ctype.base_type;
510 int count = count_array_initializer(node_type, initializer);
512 if (node_type && node_type->bit_size >= 0)
513 bit_size = implicit_array_size(sym, count);
517 sym->bit_size = bit_size;
518 sym->rank = base_type->rank;
519 return sym;
522 static struct symbol *examine_enum_type(struct symbol *sym)
524 struct symbol *base_type = examine_base_type(sym);
526 sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
527 sym->bit_size = bits_in_enum;
528 if (base_type->bit_size > sym->bit_size)
529 sym->bit_size = base_type->bit_size;
530 sym->ctype.alignment = enum_alignment;
531 if (base_type->ctype.alignment > sym->ctype.alignment)
532 sym->ctype.alignment = base_type->ctype.alignment;
533 return sym;
536 static struct symbol *examine_pointer_type(struct symbol *sym)
539 * Since pointers to incomplete types can be used,
540 * for example in a struct-declaration-list,
541 * the base type must *not* be examined here.
542 * It thus means that it needs to be done later,
543 * when the base type of the pointer is looked at.
545 if (!sym->bit_size)
546 sym->bit_size = bits_in_pointer;
547 if (!sym->ctype.alignment)
548 sym->ctype.alignment = pointer_alignment;
549 return sym;
552 static struct symbol *examine_typeof(struct symbol *sym)
554 struct symbol *base = evaluate_expression(sym->initializer);
555 unsigned long mod = 0;
557 if (!base)
558 base = &bad_ctype;
559 if (base->type == SYM_NODE) {
560 mod |= base->ctype.modifiers & MOD_TYPEOF;
561 base = base->ctype.base_type;
563 if (base->type == SYM_BITFIELD)
564 warning(base->pos, "typeof applied to bitfield type");
565 sym->type = SYM_NODE;
566 sym->ctype.modifiers = mod;
567 sym->ctype.base_type = base;
568 return examine_node_type(sym);
572 * Fill in type size and alignment information for
573 * regular SYM_TYPE things.
575 struct symbol *examine_symbol_type(struct symbol * sym)
577 if (!sym)
578 return sym;
580 /* Already done? */
581 if (sym->examined)
582 return sym;
583 sym->examined = 1;
585 switch (sym->type) {
586 case SYM_FN:
587 case SYM_NODE:
588 return examine_node_type(sym);
589 case SYM_ARRAY:
590 return examine_array_type(sym);
591 case SYM_STRUCT:
592 return examine_struct_union_type(sym, 1);
593 case SYM_UNION:
594 return examine_struct_union_type(sym, 0);
595 case SYM_PTR:
596 return examine_pointer_type(sym);
597 case SYM_ENUM:
598 return examine_enum_type(sym);
599 case SYM_BITFIELD:
600 return examine_bitfield_type(sym);
601 case SYM_BASETYPE:
602 /* Size and alignment had better already be set up */
603 return sym;
604 case SYM_TYPEOF:
605 return examine_typeof(sym);
606 case SYM_PREPROCESSOR:
607 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
608 return NULL;
609 case SYM_UNINITIALIZED:
610 // sparse_error(sym->pos, "ctype on uninitialized symbol '%s'", show_typename(sym));
611 return NULL;
612 case SYM_RESTRICT:
613 examine_base_type(sym);
614 return sym;
615 case SYM_FOULED:
616 examine_base_type(sym);
617 return sym;
618 default:
619 // sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
620 break;
622 return sym;
625 const char* get_type_name(enum type type)
627 const char *type_lookup[] = {
628 [SYM_UNINITIALIZED] = "uninitialized",
629 [SYM_PREPROCESSOR] = "preprocessor",
630 [SYM_BASETYPE] = "basetype",
631 [SYM_NODE] = "node",
632 [SYM_PTR] = "pointer",
633 [SYM_FN] = "function",
634 [SYM_ARRAY] = "array",
635 [SYM_STRUCT] = "struct",
636 [SYM_UNION] = "union",
637 [SYM_ENUM] = "enum",
638 [SYM_TYPEOF] = "typeof",
639 [SYM_BITFIELD] = "bitfield",
640 [SYM_LABEL] = "label",
641 [SYM_RESTRICT] = "restrict",
642 [SYM_FOULED] = "fouled",
643 [SYM_KEYWORD] = "keyword",
644 [SYM_BAD] = "bad"};
646 if (type <= SYM_BAD)
647 return type_lookup[type];
648 else
649 return NULL;
652 struct symbol *examine_pointer_target(struct symbol *sym)
654 return examine_base_type(sym);
657 static struct symbol_list *restr, *fouled;
659 void create_fouled(struct symbol *type)
661 if (type->bit_size < bits_in_int) {
662 struct symbol *new = alloc_symbol(type->pos, type->type);
663 *new = *type;
664 new->bit_size = bits_in_int;
665 new->rank = 0;
666 new->type = SYM_FOULED;
667 new->ctype.base_type = type;
668 add_symbol(&restr, type);
669 add_symbol(&fouled, new);
673 struct symbol *befoul(struct symbol *type)
675 struct symbol *t1, *t2;
676 while (type->type == SYM_NODE)
677 type = type->ctype.base_type;
678 PREPARE_PTR_LIST(restr, t1);
679 PREPARE_PTR_LIST(fouled, t2);
680 for (;;) {
681 if (t1 == type)
682 return t2;
683 if (!t1)
684 break;
685 NEXT_PTR_LIST(t1);
686 NEXT_PTR_LIST(t2);
688 FINISH_PTR_LIST(t2);
689 FINISH_PTR_LIST(t1);
690 return NULL;
693 static void inherit_declaration(struct symbol *sym, struct symbol *prev)
695 unsigned long mods = prev->ctype.modifiers;
697 // inherit function attributes
698 sym->ctype.modifiers |= mods & MOD_FUN_ATTR;
701 void check_declaration(struct symbol *sym)
703 int warned = 0;
704 struct symbol *next = sym;
706 while ((next = next->next_id) != NULL) {
707 if (next->namespace != sym->namespace)
708 continue;
709 if (sym->scope == next->scope) {
710 sym->same_symbol = next;
711 inherit_declaration(sym, next);
712 return;
714 /* Extern in block level matches a TOPLEVEL non-static symbol */
715 if (sym->ctype.modifiers & MOD_EXTERN) {
716 if ((next->ctype.modifiers & (MOD_TOPLEVEL|MOD_STATIC)) == MOD_TOPLEVEL) {
717 sym->same_symbol = next;
718 return;
722 if (!Wshadow || warned)
723 continue;
724 if (get_sym_type(next) == SYM_FN)
725 continue;
726 warned = 1;
727 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
728 info(next->pos, "originally declared here");
732 static void inherit_static(struct symbol *sym)
734 struct symbol *prev;
736 // only 'plain' symbols are concerned
737 if (sym->ctype.modifiers & (MOD_STATIC|MOD_EXTERN))
738 return;
740 for (prev = sym->next_id; prev; prev = prev->next_id) {
741 if (prev->namespace != NS_SYMBOL)
742 continue;
743 if (prev->scope != file_scope)
744 continue;
746 sym->ctype.modifiers |= prev->ctype.modifiers & MOD_STATIC;
748 // previous declarations are already converted
749 return;
753 void bind_symbol_with_scope(struct symbol *sym, struct ident *ident, enum namespace ns, struct scope *scope)
755 if (sym->bound) {
756 sparse_error(sym->pos, "internal error: symbol type already bound");
757 return;
759 if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
760 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
761 return;
763 sym->namespace = ns;
764 sym->next_id = ident->symbols;
765 ident->symbols = sym;
766 if (sym->ident && sym->ident != ident)
767 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
768 sym->ident = ident;
769 sym->bound = 1;
771 if (ns == NS_SYMBOL && toplevel(scope)) {
772 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
774 inherit_static(sym);
776 scope = global_scope;
777 if (sym->ctype.modifiers & MOD_STATIC ||
778 is_extern_inline(sym)) {
779 scope = file_scope;
780 mod = MOD_TOPLEVEL;
782 sym->ctype.modifiers |= mod;
784 bind_scope(sym, scope);
787 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
789 struct scope *scope = block_scope;;
791 if (ns == NS_MACRO)
792 scope = file_scope;
793 if (ns == NS_LABEL)
794 scope = function_scope;
795 bind_symbol_with_scope(sym, ident, ns, scope);
798 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
800 struct ident *ident = built_in_ident(name);
801 struct symbol *sym = lookup_symbol(ident, namespace);
803 if (sym && sym->type != type)
804 die("symbol %s created with different types: %d old %d", name,
805 type, sym->type);
807 if (!sym) {
808 struct token *token = built_in_token(stream, ident);
810 sym = alloc_symbol(token->pos, type);
811 bind_symbol(sym, token->ident, namespace);
813 return sym;
818 * Abstract types
820 struct symbol int_type,
821 fp_type;
824 * C types (i.e. actual instances that the abstract types
825 * can map onto)
827 struct symbol bool_ctype, void_ctype, type_ctype,
828 char_ctype, schar_ctype, uchar_ctype,
829 short_ctype, sshort_ctype, ushort_ctype,
830 int_ctype, sint_ctype, uint_ctype,
831 long_ctype, slong_ctype, ulong_ctype,
832 llong_ctype, sllong_ctype, ullong_ctype,
833 int128_ctype, sint128_ctype, uint128_ctype,
834 float_ctype, double_ctype, ldouble_ctype,
835 string_ctype, ptr_ctype, lazy_ptr_ctype,
836 incomplete_ctype, label_ctype, bad_ctype,
837 null_ctype;
838 struct symbol autotype_ctype;
839 struct symbol schar_ptr_ctype, short_ptr_ctype;
840 struct symbol int_ptr_ctype, uint_ptr_ctype;
841 struct symbol long_ptr_ctype, ulong_ptr_ctype;
842 struct symbol llong_ptr_ctype, ullong_ptr_ctype;
843 struct symbol size_t_ptr_ctype, intmax_ptr_ctype, ptrdiff_ptr_ctype;
844 struct symbol float32_ctype, float32x_ctype;
845 struct symbol float64_ctype, float64x_ctype;
846 struct symbol float128_ctype;
847 struct symbol const_void_ctype, const_char_ctype;
848 struct symbol const_ptr_ctype, const_string_ctype;
849 struct symbol const_wchar_ctype, const_wstring_ctype;
850 struct symbol volatile_void_ctype, volatile_ptr_ctype;
851 struct symbol volatile_bool_ctype, volatile_bool_ptr_ctype;
853 struct symbol zero_int;
855 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
856 #define __IDENT(n,str,res) \
857 struct ident n = __INIT_IDENT(str,res)
859 #include "ident-list.h"
861 void init_symbols(void)
863 int stream = init_stream(NULL, "builtin", -1, includepath);
865 #define __IDENT(n,str,res) \
866 hash_ident(&n)
867 #include "ident-list.h"
869 init_parser(stream);
872 #ifdef __CHAR_UNSIGNED__
873 #define CHAR_SIGNEDNESS MOD_UNSIGNED
874 #else
875 #define CHAR_SIGNEDNESS MOD_SIGNED
876 #endif
877 // For fix-sized types
878 static int bits_in_type32 = 32;
879 static int bits_in_type64 = 64;
880 static int bits_in_type128 = 128;
882 #define T_BASETYPE SYM_BASETYPE, 0, 0, NULL, NULL, NULL
883 #define T_INT(R, S, M) SYM_BASETYPE, M, R, &bits_in_##S, &max_int_alignment, &int_type
884 #define T__INT(R, S) T_INT(R, S, MOD_SIGNED)
885 #define T_SINT(R, S) T_INT(R, S, MOD_ESIGNED)
886 #define T_UINT(R,S) T_INT(R, S, MOD_UNSIGNED)
887 #define T_FLOAT_(R,S,A) SYM_BASETYPE, 0, R, &bits_in_##S, A, &fp_type
888 #define T_FLOAT(R, S) T_FLOAT_(R, S, &max_fp_alignment)
889 #define T_PTR(B) SYM_PTR, 0, 0, &bits_in_pointer, &pointer_alignment, B
890 #define T_NODE(M,B,S,A) SYM_NODE, M, 0, S, A, B
891 #define T_CONST(B,S,A) T_NODE(MOD_CONST, B, S, A)
893 static const struct ctype_declare {
894 struct symbol *ptr;
895 enum type type;
896 unsigned long modifiers;
897 int rank;
898 int *bit_size;
899 int *maxalign;
900 struct symbol *base_type;
901 } ctype_declaration[] = {
902 { &bool_ctype, T_INT(-3, bool, MOD_UNSIGNED) },
903 { &void_ctype, T_BASETYPE },
904 { &type_ctype, T_BASETYPE },
905 { &incomplete_ctype, T_BASETYPE },
906 { &autotype_ctype, T_BASETYPE },
907 { &bad_ctype, T_BASETYPE },
909 { &char_ctype, T_INT(-2, char, CHAR_SIGNEDNESS) },
910 { &schar_ctype, T_SINT(-2, char) },
911 { &uchar_ctype, T_UINT(-2, char) },
912 { &short_ctype, T__INT(-1, short) },
913 { &sshort_ctype, T_SINT(-1, short) },
914 { &ushort_ctype, T_UINT(-1, short) },
915 { &int_ctype, T__INT( 0, int) },
916 { &sint_ctype, T_SINT( 0, int) },
917 { &uint_ctype, T_UINT( 0, int) },
918 { &long_ctype, T__INT( 1, long) },
919 { &slong_ctype, T_SINT( 1, long) },
920 { &ulong_ctype, T_UINT( 1, long) },
921 { &llong_ctype, T__INT( 2, longlong) },
922 { &sllong_ctype, T_SINT( 2, longlong) },
923 { &ullong_ctype, T_UINT( 2, longlong) },
924 { &int128_ctype, T__INT( 3, type128) },
925 { &sint128_ctype, T_SINT( 3, type128) },
926 { &uint128_ctype, T_UINT( 3, type128) },
928 { &float_ctype, T_FLOAT(-1, float) },
929 { &double_ctype, T_FLOAT( 0, double) },
930 { &ldouble_ctype, T_FLOAT( 1, longdouble) },
932 { &float32_ctype, T_FLOAT(-1, type32) },
933 { &float32x_ctype, T_FLOAT(-1, double) },
934 { &float64_ctype, T_FLOAT( 0, type64) },
935 { &float64x_ctype, T_FLOAT( 1, longdouble) },
936 { &float128_ctype, T_FLOAT_(2, type128, &max_alignment) },
938 { &string_ctype, T_PTR(&char_ctype) },
939 { &ptr_ctype, T_PTR(&void_ctype) },
940 { &null_ctype, T_PTR(&void_ctype) },
941 { &label_ctype, T_PTR(&void_ctype) },
942 { &lazy_ptr_ctype, T_PTR(&void_ctype) },
943 { &schar_ptr_ctype, T_PTR(&schar_ctype) },
944 { &short_ptr_ctype, T_PTR(&short_ctype) },
945 { &int_ptr_ctype, T_PTR(&int_ctype) },
946 { &uint_ptr_ctype, T_PTR(&uint_ctype) },
947 { &long_ptr_ctype, T_PTR(&long_ctype) },
948 { &ulong_ptr_ctype, T_PTR(&ulong_ctype) },
949 { &llong_ptr_ctype, T_PTR(&llong_ctype) },
950 { &ullong_ptr_ctype, T_PTR(&ullong_ctype) },
951 { &size_t_ptr_ctype, T_PTR(&void_ctype) }, // will be adjusted
952 { &intmax_ptr_ctype, T_PTR(&void_ctype) }, // will be adjusted
953 { &ptrdiff_ptr_ctype, T_PTR(&void_ctype) }, // will be adjusted
954 { &const_ptr_ctype, T_PTR(&const_void_ctype) },
955 { &const_string_ctype, T_PTR(&const_char_ctype) },
956 { &const_wstring_ctype,T_PTR(&const_wchar_ctype) },
958 { &const_void_ctype, T_CONST(&void_ctype, NULL, NULL) },
959 { &const_char_ctype, T_CONST(&char_ctype, &bits_in_char, &max_int_alignment)},
960 { &const_wchar_ctype, T_CONST(&int_ctype, NULL, NULL) },
961 { &volatile_void_ctype,T_NODE(MOD_VOLATILE, &void_ctype, NULL, NULL) },
962 { &volatile_ptr_ctype, T_PTR(&volatile_void_ctype) },
963 { &volatile_bool_ctype,T_NODE(MOD_VOLATILE, &bool_ctype, NULL, NULL) },
964 { &volatile_bool_ptr_ctype, T_PTR(&volatile_bool_ctype) },
965 { NULL, }
968 void init_ctype(void)
970 const struct ctype_declare *ctype;
972 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
973 struct symbol *sym = ctype->ptr;
974 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
975 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
976 unsigned long alignment = bits_to_bytes(bit_size);
978 if (alignment > maxalign)
979 alignment = maxalign;
980 sym->type = ctype->type;
981 sym->rank = ctype->rank;
982 sym->bit_size = bit_size;
983 sym->ctype.alignment = alignment;
984 sym->ctype.base_type = ctype->base_type;
985 sym->ctype.modifiers = ctype->modifiers;
987 if (sym->type == SYM_NODE) {
988 struct symbol *base = sym->ctype.base_type;
989 sym->rank = base->rank;
990 if (!ctype->bit_size)
991 sym->bit_size = base->bit_size;
992 if (!ctype->maxalign)
993 sym->ctype.alignment = base->ctype.alignment;
997 // and now some adjustments
998 if (funsigned_char) {
999 char_ctype.ctype.modifiers |= MOD_UNSIGNED;
1000 char_ctype.ctype.modifiers &= ~MOD_SIGNED;
1003 if (!ptrdiff_ctype)
1004 ptrdiff_ctype = ssize_t_ctype;
1005 if (!intptr_ctype)
1006 intptr_ctype = ssize_t_ctype;
1007 if (!uintptr_ctype)
1008 uintptr_ctype = size_t_ctype;
1010 size_t_ptr_ctype.ctype.base_type = size_t_ctype;
1011 intmax_ptr_ctype.ctype.base_type = intmax_ctype;
1012 ptrdiff_ptr_ctype.ctype.base_type = ptrdiff_ctype;
1014 const_wchar_ctype.ctype.base_type = wchar_ctype;
1015 const_wchar_ctype.rank = wchar_ctype->rank;
1016 const_wchar_ctype.ctype.alignment = wchar_ctype->ctype.alignment;
1017 const_wchar_ctype.bit_size = wchar_ctype->bit_size;