double_checking: avoid more false positives
[smatch.git] / symbol.c
blob5c2e8e171b53c0fca0765971b4bcadfca1e61440
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 = array_element_offset(base_type->bit_size,
239 get_expression_value_silent(array_size));
240 if (array_size->type != EXPR_VALUE) {
241 if (Wvla)
242 warning(array_size->pos, "Variable length array is used.");
243 bit_size = -1;
246 alignment = base_type->ctype.alignment;
247 if (!sym->ctype.alignment)
248 sym->ctype.alignment = alignment;
249 sym->bit_size = bit_size;
250 return sym;
253 static struct symbol *examine_bitfield_type(struct symbol *sym)
255 struct symbol *base_type = examine_base_type(sym);
256 unsigned long bit_size, alignment, modifiers;
258 if (!base_type)
259 return sym;
260 bit_size = base_type->bit_size;
261 if (sym->bit_size > bit_size)
262 warning(sym->pos, "impossible field-width, %d, for this type", sym->bit_size);
264 alignment = base_type->ctype.alignment;
265 if (!sym->ctype.alignment)
266 sym->ctype.alignment = alignment;
267 modifiers = base_type->ctype.modifiers;
269 /* Bitfields are unsigned, unless the base type was explicitly signed */
270 if (!(modifiers & MOD_EXPLICITLY_SIGNED))
271 modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
272 sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
273 return sym;
277 * "typeof" will have to merge the types together
279 void merge_type(struct symbol *sym, struct symbol *base_type)
281 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
282 merge_attr(&sym->ctype, &base_type->ctype);
283 sym->ctype.base_type = base_type->ctype.base_type;
284 if (sym->ctype.base_type->type == SYM_NODE)
285 merge_type(sym, sym->ctype.base_type);
288 static int count_array_initializer(struct symbol *t, struct expression *expr)
290 int nr = 0;
291 int is_char = 0;
294 * Arrays of character types are special; they can be initialized by
295 * string literal _or_ by string literal in braces. The latter means
296 * that with T x[] = {<string literal>} number of elements in x depends
297 * on T - if it's a character type, we get the length of string literal
298 * (including NUL), otherwise we have one element here.
300 if (t->ctype.base_type == &int_type && t->ctype.modifiers & MOD_CHAR)
301 is_char = 1;
303 switch (expr->type) {
304 case EXPR_INITIALIZER: {
305 struct expression *entry;
306 int count = 0;
307 int str_len = 0;
308 FOR_EACH_PTR(expr->expr_list, entry) {
309 count++;
310 switch (entry->type) {
311 case EXPR_INDEX:
312 if (entry->idx_to >= nr)
313 nr = entry->idx_to+1;
314 break;
315 case EXPR_PREOP: {
316 struct expression *e = entry;
317 if (is_char) {
318 while (e && e->type == EXPR_PREOP && e->op == '(')
319 e = e->unop;
320 if (e && e->type == EXPR_STRING) {
321 entry = e;
322 case EXPR_STRING:
323 if (is_char)
324 str_len = entry->string->length;
330 default:
331 nr++;
333 } END_FOR_EACH_PTR(entry);
334 if (count == 1 && str_len)
335 nr = str_len;
336 break;
338 case EXPR_PREOP:
339 if (is_char) {
340 struct expression *e = expr;
341 while (e && e->type == EXPR_PREOP && e->op == '(')
342 e = e->unop;
343 if (e && e->type == EXPR_STRING) {
344 expr = e;
345 case EXPR_STRING:
346 if (is_char)
347 nr = expr->string->length;
350 break;
351 default:
352 break;
354 return nr;
357 static struct expression *get_symbol_initializer(struct symbol *sym)
359 do {
360 if (sym->initializer)
361 return sym->initializer;
362 } while ((sym = sym->same_symbol) != NULL);
363 return NULL;
366 static struct symbol * examine_node_type(struct symbol *sym)
368 struct symbol *base_type = examine_base_type(sym);
369 int bit_size;
370 unsigned long alignment;
372 /* SYM_NODE - figure out what the type of the node was.. */
373 bit_size = 0;
374 alignment = 0;
375 if (!base_type)
376 return sym;
378 bit_size = base_type->bit_size;
379 alignment = base_type->ctype.alignment;
381 /* Pick up signedness information into the node */
382 sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
384 if (!sym->ctype.alignment)
385 sym->ctype.alignment = alignment;
387 /* Unsized array? The size might come from the initializer.. */
388 if (bit_size < 0 && base_type->type == SYM_ARRAY) {
389 struct expression *initializer = get_symbol_initializer(sym);
390 if (initializer) {
391 struct symbol *node_type = base_type->ctype.base_type;
392 int count = count_array_initializer(node_type, initializer);
394 if (node_type && node_type->bit_size >= 0)
395 bit_size = node_type->bit_size * count;
399 sym->bit_size = bit_size;
400 return sym;
403 static struct symbol *examine_enum_type(struct symbol *sym)
405 struct symbol *base_type = examine_base_type(sym);
407 sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
408 sym->bit_size = bits_in_enum;
409 if (base_type->bit_size > sym->bit_size)
410 sym->bit_size = base_type->bit_size;
411 sym->ctype.alignment = enum_alignment;
412 if (base_type->ctype.alignment > sym->ctype.alignment)
413 sym->ctype.alignment = base_type->ctype.alignment;
414 return sym;
417 static struct symbol *examine_pointer_type(struct symbol *sym)
420 * We need to set the pointer size first, and
421 * examine the thing we point to only afterwards.
422 * That's because this pointer type may end up
423 * being needed for the base type size evaluation.
425 if (!sym->bit_size)
426 sym->bit_size = bits_in_pointer;
427 if (!sym->ctype.alignment)
428 sym->ctype.alignment = pointer_alignment;
429 return sym;
433 * Fill in type size and alignment information for
434 * regular SYM_TYPE things.
436 struct symbol *examine_symbol_type(struct symbol * sym)
438 if (!sym)
439 return sym;
441 /* Already done? */
442 if (sym->examined)
443 return sym;
444 sym->examined = 1;
446 switch (sym->type) {
447 case SYM_FN:
448 case SYM_NODE:
449 return examine_node_type(sym);
450 case SYM_ARRAY:
451 return examine_array_type(sym);
452 case SYM_STRUCT:
453 return examine_struct_union_type(sym, 1);
454 case SYM_UNION:
455 return examine_struct_union_type(sym, 0);
456 case SYM_PTR:
457 return examine_pointer_type(sym);
458 case SYM_ENUM:
459 return examine_enum_type(sym);
460 case SYM_BITFIELD:
461 return examine_bitfield_type(sym);
462 case SYM_BASETYPE:
463 /* Size and alignment had better already be set up */
464 return sym;
465 case SYM_TYPEOF: {
466 struct symbol *base = evaluate_expression(sym->initializer);
467 if (base) {
468 if (is_bitfield_type(base))
469 warning(base->pos, "typeof applied to bitfield type");
470 if (base->type == SYM_NODE)
471 base = base->ctype.base_type;
472 sym->type = SYM_NODE;
473 sym->ctype.modifiers = 0;
474 sym->ctype.base_type = base;
475 return examine_node_type(sym);
477 break;
479 case SYM_PREPROCESSOR:
480 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
481 return NULL;
482 case SYM_UNINITIALIZED:
483 // sparse_error(sym->pos, "ctype on uninitialized symbol %p", sym);
484 return NULL;
485 case SYM_RESTRICT:
486 examine_base_type(sym);
487 return sym;
488 case SYM_FOULED:
489 examine_base_type(sym);
490 return sym;
491 default:
492 sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
493 break;
495 return sym;
498 const char* get_type_name(enum type type)
500 const char *type_lookup[] = {
501 [SYM_UNINITIALIZED] = "uninitialized",
502 [SYM_PREPROCESSOR] = "preprocessor",
503 [SYM_BASETYPE] = "basetype",
504 [SYM_NODE] = "node",
505 [SYM_PTR] = "pointer",
506 [SYM_FN] = "function",
507 [SYM_ARRAY] = "array",
508 [SYM_STRUCT] = "struct",
509 [SYM_UNION] = "union",
510 [SYM_ENUM] = "enum",
511 [SYM_TYPEDEF] = "typedef",
512 [SYM_TYPEOF] = "typeof",
513 [SYM_MEMBER] = "member",
514 [SYM_BITFIELD] = "bitfield",
515 [SYM_LABEL] = "label",
516 [SYM_RESTRICT] = "restrict",
517 [SYM_FOULED] = "fouled",
518 [SYM_KEYWORD] = "keyword",
519 [SYM_BAD] = "bad"};
521 if (type <= SYM_BAD)
522 return type_lookup[type];
523 else
524 return NULL;
527 struct symbol *examine_pointer_target(struct symbol *sym)
529 return examine_base_type(sym);
532 static struct symbol_list *restr, *fouled;
534 void create_fouled(struct symbol *type)
536 if (type->bit_size < bits_in_int) {
537 struct symbol *new = alloc_symbol(type->pos, type->type);
538 *new = *type;
539 new->bit_size = bits_in_int;
540 new->type = SYM_FOULED;
541 new->ctype.base_type = type;
542 add_symbol(&restr, type);
543 add_symbol(&fouled, new);
547 struct symbol *befoul(struct symbol *type)
549 struct symbol *t1, *t2;
550 while (type->type == SYM_NODE)
551 type = type->ctype.base_type;
552 PREPARE_PTR_LIST(restr, t1);
553 PREPARE_PTR_LIST(fouled, t2);
554 for (;;) {
555 if (t1 == type)
556 return t2;
557 if (!t1)
558 break;
559 NEXT_PTR_LIST(t1);
560 NEXT_PTR_LIST(t2);
562 FINISH_PTR_LIST(t2);
563 FINISH_PTR_LIST(t1);
564 return NULL;
567 void check_declaration(struct symbol *sym)
569 int warned = 0;
570 struct symbol *next = sym;
572 while ((next = next->next_id) != NULL) {
573 if (next->namespace != sym->namespace)
574 continue;
575 if (sym->scope == next->scope) {
576 sym->same_symbol = next;
577 return;
579 /* Extern in block level matches a TOPLEVEL non-static symbol */
580 if (sym->ctype.modifiers & MOD_EXTERN) {
581 if ((next->ctype.modifiers & (MOD_TOPLEVEL|MOD_STATIC)) == MOD_TOPLEVEL) {
582 sym->same_symbol = next;
583 return;
587 if (!Wshadow || warned)
588 continue;
589 if (get_sym_type(next) == SYM_FN)
590 continue;
591 warned = 1;
592 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
593 info(next->pos, "originally declared here");
597 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
599 struct scope *scope;
600 if (sym->bound) {
601 sparse_error(sym->pos, "internal error: symbol type already bound");
602 return;
604 if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
605 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
606 return;
608 sym->namespace = ns;
609 sym->next_id = ident->symbols;
610 ident->symbols = sym;
611 if (sym->ident && sym->ident != ident)
612 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
613 sym->ident = ident;
614 sym->bound = 1;
616 scope = block_scope;
617 if (ns == NS_SYMBOL && toplevel(scope)) {
618 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
620 scope = global_scope;
621 if (sym->ctype.modifiers & MOD_STATIC ||
622 is_extern_inline(sym)) {
623 scope = file_scope;
624 mod = MOD_TOPLEVEL;
626 sym->ctype.modifiers |= mod;
628 if (ns == NS_MACRO)
629 scope = file_scope;
630 if (ns == NS_LABEL)
631 scope = function_scope;
632 bind_scope(sym, scope);
635 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
637 struct token *token = built_in_token(stream, name);
638 struct symbol *sym = alloc_symbol(token->pos, type);
640 bind_symbol(sym, token->ident, namespace);
641 return sym;
644 static int evaluate_to_integer(struct expression *expr)
646 expr->ctype = &int_ctype;
647 return 1;
650 static int evaluate_expect(struct expression *expr)
652 /* Should we evaluate it to return the type of the first argument? */
653 expr->ctype = &int_ctype;
654 return 1;
657 static int arguments_choose(struct expression *expr)
659 struct expression_list *arglist = expr->args;
660 struct expression *arg;
661 int i = 0;
663 FOR_EACH_PTR (arglist, arg) {
664 if (!evaluate_expression(arg))
665 return 0;
666 i++;
667 } END_FOR_EACH_PTR(arg);
668 if (i < 3) {
669 sparse_error(expr->pos,
670 "not enough arguments for __builtin_choose_expr");
671 return 0;
672 } if (i > 3) {
673 sparse_error(expr->pos,
674 "too many arguments for __builtin_choose_expr");
675 return 0;
677 return 1;
680 static int evaluate_choose(struct expression *expr)
682 struct expression_list *list = expr->args;
683 struct expression *arg, *args[3];
684 int n = 0;
686 /* there will be exactly 3; we'd already verified that */
687 FOR_EACH_PTR(list, arg) {
688 args[n++] = arg;
689 } END_FOR_EACH_PTR(arg);
691 *expr = get_expression_value(args[0]) ? *args[1] : *args[2];
693 return 1;
696 static int expand_expect(struct expression *expr, int cost)
698 struct expression *arg = first_ptr_list((struct ptr_list *) expr->args);
700 if (arg)
701 *expr = *arg;
702 return 0;
706 * __builtin_warning() has type "int" and always returns 1,
707 * so that you can use it in conditionals or whatever
709 static int expand_warning(struct expression *expr, int cost)
711 struct expression *arg;
712 struct expression_list *arglist = expr->args;
714 FOR_EACH_PTR (arglist, arg) {
716 * Constant strings get printed out as a warning. By the
717 * time we get here, the EXPR_STRING has been fully
718 * evaluated, so by now it's an anonymous symbol with a
719 * string initializer.
721 * Just for the heck of it, allow any constant string
722 * symbol.
724 if (arg->type == EXPR_SYMBOL) {
725 struct symbol *sym = arg->symbol;
726 if (sym->initializer && sym->initializer->type == EXPR_STRING) {
727 struct string *string = sym->initializer->string;
728 warning(expr->pos, "%*s", string->length-1, string->data);
730 continue;
734 * Any other argument is a conditional. If it's
735 * non-constant, or it is false, we exit and do
736 * not print any warning.
738 if (arg->type != EXPR_VALUE)
739 goto out;
740 if (!arg->value)
741 goto out;
742 } END_FOR_EACH_PTR(arg);
743 out:
744 expr->type = EXPR_VALUE;
745 expr->value = 1;
746 expr->taint = 0;
747 return 0;
750 static struct symbol_op constant_p_op = {
751 .evaluate = evaluate_to_integer,
752 .expand = expand_constant_p
755 static struct symbol_op safe_p_op = {
756 .evaluate = evaluate_to_integer,
757 .expand = expand_safe_p
760 static struct symbol_op warning_op = {
761 .evaluate = evaluate_to_integer,
762 .expand = expand_warning
765 static struct symbol_op expect_op = {
766 .evaluate = evaluate_expect,
767 .expand = expand_expect
770 static struct symbol_op choose_op = {
771 .evaluate = evaluate_choose,
772 .args = arguments_choose,
776 * Builtin functions
778 static struct symbol builtin_fn_type = { .type = SYM_FN /* , .variadic =1 */ };
779 static struct sym_init {
780 const char *name;
781 struct symbol *base_type;
782 unsigned int modifiers;
783 struct symbol_op *op;
784 } eval_init_table[] = {
785 { "__builtin_constant_p", &builtin_fn_type, MOD_TOPLEVEL, &constant_p_op },
786 { "__builtin_safe_p", &builtin_fn_type, MOD_TOPLEVEL, &safe_p_op },
787 { "__builtin_warning", &builtin_fn_type, MOD_TOPLEVEL, &warning_op },
788 { "__builtin_expect", &builtin_fn_type, MOD_TOPLEVEL, &expect_op },
789 { "__builtin_choose_expr", &builtin_fn_type, MOD_TOPLEVEL, &choose_op },
790 { NULL, NULL, 0 }
794 * Default empty attribute
796 struct attribute null_attr = {};
799 * Abstract types
801 struct symbol int_type,
802 fp_type;
805 * C types (i.e. actual instances that the abstract types
806 * can map onto)
808 struct symbol bool_ctype, void_ctype, type_ctype,
809 char_ctype, schar_ctype, uchar_ctype,
810 short_ctype, sshort_ctype, ushort_ctype,
811 int_ctype, sint_ctype, uint_ctype,
812 long_ctype, slong_ctype, ulong_ctype,
813 llong_ctype, sllong_ctype, ullong_ctype,
814 lllong_ctype, slllong_ctype, ulllong_ctype,
815 float_ctype, double_ctype, ldouble_ctype,
816 string_ctype, ptr_ctype, lazy_ptr_ctype,
817 incomplete_ctype, label_ctype, bad_ctype,
818 null_ctype;
820 struct symbol zero_int;
822 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
823 #define __IDENT(n,str,res) \
824 struct ident n = __INIT_IDENT(str,res)
826 #include "ident-list.h"
828 void init_symbols(void)
830 int stream = init_stream("builtin", -1, includepath);
831 struct sym_init *ptr;
833 #define __IDENT(n,str,res) \
834 hash_ident(&n)
835 #include "ident-list.h"
837 init_parser(stream);
839 builtin_fn_type.variadic = 1;
840 builtin_fn_type.ctype.attribute = &null_attr;
841 for (ptr = eval_init_table; ptr->name; ptr++) {
842 struct symbol *sym;
843 sym = create_symbol(stream, ptr->name, SYM_NODE, NS_SYMBOL);
844 sym->ctype.base_type = ptr->base_type;
845 sym->ctype.modifiers = ptr->modifiers;
846 sym->ctype.attribute = &null_attr;
847 sym->op = ptr->op;
851 #ifdef __CHAR_UNSIGNED__
852 #define CHAR_SIGNEDNESS MOD_UNSIGNED
853 #else
854 #define CHAR_SIGNEDNESS MOD_SIGNED
855 #endif
857 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
858 #define MOD_LL (MOD_LONG | MOD_LONGLONG)
859 #define MOD_LLL MOD_LONGLONGLONG
860 static const struct ctype_declare {
861 struct symbol *ptr;
862 enum type type;
863 unsigned long modifiers;
864 int *bit_size;
865 int *maxalign;
866 struct symbol *base_type;
867 } ctype_declaration[] = {
868 { &bool_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_bool, &max_int_alignment, &int_type },
869 { &void_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
870 { &type_ctype, SYM_BASETYPE, MOD_TYPE, NULL, NULL, NULL },
871 { &incomplete_ctype,SYM_BASETYPE, 0, NULL, NULL, NULL },
872 { &bad_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
874 { &char_ctype, SYM_BASETYPE, CHAR_SIGNEDNESS | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
875 { &schar_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
876 { &uchar_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
877 { &short_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
878 { &sshort_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
879 { &ushort_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
880 { &int_ctype, SYM_BASETYPE, MOD_SIGNED, &bits_in_int, &max_int_alignment, &int_type },
881 { &sint_ctype, SYM_BASETYPE, MOD_ESIGNED, &bits_in_int, &max_int_alignment, &int_type },
882 { &uint_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_int, &max_int_alignment, &int_type },
883 { &long_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
884 { &slong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
885 { &ulong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
886 { &llong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
887 { &sllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
888 { &ullong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
889 { &lllong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
890 { &slllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
891 { &ulllong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
893 { &float_ctype, SYM_BASETYPE, 0, &bits_in_float, &max_fp_alignment, &fp_type },
894 { &double_ctype, SYM_BASETYPE, MOD_LONG, &bits_in_double, &max_fp_alignment, &fp_type },
895 { &ldouble_ctype, SYM_BASETYPE, MOD_LONG | MOD_LONGLONG, &bits_in_longdouble, &max_fp_alignment, &fp_type },
897 { &string_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &char_ctype },
898 { &ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
899 { &null_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
900 { &label_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
901 { &lazy_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
902 { NULL, }
904 #undef MOD_LLL
905 #undef MOD_LL
906 #undef MOD_ESIGNED
908 void init_ctype(void)
910 const struct ctype_declare *ctype;
912 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
913 struct symbol *sym = ctype->ptr;
914 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
915 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
916 unsigned long alignment = bits_to_bytes(bit_size);
918 if (alignment > maxalign)
919 alignment = maxalign;
920 sym->type = ctype->type;
921 sym->bit_size = bit_size;
922 sym->ctype.alignment = alignment;
923 sym->ctype.base_type = ctype->base_type;
924 sym->ctype.modifiers = ctype->modifiers;
925 sym->ctype.attribute = &null_attr;