[PATCH] two-arguments ?:
[smatch.git] / symbol.c
blob102685a537c7713561edb36fdc6409c860e68303
1 /*
2 * Symbol lookup and handling.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
8 */
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #include "lib.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 *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(&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 symbol *alloc_symbol(struct position pos, int type)
57 struct symbol *sym = __alloc_symbol(0);
58 sym->type = type;
59 sym->pos = pos;
60 return sym;
63 struct struct_union_info {
64 unsigned long max_align;
65 unsigned long bit_size;
66 int align_size;
70 * Unions are fairly easy to lay out ;)
72 static void lay_out_union(struct symbol *sym, void *_info, int flags)
74 struct struct_union_info *info = _info;
76 examine_symbol_type(sym);
78 // Unnamed bitfields do not affect alignment.
79 if (sym->ident || !is_bitfield_type(sym)) {
80 if (sym->ctype.alignment > info->max_align)
81 info->max_align = sym->ctype.alignment;
84 if (sym->bit_size > info->bit_size)
85 info->bit_size = sym->bit_size;
87 sym->offset = 0;
91 * Structures are a bit more interesting to lay out
93 static void lay_out_struct(struct symbol *sym, void *_info, int flags)
95 struct struct_union_info *info = _info;
96 unsigned long bit_size, align_bit_mask;
97 int base_size;
99 examine_symbol_type(sym);
101 // Unnamed bitfields do not affect alignment.
102 if (sym->ident || !is_bitfield_type(sym)) {
103 if (sym->ctype.alignment > info->max_align)
104 info->max_align = sym->ctype.alignment;
107 bit_size = info->bit_size;
108 base_size = sym->bit_size;
111 * Unsized arrays cause us to not align the resulting
112 * structure size
114 if (base_size < 0) {
115 info->align_size = 0;
116 base_size = 0;
119 align_bit_mask = (sym->ctype.alignment << 3) - 1;
122 * Bitfields have some very special rules..
124 if (is_bitfield_type (sym)) {
125 unsigned long bit_offset = bit_size & align_bit_mask;
126 int room = base_size - bit_offset;
127 // Zero-width fields just fill up the unit.
128 int width = sym->fieldwidth ? sym->fieldwidth : (bit_offset ? room : 0);
130 if (width > room) {
131 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
132 bit_offset = 0;
134 sym->offset = (bit_size - bit_offset) >> 3;
135 sym->bit_offset = bit_offset;
136 info->bit_size = bit_size + width;
137 // warning (sym->pos, "bitfield: offset=%d:%d size=:%d", sym->offset, sym->bit_offset, width);
139 return;
143 * Otherwise, just align it right and add it up..
145 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
146 sym->offset = bit_size >> 3;
148 info->bit_size = bit_size + base_size;
149 // warning (sym->pos, "regular: offset=%d", sym->offset);
152 static void examine_struct_union_type(struct symbol *sym, int advance)
154 struct struct_union_info info = { 1, 0, 1 };
155 unsigned long bit_size, bit_align;
156 void (*fn)(struct symbol *, void *, int);
158 fn = advance ? lay_out_struct : lay_out_union;
159 symbol_iterate(sym->symbol_list, fn, &info);
161 if (!sym->ctype.alignment)
162 sym->ctype.alignment = info.max_align;
163 bit_size = info.bit_size;
164 if (info.align_size) {
165 bit_align = (sym->ctype.alignment << 3)-1;
166 bit_size = (bit_size + bit_align) & ~bit_align;
168 sym->bit_size = bit_size;
171 static void examine_array_type(struct symbol *sym)
173 struct symbol *base_type = sym->ctype.base_type;
174 unsigned long bit_size, alignment;
176 if (!base_type)
177 return;
178 examine_symbol_type(base_type);
179 bit_size = base_type->bit_size * get_expression_value(sym->array_size);
180 if (!sym->array_size || sym->array_size->type != EXPR_VALUE)
181 bit_size = -1;
182 alignment = base_type->ctype.alignment;
183 if (!sym->ctype.alignment)
184 sym->ctype.alignment = alignment;
185 sym->bit_size = bit_size;
188 static void examine_bitfield_type(struct symbol *sym)
190 struct symbol *base_type = sym->ctype.base_type;
191 unsigned long bit_size, alignment;
193 if (!base_type)
194 return;
195 examine_symbol_type(base_type);
196 bit_size = base_type->bit_size;
197 if (sym->fieldwidth > bit_size) {
198 warning(sym->pos, "impossible field-width, %d, for this type",
199 sym->fieldwidth);
200 sym->fieldwidth = bit_size;
203 alignment = base_type->ctype.alignment;
204 if (!sym->ctype.alignment)
205 sym->ctype.alignment = alignment;
206 sym->bit_size = bit_size;
210 * "typeof" will have to merge the types together
212 void merge_type(struct symbol *sym, struct symbol *base_type)
214 sym->ctype.as |= base_type->ctype.as;
215 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
216 sym->ctype.context |= base_type->ctype.context;
217 sym->ctype.contextmask |= base_type->ctype.contextmask;
218 sym->ctype.base_type = base_type->ctype.base_type;
222 * Fill in type size and alignment information for
223 * regular SYM_TYPE things.
225 struct symbol *examine_symbol_type(struct symbol * sym)
227 unsigned int bit_size, alignment;
228 struct symbol *base_type;
229 unsigned long modifiers;
231 if (!sym)
232 return sym;
234 /* Already done? */
235 if (sym->bit_size)
236 return sym;
238 switch (sym->type) {
239 case SYM_ARRAY:
240 examine_array_type(sym);
241 return sym;
242 case SYM_STRUCT:
243 examine_struct_union_type(sym, 1);
244 return sym;
245 case SYM_UNION:
246 examine_struct_union_type(sym, 0);
247 return sym;
248 case SYM_PTR:
249 if (!sym->bit_size)
250 sym->bit_size = bits_in_pointer;
251 if (!sym->ctype.alignment)
252 sym->ctype.alignment = pointer_alignment;
253 base_type = sym->ctype.base_type;
254 base_type = examine_symbol_type(base_type);
255 if (base_type && base_type->type == SYM_NODE)
256 merge_type(sym, base_type);
257 return sym;
258 case SYM_ENUM:
259 base_type = sym->ctype.base_type;
260 base_type = examine_symbol_type(base_type);
261 if (base_type == &bad_enum_ctype) {
262 warning(sym->pos, "invalid enum type");
263 sym->bit_size = -1;
264 return sym;
266 sym->bit_size = bits_in_enum;
267 if (base_type->bit_size > sym->bit_size)
268 sym->bit_size = base_type->bit_size;
269 sym->ctype.alignment = enum_alignment;
270 if (base_type->ctype.alignment > sym->ctype.alignment)
271 sym->ctype.alignment = base_type->ctype.alignment;
272 return sym;
273 case SYM_BITFIELD:
274 examine_bitfield_type(sym);
275 return sym;
276 case SYM_BASETYPE:
277 /* Size and alignment had better already be set up */
278 return sym;
279 case SYM_TYPEOF: {
280 struct symbol *base = evaluate_expression(sym->initializer);
281 if (base) {
282 if (is_bitfield_type(base))
283 warning(base->pos, "typeof applied to bitfield type");
284 if (base->type == SYM_NODE)
285 base = base->ctype.base_type;
286 sym->type = SYM_NODE;
287 sym->ctype.base_type = base;
289 break;
290 case SYM_PREPROCESSOR:
291 warning(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
292 return NULL;
293 case SYM_UNINITIALIZED:
294 warning(sym->pos, "ctype on uninitialized symbol %p", sym);
295 return NULL;
297 default:
298 break;
301 /* SYM_NODE - figure out what the type of the node was.. */
302 base_type = sym->ctype.base_type;
303 modifiers = sym->ctype.modifiers;
305 bit_size = 0;
306 alignment = 0;
307 if (base_type) {
308 base_type = examine_symbol_type(base_type);
309 sym->ctype.base_type = base_type;
310 if (base_type && base_type->type == SYM_NODE)
311 merge_type(sym, base_type);
313 bit_size = base_type->bit_size;
314 alignment = base_type->ctype.alignment;
315 if (base_type->fieldwidth)
316 sym->fieldwidth = base_type->fieldwidth;
319 if (!sym->ctype.alignment)
320 sym->ctype.alignment = alignment;
321 sym->bit_size = bit_size;
322 return sym;
325 void check_declaration(struct symbol *sym)
327 struct symbol *next = sym;
329 while ((next = next->next_id) != NULL) {
330 if (next->namespace != sym->namespace)
331 continue;
332 if (sym->scope == next->scope) {
333 sym->same_symbol = next;
334 return;
336 if (sym->ctype.modifiers & next->ctype.modifiers & MOD_EXTERN) {
337 sym->same_symbol = next;
338 return;
340 #if 0
341 // This may make sense from a warning standpoint:
342 // consider top-level symbols to clash with everything
343 // (but the scoping rules will mean that we actually
344 // _use_ the innermost version)
345 if (toplevel(next->scope)) {
346 sym->same_symbol = next;
347 return;
349 #endif
353 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
355 struct scope *scope;
356 if (sym->id_list) {
357 warning(sym->pos, "internal error: symbol type already bound");
358 return;
360 sym->namespace = ns;
361 sym->next_id = ident->symbols;
362 ident->symbols = sym;
363 sym->id_list = &ident->symbols;
365 scope = block_scope;
366 if (ns == NS_SYMBOL && toplevel(scope)) {
367 sym->ctype.modifiers |= MOD_TOPLEVEL | MOD_ADDRESSABLE;
368 if (sym->ctype.modifiers & MOD_STATIC)
369 scope = file_scope;
371 if (ns == NS_LABEL)
372 scope = function_scope;
373 bind_scope(sym, scope);
376 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
378 struct token *token = built_in_token(stream, name);
379 struct symbol *sym = alloc_symbol(token->pos, type);
381 sym->ident = token->ident;
382 bind_symbol(sym, token->ident, namespace);
383 return sym;
386 static int evaluate_constant_p(struct expression *expr)
388 expr->ctype = &int_ctype;
389 return 1;
392 static int expand_constant_p(struct expression *expr)
394 struct expression *arg;
395 struct expression_list *arglist = expr->args;
396 int value = 1;
398 FOR_EACH_PTR (arglist, arg) {
399 if (arg->type != EXPR_VALUE && arg->type != EXPR_FVALUE)
400 value = 0;
401 } END_FOR_EACH_PTR(arg);
403 expr->type = EXPR_VALUE;
404 expr->value = value;
405 return 0;
409 * __builtin_warning() has type "int" and always returns 1,
410 * so that you can use it in conditionals or whatever
412 static int evaluate_warning(struct expression *expr)
414 expr->ctype = &int_ctype;
415 return 1;
418 static int expand_warning(struct expression *expr)
420 struct expression *arg;
421 struct expression_list *arglist = expr->args;
423 FOR_EACH_PTR (arglist, arg) {
425 * Constant strings get printed out as a warning. By the
426 * time we get here, the EXPR_STRING has been fully
427 * evaluated, so by now it's an anonymous symbol with a
428 * string initializer.
430 * Just for the heck of it, allow any constant string
431 * symbol.
433 if (arg->type == EXPR_SYMBOL) {
434 struct symbol *sym = arg->symbol;
435 if (sym->initializer && sym->initializer->type == EXPR_STRING) {
436 struct string *string = sym->initializer->string;
437 warning(expr->pos, "%*s", string->length-1, string->data);
439 continue;
443 * Any other argument is a conditional. If it's
444 * non-constant, or it is false, we exit and do
445 * not print any warning.
447 if (arg->type != EXPR_VALUE)
448 goto out;
449 if (!arg->value)
450 goto out;
451 } END_FOR_EACH_PTR(arg);
452 out:
453 expr->type = EXPR_VALUE;
454 expr->value = 1;
455 return 0;
459 * Type and storage class keywords need to have the symbols
460 * created for them, so that the parser can have enough semantic
461 * information to do parsing.
463 * "double" == "long float", "long double" == "long long float"
465 struct sym_init {
466 const char *name;
467 struct symbol *base_type;
468 unsigned int modifiers;
469 struct symbol_op *op;
470 } symbol_init_table[] = {
471 /* Storage class */
472 { "auto", NULL, MOD_AUTO },
473 { "register", NULL, MOD_REGISTER },
474 { "static", NULL, MOD_STATIC },
475 { "extern", NULL, MOD_EXTERN },
477 /* Type specifiers */
478 { "void", &void_ctype, 0 },
479 { "char", NULL, MOD_CHAR },
480 { "short", NULL, MOD_SHORT },
481 { "int", &int_type, 0 },
482 { "long", NULL, MOD_LONG },
483 { "float", &fp_type, 0 },
484 { "double", &fp_type, MOD_LONG },
485 { "signed", NULL, MOD_SIGNED | MOD_EXPLICITLY_SIGNED },
486 { "__signed", NULL, MOD_SIGNED | MOD_EXPLICITLY_SIGNED },
487 { "__signed__", NULL, MOD_SIGNED | MOD_EXPLICITLY_SIGNED },
488 { "unsigned", NULL, MOD_UNSIGNED },
489 { "__label__", &label_ctype, MOD_LABEL | MOD_UNSIGNED },
491 /* Type qualifiers */
492 { "const", NULL, MOD_CONST },
493 { "__const", NULL, MOD_CONST },
494 { "__const__", NULL, MOD_CONST },
495 { "volatile", NULL, MOD_VOLATILE },
496 { "__volatile", NULL, MOD_VOLATILE },
497 { "__volatile__", NULL, MOD_VOLATILE },
499 /* Predeclared types */
500 { "__builtin_va_list", &int_type, 0 },
502 /* Typedef.. */
503 { "typedef", NULL, MOD_TYPEDEF },
505 /* Extended types */
506 { "typeof", NULL, MOD_TYPEOF },
507 { "__typeof", NULL, MOD_TYPEOF },
508 { "__typeof__", NULL, MOD_TYPEOF },
510 #if 0
511 { "attribute", NULL, MOD_ATTRIBUTE },
512 #endif
513 { "__attribute", NULL, MOD_ATTRIBUTE },
514 { "__attribute__", NULL, MOD_ATTRIBUTE },
516 { "struct", NULL, MOD_STRUCTOF },
517 { "union", NULL, MOD_UNIONOF },
518 { "enum", NULL, MOD_ENUMOF },
520 { "inline", NULL, MOD_INLINE },
521 { "__inline", NULL, MOD_INLINE },
522 { "__inline__", NULL, MOD_INLINE },
524 /* Ignored for now.. */
525 { "restrict", NULL, 0 },
526 { "__restrict", NULL, 0 },
528 { NULL, NULL, 0 }
531 static struct symbol_op constant_p_op = {
532 .evaluate = evaluate_constant_p,
533 .expand = expand_constant_p
536 static struct symbol_op warning_op = {
537 .evaluate = evaluate_warning,
538 .expand = expand_warning
542 * Builtin functions
544 static struct symbol builtin_fn_type = { .type = SYM_FN /* , .variadic =1 */ };
545 static struct sym_init eval_init_table[] = {
546 { "__builtin_constant_p", &builtin_fn_type, MOD_TOPLEVEL, &constant_p_op },
547 { "__builtin_warning", &builtin_fn_type, MOD_TOPLEVEL, &warning_op },
548 { NULL, NULL, 0 }
553 * Abstract types
555 struct symbol int_type,
556 fp_type,
557 vector_type,
558 bad_type;
561 * C types (ie actual instances that the abstract types
562 * can map onto)
564 struct symbol bool_ctype, void_ctype, type_ctype,
565 char_ctype, schar_ctype, uchar_ctype,
566 short_ctype, sshort_ctype, ushort_ctype,
567 int_ctype, sint_ctype, uint_ctype,
568 long_ctype, slong_ctype, ulong_ctype,
569 llong_ctype, sllong_ctype, ullong_ctype,
570 float_ctype, double_ctype, ldouble_ctype,
571 string_ctype, ptr_ctype, lazy_ptr_ctype,
572 incomplete_ctype, label_ctype, bad_enum_ctype;
575 #define __INIT_IDENT(str) { .len = sizeof(str)-1, .name = str }
576 #define __IDENT(n,str) \
577 struct ident n = __INIT_IDENT(str)
579 #include "ident-list.h"
581 void init_symbols(void)
583 int stream = init_stream("builtin", -1, includepath);
584 struct sym_init *ptr;
586 #define __IDENT(n,str) \
587 hash_ident(&n)
588 #include "ident-list.h"
590 for (ptr = symbol_init_table; ptr->name; ptr++) {
591 struct symbol *sym;
592 sym = create_symbol(stream, ptr->name, SYM_NODE, NS_TYPEDEF);
593 sym->ctype.base_type = ptr->base_type;
594 sym->ctype.modifiers = ptr->modifiers;
597 builtin_fn_type.variadic = 1;
598 for (ptr = eval_init_table; ptr->name; ptr++) {
599 struct symbol *sym;
600 sym = create_symbol(stream, ptr->name, SYM_NODE, NS_SYMBOL);
601 sym->ctype.base_type = ptr->base_type;
602 sym->ctype.modifiers = ptr->modifiers;
603 sym->op = ptr->op;
607 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
608 #define MOD_LL (MOD_LONG | MOD_LONGLONG)
609 static const struct ctype_declare {
610 struct symbol *ptr;
611 enum type type;
612 unsigned long modifiers;
613 int *bit_size;
614 int *maxalign;
615 struct symbol *base_type;
616 } ctype_declaration[] = {
617 { &bool_ctype, SYM_BASETYPE, 0, &bits_in_int, &max_int_alignment, &int_type },
618 { &void_ctype, SYM_BASETYPE, 0, NULL, NULL, NULL },
619 { &type_ctype, SYM_BASETYPE, MOD_TYPE, NULL, NULL, NULL },
620 { &incomplete_ctype,SYM_BASETYPE, 0, NULL, NULL, NULL },
621 { &bad_enum_ctype, SYM_BAD, 0, NULL, NULL, NULL },
623 { &char_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
624 { &schar_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
625 { &uchar_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
626 { &short_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
627 { &sshort_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
628 { &ushort_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
629 { &int_ctype, SYM_BASETYPE, MOD_SIGNED, &bits_in_int, &max_int_alignment, &int_type },
630 { &sint_ctype, SYM_BASETYPE, MOD_ESIGNED, &bits_in_int, &max_int_alignment, &int_type },
631 { &uint_ctype, SYM_BASETYPE, MOD_UNSIGNED, &bits_in_int, &max_int_alignment, &int_type },
632 { &long_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
633 { &slong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
634 { &ulong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
635 { &llong_ctype, SYM_BASETYPE, MOD_SIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
636 { &sllong_ctype, SYM_BASETYPE, MOD_ESIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
637 { &ullong_ctype, SYM_BASETYPE, MOD_UNSIGNED | MOD_LL, &bits_in_longlong, &max_int_alignment, &int_type },
639 { &float_ctype, SYM_BASETYPE, 0, &bits_in_float, &max_fp_alignment, &fp_type },
640 { &double_ctype, SYM_BASETYPE, MOD_LONG, &bits_in_double, &max_fp_alignment, &fp_type },
641 { &ldouble_ctype, SYM_BASETYPE, MOD_LONG | MOD_LONGLONG, &bits_in_longdouble, &max_fp_alignment, &fp_type },
643 { &string_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &char_ctype },
644 { &ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
645 { &label_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
646 { &lazy_ptr_ctype, SYM_PTR, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
647 { NULL, }
649 #undef MOD_LL
650 #undef MOD_ESIGNED
652 void init_ctype(void)
654 const struct ctype_declare *ctype;
656 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
657 struct symbol *sym = ctype->ptr;
658 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
659 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
660 unsigned long alignment = bit_size >> 3;
662 if (alignment > maxalign)
663 alignment = maxalign;
664 sym->type = ctype->type;
665 sym->bit_size = bit_size;
666 sym->ctype.alignment = alignment;
667 sym->ctype.base_type = ctype->base_type;
668 sym->ctype.modifiers = ctype->modifiers;