If you have something like
[smatch.git] / symbol.c
blob32929075e6ebc9cd0dd95bf3631737cdc3f0b629
1 /*
2 * Symbol lookup and handling.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
8 */
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
13 #include "lib.h"
14 #include "allocate.h"
15 #include "token.h"
16 #include "parse.h"
17 #include "symbol.h"
18 #include "scope.h"
19 #include "expression.h"
21 #include "target.h"
24 * Secondary symbol list for stuff that needs to be output because it
25 * was used.
27 struct symbol_list *translation_unit_used_list = NULL;
30 * If the symbol is an inline symbol, add it to the list of symbols to parse
32 void access_symbol(struct symbol *sym)
34 if (sym->ctype.modifiers & MOD_INLINE) {
35 if (!(sym->ctype.modifiers & MOD_ACCESSED)) {
36 add_symbol(&translation_unit_used_list, sym);
37 sym->ctype.modifiers |= MOD_ACCESSED;
42 struct symbol *lookup_symbol(struct ident *ident, enum namespace ns)
44 struct symbol *sym;
46 for (sym = ident->symbols; sym; sym = sym->next_id) {
47 if (sym->namespace & ns) {
48 sym->used = 1;
49 return sym;
52 return sym;
55 struct context *alloc_context(void)
57 return __alloc_context(0);
60 struct symbol *alloc_symbol(struct position pos, int type)
62 struct symbol *sym = __alloc_symbol(0);
63 sym->type = type;
64 sym->pos = pos;
65 sym->endpos.type = 0;
66 return sym;
69 struct struct_union_info {
70 unsigned long max_align;
71 unsigned long bit_size;
72 int align_size;
76 * Unions are fairly easy to lay out ;)
78 static void lay_out_union(struct symbol *sym, struct struct_union_info *info)
80 examine_symbol_type(sym);
82 // Unnamed bitfields do not affect alignment.
83 if (sym->ident || !is_bitfield_type(sym)) {
84 if (sym->ctype.alignment > info->max_align)
85 info->max_align = sym->ctype.alignment;
88 if (sym->bit_size > info->bit_size)
89 info->bit_size = sym->bit_size;
91 sym->offset = 0;
94 static int bitfield_base_size(struct symbol *sym)
96 if (sym->type == SYM_NODE)
97 sym = sym->ctype.base_type;
98 if (sym->type == SYM_BITFIELD)
99 sym = sym->ctype.base_type;
100 return sym->bit_size;
104 * Structures are a bit more interesting to lay out
106 static void lay_out_struct(struct symbol *sym, struct struct_union_info *info)
108 unsigned long bit_size, align_bit_mask;
109 int base_size;
111 examine_symbol_type(sym);
113 // Unnamed bitfields do not affect alignment.
114 if (sym->ident || !is_bitfield_type(sym)) {
115 if (sym->ctype.alignment > info->max_align)
116 info->max_align = sym->ctype.alignment;
119 bit_size = info->bit_size;
120 base_size = sym->bit_size;
123 * Unsized arrays cause us to not align the resulting
124 * structure size
126 if (base_size < 0) {
127 info->align_size = 0;
128 base_size = 0;
131 align_bit_mask = (sym->ctype.alignment << 3) - 1;
134 * Bitfields have some very special rules..
136 if (is_bitfield_type (sym)) {
137 unsigned long bit_offset = bit_size & align_bit_mask;
138 int room = bitfield_base_size(sym) - bit_offset;
139 // Zero-width fields just fill up the unit.
140 int width = base_size ? : (bit_offset ? room : 0);
142 if (width > room) {
143 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
144 bit_offset = 0;
146 sym->offset = (bit_size - bit_offset) >> 3;
147 sym->bit_offset = bit_offset;
148 sym->ctype.base_type->bit_offset = bit_offset;
149 info->bit_size = bit_size + width;
150 // warning (sym->pos, "bitfield: offset=%d:%d size=:%d", sym->offset, sym->bit_offset, width);
152 return;
156 * Otherwise, just align it right and add it up..
158 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
159 sym->offset = bit_size >> 3;
161 info->bit_size = bit_size + base_size;
162 // warning (sym->pos, "regular: offset=%d", sym->offset);
165 static struct symbol * examine_struct_union_type(struct symbol *sym, int advance)
167 struct struct_union_info info = {
168 .max_align = 1,
169 .bit_size = 0,
170 .align_size = 1
172 unsigned long bit_size, bit_align;
173 void (*fn)(struct symbol *, struct struct_union_info *);
174 struct symbol *member;
176 fn = advance ? lay_out_struct : lay_out_union;
177 FOR_EACH_PTR(sym->symbol_list, member) {
178 fn(member, &info);
179 } END_FOR_EACH_PTR(member);
181 if (!sym->ctype.alignment)
182 sym->ctype.alignment = info.max_align;
183 bit_size = info.bit_size;
184 if (info.align_size) {
185 bit_align = (sym->ctype.alignment << 3)-1;
186 bit_size = (bit_size + bit_align) & ~bit_align;
188 sym->bit_size = bit_size;
189 return sym;
192 static struct symbol *examine_base_type(struct symbol *sym)
194 struct symbol *base_type;
196 /* Check the base type */
197 base_type = examine_symbol_type(sym->ctype.base_type);
198 if (!base_type || base_type->type == SYM_PTR)
199 return base_type;
200 sym->ctype.as |= base_type->ctype.as;
201 sym->ctype.modifiers |= base_type->ctype.modifiers & MOD_PTRINHERIT;
202 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
203 (struct ptr_list **)&sym->ctype.contexts);
204 if (base_type->type == SYM_NODE) {
205 base_type = base_type->ctype.base_type;
206 sym->ctype.base_type = base_type;
208 return base_type;
211 static struct symbol * examine_array_type(struct symbol *sym)
213 struct symbol *base_type = examine_base_type(sym);
214 unsigned long bit_size, alignment;
216 if (!base_type)
217 return sym;
218 bit_size = base_type->bit_size * get_expression_value(sym->array_size);
219 if (!sym->array_size || sym->array_size->type != EXPR_VALUE)
220 bit_size = -1;
221 alignment = base_type->ctype.alignment;
222 if (!sym->ctype.alignment)
223 sym->ctype.alignment = alignment;
224 sym->bit_size = bit_size;
225 return sym;
228 static struct symbol *examine_bitfield_type(struct symbol *sym)
230 struct symbol *base_type = examine_base_type(sym);
231 unsigned long bit_size, alignment, modifiers;
233 if (!base_type)
234 return sym;
235 bit_size = base_type->bit_size;
236 if (sym->bit_size > bit_size)
237 warning(sym->pos, "impossible field-width, %d, for this type", sym->bit_size);
239 alignment = base_type->ctype.alignment;
240 if (!sym->ctype.alignment)
241 sym->ctype.alignment = alignment;
242 modifiers = base_type->ctype.modifiers;
244 /* Bitfields are unsigned, unless the base type was explicitly signed */
245 if (!(modifiers & MOD_EXPLICITLY_SIGNED))
246 modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
247 sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
248 return sym;
252 * "typeof" will have to merge the types together
254 void merge_type(struct symbol *sym, struct symbol *base_type)
256 sym->ctype.as |= base_type->ctype.as;
257 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
258 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
259 (struct ptr_list **)&sym->ctype.contexts);
260 sym->ctype.base_type = base_type->ctype.base_type;
261 if (sym->ctype.base_type->type == SYM_NODE)
262 merge_type(sym, sym->ctype.base_type);
265 static int count_array_initializer(struct symbol *t, struct expression *expr)
267 int nr = 0;
268 int is_char = 0;
271 * Arrays of character types are special; they can be initialized by
272 * string literal _or_ by string literal in braces. The latter means
273 * that with T x[] = {<string literal>} number of elements in x depends
274 * on T - if it's a character type, we get the length of string literal
275 * (including NUL), otherwise we have one element here.
277 if (t->ctype.base_type == &int_type && t->ctype.modifiers & MOD_CHAR)
278 is_char = 1;
280 switch (expr->type) {
281 case EXPR_INITIALIZER: {
282 struct expression *entry;
283 int count = 0;
284 int str_len = 0;
285 FOR_EACH_PTR(expr->expr_list, entry) {
286 count++;
287 switch (entry->type) {
288 case EXPR_INDEX:
289 if (entry->idx_to >= nr)
290 nr = entry->idx_to+1;
291 break;
292 case EXPR_STRING:
293 if (is_char)
294 str_len = entry->string->length;
295 default:
296 nr++;
298 } END_FOR_EACH_PTR(entry);
299 if (count == 1 && str_len)
300 nr = str_len;
301 break;
303 case EXPR_STRING:
304 if (is_char)
305 nr = expr->string->length;
306 default:
307 break;
309 return nr;
312 static struct symbol * examine_node_type(struct symbol *sym)
314 struct symbol *base_type = examine_base_type(sym);
315 int bit_size;
316 unsigned long alignment, modifiers;
318 /* SYM_NODE - figure out what the type of the node was.. */
319 modifiers = sym->ctype.modifiers;
321 bit_size = 0;
322 alignment = 0;
323 if (!base_type)
324 return sym;
326 bit_size = base_type->bit_size;
327 alignment = base_type->ctype.alignment;
329 /* Pick up signedness information into the node */
330 sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
332 if (!sym->ctype.alignment)
333 sym->ctype.alignment = alignment;
335 /* Unsized array? The size might come from the initializer.. */
336 if (bit_size < 0 && base_type->type == SYM_ARRAY && sym->initializer) {
337 struct symbol *node_type = base_type->ctype.base_type;
338 int count = count_array_initializer(node_type, sym->initializer);
340 if (node_type && node_type->bit_size >= 0)
341 bit_size = node_type->bit_size * count;
344 sym->bit_size = bit_size;
345 return sym;
348 static struct symbol *examine_enum_type(struct symbol *sym)
350 struct symbol *base_type = examine_base_type(sym);
352 sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
353 sym->bit_size = bits_in_enum;
354 if (base_type->bit_size > sym->bit_size)
355 sym->bit_size = base_type->bit_size;
356 sym->ctype.alignment = enum_alignment;
357 if (base_type->ctype.alignment > sym->ctype.alignment)
358 sym->ctype.alignment = base_type->ctype.alignment;
359 return sym;
362 static struct symbol *examine_pointer_type(struct symbol *sym)
365 * We need to set the pointer size first, and
366 * examine the thing we point to only afterwards.
367 * That's because this pointer type may end up
368 * being needed for the base type size evaluation.
370 if (!sym->bit_size)
371 sym->bit_size = bits_in_pointer;
372 if (!sym->ctype.alignment)
373 sym->ctype.alignment = pointer_alignment;
374 return sym;
378 * Fill in type size and alignment information for
379 * regular SYM_TYPE things.
381 struct symbol *examine_symbol_type(struct symbol * sym)
383 if (!sym)
384 return sym;
386 /* Already done? */
387 if (sym->examined)
388 return sym;
389 sym->examined = 1;
391 switch (sym->type) {
392 case SYM_FN:
393 case SYM_NODE:
394 return examine_node_type(sym);
395 case SYM_ARRAY:
396 return examine_array_type(sym);
397 case SYM_STRUCT:
398 return examine_struct_union_type(sym, 1);
399 case SYM_UNION:
400 return examine_struct_union_type(sym, 0);
401 case SYM_PTR:
402 return examine_pointer_type(sym);
403 case SYM_ENUM:
404 return examine_enum_type(sym);
405 case SYM_BITFIELD:
406 return examine_bitfield_type(sym);
407 case SYM_BASETYPE:
408 /* Size and alignment had better already be set up */
409 return sym;
410 case SYM_TYPEOF: {
411 struct symbol *base = evaluate_expression(sym->initializer);
412 if (base) {
413 if (is_bitfield_type(base))
414 warning(base->pos, "typeof applied to bitfield type");
415 if (base->type == SYM_NODE)
416 base = base->ctype.base_type;
417 sym->type = SYM_NODE;
418 sym->ctype.modifiers = 0;
419 sym->ctype.base_type = base;
420 return examine_node_type(sym);
422 break;
424 case SYM_PREPROCESSOR:
425 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
426 return NULL;
427 case SYM_UNINITIALIZED:
428 sparse_error(sym->pos, "ctype on uninitialized symbol %p", sym);
429 return NULL;
430 case SYM_RESTRICT:
431 examine_base_type(sym);
432 return sym;
433 case SYM_FOULED:
434 examine_base_type(sym);
435 return sym;
436 default:
437 sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
438 break;
440 return sym;
443 const char* get_type_name(enum type type)
445 const char *type_lookup[] = {
446 [SYM_UNINITIALIZED] = "uninitialized",
447 [SYM_PREPROCESSOR] = "preprocessor",
448 [SYM_BASETYPE] = "basetype",
449 [SYM_NODE] = "node",
450 [SYM_PTR] = "pointer",
451 [SYM_FN] = "function",
452 [SYM_ARRAY] = "array",
453 [SYM_STRUCT] = "struct",
454 [SYM_UNION] = "union",
455 [SYM_ENUM] = "enum",
456 [SYM_TYPEDEF] = "typedef",
457 [SYM_TYPEOF] = "typeof",
458 [SYM_MEMBER] = "member",
459 [SYM_BITFIELD] = "bitfield",
460 [SYM_LABEL] = "label",
461 [SYM_RESTRICT] = "restrict",
462 [SYM_FOULED] = "fouled",
463 [SYM_KEYWORD] = "keyword",
464 [SYM_BAD] = "bad"};
466 if (type <= SYM_BAD)
467 return type_lookup[type];
468 else
469 return NULL;
472 struct symbol *examine_pointer_target(struct symbol *sym)
474 return examine_base_type(sym);
477 static struct symbol_list *restr, *fouled;
479 void create_fouled(struct symbol *type)
481 if (type->bit_size < bits_in_int) {
482 struct symbol *new = alloc_symbol(type->pos, type->type);
483 *new = *type;
484 new->bit_size = bits_in_int;
485 new->type = SYM_FOULED;
486 new->ctype.base_type = type;
487 add_symbol(&restr, type);
488 add_symbol(&fouled, new);
492 struct symbol *befoul(struct symbol *type)
494 struct symbol *t1, *t2;
495 while (type->type == SYM_NODE)
496 type = type->ctype.base_type;
497 PREPARE_PTR_LIST(restr, t1);
498 PREPARE_PTR_LIST(fouled, t2);
499 for (;;) {
500 if (t1 == type)
501 return t2;
502 if (!t1)
503 break;
504 NEXT_PTR_LIST(t1);
505 NEXT_PTR_LIST(t2);
507 FINISH_PTR_LIST(t2);
508 FINISH_PTR_LIST(t1);
509 return NULL;
512 void check_declaration(struct symbol *sym)
514 int warned = 0;
515 struct symbol *next = sym;
517 while ((next = next->next_id) != NULL) {
518 if (next->namespace != sym->namespace)
519 continue;
520 if (sym->scope == next->scope) {
521 sym->same_symbol = next;
522 return;
524 if (sym->ctype.modifiers & next->ctype.modifiers & MOD_EXTERN) {
525 sym->same_symbol = next;
526 return;
529 if (!Wshadow || warned)
530 continue;
531 if (get_sym_type(next) == SYM_FN)
532 continue;
533 warned = 1;
534 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
535 info(next->pos, "originally declared here");
539 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
541 struct scope *scope;
542 if (sym->bound) {
543 sparse_error(sym->pos, "internal error: symbol type already bound");
544 return;
546 if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
547 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
548 return;
550 sym->namespace = ns;
551 sym->next_id = ident->symbols;
552 ident->symbols = sym;
553 if (sym->ident && sym->ident != ident)
554 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
555 sym->ident = ident;
556 sym->bound = 1;
558 scope = block_scope;
559 if (ns == NS_SYMBOL && toplevel(scope)) {
560 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
561 scope = global_scope;
562 if (sym->ctype.modifiers & MOD_STATIC) {
563 scope = file_scope;
564 mod = MOD_TOPLEVEL;
566 sym->ctype.modifiers |= mod;
568 if (ns == NS_MACRO)
569 scope = file_scope;
570 if (ns == NS_LABEL)
571 scope = function_scope;
572 bind_scope(sym, scope);
575 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
577 struct token *token = built_in_token(stream, name);
578 struct symbol *sym = alloc_symbol(token->pos, type);
580 bind_symbol(sym, token->ident, namespace);
581 return sym;
584 static int evaluate_to_integer(struct expression *expr)
586 expr->ctype = &int_ctype;
587 return 1;
590 static int evaluate_expect(struct expression *expr)
592 /* Should we evaluate it to return the type of the first argument? */
593 expr->ctype = &int_ctype;
594 return 1;
597 static int arguments_choose(struct expression *expr)
599 struct expression_list *arglist = expr->args;
600 struct expression *arg;
601 int i = 0;
603 FOR_EACH_PTR (arglist, arg) {
604 if (!evaluate_expression(arg))
605 return 0;
606 i++;
607 } END_FOR_EACH_PTR(arg);
608 if (i < 3) {
609 sparse_error(expr->pos,
610 "not enough arguments for __builtin_choose_expr");
611 return 0;
612 } if (i > 3) {
613 sparse_error(expr->pos,
614 "too many arguments for __builtin_choose_expr");
615 return 0;
617 return 1;
620 static int evaluate_choose(struct expression *expr)
622 struct expression_list *list = expr->args;
623 struct expression *arg, *args[3];
624 int n = 0;
626 /* there will be exactly 3; we'd already verified that */
627 FOR_EACH_PTR(list, arg) {
628 args[n++] = arg;
629 } END_FOR_EACH_PTR(arg);
631 *expr = get_expression_value(args[0]) ? *args[1] : *args[2];
633 return 1;
636 static int expand_expect(struct expression *expr, int cost)
638 struct expression *arg = first_ptr_list((struct ptr_list *) expr->args);
640 if (arg)
641 *expr = *arg;
642 return 0;
646 * __builtin_warning() has type "int" and always returns 1,
647 * so that you can use it in conditionals or whatever
649 static int expand_warning(struct expression *expr, int cost)
651 struct expression *arg;
652 struct expression_list *arglist = expr->args;
654 FOR_EACH_PTR (arglist, arg) {
656 * Constant strings get printed out as a warning. By the
657 * time we get here, the EXPR_STRING has been fully
658 * evaluated, so by now it's an anonymous symbol with a
659 * string initializer.
661 * Just for the heck of it, allow any constant string
662 * symbol.
664 if (arg->type == EXPR_SYMBOL) {
665 struct symbol *sym = arg->symbol;
666 if (sym->initializer && sym->initializer->type == EXPR_STRING) {
667 struct string *string = sym->initializer->string;
668 warning(expr->pos, "%*s", string->length-1, string->data);
670 continue;
674 * Any other argument is a conditional. If it's
675 * non-constant, or it is false, we exit and do
676 * not print any warning.
678 if (arg->type != EXPR_VALUE)
679 goto out;
680 if (!arg->value)
681 goto out;
682 } END_FOR_EACH_PTR(arg);
683 out:
684 expr->type = EXPR_VALUE;
685 expr->value = 1;
686 expr->taint = 0;
687 return 0;
691 * Type and storage class keywords need to have the symbols
692 * created for them, so that the parser can have enough semantic
693 * information to do parsing.
695 * "double" == "long float", "long double" == "long long float"
697 static struct sym_init {
698 const char *name;
699 struct symbol *base_type;
700 unsigned int modifiers;
701 struct symbol_op *op;
702 } symbol_init_table[] = {
703 /* Storage class */
704 { "auto", NULL, MOD_AUTO },
705 { "register", NULL, MOD_REGISTER },
706 { "static", NULL, MOD_STATIC },
707 { "extern", NULL, MOD_EXTERN },
709 /* Type specifiers */
710 { "void", &void_ctype, 0 },
711 { "char", NULL, MOD_CHAR },
712 { "short", NULL, MOD_SHORT },
713 { "int", &int_type, 0 },
714 { "long", NULL, MOD_LONG },
715 { "float", &fp_type, 0 },
716 { "double", &fp_type, MOD_LONG },
717 { "signed", NULL, MOD_SIGNED | MOD_EXPLICITLY_SIGNED },
718 { "__signed", NULL, MOD_SIGNED | MOD_EXPLICITLY_SIGNED },
719 { "__signed__", NULL, MOD_SIGNED | MOD_EXPLICITLY_SIGNED },
720 { "unsigned", NULL, MOD_UNSIGNED },
721 { "__label__", &label_ctype, MOD_LABEL | MOD_UNSIGNED },
722 { "_Bool", &bool_ctype, MOD_UNSIGNED },
724 /* Predeclared types */
725 { "__builtin_va_list", &int_type, 0 },
727 { NULL, NULL, 0 }
730 static struct symbol_op constant_p_op = {
731 .evaluate = evaluate_to_integer,
732 .expand = expand_constant_p
735 static struct symbol_op safe_p_op = {
736 .evaluate = evaluate_to_integer,
737 .expand = expand_safe_p
740 static struct symbol_op warning_op = {
741 .evaluate = evaluate_to_integer,
742 .expand = expand_warning
745 static struct symbol_op expect_op = {
746 .evaluate = evaluate_expect,
747 .expand = expand_expect
750 static struct symbol_op choose_op = {
751 .evaluate = evaluate_choose,
752 .args = arguments_choose,
756 * Builtin functions
758 static struct symbol builtin_fn_type = { .type = SYM_FN /* , .variadic =1 */ };
759 static struct sym_init eval_init_table[] = {
760 { "__builtin_constant_p", &builtin_fn_type, MOD_TOPLEVEL, &constant_p_op },
761 { "__builtin_safe_p", &builtin_fn_type, MOD_TOPLEVEL, &safe_p_op },
762 { "__builtin_warning", &builtin_fn_type, MOD_TOPLEVEL, &warning_op },
763 { "__builtin_expect", &builtin_fn_type, MOD_TOPLEVEL, &expect_op },
764 { "__builtin_choose_expr", &builtin_fn_type, MOD_TOPLEVEL, &choose_op },
765 { NULL, NULL, 0 }
770 * Abstract types
772 struct symbol int_type,
773 fp_type;
776 * C types (i.e. actual instances that the abstract types
777 * can map onto)
779 struct symbol bool_ctype, void_ctype, type_ctype,
780 char_ctype, schar_ctype, uchar_ctype,
781 short_ctype, sshort_ctype, ushort_ctype,
782 int_ctype, sint_ctype, uint_ctype,
783 long_ctype, slong_ctype, ulong_ctype,
784 llong_ctype, sllong_ctype, ullong_ctype,
785 float_ctype, double_ctype, ldouble_ctype,
786 string_ctype, ptr_ctype, lazy_ptr_ctype,
787 incomplete_ctype, label_ctype, bad_ctype,
788 null_ctype;
790 struct symbol zero_int;
792 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
793 #define __IDENT(n,str,res) \
794 struct ident n = __INIT_IDENT(str,res)
796 #include "ident-list.h"
798 void init_symbols(void)
800 int stream = init_stream("builtin", -1, includepath);
801 struct sym_init *ptr;
803 #define __IDENT(n,str,res) \
804 hash_ident(&n)
805 #include "ident-list.h"
807 init_parser(stream);
808 for (ptr = symbol_init_table; ptr->name; ptr++) {
809 struct symbol *sym;
810 sym = create_symbol(stream, ptr->name, SYM_NODE, NS_TYPEDEF);
811 sym->ident->reserved = 1;
812 sym->ctype.base_type = ptr->base_type;
813 sym->ctype.modifiers = ptr->modifiers;
816 builtin_fn_type.variadic = 1;
817 for (ptr = eval_init_table; ptr->name; ptr++) {
818 struct symbol *sym;
819 sym = create_symbol(stream, ptr->name, SYM_NODE, NS_SYMBOL);
820 sym->ctype.base_type = ptr->base_type;
821 sym->ctype.modifiers = ptr->modifiers;
822 sym->op = ptr->op;
826 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
827 #define MOD_LL (MOD_LONG | MOD_LONGLONG)
828 static const struct ctype_declare {
829 struct symbol *ptr;
830 enum type type;
831 unsigned long modifiers;
832 int *bit_size;
833 int *maxalign;
834 struct symbol *base_type;
835 } ctype_declaration[] = {
836 { &bool_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_bool, &max_int_alignment, &int_type },
837 { &void_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
838 { &type_ctype, SYM_BASETYPE, MOD_TYPE, NULL, NULL, NULL },
839 { &incomplete_ctype,SYM_BASETYPE, 0, NULL, NULL, NULL },
840 { &bad_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
842 { &char_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
843 { &schar_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
844 { &uchar_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
845 { &short_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
846 { &sshort_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
847 { &ushort_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
848 { &int_ctype, SYM_BASETYPE, MOD_SIGNED, &bits_in_int, &max_int_alignment, &int_type },
849 { &sint_ctype, SYM_BASETYPE, MOD_ESIGNED, &bits_in_int, &max_int_alignment, &int_type },
850 { &uint_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_int, &max_int_alignment, &int_type },
851 { &long_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
852 { &slong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
853 { &ulong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
854 { &llong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
855 { &sllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
856 { &ullong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
858 { &float_ctype, SYM_BASETYPE, 0, &bits_in_float, &max_fp_alignment, &fp_type },
859 { &double_ctype, SYM_BASETYPE, MOD_LONG, &bits_in_double, &max_fp_alignment, &fp_type },
860 { &ldouble_ctype, SYM_BASETYPE, MOD_LONG | MOD_LONGLONG, &bits_in_longdouble, &max_fp_alignment, &fp_type },
862 { &string_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &char_ctype },
863 { &ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
864 { &null_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
865 { &label_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
866 { &lazy_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
867 { NULL, }
869 #undef MOD_LL
870 #undef MOD_ESIGNED
872 void init_ctype(void)
874 const struct ctype_declare *ctype;
876 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
877 struct symbol *sym = ctype->ptr;
878 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
879 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
880 unsigned long alignment = (bit_size + 7) >> 3;
882 if (alignment > maxalign)
883 alignment = maxalign;
884 sym->type = ctype->type;
885 sym->bit_size = bit_size;
886 sym->ctype.alignment = alignment;
887 sym->ctype.base_type = ctype->base_type;
888 sym->ctype.modifiers = ctype->modifiers;