rosenberg: handle bit fields better
[smatch.git] / symbol.c
blob521de903ea8b9f457e5ca015b0efcc4ee20ce8c9
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 symbol *lookup_macro_symbol(const char *macro)
74 struct ident *id;
76 id = built_in_ident(macro);
77 return lookup_symbol(id, NS_MACRO);
80 struct context *alloc_context(void)
82 return __alloc_context(0);
85 struct symbol *alloc_symbol(struct position pos, int type)
87 struct symbol *sym = __alloc_symbol(0);
88 sym->type = type;
89 sym->pos = pos;
90 sym->endpos.type = 0;
91 return sym;
94 struct struct_union_info {
95 unsigned long max_align;
96 unsigned long bit_size;
97 int align_size;
98 char has_flex_array;
99 bool packed;
100 struct symbol *flex_array;
104 * Unions are fairly easy to lay out ;)
106 static void lay_out_union(struct symbol *sym, struct struct_union_info *info)
108 if (sym->bit_size < 0 && is_array_type(sym))
109 sparse_error(sym->pos, "flexible array member '%s' in a union", show_ident(sym->ident));
111 if (sym->bit_size > info->bit_size)
112 info->bit_size = sym->bit_size;
114 sym->offset = 0;
117 static int bitfield_base_size(struct symbol *sym)
119 if (sym->type == SYM_NODE)
120 sym = sym->ctype.base_type;
121 if (sym->type == SYM_BITFIELD)
122 sym = sym->ctype.base_type;
123 return sym->bit_size;
127 * Structures are a bit more interesting to lay out
129 static void lay_out_struct(struct symbol *sym, struct struct_union_info *info)
131 unsigned long bit_size, align_bit_mask;
132 unsigned long alignment;
133 int base_size;
135 bit_size = info->bit_size;
136 base_size = sym->bit_size;
139 * If the member is unsized, either it's a flexible array or
140 * it's invalid and a warning has already been issued.
142 if (base_size < 0) {
143 if (!is_array_type(sym))
144 return;
145 base_size = 0;
146 info->flex_array = sym;
149 alignment = info->packed ? 1 : sym->ctype.alignment;
150 align_bit_mask = bytes_to_bits(alignment) - 1;
153 * Bitfields have some very special rules..
155 if (is_bitfield_type (sym)) {
156 unsigned long bit_offset = bit_size & align_bit_mask;
157 int room = bitfield_base_size(sym) - bit_offset;
158 // Zero-width fields just fill up the unit.
159 int width = base_size ? : (bit_offset ? room : 0);
161 if (width > room && !info->packed) {
162 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
163 bit_offset = 0;
165 sym->offset = bits_to_bytes(bit_size - bit_offset);
166 sym->bit_offset = bit_offset;
167 sym->ctype.base_type->bit_offset = bit_offset;
168 info->bit_size = bit_size + width;
169 // warning (sym->pos, "bitfield: offset=%d:%d size=:%d", sym->offset, sym->bit_offset, width);
171 if (info->packed && sym->type == SYM_NODE)
172 sym->packed = 1;
173 return;
177 * Otherwise, just align it right and add it up..
179 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
180 sym->offset = bits_to_bytes(bit_size);
182 info->bit_size = bit_size + base_size;
183 // warning (sym->pos, "regular: offset=%d", sym->offset);
186 static struct symbol * examine_struct_union_type(struct symbol *sym, int advance)
188 struct struct_union_info info = {
189 .packed = sym->packed,
190 .max_align = 1,
191 .bit_size = 0,
192 .align_size = 1
194 unsigned long bit_size, bit_align;
195 void (*fn)(struct symbol *, struct struct_union_info *);
196 struct symbol *member;
198 fn = advance ? lay_out_struct : lay_out_union;
199 FOR_EACH_PTR(sym->symbol_list, member) {
200 if (member->ctype.base_type == &autotype_ctype) {
201 sparse_error(member->pos, "member '%s' has __auto_type", show_ident(member->ident));
202 member->ctype.base_type = &incomplete_ctype;
204 if (info.flex_array)
205 sparse_error(info.flex_array->pos, "flexible array member '%s' is not last", show_ident(info.flex_array->ident));
206 examine_symbol_type(member);
208 if (member->ctype.alignment > info.max_align && !sym->packed) {
209 // Unnamed bitfields do not affect alignment.
210 if (member->ident || !is_bitfield_type(member))
211 info.max_align = member->ctype.alignment;
214 if (has_flexible_array(member))
215 info.has_flex_array = 1;
216 if (has_flexible_array(member) && Wflexible_array_nested)
217 warning(member->pos, "nested flexible array");
218 fn(member, &info);
219 } END_FOR_EACH_PTR(member);
221 if (!sym->ctype.alignment)
222 sym->ctype.alignment = info.max_align;
223 bit_size = info.bit_size;
224 if (info.align_size) {
225 bit_align = bytes_to_bits(sym->ctype.alignment)-1;
226 bit_size = (bit_size + bit_align) & ~bit_align;
228 if (info.flex_array) {
229 info.has_flex_array = 1;
231 if (info.has_flex_array && (!is_union_type(sym) || Wflexible_array_union))
232 sym->has_flex_array = 1;
233 sym->bit_size = bit_size;
234 return sym;
237 static struct symbol *examine_base_type(struct symbol *sym)
239 struct symbol *base_type;
241 if (sym->ctype.base_type == &autotype_ctype) {
242 struct symbol *type = evaluate_expression(sym->initializer);
243 if (!type)
244 type = &bad_ctype;
245 if (is_bitfield_type(type)) {
246 warning(sym->pos, "__auto_type on bitfield");
247 if (type->type == SYM_NODE)
248 type = type->ctype.base_type;
249 type = type->ctype.base_type;
251 sym->ctype.base_type = type;
254 /* Check the base type */
255 base_type = examine_symbol_type(sym->ctype.base_type);
256 if (!base_type || base_type->type == SYM_PTR)
257 return base_type;
258 combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
259 sym->ctype.modifiers |= base_type->ctype.modifiers & MOD_PTRINHERIT;
260 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
261 (struct ptr_list **)&sym->ctype.contexts);
262 if (base_type->type == SYM_NODE) {
263 base_type = base_type->ctype.base_type;
264 sym->ctype.base_type = base_type;
265 sym->rank = base_type->rank;
267 return base_type;
270 static struct symbol * examine_array_type(struct symbol *sym)
272 struct symbol *base_type = examine_base_type(sym);
273 unsigned long bit_size = -1, alignment;
274 struct expression *array_size = sym->array_size;
276 if (!base_type)
277 return sym;
279 if (array_size) {
280 bit_size = array_element_offset(base_type->bit_size,
281 get_expression_value_silent(array_size));
282 if (array_size->type != EXPR_VALUE) {
283 if (Wvla)
284 warning(array_size->pos, "Variable length array is used.");
285 bit_size = -1;
288 if (has_flexible_array(base_type) && Wflexible_array_array)
289 warning(sym->pos, "array of flexible structures");
290 alignment = base_type->ctype.alignment;
291 if (!sym->ctype.alignment)
292 sym->ctype.alignment = alignment;
293 sym->bit_size = bit_size;
294 return sym;
297 static struct symbol *examine_bitfield_type(struct symbol *sym)
299 struct symbol *base_type = examine_base_type(sym);
300 unsigned long alignment, modifiers;
302 if (!base_type)
303 return sym;
304 if (sym->bit_size > base_type->bit_size) {
305 sparse_error(sym->pos, "bitfield '%s' is wider (%d) than its type (%s)",
306 show_ident(sym->ident), sym->bit_size, show_typename(base_type));
307 sym->bit_size = -1;
310 alignment = base_type->ctype.alignment;
311 if (!sym->ctype.alignment)
312 sym->ctype.alignment = alignment;
313 modifiers = base_type->ctype.modifiers;
315 /* use -funsigned-bitfields to determine the sign if not explicit */
316 if (!(modifiers & MOD_EXPLICITLY_SIGNED) && funsigned_bitfields)
317 modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
318 sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
319 return sym;
323 * "typeof" will have to merge the types together
325 void merge_type(struct symbol *sym, struct symbol *base_type)
327 combine_address_space(sym->pos, &sym->ctype.as, base_type->ctype.as);
328 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
329 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
330 (struct ptr_list **)&sym->ctype.contexts);
331 sym->ctype.base_type = base_type->ctype.base_type;
332 if (sym->ctype.base_type->type == SYM_NODE)
333 merge_type(sym, sym->ctype.base_type);
336 static bool is_wstring_expr(struct expression *expr)
338 while (expr) {
339 switch (expr->type) {
340 case EXPR_STRING:
341 return 1;
342 case EXPR_INITIALIZER:
343 if (expression_list_size(expr->expr_list) != 1)
344 return 0;
345 expr = first_expression(expr->expr_list);
346 break;
347 case EXPR_PREOP:
348 if (expr->op == '(') {
349 expr = expr->unop;
350 break;
352 default:
353 return 0;
356 return 0;
359 static int count_array_initializer(struct symbol *t, struct expression *expr)
361 int nr = 0;
362 int is_char = 0;
365 * Arrays of character types are special; they can be initialized by
366 * string literal _or_ by string literal in braces. The latter means
367 * that with T x[] = {<string literal>} number of elements in x depends
368 * on T - if it's a character type, we get the length of string literal
369 * (including NUL), otherwise we have one element here.
371 if (t->ctype.base_type == &int_type && t->rank == -2)
372 is_char = 1;
373 else if (t == wchar_ctype && is_wstring_expr(expr))
374 is_char = 1;
376 switch (expr->type) {
377 case EXPR_INITIALIZER: {
378 struct expression *entry;
379 int count = 0;
380 int str_len = 0;
381 FOR_EACH_PTR(expr->expr_list, entry) {
382 count++;
383 switch (entry->type) {
384 case EXPR_INDEX:
385 if (entry->idx_to >= nr)
386 nr = entry->idx_to+1;
387 break;
388 case EXPR_PREOP: {
389 struct expression *e = entry;
390 if (is_char) {
391 while (e && e->type == EXPR_PREOP && e->op == '(')
392 e = e->unop;
393 if (e && e->type == EXPR_STRING) {
394 entry = e;
395 case EXPR_STRING:
396 if (is_char)
397 str_len = entry->string->length;
403 default:
404 nr++;
406 } END_FOR_EACH_PTR(entry);
407 if (count == 1 && str_len)
408 nr = str_len;
409 break;
411 case EXPR_PREOP:
412 if (is_char) {
413 struct expression *e = expr;
414 while (e && e->type == EXPR_PREOP && e->op == '(')
415 e = e->unop;
416 if (e && e->type == EXPR_STRING) {
417 expr = e;
418 case EXPR_STRING:
419 if (is_char)
420 nr = expr->string->length;
423 break;
424 default:
425 break;
427 return nr;
430 static struct expression *get_symbol_initializer(struct symbol *sym)
432 do {
433 if (sym->initializer)
434 return sym->initializer;
435 } while ((sym = sym->same_symbol) != NULL);
436 return NULL;
439 static unsigned int implicit_array_size(struct symbol *node, unsigned int count)
441 struct symbol *arr_ori = node->ctype.base_type;
442 struct symbol *arr_new = alloc_symbol(node->pos, SYM_ARRAY);
443 struct symbol *elem_type = arr_ori->ctype.base_type;
444 struct expression *size = alloc_const_expression(node->pos, count);
445 unsigned int bit_size = array_element_offset(elem_type->bit_size, count);
447 *arr_new = *arr_ori;
448 arr_new->bit_size = bit_size;
449 arr_new->array_size = size;
450 node->array_size = size;
451 node->ctype.base_type = arr_new;
453 return bit_size;
456 static struct symbol * examine_node_type(struct symbol *sym)
458 struct symbol *base_type = examine_base_type(sym);
459 int bit_size;
460 unsigned long alignment;
462 /* SYM_NODE - figure out what the type of the node was.. */
463 bit_size = 0;
464 alignment = 0;
465 if (!base_type)
466 return sym;
468 bit_size = base_type->bit_size;
469 alignment = base_type->ctype.alignment;
471 /* Pick up signedness information into the node */
472 sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
474 if (!sym->ctype.alignment)
475 sym->ctype.alignment = alignment;
477 /* Unsized array? The size might come from the initializer.. */
478 if (bit_size < 0 && base_type->type == SYM_ARRAY) {
479 struct expression *initializer = get_symbol_initializer(sym);
480 if (initializer) {
481 struct symbol *node_type = base_type->ctype.base_type;
482 int count = count_array_initializer(node_type, initializer);
484 if (node_type && node_type->bit_size >= 0)
485 bit_size = implicit_array_size(sym, count);
489 sym->bit_size = bit_size;
490 sym->rank = base_type->rank;
491 return sym;
494 static struct symbol *examine_enum_type(struct symbol *sym)
496 struct symbol *base_type = examine_base_type(sym);
498 sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
499 sym->bit_size = bits_in_enum;
500 if (base_type->bit_size > sym->bit_size)
501 sym->bit_size = base_type->bit_size;
502 sym->ctype.alignment = enum_alignment;
503 if (base_type->ctype.alignment > sym->ctype.alignment)
504 sym->ctype.alignment = base_type->ctype.alignment;
505 return sym;
508 static struct symbol *examine_pointer_type(struct symbol *sym)
511 * Since pointers to incomplete types can be used,
512 * for example in a struct-declaration-list,
513 * the base type must *not* be examined here.
514 * It thus means that it needs to be done later,
515 * when the base type of the pointer is looked at.
517 if (!sym->bit_size)
518 sym->bit_size = bits_in_pointer;
519 if (!sym->ctype.alignment)
520 sym->ctype.alignment = pointer_alignment;
521 return sym;
524 static struct symbol *examine_typeof(struct symbol *sym)
526 struct symbol *base = evaluate_expression(sym->initializer);
527 unsigned long mod = 0;
529 if (!base)
530 base = &bad_ctype;
531 if (base->type == SYM_NODE) {
532 mod |= base->ctype.modifiers & MOD_TYPEOF;
533 base = base->ctype.base_type;
535 if (base->type == SYM_BITFIELD)
536 warning(base->pos, "typeof applied to bitfield type");
537 sym->type = SYM_NODE;
538 sym->ctype.modifiers = mod;
539 sym->ctype.base_type = base;
540 return examine_node_type(sym);
544 * Fill in type size and alignment information for
545 * regular SYM_TYPE things.
547 struct symbol *examine_symbol_type(struct symbol * sym)
549 if (!sym)
550 return sym;
552 /* Already done? */
553 if (sym->examined)
554 return sym;
555 sym->examined = 1;
557 switch (sym->type) {
558 case SYM_FN:
559 case SYM_NODE:
560 return examine_node_type(sym);
561 case SYM_ARRAY:
562 return examine_array_type(sym);
563 case SYM_STRUCT:
564 return examine_struct_union_type(sym, 1);
565 case SYM_UNION:
566 return examine_struct_union_type(sym, 0);
567 case SYM_PTR:
568 return examine_pointer_type(sym);
569 case SYM_ENUM:
570 return examine_enum_type(sym);
571 case SYM_BITFIELD:
572 return examine_bitfield_type(sym);
573 case SYM_BASETYPE:
574 /* Size and alignment had better already be set up */
575 return sym;
576 case SYM_TYPEOF:
577 return examine_typeof(sym);
578 case SYM_PREPROCESSOR:
579 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
580 return NULL;
581 case SYM_UNINITIALIZED:
582 // sparse_error(sym->pos, "ctype on uninitialized symbol '%s'", show_typename(sym));
583 return NULL;
584 case SYM_RESTRICT:
585 examine_base_type(sym);
586 return sym;
587 case SYM_FOULED:
588 examine_base_type(sym);
589 return sym;
590 default:
591 // sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
592 break;
594 return sym;
597 const char* get_type_name(enum type type)
599 const char *type_lookup[] = {
600 [SYM_UNINITIALIZED] = "uninitialized",
601 [SYM_PREPROCESSOR] = "preprocessor",
602 [SYM_BASETYPE] = "basetype",
603 [SYM_NODE] = "node",
604 [SYM_PTR] = "pointer",
605 [SYM_FN] = "function",
606 [SYM_ARRAY] = "array",
607 [SYM_STRUCT] = "struct",
608 [SYM_UNION] = "union",
609 [SYM_ENUM] = "enum",
610 [SYM_TYPEOF] = "typeof",
611 [SYM_BITFIELD] = "bitfield",
612 [SYM_LABEL] = "label",
613 [SYM_RESTRICT] = "restrict",
614 [SYM_FOULED] = "fouled",
615 [SYM_KEYWORD] = "keyword",
616 [SYM_BAD] = "bad"};
618 if (type <= SYM_BAD)
619 return type_lookup[type];
620 else
621 return NULL;
624 struct symbol *examine_pointer_target(struct symbol *sym)
626 return examine_base_type(sym);
629 static struct symbol_list *restr, *fouled;
631 void create_fouled(struct symbol *type)
633 if (type->bit_size < bits_in_int) {
634 struct symbol *new = alloc_symbol(type->pos, type->type);
635 *new = *type;
636 new->bit_size = bits_in_int;
637 new->rank = 0;
638 new->type = SYM_FOULED;
639 new->ctype.base_type = type;
640 add_symbol(&restr, type);
641 add_symbol(&fouled, new);
645 struct symbol *befoul(struct symbol *type)
647 struct symbol *t1, *t2;
648 while (type->type == SYM_NODE)
649 type = type->ctype.base_type;
650 PREPARE_PTR_LIST(restr, t1);
651 PREPARE_PTR_LIST(fouled, t2);
652 for (;;) {
653 if (t1 == type)
654 return t2;
655 if (!t1)
656 break;
657 NEXT_PTR_LIST(t1);
658 NEXT_PTR_LIST(t2);
660 FINISH_PTR_LIST(t2);
661 FINISH_PTR_LIST(t1);
662 return NULL;
665 static void inherit_declaration(struct symbol *sym, struct symbol *prev)
667 unsigned long mods = prev->ctype.modifiers;
669 // inherit function attributes
670 sym->ctype.modifiers |= mods & MOD_FUN_ATTR;
673 void check_declaration(struct symbol *sym)
675 int warned = 0;
676 struct symbol *next = sym;
678 while ((next = next->next_id) != NULL) {
679 if (next->namespace != sym->namespace)
680 continue;
681 if (sym->scope == next->scope) {
682 sym->same_symbol = next;
683 inherit_declaration(sym, next);
684 return;
686 /* Extern in block level matches a TOPLEVEL non-static symbol */
687 if (sym->ctype.modifiers & MOD_EXTERN) {
688 if ((next->ctype.modifiers & (MOD_TOPLEVEL|MOD_STATIC)) == MOD_TOPLEVEL) {
689 sym->same_symbol = next;
690 return;
694 if (!Wshadow || warned)
695 continue;
696 if (get_sym_type(next) == SYM_FN)
697 continue;
698 warned = 1;
699 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
700 info(next->pos, "originally declared here");
704 static void inherit_static(struct symbol *sym)
706 struct symbol *prev;
708 // only 'plain' symbols are concerned
709 if (sym->ctype.modifiers & (MOD_STATIC|MOD_EXTERN))
710 return;
712 for (prev = sym->next_id; prev; prev = prev->next_id) {
713 if (prev->namespace != NS_SYMBOL)
714 continue;
715 if (prev->scope != file_scope)
716 continue;
718 sym->ctype.modifiers |= prev->ctype.modifiers & MOD_STATIC;
720 // previous declarations are already converted
721 return;
725 void bind_symbol_with_scope(struct symbol *sym, struct ident *ident, enum namespace ns, struct scope *scope)
727 if (sym->bound) {
728 sparse_error(sym->pos, "internal error: symbol type already bound");
729 return;
731 if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
732 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
733 return;
735 sym->namespace = ns;
736 sym->next_id = ident->symbols;
737 ident->symbols = sym;
738 if (sym->ident && sym->ident != ident)
739 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
740 sym->ident = ident;
741 sym->bound = 1;
743 if (ns == NS_SYMBOL && toplevel(scope)) {
744 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
746 inherit_static(sym);
748 scope = global_scope;
749 if (sym->ctype.modifiers & MOD_STATIC ||
750 is_extern_inline(sym)) {
751 scope = file_scope;
752 mod = MOD_TOPLEVEL;
754 sym->ctype.modifiers |= mod;
756 bind_scope(sym, scope);
759 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
761 struct scope *scope = block_scope;;
763 if (ns == NS_MACRO)
764 scope = file_scope;
765 if (ns == NS_LABEL)
766 scope = function_scope;
767 bind_symbol_with_scope(sym, ident, ns, scope);
770 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
772 struct ident *ident = built_in_ident(name);
773 struct symbol *sym = lookup_symbol(ident, namespace);
775 if (sym && sym->type != type)
776 die("symbol %s created with different types: %d old %d", name,
777 type, sym->type);
779 if (!sym) {
780 struct token *token = built_in_token(stream, ident);
782 sym = alloc_symbol(token->pos, type);
783 bind_symbol(sym, token->ident, namespace);
785 return sym;
790 * Abstract types
792 struct symbol int_type,
793 fp_type;
796 * C types (i.e. actual instances that the abstract types
797 * can map onto)
799 struct symbol bool_ctype, void_ctype, type_ctype,
800 char_ctype, schar_ctype, uchar_ctype,
801 short_ctype, sshort_ctype, ushort_ctype,
802 int_ctype, sint_ctype, uint_ctype,
803 long_ctype, slong_ctype, ulong_ctype,
804 llong_ctype, sllong_ctype, ullong_ctype,
805 int128_ctype, sint128_ctype, uint128_ctype,
806 float_ctype, double_ctype, ldouble_ctype,
807 string_ctype, ptr_ctype, lazy_ptr_ctype,
808 incomplete_ctype, label_ctype, bad_ctype,
809 null_ctype;
810 struct symbol autotype_ctype;
811 struct symbol schar_ptr_ctype, short_ptr_ctype;
812 struct symbol int_ptr_ctype, uint_ptr_ctype;
813 struct symbol long_ptr_ctype, ulong_ptr_ctype;
814 struct symbol llong_ptr_ctype, ullong_ptr_ctype;
815 struct symbol size_t_ptr_ctype, intmax_ptr_ctype, ptrdiff_ptr_ctype;
816 struct symbol float32_ctype, float32x_ctype;
817 struct symbol float64_ctype, float64x_ctype;
818 struct symbol float128_ctype;
819 struct symbol const_void_ctype, const_char_ctype;
820 struct symbol const_ptr_ctype, const_string_ctype;
821 struct symbol const_wchar_ctype, const_wstring_ctype;
822 struct symbol volatile_void_ctype, volatile_ptr_ctype;
823 struct symbol volatile_bool_ctype, volatile_bool_ptr_ctype;
825 struct symbol zero_int;
827 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
828 #define __IDENT(n,str,res) \
829 struct ident n = __INIT_IDENT(str,res)
831 #include "ident-list.h"
833 void init_symbols(void)
835 int stream = init_stream(NULL, "builtin", -1, includepath);
837 #define __IDENT(n,str,res) \
838 hash_ident(&n)
839 #include "ident-list.h"
841 init_parser(stream);
844 #ifdef __CHAR_UNSIGNED__
845 #define CHAR_SIGNEDNESS MOD_UNSIGNED
846 #else
847 #define CHAR_SIGNEDNESS MOD_SIGNED
848 #endif
849 // For fix-sized types
850 static int bits_in_type32 = 32;
851 static int bits_in_type64 = 64;
852 static int bits_in_type128 = 128;
854 #define T_BASETYPE SYM_BASETYPE, 0, 0, NULL, NULL, NULL
855 #define T_INT(R, S, M) SYM_BASETYPE, M, R, &bits_in_##S, &max_int_alignment, &int_type
856 #define T__INT(R, S) T_INT(R, S, MOD_SIGNED)
857 #define T_SINT(R, S) T_INT(R, S, MOD_ESIGNED)
858 #define T_UINT(R,S) T_INT(R, S, MOD_UNSIGNED)
859 #define T_FLOAT_(R,S,A) SYM_BASETYPE, 0, R, &bits_in_##S, A, &fp_type
860 #define T_FLOAT(R, S) T_FLOAT_(R, S, &max_fp_alignment)
861 #define T_PTR(B) SYM_PTR, 0, 0, &bits_in_pointer, &pointer_alignment, B
862 #define T_NODE(M,B,S,A) SYM_NODE, M, 0, S, A, B
863 #define T_CONST(B,S,A) T_NODE(MOD_CONST, B, S, A)
865 static const struct ctype_declare {
866 struct symbol *ptr;
867 enum type type;
868 unsigned long modifiers;
869 int rank;
870 int *bit_size;
871 int *maxalign;
872 struct symbol *base_type;
873 } ctype_declaration[] = {
874 { &bool_ctype, T_INT(-3, bool, MOD_UNSIGNED) },
875 { &void_ctype, T_BASETYPE },
876 { &type_ctype, T_BASETYPE },
877 { &incomplete_ctype, T_BASETYPE },
878 { &autotype_ctype, T_BASETYPE },
879 { &bad_ctype, T_BASETYPE },
881 { &char_ctype, T_INT(-2, char, CHAR_SIGNEDNESS) },
882 { &schar_ctype, T_SINT(-2, char) },
883 { &uchar_ctype, T_UINT(-2, char) },
884 { &short_ctype, T__INT(-1, short) },
885 { &sshort_ctype, T_SINT(-1, short) },
886 { &ushort_ctype, T_UINT(-1, short) },
887 { &int_ctype, T__INT( 0, int) },
888 { &sint_ctype, T_SINT( 0, int) },
889 { &uint_ctype, T_UINT( 0, int) },
890 { &long_ctype, T__INT( 1, long) },
891 { &slong_ctype, T_SINT( 1, long) },
892 { &ulong_ctype, T_UINT( 1, long) },
893 { &llong_ctype, T__INT( 2, longlong) },
894 { &sllong_ctype, T_SINT( 2, longlong) },
895 { &ullong_ctype, T_UINT( 2, longlong) },
896 { &int128_ctype, T__INT( 3, type128) },
897 { &sint128_ctype, T_SINT( 3, type128) },
898 { &uint128_ctype, T_UINT( 3, type128) },
900 { &float_ctype, T_FLOAT(-1, float) },
901 { &double_ctype, T_FLOAT( 0, double) },
902 { &ldouble_ctype, T_FLOAT( 1, longdouble) },
904 { &float32_ctype, T_FLOAT(-1, type32) },
905 { &float32x_ctype, T_FLOAT(-1, double) },
906 { &float64_ctype, T_FLOAT( 0, type64) },
907 { &float64x_ctype, T_FLOAT( 1, longdouble) },
908 { &float128_ctype, T_FLOAT_(2, type128, &max_alignment) },
910 { &string_ctype, T_PTR(&char_ctype) },
911 { &ptr_ctype, T_PTR(&void_ctype) },
912 { &null_ctype, T_PTR(&void_ctype) },
913 { &label_ctype, T_PTR(&void_ctype) },
914 { &lazy_ptr_ctype, T_PTR(&void_ctype) },
915 { &schar_ptr_ctype, T_PTR(&schar_ctype) },
916 { &short_ptr_ctype, T_PTR(&short_ctype) },
917 { &int_ptr_ctype, T_PTR(&int_ctype) },
918 { &uint_ptr_ctype, T_PTR(&uint_ctype) },
919 { &long_ptr_ctype, T_PTR(&long_ctype) },
920 { &ulong_ptr_ctype, T_PTR(&ulong_ctype) },
921 { &llong_ptr_ctype, T_PTR(&llong_ctype) },
922 { &ullong_ptr_ctype, T_PTR(&ullong_ctype) },
923 { &size_t_ptr_ctype, T_PTR(&void_ctype) }, // will be adjusted
924 { &intmax_ptr_ctype, T_PTR(&void_ctype) }, // will be adjusted
925 { &ptrdiff_ptr_ctype, T_PTR(&void_ctype) }, // will be adjusted
926 { &const_ptr_ctype, T_PTR(&const_void_ctype) },
927 { &const_string_ctype, T_PTR(&const_char_ctype) },
928 { &const_wstring_ctype,T_PTR(&const_wchar_ctype) },
930 { &const_void_ctype, T_CONST(&void_ctype, NULL, NULL) },
931 { &const_char_ctype, T_CONST(&char_ctype, &bits_in_char, &max_int_alignment)},
932 { &const_wchar_ctype, T_CONST(&int_ctype, NULL, NULL) },
933 { &volatile_void_ctype,T_NODE(MOD_VOLATILE, &void_ctype, NULL, NULL) },
934 { &volatile_ptr_ctype, T_PTR(&volatile_void_ctype) },
935 { &volatile_bool_ctype,T_NODE(MOD_VOLATILE, &bool_ctype, NULL, NULL) },
936 { &volatile_bool_ptr_ctype, T_PTR(&volatile_bool_ctype) },
937 { NULL, }
940 void init_ctype(void)
942 const struct ctype_declare *ctype;
944 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
945 struct symbol *sym = ctype->ptr;
946 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
947 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
948 unsigned long alignment = bits_to_bytes(bit_size);
950 if (alignment > maxalign)
951 alignment = maxalign;
952 sym->type = ctype->type;
953 sym->rank = ctype->rank;
954 sym->bit_size = bit_size;
955 sym->ctype.alignment = alignment;
956 sym->ctype.base_type = ctype->base_type;
957 sym->ctype.modifiers = ctype->modifiers;
959 if (sym->type == SYM_NODE) {
960 struct symbol *base = sym->ctype.base_type;
961 sym->rank = base->rank;
962 if (!ctype->bit_size)
963 sym->bit_size = base->bit_size;
964 if (!ctype->maxalign)
965 sym->ctype.alignment = base->ctype.alignment;
969 // and now some adjustments
970 if (funsigned_char) {
971 char_ctype.ctype.modifiers |= MOD_UNSIGNED;
972 char_ctype.ctype.modifiers &= ~MOD_SIGNED;
975 if (!ptrdiff_ctype)
976 ptrdiff_ctype = ssize_t_ctype;
977 if (!intptr_ctype)
978 intptr_ctype = ssize_t_ctype;
979 if (!uintptr_ctype)
980 uintptr_ctype = size_t_ctype;
982 size_t_ptr_ctype.ctype.base_type = size_t_ctype;
983 intmax_ptr_ctype.ctype.base_type = intmax_ctype;
984 ptrdiff_ptr_ctype.ctype.base_type = ptrdiff_ctype;
986 const_wchar_ctype.ctype.base_type = wchar_ctype;
987 const_wchar_ctype.rank = wchar_ctype->rank;
988 const_wchar_ctype.ctype.alignment = wchar_ctype->ctype.alignment;
989 const_wchar_ctype.bit_size = wchar_ctype->bit_size;