Make our "__builtin_va_arg()" thing a bit closer to real.
[smatch.git] / parse.c
blob1556e69cbba97b0db36b46f22154be7e5c9c0f3b
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 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 "token.h"
23 #include "parse.h"
24 #include "symbol.h"
25 #include "scope.h"
26 #include "expression.h"
27 #include "target.h"
29 static struct symbol_list **function_symbol_list;
30 struct symbol_list *function_computed_target_list;
31 struct statement_list *function_computed_goto_list;
33 // Add a symbol to the list of function-local symbols
34 #define fn_local_symbol(x) add_symbol(function_symbol_list, (x))
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 static int match_idents(struct token *token, ...)
41 va_list args;
43 if (token_type(token) != TOKEN_IDENT)
44 return 0;
46 va_start(args, token);
47 for (;;) {
48 struct ident * next = va_arg(args, struct ident *);
49 if (!next)
50 return 0;
51 if (token->ident == next)
52 return 1;
57 struct statement *alloc_statement(struct position pos, int type)
59 struct statement *stmt = __alloc_statement(0);
60 stmt->type = type;
61 stmt->pos = pos;
62 return stmt;
65 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
67 static struct symbol * indirect(struct position pos, struct ctype *ctype, int type)
69 struct symbol *sym = alloc_symbol(pos, type);
71 sym->ctype.base_type = ctype->base_type;
72 sym->ctype.modifiers = ctype->modifiers & ~MOD_STORAGE;
74 ctype->base_type = sym;
75 ctype->modifiers &= MOD_STORAGE;
76 return sym;
79 static struct symbol *lookup_or_create_symbol(enum namespace ns, enum type type, struct token *token)
81 struct symbol *sym = lookup_symbol(token->ident, ns);
82 if (!sym) {
83 sym = alloc_symbol(token->pos, type);
84 bind_symbol(sym, token->ident, ns);
85 if (type == SYM_LABEL)
86 fn_local_symbol(sym);
88 return sym;
92 * NOTE! NS_LABEL is not just a different namespace,
93 * it also ends up using function scope instead of the
94 * regular symbol scope.
96 struct symbol *label_symbol(struct token *token)
98 return lookup_or_create_symbol(NS_LABEL, SYM_LABEL, token);
101 struct token *struct_union_enum_specifier(enum type type,
102 struct token *token, struct ctype *ctype,
103 struct token *(*parse)(struct token *, struct symbol *))
105 struct symbol *sym;
107 ctype->modifiers = 0;
108 if (token_type(token) == TOKEN_IDENT) {
109 sym = lookup_symbol(token->ident, NS_STRUCT);
110 if (!sym ||
111 (sym->scope != block_scope &&
112 (match_op(token->next,';') || match_op(token->next,'{')))) {
113 // Either a new symbol, or else an out-of-scope
114 // symbol being redefined.
115 sym = alloc_symbol(token->pos, type);
116 bind_symbol(sym, token->ident, NS_STRUCT);
118 if (sym->type != type)
119 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
120 token = token->next;
121 ctype->base_type = sym;
122 if (match_op(token, '{')) {
123 // The following test is actually wrong for empty
124 // structs, but (1) they are not C99, (2) gcc does
125 // the same thing, and (3) it's easier.
126 if (sym->symbol_list)
127 error_die(token->pos, "redefinition of %s", show_typename (sym));
128 token = parse(token->next, sym);
129 token = expect(token, '}', "at end of struct-union-enum-specifier");
131 return token;
134 // private struct/union/enum type
135 if (!match_op(token, '{')) {
136 warning(token->pos, "expected declaration");
137 ctype->base_type = &bad_ctype;
138 return token;
141 sym = alloc_symbol(token->pos, type);
142 token = parse(token->next, sym);
143 ctype->base_type = sym;
144 return expect(token, '}', "at end of specifier");
147 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
149 return struct_declaration_list(token, &sym->symbol_list);
152 struct token *struct_or_union_specifier(enum type type, struct token *token, struct ctype *ctype)
154 return struct_union_enum_specifier(type, token, ctype, parse_struct_declaration);
157 typedef struct {
158 int x;
159 unsigned long long y;
160 } Num;
162 static void upper_boundary(Num *n, Num *v)
164 if (n->x > v->x)
165 return;
166 if (n->x < v->x) {
167 *n = *v;
168 return;
170 if (n->y < v->y)
171 n->y = v->y;
174 static void lower_boundary(Num *n, Num *v)
176 if (n->x < v->x)
177 return;
178 if (n->x > v->x) {
179 *n = *v;
180 return;
182 if (n->y > v->y)
183 n->y = v->y;
186 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
188 int shift = type->bit_size;
189 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
191 if (!is_unsigned)
192 shift--;
193 if (upper->x == 0 && upper->y >> shift)
194 return 0;
195 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
196 return 1;
197 return 0;
200 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
202 unsigned long long lastval = 0;
203 struct symbol *ctype = NULL, *base_type = NULL;
204 Num upper = {-1, 0}, lower = {1, 0};
206 while (token_type(token) == TOKEN_IDENT) {
207 struct token *next = token->next;
208 struct symbol *sym;
210 sym = alloc_symbol(token->pos, SYM_ENUM);
211 bind_symbol(sym, token->ident, NS_SYMBOL);
213 if (match_op(next, '=')) {
214 struct expression *expr;
215 next = constant_expression(next->next, &expr);
216 lastval = get_expression_value(expr);
217 ctype = expr->ctype;
218 } else if (!ctype) {
219 ctype = &int_ctype;
220 } else if (is_int_type(ctype)) {
221 lastval++;
222 } else {
223 error_die(token->pos, "can't increment the last enum member");
226 sym->value = lastval;
227 sym->ctype.base_type = ctype;
229 if (base_type != &bad_ctype) {
230 if (ctype->type == SYM_NODE)
231 ctype = ctype->ctype.base_type;
232 if (ctype->type == SYM_ENUM)
233 ctype = ctype->ctype.base_type;
235 * base_type rules:
236 * - if all enum's are of the same type, then
237 * the base_type is that type (two first
238 * cases)
239 * - if enums are of different types, they
240 * all have to be integer types, and the
241 * base type is "int_ctype".
242 * - otherwise the base_type is "bad_ctype".
244 if (!base_type) {
245 base_type = ctype;
246 } else if (ctype == base_type) {
247 /* nothing */
248 } else if (is_int_type(base_type) && is_int_type(ctype)) {
249 base_type = &int_ctype;
250 } else
251 base_type = &bad_ctype;
253 if (is_int_type(base_type)) {
254 Num v = {.y = lastval};
255 if (ctype->ctype.modifiers & MOD_UNSIGNED)
256 v.x = 0;
257 else if ((long long)lastval >= 0)
258 v.x = 0;
259 else
260 v.x = -1;
261 upper_boundary(&upper, &v);
262 lower_boundary(&lower, &v);
264 token = next;
265 if (!match_op(token, ','))
266 break;
267 token = token->next;
269 if (!base_type)
270 base_type = &bad_ctype;
271 else if (!is_int_type(base_type))
272 base_type = base_type;
273 else if (type_is_ok(base_type, &upper, &lower))
274 base_type = base_type;
275 else if (type_is_ok(&int_ctype, &upper, &lower))
276 base_type = &int_ctype;
277 else if (type_is_ok(&uint_ctype, &upper, &lower))
278 base_type = &uint_ctype;
279 else if (type_is_ok(&long_ctype, &upper, &lower))
280 base_type = &long_ctype;
281 else if (type_is_ok(&ulong_ctype, &upper, &lower))
282 base_type = &ulong_ctype;
283 else if (type_is_ok(&llong_ctype, &upper, &lower))
284 base_type = &llong_ctype;
285 else if (type_is_ok(&ullong_ctype, &upper, &lower))
286 base_type = &ullong_ctype;
287 else
288 base_type = &bad_ctype;
289 parent->ctype.base_type = base_type;
290 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
291 return token;
294 struct token *enum_specifier(struct token *token, struct ctype *ctype)
296 return struct_union_enum_specifier(SYM_ENUM, token, ctype, parse_enum_declaration);
299 struct token *typeof_specifier(struct token *token, struct ctype *ctype)
301 struct symbol *sym;
303 if (!match_op(token, '(')) {
304 warning(token->pos, "expected '(' after typeof");
305 return token;
307 if (lookup_type(token->next)) {
308 token = typename(token->next, &sym);
309 *ctype = sym->ctype;
310 } else {
311 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
312 token = parse_expression(token->next, &typeof_sym->initializer);
314 ctype->modifiers = 0;
315 ctype->base_type = typeof_sym;
317 return expect(token, ')', "after typeof");
320 static const char * handle_attribute(struct ctype *ctype, struct ident *attribute, struct expression *expr)
322 if (attribute == &packed_ident ||
323 attribute == &__packed___ident) {
324 ctype->alignment = 1;
325 return NULL;
327 if (attribute == &aligned_ident ||
328 attribute == &__aligned___ident) {
329 int alignment = max_alignment;
330 if (expr)
331 alignment = get_expression_value(expr);
332 ctype->alignment = alignment;
333 return NULL;
335 if (attribute == &nocast_ident) {
336 ctype->modifiers |= MOD_NOCAST;
337 return NULL;
339 if (attribute == &noderef_ident) {
340 ctype->modifiers |= MOD_NODEREF;
341 return NULL;
343 if (attribute == &safe_ident) {
344 ctype->modifiers |= MOD_SAFE;
345 return NULL;
347 if (attribute == &force_ident) {
348 ctype->modifiers |= MOD_FORCE;
349 return NULL;
351 if (attribute == &bitwise_ident) {
352 if (Wbitwise)
353 ctype->modifiers |= MOD_BITWISE;
354 return NULL;
356 if (attribute == &address_space_ident) {
357 if (!expr)
358 return "expected address space number";
359 ctype->as = get_expression_value(expr);
360 return NULL;
362 if (attribute == &context_ident) {
363 if (expr && expr->type == EXPR_COMMA) {
364 int input = get_expression_value(expr->left);
365 int output = get_expression_value(expr->right);
366 ctype->in_context = input;
367 ctype->out_context = output;
368 return NULL;
370 return "expected context input/output values";
372 if (attribute == &mode_ident ||
373 attribute == &__mode___ident) {
374 if (expr && expr->type == EXPR_SYMBOL) {
375 struct ident *ident = expr->symbol_name;
378 * Match against __QI__/__HI__/__SI__/__DI__
380 * FIXME! This is broken - we don't actually get
381 * the type information updated properly at this
382 * stage for some reason.
384 if (ident == &__QI___ident ||
385 ident == &QI_ident) {
386 ctype->modifiers |= MOD_CHAR;
387 ctype->base_type = ctype_integer(ctype->modifiers);
388 return NULL;
390 if (ident == &__HI___ident ||
391 ident == &HI_ident) {
392 ctype->modifiers |= MOD_SHORT;
393 ctype->base_type = ctype_integer(ctype->modifiers);
394 return NULL;
396 if (ident == &__SI___ident ||
397 ident == &SI_ident) {
398 /* Nothing? */
399 return NULL;
401 if (ident == &__DI___ident ||
402 ident == &DI_ident) {
403 ctype->modifiers |= MOD_LONGLONG;
404 ctype->base_type = ctype_integer(ctype->modifiers);
405 return NULL;
407 if (ident == &__word___ident ||
408 ident == &word_ident) {
409 ctype->modifiers |= MOD_LONG;
410 ctype->base_type = ctype_integer(ctype->modifiers);
411 return NULL;
413 return "unknown mode attribute";
415 return "expected attribute mode symbol";
418 /* Throw away for now.. */
419 if (attribute == &format_ident ||
420 attribute == &__format___ident)
421 return NULL;
422 if (attribute == &section_ident ||
423 attribute == &__section___ident)
424 return NULL;
425 if (attribute == &unused_ident ||
426 attribute == &__unused___ident)
427 return NULL;
428 if (attribute == &const_ident ||
429 attribute == &__const_ident ||
430 attribute == &__const___ident)
431 return NULL;
432 if (attribute == &noreturn_ident ||
433 attribute == &__noreturn___ident)
434 return NULL;
435 if (attribute == &regparm_ident)
436 return NULL;
437 if (attribute == &weak_ident)
438 return NULL;
439 if (attribute == &alias_ident)
440 return NULL;
441 if (attribute == &pure_ident)
442 return NULL;
443 if (attribute == &always_inline_ident)
444 return NULL;
445 if (attribute == &syscall_linkage_ident)
446 return NULL;
447 if (attribute == &visibility_ident)
448 return NULL;
449 if (attribute == &model_ident ||
450 attribute == &__model___ident)
451 return NULL;
453 return "unknown attribute";
456 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
458 ctype->modifiers = 0;
459 token = expect(token, '(', "after attribute");
460 token = expect(token, '(', "after attribute");
462 for (;;) {
463 const char *error;
464 struct ident *attribute_name;
465 struct expression *attribute_expr;
467 if (eof_token(token))
468 break;
469 if (match_op(token, ';'))
470 break;
471 if (token_type(token) != TOKEN_IDENT)
472 break;
473 attribute_name = token->ident;
474 token = token->next;
475 attribute_expr = NULL;
476 if (match_op(token, '('))
477 token = parens_expression(token, &attribute_expr, "in attribute");
478 error = handle_attribute(ctype, attribute_name, attribute_expr);
479 if (error)
480 warning(token->pos, "attribute '%s': %s", show_ident(attribute_name), error);
481 if (!match_op(token, ','))
482 break;
483 token = token->next;
486 token = expect(token, ')', "after attribute");
487 token = expect(token, ')', "after attribute");
488 return token;
491 struct symbol * ctype_integer(unsigned long spec)
493 static struct symbol *const integer_ctypes[][3] = {
494 { &llong_ctype, &sllong_ctype, &ullong_ctype },
495 { &long_ctype, &slong_ctype, &ulong_ctype },
496 { &short_ctype, &sshort_ctype, &ushort_ctype },
497 { &char_ctype, &schar_ctype, &uchar_ctype },
498 { &int_ctype, &sint_ctype, &uint_ctype },
500 struct symbol *const (*ctype)[3];
501 int sub;
503 ctype = integer_ctypes;
504 if (!(spec & MOD_LONGLONG)) {
505 ctype++;
506 if (!(spec & MOD_LONG)) {
507 ctype++;
508 if (!(spec & MOD_SHORT)) {
509 ctype++;
510 if (!(spec & MOD_CHAR))
511 ctype++;
516 sub = ((spec & MOD_UNSIGNED)
518 : ((spec & MOD_EXPLICITLY_SIGNED)
520 : 0));
522 return ctype[0][sub];
525 struct symbol * ctype_fp(unsigned long spec)
527 if (spec & MOD_LONGLONG)
528 return &ldouble_ctype;
529 if (spec & MOD_LONG)
530 return &double_ctype;
531 return &float_ctype;
534 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
536 unsigned long mod = thistype->modifiers;
538 if (mod) {
539 unsigned long old = ctype->modifiers;
540 unsigned long extra = 0, dup, conflict;
542 if (mod & old & MOD_LONG) {
543 extra = MOD_LONGLONG | MOD_LONG;
544 mod &= ~MOD_LONG;
545 old &= ~MOD_LONG;
547 dup = (mod & old) | (extra & old) | (extra & mod);
548 if (dup)
549 warning(pos, "Just how %sdo you want this type to be?",
550 modifier_string(dup));
552 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
553 if (conflict)
554 warning(pos, "You cannot have both long and short modifiers.");
556 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
557 if (conflict)
558 warning(pos, "You cannot have both signed and unsigned modifiers.");
560 // Only one storage modifier allowed, except that "inline" doesn't count.
561 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
562 conflict &= (conflict - 1);
563 if (conflict)
564 warning(pos, "multiple storage classes");
566 ctype->modifiers = old | mod | extra;
569 /* Context mask and value */
570 ctype->in_context += thistype->in_context;
571 ctype->out_context += thistype->out_context;
573 /* Alignment */
574 if (thistype->alignment & (thistype->alignment-1)) {
575 warning(pos, "I don't like non-power-of-2 alignments");
576 thistype->alignment = 0;
578 if (thistype->alignment > ctype->alignment)
579 ctype->alignment = thistype->alignment;
581 /* Address space */
582 ctype->as = thistype->as;
585 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
587 unsigned long banned, wrong;
588 unsigned long this_mod = s->ctype.modifiers;
589 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
590 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
592 if (this_mod & (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF))
593 banned = BANNED_SIZE | BANNED_SIGN;
594 else if (this_mod & MOD_SPECIALBITS)
595 banned = 0;
596 else if (s->ctype.base_type == &fp_type)
597 banned = BANNED_SIGN;
598 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
599 banned = 0;
600 else {
601 // label_type
602 // void_type
603 // bad_type
604 // vector_type <-- whatever that is
605 banned = BANNED_SIZE | BANNED_SIGN;
608 wrong = mod & banned;
609 if (wrong)
610 warning(*pos, "modifier %sis invalid in this context",
611 modifier_string (wrong));
615 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
617 struct token *token;
619 while ( (token = next) != NULL ) {
620 struct ctype thistype;
621 struct ident *ident;
622 struct symbol *s, *type;
623 unsigned long mod;
625 next = token->next;
626 if (token_type(token) != TOKEN_IDENT)
627 break;
628 ident = token->ident;
630 s = lookup_symbol(ident, NS_TYPEDEF);
631 if (!s)
632 break;
633 thistype = s->ctype;
634 mod = thistype.modifiers;
635 if (qual && (mod & ~(MOD_ATTRIBUTE | MOD_CONST | MOD_VOLATILE)))
636 break;
637 if (mod & MOD_SPECIALBITS) {
638 if (mod & MOD_STRUCTOF)
639 next = struct_or_union_specifier(SYM_STRUCT, next, &thistype);
640 else if (mod & MOD_UNIONOF)
641 next = struct_or_union_specifier(SYM_UNION, next, &thistype);
642 else if (mod & MOD_ENUMOF)
643 next = enum_specifier(next, &thistype);
644 else if (mod & MOD_ATTRIBUTE)
645 next = attribute_specifier(next, &thistype);
646 else if (mod & MOD_TYPEOF)
647 next = typeof_specifier(next, &thistype);
648 mod = thistype.modifiers;
650 type = thistype.base_type;
651 if (type) {
652 if (qual)
653 break;
654 if (ctype->base_type)
655 break;
656 /* User types only mix with qualifiers */
657 if (mod & MOD_USERTYPE) {
658 if (ctype->modifiers & MOD_SPECIFIER)
659 break;
661 ctype->base_type = type;
664 check_modifiers(&token->pos, s, ctype->modifiers);
665 apply_ctype(token->pos, &thistype, ctype);
668 /* Turn the "virtual types" into real types with real sizes etc */
669 if (!ctype->base_type) {
670 struct symbol *base = &incomplete_ctype;
673 * If we have modifiers, we'll default to an integer
674 * type, and "ctype_integer()" will turn this into
675 * a specific one.
677 if (ctype->modifiers & MOD_SPECIFIER)
678 base = &int_type;
679 ctype->base_type = base;
681 if (ctype->base_type == &int_type) {
682 ctype->base_type = ctype_integer(ctype->modifiers);
683 ctype->modifiers &= ~MOD_SPECIFIER;
684 } else if (ctype->base_type == &fp_type) {
685 ctype->base_type = ctype_fp(ctype->modifiers);
686 ctype->modifiers &= ~MOD_SPECIFIER;
688 if (ctype->modifiers & MOD_BITWISE) {
689 struct symbol *type;
690 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
691 if (!is_int_type(ctype->base_type)) {
692 warning(token->pos, "invalid modifier");
693 return token;
695 type = alloc_symbol(token->pos, SYM_BASETYPE);
696 *type = *ctype->base_type;
697 type->ctype.base_type = ctype->base_type;
698 type->type = SYM_RESTRICT;
699 type->ctype.modifiers &= ~MOD_SPECIFIER;
700 ctype->base_type = type;
702 return token;
705 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
707 struct expression *expr = NULL;
709 token = parse_expression(token, &expr);
710 sym->array_size = expr;
711 return token;
714 static struct token *parameter_type_list(struct token *, struct symbol *);
715 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
717 static struct token *handle_attributes(struct token *token, struct ctype *ctype)
719 for (;;) {
720 if (token_type(token) != TOKEN_IDENT)
721 break;
722 if (match_idents(token, &__attribute___ident, &__attribute_ident, NULL)) {
723 struct ctype thistype = { 0, };
724 token = attribute_specifier(token->next, &thistype);
725 apply_ctype(token->pos, &thistype, ctype);
726 continue;
728 if (match_idents(token, &asm_ident, &__asm_ident, &__asm___ident)) {
729 struct expression *expr;
730 token = expect(token->next, '(', "after asm");
731 token = parse_expression(token->next, &expr);
732 token = expect(token, ')', "after asm");
733 continue;
735 break;
737 return token;
740 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
742 struct ctype *ctype = &decl->ctype;
744 if (p && token_type(token) == TOKEN_IDENT) {
745 *p = token->ident;
746 token = token->next;
749 for (;;) {
750 token = handle_attributes(token, ctype);
752 if (token_type(token) != TOKEN_SPECIAL)
753 return token;
756 * This can be either a parameter list or a grouping.
757 * For the direct (non-abstract) case, we know if must be
758 * a parameter list if we already saw the identifier.
759 * For the abstract case, we know if must be a parameter
760 * list if it is empty or starts with a type.
762 if (token->special == '(') {
763 struct symbol *sym;
764 struct token *next = token->next;
765 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
767 if (!fn) {
768 struct symbol *base_type = ctype->base_type;
769 token = declarator(next, decl, p);
770 token = expect(token, ')', "in nested declarator");
771 while (ctype->base_type != base_type)
772 ctype = &ctype->base_type->ctype;
773 p = NULL;
774 continue;
777 sym = indirect(token->pos, ctype, SYM_FN);
778 token = parameter_type_list(next, sym);
779 token = expect(token, ')', "in function declarator");
780 continue;
782 if (token->special == '[') {
783 struct symbol *array = indirect(token->pos, ctype, SYM_ARRAY);
784 token = abstract_array_declarator(token->next, array);
785 token = expect(token, ']', "in abstract_array_declarator");
786 ctype = &array->ctype;
787 continue;
789 break;
791 return token;
794 static struct token *pointer(struct token *token, struct ctype *ctype)
796 unsigned long modifiers;
797 struct symbol *base_type;
799 modifiers = ctype->modifiers & ~(MOD_TYPEDEF | MOD_ATTRIBUTE);
800 base_type = ctype->base_type;
801 ctype->modifiers = modifiers;
803 while (match_op(token,'*')) {
804 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
805 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
806 ptr->ctype.as = ctype->as;
807 ptr->ctype.in_context += ctype->in_context;
808 ptr->ctype.out_context += ctype->out_context;
809 ptr->ctype.base_type = base_type;
811 base_type = ptr;
812 ctype->modifiers = modifiers & MOD_STORAGE;
813 ctype->base_type = base_type;
814 ctype->as = 0;
815 ctype->in_context = 0;
816 ctype->out_context = 0;
818 token = declaration_specifiers(token->next, ctype, 1);
819 modifiers = ctype->modifiers;
821 return token;
824 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
826 token = pointer(token, &sym->ctype);
827 return direct_declarator(token, sym, p);
830 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
832 struct ctype *ctype = &decl->ctype;
833 struct expression *expr;
834 struct symbol *bitfield;
835 long long width;
837 if (!is_int_type(ctype->base_type)) {
838 warning(token->pos, "invalid bitfield specifier for type %s.",
839 show_typename(ctype->base_type));
840 // Parse this to recover gracefully.
841 return conditional_expression(token->next, &expr);
844 bitfield = indirect(token->pos, ctype, SYM_BITFIELD);
845 token = conditional_expression(token->next, &expr);
846 width = get_expression_value(expr);
847 bitfield->bit_size = width;
849 if (width < 0 || width > INT_MAX) {
850 warning(token->pos, "invalid bitfield width, %lld.", width);
851 width = -1;
852 } else if (decl->ident && width == 0) {
853 warning(token->pos, "invalid named zero-width bitfield `%s'",
854 show_ident(decl->ident));
855 width = -1;
856 } else if (decl->ident) {
857 struct symbol *base_type = bitfield->ctype.base_type;
858 int is_signed = !(base_type->ctype.modifiers & MOD_UNSIGNED);
859 if (width == 1 && is_signed) {
860 // Valid values are either {-1;0} or {0}, depending on integer
861 // representation. The latter makes for very efficient code...
862 warning(token->pos, "dubious one-bit signed bitfield");
864 if (Wdefault_bitfield_sign &&
865 base_type->type != SYM_ENUM &&
866 !(base_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
867 is_signed) {
868 // The sign of bitfields is unspecified by default.
869 warning (token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
872 bitfield->bit_size = width;
873 return token;
876 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
878 while (!match_op(token, '}')) {
879 struct ctype ctype = {0, };
881 token = declaration_specifiers(token, &ctype, 0);
882 for (;;) {
883 struct ident *ident = NULL;
884 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
885 decl->ctype = ctype;
886 token = declarator(token, decl, &ident);
887 decl->ident = ident;
888 if (match_op(token, ':')) {
889 token = handle_bitfield(token, decl);
890 token = handle_attributes(token, &decl->ctype);
892 add_symbol(list, decl);
893 if (!match_op(token, ','))
894 break;
895 token = token->next;
897 if (!match_op(token, ';')) {
898 warning(token->pos, "expected ; at end of declaration");
899 break;
901 token = token->next;
903 return token;
906 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
908 struct ident *ident = NULL;
909 struct symbol *sym;
910 struct ctype ctype = { 0, };
912 token = declaration_specifiers(token, &ctype, 0);
913 sym = alloc_symbol(token->pos, SYM_NODE);
914 sym->ctype = ctype;
915 *tree = sym;
916 token = declarator(token, sym, &ident);
917 sym->ident = ident;
918 return token;
921 struct token *typename(struct token *token, struct symbol **p)
923 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
924 *p = sym;
925 token = declaration_specifiers(token, &sym->ctype, 0);
926 return declarator(token, sym, NULL);
929 struct token *expression_statement(struct token *token, struct expression **tree)
931 token = parse_expression(token, tree);
932 return expect(token, ';', "at end of statement");
935 static struct token *parse_asm_operands(struct token *token, struct statement *stmt)
937 struct expression *expr;
939 /* Allow empty operands */
940 if (match_op(token->next, ':') || match_op(token->next, ')'))
941 return token->next;
942 do {
943 if (match_op(token->next, '[') &&
944 token_type(token->next->next) == TOKEN_IDENT &&
945 match_op(token->next->next->next, ']'))
946 token = token->next->next->next;
947 token = primary_expression(token->next, &expr);
948 token = parens_expression(token, &expr, "in asm parameter");
949 } while (match_op(token, ','));
950 return token;
953 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt)
955 struct expression *expr;
957 do {
958 token = primary_expression(token->next, &expr);
959 } while (match_op(token, ','));
960 return token;
963 static struct token *parse_asm(struct token *token, struct statement *stmt)
965 struct expression *expr;
967 stmt->type = STMT_ASM;
968 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
969 token = token->next;
971 token = expect(token, '(', "after asm");
972 token = parse_expression(token->next, &expr);
973 if (match_op(token, ':'))
974 token = parse_asm_operands(token, stmt);
975 if (match_op(token, ':'))
976 token = parse_asm_operands(token, stmt);
977 if (match_op(token, ':'))
978 token = parse_asm_clobbers(token, stmt);
979 token = expect(token, ')', "after asm");
980 return expect(token, ';', "at end of asm-statement");
983 /* Make a statement out of an expression */
984 static struct statement *make_statement(struct expression *expr)
986 struct statement *stmt;
988 if (!expr)
989 return NULL;
990 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
991 stmt->expression = expr;
992 return stmt;
996 * All iterators have two symbols associated with them:
997 * the "continue" and "break" symbols, which are targets
998 * for continue and break statements respectively.
1000 * They are in a special name-space, but they follow
1001 * all the normal visibility rules, so nested iterators
1002 * automatically work right.
1004 static void start_iterator(struct statement *stmt)
1006 struct symbol *cont, *brk;
1008 start_symbol_scope();
1009 cont = alloc_symbol(stmt->pos, SYM_NODE);
1010 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1011 brk = alloc_symbol(stmt->pos, SYM_NODE);
1012 bind_symbol(brk, &break_ident, NS_ITERATOR);
1014 stmt->type = STMT_ITERATOR;
1015 stmt->iterator_break = brk;
1016 stmt->iterator_continue = cont;
1017 fn_local_symbol(brk);
1018 fn_local_symbol(cont);
1021 static void end_iterator(struct statement *stmt)
1023 end_symbol_scope();
1026 static struct statement *start_function(struct symbol *sym)
1028 struct symbol *ret;
1029 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1031 start_function_scope();
1032 ret = alloc_symbol(sym->pos, SYM_NODE);
1033 ret->ctype = sym->ctype.base_type->ctype;
1034 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1035 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1036 bind_symbol(ret, &return_ident, NS_ITERATOR);
1037 stmt->ret = ret;
1038 fn_local_symbol(ret);
1040 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1041 current_fn = sym;
1043 return stmt;
1046 static void end_function(struct symbol *sym)
1048 current_fn = NULL;
1049 end_function_scope();
1053 * A "switch()" statement, like an iterator, has a
1054 * the "break" symbol associated with it. It works
1055 * exactly like the iterator break - it's the target
1056 * for any break-statements in scope, and means that
1057 * "break" handling doesn't even need to know whether
1058 * it's breaking out of an iterator or a switch.
1060 * In addition, the "case" symbol is a marker for the
1061 * case/default statements to find the switch statement
1062 * that they are associated with.
1064 static void start_switch(struct statement *stmt)
1066 struct symbol *brk, *switch_case;
1068 start_symbol_scope();
1069 brk = alloc_symbol(stmt->pos, SYM_NODE);
1070 bind_symbol(brk, &break_ident, NS_ITERATOR);
1072 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1073 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1074 switch_case->stmt = stmt;
1076 stmt->type = STMT_SWITCH;
1077 stmt->switch_break = brk;
1078 stmt->switch_case = switch_case;
1080 fn_local_symbol(brk);
1081 fn_local_symbol(switch_case);
1084 static void end_switch(struct statement *stmt)
1086 if (!stmt->switch_case->symbol_list)
1087 warning(stmt->pos, "switch with no cases");
1088 end_symbol_scope();
1091 static void add_case_statement(struct statement *stmt)
1093 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1094 struct symbol *sym;
1096 if (!target) {
1097 warning(stmt->pos, "not in switch scope");
1098 stmt->type = STMT_NONE;
1099 return;
1101 sym = alloc_symbol(stmt->pos, SYM_NODE);
1102 add_symbol(&target->symbol_list, sym);
1103 sym->stmt = stmt;
1104 stmt->case_label = sym;
1105 fn_local_symbol(sym);
1108 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1110 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1112 if (!target)
1113 error_die(token->pos, "internal error: return without a function target");
1114 stmt->type = STMT_RETURN;
1115 stmt->ret_target = target;
1116 return expression_statement(token->next, &stmt->ret_value);
1119 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1121 struct symbol_list *syms;
1122 struct expression *e1, *e2, *e3;
1123 struct statement *iterator;
1125 start_iterator(stmt);
1126 token = expect(token->next, '(', "after 'for'");
1128 syms = NULL;
1129 e1 = NULL;
1130 /* C99 variable declaration? */
1131 if (lookup_type(token)) {
1132 token = external_declaration(token, &syms);
1133 } else {
1134 token = parse_expression(token, &e1);
1135 token = expect(token, ';', "in 'for'");
1137 token = parse_expression(token, &e2);
1138 token = expect(token, ';', "in 'for'");
1139 token = parse_expression(token, &e3);
1140 token = expect(token, ')', "in 'for'");
1141 token = statement(token, &iterator);
1143 stmt->iterator_syms = syms;
1144 stmt->iterator_pre_statement = make_statement(e1);
1145 stmt->iterator_pre_condition = e2;
1146 stmt->iterator_post_statement = make_statement(e3);
1147 stmt->iterator_post_condition = e2;
1148 stmt->iterator_statement = iterator;
1149 end_iterator(stmt);
1151 return token;
1154 struct token *parse_while_statement(struct token *token, struct statement *stmt)
1156 struct expression *expr;
1157 struct statement *iterator;
1159 start_iterator(stmt);
1160 token = parens_expression(token->next, &expr, "after 'while'");
1161 token = statement(token, &iterator);
1163 stmt->iterator_pre_condition = expr;
1164 stmt->iterator_post_condition = expr;
1165 stmt->iterator_statement = iterator;
1166 end_iterator(stmt);
1168 return token;
1171 struct token *parse_do_statement(struct token *token, struct statement *stmt)
1173 struct expression *expr;
1174 struct statement *iterator;
1176 start_iterator(stmt);
1177 token = statement(token->next, &iterator);
1178 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1179 token = token->next;
1180 else
1181 warning(token->pos, "expected 'while' after 'do'");
1182 token = parens_expression(token, &expr, "after 'do-while'");
1184 stmt->iterator_post_condition = expr;
1185 stmt->iterator_statement = iterator;
1186 end_iterator(stmt);
1188 return expect(token, ';', "after statement");
1191 static struct token *statement(struct token *token, struct statement **tree)
1193 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1195 *tree = stmt;
1196 if (token_type(token) == TOKEN_IDENT) {
1197 if (token->ident == &if_ident) {
1198 stmt->type = STMT_IF;
1199 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1200 token = statement(token, &stmt->if_true);
1201 if (token_type(token) != TOKEN_IDENT)
1202 return token;
1203 if (token->ident != &else_ident)
1204 return token;
1205 return statement(token->next, &stmt->if_false);
1208 if (token->ident == &return_ident)
1209 return parse_return_statement(token, stmt);
1211 if (token->ident == &break_ident || token->ident == &continue_ident) {
1212 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1213 stmt->type = STMT_GOTO;
1214 stmt->goto_label = target;
1215 if (!target)
1216 warning(stmt->pos, "break/continue not in iterator scope");
1217 return expect(token->next, ';', "at end of statement");
1219 if (token->ident == &default_ident) {
1220 token = token->next;
1221 goto default_statement;
1223 if (token->ident == &case_ident) {
1224 token = parse_expression(token->next, &stmt->case_expression);
1225 if (match_op(token, SPECIAL_ELLIPSIS))
1226 token = parse_expression(token->next, &stmt->case_to);
1227 default_statement:
1228 stmt->type = STMT_CASE;
1229 token = expect(token, ':', "after default/case");
1230 add_case_statement(stmt);
1231 return statement(token, &stmt->case_statement);
1233 if (token->ident == &switch_ident) {
1234 stmt->type = STMT_SWITCH;
1235 start_switch(stmt);
1236 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1237 token = statement(token, &stmt->switch_statement);
1238 end_switch(stmt);
1239 return token;
1241 if (token->ident == &for_ident)
1242 return parse_for_statement(token, stmt);
1244 if (token->ident == &while_ident)
1245 return parse_while_statement(token, stmt);
1247 if (token->ident == &do_ident)
1248 return parse_do_statement(token, stmt);
1250 if (token->ident == &goto_ident) {
1251 stmt->type = STMT_GOTO;
1252 token = token->next;
1253 if (match_op(token, '*')) {
1254 token = parse_expression(token->next, &stmt->goto_expression);
1255 add_statement(&function_computed_goto_list, stmt);
1256 } else if (token_type(token) == TOKEN_IDENT) {
1257 stmt->goto_label = label_symbol(token);
1258 token = token->next;
1259 } else {
1260 warning(token->pos, "Expected identifier or goto expression");
1262 return expect(token, ';', "at end of statement");
1264 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1265 return parse_asm(token->next, stmt);
1267 if (token->ident == &__context___ident) {
1268 stmt->type = STMT_INTERNAL;
1269 token = parse_expression(token->next, &stmt->expression);
1270 return expect(token, ';', "at end of statement");
1272 if (match_op(token->next, ':')) {
1273 stmt->type = STMT_LABEL;
1274 stmt->label_identifier = label_symbol(token);
1275 return statement(token->next->next, &stmt->label_statement);
1279 if (match_op(token, '{')) {
1280 stmt->type = STMT_COMPOUND;
1281 start_symbol_scope();
1282 token = compound_statement(token->next, stmt);
1283 end_symbol_scope();
1285 return expect(token, '}', "at end of compound statement");
1288 stmt->type = STMT_EXPRESSION;
1289 return expression_statement(token, &stmt->expression);
1292 struct token * statement_list(struct token *token, struct statement_list **list)
1294 for (;;) {
1295 struct statement * stmt;
1296 if (eof_token(token))
1297 break;
1298 if (match_op(token, '}'))
1299 break;
1300 token = statement(token, &stmt);
1301 add_statement(list, stmt);
1303 return token;
1306 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
1308 struct symbol_list **list = &fn->arguments;
1310 if (match_op(token, ')')) {
1311 // No warning for "void oink ();"
1312 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1313 if (!match_op(token->next, ';'))
1314 warning(token->pos, "non-ANSI function declaration");
1315 return token;
1318 for (;;) {
1319 struct symbol *sym;
1321 if (match_op(token, SPECIAL_ELLIPSIS)) {
1322 if (!*list)
1323 warning(token->pos, "variadic functions must have one named argument");
1324 fn->variadic = 1;
1325 token = token->next;
1326 break;
1329 sym = alloc_symbol(token->pos, SYM_NODE);
1330 token = parameter_declaration(token, &sym);
1331 if (sym->ctype.base_type == &void_ctype) {
1332 /* Special case: (void) */
1333 if (!*list && !sym->ident)
1334 break;
1335 warning(token->pos, "void parameter");
1337 add_symbol(list, sym);
1338 if (!match_op(token, ','))
1339 break;
1340 token = token->next;
1343 return token;
1346 struct token *compound_statement(struct token *token, struct statement *stmt)
1348 while (!eof_token(token)) {
1349 if (!lookup_type(token))
1350 break;
1351 token = external_declaration(token, &stmt->syms);
1353 token = statement_list(token, &stmt->stmts);
1354 return token;
1357 static struct expression *identifier_expression(struct token *token)
1359 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1360 expr->expr_ident = token->ident;
1361 return expr;
1364 static struct expression *index_expression(struct expression *from, struct expression *to)
1366 int idx_from, idx_to;
1367 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1369 idx_from = get_expression_value(from);
1370 idx_to = idx_from;
1371 if (to) {
1372 idx_to = get_expression_value(to);
1373 if (idx_to < idx_from || idx_from < 0)
1374 warning(from->pos, "nonsense array initializer index range");
1376 expr->idx_from = idx_from;
1377 expr->idx_to = idx_to;
1378 return expr;
1381 static struct token *single_initializer(struct expression **ep, struct token *token)
1383 int expect_equal = 0;
1384 struct token *next = token->next;
1385 struct expression **tail = ep;
1386 int nested;
1388 *ep = NULL;
1390 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1391 struct expression *expr = identifier_expression(token);
1392 warning(token->pos, "obsolete struct initializer, use C99 syntax");
1393 token = initializer(&expr->ident_expression, next->next);
1394 if (expr->ident_expression)
1395 *ep = expr;
1396 return token;
1399 for (tail = ep, nested = 0; ; nested++, next = token->next) {
1400 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
1401 struct expression *expr = identifier_expression(next);
1402 *tail = expr;
1403 tail = &expr->ident_expression;
1404 expect_equal = 1;
1405 token = next->next;
1406 } else if (match_op(token, '[')) {
1407 struct expression *from = NULL, *to = NULL, *expr;
1408 token = constant_expression(token->next, &from);
1409 if (match_op(token, SPECIAL_ELLIPSIS))
1410 token = constant_expression(token->next, &to);
1411 expr = index_expression(from, to);
1412 *tail = expr;
1413 tail = &expr->idx_expression;
1414 token = expect(token, ']', "at end of initializer index");
1415 if (nested)
1416 expect_equal = 1;
1417 } else {
1418 break;
1421 if (nested && !expect_equal) {
1422 if (!match_op(token, '='))
1423 warning(token->pos, "obsolete array initializer, use C99 syntax");
1424 else
1425 expect_equal = 1;
1427 if (expect_equal)
1428 token = expect(token, '=', "at end of initializer index");
1430 token = initializer(tail, token);
1431 if (!*tail)
1432 *ep = NULL;
1433 return token;
1436 static struct token *initializer_list(struct expression_list **list, struct token *token)
1438 struct expression *expr;
1440 for (;;) {
1441 token = single_initializer(&expr, token);
1442 if (!expr)
1443 break;
1444 add_expression(list, expr);
1445 if (!match_op(token, ','))
1446 break;
1447 token = token->next;
1449 return token;
1452 struct token *initializer(struct expression **tree, struct token *token)
1454 if (match_op(token, '{')) {
1455 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1456 *tree = expr;
1457 token = initializer_list(&expr->expr_list, token->next);
1458 return expect(token, '}', "at end of initializer");
1460 return assignment_expression(token, tree);
1463 static void declare_argument(struct symbol *sym, struct symbol *fn)
1465 if (!sym->ident) {
1466 warning(sym->pos, "no identifier for function argument");
1467 return;
1469 bind_symbol(sym, sym->ident, NS_SYMBOL);
1472 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1473 struct symbol_list **list)
1475 struct symbol *base_type = decl->ctype.base_type;
1476 struct statement *stmt, **p;
1477 struct symbol *arg;
1479 if (decl->ctype.modifiers & MOD_INLINE) {
1480 function_symbol_list = &decl->inline_symbol_list;
1481 p = &base_type->inline_stmt;
1482 } else {
1483 function_symbol_list = &decl->symbol_list;
1484 p = &base_type->stmt;
1486 function_computed_target_list = NULL;
1487 function_computed_goto_list = NULL;
1489 if (decl->ctype.modifiers & MOD_EXTERN) {
1490 if (!(decl->ctype.modifiers & MOD_INLINE))
1491 warning(decl->pos, "function with external linkage has definition");
1493 if (!(decl->ctype.modifiers & MOD_STATIC))
1494 decl->ctype.modifiers |= MOD_EXTERN;
1496 stmt = start_function(decl);
1498 *p = stmt;
1499 FOR_EACH_PTR (base_type->arguments, arg) {
1500 declare_argument(arg, base_type);
1501 } END_FOR_EACH_PTR(arg);
1503 token = compound_statement(token->next, stmt);
1505 end_function(decl);
1506 if (!(decl->ctype.modifiers & MOD_INLINE))
1507 add_symbol(list, decl);
1508 check_declaration(decl);
1509 function_symbol_list = NULL;
1510 if (function_computed_goto_list) {
1511 if (!function_computed_target_list)
1512 warning(decl->pos, "function has computed goto but no targets?");
1513 else {
1514 struct statement *stmt;
1515 FOR_EACH_PTR(function_computed_goto_list, stmt) {
1516 stmt->target_list = function_computed_target_list;
1517 } END_FOR_EACH_PTR(stmt);
1520 return expect(token, '}', "at end of function");
1523 static void promote_k_r_types(struct symbol *arg)
1525 struct symbol *base = arg->ctype.base_type;
1526 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
1527 arg->ctype.base_type = &int_ctype;
1531 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
1533 struct symbol_list *real_args = fn->ctype.base_type->arguments;
1534 struct symbol *arg;
1536 FOR_EACH_PTR(real_args, arg) {
1537 struct symbol *type;
1539 /* This is quadratic in the number of arguments. We _really_ don't care */
1540 FOR_EACH_PTR(argtypes, type) {
1541 if (type->ident == arg->ident)
1542 goto match;
1543 } END_FOR_EACH_PTR(type);
1544 warning(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
1545 continue;
1546 match:
1547 type->used = 1;
1548 /* "char" and "short" promote to "int" */
1549 promote_k_r_types(type);
1551 arg->ctype = type->ctype;
1552 } END_FOR_EACH_PTR(arg);
1554 FOR_EACH_PTR(argtypes, arg) {
1555 if (!arg->used)
1556 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
1557 } END_FOR_EACH_PTR(arg);
1561 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
1562 struct symbol_list **list)
1564 struct symbol_list *args = NULL;
1566 warning(token->pos, "non-ANSI function declaration");
1567 do {
1568 token = external_declaration(token, &args);
1569 } while (lookup_type(token));
1571 apply_k_r_types(args, decl);
1573 if (!match_op(token, '{')) {
1574 warning(token->pos, "expected function body");
1575 return token;
1577 return parse_function_body(token, decl, list);
1581 static struct token *external_declaration(struct token *token, struct symbol_list **list)
1583 struct ident *ident = NULL;
1584 struct symbol *decl;
1585 struct ctype ctype = { 0, };
1586 struct symbol *base_type;
1587 int is_typedef;
1589 /* Top-level inline asm? */
1590 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1591 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
1592 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
1593 struct statement *stmt;
1595 anon->ctype.base_type = fn;
1596 function_symbol_list = &anon->symbol_list;
1597 stmt = start_function(anon);
1598 token = parse_asm(token->next, stmt);
1599 end_function(anon);
1600 function_symbol_list = NULL;
1601 add_symbol(list, anon);
1602 return token;
1605 /* Parse declaration-specifiers, if any */
1606 token = declaration_specifiers(token, &ctype, 0);
1607 decl = alloc_symbol(token->pos, SYM_NODE);
1608 decl->ctype = ctype;
1609 token = declarator(token, decl, &ident);
1611 /* Just a type declaration? */
1612 if (!ident)
1613 return expect(token, ';', "end of type declaration");
1615 /* type define declaration? */
1616 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
1618 /* Typedef's don't have meaningful storage */
1619 if (is_typedef) {
1620 ctype.modifiers &= ~MOD_STORAGE;
1621 decl->ctype.modifiers &= ~MOD_STORAGE;
1622 decl->ctype.modifiers |= MOD_USERTYPE;
1625 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1627 base_type = decl->ctype.base_type;
1628 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1629 /* K&R argument declaration? */
1630 if (lookup_type(token))
1631 return parse_k_r_arguments(token, decl, list);
1632 if (match_op(token, '{'))
1633 return parse_function_body(token, decl, list);
1635 if (!(decl->ctype.modifiers & MOD_STATIC))
1636 decl->ctype.modifiers |= MOD_EXTERN;
1637 } else if (!is_typedef && base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
1638 warning(token->pos, "void declaration");
1641 for (;;) {
1642 if (!is_typedef && match_op(token, '=')) {
1643 if (decl->ctype.modifiers & MOD_EXTERN) {
1644 warning(decl->pos, "symbol with external linkage has initializer");
1645 decl->ctype.modifiers &= ~MOD_EXTERN;
1647 token = initializer(&decl->initializer, token->next);
1649 if (!is_typedef) {
1650 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
1651 add_symbol(list, decl);
1652 if (function_symbol_list)
1653 fn_local_symbol(decl);
1656 check_declaration(decl);
1658 if (!match_op(token, ','))
1659 break;
1661 token = token->next;
1662 ident = NULL;
1663 decl = alloc_symbol(token->pos, SYM_NODE);
1664 decl->ctype = ctype;
1665 token = declaration_specifiers(token, &decl->ctype, 1);
1666 token = declarator(token, decl, &ident);
1667 if (!ident) {
1668 warning(token->pos, "expected identifier name in type definition");
1669 return token;
1672 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1674 /* Function declarations are automatically extern unless specifically static */
1675 base_type = decl->ctype.base_type;
1676 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1677 if (!(decl->ctype.modifiers & MOD_STATIC))
1678 decl->ctype.modifiers |= MOD_EXTERN;
1681 return expect(token, ';', "at end of declaration");
1684 void translation_unit(struct token *token, struct symbol_list **list)
1686 while (!eof_token(token))
1687 token = external_declaration(token, list);
1688 // They aren't needed any more
1689 clear_token_alloc();