user_data: kstrtoul() and friends should taint data as untrusted
[smatch.git] / symbol.c
blobabe227cf33c99e3e056f805dfd412ef94f9e8ac7
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 expression *get_symbol_initializer(struct symbol *sym)
358 do {
359 if (sym->initializer)
360 return sym->initializer;
361 } while ((sym = sym->same_symbol) != NULL);
362 return NULL;
365 static struct symbol * examine_node_type(struct symbol *sym)
367 struct symbol *base_type = examine_base_type(sym);
368 int bit_size;
369 unsigned long alignment;
371 /* SYM_NODE - figure out what the type of the node was.. */
372 bit_size = 0;
373 alignment = 0;
374 if (!base_type)
375 return sym;
377 bit_size = base_type->bit_size;
378 alignment = base_type->ctype.alignment;
380 /* Pick up signedness information into the node */
381 sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
383 if (!sym->ctype.alignment)
384 sym->ctype.alignment = alignment;
386 /* Unsized array? The size might come from the initializer.. */
387 if (bit_size < 0 && base_type->type == SYM_ARRAY) {
388 struct expression *initializer = get_symbol_initializer(sym);
389 if (initializer) {
390 struct symbol *node_type = base_type->ctype.base_type;
391 int count = count_array_initializer(node_type, initializer);
393 if (node_type && node_type->bit_size >= 0)
394 bit_size = node_type->bit_size * count;
398 sym->bit_size = bit_size;
399 return sym;
402 static struct symbol *examine_enum_type(struct symbol *sym)
404 struct symbol *base_type = examine_base_type(sym);
406 sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
407 sym->bit_size = bits_in_enum;
408 if (base_type->bit_size > sym->bit_size)
409 sym->bit_size = base_type->bit_size;
410 sym->ctype.alignment = enum_alignment;
411 if (base_type->ctype.alignment > sym->ctype.alignment)
412 sym->ctype.alignment = base_type->ctype.alignment;
413 return sym;
416 static struct symbol *examine_pointer_type(struct symbol *sym)
419 * We need to set the pointer size first, and
420 * examine the thing we point to only afterwards.
421 * That's because this pointer type may end up
422 * being needed for the base type size evaluation.
424 if (!sym->bit_size)
425 sym->bit_size = bits_in_pointer;
426 if (!sym->ctype.alignment)
427 sym->ctype.alignment = pointer_alignment;
428 return sym;
432 * Fill in type size and alignment information for
433 * regular SYM_TYPE things.
435 struct symbol *examine_symbol_type(struct symbol * sym)
437 if (!sym)
438 return sym;
440 /* Already done? */
441 if (sym->examined)
442 return sym;
443 sym->examined = 1;
445 switch (sym->type) {
446 case SYM_FN:
447 case SYM_NODE:
448 return examine_node_type(sym);
449 case SYM_ARRAY:
450 return examine_array_type(sym);
451 case SYM_STRUCT:
452 return examine_struct_union_type(sym, 1);
453 case SYM_UNION:
454 return examine_struct_union_type(sym, 0);
455 case SYM_PTR:
456 return examine_pointer_type(sym);
457 case SYM_ENUM:
458 return examine_enum_type(sym);
459 case SYM_BITFIELD:
460 return examine_bitfield_type(sym);
461 case SYM_BASETYPE:
462 /* Size and alignment had better already be set up */
463 return sym;
464 case SYM_TYPEOF: {
465 struct symbol *base = evaluate_expression(sym->initializer);
466 if (base) {
467 if (is_bitfield_type(base))
468 warning(base->pos, "typeof applied to bitfield type");
469 if (base->type == SYM_NODE)
470 base = base->ctype.base_type;
471 sym->type = SYM_NODE;
472 sym->ctype.modifiers = 0;
473 sym->ctype.base_type = base;
474 return examine_node_type(sym);
476 break;
478 case SYM_PREPROCESSOR:
479 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
480 return NULL;
481 case SYM_UNINITIALIZED:
482 // sparse_error(sym->pos, "ctype on uninitialized symbol %p", sym);
483 return NULL;
484 case SYM_RESTRICT:
485 examine_base_type(sym);
486 return sym;
487 case SYM_FOULED:
488 examine_base_type(sym);
489 return sym;
490 default:
491 sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
492 break;
494 return sym;
497 const char* get_type_name(enum type type)
499 const char *type_lookup[] = {
500 [SYM_UNINITIALIZED] = "uninitialized",
501 [SYM_PREPROCESSOR] = "preprocessor",
502 [SYM_BASETYPE] = "basetype",
503 [SYM_NODE] = "node",
504 [SYM_PTR] = "pointer",
505 [SYM_FN] = "function",
506 [SYM_ARRAY] = "array",
507 [SYM_STRUCT] = "struct",
508 [SYM_UNION] = "union",
509 [SYM_ENUM] = "enum",
510 [SYM_TYPEDEF] = "typedef",
511 [SYM_TYPEOF] = "typeof",
512 [SYM_MEMBER] = "member",
513 [SYM_BITFIELD] = "bitfield",
514 [SYM_LABEL] = "label",
515 [SYM_RESTRICT] = "restrict",
516 [SYM_FOULED] = "fouled",
517 [SYM_KEYWORD] = "keyword",
518 [SYM_BAD] = "bad"};
520 if (type <= SYM_BAD)
521 return type_lookup[type];
522 else
523 return NULL;
526 struct symbol *examine_pointer_target(struct symbol *sym)
528 return examine_base_type(sym);
531 static struct symbol_list *restr, *fouled;
533 void create_fouled(struct symbol *type)
535 if (type->bit_size < bits_in_int) {
536 struct symbol *new = alloc_symbol(type->pos, type->type);
537 *new = *type;
538 new->bit_size = bits_in_int;
539 new->type = SYM_FOULED;
540 new->ctype.base_type = type;
541 add_symbol(&restr, type);
542 add_symbol(&fouled, new);
546 struct symbol *befoul(struct symbol *type)
548 struct symbol *t1, *t2;
549 while (type->type == SYM_NODE)
550 type = type->ctype.base_type;
551 PREPARE_PTR_LIST(restr, t1);
552 PREPARE_PTR_LIST(fouled, t2);
553 for (;;) {
554 if (t1 == type)
555 return t2;
556 if (!t1)
557 break;
558 NEXT_PTR_LIST(t1);
559 NEXT_PTR_LIST(t2);
561 FINISH_PTR_LIST(t2);
562 FINISH_PTR_LIST(t1);
563 return NULL;
566 void check_declaration(struct symbol *sym)
568 int warned = 0;
569 struct symbol *next = sym;
571 while ((next = next->next_id) != NULL) {
572 if (next->namespace != sym->namespace)
573 continue;
574 if (sym->scope == next->scope) {
575 sym->same_symbol = next;
576 return;
578 if (sym->ctype.modifiers & next->ctype.modifiers & MOD_EXTERN) {
579 if ((sym->ctype.modifiers ^ next->ctype.modifiers) & MOD_INLINE)
580 continue;
581 sym->same_symbol = next;
582 return;
585 if (!Wshadow || warned)
586 continue;
587 if (get_sym_type(next) == SYM_FN)
588 continue;
589 warned = 1;
590 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
591 info(next->pos, "originally declared here");
595 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
597 struct scope *scope;
598 if (sym->bound) {
599 sparse_error(sym->pos, "internal error: symbol type already bound");
600 return;
602 if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
603 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
604 return;
606 sym->namespace = ns;
607 sym->next_id = ident->symbols;
608 ident->symbols = sym;
609 if (sym->ident && sym->ident != ident)
610 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
611 sym->ident = ident;
612 sym->bound = 1;
614 scope = block_scope;
615 if (ns == NS_SYMBOL && toplevel(scope)) {
616 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
618 scope = global_scope;
619 if (sym->ctype.modifiers & MOD_STATIC ||
620 is_extern_inline(sym)) {
621 scope = file_scope;
622 mod = MOD_TOPLEVEL;
624 sym->ctype.modifiers |= mod;
626 if (ns == NS_MACRO)
627 scope = file_scope;
628 if (ns == NS_LABEL)
629 scope = function_scope;
630 bind_scope(sym, scope);
633 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
635 struct token *token = built_in_token(stream, name);
636 struct symbol *sym = alloc_symbol(token->pos, type);
638 bind_symbol(sym, token->ident, namespace);
639 return sym;
642 static int evaluate_to_integer(struct expression *expr)
644 expr->ctype = &int_ctype;
645 return 1;
648 static int evaluate_expect(struct expression *expr)
650 /* Should we evaluate it to return the type of the first argument? */
651 expr->ctype = &int_ctype;
652 return 1;
655 static int arguments_choose(struct expression *expr)
657 struct expression_list *arglist = expr->args;
658 struct expression *arg;
659 int i = 0;
661 FOR_EACH_PTR (arglist, arg) {
662 if (!evaluate_expression(arg))
663 return 0;
664 i++;
665 } END_FOR_EACH_PTR(arg);
666 if (i < 3) {
667 sparse_error(expr->pos,
668 "not enough arguments for __builtin_choose_expr");
669 return 0;
670 } if (i > 3) {
671 sparse_error(expr->pos,
672 "too many arguments for __builtin_choose_expr");
673 return 0;
675 return 1;
678 static int evaluate_choose(struct expression *expr)
680 struct expression_list *list = expr->args;
681 struct expression *arg, *args[3];
682 int n = 0;
684 /* there will be exactly 3; we'd already verified that */
685 FOR_EACH_PTR(list, arg) {
686 args[n++] = arg;
687 } END_FOR_EACH_PTR(arg);
689 *expr = get_expression_value(args[0]) ? *args[1] : *args[2];
691 return 1;
694 static int expand_expect(struct expression *expr, int cost)
696 struct expression *arg = first_ptr_list((struct ptr_list *) expr->args);
698 if (arg)
699 *expr = *arg;
700 return 0;
704 * __builtin_warning() has type "int" and always returns 1,
705 * so that you can use it in conditionals or whatever
707 static int expand_warning(struct expression *expr, int cost)
709 struct expression *arg;
710 struct expression_list *arglist = expr->args;
712 FOR_EACH_PTR (arglist, arg) {
714 * Constant strings get printed out as a warning. By the
715 * time we get here, the EXPR_STRING has been fully
716 * evaluated, so by now it's an anonymous symbol with a
717 * string initializer.
719 * Just for the heck of it, allow any constant string
720 * symbol.
722 if (arg->type == EXPR_SYMBOL) {
723 struct symbol *sym = arg->symbol;
724 if (sym->initializer && sym->initializer->type == EXPR_STRING) {
725 struct string *string = sym->initializer->string;
726 warning(expr->pos, "%*s", string->length-1, string->data);
728 continue;
732 * Any other argument is a conditional. If it's
733 * non-constant, or it is false, we exit and do
734 * not print any warning.
736 if (arg->type != EXPR_VALUE)
737 goto out;
738 if (!arg->value)
739 goto out;
740 } END_FOR_EACH_PTR(arg);
741 out:
742 expr->type = EXPR_VALUE;
743 expr->value = 1;
744 expr->taint = 0;
745 return 0;
748 static struct symbol_op constant_p_op = {
749 .evaluate = evaluate_to_integer,
750 .expand = expand_constant_p
753 static struct symbol_op safe_p_op = {
754 .evaluate = evaluate_to_integer,
755 .expand = expand_safe_p
758 static struct symbol_op warning_op = {
759 .evaluate = evaluate_to_integer,
760 .expand = expand_warning
763 static struct symbol_op expect_op = {
764 .evaluate = evaluate_expect,
765 .expand = expand_expect
768 static struct symbol_op choose_op = {
769 .evaluate = evaluate_choose,
770 .args = arguments_choose,
774 * Builtin functions
776 static struct symbol builtin_fn_type = { .type = SYM_FN /* , .variadic =1 */ };
777 static struct sym_init {
778 const char *name;
779 struct symbol *base_type;
780 unsigned int modifiers;
781 struct symbol_op *op;
782 } eval_init_table[] = {
783 { "__builtin_constant_p", &builtin_fn_type, MOD_TOPLEVEL, &constant_p_op },
784 { "__builtin_safe_p", &builtin_fn_type, MOD_TOPLEVEL, &safe_p_op },
785 { "__builtin_warning", &builtin_fn_type, MOD_TOPLEVEL, &warning_op },
786 { "__builtin_expect", &builtin_fn_type, MOD_TOPLEVEL, &expect_op },
787 { "__builtin_choose_expr", &builtin_fn_type, MOD_TOPLEVEL, &choose_op },
788 { NULL, NULL, 0 }
792 * Default empty attribute
794 struct attribute null_attr = {};
797 * Abstract types
799 struct symbol int_type,
800 fp_type;
803 * C types (i.e. actual instances that the abstract types
804 * can map onto)
806 struct symbol bool_ctype, void_ctype, type_ctype,
807 char_ctype, schar_ctype, uchar_ctype,
808 short_ctype, sshort_ctype, ushort_ctype,
809 int_ctype, sint_ctype, uint_ctype,
810 long_ctype, slong_ctype, ulong_ctype,
811 llong_ctype, sllong_ctype, ullong_ctype,
812 lllong_ctype, slllong_ctype, ulllong_ctype,
813 float_ctype, double_ctype, ldouble_ctype,
814 string_ctype, ptr_ctype, lazy_ptr_ctype,
815 incomplete_ctype, label_ctype, bad_ctype,
816 null_ctype;
818 struct symbol zero_int;
820 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
821 #define __IDENT(n,str,res) \
822 struct ident n = __INIT_IDENT(str,res)
824 #include "ident-list.h"
826 void init_symbols(void)
828 int stream = init_stream("builtin", -1, includepath);
829 struct sym_init *ptr;
831 #define __IDENT(n,str,res) \
832 hash_ident(&n)
833 #include "ident-list.h"
835 init_parser(stream);
837 builtin_fn_type.variadic = 1;
838 builtin_fn_type.ctype.attribute = &null_attr;
839 for (ptr = eval_init_table; ptr->name; ptr++) {
840 struct symbol *sym;
841 sym = create_symbol(stream, ptr->name, SYM_NODE, NS_SYMBOL);
842 sym->ctype.base_type = ptr->base_type;
843 sym->ctype.modifiers = ptr->modifiers;
844 sym->ctype.attribute = &null_attr;
845 sym->op = ptr->op;
849 #ifdef __CHAR_UNSIGNED__
850 #define CHAR_SIGNEDNESS MOD_UNSIGNED
851 #else
852 #define CHAR_SIGNEDNESS MOD_SIGNED
853 #endif
855 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
856 #define MOD_LL (MOD_LONG | MOD_LONGLONG)
857 #define MOD_LLL MOD_LONGLONGLONG
858 static const struct ctype_declare {
859 struct symbol *ptr;
860 enum type type;
861 unsigned long modifiers;
862 int *bit_size;
863 int *maxalign;
864 struct symbol *base_type;
865 } ctype_declaration[] = {
866 { &bool_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_bool, &max_int_alignment, &int_type },
867 { &void_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
868 { &type_ctype, SYM_BASETYPE, MOD_TYPE, NULL, NULL, NULL },
869 { &incomplete_ctype,SYM_BASETYPE, 0, NULL, NULL, NULL },
870 { &bad_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
872 { &char_ctype, SYM_BASETYPE, CHAR_SIGNEDNESS | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
873 { &schar_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
874 { &uchar_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
875 { &short_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
876 { &sshort_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
877 { &ushort_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
878 { &int_ctype, SYM_BASETYPE, MOD_SIGNED, &bits_in_int, &max_int_alignment, &int_type },
879 { &sint_ctype, SYM_BASETYPE, MOD_ESIGNED, &bits_in_int, &max_int_alignment, &int_type },
880 { &uint_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_int, &max_int_alignment, &int_type },
881 { &long_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
882 { &slong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
883 { &ulong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
884 { &llong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
885 { &sllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
886 { &ullong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
887 { &lllong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
888 { &slllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
889 { &ulllong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
891 { &float_ctype, SYM_BASETYPE, 0, &bits_in_float, &max_fp_alignment, &fp_type },
892 { &double_ctype, SYM_BASETYPE, MOD_LONG, &bits_in_double, &max_fp_alignment, &fp_type },
893 { &ldouble_ctype, SYM_BASETYPE, MOD_LONG | MOD_LONGLONG, &bits_in_longdouble, &max_fp_alignment, &fp_type },
895 { &string_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &char_ctype },
896 { &ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
897 { &null_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
898 { &label_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
899 { &lazy_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
900 { NULL, }
902 #undef MOD_LLL
903 #undef MOD_LL
904 #undef MOD_ESIGNED
906 void init_ctype(void)
908 const struct ctype_declare *ctype;
910 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
911 struct symbol *sym = ctype->ptr;
912 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
913 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
914 unsigned long alignment = bits_to_bytes(bit_size + bits_in_char - 1);
916 if (alignment > maxalign)
917 alignment = maxalign;
918 sym->type = ctype->type;
919 sym->bit_size = bit_size;
920 sym->ctype.alignment = alignment;
921 sym->ctype.base_type = ctype->base_type;
922 sym->ctype.modifiers = ctype->modifiers;
923 sym->ctype.attribute = &null_attr;