Fix -E handling
[smatch.git] / parse.c
blob843b2ad732b99822eca4e08d3a6aab07547b42fd
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
8 * Copyright (C) 2004 Christopher Li
10 * Licensed under the Open Software License version 1.1
13 #include <stdarg.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <limits.h>
22 #include "lib.h"
23 #include "allocate.h"
24 #include "token.h"
25 #include "parse.h"
26 #include "symbol.h"
27 #include "scope.h"
28 #include "expression.h"
29 #include "target.h"
31 #define warn_on_mixed (1)
33 static struct symbol_list **function_symbol_list;
34 struct symbol_list *function_computed_target_list;
35 struct statement_list *function_computed_goto_list;
37 static struct token *statement(struct token *token, struct statement **tree);
38 static struct token *handle_attributes(struct token *token, struct ctype *ctype, unsigned int keywords);
40 static struct token *struct_specifier(struct token *token, struct ctype *ctype);
41 static struct token *union_specifier(struct token *token, struct ctype *ctype);
42 static struct token *enum_specifier(struct token *token, struct ctype *ctype);
43 static struct token *attribute_specifier(struct token *token, struct ctype *ctype);
44 static struct token *typeof_specifier(struct token *token, struct ctype *ctype);
46 static struct token *parse_if_statement(struct token *token, struct statement *stmt);
47 static struct token *parse_return_statement(struct token *token, struct statement *stmt);
48 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt);
49 static struct token *parse_default_statement(struct token *token, struct statement *stmt);
50 static struct token *parse_case_statement(struct token *token, struct statement *stmt);
51 static struct token *parse_switch_statement(struct token *token, struct statement *stmt);
52 static struct token *parse_for_statement(struct token *token, struct statement *stmt);
53 static struct token *parse_while_statement(struct token *token, struct statement *stmt);
54 static struct token *parse_do_statement(struct token *token, struct statement *stmt);
55 static struct token *parse_goto_statement(struct token *token, struct statement *stmt);
56 static struct token *parse_context_statement(struct token *token, struct statement *stmt);
57 static struct token *parse_range_statement(struct token *token, struct statement *stmt);
58 static struct token *parse_asm_statement(struct token *token, struct statement *stmt);
59 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list);
60 static struct token *parse_asm_declarator(struct token *token, struct ctype *ctype);
63 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct ctype *ctype);
64 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct ctype *ctype);
65 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct ctype *ctype);
66 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct ctype *ctype);
67 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct ctype *ctype);
68 static struct token *attribute_context(struct token *token, struct symbol *attr, struct ctype *ctype);
69 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct ctype *ctype);
70 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct ctype *ctype);
73 static struct symbol_op modifier_op = {
74 .type = KW_MODIFIER,
77 static struct symbol_op qualifier_op = {
78 .type = KW_QUALIFIER,
81 static struct symbol_op typeof_op = {
82 .type = KW_TYPEOF,
83 .declarator = typeof_specifier,
86 static struct symbol_op attribute_op = {
87 .type = KW_ATTRIBUTE,
88 .declarator = attribute_specifier,
91 static struct symbol_op struct_op = {
92 .type = KW_SPECIFIER,
93 .declarator = struct_specifier,
96 static struct symbol_op union_op = {
97 .type = KW_SPECIFIER,
98 .declarator = union_specifier,
101 static struct symbol_op enum_op = {
102 .type = KW_SPECIFIER,
103 .declarator = enum_specifier,
108 static struct symbol_op if_op = {
109 .statement = parse_if_statement,
112 static struct symbol_op return_op = {
113 .statement = parse_return_statement,
116 static struct symbol_op loop_iter_op = {
117 .statement = parse_loop_iterator,
120 static struct symbol_op default_op = {
121 .statement = parse_default_statement,
124 static struct symbol_op case_op = {
125 .statement = parse_case_statement,
128 static struct symbol_op switch_op = {
129 .statement = parse_switch_statement,
132 static struct symbol_op for_op = {
133 .statement = parse_for_statement,
136 static struct symbol_op while_op = {
137 .statement = parse_while_statement,
140 static struct symbol_op do_op = {
141 .statement = parse_do_statement,
144 static struct symbol_op goto_op = {
145 .statement = parse_goto_statement,
148 static struct symbol_op __context___op = {
149 .statement = parse_context_statement,
152 static struct symbol_op range_op = {
153 .statement = parse_range_statement,
156 static struct symbol_op asm_op = {
157 .type = KW_ASM,
158 .declarator = parse_asm_declarator,
159 .statement = parse_asm_statement,
160 .toplevel = toplevel_asm_declaration,
163 static struct symbol_op packed_op = {
164 .attribute = attribute_packed,
167 static struct symbol_op aligned_op = {
168 .attribute = attribute_aligned,
171 static struct symbol_op attr_mod_op = {
172 .attribute = attribute_modifier,
175 static struct symbol_op address_space_op = {
176 .attribute = attribute_address_space,
179 static struct symbol_op mode_op = {
180 .attribute = attribute_mode,
183 static struct symbol_op context_op = {
184 .attribute = attribute_context,
187 static struct symbol_op transparent_union_op = {
188 .attribute = attribute_transparent_union,
191 static struct symbol_op ignore_attr_op = {
192 .attribute = ignore_attribute,
195 static struct symbol_op mode_spec_op = {
196 .type = KW_MODE,
199 static struct init_keyword {
200 const char *name;
201 enum namespace ns;
202 unsigned long modifiers;
203 struct symbol_op *op;
204 } keyword_table[] = {
205 /* Type qualifiers */
206 { "const", NS_TYPEDEF, MOD_CONST, .op = &qualifier_op },
207 { "__const", NS_TYPEDEF, MOD_CONST, .op = &qualifier_op },
208 { "__const__", NS_TYPEDEF, MOD_CONST, .op = &qualifier_op },
209 { "volatile", NS_TYPEDEF, MOD_VOLATILE, .op = &qualifier_op },
210 { "__volatile", NS_TYPEDEF, MOD_VOLATILE, .op = &qualifier_op },
211 { "__volatile__", NS_TYPEDEF, MOD_VOLATILE, .op = &qualifier_op },
213 /* Typedef.. */
214 { "typedef", NS_TYPEDEF, MOD_TYPEDEF, .op = &modifier_op },
216 /* Extended types */
217 { "typeof", NS_TYPEDEF, .op = &typeof_op },
218 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
219 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
221 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
222 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
224 { "struct", NS_TYPEDEF, .op = &struct_op },
225 { "union", NS_TYPEDEF, .op = &union_op },
226 { "enum", NS_TYPEDEF, .op = &enum_op },
228 { "inline", NS_TYPEDEF, MOD_INLINE, .op = &modifier_op },
229 { "__inline", NS_TYPEDEF, MOD_INLINE, .op = &modifier_op },
230 { "__inline__", NS_TYPEDEF, MOD_INLINE, .op = &modifier_op },
232 /* Ignored for now.. */
233 { "restrict", NS_TYPEDEF, .op = &qualifier_op},
234 { "__restrict", NS_TYPEDEF, .op = &qualifier_op},
236 /* Statement */
237 { "if", NS_KEYWORD, .op = &if_op },
238 { "return", NS_KEYWORD, .op = &return_op },
239 { "break", NS_KEYWORD, .op = &loop_iter_op },
240 { "continue", NS_KEYWORD, .op = &loop_iter_op },
241 { "default", NS_KEYWORD, .op = &default_op },
242 { "case", NS_KEYWORD, .op = &case_op },
243 { "switch", NS_KEYWORD, .op = &switch_op },
244 { "for", NS_KEYWORD, .op = &for_op },
245 { "while", NS_KEYWORD, .op = &while_op },
246 { "do", NS_KEYWORD, .op = &do_op },
247 { "goto", NS_KEYWORD, .op = &goto_op },
248 { "__context__",NS_KEYWORD, .op = &__context___op },
249 { "__range__", NS_KEYWORD, .op = &range_op },
250 { "asm", NS_KEYWORD, .op = &asm_op },
251 { "__asm", NS_KEYWORD, .op = &asm_op },
252 { "__asm__", NS_KEYWORD, .op = &asm_op },
254 /* Attribute */
255 { "packed", NS_KEYWORD, .op = &packed_op },
256 { "__packed__", NS_KEYWORD, .op = &packed_op },
257 { "aligned", NS_KEYWORD, .op = &aligned_op },
258 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
259 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
260 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
261 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
262 { "force", NS_KEYWORD, MOD_FORCE, .op = &attr_mod_op },
263 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
264 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
265 { "address_space",NS_KEYWORD, .op = &address_space_op },
266 { "mode", NS_KEYWORD, .op = &mode_op },
267 { "context", NS_KEYWORD, .op = &context_op },
268 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
270 { "__mode__", NS_KEYWORD, .op = &mode_op },
271 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_spec_op },
272 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_spec_op },
273 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_spec_op },
274 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_spec_op },
275 { "SI", NS_KEYWORD, .op = &mode_spec_op },
276 { "__SI__", NS_KEYWORD, .op = &mode_spec_op },
277 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_spec_op },
278 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_spec_op },
279 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_spec_op },
280 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_spec_op },
282 /* Ignored attributes */
283 { "nothrow", NS_KEYWORD, .op = &ignore_attr_op },
284 { "__nothrow", NS_KEYWORD, .op = &ignore_attr_op },
285 { "__nothrow__", NS_KEYWORD, .op = &ignore_attr_op },
286 { "__malloc__", NS_KEYWORD, .op = &ignore_attr_op },
287 { "nonnull", NS_KEYWORD, .op = &ignore_attr_op },
288 { "__nonnull", NS_KEYWORD, .op = &ignore_attr_op },
289 { "__nonnull__", NS_KEYWORD, .op = &ignore_attr_op },
290 { "format", NS_KEYWORD, .op = &ignore_attr_op },
291 { "__format__", NS_KEYWORD, .op = &ignore_attr_op },
292 { "format_arg", NS_KEYWORD, .op = &ignore_attr_op },
293 { "__format_arg__", NS_KEYWORD, .op = &ignore_attr_op },
294 { "section", NS_KEYWORD, .op = &ignore_attr_op },
295 { "__section__",NS_KEYWORD, .op = &ignore_attr_op },
296 { "unused", NS_KEYWORD, .op = &ignore_attr_op },
297 { "__unused__", NS_KEYWORD, .op = &ignore_attr_op },
298 { "const", NS_KEYWORD, .op = &ignore_attr_op },
299 { "__const", NS_KEYWORD, .op = &ignore_attr_op },
300 { "__const__", NS_KEYWORD, .op = &ignore_attr_op },
301 { "noreturn", NS_KEYWORD, .op = &ignore_attr_op },
302 { "__noreturn__", NS_KEYWORD, .op = &ignore_attr_op },
303 { "no_instrument_function", NS_KEYWORD, .op = &ignore_attr_op },
304 { "__no_instrument_function__", NS_KEYWORD, .op = &ignore_attr_op },
305 { "sentinel", NS_KEYWORD, .op = &ignore_attr_op },
306 { "__sentinel__", NS_KEYWORD, .op = &ignore_attr_op },
307 { "regparm", NS_KEYWORD, .op = &ignore_attr_op },
308 { "__regparm__", NS_KEYWORD, .op = &ignore_attr_op },
309 { "weak", NS_KEYWORD, .op = &ignore_attr_op },
310 { "__weak__", NS_KEYWORD, .op = &ignore_attr_op },
311 { "alias", NS_KEYWORD, .op = &ignore_attr_op },
312 { "__alias__", NS_KEYWORD, .op = &ignore_attr_op },
313 { "pure", NS_KEYWORD, .op = &ignore_attr_op },
314 { "__pure__", NS_KEYWORD, .op = &ignore_attr_op },
315 { "always_inline", NS_KEYWORD, .op = &ignore_attr_op },
316 { "__always_inline__", NS_KEYWORD, .op = &ignore_attr_op },
317 { "syscall_linkage", NS_KEYWORD, .op = &ignore_attr_op },
318 { "__syscall_linkage__", NS_KEYWORD, .op = &ignore_attr_op },
319 { "visibility", NS_KEYWORD, .op = &ignore_attr_op },
320 { "__visibility__", NS_KEYWORD, .op = &ignore_attr_op },
321 { "deprecated", NS_KEYWORD, .op = &ignore_attr_op },
322 { "__deprecated__", NS_KEYWORD, .op = &ignore_attr_op },
323 { "noinline", NS_KEYWORD, .op = &ignore_attr_op },
324 { "__noinline__", NS_KEYWORD, .op = &ignore_attr_op },
325 { "used", NS_KEYWORD, .op = &ignore_attr_op },
326 { "__used__", NS_KEYWORD, .op = &ignore_attr_op },
327 { "warn_unused_result", NS_KEYWORD, .op = &ignore_attr_op },
328 { "__warn_unused_result__", NS_KEYWORD, .op = &ignore_attr_op },
329 { "model", NS_KEYWORD, .op = &ignore_attr_op },
330 { "__model__", NS_KEYWORD, .op = &ignore_attr_op },
331 { "cdecl", NS_KEYWORD, .op = &ignore_attr_op },
332 { "__cdecl__", NS_KEYWORD, .op = &ignore_attr_op },
333 { "stdcall", NS_KEYWORD, .op = &ignore_attr_op },
334 { "__stdcall__", NS_KEYWORD, .op = &ignore_attr_op },
335 { "fastcall", NS_KEYWORD, .op = &ignore_attr_op },
336 { "__fastcall__", NS_KEYWORD, .op = &ignore_attr_op },
337 { "dllimport", NS_KEYWORD, .op = &ignore_attr_op },
338 { "__dllimport__", NS_KEYWORD, .op = &ignore_attr_op },
339 { "dllexport", NS_KEYWORD, .op = &ignore_attr_op },
340 { "__dllexport__", NS_KEYWORD, .op = &ignore_attr_op },
341 { "constructor", NS_KEYWORD, .op = &ignore_attr_op },
342 { "__constructor__", NS_KEYWORD, .op = &ignore_attr_op },
343 { "destructor", NS_KEYWORD, .op = &ignore_attr_op },
344 { "__destructor__", NS_KEYWORD, .op = &ignore_attr_op },
347 void init_parser(int stream)
349 int i;
350 for (i = 0; i < sizeof keyword_table/sizeof keyword_table[0]; i++) {
351 struct init_keyword *ptr = keyword_table + i;
352 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
353 sym->ident->keyword = 1;
354 sym->ctype.modifiers = ptr->modifiers;
355 sym->op = ptr->op;
359 // Add a symbol to the list of function-local symbols
360 static void fn_local_symbol(struct symbol *sym)
362 if (function_symbol_list)
363 add_symbol(function_symbol_list, sym);
366 static int SENTINEL_ATTR match_idents(struct token *token, ...)
368 va_list args;
370 if (token_type(token) != TOKEN_IDENT)
371 return 0;
373 va_start(args, token);
374 for (;;) {
375 struct ident * next = va_arg(args, struct ident *);
376 if (!next)
377 return 0;
378 if (token->ident == next)
379 return 1;
384 struct statement *alloc_statement(struct position pos, int type)
386 struct statement *stmt = __alloc_statement(0);
387 stmt->type = type;
388 stmt->pos = pos;
389 return stmt;
392 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
394 static int apply_modifiers(struct position pos, struct ctype *ctype)
396 struct symbol *base;
398 while ((base = ctype->base_type)) {
399 switch (base->type) {
400 case SYM_FN:
401 case SYM_ENUM:
402 case SYM_ARRAY:
403 case SYM_BITFIELD:
404 case SYM_PTR:
405 ctype = &base->ctype;
406 continue;
408 break;
411 /* Turn the "virtual types" into real types with real sizes etc */
412 if (ctype->base_type == &int_type) {
413 ctype->base_type = ctype_integer(ctype->modifiers);
414 ctype->modifiers &= ~MOD_SPECIFIER;
415 } else if (ctype->base_type == &fp_type) {
416 ctype->base_type = ctype_fp(ctype->modifiers);
417 ctype->modifiers &= ~MOD_SPECIFIER;
420 if (ctype->modifiers & MOD_BITWISE) {
421 struct symbol *type;
422 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
423 if (!is_int_type(ctype->base_type)) {
424 sparse_error(pos, "invalid modifier");
425 return 1;
427 type = alloc_symbol(pos, SYM_BASETYPE);
428 *type = *ctype->base_type;
429 type->ctype.base_type = ctype->base_type;
430 type->type = SYM_RESTRICT;
431 type->ctype.modifiers &= ~MOD_SPECIFIER;
432 ctype->base_type = type;
433 create_fouled(type);
435 return 0;
438 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
440 struct symbol *sym = alloc_symbol(pos, type);
442 sym->ctype.base_type = ctype->base_type;
443 sym->ctype.modifiers = ctype->modifiers & ~MOD_STORAGE;
445 ctype->base_type = sym;
446 ctype->modifiers &= MOD_STORAGE;
447 return sym;
450 static struct symbol *lookup_or_create_symbol(enum namespace ns, enum type type, struct token *token)
452 struct symbol *sym = lookup_symbol(token->ident, ns);
453 if (!sym) {
454 sym = alloc_symbol(token->pos, type);
455 bind_symbol(sym, token->ident, ns);
456 if (type == SYM_LABEL)
457 fn_local_symbol(sym);
459 return sym;
463 * NOTE! NS_LABEL is not just a different namespace,
464 * it also ends up using function scope instead of the
465 * regular symbol scope.
467 struct symbol *label_symbol(struct token *token)
469 return lookup_or_create_symbol(NS_LABEL, SYM_LABEL, token);
472 static struct token *struct_union_enum_specifier(enum type type,
473 struct token *token, struct ctype *ctype,
474 struct token *(*parse)(struct token *, struct symbol *))
476 struct symbol *sym;
477 struct position *repos;
479 ctype->modifiers = 0;
480 token = handle_attributes(token, ctype, KW_ATTRIBUTE | KW_ASM);
481 if (token_type(token) == TOKEN_IDENT) {
482 sym = lookup_symbol(token->ident, NS_STRUCT);
483 if (!sym ||
484 (sym->scope != block_scope &&
485 (match_op(token->next,';') || match_op(token->next,'{')))) {
486 // Either a new symbol, or else an out-of-scope
487 // symbol being redefined.
488 sym = alloc_symbol(token->pos, type);
489 bind_symbol(sym, token->ident, NS_STRUCT);
491 if (sym->type != type)
492 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
493 ctype->base_type = sym;
494 repos = &token->pos;
495 token = token->next;
496 if (match_op(token, '{')) {
497 // The following test is actually wrong for empty
498 // structs, but (1) they are not C99, (2) gcc does
499 // the same thing, and (3) it's easier.
500 if (sym->symbol_list)
501 error_die(token->pos, "redefinition of %s", show_typename (sym));
502 sym->pos = *repos;
503 token = parse(token->next, sym);
504 token = expect(token, '}', "at end of struct-union-enum-specifier");
506 // Mark the structure as needing re-examination
507 sym->examined = 0;
509 return token;
512 // private struct/union/enum type
513 if (!match_op(token, '{')) {
514 sparse_error(token->pos, "expected declaration");
515 ctype->base_type = &bad_ctype;
516 return token;
519 sym = alloc_symbol(token->pos, type);
520 token = parse(token->next, sym);
521 ctype->base_type = sym;
522 return expect(token, '}', "at end of specifier");
525 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
527 return struct_declaration_list(token, &sym->symbol_list);
530 static struct token *struct_specifier(struct token *token, struct ctype *ctype)
532 return struct_union_enum_specifier(SYM_STRUCT, token, ctype, parse_struct_declaration);
535 static struct token *union_specifier(struct token *token, struct ctype *ctype)
537 return struct_union_enum_specifier(SYM_UNION, token, ctype, parse_struct_declaration);
541 typedef struct {
542 int x;
543 unsigned long long y;
544 } Num;
546 static void upper_boundary(Num *n, Num *v)
548 if (n->x > v->x)
549 return;
550 if (n->x < v->x) {
551 *n = *v;
552 return;
554 if (n->y < v->y)
555 n->y = v->y;
558 static void lower_boundary(Num *n, Num *v)
560 if (n->x < v->x)
561 return;
562 if (n->x > v->x) {
563 *n = *v;
564 return;
566 if (n->y > v->y)
567 n->y = v->y;
570 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
572 int shift = type->bit_size;
573 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
575 if (!is_unsigned)
576 shift--;
577 if (upper->x == 0 && upper->y >> shift)
578 return 0;
579 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
580 return 1;
581 return 0;
584 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
586 if (s1->bit_size < s2->bit_size) {
587 s1 = s2;
588 } else if (s1->bit_size == s2->bit_size) {
589 if (s2->ctype.modifiers & MOD_UNSIGNED)
590 s1 = s2;
592 if (s1->bit_size < bits_in_int)
593 return &int_ctype;
594 return s1;
597 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
599 struct symbol *sym;
601 FOR_EACH_PTR(list, sym) {
602 struct expression *expr = sym->initializer;
603 struct symbol *ctype;
604 if (expr->type != EXPR_VALUE)
605 continue;
606 ctype = expr->ctype;
607 if (ctype->bit_size == base_type->bit_size)
608 continue;
609 cast_value(expr, base_type, expr, ctype);
610 } END_FOR_EACH_PTR(sym);
613 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
615 unsigned long long lastval = 0;
616 struct symbol *ctype = NULL, *base_type = NULL;
617 Num upper = {-1, 0}, lower = {1, 0};
618 struct symbol_list *entries = NULL;
620 parent->examined = 1;
621 parent->ctype.base_type = &int_ctype;
622 while (token_type(token) == TOKEN_IDENT) {
623 struct expression *expr = NULL;
624 struct token *next = token->next;
625 struct symbol *sym;
627 sym = alloc_symbol(token->pos, SYM_NODE);
628 bind_symbol(sym, token->ident, NS_SYMBOL);
629 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
631 if (match_op(next, '=')) {
632 next = constant_expression(next->next, &expr);
633 lastval = get_expression_value(expr);
634 ctype = &void_ctype;
635 if (expr && expr->ctype)
636 ctype = expr->ctype;
637 } else if (!ctype) {
638 ctype = &int_ctype;
639 } else if (is_int_type(ctype)) {
640 lastval++;
641 } else {
642 error_die(token->pos, "can't increment the last enum member");
645 if (!expr) {
646 expr = alloc_expression(token->pos, EXPR_VALUE);
647 expr->value = lastval;
648 expr->ctype = ctype;
651 sym->initializer = expr;
652 sym->ctype.base_type = parent;
653 add_ptr_list(&entries, sym);
655 if (base_type != &bad_ctype) {
656 if (ctype->type == SYM_NODE)
657 ctype = ctype->ctype.base_type;
658 if (ctype->type == SYM_ENUM) {
659 if (ctype == parent)
660 ctype = base_type;
661 else
662 ctype = ctype->ctype.base_type;
665 * base_type rules:
666 * - if all enums are of the same type, then
667 * the base_type is that type (two first
668 * cases)
669 * - if enums are of different types, they
670 * all have to be integer types, and the
671 * base type is at least "int_ctype".
672 * - otherwise the base_type is "bad_ctype".
674 if (!base_type) {
675 base_type = ctype;
676 } else if (ctype == base_type) {
677 /* nothing */
678 } else if (is_int_type(base_type) && is_int_type(ctype)) {
679 base_type = bigger_enum_type(base_type, ctype);
680 } else
681 base_type = &bad_ctype;
682 parent->ctype.base_type = base_type;
684 if (is_int_type(base_type)) {
685 Num v = {.y = lastval};
686 if (ctype->ctype.modifiers & MOD_UNSIGNED)
687 v.x = 0;
688 else if ((long long)lastval >= 0)
689 v.x = 0;
690 else
691 v.x = -1;
692 upper_boundary(&upper, &v);
693 lower_boundary(&lower, &v);
695 token = next;
696 if (!match_op(token, ','))
697 break;
698 token = token->next;
700 if (!base_type) {
701 sparse_error(token->pos, "bad enum definition");
702 base_type = &bad_ctype;
704 else if (!is_int_type(base_type))
705 base_type = base_type;
706 else if (type_is_ok(base_type, &upper, &lower))
707 base_type = base_type;
708 else if (type_is_ok(&int_ctype, &upper, &lower))
709 base_type = &int_ctype;
710 else if (type_is_ok(&uint_ctype, &upper, &lower))
711 base_type = &uint_ctype;
712 else if (type_is_ok(&long_ctype, &upper, &lower))
713 base_type = &long_ctype;
714 else if (type_is_ok(&ulong_ctype, &upper, &lower))
715 base_type = &ulong_ctype;
716 else if (type_is_ok(&llong_ctype, &upper, &lower))
717 base_type = &llong_ctype;
718 else if (type_is_ok(&ullong_ctype, &upper, &lower))
719 base_type = &ullong_ctype;
720 else
721 base_type = &bad_ctype;
722 parent->ctype.base_type = base_type;
723 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
724 parent->examined = 0;
726 cast_enum_list(entries, base_type);
727 free_ptr_list(&entries);
729 return token;
732 static struct token *enum_specifier(struct token *token, struct ctype *ctype)
734 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctype, parse_enum_declaration);
736 ctype = &ctype->base_type->ctype;
737 if (!ctype->base_type)
738 ctype->base_type = &incomplete_ctype;
740 return ret;
743 static struct token *typeof_specifier(struct token *token, struct ctype *ctype)
745 struct symbol *sym;
747 if (!match_op(token, '(')) {
748 sparse_error(token->pos, "expected '(' after typeof");
749 return token;
751 if (lookup_type(token->next)) {
752 token = typename(token->next, &sym);
753 *ctype = sym->ctype;
754 } else {
755 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
756 token = parse_expression(token->next, &typeof_sym->initializer);
758 ctype->modifiers = 0;
759 ctype->base_type = typeof_sym;
761 return expect(token, ')', "after typeof");
764 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct ctype *ctype)
766 struct expression *expr = NULL;
767 if (match_op(token, '('))
768 token = parens_expression(token, &expr, "in attribute");
769 return token;
772 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct ctype *ctype)
774 ctype->alignment = 1;
775 return token;
778 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct ctype *ctype)
780 int alignment = max_alignment;
781 struct expression *expr = NULL;
783 if (match_op(token, '(')) {
784 token = parens_expression(token, &expr, "in attribute");
785 if (expr)
786 alignment = get_expression_value(expr);
788 ctype->alignment = alignment;
789 return token;
792 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct ctype *ctype)
794 ctype->modifiers |= attr->ctype.modifiers;
795 return token;
798 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct ctype *ctype)
800 struct expression *expr = NULL;
801 token = expect(token, '(', "after address_space attribute");
802 token = conditional_expression(token, &expr);
803 if (expr)
804 ctype->as = get_expression_value(expr);
805 token = expect(token, ')', "after address_space attribute");
806 return token;
809 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct ctype *ctype)
811 token = expect(token, '(', "after mode attribute");
812 if (token_type(token) == TOKEN_IDENT) {
813 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
814 if (mode && mode->op->type == KW_MODE)
815 ctype->modifiers |= mode->ctype.modifiers;
816 else
817 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
818 token = token->next;
819 } else
820 sparse_error(token->pos, "expect attribute mode symbol\n");
821 token = expect(token, ')', "after mode attribute");
822 return token;
825 static struct token *attribute_context(struct token *token, struct symbol *attr, struct ctype *ctype)
827 struct context *context = alloc_context();
828 struct expression *args[3];
829 int argc = 0;
831 token = expect(token, '(', "after context attribute");
832 while (!match_op(token, ')')) {
833 struct expression *expr = NULL;
834 token = conditional_expression(token, &expr);
835 if (!expr)
836 break;
837 if (argc < 3)
838 args[argc++] = expr;
839 if (!match_op(token, ','))
840 break;
841 token = token->next;
844 switch(argc) {
845 case 0:
846 sparse_error(token->pos, "expected context input/output values");
847 break;
848 case 1:
849 context->in = get_expression_value(args[0]);
850 break;
851 case 2:
852 context->in = get_expression_value(args[0]);
853 context->out = get_expression_value(args[1]);
854 break;
855 case 3:
856 context->context = args[0];
857 context->in = get_expression_value(args[1]);
858 context->out = get_expression_value(args[2]);
859 break;
862 if (argc)
863 add_ptr_list(&ctype->contexts, context);
865 token = expect(token, ')', "after context attribute");
866 return token;
869 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct ctype *ctype)
871 if (Wtransparent_union)
872 sparse_error(token->pos, "ignoring attribute __transparent_union__");
873 return token;
876 static struct token *recover_unknown_attribute(struct token *token)
878 struct expression *expr = NULL;
880 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
881 token = token->next;
882 if (match_op(token, '('))
883 token = parens_expression(token, &expr, "in attribute");
884 return token;
887 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
889 ctype->modifiers = 0;
890 token = expect(token, '(', "after attribute");
891 token = expect(token, '(', "after attribute");
893 for (;;) {
894 struct ident *attribute_name;
895 struct symbol *attr;
897 if (eof_token(token))
898 break;
899 if (match_op(token, ';'))
900 break;
901 if (token_type(token) != TOKEN_IDENT)
902 break;
903 attribute_name = token->ident;
904 attr = lookup_keyword(attribute_name, NS_KEYWORD);
905 if (attr && attr->op->attribute)
906 token = attr->op->attribute(token->next, attr, ctype);
907 else
908 token = recover_unknown_attribute(token);
910 if (!match_op(token, ','))
911 break;
912 token = token->next;
915 token = expect(token, ')', "after attribute");
916 token = expect(token, ')', "after attribute");
917 return token;
920 struct symbol * ctype_integer(unsigned long spec)
922 static struct symbol *const integer_ctypes[][3] = {
923 { &llong_ctype, &sllong_ctype, &ullong_ctype },
924 { &long_ctype, &slong_ctype, &ulong_ctype },
925 { &short_ctype, &sshort_ctype, &ushort_ctype },
926 { &char_ctype, &schar_ctype, &uchar_ctype },
927 { &int_ctype, &sint_ctype, &uint_ctype },
929 struct symbol *const (*ctype)[3];
930 int sub;
932 ctype = integer_ctypes;
933 if (!(spec & MOD_LONGLONG)) {
934 ctype++;
935 if (!(spec & MOD_LONG)) {
936 ctype++;
937 if (!(spec & MOD_SHORT)) {
938 ctype++;
939 if (!(spec & MOD_CHAR))
940 ctype++;
945 sub = ((spec & MOD_UNSIGNED)
947 : ((spec & MOD_EXPLICITLY_SIGNED)
949 : 0));
951 return ctype[0][sub];
954 struct symbol * ctype_fp(unsigned long spec)
956 if (spec & MOD_LONGLONG)
957 return &ldouble_ctype;
958 if (spec & MOD_LONG)
959 return &double_ctype;
960 return &float_ctype;
963 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
965 unsigned long mod = thistype->modifiers;
967 if (mod) {
968 unsigned long old = ctype->modifiers;
969 unsigned long extra = 0, dup, conflict;
971 if (mod & old & MOD_LONG) {
972 extra = MOD_LONGLONG | MOD_LONG;
973 mod &= ~MOD_LONG;
974 old &= ~MOD_LONG;
976 dup = (mod & old) | (extra & old) | (extra & mod);
977 if (dup)
978 sparse_error(pos, "Just how %sdo you want this type to be?",
979 modifier_string(dup));
981 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
982 if (conflict)
983 sparse_error(pos, "You cannot have both long and short modifiers.");
985 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
986 if (conflict)
987 sparse_error(pos, "You cannot have both signed and unsigned modifiers.");
989 // Only one storage modifier allowed, except that "inline" doesn't count.
990 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
991 conflict &= (conflict - 1);
992 if (conflict)
993 sparse_error(pos, "multiple storage classes");
995 ctype->modifiers = old | mod | extra;
998 /* Context */
999 concat_ptr_list((struct ptr_list *)thistype->contexts,
1000 (struct ptr_list **)&ctype->contexts);
1002 /* Alignment */
1003 if (thistype->alignment & (thistype->alignment-1)) {
1004 warning(pos, "I don't like non-power-of-2 alignments");
1005 thistype->alignment = 0;
1007 if (thistype->alignment > ctype->alignment)
1008 ctype->alignment = thistype->alignment;
1010 /* Address space */
1011 if (thistype->as)
1012 ctype->as = thistype->as;
1015 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
1017 unsigned long banned, wrong;
1018 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
1019 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
1021 if (s->type == SYM_KEYWORD)
1022 banned = s->op->type == KW_SPECIFIER ? (BANNED_SIZE | BANNED_SIGN) : 0;
1023 else if (s->ctype.base_type == &fp_type)
1024 banned = BANNED_SIGN;
1025 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
1026 banned = 0;
1027 else {
1028 // label_type
1029 // void_type
1030 // bad_type
1031 // vector_type <-- whatever that is
1032 banned = BANNED_SIZE | BANNED_SIGN;
1035 wrong = mod & banned;
1036 if (wrong)
1037 sparse_error(*pos, "modifier %sis invalid in this context",
1038 modifier_string (wrong));
1041 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
1043 struct token *token;
1045 while ( (token = next) != NULL ) {
1046 struct ctype thistype;
1047 struct ident *ident;
1048 struct symbol *s, *type;
1049 unsigned long mod;
1051 next = token->next;
1052 if (token_type(token) != TOKEN_IDENT)
1053 break;
1054 ident = token->ident;
1056 s = lookup_symbol(ident, NS_TYPEDEF);
1057 if (!s)
1058 break;
1059 thistype = s->ctype;
1060 mod = thistype.modifiers;
1061 if (qual) {
1062 if (s->type != SYM_KEYWORD)
1063 break;
1064 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1065 break;
1067 if (s->type == SYM_KEYWORD && s->op->declarator) {
1068 next = s->op->declarator(next, &thistype);
1069 mod = thistype.modifiers;
1071 type = thistype.base_type;
1072 if (type) {
1073 if (qual)
1074 break;
1075 if (ctype->base_type)
1076 break;
1077 /* User types only mix with qualifiers */
1078 if (mod & MOD_USERTYPE) {
1079 if (ctype->modifiers & MOD_SPECIFIER)
1080 break;
1082 ctype->base_type = type;
1085 check_modifiers(&token->pos, s, ctype->modifiers);
1086 apply_ctype(token->pos, &thistype, ctype);
1089 if (!ctype->base_type) {
1090 struct symbol *base = &incomplete_ctype;
1093 * If we have modifiers, we'll default to an integer
1094 * type, and "ctype_integer()" will turn this into
1095 * a specific one.
1097 if (ctype->modifiers & MOD_SPECIFIER)
1098 base = &int_type;
1099 ctype->base_type = base;
1101 return token;
1104 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1106 struct expression *expr = NULL;
1108 token = parse_expression(token, &expr);
1109 sym->array_size = expr;
1110 return token;
1113 static struct token *parameter_type_list(struct token *, struct symbol *, struct ident **p);
1114 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
1116 static struct token *handle_attributes(struct token *token, struct ctype *ctype, unsigned int keywords)
1118 struct symbol *keyword;
1119 for (;;) {
1120 struct ctype thistype = { 0, };
1121 if (token_type(token) != TOKEN_IDENT)
1122 break;
1123 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1124 if (!keyword || keyword->type != SYM_KEYWORD)
1125 break;
1126 if (!(keyword->op->type & keywords))
1127 break;
1128 token = keyword->op->declarator(token->next, &thistype);
1129 apply_ctype(token->pos, &thistype, ctype);
1131 return token;
1134 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
1136 struct ctype *ctype = &decl->ctype;
1138 if (p && token_type(token) == TOKEN_IDENT) {
1139 *p = token->ident;
1140 token = token->next;
1143 for (;;) {
1144 token = handle_attributes(token, ctype, KW_ATTRIBUTE | KW_ASM);
1146 if (token_type(token) != TOKEN_SPECIAL)
1147 return token;
1150 * This can be either a parameter list or a grouping.
1151 * For the direct (non-abstract) case, we know if must be
1152 * a parameter list if we already saw the identifier.
1153 * For the abstract case, we know if must be a parameter
1154 * list if it is empty or starts with a type.
1156 if (token->special == '(') {
1157 struct symbol *sym;
1158 struct token *next = token->next;
1159 int fn;
1161 next = handle_attributes(next, ctype, KW_ATTRIBUTE);
1162 fn = (p && *p) || match_op(next, ')') || lookup_type(next);
1164 if (!fn) {
1165 struct symbol *base_type = ctype->base_type;
1166 token = declarator(next, decl, p);
1167 token = expect(token, ')', "in nested declarator");
1168 while (ctype->base_type != base_type)
1169 ctype = &ctype->base_type->ctype;
1170 p = NULL;
1171 continue;
1174 sym = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1175 token = parameter_type_list(next, sym, p);
1176 token = expect(token, ')', "in function declarator");
1177 continue;
1179 if (token->special == '[') {
1180 struct symbol *array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1181 token = abstract_array_declarator(token->next, array);
1182 token = expect(token, ']', "in abstract_array_declarator");
1183 ctype = &array->ctype;
1184 continue;
1186 break;
1188 return token;
1191 static struct token *pointer(struct token *token, struct ctype *ctype)
1193 unsigned long modifiers;
1194 struct symbol *base_type;
1196 modifiers = ctype->modifiers & ~MOD_TYPEDEF;
1197 base_type = ctype->base_type;
1198 ctype->modifiers = modifiers;
1200 while (match_op(token,'*')) {
1201 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1202 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
1203 ptr->ctype.as = ctype->as;
1204 concat_ptr_list((struct ptr_list *)ctype->contexts,
1205 (struct ptr_list **)&ptr->ctype.contexts);
1206 ptr->ctype.base_type = base_type;
1208 base_type = ptr;
1209 ctype->modifiers = modifiers & MOD_STORAGE;
1210 ctype->base_type = base_type;
1211 ctype->as = 0;
1212 free_ptr_list(&ctype->contexts);
1214 token = declaration_specifiers(token->next, ctype, 1);
1215 modifiers = ctype->modifiers;
1217 return token;
1220 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
1222 token = pointer(token, &sym->ctype);
1223 return direct_declarator(token, sym, p);
1226 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
1228 struct ctype *ctype = &decl->ctype;
1229 struct expression *expr;
1230 struct symbol *bitfield;
1231 long long width;
1233 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1234 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1235 show_typename(ctype->base_type));
1236 // Parse this to recover gracefully.
1237 return conditional_expression(token->next, &expr);
1240 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1241 token = conditional_expression(token->next, &expr);
1242 width = get_expression_value(expr);
1243 bitfield->bit_size = width;
1245 if (width < 0 || width > INT_MAX) {
1246 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1247 width = -1;
1248 } else if (decl->ident && width == 0) {
1249 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1250 show_ident(decl->ident));
1251 width = -1;
1252 } else if (decl->ident) {
1253 struct symbol *base_type = bitfield->ctype.base_type;
1254 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1255 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1256 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1257 // Valid values are either {-1;0} or {0}, depending on integer
1258 // representation. The latter makes for very efficient code...
1259 sparse_error(token->pos, "dubious one-bit signed bitfield");
1261 if (Wdefault_bitfield_sign &&
1262 bitfield_type->type != SYM_ENUM &&
1263 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1264 is_signed) {
1265 // The sign of bitfields is unspecified by default.
1266 sparse_error(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1269 bitfield->bit_size = width;
1270 return token;
1273 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1275 struct ctype ctype = {0, };
1277 token = declaration_specifiers(token, &ctype, 0);
1278 for (;;) {
1279 struct ident *ident = NULL;
1280 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1281 decl->ctype = ctype;
1282 token = declarator(token, decl, &ident);
1283 decl->ident = ident;
1284 if (match_op(token, ':')) {
1285 token = handle_bitfield(token, decl);
1286 token = handle_attributes(token, &decl->ctype, KW_ATTRIBUTE | KW_ASM);
1288 apply_modifiers(token->pos, &decl->ctype);
1289 add_symbol(list, decl);
1290 if (!match_op(token, ','))
1291 break;
1292 token = token->next;
1294 return token;
1297 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1299 while (!match_op(token, '}')) {
1300 if (!match_op(token, ';'))
1301 token = declaration_list(token, list);
1302 if (!match_op(token, ';')) {
1303 sparse_error(token->pos, "expected ; at end of declaration");
1304 break;
1306 token = token->next;
1308 return token;
1311 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
1313 struct ident *ident = NULL;
1314 struct symbol *sym;
1315 struct ctype ctype = { 0, };
1317 token = declaration_specifiers(token, &ctype, 0);
1318 sym = alloc_symbol(token->pos, SYM_NODE);
1319 sym->ctype = ctype;
1320 *tree = sym;
1321 token = declarator(token, sym, &ident);
1322 sym->ident = ident;
1323 apply_modifiers(token->pos, &sym->ctype);
1324 return token;
1327 struct token *typename(struct token *token, struct symbol **p)
1329 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1330 *p = sym;
1331 token = declaration_specifiers(token, &sym->ctype, 0);
1332 token = declarator(token, sym, NULL);
1333 apply_modifiers(token->pos, &sym->ctype);
1334 return token;
1337 static struct token *expression_statement(struct token *token, struct expression **tree)
1339 token = parse_expression(token, tree);
1340 return expect(token, ';', "at end of statement");
1343 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1344 struct expression_list **inout)
1346 struct expression *expr;
1348 /* Allow empty operands */
1349 if (match_op(token->next, ':') || match_op(token->next, ')'))
1350 return token->next;
1351 do {
1352 struct ident *ident = NULL;
1353 if (match_op(token->next, '[') &&
1354 token_type(token->next->next) == TOKEN_IDENT &&
1355 match_op(token->next->next->next, ']')) {
1356 ident = token->next->next->ident;
1357 token = token->next->next->next;
1359 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1360 token = primary_expression(token->next, &expr);
1361 add_expression(inout, expr);
1362 token = parens_expression(token, &expr, "in asm parameter");
1363 add_expression(inout, expr);
1364 } while (match_op(token, ','));
1365 return token;
1368 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1369 struct expression_list **clobbers)
1371 struct expression *expr;
1373 do {
1374 token = primary_expression(token->next, &expr);
1375 add_expression(clobbers, expr);
1376 } while (match_op(token, ','));
1377 return token;
1380 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1382 token = token->next;
1383 stmt->type = STMT_ASM;
1384 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1385 token = token->next;
1387 token = expect(token, '(', "after asm");
1388 token = parse_expression(token, &stmt->asm_string);
1389 if (match_op(token, ':'))
1390 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1391 if (match_op(token, ':'))
1392 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1393 if (match_op(token, ':'))
1394 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1395 token = expect(token, ')', "after asm");
1396 return expect(token, ';', "at end of asm-statement");
1399 static struct token *parse_asm_declarator(struct token *token, struct ctype *ctype)
1401 struct expression *expr;
1402 token = expect(token, '(', "after asm");
1403 token = parse_expression(token->next, &expr);
1404 token = expect(token, ')', "after asm");
1405 return token;
1408 /* Make a statement out of an expression */
1409 static struct statement *make_statement(struct expression *expr)
1411 struct statement *stmt;
1413 if (!expr)
1414 return NULL;
1415 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1416 stmt->expression = expr;
1417 return stmt;
1421 * All iterators have two symbols associated with them:
1422 * the "continue" and "break" symbols, which are targets
1423 * for continue and break statements respectively.
1425 * They are in a special name-space, but they follow
1426 * all the normal visibility rules, so nested iterators
1427 * automatically work right.
1429 static void start_iterator(struct statement *stmt)
1431 struct symbol *cont, *brk;
1433 start_symbol_scope();
1434 cont = alloc_symbol(stmt->pos, SYM_NODE);
1435 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1436 brk = alloc_symbol(stmt->pos, SYM_NODE);
1437 bind_symbol(brk, &break_ident, NS_ITERATOR);
1439 stmt->type = STMT_ITERATOR;
1440 stmt->iterator_break = brk;
1441 stmt->iterator_continue = cont;
1442 fn_local_symbol(brk);
1443 fn_local_symbol(cont);
1446 static void end_iterator(struct statement *stmt)
1448 end_symbol_scope();
1451 static struct statement *start_function(struct symbol *sym)
1453 struct symbol *ret;
1454 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1456 start_function_scope();
1457 ret = alloc_symbol(sym->pos, SYM_NODE);
1458 ret->ctype = sym->ctype.base_type->ctype;
1459 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1460 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1461 bind_symbol(ret, &return_ident, NS_ITERATOR);
1462 stmt->ret = ret;
1463 fn_local_symbol(ret);
1465 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1466 current_fn = sym;
1468 return stmt;
1471 static void end_function(struct symbol *sym)
1473 current_fn = NULL;
1474 end_function_scope();
1478 * A "switch()" statement, like an iterator, has a
1479 * the "break" symbol associated with it. It works
1480 * exactly like the iterator break - it's the target
1481 * for any break-statements in scope, and means that
1482 * "break" handling doesn't even need to know whether
1483 * it's breaking out of an iterator or a switch.
1485 * In addition, the "case" symbol is a marker for the
1486 * case/default statements to find the switch statement
1487 * that they are associated with.
1489 static void start_switch(struct statement *stmt)
1491 struct symbol *brk, *switch_case;
1493 start_symbol_scope();
1494 brk = alloc_symbol(stmt->pos, SYM_NODE);
1495 bind_symbol(brk, &break_ident, NS_ITERATOR);
1497 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1498 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1499 switch_case->stmt = stmt;
1501 stmt->type = STMT_SWITCH;
1502 stmt->switch_break = brk;
1503 stmt->switch_case = switch_case;
1505 fn_local_symbol(brk);
1506 fn_local_symbol(switch_case);
1509 static void end_switch(struct statement *stmt)
1511 if (!stmt->switch_case->symbol_list)
1512 warning(stmt->pos, "switch with no cases");
1513 end_symbol_scope();
1516 static void add_case_statement(struct statement *stmt)
1518 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1519 struct symbol *sym;
1521 if (!target) {
1522 sparse_error(stmt->pos, "not in switch scope");
1523 stmt->type = STMT_NONE;
1524 return;
1526 sym = alloc_symbol(stmt->pos, SYM_NODE);
1527 add_symbol(&target->symbol_list, sym);
1528 sym->stmt = stmt;
1529 stmt->case_label = sym;
1530 fn_local_symbol(sym);
1533 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1535 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1537 if (!target)
1538 error_die(token->pos, "internal error: return without a function target");
1539 stmt->type = STMT_RETURN;
1540 stmt->ret_target = target;
1541 return expression_statement(token->next, &stmt->ret_value);
1544 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1546 struct symbol_list *syms;
1547 struct expression *e1, *e2, *e3;
1548 struct statement *iterator;
1550 start_iterator(stmt);
1551 token = expect(token->next, '(', "after 'for'");
1553 syms = NULL;
1554 e1 = NULL;
1555 /* C99 variable declaration? */
1556 if (lookup_type(token)) {
1557 token = external_declaration(token, &syms);
1558 } else {
1559 token = parse_expression(token, &e1);
1560 token = expect(token, ';', "in 'for'");
1562 token = parse_expression(token, &e2);
1563 token = expect(token, ';', "in 'for'");
1564 token = parse_expression(token, &e3);
1565 token = expect(token, ')', "in 'for'");
1566 token = statement(token, &iterator);
1568 stmt->iterator_syms = syms;
1569 stmt->iterator_pre_statement = make_statement(e1);
1570 stmt->iterator_pre_condition = e2;
1571 stmt->iterator_post_statement = make_statement(e3);
1572 stmt->iterator_post_condition = NULL;
1573 stmt->iterator_statement = iterator;
1574 end_iterator(stmt);
1576 return token;
1579 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
1581 struct expression *expr;
1582 struct statement *iterator;
1584 start_iterator(stmt);
1585 token = parens_expression(token->next, &expr, "after 'while'");
1586 token = statement(token, &iterator);
1588 stmt->iterator_pre_condition = expr;
1589 stmt->iterator_post_condition = NULL;
1590 stmt->iterator_statement = iterator;
1591 end_iterator(stmt);
1593 return token;
1596 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
1598 struct expression *expr;
1599 struct statement *iterator;
1601 start_iterator(stmt);
1602 token = statement(token->next, &iterator);
1603 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1604 token = token->next;
1605 else
1606 sparse_error(token->pos, "expected 'while' after 'do'");
1607 token = parens_expression(token, &expr, "after 'do-while'");
1609 stmt->iterator_post_condition = expr;
1610 stmt->iterator_statement = iterator;
1611 end_iterator(stmt);
1613 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
1614 warning(iterator->pos, "do-while statement is not a compound statement");
1616 return expect(token, ';', "after statement");
1619 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
1621 stmt->type = STMT_IF;
1622 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1623 token = statement(token, &stmt->if_true);
1624 if (token_type(token) != TOKEN_IDENT)
1625 return token;
1626 if (token->ident != &else_ident)
1627 return token;
1628 return statement(token->next, &stmt->if_false);
1631 static inline struct token *case_statement(struct token *token, struct statement *stmt)
1633 stmt->type = STMT_CASE;
1634 token = expect(token, ':', "after default/case");
1635 add_case_statement(stmt);
1636 return statement(token, &stmt->case_statement);
1639 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
1641 token = parse_expression(token->next, &stmt->case_expression);
1642 if (match_op(token, SPECIAL_ELLIPSIS))
1643 token = parse_expression(token->next, &stmt->case_to);
1644 return case_statement(token, stmt);
1647 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
1649 return case_statement(token->next, stmt);
1652 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
1654 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1655 stmt->type = STMT_GOTO;
1656 stmt->goto_label = target;
1657 if (!target)
1658 sparse_error(stmt->pos, "break/continue not in iterator scope");
1659 return expect(token->next, ';', "at end of statement");
1662 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
1664 stmt->type = STMT_SWITCH;
1665 start_switch(stmt);
1666 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1667 token = statement(token, &stmt->switch_statement);
1668 end_switch(stmt);
1669 return token;
1672 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
1674 stmt->type = STMT_GOTO;
1675 token = token->next;
1676 if (match_op(token, '*')) {
1677 token = parse_expression(token->next, &stmt->goto_expression);
1678 add_statement(&function_computed_goto_list, stmt);
1679 } else if (token_type(token) == TOKEN_IDENT) {
1680 stmt->goto_label = label_symbol(token);
1681 token = token->next;
1682 } else {
1683 sparse_error(token->pos, "Expected identifier or goto expression");
1685 return expect(token, ';', "at end of statement");
1688 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
1690 stmt->type = STMT_CONTEXT;
1691 token = parse_expression(token->next, &stmt->expression);
1692 if(stmt->expression->type == EXPR_PREOP
1693 && stmt->expression->op == '('
1694 && stmt->expression->unop->type == EXPR_COMMA) {
1695 struct expression *expr;
1696 expr = stmt->expression->unop;
1697 stmt->context = expr->left;
1698 stmt->expression = expr->right;
1700 return expect(token, ';', "at end of statement");
1703 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
1705 stmt->type = STMT_RANGE;
1706 token = assignment_expression(token->next, &stmt->range_expression);
1707 token = expect(token, ',', "after range expression");
1708 token = assignment_expression(token, &stmt->range_low);
1709 token = expect(token, ',', "after low range");
1710 token = assignment_expression(token, &stmt->range_high);
1711 return expect(token, ';', "after range statement");
1714 static struct token *statement(struct token *token, struct statement **tree)
1716 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1718 *tree = stmt;
1719 if (token_type(token) == TOKEN_IDENT) {
1720 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
1721 if (s && s->op->statement)
1722 return s->op->statement(token, stmt);
1724 if (match_op(token->next, ':')) {
1725 stmt->type = STMT_LABEL;
1726 stmt->label_identifier = label_symbol(token);
1727 token = handle_attributes(token->next->next, &stmt->label_identifier->ctype, KW_ATTRIBUTE);
1728 return statement(token, &stmt->label_statement);
1732 if (match_op(token, '{')) {
1733 stmt->type = STMT_COMPOUND;
1734 start_symbol_scope();
1735 token = compound_statement(token->next, stmt);
1736 end_symbol_scope();
1738 return expect(token, '}', "at end of compound statement");
1741 stmt->type = STMT_EXPRESSION;
1742 return expression_statement(token, &stmt->expression);
1745 static struct token * statement_list(struct token *token, struct statement_list **list)
1747 int seen_statement = 0;
1748 for (;;) {
1749 struct statement * stmt;
1750 if (eof_token(token))
1751 break;
1752 if (match_op(token, '}'))
1753 break;
1754 if (lookup_type(token)) {
1755 if (seen_statement) {
1756 warning(token->pos, "mixing declarations and code");
1757 seen_statement = 0;
1759 stmt = alloc_statement(token->pos, STMT_DECLARATION);
1760 token = external_declaration(token, &stmt->declaration);
1761 } else {
1762 seen_statement = warn_on_mixed;
1763 token = statement(token, &stmt);
1765 add_statement(list, stmt);
1767 return token;
1770 static struct token *parameter_type_list(struct token *token, struct symbol *fn, struct ident **p)
1772 struct symbol_list **list = &fn->arguments;
1774 if (match_op(token, ')')) {
1775 // No warning for "void oink ();"
1776 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1777 if (p && !match_op(token->next, ';'))
1778 warning(token->pos, "non-ANSI function declaration of function '%s'", show_ident(*p));
1779 return token;
1782 for (;;) {
1783 struct symbol *sym;
1785 if (match_op(token, SPECIAL_ELLIPSIS)) {
1786 if (!*list)
1787 warning(token->pos, "variadic functions must have one named argument");
1788 fn->variadic = 1;
1789 token = token->next;
1790 break;
1793 sym = alloc_symbol(token->pos, SYM_NODE);
1794 token = parameter_declaration(token, &sym);
1795 if (sym->ctype.base_type == &void_ctype) {
1796 /* Special case: (void) */
1797 if (!*list && !sym->ident)
1798 break;
1799 warning(token->pos, "void parameter");
1801 add_symbol(list, sym);
1802 if (!match_op(token, ','))
1803 break;
1804 token = token->next;
1807 return token;
1810 struct token *compound_statement(struct token *token, struct statement *stmt)
1812 token = statement_list(token, &stmt->stmts);
1813 return token;
1816 static struct expression *identifier_expression(struct token *token)
1818 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1819 expr->expr_ident = token->ident;
1820 return expr;
1823 static struct expression *index_expression(struct expression *from, struct expression *to)
1825 int idx_from, idx_to;
1826 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1828 idx_from = get_expression_value(from);
1829 idx_to = idx_from;
1830 if (to) {
1831 idx_to = get_expression_value(to);
1832 if (idx_to < idx_from || idx_from < 0)
1833 warning(from->pos, "nonsense array initializer index range");
1835 expr->idx_from = idx_from;
1836 expr->idx_to = idx_to;
1837 return expr;
1840 static struct token *single_initializer(struct expression **ep, struct token *token)
1842 int expect_equal = 0;
1843 struct token *next = token->next;
1844 struct expression **tail = ep;
1845 int nested;
1847 *ep = NULL;
1849 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1850 struct expression *expr = identifier_expression(token);
1851 if (Wold_initializer)
1852 warning(token->pos, "obsolete struct initializer, use C99 syntax");
1853 token = initializer(&expr->ident_expression, next->next);
1854 if (expr->ident_expression)
1855 *ep = expr;
1856 return token;
1859 for (tail = ep, nested = 0; ; nested++, next = token->next) {
1860 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
1861 struct expression *expr = identifier_expression(next);
1862 *tail = expr;
1863 tail = &expr->ident_expression;
1864 expect_equal = 1;
1865 token = next->next;
1866 } else if (match_op(token, '[')) {
1867 struct expression *from = NULL, *to = NULL, *expr;
1868 token = constant_expression(token->next, &from);
1869 if (!from) {
1870 sparse_error(token->pos, "Expected constant expression");
1871 break;
1873 if (match_op(token, SPECIAL_ELLIPSIS))
1874 token = constant_expression(token->next, &to);
1875 expr = index_expression(from, to);
1876 *tail = expr;
1877 tail = &expr->idx_expression;
1878 token = expect(token, ']', "at end of initializer index");
1879 if (nested)
1880 expect_equal = 1;
1881 } else {
1882 break;
1885 if (nested && !expect_equal) {
1886 if (!match_op(token, '='))
1887 warning(token->pos, "obsolete array initializer, use C99 syntax");
1888 else
1889 expect_equal = 1;
1891 if (expect_equal)
1892 token = expect(token, '=', "at end of initializer index");
1894 token = initializer(tail, token);
1895 if (!*tail)
1896 *ep = NULL;
1897 return token;
1900 static struct token *initializer_list(struct expression_list **list, struct token *token)
1902 struct expression *expr;
1904 for (;;) {
1905 token = single_initializer(&expr, token);
1906 if (!expr)
1907 break;
1908 add_expression(list, expr);
1909 if (!match_op(token, ','))
1910 break;
1911 token = token->next;
1913 return token;
1916 struct token *initializer(struct expression **tree, struct token *token)
1918 if (match_op(token, '{')) {
1919 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1920 *tree = expr;
1921 token = initializer_list(&expr->expr_list, token->next);
1922 return expect(token, '}', "at end of initializer");
1924 return assignment_expression(token, tree);
1927 static void declare_argument(struct symbol *sym, struct symbol *fn)
1929 if (!sym->ident) {
1930 sparse_error(sym->pos, "no identifier for function argument");
1931 return;
1933 bind_symbol(sym, sym->ident, NS_SYMBOL);
1936 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1937 struct symbol_list **list)
1939 struct symbol_list **old_symbol_list;
1940 struct symbol *base_type = decl->ctype.base_type;
1941 struct statement *stmt, **p;
1942 struct symbol *arg;
1944 old_symbol_list = function_symbol_list;
1945 if (decl->ctype.modifiers & MOD_INLINE) {
1946 function_symbol_list = &decl->inline_symbol_list;
1947 p = &base_type->inline_stmt;
1948 } else {
1949 function_symbol_list = &decl->symbol_list;
1950 p = &base_type->stmt;
1952 function_computed_target_list = NULL;
1953 function_computed_goto_list = NULL;
1955 if (decl->ctype.modifiers & MOD_EXTERN) {
1956 if (!(decl->ctype.modifiers & MOD_INLINE))
1957 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
1959 if (!(decl->ctype.modifiers & MOD_STATIC))
1960 decl->ctype.modifiers |= MOD_EXTERN;
1962 stmt = start_function(decl);
1964 *p = stmt;
1965 FOR_EACH_PTR (base_type->arguments, arg) {
1966 declare_argument(arg, base_type);
1967 } END_FOR_EACH_PTR(arg);
1969 token = compound_statement(token->next, stmt);
1971 end_function(decl);
1972 if (!(decl->ctype.modifiers & MOD_INLINE))
1973 add_symbol(list, decl);
1974 check_declaration(decl);
1975 function_symbol_list = old_symbol_list;
1976 if (function_computed_goto_list) {
1977 if (!function_computed_target_list)
1978 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
1979 else {
1980 FOR_EACH_PTR(function_computed_goto_list, stmt) {
1981 stmt->target_list = function_computed_target_list;
1982 } END_FOR_EACH_PTR(stmt);
1985 return expect(token, '}', "at end of function");
1988 static void promote_k_r_types(struct symbol *arg)
1990 struct symbol *base = arg->ctype.base_type;
1991 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
1992 arg->ctype.base_type = &int_ctype;
1996 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
1998 struct symbol_list *real_args = fn->ctype.base_type->arguments;
1999 struct symbol *arg;
2001 FOR_EACH_PTR(real_args, arg) {
2002 struct symbol *type;
2004 /* This is quadratic in the number of arguments. We _really_ don't care */
2005 FOR_EACH_PTR(argtypes, type) {
2006 if (type->ident == arg->ident)
2007 goto match;
2008 } END_FOR_EACH_PTR(type);
2009 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2010 continue;
2011 match:
2012 type->used = 1;
2013 /* "char" and "short" promote to "int" */
2014 promote_k_r_types(type);
2016 arg->ctype = type->ctype;
2017 } END_FOR_EACH_PTR(arg);
2019 FOR_EACH_PTR(argtypes, arg) {
2020 if (!arg->used)
2021 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2022 } END_FOR_EACH_PTR(arg);
2026 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2027 struct symbol_list **list)
2029 struct symbol_list *args = NULL;
2031 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2032 do {
2033 token = declaration_list(token, &args);
2034 if (!match_op(token, ';')) {
2035 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2036 break;
2038 token = token->next;
2039 } while (lookup_type(token));
2041 apply_k_r_types(args, decl);
2043 if (!match_op(token, '{')) {
2044 sparse_error(token->pos, "expected function body");
2045 return token;
2047 return parse_function_body(token, decl, list);
2050 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2052 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2053 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2054 struct statement *stmt;
2056 anon->ctype.base_type = fn;
2057 stmt = alloc_statement(token->pos, STMT_NONE);
2058 fn->stmt = stmt;
2060 token = parse_asm_statement(token, stmt);
2062 add_symbol(list, anon);
2063 return token;
2066 struct token *external_declaration(struct token *token, struct symbol_list **list)
2068 struct ident *ident = NULL;
2069 struct symbol *decl;
2070 struct ctype ctype = { 0, };
2071 struct symbol *base_type;
2072 int is_typedef;
2074 /* Top-level inline asm? */
2075 if (token_type(token) == TOKEN_IDENT) {
2076 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2077 if (s && s->op->toplevel)
2078 return s->op->toplevel(token, list);
2081 /* Parse declaration-specifiers, if any */
2082 token = declaration_specifiers(token, &ctype, 0);
2083 decl = alloc_symbol(token->pos, SYM_NODE);
2084 decl->ctype = ctype;
2085 token = declarator(token, decl, &ident);
2086 apply_modifiers(token->pos, &decl->ctype);
2088 /* Just a type declaration? */
2089 if (!ident)
2090 return expect(token, ';', "end of type declaration");
2092 /* type define declaration? */
2093 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
2095 /* Typedefs don't have meaningful storage */
2096 if (is_typedef) {
2097 ctype.modifiers &= ~MOD_STORAGE;
2098 decl->ctype.modifiers &= ~MOD_STORAGE;
2099 decl->ctype.modifiers |= MOD_USERTYPE;
2102 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2104 base_type = decl->ctype.base_type;
2106 if (is_typedef) {
2107 if (base_type && !base_type->ident)
2108 base_type->ident = ident;
2109 } else if (base_type && base_type->type == SYM_FN) {
2110 /* K&R argument declaration? */
2111 if (lookup_type(token))
2112 return parse_k_r_arguments(token, decl, list);
2113 if (match_op(token, '{'))
2114 return parse_function_body(token, decl, list);
2116 if (!(decl->ctype.modifiers & MOD_STATIC))
2117 decl->ctype.modifiers |= MOD_EXTERN;
2118 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2119 sparse_error(token->pos, "void declaration");
2122 for (;;) {
2123 if (!is_typedef && match_op(token, '=')) {
2124 if (decl->ctype.modifiers & MOD_EXTERN) {
2125 warning(decl->pos, "symbol with external linkage has initializer");
2126 decl->ctype.modifiers &= ~MOD_EXTERN;
2128 token = initializer(&decl->initializer, token->next);
2130 if (!is_typedef) {
2131 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2132 add_symbol(list, decl);
2133 fn_local_symbol(decl);
2136 check_declaration(decl);
2138 if (!match_op(token, ','))
2139 break;
2141 token = token->next;
2142 ident = NULL;
2143 decl = alloc_symbol(token->pos, SYM_NODE);
2144 decl->ctype = ctype;
2145 token = declaration_specifiers(token, &decl->ctype, 1);
2146 token = declarator(token, decl, &ident);
2147 apply_modifiers(token->pos, &decl->ctype);
2148 if (!ident) {
2149 sparse_error(token->pos, "expected identifier name in type definition");
2150 return token;
2153 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2155 /* Function declarations are automatically extern unless specifically static */
2156 base_type = decl->ctype.base_type;
2157 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2158 if (!(decl->ctype.modifiers & MOD_STATIC))
2159 decl->ctype.modifiers |= MOD_EXTERN;
2162 return expect(token, ';', "at end of declaration");