db: use the load_strings_from_file() helper function
[smatch.git] / symbol.c
blob11a0994b9cd576c33dfd60621fa1d00aae9ecdec
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;
90 char has_flex_array;
91 bool packed;
92 struct symbol *flex_array;
96 * Unions are fairly easy to lay out ;)
98 static void lay_out_union(struct symbol *sym, struct struct_union_info *info)
100 if (sym->bit_size < 0 && is_array_type(sym))
101 sparse_error(sym->pos, "flexible array member '%s' in a union", show_ident(sym->ident));
103 if (sym->bit_size > info->bit_size)
104 info->bit_size = sym->bit_size;
106 sym->offset = 0;
109 static int bitfield_base_size(struct symbol *sym)
111 if (sym->type == SYM_NODE)
112 sym = sym->ctype.base_type;
113 if (sym->type == SYM_BITFIELD)
114 sym = sym->ctype.base_type;
115 return sym->bit_size;
119 * Structures are a bit more interesting to lay out
121 static void lay_out_struct(struct symbol *sym, struct struct_union_info *info)
123 unsigned long bit_size, align_bit_mask;
124 unsigned long alignment;
125 int base_size;
127 bit_size = info->bit_size;
128 base_size = sym->bit_size;
131 * If the member is unsized, either it's a flexible array or
132 * it's invalid and a warning has already been issued.
134 if (base_size < 0) {
135 if (!is_array_type(sym))
136 return;
137 base_size = 0;
138 info->flex_array = sym;
141 alignment = info->packed ? 1 : sym->ctype.alignment;
142 align_bit_mask = bytes_to_bits(alignment) - 1;
145 * Bitfields have some very special rules..
147 if (is_bitfield_type (sym)) {
148 unsigned long bit_offset = bit_size & align_bit_mask;
149 int room = bitfield_base_size(sym) - bit_offset;
150 // Zero-width fields just fill up the unit.
151 int width = base_size ? : (bit_offset ? room : 0);
153 if (width > room && !info->packed) {
154 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
155 bit_offset = 0;
157 sym->offset = bits_to_bytes(bit_size - bit_offset);
158 sym->bit_offset = bit_offset;
159 sym->ctype.base_type->bit_offset = bit_offset;
160 info->bit_size = bit_size + width;
161 // warning (sym->pos, "bitfield: offset=%d:%d size=:%d", sym->offset, sym->bit_offset, width);
163 if (info->packed && sym->type == SYM_NODE)
164 sym->packed = 1;
165 return;
169 * Otherwise, just align it right and add it up..
171 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
172 sym->offset = bits_to_bytes(bit_size);
174 info->bit_size = bit_size + base_size;
175 // warning (sym->pos, "regular: offset=%d", sym->offset);
178 static struct symbol * examine_struct_union_type(struct symbol *sym, int advance)
180 struct struct_union_info info = {
181 .packed = sym->packed,
182 .max_align = 1,
183 .bit_size = 0,
184 .align_size = 1
186 unsigned long bit_size, bit_align;
187 void (*fn)(struct symbol *, struct struct_union_info *);
188 struct symbol *member;
190 fn = advance ? lay_out_struct : lay_out_union;
191 FOR_EACH_PTR(sym->symbol_list, member) {
192 if (member->ctype.base_type == &autotype_ctype) {
193 sparse_error(member->pos, "member '%s' has __auto_type", show_ident(member->ident));
194 member->ctype.base_type = &incomplete_ctype;
196 if (info.flex_array)
197 sparse_error(info.flex_array->pos, "flexible array member '%s' is not last", show_ident(info.flex_array->ident));
198 examine_symbol_type(member);
200 if (member->ctype.alignment > info.max_align && !sym->packed) {
201 // Unnamed bitfields do not affect alignment.
202 if (member->ident || !is_bitfield_type(member))
203 info.max_align = member->ctype.alignment;
206 if (has_flexible_array(member))
207 info.has_flex_array = 1;
208 if (has_flexible_array(member) && Wflexible_array_nested)
209 warning(member->pos, "nested flexible array");
210 fn(member, &info);
211 } END_FOR_EACH_PTR(member);
213 if (!sym->ctype.alignment)
214 sym->ctype.alignment = info.max_align;
215 bit_size = info.bit_size;
216 if (info.align_size) {
217 bit_align = bytes_to_bits(sym->ctype.alignment)-1;
218 bit_size = (bit_size + bit_align) & ~bit_align;
220 if (info.flex_array) {
221 info.has_flex_array = 1;
223 if (info.has_flex_array && (!is_union_type(sym) || Wflexible_array_union))
224 sym->has_flex_array = 1;
225 sym->bit_size = bit_size;
226 return sym;
229 static struct symbol *examine_base_type(struct symbol *sym)
231 struct symbol *base_type;
233 if (sym->ctype.base_type == &autotype_ctype) {
234 struct symbol *type = evaluate_expression(sym->initializer);
235 if (!type)
236 type = &bad_ctype;
237 if (is_bitfield_type(type)) {
238 warning(sym->pos, "__auto_type on bitfield");
239 if (type->type == SYM_NODE)
240 type = type->ctype.base_type;
241 type = type->ctype.base_type;
243 sym->ctype.base_type = type;
246 /* Check the base type */
247 base_type = examine_symbol_type(sym->ctype.base_type);
248 if (!base_type || base_type->type == SYM_PTR)
249 return base_type;
250 combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
251 sym->ctype.modifiers |= base_type->ctype.modifiers & MOD_PTRINHERIT;
252 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
253 (struct ptr_list **)&sym->ctype.contexts);
254 if (base_type->type == SYM_NODE) {
255 base_type = base_type->ctype.base_type;
256 sym->ctype.base_type = base_type;
257 sym->rank = base_type->rank;
259 return base_type;
262 static struct symbol * examine_array_type(struct symbol *sym)
264 struct symbol *base_type = examine_base_type(sym);
265 unsigned long bit_size = -1, alignment;
266 struct expression *array_size = sym->array_size;
268 if (!base_type)
269 return sym;
271 if (array_size) {
272 bit_size = array_element_offset(base_type->bit_size,
273 get_expression_value_silent(array_size));
274 if (array_size->type != EXPR_VALUE) {
275 if (Wvla)
276 warning(array_size->pos, "Variable length array is used.");
277 bit_size = -1;
280 if (has_flexible_array(base_type) && Wflexible_array_array)
281 warning(sym->pos, "array of flexible structures");
282 alignment = base_type->ctype.alignment;
283 if (!sym->ctype.alignment)
284 sym->ctype.alignment = alignment;
285 sym->bit_size = bit_size;
286 return sym;
289 static struct symbol *examine_bitfield_type(struct symbol *sym)
291 struct symbol *base_type = examine_base_type(sym);
292 unsigned long alignment, modifiers;
294 if (!base_type)
295 return sym;
296 if (sym->bit_size > base_type->bit_size) {
297 sparse_error(sym->pos, "bitfield '%s' is wider (%d) than its type (%s)",
298 show_ident(sym->ident), sym->bit_size, show_typename(base_type));
299 sym->bit_size = -1;
302 alignment = base_type->ctype.alignment;
303 if (!sym->ctype.alignment)
304 sym->ctype.alignment = alignment;
305 modifiers = base_type->ctype.modifiers;
307 /* use -funsigned-bitfields to determine the sign if not explicit */
308 if (!(modifiers & MOD_EXPLICITLY_SIGNED) && funsigned_bitfields)
309 modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
310 sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
311 return sym;
315 * "typeof" will have to merge the types together
317 void merge_type(struct symbol *sym, struct symbol *base_type)
319 combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
320 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
321 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
322 (struct ptr_list **)&sym->ctype.contexts);
323 sym->ctype.base_type = base_type->ctype.base_type;
324 if (sym->ctype.base_type->type == SYM_NODE)
325 merge_type(sym, sym->ctype.base_type);
328 static bool is_wstring_expr(struct expression *expr)
330 while (expr) {
331 switch (expr->type) {
332 case EXPR_STRING:
333 return 1;
334 case EXPR_INITIALIZER:
335 if (expression_list_size(expr->expr_list) != 1)
336 return 0;
337 expr = first_expression(expr->expr_list);
338 break;
339 case EXPR_PREOP:
340 if (expr->op == '(') {
341 expr = expr->unop;
342 break;
344 default:
345 return 0;
348 return 0;
351 static int count_array_initializer(struct symbol *t, struct expression *expr)
353 int nr = 0;
354 int is_char = 0;
357 * Arrays of character types are special; they can be initialized by
358 * string literal _or_ by string literal in braces. The latter means
359 * that with T x[] = {<string literal>} number of elements in x depends
360 * on T - if it's a character type, we get the length of string literal
361 * (including NUL), otherwise we have one element here.
363 if (t->ctype.base_type == &int_type && t->rank == -2)
364 is_char = 1;
365 else if (t == wchar_ctype && is_wstring_expr(expr))
366 is_char = 1;
368 switch (expr->type) {
369 case EXPR_INITIALIZER: {
370 struct expression *entry;
371 int count = 0;
372 int str_len = 0;
373 FOR_EACH_PTR(expr->expr_list, entry) {
374 count++;
375 switch (entry->type) {
376 case EXPR_INDEX:
377 if (entry->idx_to >= nr)
378 nr = entry->idx_to+1;
379 break;
380 case EXPR_PREOP: {
381 struct expression *e = entry;
382 if (is_char) {
383 while (e && e->type == EXPR_PREOP && e->op == '(')
384 e = e->unop;
385 if (e && e->type == EXPR_STRING) {
386 entry = e;
387 case EXPR_STRING:
388 if (is_char)
389 str_len = entry->string->length;
395 default:
396 nr++;
398 } END_FOR_EACH_PTR(entry);
399 if (count == 1 && str_len)
400 nr = str_len;
401 break;
403 case EXPR_PREOP:
404 if (is_char) {
405 struct expression *e = expr;
406 while (e && e->type == EXPR_PREOP && e->op == '(')
407 e = e->unop;
408 if (e && e->type == EXPR_STRING) {
409 expr = e;
410 case EXPR_STRING:
411 if (is_char)
412 nr = expr->string->length;
415 break;
416 default:
417 break;
419 return nr;
422 static struct expression *get_symbol_initializer(struct symbol *sym)
424 do {
425 if (sym->initializer)
426 return sym->initializer;
427 } while ((sym = sym->same_symbol) != NULL);
428 return NULL;
431 static unsigned int implicit_array_size(struct symbol *node, unsigned int count)
433 struct symbol *arr_ori = node->ctype.base_type;
434 struct symbol *arr_new = alloc_symbol(node->pos, SYM_ARRAY);
435 struct symbol *elem_type = arr_ori->ctype.base_type;
436 struct expression *size = alloc_const_expression(node->pos, count);
437 unsigned int bit_size = array_element_offset(elem_type->bit_size, count);
439 *arr_new = *arr_ori;
440 arr_new->bit_size = bit_size;
441 arr_new->array_size = size;
442 node->array_size = size;
443 node->ctype.base_type = arr_new;
445 return bit_size;
448 static struct symbol * examine_node_type(struct symbol *sym)
450 struct symbol *base_type = examine_base_type(sym);
451 int bit_size;
452 unsigned long alignment;
454 /* SYM_NODE - figure out what the type of the node was.. */
455 bit_size = 0;
456 alignment = 0;
457 if (!base_type)
458 return sym;
460 bit_size = base_type->bit_size;
461 alignment = base_type->ctype.alignment;
463 /* Pick up signedness information into the node */
464 sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
466 if (!sym->ctype.alignment)
467 sym->ctype.alignment = alignment;
469 /* Unsized array? The size might come from the initializer.. */
470 if (bit_size < 0 && base_type->type == SYM_ARRAY) {
471 struct expression *initializer = get_symbol_initializer(sym);
472 if (initializer) {
473 struct symbol *node_type = base_type->ctype.base_type;
474 int count = count_array_initializer(node_type, initializer);
476 if (node_type && node_type->bit_size >= 0)
477 bit_size = implicit_array_size(sym, count);
481 sym->bit_size = bit_size;
482 sym->rank = base_type->rank;
483 return sym;
486 static struct symbol *examine_enum_type(struct symbol *sym)
488 struct symbol *base_type = examine_base_type(sym);
490 sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
491 sym->bit_size = bits_in_enum;
492 if (base_type->bit_size > sym->bit_size)
493 sym->bit_size = base_type->bit_size;
494 sym->ctype.alignment = enum_alignment;
495 if (base_type->ctype.alignment > sym->ctype.alignment)
496 sym->ctype.alignment = base_type->ctype.alignment;
497 return sym;
500 static struct symbol *examine_pointer_type(struct symbol *sym)
503 * Since pointers to incomplete types can be used,
504 * for example in a struct-declaration-list,
505 * the base type must *not* be examined here.
506 * It thus means that it needs to be done later,
507 * when the base type of the pointer is looked at.
509 if (!sym->bit_size)
510 sym->bit_size = bits_in_pointer;
511 if (!sym->ctype.alignment)
512 sym->ctype.alignment = pointer_alignment;
513 return sym;
516 static struct symbol *examine_typeof(struct symbol *sym)
518 struct symbol *base = evaluate_expression(sym->initializer);
519 unsigned long mod = 0;
521 if (!base)
522 base = &bad_ctype;
523 if (base->type == SYM_NODE) {
524 mod |= base->ctype.modifiers & MOD_TYPEOF;
525 base = base->ctype.base_type;
527 if (base->type == SYM_BITFIELD)
528 warning(base->pos, "typeof applied to bitfield type");
529 sym->type = SYM_NODE;
530 sym->ctype.modifiers = mod;
531 sym->ctype.base_type = base;
532 return examine_node_type(sym);
536 * Fill in type size and alignment information for
537 * regular SYM_TYPE things.
539 struct symbol *examine_symbol_type(struct symbol * sym)
541 if (!sym)
542 return sym;
544 /* Already done? */
545 if (sym->examined)
546 return sym;
547 sym->examined = 1;
549 switch (sym->type) {
550 case SYM_FN:
551 case SYM_NODE:
552 return examine_node_type(sym);
553 case SYM_ARRAY:
554 return examine_array_type(sym);
555 case SYM_STRUCT:
556 return examine_struct_union_type(sym, 1);
557 case SYM_UNION:
558 return examine_struct_union_type(sym, 0);
559 case SYM_PTR:
560 return examine_pointer_type(sym);
561 case SYM_ENUM:
562 return examine_enum_type(sym);
563 case SYM_BITFIELD:
564 return examine_bitfield_type(sym);
565 case SYM_BASETYPE:
566 /* Size and alignment had better already be set up */
567 return sym;
568 case SYM_TYPEOF:
569 return examine_typeof(sym);
570 case SYM_PREPROCESSOR:
571 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
572 return NULL;
573 case SYM_UNINITIALIZED:
574 // sparse_error(sym->pos, "ctype on uninitialized symbol '%s'", show_typename(sym));
575 return NULL;
576 case SYM_RESTRICT:
577 examine_base_type(sym);
578 return sym;
579 case SYM_FOULED:
580 examine_base_type(sym);
581 return sym;
582 default:
583 // sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
584 break;
586 return sym;
589 const char* get_type_name(enum type type)
591 const char *type_lookup[] = {
592 [SYM_UNINITIALIZED] = "uninitialized",
593 [SYM_PREPROCESSOR] = "preprocessor",
594 [SYM_BASETYPE] = "basetype",
595 [SYM_NODE] = "node",
596 [SYM_PTR] = "pointer",
597 [SYM_FN] = "function",
598 [SYM_ARRAY] = "array",
599 [SYM_STRUCT] = "struct",
600 [SYM_UNION] = "union",
601 [SYM_ENUM] = "enum",
602 [SYM_TYPEOF] = "typeof",
603 [SYM_BITFIELD] = "bitfield",
604 [SYM_LABEL] = "label",
605 [SYM_RESTRICT] = "restrict",
606 [SYM_FOULED] = "fouled",
607 [SYM_KEYWORD] = "keyword",
608 [SYM_BAD] = "bad"};
610 if (type <= SYM_BAD)
611 return type_lookup[type];
612 else
613 return NULL;
616 struct symbol *examine_pointer_target(struct symbol *sym)
618 return examine_base_type(sym);
621 static struct symbol_list *restr, *fouled;
623 void create_fouled(struct symbol *type)
625 if (type->bit_size < bits_in_int) {
626 struct symbol *new = alloc_symbol(type->pos, type->type);
627 *new = *type;
628 new->bit_size = bits_in_int;
629 new->rank = 0;
630 new->type = SYM_FOULED;
631 new->ctype.base_type = type;
632 add_symbol(&restr, type);
633 add_symbol(&fouled, new);
637 struct symbol *befoul(struct symbol *type)
639 struct symbol *t1, *t2;
640 while (type->type == SYM_NODE)
641 type = type->ctype.base_type;
642 PREPARE_PTR_LIST(restr, t1);
643 PREPARE_PTR_LIST(fouled, t2);
644 for (;;) {
645 if (t1 == type)
646 return t2;
647 if (!t1)
648 break;
649 NEXT_PTR_LIST(t1);
650 NEXT_PTR_LIST(t2);
652 FINISH_PTR_LIST(t2);
653 FINISH_PTR_LIST(t1);
654 return NULL;
657 static void inherit_declaration(struct symbol *sym, struct symbol *prev)
659 unsigned long mods = prev->ctype.modifiers;
661 // inherit function attributes
662 sym->ctype.modifiers |= mods & MOD_FUN_ATTR;
665 void check_declaration(struct symbol *sym)
667 int warned = 0;
668 struct symbol *next = sym;
670 while ((next = next->next_id) != NULL) {
671 if (next->namespace != sym->namespace)
672 continue;
673 if (sym->scope == next->scope) {
674 sym->same_symbol = next;
675 inherit_declaration(sym, next);
676 return;
678 /* Extern in block level matches a TOPLEVEL non-static symbol */
679 if (sym->ctype.modifiers & MOD_EXTERN) {
680 if ((next->ctype.modifiers & (MOD_TOPLEVEL|MOD_STATIC)) == MOD_TOPLEVEL) {
681 sym->same_symbol = next;
682 return;
686 if (!Wshadow || warned)
687 continue;
688 if (get_sym_type(next) == SYM_FN)
689 continue;
690 warned = 1;
691 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
692 info(next->pos, "originally declared here");
696 static void inherit_static(struct symbol *sym)
698 struct symbol *prev;
700 // only 'plain' symbols are concerned
701 if (sym->ctype.modifiers & (MOD_STATIC|MOD_EXTERN))
702 return;
704 for (prev = sym->next_id; prev; prev = prev->next_id) {
705 if (prev->namespace != NS_SYMBOL)
706 continue;
707 if (prev->scope != file_scope)
708 continue;
710 sym->ctype.modifiers |= prev->ctype.modifiers & MOD_STATIC;
712 // previous declarations are already converted
713 return;
717 void bind_symbol_with_scope(struct symbol *sym, struct ident *ident, enum namespace ns, struct scope *scope)
719 if (sym->bound) {
720 sparse_error(sym->pos, "internal error: symbol type already bound");
721 return;
723 if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
724 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
725 return;
727 sym->namespace = ns;
728 sym->next_id = ident->symbols;
729 ident->symbols = sym;
730 if (sym->ident && sym->ident != ident)
731 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
732 sym->ident = ident;
733 sym->bound = 1;
735 if (ns == NS_SYMBOL && toplevel(scope)) {
736 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
738 inherit_static(sym);
740 scope = global_scope;
741 if (sym->ctype.modifiers & MOD_STATIC ||
742 is_extern_inline(sym)) {
743 scope = file_scope;
744 mod = MOD_TOPLEVEL;
746 sym->ctype.modifiers |= mod;
748 bind_scope(sym, scope);
751 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
753 struct scope *scope = block_scope;;
755 if (ns == NS_MACRO)
756 scope = file_scope;
757 if (ns == NS_LABEL)
758 scope = function_scope;
759 bind_symbol_with_scope(sym, ident, ns, scope);
762 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
764 struct ident *ident = built_in_ident(name);
765 struct symbol *sym = lookup_symbol(ident, namespace);
767 if (sym && sym->type != type)
768 die("symbol %s created with different types: %d old %d", name,
769 type, sym->type);
771 if (!sym) {
772 struct token *token = built_in_token(stream, ident);
774 sym = alloc_symbol(token->pos, type);
775 bind_symbol(sym, token->ident, namespace);
777 return sym;
782 * Abstract types
784 struct symbol int_type,
785 fp_type;
788 * C types (i.e. actual instances that the abstract types
789 * can map onto)
791 struct symbol bool_ctype, void_ctype, type_ctype,
792 char_ctype, schar_ctype, uchar_ctype,
793 short_ctype, sshort_ctype, ushort_ctype,
794 int_ctype, sint_ctype, uint_ctype,
795 long_ctype, slong_ctype, ulong_ctype,
796 llong_ctype, sllong_ctype, ullong_ctype,
797 int128_ctype, sint128_ctype, uint128_ctype,
798 float_ctype, double_ctype, ldouble_ctype,
799 string_ctype, ptr_ctype, lazy_ptr_ctype,
800 incomplete_ctype, label_ctype, bad_ctype,
801 null_ctype;
802 struct symbol autotype_ctype;
803 struct symbol schar_ptr_ctype, short_ptr_ctype;
804 struct symbol int_ptr_ctype, uint_ptr_ctype;
805 struct symbol long_ptr_ctype, ulong_ptr_ctype;
806 struct symbol llong_ptr_ctype, ullong_ptr_ctype;
807 struct symbol size_t_ptr_ctype, intmax_ptr_ctype, ptrdiff_ptr_ctype;
808 struct symbol float32_ctype, float32x_ctype;
809 struct symbol float64_ctype, float64x_ctype;
810 struct symbol float128_ctype;
811 struct symbol const_void_ctype, const_char_ctype;
812 struct symbol const_ptr_ctype, const_string_ctype;
813 struct symbol const_wchar_ctype, const_wstring_ctype;
814 struct symbol volatile_void_ctype, volatile_ptr_ctype;
815 struct symbol volatile_bool_ctype, volatile_bool_ptr_ctype;
817 struct symbol zero_int;
819 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
820 #define __IDENT(n,str,res) \
821 struct ident n = __INIT_IDENT(str,res)
823 #include "ident-list.h"
825 void init_symbols(void)
827 int stream = init_stream(NULL, "builtin", -1, includepath);
829 #define __IDENT(n,str,res) \
830 hash_ident(&n)
831 #include "ident-list.h"
833 init_parser(stream);
836 #ifdef __CHAR_UNSIGNED__
837 #define CHAR_SIGNEDNESS MOD_UNSIGNED
838 #else
839 #define CHAR_SIGNEDNESS MOD_SIGNED
840 #endif
841 // For fix-sized types
842 static int bits_in_type32 = 32;
843 static int bits_in_type64 = 64;
844 static int bits_in_type128 = 128;
846 #define T_BASETYPE SYM_BASETYPE, 0, 0, NULL, NULL, NULL
847 #define T_INT(R, S, M) SYM_BASETYPE, M, R, &bits_in_##S, &max_int_alignment, &int_type
848 #define T__INT(R, S) T_INT(R, S, MOD_SIGNED)
849 #define T_SINT(R, S) T_INT(R, S, MOD_ESIGNED)
850 #define T_UINT(R,S) T_INT(R, S, MOD_UNSIGNED)
851 #define T_FLOAT_(R,S,A) SYM_BASETYPE, 0, R, &bits_in_##S, A, &fp_type
852 #define T_FLOAT(R, S) T_FLOAT_(R, S, &max_fp_alignment)
853 #define T_PTR(B) SYM_PTR, 0, 0, &bits_in_pointer, &pointer_alignment, B
854 #define T_NODE(M,B,S,A) SYM_NODE, M, 0, S, A, B
855 #define T_CONST(B,S,A) T_NODE(MOD_CONST, B, S, A)
857 static const struct ctype_declare {
858 struct symbol *ptr;
859 enum type type;
860 unsigned long modifiers;
861 int rank;
862 int *bit_size;
863 int *maxalign;
864 struct symbol *base_type;
865 } ctype_declaration[] = {
866 { &bool_ctype, T_INT(-3, bool, MOD_UNSIGNED) },
867 { &void_ctype, T_BASETYPE },
868 { &type_ctype, T_BASETYPE },
869 { &incomplete_ctype, T_BASETYPE },
870 { &autotype_ctype, T_BASETYPE },
871 { &bad_ctype, T_BASETYPE },
873 { &char_ctype, T_INT(-2, char, CHAR_SIGNEDNESS) },
874 { &schar_ctype, T_SINT(-2, char) },
875 { &uchar_ctype, T_UINT(-2, char) },
876 { &short_ctype, T__INT(-1, short) },
877 { &sshort_ctype, T_SINT(-1, short) },
878 { &ushort_ctype, T_UINT(-1, short) },
879 { &int_ctype, T__INT( 0, int) },
880 { &sint_ctype, T_SINT( 0, int) },
881 { &uint_ctype, T_UINT( 0, int) },
882 { &long_ctype, T__INT( 1, long) },
883 { &slong_ctype, T_SINT( 1, long) },
884 { &ulong_ctype, T_UINT( 1, long) },
885 { &llong_ctype, T__INT( 2, longlong) },
886 { &sllong_ctype, T_SINT( 2, longlong) },
887 { &ullong_ctype, T_UINT( 2, longlong) },
888 { &int128_ctype, T__INT( 3, type128) },
889 { &sint128_ctype, T_SINT( 3, type128) },
890 { &uint128_ctype, T_UINT( 3, type128) },
892 { &float_ctype, T_FLOAT(-1, float) },
893 { &double_ctype, T_FLOAT( 0, double) },
894 { &ldouble_ctype, T_FLOAT( 1, longdouble) },
896 { &float32_ctype, T_FLOAT(-1, type32) },
897 { &float32x_ctype, T_FLOAT(-1, double) },
898 { &float64_ctype, T_FLOAT( 0, type64) },
899 { &float64x_ctype, T_FLOAT( 1, longdouble) },
900 { &float128_ctype, T_FLOAT_(2, type128, &max_alignment) },
902 { &string_ctype, T_PTR(&char_ctype) },
903 { &ptr_ctype, T_PTR(&void_ctype) },
904 { &null_ctype, T_PTR(&void_ctype) },
905 { &label_ctype, T_PTR(&void_ctype) },
906 { &lazy_ptr_ctype, T_PTR(&void_ctype) },
907 { &schar_ptr_ctype, T_PTR(&schar_ctype) },
908 { &short_ptr_ctype, T_PTR(&short_ctype) },
909 { &int_ptr_ctype, T_PTR(&int_ctype) },
910 { &uint_ptr_ctype, T_PTR(&uint_ctype) },
911 { &long_ptr_ctype, T_PTR(&long_ctype) },
912 { &ulong_ptr_ctype, T_PTR(&ulong_ctype) },
913 { &llong_ptr_ctype, T_PTR(&llong_ctype) },
914 { &ullong_ptr_ctype, T_PTR(&ullong_ctype) },
915 { &size_t_ptr_ctype, T_PTR(&void_ctype) }, // will be adjusted
916 { &intmax_ptr_ctype, T_PTR(&void_ctype) }, // will be adjusted
917 { &ptrdiff_ptr_ctype, T_PTR(&void_ctype) }, // will be adjusted
918 { &const_ptr_ctype, T_PTR(&const_void_ctype) },
919 { &const_string_ctype, T_PTR(&const_char_ctype) },
920 { &const_wstring_ctype,T_PTR(&const_wchar_ctype) },
922 { &const_void_ctype, T_CONST(&void_ctype, NULL, NULL) },
923 { &const_char_ctype, T_CONST(&char_ctype, &bits_in_char, &max_int_alignment)},
924 { &const_wchar_ctype, T_CONST(&int_ctype, NULL, NULL) },
925 { &volatile_void_ctype,T_NODE(MOD_VOLATILE, &void_ctype, NULL, NULL) },
926 { &volatile_ptr_ctype, T_PTR(&volatile_void_ctype) },
927 { &volatile_bool_ctype,T_NODE(MOD_VOLATILE, &bool_ctype, NULL, NULL) },
928 { &volatile_bool_ptr_ctype, T_PTR(&volatile_bool_ctype) },
929 { NULL, }
932 void init_ctype(void)
934 const struct ctype_declare *ctype;
936 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
937 struct symbol *sym = ctype->ptr;
938 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
939 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
940 unsigned long alignment = bits_to_bytes(bit_size);
942 if (alignment > maxalign)
943 alignment = maxalign;
944 sym->type = ctype->type;
945 sym->rank = ctype->rank;
946 sym->bit_size = bit_size;
947 sym->ctype.alignment = alignment;
948 sym->ctype.base_type = ctype->base_type;
949 sym->ctype.modifiers = ctype->modifiers;
951 if (sym->type == SYM_NODE) {
952 struct symbol *base = sym->ctype.base_type;
953 sym->rank = base->rank;
954 if (!ctype->bit_size)
955 sym->bit_size = base->bit_size;
956 if (!ctype->maxalign)
957 sym->ctype.alignment = base->ctype.alignment;
961 // and now some adjustments
962 if (funsigned_char) {
963 char_ctype.ctype.modifiers |= MOD_UNSIGNED;
964 char_ctype.ctype.modifiers &= ~MOD_SIGNED;
967 if (!ptrdiff_ctype)
968 ptrdiff_ctype = ssize_t_ctype;
969 if (!intptr_ctype)
970 intptr_ctype = ssize_t_ctype;
971 if (!uintptr_ctype)
972 uintptr_ctype = size_t_ctype;
974 size_t_ptr_ctype.ctype.base_type = size_t_ctype;
975 intmax_ptr_ctype.ctype.base_type = intmax_ctype;
976 ptrdiff_ptr_ctype.ctype.base_type = ptrdiff_ctype;
978 const_wchar_ctype.ctype.base_type = wchar_ctype;
979 const_wchar_ctype.rank = wchar_ctype->rank;
980 const_wchar_ctype.ctype.alignment = wchar_ctype->ctype.alignment;
981 const_wchar_ctype.bit_size = wchar_ctype->bit_size;