[PATCH] trivial ansi-c declear
[smatch.git] / parse.c
blob859f57185902965451ff88f4a1d424c16749c966
1 /*
2 * Stupid C parser, version 1e-6.
4 * Let's see how hard this is to do.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003 Linus Torvalds
9 * Licensed under the Open Software License version 1.1
12 #include <stdarg.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <fcntl.h>
20 #include "lib.h"
21 #include "token.h"
22 #include "parse.h"
23 #include "symbol.h"
24 #include "scope.h"
25 #include "expression.h"
26 #include "target.h"
28 static struct symbol_list **function_symbol_list;
29 struct symbol_list *function_computed_target_list;
30 struct statement_list *function_computed_goto_list;
32 // Add a symbol to the list of function-local symbols
33 #define fn_local_symbol(x) add_symbol(function_symbol_list, (x))
35 static struct token *statement(struct token *token, struct statement **tree);
36 static struct token *external_declaration(struct token *token, struct symbol_list **list);
38 static int match_idents(struct token *token, ...)
40 va_list args;
42 if (token_type(token) != TOKEN_IDENT)
43 return 0;
45 va_start(args, token);
46 for (;;) {
47 struct ident * next = va_arg(args, struct ident *);
48 if (!next)
49 return 0;
50 if (token->ident == next)
51 return 1;
56 struct statement *alloc_statement(struct position pos, int type)
58 struct statement *stmt = __alloc_statement(0);
59 stmt->type = type;
60 stmt->pos = pos;
61 return stmt;
64 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
66 static struct symbol * indirect(struct position pos, struct ctype *ctype, int type)
68 struct symbol *sym = alloc_symbol(pos, type);
70 sym->ctype.base_type = ctype->base_type;
71 sym->ctype.modifiers = ctype->modifiers & ~MOD_STORAGE;
73 ctype->base_type = sym;
74 ctype->modifiers &= MOD_STORAGE;
75 return sym;
78 static struct symbol *lookup_or_create_symbol(enum namespace ns, enum type type, struct token *token)
80 struct symbol *sym = lookup_symbol(token->ident, ns);
81 if (!sym) {
82 sym = alloc_symbol(token->pos, type);
83 sym->ident = token->ident;
84 bind_symbol(sym, token->ident, ns);
85 if (type == SYM_LABEL)
86 fn_local_symbol(sym);
88 return sym;
92 * NOTE! NS_LABEL is not just a different namespace,
93 * it also ends up using function scope instead of the
94 * regular symbol scope.
96 struct symbol *label_symbol(struct token *token)
98 return lookup_or_create_symbol(NS_LABEL, SYM_LABEL, token);
101 struct token *struct_union_enum_specifier(enum type type,
102 struct token *token, struct ctype *ctype,
103 struct token *(*parse)(struct token *, struct symbol *))
105 struct symbol *sym;
107 ctype->modifiers = 0;
108 if (token_type(token) == TOKEN_IDENT) {
109 sym = lookup_symbol(token->ident, NS_STRUCT);
110 if (!sym ||
111 (sym->scope != block_scope &&
112 (match_op(token->next,';') || match_op(token->next,'{')))) {
113 // Either a new symbol, or else an out-of-scope
114 // symbol being redefined.
115 sym = alloc_symbol(token->pos, type);
116 sym->ident = token->ident;
117 bind_symbol(sym, token->ident, NS_STRUCT);
119 if (sym->type != type)
120 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
121 token = token->next;
122 ctype->base_type = sym;
123 if (match_op(token, '{')) {
124 // The following test is actually wrong for empty
125 // structs, but (1) they are not C99, (2) gcc does
126 // the same thing, and (3) it's easier.
127 if (sym->symbol_list)
128 error_die(token->pos, "redefinition of %s", show_typename (sym));
129 token = parse(token->next, sym);
130 token = expect(token, '}', "at end of struct-union-enum-specifier");
132 return token;
135 // private struct/union/enum type
136 if (!match_op(token, '{')) {
137 warning(token->pos, "expected declaration");
138 ctype->base_type = &bad_type;
139 return token;
142 sym = alloc_symbol(token->pos, type);
143 token = parse(token->next, sym);
144 ctype->base_type = sym;
145 return expect(token, '}', "at end of specifier");
148 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
150 return struct_declaration_list(token, &sym->symbol_list);
153 struct token *struct_or_union_specifier(enum type type, struct token *token, struct ctype *ctype)
155 return struct_union_enum_specifier(type, token, ctype, parse_struct_declaration);
158 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
160 int nextval = 0;
161 while (token_type(token) == TOKEN_IDENT) {
162 struct token *next = token->next;
163 struct symbol *sym;
165 sym = alloc_symbol(token->pos, SYM_ENUM);
166 bind_symbol(sym, token->ident, NS_SYMBOL);
167 sym->ctype.base_type = parent;
168 parent->ctype.base_type = &int_ctype;
170 if (match_op(next, '=')) {
171 struct expression *expr;
172 next = constant_expression(next->next, &expr);
173 nextval = get_expression_value(expr);
175 sym->value = nextval;
177 token = next;
178 if (!match_op(token, ','))
179 break;
180 token = token->next;
181 nextval = nextval + 1;
183 return token;
186 struct token *enum_specifier(struct token *token, struct ctype *ctype)
188 return struct_union_enum_specifier(SYM_ENUM, token, ctype, parse_enum_declaration);
191 struct token *typeof_specifier(struct token *token, struct ctype *ctype)
193 struct symbol *sym;
195 if (!match_op(token, '(')) {
196 warning(token->pos, "expected '(' after typeof");
197 return token;
199 if (lookup_type(token->next)) {
200 token = typename(token->next, &sym);
201 *ctype = sym->ctype;
202 } else {
203 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
204 token = parse_expression(token->next, &typeof_sym->initializer);
206 ctype->modifiers = 0;
207 ctype->base_type = typeof_sym;
209 return expect(token, ')', "after typeof");
212 static const char * handle_attribute(struct ctype *ctype, struct ident *attribute, struct expression *expr)
214 if (attribute == &packed_ident ||
215 attribute == &__packed___ident) {
216 ctype->alignment = 1;
217 return NULL;
219 if (attribute == &aligned_ident ||
220 attribute == &__aligned___ident) {
221 int alignment = max_alignment;
222 if (expr)
223 alignment = get_expression_value(expr);
224 ctype->alignment = alignment;
225 return NULL;
227 if (attribute == &nocast_ident) {
228 ctype->modifiers |= MOD_NOCAST;
229 return NULL;
231 if (attribute == &noderef_ident) {
232 ctype->modifiers |= MOD_NODEREF;
233 return NULL;
235 if (attribute == &safe_ident) {
236 ctype->modifiers |= MOD_SAFE;
237 return NULL;
239 if (attribute == &force_ident) {
240 ctype->modifiers |= MOD_FORCE;
241 return NULL;
243 if (attribute == &bitwise_ident) {
244 if (Wbitwise)
245 ctype->modifiers |= MOD_BITWISE;
246 return NULL;
248 if (attribute == &address_space_ident) {
249 if (!expr)
250 return "expected address space number";
251 ctype->as = get_expression_value(expr);
252 return NULL;
254 if (attribute == &context_ident) {
255 if (expr && expr->type == EXPR_COMMA) {
256 int mask = get_expression_value(expr->left);
257 int value = get_expression_value(expr->right);
258 if (value & ~mask)
259 return "nonsense attribute types";
260 ctype->contextmask |= mask;
261 ctype->context |= value;
262 return NULL;
264 return "expected context mask and value";
266 if (attribute == &mode_ident ||
267 attribute == &__mode___ident) {
268 if (expr && expr->type == EXPR_SYMBOL) {
269 struct ident *ident = expr->symbol_name;
272 * Match against __QI__/__HI__/__SI__/__DI__
274 * FIXME! This is broken - we don't actually get
275 * the type information updated properly at this
276 * stage for some reason.
278 if (ident == &__QI___ident ||
279 ident == &QI_ident) {
280 ctype->modifiers |= MOD_CHAR;
281 ctype->base_type = ctype_integer(ctype->modifiers);
282 return NULL;
284 if (ident == &__HI___ident ||
285 ident == &HI_ident) {
286 ctype->modifiers |= MOD_SHORT;
287 ctype->base_type = ctype_integer(ctype->modifiers);
288 return NULL;
290 if (ident == &__SI___ident ||
291 ident == &SI_ident) {
292 /* Nothing? */
293 return NULL;
295 if (ident == &__DI___ident ||
296 ident == &DI_ident) {
297 ctype->modifiers |= MOD_LONGLONG;
298 ctype->base_type = ctype_integer(ctype->modifiers);
299 return NULL;
301 if (ident == &__word___ident ||
302 ident == &word_ident) {
303 ctype->modifiers |= MOD_LONG;
304 ctype->base_type = ctype_integer(ctype->modifiers);
305 return NULL;
307 return "unknown mode attribute";
309 return "expected attribute mode symbol";
312 /* Throw away for now.. */
313 if (attribute == &format_ident ||
314 attribute == &__format___ident)
315 return NULL;
316 if (attribute == &section_ident ||
317 attribute == &__section___ident)
318 return NULL;
319 if (attribute == &unused_ident ||
320 attribute == &__unused___ident)
321 return NULL;
322 if (attribute == &const_ident ||
323 attribute == &__const_ident ||
324 attribute == &__const___ident)
325 return NULL;
326 if (attribute == &noreturn_ident ||
327 attribute == &__noreturn___ident)
328 return NULL;
329 if (attribute == &regparm_ident)
330 return NULL;
331 if (attribute == &weak_ident)
332 return NULL;
333 if (attribute == &alias_ident)
334 return NULL;
335 if (attribute == &pure_ident)
336 return NULL;
337 if (attribute == &always_inline_ident)
338 return NULL;
339 if (attribute == &syscall_linkage_ident)
340 return NULL;
341 if (attribute == &visibility_ident)
342 return NULL;
344 return "unknown attribute";
347 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
349 ctype->modifiers = 0;
350 token = expect(token, '(', "after attribute");
351 token = expect(token, '(', "after attribute");
353 for (;;) {
354 const char *error;
355 struct ident *attribute_name;
356 struct expression *attribute_expr;
358 if (eof_token(token))
359 break;
360 if (match_op(token, ';'))
361 break;
362 if (token_type(token) != TOKEN_IDENT)
363 break;
364 attribute_name = token->ident;
365 token = token->next;
366 attribute_expr = NULL;
367 if (match_op(token, '('))
368 token = parens_expression(token, &attribute_expr, "in attribute");
369 error = handle_attribute(ctype, attribute_name, attribute_expr);
370 if (error)
371 warning(token->pos, "attribute '%s': %s", show_ident(attribute_name), error);
372 if (!match_op(token, ','))
373 break;
374 token = token->next;
377 token = expect(token, ')', "after attribute");
378 token = expect(token, ')', "after attribute");
379 return token;
382 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
383 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
385 struct symbol * ctype_integer(unsigned long spec)
387 static struct symbol *const integer_ctypes[][3] = {
388 { &llong_ctype, &sllong_ctype, &ullong_ctype },
389 { &long_ctype, &slong_ctype, &ulong_ctype },
390 { &short_ctype, &sshort_ctype, &ushort_ctype },
391 { &char_ctype, &schar_ctype, &uchar_ctype },
392 { &int_ctype, &sint_ctype, &uint_ctype },
394 struct symbol *const (*ctype)[3];
395 int sub;
397 ctype = integer_ctypes;
398 if (!(spec & MOD_LONGLONG)) {
399 ctype++;
400 if (!(spec & MOD_LONG)) {
401 ctype++;
402 if (!(spec & MOD_SHORT)) {
403 ctype++;
404 if (!(spec & MOD_CHAR))
405 ctype++;
410 sub = ((spec & MOD_UNSIGNED)
412 : ((spec & MOD_EXPLICITLY_SIGNED)
414 : 0));
416 return ctype[0][sub];
419 struct symbol * ctype_fp(unsigned long spec)
421 if (spec & MOD_LONGLONG)
422 return &ldouble_ctype;
423 if (spec & MOD_LONG)
424 return &double_ctype;
425 return &float_ctype;
428 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
430 unsigned long mod = thistype->modifiers;
432 if (mod) {
433 unsigned long old = ctype->modifiers;
434 unsigned long extra = 0, dup, conflict;
436 if (mod & old & MOD_LONG) {
437 extra = MOD_LONGLONG | MOD_LONG;
438 mod &= ~MOD_LONG;
439 old &= ~MOD_LONG;
441 dup = (mod & old) | (extra & old) | (extra & mod);
442 if (dup)
443 warning(pos, "Just how %sdo you want this type to be?",
444 modifier_string(dup));
446 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
447 if (conflict)
448 warning(pos, "You cannot have both long and short modifiers.");
450 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
451 if (conflict)
452 warning(pos, "You cannot have both signed and unsigned modifiers.");
454 // Only one storage modifier allowed, except that "inline" doesn't count.
455 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
456 conflict &= (conflict - 1);
457 if (conflict)
458 warning(pos, "multiple storage classes");
460 ctype->modifiers = old | mod | extra;
463 /* Context mask and value */
464 if ((ctype->context ^ thistype->context) & (ctype->contextmask & thistype->contextmask)) {
465 warning(pos, "inconsistent attribute types");
466 thistype->context = 0;
467 thistype->contextmask = 0;
469 ctype->context |= thistype->context;
470 ctype->contextmask |= thistype->contextmask;
472 /* Alignment */
473 if (thistype->alignment & (thistype->alignment-1)) {
474 warning(pos, "I don't like non-power-of-2 alignments");
475 thistype->alignment = 0;
477 if (thistype->alignment > ctype->alignment)
478 ctype->alignment = thistype->alignment;
480 /* Address space */
481 ctype->as = thistype->as;
484 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
486 unsigned long banned, wrong;
487 unsigned long this_mod = s->ctype.modifiers;
488 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
489 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
491 if (this_mod & (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF))
492 banned = BANNED_SIZE | BANNED_SIGN;
493 else if (this_mod & MOD_SPECIALBITS)
494 banned = 0;
495 else if (s->ctype.base_type == &fp_type)
496 banned = BANNED_SIGN;
497 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
498 banned = 0;
499 else {
500 // label_type
501 // void_type
502 // bad_type
503 // vector_type <-- whatever that is
504 banned = BANNED_SIZE | BANNED_SIGN;
507 wrong = mod & banned;
508 if (wrong)
509 warning(*pos, "modifier %sis invalid in this context",
510 modifier_string (wrong));
514 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
516 struct token *token;
518 while ( (token = next) != NULL ) {
519 struct ctype thistype;
520 struct ident *ident;
521 struct symbol *s, *type;
522 unsigned long mod;
524 next = token->next;
525 if (token_type(token) != TOKEN_IDENT)
526 break;
527 ident = token->ident;
529 s = lookup_symbol(ident, NS_TYPEDEF);
530 if (!s)
531 break;
532 thistype = s->ctype;
533 mod = thistype.modifiers;
534 if (qual && (mod & ~(MOD_ATTRIBUTE | MOD_CONST | MOD_VOLATILE)))
535 break;
536 if (mod & MOD_SPECIALBITS) {
537 if (mod & MOD_STRUCTOF)
538 next = struct_or_union_specifier(SYM_STRUCT, next, &thistype);
539 else if (mod & MOD_UNIONOF)
540 next = struct_or_union_specifier(SYM_UNION, next, &thistype);
541 else if (mod & MOD_ENUMOF)
542 next = enum_specifier(next, &thistype);
543 else if (mod & MOD_ATTRIBUTE)
544 next = attribute_specifier(next, &thistype);
545 else if (mod & MOD_TYPEOF)
546 next = typeof_specifier(next, &thistype);
547 mod = thistype.modifiers;
549 type = thistype.base_type;
550 if (type) {
551 if (qual)
552 break;
553 if (ctype->base_type)
554 break;
555 /* User types only mix with qualifiers */
556 if (mod & MOD_USERTYPE) {
557 if (ctype->modifiers & MOD_SPECIFIER)
558 break;
560 ctype->base_type = type;
563 check_modifiers(&token->pos, s, ctype->modifiers);
564 apply_ctype(token->pos, &thistype, ctype);
567 /* Turn the "virtual types" into real types with real sizes etc */
568 if (!ctype->base_type) {
569 struct symbol *base = &incomplete_ctype;
572 * If we have modifiers, we'll default to an integer
573 * type, and "ctype_integer()" will turn this into
574 * a specific one.
576 if (ctype->modifiers & MOD_SPECIFIER)
577 base = &int_type;
578 ctype->base_type = base;
580 if (ctype->base_type == &int_type) {
581 ctype->base_type = ctype_integer(ctype->modifiers);
582 ctype->modifiers &= ~MOD_SPECIFIER;
583 } else if (ctype->base_type == &fp_type) {
584 ctype->base_type = ctype_fp(ctype->modifiers);
585 ctype->modifiers &= ~MOD_SPECIFIER;
587 if (ctype->modifiers & MOD_BITWISE) {
588 struct symbol *type;
589 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
590 if (!is_int_type(ctype->base_type)) {
591 warning(token->pos, "invalid modifier");
592 return token;
594 type = alloc_symbol(token->pos, SYM_BASETYPE);
595 *type = *ctype->base_type;
596 type->ctype.base_type = ctype->base_type;
597 type->type = SYM_RESTRICT;
598 type->ctype.modifiers &= ~MOD_SPECIFIER;
599 ctype->base_type = type;
601 return token;
604 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
606 struct expression *expr = NULL;
608 token = parse_expression(token, &expr);
609 sym->array_size = expr;
610 return token;
613 static struct token *parameter_type_list(struct token *, struct symbol *);
614 static struct token *declarator(struct token *token, struct symbol **tree, struct ident **p);
616 static struct token *handle_attributes(struct token *token, struct ctype *ctype)
618 while (match_idents(token, &__attribute___ident, &__attribute_ident, NULL)) {
619 struct ctype thistype = { 0, };
620 token = attribute_specifier(token->next, &thistype);
621 apply_ctype(token->pos, &thistype, ctype);
623 return token;
626 static struct token *direct_declarator(struct token *token, struct symbol **tree, struct ident **p)
628 struct ctype *ctype = &(*tree)->ctype;
630 if (p && token_type(token) == TOKEN_IDENT) {
631 *p = token->ident;
632 token = token->next;
635 for (;;) {
636 token = handle_attributes(token, ctype);
638 if (token_type(token) != TOKEN_SPECIAL)
639 return token;
642 * This can be either a parameter list or a grouping.
643 * For the direct (non-abstract) case, we know if must be
644 * a parameter list if we already saw the identifier.
645 * For the abstract case, we know if must be a parameter
646 * list if it is empty or starts with a type.
648 if (token->special == '(') {
649 struct symbol *sym;
650 struct token *next = token->next;
651 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
653 if (!fn) {
654 struct symbol *base_type = ctype->base_type;
655 token = declarator(next, tree, p);
656 token = expect(token, ')', "in nested declarator");
657 while (ctype->base_type != base_type)
658 ctype = &ctype->base_type->ctype;
659 p = NULL;
660 continue;
663 sym = indirect(token->pos, ctype, SYM_FN);
664 token = parameter_type_list(next, sym);
665 token = expect(token, ')', "in function declarator");
666 continue;
668 if (token->special == '[') {
669 struct symbol *array = indirect(token->pos, ctype, SYM_ARRAY);
670 token = abstract_array_declarator(token->next, array);
671 token = expect(token, ']', "in abstract_array_declarator");
672 ctype = &array->ctype;
673 continue;
675 break;
677 if (p) {
678 (*tree)->ident = *p;
680 return token;
683 static struct token *pointer(struct token *token, struct ctype *ctype)
685 unsigned long modifiers;
686 struct symbol *base_type;
688 modifiers = ctype->modifiers & ~(MOD_TYPEDEF | MOD_ATTRIBUTE);
689 base_type = ctype->base_type;
690 ctype->modifiers = modifiers;
692 while (match_op(token,'*')) {
693 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
694 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
695 ptr->ctype.as = ctype->as;
696 ptr->ctype.context = ctype->context;
697 ptr->ctype.contextmask = ctype->contextmask;
698 ptr->ctype.base_type = base_type;
700 base_type = ptr;
701 ctype->modifiers = modifiers & MOD_STORAGE;
702 ctype->base_type = base_type;
703 ctype->as = 0;
704 ctype->context = 0;
705 ctype->contextmask = 0;
707 token = declaration_specifiers(token->next, ctype, 1);
708 modifiers = ctype->modifiers;
710 return token;
713 static struct token *declarator(struct token *token, struct symbol **tree, struct ident **p)
715 token = pointer(token, &(*tree)->ctype);
716 return direct_declarator(token, tree, p);
719 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
721 struct ctype *ctype = &decl->ctype;
722 struct expression *expr;
723 struct symbol *bitfield;
724 long long width;
726 if (!is_int_type(ctype->base_type)) {
727 warning(token->pos, "invalid bitfield specifier for type %s.",
728 show_typename(ctype->base_type));
729 // Parse this to recover gracefully.
730 return conditional_expression(token->next, &expr);
733 bitfield = indirect(token->pos, ctype, SYM_BITFIELD);
734 token = conditional_expression(token->next, &expr);
735 width = get_expression_value(expr);
736 bitfield->fieldwidth = width;
738 if (width < 0) {
739 warning(token->pos, "invalid negative bitfield width, %lld.", width);
740 bitfield->fieldwidth = 8;
741 } else if (decl->ident && width == 0) {
742 warning(token->pos, "invalid named zero-width bitfield `%s'",
743 show_ident(decl->ident));
744 bitfield->fieldwidth = 8;
745 } else if (width != bitfield->fieldwidth) {
746 // Overflow.
747 unsigned int stupid_gcc = -1;
748 bitfield->fieldwidth = stupid_gcc;
749 warning(token->pos, "truncating large bitfield from %lld to %d bits", width, bitfield->fieldwidth);
750 } else if (decl->ident) {
751 struct symbol *base_type = bitfield->ctype.base_type;
752 int is_signed = !(base_type->ctype.modifiers & MOD_UNSIGNED);
753 if (bitfield->fieldwidth == 1 && is_signed) {
754 // Valid values are either {-1;0} or {0}, depending on integer
755 // representation. The latter makes for very efficient code...
756 warning(token->pos, "dubious one-bit signed bitfield");
758 if (Wdefault_bitfield_sign &&
759 base_type->type != SYM_ENUM &&
760 !(base_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
761 is_signed) {
762 // The sign of bitfields is unspecified by default.
763 warning (token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
766 return token;
769 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
771 while (!match_op(token, '}')) {
772 struct ctype ctype = {0, };
774 token = declaration_specifiers(token, &ctype, 0);
775 for (;;) {
776 struct ident *ident = NULL;
777 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
778 decl->ctype = ctype;
779 token = declarator(token, &decl, &ident);
780 if (match_op(token, ':')) {
781 token = handle_bitfield(token, decl);
782 token = handle_attributes(token, &decl->ctype);
784 add_symbol(list, decl);
785 if (!match_op(token, ','))
786 break;
787 token = token->next;
789 if (!match_op(token, ';')) {
790 warning(token->pos, "expected ; at end of declaration");
791 break;
793 token = token->next;
795 return token;
798 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
800 struct ident *ident = NULL;
801 struct symbol *sym;
802 struct ctype ctype = { 0, };
804 token = declaration_specifiers(token, &ctype, 0);
805 sym = alloc_symbol(token->pos, SYM_NODE);
806 sym->ctype = ctype;
807 *tree = sym;
808 token = declarator(token, tree, &ident);
809 return token;
812 struct token *typename(struct token *token, struct symbol **p)
814 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
815 *p = sym;
816 token = declaration_specifiers(token, &sym->ctype, 0);
817 return declarator(token, &sym, NULL);
820 struct token *expression_statement(struct token *token, struct expression **tree)
822 token = parse_expression(token, tree);
823 return expect(token, ';', "at end of statement");
826 static struct token *parse_asm_operands(struct token *token, struct statement *stmt)
828 struct expression *expr;
830 /* Allow empty operands */
831 if (match_op(token->next, ':') || match_op(token->next, ')'))
832 return token->next;
833 do {
834 if (match_op(token->next, '[') &&
835 token_type(token->next->next) == TOKEN_IDENT &&
836 match_op(token->next->next->next, ']'))
837 token = token->next->next->next;
838 token = primary_expression(token->next, &expr);
839 token = parens_expression(token, &expr, "in asm parameter");
840 } while (match_op(token, ','));
841 return token;
844 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt)
846 struct expression *expr;
848 do {
849 token = primary_expression(token->next, &expr);
850 } while (match_op(token, ','));
851 return token;
854 static struct token *parse_asm(struct token *token, struct statement *stmt)
856 struct expression *expr;
858 stmt->type = STMT_ASM;
859 if (match_idents(token, &__volatile___ident, &volatile_ident, NULL)) {
860 token = token->next;
862 token = expect(token, '(', "after asm");
863 token = parse_expression(token->next, &expr);
864 if (match_op(token, ':'))
865 token = parse_asm_operands(token, stmt);
866 if (match_op(token, ':'))
867 token = parse_asm_operands(token, stmt);
868 if (match_op(token, ':'))
869 token = parse_asm_clobbers(token, stmt);
870 token = expect(token, ')', "after asm");
871 return expect(token, ';', "at end of asm-statement");
874 /* Make a statement out of an expression */
875 static struct statement *make_statement(struct expression *expr)
877 struct statement *stmt;
879 if (!expr)
880 return NULL;
881 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
882 stmt->expression = expr;
883 return stmt;
887 * All iterators have two symbols associated with them:
888 * the "continue" and "break" symbols, which are targets
889 * for continue and break statements respectively.
891 * They are in a special name-space, but they follow
892 * all the normal visibility rules, so nested iterators
893 * automatically work right.
895 static void start_iterator(struct statement *stmt)
897 struct symbol *cont, *brk;
899 start_symbol_scope();
900 cont = alloc_symbol(stmt->pos, SYM_NODE);
901 cont->ident = &continue_ident;
902 bind_symbol(cont, &continue_ident, NS_ITERATOR);
903 brk = alloc_symbol(stmt->pos, SYM_NODE);
904 brk->ident = &break_ident;
905 bind_symbol(brk, &break_ident, NS_ITERATOR);
907 stmt->type = STMT_ITERATOR;
908 stmt->iterator_break = brk;
909 stmt->iterator_continue = cont;
910 fn_local_symbol(brk);
911 fn_local_symbol(cont);
914 static void end_iterator(struct statement *stmt)
916 end_symbol_scope();
919 static struct statement *start_function(struct symbol *sym)
921 struct symbol *ret;
922 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
924 start_function_scope();
925 ret = alloc_symbol(sym->pos, SYM_NODE);
926 ret->ident = &return_ident;
927 ret->ctype = sym->ctype.base_type->ctype;
928 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
929 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
930 bind_symbol(ret, &return_ident, NS_ITERATOR);
931 stmt->ret = ret;
932 fn_local_symbol(ret);
934 // static const char __func__[] = "function-name";
935 if (sym->ident) {
936 struct symbol *funcname = alloc_symbol(sym->pos, SYM_NODE);
937 struct symbol *array = alloc_symbol(sym->pos, SYM_ARRAY);
938 struct expression *expr = alloc_expression(sym->pos, EXPR_STRING);
939 int len = sym->ident->len;
940 struct string *string = __alloc_string(len+1);
942 array->ctype.base_type = &char_ctype;
943 array->ctype.modifiers = MOD_CONST | MOD_STATIC;
945 memcpy(string->data, sym->ident->name, len);
946 string->data[len] = '\0';
947 string->length = len + 1;
949 expr->string = string;
951 funcname->initializer = expr;
952 funcname->ctype.modifiers = array->ctype.modifiers;
953 funcname->ctype.base_type = array;
954 funcname->ident = &__func___ident;
955 bind_symbol(funcname, &__func___ident, NS_SYMBOL);
957 add_symbol(&stmt->syms, funcname);
958 fn_local_symbol(funcname);
961 return stmt;
964 static void end_function(struct symbol *sym)
966 end_function_scope();
970 * A "switch()" statement, like an iterator, has a
971 * the "break" symbol associated with it. It works
972 * exactly like the iterator break - it's the target
973 * for any break-statements in scope, and means that
974 * "break" handling doesn't even need to know whether
975 * it's breaking out of an iterator or a switch.
977 * In addition, the "case" symbol is a marker for the
978 * case/default statements to find the switch statement
979 * that they are associated with.
981 static void start_switch(struct statement *stmt)
983 struct symbol *brk, *switch_case;
985 start_symbol_scope();
986 brk = alloc_symbol(stmt->pos, SYM_NODE);
987 brk->ident = &break_ident;
988 bind_symbol(brk, &break_ident, NS_ITERATOR);
990 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
991 switch_case->ident = &case_ident;
992 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
993 switch_case->stmt = stmt;
995 stmt->type = STMT_SWITCH;
996 stmt->switch_break = brk;
997 stmt->switch_case = switch_case;
999 fn_local_symbol(brk);
1000 fn_local_symbol(switch_case);
1003 static void end_switch(struct statement *stmt)
1005 if (!stmt->switch_case->symbol_list)
1006 warning(stmt->pos, "switch with no cases");
1007 end_symbol_scope();
1010 static void add_case_statement(struct statement *stmt)
1012 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1013 struct symbol *sym;
1015 if (!target) {
1016 warning(stmt->pos, "not in switch scope");
1017 return;
1019 sym = alloc_symbol(stmt->pos, SYM_NODE);
1020 add_symbol(&target->symbol_list, sym);
1021 sym->stmt = stmt;
1022 stmt->case_label = sym;
1023 fn_local_symbol(sym);
1026 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1028 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1030 if (!target)
1031 error_die(token->pos, "internal error: return without a function target");
1032 stmt->type = STMT_RETURN;
1033 stmt->ret_target = target;
1034 return expression_statement(token->next, &stmt->ret_value);
1037 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1039 struct symbol_list *syms;
1040 struct expression *e1, *e2, *e3;
1041 struct statement *iterator;
1043 start_iterator(stmt);
1044 token = expect(token->next, '(', "after 'for'");
1046 syms = NULL;
1047 e1 = NULL;
1048 /* C99 variable declaration? */
1049 if (lookup_type(token)) {
1050 token = external_declaration(token, &syms);
1051 } else {
1052 token = parse_expression(token, &e1);
1053 token = expect(token, ';', "in 'for'");
1055 token = parse_expression(token, &e2);
1056 token = expect(token, ';', "in 'for'");
1057 token = parse_expression(token, &e3);
1058 token = expect(token, ')', "in 'for'");
1059 token = statement(token, &iterator);
1061 stmt->iterator_syms = syms;
1062 stmt->iterator_pre_statement = make_statement(e1);
1063 stmt->iterator_pre_condition = e2;
1064 stmt->iterator_post_statement = make_statement(e3);
1065 stmt->iterator_post_condition = e2;
1066 stmt->iterator_statement = iterator;
1067 end_iterator(stmt);
1069 return token;
1072 struct token *parse_while_statement(struct token *token, struct statement *stmt)
1074 struct expression *expr;
1075 struct statement *iterator;
1077 start_iterator(stmt);
1078 token = parens_expression(token->next, &expr, "after 'while'");
1079 token = statement(token, &iterator);
1081 stmt->iterator_pre_condition = expr;
1082 stmt->iterator_post_condition = expr;
1083 stmt->iterator_statement = iterator;
1084 end_iterator(stmt);
1086 return token;
1089 struct token *parse_do_statement(struct token *token, struct statement *stmt)
1091 struct expression *expr;
1092 struct statement *iterator;
1094 start_iterator(stmt);
1095 token = statement(token->next, &iterator);
1096 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1097 token = token->next;
1098 else
1099 warning(token->pos, "expected 'while' after 'do'");
1100 token = parens_expression(token, &expr, "after 'do-while'");
1102 stmt->iterator_post_condition = expr;
1103 stmt->iterator_statement = iterator;
1104 end_iterator(stmt);
1106 return expect(token, ';', "after statement");
1109 static struct token *statement(struct token *token, struct statement **tree)
1111 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1113 *tree = stmt;
1114 if (token_type(token) == TOKEN_IDENT) {
1115 if (token->ident == &if_ident) {
1116 stmt->type = STMT_IF;
1117 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1118 token = statement(token, &stmt->if_true);
1119 if (token_type(token) != TOKEN_IDENT)
1120 return token;
1121 if (token->ident != &else_ident)
1122 return token;
1123 return statement(token->next, &stmt->if_false);
1126 if (token->ident == &return_ident)
1127 return parse_return_statement(token, stmt);
1129 if (token->ident == &break_ident || token->ident == &continue_ident) {
1130 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1131 stmt->type = STMT_GOTO;
1132 stmt->goto_label = target;
1133 if (!target)
1134 warning(stmt->pos, "break/continue not in iterator scope");
1135 return expect(token->next, ';', "at end of statement");
1137 if (token->ident == &default_ident) {
1138 token = token->next;
1139 goto default_statement;
1141 if (token->ident == &case_ident) {
1142 token = parse_expression(token->next, &stmt->case_expression);
1143 if (match_op(token, SPECIAL_ELLIPSIS))
1144 token = parse_expression(token->next, &stmt->case_to);
1145 default_statement:
1146 stmt->type = STMT_CASE;
1147 token = expect(token, ':', "after default/case");
1148 add_case_statement(stmt);
1149 return statement(token, &stmt->case_statement);
1151 if (token->ident == &switch_ident) {
1152 stmt->type = STMT_SWITCH;
1153 start_switch(stmt);
1154 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1155 token = statement(token, &stmt->switch_statement);
1156 end_switch(stmt);
1157 return token;
1159 if (token->ident == &for_ident)
1160 return parse_for_statement(token, stmt);
1162 if (token->ident == &while_ident)
1163 return parse_while_statement(token, stmt);
1165 if (token->ident == &do_ident)
1166 return parse_do_statement(token, stmt);
1168 if (token->ident == &goto_ident) {
1169 stmt->type = STMT_GOTO;
1170 token = token->next;
1171 if (match_op(token, '*')) {
1172 token = parse_expression(token->next, &stmt->goto_expression);
1173 add_statement(&function_computed_goto_list, stmt);
1174 } else if (token_type(token) == TOKEN_IDENT) {
1175 stmt->goto_label = label_symbol(token);
1176 token = token->next;
1177 } else {
1178 warning(token->pos, "Expected identifier or goto expression");
1180 return expect(token, ';', "at end of statement");
1182 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1183 return parse_asm(token->next, stmt);
1185 if (match_op(token->next, ':')) {
1186 stmt->type = STMT_LABEL;
1187 stmt->label_identifier = label_symbol(token);
1188 return statement(token->next->next, &stmt->label_statement);
1192 if (match_op(token, '{')) {
1193 stmt->type = STMT_COMPOUND;
1194 start_symbol_scope();
1195 token = compound_statement(token->next, stmt);
1196 end_symbol_scope();
1198 return expect(token, '}', "at end of compound statement");
1201 stmt->type = STMT_EXPRESSION;
1202 return expression_statement(token, &stmt->expression);
1205 struct token * statement_list(struct token *token, struct statement_list **list)
1207 for (;;) {
1208 struct statement * stmt;
1209 if (eof_token(token))
1210 break;
1211 if (match_op(token, '}'))
1212 break;
1213 token = statement(token, &stmt);
1214 add_statement(list, stmt);
1216 return token;
1219 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
1221 struct symbol_list **list = &fn->arguments;
1223 if (match_op(token, ')')) {
1224 // No warning for "void oink ();"
1225 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1226 if (!match_op(token->next, ';'))
1227 warning(token->pos, "non-ANSI function declaration");
1228 return token;
1231 for (;;) {
1232 struct symbol *sym;
1234 if (match_op(token, SPECIAL_ELLIPSIS)) {
1235 if (!*list)
1236 warning(token->pos, "variadic functions must have one named argument");
1237 fn->variadic = 1;
1238 token = token->next;
1239 break;
1242 sym = alloc_symbol(token->pos, SYM_NODE);
1243 token = parameter_declaration(token, &sym);
1244 if (sym->ctype.base_type == &void_ctype) {
1245 /* Special case: (void) */
1246 if (!*list && !sym->ident)
1247 break;
1248 warning(token->pos, "void parameter");
1250 add_symbol(list, sym);
1251 if (!match_op(token, ','))
1252 break;
1253 token = token->next;
1256 return token;
1259 struct token *compound_statement(struct token *token, struct statement *stmt)
1261 while (!eof_token(token)) {
1262 if (!lookup_type(token))
1263 break;
1264 token = external_declaration(token, &stmt->syms);
1266 token = statement_list(token, &stmt->stmts);
1267 return token;
1270 static struct expression *identifier_expression(struct token *token)
1272 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1273 expr->expr_ident = token->ident;
1274 return expr;
1277 static struct expression *index_expression(struct expression *from, struct expression *to)
1279 int idx_from, idx_to;
1280 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1282 idx_from = get_expression_value(from);
1283 idx_to = idx_from;
1284 if (to) {
1285 idx_to = get_expression_value(to);
1286 if (idx_to < idx_from || idx_from < 0)
1287 warning(from->pos, "nonsense array initializer index range");
1289 expr->idx_from = idx_from;
1290 expr->idx_to = idx_to;
1291 return expr;
1294 static struct token *initializer_list(struct expression_list **list, struct token *token)
1296 for (;;) {
1297 struct token *next = token->next;
1298 struct expression *expr;
1300 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT) && match_op(next->next, '=')) {
1301 add_expression(list, identifier_expression(next));
1302 token = next->next->next;
1303 } else if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1304 add_expression(list, identifier_expression(token));
1305 token = next->next;
1306 } else if (match_op(token, '[')) {
1307 struct expression *from = NULL, *to = NULL;
1308 token = constant_expression(token->next, &from);
1309 if (match_op(token, SPECIAL_ELLIPSIS))
1310 token = constant_expression(token->next, &to);
1311 add_expression(list, index_expression(from, to));
1312 token = expect(token, ']', "at end of initializer index");
1313 token = expect(token, '=', "at end of initializer index");
1316 expr = NULL;
1317 token = initializer(&expr, token);
1318 if (!expr)
1319 break;
1320 add_expression(list, expr);
1321 if (!match_op(token, ','))
1322 break;
1323 token = token->next;
1325 return token;
1328 struct token *initializer(struct expression **tree, struct token *token)
1330 if (match_op(token, '{')) {
1331 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1332 *tree = expr;
1333 token = initializer_list(&expr->expr_list, token->next);
1334 return expect(token, '}', "at end of initializer");
1336 return assignment_expression(token, tree);
1339 static void declare_argument(struct symbol *sym, struct symbol *fn)
1341 if (!sym->ident) {
1342 warning(sym->pos, "no identifier for function argument");
1343 return;
1345 bind_symbol(sym, sym->ident, NS_SYMBOL);
1348 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1349 struct symbol_list **list)
1351 struct symbol *base_type = decl->ctype.base_type;
1352 struct statement *stmt, **p;
1353 struct symbol *arg;
1355 if (decl->ctype.modifiers & MOD_INLINE) {
1356 function_symbol_list = &decl->inline_symbol_list;
1357 p = &base_type->inline_stmt;
1358 } else {
1359 function_symbol_list = &decl->symbol_list;
1360 p = &base_type->stmt;
1362 function_computed_target_list = NULL;
1363 function_computed_goto_list = NULL;
1365 if (decl->ctype.modifiers & MOD_EXTERN) {
1366 if (!(decl->ctype.modifiers & MOD_INLINE))
1367 warning(decl->pos, "function with external linkage has definition");
1369 if (!(decl->ctype.modifiers & MOD_STATIC))
1370 decl->ctype.modifiers |= MOD_EXTERN;
1372 stmt = start_function(decl);
1374 *p = stmt;
1375 FOR_EACH_PTR (base_type->arguments, arg) {
1376 declare_argument(arg, base_type);
1377 } END_FOR_EACH_PTR(arg);
1379 token = compound_statement(token->next, stmt);
1381 end_function(decl);
1382 if (!(decl->ctype.modifiers & MOD_INLINE))
1383 add_symbol(list, decl);
1384 check_declaration(decl);
1385 function_symbol_list = NULL;
1386 if (function_computed_goto_list) {
1387 if (!function_computed_target_list)
1388 warning(decl->pos, "function has computed goto but no targets?");
1389 else {
1390 struct statement *stmt;
1391 FOR_EACH_PTR(function_computed_goto_list, stmt) {
1392 stmt->target_list = function_computed_target_list;
1393 } END_FOR_EACH_PTR(stmt);
1396 return expect(token, '}', "at end of function");
1399 static void promote_k_r_types(struct symbol *arg)
1401 struct symbol *base = arg->ctype.base_type;
1402 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
1403 arg->ctype.base_type = &int_ctype;
1407 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
1409 struct symbol_list *real_args = fn->ctype.base_type->arguments;
1410 struct symbol *arg;
1412 FOR_EACH_PTR(real_args, arg) {
1413 struct symbol *type;
1415 /* This is quadratic in the number of arguments. We _really_ don't care */
1416 FOR_EACH_PTR(argtypes, type) {
1417 if (type->ident == arg->ident)
1418 goto match;
1419 } END_FOR_EACH_PTR(type);
1420 warning(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
1421 continue;
1422 match:
1423 type->used = 1;
1424 /* "char" and "short" promote to "int" */
1425 promote_k_r_types(type);
1427 arg->ctype = type->ctype;
1428 } END_FOR_EACH_PTR(arg);
1430 FOR_EACH_PTR(argtypes, arg) {
1431 if (!arg->used)
1432 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
1433 } END_FOR_EACH_PTR(arg);
1437 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
1438 struct symbol_list **list)
1440 struct symbol_list *args = NULL;
1442 warning(token->pos, "non-ANSI function declaration");
1443 do {
1444 token = external_declaration(token, &args);
1445 } while (lookup_type(token));
1447 apply_k_r_types(args, decl);
1449 if (!match_op(token, '{')) {
1450 warning(token->pos, "expected function body");
1451 return token;
1453 return parse_function_body(token, decl, list);
1457 static struct token *external_declaration(struct token *token, struct symbol_list **list)
1459 struct ident *ident = NULL;
1460 struct symbol *decl;
1461 struct ctype ctype = { 0, };
1462 struct symbol *base_type;
1463 int is_typedef;
1465 /* Top-level inline asm? */
1466 if (match_idents(token, &asm_ident, &__asm___ident, &__asm_ident, NULL)) {
1467 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
1468 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
1469 struct statement *stmt;
1471 anon->ctype.base_type = fn;
1472 function_symbol_list = &anon->symbol_list;
1473 stmt = start_function(anon);
1474 token = parse_asm(token->next, stmt);
1475 end_function(anon);
1476 function_symbol_list = NULL;
1477 add_symbol(list, anon);
1478 return token;
1481 /* Parse declaration-specifiers, if any */
1482 token = declaration_specifiers(token, &ctype, 0);
1483 decl = alloc_symbol(token->pos, SYM_NODE);
1484 decl->ctype = ctype;
1485 token = declarator(token, &decl, &ident);
1487 /* Just a type declaration? */
1488 if (!ident)
1489 return expect(token, ';', "end of type declaration");
1491 decl->ident = ident;
1493 /* type define declaration? */
1494 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
1496 /* Typedef's don't have meaningful storage */
1497 if (is_typedef) {
1498 ctype.modifiers &= ~MOD_STORAGE;
1499 decl->ctype.modifiers &= ~MOD_STORAGE;
1500 decl->ctype.modifiers |= MOD_USERTYPE;
1503 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1505 base_type = decl->ctype.base_type;
1506 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1507 /* K&R argument declaration? */
1508 if (lookup_type(token))
1509 return parse_k_r_arguments(token, decl, list);
1510 if (match_op(token, '{'))
1511 return parse_function_body(token, decl, list);
1513 if (!(decl->ctype.modifiers & MOD_STATIC))
1514 decl->ctype.modifiers |= MOD_EXTERN;
1515 } else if (!is_typedef && base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
1516 warning(token->pos, "void declaration");
1519 for (;;) {
1520 if (token_type(token) == TOKEN_IDENT) {
1521 if (token->ident == &asm_ident || token->ident == &__asm_ident || token->ident == &__asm___ident) {
1522 struct expression *expr;
1524 token = expect(token->next, '(', "after asm");
1525 token = parse_expression(token->next, &expr);
1526 token = expect(token, ')', "after asm");
1529 if (!is_typedef && match_op(token, '=')) {
1530 if (decl->ctype.modifiers & MOD_EXTERN) {
1531 warning(decl->pos, "symbol with external linkage has initializer");
1532 decl->ctype.modifiers &= ~MOD_EXTERN;
1534 token = initializer(&decl->initializer, token->next);
1536 if (!is_typedef) {
1537 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
1538 add_symbol(list, decl);
1539 if (function_symbol_list)
1540 fn_local_symbol(decl);
1543 check_declaration(decl);
1545 if (!match_op(token, ','))
1546 break;
1548 token = token->next;
1549 ident = NULL;
1550 decl = alloc_symbol(token->pos, SYM_NODE);
1551 decl->ctype = ctype;
1552 token = declaration_specifiers(token, &decl->ctype, 1);
1553 token = declarator(token, &decl, &ident);
1554 if (!ident) {
1555 warning(token->pos, "expected identifier name in type definition");
1556 return token;
1559 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
1561 /* Function declarations are automatically extern unless specifically static */
1562 base_type = decl->ctype.base_type;
1563 if (!is_typedef && base_type && base_type->type == SYM_FN) {
1564 if (!(decl->ctype.modifiers & MOD_STATIC))
1565 decl->ctype.modifiers |= MOD_EXTERN;
1568 return expect(token, ';', "at end of declaration");
1571 void translation_unit(struct token *token, struct symbol_list **list)
1573 while (!eof_token(token))
1574 token = external_declaration(token, list);
1575 // They aren't needed any more
1576 clear_token_alloc();