Use %td when printing a ptrdiff_t to avoid problems on 64-bit platforms
[smatch.git] / parse.c
blobdc4e2d7570de73ce18d80e824f280f87126a86a3
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 { "section", NS_KEYWORD, .op = &ignore_attr_op },
294 { "__section__",NS_KEYWORD, .op = &ignore_attr_op },
295 { "unused", NS_KEYWORD, .op = &ignore_attr_op },
296 { "__unused__", NS_KEYWORD, .op = &ignore_attr_op },
297 { "const", NS_KEYWORD, .op = &ignore_attr_op },
298 { "__const", NS_KEYWORD, .op = &ignore_attr_op },
299 { "__const__", NS_KEYWORD, .op = &ignore_attr_op },
300 { "noreturn", NS_KEYWORD, .op = &ignore_attr_op },
301 { "__noreturn__", NS_KEYWORD, .op = &ignore_attr_op },
302 { "no_instrument_function", NS_KEYWORD, .op = &ignore_attr_op },
303 { "__no_instrument_function__", NS_KEYWORD, .op = &ignore_attr_op },
304 { "sentinel", NS_KEYWORD, .op = &ignore_attr_op },
305 { "__sentinel__", NS_KEYWORD, .op = &ignore_attr_op },
306 { "regparm", NS_KEYWORD, .op = &ignore_attr_op },
307 { "__regparm__", NS_KEYWORD, .op = &ignore_attr_op },
308 { "weak", NS_KEYWORD, .op = &ignore_attr_op },
309 { "__weak__", NS_KEYWORD, .op = &ignore_attr_op },
310 { "alias", NS_KEYWORD, .op = &ignore_attr_op },
311 { "__alias__", NS_KEYWORD, .op = &ignore_attr_op },
312 { "pure", NS_KEYWORD, .op = &ignore_attr_op },
313 { "__pure__", NS_KEYWORD, .op = &ignore_attr_op },
314 { "always_inline", NS_KEYWORD, .op = &ignore_attr_op },
315 { "syscall_linkage", NS_KEYWORD, .op = &ignore_attr_op },
316 { "visibility", NS_KEYWORD, .op = &ignore_attr_op },
317 { "__visibility__", NS_KEYWORD, .op = &ignore_attr_op },
318 { "deprecated", NS_KEYWORD, .op = &ignore_attr_op },
319 { "__deprecated__", NS_KEYWORD, .op = &ignore_attr_op },
320 { "noinline", NS_KEYWORD, .op = &ignore_attr_op },
321 { "__used__", NS_KEYWORD, .op = &ignore_attr_op },
322 { "warn_unused_result", NS_KEYWORD, .op = &ignore_attr_op },
323 { "__warn_unused_result__", NS_KEYWORD, .op = &ignore_attr_op },
324 { "model", NS_KEYWORD, .op = &ignore_attr_op },
325 { "__model__", NS_KEYWORD, .op = &ignore_attr_op },
326 { "cdecl", NS_KEYWORD, .op = &ignore_attr_op },
327 { "__cdecl__", NS_KEYWORD, .op = &ignore_attr_op },
328 { "stdcall", NS_KEYWORD, .op = &ignore_attr_op },
329 { "__stdcall__", NS_KEYWORD, .op = &ignore_attr_op },
332 void init_parser(int stream)
334 int i;
335 for (i = 0; i < sizeof keyword_table/sizeof keyword_table[0]; i++) {
336 struct init_keyword *ptr = keyword_table + i;
337 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
338 sym->ident->keyword = 1;
339 sym->ctype.modifiers = ptr->modifiers;
340 sym->op = ptr->op;
344 // Add a symbol to the list of function-local symbols
345 static void fn_local_symbol(struct symbol *sym)
347 if (function_symbol_list)
348 add_symbol(function_symbol_list, sym);
351 static int SENTINEL_ATTR match_idents(struct token *token, ...)
353 va_list args;
355 if (token_type(token) != TOKEN_IDENT)
356 return 0;
358 va_start(args, token);
359 for (;;) {
360 struct ident * next = va_arg(args, struct ident *);
361 if (!next)
362 return 0;
363 if (token->ident == next)
364 return 1;
369 struct statement *alloc_statement(struct position pos, int type)
371 struct statement *stmt = __alloc_statement(0);
372 stmt->type = type;
373 stmt->pos = pos;
374 return stmt;
377 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
379 static int apply_modifiers(struct position pos, struct ctype *ctype)
381 struct symbol *base;
383 while ((base = ctype->base_type)) {
384 switch (base->type) {
385 case SYM_FN:
386 case SYM_ENUM:
387 case SYM_ARRAY:
388 case SYM_BITFIELD:
389 case SYM_PTR:
390 ctype = &base->ctype;
391 continue;
393 break;
396 /* Turn the "virtual types" into real types with real sizes etc */
397 if (ctype->base_type == &int_type) {
398 ctype->base_type = ctype_integer(ctype->modifiers);
399 ctype->modifiers &= ~MOD_SPECIFIER;
400 } else if (ctype->base_type == &fp_type) {
401 ctype->base_type = ctype_fp(ctype->modifiers);
402 ctype->modifiers &= ~MOD_SPECIFIER;
405 if (ctype->modifiers & MOD_BITWISE) {
406 struct symbol *type;
407 ctype->modifiers &= ~(MOD_BITWISE | MOD_SPECIFIER);
408 if (!is_int_type(ctype->base_type)) {
409 sparse_error(pos, "invalid modifier");
410 return 1;
412 type = alloc_symbol(pos, SYM_BASETYPE);
413 *type = *ctype->base_type;
414 type->ctype.base_type = ctype->base_type;
415 type->type = SYM_RESTRICT;
416 type->ctype.modifiers &= ~MOD_SPECIFIER;
417 ctype->base_type = type;
418 create_fouled(type);
420 return 0;
423 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
425 struct symbol *sym = alloc_symbol(pos, type);
427 sym->ctype.base_type = ctype->base_type;
428 sym->ctype.modifiers = ctype->modifiers & ~MOD_STORAGE;
430 ctype->base_type = sym;
431 ctype->modifiers &= MOD_STORAGE;
432 return sym;
435 static struct symbol *lookup_or_create_symbol(enum namespace ns, enum type type, struct token *token)
437 struct symbol *sym = lookup_symbol(token->ident, ns);
438 if (!sym) {
439 sym = alloc_symbol(token->pos, type);
440 bind_symbol(sym, token->ident, ns);
441 if (type == SYM_LABEL)
442 fn_local_symbol(sym);
444 return sym;
448 * NOTE! NS_LABEL is not just a different namespace,
449 * it also ends up using function scope instead of the
450 * regular symbol scope.
452 struct symbol *label_symbol(struct token *token)
454 return lookup_or_create_symbol(NS_LABEL, SYM_LABEL, token);
457 static struct token *struct_union_enum_specifier(enum type type,
458 struct token *token, struct ctype *ctype,
459 struct token *(*parse)(struct token *, struct symbol *))
461 struct symbol *sym;
462 struct position *repos;
464 ctype->modifiers = 0;
465 token = handle_attributes(token, ctype, KW_ATTRIBUTE | KW_ASM);
466 if (token_type(token) == TOKEN_IDENT) {
467 sym = lookup_symbol(token->ident, NS_STRUCT);
468 if (!sym ||
469 (sym->scope != block_scope &&
470 (match_op(token->next,';') || match_op(token->next,'{')))) {
471 // Either a new symbol, or else an out-of-scope
472 // symbol being redefined.
473 sym = alloc_symbol(token->pos, type);
474 bind_symbol(sym, token->ident, NS_STRUCT);
476 if (sym->type != type)
477 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
478 ctype->base_type = sym;
479 repos = &token->pos;
480 token = token->next;
481 if (match_op(token, '{')) {
482 // The following test is actually wrong for empty
483 // structs, but (1) they are not C99, (2) gcc does
484 // the same thing, and (3) it's easier.
485 if (sym->symbol_list)
486 error_die(token->pos, "redefinition of %s", show_typename (sym));
487 sym->pos = *repos;
488 token = parse(token->next, sym);
489 token = expect(token, '}', "at end of struct-union-enum-specifier");
491 // Mark the structure as needing re-examination
492 sym->examined = 0;
494 return token;
497 // private struct/union/enum type
498 if (!match_op(token, '{')) {
499 sparse_error(token->pos, "expected declaration");
500 ctype->base_type = &bad_ctype;
501 return token;
504 sym = alloc_symbol(token->pos, type);
505 token = parse(token->next, sym);
506 ctype->base_type = sym;
507 return expect(token, '}', "at end of specifier");
510 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
512 return struct_declaration_list(token, &sym->symbol_list);
515 static struct token *struct_specifier(struct token *token, struct ctype *ctype)
517 return struct_union_enum_specifier(SYM_STRUCT, token, ctype, parse_struct_declaration);
520 static struct token *union_specifier(struct token *token, struct ctype *ctype)
522 return struct_union_enum_specifier(SYM_UNION, token, ctype, parse_struct_declaration);
526 typedef struct {
527 int x;
528 unsigned long long y;
529 } Num;
531 static void upper_boundary(Num *n, Num *v)
533 if (n->x > v->x)
534 return;
535 if (n->x < v->x) {
536 *n = *v;
537 return;
539 if (n->y < v->y)
540 n->y = v->y;
543 static void lower_boundary(Num *n, Num *v)
545 if (n->x < v->x)
546 return;
547 if (n->x > v->x) {
548 *n = *v;
549 return;
551 if (n->y > v->y)
552 n->y = v->y;
555 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
557 int shift = type->bit_size;
558 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
560 if (!is_unsigned)
561 shift--;
562 if (upper->x == 0 && upper->y >> shift)
563 return 0;
564 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
565 return 1;
566 return 0;
569 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
571 if (s1->bit_size < s2->bit_size) {
572 s1 = s2;
573 } else if (s1->bit_size == s2->bit_size) {
574 if (s2->ctype.modifiers & MOD_UNSIGNED)
575 s1 = s2;
577 if (s1->bit_size < bits_in_int)
578 return &int_ctype;
579 return s1;
582 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
584 struct symbol *sym;
586 FOR_EACH_PTR(list, sym) {
587 struct expression *expr = sym->initializer;
588 struct symbol *ctype;
589 if (expr->type != EXPR_VALUE)
590 continue;
591 ctype = expr->ctype;
592 if (ctype->bit_size == base_type->bit_size)
593 continue;
594 cast_value(expr, base_type, expr, ctype);
595 } END_FOR_EACH_PTR(sym);
598 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
600 unsigned long long lastval = 0;
601 struct symbol *ctype = NULL, *base_type = NULL;
602 Num upper = {-1, 0}, lower = {1, 0};
603 struct symbol_list *entries = NULL;
605 parent->examined = 1;
606 parent->ctype.base_type = &int_ctype;
607 while (token_type(token) == TOKEN_IDENT) {
608 struct expression *expr = NULL;
609 struct token *next = token->next;
610 struct symbol *sym;
612 sym = alloc_symbol(token->pos, SYM_NODE);
613 bind_symbol(sym, token->ident, NS_SYMBOL);
614 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
616 if (match_op(next, '=')) {
617 next = constant_expression(next->next, &expr);
618 lastval = get_expression_value(expr);
619 ctype = &void_ctype;
620 if (expr && expr->ctype)
621 ctype = expr->ctype;
622 } else if (!ctype) {
623 ctype = &int_ctype;
624 } else if (is_int_type(ctype)) {
625 lastval++;
626 } else {
627 error_die(token->pos, "can't increment the last enum member");
630 if (!expr) {
631 expr = alloc_expression(token->pos, EXPR_VALUE);
632 expr->value = lastval;
633 expr->ctype = ctype;
636 sym->initializer = expr;
637 sym->ctype.base_type = parent;
638 add_ptr_list(&entries, sym);
640 if (base_type != &bad_ctype) {
641 if (ctype->type == SYM_NODE)
642 ctype = ctype->ctype.base_type;
643 if (ctype->type == SYM_ENUM) {
644 if (ctype == parent)
645 ctype = base_type;
646 else
647 ctype = ctype->ctype.base_type;
650 * base_type rules:
651 * - if all enums are of the same type, then
652 * the base_type is that type (two first
653 * cases)
654 * - if enums are of different types, they
655 * all have to be integer types, and the
656 * base type is at least "int_ctype".
657 * - otherwise the base_type is "bad_ctype".
659 if (!base_type) {
660 base_type = ctype;
661 } else if (ctype == base_type) {
662 /* nothing */
663 } else if (is_int_type(base_type) && is_int_type(ctype)) {
664 base_type = bigger_enum_type(base_type, ctype);
665 } else
666 base_type = &bad_ctype;
667 parent->ctype.base_type = base_type;
669 if (is_int_type(base_type)) {
670 Num v = {.y = lastval};
671 if (ctype->ctype.modifiers & MOD_UNSIGNED)
672 v.x = 0;
673 else if ((long long)lastval >= 0)
674 v.x = 0;
675 else
676 v.x = -1;
677 upper_boundary(&upper, &v);
678 lower_boundary(&lower, &v);
680 token = next;
681 if (!match_op(token, ','))
682 break;
683 token = token->next;
685 if (!base_type) {
686 sparse_error(token->pos, "bad enum definition");
687 base_type = &bad_ctype;
689 else if (!is_int_type(base_type))
690 base_type = base_type;
691 else if (type_is_ok(base_type, &upper, &lower))
692 base_type = base_type;
693 else if (type_is_ok(&int_ctype, &upper, &lower))
694 base_type = &int_ctype;
695 else if (type_is_ok(&uint_ctype, &upper, &lower))
696 base_type = &uint_ctype;
697 else if (type_is_ok(&long_ctype, &upper, &lower))
698 base_type = &long_ctype;
699 else if (type_is_ok(&ulong_ctype, &upper, &lower))
700 base_type = &ulong_ctype;
701 else if (type_is_ok(&llong_ctype, &upper, &lower))
702 base_type = &llong_ctype;
703 else if (type_is_ok(&ullong_ctype, &upper, &lower))
704 base_type = &ullong_ctype;
705 else
706 base_type = &bad_ctype;
707 parent->ctype.base_type = base_type;
708 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
709 parent->examined = 0;
711 cast_enum_list(entries, base_type);
712 free_ptr_list(&entries);
714 return token;
717 static struct token *enum_specifier(struct token *token, struct ctype *ctype)
719 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctype, parse_enum_declaration);
721 ctype = &ctype->base_type->ctype;
722 if (!ctype->base_type)
723 ctype->base_type = &incomplete_ctype;
725 return ret;
728 static struct token *typeof_specifier(struct token *token, struct ctype *ctype)
730 struct symbol *sym;
732 if (!match_op(token, '(')) {
733 sparse_error(token->pos, "expected '(' after typeof");
734 return token;
736 if (lookup_type(token->next)) {
737 token = typename(token->next, &sym);
738 *ctype = sym->ctype;
739 } else {
740 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
741 token = parse_expression(token->next, &typeof_sym->initializer);
743 ctype->modifiers = 0;
744 ctype->base_type = typeof_sym;
746 return expect(token, ')', "after typeof");
749 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct ctype *ctype)
751 struct expression *expr = NULL;
752 if (match_op(token, '('))
753 token = parens_expression(token, &expr, "in attribute");
754 return token;
757 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct ctype *ctype)
759 ctype->alignment = 1;
760 return token;
763 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct ctype *ctype)
765 int alignment = max_alignment;
766 struct expression *expr = NULL;
768 if (match_op(token, '(')) {
769 token = parens_expression(token, &expr, "in attribute");
770 if (expr)
771 alignment = get_expression_value(expr);
773 ctype->alignment = alignment;
774 return token;
777 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct ctype *ctype)
779 ctype->modifiers |= attr->ctype.modifiers;
780 return token;
783 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct ctype *ctype)
785 struct expression *expr = NULL;
786 token = expect(token, '(', "after address_space attribute");
787 token = conditional_expression(token, &expr);
788 if (expr)
789 ctype->as = get_expression_value(expr);
790 token = expect(token, ')', "after address_space attribute");
791 return token;
794 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct ctype *ctype)
796 token = expect(token, '(', "after mode attribute");
797 if (token_type(token) == TOKEN_IDENT) {
798 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
799 if (mode && mode->op->type == KW_MODE)
800 ctype->modifiers |= mode->ctype.modifiers;
801 else
802 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
803 token = token->next;
804 } else
805 sparse_error(token->pos, "expect attribute mode symbol\n");
806 token = expect(token, ')', "after mode attribute");
807 return token;
810 static struct token *attribute_context(struct token *token, struct symbol *attr, struct ctype *ctype)
812 struct context *context = alloc_context();
813 struct expression *args[3];
814 int argc = 0;
816 token = expect(token, '(', "after context attribute");
817 while (!match_op(token, ')')) {
818 struct expression *expr = NULL;
819 token = conditional_expression(token, &expr);
820 if (!expr)
821 break;
822 if (argc < 3)
823 args[argc++] = expr;
824 if (!match_op(token, ','))
825 break;
826 token = token->next;
829 switch(argc) {
830 case 0:
831 sparse_error(token->pos, "expected context input/output values");
832 break;
833 case 1:
834 context->in = get_expression_value(args[0]);
835 break;
836 case 2:
837 context->in = get_expression_value(args[0]);
838 context->out = get_expression_value(args[1]);
839 break;
840 case 3:
841 context->context = args[0];
842 context->in = get_expression_value(args[1]);
843 context->out = get_expression_value(args[2]);
844 break;
847 if (argc)
848 add_ptr_list(&ctype->contexts, context);
850 token = expect(token, ')', "after context attribute");
851 return token;
854 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct ctype *ctype)
856 if (Wtransparent_union)
857 sparse_error(token->pos, "ignoring attribute __transparent_union__");
858 return token;
861 static struct token *recover_unknown_attribute(struct token *token)
863 struct expression *expr = NULL;
865 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
866 token = token->next;
867 if (match_op(token, '('))
868 token = parens_expression(token, &expr, "in attribute");
869 return token;
872 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
874 ctype->modifiers = 0;
875 token = expect(token, '(', "after attribute");
876 token = expect(token, '(', "after attribute");
878 for (;;) {
879 struct ident *attribute_name;
880 struct symbol *attr;
882 if (eof_token(token))
883 break;
884 if (match_op(token, ';'))
885 break;
886 if (token_type(token) != TOKEN_IDENT)
887 break;
888 attribute_name = token->ident;
889 attr = lookup_keyword(attribute_name, NS_KEYWORD);
890 if (attr && attr->op->attribute)
891 token = attr->op->attribute(token->next, attr, ctype);
892 else
893 token = recover_unknown_attribute(token);
895 if (!match_op(token, ','))
896 break;
897 token = token->next;
900 token = expect(token, ')', "after attribute");
901 token = expect(token, ')', "after attribute");
902 return token;
905 struct symbol * ctype_integer(unsigned long spec)
907 static struct symbol *const integer_ctypes[][3] = {
908 { &llong_ctype, &sllong_ctype, &ullong_ctype },
909 { &long_ctype, &slong_ctype, &ulong_ctype },
910 { &short_ctype, &sshort_ctype, &ushort_ctype },
911 { &char_ctype, &schar_ctype, &uchar_ctype },
912 { &int_ctype, &sint_ctype, &uint_ctype },
914 struct symbol *const (*ctype)[3];
915 int sub;
917 ctype = integer_ctypes;
918 if (!(spec & MOD_LONGLONG)) {
919 ctype++;
920 if (!(spec & MOD_LONG)) {
921 ctype++;
922 if (!(spec & MOD_SHORT)) {
923 ctype++;
924 if (!(spec & MOD_CHAR))
925 ctype++;
930 sub = ((spec & MOD_UNSIGNED)
932 : ((spec & MOD_EXPLICITLY_SIGNED)
934 : 0));
936 return ctype[0][sub];
939 struct symbol * ctype_fp(unsigned long spec)
941 if (spec & MOD_LONGLONG)
942 return &ldouble_ctype;
943 if (spec & MOD_LONG)
944 return &double_ctype;
945 return &float_ctype;
948 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
950 unsigned long mod = thistype->modifiers;
952 if (mod) {
953 unsigned long old = ctype->modifiers;
954 unsigned long extra = 0, dup, conflict;
956 if (mod & old & MOD_LONG) {
957 extra = MOD_LONGLONG | MOD_LONG;
958 mod &= ~MOD_LONG;
959 old &= ~MOD_LONG;
961 dup = (mod & old) | (extra & old) | (extra & mod);
962 if (dup)
963 sparse_error(pos, "Just how %sdo you want this type to be?",
964 modifier_string(dup));
966 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
967 if (conflict)
968 sparse_error(pos, "You cannot have both long and short modifiers.");
970 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
971 if (conflict)
972 sparse_error(pos, "You cannot have both signed and unsigned modifiers.");
974 // Only one storage modifier allowed, except that "inline" doesn't count.
975 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
976 conflict &= (conflict - 1);
977 if (conflict)
978 sparse_error(pos, "multiple storage classes");
980 ctype->modifiers = old | mod | extra;
983 /* Context */
984 concat_ptr_list((struct ptr_list *)thistype->contexts,
985 (struct ptr_list **)&ctype->contexts);
987 /* Alignment */
988 if (thistype->alignment & (thistype->alignment-1)) {
989 warning(pos, "I don't like non-power-of-2 alignments");
990 thistype->alignment = 0;
992 if (thistype->alignment > ctype->alignment)
993 ctype->alignment = thistype->alignment;
995 /* Address space */
996 if (thistype->as)
997 ctype->as = thistype->as;
1000 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
1002 unsigned long banned, wrong;
1003 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
1004 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
1006 if (s->type == SYM_KEYWORD)
1007 banned = s->op->type == KW_SPECIFIER ? (BANNED_SIZE | BANNED_SIGN) : 0;
1008 else if (s->ctype.base_type == &fp_type)
1009 banned = BANNED_SIGN;
1010 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
1011 banned = 0;
1012 else {
1013 // label_type
1014 // void_type
1015 // bad_type
1016 // vector_type <-- whatever that is
1017 banned = BANNED_SIZE | BANNED_SIGN;
1020 wrong = mod & banned;
1021 if (wrong)
1022 sparse_error(*pos, "modifier %sis invalid in this context",
1023 modifier_string (wrong));
1026 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
1028 struct token *token;
1030 while ( (token = next) != NULL ) {
1031 struct ctype thistype;
1032 struct ident *ident;
1033 struct symbol *s, *type;
1034 unsigned long mod;
1036 next = token->next;
1037 if (token_type(token) != TOKEN_IDENT)
1038 break;
1039 ident = token->ident;
1041 s = lookup_symbol(ident, NS_TYPEDEF);
1042 if (!s)
1043 break;
1044 thistype = s->ctype;
1045 mod = thistype.modifiers;
1046 if (qual) {
1047 if (s->type != SYM_KEYWORD)
1048 break;
1049 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1050 break;
1052 if (s->type == SYM_KEYWORD && s->op->declarator) {
1053 next = s->op->declarator(next, &thistype);
1054 mod = thistype.modifiers;
1056 type = thistype.base_type;
1057 if (type) {
1058 if (qual)
1059 break;
1060 if (ctype->base_type)
1061 break;
1062 /* User types only mix with qualifiers */
1063 if (mod & MOD_USERTYPE) {
1064 if (ctype->modifiers & MOD_SPECIFIER)
1065 break;
1067 ctype->base_type = type;
1070 check_modifiers(&token->pos, s, ctype->modifiers);
1071 apply_ctype(token->pos, &thistype, ctype);
1074 if (!ctype->base_type) {
1075 struct symbol *base = &incomplete_ctype;
1078 * If we have modifiers, we'll default to an integer
1079 * type, and "ctype_integer()" will turn this into
1080 * a specific one.
1082 if (ctype->modifiers & MOD_SPECIFIER)
1083 base = &int_type;
1084 ctype->base_type = base;
1086 return token;
1089 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1091 struct expression *expr = NULL;
1093 token = parse_expression(token, &expr);
1094 sym->array_size = expr;
1095 return token;
1098 static struct token *parameter_type_list(struct token *, struct symbol *, struct ident **p);
1099 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
1101 static struct token *handle_attributes(struct token *token, struct ctype *ctype, unsigned int keywords)
1103 struct symbol *keyword;
1104 for (;;) {
1105 struct ctype thistype = { 0, };
1106 if (token_type(token) != TOKEN_IDENT)
1107 break;
1108 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1109 if (!keyword || keyword->type != SYM_KEYWORD)
1110 break;
1111 if (!(keyword->op->type & keywords))
1112 break;
1113 token = keyword->op->declarator(token->next, &thistype);
1114 apply_ctype(token->pos, &thistype, ctype);
1116 return token;
1119 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
1121 struct ctype *ctype = &decl->ctype;
1123 if (p && token_type(token) == TOKEN_IDENT) {
1124 *p = token->ident;
1125 token = token->next;
1128 for (;;) {
1129 token = handle_attributes(token, ctype, KW_ATTRIBUTE | KW_ASM);
1131 if (token_type(token) != TOKEN_SPECIAL)
1132 return token;
1135 * This can be either a parameter list or a grouping.
1136 * For the direct (non-abstract) case, we know if must be
1137 * a parameter list if we already saw the identifier.
1138 * For the abstract case, we know if must be a parameter
1139 * list if it is empty or starts with a type.
1141 if (token->special == '(') {
1142 struct symbol *sym;
1143 struct token *next = token->next;
1144 int fn = (p && *p) || match_op(next, ')') || lookup_type(next);
1146 if (!fn) {
1147 struct symbol *base_type = ctype->base_type;
1148 token = declarator(next, decl, p);
1149 token = expect(token, ')', "in nested declarator");
1150 while (ctype->base_type != base_type)
1151 ctype = &ctype->base_type->ctype;
1152 p = NULL;
1153 continue;
1156 sym = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1157 token = parameter_type_list(next, sym, p);
1158 token = expect(token, ')', "in function declarator");
1159 continue;
1161 if (token->special == '[') {
1162 struct symbol *array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1163 token = abstract_array_declarator(token->next, array);
1164 token = expect(token, ']', "in abstract_array_declarator");
1165 ctype = &array->ctype;
1166 continue;
1168 break;
1170 return token;
1173 static struct token *pointer(struct token *token, struct ctype *ctype)
1175 unsigned long modifiers;
1176 struct symbol *base_type;
1178 modifiers = ctype->modifiers & ~MOD_TYPEDEF;
1179 base_type = ctype->base_type;
1180 ctype->modifiers = modifiers;
1182 while (match_op(token,'*')) {
1183 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1184 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
1185 ptr->ctype.as = ctype->as;
1186 concat_ptr_list((struct ptr_list *)ctype->contexts,
1187 (struct ptr_list **)&ptr->ctype.contexts);
1188 ptr->ctype.base_type = base_type;
1190 base_type = ptr;
1191 ctype->modifiers = modifiers & MOD_STORAGE;
1192 ctype->base_type = base_type;
1193 ctype->as = 0;
1194 free_ptr_list(&ctype->contexts);
1196 token = declaration_specifiers(token->next, ctype, 1);
1197 modifiers = ctype->modifiers;
1199 return token;
1202 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
1204 token = pointer(token, &sym->ctype);
1205 return direct_declarator(token, sym, p);
1208 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
1210 struct ctype *ctype = &decl->ctype;
1211 struct expression *expr;
1212 struct symbol *bitfield;
1213 long long width;
1215 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1216 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1217 show_typename(ctype->base_type));
1218 // Parse this to recover gracefully.
1219 return conditional_expression(token->next, &expr);
1222 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1223 token = conditional_expression(token->next, &expr);
1224 width = get_expression_value(expr);
1225 bitfield->bit_size = width;
1227 if (width < 0 || width > INT_MAX) {
1228 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1229 width = -1;
1230 } else if (decl->ident && width == 0) {
1231 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1232 show_ident(decl->ident));
1233 width = -1;
1234 } else if (decl->ident) {
1235 struct symbol *base_type = bitfield->ctype.base_type;
1236 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1237 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1238 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1239 // Valid values are either {-1;0} or {0}, depending on integer
1240 // representation. The latter makes for very efficient code...
1241 sparse_error(token->pos, "dubious one-bit signed bitfield");
1243 if (Wdefault_bitfield_sign &&
1244 bitfield_type->type != SYM_ENUM &&
1245 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1246 is_signed) {
1247 // The sign of bitfields is unspecified by default.
1248 sparse_error(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1251 bitfield->bit_size = width;
1252 return token;
1255 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1257 struct ctype ctype = {0, };
1259 token = declaration_specifiers(token, &ctype, 0);
1260 for (;;) {
1261 struct ident *ident = NULL;
1262 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1263 decl->ctype = ctype;
1264 token = declarator(token, decl, &ident);
1265 decl->ident = ident;
1266 if (match_op(token, ':')) {
1267 token = handle_bitfield(token, decl);
1268 token = handle_attributes(token, &decl->ctype, KW_ATTRIBUTE | KW_ASM);
1270 apply_modifiers(token->pos, &decl->ctype);
1271 add_symbol(list, decl);
1272 if (!match_op(token, ','))
1273 break;
1274 token = token->next;
1276 return token;
1279 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1281 while (!match_op(token, '}')) {
1282 if (!match_op(token, ';'))
1283 token = declaration_list(token, list);
1284 if (!match_op(token, ';')) {
1285 sparse_error(token->pos, "expected ; at end of declaration");
1286 break;
1288 token = token->next;
1290 return token;
1293 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
1295 struct ident *ident = NULL;
1296 struct symbol *sym;
1297 struct ctype ctype = { 0, };
1299 token = declaration_specifiers(token, &ctype, 0);
1300 sym = alloc_symbol(token->pos, SYM_NODE);
1301 sym->ctype = ctype;
1302 *tree = sym;
1303 token = declarator(token, sym, &ident);
1304 sym->ident = ident;
1305 apply_modifiers(token->pos, &sym->ctype);
1306 return token;
1309 struct token *typename(struct token *token, struct symbol **p)
1311 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1312 *p = sym;
1313 token = declaration_specifiers(token, &sym->ctype, 0);
1314 token = declarator(token, sym, NULL);
1315 apply_modifiers(token->pos, &sym->ctype);
1316 return token;
1319 static struct token *expression_statement(struct token *token, struct expression **tree)
1321 token = parse_expression(token, tree);
1322 return expect(token, ';', "at end of statement");
1325 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1326 struct expression_list **inout)
1328 struct expression *expr;
1330 /* Allow empty operands */
1331 if (match_op(token->next, ':') || match_op(token->next, ')'))
1332 return token->next;
1333 do {
1334 struct ident *ident = NULL;
1335 if (match_op(token->next, '[') &&
1336 token_type(token->next->next) == TOKEN_IDENT &&
1337 match_op(token->next->next->next, ']')) {
1338 ident = token->next->next->ident;
1339 token = token->next->next->next;
1341 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1342 token = primary_expression(token->next, &expr);
1343 add_expression(inout, expr);
1344 token = parens_expression(token, &expr, "in asm parameter");
1345 add_expression(inout, expr);
1346 } while (match_op(token, ','));
1347 return token;
1350 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1351 struct expression_list **clobbers)
1353 struct expression *expr;
1355 do {
1356 token = primary_expression(token->next, &expr);
1357 add_expression(clobbers, expr);
1358 } while (match_op(token, ','));
1359 return token;
1362 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1364 token = token->next;
1365 stmt->type = STMT_ASM;
1366 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1367 token = token->next;
1369 token = expect(token, '(', "after asm");
1370 token = parse_expression(token, &stmt->asm_string);
1371 if (match_op(token, ':'))
1372 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1373 if (match_op(token, ':'))
1374 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1375 if (match_op(token, ':'))
1376 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1377 token = expect(token, ')', "after asm");
1378 return expect(token, ';', "at end of asm-statement");
1381 static struct token *parse_asm_declarator(struct token *token, struct ctype *ctype)
1383 struct expression *expr;
1384 token = expect(token, '(', "after asm");
1385 token = parse_expression(token->next, &expr);
1386 token = expect(token, ')', "after asm");
1387 return token;
1390 /* Make a statement out of an expression */
1391 static struct statement *make_statement(struct expression *expr)
1393 struct statement *stmt;
1395 if (!expr)
1396 return NULL;
1397 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1398 stmt->expression = expr;
1399 return stmt;
1403 * All iterators have two symbols associated with them:
1404 * the "continue" and "break" symbols, which are targets
1405 * for continue and break statements respectively.
1407 * They are in a special name-space, but they follow
1408 * all the normal visibility rules, so nested iterators
1409 * automatically work right.
1411 static void start_iterator(struct statement *stmt)
1413 struct symbol *cont, *brk;
1415 start_symbol_scope();
1416 cont = alloc_symbol(stmt->pos, SYM_NODE);
1417 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1418 brk = alloc_symbol(stmt->pos, SYM_NODE);
1419 bind_symbol(brk, &break_ident, NS_ITERATOR);
1421 stmt->type = STMT_ITERATOR;
1422 stmt->iterator_break = brk;
1423 stmt->iterator_continue = cont;
1424 fn_local_symbol(brk);
1425 fn_local_symbol(cont);
1428 static void end_iterator(struct statement *stmt)
1430 end_symbol_scope();
1433 static struct statement *start_function(struct symbol *sym)
1435 struct symbol *ret;
1436 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1438 start_function_scope();
1439 ret = alloc_symbol(sym->pos, SYM_NODE);
1440 ret->ctype = sym->ctype.base_type->ctype;
1441 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1442 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1443 bind_symbol(ret, &return_ident, NS_ITERATOR);
1444 stmt->ret = ret;
1445 fn_local_symbol(ret);
1447 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1448 current_fn = sym;
1450 return stmt;
1453 static void end_function(struct symbol *sym)
1455 current_fn = NULL;
1456 end_function_scope();
1460 * A "switch()" statement, like an iterator, has a
1461 * the "break" symbol associated with it. It works
1462 * exactly like the iterator break - it's the target
1463 * for any break-statements in scope, and means that
1464 * "break" handling doesn't even need to know whether
1465 * it's breaking out of an iterator or a switch.
1467 * In addition, the "case" symbol is a marker for the
1468 * case/default statements to find the switch statement
1469 * that they are associated with.
1471 static void start_switch(struct statement *stmt)
1473 struct symbol *brk, *switch_case;
1475 start_symbol_scope();
1476 brk = alloc_symbol(stmt->pos, SYM_NODE);
1477 bind_symbol(brk, &break_ident, NS_ITERATOR);
1479 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1480 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1481 switch_case->stmt = stmt;
1483 stmt->type = STMT_SWITCH;
1484 stmt->switch_break = brk;
1485 stmt->switch_case = switch_case;
1487 fn_local_symbol(brk);
1488 fn_local_symbol(switch_case);
1491 static void end_switch(struct statement *stmt)
1493 if (!stmt->switch_case->symbol_list)
1494 warning(stmt->pos, "switch with no cases");
1495 end_symbol_scope();
1498 static void add_case_statement(struct statement *stmt)
1500 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1501 struct symbol *sym;
1503 if (!target) {
1504 sparse_error(stmt->pos, "not in switch scope");
1505 stmt->type = STMT_NONE;
1506 return;
1508 sym = alloc_symbol(stmt->pos, SYM_NODE);
1509 add_symbol(&target->symbol_list, sym);
1510 sym->stmt = stmt;
1511 stmt->case_label = sym;
1512 fn_local_symbol(sym);
1515 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1517 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1519 if (!target)
1520 error_die(token->pos, "internal error: return without a function target");
1521 stmt->type = STMT_RETURN;
1522 stmt->ret_target = target;
1523 return expression_statement(token->next, &stmt->ret_value);
1526 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1528 struct symbol_list *syms;
1529 struct expression *e1, *e2, *e3;
1530 struct statement *iterator;
1532 start_iterator(stmt);
1533 token = expect(token->next, '(', "after 'for'");
1535 syms = NULL;
1536 e1 = NULL;
1537 /* C99 variable declaration? */
1538 if (lookup_type(token)) {
1539 token = external_declaration(token, &syms);
1540 } else {
1541 token = parse_expression(token, &e1);
1542 token = expect(token, ';', "in 'for'");
1544 token = parse_expression(token, &e2);
1545 token = expect(token, ';', "in 'for'");
1546 token = parse_expression(token, &e3);
1547 token = expect(token, ')', "in 'for'");
1548 token = statement(token, &iterator);
1550 stmt->iterator_syms = syms;
1551 stmt->iterator_pre_statement = make_statement(e1);
1552 stmt->iterator_pre_condition = e2;
1553 stmt->iterator_post_statement = make_statement(e3);
1554 stmt->iterator_post_condition = NULL;
1555 stmt->iterator_statement = iterator;
1556 end_iterator(stmt);
1558 return token;
1561 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
1563 struct expression *expr;
1564 struct statement *iterator;
1566 start_iterator(stmt);
1567 token = parens_expression(token->next, &expr, "after 'while'");
1568 token = statement(token, &iterator);
1570 stmt->iterator_pre_condition = expr;
1571 stmt->iterator_post_condition = NULL;
1572 stmt->iterator_statement = iterator;
1573 end_iterator(stmt);
1575 return token;
1578 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
1580 struct expression *expr;
1581 struct statement *iterator;
1583 start_iterator(stmt);
1584 token = statement(token->next, &iterator);
1585 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1586 token = token->next;
1587 else
1588 sparse_error(token->pos, "expected 'while' after 'do'");
1589 token = parens_expression(token, &expr, "after 'do-while'");
1591 stmt->iterator_post_condition = expr;
1592 stmt->iterator_statement = iterator;
1593 end_iterator(stmt);
1595 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
1596 warning(iterator->pos, "do-while statement is not a compound statement");
1598 return expect(token, ';', "after statement");
1601 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
1603 stmt->type = STMT_IF;
1604 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1605 token = statement(token, &stmt->if_true);
1606 if (token_type(token) != TOKEN_IDENT)
1607 return token;
1608 if (token->ident != &else_ident)
1609 return token;
1610 return statement(token->next, &stmt->if_false);
1613 static inline struct token *case_statement(struct token *token, struct statement *stmt)
1615 stmt->type = STMT_CASE;
1616 token = expect(token, ':', "after default/case");
1617 add_case_statement(stmt);
1618 return statement(token, &stmt->case_statement);
1621 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
1623 token = parse_expression(token->next, &stmt->case_expression);
1624 if (match_op(token, SPECIAL_ELLIPSIS))
1625 token = parse_expression(token->next, &stmt->case_to);
1626 return case_statement(token, stmt);
1629 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
1631 return case_statement(token->next, stmt);
1634 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
1636 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1637 stmt->type = STMT_GOTO;
1638 stmt->goto_label = target;
1639 if (!target)
1640 sparse_error(stmt->pos, "break/continue not in iterator scope");
1641 return expect(token->next, ';', "at end of statement");
1644 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
1646 stmt->type = STMT_SWITCH;
1647 start_switch(stmt);
1648 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1649 token = statement(token, &stmt->switch_statement);
1650 end_switch(stmt);
1651 return token;
1654 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
1656 stmt->type = STMT_GOTO;
1657 token = token->next;
1658 if (match_op(token, '*')) {
1659 token = parse_expression(token->next, &stmt->goto_expression);
1660 add_statement(&function_computed_goto_list, stmt);
1661 } else if (token_type(token) == TOKEN_IDENT) {
1662 stmt->goto_label = label_symbol(token);
1663 token = token->next;
1664 } else {
1665 sparse_error(token->pos, "Expected identifier or goto expression");
1667 return expect(token, ';', "at end of statement");
1670 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
1672 stmt->type = STMT_CONTEXT;
1673 token = parse_expression(token->next, &stmt->expression);
1674 if(stmt->expression->type == EXPR_PREOP
1675 && stmt->expression->op == '('
1676 && stmt->expression->unop->type == EXPR_COMMA) {
1677 struct expression *expr;
1678 expr = stmt->expression->unop;
1679 stmt->context = expr->left;
1680 stmt->expression = expr->right;
1682 return expect(token, ';', "at end of statement");
1685 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
1687 stmt->type = STMT_RANGE;
1688 token = assignment_expression(token->next, &stmt->range_expression);
1689 token = expect(token, ',', "after range expression");
1690 token = assignment_expression(token, &stmt->range_low);
1691 token = expect(token, ',', "after low range");
1692 token = assignment_expression(token, &stmt->range_high);
1693 return expect(token, ';', "after range statement");
1696 static struct token *statement(struct token *token, struct statement **tree)
1698 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1700 *tree = stmt;
1701 if (token_type(token) == TOKEN_IDENT) {
1702 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
1703 if (s && s->op->statement)
1704 return s->op->statement(token, stmt);
1706 if (match_op(token->next, ':')) {
1707 stmt->type = STMT_LABEL;
1708 stmt->label_identifier = label_symbol(token);
1709 token = handle_attributes(token->next->next, &stmt->label_identifier->ctype, KW_ATTRIBUTE);
1710 return statement(token, &stmt->label_statement);
1714 if (match_op(token, '{')) {
1715 stmt->type = STMT_COMPOUND;
1716 start_symbol_scope();
1717 token = compound_statement(token->next, stmt);
1718 end_symbol_scope();
1720 return expect(token, '}', "at end of compound statement");
1723 stmt->type = STMT_EXPRESSION;
1724 return expression_statement(token, &stmt->expression);
1727 static struct token * statement_list(struct token *token, struct statement_list **list)
1729 int seen_statement = 0;
1730 for (;;) {
1731 struct statement * stmt;
1732 if (eof_token(token))
1733 break;
1734 if (match_op(token, '}'))
1735 break;
1736 if (lookup_type(token)) {
1737 if (seen_statement) {
1738 warning(token->pos, "mixing declarations and code");
1739 seen_statement = 0;
1741 stmt = alloc_statement(token->pos, STMT_DECLARATION);
1742 token = external_declaration(token, &stmt->declaration);
1743 } else {
1744 seen_statement = warn_on_mixed;
1745 token = statement(token, &stmt);
1747 add_statement(list, stmt);
1749 return token;
1752 static struct token *parameter_type_list(struct token *token, struct symbol *fn, struct ident **p)
1754 struct symbol_list **list = &fn->arguments;
1756 if (match_op(token, ')')) {
1757 // No warning for "void oink ();"
1758 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1759 if (p && !match_op(token->next, ';'))
1760 warning(token->pos, "non-ANSI function declaration of function '%s'", show_ident(*p));
1761 return token;
1764 for (;;) {
1765 struct symbol *sym;
1767 if (match_op(token, SPECIAL_ELLIPSIS)) {
1768 if (!*list)
1769 warning(token->pos, "variadic functions must have one named argument");
1770 fn->variadic = 1;
1771 token = token->next;
1772 break;
1775 sym = alloc_symbol(token->pos, SYM_NODE);
1776 token = parameter_declaration(token, &sym);
1777 if (sym->ctype.base_type == &void_ctype) {
1778 /* Special case: (void) */
1779 if (!*list && !sym->ident)
1780 break;
1781 warning(token->pos, "void parameter");
1783 add_symbol(list, sym);
1784 if (!match_op(token, ','))
1785 break;
1786 token = token->next;
1789 return token;
1792 struct token *compound_statement(struct token *token, struct statement *stmt)
1794 token = statement_list(token, &stmt->stmts);
1795 return token;
1798 static struct expression *identifier_expression(struct token *token)
1800 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1801 expr->expr_ident = token->ident;
1802 return expr;
1805 static struct expression *index_expression(struct expression *from, struct expression *to)
1807 int idx_from, idx_to;
1808 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1810 idx_from = get_expression_value(from);
1811 idx_to = idx_from;
1812 if (to) {
1813 idx_to = get_expression_value(to);
1814 if (idx_to < idx_from || idx_from < 0)
1815 warning(from->pos, "nonsense array initializer index range");
1817 expr->idx_from = idx_from;
1818 expr->idx_to = idx_to;
1819 return expr;
1822 static struct token *single_initializer(struct expression **ep, struct token *token)
1824 int expect_equal = 0;
1825 struct token *next = token->next;
1826 struct expression **tail = ep;
1827 int nested;
1829 *ep = NULL;
1831 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1832 struct expression *expr = identifier_expression(token);
1833 if (Wold_initializer)
1834 warning(token->pos, "obsolete struct initializer, use C99 syntax");
1835 token = initializer(&expr->ident_expression, next->next);
1836 if (expr->ident_expression)
1837 *ep = expr;
1838 return token;
1841 for (tail = ep, nested = 0; ; nested++, next = token->next) {
1842 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
1843 struct expression *expr = identifier_expression(next);
1844 *tail = expr;
1845 tail = &expr->ident_expression;
1846 expect_equal = 1;
1847 token = next->next;
1848 } else if (match_op(token, '[')) {
1849 struct expression *from = NULL, *to = NULL, *expr;
1850 token = constant_expression(token->next, &from);
1851 if (!from) {
1852 sparse_error(token->pos, "Expected constant expression");
1853 break;
1855 if (match_op(token, SPECIAL_ELLIPSIS))
1856 token = constant_expression(token->next, &to);
1857 expr = index_expression(from, to);
1858 *tail = expr;
1859 tail = &expr->idx_expression;
1860 token = expect(token, ']', "at end of initializer index");
1861 if (nested)
1862 expect_equal = 1;
1863 } else {
1864 break;
1867 if (nested && !expect_equal) {
1868 if (!match_op(token, '='))
1869 warning(token->pos, "obsolete array initializer, use C99 syntax");
1870 else
1871 expect_equal = 1;
1873 if (expect_equal)
1874 token = expect(token, '=', "at end of initializer index");
1876 token = initializer(tail, token);
1877 if (!*tail)
1878 *ep = NULL;
1879 return token;
1882 static struct token *initializer_list(struct expression_list **list, struct token *token)
1884 struct expression *expr;
1886 for (;;) {
1887 token = single_initializer(&expr, token);
1888 if (!expr)
1889 break;
1890 add_expression(list, expr);
1891 if (!match_op(token, ','))
1892 break;
1893 token = token->next;
1895 return token;
1898 struct token *initializer(struct expression **tree, struct token *token)
1900 if (match_op(token, '{')) {
1901 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1902 *tree = expr;
1903 token = initializer_list(&expr->expr_list, token->next);
1904 return expect(token, '}', "at end of initializer");
1906 return assignment_expression(token, tree);
1909 static void declare_argument(struct symbol *sym, struct symbol *fn)
1911 if (!sym->ident) {
1912 sparse_error(sym->pos, "no identifier for function argument");
1913 return;
1915 bind_symbol(sym, sym->ident, NS_SYMBOL);
1918 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1919 struct symbol_list **list)
1921 struct symbol_list **old_symbol_list;
1922 struct symbol *base_type = decl->ctype.base_type;
1923 struct statement *stmt, **p;
1924 struct symbol *arg;
1926 old_symbol_list = function_symbol_list;
1927 if (decl->ctype.modifiers & MOD_INLINE) {
1928 function_symbol_list = &decl->inline_symbol_list;
1929 p = &base_type->inline_stmt;
1930 } else {
1931 function_symbol_list = &decl->symbol_list;
1932 p = &base_type->stmt;
1934 function_computed_target_list = NULL;
1935 function_computed_goto_list = NULL;
1937 if (decl->ctype.modifiers & MOD_EXTERN) {
1938 if (!(decl->ctype.modifiers & MOD_INLINE))
1939 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
1941 if (!(decl->ctype.modifiers & MOD_STATIC))
1942 decl->ctype.modifiers |= MOD_EXTERN;
1944 stmt = start_function(decl);
1946 *p = stmt;
1947 FOR_EACH_PTR (base_type->arguments, arg) {
1948 declare_argument(arg, base_type);
1949 } END_FOR_EACH_PTR(arg);
1951 token = compound_statement(token->next, stmt);
1953 end_function(decl);
1954 if (!(decl->ctype.modifiers & MOD_INLINE))
1955 add_symbol(list, decl);
1956 check_declaration(decl);
1957 function_symbol_list = old_symbol_list;
1958 if (function_computed_goto_list) {
1959 if (!function_computed_target_list)
1960 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
1961 else {
1962 FOR_EACH_PTR(function_computed_goto_list, stmt) {
1963 stmt->target_list = function_computed_target_list;
1964 } END_FOR_EACH_PTR(stmt);
1967 return expect(token, '}', "at end of function");
1970 static void promote_k_r_types(struct symbol *arg)
1972 struct symbol *base = arg->ctype.base_type;
1973 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
1974 arg->ctype.base_type = &int_ctype;
1978 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
1980 struct symbol_list *real_args = fn->ctype.base_type->arguments;
1981 struct symbol *arg;
1983 FOR_EACH_PTR(real_args, arg) {
1984 struct symbol *type;
1986 /* This is quadratic in the number of arguments. We _really_ don't care */
1987 FOR_EACH_PTR(argtypes, type) {
1988 if (type->ident == arg->ident)
1989 goto match;
1990 } END_FOR_EACH_PTR(type);
1991 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
1992 continue;
1993 match:
1994 type->used = 1;
1995 /* "char" and "short" promote to "int" */
1996 promote_k_r_types(type);
1998 arg->ctype = type->ctype;
1999 } END_FOR_EACH_PTR(arg);
2001 FOR_EACH_PTR(argtypes, arg) {
2002 if (!arg->used)
2003 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2004 } END_FOR_EACH_PTR(arg);
2008 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2009 struct symbol_list **list)
2011 struct symbol_list *args = NULL;
2013 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2014 do {
2015 token = declaration_list(token, &args);
2016 if (!match_op(token, ';')) {
2017 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2018 break;
2020 token = token->next;
2021 } while (lookup_type(token));
2023 apply_k_r_types(args, decl);
2025 if (!match_op(token, '{')) {
2026 sparse_error(token->pos, "expected function body");
2027 return token;
2029 return parse_function_body(token, decl, list);
2032 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2034 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2035 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2036 struct statement *stmt;
2038 anon->ctype.base_type = fn;
2039 stmt = alloc_statement(token->pos, STMT_NONE);
2040 fn->stmt = stmt;
2042 token = parse_asm_statement(token, stmt);
2044 add_symbol(list, anon);
2045 return token;
2048 struct token *external_declaration(struct token *token, struct symbol_list **list)
2050 struct ident *ident = NULL;
2051 struct symbol *decl;
2052 struct ctype ctype = { 0, };
2053 struct symbol *base_type;
2054 int is_typedef;
2056 /* Top-level inline asm? */
2057 if (token_type(token) == TOKEN_IDENT) {
2058 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2059 if (s && s->op->toplevel)
2060 return s->op->toplevel(token, list);
2063 /* Parse declaration-specifiers, if any */
2064 token = declaration_specifiers(token, &ctype, 0);
2065 decl = alloc_symbol(token->pos, SYM_NODE);
2066 decl->ctype = ctype;
2067 token = declarator(token, decl, &ident);
2068 apply_modifiers(token->pos, &decl->ctype);
2070 /* Just a type declaration? */
2071 if (!ident)
2072 return expect(token, ';', "end of type declaration");
2074 /* type define declaration? */
2075 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
2077 /* Typedefs don't have meaningful storage */
2078 if (is_typedef) {
2079 ctype.modifiers &= ~MOD_STORAGE;
2080 decl->ctype.modifiers &= ~MOD_STORAGE;
2081 decl->ctype.modifiers |= MOD_USERTYPE;
2084 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2086 base_type = decl->ctype.base_type;
2088 if (is_typedef) {
2089 if (base_type && !base_type->ident)
2090 base_type->ident = ident;
2091 } else if (base_type && base_type->type == SYM_FN) {
2092 /* K&R argument declaration? */
2093 if (lookup_type(token))
2094 return parse_k_r_arguments(token, decl, list);
2095 if (match_op(token, '{'))
2096 return parse_function_body(token, decl, list);
2098 if (!(decl->ctype.modifiers & MOD_STATIC))
2099 decl->ctype.modifiers |= MOD_EXTERN;
2100 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2101 sparse_error(token->pos, "void declaration");
2104 for (;;) {
2105 if (!is_typedef && match_op(token, '=')) {
2106 if (decl->ctype.modifiers & MOD_EXTERN) {
2107 warning(decl->pos, "symbol with external linkage has initializer");
2108 decl->ctype.modifiers &= ~MOD_EXTERN;
2110 token = initializer(&decl->initializer, token->next);
2112 if (!is_typedef) {
2113 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2114 add_symbol(list, decl);
2115 fn_local_symbol(decl);
2118 check_declaration(decl);
2120 if (!match_op(token, ','))
2121 break;
2123 token = token->next;
2124 ident = NULL;
2125 decl = alloc_symbol(token->pos, SYM_NODE);
2126 decl->ctype = ctype;
2127 token = declaration_specifiers(token, &decl->ctype, 1);
2128 token = declarator(token, decl, &ident);
2129 apply_modifiers(token->pos, &decl->ctype);
2130 if (!ident) {
2131 sparse_error(token->pos, "expected identifier name in type definition");
2132 return token;
2135 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2137 /* Function declarations are automatically extern unless specifically static */
2138 base_type = decl->ctype.base_type;
2139 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2140 if (!(decl->ctype.modifiers & MOD_STATIC))
2141 decl->ctype.modifiers |= MOD_EXTERN;
2144 return expect(token, ';', "at end of declaration");