Get comparison sizes right.
[smatch.git] / parse.c
blob3063adb086b9940ceb4f85e11351486c68b70e78
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 attribute == &__format_arg___ident)
425 return NULL;
426 if (attribute == &section_ident ||
427 attribute == &__section___ident)
428 return NULL;
429 if (attribute == &unused_ident ||
430 attribute == &__unused___ident)
431 return NULL;
432 if (attribute == &const_ident ||
433 attribute == &__const_ident ||
434 attribute == &__const___ident)
435 return NULL;
436 if (attribute == &noreturn_ident ||
437 attribute == &__noreturn___ident)
438 return NULL;
439 if (attribute == &regparm_ident)
440 return NULL;
441 if (attribute == &weak_ident)
442 return NULL;
443 if (attribute == &alias_ident)
444 return NULL;
445 if (attribute == &pure_ident)
446 return NULL;
447 if (attribute == &always_inline_ident)
448 return NULL;
449 if (attribute == &syscall_linkage_ident)
450 return NULL;
451 if (attribute == &visibility_ident)
452 return NULL;
453 if (attribute == &model_ident ||
454 attribute == &__model___ident)
455 return NULL;
457 return "unknown attribute";
460 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
462 ctype->modifiers = 0;
463 token = expect(token, '(', "after attribute");
464 token = expect(token, '(', "after attribute");
466 for (;;) {
467 const char *error;
468 struct ident *attribute_name;
469 struct expression *attribute_expr;
471 if (eof_token(token))
472 break;
473 if (match_op(token, ';'))
474 break;
475 if (token_type(token) != TOKEN_IDENT)
476 break;
477 attribute_name = token->ident;
478 token = token->next;
479 attribute_expr = NULL;
480 if (match_op(token, '('))
481 token = parens_expression(token, &attribute_expr, "in attribute");
482 error = handle_attribute(ctype, attribute_name, attribute_expr);
483 if (error)
484 warning(token->pos, "attribute '%s': %s", show_ident(attribute_name), error);
485 if (!match_op(token, ','))
486 break;
487 token = token->next;
490 token = expect(token, ')', "after attribute");
491 token = expect(token, ')', "after attribute");
492 return token;
495 struct symbol * ctype_integer(unsigned long spec)
497 static struct symbol *const integer_ctypes[][3] = {
498 { &llong_ctype, &sllong_ctype, &ullong_ctype },
499 { &long_ctype, &slong_ctype, &ulong_ctype },
500 { &short_ctype, &sshort_ctype, &ushort_ctype },
501 { &char_ctype, &schar_ctype, &uchar_ctype },
502 { &int_ctype, &sint_ctype, &uint_ctype },
504 struct symbol *const (*ctype)[3];
505 int sub;
507 ctype = integer_ctypes;
508 if (!(spec & MOD_LONGLONG)) {
509 ctype++;
510 if (!(spec & MOD_LONG)) {
511 ctype++;
512 if (!(spec & MOD_SHORT)) {
513 ctype++;
514 if (!(spec & MOD_CHAR))
515 ctype++;
520 sub = ((spec & MOD_UNSIGNED)
522 : ((spec & MOD_EXPLICITLY_SIGNED)
524 : 0));
526 return ctype[0][sub];
529 struct symbol * ctype_fp(unsigned long spec)
531 if (spec & MOD_LONGLONG)
532 return &ldouble_ctype;
533 if (spec & MOD_LONG)
534 return &double_ctype;
535 return &float_ctype;
538 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
540 unsigned long mod = thistype->modifiers;
542 if (mod) {
543 unsigned long old = ctype->modifiers;
544 unsigned long extra = 0, dup, conflict;
546 if (mod & old & MOD_LONG) {
547 extra = MOD_LONGLONG | MOD_LONG;
548 mod &= ~MOD_LONG;
549 old &= ~MOD_LONG;
551 dup = (mod & old) | (extra & old) | (extra & mod);
552 if (dup)
553 warning(pos, "Just how %sdo you want this type to be?",
554 modifier_string(dup));
556 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
557 if (conflict)
558 warning(pos, "You cannot have both long and short modifiers.");
560 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
561 if (conflict)
562 warning(pos, "You cannot have both signed and unsigned modifiers.");
564 // Only one storage modifier allowed, except that "inline" doesn't count.
565 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
566 conflict &= (conflict - 1);
567 if (conflict)
568 warning(pos, "multiple storage classes");
570 ctype->modifiers = old | mod | extra;
573 /* Context mask and value */
574 ctype->in_context += thistype->in_context;
575 ctype->out_context += thistype->out_context;
577 /* Alignment */
578 if (thistype->alignment & (thistype->alignment-1)) {
579 warning(pos, "I don't like non-power-of-2 alignments");
580 thistype->alignment = 0;
582 if (thistype->alignment > ctype->alignment)
583 ctype->alignment = thistype->alignment;
585 /* Address space */
586 ctype->as = thistype->as;
589 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
591 unsigned long banned, wrong;
592 unsigned long this_mod = s->ctype.modifiers;
593 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
594 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
596 if (this_mod & (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF))
597 banned = BANNED_SIZE | BANNED_SIGN;
598 else if (this_mod & MOD_SPECIALBITS)
599 banned = 0;
600 else if (s->ctype.base_type == &fp_type)
601 banned = BANNED_SIGN;
602 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
603 banned = 0;
604 else {
605 // label_type
606 // void_type
607 // bad_type
608 // vector_type <-- whatever that is
609 banned = BANNED_SIZE | BANNED_SIGN;
612 wrong = mod & banned;
613 if (wrong)
614 warning(*pos, "modifier %sis invalid in this context",
615 modifier_string (wrong));
619 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
621 struct token *token;
623 while ( (token = next) != NULL ) {
624 struct ctype thistype;
625 struct ident *ident;
626 struct symbol *s, *type;
627 unsigned long mod;
629 next = token->next;
630 if (token_type(token) != TOKEN_IDENT)
631 break;
632 ident = token->ident;
634 s = lookup_symbol(ident, NS_TYPEDEF);
635 if (!s)
636 break;
637 thistype = s->ctype;
638 mod = thistype.modifiers;
639 if (qual && (mod & ~(MOD_ATTRIBUTE | MOD_CONST | MOD_VOLATILE)))
640 break;
641 if (mod & MOD_SPECIALBITS) {
642 if (mod & MOD_STRUCTOF)
643 next = struct_or_union_specifier(SYM_STRUCT, next, &thistype);
644 else if (mod & MOD_UNIONOF)
645 next = struct_or_union_specifier(SYM_UNION, next, &thistype);
646 else if (mod & MOD_ENUMOF)
647 next = enum_specifier(next, &thistype);
648 else if (mod & MOD_ATTRIBUTE)
649 next = attribute_specifier(next, &thistype);
650 else if (mod & MOD_TYPEOF)
651 next = typeof_specifier(next, &thistype);
652 mod = thistype.modifiers;
654 type = thistype.base_type;
655 if (type) {
656 if (qual)
657 break;
658 if (ctype->base_type)
659 break;
660 /* User types only mix with qualifiers */
661 if (mod & MOD_USERTYPE) {
662 if (ctype->modifiers & MOD_SPECIFIER)
663 break;
665 ctype->base_type = type;
668 check_modifiers(&token->pos, s, ctype->modifiers);
669 apply_ctype(token->pos, &thistype, ctype);
672 /* Turn the "virtual types" into real types with real sizes etc */
673 if (!ctype->base_type) {
674 struct symbol *base = &incomplete_ctype;
677 * If we have modifiers, we'll default to an integer
678 * type, and "ctype_integer()" will turn this into
679 * a specific one.
681 if (ctype->modifiers & MOD_SPECIFIER)
682 base = &int_type;
683 ctype->base_type = base;
685 if (ctype->base_type == &int_type) {
686 ctype->base_type = ctype_integer(ctype->modifiers);
687 ctype->modifiers &= ~MOD_SPECIFIER;
688 } else if (ctype->base_type == &fp_type) {
689 ctype->base_type = ctype_fp(ctype->modifiers);
690 ctype->modifiers &= ~MOD_SPECIFIER;
692 if (ctype->modifiers & MOD_BITWISE) {
693 struct symbol *type;
694 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
695 if (!is_int_type(ctype->base_type)) {
696 warning(token->pos, "invalid modifier");
697 return token;
699 type = alloc_symbol(token->pos, SYM_BASETYPE);
700 *type = *ctype->base_type;
701 type->ctype.base_type = ctype->base_type;
702 type->type = SYM_RESTRICT;
703 type->ctype.modifiers &= ~MOD_SPECIFIER;
704 ctype->base_type = type;
706 return token;
709 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
711 struct expression *expr = NULL;
713 token = parse_expression(token, &expr);
714 sym->array_size = expr;
715 return token;
718 static struct token *parameter_type_list(struct token *, struct symbol *);
719 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
721 static struct token *handle_attributes(struct token *token, struct ctype *ctype)
723 for (;;) {
724 if (token_type(token) != TOKEN_IDENT)
725 break;
726 if (match_idents(token, &__attribute___ident, &__attribute_ident, NULL)) {
727 struct ctype thistype = { 0, };
728 token = attribute_specifier(token->next, &thistype);
729 apply_ctype(token->pos, &thistype, ctype);
730 continue;
732 if (match_idents(token, &asm_ident, &__asm_ident, &__asm___ident)) {
733 struct expression *expr;
734 token = expect(token->next, '(', "after asm");
735 token = parse_expression(token->next, &expr);
736 token = expect(token, ')', "after asm");
737 continue;
739 break;
741 return token;
744 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
746 struct ctype *ctype = &decl->ctype;
748 if (p && token_type(token) == TOKEN_IDENT) {
749 *p = token->ident;
750 token = token->next;
753 for (;;) {
754 token = handle_attributes(token, ctype);
756 if (token_type(token) != TOKEN_SPECIAL)
757 return token;
760 * This can be either a parameter list or a grouping.
761 * For the direct (non-abstract) case, we know if must be
762 * a parameter list if we already saw the identifier.
763 * For the abstract case, we know if must be a parameter
764 * list if it is empty or starts with a type.
766 if (token->special == '(') {
767 struct symbol *sym;
768 struct token *next = token->next;
769 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
771 if (!fn) {
772 struct symbol *base_type = ctype->base_type;
773 token = declarator(next, decl, p);
774 token = expect(token, ')', "in nested declarator");
775 while (ctype->base_type != base_type)
776 ctype = &ctype->base_type->ctype;
777 p = NULL;
778 continue;
781 sym = indirect(token->pos, ctype, SYM_FN);
782 token = parameter_type_list(next, sym);
783 token = expect(token, ')', "in function declarator");
784 continue;
786 if (token->special == '[') {
787 struct symbol *array = indirect(token->pos, ctype, SYM_ARRAY);
788 token = abstract_array_declarator(token->next, array);
789 token = expect(token, ']', "in abstract_array_declarator");
790 ctype = &array->ctype;
791 continue;
793 break;
795 return token;
798 static struct token *pointer(struct token *token, struct ctype *ctype)
800 unsigned long modifiers;
801 struct symbol *base_type;
803 modifiers = ctype->modifiers & ~(MOD_TYPEDEF | MOD_ATTRIBUTE);
804 base_type = ctype->base_type;
805 ctype->modifiers = modifiers;
807 while (match_op(token,'*')) {
808 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
809 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
810 ptr->ctype.as = ctype->as;
811 ptr->ctype.in_context += ctype->in_context;
812 ptr->ctype.out_context += ctype->out_context;
813 ptr->ctype.base_type = base_type;
815 base_type = ptr;
816 ctype->modifiers = modifiers & MOD_STORAGE;
817 ctype->base_type = base_type;
818 ctype->as = 0;
819 ctype->in_context = 0;
820 ctype->out_context = 0;
822 token = declaration_specifiers(token->next, ctype, 1);
823 modifiers = ctype->modifiers;
825 return token;
828 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
830 token = pointer(token, &sym->ctype);
831 return direct_declarator(token, sym, p);
834 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
836 struct ctype *ctype = &decl->ctype;
837 struct expression *expr;
838 struct symbol *bitfield;
839 long long width;
841 if (!is_int_type(ctype->base_type)) {
842 warning(token->pos, "invalid bitfield specifier for type %s.",
843 show_typename(ctype->base_type));
844 // Parse this to recover gracefully.
845 return conditional_expression(token->next, &expr);
848 bitfield = indirect(token->pos, ctype, SYM_BITFIELD);
849 token = conditional_expression(token->next, &expr);
850 width = get_expression_value(expr);
851 bitfield->bit_size = width;
853 if (width < 0 || width > INT_MAX) {
854 warning(token->pos, "invalid bitfield width, %lld.", width);
855 width = -1;
856 } else if (decl->ident && width == 0) {
857 warning(token->pos, "invalid named zero-width bitfield `%s'",
858 show_ident(decl->ident));
859 width = -1;
860 } else if (decl->ident) {
861 struct symbol *base_type = bitfield->ctype.base_type;
862 int is_signed = !(base_type->ctype.modifiers & MOD_UNSIGNED);
863 if (width == 1 && is_signed) {
864 // Valid values are either {-1;0} or {0}, depending on integer
865 // representation. The latter makes for very efficient code...
866 warning(token->pos, "dubious one-bit signed bitfield");
868 if (Wdefault_bitfield_sign &&
869 base_type->type != SYM_ENUM &&
870 !(base_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
871 is_signed) {
872 // The sign of bitfields is unspecified by default.
873 warning (token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
876 bitfield->bit_size = width;
877 return token;
880 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
882 while (!match_op(token, '}')) {
883 struct ctype ctype = {0, };
885 token = declaration_specifiers(token, &ctype, 0);
886 for (;;) {
887 struct ident *ident = NULL;
888 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
889 decl->ctype = ctype;
890 token = declarator(token, decl, &ident);
891 decl->ident = ident;
892 if (match_op(token, ':')) {
893 token = handle_bitfield(token, decl);
894 token = handle_attributes(token, &decl->ctype);
896 add_symbol(list, decl);
897 if (!match_op(token, ','))
898 break;
899 token = token->next;
901 if (!match_op(token, ';')) {
902 warning(token->pos, "expected ; at end of declaration");
903 break;
905 token = token->next;
907 return token;
910 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
912 struct ident *ident = NULL;
913 struct symbol *sym;
914 struct ctype ctype = { 0, };
916 token = declaration_specifiers(token, &ctype, 0);
917 sym = alloc_symbol(token->pos, SYM_NODE);
918 sym->ctype = ctype;
919 *tree = sym;
920 token = declarator(token, sym, &ident);
921 sym->ident = ident;
922 return token;
925 struct token *typename(struct token *token, struct symbol **p)
927 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
928 *p = sym;
929 token = declaration_specifiers(token, &sym->ctype, 0);
930 return declarator(token, sym, NULL);
933 struct token *expression_statement(struct token *token, struct expression **tree)
935 token = parse_expression(token, tree);
936 return expect(token, ';', "at end of statement");
939 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
940 struct expression_list **inout)
942 struct expression *expr;
944 /* Allow empty operands */
945 if (match_op(token->next, ':') || match_op(token->next, ')'))
946 return token->next;
947 do {
948 struct ident *ident = NULL;
949 if (match_op(token->next, '[') &&
950 token_type(token->next->next) == TOKEN_IDENT &&
951 match_op(token->next->next->next, ']')) {
952 ident = token->next->next->ident;
953 token = token->next->next->next;
955 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
956 token = primary_expression(token->next, &expr);
957 add_expression(inout, expr);
958 token = parens_expression(token, &expr, "in asm parameter");
959 add_expression(inout, expr);
960 } while (match_op(token, ','));
961 return token;
964 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
965 struct expression_list **clobbers)
967 struct expression *expr;
969 do {
970 token = primary_expression(token->next, &expr);
971 add_expression(clobbers, expr);
972 } while (match_op(token, ','));
973 return token;
976 static struct token *parse_asm(struct token *token, struct statement *stmt)
978 stmt->type = STMT_ASM;
979 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
980 token = token->next;
982 token = expect(token, '(', "after asm");
983 token = parse_expression(token, &stmt->asm_string);
984 if (match_op(token, ':'))
985 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
986 if (match_op(token, ':'))
987 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
988 if (match_op(token, ':'))
989 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
990 token = expect(token, ')', "after asm");
991 return expect(token, ';', "at end of asm-statement");
994 /* Make a statement out of an expression */
995 static struct statement *make_statement(struct expression *expr)
997 struct statement *stmt;
999 if (!expr)
1000 return NULL;
1001 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1002 stmt->expression = expr;
1003 return stmt;
1007 * All iterators have two symbols associated with them:
1008 * the "continue" and "break" symbols, which are targets
1009 * for continue and break statements respectively.
1011 * They are in a special name-space, but they follow
1012 * all the normal visibility rules, so nested iterators
1013 * automatically work right.
1015 static void start_iterator(struct statement *stmt)
1017 struct symbol *cont, *brk;
1019 start_symbol_scope();
1020 cont = alloc_symbol(stmt->pos, SYM_NODE);
1021 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1022 brk = alloc_symbol(stmt->pos, SYM_NODE);
1023 bind_symbol(brk, &break_ident, NS_ITERATOR);
1025 stmt->type = STMT_ITERATOR;
1026 stmt->iterator_break = brk;
1027 stmt->iterator_continue = cont;
1028 fn_local_symbol(brk);
1029 fn_local_symbol(cont);
1032 static void end_iterator(struct statement *stmt)
1034 end_symbol_scope();
1037 static struct statement *start_function(struct symbol *sym)
1039 struct symbol *ret;
1040 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1042 start_function_scope();
1043 ret = alloc_symbol(sym->pos, SYM_NODE);
1044 ret->ctype = sym->ctype.base_type->ctype;
1045 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1046 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1047 bind_symbol(ret, &return_ident, NS_ITERATOR);
1048 stmt->ret = ret;
1049 fn_local_symbol(ret);
1051 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1052 current_fn = sym;
1054 return stmt;
1057 static void end_function(struct symbol *sym)
1059 current_fn = NULL;
1060 end_function_scope();
1064 * A "switch()" statement, like an iterator, has a
1065 * the "break" symbol associated with it. It works
1066 * exactly like the iterator break - it's the target
1067 * for any break-statements in scope, and means that
1068 * "break" handling doesn't even need to know whether
1069 * it's breaking out of an iterator or a switch.
1071 * In addition, the "case" symbol is a marker for the
1072 * case/default statements to find the switch statement
1073 * that they are associated with.
1075 static void start_switch(struct statement *stmt)
1077 struct symbol *brk, *switch_case;
1079 start_symbol_scope();
1080 brk = alloc_symbol(stmt->pos, SYM_NODE);
1081 bind_symbol(brk, &break_ident, NS_ITERATOR);
1083 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1084 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1085 switch_case->stmt = stmt;
1087 stmt->type = STMT_SWITCH;
1088 stmt->switch_break = brk;
1089 stmt->switch_case = switch_case;
1091 fn_local_symbol(brk);
1092 fn_local_symbol(switch_case);
1095 static void end_switch(struct statement *stmt)
1097 if (!stmt->switch_case->symbol_list)
1098 warning(stmt->pos, "switch with no cases");
1099 end_symbol_scope();
1102 static void add_case_statement(struct statement *stmt)
1104 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1105 struct symbol *sym;
1107 if (!target) {
1108 warning(stmt->pos, "not in switch scope");
1109 stmt->type = STMT_NONE;
1110 return;
1112 sym = alloc_symbol(stmt->pos, SYM_NODE);
1113 add_symbol(&target->symbol_list, sym);
1114 sym->stmt = stmt;
1115 stmt->case_label = sym;
1116 fn_local_symbol(sym);
1119 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1121 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1123 if (!target)
1124 error_die(token->pos, "internal error: return without a function target");
1125 stmt->type = STMT_RETURN;
1126 stmt->ret_target = target;
1127 return expression_statement(token->next, &stmt->ret_value);
1130 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1132 struct symbol_list *syms;
1133 struct expression *e1, *e2, *e3;
1134 struct statement *iterator;
1136 start_iterator(stmt);
1137 token = expect(token->next, '(', "after 'for'");
1139 syms = NULL;
1140 e1 = NULL;
1141 /* C99 variable declaration? */
1142 if (lookup_type(token)) {
1143 token = external_declaration(token, &syms);
1144 } else {
1145 token = parse_expression(token, &e1);
1146 token = expect(token, ';', "in 'for'");
1148 token = parse_expression(token, &e2);
1149 token = expect(token, ';', "in 'for'");
1150 token = parse_expression(token, &e3);
1151 token = expect(token, ')', "in 'for'");
1152 token = statement(token, &iterator);
1154 stmt->iterator_syms = syms;
1155 stmt->iterator_pre_statement = make_statement(e1);
1156 stmt->iterator_pre_condition = e2;
1157 stmt->iterator_post_statement = make_statement(e3);
1158 stmt->iterator_post_condition = e2;
1159 stmt->iterator_statement = iterator;
1160 end_iterator(stmt);
1162 return token;
1165 struct token *parse_while_statement(struct token *token, struct statement *stmt)
1167 struct expression *expr;
1168 struct statement *iterator;
1170 start_iterator(stmt);
1171 token = parens_expression(token->next, &expr, "after 'while'");
1172 token = statement(token, &iterator);
1174 stmt->iterator_pre_condition = expr;
1175 stmt->iterator_post_condition = expr;
1176 stmt->iterator_statement = iterator;
1177 end_iterator(stmt);
1179 return token;
1182 struct token *parse_do_statement(struct token *token, struct statement *stmt)
1184 struct expression *expr;
1185 struct statement *iterator;
1187 start_iterator(stmt);
1188 token = statement(token->next, &iterator);
1189 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1190 token = token->next;
1191 else
1192 warning(token->pos, "expected 'while' after 'do'");
1193 token = parens_expression(token, &expr, "after 'do-while'");
1195 stmt->iterator_post_condition = expr;
1196 stmt->iterator_statement = iterator;
1197 end_iterator(stmt);
1199 return expect(token, ';', "after statement");
1202 static struct token *statement(struct token *token, struct statement **tree)
1204 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1206 *tree = stmt;
1207 if (token_type(token) == TOKEN_IDENT) {
1208 if (token->ident == &if_ident) {
1209 stmt->type = STMT_IF;
1210 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1211 token = statement(token, &stmt->if_true);
1212 if (token_type(token) != TOKEN_IDENT)
1213 return token;
1214 if (token->ident != &else_ident)
1215 return token;
1216 return statement(token->next, &stmt->if_false);
1219 if (token->ident == &return_ident)
1220 return parse_return_statement(token, stmt);
1222 if (token->ident == &break_ident || token->ident == &continue_ident) {
1223 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1224 stmt->type = STMT_GOTO;
1225 stmt->goto_label = target;
1226 if (!target)
1227 warning(stmt->pos, "break/continue not in iterator scope");
1228 return expect(token->next, ';', "at end of statement");
1230 if (token->ident == &default_ident) {
1231 token = token->next;
1232 goto default_statement;
1234 if (token->ident == &case_ident) {
1235 token = parse_expression(token->next, &stmt->case_expression);
1236 if (match_op(token, SPECIAL_ELLIPSIS))
1237 token = parse_expression(token->next, &stmt->case_to);
1238 default_statement:
1239 stmt->type = STMT_CASE;
1240 token = expect(token, ':', "after default/case");
1241 add_case_statement(stmt);
1242 return statement(token, &stmt->case_statement);
1244 if (token->ident == &switch_ident) {
1245 stmt->type = STMT_SWITCH;
1246 start_switch(stmt);
1247 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1248 token = statement(token, &stmt->switch_statement);
1249 end_switch(stmt);
1250 return token;
1252 if (token->ident == &for_ident)
1253 return parse_for_statement(token, stmt);
1255 if (token->ident == &while_ident)
1256 return parse_while_statement(token, stmt);
1258 if (token->ident == &do_ident)
1259 return parse_do_statement(token, stmt);
1261 if (token->ident == &goto_ident) {
1262 stmt->type = STMT_GOTO;
1263 token = token->next;
1264 if (match_op(token, '*')) {
1265 token = parse_expression(token->next, &stmt->goto_expression);
1266 add_statement(&function_computed_goto_list, stmt);
1267 } else if (token_type(token) == TOKEN_IDENT) {
1268 stmt->goto_label = label_symbol(token);
1269 token = token->next;
1270 } else {
1271 warning(token->pos, "Expected identifier or goto expression");
1273 return expect(token, ';', "at end of statement");
1275 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1276 return parse_asm(token->next, stmt);
1278 if (token->ident == &__context___ident) {
1279 stmt->type = STMT_INTERNAL;
1280 token = parse_expression(token->next, &stmt->expression);
1281 return expect(token, ';', "at end of statement");
1283 if (match_op(token->next, ':')) {
1284 stmt->type = STMT_LABEL;
1285 stmt->label_identifier = label_symbol(token);
1286 return statement(token->next->next, &stmt->label_statement);
1290 if (match_op(token, '{')) {
1291 stmt->type = STMT_COMPOUND;
1292 start_symbol_scope();
1293 token = compound_statement(token->next, stmt);
1294 end_symbol_scope();
1296 return expect(token, '}', "at end of compound statement");
1299 stmt->type = STMT_EXPRESSION;
1300 return expression_statement(token, &stmt->expression);
1303 static struct token * statement_list(struct token *token, struct statement_list **list, struct symbol_list **syms)
1305 for (;;) {
1306 struct statement * stmt;
1307 if (eof_token(token))
1308 break;
1309 if (match_op(token, '}'))
1310 break;
1311 if (lookup_type(token)) {
1312 if (warn_on_mixed && *list)
1313 warning(token->pos, "mixing declarations and code");
1314 token = external_declaration(token, syms);
1315 continue;
1317 token = statement(token, &stmt);
1318 add_statement(list, stmt);
1320 return token;
1323 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
1325 struct symbol_list **list = &fn->arguments;
1327 if (match_op(token, ')')) {
1328 // No warning for "void oink ();"
1329 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1330 if (!match_op(token->next, ';'))
1331 warning(token->pos, "non-ANSI function declaration");
1332 return token;
1335 for (;;) {
1336 struct symbol *sym;
1338 if (match_op(token, SPECIAL_ELLIPSIS)) {
1339 if (!*list)
1340 warning(token->pos, "variadic functions must have one named argument");
1341 fn->variadic = 1;
1342 token = token->next;
1343 break;
1346 sym = alloc_symbol(token->pos, SYM_NODE);
1347 token = parameter_declaration(token, &sym);
1348 if (sym->ctype.base_type == &void_ctype) {
1349 /* Special case: (void) */
1350 if (!*list && !sym->ident)
1351 break;
1352 warning(token->pos, "void parameter");
1354 add_symbol(list, sym);
1355 if (!match_op(token, ','))
1356 break;
1357 token = token->next;
1360 return token;
1363 struct token *compound_statement(struct token *token, struct statement *stmt)
1365 token = statement_list(token, &stmt->stmts, &stmt->syms);
1366 return token;
1369 static struct expression *identifier_expression(struct token *token)
1371 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1372 expr->expr_ident = token->ident;
1373 return expr;
1376 static struct expression *index_expression(struct expression *from, struct expression *to)
1378 int idx_from, idx_to;
1379 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1381 idx_from = get_expression_value(from);
1382 idx_to = idx_from;
1383 if (to) {
1384 idx_to = get_expression_value(to);
1385 if (idx_to < idx_from || idx_from < 0)
1386 warning(from->pos, "nonsense array initializer index range");
1388 expr->idx_from = idx_from;
1389 expr->idx_to = idx_to;
1390 return expr;
1393 static struct token *single_initializer(struct expression **ep, struct token *token)
1395 int expect_equal = 0;
1396 struct token *next = token->next;
1397 struct expression **tail = ep;
1398 int nested;
1400 *ep = NULL;
1402 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1403 struct expression *expr = identifier_expression(token);
1404 warning(token->pos, "obsolete struct initializer, use C99 syntax");
1405 token = initializer(&expr->ident_expression, next->next);
1406 if (expr->ident_expression)
1407 *ep = expr;
1408 return token;
1411 for (tail = ep, nested = 0; ; nested++, next = token->next) {
1412 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
1413 struct expression *expr = identifier_expression(next);
1414 *tail = expr;
1415 tail = &expr->ident_expression;
1416 expect_equal = 1;
1417 token = next->next;
1418 } else if (match_op(token, '[')) {
1419 struct expression *from = NULL, *to = NULL, *expr;
1420 token = constant_expression(token->next, &from);
1421 if (match_op(token, SPECIAL_ELLIPSIS))
1422 token = constant_expression(token->next, &to);
1423 expr = index_expression(from, to);
1424 *tail = expr;
1425 tail = &expr->idx_expression;
1426 token = expect(token, ']', "at end of initializer index");
1427 if (nested)
1428 expect_equal = 1;
1429 } else {
1430 break;
1433 if (nested && !expect_equal) {
1434 if (!match_op(token, '='))
1435 warning(token->pos, "obsolete array initializer, use C99 syntax");
1436 else
1437 expect_equal = 1;
1439 if (expect_equal)
1440 token = expect(token, '=', "at end of initializer index");
1442 token = initializer(tail, token);
1443 if (!*tail)
1444 *ep = NULL;
1445 return token;
1448 static struct token *initializer_list(struct expression_list **list, struct token *token)
1450 struct expression *expr;
1452 for (;;) {
1453 token = single_initializer(&expr, token);
1454 if (!expr)
1455 break;
1456 add_expression(list, expr);
1457 if (!match_op(token, ','))
1458 break;
1459 token = token->next;
1461 return token;
1464 struct token *initializer(struct expression **tree, struct token *token)
1466 if (match_op(token, '{')) {
1467 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1468 *tree = expr;
1469 token = initializer_list(&expr->expr_list, token->next);
1470 return expect(token, '}', "at end of initializer");
1472 return assignment_expression(token, tree);
1475 static void declare_argument(struct symbol *sym, struct symbol *fn)
1477 if (!sym->ident) {
1478 warning(sym->pos, "no identifier for function argument");
1479 return;
1481 bind_symbol(sym, sym->ident, NS_SYMBOL);
1484 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1485 struct symbol_list **list)
1487 struct symbol_list **old_symbol_list;
1488 struct symbol *base_type = decl->ctype.base_type;
1489 struct statement *stmt, **p;
1490 struct symbol *arg;
1492 old_symbol_list = function_symbol_list;
1493 if (decl->ctype.modifiers & MOD_INLINE) {
1494 function_symbol_list = &decl->inline_symbol_list;
1495 p = &base_type->inline_stmt;
1496 } else {
1497 function_symbol_list = &decl->symbol_list;
1498 p = &base_type->stmt;
1500 function_computed_target_list = NULL;
1501 function_computed_goto_list = NULL;
1503 if (decl->ctype.modifiers & MOD_EXTERN) {
1504 if (!(decl->ctype.modifiers & MOD_INLINE))
1505 warning(decl->pos, "function with external linkage has definition");
1507 if (!(decl->ctype.modifiers & MOD_STATIC))
1508 decl->ctype.modifiers |= MOD_EXTERN;
1510 stmt = start_function(decl);
1512 *p = stmt;
1513 FOR_EACH_PTR (base_type->arguments, arg) {
1514 declare_argument(arg, base_type);
1515 } END_FOR_EACH_PTR(arg);
1517 token = compound_statement(token->next, stmt);
1519 end_function(decl);
1520 if (!(decl->ctype.modifiers & MOD_INLINE))
1521 add_symbol(list, decl);
1522 check_declaration(decl);
1523 function_symbol_list = old_symbol_list;
1524 if (function_computed_goto_list) {
1525 if (!function_computed_target_list)
1526 warning(decl->pos, "function has computed goto but no targets?");
1527 else {
1528 struct statement *stmt;
1529 FOR_EACH_PTR(function_computed_goto_list, stmt) {
1530 stmt->target_list = function_computed_target_list;
1531 } END_FOR_EACH_PTR(stmt);
1534 return expect(token, '}', "at end of function");
1537 static void promote_k_r_types(struct symbol *arg)
1539 struct symbol *base = arg->ctype.base_type;
1540 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
1541 arg->ctype.base_type = &int_ctype;
1545 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
1547 struct symbol_list *real_args = fn->ctype.base_type->arguments;
1548 struct symbol *arg;
1550 FOR_EACH_PTR(real_args, arg) {
1551 struct symbol *type;
1553 /* This is quadratic in the number of arguments. We _really_ don't care */
1554 FOR_EACH_PTR(argtypes, type) {
1555 if (type->ident == arg->ident)
1556 goto match;
1557 } END_FOR_EACH_PTR(type);
1558 warning(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
1559 continue;
1560 match:
1561 type->used = 1;
1562 /* "char" and "short" promote to "int" */
1563 promote_k_r_types(type);
1565 arg->ctype = type->ctype;
1566 } END_FOR_EACH_PTR(arg);
1568 FOR_EACH_PTR(argtypes, arg) {
1569 if (!arg->used)
1570 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
1571 } END_FOR_EACH_PTR(arg);
1575 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
1576 struct symbol_list **list)
1578 struct symbol_list *args = NULL;
1580 warning(token->pos, "non-ANSI function declaration");
1581 do {
1582 token = external_declaration(token, &args);
1583 } while (lookup_type(token));
1585 apply_k_r_types(args, decl);
1587 if (!match_op(token, '{')) {
1588 warning(token->pos, "expected function body");
1589 return token;
1591 return parse_function_body(token, decl, list);
1595 static struct token *external_declaration(struct token *token, struct symbol_list **list)
1597 struct ident *ident = NULL;
1598 struct symbol *decl;
1599 struct ctype ctype = { 0, };
1600 struct symbol *base_type;
1601 int is_typedef;
1603 /* Top-level inline asm? */
1604 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1605 struct symbol_list **old_symbol_list;
1606 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
1607 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
1608 struct statement *stmt;
1610 anon->ctype.base_type = fn;
1611 old_symbol_list = function_symbol_list;
1612 function_symbol_list = &anon->symbol_list;
1613 stmt = start_function(anon);
1614 token = parse_asm(token->next, stmt);
1615 end_function(anon);
1616 function_symbol_list = old_symbol_list;
1617 add_symbol(list, anon);
1618 return token;
1621 /* Parse declaration-specifiers, if any */
1622 token = declaration_specifiers(token, &ctype, 0);
1623 decl = alloc_symbol(token->pos, SYM_NODE);
1624 decl->ctype = ctype;
1625 token = declarator(token, decl, &ident);
1627 /* Just a type declaration? */
1628 if (!ident)
1629 return expect(token, ';', "end of type declaration");
1631 /* type define declaration? */
1632 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
1634 /* Typedef's don't have meaningful storage */
1635 if (is_typedef) {
1636 ctype.modifiers &= ~MOD_STORAGE;
1637 decl->ctype.modifiers &= ~MOD_STORAGE;
1638 decl->ctype.modifiers |= MOD_USERTYPE;
1641 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1643 base_type = decl->ctype.base_type;
1644 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1645 /* K&R argument declaration? */
1646 if (lookup_type(token))
1647 return parse_k_r_arguments(token, decl, list);
1648 if (match_op(token, '{'))
1649 return parse_function_body(token, decl, list);
1651 if (!(decl->ctype.modifiers & MOD_STATIC))
1652 decl->ctype.modifiers |= MOD_EXTERN;
1653 } else if (!is_typedef && base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
1654 warning(token->pos, "void declaration");
1657 for (;;) {
1658 if (!is_typedef && match_op(token, '=')) {
1659 if (decl->ctype.modifiers & MOD_EXTERN) {
1660 warning(decl->pos, "symbol with external linkage has initializer");
1661 decl->ctype.modifiers &= ~MOD_EXTERN;
1663 token = initializer(&decl->initializer, token->next);
1665 if (!is_typedef) {
1666 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
1667 add_symbol(list, decl);
1668 fn_local_symbol(decl);
1671 check_declaration(decl);
1673 if (!match_op(token, ','))
1674 break;
1676 token = token->next;
1677 ident = NULL;
1678 decl = alloc_symbol(token->pos, SYM_NODE);
1679 decl->ctype = ctype;
1680 token = declaration_specifiers(token, &decl->ctype, 1);
1681 token = declarator(token, decl, &ident);
1682 if (!ident) {
1683 warning(token->pos, "expected identifier name in type definition");
1684 return token;
1687 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1689 /* Function declarations are automatically extern unless specifically static */
1690 base_type = decl->ctype.base_type;
1691 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1692 if (!(decl->ctype.modifiers & MOD_STATIC))
1693 decl->ctype.modifiers |= MOD_EXTERN;
1696 return expect(token, ';', "at end of declaration");
1699 struct symbol_list *translation_unit(struct token *token)
1701 while (!eof_token(token))
1702 token = external_declaration(token, &translation_unit_used_list);
1703 // They aren't needed any more
1704 clear_token_alloc();
1706 /* Evaluate the symbol list */
1707 evaluate_symbol_list(translation_unit_used_list);
1708 return translation_unit_used_list;