added a bunch of gcc builtins
[smatch.git] / parse.c
blobdc208121ca93d94cfd317d9632e37815ed1572fe
1 /*
2 * Stupid C parser, version 1e-6.
4 * Let's see how hard this is to do.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003-2004 Linus Torvalds
9 * Licensed under the Open Software License version 1.1
12 #include <stdarg.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <limits.h>
21 #include "lib.h"
22 #include "allocate.h"
23 #include "token.h"
24 #include "parse.h"
25 #include "symbol.h"
26 #include "scope.h"
27 #include "expression.h"
28 #include "target.h"
30 #define warn_on_mixed (1)
32 static struct symbol_list **function_symbol_list;
33 struct symbol_list *function_computed_target_list;
34 struct statement_list *function_computed_goto_list;
36 static struct token *statement(struct token *token, struct statement **tree);
38 // Add a symbol to the list of function-local symbols
39 static void fn_local_symbol(struct symbol *sym)
41 if (function_symbol_list)
42 add_symbol(function_symbol_list, sym);
45 static int match_idents(struct token *token, ...)
47 va_list args;
49 if (token_type(token) != TOKEN_IDENT)
50 return 0;
52 va_start(args, token);
53 for (;;) {
54 struct ident * next = va_arg(args, struct ident *);
55 if (!next)
56 return 0;
57 if (token->ident == next)
58 return 1;
63 struct statement *alloc_statement(struct position pos, int type)
65 struct statement *stmt = __alloc_statement(0);
66 stmt->type = type;
67 stmt->pos = pos;
68 return stmt;
71 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
73 static struct symbol * indirect(struct position pos, struct ctype *ctype, int type)
75 struct symbol *sym = alloc_symbol(pos, type);
77 sym->ctype.base_type = ctype->base_type;
78 sym->ctype.modifiers = ctype->modifiers & ~MOD_STORAGE;
80 ctype->base_type = sym;
81 ctype->modifiers &= MOD_STORAGE;
82 return sym;
85 static struct symbol *lookup_or_create_symbol(enum namespace ns, enum type type, struct token *token)
87 struct symbol *sym = lookup_symbol(token->ident, ns);
88 if (!sym) {
89 sym = alloc_symbol(token->pos, type);
90 bind_symbol(sym, token->ident, ns);
91 if (type == SYM_LABEL)
92 fn_local_symbol(sym);
94 return sym;
98 * NOTE! NS_LABEL is not just a different namespace,
99 * it also ends up using function scope instead of the
100 * regular symbol scope.
102 struct symbol *label_symbol(struct token *token)
104 return lookup_or_create_symbol(NS_LABEL, SYM_LABEL, token);
107 static struct token *struct_union_enum_specifier(enum type type,
108 struct token *token, struct ctype *ctype,
109 struct token *(*parse)(struct token *, struct symbol *))
111 struct symbol *sym;
112 struct position *repos;
114 ctype->modifiers = 0;
115 if (token_type(token) == TOKEN_IDENT) {
116 sym = lookup_symbol(token->ident, NS_STRUCT);
117 if (!sym ||
118 (sym->scope != block_scope &&
119 (match_op(token->next,';') || match_op(token->next,'{')))) {
120 // Either a new symbol, or else an out-of-scope
121 // symbol being redefined.
122 sym = alloc_symbol(token->pos, type);
123 bind_symbol(sym, token->ident, NS_STRUCT);
125 if (sym->type != type)
126 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
127 ctype->base_type = sym;
128 repos = &token->pos;
129 token = token->next;
130 if (match_op(token, '{')) {
131 // The following test is actually wrong for empty
132 // structs, but (1) they are not C99, (2) gcc does
133 // the same thing, and (3) it's easier.
134 if (sym->symbol_list)
135 error_die(token->pos, "redefinition of %s", show_typename (sym));
136 sym->pos = *repos;
137 token = parse(token->next, sym);
138 token = expect(token, '}', "at end of struct-union-enum-specifier");
140 // Mark the structure as needing re-examination
141 sym->examined = 0;
143 return token;
146 // private struct/union/enum type
147 if (!match_op(token, '{')) {
148 sparse_error(token->pos, "expected declaration");
149 ctype->base_type = &bad_ctype;
150 return token;
153 sym = alloc_symbol(token->pos, type);
154 token = parse(token->next, sym);
155 ctype->base_type = sym;
156 return expect(token, '}', "at end of specifier");
159 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
161 return struct_declaration_list(token, &sym->symbol_list);
164 static struct token *struct_or_union_specifier(enum type type, struct token *token, struct ctype *ctype)
166 return struct_union_enum_specifier(type, token, ctype, parse_struct_declaration);
169 typedef struct {
170 int x;
171 unsigned long long y;
172 } Num;
174 static void upper_boundary(Num *n, Num *v)
176 if (n->x > v->x)
177 return;
178 if (n->x < v->x) {
179 *n = *v;
180 return;
182 if (n->y < v->y)
183 n->y = v->y;
186 static void lower_boundary(Num *n, Num *v)
188 if (n->x < v->x)
189 return;
190 if (n->x > v->x) {
191 *n = *v;
192 return;
194 if (n->y > v->y)
195 n->y = v->y;
198 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
200 int shift = type->bit_size;
201 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
203 if (!is_unsigned)
204 shift--;
205 if (upper->x == 0 && upper->y >> shift)
206 return 0;
207 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
208 return 1;
209 return 0;
212 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
214 if (s1->bit_size < s2->bit_size) {
215 s1 = s2;
216 } else if (s1->bit_size == s2->bit_size) {
217 if (s2->ctype.modifiers & MOD_UNSIGNED)
218 s1 = s2;
220 if (s1->bit_size < bits_in_int)
221 return &int_ctype;
222 return s1;
225 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
227 struct symbol *sym;
229 FOR_EACH_PTR(list, sym) {
230 struct expression *expr = sym->initializer;
231 struct symbol *ctype;
232 if (expr->type != EXPR_VALUE)
233 continue;
234 ctype = expr->ctype;
235 if (ctype->bit_size == base_type->bit_size)
236 continue;
237 cast_value(expr, base_type, expr, ctype);
238 } END_FOR_EACH_PTR(sym);
241 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
243 unsigned long long lastval = 0;
244 struct symbol *ctype = NULL, *base_type = NULL;
245 Num upper = {-1, 0}, lower = {1, 0};
246 struct symbol_list *entries = NULL;
248 parent->examined = 1;
249 parent->ctype.base_type = &int_ctype;
250 while (token_type(token) == TOKEN_IDENT) {
251 struct expression *expr = NULL;
252 struct token *next = token->next;
253 struct symbol *sym;
255 sym = alloc_symbol(token->pos, SYM_NODE);
256 bind_symbol(sym, token->ident, NS_SYMBOL);
257 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
259 if (match_op(next, '=')) {
260 next = constant_expression(next->next, &expr);
261 lastval = get_expression_value(expr);
262 ctype = &void_ctype;
263 if (expr && expr->ctype)
264 ctype = expr->ctype;
265 } else if (!ctype) {
266 ctype = &int_ctype;
267 } else if (is_int_type(ctype)) {
268 lastval++;
269 } else {
270 error_die(token->pos, "can't increment the last enum member");
273 if (!expr) {
274 expr = alloc_expression(token->pos, EXPR_VALUE);
275 expr->value = lastval;
276 expr->ctype = ctype;
279 sym->initializer = expr;
280 sym->ctype.base_type = parent;
281 add_ptr_list(&entries, sym);
283 if (base_type != &bad_ctype) {
284 if (ctype->type == SYM_NODE)
285 ctype = ctype->ctype.base_type;
286 if (ctype->type == SYM_ENUM) {
287 if (ctype == parent)
288 ctype = base_type;
289 else
290 ctype = ctype->ctype.base_type;
293 * base_type rules:
294 * - if all enum's are of the same type, then
295 * the base_type is that type (two first
296 * cases)
297 * - if enums are of different types, they
298 * all have to be integer types, and the
299 * base type is at least "int_ctype".
300 * - otherwise the base_type is "bad_ctype".
302 if (!base_type) {
303 base_type = ctype;
304 } else if (ctype == base_type) {
305 /* nothing */
306 } else if (is_int_type(base_type) && is_int_type(ctype)) {
307 base_type = bigger_enum_type(base_type, ctype);
308 } else
309 base_type = &bad_ctype;
310 parent->ctype.base_type = base_type;
312 if (is_int_type(base_type)) {
313 Num v = {.y = lastval};
314 if (ctype->ctype.modifiers & MOD_UNSIGNED)
315 v.x = 0;
316 else if ((long long)lastval >= 0)
317 v.x = 0;
318 else
319 v.x = -1;
320 upper_boundary(&upper, &v);
321 lower_boundary(&lower, &v);
323 token = next;
324 if (!match_op(token, ','))
325 break;
326 token = token->next;
328 if (!base_type) {
329 sparse_error(token->pos, "bad enum definition");
330 base_type = &bad_ctype;
332 else if (!is_int_type(base_type))
333 base_type = base_type;
334 else if (type_is_ok(base_type, &upper, &lower))
335 base_type = base_type;
336 else if (type_is_ok(&int_ctype, &upper, &lower))
337 base_type = &int_ctype;
338 else if (type_is_ok(&uint_ctype, &upper, &lower))
339 base_type = &uint_ctype;
340 else if (type_is_ok(&long_ctype, &upper, &lower))
341 base_type = &long_ctype;
342 else if (type_is_ok(&ulong_ctype, &upper, &lower))
343 base_type = &ulong_ctype;
344 else if (type_is_ok(&llong_ctype, &upper, &lower))
345 base_type = &llong_ctype;
346 else if (type_is_ok(&ullong_ctype, &upper, &lower))
347 base_type = &ullong_ctype;
348 else
349 base_type = &bad_ctype;
350 parent->ctype.base_type = base_type;
351 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
352 parent->examined = 0;
354 cast_enum_list(entries, base_type);
355 free_ptr_list(&entries);
357 return token;
360 static struct token *enum_specifier(struct token *token, struct ctype *ctype)
362 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctype, parse_enum_declaration);
364 ctype = &ctype->base_type->ctype;
365 if (!ctype->base_type)
366 ctype->base_type = &incomplete_ctype;
368 return ret;
371 static struct token *typeof_specifier(struct token *token, struct ctype *ctype)
373 struct symbol *sym;
375 if (!match_op(token, '(')) {
376 sparse_error(token->pos, "expected '(' after typeof");
377 return token;
379 if (lookup_type(token->next)) {
380 token = typename(token->next, &sym);
381 *ctype = sym->ctype;
382 } else {
383 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
384 token = parse_expression(token->next, &typeof_sym->initializer);
386 ctype->modifiers = 0;
387 ctype->base_type = typeof_sym;
389 return expect(token, ')', "after typeof");
392 static const char * handle_attribute(struct ctype *ctype, struct ident *attribute, struct expression *expr)
394 if (attribute == &packed_ident ||
395 attribute == &__packed___ident) {
396 ctype->alignment = 1;
397 return NULL;
399 if (attribute == &aligned_ident ||
400 attribute == &__aligned___ident) {
401 int alignment = max_alignment;
402 if (expr)
403 alignment = get_expression_value(expr);
404 ctype->alignment = alignment;
405 return NULL;
407 if (attribute == &nocast_ident) {
408 ctype->modifiers |= MOD_NOCAST;
409 return NULL;
411 if (attribute == &noderef_ident) {
412 ctype->modifiers |= MOD_NODEREF;
413 return NULL;
415 if (attribute == &safe_ident) {
416 ctype->modifiers |= MOD_SAFE;
417 return NULL;
419 if (attribute == &force_ident) {
420 ctype->modifiers |= MOD_FORCE;
421 return NULL;
423 if (attribute == &bitwise_ident ||
424 attribute == &__bitwise___ident) {
425 if (Wbitwise)
426 ctype->modifiers |= MOD_BITWISE;
427 return NULL;
429 if (attribute == &address_space_ident) {
430 if (!expr)
431 return "expected address space number";
432 ctype->as = get_expression_value(expr);
433 return NULL;
435 if (attribute == &context_ident) {
436 if (expr && expr->type == EXPR_COMMA) {
437 struct context *context = alloc_context();
438 if(expr->left->type == EXPR_COMMA) {
439 context->context = expr->left->left;
440 context->in = get_expression_value(
441 expr->left->right);
442 } else {
443 context->context = NULL;
444 context->in = get_expression_value(expr->left);
446 context->out = get_expression_value(expr->right);
447 add_ptr_list(&ctype->contexts, context);
448 return NULL;
450 return "expected context input/output values";
452 if (attribute == &mode_ident ||
453 attribute == &__mode___ident) {
454 if (expr && expr->type == EXPR_SYMBOL) {
455 struct ident *ident = expr->symbol_name;
458 * Match against __QI__/__HI__/__SI__/__DI__
460 * FIXME! This is broken - we don't actually get
461 * the type information updated properly at this
462 * stage for some reason.
464 if (ident == &__QI___ident ||
465 ident == &QI_ident) {
466 ctype->modifiers |= MOD_CHAR;
467 return NULL;
469 if (ident == &__HI___ident ||
470 ident == &HI_ident) {
471 ctype->modifiers |= MOD_SHORT;
472 return NULL;
474 if (ident == &__SI___ident ||
475 ident == &SI_ident) {
476 /* Nothing? */
477 return NULL;
479 if (ident == &__DI___ident ||
480 ident == &DI_ident) {
481 ctype->modifiers |= MOD_LONGLONG;
482 return NULL;
484 if (ident == &__word___ident ||
485 ident == &word_ident) {
486 ctype->modifiers |= MOD_LONG;
487 return NULL;
489 return "unknown mode attribute";
491 return "expected attribute mode symbol";
494 /* Throw away for now.. */
495 if (attribute == &__transparent_union___ident) {
496 if (Wtransparent_union)
497 return "ignoring attribute __transparent_union__";
498 return NULL;
500 if (attribute == &nothrow_ident ||
501 attribute == &__nothrow_ident ||
502 attribute == &__nothrow___ident)
503 return NULL;
504 if (attribute == &__malloc___ident)
505 return NULL;
506 if (attribute == &nonnull_ident ||
507 attribute == &__nonnull_ident ||
508 attribute == &__nonnull___ident)
509 return NULL;
510 if (attribute == &format_ident ||
511 attribute == &__format___ident ||
512 attribute == &__format_arg___ident)
513 return NULL;
514 if (attribute == &section_ident ||
515 attribute == &__section___ident)
516 return NULL;
517 if (attribute == &unused_ident ||
518 attribute == &__unused___ident)
519 return NULL;
520 if (attribute == &const_ident ||
521 attribute == &__const_ident ||
522 attribute == &__const___ident)
523 return NULL;
524 if (attribute == &noreturn_ident ||
525 attribute == &__noreturn___ident)
526 return NULL;
527 if (attribute == &no_instrument_function_ident ||
528 attribute == &__no_instrument_function___ident)
529 return NULL;
530 if (attribute == &sentinel_ident ||
531 attribute == &__sentinel___ident)
532 return NULL;
533 if (attribute == &regparm_ident)
534 return NULL;
535 if (attribute == &weak_ident ||
536 attribute == &__weak___ident)
537 return NULL;
538 if (attribute == &alias_ident ||
539 attribute == &__alias___ident)
540 return NULL;
541 if (attribute == &pure_ident ||
542 attribute == &__pure___ident)
543 return NULL;
544 if (attribute == &always_inline_ident)
545 return NULL;
546 if (attribute == &syscall_linkage_ident)
547 return NULL;
548 if (attribute == &visibility_ident ||
549 attribute == &__visibility___ident)
550 return NULL;
551 if (attribute == &deprecated_ident ||
552 attribute == &__deprecated___ident)
553 return NULL;
554 if (attribute == &noinline_ident)
555 return NULL;
556 if (attribute == &__used___ident)
557 return NULL;
558 if (attribute == &warn_unused_result_ident ||
559 attribute == &__warn_unused_result___ident)
560 return NULL;
561 if (attribute == &model_ident ||
562 attribute == &__model___ident)
563 return NULL;
565 return "unknown attribute";
568 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
570 ctype->modifiers = 0;
571 token = expect(token, '(', "after attribute");
572 token = expect(token, '(', "after attribute");
574 for (;;) {
575 const char *error_str;
576 struct ident *attribute_name;
577 struct expression *attribute_expr;
579 if (eof_token(token))
580 break;
581 if (match_op(token, ';'))
582 break;
583 if (token_type(token) != TOKEN_IDENT)
584 break;
585 attribute_name = token->ident;
586 token = token->next;
587 attribute_expr = NULL;
588 if (match_op(token, '('))
589 token = parens_expression(token, &attribute_expr, "in attribute");
590 error_str = handle_attribute(ctype, attribute_name, attribute_expr);
591 if (error_str)
592 sparse_error(token->pos, "attribute '%s': %s", show_ident(attribute_name), error_str);
593 if (!match_op(token, ','))
594 break;
595 token = token->next;
598 token = expect(token, ')', "after attribute");
599 token = expect(token, ')', "after attribute");
600 return token;
603 struct symbol * ctype_integer(unsigned long spec)
605 static struct symbol *const integer_ctypes[][3] = {
606 { &llong_ctype, &sllong_ctype, &ullong_ctype },
607 { &long_ctype, &slong_ctype, &ulong_ctype },
608 { &short_ctype, &sshort_ctype, &ushort_ctype },
609 { &char_ctype, &schar_ctype, &uchar_ctype },
610 { &int_ctype, &sint_ctype, &uint_ctype },
612 struct symbol *const (*ctype)[3];
613 int sub;
615 ctype = integer_ctypes;
616 if (!(spec & MOD_LONGLONG)) {
617 ctype++;
618 if (!(spec & MOD_LONG)) {
619 ctype++;
620 if (!(spec & MOD_SHORT)) {
621 ctype++;
622 if (!(spec & MOD_CHAR))
623 ctype++;
628 sub = ((spec & MOD_UNSIGNED)
630 : ((spec & MOD_EXPLICITLY_SIGNED)
632 : 0));
634 return ctype[0][sub];
637 struct symbol * ctype_fp(unsigned long spec)
639 if (spec & MOD_LONGLONG)
640 return &ldouble_ctype;
641 if (spec & MOD_LONG)
642 return &double_ctype;
643 return &float_ctype;
646 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
648 unsigned long mod = thistype->modifiers;
650 if (mod) {
651 unsigned long old = ctype->modifiers;
652 unsigned long extra = 0, dup, conflict;
654 if (mod & old & MOD_LONG) {
655 extra = MOD_LONGLONG | MOD_LONG;
656 mod &= ~MOD_LONG;
657 old &= ~MOD_LONG;
659 dup = (mod & old) | (extra & old) | (extra & mod);
660 if (dup)
661 sparse_error(pos, "Just how %sdo you want this type to be?",
662 modifier_string(dup));
664 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
665 if (conflict)
666 sparse_error(pos, "You cannot have both long and short modifiers.");
668 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
669 if (conflict)
670 sparse_error(pos, "You cannot have both signed and unsigned modifiers.");
672 // Only one storage modifier allowed, except that "inline" doesn't count.
673 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
674 conflict &= (conflict - 1);
675 if (conflict)
676 sparse_error(pos, "multiple storage classes");
678 ctype->modifiers = old | mod | extra;
681 /* Context */
682 concat_ptr_list((struct ptr_list *)thistype->contexts,
683 (struct ptr_list **)&ctype->contexts);
685 /* Alignment */
686 if (thistype->alignment & (thistype->alignment-1)) {
687 warning(pos, "I don't like non-power-of-2 alignments");
688 thistype->alignment = 0;
690 if (thistype->alignment > ctype->alignment)
691 ctype->alignment = thistype->alignment;
693 /* Address space */
694 if (thistype->as)
695 ctype->as = thistype->as;
698 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
700 unsigned long banned, wrong;
701 unsigned long this_mod = s->ctype.modifiers;
702 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
703 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
705 if (this_mod & (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF))
706 banned = BANNED_SIZE | BANNED_SIGN;
707 else if (this_mod & MOD_SPECIALBITS)
708 banned = 0;
709 else if (s->ctype.base_type == &fp_type)
710 banned = BANNED_SIGN;
711 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
712 banned = 0;
713 else {
714 // label_type
715 // void_type
716 // bad_type
717 // vector_type <-- whatever that is
718 banned = BANNED_SIZE | BANNED_SIGN;
721 wrong = mod & banned;
722 if (wrong)
723 sparse_error(*pos, "modifier %sis invalid in this context",
724 modifier_string (wrong));
728 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
730 struct token *token;
732 while ( (token = next) != NULL ) {
733 struct ctype thistype;
734 struct ident *ident;
735 struct symbol *s, *type;
736 unsigned long mod;
738 next = token->next;
739 if (token_type(token) != TOKEN_IDENT)
740 break;
741 ident = token->ident;
743 s = lookup_symbol(ident, NS_TYPEDEF);
744 if (!s)
745 break;
746 thistype = s->ctype;
747 mod = thistype.modifiers;
748 if (qual && (mod & ~(MOD_ATTRIBUTE | MOD_CONST | MOD_VOLATILE)))
749 break;
750 if (mod & MOD_SPECIALBITS) {
751 if (mod & MOD_STRUCTOF)
752 next = struct_or_union_specifier(SYM_STRUCT, next, &thistype);
753 else if (mod & MOD_UNIONOF)
754 next = struct_or_union_specifier(SYM_UNION, next, &thistype);
755 else if (mod & MOD_ENUMOF)
756 next = enum_specifier(next, &thistype);
757 else if (mod & MOD_ATTRIBUTE)
758 next = attribute_specifier(next, &thistype);
759 else if (mod & MOD_TYPEOF)
760 next = typeof_specifier(next, &thistype);
761 mod = thistype.modifiers;
763 type = thistype.base_type;
764 if (type) {
765 if (qual)
766 break;
767 if (ctype->base_type)
768 break;
769 /* User types only mix with qualifiers */
770 if (mod & MOD_USERTYPE) {
771 if (ctype->modifiers & MOD_SPECIFIER)
772 break;
774 ctype->base_type = type;
777 check_modifiers(&token->pos, s, ctype->modifiers);
778 apply_ctype(token->pos, &thistype, ctype);
781 /* Turn the "virtual types" into real types with real sizes etc */
782 if (!ctype->base_type) {
783 struct symbol *base = &incomplete_ctype;
786 * If we have modifiers, we'll default to an integer
787 * type, and "ctype_integer()" will turn this into
788 * a specific one.
790 if (ctype->modifiers & MOD_SPECIFIER)
791 base = &int_type;
792 ctype->base_type = base;
794 if (ctype->base_type == &int_type) {
795 ctype->base_type = ctype_integer(ctype->modifiers);
796 ctype->modifiers &= ~MOD_SPECIFIER;
797 } else if (ctype->base_type == &fp_type) {
798 ctype->base_type = ctype_fp(ctype->modifiers);
799 ctype->modifiers &= ~MOD_SPECIFIER;
801 if (ctype->modifiers & MOD_BITWISE) {
802 struct symbol *type;
803 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
804 if (!is_int_type(ctype->base_type)) {
805 sparse_error(token->pos, "invalid modifier");
806 return token;
808 type = alloc_symbol(token->pos, SYM_BASETYPE);
809 *type = *ctype->base_type;
810 type->ctype.base_type = ctype->base_type;
811 type->type = SYM_RESTRICT;
812 type->ctype.modifiers &= ~MOD_SPECIFIER;
813 ctype->base_type = type;
814 create_fouled(type);
816 return token;
819 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
821 struct expression *expr = NULL;
823 token = parse_expression(token, &expr);
824 sym->array_size = expr;
825 return token;
828 static struct token *parameter_type_list(struct token *, struct symbol *, struct ident **p);
829 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
831 static struct token *handle_attributes(struct token *token, struct ctype *ctype)
833 for (;;) {
834 if (token_type(token) != TOKEN_IDENT)
835 break;
836 if (match_idents(token, &__attribute___ident, &__attribute_ident, NULL)) {
837 struct ctype thistype = { 0, };
838 token = attribute_specifier(token->next, &thistype);
839 apply_ctype(token->pos, &thistype, ctype);
840 continue;
842 if (match_idents(token, &asm_ident, &__asm_ident, &__asm___ident)) {
843 struct expression *expr;
844 token = expect(token->next, '(', "after asm");
845 token = parse_expression(token->next, &expr);
846 token = expect(token, ')', "after asm");
847 continue;
849 break;
851 return token;
854 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
856 struct ctype *ctype = &decl->ctype;
858 if (p && token_type(token) == TOKEN_IDENT) {
859 *p = token->ident;
860 token = token->next;
863 for (;;) {
864 token = handle_attributes(token, ctype);
866 if (token_type(token) != TOKEN_SPECIAL)
867 return token;
870 * This can be either a parameter list or a grouping.
871 * For the direct (non-abstract) case, we know if must be
872 * a parameter list if we already saw the identifier.
873 * For the abstract case, we know if must be a parameter
874 * list if it is empty or starts with a type.
876 if (token->special == '(') {
877 struct symbol *sym;
878 struct token *next = token->next;
879 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
881 if (!fn) {
882 struct symbol *base_type = ctype->base_type;
883 token = declarator(next, decl, p);
884 token = expect(token, ')', "in nested declarator");
885 while (ctype->base_type != base_type)
886 ctype = &ctype->base_type->ctype;
887 p = NULL;
888 continue;
891 sym = indirect(token->pos, ctype, SYM_FN);
892 token = parameter_type_list(next, sym, p);
893 token = expect(token, ')', "in function declarator");
894 continue;
896 if (token->special == '[') {
897 struct symbol *array = indirect(token->pos, ctype, SYM_ARRAY);
898 token = abstract_array_declarator(token->next, array);
899 token = expect(token, ']', "in abstract_array_declarator");
900 ctype = &array->ctype;
901 continue;
903 break;
905 return token;
908 static struct token *pointer(struct token *token, struct ctype *ctype)
910 unsigned long modifiers;
911 struct symbol *base_type;
913 modifiers = ctype->modifiers & ~(MOD_TYPEDEF | MOD_ATTRIBUTE);
914 base_type = ctype->base_type;
915 ctype->modifiers = modifiers;
917 while (match_op(token,'*')) {
918 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
919 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
920 ptr->ctype.as = ctype->as;
921 concat_ptr_list((struct ptr_list *)ctype->contexts,
922 (struct ptr_list **)&ptr->ctype.contexts);
923 ptr->ctype.base_type = base_type;
925 base_type = ptr;
926 ctype->modifiers = modifiers & MOD_STORAGE;
927 ctype->base_type = base_type;
928 ctype->as = 0;
929 free_ptr_list(&ctype->contexts);
931 token = declaration_specifiers(token->next, ctype, 1);
932 modifiers = ctype->modifiers;
934 return token;
937 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
939 token = pointer(token, &sym->ctype);
940 return direct_declarator(token, sym, p);
943 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
945 struct ctype *ctype = &decl->ctype;
946 struct expression *expr;
947 struct symbol *bitfield;
948 long long width;
950 if (!is_int_type(ctype->base_type)) {
951 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
952 show_typename(ctype->base_type));
953 // Parse this to recover gracefully.
954 return conditional_expression(token->next, &expr);
957 bitfield = indirect(token->pos, ctype, SYM_BITFIELD);
958 token = conditional_expression(token->next, &expr);
959 width = get_expression_value(expr);
960 bitfield->bit_size = width;
962 if (width < 0 || width > INT_MAX) {
963 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
964 width = -1;
965 } else if (decl->ident && width == 0) {
966 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
967 show_ident(decl->ident));
968 width = -1;
969 } else if (decl->ident) {
970 struct symbol *base_type = bitfield->ctype.base_type;
971 int is_signed = !(base_type->ctype.modifiers & MOD_UNSIGNED);
972 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
973 // Valid values are either {-1;0} or {0}, depending on integer
974 // representation. The latter makes for very efficient code...
975 sparse_error(token->pos, "dubious one-bit signed bitfield");
977 if (Wdefault_bitfield_sign &&
978 base_type->type != SYM_ENUM &&
979 !(base_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
980 is_signed) {
981 // The sign of bitfields is unspecified by default.
982 sparse_error(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
985 bitfield->bit_size = width;
986 return token;
989 static struct token *declaration_list(struct token *token, struct symbol_list **list)
991 struct ctype ctype = {0, };
993 token = declaration_specifiers(token, &ctype, 0);
994 for (;;) {
995 struct ident *ident = NULL;
996 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
997 decl->ctype = ctype;
998 token = declarator(token, decl, &ident);
999 decl->ident = ident;
1000 if (match_op(token, ':')) {
1001 token = handle_bitfield(token, decl);
1002 token = handle_attributes(token, &decl->ctype);
1004 add_symbol(list, decl);
1005 if (!match_op(token, ','))
1006 break;
1007 token = token->next;
1009 return token;
1012 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1014 while (!match_op(token, '}')) {
1015 token = declaration_list(token, list);
1016 if (!match_op(token, ';')) {
1017 sparse_error(token->pos, "expected ; at end of declaration");
1018 break;
1020 token = token->next;
1022 return token;
1025 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
1027 struct ident *ident = NULL;
1028 struct symbol *sym;
1029 struct ctype ctype = { 0, };
1031 token = declaration_specifiers(token, &ctype, 0);
1032 sym = alloc_symbol(token->pos, SYM_NODE);
1033 sym->ctype = ctype;
1034 *tree = sym;
1035 token = declarator(token, sym, &ident);
1036 sym->ident = ident;
1037 return token;
1040 struct token *typename(struct token *token, struct symbol **p)
1042 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1043 *p = sym;
1044 token = declaration_specifiers(token, &sym->ctype, 0);
1045 return declarator(token, sym, NULL);
1048 static struct token *expression_statement(struct token *token, struct expression **tree)
1050 token = parse_expression(token, tree);
1051 return expect(token, ';', "at end of statement");
1054 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1055 struct expression_list **inout)
1057 struct expression *expr;
1059 /* Allow empty operands */
1060 if (match_op(token->next, ':') || match_op(token->next, ')'))
1061 return token->next;
1062 do {
1063 struct ident *ident = NULL;
1064 if (match_op(token->next, '[') &&
1065 token_type(token->next->next) == TOKEN_IDENT &&
1066 match_op(token->next->next->next, ']')) {
1067 ident = token->next->next->ident;
1068 token = token->next->next->next;
1070 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1071 token = primary_expression(token->next, &expr);
1072 add_expression(inout, expr);
1073 token = parens_expression(token, &expr, "in asm parameter");
1074 add_expression(inout, expr);
1075 } while (match_op(token, ','));
1076 return token;
1079 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1080 struct expression_list **clobbers)
1082 struct expression *expr;
1084 do {
1085 token = primary_expression(token->next, &expr);
1086 add_expression(clobbers, expr);
1087 } while (match_op(token, ','));
1088 return token;
1091 static struct token *parse_asm(struct token *token, struct statement *stmt)
1093 stmt->type = STMT_ASM;
1094 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1095 token = token->next;
1097 token = expect(token, '(', "after asm");
1098 token = parse_expression(token, &stmt->asm_string);
1099 if (match_op(token, ':'))
1100 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1101 if (match_op(token, ':'))
1102 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1103 if (match_op(token, ':'))
1104 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1105 token = expect(token, ')', "after asm");
1106 return expect(token, ';', "at end of asm-statement");
1109 /* Make a statement out of an expression */
1110 static struct statement *make_statement(struct expression *expr)
1112 struct statement *stmt;
1114 if (!expr)
1115 return NULL;
1116 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1117 stmt->expression = expr;
1118 return stmt;
1122 * All iterators have two symbols associated with them:
1123 * the "continue" and "break" symbols, which are targets
1124 * for continue and break statements respectively.
1126 * They are in a special name-space, but they follow
1127 * all the normal visibility rules, so nested iterators
1128 * automatically work right.
1130 static void start_iterator(struct statement *stmt)
1132 struct symbol *cont, *brk;
1134 start_symbol_scope();
1135 cont = alloc_symbol(stmt->pos, SYM_NODE);
1136 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1137 brk = alloc_symbol(stmt->pos, SYM_NODE);
1138 bind_symbol(brk, &break_ident, NS_ITERATOR);
1140 stmt->type = STMT_ITERATOR;
1141 stmt->iterator_break = brk;
1142 stmt->iterator_continue = cont;
1143 fn_local_symbol(brk);
1144 fn_local_symbol(cont);
1147 static void end_iterator(struct statement *stmt)
1149 end_symbol_scope();
1152 static struct statement *start_function(struct symbol *sym)
1154 struct symbol *ret;
1155 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1157 start_function_scope();
1158 ret = alloc_symbol(sym->pos, SYM_NODE);
1159 ret->ctype = sym->ctype.base_type->ctype;
1160 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1161 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1162 bind_symbol(ret, &return_ident, NS_ITERATOR);
1163 stmt->ret = ret;
1164 fn_local_symbol(ret);
1166 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1167 current_fn = sym;
1169 return stmt;
1172 static void end_function(struct symbol *sym)
1174 current_fn = NULL;
1175 end_function_scope();
1179 * A "switch()" statement, like an iterator, has a
1180 * the "break" symbol associated with it. It works
1181 * exactly like the iterator break - it's the target
1182 * for any break-statements in scope, and means that
1183 * "break" handling doesn't even need to know whether
1184 * it's breaking out of an iterator or a switch.
1186 * In addition, the "case" symbol is a marker for the
1187 * case/default statements to find the switch statement
1188 * that they are associated with.
1190 static void start_switch(struct statement *stmt)
1192 struct symbol *brk, *switch_case;
1194 start_symbol_scope();
1195 brk = alloc_symbol(stmt->pos, SYM_NODE);
1196 bind_symbol(brk, &break_ident, NS_ITERATOR);
1198 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1199 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1200 switch_case->stmt = stmt;
1202 stmt->type = STMT_SWITCH;
1203 stmt->switch_break = brk;
1204 stmt->switch_case = switch_case;
1206 fn_local_symbol(brk);
1207 fn_local_symbol(switch_case);
1210 static void end_switch(struct statement *stmt)
1212 if (!stmt->switch_case->symbol_list)
1213 warning(stmt->pos, "switch with no cases");
1214 end_symbol_scope();
1217 static void add_case_statement(struct statement *stmt)
1219 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1220 struct symbol *sym;
1222 if (!target) {
1223 sparse_error(stmt->pos, "not in switch scope");
1224 stmt->type = STMT_NONE;
1225 return;
1227 sym = alloc_symbol(stmt->pos, SYM_NODE);
1228 add_symbol(&target->symbol_list, sym);
1229 sym->stmt = stmt;
1230 stmt->case_label = sym;
1231 fn_local_symbol(sym);
1234 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1236 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1238 if (!target)
1239 error_die(token->pos, "internal error: return without a function target");
1240 stmt->type = STMT_RETURN;
1241 stmt->ret_target = target;
1242 return expression_statement(token->next, &stmt->ret_value);
1245 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1247 struct symbol_list *syms;
1248 struct expression *e1, *e2, *e3;
1249 struct statement *iterator;
1251 start_iterator(stmt);
1252 token = expect(token->next, '(', "after 'for'");
1254 syms = NULL;
1255 e1 = NULL;
1256 /* C99 variable declaration? */
1257 if (lookup_type(token)) {
1258 token = external_declaration(token, &syms);
1259 } else {
1260 token = parse_expression(token, &e1);
1261 token = expect(token, ';', "in 'for'");
1263 token = parse_expression(token, &e2);
1264 token = expect(token, ';', "in 'for'");
1265 token = parse_expression(token, &e3);
1266 token = expect(token, ')', "in 'for'");
1267 token = statement(token, &iterator);
1269 stmt->iterator_syms = syms;
1270 stmt->iterator_pre_statement = make_statement(e1);
1271 stmt->iterator_pre_condition = e2;
1272 stmt->iterator_post_statement = make_statement(e3);
1273 stmt->iterator_post_condition = NULL;
1274 stmt->iterator_statement = iterator;
1275 end_iterator(stmt);
1277 return token;
1280 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
1282 struct expression *expr;
1283 struct statement *iterator;
1285 start_iterator(stmt);
1286 token = parens_expression(token->next, &expr, "after 'while'");
1287 token = statement(token, &iterator);
1289 stmt->iterator_pre_condition = expr;
1290 stmt->iterator_post_condition = NULL;
1291 stmt->iterator_statement = iterator;
1292 end_iterator(stmt);
1294 return token;
1297 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
1299 struct expression *expr;
1300 struct statement *iterator;
1302 start_iterator(stmt);
1303 token = statement(token->next, &iterator);
1304 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1305 token = token->next;
1306 else
1307 sparse_error(token->pos, "expected 'while' after 'do'");
1308 token = parens_expression(token, &expr, "after 'do-while'");
1310 stmt->iterator_post_condition = expr;
1311 stmt->iterator_statement = iterator;
1312 end_iterator(stmt);
1314 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
1315 warning(iterator->pos, "do-while statement is not a compound statement");
1317 return expect(token, ';', "after statement");
1320 static struct token *statement(struct token *token, struct statement **tree)
1322 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1324 *tree = stmt;
1325 if (token_type(token) == TOKEN_IDENT) {
1326 if (token->ident == &if_ident) {
1327 stmt->type = STMT_IF;
1328 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1329 token = statement(token, &stmt->if_true);
1330 if (token_type(token) != TOKEN_IDENT)
1331 return token;
1332 if (token->ident != &else_ident)
1333 return token;
1334 return statement(token->next, &stmt->if_false);
1337 if (token->ident == &return_ident)
1338 return parse_return_statement(token, stmt);
1340 if (token->ident == &break_ident || token->ident == &continue_ident) {
1341 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1342 stmt->type = STMT_GOTO;
1343 stmt->goto_label = target;
1344 if (!target)
1345 sparse_error(stmt->pos, "break/continue not in iterator scope");
1346 return expect(token->next, ';', "at end of statement");
1348 if (token->ident == &default_ident) {
1349 token = token->next;
1350 goto default_statement;
1352 if (token->ident == &case_ident) {
1353 token = parse_expression(token->next, &stmt->case_expression);
1354 if (match_op(token, SPECIAL_ELLIPSIS))
1355 token = parse_expression(token->next, &stmt->case_to);
1356 default_statement:
1357 stmt->type = STMT_CASE;
1358 token = expect(token, ':', "after default/case");
1359 add_case_statement(stmt);
1360 return statement(token, &stmt->case_statement);
1362 if (token->ident == &switch_ident) {
1363 stmt->type = STMT_SWITCH;
1364 start_switch(stmt);
1365 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1366 token = statement(token, &stmt->switch_statement);
1367 end_switch(stmt);
1368 return token;
1370 if (token->ident == &for_ident)
1371 return parse_for_statement(token, stmt);
1373 if (token->ident == &while_ident)
1374 return parse_while_statement(token, stmt);
1376 if (token->ident == &do_ident)
1377 return parse_do_statement(token, stmt);
1379 if (token->ident == &goto_ident) {
1380 stmt->type = STMT_GOTO;
1381 token = token->next;
1382 if (match_op(token, '*')) {
1383 token = parse_expression(token->next, &stmt->goto_expression);
1384 add_statement(&function_computed_goto_list, stmt);
1385 } else if (token_type(token) == TOKEN_IDENT) {
1386 stmt->goto_label = label_symbol(token);
1387 token = token->next;
1388 } else {
1389 sparse_error(token->pos, "Expected identifier or goto expression");
1391 return expect(token, ';', "at end of statement");
1393 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1394 return parse_asm(token->next, stmt);
1396 if (token->ident == &__context___ident) {
1397 stmt->type = STMT_CONTEXT;
1398 token = parse_expression(token->next, &stmt->expression);
1399 if(stmt->expression->type == EXPR_PREOP
1400 && stmt->expression->op == '('
1401 && stmt->expression->unop->type == EXPR_COMMA) {
1402 struct expression *expr;
1403 expr = stmt->expression->unop;
1404 stmt->context = expr->left;
1405 stmt->expression = expr->right;
1407 return expect(token, ';', "at end of statement");
1409 if (token->ident == &__range___ident) {
1410 stmt->type = STMT_RANGE;
1411 token = assignment_expression(token->next, &stmt->range_expression);
1412 token = expect(token, ',', "after range expression");
1413 token = assignment_expression(token, &stmt->range_low);
1414 token = expect(token, ',', "after low range");
1415 token = assignment_expression(token, &stmt->range_high);
1416 return expect(token, ';', "after range statement");
1418 if (match_op(token->next, ':')) {
1419 stmt->type = STMT_LABEL;
1420 stmt->label_identifier = label_symbol(token);
1421 return statement(token->next->next, &stmt->label_statement);
1425 if (match_op(token, '{')) {
1426 stmt->type = STMT_COMPOUND;
1427 start_symbol_scope();
1428 token = compound_statement(token->next, stmt);
1429 end_symbol_scope();
1431 return expect(token, '}', "at end of compound statement");
1434 stmt->type = STMT_EXPRESSION;
1435 return expression_statement(token, &stmt->expression);
1438 static struct token * statement_list(struct token *token, struct statement_list **list)
1440 int seen_statement = 0;
1441 for (;;) {
1442 struct statement * stmt;
1443 if (eof_token(token))
1444 break;
1445 if (match_op(token, '}'))
1446 break;
1447 if (lookup_type(token)) {
1448 if (seen_statement) {
1449 warning(token->pos, "mixing declarations and code");
1450 seen_statement = 0;
1452 stmt = alloc_statement(token->pos, STMT_DECLARATION);
1453 token = external_declaration(token, &stmt->declaration);
1454 } else {
1455 seen_statement = warn_on_mixed;
1456 token = statement(token, &stmt);
1458 add_statement(list, stmt);
1460 return token;
1463 static struct token *parameter_type_list(struct token *token, struct symbol *fn, struct ident **p)
1465 struct symbol_list **list = &fn->arguments;
1467 if (match_op(token, ')')) {
1468 // No warning for "void oink ();"
1469 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1470 if (p && !match_op(token->next, ';'))
1471 warning(token->pos, "non-ANSI function declaration of function '%s'", show_ident(*p));
1472 return token;
1475 for (;;) {
1476 struct symbol *sym;
1478 if (match_op(token, SPECIAL_ELLIPSIS)) {
1479 if (!*list)
1480 warning(token->pos, "variadic functions must have one named argument");
1481 fn->variadic = 1;
1482 token = token->next;
1483 break;
1486 sym = alloc_symbol(token->pos, SYM_NODE);
1487 token = parameter_declaration(token, &sym);
1488 if (sym->ctype.base_type == &void_ctype) {
1489 /* Special case: (void) */
1490 if (!*list && !sym->ident)
1491 break;
1492 warning(token->pos, "void parameter");
1494 add_symbol(list, sym);
1495 if (!match_op(token, ','))
1496 break;
1497 token = token->next;
1500 return token;
1503 struct token *compound_statement(struct token *token, struct statement *stmt)
1505 token = statement_list(token, &stmt->stmts);
1506 return token;
1509 static struct expression *identifier_expression(struct token *token)
1511 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1512 expr->expr_ident = token->ident;
1513 return expr;
1516 static struct expression *index_expression(struct expression *from, struct expression *to)
1518 int idx_from, idx_to;
1519 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1521 idx_from = get_expression_value(from);
1522 idx_to = idx_from;
1523 if (to) {
1524 idx_to = get_expression_value(to);
1525 if (idx_to < idx_from || idx_from < 0)
1526 warning(from->pos, "nonsense array initializer index range");
1528 expr->idx_from = idx_from;
1529 expr->idx_to = idx_to;
1530 return expr;
1533 static struct token *single_initializer(struct expression **ep, struct token *token)
1535 int expect_equal = 0;
1536 struct token *next = token->next;
1537 struct expression **tail = ep;
1538 int nested;
1540 *ep = NULL;
1542 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1543 struct expression *expr = identifier_expression(token);
1544 warning(token->pos, "obsolete struct initializer, use C99 syntax");
1545 token = initializer(&expr->ident_expression, next->next);
1546 if (expr->ident_expression)
1547 *ep = expr;
1548 return token;
1551 for (tail = ep, nested = 0; ; nested++, next = token->next) {
1552 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
1553 struct expression *expr = identifier_expression(next);
1554 *tail = expr;
1555 tail = &expr->ident_expression;
1556 expect_equal = 1;
1557 token = next->next;
1558 } else if (match_op(token, '[')) {
1559 struct expression *from = NULL, *to = NULL, *expr;
1560 token = constant_expression(token->next, &from);
1561 if (!from) {
1562 sparse_error(token->pos, "Expected constant expression");
1563 break;
1565 if (match_op(token, SPECIAL_ELLIPSIS))
1566 token = constant_expression(token->next, &to);
1567 expr = index_expression(from, to);
1568 *tail = expr;
1569 tail = &expr->idx_expression;
1570 token = expect(token, ']', "at end of initializer index");
1571 if (nested)
1572 expect_equal = 1;
1573 } else {
1574 break;
1577 if (nested && !expect_equal) {
1578 if (!match_op(token, '='))
1579 warning(token->pos, "obsolete array initializer, use C99 syntax");
1580 else
1581 expect_equal = 1;
1583 if (expect_equal)
1584 token = expect(token, '=', "at end of initializer index");
1586 token = initializer(tail, token);
1587 if (!*tail)
1588 *ep = NULL;
1589 return token;
1592 static struct token *initializer_list(struct expression_list **list, struct token *token)
1594 struct expression *expr;
1596 for (;;) {
1597 token = single_initializer(&expr, token);
1598 if (!expr)
1599 break;
1600 add_expression(list, expr);
1601 if (!match_op(token, ','))
1602 break;
1603 token = token->next;
1605 return token;
1608 struct token *initializer(struct expression **tree, struct token *token)
1610 if (match_op(token, '{')) {
1611 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1612 *tree = expr;
1613 token = initializer_list(&expr->expr_list, token->next);
1614 return expect(token, '}', "at end of initializer");
1616 return assignment_expression(token, tree);
1619 static void declare_argument(struct symbol *sym, struct symbol *fn)
1621 if (!sym->ident) {
1622 sparse_error(sym->pos, "no identifier for function argument");
1623 return;
1625 bind_symbol(sym, sym->ident, NS_SYMBOL);
1628 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1629 struct symbol_list **list)
1631 struct symbol_list **old_symbol_list;
1632 struct symbol *base_type = decl->ctype.base_type;
1633 struct statement *stmt, **p;
1634 struct symbol *arg;
1636 old_symbol_list = function_symbol_list;
1637 if (decl->ctype.modifiers & MOD_INLINE) {
1638 function_symbol_list = &decl->inline_symbol_list;
1639 p = &base_type->inline_stmt;
1640 } else {
1641 function_symbol_list = &decl->symbol_list;
1642 p = &base_type->stmt;
1644 function_computed_target_list = NULL;
1645 function_computed_goto_list = NULL;
1647 if (decl->ctype.modifiers & MOD_EXTERN) {
1648 if (!(decl->ctype.modifiers & MOD_INLINE))
1649 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
1651 if (!(decl->ctype.modifiers & MOD_STATIC))
1652 decl->ctype.modifiers |= MOD_EXTERN;
1654 stmt = start_function(decl);
1656 *p = stmt;
1657 FOR_EACH_PTR (base_type->arguments, arg) {
1658 declare_argument(arg, base_type);
1659 } END_FOR_EACH_PTR(arg);
1661 token = compound_statement(token->next, stmt);
1663 end_function(decl);
1664 if (!(decl->ctype.modifiers & MOD_INLINE))
1665 add_symbol(list, decl);
1666 check_declaration(decl);
1667 function_symbol_list = old_symbol_list;
1668 if (function_computed_goto_list) {
1669 if (!function_computed_target_list)
1670 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
1671 else {
1672 struct statement *stmt;
1673 FOR_EACH_PTR(function_computed_goto_list, stmt) {
1674 stmt->target_list = function_computed_target_list;
1675 } END_FOR_EACH_PTR(stmt);
1678 return expect(token, '}', "at end of function");
1681 static void promote_k_r_types(struct symbol *arg)
1683 struct symbol *base = arg->ctype.base_type;
1684 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
1685 arg->ctype.base_type = &int_ctype;
1689 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
1691 struct symbol_list *real_args = fn->ctype.base_type->arguments;
1692 struct symbol *arg;
1694 FOR_EACH_PTR(real_args, arg) {
1695 struct symbol *type;
1697 /* This is quadratic in the number of arguments. We _really_ don't care */
1698 FOR_EACH_PTR(argtypes, type) {
1699 if (type->ident == arg->ident)
1700 goto match;
1701 } END_FOR_EACH_PTR(type);
1702 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
1703 continue;
1704 match:
1705 type->used = 1;
1706 /* "char" and "short" promote to "int" */
1707 promote_k_r_types(type);
1709 arg->ctype = type->ctype;
1710 } END_FOR_EACH_PTR(arg);
1712 FOR_EACH_PTR(argtypes, arg) {
1713 if (!arg->used)
1714 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
1715 } END_FOR_EACH_PTR(arg);
1719 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
1720 struct symbol_list **list)
1722 struct symbol_list *args = NULL;
1724 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
1725 do {
1726 token = declaration_list(token, &args);
1727 if (!match_op(token, ';')) {
1728 sparse_error(token->pos, "expected ';' at end of parameter declaration");
1729 break;
1731 token = token->next;
1732 } while (lookup_type(token));
1734 apply_k_r_types(args, decl);
1736 if (!match_op(token, '{')) {
1737 sparse_error(token->pos, "expected function body");
1738 return token;
1740 return parse_function_body(token, decl, list);
1744 struct token *external_declaration(struct token *token, struct symbol_list **list)
1746 struct ident *ident = NULL;
1747 struct symbol *decl;
1748 struct ctype ctype = { 0, };
1749 struct symbol *base_type;
1750 int is_typedef;
1752 /* Top-level inline asm? */
1753 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1754 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
1755 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
1756 struct statement *stmt;
1758 anon->ctype.base_type = fn;
1759 stmt = alloc_statement(token->pos, STMT_NONE);
1760 fn->stmt = stmt;
1762 token = parse_asm(token->next, stmt);
1764 add_symbol(list, anon);
1765 return token;
1768 /* Parse declaration-specifiers, if any */
1769 token = declaration_specifiers(token, &ctype, 0);
1770 decl = alloc_symbol(token->pos, SYM_NODE);
1771 decl->ctype = ctype;
1772 token = declarator(token, decl, &ident);
1774 /* Just a type declaration? */
1775 if (!ident)
1776 return expect(token, ';', "end of type declaration");
1778 /* type define declaration? */
1779 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
1781 /* Typedef's don't have meaningful storage */
1782 if (is_typedef) {
1783 ctype.modifiers &= ~MOD_STORAGE;
1784 decl->ctype.modifiers &= ~MOD_STORAGE;
1785 decl->ctype.modifiers |= MOD_USERTYPE;
1788 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1790 base_type = decl->ctype.base_type;
1792 if (is_typedef) {
1793 if (base_type && !base_type->ident)
1794 base_type->ident = ident;
1795 } else if (base_type && base_type->type == SYM_FN) {
1796 /* K&R argument declaration? */
1797 if (lookup_type(token))
1798 return parse_k_r_arguments(token, decl, list);
1799 if (match_op(token, '{'))
1800 return parse_function_body(token, decl, list);
1802 if (!(decl->ctype.modifiers & MOD_STATIC))
1803 decl->ctype.modifiers |= MOD_EXTERN;
1804 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
1805 sparse_error(token->pos, "void declaration");
1808 for (;;) {
1809 if (!is_typedef && match_op(token, '=')) {
1810 if (decl->ctype.modifiers & MOD_EXTERN) {
1811 warning(decl->pos, "symbol with external linkage has initializer");
1812 decl->ctype.modifiers &= ~MOD_EXTERN;
1814 token = initializer(&decl->initializer, token->next);
1816 if (!is_typedef) {
1817 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
1818 add_symbol(list, decl);
1819 fn_local_symbol(decl);
1822 check_declaration(decl);
1824 if (!match_op(token, ','))
1825 break;
1827 token = token->next;
1828 ident = NULL;
1829 decl = alloc_symbol(token->pos, SYM_NODE);
1830 decl->ctype = ctype;
1831 token = declaration_specifiers(token, &decl->ctype, 1);
1832 token = declarator(token, decl, &ident);
1833 if (!ident) {
1834 sparse_error(token->pos, "expected identifier name in type definition");
1835 return token;
1838 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1840 /* Function declarations are automatically extern unless specifically static */
1841 base_type = decl->ctype.base_type;
1842 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1843 if (!(decl->ctype.modifiers & MOD_STATIC))
1844 decl->ctype.modifiers |= MOD_EXTERN;
1847 return expect(token, ';', "at end of declaration");