stree fallout: implications not working 100%
[smatch.git] / symbol.c
blobc0ab564353e480e71b16d05c1f634ae943cfdd59
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"
37 #include "target.h"
40 * Secondary symbol list for stuff that needs to be output because it
41 * was used.
43 struct symbol_list *translation_unit_used_list = NULL;
46 * If the symbol is an inline symbol, add it to the list of symbols to parse
48 void access_symbol(struct symbol *sym)
50 if (sym->ctype.modifiers & MOD_INLINE) {
51 if (!(sym->ctype.modifiers & MOD_ACCESSED)) {
52 add_symbol(&translation_unit_used_list, sym);
53 sym->ctype.modifiers |= MOD_ACCESSED;
58 struct symbol *lookup_symbol(struct ident *ident, enum namespace ns)
60 struct symbol *sym;
62 for (sym = ident->symbols; sym; sym = sym->next_id) {
63 if (sym->namespace & ns) {
64 sym->used = 1;
65 return sym;
68 return NULL;
71 struct context *alloc_context(void)
73 return __alloc_context(0);
76 struct symbol *alloc_symbol(struct position pos, int type)
78 struct symbol *sym = __alloc_symbol(0);
79 sym->type = type;
80 sym->pos = pos;
81 sym->endpos.type = 0;
82 sym->ctype.attribute = &null_attr;
83 return sym;
86 struct struct_union_info {
87 unsigned long max_align;
88 unsigned long bit_size;
89 int align_size;
93 * Unions are fairly easy to lay out ;)
95 static void lay_out_union(struct symbol *sym, struct struct_union_info *info)
97 examine_symbol_type(sym);
99 // Unnamed bitfields do not affect alignment.
100 if (sym->ident || !is_bitfield_type(sym)) {
101 if (sym->ctype.alignment > info->max_align)
102 info->max_align = sym->ctype.alignment;
105 if (sym->bit_size > info->bit_size)
106 info->bit_size = sym->bit_size;
108 sym->offset = 0;
111 static int bitfield_base_size(struct symbol *sym)
113 if (sym->type == SYM_NODE)
114 sym = sym->ctype.base_type;
115 if (sym->type == SYM_BITFIELD)
116 sym = sym->ctype.base_type;
117 return sym->bit_size;
121 * Structures are a bit more interesting to lay out
123 static void lay_out_struct(struct symbol *sym, struct struct_union_info *info)
125 unsigned long bit_size, align_bit_mask;
126 int base_size;
128 examine_symbol_type(sym);
130 // Unnamed bitfields do not affect alignment.
131 if (sym->ident || !is_bitfield_type(sym)) {
132 if (sym->ctype.alignment > info->max_align)
133 info->max_align = sym->ctype.alignment;
136 bit_size = info->bit_size;
137 base_size = sym->bit_size;
140 * Unsized arrays cause us to not align the resulting
141 * structure size
143 if (base_size < 0) {
144 info->align_size = 0;
145 base_size = 0;
148 align_bit_mask = bytes_to_bits(sym->ctype.alignment) - 1;
151 * Bitfields have some very special rules..
153 if (is_bitfield_type (sym)) {
154 unsigned long bit_offset = bit_size & align_bit_mask;
155 int room = bitfield_base_size(sym) - bit_offset;
156 // Zero-width fields just fill up the unit.
157 int width = base_size ? : (bit_offset ? room : 0);
159 if (width > room) {
160 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
161 bit_offset = 0;
163 sym->offset = bits_to_bytes(bit_size - bit_offset);
164 sym->bit_offset = bit_offset;
165 sym->ctype.base_type->bit_offset = bit_offset;
166 info->bit_size = bit_size + width;
167 // warning (sym->pos, "bitfield: offset=%d:%d size=:%d", sym->offset, sym->bit_offset, width);
169 return;
173 * Otherwise, just align it right and add it up..
175 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
176 sym->offset = bits_to_bytes(bit_size);
178 info->bit_size = bit_size + base_size;
179 // warning (sym->pos, "regular: offset=%d", sym->offset);
182 static struct symbol * examine_struct_union_type(struct symbol *sym, int advance)
184 struct struct_union_info info = {
185 .max_align = 1,
186 .bit_size = 0,
187 .align_size = 1
189 unsigned long bit_size, bit_align;
190 void (*fn)(struct symbol *, struct struct_union_info *);
191 struct symbol *member;
193 fn = advance ? lay_out_struct : lay_out_union;
194 FOR_EACH_PTR(sym->symbol_list, member) {
195 fn(member, &info);
196 } END_FOR_EACH_PTR(member);
198 if (!sym->ctype.alignment)
199 sym->ctype.alignment = info.max_align;
200 bit_size = info.bit_size;
201 if (info.align_size) {
202 bit_align = bytes_to_bits(sym->ctype.alignment)-1;
203 bit_size = (bit_size + bit_align) & ~bit_align;
205 sym->bit_size = bit_size;
206 return sym;
209 static struct symbol *examine_base_type(struct symbol *sym)
211 struct symbol *base_type;
213 /* Check the base type */
214 base_type = examine_symbol_type(sym->ctype.base_type);
215 if (!base_type || base_type->type == SYM_PTR)
216 return base_type;
217 sym->ctype.modifiers |= base_type->ctype.modifiers & MOD_PTRINHERIT;
219 merge_attr(&sym->ctype, &base_type->ctype);
221 if (base_type->type == SYM_NODE) {
222 base_type = base_type->ctype.base_type;
223 sym->ctype.base_type = base_type;
225 return base_type;
228 static struct symbol * examine_array_type(struct symbol *sym)
230 struct symbol *base_type = examine_base_type(sym);
231 unsigned long bit_size = -1, alignment;
232 struct expression *array_size = sym->array_size;
234 if (!base_type)
235 return sym;
237 if (array_size) {
238 bit_size = base_type->bit_size * get_expression_value_silent(array_size);
239 if (array_size->type != EXPR_VALUE) {
240 if (Wvla)
241 warning(array_size->pos, "Variable length array is used.");
242 bit_size = -1;
245 alignment = base_type->ctype.alignment;
246 if (!sym->ctype.alignment)
247 sym->ctype.alignment = alignment;
248 sym->bit_size = bit_size;
249 return sym;
252 static struct symbol *examine_bitfield_type(struct symbol *sym)
254 struct symbol *base_type = examine_base_type(sym);
255 unsigned long bit_size, alignment, modifiers;
257 if (!base_type)
258 return sym;
259 bit_size = base_type->bit_size;
260 if (sym->bit_size > bit_size)
261 warning(sym->pos, "impossible field-width, %d, for this type", sym->bit_size);
263 alignment = base_type->ctype.alignment;
264 if (!sym->ctype.alignment)
265 sym->ctype.alignment = alignment;
266 modifiers = base_type->ctype.modifiers;
268 /* Bitfields are unsigned, unless the base type was explicitly signed */
269 if (!(modifiers & MOD_EXPLICITLY_SIGNED))
270 modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
271 sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
272 return sym;
276 * "typeof" will have to merge the types together
278 void merge_type(struct symbol *sym, struct symbol *base_type)
280 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
281 merge_attr(&sym->ctype, &base_type->ctype);
282 sym->ctype.base_type = base_type->ctype.base_type;
283 if (sym->ctype.base_type->type == SYM_NODE)
284 merge_type(sym, sym->ctype.base_type);
287 static int count_array_initializer(struct symbol *t, struct expression *expr)
289 int nr = 0;
290 int is_char = 0;
293 * Arrays of character types are special; they can be initialized by
294 * string literal _or_ by string literal in braces. The latter means
295 * that with T x[] = {<string literal>} number of elements in x depends
296 * on T - if it's a character type, we get the length of string literal
297 * (including NUL), otherwise we have one element here.
299 if (t->ctype.base_type == &int_type && t->ctype.modifiers & MOD_CHAR)
300 is_char = 1;
302 switch (expr->type) {
303 case EXPR_INITIALIZER: {
304 struct expression *entry;
305 int count = 0;
306 int str_len = 0;
307 FOR_EACH_PTR(expr->expr_list, entry) {
308 count++;
309 switch (entry->type) {
310 case EXPR_INDEX:
311 if (entry->idx_to >= nr)
312 nr = entry->idx_to+1;
313 break;
314 case EXPR_PREOP: {
315 struct expression *e = entry;
316 if (is_char) {
317 while (e && e->type == EXPR_PREOP && e->op == '(')
318 e = e->unop;
319 if (e && e->type == EXPR_STRING) {
320 entry = e;
321 case EXPR_STRING:
322 if (is_char)
323 str_len = entry->string->length;
329 default:
330 nr++;
332 } END_FOR_EACH_PTR(entry);
333 if (count == 1 && str_len)
334 nr = str_len;
335 break;
337 case EXPR_PREOP:
338 if (is_char) {
339 struct expression *e = expr;
340 while (e && e->type == EXPR_PREOP && e->op == '(')
341 e = e->unop;
342 if (e && e->type == EXPR_STRING) {
343 expr = e;
344 case EXPR_STRING:
345 if (is_char)
346 nr = expr->string->length;
349 break;
350 default:
351 break;
353 return nr;
356 static struct symbol * examine_node_type(struct symbol *sym)
358 struct symbol *base_type = examine_base_type(sym);
359 int bit_size;
360 unsigned long alignment;
362 /* SYM_NODE - figure out what the type of the node was.. */
363 bit_size = 0;
364 alignment = 0;
365 if (!base_type)
366 return sym;
368 bit_size = base_type->bit_size;
369 alignment = base_type->ctype.alignment;
371 /* Pick up signedness information into the node */
372 sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
374 if (!sym->ctype.alignment)
375 sym->ctype.alignment = alignment;
377 /* Unsized array? The size might come from the initializer.. */
378 if (bit_size < 0 && base_type->type == SYM_ARRAY && sym->initializer) {
379 struct symbol *node_type = base_type->ctype.base_type;
380 int count = count_array_initializer(node_type, sym->initializer);
382 if (node_type && node_type->bit_size >= 0)
383 bit_size = node_type->bit_size * count;
386 sym->bit_size = bit_size;
387 return sym;
390 static struct symbol *examine_enum_type(struct symbol *sym)
392 struct symbol *base_type = examine_base_type(sym);
394 sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
395 sym->bit_size = bits_in_enum;
396 if (base_type->bit_size > sym->bit_size)
397 sym->bit_size = base_type->bit_size;
398 sym->ctype.alignment = enum_alignment;
399 if (base_type->ctype.alignment > sym->ctype.alignment)
400 sym->ctype.alignment = base_type->ctype.alignment;
401 return sym;
404 static struct symbol *examine_pointer_type(struct symbol *sym)
407 * We need to set the pointer size first, and
408 * examine the thing we point to only afterwards.
409 * That's because this pointer type may end up
410 * being needed for the base type size evaluation.
412 if (!sym->bit_size)
413 sym->bit_size = bits_in_pointer;
414 if (!sym->ctype.alignment)
415 sym->ctype.alignment = pointer_alignment;
416 return sym;
420 * Fill in type size and alignment information for
421 * regular SYM_TYPE things.
423 struct symbol *examine_symbol_type(struct symbol * sym)
425 if (!sym)
426 return sym;
428 /* Already done? */
429 if (sym->examined)
430 return sym;
431 sym->examined = 1;
433 switch (sym->type) {
434 case SYM_FN:
435 case SYM_NODE:
436 return examine_node_type(sym);
437 case SYM_ARRAY:
438 return examine_array_type(sym);
439 case SYM_STRUCT:
440 return examine_struct_union_type(sym, 1);
441 case SYM_UNION:
442 return examine_struct_union_type(sym, 0);
443 case SYM_PTR:
444 return examine_pointer_type(sym);
445 case SYM_ENUM:
446 return examine_enum_type(sym);
447 case SYM_BITFIELD:
448 return examine_bitfield_type(sym);
449 case SYM_BASETYPE:
450 /* Size and alignment had better already be set up */
451 return sym;
452 case SYM_TYPEOF: {
453 struct symbol *base = evaluate_expression(sym->initializer);
454 if (base) {
455 if (is_bitfield_type(base))
456 warning(base->pos, "typeof applied to bitfield type");
457 if (base->type == SYM_NODE)
458 base = base->ctype.base_type;
459 sym->type = SYM_NODE;
460 sym->ctype.modifiers = 0;
461 sym->ctype.base_type = base;
462 return examine_node_type(sym);
464 break;
466 case SYM_PREPROCESSOR:
467 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
468 return NULL;
469 case SYM_UNINITIALIZED:
470 // sparse_error(sym->pos, "ctype on uninitialized symbol %p", sym);
471 return NULL;
472 case SYM_RESTRICT:
473 examine_base_type(sym);
474 return sym;
475 case SYM_FOULED:
476 examine_base_type(sym);
477 return sym;
478 default:
479 sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
480 break;
482 return sym;
485 const char* get_type_name(enum type type)
487 const char *type_lookup[] = {
488 [SYM_UNINITIALIZED] = "uninitialized",
489 [SYM_PREPROCESSOR] = "preprocessor",
490 [SYM_BASETYPE] = "basetype",
491 [SYM_NODE] = "node",
492 [SYM_PTR] = "pointer",
493 [SYM_FN] = "function",
494 [SYM_ARRAY] = "array",
495 [SYM_STRUCT] = "struct",
496 [SYM_UNION] = "union",
497 [SYM_ENUM] = "enum",
498 [SYM_TYPEDEF] = "typedef",
499 [SYM_TYPEOF] = "typeof",
500 [SYM_MEMBER] = "member",
501 [SYM_BITFIELD] = "bitfield",
502 [SYM_LABEL] = "label",
503 [SYM_RESTRICT] = "restrict",
504 [SYM_FOULED] = "fouled",
505 [SYM_KEYWORD] = "keyword",
506 [SYM_BAD] = "bad"};
508 if (type <= SYM_BAD)
509 return type_lookup[type];
510 else
511 return NULL;
514 struct symbol *examine_pointer_target(struct symbol *sym)
516 return examine_base_type(sym);
519 static struct symbol_list *restr, *fouled;
521 void create_fouled(struct symbol *type)
523 if (type->bit_size < bits_in_int) {
524 struct symbol *new = alloc_symbol(type->pos, type->type);
525 *new = *type;
526 new->bit_size = bits_in_int;
527 new->type = SYM_FOULED;
528 new->ctype.base_type = type;
529 add_symbol(&restr, type);
530 add_symbol(&fouled, new);
534 struct symbol *befoul(struct symbol *type)
536 struct symbol *t1, *t2;
537 while (type->type == SYM_NODE)
538 type = type->ctype.base_type;
539 PREPARE_PTR_LIST(restr, t1);
540 PREPARE_PTR_LIST(fouled, t2);
541 for (;;) {
542 if (t1 == type)
543 return t2;
544 if (!t1)
545 break;
546 NEXT_PTR_LIST(t1);
547 NEXT_PTR_LIST(t2);
549 FINISH_PTR_LIST(t2);
550 FINISH_PTR_LIST(t1);
551 return NULL;
554 void check_declaration(struct symbol *sym)
556 int warned = 0;
557 struct symbol *next = sym;
559 while ((next = next->next_id) != NULL) {
560 if (next->namespace != sym->namespace)
561 continue;
562 if (sym->scope == next->scope) {
563 sym->same_symbol = next;
564 return;
566 if (sym->ctype.modifiers & next->ctype.modifiers & MOD_EXTERN) {
567 if ((sym->ctype.modifiers ^ next->ctype.modifiers) & MOD_INLINE)
568 continue;
569 sym->same_symbol = next;
570 return;
573 if (!Wshadow || warned)
574 continue;
575 if (get_sym_type(next) == SYM_FN)
576 continue;
577 warned = 1;
578 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
579 info(next->pos, "originally declared here");
583 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
585 struct scope *scope;
586 if (sym->bound) {
587 sparse_error(sym->pos, "internal error: symbol type already bound");
588 return;
590 if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
591 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
592 return;
594 sym->namespace = ns;
595 sym->next_id = ident->symbols;
596 ident->symbols = sym;
597 if (sym->ident && sym->ident != ident)
598 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
599 sym->ident = ident;
600 sym->bound = 1;
602 scope = block_scope;
603 if (ns == NS_SYMBOL && toplevel(scope)) {
604 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
606 scope = global_scope;
607 if (sym->ctype.modifiers & MOD_STATIC ||
608 is_extern_inline(sym)) {
609 scope = file_scope;
610 mod = MOD_TOPLEVEL;
612 sym->ctype.modifiers |= mod;
614 if (ns == NS_MACRO)
615 scope = file_scope;
616 if (ns == NS_LABEL)
617 scope = function_scope;
618 bind_scope(sym, scope);
621 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
623 struct token *token = built_in_token(stream, name);
624 struct symbol *sym = alloc_symbol(token->pos, type);
626 bind_symbol(sym, token->ident, namespace);
627 return sym;
630 static int evaluate_to_integer(struct expression *expr)
632 expr->ctype = &int_ctype;
633 return 1;
636 static int evaluate_expect(struct expression *expr)
638 /* Should we evaluate it to return the type of the first argument? */
639 expr->ctype = &int_ctype;
640 return 1;
643 static int arguments_choose(struct expression *expr)
645 struct expression_list *arglist = expr->args;
646 struct expression *arg;
647 int i = 0;
649 FOR_EACH_PTR (arglist, arg) {
650 if (!evaluate_expression(arg))
651 return 0;
652 i++;
653 } END_FOR_EACH_PTR(arg);
654 if (i < 3) {
655 sparse_error(expr->pos,
656 "not enough arguments for __builtin_choose_expr");
657 return 0;
658 } if (i > 3) {
659 sparse_error(expr->pos,
660 "too many arguments for __builtin_choose_expr");
661 return 0;
663 return 1;
666 static int evaluate_choose(struct expression *expr)
668 struct expression_list *list = expr->args;
669 struct expression *arg, *args[3];
670 int n = 0;
672 /* there will be exactly 3; we'd already verified that */
673 FOR_EACH_PTR(list, arg) {
674 args[n++] = arg;
675 } END_FOR_EACH_PTR(arg);
677 *expr = get_expression_value(args[0]) ? *args[1] : *args[2];
679 return 1;
682 static int expand_expect(struct expression *expr, int cost)
684 struct expression *arg = first_ptr_list((struct ptr_list *) expr->args);
686 if (arg)
687 *expr = *arg;
688 return 0;
692 * __builtin_warning() has type "int" and always returns 1,
693 * so that you can use it in conditionals or whatever
695 static int expand_warning(struct expression *expr, int cost)
697 struct expression *arg;
698 struct expression_list *arglist = expr->args;
700 FOR_EACH_PTR (arglist, arg) {
702 * Constant strings get printed out as a warning. By the
703 * time we get here, the EXPR_STRING has been fully
704 * evaluated, so by now it's an anonymous symbol with a
705 * string initializer.
707 * Just for the heck of it, allow any constant string
708 * symbol.
710 if (arg->type == EXPR_SYMBOL) {
711 struct symbol *sym = arg->symbol;
712 if (sym->initializer && sym->initializer->type == EXPR_STRING) {
713 struct string *string = sym->initializer->string;
714 warning(expr->pos, "%*s", string->length-1, string->data);
716 continue;
720 * Any other argument is a conditional. If it's
721 * non-constant, or it is false, we exit and do
722 * not print any warning.
724 if (arg->type != EXPR_VALUE)
725 goto out;
726 if (!arg->value)
727 goto out;
728 } END_FOR_EACH_PTR(arg);
729 out:
730 expr->type = EXPR_VALUE;
731 expr->value = 1;
732 expr->taint = 0;
733 return 0;
736 static struct symbol_op constant_p_op = {
737 .evaluate = evaluate_to_integer,
738 .expand = expand_constant_p
741 static struct symbol_op safe_p_op = {
742 .evaluate = evaluate_to_integer,
743 .expand = expand_safe_p
746 static struct symbol_op warning_op = {
747 .evaluate = evaluate_to_integer,
748 .expand = expand_warning
751 static struct symbol_op expect_op = {
752 .evaluate = evaluate_expect,
753 .expand = expand_expect
756 static struct symbol_op choose_op = {
757 .evaluate = evaluate_choose,
758 .args = arguments_choose,
762 * Builtin functions
764 static struct symbol builtin_fn_type = { .type = SYM_FN /* , .variadic =1 */ };
765 static struct sym_init {
766 const char *name;
767 struct symbol *base_type;
768 unsigned int modifiers;
769 struct symbol_op *op;
770 } eval_init_table[] = {
771 { "__builtin_constant_p", &builtin_fn_type, MOD_TOPLEVEL, &constant_p_op },
772 { "__builtin_safe_p", &builtin_fn_type, MOD_TOPLEVEL, &safe_p_op },
773 { "__builtin_warning", &builtin_fn_type, MOD_TOPLEVEL, &warning_op },
774 { "__builtin_expect", &builtin_fn_type, MOD_TOPLEVEL, &expect_op },
775 { "__builtin_choose_expr", &builtin_fn_type, MOD_TOPLEVEL, &choose_op },
776 { NULL, NULL, 0 }
780 * Default empty attribute
782 struct attribute null_attr = {};
785 * Abstract types
787 struct symbol int_type,
788 fp_type;
791 * C types (i.e. actual instances that the abstract types
792 * can map onto)
794 struct symbol bool_ctype, void_ctype, type_ctype,
795 char_ctype, schar_ctype, uchar_ctype,
796 short_ctype, sshort_ctype, ushort_ctype,
797 int_ctype, sint_ctype, uint_ctype,
798 long_ctype, slong_ctype, ulong_ctype,
799 llong_ctype, sllong_ctype, ullong_ctype,
800 lllong_ctype, slllong_ctype, ulllong_ctype,
801 float_ctype, double_ctype, ldouble_ctype,
802 string_ctype, ptr_ctype, lazy_ptr_ctype,
803 incomplete_ctype, label_ctype, bad_ctype,
804 null_ctype;
806 struct symbol zero_int;
808 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
809 #define __IDENT(n,str,res) \
810 struct ident n = __INIT_IDENT(str,res)
812 #include "ident-list.h"
814 void init_symbols(void)
816 int stream = init_stream("builtin", -1, includepath);
817 struct sym_init *ptr;
819 #define __IDENT(n,str,res) \
820 hash_ident(&n)
821 #include "ident-list.h"
823 init_parser(stream);
825 builtin_fn_type.variadic = 1;
826 builtin_fn_type.ctype.attribute = &null_attr;
827 for (ptr = eval_init_table; ptr->name; ptr++) {
828 struct symbol *sym;
829 sym = create_symbol(stream, ptr->name, SYM_NODE, NS_SYMBOL);
830 sym->ctype.base_type = ptr->base_type;
831 sym->ctype.modifiers = ptr->modifiers;
832 sym->ctype.attribute = &null_attr;
833 sym->op = ptr->op;
837 #ifdef __CHAR_UNSIGNED__
838 #define CHAR_SIGNEDNESS MOD_UNSIGNED
839 #else
840 #define CHAR_SIGNEDNESS MOD_SIGNED
841 #endif
843 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
844 #define MOD_LL (MOD_LONG | MOD_LONGLONG)
845 #define MOD_LLL MOD_LONGLONGLONG
846 static const struct ctype_declare {
847 struct symbol *ptr;
848 enum type type;
849 unsigned long modifiers;
850 int *bit_size;
851 int *maxalign;
852 struct symbol *base_type;
853 } ctype_declaration[] = {
854 { &bool_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_bool, &max_int_alignment, &int_type },
855 { &void_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
856 { &type_ctype, SYM_BASETYPE, MOD_TYPE, NULL, NULL, NULL },
857 { &incomplete_ctype,SYM_BASETYPE, 0, NULL, NULL, NULL },
858 { &bad_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
860 { &char_ctype, SYM_BASETYPE, CHAR_SIGNEDNESS | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
861 { &schar_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
862 { &uchar_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
863 { &short_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
864 { &sshort_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
865 { &ushort_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
866 { &int_ctype, SYM_BASETYPE, MOD_SIGNED, &bits_in_int, &max_int_alignment, &int_type },
867 { &sint_ctype, SYM_BASETYPE, MOD_ESIGNED, &bits_in_int, &max_int_alignment, &int_type },
868 { &uint_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_int, &max_int_alignment, &int_type },
869 { &long_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
870 { &slong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
871 { &ulong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
872 { &llong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
873 { &sllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
874 { &ullong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
875 { &lllong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
876 { &slllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
877 { &ulllong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
879 { &float_ctype, SYM_BASETYPE, 0, &bits_in_float, &max_fp_alignment, &fp_type },
880 { &double_ctype, SYM_BASETYPE, MOD_LONG, &bits_in_double, &max_fp_alignment, &fp_type },
881 { &ldouble_ctype, SYM_BASETYPE, MOD_LONG | MOD_LONGLONG, &bits_in_longdouble, &max_fp_alignment, &fp_type },
883 { &string_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &char_ctype },
884 { &ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
885 { &null_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
886 { &label_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
887 { &lazy_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
888 { NULL, }
890 #undef MOD_LLL
891 #undef MOD_LL
892 #undef MOD_ESIGNED
894 void init_ctype(void)
896 const struct ctype_declare *ctype;
898 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
899 struct symbol *sym = ctype->ptr;
900 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
901 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
902 unsigned long alignment = bits_to_bytes(bit_size + bits_in_char - 1);
904 if (alignment > maxalign)
905 alignment = maxalign;
906 sym->type = ctype->type;
907 sym->bit_size = bit_size;
908 sym->ctype.alignment = alignment;
909 sym->ctype.base_type = ctype->base_type;
910 sym->ctype.modifiers = ctype->modifiers;
911 sym->ctype.attribute = &null_attr;