Fix assignment and conditional expression parsing with no left side.
[smatch.git] / parse.c
blob629d638fda2ce959f55b7324055e32f042ac7991
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 *external_declaration(struct token *token, struct symbol_list **list);
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 struct symbol * indirect(struct position pos, struct ctype *ctype, int type)
76 struct symbol *sym = alloc_symbol(pos, type);
78 sym->ctype.base_type = ctype->base_type;
79 sym->ctype.modifiers = ctype->modifiers & ~MOD_STORAGE;
81 ctype->base_type = sym;
82 ctype->modifiers &= MOD_STORAGE;
83 return sym;
86 static struct symbol *lookup_or_create_symbol(enum namespace ns, enum type type, struct token *token)
88 struct symbol *sym = lookup_symbol(token->ident, ns);
89 if (!sym) {
90 sym = alloc_symbol(token->pos, type);
91 bind_symbol(sym, token->ident, ns);
92 if (type == SYM_LABEL)
93 fn_local_symbol(sym);
95 return sym;
99 * NOTE! NS_LABEL is not just a different namespace,
100 * it also ends up using function scope instead of the
101 * regular symbol scope.
103 struct symbol *label_symbol(struct token *token)
105 return lookup_or_create_symbol(NS_LABEL, SYM_LABEL, token);
108 static struct token *struct_union_enum_specifier(enum type type,
109 struct token *token, struct ctype *ctype,
110 struct token *(*parse)(struct token *, struct symbol *))
112 struct symbol *sym;
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 token = token->next;
128 ctype->base_type = sym;
129 if (match_op(token, '{')) {
130 // The following test is actually wrong for empty
131 // structs, but (1) they are not C99, (2) gcc does
132 // the same thing, and (3) it's easier.
133 if (sym->symbol_list)
134 error_die(token->pos, "redefinition of %s", show_typename (sym));
135 token = parse(token->next, sym);
136 token = expect(token, '}', "at end of struct-union-enum-specifier");
138 // Mark the structure as needing re-examination
139 sym->examined = 0;
141 return token;
144 // private struct/union/enum type
145 if (!match_op(token, '{')) {
146 warning(token->pos, "expected declaration");
147 ctype->base_type = &bad_ctype;
148 return token;
151 sym = alloc_symbol(token->pos, type);
152 token = parse(token->next, sym);
153 ctype->base_type = sym;
154 return expect(token, '}', "at end of specifier");
157 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
159 return struct_declaration_list(token, &sym->symbol_list);
162 static struct token *struct_or_union_specifier(enum type type, struct token *token, struct ctype *ctype)
164 return struct_union_enum_specifier(type, token, ctype, parse_struct_declaration);
167 typedef struct {
168 int x;
169 unsigned long long y;
170 } Num;
172 static void upper_boundary(Num *n, Num *v)
174 if (n->x > v->x)
175 return;
176 if (n->x < v->x) {
177 *n = *v;
178 return;
180 if (n->y < v->y)
181 n->y = v->y;
184 static void lower_boundary(Num *n, Num *v)
186 if (n->x < v->x)
187 return;
188 if (n->x > v->x) {
189 *n = *v;
190 return;
192 if (n->y > v->y)
193 n->y = v->y;
196 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
198 int shift = type->bit_size;
199 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
201 if (!is_unsigned)
202 shift--;
203 if (upper->x == 0 && upper->y >> shift)
204 return 0;
205 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
206 return 1;
207 return 0;
210 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
212 unsigned long long lastval = 0;
213 struct symbol *ctype = NULL, *base_type = NULL;
214 Num upper = {-1, 0}, lower = {1, 0};
216 parent->examined = 1;
217 parent->ctype.base_type = &int_ctype;
218 while (token_type(token) == TOKEN_IDENT) {
219 struct expression *expr = NULL;
220 struct token *next = token->next;
221 struct symbol *sym;
223 sym = alloc_symbol(token->pos, SYM_NODE);
224 bind_symbol(sym, token->ident, NS_SYMBOL);
225 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
227 if (match_op(next, '=')) {
228 next = constant_expression(next->next, &expr);
229 lastval = get_expression_value(expr);
230 ctype = &void_ctype;
231 if (expr && expr->ctype)
232 ctype = expr->ctype;
233 } else if (!ctype) {
234 ctype = &int_ctype;
235 } else if (is_int_type(ctype)) {
236 lastval++;
237 } else {
238 error_die(token->pos, "can't increment the last enum member");
241 if (!expr) {
242 expr = alloc_expression(token->pos, EXPR_VALUE);
243 expr->value = lastval;
246 sym->initializer = expr;
247 sym->ctype.base_type = parent;
249 if (base_type != &bad_ctype) {
250 if (ctype->type == SYM_NODE)
251 ctype = ctype->ctype.base_type;
252 if (ctype->type == SYM_ENUM) {
253 if (ctype == parent)
254 ctype = base_type;
255 else
256 ctype = ctype->ctype.base_type;
259 * base_type rules:
260 * - if all enum's are of the same type, then
261 * the base_type is that type (two first
262 * cases)
263 * - if enums are of different types, they
264 * all have to be integer types, and the
265 * base type is "int_ctype".
266 * - otherwise the base_type is "bad_ctype".
268 if (!base_type) {
269 base_type = ctype;
270 } else if (ctype == base_type) {
271 /* nothing */
272 } else if (is_int_type(base_type) && is_int_type(ctype)) {
273 base_type = &int_ctype;
274 } else
275 base_type = &bad_ctype;
277 if (is_int_type(base_type)) {
278 Num v = {.y = lastval};
279 if (ctype->ctype.modifiers & MOD_UNSIGNED)
280 v.x = 0;
281 else if ((long long)lastval >= 0)
282 v.x = 0;
283 else
284 v.x = -1;
285 upper_boundary(&upper, &v);
286 lower_boundary(&lower, &v);
288 token = next;
289 if (!match_op(token, ','))
290 break;
291 token = token->next;
293 if (!base_type)
294 base_type = &bad_ctype;
295 else if (!is_int_type(base_type))
296 base_type = base_type;
297 else if (type_is_ok(base_type, &upper, &lower))
298 base_type = base_type;
299 else if (type_is_ok(&int_ctype, &upper, &lower))
300 base_type = &int_ctype;
301 else if (type_is_ok(&uint_ctype, &upper, &lower))
302 base_type = &uint_ctype;
303 else if (type_is_ok(&long_ctype, &upper, &lower))
304 base_type = &long_ctype;
305 else if (type_is_ok(&ulong_ctype, &upper, &lower))
306 base_type = &ulong_ctype;
307 else if (type_is_ok(&llong_ctype, &upper, &lower))
308 base_type = &llong_ctype;
309 else if (type_is_ok(&ullong_ctype, &upper, &lower))
310 base_type = &ullong_ctype;
311 else
312 base_type = &bad_ctype;
313 parent->ctype.base_type = base_type;
314 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
315 parent->examined = 0;
316 return token;
319 static struct token *enum_specifier(struct token *token, struct ctype *ctype)
321 return struct_union_enum_specifier(SYM_ENUM, token, ctype, parse_enum_declaration);
324 static struct token *typeof_specifier(struct token *token, struct ctype *ctype)
326 struct symbol *sym;
328 if (!match_op(token, '(')) {
329 warning(token->pos, "expected '(' after typeof");
330 return token;
332 if (lookup_type(token->next)) {
333 token = typename(token->next, &sym);
334 *ctype = sym->ctype;
335 } else {
336 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
337 token = parse_expression(token->next, &typeof_sym->initializer);
339 ctype->modifiers = 0;
340 ctype->base_type = typeof_sym;
342 return expect(token, ')', "after typeof");
345 static const char * handle_attribute(struct ctype *ctype, struct ident *attribute, struct expression *expr)
347 if (attribute == &packed_ident ||
348 attribute == &__packed___ident) {
349 ctype->alignment = 1;
350 return NULL;
352 if (attribute == &aligned_ident ||
353 attribute == &__aligned___ident) {
354 int alignment = max_alignment;
355 if (expr)
356 alignment = get_expression_value(expr);
357 ctype->alignment = alignment;
358 return NULL;
360 if (attribute == &nocast_ident) {
361 ctype->modifiers |= MOD_NOCAST;
362 return NULL;
364 if (attribute == &noderef_ident) {
365 ctype->modifiers |= MOD_NODEREF;
366 return NULL;
368 if (attribute == &safe_ident) {
369 ctype->modifiers |= MOD_SAFE;
370 return NULL;
372 if (attribute == &force_ident) {
373 ctype->modifiers |= MOD_FORCE;
374 return NULL;
376 if (attribute == &bitwise_ident) {
377 if (Wbitwise)
378 ctype->modifiers |= MOD_BITWISE;
379 return NULL;
381 if (attribute == &address_space_ident) {
382 if (!expr)
383 return "expected address space number";
384 ctype->as = get_expression_value(expr);
385 return NULL;
387 if (attribute == &context_ident) {
388 if (expr && expr->type == EXPR_COMMA) {
389 int input = get_expression_value(expr->left);
390 int output = get_expression_value(expr->right);
391 ctype->in_context = input;
392 ctype->out_context = output;
393 return NULL;
395 return "expected context input/output values";
397 if (attribute == &mode_ident ||
398 attribute == &__mode___ident) {
399 if (expr && expr->type == EXPR_SYMBOL) {
400 struct ident *ident = expr->symbol_name;
403 * Match against __QI__/__HI__/__SI__/__DI__
405 * FIXME! This is broken - we don't actually get
406 * the type information updated properly at this
407 * stage for some reason.
409 if (ident == &__QI___ident ||
410 ident == &QI_ident) {
411 ctype->modifiers |= MOD_CHAR;
412 return NULL;
414 if (ident == &__HI___ident ||
415 ident == &HI_ident) {
416 ctype->modifiers |= MOD_SHORT;
417 return NULL;
419 if (ident == &__SI___ident ||
420 ident == &SI_ident) {
421 /* Nothing? */
422 return NULL;
424 if (ident == &__DI___ident ||
425 ident == &DI_ident) {
426 ctype->modifiers |= MOD_LONGLONG;
427 return NULL;
429 if (ident == &__word___ident ||
430 ident == &word_ident) {
431 ctype->modifiers |= MOD_LONG;
432 return NULL;
434 return "unknown mode attribute";
436 return "expected attribute mode symbol";
439 /* Throw away for now.. */
440 if (attribute == &format_ident ||
441 attribute == &__format___ident ||
442 attribute == &__format_arg___ident)
443 return NULL;
444 if (attribute == &section_ident ||
445 attribute == &__section___ident)
446 return NULL;
447 if (attribute == &unused_ident ||
448 attribute == &__unused___ident)
449 return NULL;
450 if (attribute == &const_ident ||
451 attribute == &__const_ident ||
452 attribute == &__const___ident)
453 return NULL;
454 if (attribute == &noreturn_ident ||
455 attribute == &__noreturn___ident)
456 return NULL;
457 if (attribute == &regparm_ident)
458 return NULL;
459 if (attribute == &weak_ident)
460 return NULL;
461 if (attribute == &alias_ident)
462 return NULL;
463 if (attribute == &pure_ident)
464 return NULL;
465 if (attribute == &always_inline_ident)
466 return NULL;
467 if (attribute == &syscall_linkage_ident)
468 return NULL;
469 if (attribute == &visibility_ident)
470 return NULL;
471 if (attribute == &deprecated_ident)
472 return NULL;
473 if (attribute == &noinline_ident)
474 return NULL;
475 if (attribute == &__used___ident)
476 return NULL;
477 if (attribute == &warn_unused_result_ident)
478 return NULL;
479 if (attribute == &model_ident ||
480 attribute == &__model___ident)
481 return NULL;
483 return "unknown attribute";
486 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
488 ctype->modifiers = 0;
489 token = expect(token, '(', "after attribute");
490 token = expect(token, '(', "after attribute");
492 for (;;) {
493 const char *error;
494 struct ident *attribute_name;
495 struct expression *attribute_expr;
497 if (eof_token(token))
498 break;
499 if (match_op(token, ';'))
500 break;
501 if (token_type(token) != TOKEN_IDENT)
502 break;
503 attribute_name = token->ident;
504 token = token->next;
505 attribute_expr = NULL;
506 if (match_op(token, '('))
507 token = parens_expression(token, &attribute_expr, "in attribute");
508 error = handle_attribute(ctype, attribute_name, attribute_expr);
509 if (error)
510 warning(token->pos, "attribute '%s': %s", show_ident(attribute_name), error);
511 if (!match_op(token, ','))
512 break;
513 token = token->next;
516 token = expect(token, ')', "after attribute");
517 token = expect(token, ')', "after attribute");
518 return token;
521 struct symbol * ctype_integer(unsigned long spec)
523 static struct symbol *const integer_ctypes[][3] = {
524 { &llong_ctype, &sllong_ctype, &ullong_ctype },
525 { &long_ctype, &slong_ctype, &ulong_ctype },
526 { &short_ctype, &sshort_ctype, &ushort_ctype },
527 { &char_ctype, &schar_ctype, &uchar_ctype },
528 { &int_ctype, &sint_ctype, &uint_ctype },
530 struct symbol *const (*ctype)[3];
531 int sub;
533 ctype = integer_ctypes;
534 if (!(spec & MOD_LONGLONG)) {
535 ctype++;
536 if (!(spec & MOD_LONG)) {
537 ctype++;
538 if (!(spec & MOD_SHORT)) {
539 ctype++;
540 if (!(spec & MOD_CHAR))
541 ctype++;
546 sub = ((spec & MOD_UNSIGNED)
548 : ((spec & MOD_EXPLICITLY_SIGNED)
550 : 0));
552 return ctype[0][sub];
555 struct symbol * ctype_fp(unsigned long spec)
557 if (spec & MOD_LONGLONG)
558 return &ldouble_ctype;
559 if (spec & MOD_LONG)
560 return &double_ctype;
561 return &float_ctype;
564 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
566 unsigned long mod = thistype->modifiers;
568 if (mod) {
569 unsigned long old = ctype->modifiers;
570 unsigned long extra = 0, dup, conflict;
572 if (mod & old & MOD_LONG) {
573 extra = MOD_LONGLONG | MOD_LONG;
574 mod &= ~MOD_LONG;
575 old &= ~MOD_LONG;
577 dup = (mod & old) | (extra & old) | (extra & mod);
578 if (dup)
579 warning(pos, "Just how %sdo you want this type to be?",
580 modifier_string(dup));
582 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
583 if (conflict)
584 warning(pos, "You cannot have both long and short modifiers.");
586 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
587 if (conflict)
588 warning(pos, "You cannot have both signed and unsigned modifiers.");
590 // Only one storage modifier allowed, except that "inline" doesn't count.
591 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
592 conflict &= (conflict - 1);
593 if (conflict)
594 warning(pos, "multiple storage classes");
596 ctype->modifiers = old | mod | extra;
599 /* Context mask and value */
600 ctype->in_context += thistype->in_context;
601 ctype->out_context += thistype->out_context;
603 /* Alignment */
604 if (thistype->alignment & (thistype->alignment-1)) {
605 warning(pos, "I don't like non-power-of-2 alignments");
606 thistype->alignment = 0;
608 if (thistype->alignment > ctype->alignment)
609 ctype->alignment = thistype->alignment;
611 /* Address space */
612 ctype->as = thistype->as;
615 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
617 unsigned long banned, wrong;
618 unsigned long this_mod = s->ctype.modifiers;
619 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
620 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
622 if (this_mod & (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF))
623 banned = BANNED_SIZE | BANNED_SIGN;
624 else if (this_mod & MOD_SPECIALBITS)
625 banned = 0;
626 else if (s->ctype.base_type == &fp_type)
627 banned = BANNED_SIGN;
628 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
629 banned = 0;
630 else {
631 // label_type
632 // void_type
633 // bad_type
634 // vector_type <-- whatever that is
635 banned = BANNED_SIZE | BANNED_SIGN;
638 wrong = mod & banned;
639 if (wrong)
640 warning(*pos, "modifier %sis invalid in this context",
641 modifier_string (wrong));
645 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
647 struct token *token;
649 while ( (token = next) != NULL ) {
650 struct ctype thistype;
651 struct ident *ident;
652 struct symbol *s, *type;
653 unsigned long mod;
655 next = token->next;
656 if (token_type(token) != TOKEN_IDENT)
657 break;
658 ident = token->ident;
660 s = lookup_symbol(ident, NS_TYPEDEF);
661 if (!s)
662 break;
663 thistype = s->ctype;
664 mod = thistype.modifiers;
665 if (qual && (mod & ~(MOD_ATTRIBUTE | MOD_CONST | MOD_VOLATILE)))
666 break;
667 if (mod & MOD_SPECIALBITS) {
668 if (mod & MOD_STRUCTOF)
669 next = struct_or_union_specifier(SYM_STRUCT, next, &thistype);
670 else if (mod & MOD_UNIONOF)
671 next = struct_or_union_specifier(SYM_UNION, next, &thistype);
672 else if (mod & MOD_ENUMOF)
673 next = enum_specifier(next, &thistype);
674 else if (mod & MOD_ATTRIBUTE)
675 next = attribute_specifier(next, &thistype);
676 else if (mod & MOD_TYPEOF)
677 next = typeof_specifier(next, &thistype);
678 mod = thistype.modifiers;
680 type = thistype.base_type;
681 if (type) {
682 if (qual)
683 break;
684 if (ctype->base_type)
685 break;
686 /* User types only mix with qualifiers */
687 if (mod & MOD_USERTYPE) {
688 if (ctype->modifiers & MOD_SPECIFIER)
689 break;
691 ctype->base_type = type;
694 check_modifiers(&token->pos, s, ctype->modifiers);
695 apply_ctype(token->pos, &thistype, ctype);
698 /* Turn the "virtual types" into real types with real sizes etc */
699 if (!ctype->base_type) {
700 struct symbol *base = &incomplete_ctype;
703 * If we have modifiers, we'll default to an integer
704 * type, and "ctype_integer()" will turn this into
705 * a specific one.
707 if (ctype->modifiers & MOD_SPECIFIER)
708 base = &int_type;
709 ctype->base_type = base;
711 if (ctype->base_type == &int_type) {
712 ctype->base_type = ctype_integer(ctype->modifiers);
713 ctype->modifiers &= ~MOD_SPECIFIER;
714 } else if (ctype->base_type == &fp_type) {
715 ctype->base_type = ctype_fp(ctype->modifiers);
716 ctype->modifiers &= ~MOD_SPECIFIER;
718 if (ctype->modifiers & MOD_BITWISE) {
719 struct symbol *type;
720 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
721 if (!is_int_type(ctype->base_type)) {
722 warning(token->pos, "invalid modifier");
723 return token;
725 type = alloc_symbol(token->pos, SYM_BASETYPE);
726 *type = *ctype->base_type;
727 type->ctype.base_type = ctype->base_type;
728 type->type = SYM_RESTRICT;
729 type->ctype.modifiers &= ~MOD_SPECIFIER;
730 ctype->base_type = type;
732 return token;
735 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
737 struct expression *expr = NULL;
739 token = parse_expression(token, &expr);
740 sym->array_size = expr;
741 return token;
744 static struct token *parameter_type_list(struct token *, struct symbol *, struct ident **p);
745 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
747 static struct token *handle_attributes(struct token *token, struct ctype *ctype)
749 for (;;) {
750 if (token_type(token) != TOKEN_IDENT)
751 break;
752 if (match_idents(token, &__attribute___ident, &__attribute_ident, NULL)) {
753 struct ctype thistype = { 0, };
754 token = attribute_specifier(token->next, &thistype);
755 apply_ctype(token->pos, &thistype, ctype);
756 continue;
758 if (match_idents(token, &asm_ident, &__asm_ident, &__asm___ident)) {
759 struct expression *expr;
760 token = expect(token->next, '(', "after asm");
761 token = parse_expression(token->next, &expr);
762 token = expect(token, ')', "after asm");
763 continue;
765 break;
767 return token;
770 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
772 struct ctype *ctype = &decl->ctype;
774 if (p && token_type(token) == TOKEN_IDENT) {
775 *p = token->ident;
776 token = token->next;
779 for (;;) {
780 token = handle_attributes(token, ctype);
782 if (token_type(token) != TOKEN_SPECIAL)
783 return token;
786 * This can be either a parameter list or a grouping.
787 * For the direct (non-abstract) case, we know if must be
788 * a parameter list if we already saw the identifier.
789 * For the abstract case, we know if must be a parameter
790 * list if it is empty or starts with a type.
792 if (token->special == '(') {
793 struct symbol *sym;
794 struct token *next = token->next;
795 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
797 if (!fn) {
798 struct symbol *base_type = ctype->base_type;
799 token = declarator(next, decl, p);
800 token = expect(token, ')', "in nested declarator");
801 while (ctype->base_type != base_type)
802 ctype = &ctype->base_type->ctype;
803 p = NULL;
804 continue;
807 sym = indirect(token->pos, ctype, SYM_FN);
808 token = parameter_type_list(next, sym, p);
809 token = expect(token, ')', "in function declarator");
810 continue;
812 if (token->special == '[') {
813 struct symbol *array = indirect(token->pos, ctype, SYM_ARRAY);
814 token = abstract_array_declarator(token->next, array);
815 token = expect(token, ']', "in abstract_array_declarator");
816 ctype = &array->ctype;
817 continue;
819 break;
821 return token;
824 static struct token *pointer(struct token *token, struct ctype *ctype)
826 unsigned long modifiers;
827 struct symbol *base_type;
829 modifiers = ctype->modifiers & ~(MOD_TYPEDEF | MOD_ATTRIBUTE);
830 base_type = ctype->base_type;
831 ctype->modifiers = modifiers;
833 while (match_op(token,'*')) {
834 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
835 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
836 ptr->ctype.as = ctype->as;
837 ptr->ctype.in_context += ctype->in_context;
838 ptr->ctype.out_context += ctype->out_context;
839 ptr->ctype.base_type = base_type;
841 base_type = ptr;
842 ctype->modifiers = modifiers & MOD_STORAGE;
843 ctype->base_type = base_type;
844 ctype->as = 0;
845 ctype->in_context = 0;
846 ctype->out_context = 0;
848 token = declaration_specifiers(token->next, ctype, 1);
849 modifiers = ctype->modifiers;
851 return token;
854 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
856 token = pointer(token, &sym->ctype);
857 return direct_declarator(token, sym, p);
860 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
862 struct ctype *ctype = &decl->ctype;
863 struct expression *expr;
864 struct symbol *bitfield;
865 long long width;
867 if (!is_int_type(ctype->base_type)) {
868 warning(token->pos, "invalid bitfield specifier for type %s.",
869 show_typename(ctype->base_type));
870 // Parse this to recover gracefully.
871 return conditional_expression(token->next, &expr);
874 bitfield = indirect(token->pos, ctype, SYM_BITFIELD);
875 token = conditional_expression(token->next, &expr);
876 width = get_expression_value(expr);
877 bitfield->bit_size = width;
879 if (width < 0 || width > INT_MAX) {
880 warning(token->pos, "invalid bitfield width, %lld.", width);
881 width = -1;
882 } else if (decl->ident && width == 0) {
883 warning(token->pos, "invalid named zero-width bitfield `%s'",
884 show_ident(decl->ident));
885 width = -1;
886 } else if (decl->ident) {
887 struct symbol *base_type = bitfield->ctype.base_type;
888 int is_signed = !(base_type->ctype.modifiers & MOD_UNSIGNED);
889 if (width == 1 && is_signed) {
890 // Valid values are either {-1;0} or {0}, depending on integer
891 // representation. The latter makes for very efficient code...
892 warning(token->pos, "dubious one-bit signed bitfield");
894 if (Wdefault_bitfield_sign &&
895 base_type->type != SYM_ENUM &&
896 !(base_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
897 is_signed) {
898 // The sign of bitfields is unspecified by default.
899 warning (token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
902 bitfield->bit_size = width;
903 return token;
906 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
908 while (!match_op(token, '}')) {
909 struct ctype ctype = {0, };
911 token = declaration_specifiers(token, &ctype, 0);
912 for (;;) {
913 struct ident *ident = NULL;
914 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
915 decl->ctype = ctype;
916 token = declarator(token, decl, &ident);
917 decl->ident = ident;
918 if (match_op(token, ':')) {
919 token = handle_bitfield(token, decl);
920 token = handle_attributes(token, &decl->ctype);
922 add_symbol(list, decl);
923 if (!match_op(token, ','))
924 break;
925 token = token->next;
927 if (!match_op(token, ';')) {
928 warning(token->pos, "expected ; at end of declaration");
929 break;
931 token = token->next;
933 return token;
936 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
938 struct ident *ident = NULL;
939 struct symbol *sym;
940 struct ctype ctype = { 0, };
942 token = declaration_specifiers(token, &ctype, 0);
943 sym = alloc_symbol(token->pos, SYM_NODE);
944 sym->ctype = ctype;
945 *tree = sym;
946 token = declarator(token, sym, &ident);
947 sym->ident = ident;
948 return token;
951 struct token *typename(struct token *token, struct symbol **p)
953 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
954 *p = sym;
955 token = declaration_specifiers(token, &sym->ctype, 0);
956 return declarator(token, sym, NULL);
959 static struct token *expression_statement(struct token *token, struct expression **tree)
961 token = parse_expression(token, tree);
962 return expect(token, ';', "at end of statement");
965 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
966 struct expression_list **inout)
968 struct expression *expr;
970 /* Allow empty operands */
971 if (match_op(token->next, ':') || match_op(token->next, ')'))
972 return token->next;
973 do {
974 struct ident *ident = NULL;
975 if (match_op(token->next, '[') &&
976 token_type(token->next->next) == TOKEN_IDENT &&
977 match_op(token->next->next->next, ']')) {
978 ident = token->next->next->ident;
979 token = token->next->next->next;
981 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
982 token = primary_expression(token->next, &expr);
983 add_expression(inout, expr);
984 token = parens_expression(token, &expr, "in asm parameter");
985 add_expression(inout, expr);
986 } while (match_op(token, ','));
987 return token;
990 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
991 struct expression_list **clobbers)
993 struct expression *expr;
995 do {
996 token = primary_expression(token->next, &expr);
997 add_expression(clobbers, expr);
998 } while (match_op(token, ','));
999 return token;
1002 static struct token *parse_asm(struct token *token, struct statement *stmt)
1004 stmt->type = STMT_ASM;
1005 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1006 token = token->next;
1008 token = expect(token, '(', "after asm");
1009 token = parse_expression(token, &stmt->asm_string);
1010 if (match_op(token, ':'))
1011 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1012 if (match_op(token, ':'))
1013 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1014 if (match_op(token, ':'))
1015 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1016 token = expect(token, ')', "after asm");
1017 return expect(token, ';', "at end of asm-statement");
1020 /* Make a statement out of an expression */
1021 static struct statement *make_statement(struct expression *expr)
1023 struct statement *stmt;
1025 if (!expr)
1026 return NULL;
1027 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1028 stmt->expression = expr;
1029 return stmt;
1033 * All iterators have two symbols associated with them:
1034 * the "continue" and "break" symbols, which are targets
1035 * for continue and break statements respectively.
1037 * They are in a special name-space, but they follow
1038 * all the normal visibility rules, so nested iterators
1039 * automatically work right.
1041 static void start_iterator(struct statement *stmt)
1043 struct symbol *cont, *brk;
1045 start_symbol_scope();
1046 cont = alloc_symbol(stmt->pos, SYM_NODE);
1047 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1048 brk = alloc_symbol(stmt->pos, SYM_NODE);
1049 bind_symbol(brk, &break_ident, NS_ITERATOR);
1051 stmt->type = STMT_ITERATOR;
1052 stmt->iterator_break = brk;
1053 stmt->iterator_continue = cont;
1054 fn_local_symbol(brk);
1055 fn_local_symbol(cont);
1058 static void end_iterator(struct statement *stmt)
1060 end_symbol_scope();
1063 static struct statement *start_function(struct symbol *sym)
1065 struct symbol *ret;
1066 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1068 start_function_scope();
1069 ret = alloc_symbol(sym->pos, SYM_NODE);
1070 ret->ctype = sym->ctype.base_type->ctype;
1071 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1072 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1073 bind_symbol(ret, &return_ident, NS_ITERATOR);
1074 stmt->ret = ret;
1075 fn_local_symbol(ret);
1077 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1078 current_fn = sym;
1080 return stmt;
1083 static void end_function(struct symbol *sym)
1085 current_fn = NULL;
1086 end_function_scope();
1090 * A "switch()" statement, like an iterator, has a
1091 * the "break" symbol associated with it. It works
1092 * exactly like the iterator break - it's the target
1093 * for any break-statements in scope, and means that
1094 * "break" handling doesn't even need to know whether
1095 * it's breaking out of an iterator or a switch.
1097 * In addition, the "case" symbol is a marker for the
1098 * case/default statements to find the switch statement
1099 * that they are associated with.
1101 static void start_switch(struct statement *stmt)
1103 struct symbol *brk, *switch_case;
1105 start_symbol_scope();
1106 brk = alloc_symbol(stmt->pos, SYM_NODE);
1107 bind_symbol(brk, &break_ident, NS_ITERATOR);
1109 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1110 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1111 switch_case->stmt = stmt;
1113 stmt->type = STMT_SWITCH;
1114 stmt->switch_break = brk;
1115 stmt->switch_case = switch_case;
1117 fn_local_symbol(brk);
1118 fn_local_symbol(switch_case);
1121 static void end_switch(struct statement *stmt)
1123 if (!stmt->switch_case->symbol_list)
1124 warning(stmt->pos, "switch with no cases");
1125 end_symbol_scope();
1128 static void add_case_statement(struct statement *stmt)
1130 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1131 struct symbol *sym;
1133 if (!target) {
1134 warning(stmt->pos, "not in switch scope");
1135 stmt->type = STMT_NONE;
1136 return;
1138 sym = alloc_symbol(stmt->pos, SYM_NODE);
1139 add_symbol(&target->symbol_list, sym);
1140 sym->stmt = stmt;
1141 stmt->case_label = sym;
1142 fn_local_symbol(sym);
1145 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1147 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1149 if (!target)
1150 error_die(token->pos, "internal error: return without a function target");
1151 stmt->type = STMT_RETURN;
1152 stmt->ret_target = target;
1153 return expression_statement(token->next, &stmt->ret_value);
1156 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1158 struct symbol_list *syms;
1159 struct expression *e1, *e2, *e3;
1160 struct statement *iterator;
1162 start_iterator(stmt);
1163 token = expect(token->next, '(', "after 'for'");
1165 syms = NULL;
1166 e1 = NULL;
1167 /* C99 variable declaration? */
1168 if (lookup_type(token)) {
1169 token = external_declaration(token, &syms);
1170 } else {
1171 token = parse_expression(token, &e1);
1172 token = expect(token, ';', "in 'for'");
1174 token = parse_expression(token, &e2);
1175 token = expect(token, ';', "in 'for'");
1176 token = parse_expression(token, &e3);
1177 token = expect(token, ')', "in 'for'");
1178 token = statement(token, &iterator);
1180 stmt->iterator_syms = syms;
1181 stmt->iterator_pre_statement = make_statement(e1);
1182 stmt->iterator_pre_condition = e2;
1183 stmt->iterator_post_statement = make_statement(e3);
1184 stmt->iterator_post_condition = e2;
1185 stmt->iterator_statement = iterator;
1186 end_iterator(stmt);
1188 return token;
1191 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
1193 struct expression *expr;
1194 struct statement *iterator;
1196 start_iterator(stmt);
1197 token = parens_expression(token->next, &expr, "after 'while'");
1198 token = statement(token, &iterator);
1200 stmt->iterator_pre_condition = expr;
1201 stmt->iterator_post_condition = expr;
1202 stmt->iterator_statement = iterator;
1203 end_iterator(stmt);
1205 return token;
1208 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
1210 struct expression *expr;
1211 struct statement *iterator;
1213 start_iterator(stmt);
1214 token = statement(token->next, &iterator);
1215 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1216 token = token->next;
1217 else
1218 warning(token->pos, "expected 'while' after 'do'");
1219 token = parens_expression(token, &expr, "after 'do-while'");
1221 stmt->iterator_post_condition = expr;
1222 stmt->iterator_statement = iterator;
1223 end_iterator(stmt);
1225 return expect(token, ';', "after statement");
1228 static struct token *statement(struct token *token, struct statement **tree)
1230 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1232 *tree = stmt;
1233 if (token_type(token) == TOKEN_IDENT) {
1234 if (token->ident == &if_ident) {
1235 stmt->type = STMT_IF;
1236 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1237 token = statement(token, &stmt->if_true);
1238 if (token_type(token) != TOKEN_IDENT)
1239 return token;
1240 if (token->ident != &else_ident)
1241 return token;
1242 return statement(token->next, &stmt->if_false);
1245 if (token->ident == &return_ident)
1246 return parse_return_statement(token, stmt);
1248 if (token->ident == &break_ident || token->ident == &continue_ident) {
1249 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1250 stmt->type = STMT_GOTO;
1251 stmt->goto_label = target;
1252 if (!target)
1253 warning(stmt->pos, "break/continue not in iterator scope");
1254 return expect(token->next, ';', "at end of statement");
1256 if (token->ident == &default_ident) {
1257 token = token->next;
1258 goto default_statement;
1260 if (token->ident == &case_ident) {
1261 token = parse_expression(token->next, &stmt->case_expression);
1262 if (match_op(token, SPECIAL_ELLIPSIS))
1263 token = parse_expression(token->next, &stmt->case_to);
1264 default_statement:
1265 stmt->type = STMT_CASE;
1266 token = expect(token, ':', "after default/case");
1267 add_case_statement(stmt);
1268 return statement(token, &stmt->case_statement);
1270 if (token->ident == &switch_ident) {
1271 stmt->type = STMT_SWITCH;
1272 start_switch(stmt);
1273 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1274 token = statement(token, &stmt->switch_statement);
1275 end_switch(stmt);
1276 return token;
1278 if (token->ident == &for_ident)
1279 return parse_for_statement(token, stmt);
1281 if (token->ident == &while_ident)
1282 return parse_while_statement(token, stmt);
1284 if (token->ident == &do_ident)
1285 return parse_do_statement(token, stmt);
1287 if (token->ident == &goto_ident) {
1288 stmt->type = STMT_GOTO;
1289 token = token->next;
1290 if (match_op(token, '*')) {
1291 token = parse_expression(token->next, &stmt->goto_expression);
1292 add_statement(&function_computed_goto_list, stmt);
1293 } else if (token_type(token) == TOKEN_IDENT) {
1294 stmt->goto_label = label_symbol(token);
1295 token = token->next;
1296 } else {
1297 warning(token->pos, "Expected identifier or goto expression");
1299 return expect(token, ';', "at end of statement");
1301 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1302 return parse_asm(token->next, stmt);
1304 if (token->ident == &__context___ident) {
1305 stmt->type = STMT_CONTEXT;
1306 token = parse_expression(token->next, &stmt->expression);
1307 return expect(token, ';', "at end of statement");
1309 if (token->ident == &__range___ident) {
1310 stmt->type = STMT_RANGE;
1311 token = assignment_expression(token->next, &stmt->range_expression);
1312 token = expect(token, ',', "after range expression");
1313 token = assignment_expression(token, &stmt->range_low);
1314 token = expect(token, ',', "after low range");
1315 token = assignment_expression(token, &stmt->range_high);
1316 return expect(token, ';', "after range statement");
1318 if (match_op(token->next, ':')) {
1319 stmt->type = STMT_LABEL;
1320 stmt->label_identifier = label_symbol(token);
1321 return statement(token->next->next, &stmt->label_statement);
1325 if (match_op(token, '{')) {
1326 stmt->type = STMT_COMPOUND;
1327 start_symbol_scope();
1328 token = compound_statement(token->next, stmt);
1329 end_symbol_scope();
1331 return expect(token, '}', "at end of compound statement");
1334 stmt->type = STMT_EXPRESSION;
1335 return expression_statement(token, &stmt->expression);
1338 static struct token * statement_list(struct token *token, struct statement_list **list, struct symbol_list **syms)
1340 for (;;) {
1341 struct statement * stmt;
1342 if (eof_token(token))
1343 break;
1344 if (match_op(token, '}'))
1345 break;
1346 if (lookup_type(token)) {
1347 if (warn_on_mixed && *list)
1348 warning(token->pos, "mixing declarations and code");
1349 token = external_declaration(token, syms);
1350 continue;
1352 token = statement(token, &stmt);
1353 add_statement(list, stmt);
1355 return token;
1358 static struct token *parameter_type_list(struct token *token, struct symbol *fn, struct ident **p)
1360 struct symbol_list **list = &fn->arguments;
1362 if (match_op(token, ')')) {
1363 // No warning for "void oink ();"
1364 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1365 if (p && !match_op(token->next, ';'))
1366 warning(token->pos, "non-ANSI function declaration of function '%s'", show_ident(*p));
1367 return token;
1370 for (;;) {
1371 struct symbol *sym;
1373 if (match_op(token, SPECIAL_ELLIPSIS)) {
1374 if (!*list)
1375 warning(token->pos, "variadic functions must have one named argument");
1376 fn->variadic = 1;
1377 token = token->next;
1378 break;
1381 sym = alloc_symbol(token->pos, SYM_NODE);
1382 token = parameter_declaration(token, &sym);
1383 if (sym->ctype.base_type == &void_ctype) {
1384 /* Special case: (void) */
1385 if (!*list && !sym->ident)
1386 break;
1387 warning(token->pos, "void parameter");
1389 add_symbol(list, sym);
1390 if (!match_op(token, ','))
1391 break;
1392 token = token->next;
1395 return token;
1398 struct token *compound_statement(struct token *token, struct statement *stmt)
1400 token = statement_list(token, &stmt->stmts, &stmt->syms);
1401 return token;
1404 static struct expression *identifier_expression(struct token *token)
1406 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1407 expr->expr_ident = token->ident;
1408 return expr;
1411 static struct expression *index_expression(struct expression *from, struct expression *to)
1413 int idx_from, idx_to;
1414 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1416 idx_from = get_expression_value(from);
1417 idx_to = idx_from;
1418 if (to) {
1419 idx_to = get_expression_value(to);
1420 if (idx_to < idx_from || idx_from < 0)
1421 warning(from->pos, "nonsense array initializer index range");
1423 expr->idx_from = idx_from;
1424 expr->idx_to = idx_to;
1425 return expr;
1428 static struct token *single_initializer(struct expression **ep, struct token *token)
1430 int expect_equal = 0;
1431 struct token *next = token->next;
1432 struct expression **tail = ep;
1433 int nested;
1435 *ep = NULL;
1437 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1438 struct expression *expr = identifier_expression(token);
1439 warning(token->pos, "obsolete struct initializer, use C99 syntax");
1440 token = initializer(&expr->ident_expression, next->next);
1441 if (expr->ident_expression)
1442 *ep = expr;
1443 return token;
1446 for (tail = ep, nested = 0; ; nested++, next = token->next) {
1447 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
1448 struct expression *expr = identifier_expression(next);
1449 *tail = expr;
1450 tail = &expr->ident_expression;
1451 expect_equal = 1;
1452 token = next->next;
1453 } else if (match_op(token, '[')) {
1454 struct expression *from = NULL, *to = NULL, *expr;
1455 token = constant_expression(token->next, &from);
1456 if (match_op(token, SPECIAL_ELLIPSIS))
1457 token = constant_expression(token->next, &to);
1458 expr = index_expression(from, to);
1459 *tail = expr;
1460 tail = &expr->idx_expression;
1461 token = expect(token, ']', "at end of initializer index");
1462 if (nested)
1463 expect_equal = 1;
1464 } else {
1465 break;
1468 if (nested && !expect_equal) {
1469 if (!match_op(token, '='))
1470 warning(token->pos, "obsolete array initializer, use C99 syntax");
1471 else
1472 expect_equal = 1;
1474 if (expect_equal)
1475 token = expect(token, '=', "at end of initializer index");
1477 token = initializer(tail, token);
1478 if (!*tail)
1479 *ep = NULL;
1480 return token;
1483 static struct token *initializer_list(struct expression_list **list, struct token *token)
1485 struct expression *expr;
1487 for (;;) {
1488 token = single_initializer(&expr, token);
1489 if (!expr)
1490 break;
1491 add_expression(list, expr);
1492 if (!match_op(token, ','))
1493 break;
1494 token = token->next;
1496 return token;
1499 struct token *initializer(struct expression **tree, struct token *token)
1501 if (match_op(token, '{')) {
1502 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1503 *tree = expr;
1504 token = initializer_list(&expr->expr_list, token->next);
1505 return expect(token, '}', "at end of initializer");
1507 return assignment_expression(token, tree);
1510 static void declare_argument(struct symbol *sym, struct symbol *fn)
1512 if (!sym->ident) {
1513 warning(sym->pos, "no identifier for function argument");
1514 return;
1516 bind_symbol(sym, sym->ident, NS_SYMBOL);
1519 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1520 struct symbol_list **list)
1522 struct symbol_list **old_symbol_list;
1523 struct symbol *base_type = decl->ctype.base_type;
1524 struct statement *stmt, **p;
1525 struct symbol *arg;
1527 old_symbol_list = function_symbol_list;
1528 if (decl->ctype.modifiers & MOD_INLINE) {
1529 function_symbol_list = &decl->inline_symbol_list;
1530 p = &base_type->inline_stmt;
1531 } else {
1532 function_symbol_list = &decl->symbol_list;
1533 p = &base_type->stmt;
1535 function_computed_target_list = NULL;
1536 function_computed_goto_list = NULL;
1538 if (decl->ctype.modifiers & MOD_EXTERN) {
1539 if (!(decl->ctype.modifiers & MOD_INLINE))
1540 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
1542 if (!(decl->ctype.modifiers & MOD_STATIC))
1543 decl->ctype.modifiers |= MOD_EXTERN;
1545 stmt = start_function(decl);
1547 *p = stmt;
1548 FOR_EACH_PTR (base_type->arguments, arg) {
1549 declare_argument(arg, base_type);
1550 } END_FOR_EACH_PTR(arg);
1552 token = compound_statement(token->next, stmt);
1554 end_function(decl);
1555 if (!(decl->ctype.modifiers & MOD_INLINE))
1556 add_symbol(list, decl);
1557 check_declaration(decl);
1558 function_symbol_list = old_symbol_list;
1559 if (function_computed_goto_list) {
1560 if (!function_computed_target_list)
1561 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
1562 else {
1563 struct statement *stmt;
1564 FOR_EACH_PTR(function_computed_goto_list, stmt) {
1565 stmt->target_list = function_computed_target_list;
1566 } END_FOR_EACH_PTR(stmt);
1569 return expect(token, '}', "at end of function");
1572 static void promote_k_r_types(struct symbol *arg)
1574 struct symbol *base = arg->ctype.base_type;
1575 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
1576 arg->ctype.base_type = &int_ctype;
1580 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
1582 struct symbol_list *real_args = fn->ctype.base_type->arguments;
1583 struct symbol *arg;
1585 FOR_EACH_PTR(real_args, arg) {
1586 struct symbol *type;
1588 /* This is quadratic in the number of arguments. We _really_ don't care */
1589 FOR_EACH_PTR(argtypes, type) {
1590 if (type->ident == arg->ident)
1591 goto match;
1592 } END_FOR_EACH_PTR(type);
1593 warning(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
1594 continue;
1595 match:
1596 type->used = 1;
1597 /* "char" and "short" promote to "int" */
1598 promote_k_r_types(type);
1600 arg->ctype = type->ctype;
1601 } END_FOR_EACH_PTR(arg);
1603 FOR_EACH_PTR(argtypes, arg) {
1604 if (!arg->used)
1605 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
1606 } END_FOR_EACH_PTR(arg);
1610 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
1611 struct symbol_list **list)
1613 struct symbol_list *args = NULL;
1615 warning(token->pos, "non-ANSI function declaration of function '%s'", show_ident(decl->ident));
1616 do {
1617 token = external_declaration(token, &args);
1618 } while (lookup_type(token));
1620 apply_k_r_types(args, decl);
1622 if (!match_op(token, '{')) {
1623 warning(token->pos, "expected function body");
1624 return token;
1626 return parse_function_body(token, decl, list);
1630 static struct token *external_declaration(struct token *token, struct symbol_list **list)
1632 struct ident *ident = NULL;
1633 struct symbol *decl;
1634 struct ctype ctype = { 0, };
1635 struct symbol *base_type;
1636 int is_typedef;
1638 /* Top-level inline asm? */
1639 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1640 struct symbol_list **old_symbol_list;
1641 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
1642 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
1643 struct statement *stmt;
1645 anon->ctype.base_type = fn;
1646 old_symbol_list = function_symbol_list;
1647 function_symbol_list = &anon->symbol_list;
1648 stmt = start_function(anon);
1649 token = parse_asm(token->next, stmt);
1650 end_function(anon);
1651 function_symbol_list = old_symbol_list;
1652 add_symbol(list, anon);
1653 return token;
1656 /* Parse declaration-specifiers, if any */
1657 token = declaration_specifiers(token, &ctype, 0);
1658 decl = alloc_symbol(token->pos, SYM_NODE);
1659 decl->ctype = ctype;
1660 token = declarator(token, decl, &ident);
1662 /* Just a type declaration? */
1663 if (!ident)
1664 return expect(token, ';', "end of type declaration");
1666 /* type define declaration? */
1667 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
1669 /* Typedef's don't have meaningful storage */
1670 if (is_typedef) {
1671 ctype.modifiers &= ~MOD_STORAGE;
1672 decl->ctype.modifiers &= ~MOD_STORAGE;
1673 decl->ctype.modifiers |= MOD_USERTYPE;
1676 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1678 base_type = decl->ctype.base_type;
1679 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1680 /* K&R argument declaration? */
1681 if (lookup_type(token))
1682 return parse_k_r_arguments(token, decl, list);
1683 if (match_op(token, '{'))
1684 return parse_function_body(token, decl, list);
1686 if (!(decl->ctype.modifiers & MOD_STATIC))
1687 decl->ctype.modifiers |= MOD_EXTERN;
1688 } else if (!is_typedef && base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
1689 warning(token->pos, "void declaration");
1692 for (;;) {
1693 if (!is_typedef && match_op(token, '=')) {
1694 if (decl->ctype.modifiers & MOD_EXTERN) {
1695 warning(decl->pos, "symbol with external linkage has initializer");
1696 decl->ctype.modifiers &= ~MOD_EXTERN;
1698 token = initializer(&decl->initializer, token->next);
1700 if (!is_typedef) {
1701 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
1702 add_symbol(list, decl);
1703 fn_local_symbol(decl);
1706 check_declaration(decl);
1708 if (!match_op(token, ','))
1709 break;
1711 token = token->next;
1712 ident = NULL;
1713 decl = alloc_symbol(token->pos, SYM_NODE);
1714 decl->ctype = ctype;
1715 token = declaration_specifiers(token, &decl->ctype, 1);
1716 token = declarator(token, decl, &ident);
1717 if (!ident) {
1718 warning(token->pos, "expected identifier name in type definition");
1719 return token;
1722 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1724 /* Function declarations are automatically extern unless specifically static */
1725 base_type = decl->ctype.base_type;
1726 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1727 if (!(decl->ctype.modifiers & MOD_STATIC))
1728 decl->ctype.modifiers |= MOD_EXTERN;
1731 return expect(token, ';', "at end of declaration");
1734 struct symbol_list *translation_unit(struct token *token)
1736 while (!eof_token(token))
1737 token = external_declaration(token, &translation_unit_used_list);
1738 // They aren't needed any more
1739 clear_token_alloc();
1741 /* Evaluate the symbol list */
1742 evaluate_symbol_list(translation_unit_used_list);
1743 return translation_unit_used_list;