Merge branch 'llvmcore'
[smatch.git] / symbol.c
blob2ec620002513103bcc2498064ac9da4f15c6d051
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 NULL;
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 = bytes_to_bits(sym->ctype.alignment) - 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 = bits_to_bytes(bit_size - bit_offset);
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 = bits_to_bytes(bit_size);
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 = bytes_to_bits(sym->ctype.alignment)-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 = -1, alignment;
215 struct expression *array_size = sym->array_size;
217 if (!base_type)
218 return sym;
220 if (array_size) {
221 bit_size = base_type->bit_size * get_expression_value_silent(array_size);
222 if (array_size->type != EXPR_VALUE) {
223 if (Wvla)
224 warning(array_size->pos, "Variable length array is used.");
225 bit_size = -1;
228 alignment = base_type->ctype.alignment;
229 if (!sym->ctype.alignment)
230 sym->ctype.alignment = alignment;
231 sym->bit_size = bit_size;
232 return sym;
235 static struct symbol *examine_bitfield_type(struct symbol *sym)
237 struct symbol *base_type = examine_base_type(sym);
238 unsigned long bit_size, alignment, modifiers;
240 if (!base_type)
241 return sym;
242 bit_size = base_type->bit_size;
243 if (sym->bit_size > bit_size)
244 warning(sym->pos, "impossible field-width, %d, for this type", sym->bit_size);
246 alignment = base_type->ctype.alignment;
247 if (!sym->ctype.alignment)
248 sym->ctype.alignment = alignment;
249 modifiers = base_type->ctype.modifiers;
251 /* Bitfields are unsigned, unless the base type was explicitly signed */
252 if (!(modifiers & MOD_EXPLICITLY_SIGNED))
253 modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
254 sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
255 return sym;
259 * "typeof" will have to merge the types together
261 void merge_type(struct symbol *sym, struct symbol *base_type)
263 sym->ctype.as |= base_type->ctype.as;
264 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
265 concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
266 (struct ptr_list **)&sym->ctype.contexts);
267 sym->ctype.base_type = base_type->ctype.base_type;
268 if (sym->ctype.base_type->type == SYM_NODE)
269 merge_type(sym, sym->ctype.base_type);
272 static int count_array_initializer(struct symbol *t, struct expression *expr)
274 int nr = 0;
275 int is_char = 0;
278 * Arrays of character types are special; they can be initialized by
279 * string literal _or_ by string literal in braces. The latter means
280 * that with T x[] = {<string literal>} number of elements in x depends
281 * on T - if it's a character type, we get the length of string literal
282 * (including NUL), otherwise we have one element here.
284 if (t->ctype.base_type == &int_type && t->ctype.modifiers & MOD_CHAR)
285 is_char = 1;
287 switch (expr->type) {
288 case EXPR_INITIALIZER: {
289 struct expression *entry;
290 int count = 0;
291 int str_len = 0;
292 FOR_EACH_PTR(expr->expr_list, entry) {
293 count++;
294 switch (entry->type) {
295 case EXPR_INDEX:
296 if (entry->idx_to >= nr)
297 nr = entry->idx_to+1;
298 break;
299 case EXPR_PREOP: {
300 struct expression *e = entry;
301 if (is_char) {
302 while (e && e->type == EXPR_PREOP && e->op == '(')
303 e = e->unop;
304 if (e && e->type == EXPR_STRING) {
305 entry = e;
306 case EXPR_STRING:
307 if (is_char)
308 str_len = entry->string->length;
314 default:
315 nr++;
317 } END_FOR_EACH_PTR(entry);
318 if (count == 1 && str_len)
319 nr = str_len;
320 break;
322 case EXPR_PREOP:
323 if (is_char) {
324 struct expression *e = expr;
325 while (e && e->type == EXPR_PREOP && e->op == '(')
326 e = e->unop;
327 if (e && e->type == EXPR_STRING) {
328 expr = e;
329 case EXPR_STRING:
330 if (is_char)
331 nr = expr->string->length;
334 break;
335 default:
336 break;
338 return nr;
341 static struct symbol * examine_node_type(struct symbol *sym)
343 struct symbol *base_type = examine_base_type(sym);
344 int bit_size;
345 unsigned long alignment;
347 /* SYM_NODE - figure out what the type of the node was.. */
348 bit_size = 0;
349 alignment = 0;
350 if (!base_type)
351 return sym;
353 bit_size = base_type->bit_size;
354 alignment = base_type->ctype.alignment;
356 /* Pick up signedness information into the node */
357 sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
359 if (!sym->ctype.alignment)
360 sym->ctype.alignment = alignment;
362 /* Unsized array? The size might come from the initializer.. */
363 if (bit_size < 0 && base_type->type == SYM_ARRAY && sym->initializer) {
364 struct symbol *node_type = base_type->ctype.base_type;
365 int count = count_array_initializer(node_type, sym->initializer);
367 if (node_type && node_type->bit_size >= 0)
368 bit_size = node_type->bit_size * count;
371 sym->bit_size = bit_size;
372 return sym;
375 static struct symbol *examine_enum_type(struct symbol *sym)
377 struct symbol *base_type = examine_base_type(sym);
379 sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
380 sym->bit_size = bits_in_enum;
381 if (base_type->bit_size > sym->bit_size)
382 sym->bit_size = base_type->bit_size;
383 sym->ctype.alignment = enum_alignment;
384 if (base_type->ctype.alignment > sym->ctype.alignment)
385 sym->ctype.alignment = base_type->ctype.alignment;
386 return sym;
389 static struct symbol *examine_pointer_type(struct symbol *sym)
392 * We need to set the pointer size first, and
393 * examine the thing we point to only afterwards.
394 * That's because this pointer type may end up
395 * being needed for the base type size evaluation.
397 if (!sym->bit_size)
398 sym->bit_size = bits_in_pointer;
399 if (!sym->ctype.alignment)
400 sym->ctype.alignment = pointer_alignment;
401 return sym;
405 * Fill in type size and alignment information for
406 * regular SYM_TYPE things.
408 struct symbol *examine_symbol_type(struct symbol * sym)
410 if (!sym)
411 return sym;
413 /* Already done? */
414 if (sym->examined)
415 return sym;
416 sym->examined = 1;
418 switch (sym->type) {
419 case SYM_FN:
420 case SYM_NODE:
421 return examine_node_type(sym);
422 case SYM_ARRAY:
423 return examine_array_type(sym);
424 case SYM_STRUCT:
425 return examine_struct_union_type(sym, 1);
426 case SYM_UNION:
427 return examine_struct_union_type(sym, 0);
428 case SYM_PTR:
429 return examine_pointer_type(sym);
430 case SYM_ENUM:
431 return examine_enum_type(sym);
432 case SYM_BITFIELD:
433 return examine_bitfield_type(sym);
434 case SYM_BASETYPE:
435 /* Size and alignment had better already be set up */
436 return sym;
437 case SYM_TYPEOF: {
438 struct symbol *base = evaluate_expression(sym->initializer);
439 if (base) {
440 if (is_bitfield_type(base))
441 warning(base->pos, "typeof applied to bitfield type");
442 if (base->type == SYM_NODE)
443 base = base->ctype.base_type;
444 sym->type = SYM_NODE;
445 sym->ctype.modifiers = 0;
446 sym->ctype.base_type = base;
447 return examine_node_type(sym);
449 break;
451 case SYM_PREPROCESSOR:
452 sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
453 return NULL;
454 case SYM_UNINITIALIZED:
455 sparse_error(sym->pos, "ctype on uninitialized symbol %p", sym);
456 return NULL;
457 case SYM_RESTRICT:
458 examine_base_type(sym);
459 return sym;
460 case SYM_FOULED:
461 examine_base_type(sym);
462 return sym;
463 default:
464 sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
465 break;
467 return sym;
470 const char* get_type_name(enum type type)
472 const char *type_lookup[] = {
473 [SYM_UNINITIALIZED] = "uninitialized",
474 [SYM_PREPROCESSOR] = "preprocessor",
475 [SYM_BASETYPE] = "basetype",
476 [SYM_NODE] = "node",
477 [SYM_PTR] = "pointer",
478 [SYM_FN] = "function",
479 [SYM_ARRAY] = "array",
480 [SYM_STRUCT] = "struct",
481 [SYM_UNION] = "union",
482 [SYM_ENUM] = "enum",
483 [SYM_TYPEDEF] = "typedef",
484 [SYM_TYPEOF] = "typeof",
485 [SYM_MEMBER] = "member",
486 [SYM_BITFIELD] = "bitfield",
487 [SYM_LABEL] = "label",
488 [SYM_RESTRICT] = "restrict",
489 [SYM_FOULED] = "fouled",
490 [SYM_KEYWORD] = "keyword",
491 [SYM_BAD] = "bad"};
493 if (type <= SYM_BAD)
494 return type_lookup[type];
495 else
496 return NULL;
499 struct symbol *examine_pointer_target(struct symbol *sym)
501 return examine_base_type(sym);
504 static struct symbol_list *restr, *fouled;
506 void create_fouled(struct symbol *type)
508 if (type->bit_size < bits_in_int) {
509 struct symbol *new = alloc_symbol(type->pos, type->type);
510 *new = *type;
511 new->bit_size = bits_in_int;
512 new->type = SYM_FOULED;
513 new->ctype.base_type = type;
514 add_symbol(&restr, type);
515 add_symbol(&fouled, new);
519 struct symbol *befoul(struct symbol *type)
521 struct symbol *t1, *t2;
522 while (type->type == SYM_NODE)
523 type = type->ctype.base_type;
524 PREPARE_PTR_LIST(restr, t1);
525 PREPARE_PTR_LIST(fouled, t2);
526 for (;;) {
527 if (t1 == type)
528 return t2;
529 if (!t1)
530 break;
531 NEXT_PTR_LIST(t1);
532 NEXT_PTR_LIST(t2);
534 FINISH_PTR_LIST(t2);
535 FINISH_PTR_LIST(t1);
536 return NULL;
539 void check_declaration(struct symbol *sym)
541 int warned = 0;
542 struct symbol *next = sym;
544 while ((next = next->next_id) != NULL) {
545 if (next->namespace != sym->namespace)
546 continue;
547 if (sym->scope == next->scope) {
548 sym->same_symbol = next;
549 return;
551 if (sym->ctype.modifiers & next->ctype.modifiers & MOD_EXTERN) {
552 if ((sym->ctype.modifiers ^ next->ctype.modifiers) & MOD_INLINE)
553 continue;
554 sym->same_symbol = next;
555 return;
558 if (!Wshadow || warned)
559 continue;
560 if (get_sym_type(next) == SYM_FN)
561 continue;
562 warned = 1;
563 warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
564 info(next->pos, "originally declared here");
568 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
570 struct scope *scope;
571 if (sym->bound) {
572 sparse_error(sym->pos, "internal error: symbol type already bound");
573 return;
575 if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
576 sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
577 return;
579 sym->namespace = ns;
580 sym->next_id = ident->symbols;
581 ident->symbols = sym;
582 if (sym->ident && sym->ident != ident)
583 warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
584 sym->ident = ident;
585 sym->bound = 1;
587 scope = block_scope;
588 if (ns == NS_SYMBOL && toplevel(scope)) {
589 unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
591 scope = global_scope;
592 if (sym->ctype.modifiers & MOD_STATIC ||
593 is_extern_inline(sym)) {
594 scope = file_scope;
595 mod = MOD_TOPLEVEL;
597 sym->ctype.modifiers |= mod;
599 if (ns == NS_MACRO)
600 scope = file_scope;
601 if (ns == NS_LABEL)
602 scope = function_scope;
603 bind_scope(sym, scope);
606 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
608 struct token *token = built_in_token(stream, name);
609 struct symbol *sym = alloc_symbol(token->pos, type);
611 bind_symbol(sym, token->ident, namespace);
612 return sym;
615 static int evaluate_to_integer(struct expression *expr)
617 expr->ctype = &int_ctype;
618 return 1;
621 static int evaluate_expect(struct expression *expr)
623 /* Should we evaluate it to return the type of the first argument? */
624 expr->ctype = &int_ctype;
625 return 1;
628 static int arguments_choose(struct expression *expr)
630 struct expression_list *arglist = expr->args;
631 struct expression *arg;
632 int i = 0;
634 FOR_EACH_PTR (arglist, arg) {
635 if (!evaluate_expression(arg))
636 return 0;
637 i++;
638 } END_FOR_EACH_PTR(arg);
639 if (i < 3) {
640 sparse_error(expr->pos,
641 "not enough arguments for __builtin_choose_expr");
642 return 0;
643 } if (i > 3) {
644 sparse_error(expr->pos,
645 "too many arguments for __builtin_choose_expr");
646 return 0;
648 return 1;
651 static int evaluate_choose(struct expression *expr)
653 struct expression_list *list = expr->args;
654 struct expression *arg, *args[3];
655 int n = 0;
657 /* there will be exactly 3; we'd already verified that */
658 FOR_EACH_PTR(list, arg) {
659 args[n++] = arg;
660 } END_FOR_EACH_PTR(arg);
662 *expr = get_expression_value(args[0]) ? *args[1] : *args[2];
664 return 1;
667 static int expand_expect(struct expression *expr, int cost)
669 struct expression *arg = first_ptr_list((struct ptr_list *) expr->args);
671 if (arg)
672 *expr = *arg;
673 return 0;
677 * __builtin_warning() has type "int" and always returns 1,
678 * so that you can use it in conditionals or whatever
680 static int expand_warning(struct expression *expr, int cost)
682 struct expression *arg;
683 struct expression_list *arglist = expr->args;
685 FOR_EACH_PTR (arglist, arg) {
687 * Constant strings get printed out as a warning. By the
688 * time we get here, the EXPR_STRING has been fully
689 * evaluated, so by now it's an anonymous symbol with a
690 * string initializer.
692 * Just for the heck of it, allow any constant string
693 * symbol.
695 if (arg->type == EXPR_SYMBOL) {
696 struct symbol *sym = arg->symbol;
697 if (sym->initializer && sym->initializer->type == EXPR_STRING) {
698 struct string *string = sym->initializer->string;
699 warning(expr->pos, "%*s", string->length-1, string->data);
701 continue;
705 * Any other argument is a conditional. If it's
706 * non-constant, or it is false, we exit and do
707 * not print any warning.
709 if (arg->type != EXPR_VALUE)
710 goto out;
711 if (!arg->value)
712 goto out;
713 } END_FOR_EACH_PTR(arg);
714 out:
715 expr->type = EXPR_VALUE;
716 expr->value = 1;
717 expr->taint = 0;
718 return 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 {
751 const char *name;
752 struct symbol *base_type;
753 unsigned int modifiers;
754 struct symbol_op *op;
755 } eval_init_table[] = {
756 { "__builtin_constant_p", &builtin_fn_type, MOD_TOPLEVEL, &constant_p_op },
757 { "__builtin_safe_p", &builtin_fn_type, MOD_TOPLEVEL, &safe_p_op },
758 { "__builtin_warning", &builtin_fn_type, MOD_TOPLEVEL, &warning_op },
759 { "__builtin_expect", &builtin_fn_type, MOD_TOPLEVEL, &expect_op },
760 { "__builtin_choose_expr", &builtin_fn_type, MOD_TOPLEVEL, &choose_op },
761 { NULL, NULL, 0 }
766 * Abstract types
768 struct symbol int_type,
769 fp_type;
772 * C types (i.e. actual instances that the abstract types
773 * can map onto)
775 struct symbol bool_ctype, void_ctype, type_ctype,
776 char_ctype, schar_ctype, uchar_ctype,
777 short_ctype, sshort_ctype, ushort_ctype,
778 int_ctype, sint_ctype, uint_ctype,
779 long_ctype, slong_ctype, ulong_ctype,
780 llong_ctype, sllong_ctype, ullong_ctype,
781 lllong_ctype, slllong_ctype, ulllong_ctype,
782 float_ctype, double_ctype, ldouble_ctype,
783 string_ctype, ptr_ctype, lazy_ptr_ctype,
784 incomplete_ctype, label_ctype, bad_ctype,
785 null_ctype;
787 struct symbol zero_int;
789 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
790 #define __IDENT(n,str,res) \
791 struct ident n = __INIT_IDENT(str,res)
793 #include "ident-list.h"
795 void init_symbols(void)
797 int stream = init_stream("builtin", -1, includepath);
798 struct sym_init *ptr;
800 #define __IDENT(n,str,res) \
801 hash_ident(&n)
802 #include "ident-list.h"
804 init_parser(stream);
806 builtin_fn_type.variadic = 1;
807 for (ptr = eval_init_table; ptr->name; ptr++) {
808 struct symbol *sym;
809 sym = create_symbol(stream, ptr->name, SYM_NODE, NS_SYMBOL);
810 sym->ctype.base_type = ptr->base_type;
811 sym->ctype.modifiers = ptr->modifiers;
812 sym->op = ptr->op;
816 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
817 #define MOD_LL (MOD_LONG | MOD_LONGLONG)
818 #define MOD_LLL MOD_LONGLONGLONG
819 static const struct ctype_declare {
820 struct symbol *ptr;
821 enum type type;
822 unsigned long modifiers;
823 int *bit_size;
824 int *maxalign;
825 struct symbol *base_type;
826 } ctype_declaration[] = {
827 { &bool_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_bool, &max_int_alignment, &int_type },
828 { &void_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
829 { &type_ctype, SYM_BASETYPE, MOD_TYPE, NULL, NULL, NULL },
830 { &incomplete_ctype,SYM_BASETYPE, 0, NULL, NULL, NULL },
831 { &bad_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
833 { &char_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
834 { &schar_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
835 { &uchar_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
836 { &short_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
837 { &sshort_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
838 { &ushort_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
839 { &int_ctype, SYM_BASETYPE, MOD_SIGNED, &bits_in_int, &max_int_alignment, &int_type },
840 { &sint_ctype, SYM_BASETYPE, MOD_ESIGNED, &bits_in_int, &max_int_alignment, &int_type },
841 { &uint_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_int, &max_int_alignment, &int_type },
842 { &long_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
843 { &slong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
844 { &ulong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
845 { &llong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
846 { &sllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
847 { &ullong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
848 { &lllong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
849 { &slllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
850 { &ulllong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LLL, &bits_in_longlonglong, &max_int_alignment, &int_type },
852 { &float_ctype, SYM_BASETYPE, 0, &bits_in_float, &max_fp_alignment, &fp_type },
853 { &double_ctype, SYM_BASETYPE, MOD_LONG, &bits_in_double, &max_fp_alignment, &fp_type },
854 { &ldouble_ctype, SYM_BASETYPE, MOD_LONG | MOD_LONGLONG, &bits_in_longdouble, &max_fp_alignment, &fp_type },
856 { &string_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &char_ctype },
857 { &ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
858 { &null_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
859 { &label_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
860 { &lazy_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
861 { NULL, }
863 #undef MOD_LLL
864 #undef MOD_LL
865 #undef MOD_ESIGNED
867 void init_ctype(void)
869 const struct ctype_declare *ctype;
871 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
872 struct symbol *sym = ctype->ptr;
873 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
874 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
875 unsigned long alignment = bits_to_bytes(bit_size + bits_in_char - 1);
877 if (alignment > maxalign)
878 alignment = maxalign;
879 sym->type = ctype->type;
880 sym->bit_size = bit_size;
881 sym->ctype.alignment = alignment;
882 sym->ctype.base_type = ctype->base_type;
883 sym->ctype.modifiers = ctype->modifiers;