Fix a bug that match_idents forget to end with NULL
[smatch.git] / parse.c
blob30826d98919e17d276c98fdacf89d72e4624f0f3
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);
37 static struct token *handle_attributes(struct token *token, struct ctype *ctype);
39 // Add a symbol to the list of function-local symbols
40 static void fn_local_symbol(struct symbol *sym)
42 if (function_symbol_list)
43 add_symbol(function_symbol_list, sym);
46 static int match_idents(struct token *token, ...)
48 va_list args;
50 if (token_type(token) != TOKEN_IDENT)
51 return 0;
53 va_start(args, token);
54 for (;;) {
55 struct ident * next = va_arg(args, struct ident *);
56 if (!next)
57 return 0;
58 if (token->ident == next)
59 return 1;
64 struct statement *alloc_statement(struct position pos, int type)
66 struct statement *stmt = __alloc_statement(0);
67 stmt->type = type;
68 stmt->pos = pos;
69 return stmt;
72 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
74 static int apply_modifiers(struct position pos, struct ctype *ctype)
76 struct symbol *base;
78 while ((base = ctype->base_type)) {
79 switch (base->type) {
80 case SYM_FN:
81 case SYM_ENUM:
82 case SYM_ARRAY:
83 case SYM_BITFIELD:
84 case SYM_PTR:
85 ctype = &base->ctype;
86 continue;
88 break;
91 /* Turn the "virtual types" into real types with real sizes etc */
92 if (ctype->base_type == &int_type) {
93 ctype->base_type = ctype_integer(ctype->modifiers);
94 ctype->modifiers &= ~MOD_SPECIFIER;
95 } else if (ctype->base_type == &fp_type) {
96 ctype->base_type = ctype_fp(ctype->modifiers);
97 ctype->modifiers &= ~MOD_SPECIFIER;
100 if (ctype->modifiers & MOD_BITWISE) {
101 struct symbol *type;
102 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
103 if (!is_int_type(ctype->base_type)) {
104 sparse_error(pos, "invalid modifier");
105 return 1;
107 type = alloc_symbol(pos, SYM_BASETYPE);
108 *type = *ctype->base_type;
109 type->ctype.base_type = ctype->base_type;
110 type->type = SYM_RESTRICT;
111 type->ctype.modifiers &= ~MOD_SPECIFIER;
112 ctype->base_type = type;
113 create_fouled(type);
115 return 0;
118 static struct symbol * indirect(struct position pos, struct ctype *ctype, int type)
120 struct symbol *sym = alloc_symbol(pos, type);
122 sym->ctype.base_type = ctype->base_type;
123 sym->ctype.modifiers = ctype->modifiers & ~MOD_STORAGE;
125 ctype->base_type = sym;
126 ctype->modifiers &= MOD_STORAGE;
127 return sym;
130 static struct symbol *lookup_or_create_symbol(enum namespace ns, enum type type, struct token *token)
132 struct symbol *sym = lookup_symbol(token->ident, ns);
133 if (!sym) {
134 sym = alloc_symbol(token->pos, type);
135 bind_symbol(sym, token->ident, ns);
136 if (type == SYM_LABEL)
137 fn_local_symbol(sym);
139 return sym;
143 * NOTE! NS_LABEL is not just a different namespace,
144 * it also ends up using function scope instead of the
145 * regular symbol scope.
147 struct symbol *label_symbol(struct token *token)
149 return lookup_or_create_symbol(NS_LABEL, SYM_LABEL, token);
152 static struct token *struct_union_enum_specifier(enum type type,
153 struct token *token, struct ctype *ctype,
154 struct token *(*parse)(struct token *, struct symbol *))
156 struct symbol *sym;
157 struct position *repos;
159 ctype->modifiers = 0;
160 token = handle_attributes(token, ctype);
161 if (token_type(token) == TOKEN_IDENT) {
162 sym = lookup_symbol(token->ident, NS_STRUCT);
163 if (!sym ||
164 (sym->scope != block_scope &&
165 (match_op(token->next,';') || match_op(token->next,'{')))) {
166 // Either a new symbol, or else an out-of-scope
167 // symbol being redefined.
168 sym = alloc_symbol(token->pos, type);
169 bind_symbol(sym, token->ident, NS_STRUCT);
171 if (sym->type != type)
172 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
173 ctype->base_type = sym;
174 repos = &token->pos;
175 token = token->next;
176 if (match_op(token, '{')) {
177 // The following test is actually wrong for empty
178 // structs, but (1) they are not C99, (2) gcc does
179 // the same thing, and (3) it's easier.
180 if (sym->symbol_list)
181 error_die(token->pos, "redefinition of %s", show_typename (sym));
182 sym->pos = *repos;
183 token = parse(token->next, sym);
184 token = expect(token, '}', "at end of struct-union-enum-specifier");
186 // Mark the structure as needing re-examination
187 sym->examined = 0;
189 return token;
192 // private struct/union/enum type
193 if (!match_op(token, '{')) {
194 sparse_error(token->pos, "expected declaration");
195 ctype->base_type = &bad_ctype;
196 return token;
199 sym = alloc_symbol(token->pos, type);
200 token = parse(token->next, sym);
201 ctype->base_type = sym;
202 return expect(token, '}', "at end of specifier");
205 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
207 return struct_declaration_list(token, &sym->symbol_list);
210 static struct token *struct_or_union_specifier(enum type type, struct token *token, struct ctype *ctype)
212 return struct_union_enum_specifier(type, token, ctype, parse_struct_declaration);
215 typedef struct {
216 int x;
217 unsigned long long y;
218 } Num;
220 static void upper_boundary(Num *n, Num *v)
222 if (n->x > v->x)
223 return;
224 if (n->x < v->x) {
225 *n = *v;
226 return;
228 if (n->y < v->y)
229 n->y = v->y;
232 static void lower_boundary(Num *n, Num *v)
234 if (n->x < v->x)
235 return;
236 if (n->x > v->x) {
237 *n = *v;
238 return;
240 if (n->y > v->y)
241 n->y = v->y;
244 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
246 int shift = type->bit_size;
247 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
249 if (!is_unsigned)
250 shift--;
251 if (upper->x == 0 && upper->y >> shift)
252 return 0;
253 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
254 return 1;
255 return 0;
258 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
260 if (s1->bit_size < s2->bit_size) {
261 s1 = s2;
262 } else if (s1->bit_size == s2->bit_size) {
263 if (s2->ctype.modifiers & MOD_UNSIGNED)
264 s1 = s2;
266 if (s1->bit_size < bits_in_int)
267 return &int_ctype;
268 return s1;
271 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
273 struct symbol *sym;
275 FOR_EACH_PTR(list, sym) {
276 struct expression *expr = sym->initializer;
277 struct symbol *ctype;
278 if (expr->type != EXPR_VALUE)
279 continue;
280 ctype = expr->ctype;
281 if (ctype->bit_size == base_type->bit_size)
282 continue;
283 cast_value(expr, base_type, expr, ctype);
284 } END_FOR_EACH_PTR(sym);
287 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
289 unsigned long long lastval = 0;
290 struct symbol *ctype = NULL, *base_type = NULL;
291 Num upper = {-1, 0}, lower = {1, 0};
292 struct symbol_list *entries = NULL;
294 parent->examined = 1;
295 parent->ctype.base_type = &int_ctype;
296 while (token_type(token) == TOKEN_IDENT) {
297 struct expression *expr = NULL;
298 struct token *next = token->next;
299 struct symbol *sym;
301 sym = alloc_symbol(token->pos, SYM_NODE);
302 bind_symbol(sym, token->ident, NS_SYMBOL);
303 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
305 if (match_op(next, '=')) {
306 next = constant_expression(next->next, &expr);
307 lastval = get_expression_value(expr);
308 ctype = &void_ctype;
309 if (expr && expr->ctype)
310 ctype = expr->ctype;
311 } else if (!ctype) {
312 ctype = &int_ctype;
313 } else if (is_int_type(ctype)) {
314 lastval++;
315 } else {
316 error_die(token->pos, "can't increment the last enum member");
319 if (!expr) {
320 expr = alloc_expression(token->pos, EXPR_VALUE);
321 expr->value = lastval;
322 expr->ctype = ctype;
325 sym->initializer = expr;
326 sym->ctype.base_type = parent;
327 add_ptr_list(&entries, sym);
329 if (base_type != &bad_ctype) {
330 if (ctype->type == SYM_NODE)
331 ctype = ctype->ctype.base_type;
332 if (ctype->type == SYM_ENUM) {
333 if (ctype == parent)
334 ctype = base_type;
335 else
336 ctype = ctype->ctype.base_type;
339 * base_type rules:
340 * - if all enum's are of the same type, then
341 * the base_type is that type (two first
342 * cases)
343 * - if enums are of different types, they
344 * all have to be integer types, and the
345 * base type is at least "int_ctype".
346 * - otherwise the base_type is "bad_ctype".
348 if (!base_type) {
349 base_type = ctype;
350 } else if (ctype == base_type) {
351 /* nothing */
352 } else if (is_int_type(base_type) && is_int_type(ctype)) {
353 base_type = bigger_enum_type(base_type, ctype);
354 } else
355 base_type = &bad_ctype;
356 parent->ctype.base_type = base_type;
358 if (is_int_type(base_type)) {
359 Num v = {.y = lastval};
360 if (ctype->ctype.modifiers & MOD_UNSIGNED)
361 v.x = 0;
362 else if ((long long)lastval >= 0)
363 v.x = 0;
364 else
365 v.x = -1;
366 upper_boundary(&upper, &v);
367 lower_boundary(&lower, &v);
369 token = next;
370 if (!match_op(token, ','))
371 break;
372 token = token->next;
374 if (!base_type) {
375 sparse_error(token->pos, "bad enum definition");
376 base_type = &bad_ctype;
378 else if (!is_int_type(base_type))
379 base_type = base_type;
380 else if (type_is_ok(base_type, &upper, &lower))
381 base_type = base_type;
382 else if (type_is_ok(&int_ctype, &upper, &lower))
383 base_type = &int_ctype;
384 else if (type_is_ok(&uint_ctype, &upper, &lower))
385 base_type = &uint_ctype;
386 else if (type_is_ok(&long_ctype, &upper, &lower))
387 base_type = &long_ctype;
388 else if (type_is_ok(&ulong_ctype, &upper, &lower))
389 base_type = &ulong_ctype;
390 else if (type_is_ok(&llong_ctype, &upper, &lower))
391 base_type = &llong_ctype;
392 else if (type_is_ok(&ullong_ctype, &upper, &lower))
393 base_type = &ullong_ctype;
394 else
395 base_type = &bad_ctype;
396 parent->ctype.base_type = base_type;
397 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
398 parent->examined = 0;
400 cast_enum_list(entries, base_type);
401 free_ptr_list(&entries);
403 return token;
406 static struct token *enum_specifier(struct token *token, struct ctype *ctype)
408 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctype, parse_enum_declaration);
410 ctype = &ctype->base_type->ctype;
411 if (!ctype->base_type)
412 ctype->base_type = &incomplete_ctype;
414 return ret;
417 static struct token *typeof_specifier(struct token *token, struct ctype *ctype)
419 struct symbol *sym;
421 if (!match_op(token, '(')) {
422 sparse_error(token->pos, "expected '(' after typeof");
423 return token;
425 if (lookup_type(token->next)) {
426 token = typename(token->next, &sym);
427 *ctype = sym->ctype;
428 } else {
429 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
430 token = parse_expression(token->next, &typeof_sym->initializer);
432 ctype->modifiers = 0;
433 ctype->base_type = typeof_sym;
435 return expect(token, ')', "after typeof");
438 static const char * handle_attribute(struct ctype *ctype, struct ident *attribute, struct expression *expr)
440 if (attribute == &packed_ident ||
441 attribute == &__packed___ident) {
442 ctype->alignment = 1;
443 return NULL;
445 if (attribute == &aligned_ident ||
446 attribute == &__aligned___ident) {
447 int alignment = max_alignment;
448 if (expr)
449 alignment = get_expression_value(expr);
450 ctype->alignment = alignment;
451 return NULL;
453 if (attribute == &nocast_ident) {
454 ctype->modifiers |= MOD_NOCAST;
455 return NULL;
457 if (attribute == &noderef_ident) {
458 ctype->modifiers |= MOD_NODEREF;
459 return NULL;
461 if (attribute == &safe_ident) {
462 ctype->modifiers |= MOD_SAFE;
463 return NULL;
465 if (attribute == &force_ident) {
466 ctype->modifiers |= MOD_FORCE;
467 return NULL;
469 if (attribute == &bitwise_ident ||
470 attribute == &__bitwise___ident) {
471 if (Wbitwise)
472 ctype->modifiers |= MOD_BITWISE;
473 return NULL;
475 if (attribute == &address_space_ident) {
476 if (!expr)
477 return "expected address space number";
478 ctype->as = get_expression_value(expr);
479 return NULL;
481 if (attribute == &context_ident) {
482 if (expr && expr->type == EXPR_COMMA) {
483 struct context *context = alloc_context();
484 if(expr->left->type == EXPR_COMMA) {
485 context->context = expr->left->left;
486 context->in = get_expression_value(
487 expr->left->right);
488 } else {
489 context->context = NULL;
490 context->in = get_expression_value(expr->left);
492 context->out = get_expression_value(expr->right);
493 add_ptr_list(&ctype->contexts, context);
494 return NULL;
496 return "expected context input/output values";
498 if (attribute == &mode_ident ||
499 attribute == &__mode___ident) {
500 if (expr && expr->type == EXPR_SYMBOL) {
501 struct ident *ident = expr->symbol_name;
504 * Match against __QI__/__HI__/__SI__/__DI__
506 * FIXME! This is broken - we don't actually get
507 * the type information updated properly at this
508 * stage for some reason.
510 if (ident == &__QI___ident ||
511 ident == &QI_ident) {
512 ctype->modifiers |= MOD_CHAR;
513 return NULL;
515 if (ident == &__HI___ident ||
516 ident == &HI_ident) {
517 ctype->modifiers |= MOD_SHORT;
518 return NULL;
520 if (ident == &__SI___ident ||
521 ident == &SI_ident) {
522 /* Nothing? */
523 return NULL;
525 if (ident == &__DI___ident ||
526 ident == &DI_ident) {
527 ctype->modifiers |= MOD_LONGLONG;
528 return NULL;
530 if (ident == &__word___ident ||
531 ident == &word_ident) {
532 ctype->modifiers |= MOD_LONG;
533 return NULL;
535 return "unknown mode attribute";
537 return "expected attribute mode symbol";
540 /* Throw away for now.. */
541 if (attribute == &__transparent_union___ident) {
542 if (Wtransparent_union)
543 return "ignoring attribute __transparent_union__";
544 return NULL;
546 if (attribute == &nothrow_ident ||
547 attribute == &__nothrow_ident ||
548 attribute == &__nothrow___ident)
549 return NULL;
550 if (attribute == &__malloc___ident)
551 return NULL;
552 if (attribute == &nonnull_ident ||
553 attribute == &__nonnull_ident ||
554 attribute == &__nonnull___ident)
555 return NULL;
556 if (attribute == &format_ident ||
557 attribute == &__format___ident ||
558 attribute == &__format_arg___ident)
559 return NULL;
560 if (attribute == &section_ident ||
561 attribute == &__section___ident)
562 return NULL;
563 if (attribute == &unused_ident ||
564 attribute == &__unused___ident)
565 return NULL;
566 if (attribute == &const_ident ||
567 attribute == &__const_ident ||
568 attribute == &__const___ident)
569 return NULL;
570 if (attribute == &noreturn_ident ||
571 attribute == &__noreturn___ident)
572 return NULL;
573 if (attribute == &no_instrument_function_ident ||
574 attribute == &__no_instrument_function___ident)
575 return NULL;
576 if (attribute == &sentinel_ident ||
577 attribute == &__sentinel___ident)
578 return NULL;
579 if (attribute == &regparm_ident)
580 return NULL;
581 if (attribute == &weak_ident ||
582 attribute == &__weak___ident)
583 return NULL;
584 if (attribute == &alias_ident ||
585 attribute == &__alias___ident)
586 return NULL;
587 if (attribute == &pure_ident ||
588 attribute == &__pure___ident)
589 return NULL;
590 if (attribute == &always_inline_ident)
591 return NULL;
592 if (attribute == &syscall_linkage_ident)
593 return NULL;
594 if (attribute == &visibility_ident ||
595 attribute == &__visibility___ident)
596 return NULL;
597 if (attribute == &deprecated_ident ||
598 attribute == &__deprecated___ident)
599 return NULL;
600 if (attribute == &noinline_ident)
601 return NULL;
602 if (attribute == &__used___ident)
603 return NULL;
604 if (attribute == &warn_unused_result_ident ||
605 attribute == &__warn_unused_result___ident)
606 return NULL;
607 if (attribute == &model_ident ||
608 attribute == &__model___ident)
609 return NULL;
611 return "unknown attribute";
614 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
616 ctype->modifiers = 0;
617 token = expect(token, '(', "after attribute");
618 token = expect(token, '(', "after attribute");
620 for (;;) {
621 const char *error_str;
622 struct ident *attribute_name;
623 struct expression *attribute_expr;
625 if (eof_token(token))
626 break;
627 if (match_op(token, ';'))
628 break;
629 if (token_type(token) != TOKEN_IDENT)
630 break;
631 attribute_name = token->ident;
632 token = token->next;
633 attribute_expr = NULL;
634 if (match_op(token, '('))
635 token = parens_expression(token, &attribute_expr, "in attribute");
636 error_str = handle_attribute(ctype, attribute_name, attribute_expr);
637 if (error_str)
638 sparse_error(token->pos, "attribute '%s': %s", show_ident(attribute_name), error_str);
639 if (!match_op(token, ','))
640 break;
641 token = token->next;
644 token = expect(token, ')', "after attribute");
645 token = expect(token, ')', "after attribute");
646 return token;
649 struct symbol * ctype_integer(unsigned long spec)
651 static struct symbol *const integer_ctypes[][3] = {
652 { &llong_ctype, &sllong_ctype, &ullong_ctype },
653 { &long_ctype, &slong_ctype, &ulong_ctype },
654 { &short_ctype, &sshort_ctype, &ushort_ctype },
655 { &char_ctype, &schar_ctype, &uchar_ctype },
656 { &int_ctype, &sint_ctype, &uint_ctype },
658 struct symbol *const (*ctype)[3];
659 int sub;
661 ctype = integer_ctypes;
662 if (!(spec & MOD_LONGLONG)) {
663 ctype++;
664 if (!(spec & MOD_LONG)) {
665 ctype++;
666 if (!(spec & MOD_SHORT)) {
667 ctype++;
668 if (!(spec & MOD_CHAR))
669 ctype++;
674 sub = ((spec & MOD_UNSIGNED)
676 : ((spec & MOD_EXPLICITLY_SIGNED)
678 : 0));
680 return ctype[0][sub];
683 struct symbol * ctype_fp(unsigned long spec)
685 if (spec & MOD_LONGLONG)
686 return &ldouble_ctype;
687 if (spec & MOD_LONG)
688 return &double_ctype;
689 return &float_ctype;
692 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
694 unsigned long mod = thistype->modifiers;
696 if (mod) {
697 unsigned long old = ctype->modifiers;
698 unsigned long extra = 0, dup, conflict;
700 if (mod & old & MOD_LONG) {
701 extra = MOD_LONGLONG | MOD_LONG;
702 mod &= ~MOD_LONG;
703 old &= ~MOD_LONG;
705 dup = (mod & old) | (extra & old) | (extra & mod);
706 if (dup)
707 sparse_error(pos, "Just how %sdo you want this type to be?",
708 modifier_string(dup));
710 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
711 if (conflict)
712 sparse_error(pos, "You cannot have both long and short modifiers.");
714 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
715 if (conflict)
716 sparse_error(pos, "You cannot have both signed and unsigned modifiers.");
718 // Only one storage modifier allowed, except that "inline" doesn't count.
719 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
720 conflict &= (conflict - 1);
721 if (conflict)
722 sparse_error(pos, "multiple storage classes");
724 ctype->modifiers = old | mod | extra;
727 /* Context */
728 concat_ptr_list((struct ptr_list *)thistype->contexts,
729 (struct ptr_list **)&ctype->contexts);
731 /* Alignment */
732 if (thistype->alignment & (thistype->alignment-1)) {
733 warning(pos, "I don't like non-power-of-2 alignments");
734 thistype->alignment = 0;
736 if (thistype->alignment > ctype->alignment)
737 ctype->alignment = thistype->alignment;
739 /* Address space */
740 if (thistype->as)
741 ctype->as = thistype->as;
744 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
746 unsigned long banned, wrong;
747 unsigned long this_mod = s->ctype.modifiers;
748 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
749 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
751 if (this_mod & (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF))
752 banned = BANNED_SIZE | BANNED_SIGN;
753 else if (this_mod & MOD_SPECIALBITS)
754 banned = 0;
755 else if (s->ctype.base_type == &fp_type)
756 banned = BANNED_SIGN;
757 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
758 banned = 0;
759 else {
760 // label_type
761 // void_type
762 // bad_type
763 // vector_type <-- whatever that is
764 banned = BANNED_SIZE | BANNED_SIGN;
767 wrong = mod & banned;
768 if (wrong)
769 sparse_error(*pos, "modifier %sis invalid in this context",
770 modifier_string (wrong));
773 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
775 struct token *token;
777 while ( (token = next) != NULL ) {
778 struct ctype thistype;
779 struct ident *ident;
780 struct symbol *s, *type;
781 unsigned long mod;
783 next = token->next;
784 if (token_type(token) != TOKEN_IDENT)
785 break;
786 ident = token->ident;
788 s = lookup_symbol(ident, NS_TYPEDEF);
789 if (!s)
790 break;
791 thistype = s->ctype;
792 mod = thistype.modifiers;
793 if (qual && (mod & ~(MOD_ATTRIBUTE | MOD_CONST | MOD_VOLATILE)))
794 break;
795 if (mod & MOD_SPECIALBITS) {
796 if (mod & MOD_STRUCTOF)
797 next = struct_or_union_specifier(SYM_STRUCT, next, &thistype);
798 else if (mod & MOD_UNIONOF)
799 next = struct_or_union_specifier(SYM_UNION, next, &thistype);
800 else if (mod & MOD_ENUMOF)
801 next = enum_specifier(next, &thistype);
802 else if (mod & MOD_ATTRIBUTE)
803 next = attribute_specifier(next, &thistype);
804 else if (mod & MOD_TYPEOF)
805 next = typeof_specifier(next, &thistype);
806 mod = thistype.modifiers;
808 type = thistype.base_type;
809 if (type) {
810 if (qual)
811 break;
812 if (ctype->base_type)
813 break;
814 /* User types only mix with qualifiers */
815 if (mod & MOD_USERTYPE) {
816 if (ctype->modifiers & MOD_SPECIFIER)
817 break;
819 ctype->base_type = type;
822 check_modifiers(&token->pos, s, ctype->modifiers);
823 apply_ctype(token->pos, &thistype, ctype);
826 if (!ctype->base_type) {
827 struct symbol *base = &incomplete_ctype;
830 * If we have modifiers, we'll default to an integer
831 * type, and "ctype_integer()" will turn this into
832 * a specific one.
834 if (ctype->modifiers & MOD_SPECIFIER)
835 base = &int_type;
836 ctype->base_type = base;
838 return token;
841 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
843 struct expression *expr = NULL;
845 token = parse_expression(token, &expr);
846 sym->array_size = expr;
847 return token;
850 static struct token *parameter_type_list(struct token *, struct symbol *, struct ident **p);
851 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
853 static struct token *handle_attributes(struct token *token, struct ctype *ctype)
855 for (;;) {
856 if (token_type(token) != TOKEN_IDENT)
857 break;
858 if (match_idents(token, &__attribute___ident, &__attribute_ident, NULL)) {
859 struct ctype thistype = { 0, };
860 token = attribute_specifier(token->next, &thistype);
861 apply_ctype(token->pos, &thistype, ctype);
862 continue;
864 if (match_idents(token, &asm_ident, &__asm_ident, &__asm___ident, NULL)) {
865 struct expression *expr;
866 token = expect(token->next, '(', "after asm");
867 token = parse_expression(token->next, &expr);
868 token = expect(token, ')', "after asm");
869 continue;
871 break;
873 return token;
876 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
878 struct ctype *ctype = &decl->ctype;
880 if (p && token_type(token) == TOKEN_IDENT) {
881 *p = token->ident;
882 token = token->next;
885 for (;;) {
886 token = handle_attributes(token, ctype);
888 if (token_type(token) != TOKEN_SPECIAL)
889 return token;
892 * This can be either a parameter list or a grouping.
893 * For the direct (non-abstract) case, we know if must be
894 * a parameter list if we already saw the identifier.
895 * For the abstract case, we know if must be a parameter
896 * list if it is empty or starts with a type.
898 if (token->special == '(') {
899 struct symbol *sym;
900 struct token *next = token->next;
901 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
903 if (!fn) {
904 struct symbol *base_type = ctype->base_type;
905 token = declarator(next, decl, p);
906 token = expect(token, ')', "in nested declarator");
907 while (ctype->base_type != base_type)
908 ctype = &ctype->base_type->ctype;
909 p = NULL;
910 continue;
913 sym = indirect(token->pos, ctype, SYM_FN);
914 token = parameter_type_list(next, sym, p);
915 token = expect(token, ')', "in function declarator");
916 continue;
918 if (token->special == '[') {
919 struct symbol *array = indirect(token->pos, ctype, SYM_ARRAY);
920 token = abstract_array_declarator(token->next, array);
921 token = expect(token, ']', "in abstract_array_declarator");
922 ctype = &array->ctype;
923 continue;
925 break;
927 return token;
930 static struct token *pointer(struct token *token, struct ctype *ctype)
932 unsigned long modifiers;
933 struct symbol *base_type;
935 modifiers = ctype->modifiers & ~(MOD_TYPEDEF | MOD_ATTRIBUTE);
936 base_type = ctype->base_type;
937 ctype->modifiers = modifiers;
939 while (match_op(token,'*')) {
940 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
941 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
942 ptr->ctype.as = ctype->as;
943 concat_ptr_list((struct ptr_list *)ctype->contexts,
944 (struct ptr_list **)&ptr->ctype.contexts);
945 ptr->ctype.base_type = base_type;
947 base_type = ptr;
948 ctype->modifiers = modifiers & MOD_STORAGE;
949 ctype->base_type = base_type;
950 ctype->as = 0;
951 free_ptr_list(&ctype->contexts);
953 token = declaration_specifiers(token->next, ctype, 1);
954 modifiers = ctype->modifiers;
956 return token;
959 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
961 token = pointer(token, &sym->ctype);
962 return direct_declarator(token, sym, p);
965 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
967 struct ctype *ctype = &decl->ctype;
968 struct expression *expr;
969 struct symbol *bitfield;
970 long long width;
972 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
973 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
974 show_typename(ctype->base_type));
975 // Parse this to recover gracefully.
976 return conditional_expression(token->next, &expr);
979 bitfield = indirect(token->pos, ctype, SYM_BITFIELD);
980 token = conditional_expression(token->next, &expr);
981 width = get_expression_value(expr);
982 bitfield->bit_size = width;
984 if (width < 0 || width > INT_MAX) {
985 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
986 width = -1;
987 } else if (decl->ident && width == 0) {
988 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
989 show_ident(decl->ident));
990 width = -1;
991 } else if (decl->ident) {
992 struct symbol *base_type = bitfield->ctype.base_type;
993 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
994 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
995 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
996 // Valid values are either {-1;0} or {0}, depending on integer
997 // representation. The latter makes for very efficient code...
998 sparse_error(token->pos, "dubious one-bit signed bitfield");
1000 if (Wdefault_bitfield_sign &&
1001 bitfield_type->type != SYM_ENUM &&
1002 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1003 is_signed) {
1004 // The sign of bitfields is unspecified by default.
1005 sparse_error(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1008 bitfield->bit_size = width;
1009 return token;
1012 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1014 struct ctype ctype = {0, };
1016 token = declaration_specifiers(token, &ctype, 0);
1017 for (;;) {
1018 struct ident *ident = NULL;
1019 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1020 decl->ctype = ctype;
1021 token = declarator(token, decl, &ident);
1022 decl->ident = ident;
1023 if (match_op(token, ':')) {
1024 token = handle_bitfield(token, decl);
1025 token = handle_attributes(token, &decl->ctype);
1027 apply_modifiers(token->pos, &decl->ctype);
1028 add_symbol(list, decl);
1029 if (!match_op(token, ','))
1030 break;
1031 token = token->next;
1033 return token;
1036 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1038 while (!match_op(token, '}')) {
1039 if (!match_op(token, ';'))
1040 token = declaration_list(token, list);
1041 if (!match_op(token, ';')) {
1042 sparse_error(token->pos, "expected ; at end of declaration");
1043 break;
1045 token = token->next;
1047 return token;
1050 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
1052 struct ident *ident = NULL;
1053 struct symbol *sym;
1054 struct ctype ctype = { 0, };
1056 token = declaration_specifiers(token, &ctype, 0);
1057 sym = alloc_symbol(token->pos, SYM_NODE);
1058 sym->ctype = ctype;
1059 *tree = sym;
1060 token = declarator(token, sym, &ident);
1061 sym->ident = ident;
1062 apply_modifiers(token->pos, &sym->ctype);
1063 return token;
1066 struct token *typename(struct token *token, struct symbol **p)
1068 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1069 *p = sym;
1070 token = declaration_specifiers(token, &sym->ctype, 0);
1071 token = declarator(token, sym, NULL);
1072 apply_modifiers(token->pos, &sym->ctype);
1073 return token;
1076 static struct token *expression_statement(struct token *token, struct expression **tree)
1078 token = parse_expression(token, tree);
1079 return expect(token, ';', "at end of statement");
1082 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1083 struct expression_list **inout)
1085 struct expression *expr;
1087 /* Allow empty operands */
1088 if (match_op(token->next, ':') || match_op(token->next, ')'))
1089 return token->next;
1090 do {
1091 struct ident *ident = NULL;
1092 if (match_op(token->next, '[') &&
1093 token_type(token->next->next) == TOKEN_IDENT &&
1094 match_op(token->next->next->next, ']')) {
1095 ident = token->next->next->ident;
1096 token = token->next->next->next;
1098 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1099 token = primary_expression(token->next, &expr);
1100 add_expression(inout, expr);
1101 token = parens_expression(token, &expr, "in asm parameter");
1102 add_expression(inout, expr);
1103 } while (match_op(token, ','));
1104 return token;
1107 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1108 struct expression_list **clobbers)
1110 struct expression *expr;
1112 do {
1113 token = primary_expression(token->next, &expr);
1114 add_expression(clobbers, expr);
1115 } while (match_op(token, ','));
1116 return token;
1119 static struct token *parse_asm(struct token *token, struct statement *stmt)
1121 stmt->type = STMT_ASM;
1122 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1123 token = token->next;
1125 token = expect(token, '(', "after asm");
1126 token = parse_expression(token, &stmt->asm_string);
1127 if (match_op(token, ':'))
1128 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1129 if (match_op(token, ':'))
1130 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1131 if (match_op(token, ':'))
1132 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1133 token = expect(token, ')', "after asm");
1134 return expect(token, ';', "at end of asm-statement");
1137 /* Make a statement out of an expression */
1138 static struct statement *make_statement(struct expression *expr)
1140 struct statement *stmt;
1142 if (!expr)
1143 return NULL;
1144 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1145 stmt->expression = expr;
1146 return stmt;
1150 * All iterators have two symbols associated with them:
1151 * the "continue" and "break" symbols, which are targets
1152 * for continue and break statements respectively.
1154 * They are in a special name-space, but they follow
1155 * all the normal visibility rules, so nested iterators
1156 * automatically work right.
1158 static void start_iterator(struct statement *stmt)
1160 struct symbol *cont, *brk;
1162 start_symbol_scope();
1163 cont = alloc_symbol(stmt->pos, SYM_NODE);
1164 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1165 brk = alloc_symbol(stmt->pos, SYM_NODE);
1166 bind_symbol(brk, &break_ident, NS_ITERATOR);
1168 stmt->type = STMT_ITERATOR;
1169 stmt->iterator_break = brk;
1170 stmt->iterator_continue = cont;
1171 fn_local_symbol(brk);
1172 fn_local_symbol(cont);
1175 static void end_iterator(struct statement *stmt)
1177 end_symbol_scope();
1180 static struct statement *start_function(struct symbol *sym)
1182 struct symbol *ret;
1183 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1185 start_function_scope();
1186 ret = alloc_symbol(sym->pos, SYM_NODE);
1187 ret->ctype = sym->ctype.base_type->ctype;
1188 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1189 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1190 bind_symbol(ret, &return_ident, NS_ITERATOR);
1191 stmt->ret = ret;
1192 fn_local_symbol(ret);
1194 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1195 current_fn = sym;
1197 return stmt;
1200 static void end_function(struct symbol *sym)
1202 current_fn = NULL;
1203 end_function_scope();
1207 * A "switch()" statement, like an iterator, has a
1208 * the "break" symbol associated with it. It works
1209 * exactly like the iterator break - it's the target
1210 * for any break-statements in scope, and means that
1211 * "break" handling doesn't even need to know whether
1212 * it's breaking out of an iterator or a switch.
1214 * In addition, the "case" symbol is a marker for the
1215 * case/default statements to find the switch statement
1216 * that they are associated with.
1218 static void start_switch(struct statement *stmt)
1220 struct symbol *brk, *switch_case;
1222 start_symbol_scope();
1223 brk = alloc_symbol(stmt->pos, SYM_NODE);
1224 bind_symbol(brk, &break_ident, NS_ITERATOR);
1226 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1227 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1228 switch_case->stmt = stmt;
1230 stmt->type = STMT_SWITCH;
1231 stmt->switch_break = brk;
1232 stmt->switch_case = switch_case;
1234 fn_local_symbol(brk);
1235 fn_local_symbol(switch_case);
1238 static void end_switch(struct statement *stmt)
1240 if (!stmt->switch_case->symbol_list)
1241 warning(stmt->pos, "switch with no cases");
1242 end_symbol_scope();
1245 static void add_case_statement(struct statement *stmt)
1247 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1248 struct symbol *sym;
1250 if (!target) {
1251 sparse_error(stmt->pos, "not in switch scope");
1252 stmt->type = STMT_NONE;
1253 return;
1255 sym = alloc_symbol(stmt->pos, SYM_NODE);
1256 add_symbol(&target->symbol_list, sym);
1257 sym->stmt = stmt;
1258 stmt->case_label = sym;
1259 fn_local_symbol(sym);
1262 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1264 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1266 if (!target)
1267 error_die(token->pos, "internal error: return without a function target");
1268 stmt->type = STMT_RETURN;
1269 stmt->ret_target = target;
1270 return expression_statement(token->next, &stmt->ret_value);
1273 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1275 struct symbol_list *syms;
1276 struct expression *e1, *e2, *e3;
1277 struct statement *iterator;
1279 start_iterator(stmt);
1280 token = expect(token->next, '(', "after 'for'");
1282 syms = NULL;
1283 e1 = NULL;
1284 /* C99 variable declaration? */
1285 if (lookup_type(token)) {
1286 token = external_declaration(token, &syms);
1287 } else {
1288 token = parse_expression(token, &e1);
1289 token = expect(token, ';', "in 'for'");
1291 token = parse_expression(token, &e2);
1292 token = expect(token, ';', "in 'for'");
1293 token = parse_expression(token, &e3);
1294 token = expect(token, ')', "in 'for'");
1295 token = statement(token, &iterator);
1297 stmt->iterator_syms = syms;
1298 stmt->iterator_pre_statement = make_statement(e1);
1299 stmt->iterator_pre_condition = e2;
1300 stmt->iterator_post_statement = make_statement(e3);
1301 stmt->iterator_post_condition = NULL;
1302 stmt->iterator_statement = iterator;
1303 end_iterator(stmt);
1305 return token;
1308 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
1310 struct expression *expr;
1311 struct statement *iterator;
1313 start_iterator(stmt);
1314 token = parens_expression(token->next, &expr, "after 'while'");
1315 token = statement(token, &iterator);
1317 stmt->iterator_pre_condition = expr;
1318 stmt->iterator_post_condition = NULL;
1319 stmt->iterator_statement = iterator;
1320 end_iterator(stmt);
1322 return token;
1325 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
1327 struct expression *expr;
1328 struct statement *iterator;
1330 start_iterator(stmt);
1331 token = statement(token->next, &iterator);
1332 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1333 token = token->next;
1334 else
1335 sparse_error(token->pos, "expected 'while' after 'do'");
1336 token = parens_expression(token, &expr, "after 'do-while'");
1338 stmt->iterator_post_condition = expr;
1339 stmt->iterator_statement = iterator;
1340 end_iterator(stmt);
1342 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
1343 warning(iterator->pos, "do-while statement is not a compound statement");
1345 return expect(token, ';', "after statement");
1348 static struct token *statement(struct token *token, struct statement **tree)
1350 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1352 *tree = stmt;
1353 if (token_type(token) == TOKEN_IDENT) {
1354 if (token->ident == &if_ident) {
1355 stmt->type = STMT_IF;
1356 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1357 token = statement(token, &stmt->if_true);
1358 if (token_type(token) != TOKEN_IDENT)
1359 return token;
1360 if (token->ident != &else_ident)
1361 return token;
1362 return statement(token->next, &stmt->if_false);
1365 if (token->ident == &return_ident)
1366 return parse_return_statement(token, stmt);
1368 if (token->ident == &break_ident || token->ident == &continue_ident) {
1369 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1370 stmt->type = STMT_GOTO;
1371 stmt->goto_label = target;
1372 if (!target)
1373 sparse_error(stmt->pos, "break/continue not in iterator scope");
1374 return expect(token->next, ';', "at end of statement");
1376 if (token->ident == &default_ident) {
1377 token = token->next;
1378 goto default_statement;
1380 if (token->ident == &case_ident) {
1381 token = parse_expression(token->next, &stmt->case_expression);
1382 if (match_op(token, SPECIAL_ELLIPSIS))
1383 token = parse_expression(token->next, &stmt->case_to);
1384 default_statement:
1385 stmt->type = STMT_CASE;
1386 token = expect(token, ':', "after default/case");
1387 add_case_statement(stmt);
1388 return statement(token, &stmt->case_statement);
1390 if (token->ident == &switch_ident) {
1391 stmt->type = STMT_SWITCH;
1392 start_switch(stmt);
1393 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1394 token = statement(token, &stmt->switch_statement);
1395 end_switch(stmt);
1396 return token;
1398 if (token->ident == &for_ident)
1399 return parse_for_statement(token, stmt);
1401 if (token->ident == &while_ident)
1402 return parse_while_statement(token, stmt);
1404 if (token->ident == &do_ident)
1405 return parse_do_statement(token, stmt);
1407 if (token->ident == &goto_ident) {
1408 stmt->type = STMT_GOTO;
1409 token = token->next;
1410 if (match_op(token, '*')) {
1411 token = parse_expression(token->next, &stmt->goto_expression);
1412 add_statement(&function_computed_goto_list, stmt);
1413 } else if (token_type(token) == TOKEN_IDENT) {
1414 stmt->goto_label = label_symbol(token);
1415 token = token->next;
1416 } else {
1417 sparse_error(token->pos, "Expected identifier or goto expression");
1419 return expect(token, ';', "at end of statement");
1421 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1422 return parse_asm(token->next, stmt);
1424 if (token->ident == &__context___ident) {
1425 stmt->type = STMT_CONTEXT;
1426 token = parse_expression(token->next, &stmt->expression);
1427 if(stmt->expression->type == EXPR_PREOP
1428 && stmt->expression->op == '('
1429 && stmt->expression->unop->type == EXPR_COMMA) {
1430 struct expression *expr;
1431 expr = stmt->expression->unop;
1432 stmt->context = expr->left;
1433 stmt->expression = expr->right;
1435 return expect(token, ';', "at end of statement");
1437 if (token->ident == &__range___ident) {
1438 stmt->type = STMT_RANGE;
1439 token = assignment_expression(token->next, &stmt->range_expression);
1440 token = expect(token, ',', "after range expression");
1441 token = assignment_expression(token, &stmt->range_low);
1442 token = expect(token, ',', "after low range");
1443 token = assignment_expression(token, &stmt->range_high);
1444 return expect(token, ';', "after range statement");
1446 if (match_op(token->next, ':')) {
1447 stmt->type = STMT_LABEL;
1448 stmt->label_identifier = label_symbol(token);
1449 return statement(token->next->next, &stmt->label_statement);
1453 if (match_op(token, '{')) {
1454 stmt->type = STMT_COMPOUND;
1455 start_symbol_scope();
1456 token = compound_statement(token->next, stmt);
1457 end_symbol_scope();
1459 return expect(token, '}', "at end of compound statement");
1462 stmt->type = STMT_EXPRESSION;
1463 return expression_statement(token, &stmt->expression);
1466 static struct token * statement_list(struct token *token, struct statement_list **list)
1468 int seen_statement = 0;
1469 for (;;) {
1470 struct statement * stmt;
1471 if (eof_token(token))
1472 break;
1473 if (match_op(token, '}'))
1474 break;
1475 if (lookup_type(token)) {
1476 if (seen_statement) {
1477 warning(token->pos, "mixing declarations and code");
1478 seen_statement = 0;
1480 stmt = alloc_statement(token->pos, STMT_DECLARATION);
1481 token = external_declaration(token, &stmt->declaration);
1482 } else {
1483 seen_statement = warn_on_mixed;
1484 token = statement(token, &stmt);
1486 add_statement(list, stmt);
1488 return token;
1491 static struct token *parameter_type_list(struct token *token, struct symbol *fn, struct ident **p)
1493 struct symbol_list **list = &fn->arguments;
1495 if (match_op(token, ')')) {
1496 // No warning for "void oink ();"
1497 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1498 if (p && !match_op(token->next, ';'))
1499 warning(token->pos, "non-ANSI function declaration of function '%s'", show_ident(*p));
1500 return token;
1503 for (;;) {
1504 struct symbol *sym;
1506 if (match_op(token, SPECIAL_ELLIPSIS)) {
1507 if (!*list)
1508 warning(token->pos, "variadic functions must have one named argument");
1509 fn->variadic = 1;
1510 token = token->next;
1511 break;
1514 sym = alloc_symbol(token->pos, SYM_NODE);
1515 token = parameter_declaration(token, &sym);
1516 if (sym->ctype.base_type == &void_ctype) {
1517 /* Special case: (void) */
1518 if (!*list && !sym->ident)
1519 break;
1520 warning(token->pos, "void parameter");
1522 add_symbol(list, sym);
1523 if (!match_op(token, ','))
1524 break;
1525 token = token->next;
1528 return token;
1531 struct token *compound_statement(struct token *token, struct statement *stmt)
1533 token = statement_list(token, &stmt->stmts);
1534 return token;
1537 static struct expression *identifier_expression(struct token *token)
1539 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1540 expr->expr_ident = token->ident;
1541 return expr;
1544 static struct expression *index_expression(struct expression *from, struct expression *to)
1546 int idx_from, idx_to;
1547 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1549 idx_from = get_expression_value(from);
1550 idx_to = idx_from;
1551 if (to) {
1552 idx_to = get_expression_value(to);
1553 if (idx_to < idx_from || idx_from < 0)
1554 warning(from->pos, "nonsense array initializer index range");
1556 expr->idx_from = idx_from;
1557 expr->idx_to = idx_to;
1558 return expr;
1561 static struct token *single_initializer(struct expression **ep, struct token *token)
1563 int expect_equal = 0;
1564 struct token *next = token->next;
1565 struct expression **tail = ep;
1566 int nested;
1568 *ep = NULL;
1570 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1571 struct expression *expr = identifier_expression(token);
1572 warning(token->pos, "obsolete struct initializer, use C99 syntax");
1573 token = initializer(&expr->ident_expression, next->next);
1574 if (expr->ident_expression)
1575 *ep = expr;
1576 return token;
1579 for (tail = ep, nested = 0; ; nested++, next = token->next) {
1580 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
1581 struct expression *expr = identifier_expression(next);
1582 *tail = expr;
1583 tail = &expr->ident_expression;
1584 expect_equal = 1;
1585 token = next->next;
1586 } else if (match_op(token, '[')) {
1587 struct expression *from = NULL, *to = NULL, *expr;
1588 token = constant_expression(token->next, &from);
1589 if (!from) {
1590 sparse_error(token->pos, "Expected constant expression");
1591 break;
1593 if (match_op(token, SPECIAL_ELLIPSIS))
1594 token = constant_expression(token->next, &to);
1595 expr = index_expression(from, to);
1596 *tail = expr;
1597 tail = &expr->idx_expression;
1598 token = expect(token, ']', "at end of initializer index");
1599 if (nested)
1600 expect_equal = 1;
1601 } else {
1602 break;
1605 if (nested && !expect_equal) {
1606 if (!match_op(token, '='))
1607 warning(token->pos, "obsolete array initializer, use C99 syntax");
1608 else
1609 expect_equal = 1;
1611 if (expect_equal)
1612 token = expect(token, '=', "at end of initializer index");
1614 token = initializer(tail, token);
1615 if (!*tail)
1616 *ep = NULL;
1617 return token;
1620 static struct token *initializer_list(struct expression_list **list, struct token *token)
1622 struct expression *expr;
1624 for (;;) {
1625 token = single_initializer(&expr, token);
1626 if (!expr)
1627 break;
1628 add_expression(list, expr);
1629 if (!match_op(token, ','))
1630 break;
1631 token = token->next;
1633 return token;
1636 struct token *initializer(struct expression **tree, struct token *token)
1638 if (match_op(token, '{')) {
1639 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1640 *tree = expr;
1641 token = initializer_list(&expr->expr_list, token->next);
1642 return expect(token, '}', "at end of initializer");
1644 return assignment_expression(token, tree);
1647 static void declare_argument(struct symbol *sym, struct symbol *fn)
1649 if (!sym->ident) {
1650 sparse_error(sym->pos, "no identifier for function argument");
1651 return;
1653 bind_symbol(sym, sym->ident, NS_SYMBOL);
1656 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1657 struct symbol_list **list)
1659 struct symbol_list **old_symbol_list;
1660 struct symbol *base_type = decl->ctype.base_type;
1661 struct statement *stmt, **p;
1662 struct symbol *arg;
1664 old_symbol_list = function_symbol_list;
1665 if (decl->ctype.modifiers & MOD_INLINE) {
1666 function_symbol_list = &decl->inline_symbol_list;
1667 p = &base_type->inline_stmt;
1668 } else {
1669 function_symbol_list = &decl->symbol_list;
1670 p = &base_type->stmt;
1672 function_computed_target_list = NULL;
1673 function_computed_goto_list = NULL;
1675 if (decl->ctype.modifiers & MOD_EXTERN) {
1676 if (!(decl->ctype.modifiers & MOD_INLINE))
1677 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
1679 if (!(decl->ctype.modifiers & MOD_STATIC))
1680 decl->ctype.modifiers |= MOD_EXTERN;
1682 stmt = start_function(decl);
1684 *p = stmt;
1685 FOR_EACH_PTR (base_type->arguments, arg) {
1686 declare_argument(arg, base_type);
1687 } END_FOR_EACH_PTR(arg);
1689 token = compound_statement(token->next, stmt);
1691 end_function(decl);
1692 if (!(decl->ctype.modifiers & MOD_INLINE))
1693 add_symbol(list, decl);
1694 check_declaration(decl);
1695 function_symbol_list = old_symbol_list;
1696 if (function_computed_goto_list) {
1697 if (!function_computed_target_list)
1698 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
1699 else {
1700 struct statement *stmt;
1701 FOR_EACH_PTR(function_computed_goto_list, stmt) {
1702 stmt->target_list = function_computed_target_list;
1703 } END_FOR_EACH_PTR(stmt);
1706 return expect(token, '}', "at end of function");
1709 static void promote_k_r_types(struct symbol *arg)
1711 struct symbol *base = arg->ctype.base_type;
1712 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
1713 arg->ctype.base_type = &int_ctype;
1717 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
1719 struct symbol_list *real_args = fn->ctype.base_type->arguments;
1720 struct symbol *arg;
1722 FOR_EACH_PTR(real_args, arg) {
1723 struct symbol *type;
1725 /* This is quadratic in the number of arguments. We _really_ don't care */
1726 FOR_EACH_PTR(argtypes, type) {
1727 if (type->ident == arg->ident)
1728 goto match;
1729 } END_FOR_EACH_PTR(type);
1730 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
1731 continue;
1732 match:
1733 type->used = 1;
1734 /* "char" and "short" promote to "int" */
1735 promote_k_r_types(type);
1737 arg->ctype = type->ctype;
1738 } END_FOR_EACH_PTR(arg);
1740 FOR_EACH_PTR(argtypes, arg) {
1741 if (!arg->used)
1742 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
1743 } END_FOR_EACH_PTR(arg);
1747 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
1748 struct symbol_list **list)
1750 struct symbol_list *args = NULL;
1752 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
1753 do {
1754 token = declaration_list(token, &args);
1755 if (!match_op(token, ';')) {
1756 sparse_error(token->pos, "expected ';' at end of parameter declaration");
1757 break;
1759 token = token->next;
1760 } while (lookup_type(token));
1762 apply_k_r_types(args, decl);
1764 if (!match_op(token, '{')) {
1765 sparse_error(token->pos, "expected function body");
1766 return token;
1768 return parse_function_body(token, decl, list);
1772 struct token *external_declaration(struct token *token, struct symbol_list **list)
1774 struct ident *ident = NULL;
1775 struct symbol *decl;
1776 struct ctype ctype = { 0, };
1777 struct symbol *base_type;
1778 int is_typedef;
1780 /* Top-level inline asm? */
1781 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1782 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
1783 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
1784 struct statement *stmt;
1786 anon->ctype.base_type = fn;
1787 stmt = alloc_statement(token->pos, STMT_NONE);
1788 fn->stmt = stmt;
1790 token = parse_asm(token->next, stmt);
1792 add_symbol(list, anon);
1793 return token;
1796 /* Parse declaration-specifiers, if any */
1797 token = declaration_specifiers(token, &ctype, 0);
1798 decl = alloc_symbol(token->pos, SYM_NODE);
1799 decl->ctype = ctype;
1800 token = declarator(token, decl, &ident);
1801 apply_modifiers(token->pos, &decl->ctype);
1803 /* Just a type declaration? */
1804 if (!ident)
1805 return expect(token, ';', "end of type declaration");
1807 /* type define declaration? */
1808 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
1810 /* Typedef's don't have meaningful storage */
1811 if (is_typedef) {
1812 ctype.modifiers &= ~MOD_STORAGE;
1813 decl->ctype.modifiers &= ~MOD_STORAGE;
1814 decl->ctype.modifiers |= MOD_USERTYPE;
1817 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1819 base_type = decl->ctype.base_type;
1821 if (is_typedef) {
1822 if (base_type && !base_type->ident)
1823 base_type->ident = ident;
1824 } else if (base_type && base_type->type == SYM_FN) {
1825 /* K&R argument declaration? */
1826 if (lookup_type(token))
1827 return parse_k_r_arguments(token, decl, list);
1828 if (match_op(token, '{'))
1829 return parse_function_body(token, decl, list);
1831 if (!(decl->ctype.modifiers & MOD_STATIC))
1832 decl->ctype.modifiers |= MOD_EXTERN;
1833 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
1834 sparse_error(token->pos, "void declaration");
1837 for (;;) {
1838 if (!is_typedef && match_op(token, '=')) {
1839 if (decl->ctype.modifiers & MOD_EXTERN) {
1840 warning(decl->pos, "symbol with external linkage has initializer");
1841 decl->ctype.modifiers &= ~MOD_EXTERN;
1843 token = initializer(&decl->initializer, token->next);
1845 if (!is_typedef) {
1846 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
1847 add_symbol(list, decl);
1848 fn_local_symbol(decl);
1851 check_declaration(decl);
1853 if (!match_op(token, ','))
1854 break;
1856 token = token->next;
1857 ident = NULL;
1858 decl = alloc_symbol(token->pos, SYM_NODE);
1859 decl->ctype = ctype;
1860 token = declaration_specifiers(token, &decl->ctype, 1);
1861 token = declarator(token, decl, &ident);
1862 apply_modifiers(token->pos, &decl->ctype);
1863 if (!ident) {
1864 sparse_error(token->pos, "expected identifier name in type definition");
1865 return token;
1868 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1870 /* Function declarations are automatically extern unless specifically static */
1871 base_type = decl->ctype.base_type;
1872 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1873 if (!(decl->ctype.modifiers & MOD_STATIC))
1874 decl->ctype.modifiers |= MOD_EXTERN;
1877 return expect(token, ';', "at end of declaration");