Special evaluation rules for function argument types:
[smatch.git] / symbol.c
blob8d887919dd6daeb5dae20d0c5900a97e2571f30b
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;
69 * Unions are easy to lay out ;)
71 static void lay_out_union(struct symbol *sym, void *_info, int flags)
73 struct struct_union_info *info = _info;
75 examine_symbol_type(sym);
76 if (sym->ctype.alignment > info->max_align)
77 info->max_align = sym->ctype.alignment;
78 if (sym->bit_size > info->bit_size)
79 info->bit_size = sym->bit_size;
81 sym->offset = 0;
85 * Structures are a bit more interesting to lay out
87 static void lay_out_struct(struct symbol *sym, void *_info, int flags)
89 struct struct_union_info *info = _info;
90 unsigned long bit_size, base_size;
91 unsigned long align_bit_mask;
93 examine_symbol_type(sym);
94 if (sym->ctype.alignment > info->max_align)
95 info->max_align = sym->ctype.alignment;
97 bit_size = info->bit_size;
98 base_size = sym->bit_size;
99 align_bit_mask = (sym->ctype.alignment << 3) - 1;
102 * Bitfields have some very special rules..
104 if (sym->fieldwidth) {
105 unsigned long bit_offset = bit_size & align_bit_mask;
107 if (bit_offset + sym->fieldwidth > base_size) {
108 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
109 bit_offset = 0;
111 sym->offset = (bit_size - bit_offset) >> 3;
112 sym->bit_offset = bit_offset;
113 info->bit_size = bit_size + sym->fieldwidth;
114 return;
118 * Otherwise, just align it right and add it up..
120 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
121 sym->offset = bit_size >> 3;
123 info->bit_size = bit_size + sym->bit_size;
126 static void examine_struct_union_type(struct symbol *sym, int advance)
128 struct struct_union_info info = { 1, 0 };
129 unsigned long bit_size, bit_align;
130 void (*fn)(struct symbol *, void *, int);
132 fn = advance ? lay_out_struct : lay_out_union;
133 symbol_iterate(sym->symbol_list, fn, &info);
135 if (!sym->ctype.alignment)
136 sym->ctype.alignment = info.max_align;
137 bit_size = info.bit_size;
138 bit_align = (sym->ctype.alignment << 3)-1;
139 bit_size = (bit_size + bit_align) & ~bit_align;
140 sym->bit_size = bit_size;
143 static void examine_array_type(struct symbol *sym)
145 struct symbol *base_type = sym->ctype.base_type;
146 unsigned long bit_size, alignment;
148 if (!base_type)
149 return;
150 examine_symbol_type(base_type);
151 bit_size = base_type->bit_size * get_expression_value(sym->array_size);
152 if (!sym->array_size)
153 bit_size = -1;
154 alignment = base_type->ctype.alignment;
155 if (!sym->ctype.alignment)
156 sym->ctype.alignment = alignment;
157 sym->bit_size = bit_size;
160 static void examine_bitfield_type(struct symbol *sym)
162 struct symbol *base_type = sym->ctype.base_type;
163 unsigned long bit_size, alignment;
165 if (!base_type)
166 return;
167 examine_symbol_type(base_type);
168 bit_size = base_type->bit_size;
169 if (sym->fieldwidth > bit_size) {
170 warn(sym->pos, "impossible field-width for this type");
171 sym->fieldwidth = bit_size;
173 alignment = base_type->ctype.alignment;
174 if (!sym->ctype.alignment)
175 sym->ctype.alignment = alignment;
176 sym->bit_size = bit_size;
180 * "typeof" will have to merge the types together
182 void merge_type(struct symbol *sym, struct symbol *base_type)
184 sym->ctype.as |= base_type->ctype.as;
185 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
186 sym->ctype.context |= base_type->ctype.context;
187 sym->ctype.contextmask |= base_type->ctype.contextmask;
188 sym->ctype.base_type = base_type->ctype.base_type;
192 * Fill in type size and alignment information for
193 * regular SYM_TYPE things.
195 struct symbol *examine_symbol_type(struct symbol * sym)
197 unsigned int bit_size, alignment;
198 struct symbol *base_type;
199 unsigned long modifiers;
201 if (!sym)
202 return sym;
204 /* Already done? */
205 if (sym->bit_size)
206 return sym;
208 switch (sym->type) {
209 case SYM_ARRAY:
210 examine_array_type(sym);
211 return sym;
212 case SYM_STRUCT:
213 examine_struct_union_type(sym, 1);
214 return sym;
215 case SYM_UNION:
216 examine_struct_union_type(sym, 0);
217 return sym;
218 case SYM_PTR:
219 if (!sym->bit_size)
220 sym->bit_size = bits_in_pointer;
221 if (!sym->ctype.alignment)
222 sym->ctype.alignment = pointer_alignment;
223 base_type = sym->ctype.base_type;
224 base_type = examine_symbol_type(base_type);
225 if (base_type && base_type->type == SYM_NODE)
226 merge_type(sym, base_type);
227 return sym;
228 case SYM_ENUM:
229 if (!sym->bit_size)
230 sym->bit_size = bits_in_enum;
231 if (!sym->ctype.alignment)
232 sym->ctype.alignment = enum_alignment;
233 return sym;
234 case SYM_BITFIELD:
235 examine_bitfield_type(sym);
236 return sym;
237 case SYM_BASETYPE:
238 /* Size and alignment had better already be set up */
239 return sym;
240 case SYM_TYPEOF: {
241 struct symbol *base = evaluate_expression(sym->initializer);
242 if (base)
243 return base;
244 break;
246 default:
247 break;
250 /* SYM_NODE - figure out what the type of the node was.. */
251 base_type = sym->ctype.base_type;
252 modifiers = sym->ctype.modifiers;
254 bit_size = 0;
255 alignment = 0;
256 if (base_type) {
257 base_type = examine_symbol_type(base_type);
258 sym->ctype.base_type = base_type;
259 if (base_type && base_type->type == SYM_NODE)
260 merge_type(sym, base_type);
262 bit_size = base_type->bit_size;
263 alignment = base_type->ctype.alignment;
264 if (base_type->fieldwidth)
265 sym->fieldwidth = base_type->fieldwidth;
268 if (!sym->ctype.alignment)
269 sym->ctype.alignment = alignment;
270 sym->bit_size = bit_size;
271 return sym;
274 void check_declaration(struct symbol *sym)
276 struct symbol *next = sym;
278 while ((next = next->next_id) != NULL) {
279 if (next->namespace != sym->namespace)
280 continue;
281 if (sym->scope == next->scope) {
282 sym->same_symbol = next;
283 return;
285 if (sym->ctype.modifiers & next->ctype.modifiers & MOD_EXTERN) {
286 sym->same_symbol = next;
287 return;
289 #if 0
290 // This may make sense from a warning standpoint:
291 // consider top-level symbols to clash with everything
292 // (but the scoping rules will mean that we actually
293 // _use_ the innermost version)
294 if (toplevel(next->scope)) {
295 sym->same_symbol = next;
296 return;
298 #endif
302 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
304 struct scope *scope;
305 if (sym->id_list) {
306 warn(sym->pos, "internal error: symbol type already bound");
307 return;
309 sym->namespace = ns;
310 sym->next_id = ident->symbols;
311 ident->symbols = sym;
312 sym->id_list = &ident->symbols;
314 scope = block_scope;
315 if (ns != NS_TYPEDEF && toplevel(scope)) {
316 sym->ctype.modifiers |= MOD_TOPLEVEL | MOD_ADDRESSABLE;
317 if (sym->ctype.modifiers & MOD_STATIC)
318 scope = file_scope;
320 if (ns == NS_LABEL)
321 scope = function_scope;
322 bind_scope(sym, scope);
325 static struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
327 struct token *token = built_in_token(stream, name);
328 struct symbol *sym = alloc_symbol(token->pos, type);
330 sym->ident = token->ident;
331 bind_symbol(sym, token->ident, namespace);
332 return sym;
335 static int evaluate_constant_p(struct expression *expr)
337 expr->ctype = &int_ctype;
338 return 1;
341 static void expand_constant_p(struct expression *expr)
343 struct expression *arg;
344 struct expression_list *arglist = expr->args;
345 int value = 1;
347 FOR_EACH_PTR (arglist, arg) {
348 if (arg->type != EXPR_VALUE)
349 value = 0;
350 } END_FOR_EACH_PTR;
352 expr->type = EXPR_VALUE;
353 expr->value = value;
357 * Type and storage class keywords need to have the symbols
358 * created for them, so that the parser can have enough semantic
359 * information to do parsing.
361 * "double" == "long float", "long double" == "long long float"
363 struct sym_init {
364 const char *name;
365 struct symbol *base_type;
366 unsigned int modifiers;
367 struct symbol_op *op;
368 } symbol_init_table[] = {
369 /* Storage class */
370 { "auto", NULL, MOD_AUTO },
371 { "register", NULL, MOD_REGISTER },
372 { "static", NULL, MOD_STATIC },
373 { "extern", NULL, MOD_EXTERN },
375 /* Type specifiers */
376 { "void", &void_ctype, 0 },
377 { "char", NULL, MOD_CHAR },
378 { "short", NULL, MOD_SHORT },
379 { "int", &int_type, 0 },
380 { "long", NULL, MOD_LONG },
381 { "float", &fp_type, 0 },
382 { "double", &fp_type, MOD_LONG },
383 { "signed", NULL, MOD_SIGNED },
384 { "__signed", NULL, MOD_SIGNED },
385 { "__signed__", NULL, MOD_SIGNED },
386 { "unsigned", NULL, MOD_UNSIGNED },
387 { "__label__", &label_type, MOD_LABEL | MOD_UNSIGNED },
389 /* Type qualifiers */
390 { "const", NULL, MOD_CONST },
391 { "__const", NULL, MOD_CONST },
392 { "__const__", NULL, MOD_CONST },
393 { "volatile", NULL, MOD_VOLATILE },
394 { "__volatile", NULL, MOD_VOLATILE },
395 { "__volatile__", NULL, MOD_VOLATILE },
397 /* Predeclared types */
398 { "__builtin_va_list", &int_type, 0 },
400 /* Typedef.. */
401 { "typedef", NULL, MOD_TYPEDEF },
403 /* Extended types */
404 { "typeof", NULL, MOD_TYPEOF },
405 { "__typeof", NULL, MOD_TYPEOF },
406 { "__typeof__", NULL, MOD_TYPEOF },
408 #if 0
409 { "attribute", NULL, MOD_ATTRIBUTE },
410 #endif
411 { "__attribute", NULL, MOD_ATTRIBUTE },
412 { "__attribute__", NULL, MOD_ATTRIBUTE },
414 { "struct", NULL, MOD_STRUCTOF },
415 { "union", NULL, MOD_UNIONOF },
416 { "enum", NULL, MOD_ENUMOF },
418 { "inline", NULL, MOD_INLINE },
419 { "__inline", NULL, MOD_INLINE },
420 { "__inline__", NULL, MOD_INLINE },
422 /* Ignored for now.. */
423 { "restrict", NULL, 0 },
424 { "__restrict", NULL, 0 },
426 { NULL, NULL, 0 }
429 struct symbol_op constant_p_op = {
430 .evaluate = evaluate_constant_p,
431 .expand = expand_constant_p
435 * Builtin functions
437 static struct symbol builtin_fn_type = { .type = SYM_FN };
438 struct sym_init eval_init_table[] = {
439 { "__builtin_constant_p", &builtin_fn_type, MOD_TOPLEVEL, &constant_p_op },
441 { NULL, NULL, 0 }
446 * Abstract types
448 struct symbol int_type,
449 fp_type,
450 label_type,
451 vector_type,
452 bad_type;
455 * C types (ie actual instances that the abstract types
456 * can map onto)
458 struct symbol bool_ctype, void_ctype, type_ctype,
459 char_ctype, uchar_ctype,
460 short_ctype, ushort_ctype,
461 int_ctype, uint_ctype,
462 long_ctype, ulong_ctype,
463 llong_ctype, ullong_ctype,
464 float_ctype, double_ctype, ldouble_ctype,
465 string_ctype, ptr_ctype, incomplete_ctype;
467 struct ctype_declare {
468 struct symbol *ptr;
469 unsigned long modifiers;
470 int *bit_size;
471 int *maxalign;
472 struct symbol *base_type;
473 } ctype_declaration[] = {
474 { &bool_ctype, 0, &bits_in_int, &max_int_alignment, &int_type },
475 { &void_ctype, 0, NULL, NULL, NULL },
476 { &type_ctype, MOD_TYPE, NULL, NULL, NULL },
477 { &incomplete_ctype, 0, NULL, NULL, NULL },
479 { &char_ctype, MOD_SIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
480 { &uchar_ctype, MOD_UNSIGNED | MOD_CHAR, &bits_in_char, &max_int_alignment, &int_type },
481 { &short_ctype, MOD_SIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
482 { &ushort_ctype, MOD_UNSIGNED | MOD_SHORT, &bits_in_short, &max_int_alignment, &int_type },
483 { &int_ctype, MOD_SIGNED, &bits_in_int, &max_int_alignment, &int_type },
484 { &uint_ctype, MOD_UNSIGNED, &bits_in_int, &max_int_alignment, &int_type },
485 { &long_ctype, MOD_SIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
486 { &ulong_ctype, MOD_UNSIGNED | MOD_LONG, &bits_in_long, &max_int_alignment, &int_type },
487 { &llong_ctype, MOD_SIGNED | MOD_LONG | MOD_LONGLONG, &bits_in_longlong, &max_int_alignment, &int_type },
488 { &ullong_ctype, MOD_UNSIGNED | MOD_LONG | MOD_LONGLONG, &bits_in_longlong, &max_int_alignment, &int_type },
490 { &float_ctype, 0, &bits_in_float, &max_fp_alignment, &fp_type },
491 { &double_ctype, MOD_LONG, &bits_in_double, &max_fp_alignment, &fp_type },
492 { &ldouble_ctype,MOD_LONG | MOD_LONGLONG, &bits_in_longdouble, &max_fp_alignment, &fp_type },
494 { &string_ctype, 0, &bits_in_pointer, &pointer_alignment, &char_ctype },
495 { &ptr_ctype, 0, &bits_in_pointer, &pointer_alignment, &void_ctype },
496 { NULL, }
500 #define __INIT_IDENT(str) { .len = sizeof(str)-1, .name = str }
501 #define __IDENT(n,str) \
502 struct ident n ## _ident = __INIT_IDENT(str)
503 #define IDENT(n) __IDENT(n, #n)
505 IDENT(struct); IDENT(union); IDENT(enum);
506 IDENT(sizeof);
507 IDENT(alignof); IDENT(__alignof); IDENT(__alignof__);
508 IDENT(if); IDENT(else); IDENT(return);
509 IDENT(switch); IDENT(case); IDENT(default);
510 IDENT(break); IDENT(continue);
511 IDENT(for); IDENT(while); IDENT(do); IDENT(goto);
513 IDENT(__asm__); IDENT(__asm); IDENT(asm);
514 IDENT(__volatile__); IDENT(__volatile); IDENT(volatile);
515 IDENT(__attribute__); IDENT(__attribute);
516 IDENT(defined);
518 __IDENT(pragma, "__pragma__");
520 struct ident __VA_ARGS___ident = __INIT_IDENT("__VA_ARGS__");
521 struct ident __LINE___ident = __INIT_IDENT("__LINE__");
522 struct ident __FILE___ident = __INIT_IDENT("__FILE__");
524 void init_symbols(void)
526 int stream = init_stream("builtin", -1);
527 struct sym_init *ptr;
529 hash_ident(&sizeof_ident);
530 hash_ident(&alignof_ident);
531 hash_ident(&__alignof_ident);
532 hash_ident(&__alignof___ident);
533 hash_ident(&if_ident);
534 hash_ident(&else_ident);
535 hash_ident(&return_ident);
536 hash_ident(&switch_ident);
537 hash_ident(&case_ident);
538 hash_ident(&default_ident);
539 hash_ident(&break_ident);
540 hash_ident(&continue_ident);
541 hash_ident(&for_ident);
542 hash_ident(&while_ident);
543 hash_ident(&do_ident);
544 hash_ident(&goto_ident);
545 hash_ident(&__attribute___ident);
546 hash_ident(&__attribute_ident);
547 hash_ident(&__asm___ident);
548 hash_ident(&__asm_ident);
549 hash_ident(&asm_ident);
550 hash_ident(&__volatile___ident);
551 hash_ident(&__volatile_ident);
552 hash_ident(&volatile_ident);
553 hash_ident(&defined_ident);
554 hash_ident(&__LINE___ident);
555 hash_ident(&__FILE___ident);
556 hash_ident(&__VA_ARGS___ident);
557 hash_ident(&pragma_ident);
558 for (ptr = symbol_init_table; ptr->name; ptr++) {
559 struct symbol *sym;
560 sym = create_symbol(stream, ptr->name, SYM_NODE, NS_TYPEDEF);
561 sym->ctype.base_type = ptr->base_type;
562 sym->ctype.modifiers = ptr->modifiers;
565 builtin_fn_type.variadic = 1;
566 for (ptr = eval_init_table; ptr->name; ptr++) {
567 struct symbol *sym;
568 sym = create_symbol(stream, ptr->name, SYM_NODE, NS_SYMBOL);
569 sym->ctype.base_type = ptr->base_type;
570 sym->ctype.modifiers = ptr->modifiers;
571 sym->op = ptr->op;
575 void init_ctype(void)
577 struct ctype_declare *ctype;
579 ptr_ctype.type = SYM_PTR;
580 string_ctype.type = SYM_PTR;
581 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
582 struct symbol *sym = ctype->ptr;
583 unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
584 unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
585 unsigned long alignment = bit_size >> 3;
587 if (alignment > maxalign)
588 alignment = maxalign;
589 sym->bit_size = bit_size;
590 sym->ctype.alignment = alignment;
591 sym->ctype.base_type = ctype->base_type;
592 sym->ctype.modifiers = ctype->modifiers;