xref: /illumos-gate/usr/src/tools/smatch/src/symbol.c (revision 2bc92914)
1 /*
2  * Symbol lookup and handling.
3  *
4  * Copyright (C) 2003 Transmeta Corp.
5  *               2003-2004 Linus Torvalds
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 
29 #include "lib.h"
30 #include "allocate.h"
31 #include "token.h"
32 #include "parse.h"
33 #include "symbol.h"
34 #include "scope.h"
35 #include "expression.h"
36 
37 #include "target.h"
38 
39 /*
40  * Secondary symbol list for stuff that needs to be output because it
41  * was used.
42  */
43 struct symbol_list *translation_unit_used_list = NULL;
44 
45 /*
46  * If the symbol is an inline symbol, add it to the list of symbols to parse
47  */
48 void access_symbol(struct symbol *sym)
49 {
50 	if (sym->ctype.modifiers & MOD_INLINE) {
51 		if (!(sym->ctype.modifiers & MOD_ACCESSED)) {
52 			add_symbol(&translation_unit_used_list, sym);
53 			sym->ctype.modifiers |= MOD_ACCESSED;
54 		}
55 	}
56 }
57 
58 struct symbol *lookup_symbol(struct ident *ident, enum namespace ns)
59 {
60 	struct symbol *sym;
61 
62 	for (sym = ident->symbols; sym; sym = sym->next_id) {
63 		if (sym->namespace & ns) {
64 			sym->used = 1;
65 			return sym;
66 		}
67 	}
68 	return NULL;
69 }
70 
71 struct context *alloc_context(void)
72 {
73 	return __alloc_context(0);
74 }
75 
76 struct symbol *alloc_symbol(struct position pos, int type)
77 {
78 	struct symbol *sym = __alloc_symbol(0);
79 	sym->type = type;
80 	sym->pos = pos;
81 	sym->endpos.type = 0;
82 	return sym;
83 }
84 
85 struct struct_union_info {
86 	unsigned long max_align;
87 	unsigned long bit_size;
88 	int align_size;
89 };
90 
91 /*
92  * Unions are fairly easy to lay out ;)
93  */
94 static void lay_out_union(struct symbol *sym, struct struct_union_info *info)
95 {
96 	examine_symbol_type(sym);
97 
98 	// Unnamed bitfields do not affect alignment.
99 	if (sym->ident || !is_bitfield_type(sym)) {
100 		if (sym->ctype.alignment > info->max_align)
101 			info->max_align = sym->ctype.alignment;
102 	}
103 
104 	if (sym->bit_size > info->bit_size)
105 		info->bit_size = sym->bit_size;
106 
107 	sym->offset = 0;
108 }
109 
110 static int bitfield_base_size(struct symbol *sym)
111 {
112 	if (sym->type == SYM_NODE)
113 		sym = sym->ctype.base_type;
114 	if (sym->type == SYM_BITFIELD)
115 		sym = sym->ctype.base_type;
116 	return sym->bit_size;
117 }
118 
119 /*
120  * Structures are a bit more interesting to lay out
121  */
122 static void lay_out_struct(struct symbol *sym, struct struct_union_info *info)
123 {
124 	unsigned long bit_size, align_bit_mask;
125 	int base_size;
126 
127 	examine_symbol_type(sym);
128 
129 	// Unnamed bitfields do not affect alignment.
130 	if (sym->ident || !is_bitfield_type(sym)) {
131 		if (sym->ctype.alignment > info->max_align)
132 			info->max_align = sym->ctype.alignment;
133 	}
134 
135 	bit_size = info->bit_size;
136 	base_size = sym->bit_size;
137 
138 	/*
139 	 * Unsized arrays cause us to not align the resulting
140 	 * structure size
141 	 */
142 	if (base_size < 0) {
143 		info->align_size = 0;
144 		base_size = 0;
145 	}
146 
147 	align_bit_mask = bytes_to_bits(sym->ctype.alignment) - 1;
148 
149 	/*
150 	 * Bitfields have some very special rules..
151 	 */
152 	if (is_bitfield_type (sym)) {
153 		unsigned long bit_offset = bit_size & align_bit_mask;
154 		int room = bitfield_base_size(sym) - bit_offset;
155 		// Zero-width fields just fill up the unit.
156 		int width = base_size ? : (bit_offset ? room : 0);
157 
158 		if (width > room) {
159 			bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
160 			bit_offset = 0;
161 		}
162 		sym->offset = bits_to_bytes(bit_size - bit_offset);
163 		sym->bit_offset = bit_offset;
164 		sym->ctype.base_type->bit_offset = bit_offset;
165 		info->bit_size = bit_size + width;
166 		// warning (sym->pos, "bitfield: offset=%d:%d  size=:%d", sym->offset, sym->bit_offset, width);
167 
168 		return;
169 	}
170 
171 	/*
172 	 * Otherwise, just align it right and add it up..
173 	 */
174 	bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
175 	sym->offset = bits_to_bytes(bit_size);
176 
177 	info->bit_size = bit_size + base_size;
178 	// warning (sym->pos, "regular: offset=%d", sym->offset);
179 }
180 
181 static struct symbol * examine_struct_union_type(struct symbol *sym, int advance)
182 {
183 	struct struct_union_info info = {
184 		.max_align = 1,
185 		.bit_size = 0,
186 		.align_size = 1
187 	};
188 	unsigned long bit_size, bit_align;
189 	void (*fn)(struct symbol *, struct struct_union_info *);
190 	struct symbol *member;
191 
192 	fn = advance ? lay_out_struct : lay_out_union;
193 	FOR_EACH_PTR(sym->symbol_list, member) {
194 		fn(member, &info);
195 	} END_FOR_EACH_PTR(member);
196 
197 	if (!sym->ctype.alignment)
198 		sym->ctype.alignment = info.max_align;
199 	bit_size = info.bit_size;
200 	if (info.align_size) {
201 		bit_align = bytes_to_bits(sym->ctype.alignment)-1;
202 		bit_size = (bit_size + bit_align) & ~bit_align;
203 	}
204 	sym->bit_size = bit_size;
205 	return sym;
206 }
207 
208 static struct symbol *examine_base_type(struct symbol *sym)
209 {
210 	struct symbol *base_type;
211 
212 	/* Check the base type */
213 	base_type = examine_symbol_type(sym->ctype.base_type);
214 	if (!base_type || base_type->type == SYM_PTR)
215 		return base_type;
216 	sym->ctype.as |= base_type->ctype.as;
217 	sym->ctype.modifiers |= base_type->ctype.modifiers & MOD_PTRINHERIT;
218 	concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
219 			(struct ptr_list **)&sym->ctype.contexts);
220 	if (base_type->type == SYM_NODE) {
221 		base_type = base_type->ctype.base_type;
222 		sym->ctype.base_type = base_type;
223 	}
224 	return base_type;
225 }
226 
227 static struct symbol * examine_array_type(struct symbol *sym)
228 {
229 	struct symbol *base_type = examine_base_type(sym);
230 	unsigned long bit_size = -1, alignment;
231 	struct expression *array_size = sym->array_size;
232 
233 	if (!base_type)
234 		return sym;
235 
236 	if (array_size) {
237 		bit_size = array_element_offset(base_type->bit_size,
238 						get_expression_value_silent(array_size));
239 		if (array_size->type != EXPR_VALUE) {
240 			if (Wvla)
241 				warning(array_size->pos, "Variable length array is used.");
242 			bit_size = -1;
243 		}
244 	}
245 	alignment = base_type->ctype.alignment;
246 	if (!sym->ctype.alignment)
247 		sym->ctype.alignment = alignment;
248 	sym->bit_size = bit_size;
249 	return sym;
250 }
251 
252 static struct symbol *examine_bitfield_type(struct symbol *sym)
253 {
254 	struct symbol *base_type = examine_base_type(sym);
255 	unsigned long bit_size, alignment, modifiers;
256 
257 	if (!base_type)
258 		return sym;
259 	bit_size = base_type->bit_size;
260 	if (sym->bit_size > bit_size)
261 		warning(sym->pos, "impossible field-width, %d, for this type",  sym->bit_size);
262 
263 	alignment = base_type->ctype.alignment;
264 	if (!sym->ctype.alignment)
265 		sym->ctype.alignment = alignment;
266 	modifiers = base_type->ctype.modifiers;
267 
268 	/* Bitfields are unsigned, unless the base type was explicitly signed */
269 	if (!(modifiers & MOD_EXPLICITLY_SIGNED))
270 		modifiers = (modifiers & ~MOD_SIGNED) | MOD_UNSIGNED;
271 	sym->ctype.modifiers |= modifiers & MOD_SIGNEDNESS;
272 	return sym;
273 }
274 
275 /*
276  * "typeof" will have to merge the types together
277  */
278 void merge_type(struct symbol *sym, struct symbol *base_type)
279 {
280 	sym->ctype.as |= base_type->ctype.as;
281 	sym->ctype.modifiers |= (base_type->ctype.modifiers & ~MOD_STORAGE);
282 	concat_ptr_list((struct ptr_list *)base_type->ctype.contexts,
283 	                (struct ptr_list **)&sym->ctype.contexts);
284 	sym->ctype.base_type = base_type->ctype.base_type;
285 	if (sym->ctype.base_type->type == SYM_NODE)
286 		merge_type(sym, sym->ctype.base_type);
287 }
288 
289 static int count_array_initializer(struct symbol *t, struct expression *expr)
290 {
291 	int nr = 0;
292 	int is_char = 0;
293 
294 	/*
295 	 * Arrays of character types are special; they can be initialized by
296 	 * string literal _or_ by string literal in braces.  The latter means
297 	 * that with T x[] = {<string literal>} number of elements in x depends
298 	 * on T - if it's a character type, we get the length of string literal
299 	 * (including NUL), otherwise we have one element here.
300 	 */
301 	if (t->ctype.base_type == &int_type && t->ctype.modifiers & MOD_CHAR)
302 		is_char = 1;
303 
304 	switch (expr->type) {
305 	case EXPR_INITIALIZER: {
306 		struct expression *entry;
307 		int count = 0;
308 		int str_len = 0;
309 		FOR_EACH_PTR(expr->expr_list, entry) {
310 			count++;
311 			switch (entry->type) {
312 			case EXPR_INDEX:
313 				if (entry->idx_to >= nr)
314 					nr = entry->idx_to+1;
315 				break;
316 			case EXPR_PREOP: {
317 				struct expression *e = entry;
318 				if (is_char) {
319 					while (e && e->type == EXPR_PREOP && e->op == '(')
320 						e = e->unop;
321 					if (e && e->type == EXPR_STRING) {
322 						entry = e;
323 			case EXPR_STRING:
324 						if (is_char)
325 							str_len = entry->string->length;
326 					}
327 
328 
329 				}
330 			}
331 			default:
332 				nr++;
333 			}
334 		} END_FOR_EACH_PTR(entry);
335 		if (count == 1 && str_len)
336 			nr = str_len;
337 		break;
338 	}
339 	case EXPR_PREOP:
340 		if (is_char) {
341 			struct expression *e = expr;
342 			while (e && e->type == EXPR_PREOP && e->op == '(')
343 				e = e->unop;
344 			if (e && e->type == EXPR_STRING) {
345 				expr = e;
346 	case EXPR_STRING:
347 				if (is_char)
348 					nr = expr->string->length;
349 			}
350 		}
351 		break;
352 	default:
353 		break;
354 	}
355 	return nr;
356 }
357 
358 static struct expression *get_symbol_initializer(struct symbol *sym)
359 {
360 	do {
361 		if (sym->initializer)
362 			return sym->initializer;
363 	} while ((sym = sym->same_symbol) != NULL);
364 	return NULL;
365 }
366 
367 static struct symbol * examine_node_type(struct symbol *sym)
368 {
369 	struct symbol *base_type = examine_base_type(sym);
370 	int bit_size;
371 	unsigned long alignment;
372 
373 	/* SYM_NODE - figure out what the type of the node was.. */
374 	bit_size = 0;
375 	alignment = 0;
376 	if (!base_type)
377 		return sym;
378 
379 	bit_size = base_type->bit_size;
380 	alignment = base_type->ctype.alignment;
381 
382 	/* Pick up signedness information into the node */
383 	sym->ctype.modifiers |= (MOD_SIGNEDNESS & base_type->ctype.modifiers);
384 
385 	if (!sym->ctype.alignment)
386 		sym->ctype.alignment = alignment;
387 
388 	/* Unsized array? The size might come from the initializer.. */
389 	if (bit_size < 0 && base_type->type == SYM_ARRAY) {
390 		struct expression *initializer = get_symbol_initializer(sym);
391 		if (initializer) {
392 			struct symbol *node_type = base_type->ctype.base_type;
393 			int count = count_array_initializer(node_type, initializer);
394 
395 			if (node_type && node_type->bit_size >= 0)
396 				bit_size = array_element_offset(node_type->bit_size, count);
397 		}
398 	}
399 
400 	sym->bit_size = bit_size;
401 	return sym;
402 }
403 
404 static struct symbol *examine_enum_type(struct symbol *sym)
405 {
406 	struct symbol *base_type = examine_base_type(sym);
407 
408 	sym->ctype.modifiers |= (base_type->ctype.modifiers & MOD_SIGNEDNESS);
409 	sym->bit_size = bits_in_enum;
410 	if (base_type->bit_size > sym->bit_size)
411 		sym->bit_size = base_type->bit_size;
412 	sym->ctype.alignment = enum_alignment;
413 	if (base_type->ctype.alignment > sym->ctype.alignment)
414 		sym->ctype.alignment = base_type->ctype.alignment;
415 	return sym;
416 }
417 
418 static struct symbol *examine_pointer_type(struct symbol *sym)
419 {
420 	/*
421 	 * We need to set the pointer size first, and
422 	 * examine the thing we point to only afterwards.
423 	 * That's because this pointer type may end up
424 	 * being needed for the base type size evaluation.
425 	 */
426 	if (!sym->bit_size)
427 		sym->bit_size = bits_in_pointer;
428 	if (!sym->ctype.alignment)
429 		sym->ctype.alignment = pointer_alignment;
430 	return sym;
431 }
432 
433 /*
434  * Fill in type size and alignment information for
435  * regular SYM_TYPE things.
436  */
437 struct symbol *examine_symbol_type(struct symbol * sym)
438 {
439 	if (!sym)
440 		return sym;
441 
442 	/* Already done? */
443 	if (sym->examined)
444 		return sym;
445 	sym->examined = 1;
446 
447 	switch (sym->type) {
448 	case SYM_FN:
449 	case SYM_NODE:
450 		return examine_node_type(sym);
451 	case SYM_ARRAY:
452 		return examine_array_type(sym);
453 	case SYM_STRUCT:
454 		return examine_struct_union_type(sym, 1);
455 	case SYM_UNION:
456 		return examine_struct_union_type(sym, 0);
457 	case SYM_PTR:
458 		return examine_pointer_type(sym);
459 	case SYM_ENUM:
460 		return examine_enum_type(sym);
461 	case SYM_BITFIELD:
462 		return examine_bitfield_type(sym);
463 	case SYM_BASETYPE:
464 		/* Size and alignment had better already be set up */
465 		return sym;
466 	case SYM_TYPEOF: {
467 		struct symbol *base = evaluate_expression(sym->initializer);
468 		if (base) {
469 			unsigned long mod = 0;
470 
471 			if (is_bitfield_type(base))
472 				warning(base->pos, "typeof applied to bitfield type");
473 			if (base->type == SYM_NODE) {
474 				mod |= base->ctype.modifiers & MOD_TYPEOF;
475 				base = base->ctype.base_type;
476 			}
477 			sym->type = SYM_NODE;
478 			sym->ctype.modifiers = mod;
479 			sym->ctype.base_type = base;
480 			return examine_node_type(sym);
481 		}
482 		break;
483 	}
484 	case SYM_PREPROCESSOR:
485 		sparse_error(sym->pos, "ctype on preprocessor command? (%s)", show_ident(sym->ident));
486 		return NULL;
487 	case SYM_UNINITIALIZED:
488 //		sparse_error(sym->pos, "ctype on uninitialized symbol %p", sym);
489 		return NULL;
490 	case SYM_RESTRICT:
491 		examine_base_type(sym);
492 		return sym;
493 	case SYM_FOULED:
494 		examine_base_type(sym);
495 		return sym;
496 	default:
497 //		sparse_error(sym->pos, "Examining unknown symbol type %d", sym->type);
498 		break;
499 	}
500 	return sym;
501 }
502 
503 const char* get_type_name(enum type type)
504 {
505 	const char *type_lookup[] = {
506 	[SYM_UNINITIALIZED] = "uninitialized",
507 	[SYM_PREPROCESSOR] = "preprocessor",
508 	[SYM_BASETYPE] = "basetype",
509 	[SYM_NODE] = "node",
510 	[SYM_PTR] = "pointer",
511 	[SYM_FN] = "function",
512 	[SYM_ARRAY] = "array",
513 	[SYM_STRUCT] = "struct",
514 	[SYM_UNION] = "union",
515 	[SYM_ENUM] = "enum",
516 	[SYM_TYPEDEF] = "typedef",
517 	[SYM_TYPEOF] = "typeof",
518 	[SYM_MEMBER] = "member",
519 	[SYM_BITFIELD] = "bitfield",
520 	[SYM_LABEL] = "label",
521 	[SYM_RESTRICT] = "restrict",
522 	[SYM_FOULED] = "fouled",
523 	[SYM_KEYWORD] = "keyword",
524 	[SYM_BAD] = "bad"};
525 
526 	if (type <= SYM_BAD)
527 		return type_lookup[type];
528 	else
529 		return NULL;
530 }
531 
532 struct symbol *examine_pointer_target(struct symbol *sym)
533 {
534 	return examine_base_type(sym);
535 }
536 
537 static struct symbol_list *restr, *fouled;
538 
539 void create_fouled(struct symbol *type)
540 {
541 	if (type->bit_size < bits_in_int) {
542 		struct symbol *new = alloc_symbol(type->pos, type->type);
543 		*new = *type;
544 		new->bit_size = bits_in_int;
545 		new->type = SYM_FOULED;
546 		new->ctype.base_type = type;
547 		add_symbol(&restr, type);
548 		add_symbol(&fouled, new);
549 	}
550 }
551 
552 struct symbol *befoul(struct symbol *type)
553 {
554 	struct symbol *t1, *t2;
555 	while (type->type == SYM_NODE)
556 		type = type->ctype.base_type;
557 	PREPARE_PTR_LIST(restr, t1);
558 	PREPARE_PTR_LIST(fouled, t2);
559 	for (;;) {
560 		if (t1 == type)
561 			return t2;
562 		if (!t1)
563 			break;
564 		NEXT_PTR_LIST(t1);
565 		NEXT_PTR_LIST(t2);
566 	}
567 	FINISH_PTR_LIST(t2);
568 	FINISH_PTR_LIST(t1);
569 	return NULL;
570 }
571 
572 void check_declaration(struct symbol *sym)
573 {
574 	int warned = 0;
575 	struct symbol *next = sym;
576 
577 	while ((next = next->next_id) != NULL) {
578 		if (next->namespace != sym->namespace)
579 			continue;
580 		if (sym->scope == next->scope) {
581 			sym->same_symbol = next;
582 			return;
583 		}
584 		/* Extern in block level matches a TOPLEVEL non-static symbol */
585 		if (sym->ctype.modifiers & MOD_EXTERN) {
586 			if ((next->ctype.modifiers & (MOD_TOPLEVEL|MOD_STATIC)) == MOD_TOPLEVEL) {
587 				sym->same_symbol = next;
588 				return;
589 			}
590 		}
591 
592 		if (!Wshadow || warned)
593 			continue;
594 		if (get_sym_type(next) == SYM_FN)
595 			continue;
596 		warned = 1;
597 		warning(sym->pos, "symbol '%s' shadows an earlier one", show_ident(sym->ident));
598 		info(next->pos, "originally declared here");
599 	}
600 }
601 
602 void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
603 {
604 	struct scope *scope;
605 	if (sym->bound) {
606 		sparse_error(sym->pos, "internal error: symbol type already bound");
607 		return;
608 	}
609 	if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
610 		sparse_error(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
611 		return;
612 	}
613 	sym->namespace = ns;
614 	sym->next_id = ident->symbols;
615 	ident->symbols = sym;
616 	if (sym->ident && sym->ident != ident)
617 		warning(sym->pos, "Symbol '%s' already bound", show_ident(sym->ident));
618 	sym->ident = ident;
619 	sym->bound = 1;
620 
621 	scope = block_scope;
622 	if (ns == NS_SYMBOL && toplevel(scope)) {
623 		unsigned mod = MOD_ADDRESSABLE | MOD_TOPLEVEL;
624 
625 		scope = global_scope;
626 		if (sym->ctype.modifiers & MOD_STATIC ||
627 		    is_extern_inline(sym)) {
628 			scope = file_scope;
629 			mod = MOD_TOPLEVEL;
630 		}
631 		sym->ctype.modifiers |= mod;
632 	}
633 	if (ns == NS_MACRO)
634 		scope = file_scope;
635 	if (ns == NS_LABEL)
636 		scope = function_scope;
637 	bind_scope(sym, scope);
638 }
639 
640 struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
641 {
642 	struct ident *ident = built_in_ident(name);
643 	struct symbol *sym = lookup_symbol(ident, namespace);
644 
645 	if (sym && sym->type != type)
646 		die("symbol %s created with different types: %d old %d", name,
647 				type, sym->type);
648 
649 	if (!sym) {
650 		struct token *token = built_in_token(stream, ident);
651 
652 		sym = alloc_symbol(token->pos, type);
653 		bind_symbol(sym, token->ident, namespace);
654 	}
655 	return sym;
656 }
657 
658 
659 /*
660  * Abstract types
661  */
662 struct symbol	int_type,
663 		fp_type;
664 
665 /*
666  * C types (i.e. actual instances that the abstract types
667  * can map onto)
668  */
669 struct symbol	bool_ctype, void_ctype, type_ctype,
670 		char_ctype, schar_ctype, uchar_ctype,
671 		short_ctype, sshort_ctype, ushort_ctype,
672 		int_ctype, sint_ctype, uint_ctype,
673 		long_ctype, slong_ctype, ulong_ctype,
674 		llong_ctype, sllong_ctype, ullong_ctype,
675 		lllong_ctype, slllong_ctype, ulllong_ctype,
676 		float_ctype, double_ctype, ldouble_ctype,
677 		string_ctype, ptr_ctype, lazy_ptr_ctype,
678 		incomplete_ctype, label_ctype, bad_ctype,
679 		null_ctype;
680 
681 struct symbol	zero_int;
682 
683 #define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
684 #define __IDENT(n,str,res) \
685 	struct ident n  = __INIT_IDENT(str,res)
686 
687 #include "ident-list.h"
688 
689 void init_symbols(void)
690 {
691 	int stream = init_stream("builtin", -1, includepath);
692 
693 #define __IDENT(n,str,res) \
694 	hash_ident(&n)
695 #include "ident-list.h"
696 
697 	init_parser(stream);
698 	init_builtins(stream);
699 }
700 
701 #ifdef __CHAR_UNSIGNED__
702 #define CHAR_SIGNEDNESS MOD_UNSIGNED
703 #else
704 #define CHAR_SIGNEDNESS MOD_SIGNED
705 #endif
706 
707 #define MOD_ESIGNED (MOD_SIGNED | MOD_EXPLICITLY_SIGNED)
708 #define MOD_LL (MOD_LONG | MOD_LONGLONG)
709 #define MOD_LLL MOD_LONGLONGLONG
710 static const struct ctype_declare {
711 	struct symbol *ptr;
712 	enum type type;
713 	unsigned long modifiers;
714 	int *bit_size;
715 	int *maxalign;
716 	struct symbol *base_type;
717 } ctype_declaration[] = {
718 	{ &bool_ctype,	    SYM_BASETYPE, MOD_UNSIGNED,		    &bits_in_bool,	     &max_int_alignment, &int_type },
719 	{ &void_ctype,	    SYM_BASETYPE, 0,			    NULL,	     NULL,		 NULL },
720 	{ &type_ctype,	    SYM_BASETYPE, MOD_TYPE,		    NULL,		     NULL,		 NULL },
721 	{ &incomplete_ctype,SYM_BASETYPE, 0,			    NULL,		     NULL,		 NULL },
722 	{ &bad_ctype,	    SYM_BASETYPE, 0,			    NULL,		     NULL,		 NULL },
723 
724 	{ &char_ctype,	    SYM_BASETYPE, CHAR_SIGNEDNESS | MOD_CHAR,    &bits_in_char,	     &max_int_alignment, &int_type },
725 	{ &schar_ctype,	    SYM_BASETYPE, MOD_ESIGNED | MOD_CHAR,   &bits_in_char,	     &max_int_alignment, &int_type },
726 	{ &uchar_ctype,	    SYM_BASETYPE, MOD_UNSIGNED | MOD_CHAR,  &bits_in_char,	     &max_int_alignment, &int_type },
727 	{ &short_ctype,	    SYM_BASETYPE, MOD_SIGNED | MOD_SHORT,   &bits_in_short,	     &max_int_alignment, &int_type },
728 	{ &sshort_ctype,    SYM_BASETYPE, MOD_ESIGNED | MOD_SHORT,  &bits_in_short,	     &max_int_alignment, &int_type },
729 	{ &ushort_ctype,    SYM_BASETYPE, MOD_UNSIGNED | MOD_SHORT, &bits_in_short,	     &max_int_alignment, &int_type },
730 	{ &int_ctype,	    SYM_BASETYPE, MOD_SIGNED,		    &bits_in_int,	     &max_int_alignment, &int_type },
731 	{ &sint_ctype,	    SYM_BASETYPE, MOD_ESIGNED,		    &bits_in_int,	     &max_int_alignment, &int_type },
732 	{ &uint_ctype,	    SYM_BASETYPE, MOD_UNSIGNED,		    &bits_in_int,	     &max_int_alignment, &int_type },
733 	{ &long_ctype,	    SYM_BASETYPE, MOD_SIGNED | MOD_LONG,    &bits_in_long,	     &max_int_alignment, &int_type },
734 	{ &slong_ctype,	    SYM_BASETYPE, MOD_ESIGNED | MOD_LONG,   &bits_in_long,	     &max_int_alignment, &int_type },
735 	{ &ulong_ctype,	    SYM_BASETYPE, MOD_UNSIGNED | MOD_LONG,  &bits_in_long,	     &max_int_alignment, &int_type },
736 	{ &llong_ctype,	    SYM_BASETYPE, MOD_SIGNED | MOD_LL,	    &bits_in_longlong,       &max_int_alignment, &int_type },
737 	{ &sllong_ctype,    SYM_BASETYPE, MOD_ESIGNED | MOD_LL,	    &bits_in_longlong,       &max_int_alignment, &int_type },
738 	{ &ullong_ctype,    SYM_BASETYPE, MOD_UNSIGNED | MOD_LL,    &bits_in_longlong,       &max_int_alignment, &int_type },
739 	{ &lllong_ctype,    SYM_BASETYPE, MOD_SIGNED | MOD_LLL,	    &bits_in_longlonglong,   &max_int_alignment, &int_type },
740 	{ &slllong_ctype,   SYM_BASETYPE, MOD_ESIGNED | MOD_LLL,    &bits_in_longlonglong,   &max_int_alignment, &int_type },
741 	{ &ulllong_ctype,   SYM_BASETYPE, MOD_UNSIGNED | MOD_LLL,   &bits_in_longlonglong,   &max_int_alignment, &int_type },
742 
743 	{ &float_ctype,	    SYM_BASETYPE,  0,			    &bits_in_float,          &max_fp_alignment,  &fp_type },
744 	{ &double_ctype,    SYM_BASETYPE, MOD_LONG,		    &bits_in_double,         &max_fp_alignment,  &fp_type },
745 	{ &ldouble_ctype,   SYM_BASETYPE, MOD_LONG | MOD_LONGLONG,  &bits_in_longdouble,     &max_fp_alignment,  &fp_type },
746 
747 	{ &string_ctype,    SYM_PTR,	  0,			    &bits_in_pointer,        &pointer_alignment, &char_ctype },
748 	{ &ptr_ctype,	    SYM_PTR,	  0,			    &bits_in_pointer,        &pointer_alignment, &void_ctype },
749 	{ &null_ctype,	    SYM_PTR,	  0,			    &bits_in_pointer,        &pointer_alignment, &void_ctype },
750 	{ &label_ctype,	    SYM_PTR,	  0,			    &bits_in_pointer,        &pointer_alignment, &void_ctype },
751 	{ &lazy_ptr_ctype,  SYM_PTR,	  0,			    &bits_in_pointer,        &pointer_alignment, &void_ctype },
752 	{ NULL, }
753 };
754 #undef MOD_LLL
755 #undef MOD_LL
756 #undef MOD_ESIGNED
757 
758 void init_ctype(void)
759 {
760 	const struct ctype_declare *ctype;
761 
762 	for (ctype = ctype_declaration ; ctype->ptr; ctype++) {
763 		struct symbol *sym = ctype->ptr;
764 		unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
765 		unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
766 		unsigned long alignment = bits_to_bytes(bit_size);
767 
768 		if (alignment > maxalign)
769 			alignment = maxalign;
770 		sym->type = ctype->type;
771 		sym->bit_size = bit_size;
772 		sym->ctype.alignment = alignment;
773 		sym->ctype.base_type = ctype->base_type;
774 		sym->ctype.modifiers = ctype->modifiers;
775 	}
776 }
777