fix 'weak' attribute loss
[smatch.git] / symbol.c
blob1a3f59480b9c94b1d97c245283ec5f8a72fdab8c
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 return sym;
68 struct struct_union_info {
69 unsigned long max_align;
70 unsigned long bit_size;
71 int align_size;
75 * Unions are fairly easy to lay out ;)
77 static void lay_out_union(struct symbol *sym, struct struct_union_info *info)
79 examine_symbol_type(sym);
81 // Unnamed bitfields do not affect alignment.
82 if (sym->ident || !is_bitfield_type(sym)) {
83 if (sym->ctype.alignment > info->max_align)
84 info->max_align = sym->ctype.alignment;
87 if (sym->bit_size > info->bit_size)
88 info->bit_size = sym->bit_size;
90 sym->offset = 0;
93 static int bitfield_base_size(struct symbol *sym)
95 if (sym->type == SYM_NODE)
96 sym = sym->ctype.base_type;
97 if (sym->type == SYM_BITFIELD)
98 sym = sym->ctype.base_type;
99 return sym->bit_size;
103 * Structures are a bit more interesting to lay out
105 static void lay_out_struct(struct symbol *sym, struct struct_union_info *info)
107 unsigned long bit_size, align_bit_mask;
108 int base_size;
110 examine_symbol_type(sym);
112 // Unnamed bitfields do not affect alignment.
113 if (sym->ident || !is_bitfield_type(sym)) {
114 if (sym->ctype.alignment > info->max_align)
115 info->max_align = sym->ctype.alignment;
118 bit_size = info->bit_size;
119 base_size = sym->bit_size;
122 * Unsized arrays cause us to not align the resulting
123 * structure size
125 if (base_size < 0) {
126 info->align_size = 0;
127 base_size = 0;
130 align_bit_mask = (sym->ctype.alignment << 3) - 1;
133 * Bitfields have some very special rules..
135 if (is_bitfield_type (sym)) {
136 unsigned long bit_offset = bit_size & align_bit_mask;
137 int room = bitfield_base_size(sym) - bit_offset;
138 // Zero-width fields just fill up the unit.
139 int width = base_size ? : (bit_offset ? room : 0);
141 if (width > room) {
142 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
143 bit_offset = 0;
145 sym->offset = (bit_size - bit_offset) >> 3;
146 sym->bit_offset = bit_offset;
147 sym->ctype.base_type->bit_offset = bit_offset;
148 info->bit_size = bit_size + width;
149 // warning (sym->pos, "bitfield: offset=%d:%d size=:%d", sym->offset, sym->bit_offset, width);
151 return;
155 * Otherwise, just align it right and add it up..
157 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
158 sym->offset = bit_size >> 3;
160 info->bit_size = bit_size + base_size;
161 // warning (sym->pos, "regular: offset=%d", sym->offset);
164 static struct symbol * examine_struct_union_type(struct symbol *sym, int advance)
166 struct struct_union_info info = {
167 .max_align = 1,
168 .bit_size = 0,
169 .align_size = 1
171 unsigned long bit_size, bit_align;
172 void (*fn)(struct symbol *, struct struct_union_info *);
173 struct symbol *member;
175 fn = advance ? lay_out_struct : lay_out_union;
176 FOR_EACH_PTR(sym->symbol_list, member) {
177 fn(member, &info);
178 } END_FOR_EACH_PTR(member);
180 if (!sym->ctype.alignment)
181 sym->ctype.alignment = info.max_align;
182 bit_size = info.bit_size;
183 if (info.align_size) {
184 bit_align = (sym->ctype.alignment << 3)-1;
185 bit_size = (bit_size + bit_align) & ~bit_align;
187 sym->bit_size = bit_size;
188 return sym;
191 static struct symbol *examine_base_type(struct symbol *sym)
193 struct symbol *base_type;
195 /* Check the basetype */
196 base_type = sym->ctype.base_type;
197 if (base_type) {
198 base_type = examine_symbol_type(base_type);
200 /* "typeof" can cause this */
201 if (base_type && base_type->type == SYM_NODE)
202 merge_type(sym, base_type);
204 return base_type;
207 static struct symbol * examine_array_type(struct symbol *sym)
209 struct symbol *base_type = examine_base_type(sym);
210 unsigned long bit_size, alignment;
212 if (!base_type)
213 return sym;
214 bit_size = base_type->bit_size * get_expression_value(sym->array_size);
215 if (!sym->array_size || sym->array_size->type != EXPR_VALUE)
216 bit_size = -1;
217 alignment = base_type->ctype.alignment;
218 if (!sym->ctype.alignment)
219 sym->ctype.alignment = alignment;
220 sym->bit_size = bit_size;
221 return sym;
224 static struct symbol *examine_bitfield_type(struct symbol *sym)
226 struct symbol *base_type = examine_base_type(sym);
227 unsigned long bit_size, alignment, modifiers;
229 if (!base_type)
230 return sym;
231 bit_size = base_type->bit_size;
232 if (sym->bit_size > bit_size)
233 warning(sym->pos, "impossible field-width, %d, for this type", sym->bit_size);
235 alignment = base_type->ctype.alignment;
236 if (!sym->ctype.alignment)
237 sym->ctype.alignment = alignment;
238 modifiers = base_type->ctype.modifiers;
240 /* Bitfields are unsigned, unless the base type was explicitly signed */
241 if (!(modifiers & MOD_EXPLICITLY_SIGNED))
242 modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
243 sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
244 return sym;
248 * "typeof" will have to merge the types together
250 void merge_type(struct symbol *sym, struct symbol *base_type)
252 sym->ctype.as |= base_type->ctype.as;
253 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
254 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
255 (struct ptr_list **)&sym->ctype.contexts);
256 sym->ctype.base_type = base_type->ctype.base_type;
259 static int count_array_initializer(struct symbol *t, struct expression *expr)
261 int nr = 0;
262 int is_char = 0;
265 * Arrays of character types are special; they can be initialized by
266 * string literal _or_ by string literal in braces. The latter means
267 * that with T x[] = {<string literal>} number of elements in x depends
268 * on T - if it's a character type, we get the length of string literal
269 * (including NUL), otherwise we have one element here.
271 if (t->ctype.base_type == &int_type && t->ctype.modifiers & MOD_CHAR)
272 is_char = 1;
274 switch (expr->type) {
275 case EXPR_INITIALIZER: {
276 struct expression *entry;
277 int count = 0;
278 int str_len = 0;
279 FOR_EACH_PTR(expr->expr_list, entry) {
280 count++;
281 switch (entry->type) {
282 case EXPR_INDEX:
283 if (entry->idx_to >= nr)
284 nr = entry->idx_to+1;
285 break;
286 case EXPR_STRING:
287 if (is_char)
288 str_len = entry->string->length;
289 default:
290 nr++;
292 } END_FOR_EACH_PTR(entry);
293 if (count == 1 && str_len)
294 nr = str_len;
295 break;
297 case EXPR_STRING:
298 if (is_char)
299 nr = expr->string->length;
300 default:
301 break;
303 return nr;
306 static struct symbol * examine_node_type(struct symbol *sym)
308 struct symbol *base_type = examine_base_type(sym);
309 int bit_size;
310 unsigned long alignment, modifiers;
312 /* SYM_NODE - figure out what the type of the node was.. */
313 modifiers = sym->ctype.modifiers;
315 bit_size = 0;
316 alignment = 0;
317 if (!base_type)
318 return sym;
320 bit_size = base_type->bit_size;
321 alignment = base_type->ctype.alignment;
323 /* Pick up signedness information into the node */
324 sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
326 if (!sym->ctype.alignment)
327 sym->ctype.alignment = alignment;
329 /* Unsized array? The size might come from the initializer.. */
330 if (bit_size < 0 && base_type->type == SYM_ARRAY && sym->initializer) {
331 struct symbol *node_type = base_type->ctype.base_type;
332 int count = count_array_initializer(node_type, sym->initializer);
334 if (node_type && node_type->bit_size >= 0)
335 bit_size = node_type->bit_size * count;
338 sym->bit_size = bit_size;
339 return sym;
342 static struct symbol *examine_enum_type(struct symbol *sym)
344 struct symbol *base_type = examine_base_type(sym);
346 sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
347 sym->bit_size = bits_in_enum;
348 if (base_type->bit_size > sym->bit_size)
349 sym->bit_size = base_type->bit_size;
350 sym->ctype.alignment = enum_alignment;
351 if (base_type->ctype.alignment > sym->ctype.alignment)
352 sym->ctype.alignment = base_type->ctype.alignment;
353 return sym;
356 static struct symbol *examine_pointer_type(struct symbol *sym)
359 * We need to set the pointer size first, and
360 * examine the thing we point to only afterwards.
361 * That's because this pointer type may end up
362 * being needed for the base type size evaluation.
364 if (!sym->bit_size)
365 sym->bit_size = bits_in_pointer;
366 if (!sym->ctype.alignment)
367 sym->ctype.alignment = pointer_alignment;
368 return sym;
372 * Fill in type size and alignment information for
373 * regular SYM_TYPE things.
375 struct symbol *examine_symbol_type(struct symbol * sym)
377 if (!sym)
378 return sym;
380 /* Already done? */
381 if (sym->examined)
382 return sym;
383 sym->examined = 1;
385 switch (sym->type) {
386 case SYM_FN:
387 case SYM_NODE:
388 return examine_node_type(sym);
389 case SYM_ARRAY:
390 return examine_array_type(sym);
391 case SYM_STRUCT:
392 return examine_struct_union_type(sym, 1);
393 case SYM_UNION:
394 return examine_struct_union_type(sym, 0);
395 case SYM_PTR:
396 return examine_pointer_type(sym);
397 case SYM_ENUM:
398 return examine_enum_type(sym);
399 case SYM_BITFIELD:
400 return examine_bitfield_type(sym);
401 case SYM_BASETYPE:
402 /* Size and alignment had better already be set up */
403 return sym;
404 case SYM_TYPEOF: {
405 struct symbol *base = evaluate_expression(sym->initializer);
406 if (base) {
407 if (is_bitfield_type(base))
408 warning(base->pos, "typeof applied to bitfield type");
409 if (base->type == SYM_NODE)
410 base = base->ctype.base_type;
411 *sym = *base;
412 break;
414 break;
416 case SYM_PREPROCESSOR:
417 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
418 return NULL;
419 case SYM_UNINITIALIZED:
420 sparse_error(sym->pos, "ctype on uninitialized symbol %p", sym);
421 return NULL;
422 case SYM_RESTRICT:
423 examine_base_type(sym);
424 return sym;
425 case SYM_FOULED:
426 examine_base_type(sym);
427 return sym;
428 default:
429 sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
430 break;
432 return sym;
435 static struct symbol_list *restr, *fouled;
437 void create_fouled(struct symbol *type)
439 if (type->bit_size < bits_in_int) {
440 struct symbol *new = alloc_symbol(type->pos, type->type);
441 *new = *type;
442 new->bit_size = bits_in_int;
443 new->type = SYM_FOULED;
444 new->ctype.base_type = type;
445 add_symbol(&restr, type);
446 add_symbol(&fouled, new);
450 struct symbol *befoul(struct symbol *type)
452 struct symbol *t1, *t2;
453 while (type->type == SYM_NODE)
454 type = type->ctype.base_type;
455 PREPARE_PTR_LIST(restr, t1);
456 PREPARE_PTR_LIST(fouled, t2);
457 for (;;) {
458 if (t1 == type)
459 return t2;
460 if (!t1)
461 break;
462 NEXT_PTR_LIST(t1);
463 NEXT_PTR_LIST(t2);
465 FINISH_PTR_LIST(t2);
466 FINISH_PTR_LIST(t1);
467 return NULL;
470 void check_declaration(struct symbol *sym)
472 int warned = 0;
473 struct symbol *next = sym;
475 while ((next = next->next_id) != NULL) {
476 if (next->namespace != sym->namespace)
477 continue;
478 if (sym->scope == next->scope) {
479 sym->same_symbol = next;
480 return;
482 if (sym->ctype.modifiers & next->ctype.modifiers & MOD_EXTERN) {
483 sym->same_symbol = next;
484 return;
487 if (!Wshadow || warned)
488 continue;
489 if (get_sym_type(next) == SYM_FN)
490 continue;
491 warned = 1;
492 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
493 info(next->pos, "originally declared here");
497 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
499 struct scope *scope;
500 if (sym->id_list) {
501 sparse_error(sym->pos, "internal error: symbol type already bound");
502 return;
504 if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
505 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
506 return;
508 sym->namespace = ns;
509 sym->next_id = ident->symbols;
510 ident->symbols = sym;
511 sym->id_list = &ident->symbols;
512 if (sym->ident && sym->ident != ident)
513 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
514 sym->ident = ident;
516 scope = block_scope;
517 if (ns == NS_SYMBOL && toplevel(scope)) {
518 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
519 scope = global_scope;
520 if (sym->ctype.modifiers & MOD_STATIC) {
521 scope = file_scope;
522 mod = MOD_TOPLEVEL;
524 sym->ctype.modifiers |= mod;
526 if (ns == NS_MACRO)
527 scope = file_scope;
528 if (ns == NS_LABEL)
529 scope = function_scope;
530 bind_scope(sym, scope);
533 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
535 struct token *token = built_in_token(stream, name);
536 struct symbol *sym = alloc_symbol(token->pos, type);
538 bind_symbol(sym, token->ident, namespace);
539 return sym;
542 static int evaluate_to_integer(struct expression *expr)
544 expr->ctype = &int_ctype;
545 return 1;
548 static int evaluate_expect(struct expression *expr)
550 /* Should we evaluate it to return the type of the first argument? */
551 expr->ctype = &int_ctype;
552 return 1;
555 static int arguments_choose(struct expression *expr)
557 struct expression_list *arglist = expr->args;
558 struct expression *arg;
559 int i = 0;
561 FOR_EACH_PTR (arglist, arg) {
562 if (!evaluate_expression(arg))
563 return 0;
564 i++;
565 } END_FOR_EACH_PTR(arg);
566 if (i < 3) {
567 sparse_error(expr->pos,
568 "not enough arguments for __builtin_choose_expr");
569 return 0;
570 } if (i > 3) {
571 sparse_error(expr->pos,
572 "too many arguments for __builtin_choose_expr");
573 return 0;
575 return 1;
578 static int evaluate_choose(struct expression *expr)
580 struct expression_list *list = expr->args;
581 struct expression *arg, *args[3];
582 int n = 0;
584 /* there will be exactly 3; we'd already verified that */
585 FOR_EACH_PTR(list, arg) {
586 args[n++] = arg;
587 } END_FOR_EACH_PTR(arg);
589 *expr = get_expression_value(args[0]) ? *args[1] : *args[2];
591 return 1;
594 static int expand_expect(struct expression *expr, int cost)
596 struct expression *arg = first_ptr_list((struct ptr_list *) expr->args);
598 if (arg)
599 *expr = *arg;
600 return 0;
604 * __builtin_warning() has type "int" and always returns 1,
605 * so that you can use it in conditionals or whatever
607 static int expand_warning(struct expression *expr, int cost)
609 struct expression *arg;
610 struct expression_list *arglist = expr->args;
612 FOR_EACH_PTR (arglist, arg) {
614 * Constant strings get printed out as a warning. By the
615 * time we get here, the EXPR_STRING has been fully
616 * evaluated, so by now it's an anonymous symbol with a
617 * string initializer.
619 * Just for the heck of it, allow any constant string
620 * symbol.
622 if (arg->type == EXPR_SYMBOL) {
623 struct symbol *sym = arg->symbol;
624 if (sym->initializer && sym->initializer->type == EXPR_STRING) {
625 struct string *string = sym->initializer->string;
626 warning(expr->pos, "%*s", string->length-1, string->data);
628 continue;
632 * Any other argument is a conditional. If it's
633 * non-constant, or it is false, we exit and do
634 * not print any warning.
636 if (arg->type != EXPR_VALUE)
637 goto out;
638 if (!arg->value)
639 goto out;
640 } END_FOR_EACH_PTR(arg);
641 out:
642 expr->type = EXPR_VALUE;
643 expr->value = 1;
644 return 0;
648 * Type and storage class keywords need to have the symbols
649 * created for them, so that the parser can have enough semantic
650 * information to do parsing.
652 * "double" == "long float", "long double" == "long long float"
654 static struct sym_init {
655 const char *name;
656 struct symbol *base_type;
657 unsigned int modifiers;
658 struct symbol_op *op;
659 } symbol_init_table[] = {
660 /* Storage class */
661 { "auto", NULL, MOD_AUTO },
662 { "register", NULL, MOD_REGISTER },
663 { "static", NULL, MOD_STATIC },
664 { "extern", NULL, MOD_EXTERN },
666 /* Type specifiers */
667 { "void", &void_ctype, 0 },
668 { "char", NULL, MOD_CHAR },
669 { "short", NULL, MOD_SHORT },
670 { "int", &int_type, 0 },
671 { "long", NULL, MOD_LONG },
672 { "float", &fp_type, 0 },
673 { "double", &fp_type, MOD_LONG },
674 { "signed", NULL, MOD_SIGNED | MOD_EXPLICITLY_SIGNED },
675 { "__signed", NULL, MOD_SIGNED | MOD_EXPLICITLY_SIGNED },
676 { "__signed__", NULL, MOD_SIGNED | MOD_EXPLICITLY_SIGNED },
677 { "unsigned", NULL, MOD_UNSIGNED },
678 { "__label__", &label_ctype, MOD_LABEL | MOD_UNSIGNED },
679 { "_Bool", &bool_ctype, MOD_UNSIGNED },
681 /* Type qualifiers */
682 { "const", NULL, MOD_CONST },
683 { "__const", NULL, MOD_CONST },
684 { "__const__", NULL, MOD_CONST },
685 { "volatile", NULL, MOD_VOLATILE },
686 { "__volatile", NULL, MOD_VOLATILE },
687 { "__volatile__", NULL, MOD_VOLATILE },
689 /* Predeclared types */
690 { "__builtin_va_list", &int_type, 0 },
692 /* Typedef.. */
693 { "typedef", NULL, MOD_TYPEDEF },
695 /* Extended types */
696 { "typeof", NULL, MOD_TYPEOF },
697 { "__typeof", NULL, MOD_TYPEOF },
698 { "__typeof__", NULL, MOD_TYPEOF },
700 #if 0
701 { "attribute", NULL, MOD_ATTRIBUTE },
702 #endif
703 { "__attribute", NULL, MOD_ATTRIBUTE },
704 { "__attribute__", NULL, MOD_ATTRIBUTE },
706 { "struct", NULL, MOD_STRUCTOF },
707 { "union", NULL, MOD_UNIONOF },
708 { "enum", NULL, MOD_ENUMOF },
710 { "inline", NULL, MOD_INLINE },
711 { "__inline", NULL, MOD_INLINE },
712 { "__inline__", NULL, MOD_INLINE },
714 /* Ignored for now.. */
715 { "restrict", NULL, 0 },
716 { "__restrict", NULL, 0 },
718 { NULL, NULL, 0 }
721 static struct symbol_op constant_p_op = {
722 .evaluate = evaluate_to_integer,
723 .expand = expand_constant_p
726 static struct symbol_op safe_p_op = {
727 .evaluate = evaluate_to_integer,
728 .expand = expand_safe_p
731 static struct symbol_op warning_op = {
732 .evaluate = evaluate_to_integer,
733 .expand = expand_warning
736 static struct symbol_op expect_op = {
737 .evaluate = evaluate_expect,
738 .expand = expand_expect
741 static struct symbol_op choose_op = {
742 .evaluate = evaluate_choose,
743 .args = arguments_choose,
747 * Builtin functions
749 static struct symbol builtin_fn_type = { .type = SYM_FN /* , .variadic =1 */ };
750 static struct sym_init eval_init_table[] = {
751 { "__builtin_constant_p", &builtin_fn_type, MOD_TOPLEVEL, &constant_p_op },
752 { "__builtin_safe_p", &builtin_fn_type, MOD_TOPLEVEL, &safe_p_op },
753 { "__builtin_warning", &builtin_fn_type, MOD_TOPLEVEL, &warning_op },
754 { "__builtin_expect", &builtin_fn_type, MOD_TOPLEVEL, &expect_op },
755 { "__builtin_choose_expr", &builtin_fn_type, MOD_TOPLEVEL, &choose_op },
756 { NULL, NULL, 0 }
761 * Abstract types
763 struct symbol int_type,
764 fp_type;
767 * C types (ie actual instances that the abstract types
768 * can map onto)
770 struct symbol bool_ctype, void_ctype, type_ctype,
771 char_ctype, schar_ctype, uchar_ctype,
772 short_ctype, sshort_ctype, ushort_ctype,
773 int_ctype, sint_ctype, uint_ctype,
774 long_ctype, slong_ctype, ulong_ctype,
775 llong_ctype, sllong_ctype, ullong_ctype,
776 float_ctype, double_ctype, ldouble_ctype,
777 string_ctype, ptr_ctype, lazy_ptr_ctype,
778 incomplete_ctype, label_ctype, bad_ctype;
780 struct symbol zero_int;
782 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
783 #define __IDENT(n,str,res) \
784 struct ident n = __INIT_IDENT(str,res)
786 #include "ident-list.h"
788 void init_symbols(void)
790 int stream = init_stream("builtin", -1, includepath);
791 struct sym_init *ptr;
793 #define __IDENT(n,str,res) \
794 hash_ident(&n)
795 #include "ident-list.h"
797 for (ptr = symbol_init_table; ptr->name; ptr++) {
798 struct symbol *sym;
799 sym = create_symbol(stream, ptr->name, SYM_NODE, NS_TYPEDEF);
800 sym->ident->reserved = 1;
801 sym->ctype.base_type = ptr->base_type;
802 sym->ctype.modifiers = ptr->modifiers;
805 builtin_fn_type.variadic = 1;
806 for (ptr = eval_init_table; ptr->name; ptr++) {
807 struct symbol *sym;
808 sym = create_symbol(stream, ptr->name, SYM_NODE, NS_SYMBOL);
809 sym->ctype.base_type = ptr->base_type;
810 sym->ctype.modifiers = ptr->modifiers;
811 sym->op = ptr->op;
815 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
816 #define MOD_LL (MOD_LONG | MOD_LONGLONG)
817 static const struct ctype_declare {
818 struct symbol *ptr;
819 enum type type;
820 unsigned long modifiers;
821 int *bit_size;
822 int *maxalign;
823 struct symbol *base_type;
824 } ctype_declaration[] = {
825 { &bool_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_bool, &max_int_alignment, &int_type },
826 { &void_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
827 { &type_ctype, SYM_BASETYPE, MOD_TYPE, NULL, NULL, NULL },
828 { &incomplete_ctype,SYM_BASETYPE, 0, NULL, NULL, NULL },
829 { &bad_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
831 { &char_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
832 { &schar_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
833 { &uchar_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
834 { &short_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
835 { &sshort_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
836 { &ushort_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
837 { &int_ctype, SYM_BASETYPE, MOD_SIGNED, &bits_in_int, &max_int_alignment, &int_type },
838 { &sint_ctype, SYM_BASETYPE, MOD_ESIGNED, &bits_in_int, &max_int_alignment, &int_type },
839 { &uint_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_int, &max_int_alignment, &int_type },
840 { &long_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
841 { &slong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
842 { &ulong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
843 { &llong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
844 { &sllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
845 { &ullong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
847 { &float_ctype, SYM_BASETYPE, 0, &bits_in_float, &max_fp_alignment, &fp_type },
848 { &double_ctype, SYM_BASETYPE, MOD_LONG, &bits_in_double, &max_fp_alignment, &fp_type },
849 { &ldouble_ctype, SYM_BASETYPE, MOD_LONG | MOD_LONGLONG, &bits_in_longdouble, &max_fp_alignment, &fp_type },
851 { &string_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &char_ctype },
852 { &ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
853 { &label_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
854 { &lazy_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
855 { NULL, }
857 #undef MOD_LL
858 #undef MOD_ESIGNED
860 void init_ctype(void)
862 const struct ctype_declare *ctype;
864 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
865 struct symbol *sym = ctype->ptr;
866 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
867 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
868 unsigned long alignment = bit_size >> 3;
870 if (alignment > maxalign)
871 alignment = maxalign;
872 sym->type = ctype->type;
873 sym->bit_size = bit_size;
874 sym->ctype.alignment = alignment;
875 sym->ctype.base_type = ctype->base_type;
876 sym->ctype.modifiers = ctype->modifiers;