Fix indirect type parsing (functions, arrays, bitfields). Update
[smatch.git] / symbol.c
blob42454de72e8363f8026636b79c39c500ee2ff5cf
1 /*
2 * Symbol lookup and handling.
4 * Copyright (C) 2003 Linus Torvalds, all rights reserved.
5 */
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
11 #include "lib.h"
12 #include "token.h"
13 #include "parse.h"
14 #include "symbol.h"
15 #include "scope.h"
17 #include "target.h"
19 struct symbol *lookup_symbol(struct ident *ident, enum namespace ns)
21 struct symbol *sym;
23 for (sym = ident->symbols; sym; sym = sym->next_id) {
24 if (sym->namespace == ns)
25 return sym;
27 return sym;
30 struct symbol *alloc_symbol(struct token *token, int type)
32 struct symbol *sym = __alloc_symbol(0);
33 sym->type = type;
34 sym->token = token;
35 return sym;
38 struct struct_union_info {
39 int advance;
40 unsigned long max_align;
41 unsigned long bit_size;
42 unsigned long offset;
43 unsigned long bit_offset;
46 static void examine_one_member(struct symbol *sym, void *_info, int flags)
48 struct struct_union_info *info = _info;
49 unsigned long offset = info->offset;
50 unsigned long bit_size;
52 examine_symbol_type(sym);
53 if (sym->alignment > info->max_align)
54 info->max_align = sym->alignment;
55 if (info->advance) {
56 offset += sym->alignment-1;
57 offset &= ~(sym->alignment-1);
58 sym->offset = offset;
59 info->offset = offset + (sym->bit_size >> 3);
61 bit_size = (offset << 3) + sym->bit_size;
64 * In the case of a union, we want to get the _biggest_ size.
65 * For structures, this will always be true, since the offset
66 * ends up being cumulative.
68 if (bit_size > info->bit_size)
69 info->bit_size = bit_size;
72 static void examine_struct_union_type(struct symbol *sym, int advance)
74 struct struct_union_info info = { advance, 1, 0, 0 };
75 unsigned long bit_size, bit_align;
77 symbol_iterate(sym->symbol_list, examine_one_member, &info);
78 bit_size = info.bit_size;
79 bit_align = (info.max_align << 3)-1;
80 bit_size = (bit_size + bit_align) & ~bit_align;
81 sym->bit_size = bit_size;
82 sym->alignment = info.max_align;
85 static void examine_array_type(struct symbol *sym)
87 struct symbol *base_type = sym->ctype.base_type;
88 unsigned long bit_size, alignment;
90 if (!base_type)
91 return;
92 examine_symbol_type(base_type);
93 bit_size = base_type->bit_size * sym->array_size;
94 alignment = base_type->alignment;
95 if (!sym->alignment)
96 sym->alignment = alignment;
97 sym->bit_size = bit_size;
101 * Fill in type size and alignment information for
102 * regular SYM_TYPE things.
104 void examine_symbol_type(struct symbol * sym)
106 unsigned int bit_size, alignment;
107 struct symbol *base_type;
108 unsigned long modifiers;
110 if (!sym)
111 return;
113 /* Already done? */
114 if (sym->bit_size)
115 return;
117 switch (sym->type) {
118 case SYM_ARRAY:
119 examine_array_type(sym);
120 return;
121 case SYM_STRUCT:
122 examine_struct_union_type(sym, 1);
123 return;
124 case SYM_UNION:
125 examine_struct_union_type(sym, 0);
126 return;
127 case SYM_PTR:
128 if (!sym->bit_size)
129 sym->bit_size = BITS_IN_POINTER;
130 if (!sym->alignment)
131 sym->alignment = POINTER_ALIGNMENT;
132 examine_symbol_type(sym->ctype.base_type);
133 return;
134 case SYM_ENUM:
135 if (!sym->bit_size)
136 sym->bit_size = BITS_IN_ENUM;
137 if (!sym->alignment)
138 sym->alignment = ENUM_ALIGNMENT;
139 return;
140 case SYM_BASETYPE:
141 /* Size and alignment had better already be set up */
142 return;
144 default:
145 break;
148 /* SYM_NODE - figure out what the type of the node was.. */
149 base_type = sym->ctype.base_type;
150 modifiers = sym->ctype.modifiers;
152 if (base_type) {
153 examine_symbol_type(base_type);
155 bit_size = base_type->bit_size;
156 alignment = base_type->alignment;
157 } else
158 bit_size = 0;
160 if (!sym->alignment)
161 sym->alignment = alignment;
162 sym->bit_size = bit_size;
165 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
167 if (sym->id_list) {
168 warn(sym->token, "internal error: symbol type already bound");
169 return;
171 sym->namespace = ns;
172 sym->next_id = ident->symbols;
173 ident->symbols = sym;
174 sym->id_list = &ident->symbols;
175 bind_scope(sym);
178 struct symbol *create_symbol(int stream, const char *name, int type)
180 struct token *token = built_in_token(stream, name);
181 struct symbol *sym = alloc_symbol(token, type);
182 bind_symbol(sym, token->ident, NS_TYPEDEF);
183 return sym;
187 * Type and storage class keywords need to have the symbols
188 * created for them, so that the parser can have enough semantic
189 * information to do parsing.
191 * "double" == "long float", "long double" == "long long float"
193 struct sym_init {
194 const char *name;
195 struct symbol *base_type;
196 unsigned int modifiers;
197 } symbol_init_table[] = {
198 /* Storage class */
199 { "auto", NULL, MOD_AUTO },
200 { "register", NULL, MOD_REGISTER },
201 { "static", NULL, MOD_STATIC },
202 { "extern", NULL, MOD_EXTERN },
204 /* Type specifiers */
205 { "void", &void_ctype, 0 },
206 { "char", &int_type, MOD_CHAR },
207 { "short", &int_type, MOD_SHORT },
208 { "int", &int_type, 0 },
209 { "long", NULL, MOD_LONG },
210 { "float", &fp_type, 0 },
211 { "double", &fp_type, MOD_LONG },
212 { "signed", &int_type, MOD_SIGNED },
213 { "__signed", &int_type, MOD_SIGNED },
214 { "__signed__", &int_type, MOD_SIGNED },
215 { "unsigned", &int_type, MOD_UNSIGNED },
217 /* Type qualifiers */
218 { "const", NULL, MOD_CONST },
219 { "__const", NULL, MOD_CONST },
220 { "__const__", NULL, MOD_CONST },
221 { "volatile", NULL, MOD_VOLATILE },
223 /* Predeclared types */
224 { "__builtin_va_list", &int_type, 0 },
226 /* Typedef.. */
227 { "typedef", NULL, MOD_TYPEDEF },
229 /* Extended types */
230 { "typeof", NULL, MOD_TYPEOF },
231 { "__typeof", NULL, MOD_TYPEOF },
232 { "__typeof__", NULL, MOD_TYPEOF },
234 { "attribute", NULL, MOD_ATTRIBUTE },
235 { "__attribute", NULL, MOD_ATTRIBUTE },
236 { "__attribute__", NULL, MOD_ATTRIBUTE },
238 { "struct", NULL, MOD_STRUCTOF },
239 { "union", NULL, MOD_UNIONOF },
240 { "enum", NULL, MOD_ENUMOF },
242 /* Ignored for now.. */
243 { "inline", NULL, 0 },
244 { "__inline", NULL, 0 },
245 { "__inline__", NULL, 0 },
246 { "restrict", NULL, 0 },
247 { "__restrict", NULL, 0 },
249 { NULL, NULL, 0 }
253 * Abstract types
255 struct symbol int_type,
256 fp_type,
257 vector_type,
258 bad_type;
261 * C types (ie actual instances that the abstract types
262 * can map onto)
264 struct symbol bool_ctype, void_ctype,
265 char_ctype, uchar_ctype,
266 short_ctype, ushort_ctype,
267 int_ctype, uint_ctype,
268 long_ctype, ulong_ctype,
269 llong_ctype, ullong_ctype,
270 float_ctype, double_ctype, ldouble_ctype,
271 string_ctype;
273 struct ctype_declare {
274 struct symbol *ptr;
275 unsigned long modifiers;
276 unsigned long bit_size;
277 unsigned long maxalign;
278 struct symbol *base_type;
279 } ctype_declaration[] = {
280 { &bool_ctype, 0, BITS_IN_INT, MAX_INT_ALIGNMENT, &int_type },
281 { &void_ctype, 0, -1, 0, &void_ctype },
283 { &char_ctype, MOD_SIGNED | MOD_CHAR, BITS_IN_CHAR, MAX_INT_ALIGNMENT, &int_type },
284 { &uchar_ctype, MOD_UNSIGNED | MOD_CHAR, BITS_IN_CHAR, MAX_INT_ALIGNMENT, &int_type },
285 { &short_ctype, MOD_SIGNED | MOD_SHORT, BITS_IN_SHORT, MAX_INT_ALIGNMENT, &int_type },
286 { &ushort_ctype, MOD_UNSIGNED | MOD_SHORT, BITS_IN_SHORT, MAX_INT_ALIGNMENT, &int_type },
287 { &int_ctype, 0, BITS_IN_INT, MAX_INT_ALIGNMENT, &int_type },
288 { &uint_ctype, MOD_UNSIGNED, BITS_IN_INT, MAX_INT_ALIGNMENT, &int_type },
289 { &long_ctype, MOD_SIGNED | MOD_LONG, BITS_IN_LONG, MAX_INT_ALIGNMENT, &int_type },
290 { &ulong_ctype, MOD_UNSIGNED | MOD_LONG, BITS_IN_LONG, MAX_INT_ALIGNMENT, &int_type },
291 { &llong_ctype, MOD_SIGNED | MOD_LONG | MOD_LONGLONG, BITS_IN_LONGLONG, MAX_INT_ALIGNMENT, &int_type },
292 { &ullong_ctype, MOD_UNSIGNED | MOD_LONG | MOD_LONGLONG, BITS_IN_LONGLONG, MAX_INT_ALIGNMENT, &int_type },
294 { &float_ctype, 0, BITS_IN_FLOAT, MAX_FP_ALIGNMENT, &fp_type },
295 { &double_ctype, MOD_LONG, BITS_IN_DOUBLE, MAX_FP_ALIGNMENT, &fp_type },
296 { &ldouble_ctype,MOD_LONG | MOD_LONGLONG, BITS_IN_LONGDOUBLE,MAX_FP_ALIGNMENT, &fp_type },
298 { &string_ctype, 0, BITS_IN_POINTER, POINTER_ALIGNMENT, &char_ctype },
299 { NULL, }
303 #define __IDENT(n,str) \
304 struct ident n ## _ident = { len: sizeof(str)-1, name: str }
305 #define IDENT(n) __IDENT(n, #n)
307 IDENT(struct); IDENT(union); IDENT(enum);
308 IDENT(sizeof);
309 IDENT(alignof); IDENT(__alignof); IDENT(__alignof__);
310 IDENT(if); IDENT(else); IDENT(return);
311 IDENT(switch); IDENT(case); IDENT(default);
312 IDENT(break); IDENT(continue);
313 IDENT(for); IDENT(while); IDENT(do); IDENT(goto);
315 IDENT(__asm__); IDENT(__asm); IDENT(asm);
316 IDENT(__volatile__); IDENT(__volatile); IDENT(volatile);
317 IDENT(__attribute__); IDENT(__attribute);
319 void init_symbols(void)
321 int stream = init_stream("builtin", -1);
322 struct sym_init *ptr;
323 struct ctype_declare *ctype;
325 hash_ident(&sizeof_ident);
326 hash_ident(&alignof_ident);
327 hash_ident(&__alignof_ident);
328 hash_ident(&__alignof___ident);
329 hash_ident(&if_ident);
330 hash_ident(&else_ident);
331 hash_ident(&return_ident);
332 hash_ident(&switch_ident);
333 hash_ident(&case_ident);
334 hash_ident(&default_ident);
335 hash_ident(&break_ident);
336 hash_ident(&continue_ident);
337 hash_ident(&for_ident);
338 hash_ident(&while_ident);
339 hash_ident(&do_ident);
340 hash_ident(&goto_ident);
341 hash_ident(&__attribute___ident);
342 hash_ident(&__attribute_ident);
343 hash_ident(&__asm___ident);
344 hash_ident(&__asm_ident);
345 hash_ident(&asm_ident);
346 hash_ident(&__volatile___ident);
347 hash_ident(&__volatile_ident);
348 hash_ident(&volatile_ident);
349 for (ptr = symbol_init_table; ptr->name; ptr++) {
350 struct symbol *sym;
351 sym = create_symbol(stream, ptr->name, SYM_NODE);
352 sym->ctype.base_type = ptr->base_type;
353 sym->ctype.modifiers = ptr->modifiers;
356 string_ctype.type = SYM_PTR;
357 for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
358 struct symbol *sym = ctype->ptr;
359 unsigned long bit_size = ctype->bit_size;
360 unsigned long alignment = bit_size >> 3;
362 if (alignment > ctype->maxalign)
363 alignment = ctype->maxalign;
364 sym->bit_size = bit_size;
365 sym->alignment = alignment;
366 sym->ctype.base_type = ctype->base_type;
367 sym->ctype.modifiers = ctype->modifiers;