evaluate: split out implementation of compatible_assignment_types
[smatch.git] / symbol.c
blobeb6e1215ee879a56a0d42af18c32f9bf6cd2b62c
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 return sym;
85 struct struct_union_info {
86 unsigned long max_align;
87 unsigned long bit_size;
88 int align_size;
92 * Unions are fairly easy to lay out ;)
94 static void lay_out_union(struct symbol *sym, struct struct_union_info *info)
96 examine_symbol_type(sym);
98 // Unnamed bitfields do not affect alignment.
99 if (sym->ident || !is_bitfield_type(sym)) {
100 if (sym->ctype.alignment > info->max_align)
101 info->max_align = sym->ctype.alignment;
104 if (sym->bit_size > info->bit_size)
105 info->bit_size = sym->bit_size;
107 sym->offset = 0;
110 static int bitfield_base_size(struct symbol *sym)
112 if (sym->type == SYM_NODE)
113 sym = sym->ctype.base_type;
114 if (sym->type == SYM_BITFIELD)
115 sym = sym->ctype.base_type;
116 return sym->bit_size;
120 * Structures are a bit more interesting to lay out
122 static void lay_out_struct(struct symbol *sym, struct struct_union_info *info)
124 unsigned long bit_size, align_bit_mask;
125 int base_size;
127 examine_symbol_type(sym);
129 // Unnamed bitfields do not affect alignment.
130 if (sym->ident || !is_bitfield_type(sym)) {
131 if (sym->ctype.alignment > info->max_align)
132 info->max_align = sym->ctype.alignment;
135 bit_size = info->bit_size;
136 base_size = sym->bit_size;
139 * Unsized arrays cause us to not align the resulting
140 * structure size
142 if (base_size < 0) {
143 info->align_size = 0;
144 base_size = 0;
147 align_bit_mask = bytes_to_bits(sym->ctype.alignment) - 1;
150 * Bitfields have some very special rules..
152 if (is_bitfield_type (sym)) {
153 unsigned long bit_offset = bit_size & align_bit_mask;
154 int room = bitfield_base_size(sym) - bit_offset;
155 // Zero-width fields just fill up the unit.
156 int width = base_size ? : (bit_offset ? room : 0);
158 if (width > room) {
159 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
160 bit_offset = 0;
162 sym->offset = bits_to_bytes(bit_size - bit_offset);
163 sym->bit_offset = bit_offset;
164 sym->ctype.base_type->bit_offset = bit_offset;
165 info->bit_size = bit_size + width;
166 // warning (sym->pos, "bitfield: offset=%d:%d size=:%d", sym->offset, sym->bit_offset, width);
168 return;
172 * Otherwise, just align it right and add it up..
174 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
175 sym->offset = bits_to_bytes(bit_size);
177 info->bit_size = bit_size + base_size;
178 // warning (sym->pos, "regular: offset=%d", sym->offset);
181 static struct symbol * examine_struct_union_type(struct symbol *sym, int advance)
183 struct struct_union_info info = {
184 .max_align = 1,
185 .bit_size = 0,
186 .align_size = 1
188 unsigned long bit_size, bit_align;
189 void (*fn)(struct symbol *, struct struct_union_info *);
190 struct symbol *member;
192 fn = advance ? lay_out_struct : lay_out_union;
193 FOR_EACH_PTR(sym->symbol_list, member) {
194 fn(member, &info);
195 } END_FOR_EACH_PTR(member);
197 if (!sym->ctype.alignment)
198 sym->ctype.alignment = info.max_align;
199 bit_size = info.bit_size;
200 if (info.align_size) {
201 bit_align = bytes_to_bits(sym->ctype.alignment)-1;
202 bit_size = (bit_size + bit_align) & ~bit_align;
204 sym->bit_size = bit_size;
205 return sym;
208 static struct symbol *examine_base_type(struct symbol *sym)
210 struct symbol *base_type;
212 /* Check the base type */
213 base_type = examine_symbol_type(sym->ctype.base_type);
214 if (!base_type || base_type->type == SYM_PTR)
215 return base_type;
216 sym->ctype.as |= base_type->ctype.as;
217 sym->ctype.modifiers |= base_type->ctype.modifiers & MOD_PTRINHERIT;
218 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
219 (struct ptr_list **)&sym->ctype.contexts);
220 if (base_type->type == SYM_NODE) {
221 base_type = base_type->ctype.base_type;
222 sym->ctype.base_type = base_type;
224 return base_type;
227 static struct symbol * examine_array_type(struct symbol *sym)
229 struct symbol *base_type = examine_base_type(sym);
230 unsigned long bit_size = -1, alignment;
231 struct expression *array_size = sym->array_size;
233 if (!base_type)
234 return sym;
236 if (array_size) {
237 bit_size = base_type->bit_size * get_expression_value_silent(array_size);
238 if (array_size->type != EXPR_VALUE) {
239 if (Wvla)
240 warning(array_size->pos, "Variable length array is used.");
241 bit_size = -1;
244 alignment = base_type->ctype.alignment;
245 if (!sym->ctype.alignment)
246 sym->ctype.alignment = alignment;
247 sym->bit_size = bit_size;
248 return sym;
251 static struct symbol *examine_bitfield_type(struct symbol *sym)
253 struct symbol *base_type = examine_base_type(sym);
254 unsigned long bit_size, alignment, modifiers;
256 if (!base_type)
257 return sym;
258 bit_size = base_type->bit_size;
259 if (sym->bit_size > bit_size)
260 warning(sym->pos, "impossible field-width, %d, for this type", sym->bit_size);
262 alignment = base_type->ctype.alignment;
263 if (!sym->ctype.alignment)
264 sym->ctype.alignment = alignment;
265 modifiers = base_type->ctype.modifiers;
267 /* Bitfields are unsigned, unless the base type was explicitly signed */
268 if (!(modifiers & MOD_EXPLICITLY_SIGNED))
269 modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
270 sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
271 return sym;
275 * "typeof" will have to merge the types together
277 void merge_type(struct symbol *sym, struct symbol *base_type)
279 sym->ctype.as |= base_type->ctype.as;
280 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
281 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
282 (struct ptr_list **)&sym->ctype.contexts);
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 symbol * examine_node_type(struct symbol *sym)
359 struct symbol *base_type = examine_base_type(sym);
360 int bit_size;
361 unsigned long alignment;
363 /* SYM_NODE - figure out what the type of the node was.. */
364 bit_size = 0;
365 alignment = 0;
366 if (!base_type)
367 return sym;
369 bit_size = base_type->bit_size;
370 alignment = base_type->ctype.alignment;
372 /* Pick up signedness information into the node */
373 sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
375 if (!sym->ctype.alignment)
376 sym->ctype.alignment = alignment;
378 /* Unsized array? The size might come from the initializer.. */
379 if (bit_size < 0 && base_type->type == SYM_ARRAY && sym->initializer) {
380 struct symbol *node_type = base_type->ctype.base_type;
381 int count = count_array_initializer(node_type, sym->initializer);
383 if (node_type && node_type->bit_size >= 0)
384 bit_size = node_type->bit_size * count;
387 sym->bit_size = bit_size;
388 return sym;
391 static struct symbol *examine_enum_type(struct symbol *sym)
393 struct symbol *base_type = examine_base_type(sym);
395 sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
396 sym->bit_size = bits_in_enum;
397 if (base_type->bit_size > sym->bit_size)
398 sym->bit_size = base_type->bit_size;
399 sym->ctype.alignment = enum_alignment;
400 if (base_type->ctype.alignment > sym->ctype.alignment)
401 sym->ctype.alignment = base_type->ctype.alignment;
402 return sym;
405 static struct symbol *examine_pointer_type(struct symbol *sym)
408 * We need to set the pointer size first, and
409 * examine the thing we point to only afterwards.
410 * That's because this pointer type may end up
411 * being needed for the base type size evaluation.
413 if (!sym->bit_size)
414 sym->bit_size = bits_in_pointer;
415 if (!sym->ctype.alignment)
416 sym->ctype.alignment = pointer_alignment;
417 return sym;
421 * Fill in type size and alignment information for
422 * regular SYM_TYPE things.
424 struct symbol *examine_symbol_type(struct symbol * sym)
426 if (!sym)
427 return sym;
429 /* Already done? */
430 if (sym->examined)
431 return sym;
432 sym->examined = 1;
434 switch (sym->type) {
435 case SYM_FN:
436 case SYM_NODE:
437 return examine_node_type(sym);
438 case SYM_ARRAY:
439 return examine_array_type(sym);
440 case SYM_STRUCT:
441 return examine_struct_union_type(sym, 1);
442 case SYM_UNION:
443 return examine_struct_union_type(sym, 0);
444 case SYM_PTR:
445 return examine_pointer_type(sym);
446 case SYM_ENUM:
447 return examine_enum_type(sym);
448 case SYM_BITFIELD:
449 return examine_bitfield_type(sym);
450 case SYM_BASETYPE:
451 /* Size and alignment had better already be set up */
452 return sym;
453 case SYM_TYPEOF: {
454 struct symbol *base = evaluate_expression(sym->initializer);
455 if (base) {
456 if (is_bitfield_type(base))
457 warning(base->pos, "typeof applied to bitfield type");
458 if (base->type == SYM_NODE)
459 base = base->ctype.base_type;
460 sym->type = SYM_NODE;
461 sym->ctype.modifiers = 0;
462 sym->ctype.base_type = base;
463 return examine_node_type(sym);
465 break;
467 case SYM_PREPROCESSOR:
468 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
469 return NULL;
470 case SYM_UNINITIALIZED:
471 sparse_error(sym->pos, "ctype on uninitialized symbol %p", sym);
472 return NULL;
473 case SYM_RESTRICT:
474 examine_base_type(sym);
475 return sym;
476 case SYM_FOULED:
477 examine_base_type(sym);
478 return sym;
479 default:
480 sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
481 break;
483 return sym;
486 const char* get_type_name(enum type type)
488 const char *type_lookup[] = {
489 [SYM_UNINITIALIZED] = "uninitialized",
490 [SYM_PREPROCESSOR] = "preprocessor",
491 [SYM_BASETYPE] = "basetype",
492 [SYM_NODE] = "node",
493 [SYM_PTR] = "pointer",
494 [SYM_FN] = "function",
495 [SYM_ARRAY] = "array",
496 [SYM_STRUCT] = "struct",
497 [SYM_UNION] = "union",
498 [SYM_ENUM] = "enum",
499 [SYM_TYPEDEF] = "typedef",
500 [SYM_TYPEOF] = "typeof",
501 [SYM_MEMBER] = "member",
502 [SYM_BITFIELD] = "bitfield",
503 [SYM_LABEL] = "label",
504 [SYM_RESTRICT] = "restrict",
505 [SYM_FOULED] = "fouled",
506 [SYM_KEYWORD] = "keyword",
507 [SYM_BAD] = "bad"};
509 if (type <= SYM_BAD)
510 return type_lookup[type];
511 else
512 return NULL;
515 struct symbol *examine_pointer_target(struct symbol *sym)
517 return examine_base_type(sym);
520 static struct symbol_list *restr, *fouled;
522 void create_fouled(struct symbol *type)
524 if (type->bit_size < bits_in_int) {
525 struct symbol *new = alloc_symbol(type->pos, type->type);
526 *new = *type;
527 new->bit_size = bits_in_int;
528 new->type = SYM_FOULED;
529 new->ctype.base_type = type;
530 add_symbol(&restr, type);
531 add_symbol(&fouled, new);
535 struct symbol *befoul(struct symbol *type)
537 struct symbol *t1, *t2;
538 while (type->type == SYM_NODE)
539 type = type->ctype.base_type;
540 PREPARE_PTR_LIST(restr, t1);
541 PREPARE_PTR_LIST(fouled, t2);
542 for (;;) {
543 if (t1 == type)
544 return t2;
545 if (!t1)
546 break;
547 NEXT_PTR_LIST(t1);
548 NEXT_PTR_LIST(t2);
550 FINISH_PTR_LIST(t2);
551 FINISH_PTR_LIST(t1);
552 return NULL;
555 void check_declaration(struct symbol *sym)
557 int warned = 0;
558 struct symbol *next = sym;
560 while ((next = next->next_id) != NULL) {
561 if (next->namespace != sym->namespace)
562 continue;
563 if (sym->scope == next->scope) {
564 sym->same_symbol = next;
565 return;
567 if (sym->ctype.modifiers & next->ctype.modifiers & MOD_EXTERN) {
568 if ((sym->ctype.modifiers ^ next->ctype.modifiers) & MOD_INLINE)
569 continue;
570 sym->same_symbol = next;
571 return;
574 if (!Wshadow || warned)
575 continue;
576 if (get_sym_type(next) == SYM_FN)
577 continue;
578 warned = 1;
579 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
580 info(next->pos, "originally declared here");
584 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
586 struct scope *scope;
587 if (sym->bound) {
588 sparse_error(sym->pos, "internal error: symbol type already bound");
589 return;
591 if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
592 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
593 return;
595 sym->namespace = ns;
596 sym->next_id = ident->symbols;
597 ident->symbols = sym;
598 if (sym->ident && sym->ident != ident)
599 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
600 sym->ident = ident;
601 sym->bound = 1;
603 scope = block_scope;
604 if (ns == NS_SYMBOL && toplevel(scope)) {
605 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
607 scope = global_scope;
608 if (sym->ctype.modifiers & MOD_STATIC ||
609 is_extern_inline(sym)) {
610 scope = file_scope;
611 mod = MOD_TOPLEVEL;
613 sym->ctype.modifiers |= mod;
615 if (ns == NS_MACRO)
616 scope = file_scope;
617 if (ns == NS_LABEL)
618 scope = function_scope;
619 bind_scope(sym, scope);
622 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
624 struct token *token = built_in_token(stream, name);
625 struct symbol *sym = alloc_symbol(token->pos, type);
627 bind_symbol(sym, token->ident, namespace);
628 return sym;
631 static int evaluate_to_integer(struct expression *expr)
633 expr->ctype = &int_ctype;
634 return 1;
637 static int evaluate_expect(struct expression *expr)
639 /* Should we evaluate it to return the type of the first argument? */
640 expr->ctype = &int_ctype;
641 return 1;
644 static int arguments_choose(struct expression *expr)
646 struct expression_list *arglist = expr->args;
647 struct expression *arg;
648 int i = 0;
650 FOR_EACH_PTR (arglist, arg) {
651 if (!evaluate_expression(arg))
652 return 0;
653 i++;
654 } END_FOR_EACH_PTR(arg);
655 if (i < 3) {
656 sparse_error(expr->pos,
657 "not enough arguments for __builtin_choose_expr");
658 return 0;
659 } if (i > 3) {
660 sparse_error(expr->pos,
661 "too many arguments for __builtin_choose_expr");
662 return 0;
664 return 1;
667 static int evaluate_choose(struct expression *expr)
669 struct expression_list *list = expr->args;
670 struct expression *arg, *args[3];
671 int n = 0;
673 /* there will be exactly 3; we'd already verified that */
674 FOR_EACH_PTR(list, arg) {
675 args[n++] = arg;
676 } END_FOR_EACH_PTR(arg);
678 *expr = get_expression_value(args[0]) ? *args[1] : *args[2];
680 return 1;
683 static int expand_expect(struct expression *expr, int cost)
685 struct expression *arg = first_ptr_list((struct ptr_list *) expr->args);
687 if (arg)
688 *expr = *arg;
689 return 0;
693 * __builtin_warning() has type "int" and always returns 1,
694 * so that you can use it in conditionals or whatever
696 static int expand_warning(struct expression *expr, int cost)
698 struct expression *arg;
699 struct expression_list *arglist = expr->args;
701 FOR_EACH_PTR (arglist, arg) {
703 * Constant strings get printed out as a warning. By the
704 * time we get here, the EXPR_STRING has been fully
705 * evaluated, so by now it's an anonymous symbol with a
706 * string initializer.
708 * Just for the heck of it, allow any constant string
709 * symbol.
711 if (arg->type == EXPR_SYMBOL) {
712 struct symbol *sym = arg->symbol;
713 if (sym->initializer && sym->initializer->type == EXPR_STRING) {
714 struct string *string = sym->initializer->string;
715 warning(expr->pos, "%*s", string->length-1, string->data);
717 continue;
721 * Any other argument is a conditional. If it's
722 * non-constant, or it is false, we exit and do
723 * not print any warning.
725 if (arg->type != EXPR_VALUE)
726 goto out;
727 if (!arg->value)
728 goto out;
729 } END_FOR_EACH_PTR(arg);
730 out:
731 expr->type = EXPR_VALUE;
732 expr->value = 1;
733 expr->taint = 0;
734 return 0;
737 static struct symbol_op constant_p_op = {
738 .evaluate = evaluate_to_integer,
739 .expand = expand_constant_p
742 static struct symbol_op safe_p_op = {
743 .evaluate = evaluate_to_integer,
744 .expand = expand_safe_p
747 static struct symbol_op warning_op = {
748 .evaluate = evaluate_to_integer,
749 .expand = expand_warning
752 static struct symbol_op expect_op = {
753 .evaluate = evaluate_expect,
754 .expand = expand_expect
757 static struct symbol_op choose_op = {
758 .evaluate = evaluate_choose,
759 .args = arguments_choose,
763 * Builtin functions
765 static struct symbol builtin_fn_type = { .type = SYM_FN /* , .variadic =1 */ };
766 static struct sym_init {
767 const char *name;
768 struct symbol *base_type;
769 unsigned int modifiers;
770 struct symbol_op *op;
771 } eval_init_table[] = {
772 { "__builtin_constant_p", &builtin_fn_type, MOD_TOPLEVEL, &constant_p_op },
773 { "__builtin_safe_p", &builtin_fn_type, MOD_TOPLEVEL, &safe_p_op },
774 { "__builtin_warning", &builtin_fn_type, MOD_TOPLEVEL, &warning_op },
775 { "__builtin_expect", &builtin_fn_type, MOD_TOPLEVEL, &expect_op },
776 { "__builtin_choose_expr", &builtin_fn_type, MOD_TOPLEVEL, &choose_op },
777 { NULL, NULL, 0 }
782 * Abstract types
784 struct symbol int_type,
785 fp_type;
788 * C types (i.e. actual instances that the abstract types
789 * can map onto)
791 struct symbol bool_ctype, void_ctype, type_ctype,
792 char_ctype, schar_ctype, uchar_ctype,
793 short_ctype, sshort_ctype, ushort_ctype,
794 int_ctype, sint_ctype, uint_ctype,
795 long_ctype, slong_ctype, ulong_ctype,
796 llong_ctype, sllong_ctype, ullong_ctype,
797 lllong_ctype, slllong_ctype, ulllong_ctype,
798 float_ctype, double_ctype, ldouble_ctype,
799 string_ctype, ptr_ctype, lazy_ptr_ctype,
800 incomplete_ctype, label_ctype, bad_ctype,
801 null_ctype;
803 struct symbol zero_int;
805 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
806 #define __IDENT(n,str,res) \
807 struct ident n = __INIT_IDENT(str,res)
809 #include "ident-list.h"
811 void init_symbols(void)
813 int stream = init_stream("builtin", -1, includepath);
814 struct sym_init *ptr;
816 #define __IDENT(n,str,res) \
817 hash_ident(&n)
818 #include "ident-list.h"
820 init_parser(stream);
822 builtin_fn_type.variadic = 1;
823 for (ptr = eval_init_table; ptr->name; ptr++) {
824 struct symbol *sym;
825 sym = create_symbol(stream, ptr->name, SYM_NODE, NS_SYMBOL);
826 sym->ctype.base_type = ptr->base_type;
827 sym->ctype.modifiers = ptr->modifiers;
828 sym->op = ptr->op;
832 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
833 #define MOD_LL (MOD_LONG | MOD_LONGLONG)
834 #define MOD_LLL MOD_LONGLONGLONG
835 static const struct ctype_declare {
836 struct symbol *ptr;
837 enum type type;
838 unsigned long modifiers;
839 int *bit_size;
840 int *maxalign;
841 struct symbol *base_type;
842 } ctype_declaration[] = {
843 { &bool_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_bool, &max_int_alignment, &int_type },
844 { &void_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
845 { &type_ctype, SYM_BASETYPE, MOD_TYPE, NULL, NULL, NULL },
846 { &incomplete_ctype,SYM_BASETYPE, 0, NULL, NULL, NULL },
847 { &bad_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
849 { &char_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
850 { &schar_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
851 { &uchar_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
852 { &short_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
853 { &sshort_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
854 { &ushort_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
855 { &int_ctype, SYM_BASETYPE, MOD_SIGNED, &bits_in_int, &max_int_alignment, &int_type },
856 { &sint_ctype, SYM_BASETYPE, MOD_ESIGNED, &bits_in_int, &max_int_alignment, &int_type },
857 { &uint_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_int, &max_int_alignment, &int_type },
858 { &long_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
859 { &slong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
860 { &ulong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
861 { &llong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
862 { &sllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
863 { &ullong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
864 { &lllong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
865 { &slllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
866 { &ulllong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
868 { &float_ctype, SYM_BASETYPE, 0, &bits_in_float, &max_fp_alignment, &fp_type },
869 { &double_ctype, SYM_BASETYPE, MOD_LONG, &bits_in_double, &max_fp_alignment, &fp_type },
870 { &ldouble_ctype, SYM_BASETYPE, MOD_LONG | MOD_LONGLONG, &bits_in_longdouble, &max_fp_alignment, &fp_type },
872 { &string_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &char_ctype },
873 { &ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
874 { &null_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
875 { &label_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
876 { &lazy_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
877 { NULL, }
879 #undef MOD_LLL
880 #undef MOD_LL
881 #undef MOD_ESIGNED
883 void init_ctype(void)
885 const struct ctype_declare *ctype;
887 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
888 struct symbol *sym = ctype->ptr;
889 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
890 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
891 unsigned long alignment = bits_to_bytes(bit_size + bits_in_char - 1);
893 if (alignment > maxalign)
894 alignment = maxalign;
895 sym->type = ctype->type;
896 sym->bit_size = bit_size;
897 sym->ctype.alignment = alignment;
898 sym->ctype.base_type = ctype->base_type;
899 sym->ctype.modifiers = ctype->modifiers;