[PATCH] bogus initializer offsets
[smatch.git] / parse.c
blob3764b6b5688dd4c4c2c8b25b2eb8253fbe6d719e
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 // Mark the structure as needing re-examination
139 sym->examined = 0;
141 return token;
144 // private struct/union/enum type
145 if (!match_op(token, '{')) {
146 warning(token->pos, "expected declaration");
147 ctype->base_type = &bad_ctype;
148 return token;
151 sym = alloc_symbol(token->pos, type);
152 token = parse(token->next, sym);
153 ctype->base_type = sym;
154 return expect(token, '}', "at end of specifier");
157 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
159 return struct_declaration_list(token, &sym->symbol_list);
162 struct token *struct_or_union_specifier(enum type type, struct token *token, struct ctype *ctype)
164 return struct_union_enum_specifier(type, token, ctype, parse_struct_declaration);
167 typedef struct {
168 int x;
169 unsigned long long y;
170 } Num;
172 static void upper_boundary(Num *n, Num *v)
174 if (n->x > v->x)
175 return;
176 if (n->x < v->x) {
177 *n = *v;
178 return;
180 if (n->y < v->y)
181 n->y = v->y;
184 static void lower_boundary(Num *n, Num *v)
186 if (n->x < v->x)
187 return;
188 if (n->x > v->x) {
189 *n = *v;
190 return;
192 if (n->y > v->y)
193 n->y = v->y;
196 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
198 int shift = type->bit_size;
199 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
201 if (!is_unsigned)
202 shift--;
203 if (upper->x == 0 && upper->y >> shift)
204 return 0;
205 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
206 return 1;
207 return 0;
210 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
212 unsigned long long lastval = 0;
213 struct symbol *ctype = NULL, *base_type = NULL;
214 Num upper = {-1, 0}, lower = {1, 0};
216 while (token_type(token) == TOKEN_IDENT) {
217 struct token *next = token->next;
218 struct symbol *sym;
220 sym = alloc_symbol(token->pos, SYM_ENUM);
221 bind_symbol(sym, token->ident, NS_SYMBOL);
223 if (match_op(next, '=')) {
224 struct expression *expr;
225 next = constant_expression(next->next, &expr);
226 lastval = get_expression_value(expr);
227 ctype = expr->ctype;
228 } else if (!ctype) {
229 ctype = &int_ctype;
230 } else if (is_int_type(ctype)) {
231 lastval++;
232 } else {
233 error_die(token->pos, "can't increment the last enum member");
236 sym->value = lastval;
237 sym->ctype.base_type = ctype;
239 if (base_type != &bad_ctype) {
240 if (ctype->type == SYM_NODE)
241 ctype = ctype->ctype.base_type;
242 if (ctype->type == SYM_ENUM)
243 ctype = ctype->ctype.base_type;
245 * base_type rules:
246 * - if all enum's are of the same type, then
247 * the base_type is that type (two first
248 * cases)
249 * - if enums are of different types, they
250 * all have to be integer types, and the
251 * base type is "int_ctype".
252 * - otherwise the base_type is "bad_ctype".
254 if (!base_type) {
255 base_type = ctype;
256 } else if (ctype == base_type) {
257 /* nothing */
258 } else if (is_int_type(base_type) && is_int_type(ctype)) {
259 base_type = &int_ctype;
260 } else
261 base_type = &bad_ctype;
263 if (is_int_type(base_type)) {
264 Num v = {.y = lastval};
265 if (ctype->ctype.modifiers & MOD_UNSIGNED)
266 v.x = 0;
267 else if ((long long)lastval >= 0)
268 v.x = 0;
269 else
270 v.x = -1;
271 upper_boundary(&upper, &v);
272 lower_boundary(&lower, &v);
274 token = next;
275 if (!match_op(token, ','))
276 break;
277 token = token->next;
279 if (!base_type)
280 base_type = &bad_ctype;
281 else if (!is_int_type(base_type))
282 base_type = base_type;
283 else if (type_is_ok(base_type, &upper, &lower))
284 base_type = base_type;
285 else if (type_is_ok(&int_ctype, &upper, &lower))
286 base_type = &int_ctype;
287 else if (type_is_ok(&uint_ctype, &upper, &lower))
288 base_type = &uint_ctype;
289 else if (type_is_ok(&long_ctype, &upper, &lower))
290 base_type = &long_ctype;
291 else if (type_is_ok(&ulong_ctype, &upper, &lower))
292 base_type = &ulong_ctype;
293 else if (type_is_ok(&llong_ctype, &upper, &lower))
294 base_type = &llong_ctype;
295 else if (type_is_ok(&ullong_ctype, &upper, &lower))
296 base_type = &ullong_ctype;
297 else
298 base_type = &bad_ctype;
299 parent->ctype.base_type = base_type;
300 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
301 return token;
304 struct token *enum_specifier(struct token *token, struct ctype *ctype)
306 return struct_union_enum_specifier(SYM_ENUM, token, ctype, parse_enum_declaration);
309 struct token *typeof_specifier(struct token *token, struct ctype *ctype)
311 struct symbol *sym;
313 if (!match_op(token, '(')) {
314 warning(token->pos, "expected '(' after typeof");
315 return token;
317 if (lookup_type(token->next)) {
318 token = typename(token->next, &sym);
319 *ctype = sym->ctype;
320 } else {
321 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
322 token = parse_expression(token->next, &typeof_sym->initializer);
324 ctype->modifiers = 0;
325 ctype->base_type = typeof_sym;
327 return expect(token, ')', "after typeof");
330 static const char * handle_attribute(struct ctype *ctype, struct ident *attribute, struct expression *expr)
332 if (attribute == &packed_ident ||
333 attribute == &__packed___ident) {
334 ctype->alignment = 1;
335 return NULL;
337 if (attribute == &aligned_ident ||
338 attribute == &__aligned___ident) {
339 int alignment = max_alignment;
340 if (expr)
341 alignment = get_expression_value(expr);
342 ctype->alignment = alignment;
343 return NULL;
345 if (attribute == &nocast_ident) {
346 ctype->modifiers |= MOD_NOCAST;
347 return NULL;
349 if (attribute == &noderef_ident) {
350 ctype->modifiers |= MOD_NODEREF;
351 return NULL;
353 if (attribute == &safe_ident) {
354 ctype->modifiers |= MOD_SAFE;
355 return NULL;
357 if (attribute == &force_ident) {
358 ctype->modifiers |= MOD_FORCE;
359 return NULL;
361 if (attribute == &bitwise_ident) {
362 if (Wbitwise)
363 ctype->modifiers |= MOD_BITWISE;
364 return NULL;
366 if (attribute == &address_space_ident) {
367 if (!expr)
368 return "expected address space number";
369 ctype->as = get_expression_value(expr);
370 return NULL;
372 if (attribute == &context_ident) {
373 if (expr && expr->type == EXPR_COMMA) {
374 int input = get_expression_value(expr->left);
375 int output = get_expression_value(expr->right);
376 ctype->in_context = input;
377 ctype->out_context = output;
378 return NULL;
380 return "expected context input/output values";
382 if (attribute == &mode_ident ||
383 attribute == &__mode___ident) {
384 if (expr && expr->type == EXPR_SYMBOL) {
385 struct ident *ident = expr->symbol_name;
388 * Match against __QI__/__HI__/__SI__/__DI__
390 * FIXME! This is broken - we don't actually get
391 * the type information updated properly at this
392 * stage for some reason.
394 if (ident == &__QI___ident ||
395 ident == &QI_ident) {
396 ctype->modifiers |= MOD_CHAR;
397 return NULL;
399 if (ident == &__HI___ident ||
400 ident == &HI_ident) {
401 ctype->modifiers |= MOD_SHORT;
402 return NULL;
404 if (ident == &__SI___ident ||
405 ident == &SI_ident) {
406 /* Nothing? */
407 return NULL;
409 if (ident == &__DI___ident ||
410 ident == &DI_ident) {
411 ctype->modifiers |= MOD_LONGLONG;
412 return NULL;
414 if (ident == &__word___ident ||
415 ident == &word_ident) {
416 ctype->modifiers |= MOD_LONG;
417 return NULL;
419 return "unknown mode attribute";
421 return "expected attribute mode symbol";
424 /* Throw away for now.. */
425 if (attribute == &format_ident ||
426 attribute == &__format___ident ||
427 attribute == &__format_arg___ident)
428 return NULL;
429 if (attribute == &section_ident ||
430 attribute == &__section___ident)
431 return NULL;
432 if (attribute == &unused_ident ||
433 attribute == &__unused___ident)
434 return NULL;
435 if (attribute == &const_ident ||
436 attribute == &__const_ident ||
437 attribute == &__const___ident)
438 return NULL;
439 if (attribute == &noreturn_ident ||
440 attribute == &__noreturn___ident)
441 return NULL;
442 if (attribute == &regparm_ident)
443 return NULL;
444 if (attribute == &weak_ident)
445 return NULL;
446 if (attribute == &alias_ident)
447 return NULL;
448 if (attribute == &pure_ident)
449 return NULL;
450 if (attribute == &always_inline_ident)
451 return NULL;
452 if (attribute == &syscall_linkage_ident)
453 return NULL;
454 if (attribute == &visibility_ident)
455 return NULL;
456 if (attribute == &model_ident ||
457 attribute == &__model___ident)
458 return NULL;
460 return "unknown attribute";
463 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
465 ctype->modifiers = 0;
466 token = expect(token, '(', "after attribute");
467 token = expect(token, '(', "after attribute");
469 for (;;) {
470 const char *error;
471 struct ident *attribute_name;
472 struct expression *attribute_expr;
474 if (eof_token(token))
475 break;
476 if (match_op(token, ';'))
477 break;
478 if (token_type(token) != TOKEN_IDENT)
479 break;
480 attribute_name = token->ident;
481 token = token->next;
482 attribute_expr = NULL;
483 if (match_op(token, '('))
484 token = parens_expression(token, &attribute_expr, "in attribute");
485 error = handle_attribute(ctype, attribute_name, attribute_expr);
486 if (error)
487 warning(token->pos, "attribute '%s': %s", show_ident(attribute_name), error);
488 if (!match_op(token, ','))
489 break;
490 token = token->next;
493 token = expect(token, ')', "after attribute");
494 token = expect(token, ')', "after attribute");
495 return token;
498 struct symbol * ctype_integer(unsigned long spec)
500 static struct symbol *const integer_ctypes[][3] = {
501 { &llong_ctype, &sllong_ctype, &ullong_ctype },
502 { &long_ctype, &slong_ctype, &ulong_ctype },
503 { &short_ctype, &sshort_ctype, &ushort_ctype },
504 { &char_ctype, &schar_ctype, &uchar_ctype },
505 { &int_ctype, &sint_ctype, &uint_ctype },
507 struct symbol *const (*ctype)[3];
508 int sub;
510 ctype = integer_ctypes;
511 if (!(spec & MOD_LONGLONG)) {
512 ctype++;
513 if (!(spec & MOD_LONG)) {
514 ctype++;
515 if (!(spec & MOD_SHORT)) {
516 ctype++;
517 if (!(spec & MOD_CHAR))
518 ctype++;
523 sub = ((spec & MOD_UNSIGNED)
525 : ((spec & MOD_EXPLICITLY_SIGNED)
527 : 0));
529 return ctype[0][sub];
532 struct symbol * ctype_fp(unsigned long spec)
534 if (spec & MOD_LONGLONG)
535 return &ldouble_ctype;
536 if (spec & MOD_LONG)
537 return &double_ctype;
538 return &float_ctype;
541 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
543 unsigned long mod = thistype->modifiers;
545 if (mod) {
546 unsigned long old = ctype->modifiers;
547 unsigned long extra = 0, dup, conflict;
549 if (mod & old & MOD_LONG) {
550 extra = MOD_LONGLONG | MOD_LONG;
551 mod &= ~MOD_LONG;
552 old &= ~MOD_LONG;
554 dup = (mod & old) | (extra & old) | (extra & mod);
555 if (dup)
556 warning(pos, "Just how %sdo you want this type to be?",
557 modifier_string(dup));
559 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
560 if (conflict)
561 warning(pos, "You cannot have both long and short modifiers.");
563 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
564 if (conflict)
565 warning(pos, "You cannot have both signed and unsigned modifiers.");
567 // Only one storage modifier allowed, except that "inline" doesn't count.
568 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
569 conflict &= (conflict - 1);
570 if (conflict)
571 warning(pos, "multiple storage classes");
573 ctype->modifiers = old | mod | extra;
576 /* Context mask and value */
577 ctype->in_context += thistype->in_context;
578 ctype->out_context += thistype->out_context;
580 /* Alignment */
581 if (thistype->alignment & (thistype->alignment-1)) {
582 warning(pos, "I don't like non-power-of-2 alignments");
583 thistype->alignment = 0;
585 if (thistype->alignment > ctype->alignment)
586 ctype->alignment = thistype->alignment;
588 /* Address space */
589 ctype->as = thistype->as;
592 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
594 unsigned long banned, wrong;
595 unsigned long this_mod = s->ctype.modifiers;
596 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
597 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
599 if (this_mod & (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF))
600 banned = BANNED_SIZE | BANNED_SIGN;
601 else if (this_mod & MOD_SPECIALBITS)
602 banned = 0;
603 else if (s->ctype.base_type == &fp_type)
604 banned = BANNED_SIGN;
605 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
606 banned = 0;
607 else {
608 // label_type
609 // void_type
610 // bad_type
611 // vector_type <-- whatever that is
612 banned = BANNED_SIZE | BANNED_SIGN;
615 wrong = mod & banned;
616 if (wrong)
617 warning(*pos, "modifier %sis invalid in this context",
618 modifier_string (wrong));
622 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
624 struct token *token;
626 while ( (token = next) != NULL ) {
627 struct ctype thistype;
628 struct ident *ident;
629 struct symbol *s, *type;
630 unsigned long mod;
632 next = token->next;
633 if (token_type(token) != TOKEN_IDENT)
634 break;
635 ident = token->ident;
637 s = lookup_symbol(ident, NS_TYPEDEF);
638 if (!s)
639 break;
640 thistype = s->ctype;
641 mod = thistype.modifiers;
642 if (qual && (mod & ~(MOD_ATTRIBUTE | MOD_CONST | MOD_VOLATILE)))
643 break;
644 if (mod & MOD_SPECIALBITS) {
645 if (mod & MOD_STRUCTOF)
646 next = struct_or_union_specifier(SYM_STRUCT, next, &thistype);
647 else if (mod & MOD_UNIONOF)
648 next = struct_or_union_specifier(SYM_UNION, next, &thistype);
649 else if (mod & MOD_ENUMOF)
650 next = enum_specifier(next, &thistype);
651 else if (mod & MOD_ATTRIBUTE)
652 next = attribute_specifier(next, &thistype);
653 else if (mod & MOD_TYPEOF)
654 next = typeof_specifier(next, &thistype);
655 mod = thistype.modifiers;
657 type = thistype.base_type;
658 if (type) {
659 if (qual)
660 break;
661 if (ctype->base_type)
662 break;
663 /* User types only mix with qualifiers */
664 if (mod & MOD_USERTYPE) {
665 if (ctype->modifiers & MOD_SPECIFIER)
666 break;
668 ctype->base_type = type;
671 check_modifiers(&token->pos, s, ctype->modifiers);
672 apply_ctype(token->pos, &thistype, ctype);
675 /* Turn the "virtual types" into real types with real sizes etc */
676 if (!ctype->base_type) {
677 struct symbol *base = &incomplete_ctype;
680 * If we have modifiers, we'll default to an integer
681 * type, and "ctype_integer()" will turn this into
682 * a specific one.
684 if (ctype->modifiers & MOD_SPECIFIER)
685 base = &int_type;
686 ctype->base_type = base;
688 if (ctype->base_type == &int_type) {
689 ctype->base_type = ctype_integer(ctype->modifiers);
690 ctype->modifiers &= ~MOD_SPECIFIER;
691 } else if (ctype->base_type == &fp_type) {
692 ctype->base_type = ctype_fp(ctype->modifiers);
693 ctype->modifiers &= ~MOD_SPECIFIER;
695 if (ctype->modifiers & MOD_BITWISE) {
696 struct symbol *type;
697 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
698 if (!is_int_type(ctype->base_type)) {
699 warning(token->pos, "invalid modifier");
700 return token;
702 type = alloc_symbol(token->pos, SYM_BASETYPE);
703 *type = *ctype->base_type;
704 type->ctype.base_type = ctype->base_type;
705 type->type = SYM_RESTRICT;
706 type->ctype.modifiers &= ~MOD_SPECIFIER;
707 ctype->base_type = type;
709 return token;
712 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
714 struct expression *expr = NULL;
716 token = parse_expression(token, &expr);
717 sym->array_size = expr;
718 return token;
721 static struct token *parameter_type_list(struct token *, struct symbol *);
722 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
724 static struct token *handle_attributes(struct token *token, struct ctype *ctype)
726 for (;;) {
727 if (token_type(token) != TOKEN_IDENT)
728 break;
729 if (match_idents(token, &__attribute___ident, &__attribute_ident, NULL)) {
730 struct ctype thistype = { 0, };
731 token = attribute_specifier(token->next, &thistype);
732 apply_ctype(token->pos, &thistype, ctype);
733 continue;
735 if (match_idents(token, &asm_ident, &__asm_ident, &__asm___ident)) {
736 struct expression *expr;
737 token = expect(token->next, '(', "after asm");
738 token = parse_expression(token->next, &expr);
739 token = expect(token, ')', "after asm");
740 continue;
742 break;
744 return token;
747 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
749 struct ctype *ctype = &decl->ctype;
751 if (p && token_type(token) == TOKEN_IDENT) {
752 *p = token->ident;
753 token = token->next;
756 for (;;) {
757 token = handle_attributes(token, ctype);
759 if (token_type(token) != TOKEN_SPECIAL)
760 return token;
763 * This can be either a parameter list or a grouping.
764 * For the direct (non-abstract) case, we know if must be
765 * a parameter list if we already saw the identifier.
766 * For the abstract case, we know if must be a parameter
767 * list if it is empty or starts with a type.
769 if (token->special == '(') {
770 struct symbol *sym;
771 struct token *next = token->next;
772 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
774 if (!fn) {
775 struct symbol *base_type = ctype->base_type;
776 token = declarator(next, decl, p);
777 token = expect(token, ')', "in nested declarator");
778 while (ctype->base_type != base_type)
779 ctype = &ctype->base_type->ctype;
780 p = NULL;
781 continue;
784 sym = indirect(token->pos, ctype, SYM_FN);
785 token = parameter_type_list(next, sym);
786 token = expect(token, ')', "in function declarator");
787 continue;
789 if (token->special == '[') {
790 struct symbol *array = indirect(token->pos, ctype, SYM_ARRAY);
791 token = abstract_array_declarator(token->next, array);
792 token = expect(token, ']', "in abstract_array_declarator");
793 ctype = &array->ctype;
794 continue;
796 break;
798 return token;
801 static struct token *pointer(struct token *token, struct ctype *ctype)
803 unsigned long modifiers;
804 struct symbol *base_type;
806 modifiers = ctype->modifiers & ~(MOD_TYPEDEF | MOD_ATTRIBUTE);
807 base_type = ctype->base_type;
808 ctype->modifiers = modifiers;
810 while (match_op(token,'*')) {
811 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
812 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
813 ptr->ctype.as = ctype->as;
814 ptr->ctype.in_context += ctype->in_context;
815 ptr->ctype.out_context += ctype->out_context;
816 ptr->ctype.base_type = base_type;
818 base_type = ptr;
819 ctype->modifiers = modifiers & MOD_STORAGE;
820 ctype->base_type = base_type;
821 ctype->as = 0;
822 ctype->in_context = 0;
823 ctype->out_context = 0;
825 token = declaration_specifiers(token->next, ctype, 1);
826 modifiers = ctype->modifiers;
828 return token;
831 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
833 token = pointer(token, &sym->ctype);
834 return direct_declarator(token, sym, p);
837 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
839 struct ctype *ctype = &decl->ctype;
840 struct expression *expr;
841 struct symbol *bitfield;
842 long long width;
844 if (!is_int_type(ctype->base_type)) {
845 warning(token->pos, "invalid bitfield specifier for type %s.",
846 show_typename(ctype->base_type));
847 // Parse this to recover gracefully.
848 return conditional_expression(token->next, &expr);
851 bitfield = indirect(token->pos, ctype, SYM_BITFIELD);
852 token = conditional_expression(token->next, &expr);
853 width = get_expression_value(expr);
854 bitfield->bit_size = width;
856 if (width < 0 || width > INT_MAX) {
857 warning(token->pos, "invalid bitfield width, %lld.", width);
858 width = -1;
859 } else if (decl->ident && width == 0) {
860 warning(token->pos, "invalid named zero-width bitfield `%s'",
861 show_ident(decl->ident));
862 width = -1;
863 } else if (decl->ident) {
864 struct symbol *base_type = bitfield->ctype.base_type;
865 int is_signed = !(base_type->ctype.modifiers & MOD_UNSIGNED);
866 if (width == 1 && is_signed) {
867 // Valid values are either {-1;0} or {0}, depending on integer
868 // representation. The latter makes for very efficient code...
869 warning(token->pos, "dubious one-bit signed bitfield");
871 if (Wdefault_bitfield_sign &&
872 base_type->type != SYM_ENUM &&
873 !(base_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
874 is_signed) {
875 // The sign of bitfields is unspecified by default.
876 warning (token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
879 bitfield->bit_size = width;
880 return token;
883 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
885 while (!match_op(token, '}')) {
886 struct ctype ctype = {0, };
888 token = declaration_specifiers(token, &ctype, 0);
889 for (;;) {
890 struct ident *ident = NULL;
891 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
892 decl->ctype = ctype;
893 token = declarator(token, decl, &ident);
894 decl->ident = ident;
895 if (match_op(token, ':')) {
896 token = handle_bitfield(token, decl);
897 token = handle_attributes(token, &decl->ctype);
899 add_symbol(list, decl);
900 if (!match_op(token, ','))
901 break;
902 token = token->next;
904 if (!match_op(token, ';')) {
905 warning(token->pos, "expected ; at end of declaration");
906 break;
908 token = token->next;
910 return token;
913 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
915 struct ident *ident = NULL;
916 struct symbol *sym;
917 struct ctype ctype = { 0, };
919 token = declaration_specifiers(token, &ctype, 0);
920 sym = alloc_symbol(token->pos, SYM_NODE);
921 sym->ctype = ctype;
922 *tree = sym;
923 token = declarator(token, sym, &ident);
924 sym->ident = ident;
925 return token;
928 struct token *typename(struct token *token, struct symbol **p)
930 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
931 *p = sym;
932 token = declaration_specifiers(token, &sym->ctype, 0);
933 return declarator(token, sym, NULL);
936 struct token *expression_statement(struct token *token, struct expression **tree)
938 token = parse_expression(token, tree);
939 return expect(token, ';', "at end of statement");
942 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
943 struct expression_list **inout)
945 struct expression *expr;
947 /* Allow empty operands */
948 if (match_op(token->next, ':') || match_op(token->next, ')'))
949 return token->next;
950 do {
951 struct ident *ident = NULL;
952 if (match_op(token->next, '[') &&
953 token_type(token->next->next) == TOKEN_IDENT &&
954 match_op(token->next->next->next, ']')) {
955 ident = token->next->next->ident;
956 token = token->next->next->next;
958 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
959 token = primary_expression(token->next, &expr);
960 add_expression(inout, expr);
961 token = parens_expression(token, &expr, "in asm parameter");
962 add_expression(inout, expr);
963 } while (match_op(token, ','));
964 return token;
967 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
968 struct expression_list **clobbers)
970 struct expression *expr;
972 do {
973 token = primary_expression(token->next, &expr);
974 add_expression(clobbers, expr);
975 } while (match_op(token, ','));
976 return token;
979 static struct token *parse_asm(struct token *token, struct statement *stmt)
981 stmt->type = STMT_ASM;
982 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
983 token = token->next;
985 token = expect(token, '(', "after asm");
986 token = parse_expression(token, &stmt->asm_string);
987 if (match_op(token, ':'))
988 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
989 if (match_op(token, ':'))
990 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
991 if (match_op(token, ':'))
992 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
993 token = expect(token, ')', "after asm");
994 return expect(token, ';', "at end of asm-statement");
997 /* Make a statement out of an expression */
998 static struct statement *make_statement(struct expression *expr)
1000 struct statement *stmt;
1002 if (!expr)
1003 return NULL;
1004 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1005 stmt->expression = expr;
1006 return stmt;
1010 * All iterators have two symbols associated with them:
1011 * the "continue" and "break" symbols, which are targets
1012 * for continue and break statements respectively.
1014 * They are in a special name-space, but they follow
1015 * all the normal visibility rules, so nested iterators
1016 * automatically work right.
1018 static void start_iterator(struct statement *stmt)
1020 struct symbol *cont, *brk;
1022 start_symbol_scope();
1023 cont = alloc_symbol(stmt->pos, SYM_NODE);
1024 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1025 brk = alloc_symbol(stmt->pos, SYM_NODE);
1026 bind_symbol(brk, &break_ident, NS_ITERATOR);
1028 stmt->type = STMT_ITERATOR;
1029 stmt->iterator_break = brk;
1030 stmt->iterator_continue = cont;
1031 fn_local_symbol(brk);
1032 fn_local_symbol(cont);
1035 static void end_iterator(struct statement *stmt)
1037 end_symbol_scope();
1040 static struct statement *start_function(struct symbol *sym)
1042 struct symbol *ret;
1043 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1045 start_function_scope();
1046 ret = alloc_symbol(sym->pos, SYM_NODE);
1047 ret->ctype = sym->ctype.base_type->ctype;
1048 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1049 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1050 bind_symbol(ret, &return_ident, NS_ITERATOR);
1051 stmt->ret = ret;
1052 fn_local_symbol(ret);
1054 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1055 current_fn = sym;
1057 return stmt;
1060 static void end_function(struct symbol *sym)
1062 current_fn = NULL;
1063 end_function_scope();
1067 * A "switch()" statement, like an iterator, has a
1068 * the "break" symbol associated with it. It works
1069 * exactly like the iterator break - it's the target
1070 * for any break-statements in scope, and means that
1071 * "break" handling doesn't even need to know whether
1072 * it's breaking out of an iterator or a switch.
1074 * In addition, the "case" symbol is a marker for the
1075 * case/default statements to find the switch statement
1076 * that they are associated with.
1078 static void start_switch(struct statement *stmt)
1080 struct symbol *brk, *switch_case;
1082 start_symbol_scope();
1083 brk = alloc_symbol(stmt->pos, SYM_NODE);
1084 bind_symbol(brk, &break_ident, NS_ITERATOR);
1086 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1087 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1088 switch_case->stmt = stmt;
1090 stmt->type = STMT_SWITCH;
1091 stmt->switch_break = brk;
1092 stmt->switch_case = switch_case;
1094 fn_local_symbol(brk);
1095 fn_local_symbol(switch_case);
1098 static void end_switch(struct statement *stmt)
1100 if (!stmt->switch_case->symbol_list)
1101 warning(stmt->pos, "switch with no cases");
1102 end_symbol_scope();
1105 static void add_case_statement(struct statement *stmt)
1107 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1108 struct symbol *sym;
1110 if (!target) {
1111 warning(stmt->pos, "not in switch scope");
1112 stmt->type = STMT_NONE;
1113 return;
1115 sym = alloc_symbol(stmt->pos, SYM_NODE);
1116 add_symbol(&target->symbol_list, sym);
1117 sym->stmt = stmt;
1118 stmt->case_label = sym;
1119 fn_local_symbol(sym);
1122 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1124 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1126 if (!target)
1127 error_die(token->pos, "internal error: return without a function target");
1128 stmt->type = STMT_RETURN;
1129 stmt->ret_target = target;
1130 return expression_statement(token->next, &stmt->ret_value);
1133 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1135 struct symbol_list *syms;
1136 struct expression *e1, *e2, *e3;
1137 struct statement *iterator;
1139 start_iterator(stmt);
1140 token = expect(token->next, '(', "after 'for'");
1142 syms = NULL;
1143 e1 = NULL;
1144 /* C99 variable declaration? */
1145 if (lookup_type(token)) {
1146 token = external_declaration(token, &syms);
1147 } else {
1148 token = parse_expression(token, &e1);
1149 token = expect(token, ';', "in 'for'");
1151 token = parse_expression(token, &e2);
1152 token = expect(token, ';', "in 'for'");
1153 token = parse_expression(token, &e3);
1154 token = expect(token, ')', "in 'for'");
1155 token = statement(token, &iterator);
1157 stmt->iterator_syms = syms;
1158 stmt->iterator_pre_statement = make_statement(e1);
1159 stmt->iterator_pre_condition = e2;
1160 stmt->iterator_post_statement = make_statement(e3);
1161 stmt->iterator_post_condition = e2;
1162 stmt->iterator_statement = iterator;
1163 end_iterator(stmt);
1165 return token;
1168 struct token *parse_while_statement(struct token *token, struct statement *stmt)
1170 struct expression *expr;
1171 struct statement *iterator;
1173 start_iterator(stmt);
1174 token = parens_expression(token->next, &expr, "after 'while'");
1175 token = statement(token, &iterator);
1177 stmt->iterator_pre_condition = expr;
1178 stmt->iterator_post_condition = expr;
1179 stmt->iterator_statement = iterator;
1180 end_iterator(stmt);
1182 return token;
1185 struct token *parse_do_statement(struct token *token, struct statement *stmt)
1187 struct expression *expr;
1188 struct statement *iterator;
1190 start_iterator(stmt);
1191 token = statement(token->next, &iterator);
1192 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1193 token = token->next;
1194 else
1195 warning(token->pos, "expected 'while' after 'do'");
1196 token = parens_expression(token, &expr, "after 'do-while'");
1198 stmt->iterator_post_condition = expr;
1199 stmt->iterator_statement = iterator;
1200 end_iterator(stmt);
1202 return expect(token, ';', "after statement");
1205 static struct token *statement(struct token *token, struct statement **tree)
1207 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1209 *tree = stmt;
1210 if (token_type(token) == TOKEN_IDENT) {
1211 if (token->ident == &if_ident) {
1212 stmt->type = STMT_IF;
1213 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1214 token = statement(token, &stmt->if_true);
1215 if (token_type(token) != TOKEN_IDENT)
1216 return token;
1217 if (token->ident != &else_ident)
1218 return token;
1219 return statement(token->next, &stmt->if_false);
1222 if (token->ident == &return_ident)
1223 return parse_return_statement(token, stmt);
1225 if (token->ident == &break_ident || token->ident == &continue_ident) {
1226 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1227 stmt->type = STMT_GOTO;
1228 stmt->goto_label = target;
1229 if (!target)
1230 warning(stmt->pos, "break/continue not in iterator scope");
1231 return expect(token->next, ';', "at end of statement");
1233 if (token->ident == &default_ident) {
1234 token = token->next;
1235 goto default_statement;
1237 if (token->ident == &case_ident) {
1238 token = parse_expression(token->next, &stmt->case_expression);
1239 if (match_op(token, SPECIAL_ELLIPSIS))
1240 token = parse_expression(token->next, &stmt->case_to);
1241 default_statement:
1242 stmt->type = STMT_CASE;
1243 token = expect(token, ':', "after default/case");
1244 add_case_statement(stmt);
1245 return statement(token, &stmt->case_statement);
1247 if (token->ident == &switch_ident) {
1248 stmt->type = STMT_SWITCH;
1249 start_switch(stmt);
1250 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1251 token = statement(token, &stmt->switch_statement);
1252 end_switch(stmt);
1253 return token;
1255 if (token->ident == &for_ident)
1256 return parse_for_statement(token, stmt);
1258 if (token->ident == &while_ident)
1259 return parse_while_statement(token, stmt);
1261 if (token->ident == &do_ident)
1262 return parse_do_statement(token, stmt);
1264 if (token->ident == &goto_ident) {
1265 stmt->type = STMT_GOTO;
1266 token = token->next;
1267 if (match_op(token, '*')) {
1268 token = parse_expression(token->next, &stmt->goto_expression);
1269 add_statement(&function_computed_goto_list, stmt);
1270 } else if (token_type(token) == TOKEN_IDENT) {
1271 stmt->goto_label = label_symbol(token);
1272 token = token->next;
1273 } else {
1274 warning(token->pos, "Expected identifier or goto expression");
1276 return expect(token, ';', "at end of statement");
1278 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1279 return parse_asm(token->next, stmt);
1281 if (token->ident == &__context___ident) {
1282 stmt->type = STMT_CONTEXT;
1283 token = parse_expression(token->next, &stmt->expression);
1284 return expect(token, ';', "at end of statement");
1286 if (token->ident == &__range___ident) {
1287 stmt->type = STMT_RANGE;
1288 token = assignment_expression(token->next, &stmt->range_expression);
1289 token = expect(token, ',', "after range expression");
1290 token = assignment_expression(token, &stmt->range_low);
1291 token = expect(token, ',', "after low range");
1292 token = assignment_expression(token, &stmt->range_high);
1293 return expect(token, ';', "after range statement");
1295 if (match_op(token->next, ':')) {
1296 stmt->type = STMT_LABEL;
1297 stmt->label_identifier = label_symbol(token);
1298 return statement(token->next->next, &stmt->label_statement);
1302 if (match_op(token, '{')) {
1303 stmt->type = STMT_COMPOUND;
1304 start_symbol_scope();
1305 token = compound_statement(token->next, stmt);
1306 end_symbol_scope();
1308 return expect(token, '}', "at end of compound statement");
1311 stmt->type = STMT_EXPRESSION;
1312 return expression_statement(token, &stmt->expression);
1315 static struct token * statement_list(struct token *token, struct statement_list **list, struct symbol_list **syms)
1317 for (;;) {
1318 struct statement * stmt;
1319 if (eof_token(token))
1320 break;
1321 if (match_op(token, '}'))
1322 break;
1323 if (lookup_type(token)) {
1324 if (warn_on_mixed && *list)
1325 warning(token->pos, "mixing declarations and code");
1326 token = external_declaration(token, syms);
1327 continue;
1329 token = statement(token, &stmt);
1330 add_statement(list, stmt);
1332 return token;
1335 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
1337 struct symbol_list **list = &fn->arguments;
1339 if (match_op(token, ')')) {
1340 // No warning for "void oink ();"
1341 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1342 if (!match_op(token->next, ';'))
1343 warning(token->pos, "non-ANSI function declaration");
1344 return token;
1347 for (;;) {
1348 struct symbol *sym;
1350 if (match_op(token, SPECIAL_ELLIPSIS)) {
1351 if (!*list)
1352 warning(token->pos, "variadic functions must have one named argument");
1353 fn->variadic = 1;
1354 token = token->next;
1355 break;
1358 sym = alloc_symbol(token->pos, SYM_NODE);
1359 token = parameter_declaration(token, &sym);
1360 if (sym->ctype.base_type == &void_ctype) {
1361 /* Special case: (void) */
1362 if (!*list && !sym->ident)
1363 break;
1364 warning(token->pos, "void parameter");
1366 add_symbol(list, sym);
1367 if (!match_op(token, ','))
1368 break;
1369 token = token->next;
1372 return token;
1375 struct token *compound_statement(struct token *token, struct statement *stmt)
1377 token = statement_list(token, &stmt->stmts, &stmt->syms);
1378 return token;
1381 static struct expression *identifier_expression(struct token *token)
1383 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1384 expr->expr_ident = token->ident;
1385 return expr;
1388 static struct expression *index_expression(struct expression *from, struct expression *to)
1390 int idx_from, idx_to;
1391 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1393 idx_from = get_expression_value(from);
1394 idx_to = idx_from;
1395 if (to) {
1396 idx_to = get_expression_value(to);
1397 if (idx_to < idx_from || idx_from < 0)
1398 warning(from->pos, "nonsense array initializer index range");
1400 expr->idx_from = idx_from;
1401 expr->idx_to = idx_to;
1402 return expr;
1405 static struct token *single_initializer(struct expression **ep, struct token *token)
1407 int expect_equal = 0;
1408 struct token *next = token->next;
1409 struct expression **tail = ep;
1410 int nested;
1412 *ep = NULL;
1414 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1415 struct expression *expr = identifier_expression(token);
1416 warning(token->pos, "obsolete struct initializer, use C99 syntax");
1417 token = initializer(&expr->ident_expression, next->next);
1418 if (expr->ident_expression)
1419 *ep = expr;
1420 return token;
1423 for (tail = ep, nested = 0; ; nested++, next = token->next) {
1424 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
1425 struct expression *expr = identifier_expression(next);
1426 *tail = expr;
1427 tail = &expr->ident_expression;
1428 expect_equal = 1;
1429 token = next->next;
1430 } else if (match_op(token, '[')) {
1431 struct expression *from = NULL, *to = NULL, *expr;
1432 token = constant_expression(token->next, &from);
1433 if (match_op(token, SPECIAL_ELLIPSIS))
1434 token = constant_expression(token->next, &to);
1435 expr = index_expression(from, to);
1436 *tail = expr;
1437 tail = &expr->idx_expression;
1438 token = expect(token, ']', "at end of initializer index");
1439 if (nested)
1440 expect_equal = 1;
1441 } else {
1442 break;
1445 if (nested && !expect_equal) {
1446 if (!match_op(token, '='))
1447 warning(token->pos, "obsolete array initializer, use C99 syntax");
1448 else
1449 expect_equal = 1;
1451 if (expect_equal)
1452 token = expect(token, '=', "at end of initializer index");
1454 token = initializer(tail, token);
1455 if (!*tail)
1456 *ep = NULL;
1457 return token;
1460 static struct token *initializer_list(struct expression_list **list, struct token *token)
1462 struct expression *expr;
1464 for (;;) {
1465 token = single_initializer(&expr, token);
1466 if (!expr)
1467 break;
1468 add_expression(list, expr);
1469 if (!match_op(token, ','))
1470 break;
1471 token = token->next;
1473 return token;
1476 struct token *initializer(struct expression **tree, struct token *token)
1478 if (match_op(token, '{')) {
1479 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1480 *tree = expr;
1481 token = initializer_list(&expr->expr_list, token->next);
1482 return expect(token, '}', "at end of initializer");
1484 return assignment_expression(token, tree);
1487 static void declare_argument(struct symbol *sym, struct symbol *fn)
1489 if (!sym->ident) {
1490 warning(sym->pos, "no identifier for function argument");
1491 return;
1493 bind_symbol(sym, sym->ident, NS_SYMBOL);
1496 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1497 struct symbol_list **list)
1499 struct symbol_list **old_symbol_list;
1500 struct symbol *base_type = decl->ctype.base_type;
1501 struct statement *stmt, **p;
1502 struct symbol *arg;
1504 old_symbol_list = function_symbol_list;
1505 if (decl->ctype.modifiers & MOD_INLINE) {
1506 function_symbol_list = &decl->inline_symbol_list;
1507 p = &base_type->inline_stmt;
1508 } else {
1509 function_symbol_list = &decl->symbol_list;
1510 p = &base_type->stmt;
1512 function_computed_target_list = NULL;
1513 function_computed_goto_list = NULL;
1515 if (decl->ctype.modifiers & MOD_EXTERN) {
1516 if (!(decl->ctype.modifiers & MOD_INLINE))
1517 warning(decl->pos, "function with external linkage has definition");
1519 if (!(decl->ctype.modifiers & MOD_STATIC))
1520 decl->ctype.modifiers |= MOD_EXTERN;
1522 stmt = start_function(decl);
1524 *p = stmt;
1525 FOR_EACH_PTR (base_type->arguments, arg) {
1526 declare_argument(arg, base_type);
1527 } END_FOR_EACH_PTR(arg);
1529 token = compound_statement(token->next, stmt);
1531 end_function(decl);
1532 if (!(decl->ctype.modifiers & MOD_INLINE))
1533 add_symbol(list, decl);
1534 check_declaration(decl);
1535 function_symbol_list = old_symbol_list;
1536 if (function_computed_goto_list) {
1537 if (!function_computed_target_list)
1538 warning(decl->pos, "function has computed goto but no targets?");
1539 else {
1540 struct statement *stmt;
1541 FOR_EACH_PTR(function_computed_goto_list, stmt) {
1542 stmt->target_list = function_computed_target_list;
1543 } END_FOR_EACH_PTR(stmt);
1546 return expect(token, '}', "at end of function");
1549 static void promote_k_r_types(struct symbol *arg)
1551 struct symbol *base = arg->ctype.base_type;
1552 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
1553 arg->ctype.base_type = &int_ctype;
1557 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
1559 struct symbol_list *real_args = fn->ctype.base_type->arguments;
1560 struct symbol *arg;
1562 FOR_EACH_PTR(real_args, arg) {
1563 struct symbol *type;
1565 /* This is quadratic in the number of arguments. We _really_ don't care */
1566 FOR_EACH_PTR(argtypes, type) {
1567 if (type->ident == arg->ident)
1568 goto match;
1569 } END_FOR_EACH_PTR(type);
1570 warning(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
1571 continue;
1572 match:
1573 type->used = 1;
1574 /* "char" and "short" promote to "int" */
1575 promote_k_r_types(type);
1577 arg->ctype = type->ctype;
1578 } END_FOR_EACH_PTR(arg);
1580 FOR_EACH_PTR(argtypes, arg) {
1581 if (!arg->used)
1582 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
1583 } END_FOR_EACH_PTR(arg);
1587 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
1588 struct symbol_list **list)
1590 struct symbol_list *args = NULL;
1592 warning(token->pos, "non-ANSI function declaration");
1593 do {
1594 token = external_declaration(token, &args);
1595 } while (lookup_type(token));
1597 apply_k_r_types(args, decl);
1599 if (!match_op(token, '{')) {
1600 warning(token->pos, "expected function body");
1601 return token;
1603 return parse_function_body(token, decl, list);
1607 static struct token *external_declaration(struct token *token, struct symbol_list **list)
1609 struct ident *ident = NULL;
1610 struct symbol *decl;
1611 struct ctype ctype = { 0, };
1612 struct symbol *base_type;
1613 int is_typedef;
1615 /* Top-level inline asm? */
1616 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1617 struct symbol_list **old_symbol_list;
1618 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
1619 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
1620 struct statement *stmt;
1622 anon->ctype.base_type = fn;
1623 old_symbol_list = function_symbol_list;
1624 function_symbol_list = &anon->symbol_list;
1625 stmt = start_function(anon);
1626 token = parse_asm(token->next, stmt);
1627 end_function(anon);
1628 function_symbol_list = old_symbol_list;
1629 add_symbol(list, anon);
1630 return token;
1633 /* Parse declaration-specifiers, if any */
1634 token = declaration_specifiers(token, &ctype, 0);
1635 decl = alloc_symbol(token->pos, SYM_NODE);
1636 decl->ctype = ctype;
1637 token = declarator(token, decl, &ident);
1639 /* Just a type declaration? */
1640 if (!ident)
1641 return expect(token, ';', "end of type declaration");
1643 /* type define declaration? */
1644 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
1646 /* Typedef's don't have meaningful storage */
1647 if (is_typedef) {
1648 ctype.modifiers &= ~MOD_STORAGE;
1649 decl->ctype.modifiers &= ~MOD_STORAGE;
1650 decl->ctype.modifiers |= MOD_USERTYPE;
1653 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1655 base_type = decl->ctype.base_type;
1656 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1657 /* K&R argument declaration? */
1658 if (lookup_type(token))
1659 return parse_k_r_arguments(token, decl, list);
1660 if (match_op(token, '{'))
1661 return parse_function_body(token, decl, list);
1663 if (!(decl->ctype.modifiers & MOD_STATIC))
1664 decl->ctype.modifiers |= MOD_EXTERN;
1665 } else if (!is_typedef && base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
1666 warning(token->pos, "void declaration");
1669 for (;;) {
1670 if (!is_typedef && match_op(token, '=')) {
1671 if (decl->ctype.modifiers & MOD_EXTERN) {
1672 warning(decl->pos, "symbol with external linkage has initializer");
1673 decl->ctype.modifiers &= ~MOD_EXTERN;
1675 token = initializer(&decl->initializer, token->next);
1677 if (!is_typedef) {
1678 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
1679 add_symbol(list, decl);
1680 fn_local_symbol(decl);
1683 check_declaration(decl);
1685 if (!match_op(token, ','))
1686 break;
1688 token = token->next;
1689 ident = NULL;
1690 decl = alloc_symbol(token->pos, SYM_NODE);
1691 decl->ctype = ctype;
1692 token = declaration_specifiers(token, &decl->ctype, 1);
1693 token = declarator(token, decl, &ident);
1694 if (!ident) {
1695 warning(token->pos, "expected identifier name in type definition");
1696 return token;
1699 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1701 /* Function declarations are automatically extern unless specifically static */
1702 base_type = decl->ctype.base_type;
1703 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1704 if (!(decl->ctype.modifiers & MOD_STATIC))
1705 decl->ctype.modifiers |= MOD_EXTERN;
1708 return expect(token, ';', "at end of declaration");
1711 struct symbol_list *translation_unit(struct token *token)
1713 while (!eof_token(token))
1714 token = external_declaration(token, &translation_unit_used_list);
1715 // They aren't needed any more
1716 clear_token_alloc();
1718 /* Evaluate the symbol list */
1719 evaluate_symbol_list(translation_unit_used_list);
1720 return translation_unit_used_list;