fix missing itype in SEL(x, 0/1, 1/0) --> (x ==/!= 0)
[smatch.git] / symbol.c
blob91352a3a447bbb64bc3e617302881b2f459614de
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 context *alloc_context(void)
75 return __alloc_context(0);
78 struct symbol *alloc_symbol(struct position pos, int type)
80 struct symbol *sym = __alloc_symbol(0);
81 sym->type = type;
82 sym->pos = pos;
83 sym->endpos.type = 0;
84 return sym;
87 struct struct_union_info {
88 unsigned long max_align;
89 unsigned long bit_size;
90 int align_size;
91 char has_flex_array;
92 bool packed;
93 struct symbol *flex_array;
97 * Unions are fairly easy to lay out ;)
99 static void lay_out_union(struct symbol *sym, struct struct_union_info *info)
101 if (sym->bit_size < 0 && is_array_type(sym))
102 sparse_error(sym->pos, "flexible array member '%s' in a union", show_ident(sym->ident));
104 if (sym->bit_size > info->bit_size)
105 info->bit_size = sym->bit_size;
107 sym->offset = 0;
110 static int bitfield_base_size(struct symbol *sym)
112 if (sym->type == SYM_NODE)
113 sym = sym->ctype.base_type;
114 if (sym->type == SYM_BITFIELD)
115 sym = sym->ctype.base_type;
116 return sym->bit_size;
120 * Structures are a bit more interesting to lay out
122 static void lay_out_struct(struct symbol *sym, struct struct_union_info *info)
124 unsigned long bit_size, align_bit_mask;
125 unsigned long alignment;
126 int base_size;
128 bit_size = info->bit_size;
129 base_size = sym->bit_size;
132 * If the member is unsized, either it's a flexible array or
133 * it's invalid and a warning has already been issued.
135 if (base_size < 0) {
136 if (!is_array_type(sym))
137 return;
138 base_size = 0;
139 info->flex_array = sym;
142 alignment = info->packed ? 1 : sym->ctype.alignment;
143 align_bit_mask = bytes_to_bits(alignment) - 1;
146 * Bitfields have some very special rules..
148 if (is_bitfield_type (sym)) {
149 unsigned long bit_offset = bit_size & align_bit_mask;
150 int room = bitfield_base_size(sym) - bit_offset;
151 // Zero-width fields just fill up the unit.
152 int width = base_size ? : (bit_offset ? room : 0);
154 if (width > room && !info->packed) {
155 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
156 bit_offset = 0;
158 sym->offset = bits_to_bytes(bit_size - bit_offset);
159 sym->bit_offset = bit_offset;
160 sym->ctype.base_type->bit_offset = bit_offset;
161 info->bit_size = bit_size + width;
162 // warning (sym->pos, "bitfield: offset=%d:%d size=:%d", sym->offset, sym->bit_offset, width);
164 if (info->packed && sym->type == SYM_NODE)
165 sym->packed = 1;
166 return;
170 * Otherwise, just align it right and add it up..
172 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
173 sym->offset = bits_to_bytes(bit_size);
175 info->bit_size = bit_size + base_size;
176 // warning (sym->pos, "regular: offset=%d", sym->offset);
180 // propagate properties of anonymous structs or unions into their members.
182 // :note: GCC seems to only propagate the qualifiers.
183 // :note: clang doesn't propagate anything at all.
184 static void examine_anonymous_member(struct symbol *sym)
186 unsigned long mod = sym->ctype.modifiers & MOD_QUALIFIER;
187 struct symbol *sub;
189 if (sym->type == SYM_NODE)
190 sym = sym->ctype.base_type;
191 if (sym->type != SYM_STRUCT && sym->type != SYM_UNION)
192 return;
194 FOR_EACH_PTR(sym->symbol_list, sub) {
195 assert(sub->type == SYM_NODE);
196 sub->ctype.modifiers |= mod;
198 // if nested, propagate all the way down
199 if (!sub->ident)
200 examine_anonymous_member(sub);
201 } END_FOR_EACH_PTR(sub);
204 static struct symbol * examine_struct_union_type(struct symbol *sym, int advance)
206 struct struct_union_info info = {
207 .packed = sym->packed,
208 .max_align = 1,
209 .bit_size = 0,
210 .align_size = 1
212 unsigned long bit_size, bit_align;
213 void (*fn)(struct symbol *, struct struct_union_info *);
214 struct symbol *member;
216 fn = advance ? lay_out_struct : lay_out_union;
217 FOR_EACH_PTR(sym->symbol_list, member) {
218 if (member->ctype.base_type == &autotype_ctype) {
219 sparse_error(member->pos, "member '%s' has __auto_type", show_ident(member->ident));
220 member->ctype.base_type = &incomplete_ctype;
222 if (info.flex_array)
223 sparse_error(info.flex_array->pos, "flexible array member '%s' is not last", show_ident(info.flex_array->ident));
224 examine_symbol_type(member);
225 if (!member->ident)
226 examine_anonymous_member(member);
228 if (member->ctype.alignment > info.max_align && !sym->packed) {
229 // Unnamed bitfields do not affect alignment.
230 if (member->ident || !is_bitfield_type(member))
231 info.max_align = member->ctype.alignment;
234 if (has_flexible_array(member))
235 info.has_flex_array = 1;
236 if (has_flexible_array(member) && Wflexible_array_nested)
237 warning(member->pos, "nested flexible array");
238 fn(member, &info);
239 } END_FOR_EACH_PTR(member);
241 if (!sym->ctype.alignment)
242 sym->ctype.alignment = info.max_align;
243 bit_size = info.bit_size;
244 if (info.align_size) {
245 bit_align = bytes_to_bits(sym->ctype.alignment)-1;
246 bit_size = (bit_size + bit_align) & ~bit_align;
248 if (info.flex_array) {
249 info.has_flex_array = 1;
251 if (info.has_flex_array && (!is_union_type(sym) || Wflexible_array_union))
252 sym->has_flex_array = 1;
253 sym->bit_size = bit_size;
254 return sym;
257 static struct symbol *examine_base_type(struct symbol *sym)
259 struct symbol *base_type;
261 if (sym->ctype.base_type == &autotype_ctype) {
262 struct symbol *type = evaluate_expression(sym->initializer);
263 if (!type)
264 type = &bad_ctype;
265 if (is_bitfield_type(type)) {
266 warning(sym->pos, "__auto_type on bitfield");
267 if (type->type == SYM_NODE)
268 type = type->ctype.base_type;
269 type = type->ctype.base_type;
271 sym->ctype.base_type = type;
274 /* Check the base type */
275 base_type = examine_symbol_type(sym->ctype.base_type);
276 if (!base_type || base_type->type == SYM_PTR)
277 return base_type;
278 combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
279 sym->ctype.modifiers |= base_type->ctype.modifiers & MOD_PTRINHERIT;
280 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
281 (struct ptr_list **)&sym->ctype.contexts);
282 if (base_type->type == SYM_NODE) {
283 base_type = base_type->ctype.base_type;
284 sym->ctype.base_type = base_type;
285 sym->rank = base_type->rank;
287 return base_type;
290 static struct symbol * examine_array_type(struct symbol *sym)
292 struct symbol *base_type = examine_base_type(sym);
293 unsigned long bit_size = -1, alignment;
294 struct expression *array_size = sym->array_size;
296 if (!base_type)
297 return sym;
299 if (array_size) {
300 bit_size = array_element_offset(base_type->bit_size,
301 get_expression_value_silent(array_size));
302 if (array_size->type != EXPR_VALUE) {
303 if (Wvla)
304 warning(array_size->pos, "Variable length array is used.");
305 bit_size = -1;
308 if (has_flexible_array(base_type) && Wflexible_array_array)
309 warning(sym->pos, "array of flexible structures");
310 alignment = base_type->ctype.alignment;
311 if (!sym->ctype.alignment)
312 sym->ctype.alignment = alignment;
313 sym->bit_size = bit_size;
314 return sym;
317 static struct symbol *examine_bitfield_type(struct symbol *sym)
319 struct symbol *base_type = examine_base_type(sym);
320 unsigned long alignment, modifiers;
322 if (!base_type)
323 return sym;
324 if (sym->bit_size > base_type->bit_size) {
325 sparse_error(sym->pos, "bitfield '%s' is wider (%d) than its type (%s)",
326 show_ident(sym->ident), sym->bit_size, show_typename(base_type));
327 sym->bit_size = -1;
330 alignment = base_type->ctype.alignment;
331 if (!sym->ctype.alignment)
332 sym->ctype.alignment = alignment;
333 modifiers = base_type->ctype.modifiers;
335 /* use -funsigned-bitfields to determine the sign if not explicit */
336 if (!(modifiers & MOD_EXPLICITLY_SIGNED) && funsigned_bitfields)
337 modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
338 sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
339 return sym;
343 * "typeof" will have to merge the types together
345 void merge_type(struct symbol *sym, struct symbol *base_type)
347 combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
348 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
349 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
350 (struct ptr_list **)&sym->ctype.contexts);
351 sym->ctype.base_type = base_type->ctype.base_type;
352 if (sym->ctype.base_type->type == SYM_NODE)
353 merge_type(sym, sym->ctype.base_type);
356 static bool is_wstring_expr(struct expression *expr)
358 while (expr) {
359 switch (expr->type) {
360 case EXPR_STRING:
361 return 1;
362 case EXPR_INITIALIZER:
363 if (expression_list_size(expr->expr_list) != 1)
364 return 0;
365 expr = first_expression(expr->expr_list);
366 break;
367 case EXPR_PREOP:
368 if (expr->op == '(') {
369 expr = expr->unop;
370 break;
372 default:
373 return 0;
376 return 0;
379 static int count_array_initializer(struct symbol *t, struct expression *expr)
381 int nr = 0;
382 int is_char = 0;
385 * Arrays of character types are special; they can be initialized by
386 * string literal _or_ by string literal in braces. The latter means
387 * that with T x[] = {<string literal>} number of elements in x depends
388 * on T - if it's a character type, we get the length of string literal
389 * (including NUL), otherwise we have one element here.
391 if (t->ctype.base_type == &int_type && t->rank == -2)
392 is_char = 1;
393 else if (t == wchar_ctype && is_wstring_expr(expr))
394 is_char = 1;
396 switch (expr->type) {
397 case EXPR_INITIALIZER: {
398 struct expression *entry;
399 int count = 0;
400 int str_len = 0;
401 FOR_EACH_PTR(expr->expr_list, entry) {
402 count++;
403 switch (entry->type) {
404 case EXPR_INDEX:
405 if (entry->idx_to >= nr)
406 nr = entry->idx_to+1;
407 break;
408 case EXPR_PREOP: {
409 struct expression *e = entry;
410 if (is_char) {
411 while (e && e->type == EXPR_PREOP && e->op == '(')
412 e = e->unop;
413 if (e && e->type == EXPR_STRING) {
414 entry = e;
415 case EXPR_STRING:
416 if (is_char)
417 str_len = entry->string->length;
423 default:
424 nr++;
426 } END_FOR_EACH_PTR(entry);
427 if (count == 1 && str_len)
428 nr = str_len;
429 break;
431 case EXPR_PREOP:
432 if (is_char) {
433 struct expression *e = expr;
434 while (e && e->type == EXPR_PREOP && e->op == '(')
435 e = e->unop;
436 if (e && e->type == EXPR_STRING) {
437 expr = e;
438 case EXPR_STRING:
439 if (is_char)
440 nr = expr->string->length;
443 break;
444 default:
445 break;
447 return nr;
450 static struct expression *get_symbol_initializer(struct symbol *sym)
452 do {
453 if (sym->initializer)
454 return sym->initializer;
455 } while ((sym = sym->same_symbol) != NULL);
456 return NULL;
459 static unsigned int implicit_array_size(struct symbol *node, unsigned int count)
461 struct symbol *arr_ori = node->ctype.base_type;
462 struct symbol *arr_new = alloc_symbol(node->pos, SYM_ARRAY);
463 struct symbol *elem_type = arr_ori->ctype.base_type;
464 struct expression *size = alloc_const_expression(node->pos, count);
465 unsigned int bit_size = array_element_offset(elem_type->bit_size, count);
467 *arr_new = *arr_ori;
468 arr_new->bit_size = bit_size;
469 arr_new->array_size = size;
470 node->array_size = size;
471 node->ctype.base_type = arr_new;
473 return bit_size;
476 static struct symbol * examine_node_type(struct symbol *sym)
478 struct symbol *base_type = examine_base_type(sym);
479 int bit_size;
480 unsigned long alignment;
482 /* SYM_NODE - figure out what the type of the node was.. */
483 bit_size = 0;
484 alignment = 0;
485 if (!base_type)
486 return sym;
488 bit_size = base_type->bit_size;
489 alignment = base_type->ctype.alignment;
491 /* Pick up signedness information into the node */
492 sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
494 if (!sym->ctype.alignment)
495 sym->ctype.alignment = alignment;
497 /* Unsized array? The size might come from the initializer.. */
498 if (bit_size < 0 && base_type->type == SYM_ARRAY) {
499 struct expression *initializer = get_symbol_initializer(sym);
500 if (initializer) {
501 struct symbol *node_type = base_type->ctype.base_type;
502 int count = count_array_initializer(node_type, initializer);
504 if (node_type && node_type->bit_size >= 0)
505 bit_size = implicit_array_size(sym, count);
509 sym->bit_size = bit_size;
510 sym->rank = base_type->rank;
511 return sym;
514 static struct symbol *examine_enum_type(struct symbol *sym)
516 struct symbol *base_type = examine_base_type(sym);
518 sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
519 sym->bit_size = bits_in_enum;
520 if (base_type->bit_size > sym->bit_size)
521 sym->bit_size = base_type->bit_size;
522 sym->ctype.alignment = enum_alignment;
523 if (base_type->ctype.alignment > sym->ctype.alignment)
524 sym->ctype.alignment = base_type->ctype.alignment;
525 return sym;
528 static struct symbol *examine_pointer_type(struct symbol *sym)
531 * Since pointers to incomplete types can be used,
532 * for example in a struct-declaration-list,
533 * the base type must *not* be examined here.
534 * It thus means that it needs to be done later,
535 * when the base type of the pointer is looked at.
537 if (!sym->bit_size)
538 sym->bit_size = bits_in_pointer;
539 if (!sym->ctype.alignment)
540 sym->ctype.alignment = pointer_alignment;
541 return sym;
544 static struct symbol *examine_typeof(struct symbol *sym)
546 struct symbol *base = evaluate_expression(sym->initializer);
547 unsigned long mod = 0;
549 if (!base)
550 base = &bad_ctype;
551 if (base->type == SYM_NODE) {
552 mod |= base->ctype.modifiers & MOD_TYPEOF;
553 base = base->ctype.base_type;
555 if (base->type == SYM_BITFIELD)
556 warning(base->pos, "typeof applied to bitfield type");
557 sym->type = SYM_NODE;
558 sym->ctype.modifiers = mod;
559 sym->ctype.base_type = base;
560 return examine_node_type(sym);
564 * Fill in type size and alignment information for
565 * regular SYM_TYPE things.
567 struct symbol *examine_symbol_type(struct symbol * sym)
569 if (!sym)
570 return sym;
572 /* Already done? */
573 if (sym->examined)
574 return sym;
575 sym->examined = 1;
577 switch (sym->type) {
578 case SYM_FN:
579 case SYM_NODE:
580 return examine_node_type(sym);
581 case SYM_ARRAY:
582 return examine_array_type(sym);
583 case SYM_STRUCT:
584 return examine_struct_union_type(sym, 1);
585 case SYM_UNION:
586 return examine_struct_union_type(sym, 0);
587 case SYM_PTR:
588 return examine_pointer_type(sym);
589 case SYM_ENUM:
590 return examine_enum_type(sym);
591 case SYM_BITFIELD:
592 return examine_bitfield_type(sym);
593 case SYM_BASETYPE:
594 /* Size and alignment had better already be set up */
595 return sym;
596 case SYM_TYPEOF:
597 return examine_typeof(sym);
598 case SYM_PREPROCESSOR:
599 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
600 return NULL;
601 case SYM_UNINITIALIZED:
602 sparse_error(sym->pos, "ctype on uninitialized symbol '%s'", show_typename(sym));
603 return NULL;
604 case SYM_RESTRICT:
605 examine_base_type(sym);
606 return sym;
607 case SYM_FOULED:
608 examine_base_type(sym);
609 return sym;
610 default:
611 sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
612 break;
614 return sym;
617 const char* get_type_name(enum type type)
619 const char *type_lookup[] = {
620 [SYM_UNINITIALIZED] = "uninitialized",
621 [SYM_PREPROCESSOR] = "preprocessor",
622 [SYM_BASETYPE] = "basetype",
623 [SYM_NODE] = "node",
624 [SYM_PTR] = "pointer",
625 [SYM_FN] = "function",
626 [SYM_ARRAY] = "array",
627 [SYM_STRUCT] = "struct",
628 [SYM_UNION] = "union",
629 [SYM_ENUM] = "enum",
630 [SYM_TYPEOF] = "typeof",
631 [SYM_BITFIELD] = "bitfield",
632 [SYM_LABEL] = "label",
633 [SYM_RESTRICT] = "restrict",
634 [SYM_FOULED] = "fouled",
635 [SYM_KEYWORD] = "keyword",
636 [SYM_BAD] = "bad"};
638 if (type <= SYM_BAD)
639 return type_lookup[type];
640 else
641 return NULL;
644 struct symbol *examine_pointer_target(struct symbol *sym)
646 return examine_base_type(sym);
649 static struct symbol_list *restr, *fouled;
651 void create_fouled(struct symbol *type)
653 if (type->bit_size < bits_in_int) {
654 struct symbol *new = alloc_symbol(type->pos, type->type);
655 *new = *type;
656 new->bit_size = bits_in_int;
657 new->rank = 0;
658 new->type = SYM_FOULED;
659 new->ctype.base_type = type;
660 add_symbol(&restr, type);
661 add_symbol(&fouled, new);
665 struct symbol *befoul(struct symbol *type)
667 struct symbol *t1, *t2;
668 while (type->type == SYM_NODE)
669 type = type->ctype.base_type;
670 PREPARE_PTR_LIST(restr, t1);
671 PREPARE_PTR_LIST(fouled, t2);
672 for (;;) {
673 if (t1 == type)
674 return t2;
675 if (!t1)
676 break;
677 NEXT_PTR_LIST(t1);
678 NEXT_PTR_LIST(t2);
680 FINISH_PTR_LIST(t2);
681 FINISH_PTR_LIST(t1);
682 return NULL;
685 static void inherit_declaration(struct symbol *sym, struct symbol *prev)
687 unsigned long mods = prev->ctype.modifiers;
689 // inherit function attributes
690 sym->ctype.modifiers |= mods & MOD_FUN_ATTR;
693 void check_declaration(struct symbol *sym)
695 int warned = 0;
696 struct symbol *next = sym;
698 while ((next = next->next_id) != NULL) {
699 if (next->namespace != sym->namespace)
700 continue;
701 if (sym->scope == next->scope) {
702 sym->same_symbol = next;
703 inherit_declaration(sym, next);
704 return;
706 /* Extern in block level matches a TOPLEVEL non-static symbol */
707 if (sym->ctype.modifiers & MOD_EXTERN) {
708 if ((next->ctype.modifiers & (MOD_TOPLEVEL|MOD_STATIC)) == MOD_TOPLEVEL) {
709 sym->same_symbol = next;
710 return;
714 if (!Wshadow || warned)
715 continue;
716 if (get_sym_type(next) == SYM_FN)
717 continue;
718 warned = 1;
719 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
720 info(next->pos, "originally declared here");
724 static void inherit_static(struct symbol *sym)
726 struct symbol *prev;
728 // only 'plain' symbols are concerned
729 if (sym->ctype.modifiers & (MOD_STATIC|MOD_EXTERN))
730 return;
732 for (prev = sym->next_id; prev; prev = prev->next_id) {
733 if (prev->namespace != NS_SYMBOL)
734 continue;
735 if (prev->scope != file_scope)
736 continue;
738 sym->ctype.modifiers |= prev->ctype.modifiers & MOD_STATIC;
740 // previous declarations are already converted
741 return;
745 void bind_symbol_with_scope(struct symbol *sym, struct ident *ident, enum namespace ns, struct scope *scope)
747 if (sym->bound) {
748 sparse_error(sym->pos, "internal error: symbol type already bound");
749 return;
751 if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
752 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
753 return;
755 sym->namespace = ns;
756 sym->next_id = ident->symbols;
757 ident->symbols = sym;
758 if (sym->ident && sym->ident != ident)
759 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
760 sym->ident = ident;
761 sym->bound = 1;
763 if (ns == NS_SYMBOL && toplevel(scope)) {
764 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
766 inherit_static(sym);
768 scope = global_scope;
769 if (sym->ctype.modifiers & MOD_STATIC ||
770 is_extern_inline(sym)) {
771 scope = file_scope;
772 mod = MOD_TOPLEVEL;
774 sym->ctype.modifiers |= mod;
776 bind_scope(sym, scope);
779 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
781 struct scope *scope = block_scope;;
783 if (ns == NS_MACRO)
784 scope = file_scope;
785 if (ns == NS_LABEL)
786 scope = function_scope;
787 bind_symbol_with_scope(sym, ident, ns, scope);
790 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
792 struct ident *ident = built_in_ident(name);
793 struct symbol *sym = lookup_symbol(ident, namespace);
795 if (sym && sym->type != type)
796 die("symbol %s created with different types: %d old %d", name,
797 type, sym->type);
799 if (!sym) {
800 struct token *token = built_in_token(stream, ident);
802 sym = alloc_symbol(token->pos, type);
803 bind_symbol(sym, token->ident, namespace);
805 return sym;
810 * Abstract types
812 struct symbol int_type,
813 fp_type;
816 * C types (i.e. actual instances that the abstract types
817 * can map onto)
819 struct symbol bool_ctype, void_ctype, type_ctype,
820 char_ctype, schar_ctype, uchar_ctype,
821 short_ctype, sshort_ctype, ushort_ctype,
822 int_ctype, sint_ctype, uint_ctype,
823 long_ctype, slong_ctype, ulong_ctype,
824 llong_ctype, sllong_ctype, ullong_ctype,
825 int128_ctype, sint128_ctype, uint128_ctype,
826 float_ctype, double_ctype, ldouble_ctype,
827 string_ctype, ptr_ctype, lazy_ptr_ctype,
828 incomplete_ctype, label_ctype, bad_ctype,
829 null_ctype;
830 struct symbol autotype_ctype;
831 struct symbol schar_ptr_ctype, short_ptr_ctype;
832 struct symbol int_ptr_ctype, uint_ptr_ctype;
833 struct symbol long_ptr_ctype, ulong_ptr_ctype;
834 struct symbol llong_ptr_ctype, ullong_ptr_ctype;
835 struct symbol size_t_ptr_ctype, intmax_ptr_ctype, ptrdiff_ptr_ctype;
836 struct symbol float32_ctype, float32x_ctype;
837 struct symbol float64_ctype, float64x_ctype;
838 struct symbol float128_ctype;
839 struct symbol const_void_ctype, const_char_ctype;
840 struct symbol const_ptr_ctype, const_string_ctype;
841 struct symbol const_wchar_ctype, const_wstring_ctype;
842 struct symbol volatile_void_ctype, volatile_ptr_ctype;
843 struct symbol volatile_bool_ctype, volatile_bool_ptr_ctype;
845 struct symbol zero_int;
847 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
848 #define __IDENT(n,str,res) \
849 struct ident n = __INIT_IDENT(str,res)
851 #include "ident-list.h"
853 void init_symbols(void)
855 int stream = init_stream(NULL, "builtin", -1, includepath);
857 #define __IDENT(n,str,res) \
858 hash_ident(&n)
859 #include "ident-list.h"
861 init_parser(stream);
864 // For fix-sized types
865 static int bits_in_type32 = 32;
866 static int bits_in_type64 = 64;
867 static int bits_in_type128 = 128;
869 #define T_BASETYPE SYM_BASETYPE, 0, 0, NULL, NULL, NULL
870 #define T_INT(R, S, M) SYM_BASETYPE, M, R, &bits_in_##S, &max_int_alignment, &int_type
871 #define T__INT(R, S) T_INT(R, S, MOD_SIGNED)
872 #define T_SINT(R, S) T_INT(R, S, MOD_ESIGNED)
873 #define T_UINT(R,S) T_INT(R, S, MOD_UNSIGNED)
874 #define T_FLOAT_(R,S,A) SYM_BASETYPE, 0, R, &bits_in_##S, A, &fp_type
875 #define T_FLOAT(R, S) T_FLOAT_(R, S, &max_fp_alignment)
876 #define T_PTR(B) SYM_PTR, 0, 0, &bits_in_pointer, &pointer_alignment, B
877 #define T_NODE(M,B,S,A) SYM_NODE, M, 0, S, A, B
878 #define T_CONST(B,S,A) T_NODE(MOD_CONST, B, S, A)
880 static const struct ctype_declare {
881 struct symbol *ptr;
882 enum type type;
883 unsigned long modifiers;
884 int rank;
885 int *bit_size;
886 int *maxalign;
887 struct symbol *base_type;
888 } ctype_declaration[] = {
889 { &bool_ctype, T_INT(-3, bool, MOD_UNSIGNED) },
890 { &void_ctype, T_BASETYPE },
891 { &type_ctype, T_BASETYPE },
892 { &incomplete_ctype, T_BASETYPE },
893 { &autotype_ctype, T_BASETYPE },
894 { &bad_ctype, T_BASETYPE },
896 { &char_ctype, T__INT(-2, char) },
897 { &schar_ctype, T_SINT(-2, char) },
898 { &uchar_ctype, T_UINT(-2, char) },
899 { &short_ctype, T__INT(-1, short) },
900 { &sshort_ctype, T_SINT(-1, short) },
901 { &ushort_ctype, T_UINT(-1, short) },
902 { &int_ctype, T__INT( 0, int) },
903 { &sint_ctype, T_SINT( 0, int) },
904 { &uint_ctype, T_UINT( 0, int) },
905 { &long_ctype, T__INT( 1, long) },
906 { &slong_ctype, T_SINT( 1, long) },
907 { &ulong_ctype, T_UINT( 1, long) },
908 { &llong_ctype, T__INT( 2, longlong) },
909 { &sllong_ctype, T_SINT( 2, longlong) },
910 { &ullong_ctype, T_UINT( 2, longlong) },
911 { &int128_ctype, T__INT( 3, type128) },
912 { &sint128_ctype, T_SINT( 3, type128) },
913 { &uint128_ctype, T_UINT( 3, type128) },
915 { &float_ctype, T_FLOAT(-1, float) },
916 { &double_ctype, T_FLOAT( 0, double) },
917 { &ldouble_ctype, T_FLOAT( 1, longdouble) },
919 { &float32_ctype, T_FLOAT(-1, type32) },
920 { &float32x_ctype, T_FLOAT(-1, double) },
921 { &float64_ctype, T_FLOAT( 0, type64) },
922 { &float64x_ctype, T_FLOAT( 1, longdouble) },
923 { &float128_ctype, T_FLOAT_(2, type128, &max_alignment) },
925 { &string_ctype, T_PTR(&char_ctype) },
926 { &ptr_ctype, T_PTR(&void_ctype) },
927 { &null_ctype, T_PTR(&void_ctype) },
928 { &label_ctype, T_PTR(&void_ctype) },
929 { &lazy_ptr_ctype, T_PTR(&void_ctype) },
930 { &schar_ptr_ctype, T_PTR(&schar_ctype) },
931 { &short_ptr_ctype, T_PTR(&short_ctype) },
932 { &int_ptr_ctype, T_PTR(&int_ctype) },
933 { &uint_ptr_ctype, T_PTR(&uint_ctype) },
934 { &long_ptr_ctype, T_PTR(&long_ctype) },
935 { &ulong_ptr_ctype, T_PTR(&ulong_ctype) },
936 { &llong_ptr_ctype, T_PTR(&llong_ctype) },
937 { &ullong_ptr_ctype, T_PTR(&ullong_ctype) },
938 { &size_t_ptr_ctype, T_PTR(&void_ctype) }, // will be adjusted
939 { &intmax_ptr_ctype, T_PTR(&void_ctype) }, // will be adjusted
940 { &ptrdiff_ptr_ctype, T_PTR(&void_ctype) }, // will be adjusted
941 { &const_ptr_ctype, T_PTR(&const_void_ctype) },
942 { &const_string_ctype, T_PTR(&const_char_ctype) },
943 { &const_wstring_ctype,T_PTR(&const_wchar_ctype) },
945 { &const_void_ctype, T_CONST(&void_ctype, NULL, NULL) },
946 { &const_char_ctype, T_CONST(&char_ctype, &bits_in_char, &max_int_alignment)},
947 { &const_wchar_ctype, T_CONST(&int_ctype, NULL, NULL) },
948 { &volatile_void_ctype,T_NODE(MOD_VOLATILE, &void_ctype, NULL, NULL) },
949 { &volatile_ptr_ctype, T_PTR(&volatile_void_ctype) },
950 { &volatile_bool_ctype,T_NODE(MOD_VOLATILE, &bool_ctype, NULL, NULL) },
951 { &volatile_bool_ptr_ctype, T_PTR(&volatile_bool_ctype) },
952 { NULL, }
955 void init_ctype(void)
957 const struct ctype_declare *ctype;
959 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
960 struct symbol *sym = ctype->ptr;
961 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
962 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
963 unsigned long alignment = bits_to_bytes(bit_size);
965 if (alignment > maxalign)
966 alignment = maxalign;
967 sym->type = ctype->type;
968 sym->rank = ctype->rank;
969 sym->bit_size = bit_size;
970 sym->ctype.alignment = alignment;
971 sym->ctype.base_type = ctype->base_type;
972 sym->ctype.modifiers = ctype->modifiers;
974 if (sym->type == SYM_NODE) {
975 struct symbol *base = sym->ctype.base_type;
976 sym->rank = base->rank;
977 if (!ctype->bit_size)
978 sym->bit_size = base->bit_size;
979 if (!ctype->maxalign)
980 sym->ctype.alignment = base->ctype.alignment;
984 // and now some adjustments
985 if (funsigned_char) {
986 char_ctype.ctype.modifiers |= MOD_UNSIGNED;
987 char_ctype.ctype.modifiers &= ~MOD_SIGNED;
990 if (!ptrdiff_ctype)
991 ptrdiff_ctype = ssize_t_ctype;
992 if (!intptr_ctype)
993 intptr_ctype = ssize_t_ctype;
994 if (!uintptr_ctype)
995 uintptr_ctype = size_t_ctype;
997 size_t_ptr_ctype.ctype.base_type = size_t_ctype;
998 intmax_ptr_ctype.ctype.base_type = intmax_ctype;
999 ptrdiff_ptr_ctype.ctype.base_type = ptrdiff_ctype;
1001 const_wchar_ctype.ctype.base_type = wchar_ctype;
1002 const_wchar_ctype.rank = wchar_ctype->rank;
1003 const_wchar_ctype.ctype.alignment = wchar_ctype->ctype.alignment;
1004 const_wchar_ctype.bit_size = wchar_ctype->bit_size;