[PATCH] integer_promotions() can't get SYM_NODE or SYM_ENUM
[smatch.git] / symbol.c
blob9c105c2e66a26fb947e8d084d26e0c3fa48f56fd
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 = sym->ctype.base_type;
198 if (base_type) {
199 base_type = examine_symbol_type(base_type);
201 /* "typeof" can cause this */
202 if (base_type && base_type->type == SYM_NODE)
203 merge_type(sym, base_type);
205 return base_type;
208 static struct symbol * examine_array_type(struct symbol *sym)
210 struct symbol *base_type = examine_base_type(sym);
211 unsigned long bit_size, alignment;
213 if (!base_type)
214 return sym;
215 bit_size = base_type->bit_size * get_expression_value(sym->array_size);
216 if (!sym->array_size || sym->array_size->type != EXPR_VALUE)
217 bit_size = -1;
218 alignment = base_type->ctype.alignment;
219 if (!sym->ctype.alignment)
220 sym->ctype.alignment = alignment;
221 sym->bit_size = bit_size;
222 return sym;
225 static struct symbol *examine_bitfield_type(struct symbol *sym)
227 struct symbol *base_type = examine_base_type(sym);
228 unsigned long bit_size, alignment, modifiers;
230 if (!base_type)
231 return sym;
232 bit_size = base_type->bit_size;
233 if (sym->bit_size > bit_size)
234 warning(sym->pos, "impossible field-width, %d, for this type", sym->bit_size);
236 alignment = base_type->ctype.alignment;
237 if (!sym->ctype.alignment)
238 sym->ctype.alignment = alignment;
239 modifiers = base_type->ctype.modifiers;
241 /* Bitfields are unsigned, unless the base type was explicitly signed */
242 if (!(modifiers & MOD_EXPLICITLY_SIGNED))
243 modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
244 sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
245 return sym;
249 * "typeof" will have to merge the types together
251 void merge_type(struct symbol *sym, struct symbol *base_type)
253 sym->ctype.as |= base_type->ctype.as;
254 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
255 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
256 (struct ptr_list **)&sym->ctype.contexts);
257 sym->ctype.base_type = base_type->ctype.base_type;
258 if (sym->ctype.base_type->type == SYM_NODE)
259 merge_type(sym, sym->ctype.base_type);
262 static int count_array_initializer(struct symbol *t, struct expression *expr)
264 int nr = 0;
265 int is_char = 0;
268 * Arrays of character types are special; they can be initialized by
269 * string literal _or_ by string literal in braces. The latter means
270 * that with T x[] = {<string literal>} number of elements in x depends
271 * on T - if it's a character type, we get the length of string literal
272 * (including NUL), otherwise we have one element here.
274 if (t->ctype.base_type == &int_type && t->ctype.modifiers & MOD_CHAR)
275 is_char = 1;
277 switch (expr->type) {
278 case EXPR_INITIALIZER: {
279 struct expression *entry;
280 int count = 0;
281 int str_len = 0;
282 FOR_EACH_PTR(expr->expr_list, entry) {
283 count++;
284 switch (entry->type) {
285 case EXPR_INDEX:
286 if (entry->idx_to >= nr)
287 nr = entry->idx_to+1;
288 break;
289 case EXPR_STRING:
290 if (is_char)
291 str_len = entry->string->length;
292 default:
293 nr++;
295 } END_FOR_EACH_PTR(entry);
296 if (count == 1 && str_len)
297 nr = str_len;
298 break;
300 case EXPR_STRING:
301 if (is_char)
302 nr = expr->string->length;
303 default:
304 break;
306 return nr;
309 static struct symbol * examine_node_type(struct symbol *sym)
311 struct symbol *base_type = examine_base_type(sym);
312 int bit_size;
313 unsigned long alignment, modifiers;
315 /* SYM_NODE - figure out what the type of the node was.. */
316 modifiers = sym->ctype.modifiers;
318 bit_size = 0;
319 alignment = 0;
320 if (!base_type)
321 return sym;
323 bit_size = base_type->bit_size;
324 alignment = base_type->ctype.alignment;
326 /* Pick up signedness information into the node */
327 sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
329 if (!sym->ctype.alignment)
330 sym->ctype.alignment = alignment;
332 /* Unsized array? The size might come from the initializer.. */
333 if (bit_size < 0 && base_type->type == SYM_ARRAY && sym->initializer) {
334 struct symbol *node_type = base_type->ctype.base_type;
335 int count = count_array_initializer(node_type, sym->initializer);
337 if (node_type && node_type->bit_size >= 0)
338 bit_size = node_type->bit_size * count;
341 sym->bit_size = bit_size;
342 return sym;
345 static struct symbol *examine_enum_type(struct symbol *sym)
347 struct symbol *base_type = examine_base_type(sym);
349 sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
350 sym->bit_size = bits_in_enum;
351 if (base_type->bit_size > sym->bit_size)
352 sym->bit_size = base_type->bit_size;
353 sym->ctype.alignment = enum_alignment;
354 if (base_type->ctype.alignment > sym->ctype.alignment)
355 sym->ctype.alignment = base_type->ctype.alignment;
356 return sym;
359 static struct symbol *examine_pointer_type(struct symbol *sym)
362 * We need to set the pointer size first, and
363 * examine the thing we point to only afterwards.
364 * That's because this pointer type may end up
365 * being needed for the base type size evaluation.
367 if (!sym->bit_size)
368 sym->bit_size = bits_in_pointer;
369 if (!sym->ctype.alignment)
370 sym->ctype.alignment = pointer_alignment;
371 return sym;
375 * Fill in type size and alignment information for
376 * regular SYM_TYPE things.
378 struct symbol *examine_symbol_type(struct symbol * sym)
380 if (!sym)
381 return sym;
383 /* Already done? */
384 if (sym->examined)
385 return sym;
386 sym->examined = 1;
388 switch (sym->type) {
389 case SYM_FN:
390 case SYM_NODE:
391 return examine_node_type(sym);
392 case SYM_ARRAY:
393 return examine_array_type(sym);
394 case SYM_STRUCT:
395 return examine_struct_union_type(sym, 1);
396 case SYM_UNION:
397 return examine_struct_union_type(sym, 0);
398 case SYM_PTR:
399 return examine_pointer_type(sym);
400 case SYM_ENUM:
401 return examine_enum_type(sym);
402 case SYM_BITFIELD:
403 return examine_bitfield_type(sym);
404 case SYM_BASETYPE:
405 /* Size and alignment had better already be set up */
406 return sym;
407 case SYM_TYPEOF: {
408 struct symbol *base = evaluate_expression(sym->initializer);
409 if (base) {
410 if (is_bitfield_type(base))
411 warning(base->pos, "typeof applied to bitfield type");
412 if (base->type == SYM_NODE)
413 base = base->ctype.base_type;
414 switch (base->type) {
415 case SYM_RESTRICT:
416 case SYM_UNION:
417 case SYM_STRUCT:
418 sym->type = SYM_NODE;
419 sym->ctype.modifiers = 0;
420 sym->ctype.base_type = base;
421 return examine_node_type(sym);
423 *sym = *base;
424 break;
426 break;
428 case SYM_PREPROCESSOR:
429 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
430 return NULL;
431 case SYM_UNINITIALIZED:
432 sparse_error(sym->pos, "ctype on uninitialized symbol %p", sym);
433 return NULL;
434 case SYM_RESTRICT:
435 examine_base_type(sym);
436 return sym;
437 case SYM_FOULED:
438 examine_base_type(sym);
439 return sym;
440 default:
441 sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
442 break;
444 return sym;
447 const char* get_type_name(enum type type)
449 const char *type_lookup[] = {
450 [SYM_UNINITIALIZED] = "uninitialized",
451 [SYM_PREPROCESSOR] = "preprocessor",
452 [SYM_BASETYPE] = "basetype",
453 [SYM_NODE] = "node",
454 [SYM_PTR] = "pointer",
455 [SYM_FN] = "function",
456 [SYM_ARRAY] = "array",
457 [SYM_STRUCT] = "struct",
458 [SYM_UNION] = "union",
459 [SYM_ENUM] = "enum",
460 [SYM_TYPEDEF] = "typedef",
461 [SYM_TYPEOF] = "typeof",
462 [SYM_MEMBER] = "member",
463 [SYM_BITFIELD] = "bitfield",
464 [SYM_LABEL] = "label",
465 [SYM_RESTRICT] = "restrict",
466 [SYM_FOULED] = "fouled",
467 [SYM_KEYWORD] = "keyword",
468 [SYM_BAD] = "bad"};
470 if (type <= SYM_BAD)
471 return type_lookup[type];
472 else
473 return NULL;
476 static struct symbol_list *restr, *fouled;
478 void create_fouled(struct symbol *type)
480 if (type->bit_size < bits_in_int) {
481 struct symbol *new = alloc_symbol(type->pos, type->type);
482 *new = *type;
483 new->bit_size = bits_in_int;
484 new->type = SYM_FOULED;
485 new->ctype.base_type = type;
486 add_symbol(&restr, type);
487 add_symbol(&fouled, new);
491 struct symbol *befoul(struct symbol *type)
493 struct symbol *t1, *t2;
494 while (type->type == SYM_NODE)
495 type = type->ctype.base_type;
496 PREPARE_PTR_LIST(restr, t1);
497 PREPARE_PTR_LIST(fouled, t2);
498 for (;;) {
499 if (t1 == type)
500 return t2;
501 if (!t1)
502 break;
503 NEXT_PTR_LIST(t1);
504 NEXT_PTR_LIST(t2);
506 FINISH_PTR_LIST(t2);
507 FINISH_PTR_LIST(t1);
508 return NULL;
511 void check_declaration(struct symbol *sym)
513 int warned = 0;
514 struct symbol *next = sym;
516 while ((next = next->next_id) != NULL) {
517 if (next->namespace != sym->namespace)
518 continue;
519 if (sym->scope == next->scope) {
520 sym->same_symbol = next;
521 return;
523 if (sym->ctype.modifiers & next->ctype.modifiers & MOD_EXTERN) {
524 sym->same_symbol = next;
525 return;
528 if (!Wshadow || warned)
529 continue;
530 if (get_sym_type(next) == SYM_FN)
531 continue;
532 warned = 1;
533 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
534 info(next->pos, "originally declared here");
538 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
540 struct scope *scope;
541 if (sym->id_list) {
542 sparse_error(sym->pos, "internal error: symbol type already bound");
543 return;
545 if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
546 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
547 return;
549 sym->namespace = ns;
550 sym->next_id = ident->symbols;
551 ident->symbols = sym;
552 sym->id_list = &ident->symbols;
553 if (sym->ident && sym->ident != ident)
554 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
555 sym->ident = ident;
557 scope = block_scope;
558 if (ns == NS_SYMBOL && toplevel(scope)) {
559 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
560 scope = global_scope;
561 if (sym->ctype.modifiers & MOD_STATIC) {
562 scope = file_scope;
563 mod = MOD_TOPLEVEL;
565 sym->ctype.modifiers |= mod;
567 if (ns == NS_MACRO)
568 scope = file_scope;
569 if (ns == NS_LABEL)
570 scope = function_scope;
571 bind_scope(sym, scope);
574 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
576 struct token *token = built_in_token(stream, name);
577 struct symbol *sym = alloc_symbol(token->pos, type);
579 bind_symbol(sym, token->ident, namespace);
580 return sym;
583 static int evaluate_to_integer(struct expression *expr)
585 expr->ctype = &int_ctype;
586 return 1;
589 static int evaluate_expect(struct expression *expr)
591 /* Should we evaluate it to return the type of the first argument? */
592 expr->ctype = &int_ctype;
593 return 1;
596 static int arguments_choose(struct expression *expr)
598 struct expression_list *arglist = expr->args;
599 struct expression *arg;
600 int i = 0;
602 FOR_EACH_PTR (arglist, arg) {
603 if (!evaluate_expression(arg))
604 return 0;
605 i++;
606 } END_FOR_EACH_PTR(arg);
607 if (i < 3) {
608 sparse_error(expr->pos,
609 "not enough arguments for __builtin_choose_expr");
610 return 0;
611 } if (i > 3) {
612 sparse_error(expr->pos,
613 "too many arguments for __builtin_choose_expr");
614 return 0;
616 return 1;
619 static int evaluate_choose(struct expression *expr)
621 struct expression_list *list = expr->args;
622 struct expression *arg, *args[3];
623 int n = 0;
625 /* there will be exactly 3; we'd already verified that */
626 FOR_EACH_PTR(list, arg) {
627 args[n++] = arg;
628 } END_FOR_EACH_PTR(arg);
630 *expr = get_expression_value(args[0]) ? *args[1] : *args[2];
632 return 1;
635 static int expand_expect(struct expression *expr, int cost)
637 struct expression *arg = first_ptr_list((struct ptr_list *) expr->args);
639 if (arg)
640 *expr = *arg;
641 return 0;
645 * __builtin_warning() has type "int" and always returns 1,
646 * so that you can use it in conditionals or whatever
648 static int expand_warning(struct expression *expr, int cost)
650 struct expression *arg;
651 struct expression_list *arglist = expr->args;
653 FOR_EACH_PTR (arglist, arg) {
655 * Constant strings get printed out as a warning. By the
656 * time we get here, the EXPR_STRING has been fully
657 * evaluated, so by now it's an anonymous symbol with a
658 * string initializer.
660 * Just for the heck of it, allow any constant string
661 * symbol.
663 if (arg->type == EXPR_SYMBOL) {
664 struct symbol *sym = arg->symbol;
665 if (sym->initializer && sym->initializer->type == EXPR_STRING) {
666 struct string *string = sym->initializer->string;
667 warning(expr->pos, "%*s", string->length-1, string->data);
669 continue;
673 * Any other argument is a conditional. If it's
674 * non-constant, or it is false, we exit and do
675 * not print any warning.
677 if (arg->type != EXPR_VALUE)
678 goto out;
679 if (!arg->value)
680 goto out;
681 } END_FOR_EACH_PTR(arg);
682 out:
683 expr->type = EXPR_VALUE;
684 expr->value = 1;
685 expr->taint = 0;
686 return 0;
690 * Type and storage class keywords need to have the symbols
691 * created for them, so that the parser can have enough semantic
692 * information to do parsing.
694 * "double" == "long float", "long double" == "long long float"
696 static struct sym_init {
697 const char *name;
698 struct symbol *base_type;
699 unsigned int modifiers;
700 struct symbol_op *op;
701 } symbol_init_table[] = {
702 /* Storage class */
703 { "auto", NULL, MOD_AUTO },
704 { "register", NULL, MOD_REGISTER },
705 { "static", NULL, MOD_STATIC },
706 { "extern", NULL, MOD_EXTERN },
708 /* Type specifiers */
709 { "void", &void_ctype, 0 },
710 { "char", NULL, MOD_CHAR },
711 { "short", NULL, MOD_SHORT },
712 { "int", &int_type, 0 },
713 { "long", NULL, MOD_LONG },
714 { "float", &fp_type, 0 },
715 { "double", &fp_type, MOD_LONG },
716 { "signed", NULL, MOD_SIGNED | MOD_EXPLICITLY_SIGNED },
717 { "__signed", NULL, MOD_SIGNED | MOD_EXPLICITLY_SIGNED },
718 { "__signed__", NULL, MOD_SIGNED | MOD_EXPLICITLY_SIGNED },
719 { "unsigned", NULL, MOD_UNSIGNED },
720 { "__label__", &label_ctype, MOD_LABEL | MOD_UNSIGNED },
721 { "_Bool", &bool_ctype, MOD_UNSIGNED },
723 /* Predeclared types */
724 { "__builtin_va_list", &int_type, 0 },
726 { NULL, NULL, 0 }
729 static struct symbol_op constant_p_op = {
730 .evaluate = evaluate_to_integer,
731 .expand = expand_constant_p
734 static struct symbol_op safe_p_op = {
735 .evaluate = evaluate_to_integer,
736 .expand = expand_safe_p
739 static struct symbol_op warning_op = {
740 .evaluate = evaluate_to_integer,
741 .expand = expand_warning
744 static struct symbol_op expect_op = {
745 .evaluate = evaluate_expect,
746 .expand = expand_expect
749 static struct symbol_op choose_op = {
750 .evaluate = evaluate_choose,
751 .args = arguments_choose,
755 * Builtin functions
757 static struct symbol builtin_fn_type = { .type = SYM_FN /* , .variadic =1 */ };
758 static struct sym_init eval_init_table[] = {
759 { "__builtin_constant_p", &builtin_fn_type, MOD_TOPLEVEL, &constant_p_op },
760 { "__builtin_safe_p", &builtin_fn_type, MOD_TOPLEVEL, &safe_p_op },
761 { "__builtin_warning", &builtin_fn_type, MOD_TOPLEVEL, &warning_op },
762 { "__builtin_expect", &builtin_fn_type, MOD_TOPLEVEL, &expect_op },
763 { "__builtin_choose_expr", &builtin_fn_type, MOD_TOPLEVEL, &choose_op },
764 { NULL, NULL, 0 }
769 * Abstract types
771 struct symbol int_type,
772 fp_type;
775 * C types (i.e. actual instances that the abstract types
776 * can map onto)
778 struct symbol bool_ctype, void_ctype, type_ctype,
779 char_ctype, schar_ctype, uchar_ctype,
780 short_ctype, sshort_ctype, ushort_ctype,
781 int_ctype, sint_ctype, uint_ctype,
782 long_ctype, slong_ctype, ulong_ctype,
783 llong_ctype, sllong_ctype, ullong_ctype,
784 float_ctype, double_ctype, ldouble_ctype,
785 string_ctype, ptr_ctype, lazy_ptr_ctype,
786 incomplete_ctype, label_ctype, bad_ctype,
787 null_ctype;
789 struct symbol zero_int;
791 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
792 #define __IDENT(n,str,res) \
793 struct ident n = __INIT_IDENT(str,res)
795 #include "ident-list.h"
797 void init_symbols(void)
799 int stream = init_stream("builtin", -1, includepath);
800 struct sym_init *ptr;
802 #define __IDENT(n,str,res) \
803 hash_ident(&n)
804 #include "ident-list.h"
806 init_parser(stream);
807 for (ptr = symbol_init_table; ptr->name; ptr++) {
808 struct symbol *sym;
809 sym = create_symbol(stream, ptr->name, SYM_NODE, NS_TYPEDEF);
810 sym->ident->reserved = 1;
811 sym->ctype.base_type = ptr->base_type;
812 sym->ctype.modifiers = ptr->modifiers;
815 builtin_fn_type.variadic = 1;
816 for (ptr = eval_init_table; ptr->name; ptr++) {
817 struct symbol *sym;
818 sym = create_symbol(stream, ptr->name, SYM_NODE, NS_SYMBOL);
819 sym->ctype.base_type = ptr->base_type;
820 sym->ctype.modifiers = ptr->modifiers;
821 sym->op = ptr->op;
825 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
826 #define MOD_LL (MOD_LONG | MOD_LONGLONG)
827 static const struct ctype_declare {
828 struct symbol *ptr;
829 enum type type;
830 unsigned long modifiers;
831 int *bit_size;
832 int *maxalign;
833 struct symbol *base_type;
834 } ctype_declaration[] = {
835 { &bool_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_bool, &max_int_alignment, &int_type },
836 { &void_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
837 { &type_ctype, SYM_BASETYPE, MOD_TYPE, NULL, NULL, NULL },
838 { &incomplete_ctype,SYM_BASETYPE, 0, NULL, NULL, NULL },
839 { &bad_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
841 { &char_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
842 { &schar_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
843 { &uchar_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
844 { &short_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
845 { &sshort_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
846 { &ushort_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
847 { &int_ctype, SYM_BASETYPE, MOD_SIGNED, &bits_in_int, &max_int_alignment, &int_type },
848 { &sint_ctype, SYM_BASETYPE, MOD_ESIGNED, &bits_in_int, &max_int_alignment, &int_type },
849 { &uint_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_int, &max_int_alignment, &int_type },
850 { &long_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
851 { &slong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
852 { &ulong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
853 { &llong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
854 { &sllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
855 { &ullong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
857 { &float_ctype, SYM_BASETYPE, 0, &bits_in_float, &max_fp_alignment, &fp_type },
858 { &double_ctype, SYM_BASETYPE, MOD_LONG, &bits_in_double, &max_fp_alignment, &fp_type },
859 { &ldouble_ctype, SYM_BASETYPE, MOD_LONG | MOD_LONGLONG, &bits_in_longdouble, &max_fp_alignment, &fp_type },
861 { &string_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &char_ctype },
862 { &ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
863 { &null_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
864 { &label_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
865 { &lazy_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
866 { NULL, }
868 #undef MOD_LL
869 #undef MOD_ESIGNED
871 void init_ctype(void)
873 const struct ctype_declare *ctype;
875 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
876 struct symbol *sym = ctype->ptr;
877 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
878 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
879 unsigned long alignment = (bit_size + 7) >> 3;
881 if (alignment > maxalign)
882 alignment = maxalign;
883 sym->type = ctype->type;
884 sym->bit_size = bit_size;
885 sym->ctype.alignment = alignment;
886 sym->ctype.base_type = ctype->base_type;
887 sym->ctype.modifiers = ctype->modifiers;