Clean up for/while/do parsing by separating them out
[smatch.git] / symbol.c
blob64c5971434b31264601a6844993dcf790e93b521
1 /*
2 * Symbol lookup and handling.
4 * Copyright (C) 2003 Transmeta Corp.
6 * Licensed under the Open Software License version 1.1
7 */
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
13 #include "lib.h"
14 #include "token.h"
15 #include "parse.h"
16 #include "symbol.h"
17 #include "scope.h"
18 #include "expression.h"
20 #include "target.h"
23 * Secondary symbol list for stuff that needs to be output because it
24 * was used.
26 struct symbol_list *used_list = NULL;
29 * If the symbol is an inline symbol, add it to the list of symbols to parse
31 void access_symbol(struct symbol *sym)
33 if (sym->ctype.modifiers & MOD_INLINE) {
34 if (!(sym->ctype.modifiers & MOD_ACCESSED)) {
35 add_symbol(&used_list, sym);
36 sym->ctype.modifiers |= MOD_ACCESSED;
41 struct symbol *lookup_symbol(struct ident *ident, enum namespace ns)
43 struct symbol *sym;
45 for (sym = ident->symbols; sym; sym = sym->next_id) {
46 if (sym->namespace == ns) {
47 sym->used = 1;
48 return sym;
51 return sym;
54 struct symbol *alloc_symbol(struct position pos, int type)
56 struct symbol *sym = __alloc_symbol(0);
57 sym->type = type;
58 sym->pos = pos;
59 return sym;
62 struct struct_union_info {
63 unsigned long max_align;
64 unsigned long bit_size;
68 * Unions are easy to lay out ;)
70 static void lay_out_union(struct symbol *sym, void *_info, int flags)
72 struct struct_union_info *info = _info;
74 examine_symbol_type(sym);
75 if (sym->ctype.alignment > info->max_align)
76 info->max_align = sym->ctype.alignment;
77 if (sym->bit_size > info->bit_size)
78 info->bit_size = sym->bit_size;
80 sym->offset = 0;
84 * Structures are a bit more interesting to lay out
86 static void lay_out_struct(struct symbol *sym, void *_info, int flags)
88 struct struct_union_info *info = _info;
89 unsigned long bit_size, base_size;
90 unsigned long align_bit_mask;
92 examine_symbol_type(sym);
93 if (sym->ctype.alignment > info->max_align)
94 info->max_align = sym->ctype.alignment;
96 bit_size = info->bit_size;
97 base_size = sym->bit_size;
98 align_bit_mask = (sym->ctype.alignment << 3) - 1;
101 * Bitfields have some very special rules..
103 if (sym->fieldwidth) {
104 unsigned long bit_offset = bit_size & align_bit_mask;
106 if (bit_offset + sym->fieldwidth > base_size) {
107 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
108 bit_offset = 0;
110 sym->offset = (bit_size - bit_offset) >> 3;
111 sym->bit_offset = bit_offset;
112 info->bit_size = bit_size + sym->fieldwidth;
113 return;
117 * Otherwise, just align it right and add it up..
119 bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
120 sym->offset = bit_size >> 3;
122 info->bit_size = bit_size + sym->bit_size;
125 static void examine_struct_union_type(struct symbol *sym, int advance)
127 struct struct_union_info info = { 1, 0 };
128 unsigned long bit_size, bit_align;
129 void (*fn)(struct symbol *, void *, int);
131 fn = advance ? lay_out_struct : lay_out_union;
132 symbol_iterate(sym->symbol_list, fn, &info);
134 if (!sym->ctype.alignment)
135 sym->ctype.alignment = info.max_align;
136 bit_size = info.bit_size;
137 bit_align = (sym->ctype.alignment << 3)-1;
138 bit_size = (bit_size + bit_align) & ~bit_align;
139 sym->bit_size = bit_size;
142 static void examine_array_type(struct symbol *sym)
144 struct symbol *base_type = sym->ctype.base_type;
145 unsigned long bit_size, alignment;
147 if (!base_type)
148 return;
149 examine_symbol_type(base_type);
150 bit_size = base_type->bit_size * sym->array_size;
151 if (sym->array_size < 0)
152 bit_size = -1;
153 alignment = base_type->ctype.alignment;
154 if (!sym->ctype.alignment)
155 sym->ctype.alignment = alignment;
156 sym->bit_size = bit_size;
159 static void examine_bitfield_type(struct symbol *sym)
161 struct symbol *base_type = sym->ctype.base_type;
162 unsigned long bit_size, alignment;
164 if (!base_type)
165 return;
166 examine_symbol_type(base_type);
167 bit_size = base_type->bit_size;
168 if (sym->fieldwidth > bit_size) {
169 warn(sym->pos, "impossible field-width for this type");
170 sym->fieldwidth = bit_size;
172 alignment = base_type->ctype.alignment;
173 if (!sym->ctype.alignment)
174 sym->ctype.alignment = alignment;
175 sym->bit_size = bit_size;
179 * "typeof" will have to merge the types together
181 void merge_type(struct symbol *sym, struct symbol *base_type)
183 sym->ctype.as |= base_type->ctype.as;
184 sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
185 sym->ctype.context |= base_type->ctype.context;
186 sym->ctype.contextmask |= base_type->ctype.contextmask;
187 sym->ctype.base_type = base_type->ctype.base_type;
191 * Fill in type size and alignment information for
192 * regular SYM_TYPE things.
194 struct symbol *examine_symbol_type(struct symbol * sym)
196 unsigned int bit_size, alignment;
197 struct symbol *base_type;
198 unsigned long modifiers;
200 if (!sym)
201 return sym;
203 /* Already done? */
204 if (sym->bit_size)
205 return sym;
207 switch (sym->type) {
208 case SYM_ARRAY:
209 examine_array_type(sym);
210 return sym;
211 case SYM_STRUCT:
212 examine_struct_union_type(sym, 1);
213 return sym;
214 case SYM_UNION:
215 examine_struct_union_type(sym, 0);
216 return sym;
217 case SYM_PTR:
218 if (!sym->bit_size)
219 sym->bit_size = BITS_IN_POINTER;
220 if (!sym->ctype.alignment)
221 sym->ctype.alignment = POINTER_ALIGNMENT;
222 base_type = sym->ctype.base_type;
223 base_type = examine_symbol_type(base_type);
224 if (base_type && base_type->type == SYM_NODE)
225 merge_type(sym, base_type);
226 return sym;
227 case SYM_ENUM:
228 if (!sym->bit_size)
229 sym->bit_size = BITS_IN_ENUM;
230 if (!sym->ctype.alignment)
231 sym->ctype.alignment = ENUM_ALIGNMENT;
232 return sym;
233 case SYM_BITFIELD:
234 examine_bitfield_type(sym);
235 return sym;
236 case SYM_BASETYPE:
237 /* Size and alignment had better already be set up */
238 return sym;
239 case SYM_TYPEOF: {
240 struct symbol *base = evaluate_expression(sym->initializer);
241 if (base)
242 return base;
243 break;
245 default:
246 break;
249 /* SYM_NODE - figure out what the type of the node was.. */
250 base_type = sym->ctype.base_type;
251 modifiers = sym->ctype.modifiers;
253 if (base_type) {
254 base_type = examine_symbol_type(base_type);
255 sym->ctype.base_type = base_type;
256 if (base_type && base_type->type == SYM_NODE)
257 merge_type(sym, base_type);
259 bit_size = base_type->bit_size;
260 alignment = base_type->ctype.alignment;
261 if (base_type->fieldwidth)
262 sym->fieldwidth = base_type->fieldwidth;
263 } else
264 bit_size = 0;
266 if (!sym->ctype.alignment)
267 sym->ctype.alignment = alignment;
268 sym->bit_size = bit_size;
269 return sym;
272 void check_declaration(struct symbol *sym)
274 struct symbol *next = sym;
276 while ((next = next->next_id) != NULL) {
277 if (next->namespace != sym->namespace)
278 continue;
279 if (sym->scope == next->scope) {
280 sym->same_symbol = next;
281 return;
283 if (sym->ctype.modifiers & next->ctype.modifiers & MOD_EXTERN) {
284 sym->same_symbol = next;
285 return;
287 #if 0
288 // This may make sense from a warning standpoint:
289 // consider top-level symbols to clash with everything
290 // (but the scoping rules will mean that we actually
291 // _use_ the innermost version)
292 if (toplevel(next->scope)) {
293 sym->same_symbol = next;
294 return;
296 #endif
300 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
302 struct scope *scope;
303 if (sym->id_list) {
304 warn(sym->pos, "internal error: symbol type already bound");
305 return;
307 sym->namespace = ns;
308 sym->next_id = ident->symbols;
309 ident->symbols = sym;
310 sym->id_list = &ident->symbols;
312 scope = block_scope;
313 if (toplevel(scope)) {
314 sym->ctype.modifiers |= MOD_TOPLEVEL;
315 if (sym->ctype.modifiers & MOD_STATIC)
316 scope = file_scope;
318 if (ns == NS_LABEL)
319 scope = function_scope;
320 bind_scope(sym, scope);
323 struct symbol *create_symbol(int stream, const char *name, int type)
325 struct token *token = built_in_token(stream, name);
326 struct symbol *sym = alloc_symbol(token->pos, type);
327 bind_symbol(sym, token->ident, NS_TYPEDEF);
328 return sym;
332 * Type and storage class keywords need to have the symbols
333 * created for them, so that the parser can have enough semantic
334 * information to do parsing.
336 * "double" == "long float", "long double" == "long long float"
338 struct sym_init {
339 const char *name;
340 struct symbol *base_type;
341 unsigned int modifiers;
342 } symbol_init_table[] = {
343 /* Storage class */
344 { "auto", NULL, MOD_AUTO },
345 { "register", NULL, MOD_REGISTER },
346 { "static", NULL, MOD_STATIC },
347 { "extern", NULL, MOD_EXTERN },
349 /* Type specifiers */
350 { "void", &void_ctype, 0 },
351 { "char", &int_type, MOD_CHAR },
352 { "short", &int_type, MOD_SHORT },
353 { "int", &int_type, 0 },
354 { "long", NULL, MOD_LONG },
355 { "float", &fp_type, 0 },
356 { "double", &fp_type, MOD_LONG },
357 { "signed", &int_type, MOD_SIGNED },
358 { "__signed", &int_type, MOD_SIGNED },
359 { "__signed__", &int_type, MOD_SIGNED },
360 { "unsigned", &int_type, MOD_UNSIGNED },
362 /* Type qualifiers */
363 { "const", NULL, MOD_CONST },
364 { "__const", NULL, MOD_CONST },
365 { "__const__", NULL, MOD_CONST },
366 { "volatile", NULL, MOD_VOLATILE },
367 { "__volatile", NULL, MOD_VOLATILE },
368 { "__volatile__", NULL, MOD_VOLATILE },
370 /* Predeclared types */
371 { "__builtin_va_list", &int_type, 0 },
373 /* Typedef.. */
374 { "typedef", NULL, MOD_TYPEDEF },
376 /* Extended types */
377 { "typeof", NULL, MOD_TYPEOF },
378 { "__typeof", NULL, MOD_TYPEOF },
379 { "__typeof__", NULL, MOD_TYPEOF },
381 #if 0
382 { "attribute", NULL, MOD_ATTRIBUTE },
383 { "__attribute", NULL, MOD_ATTRIBUTE },
384 #endif
385 { "__attribute__", NULL, MOD_ATTRIBUTE },
387 { "struct", NULL, MOD_STRUCTOF },
388 { "union", NULL, MOD_UNIONOF },
389 { "enum", NULL, MOD_ENUMOF },
391 { "inline", NULL, MOD_INLINE },
392 { "__inline", NULL, MOD_INLINE },
393 { "__inline__", NULL, MOD_INLINE },
395 /* Ignored for now.. */
396 { "restrict", NULL, 0 },
397 { "__restrict", NULL, 0 },
399 { NULL, NULL, 0 }
403 * Abstract types
405 struct symbol int_type,
406 fp_type,
407 vector_type,
408 bad_type;
411 * C types (ie actual instances that the abstract types
412 * can map onto)
414 struct symbol bool_ctype, void_ctype,
415 char_ctype, uchar_ctype,
416 short_ctype, ushort_ctype,
417 int_ctype, uint_ctype,
418 long_ctype, ulong_ctype,
419 llong_ctype, ullong_ctype,
420 float_ctype, double_ctype, ldouble_ctype,
421 string_ctype, ptr_ctype;
423 struct ctype_declare {
424 struct symbol *ptr;
425 unsigned long modifiers;
426 unsigned long bit_size;
427 unsigned long maxalign;
428 struct symbol *base_type;
429 } ctype_declaration[] = {
430 { &bool_ctype, 0, BITS_IN_INT, MAX_INT_ALIGNMENT, &int_type },
431 { &void_ctype, 0, -1, 0, NULL },
433 { &char_ctype, MOD_SIGNED | MOD_CHAR, BITS_IN_CHAR, MAX_INT_ALIGNMENT, &int_type },
434 { &uchar_ctype, MOD_UNSIGNED | MOD_CHAR, BITS_IN_CHAR, MAX_INT_ALIGNMENT, &int_type },
435 { &short_ctype, MOD_SIGNED | MOD_SHORT, BITS_IN_SHORT, MAX_INT_ALIGNMENT, &int_type },
436 { &ushort_ctype, MOD_UNSIGNED | MOD_SHORT, BITS_IN_SHORT, MAX_INT_ALIGNMENT, &int_type },
437 { &int_ctype, MOD_SIGNED, BITS_IN_INT, MAX_INT_ALIGNMENT, &int_type },
438 { &uint_ctype, MOD_UNSIGNED, BITS_IN_INT, MAX_INT_ALIGNMENT, &int_type },
439 { &long_ctype, MOD_SIGNED | MOD_LONG, BITS_IN_LONG, MAX_INT_ALIGNMENT, &int_type },
440 { &ulong_ctype, MOD_UNSIGNED | MOD_LONG, BITS_IN_LONG, MAX_INT_ALIGNMENT, &int_type },
441 { &llong_ctype, MOD_SIGNED | MOD_LONG | MOD_LONGLONG, BITS_IN_LONGLONG, MAX_INT_ALIGNMENT, &int_type },
442 { &ullong_ctype, MOD_UNSIGNED | MOD_LONG | MOD_LONGLONG, BITS_IN_LONGLONG, MAX_INT_ALIGNMENT, &int_type },
444 { &float_ctype, 0, BITS_IN_FLOAT, MAX_FP_ALIGNMENT, &fp_type },
445 { &double_ctype, MOD_LONG, BITS_IN_DOUBLE, MAX_FP_ALIGNMENT, &fp_type },
446 { &ldouble_ctype,MOD_LONG | MOD_LONGLONG, BITS_IN_LONGDOUBLE,MAX_FP_ALIGNMENT, &fp_type },
448 { &string_ctype, 0, BITS_IN_POINTER, POINTER_ALIGNMENT, &char_ctype },
449 { &ptr_ctype, 0, BITS_IN_POINTER, POINTER_ALIGNMENT, &void_ctype },
450 { NULL, }
454 #define __IDENT(n,str) \
455 struct ident n ## _ident = { len: sizeof(str)-1, name: str }
456 #define IDENT(n) __IDENT(n, #n)
458 IDENT(struct); IDENT(union); IDENT(enum);
459 IDENT(sizeof);
460 IDENT(alignof); IDENT(__alignof); IDENT(__alignof__);
461 IDENT(if); IDENT(else); IDENT(return);
462 IDENT(switch); IDENT(case); IDENT(default);
463 IDENT(break); IDENT(continue);
464 IDENT(for); IDENT(while); IDENT(do); IDENT(goto);
466 IDENT(__asm__); IDENT(__asm); IDENT(asm);
467 IDENT(__volatile__); IDENT(__volatile); IDENT(volatile);
468 IDENT(__attribute__); IDENT(__attribute);
470 void init_symbols(void)
472 int stream = init_stream("builtin", -1);
473 struct sym_init *ptr;
474 struct ctype_declare *ctype;
476 hash_ident(&sizeof_ident);
477 hash_ident(&alignof_ident);
478 hash_ident(&__alignof_ident);
479 hash_ident(&__alignof___ident);
480 hash_ident(&if_ident);
481 hash_ident(&else_ident);
482 hash_ident(&return_ident);
483 hash_ident(&switch_ident);
484 hash_ident(&case_ident);
485 hash_ident(&default_ident);
486 hash_ident(&break_ident);
487 hash_ident(&continue_ident);
488 hash_ident(&for_ident);
489 hash_ident(&while_ident);
490 hash_ident(&do_ident);
491 hash_ident(&goto_ident);
492 hash_ident(&__attribute___ident);
493 hash_ident(&__attribute_ident);
494 hash_ident(&__asm___ident);
495 hash_ident(&__asm_ident);
496 hash_ident(&asm_ident);
497 hash_ident(&__volatile___ident);
498 hash_ident(&__volatile_ident);
499 hash_ident(&volatile_ident);
500 for (ptr = symbol_init_table; ptr->name; ptr++) {
501 struct symbol *sym;
502 sym = create_symbol(stream, ptr->name, SYM_NODE);
503 sym->ctype.base_type = ptr->base_type;
504 sym->ctype.modifiers = ptr->modifiers;
507 ptr_ctype.type = SYM_PTR;
508 string_ctype.type = SYM_PTR;
509 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
510 struct symbol *sym = ctype->ptr;
511 unsigned long bit_size = ctype->bit_size;
512 unsigned long alignment = bit_size >> 3;
514 if (alignment > ctype->maxalign)
515 alignment = ctype->maxalign;
516 sym->bit_size = bit_size;
517 sym->ctype.alignment = alignment;
518 sym->ctype.base_type = ctype->base_type;
519 sym->ctype.modifiers = ctype->modifiers;