Add "stream_name()" helper function, and use it.
[smatch.git] / parse.c
blob39a9551a2e462d9cff440f9f178ad293418cd9fc
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 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 return token;
141 // private struct/union/enum type
142 if (!match_op(token, '{')) {
143 warning(token->pos, "expected declaration");
144 ctype->base_type = &bad_ctype;
145 return token;
148 sym = alloc_symbol(token->pos, type);
149 token = parse(token->next, sym);
150 ctype->base_type = sym;
151 return expect(token, '}', "at end of specifier");
154 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
156 return struct_declaration_list(token, &sym->symbol_list);
159 struct token *struct_or_union_specifier(enum type type, struct token *token, struct ctype *ctype)
161 return struct_union_enum_specifier(type, token, ctype, parse_struct_declaration);
164 typedef struct {
165 int x;
166 unsigned long long y;
167 } Num;
169 static void upper_boundary(Num *n, Num *v)
171 if (n->x > v->x)
172 return;
173 if (n->x < v->x) {
174 *n = *v;
175 return;
177 if (n->y < v->y)
178 n->y = v->y;
181 static void lower_boundary(Num *n, Num *v)
183 if (n->x < v->x)
184 return;
185 if (n->x > v->x) {
186 *n = *v;
187 return;
189 if (n->y > v->y)
190 n->y = v->y;
193 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
195 int shift = type->bit_size;
196 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
198 if (!is_unsigned)
199 shift--;
200 if (upper->x == 0 && upper->y >> shift)
201 return 0;
202 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
203 return 1;
204 return 0;
207 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
209 unsigned long long lastval = 0;
210 struct symbol *ctype = NULL, *base_type = NULL;
211 Num upper = {-1, 0}, lower = {1, 0};
213 while (token_type(token) == TOKEN_IDENT) {
214 struct token *next = token->next;
215 struct symbol *sym;
217 sym = alloc_symbol(token->pos, SYM_ENUM);
218 bind_symbol(sym, token->ident, NS_SYMBOL);
220 if (match_op(next, '=')) {
221 struct expression *expr;
222 next = constant_expression(next->next, &expr);
223 lastval = get_expression_value(expr);
224 ctype = expr->ctype;
225 } else if (!ctype) {
226 ctype = &int_ctype;
227 } else if (is_int_type(ctype)) {
228 lastval++;
229 } else {
230 error_die(token->pos, "can't increment the last enum member");
233 sym->value = lastval;
234 sym->ctype.base_type = ctype;
236 if (base_type != &bad_ctype) {
237 if (ctype->type == SYM_NODE)
238 ctype = ctype->ctype.base_type;
239 if (ctype->type == SYM_ENUM)
240 ctype = ctype->ctype.base_type;
242 * base_type rules:
243 * - if all enum's are of the same type, then
244 * the base_type is that type (two first
245 * cases)
246 * - if enums are of different types, they
247 * all have to be integer types, and the
248 * base type is "int_ctype".
249 * - otherwise the base_type is "bad_ctype".
251 if (!base_type) {
252 base_type = ctype;
253 } else if (ctype == base_type) {
254 /* nothing */
255 } else if (is_int_type(base_type) && is_int_type(ctype)) {
256 base_type = &int_ctype;
257 } else
258 base_type = &bad_ctype;
260 if (is_int_type(base_type)) {
261 Num v = {.y = lastval};
262 if (ctype->ctype.modifiers & MOD_UNSIGNED)
263 v.x = 0;
264 else if ((long long)lastval >= 0)
265 v.x = 0;
266 else
267 v.x = -1;
268 upper_boundary(&upper, &v);
269 lower_boundary(&lower, &v);
271 token = next;
272 if (!match_op(token, ','))
273 break;
274 token = token->next;
276 if (!base_type)
277 base_type = &bad_ctype;
278 else if (!is_int_type(base_type))
279 base_type = base_type;
280 else if (type_is_ok(base_type, &upper, &lower))
281 base_type = base_type;
282 else if (type_is_ok(&int_ctype, &upper, &lower))
283 base_type = &int_ctype;
284 else if (type_is_ok(&uint_ctype, &upper, &lower))
285 base_type = &uint_ctype;
286 else if (type_is_ok(&long_ctype, &upper, &lower))
287 base_type = &long_ctype;
288 else if (type_is_ok(&ulong_ctype, &upper, &lower))
289 base_type = &ulong_ctype;
290 else if (type_is_ok(&llong_ctype, &upper, &lower))
291 base_type = &llong_ctype;
292 else if (type_is_ok(&ullong_ctype, &upper, &lower))
293 base_type = &ullong_ctype;
294 else
295 base_type = &bad_ctype;
296 parent->ctype.base_type = base_type;
297 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
298 return token;
301 struct token *enum_specifier(struct token *token, struct ctype *ctype)
303 return struct_union_enum_specifier(SYM_ENUM, token, ctype, parse_enum_declaration);
306 struct token *typeof_specifier(struct token *token, struct ctype *ctype)
308 struct symbol *sym;
310 if (!match_op(token, '(')) {
311 warning(token->pos, "expected '(' after typeof");
312 return token;
314 if (lookup_type(token->next)) {
315 token = typename(token->next, &sym);
316 *ctype = sym->ctype;
317 } else {
318 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
319 token = parse_expression(token->next, &typeof_sym->initializer);
321 ctype->modifiers = 0;
322 ctype->base_type = typeof_sym;
324 return expect(token, ')', "after typeof");
327 static const char * handle_attribute(struct ctype *ctype, struct ident *attribute, struct expression *expr)
329 if (attribute == &packed_ident ||
330 attribute == &__packed___ident) {
331 ctype->alignment = 1;
332 return NULL;
334 if (attribute == &aligned_ident ||
335 attribute == &__aligned___ident) {
336 int alignment = max_alignment;
337 if (expr)
338 alignment = get_expression_value(expr);
339 ctype->alignment = alignment;
340 return NULL;
342 if (attribute == &nocast_ident) {
343 ctype->modifiers |= MOD_NOCAST;
344 return NULL;
346 if (attribute == &noderef_ident) {
347 ctype->modifiers |= MOD_NODEREF;
348 return NULL;
350 if (attribute == &safe_ident) {
351 ctype->modifiers |= MOD_SAFE;
352 return NULL;
354 if (attribute == &force_ident) {
355 ctype->modifiers |= MOD_FORCE;
356 return NULL;
358 if (attribute == &bitwise_ident) {
359 if (Wbitwise)
360 ctype->modifiers |= MOD_BITWISE;
361 return NULL;
363 if (attribute == &address_space_ident) {
364 if (!expr)
365 return "expected address space number";
366 ctype->as = get_expression_value(expr);
367 return NULL;
369 if (attribute == &context_ident) {
370 if (expr && expr->type == EXPR_COMMA) {
371 int input = get_expression_value(expr->left);
372 int output = get_expression_value(expr->right);
373 ctype->in_context = input;
374 ctype->out_context = output;
375 return NULL;
377 return "expected context input/output values";
379 if (attribute == &mode_ident ||
380 attribute == &__mode___ident) {
381 if (expr && expr->type == EXPR_SYMBOL) {
382 struct ident *ident = expr->symbol_name;
385 * Match against __QI__/__HI__/__SI__/__DI__
387 * FIXME! This is broken - we don't actually get
388 * the type information updated properly at this
389 * stage for some reason.
391 if (ident == &__QI___ident ||
392 ident == &QI_ident) {
393 ctype->modifiers |= MOD_CHAR;
394 return NULL;
396 if (ident == &__HI___ident ||
397 ident == &HI_ident) {
398 ctype->modifiers |= MOD_SHORT;
399 return NULL;
401 if (ident == &__SI___ident ||
402 ident == &SI_ident) {
403 /* Nothing? */
404 return NULL;
406 if (ident == &__DI___ident ||
407 ident == &DI_ident) {
408 ctype->modifiers |= MOD_LONGLONG;
409 return NULL;
411 if (ident == &__word___ident ||
412 ident == &word_ident) {
413 ctype->modifiers |= MOD_LONG;
414 return NULL;
416 return "unknown mode attribute";
418 return "expected attribute mode symbol";
421 /* Throw away for now.. */
422 if (attribute == &format_ident ||
423 attribute == &__format___ident)
424 return NULL;
425 if (attribute == &section_ident ||
426 attribute == &__section___ident)
427 return NULL;
428 if (attribute == &unused_ident ||
429 attribute == &__unused___ident)
430 return NULL;
431 if (attribute == &const_ident ||
432 attribute == &__const_ident ||
433 attribute == &__const___ident)
434 return NULL;
435 if (attribute == &noreturn_ident ||
436 attribute == &__noreturn___ident)
437 return NULL;
438 if (attribute == &regparm_ident)
439 return NULL;
440 if (attribute == &weak_ident)
441 return NULL;
442 if (attribute == &alias_ident)
443 return NULL;
444 if (attribute == &pure_ident)
445 return NULL;
446 if (attribute == &always_inline_ident)
447 return NULL;
448 if (attribute == &syscall_linkage_ident)
449 return NULL;
450 if (attribute == &visibility_ident)
451 return NULL;
452 if (attribute == &model_ident ||
453 attribute == &__model___ident)
454 return NULL;
456 return "unknown attribute";
459 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
461 ctype->modifiers = 0;
462 token = expect(token, '(', "after attribute");
463 token = expect(token, '(', "after attribute");
465 for (;;) {
466 const char *error;
467 struct ident *attribute_name;
468 struct expression *attribute_expr;
470 if (eof_token(token))
471 break;
472 if (match_op(token, ';'))
473 break;
474 if (token_type(token) != TOKEN_IDENT)
475 break;
476 attribute_name = token->ident;
477 token = token->next;
478 attribute_expr = NULL;
479 if (match_op(token, '('))
480 token = parens_expression(token, &attribute_expr, "in attribute");
481 error = handle_attribute(ctype, attribute_name, attribute_expr);
482 if (error)
483 warning(token->pos, "attribute '%s': %s", show_ident(attribute_name), error);
484 if (!match_op(token, ','))
485 break;
486 token = token->next;
489 token = expect(token, ')', "after attribute");
490 token = expect(token, ')', "after attribute");
491 return token;
494 struct symbol * ctype_integer(unsigned long spec)
496 static struct symbol *const integer_ctypes[][3] = {
497 { &llong_ctype, &sllong_ctype, &ullong_ctype },
498 { &long_ctype, &slong_ctype, &ulong_ctype },
499 { &short_ctype, &sshort_ctype, &ushort_ctype },
500 { &char_ctype, &schar_ctype, &uchar_ctype },
501 { &int_ctype, &sint_ctype, &uint_ctype },
503 struct symbol *const (*ctype)[3];
504 int sub;
506 ctype = integer_ctypes;
507 if (!(spec & MOD_LONGLONG)) {
508 ctype++;
509 if (!(spec & MOD_LONG)) {
510 ctype++;
511 if (!(spec & MOD_SHORT)) {
512 ctype++;
513 if (!(spec & MOD_CHAR))
514 ctype++;
519 sub = ((spec & MOD_UNSIGNED)
521 : ((spec & MOD_EXPLICITLY_SIGNED)
523 : 0));
525 return ctype[0][sub];
528 struct symbol * ctype_fp(unsigned long spec)
530 if (spec & MOD_LONGLONG)
531 return &ldouble_ctype;
532 if (spec & MOD_LONG)
533 return &double_ctype;
534 return &float_ctype;
537 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
539 unsigned long mod = thistype->modifiers;
541 if (mod) {
542 unsigned long old = ctype->modifiers;
543 unsigned long extra = 0, dup, conflict;
545 if (mod & old & MOD_LONG) {
546 extra = MOD_LONGLONG | MOD_LONG;
547 mod &= ~MOD_LONG;
548 old &= ~MOD_LONG;
550 dup = (mod & old) | (extra & old) | (extra & mod);
551 if (dup)
552 warning(pos, "Just how %sdo you want this type to be?",
553 modifier_string(dup));
555 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
556 if (conflict)
557 warning(pos, "You cannot have both long and short modifiers.");
559 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
560 if (conflict)
561 warning(pos, "You cannot have both signed and unsigned modifiers.");
563 // Only one storage modifier allowed, except that "inline" doesn't count.
564 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
565 conflict &= (conflict - 1);
566 if (conflict)
567 warning(pos, "multiple storage classes");
569 ctype->modifiers = old | mod | extra;
572 /* Context mask and value */
573 ctype->in_context += thistype->in_context;
574 ctype->out_context += thistype->out_context;
576 /* Alignment */
577 if (thistype->alignment & (thistype->alignment-1)) {
578 warning(pos, "I don't like non-power-of-2 alignments");
579 thistype->alignment = 0;
581 if (thistype->alignment > ctype->alignment)
582 ctype->alignment = thistype->alignment;
584 /* Address space */
585 ctype->as = thistype->as;
588 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
590 unsigned long banned, wrong;
591 unsigned long this_mod = s->ctype.modifiers;
592 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
593 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
595 if (this_mod & (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF))
596 banned = BANNED_SIZE | BANNED_SIGN;
597 else if (this_mod & MOD_SPECIALBITS)
598 banned = 0;
599 else if (s->ctype.base_type == &fp_type)
600 banned = BANNED_SIGN;
601 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
602 banned = 0;
603 else {
604 // label_type
605 // void_type
606 // bad_type
607 // vector_type <-- whatever that is
608 banned = BANNED_SIZE | BANNED_SIGN;
611 wrong = mod & banned;
612 if (wrong)
613 warning(*pos, "modifier %sis invalid in this context",
614 modifier_string (wrong));
618 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
620 struct token *token;
622 while ( (token = next) != NULL ) {
623 struct ctype thistype;
624 struct ident *ident;
625 struct symbol *s, *type;
626 unsigned long mod;
628 next = token->next;
629 if (token_type(token) != TOKEN_IDENT)
630 break;
631 ident = token->ident;
633 s = lookup_symbol(ident, NS_TYPEDEF);
634 if (!s)
635 break;
636 thistype = s->ctype;
637 mod = thistype.modifiers;
638 if (qual && (mod & ~(MOD_ATTRIBUTE | MOD_CONST | MOD_VOLATILE)))
639 break;
640 if (mod & MOD_SPECIALBITS) {
641 if (mod & MOD_STRUCTOF)
642 next = struct_or_union_specifier(SYM_STRUCT, next, &thistype);
643 else if (mod & MOD_UNIONOF)
644 next = struct_or_union_specifier(SYM_UNION, next, &thistype);
645 else if (mod & MOD_ENUMOF)
646 next = enum_specifier(next, &thistype);
647 else if (mod & MOD_ATTRIBUTE)
648 next = attribute_specifier(next, &thistype);
649 else if (mod & MOD_TYPEOF)
650 next = typeof_specifier(next, &thistype);
651 mod = thistype.modifiers;
653 type = thistype.base_type;
654 if (type) {
655 if (qual)
656 break;
657 if (ctype->base_type)
658 break;
659 /* User types only mix with qualifiers */
660 if (mod & MOD_USERTYPE) {
661 if (ctype->modifiers & MOD_SPECIFIER)
662 break;
664 ctype->base_type = type;
667 check_modifiers(&token->pos, s, ctype->modifiers);
668 apply_ctype(token->pos, &thistype, ctype);
671 /* Turn the "virtual types" into real types with real sizes etc */
672 if (!ctype->base_type) {
673 struct symbol *base = &incomplete_ctype;
676 * If we have modifiers, we'll default to an integer
677 * type, and "ctype_integer()" will turn this into
678 * a specific one.
680 if (ctype->modifiers & MOD_SPECIFIER)
681 base = &int_type;
682 ctype->base_type = base;
684 if (ctype->base_type == &int_type) {
685 ctype->base_type = ctype_integer(ctype->modifiers);
686 ctype->modifiers &= ~MOD_SPECIFIER;
687 } else if (ctype->base_type == &fp_type) {
688 ctype->base_type = ctype_fp(ctype->modifiers);
689 ctype->modifiers &= ~MOD_SPECIFIER;
691 if (ctype->modifiers & MOD_BITWISE) {
692 struct symbol *type;
693 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
694 if (!is_int_type(ctype->base_type)) {
695 warning(token->pos, "invalid modifier");
696 return token;
698 type = alloc_symbol(token->pos, SYM_BASETYPE);
699 *type = *ctype->base_type;
700 type->ctype.base_type = ctype->base_type;
701 type->type = SYM_RESTRICT;
702 type->ctype.modifiers &= ~MOD_SPECIFIER;
703 ctype->base_type = type;
705 return token;
708 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
710 struct expression *expr = NULL;
712 token = parse_expression(token, &expr);
713 sym->array_size = expr;
714 return token;
717 static struct token *parameter_type_list(struct token *, struct symbol *);
718 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
720 static struct token *handle_attributes(struct token *token, struct ctype *ctype)
722 for (;;) {
723 if (token_type(token) != TOKEN_IDENT)
724 break;
725 if (match_idents(token, &__attribute___ident, &__attribute_ident, NULL)) {
726 struct ctype thistype = { 0, };
727 token = attribute_specifier(token->next, &thistype);
728 apply_ctype(token->pos, &thistype, ctype);
729 continue;
731 if (match_idents(token, &asm_ident, &__asm_ident, &__asm___ident)) {
732 struct expression *expr;
733 token = expect(token->next, '(', "after asm");
734 token = parse_expression(token->next, &expr);
735 token = expect(token, ')', "after asm");
736 continue;
738 break;
740 return token;
743 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
745 struct ctype *ctype = &decl->ctype;
747 if (p && token_type(token) == TOKEN_IDENT) {
748 *p = token->ident;
749 token = token->next;
752 for (;;) {
753 token = handle_attributes(token, ctype);
755 if (token_type(token) != TOKEN_SPECIAL)
756 return token;
759 * This can be either a parameter list or a grouping.
760 * For the direct (non-abstract) case, we know if must be
761 * a parameter list if we already saw the identifier.
762 * For the abstract case, we know if must be a parameter
763 * list if it is empty or starts with a type.
765 if (token->special == '(') {
766 struct symbol *sym;
767 struct token *next = token->next;
768 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
770 if (!fn) {
771 struct symbol *base_type = ctype->base_type;
772 token = declarator(next, decl, p);
773 token = expect(token, ')', "in nested declarator");
774 while (ctype->base_type != base_type)
775 ctype = &ctype->base_type->ctype;
776 p = NULL;
777 continue;
780 sym = indirect(token->pos, ctype, SYM_FN);
781 token = parameter_type_list(next, sym);
782 token = expect(token, ')', "in function declarator");
783 continue;
785 if (token->special == '[') {
786 struct symbol *array = indirect(token->pos, ctype, SYM_ARRAY);
787 token = abstract_array_declarator(token->next, array);
788 token = expect(token, ']', "in abstract_array_declarator");
789 ctype = &array->ctype;
790 continue;
792 break;
794 return token;
797 static struct token *pointer(struct token *token, struct ctype *ctype)
799 unsigned long modifiers;
800 struct symbol *base_type;
802 modifiers = ctype->modifiers & ~(MOD_TYPEDEF | MOD_ATTRIBUTE);
803 base_type = ctype->base_type;
804 ctype->modifiers = modifiers;
806 while (match_op(token,'*')) {
807 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
808 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
809 ptr->ctype.as = ctype->as;
810 ptr->ctype.in_context += ctype->in_context;
811 ptr->ctype.out_context += ctype->out_context;
812 ptr->ctype.base_type = base_type;
814 base_type = ptr;
815 ctype->modifiers = modifiers & MOD_STORAGE;
816 ctype->base_type = base_type;
817 ctype->as = 0;
818 ctype->in_context = 0;
819 ctype->out_context = 0;
821 token = declaration_specifiers(token->next, ctype, 1);
822 modifiers = ctype->modifiers;
824 return token;
827 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
829 token = pointer(token, &sym->ctype);
830 return direct_declarator(token, sym, p);
833 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
835 struct ctype *ctype = &decl->ctype;
836 struct expression *expr;
837 struct symbol *bitfield;
838 long long width;
840 if (!is_int_type(ctype->base_type)) {
841 warning(token->pos, "invalid bitfield specifier for type %s.",
842 show_typename(ctype->base_type));
843 // Parse this to recover gracefully.
844 return conditional_expression(token->next, &expr);
847 bitfield = indirect(token->pos, ctype, SYM_BITFIELD);
848 token = conditional_expression(token->next, &expr);
849 width = get_expression_value(expr);
850 bitfield->bit_size = width;
852 if (width < 0 || width > INT_MAX) {
853 warning(token->pos, "invalid bitfield width, %lld.", width);
854 width = -1;
855 } else if (decl->ident && width == 0) {
856 warning(token->pos, "invalid named zero-width bitfield `%s'",
857 show_ident(decl->ident));
858 width = -1;
859 } else if (decl->ident) {
860 struct symbol *base_type = bitfield->ctype.base_type;
861 int is_signed = !(base_type->ctype.modifiers & MOD_UNSIGNED);
862 if (width == 1 && is_signed) {
863 // Valid values are either {-1;0} or {0}, depending on integer
864 // representation. The latter makes for very efficient code...
865 warning(token->pos, "dubious one-bit signed bitfield");
867 if (Wdefault_bitfield_sign &&
868 base_type->type != SYM_ENUM &&
869 !(base_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
870 is_signed) {
871 // The sign of bitfields is unspecified by default.
872 warning (token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
875 bitfield->bit_size = width;
876 return token;
879 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
881 while (!match_op(token, '}')) {
882 struct ctype ctype = {0, };
884 token = declaration_specifiers(token, &ctype, 0);
885 for (;;) {
886 struct ident *ident = NULL;
887 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
888 decl->ctype = ctype;
889 token = declarator(token, decl, &ident);
890 decl->ident = ident;
891 if (match_op(token, ':')) {
892 token = handle_bitfield(token, decl);
893 token = handle_attributes(token, &decl->ctype);
895 add_symbol(list, decl);
896 if (!match_op(token, ','))
897 break;
898 token = token->next;
900 if (!match_op(token, ';')) {
901 warning(token->pos, "expected ; at end of declaration");
902 break;
904 token = token->next;
906 return token;
909 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
911 struct ident *ident = NULL;
912 struct symbol *sym;
913 struct ctype ctype = { 0, };
915 token = declaration_specifiers(token, &ctype, 0);
916 sym = alloc_symbol(token->pos, SYM_NODE);
917 sym->ctype = ctype;
918 *tree = sym;
919 token = declarator(token, sym, &ident);
920 sym->ident = ident;
921 return token;
924 struct token *typename(struct token *token, struct symbol **p)
926 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
927 *p = sym;
928 token = declaration_specifiers(token, &sym->ctype, 0);
929 return declarator(token, sym, NULL);
932 struct token *expression_statement(struct token *token, struct expression **tree)
934 token = parse_expression(token, tree);
935 return expect(token, ';', "at end of statement");
938 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
939 struct expression_list **inout)
941 struct expression *expr;
943 /* Allow empty operands */
944 if (match_op(token->next, ':') || match_op(token->next, ')'))
945 return token->next;
946 do {
947 if (match_op(token->next, '[') &&
948 token_type(token->next->next) == TOKEN_IDENT &&
949 match_op(token->next->next->next, ']'))
950 token = token->next->next->next;
951 token = primary_expression(token->next, &expr);
952 add_expression(inout, expr);
953 token = parens_expression(token, &expr, "in asm parameter");
954 add_expression(inout, expr);
955 } while (match_op(token, ','));
956 return token;
959 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
960 struct expression_list **clobbers)
962 struct expression *expr;
964 do {
965 token = primary_expression(token->next, &expr);
966 add_expression(clobbers, expr);
967 } while (match_op(token, ','));
968 return token;
971 static struct token *parse_asm(struct token *token, struct statement *stmt)
973 stmt->type = STMT_ASM;
974 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
975 token = token->next;
977 token = expect(token, '(', "after asm");
978 token = parse_expression(token, &stmt->asm_string);
979 if (match_op(token, ':'))
980 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
981 if (match_op(token, ':'))
982 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
983 if (match_op(token, ':'))
984 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
985 token = expect(token, ')', "after asm");
986 return expect(token, ';', "at end of asm-statement");
989 /* Make a statement out of an expression */
990 static struct statement *make_statement(struct expression *expr)
992 struct statement *stmt;
994 if (!expr)
995 return NULL;
996 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
997 stmt->expression = expr;
998 return stmt;
1002 * All iterators have two symbols associated with them:
1003 * the "continue" and "break" symbols, which are targets
1004 * for continue and break statements respectively.
1006 * They are in a special name-space, but they follow
1007 * all the normal visibility rules, so nested iterators
1008 * automatically work right.
1010 static void start_iterator(struct statement *stmt)
1012 struct symbol *cont, *brk;
1014 start_symbol_scope();
1015 cont = alloc_symbol(stmt->pos, SYM_NODE);
1016 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1017 brk = alloc_symbol(stmt->pos, SYM_NODE);
1018 bind_symbol(brk, &break_ident, NS_ITERATOR);
1020 stmt->type = STMT_ITERATOR;
1021 stmt->iterator_break = brk;
1022 stmt->iterator_continue = cont;
1023 fn_local_symbol(brk);
1024 fn_local_symbol(cont);
1027 static void end_iterator(struct statement *stmt)
1029 end_symbol_scope();
1032 static struct statement *start_function(struct symbol *sym)
1034 struct symbol *ret;
1035 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1037 start_function_scope();
1038 ret = alloc_symbol(sym->pos, SYM_NODE);
1039 ret->ctype = sym->ctype.base_type->ctype;
1040 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1041 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1042 bind_symbol(ret, &return_ident, NS_ITERATOR);
1043 stmt->ret = ret;
1044 fn_local_symbol(ret);
1046 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1047 current_fn = sym;
1049 return stmt;
1052 static void end_function(struct symbol *sym)
1054 current_fn = NULL;
1055 end_function_scope();
1059 * A "switch()" statement, like an iterator, has a
1060 * the "break" symbol associated with it. It works
1061 * exactly like the iterator break - it's the target
1062 * for any break-statements in scope, and means that
1063 * "break" handling doesn't even need to know whether
1064 * it's breaking out of an iterator or a switch.
1066 * In addition, the "case" symbol is a marker for the
1067 * case/default statements to find the switch statement
1068 * that they are associated with.
1070 static void start_switch(struct statement *stmt)
1072 struct symbol *brk, *switch_case;
1074 start_symbol_scope();
1075 brk = alloc_symbol(stmt->pos, SYM_NODE);
1076 bind_symbol(brk, &break_ident, NS_ITERATOR);
1078 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1079 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1080 switch_case->stmt = stmt;
1082 stmt->type = STMT_SWITCH;
1083 stmt->switch_break = brk;
1084 stmt->switch_case = switch_case;
1086 fn_local_symbol(brk);
1087 fn_local_symbol(switch_case);
1090 static void end_switch(struct statement *stmt)
1092 if (!stmt->switch_case->symbol_list)
1093 warning(stmt->pos, "switch with no cases");
1094 end_symbol_scope();
1097 static void add_case_statement(struct statement *stmt)
1099 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1100 struct symbol *sym;
1102 if (!target) {
1103 warning(stmt->pos, "not in switch scope");
1104 stmt->type = STMT_NONE;
1105 return;
1107 sym = alloc_symbol(stmt->pos, SYM_NODE);
1108 add_symbol(&target->symbol_list, sym);
1109 sym->stmt = stmt;
1110 stmt->case_label = sym;
1111 fn_local_symbol(sym);
1114 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1116 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1118 if (!target)
1119 error_die(token->pos, "internal error: return without a function target");
1120 stmt->type = STMT_RETURN;
1121 stmt->ret_target = target;
1122 return expression_statement(token->next, &stmt->ret_value);
1125 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1127 struct symbol_list *syms;
1128 struct expression *e1, *e2, *e3;
1129 struct statement *iterator;
1131 start_iterator(stmt);
1132 token = expect(token->next, '(', "after 'for'");
1134 syms = NULL;
1135 e1 = NULL;
1136 /* C99 variable declaration? */
1137 if (lookup_type(token)) {
1138 token = external_declaration(token, &syms);
1139 } else {
1140 token = parse_expression(token, &e1);
1141 token = expect(token, ';', "in 'for'");
1143 token = parse_expression(token, &e2);
1144 token = expect(token, ';', "in 'for'");
1145 token = parse_expression(token, &e3);
1146 token = expect(token, ')', "in 'for'");
1147 token = statement(token, &iterator);
1149 stmt->iterator_syms = syms;
1150 stmt->iterator_pre_statement = make_statement(e1);
1151 stmt->iterator_pre_condition = e2;
1152 stmt->iterator_post_statement = make_statement(e3);
1153 stmt->iterator_post_condition = e2;
1154 stmt->iterator_statement = iterator;
1155 end_iterator(stmt);
1157 return token;
1160 struct token *parse_while_statement(struct token *token, struct statement *stmt)
1162 struct expression *expr;
1163 struct statement *iterator;
1165 start_iterator(stmt);
1166 token = parens_expression(token->next, &expr, "after 'while'");
1167 token = statement(token, &iterator);
1169 stmt->iterator_pre_condition = expr;
1170 stmt->iterator_post_condition = expr;
1171 stmt->iterator_statement = iterator;
1172 end_iterator(stmt);
1174 return token;
1177 struct token *parse_do_statement(struct token *token, struct statement *stmt)
1179 struct expression *expr;
1180 struct statement *iterator;
1182 start_iterator(stmt);
1183 token = statement(token->next, &iterator);
1184 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1185 token = token->next;
1186 else
1187 warning(token->pos, "expected 'while' after 'do'");
1188 token = parens_expression(token, &expr, "after 'do-while'");
1190 stmt->iterator_post_condition = expr;
1191 stmt->iterator_statement = iterator;
1192 end_iterator(stmt);
1194 return expect(token, ';', "after statement");
1197 static struct token *statement(struct token *token, struct statement **tree)
1199 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1201 *tree = stmt;
1202 if (token_type(token) == TOKEN_IDENT) {
1203 if (token->ident == &if_ident) {
1204 stmt->type = STMT_IF;
1205 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1206 token = statement(token, &stmt->if_true);
1207 if (token_type(token) != TOKEN_IDENT)
1208 return token;
1209 if (token->ident != &else_ident)
1210 return token;
1211 return statement(token->next, &stmt->if_false);
1214 if (token->ident == &return_ident)
1215 return parse_return_statement(token, stmt);
1217 if (token->ident == &break_ident || token->ident == &continue_ident) {
1218 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1219 stmt->type = STMT_GOTO;
1220 stmt->goto_label = target;
1221 if (!target)
1222 warning(stmt->pos, "break/continue not in iterator scope");
1223 return expect(token->next, ';', "at end of statement");
1225 if (token->ident == &default_ident) {
1226 token = token->next;
1227 goto default_statement;
1229 if (token->ident == &case_ident) {
1230 token = parse_expression(token->next, &stmt->case_expression);
1231 if (match_op(token, SPECIAL_ELLIPSIS))
1232 token = parse_expression(token->next, &stmt->case_to);
1233 default_statement:
1234 stmt->type = STMT_CASE;
1235 token = expect(token, ':', "after default/case");
1236 add_case_statement(stmt);
1237 return statement(token, &stmt->case_statement);
1239 if (token->ident == &switch_ident) {
1240 stmt->type = STMT_SWITCH;
1241 start_switch(stmt);
1242 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1243 token = statement(token, &stmt->switch_statement);
1244 end_switch(stmt);
1245 return token;
1247 if (token->ident == &for_ident)
1248 return parse_for_statement(token, stmt);
1250 if (token->ident == &while_ident)
1251 return parse_while_statement(token, stmt);
1253 if (token->ident == &do_ident)
1254 return parse_do_statement(token, stmt);
1256 if (token->ident == &goto_ident) {
1257 stmt->type = STMT_GOTO;
1258 token = token->next;
1259 if (match_op(token, '*')) {
1260 token = parse_expression(token->next, &stmt->goto_expression);
1261 add_statement(&function_computed_goto_list, stmt);
1262 } else if (token_type(token) == TOKEN_IDENT) {
1263 stmt->goto_label = label_symbol(token);
1264 token = token->next;
1265 } else {
1266 warning(token->pos, "Expected identifier or goto expression");
1268 return expect(token, ';', "at end of statement");
1270 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1271 return parse_asm(token->next, stmt);
1273 if (token->ident == &__context___ident) {
1274 stmt->type = STMT_INTERNAL;
1275 token = parse_expression(token->next, &stmt->expression);
1276 return expect(token, ';', "at end of statement");
1278 if (match_op(token->next, ':')) {
1279 stmt->type = STMT_LABEL;
1280 stmt->label_identifier = label_symbol(token);
1281 return statement(token->next->next, &stmt->label_statement);
1285 if (match_op(token, '{')) {
1286 stmt->type = STMT_COMPOUND;
1287 start_symbol_scope();
1288 token = compound_statement(token->next, stmt);
1289 end_symbol_scope();
1291 return expect(token, '}', "at end of compound statement");
1294 stmt->type = STMT_EXPRESSION;
1295 return expression_statement(token, &stmt->expression);
1298 static struct token * statement_list(struct token *token, struct statement_list **list, struct symbol_list **syms)
1300 for (;;) {
1301 struct statement * stmt;
1302 if (eof_token(token))
1303 break;
1304 if (match_op(token, '}'))
1305 break;
1306 if (lookup_type(token)) {
1307 if (warn_on_mixed && *list)
1308 warning(token->pos, "mixing declarations and code");
1309 token = external_declaration(token, syms);
1310 continue;
1312 token = statement(token, &stmt);
1313 add_statement(list, stmt);
1315 return token;
1318 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
1320 struct symbol_list **list = &fn->arguments;
1322 if (match_op(token, ')')) {
1323 // No warning for "void oink ();"
1324 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1325 if (!match_op(token->next, ';'))
1326 warning(token->pos, "non-ANSI function declaration");
1327 return token;
1330 for (;;) {
1331 struct symbol *sym;
1333 if (match_op(token, SPECIAL_ELLIPSIS)) {
1334 if (!*list)
1335 warning(token->pos, "variadic functions must have one named argument");
1336 fn->variadic = 1;
1337 token = token->next;
1338 break;
1341 sym = alloc_symbol(token->pos, SYM_NODE);
1342 token = parameter_declaration(token, &sym);
1343 if (sym->ctype.base_type == &void_ctype) {
1344 /* Special case: (void) */
1345 if (!*list && !sym->ident)
1346 break;
1347 warning(token->pos, "void parameter");
1349 add_symbol(list, sym);
1350 if (!match_op(token, ','))
1351 break;
1352 token = token->next;
1355 return token;
1358 struct token *compound_statement(struct token *token, struct statement *stmt)
1360 token = statement_list(token, &stmt->stmts, &stmt->syms);
1361 return token;
1364 static struct expression *identifier_expression(struct token *token)
1366 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1367 expr->expr_ident = token->ident;
1368 return expr;
1371 static struct expression *index_expression(struct expression *from, struct expression *to)
1373 int idx_from, idx_to;
1374 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1376 idx_from = get_expression_value(from);
1377 idx_to = idx_from;
1378 if (to) {
1379 idx_to = get_expression_value(to);
1380 if (idx_to < idx_from || idx_from < 0)
1381 warning(from->pos, "nonsense array initializer index range");
1383 expr->idx_from = idx_from;
1384 expr->idx_to = idx_to;
1385 return expr;
1388 static struct token *single_initializer(struct expression **ep, struct token *token)
1390 int expect_equal = 0;
1391 struct token *next = token->next;
1392 struct expression **tail = ep;
1393 int nested;
1395 *ep = NULL;
1397 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1398 struct expression *expr = identifier_expression(token);
1399 warning(token->pos, "obsolete struct initializer, use C99 syntax");
1400 token = initializer(&expr->ident_expression, next->next);
1401 if (expr->ident_expression)
1402 *ep = expr;
1403 return token;
1406 for (tail = ep, nested = 0; ; nested++, next = token->next) {
1407 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
1408 struct expression *expr = identifier_expression(next);
1409 *tail = expr;
1410 tail = &expr->ident_expression;
1411 expect_equal = 1;
1412 token = next->next;
1413 } else if (match_op(token, '[')) {
1414 struct expression *from = NULL, *to = NULL, *expr;
1415 token = constant_expression(token->next, &from);
1416 if (match_op(token, SPECIAL_ELLIPSIS))
1417 token = constant_expression(token->next, &to);
1418 expr = index_expression(from, to);
1419 *tail = expr;
1420 tail = &expr->idx_expression;
1421 token = expect(token, ']', "at end of initializer index");
1422 if (nested)
1423 expect_equal = 1;
1424 } else {
1425 break;
1428 if (nested && !expect_equal) {
1429 if (!match_op(token, '='))
1430 warning(token->pos, "obsolete array initializer, use C99 syntax");
1431 else
1432 expect_equal = 1;
1434 if (expect_equal)
1435 token = expect(token, '=', "at end of initializer index");
1437 token = initializer(tail, token);
1438 if (!*tail)
1439 *ep = NULL;
1440 return token;
1443 static struct token *initializer_list(struct expression_list **list, struct token *token)
1445 struct expression *expr;
1447 for (;;) {
1448 token = single_initializer(&expr, token);
1449 if (!expr)
1450 break;
1451 add_expression(list, expr);
1452 if (!match_op(token, ','))
1453 break;
1454 token = token->next;
1456 return token;
1459 struct token *initializer(struct expression **tree, struct token *token)
1461 if (match_op(token, '{')) {
1462 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1463 *tree = expr;
1464 token = initializer_list(&expr->expr_list, token->next);
1465 return expect(token, '}', "at end of initializer");
1467 return assignment_expression(token, tree);
1470 static void declare_argument(struct symbol *sym, struct symbol *fn)
1472 if (!sym->ident) {
1473 warning(sym->pos, "no identifier for function argument");
1474 return;
1476 bind_symbol(sym, sym->ident, NS_SYMBOL);
1479 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1480 struct symbol_list **list)
1482 struct symbol_list **old_symbol_list;
1483 struct symbol *base_type = decl->ctype.base_type;
1484 struct statement *stmt, **p;
1485 struct symbol *arg;
1487 old_symbol_list = function_symbol_list;
1488 if (decl->ctype.modifiers & MOD_INLINE) {
1489 function_symbol_list = &decl->inline_symbol_list;
1490 p = &base_type->inline_stmt;
1491 } else {
1492 function_symbol_list = &decl->symbol_list;
1493 p = &base_type->stmt;
1495 function_computed_target_list = NULL;
1496 function_computed_goto_list = NULL;
1498 if (decl->ctype.modifiers & MOD_EXTERN) {
1499 if (!(decl->ctype.modifiers & MOD_INLINE))
1500 warning(decl->pos, "function with external linkage has definition");
1502 if (!(decl->ctype.modifiers & MOD_STATIC))
1503 decl->ctype.modifiers |= MOD_EXTERN;
1505 stmt = start_function(decl);
1507 *p = stmt;
1508 FOR_EACH_PTR (base_type->arguments, arg) {
1509 declare_argument(arg, base_type);
1510 } END_FOR_EACH_PTR(arg);
1512 token = compound_statement(token->next, stmt);
1514 end_function(decl);
1515 if (!(decl->ctype.modifiers & MOD_INLINE))
1516 add_symbol(list, decl);
1517 check_declaration(decl);
1518 function_symbol_list = old_symbol_list;
1519 if (function_computed_goto_list) {
1520 if (!function_computed_target_list)
1521 warning(decl->pos, "function has computed goto but no targets?");
1522 else {
1523 struct statement *stmt;
1524 FOR_EACH_PTR(function_computed_goto_list, stmt) {
1525 stmt->target_list = function_computed_target_list;
1526 } END_FOR_EACH_PTR(stmt);
1529 return expect(token, '}', "at end of function");
1532 static void promote_k_r_types(struct symbol *arg)
1534 struct symbol *base = arg->ctype.base_type;
1535 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
1536 arg->ctype.base_type = &int_ctype;
1540 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
1542 struct symbol_list *real_args = fn->ctype.base_type->arguments;
1543 struct symbol *arg;
1545 FOR_EACH_PTR(real_args, arg) {
1546 struct symbol *type;
1548 /* This is quadratic in the number of arguments. We _really_ don't care */
1549 FOR_EACH_PTR(argtypes, type) {
1550 if (type->ident == arg->ident)
1551 goto match;
1552 } END_FOR_EACH_PTR(type);
1553 warning(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
1554 continue;
1555 match:
1556 type->used = 1;
1557 /* "char" and "short" promote to "int" */
1558 promote_k_r_types(type);
1560 arg->ctype = type->ctype;
1561 } END_FOR_EACH_PTR(arg);
1563 FOR_EACH_PTR(argtypes, arg) {
1564 if (!arg->used)
1565 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
1566 } END_FOR_EACH_PTR(arg);
1570 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
1571 struct symbol_list **list)
1573 struct symbol_list *args = NULL;
1575 warning(token->pos, "non-ANSI function declaration");
1576 do {
1577 token = external_declaration(token, &args);
1578 } while (lookup_type(token));
1580 apply_k_r_types(args, decl);
1582 if (!match_op(token, '{')) {
1583 warning(token->pos, "expected function body");
1584 return token;
1586 return parse_function_body(token, decl, list);
1590 static struct token *external_declaration(struct token *token, struct symbol_list **list)
1592 struct ident *ident = NULL;
1593 struct symbol *decl;
1594 struct ctype ctype = { 0, };
1595 struct symbol *base_type;
1596 int is_typedef;
1598 /* Top-level inline asm? */
1599 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1600 struct symbol_list **old_symbol_list;
1601 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
1602 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
1603 struct statement *stmt;
1605 anon->ctype.base_type = fn;
1606 old_symbol_list = function_symbol_list;
1607 function_symbol_list = &anon->symbol_list;
1608 stmt = start_function(anon);
1609 token = parse_asm(token->next, stmt);
1610 end_function(anon);
1611 function_symbol_list = old_symbol_list;
1612 add_symbol(list, anon);
1613 return token;
1616 /* Parse declaration-specifiers, if any */
1617 token = declaration_specifiers(token, &ctype, 0);
1618 decl = alloc_symbol(token->pos, SYM_NODE);
1619 decl->ctype = ctype;
1620 token = declarator(token, decl, &ident);
1622 /* Just a type declaration? */
1623 if (!ident)
1624 return expect(token, ';', "end of type declaration");
1626 /* type define declaration? */
1627 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
1629 /* Typedef's don't have meaningful storage */
1630 if (is_typedef) {
1631 ctype.modifiers &= ~MOD_STORAGE;
1632 decl->ctype.modifiers &= ~MOD_STORAGE;
1633 decl->ctype.modifiers |= MOD_USERTYPE;
1636 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1638 base_type = decl->ctype.base_type;
1639 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1640 /* K&R argument declaration? */
1641 if (lookup_type(token))
1642 return parse_k_r_arguments(token, decl, list);
1643 if (match_op(token, '{'))
1644 return parse_function_body(token, decl, list);
1646 if (!(decl->ctype.modifiers & MOD_STATIC))
1647 decl->ctype.modifiers |= MOD_EXTERN;
1648 } else if (!is_typedef && base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
1649 warning(token->pos, "void declaration");
1652 for (;;) {
1653 if (!is_typedef && match_op(token, '=')) {
1654 if (decl->ctype.modifiers & MOD_EXTERN) {
1655 warning(decl->pos, "symbol with external linkage has initializer");
1656 decl->ctype.modifiers &= ~MOD_EXTERN;
1658 token = initializer(&decl->initializer, token->next);
1660 if (!is_typedef) {
1661 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
1662 add_symbol(list, decl);
1663 fn_local_symbol(decl);
1666 check_declaration(decl);
1668 if (!match_op(token, ','))
1669 break;
1671 token = token->next;
1672 ident = NULL;
1673 decl = alloc_symbol(token->pos, SYM_NODE);
1674 decl->ctype = ctype;
1675 token = declaration_specifiers(token, &decl->ctype, 1);
1676 token = declarator(token, decl, &ident);
1677 if (!ident) {
1678 warning(token->pos, "expected identifier name in type definition");
1679 return token;
1682 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1684 /* Function declarations are automatically extern unless specifically static */
1685 base_type = decl->ctype.base_type;
1686 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1687 if (!(decl->ctype.modifiers & MOD_STATIC))
1688 decl->ctype.modifiers |= MOD_EXTERN;
1691 return expect(token, ';', "at end of declaration");
1694 struct symbol_list *translation_unit(struct token *token)
1696 while (!eof_token(token))
1697 token = external_declaration(token, &translation_unit_used_list);
1698 // They aren't needed any more
1699 clear_token_alloc();
1701 /* Evaluate the symbol list */
1702 evaluate_symbol_list(translation_unit_used_list);
1703 return translation_unit_used_list;