Make local declarations be statements of their own
[smatch.git] / parse.c
blobd871afdcd0b325be176feda9a2343504e7edc5de
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 int input = get_expression_value(expr->left);
438 int output = get_expression_value(expr->right);
439 ctype->in_context = input;
440 ctype->out_context = output;
441 return NULL;
443 return "expected context input/output values";
445 if (attribute == &mode_ident ||
446 attribute == &__mode___ident) {
447 if (expr && expr->type == EXPR_SYMBOL) {
448 struct ident *ident = expr->symbol_name;
451 * Match against __QI__/__HI__/__SI__/__DI__
453 * FIXME! This is broken - we don't actually get
454 * the type information updated properly at this
455 * stage for some reason.
457 if (ident == &__QI___ident ||
458 ident == &QI_ident) {
459 ctype->modifiers |= MOD_CHAR;
460 return NULL;
462 if (ident == &__HI___ident ||
463 ident == &HI_ident) {
464 ctype->modifiers |= MOD_SHORT;
465 return NULL;
467 if (ident == &__SI___ident ||
468 ident == &SI_ident) {
469 /* Nothing? */
470 return NULL;
472 if (ident == &__DI___ident ||
473 ident == &DI_ident) {
474 ctype->modifiers |= MOD_LONGLONG;
475 return NULL;
477 if (ident == &__word___ident ||
478 ident == &word_ident) {
479 ctype->modifiers |= MOD_LONG;
480 return NULL;
482 return "unknown mode attribute";
484 return "expected attribute mode symbol";
487 /* Throw away for now.. */
488 if (attribute == &__transparent_union___ident) {
489 if (Wtransparent_union)
490 return "ignoring attribute __transparent_union__";
491 return NULL;
493 if (attribute == &nothrow_ident ||
494 attribute == &__nothrow_ident ||
495 attribute == &__nothrow___ident)
496 return NULL;
497 if (attribute == &__malloc___ident)
498 return NULL;
499 if (attribute == &nonnull_ident ||
500 attribute == &__nonnull_ident ||
501 attribute == &__nonnull___ident)
502 return NULL;
503 if (attribute == &format_ident ||
504 attribute == &__format___ident ||
505 attribute == &__format_arg___ident)
506 return NULL;
507 if (attribute == &section_ident ||
508 attribute == &__section___ident)
509 return NULL;
510 if (attribute == &unused_ident ||
511 attribute == &__unused___ident)
512 return NULL;
513 if (attribute == &const_ident ||
514 attribute == &__const_ident ||
515 attribute == &__const___ident)
516 return NULL;
517 if (attribute == &noreturn_ident ||
518 attribute == &__noreturn___ident)
519 return NULL;
520 if (attribute == &regparm_ident)
521 return NULL;
522 if (attribute == &weak_ident ||
523 attribute == &__weak___ident)
524 return NULL;
525 if (attribute == &alias_ident)
526 return NULL;
527 if (attribute == &pure_ident ||
528 attribute == &__pure___ident)
529 return NULL;
530 if (attribute == &always_inline_ident)
531 return NULL;
532 if (attribute == &syscall_linkage_ident)
533 return NULL;
534 if (attribute == &visibility_ident)
535 return NULL;
536 if (attribute == &deprecated_ident ||
537 attribute == &__deprecated___ident)
538 return NULL;
539 if (attribute == &noinline_ident)
540 return NULL;
541 if (attribute == &__used___ident)
542 return NULL;
543 if (attribute == &warn_unused_result_ident ||
544 attribute == &__warn_unused_result___ident)
545 return NULL;
546 if (attribute == &model_ident ||
547 attribute == &__model___ident)
548 return NULL;
550 return "unknown attribute";
553 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
555 ctype->modifiers = 0;
556 token = expect(token, '(', "after attribute");
557 token = expect(token, '(', "after attribute");
559 for (;;) {
560 const char *error_str;
561 struct ident *attribute_name;
562 struct expression *attribute_expr;
564 if (eof_token(token))
565 break;
566 if (match_op(token, ';'))
567 break;
568 if (token_type(token) != TOKEN_IDENT)
569 break;
570 attribute_name = token->ident;
571 token = token->next;
572 attribute_expr = NULL;
573 if (match_op(token, '('))
574 token = parens_expression(token, &attribute_expr, "in attribute");
575 error_str = handle_attribute(ctype, attribute_name, attribute_expr);
576 if (error_str)
577 sparse_error(token->pos, "attribute '%s': %s", show_ident(attribute_name), error_str);
578 if (!match_op(token, ','))
579 break;
580 token = token->next;
583 token = expect(token, ')', "after attribute");
584 token = expect(token, ')', "after attribute");
585 return token;
588 struct symbol * ctype_integer(unsigned long spec)
590 static struct symbol *const integer_ctypes[][3] = {
591 { &llong_ctype, &sllong_ctype, &ullong_ctype },
592 { &long_ctype, &slong_ctype, &ulong_ctype },
593 { &short_ctype, &sshort_ctype, &ushort_ctype },
594 { &char_ctype, &schar_ctype, &uchar_ctype },
595 { &int_ctype, &sint_ctype, &uint_ctype },
597 struct symbol *const (*ctype)[3];
598 int sub;
600 ctype = integer_ctypes;
601 if (!(spec & MOD_LONGLONG)) {
602 ctype++;
603 if (!(spec & MOD_LONG)) {
604 ctype++;
605 if (!(spec & MOD_SHORT)) {
606 ctype++;
607 if (!(spec & MOD_CHAR))
608 ctype++;
613 sub = ((spec & MOD_UNSIGNED)
615 : ((spec & MOD_EXPLICITLY_SIGNED)
617 : 0));
619 return ctype[0][sub];
622 struct symbol * ctype_fp(unsigned long spec)
624 if (spec & MOD_LONGLONG)
625 return &ldouble_ctype;
626 if (spec & MOD_LONG)
627 return &double_ctype;
628 return &float_ctype;
631 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
633 unsigned long mod = thistype->modifiers;
635 if (mod) {
636 unsigned long old = ctype->modifiers;
637 unsigned long extra = 0, dup, conflict;
639 if (mod & old & MOD_LONG) {
640 extra = MOD_LONGLONG | MOD_LONG;
641 mod &= ~MOD_LONG;
642 old &= ~MOD_LONG;
644 dup = (mod & old) | (extra & old) | (extra & mod);
645 if (dup)
646 sparse_error(pos, "Just how %sdo you want this type to be?",
647 modifier_string(dup));
649 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
650 if (conflict)
651 sparse_error(pos, "You cannot have both long and short modifiers.");
653 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
654 if (conflict)
655 sparse_error(pos, "You cannot have both signed and unsigned modifiers.");
657 // Only one storage modifier allowed, except that "inline" doesn't count.
658 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
659 conflict &= (conflict - 1);
660 if (conflict)
661 sparse_error(pos, "multiple storage classes");
663 ctype->modifiers = old | mod | extra;
666 /* Context mask and value */
667 ctype->in_context += thistype->in_context;
668 ctype->out_context += thistype->out_context;
670 /* Alignment */
671 if (thistype->alignment & (thistype->alignment-1)) {
672 warning(pos, "I don't like non-power-of-2 alignments");
673 thistype->alignment = 0;
675 if (thistype->alignment > ctype->alignment)
676 ctype->alignment = thistype->alignment;
678 /* Address space */
679 if (thistype->as)
680 ctype->as = thistype->as;
683 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
685 unsigned long banned, wrong;
686 unsigned long this_mod = s->ctype.modifiers;
687 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
688 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
690 if (this_mod & (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF))
691 banned = BANNED_SIZE | BANNED_SIGN;
692 else if (this_mod & MOD_SPECIALBITS)
693 banned = 0;
694 else if (s->ctype.base_type == &fp_type)
695 banned = BANNED_SIGN;
696 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
697 banned = 0;
698 else {
699 // label_type
700 // void_type
701 // bad_type
702 // vector_type <-- whatever that is
703 banned = BANNED_SIZE | BANNED_SIGN;
706 wrong = mod & banned;
707 if (wrong)
708 sparse_error(*pos, "modifier %sis invalid in this context",
709 modifier_string (wrong));
713 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
715 struct token *token;
717 while ( (token = next) != NULL ) {
718 struct ctype thistype;
719 struct ident *ident;
720 struct symbol *s, *type;
721 unsigned long mod;
723 next = token->next;
724 if (token_type(token) != TOKEN_IDENT)
725 break;
726 ident = token->ident;
728 s = lookup_symbol(ident, NS_TYPEDEF);
729 if (!s)
730 break;
731 thistype = s->ctype;
732 mod = thistype.modifiers;
733 if (qual && (mod & ~(MOD_ATTRIBUTE | MOD_CONST | MOD_VOLATILE)))
734 break;
735 if (mod & MOD_SPECIALBITS) {
736 if (mod & MOD_STRUCTOF)
737 next = struct_or_union_specifier(SYM_STRUCT, next, &thistype);
738 else if (mod & MOD_UNIONOF)
739 next = struct_or_union_specifier(SYM_UNION, next, &thistype);
740 else if (mod & MOD_ENUMOF)
741 next = enum_specifier(next, &thistype);
742 else if (mod & MOD_ATTRIBUTE)
743 next = attribute_specifier(next, &thistype);
744 else if (mod & MOD_TYPEOF)
745 next = typeof_specifier(next, &thistype);
746 mod = thistype.modifiers;
748 type = thistype.base_type;
749 if (type) {
750 if (qual)
751 break;
752 if (ctype->base_type)
753 break;
754 /* User types only mix with qualifiers */
755 if (mod & MOD_USERTYPE) {
756 if (ctype->modifiers & MOD_SPECIFIER)
757 break;
759 ctype->base_type = type;
762 check_modifiers(&token->pos, s, ctype->modifiers);
763 apply_ctype(token->pos, &thistype, ctype);
766 /* Turn the "virtual types" into real types with real sizes etc */
767 if (!ctype->base_type) {
768 struct symbol *base = &incomplete_ctype;
771 * If we have modifiers, we'll default to an integer
772 * type, and "ctype_integer()" will turn this into
773 * a specific one.
775 if (ctype->modifiers & MOD_SPECIFIER)
776 base = &int_type;
777 ctype->base_type = base;
779 if (ctype->base_type == &int_type) {
780 ctype->base_type = ctype_integer(ctype->modifiers);
781 ctype->modifiers &= ~MOD_SPECIFIER;
782 } else if (ctype->base_type == &fp_type) {
783 ctype->base_type = ctype_fp(ctype->modifiers);
784 ctype->modifiers &= ~MOD_SPECIFIER;
786 if (ctype->modifiers & MOD_BITWISE) {
787 struct symbol *type;
788 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
789 if (!is_int_type(ctype->base_type)) {
790 sparse_error(token->pos, "invalid modifier");
791 return token;
793 type = alloc_symbol(token->pos, SYM_BASETYPE);
794 *type = *ctype->base_type;
795 type->ctype.base_type = ctype->base_type;
796 type->type = SYM_RESTRICT;
797 type->ctype.modifiers &= ~MOD_SPECIFIER;
798 ctype->base_type = type;
800 return token;
803 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
805 struct expression *expr = NULL;
807 token = parse_expression(token, &expr);
808 sym->array_size = expr;
809 return token;
812 static struct token *parameter_type_list(struct token *, struct symbol *, struct ident **p);
813 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
815 static struct token *handle_attributes(struct token *token, struct ctype *ctype)
817 for (;;) {
818 if (token_type(token) != TOKEN_IDENT)
819 break;
820 if (match_idents(token, &__attribute___ident, &__attribute_ident, NULL)) {
821 struct ctype thistype = { 0, };
822 token = attribute_specifier(token->next, &thistype);
823 apply_ctype(token->pos, &thistype, ctype);
824 continue;
826 if (match_idents(token, &asm_ident, &__asm_ident, &__asm___ident)) {
827 struct expression *expr;
828 token = expect(token->next, '(', "after asm");
829 token = parse_expression(token->next, &expr);
830 token = expect(token, ')', "after asm");
831 continue;
833 break;
835 return token;
838 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
840 struct ctype *ctype = &decl->ctype;
842 if (p && token_type(token) == TOKEN_IDENT) {
843 *p = token->ident;
844 token = token->next;
847 for (;;) {
848 token = handle_attributes(token, ctype);
850 if (token_type(token) != TOKEN_SPECIAL)
851 return token;
854 * This can be either a parameter list or a grouping.
855 * For the direct (non-abstract) case, we know if must be
856 * a parameter list if we already saw the identifier.
857 * For the abstract case, we know if must be a parameter
858 * list if it is empty or starts with a type.
860 if (token->special == '(') {
861 struct symbol *sym;
862 struct token *next = token->next;
863 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
865 if (!fn) {
866 struct symbol *base_type = ctype->base_type;
867 token = declarator(next, decl, p);
868 token = expect(token, ')', "in nested declarator");
869 while (ctype->base_type != base_type)
870 ctype = &ctype->base_type->ctype;
871 p = NULL;
872 continue;
875 sym = indirect(token->pos, ctype, SYM_FN);
876 token = parameter_type_list(next, sym, p);
877 token = expect(token, ')', "in function declarator");
878 continue;
880 if (token->special == '[') {
881 struct symbol *array = indirect(token->pos, ctype, SYM_ARRAY);
882 token = abstract_array_declarator(token->next, array);
883 token = expect(token, ']', "in abstract_array_declarator");
884 ctype = &array->ctype;
885 continue;
887 break;
889 return token;
892 static struct token *pointer(struct token *token, struct ctype *ctype)
894 unsigned long modifiers;
895 struct symbol *base_type;
897 modifiers = ctype->modifiers & ~(MOD_TYPEDEF | MOD_ATTRIBUTE);
898 base_type = ctype->base_type;
899 ctype->modifiers = modifiers;
901 while (match_op(token,'*')) {
902 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
903 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
904 ptr->ctype.as = ctype->as;
905 ptr->ctype.in_context += ctype->in_context;
906 ptr->ctype.out_context += ctype->out_context;
907 ptr->ctype.base_type = base_type;
909 base_type = ptr;
910 ctype->modifiers = modifiers & MOD_STORAGE;
911 ctype->base_type = base_type;
912 ctype->as = 0;
913 ctype->in_context = 0;
914 ctype->out_context = 0;
916 token = declaration_specifiers(token->next, ctype, 1);
917 modifiers = ctype->modifiers;
919 return token;
922 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
924 token = pointer(token, &sym->ctype);
925 return direct_declarator(token, sym, p);
928 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
930 struct ctype *ctype = &decl->ctype;
931 struct expression *expr;
932 struct symbol *bitfield;
933 long long width;
935 if (!is_int_type(ctype->base_type)) {
936 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
937 show_typename(ctype->base_type));
938 // Parse this to recover gracefully.
939 return conditional_expression(token->next, &expr);
942 bitfield = indirect(token->pos, ctype, SYM_BITFIELD);
943 token = conditional_expression(token->next, &expr);
944 width = get_expression_value(expr);
945 bitfield->bit_size = width;
947 if (width < 0 || width > INT_MAX) {
948 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
949 width = -1;
950 } else if (decl->ident && width == 0) {
951 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
952 show_ident(decl->ident));
953 width = -1;
954 } else if (decl->ident) {
955 struct symbol *base_type = bitfield->ctype.base_type;
956 int is_signed = !(base_type->ctype.modifiers & MOD_UNSIGNED);
957 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
958 // Valid values are either {-1;0} or {0}, depending on integer
959 // representation. The latter makes for very efficient code...
960 sparse_error(token->pos, "dubious one-bit signed bitfield");
962 if (Wdefault_bitfield_sign &&
963 base_type->type != SYM_ENUM &&
964 !(base_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
965 is_signed) {
966 // The sign of bitfields is unspecified by default.
967 sparse_error(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
970 bitfield->bit_size = width;
971 return token;
974 static struct token *declaration_list(struct token *token, struct symbol_list **list)
976 struct ctype ctype = {0, };
978 token = declaration_specifiers(token, &ctype, 0);
979 for (;;) {
980 struct ident *ident = NULL;
981 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
982 decl->ctype = ctype;
983 token = declarator(token, decl, &ident);
984 decl->ident = ident;
985 if (match_op(token, ':')) {
986 token = handle_bitfield(token, decl);
987 token = handle_attributes(token, &decl->ctype);
989 add_symbol(list, decl);
990 if (!match_op(token, ','))
991 break;
992 token = token->next;
994 return token;
997 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
999 while (!match_op(token, '}')) {
1000 token = declaration_list(token, list);
1001 if (!match_op(token, ';')) {
1002 sparse_error(token->pos, "expected ; at end of declaration");
1003 break;
1005 token = token->next;
1007 return token;
1010 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
1012 struct ident *ident = NULL;
1013 struct symbol *sym;
1014 struct ctype ctype = { 0, };
1016 token = declaration_specifiers(token, &ctype, 0);
1017 sym = alloc_symbol(token->pos, SYM_NODE);
1018 sym->ctype = ctype;
1019 *tree = sym;
1020 token = declarator(token, sym, &ident);
1021 sym->ident = ident;
1022 return token;
1025 struct token *typename(struct token *token, struct symbol **p)
1027 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1028 *p = sym;
1029 token = declaration_specifiers(token, &sym->ctype, 0);
1030 return declarator(token, sym, NULL);
1033 static struct token *expression_statement(struct token *token, struct expression **tree)
1035 token = parse_expression(token, tree);
1036 return expect(token, ';', "at end of statement");
1039 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1040 struct expression_list **inout)
1042 struct expression *expr;
1044 /* Allow empty operands */
1045 if (match_op(token->next, ':') || match_op(token->next, ')'))
1046 return token->next;
1047 do {
1048 struct ident *ident = NULL;
1049 if (match_op(token->next, '[') &&
1050 token_type(token->next->next) == TOKEN_IDENT &&
1051 match_op(token->next->next->next, ']')) {
1052 ident = token->next->next->ident;
1053 token = token->next->next->next;
1055 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1056 token = primary_expression(token->next, &expr);
1057 add_expression(inout, expr);
1058 token = parens_expression(token, &expr, "in asm parameter");
1059 add_expression(inout, expr);
1060 } while (match_op(token, ','));
1061 return token;
1064 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1065 struct expression_list **clobbers)
1067 struct expression *expr;
1069 do {
1070 token = primary_expression(token->next, &expr);
1071 add_expression(clobbers, expr);
1072 } while (match_op(token, ','));
1073 return token;
1076 static struct token *parse_asm(struct token *token, struct statement *stmt)
1078 stmt->type = STMT_ASM;
1079 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1080 token = token->next;
1082 token = expect(token, '(', "after asm");
1083 token = parse_expression(token, &stmt->asm_string);
1084 if (match_op(token, ':'))
1085 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1086 if (match_op(token, ':'))
1087 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1088 if (match_op(token, ':'))
1089 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1090 token = expect(token, ')', "after asm");
1091 return expect(token, ';', "at end of asm-statement");
1094 /* Make a statement out of an expression */
1095 static struct statement *make_statement(struct expression *expr)
1097 struct statement *stmt;
1099 if (!expr)
1100 return NULL;
1101 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1102 stmt->expression = expr;
1103 return stmt;
1107 * All iterators have two symbols associated with them:
1108 * the "continue" and "break" symbols, which are targets
1109 * for continue and break statements respectively.
1111 * They are in a special name-space, but they follow
1112 * all the normal visibility rules, so nested iterators
1113 * automatically work right.
1115 static void start_iterator(struct statement *stmt)
1117 struct symbol *cont, *brk;
1119 start_symbol_scope();
1120 cont = alloc_symbol(stmt->pos, SYM_NODE);
1121 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1122 brk = alloc_symbol(stmt->pos, SYM_NODE);
1123 bind_symbol(brk, &break_ident, NS_ITERATOR);
1125 stmt->type = STMT_ITERATOR;
1126 stmt->iterator_break = brk;
1127 stmt->iterator_continue = cont;
1128 fn_local_symbol(brk);
1129 fn_local_symbol(cont);
1132 static void end_iterator(struct statement *stmt)
1134 end_symbol_scope();
1137 static struct statement *start_function(struct symbol *sym)
1139 struct symbol *ret;
1140 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1142 start_function_scope();
1143 ret = alloc_symbol(sym->pos, SYM_NODE);
1144 ret->ctype = sym->ctype.base_type->ctype;
1145 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1146 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1147 bind_symbol(ret, &return_ident, NS_ITERATOR);
1148 stmt->ret = ret;
1149 fn_local_symbol(ret);
1151 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1152 current_fn = sym;
1154 return stmt;
1157 static void end_function(struct symbol *sym)
1159 current_fn = NULL;
1160 end_function_scope();
1164 * A "switch()" statement, like an iterator, has a
1165 * the "break" symbol associated with it. It works
1166 * exactly like the iterator break - it's the target
1167 * for any break-statements in scope, and means that
1168 * "break" handling doesn't even need to know whether
1169 * it's breaking out of an iterator or a switch.
1171 * In addition, the "case" symbol is a marker for the
1172 * case/default statements to find the switch statement
1173 * that they are associated with.
1175 static void start_switch(struct statement *stmt)
1177 struct symbol *brk, *switch_case;
1179 start_symbol_scope();
1180 brk = alloc_symbol(stmt->pos, SYM_NODE);
1181 bind_symbol(brk, &break_ident, NS_ITERATOR);
1183 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1184 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1185 switch_case->stmt = stmt;
1187 stmt->type = STMT_SWITCH;
1188 stmt->switch_break = brk;
1189 stmt->switch_case = switch_case;
1191 fn_local_symbol(brk);
1192 fn_local_symbol(switch_case);
1195 static void end_switch(struct statement *stmt)
1197 if (!stmt->switch_case->symbol_list)
1198 warning(stmt->pos, "switch with no cases");
1199 end_symbol_scope();
1202 static void add_case_statement(struct statement *stmt)
1204 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1205 struct symbol *sym;
1207 if (!target) {
1208 sparse_error(stmt->pos, "not in switch scope");
1209 stmt->type = STMT_NONE;
1210 return;
1212 sym = alloc_symbol(stmt->pos, SYM_NODE);
1213 add_symbol(&target->symbol_list, sym);
1214 sym->stmt = stmt;
1215 stmt->case_label = sym;
1216 fn_local_symbol(sym);
1219 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1221 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1223 if (!target)
1224 error_die(token->pos, "internal error: return without a function target");
1225 stmt->type = STMT_RETURN;
1226 stmt->ret_target = target;
1227 return expression_statement(token->next, &stmt->ret_value);
1230 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1232 struct symbol_list *syms;
1233 struct expression *e1, *e2, *e3;
1234 struct statement *iterator;
1236 start_iterator(stmt);
1237 token = expect(token->next, '(', "after 'for'");
1239 syms = NULL;
1240 e1 = NULL;
1241 /* C99 variable declaration? */
1242 if (lookup_type(token)) {
1243 token = external_declaration(token, &syms);
1244 } else {
1245 token = parse_expression(token, &e1);
1246 token = expect(token, ';', "in 'for'");
1248 token = parse_expression(token, &e2);
1249 token = expect(token, ';', "in 'for'");
1250 token = parse_expression(token, &e3);
1251 token = expect(token, ')', "in 'for'");
1252 token = statement(token, &iterator);
1254 stmt->iterator_syms = syms;
1255 stmt->iterator_pre_statement = make_statement(e1);
1256 stmt->iterator_pre_condition = e2;
1257 stmt->iterator_post_statement = make_statement(e3);
1258 stmt->iterator_post_condition = NULL;
1259 stmt->iterator_statement = iterator;
1260 end_iterator(stmt);
1262 return token;
1265 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
1267 struct expression *expr;
1268 struct statement *iterator;
1270 start_iterator(stmt);
1271 token = parens_expression(token->next, &expr, "after 'while'");
1272 token = statement(token, &iterator);
1274 stmt->iterator_pre_condition = expr;
1275 stmt->iterator_post_condition = NULL;
1276 stmt->iterator_statement = iterator;
1277 end_iterator(stmt);
1279 return token;
1282 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
1284 struct expression *expr;
1285 struct statement *iterator;
1287 start_iterator(stmt);
1288 token = statement(token->next, &iterator);
1289 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1290 token = token->next;
1291 else
1292 sparse_error(token->pos, "expected 'while' after 'do'");
1293 token = parens_expression(token, &expr, "after 'do-while'");
1295 stmt->iterator_post_condition = expr;
1296 stmt->iterator_statement = iterator;
1297 end_iterator(stmt);
1299 return expect(token, ';', "after statement");
1302 static struct token *statement(struct token *token, struct statement **tree)
1304 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1306 *tree = stmt;
1307 if (token_type(token) == TOKEN_IDENT) {
1308 if (token->ident == &if_ident) {
1309 stmt->type = STMT_IF;
1310 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1311 token = statement(token, &stmt->if_true);
1312 if (token_type(token) != TOKEN_IDENT)
1313 return token;
1314 if (token->ident != &else_ident)
1315 return token;
1316 return statement(token->next, &stmt->if_false);
1319 if (token->ident == &return_ident)
1320 return parse_return_statement(token, stmt);
1322 if (token->ident == &break_ident || token->ident == &continue_ident) {
1323 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1324 stmt->type = STMT_GOTO;
1325 stmt->goto_label = target;
1326 if (!target)
1327 sparse_error(stmt->pos, "break/continue not in iterator scope");
1328 return expect(token->next, ';', "at end of statement");
1330 if (token->ident == &default_ident) {
1331 token = token->next;
1332 goto default_statement;
1334 if (token->ident == &case_ident) {
1335 token = parse_expression(token->next, &stmt->case_expression);
1336 if (match_op(token, SPECIAL_ELLIPSIS))
1337 token = parse_expression(token->next, &stmt->case_to);
1338 default_statement:
1339 stmt->type = STMT_CASE;
1340 token = expect(token, ':', "after default/case");
1341 add_case_statement(stmt);
1342 return statement(token, &stmt->case_statement);
1344 if (token->ident == &switch_ident) {
1345 stmt->type = STMT_SWITCH;
1346 start_switch(stmt);
1347 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1348 token = statement(token, &stmt->switch_statement);
1349 end_switch(stmt);
1350 return token;
1352 if (token->ident == &for_ident)
1353 return parse_for_statement(token, stmt);
1355 if (token->ident == &while_ident)
1356 return parse_while_statement(token, stmt);
1358 if (token->ident == &do_ident)
1359 return parse_do_statement(token, stmt);
1361 if (token->ident == &goto_ident) {
1362 stmt->type = STMT_GOTO;
1363 token = token->next;
1364 if (match_op(token, '*')) {
1365 token = parse_expression(token->next, &stmt->goto_expression);
1366 add_statement(&function_computed_goto_list, stmt);
1367 } else if (token_type(token) == TOKEN_IDENT) {
1368 stmt->goto_label = label_symbol(token);
1369 token = token->next;
1370 } else {
1371 sparse_error(token->pos, "Expected identifier or goto expression");
1373 return expect(token, ';', "at end of statement");
1375 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1376 return parse_asm(token->next, stmt);
1378 if (token->ident == &__context___ident) {
1379 stmt->type = STMT_CONTEXT;
1380 token = parse_expression(token->next, &stmt->expression);
1381 return expect(token, ';', "at end of statement");
1383 if (token->ident == &__range___ident) {
1384 stmt->type = STMT_RANGE;
1385 token = assignment_expression(token->next, &stmt->range_expression);
1386 token = expect(token, ',', "after range expression");
1387 token = assignment_expression(token, &stmt->range_low);
1388 token = expect(token, ',', "after low range");
1389 token = assignment_expression(token, &stmt->range_high);
1390 return expect(token, ';', "after range statement");
1392 if (match_op(token->next, ':')) {
1393 stmt->type = STMT_LABEL;
1394 stmt->label_identifier = label_symbol(token);
1395 return statement(token->next->next, &stmt->label_statement);
1399 if (match_op(token, '{')) {
1400 stmt->type = STMT_COMPOUND;
1401 start_symbol_scope();
1402 token = compound_statement(token->next, stmt);
1403 end_symbol_scope();
1405 return expect(token, '}', "at end of compound statement");
1408 stmt->type = STMT_EXPRESSION;
1409 return expression_statement(token, &stmt->expression);
1412 static struct token * statement_list(struct token *token, struct statement_list **list)
1414 int seen_statement = 0;
1415 for (;;) {
1416 struct statement * stmt;
1417 if (eof_token(token))
1418 break;
1419 if (match_op(token, '}'))
1420 break;
1421 if (lookup_type(token)) {
1422 if (seen_statement) {
1423 warning(token->pos, "mixing declarations and code");
1424 seen_statement = 0;
1426 stmt = alloc_statement(token->pos, STMT_DECLARATION);
1427 token = external_declaration(token, &stmt->declaration);
1428 } else {
1429 seen_statement = warn_on_mixed;
1430 token = statement(token, &stmt);
1432 add_statement(list, stmt);
1434 return token;
1437 static struct token *parameter_type_list(struct token *token, struct symbol *fn, struct ident **p)
1439 struct symbol_list **list = &fn->arguments;
1441 if (match_op(token, ')')) {
1442 // No warning for "void oink ();"
1443 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1444 if (p && !match_op(token->next, ';'))
1445 warning(token->pos, "non-ANSI function declaration of function '%s'", show_ident(*p));
1446 return token;
1449 for (;;) {
1450 struct symbol *sym;
1452 if (match_op(token, SPECIAL_ELLIPSIS)) {
1453 if (!*list)
1454 warning(token->pos, "variadic functions must have one named argument");
1455 fn->variadic = 1;
1456 token = token->next;
1457 break;
1460 sym = alloc_symbol(token->pos, SYM_NODE);
1461 token = parameter_declaration(token, &sym);
1462 if (sym->ctype.base_type == &void_ctype) {
1463 /* Special case: (void) */
1464 if (!*list && !sym->ident)
1465 break;
1466 warning(token->pos, "void parameter");
1468 add_symbol(list, sym);
1469 if (!match_op(token, ','))
1470 break;
1471 token = token->next;
1474 return token;
1477 struct token *compound_statement(struct token *token, struct statement *stmt)
1479 token = statement_list(token, &stmt->stmts);
1480 return token;
1483 static struct expression *identifier_expression(struct token *token)
1485 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1486 expr->expr_ident = token->ident;
1487 return expr;
1490 static struct expression *index_expression(struct expression *from, struct expression *to)
1492 int idx_from, idx_to;
1493 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1495 idx_from = get_expression_value(from);
1496 idx_to = idx_from;
1497 if (to) {
1498 idx_to = get_expression_value(to);
1499 if (idx_to < idx_from || idx_from < 0)
1500 warning(from->pos, "nonsense array initializer index range");
1502 expr->idx_from = idx_from;
1503 expr->idx_to = idx_to;
1504 return expr;
1507 static struct token *single_initializer(struct expression **ep, struct token *token)
1509 int expect_equal = 0;
1510 struct token *next = token->next;
1511 struct expression **tail = ep;
1512 int nested;
1514 *ep = NULL;
1516 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1517 struct expression *expr = identifier_expression(token);
1518 warning(token->pos, "obsolete struct initializer, use C99 syntax");
1519 token = initializer(&expr->ident_expression, next->next);
1520 if (expr->ident_expression)
1521 *ep = expr;
1522 return token;
1525 for (tail = ep, nested = 0; ; nested++, next = token->next) {
1526 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
1527 struct expression *expr = identifier_expression(next);
1528 *tail = expr;
1529 tail = &expr->ident_expression;
1530 expect_equal = 1;
1531 token = next->next;
1532 } else if (match_op(token, '[')) {
1533 struct expression *from = NULL, *to = NULL, *expr;
1534 token = constant_expression(token->next, &from);
1535 if (!from) {
1536 sparse_error(token->pos, "Expected constant expression");
1537 break;
1539 if (match_op(token, SPECIAL_ELLIPSIS))
1540 token = constant_expression(token->next, &to);
1541 expr = index_expression(from, to);
1542 *tail = expr;
1543 tail = &expr->idx_expression;
1544 token = expect(token, ']', "at end of initializer index");
1545 if (nested)
1546 expect_equal = 1;
1547 } else {
1548 break;
1551 if (nested && !expect_equal) {
1552 if (!match_op(token, '='))
1553 warning(token->pos, "obsolete array initializer, use C99 syntax");
1554 else
1555 expect_equal = 1;
1557 if (expect_equal)
1558 token = expect(token, '=', "at end of initializer index");
1560 token = initializer(tail, token);
1561 if (!*tail)
1562 *ep = NULL;
1563 return token;
1566 static struct token *initializer_list(struct expression_list **list, struct token *token)
1568 struct expression *expr;
1570 for (;;) {
1571 token = single_initializer(&expr, token);
1572 if (!expr)
1573 break;
1574 add_expression(list, expr);
1575 if (!match_op(token, ','))
1576 break;
1577 token = token->next;
1579 return token;
1582 struct token *initializer(struct expression **tree, struct token *token)
1584 if (match_op(token, '{')) {
1585 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1586 *tree = expr;
1587 token = initializer_list(&expr->expr_list, token->next);
1588 return expect(token, '}', "at end of initializer");
1590 return assignment_expression(token, tree);
1593 static void declare_argument(struct symbol *sym, struct symbol *fn)
1595 if (!sym->ident) {
1596 sparse_error(sym->pos, "no identifier for function argument");
1597 return;
1599 bind_symbol(sym, sym->ident, NS_SYMBOL);
1602 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1603 struct symbol_list **list)
1605 struct symbol_list **old_symbol_list;
1606 struct symbol *base_type = decl->ctype.base_type;
1607 struct statement *stmt, **p;
1608 struct symbol *arg;
1610 old_symbol_list = function_symbol_list;
1611 if (decl->ctype.modifiers & MOD_INLINE) {
1612 function_symbol_list = &decl->inline_symbol_list;
1613 p = &base_type->inline_stmt;
1614 } else {
1615 function_symbol_list = &decl->symbol_list;
1616 p = &base_type->stmt;
1618 function_computed_target_list = NULL;
1619 function_computed_goto_list = NULL;
1621 if (decl->ctype.modifiers & MOD_EXTERN) {
1622 if (!(decl->ctype.modifiers & MOD_INLINE))
1623 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
1625 if (!(decl->ctype.modifiers & MOD_STATIC))
1626 decl->ctype.modifiers |= MOD_EXTERN;
1628 stmt = start_function(decl);
1630 *p = stmt;
1631 FOR_EACH_PTR (base_type->arguments, arg) {
1632 declare_argument(arg, base_type);
1633 } END_FOR_EACH_PTR(arg);
1635 token = compound_statement(token->next, stmt);
1637 end_function(decl);
1638 if (!(decl->ctype.modifiers & MOD_INLINE))
1639 add_symbol(list, decl);
1640 check_declaration(decl);
1641 function_symbol_list = old_symbol_list;
1642 if (function_computed_goto_list) {
1643 if (!function_computed_target_list)
1644 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
1645 else {
1646 struct statement *stmt;
1647 FOR_EACH_PTR(function_computed_goto_list, stmt) {
1648 stmt->target_list = function_computed_target_list;
1649 } END_FOR_EACH_PTR(stmt);
1652 return expect(token, '}', "at end of function");
1655 static void promote_k_r_types(struct symbol *arg)
1657 struct symbol *base = arg->ctype.base_type;
1658 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
1659 arg->ctype.base_type = &int_ctype;
1663 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
1665 struct symbol_list *real_args = fn->ctype.base_type->arguments;
1666 struct symbol *arg;
1668 FOR_EACH_PTR(real_args, arg) {
1669 struct symbol *type;
1671 /* This is quadratic in the number of arguments. We _really_ don't care */
1672 FOR_EACH_PTR(argtypes, type) {
1673 if (type->ident == arg->ident)
1674 goto match;
1675 } END_FOR_EACH_PTR(type);
1676 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
1677 continue;
1678 match:
1679 type->used = 1;
1680 /* "char" and "short" promote to "int" */
1681 promote_k_r_types(type);
1683 arg->ctype = type->ctype;
1684 } END_FOR_EACH_PTR(arg);
1686 FOR_EACH_PTR(argtypes, arg) {
1687 if (!arg->used)
1688 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
1689 } END_FOR_EACH_PTR(arg);
1693 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
1694 struct symbol_list **list)
1696 struct symbol_list *args = NULL;
1698 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
1699 do {
1700 token = declaration_list(token, &args);
1701 if (!match_op(token, ';')) {
1702 sparse_error(token->pos, "expected ';' at end of parameter declaration");
1703 break;
1705 token = token->next;
1706 } while (lookup_type(token));
1708 apply_k_r_types(args, decl);
1710 if (!match_op(token, '{')) {
1711 sparse_error(token->pos, "expected function body");
1712 return token;
1714 return parse_function_body(token, decl, list);
1718 struct token *external_declaration(struct token *token, struct symbol_list **list)
1720 struct ident *ident = NULL;
1721 struct symbol *decl;
1722 struct ctype ctype = { 0, };
1723 struct symbol *base_type;
1724 int is_typedef;
1726 /* Top-level inline asm? */
1727 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1728 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
1729 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
1730 struct statement *stmt;
1732 anon->ctype.base_type = fn;
1733 stmt = alloc_statement(token->pos, STMT_NONE);
1734 fn->stmt = stmt;
1736 token = parse_asm(token->next, stmt);
1738 add_symbol(list, anon);
1739 return token;
1742 /* Parse declaration-specifiers, if any */
1743 token = declaration_specifiers(token, &ctype, 0);
1744 decl = alloc_symbol(token->pos, SYM_NODE);
1745 decl->ctype = ctype;
1746 token = declarator(token, decl, &ident);
1748 /* Just a type declaration? */
1749 if (!ident)
1750 return expect(token, ';', "end of type declaration");
1752 /* type define declaration? */
1753 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
1755 /* Typedef's don't have meaningful storage */
1756 if (is_typedef) {
1757 ctype.modifiers &= ~MOD_STORAGE;
1758 decl->ctype.modifiers &= ~MOD_STORAGE;
1759 decl->ctype.modifiers |= MOD_USERTYPE;
1762 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1764 base_type = decl->ctype.base_type;
1766 if (is_typedef) {
1767 if (base_type && !base_type->ident)
1768 base_type->ident = ident;
1769 } else if (base_type && base_type->type == SYM_FN) {
1770 /* K&R argument declaration? */
1771 if (lookup_type(token))
1772 return parse_k_r_arguments(token, decl, list);
1773 if (match_op(token, '{'))
1774 return parse_function_body(token, decl, list);
1776 if (!(decl->ctype.modifiers & MOD_STATIC))
1777 decl->ctype.modifiers |= MOD_EXTERN;
1778 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
1779 sparse_error(token->pos, "void declaration");
1782 for (;;) {
1783 if (!is_typedef && match_op(token, '=')) {
1784 if (decl->ctype.modifiers & MOD_EXTERN) {
1785 warning(decl->pos, "symbol with external linkage has initializer");
1786 decl->ctype.modifiers &= ~MOD_EXTERN;
1788 token = initializer(&decl->initializer, token->next);
1790 if (!is_typedef) {
1791 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
1792 add_symbol(list, decl);
1793 fn_local_symbol(decl);
1796 check_declaration(decl);
1798 if (!match_op(token, ','))
1799 break;
1801 token = token->next;
1802 ident = NULL;
1803 decl = alloc_symbol(token->pos, SYM_NODE);
1804 decl->ctype = ctype;
1805 token = declaration_specifiers(token, &decl->ctype, 1);
1806 token = declarator(token, decl, &ident);
1807 if (!ident) {
1808 sparse_error(token->pos, "expected identifier name in type definition");
1809 return token;
1812 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1814 /* Function declarations are automatically extern unless specifically static */
1815 base_type = decl->ctype.base_type;
1816 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1817 if (!(decl->ctype.modifiers & MOD_STATIC))
1818 decl->ctype.modifiers |= MOD_EXTERN;
1821 return expect(token, ';', "at end of declaration");