Add test-suite comment to bad-array-designated-initializer.c
[smatch.git] / parse.c
blobab3a096fefd62905bf4f2608e3fcd89e38be4be0
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 struct symbol *field, *last = NULL;
528 struct token *res;
529 res = struct_declaration_list(token, &sym->symbol_list);
530 FOR_EACH_PTR(sym->symbol_list, field) {
531 if (!field->ident) {
532 struct symbol *base = field->ctype.base_type;
533 if (base && base->type == SYM_BITFIELD)
534 continue;
536 if (last)
537 last->next_subobject = field;
538 last = field;
539 } END_FOR_EACH_PTR(field);
540 return res;
543 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
545 return struct_declaration_list(token, &sym->symbol_list);
548 static struct token *struct_specifier(struct token *token, struct ctype *ctype)
550 return struct_union_enum_specifier(SYM_STRUCT, token, ctype, parse_struct_declaration);
553 static struct token *union_specifier(struct token *token, struct ctype *ctype)
555 return struct_union_enum_specifier(SYM_UNION, token, ctype, parse_union_declaration);
559 typedef struct {
560 int x;
561 unsigned long long y;
562 } Num;
564 static void upper_boundary(Num *n, Num *v)
566 if (n->x > v->x)
567 return;
568 if (n->x < v->x) {
569 *n = *v;
570 return;
572 if (n->y < v->y)
573 n->y = v->y;
576 static void lower_boundary(Num *n, Num *v)
578 if (n->x < v->x)
579 return;
580 if (n->x > v->x) {
581 *n = *v;
582 return;
584 if (n->y > v->y)
585 n->y = v->y;
588 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
590 int shift = type->bit_size;
591 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
593 if (!is_unsigned)
594 shift--;
595 if (upper->x == 0 && upper->y >> shift)
596 return 0;
597 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
598 return 1;
599 return 0;
602 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
604 if (s1->bit_size < s2->bit_size) {
605 s1 = s2;
606 } else if (s1->bit_size == s2->bit_size) {
607 if (s2->ctype.modifiers & MOD_UNSIGNED)
608 s1 = s2;
610 if (s1->bit_size < bits_in_int)
611 return &int_ctype;
612 return s1;
615 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
617 struct symbol *sym;
619 FOR_EACH_PTR(list, sym) {
620 struct expression *expr = sym->initializer;
621 struct symbol *ctype;
622 if (expr->type != EXPR_VALUE)
623 continue;
624 ctype = expr->ctype;
625 if (ctype->bit_size == base_type->bit_size)
626 continue;
627 cast_value(expr, base_type, expr, ctype);
628 } END_FOR_EACH_PTR(sym);
631 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
633 unsigned long long lastval = 0;
634 struct symbol *ctype = NULL, *base_type = NULL;
635 Num upper = {-1, 0}, lower = {1, 0};
636 struct symbol_list *entries = NULL;
638 parent->examined = 1;
639 parent->ctype.base_type = &int_ctype;
640 while (token_type(token) == TOKEN_IDENT) {
641 struct expression *expr = NULL;
642 struct token *next = token->next;
643 struct symbol *sym;
645 sym = alloc_symbol(token->pos, SYM_NODE);
646 bind_symbol(sym, token->ident, NS_SYMBOL);
647 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
649 if (match_op(next, '=')) {
650 next = constant_expression(next->next, &expr);
651 lastval = get_expression_value(expr);
652 ctype = &void_ctype;
653 if (expr && expr->ctype)
654 ctype = expr->ctype;
655 } else if (!ctype) {
656 ctype = &int_ctype;
657 } else if (is_int_type(ctype)) {
658 lastval++;
659 } else {
660 error_die(token->pos, "can't increment the last enum member");
663 if (!expr) {
664 expr = alloc_expression(token->pos, EXPR_VALUE);
665 expr->value = lastval;
666 expr->ctype = ctype;
669 sym->initializer = expr;
670 sym->enum_member = 1;
671 sym->ctype.base_type = parent;
672 add_ptr_list(&entries, sym);
674 if (base_type != &bad_ctype) {
675 if (ctype->type == SYM_NODE)
676 ctype = ctype->ctype.base_type;
677 if (ctype->type == SYM_ENUM) {
678 if (ctype == parent)
679 ctype = base_type;
680 else
681 ctype = ctype->ctype.base_type;
684 * base_type rules:
685 * - if all enums are of the same type, then
686 * the base_type is that type (two first
687 * cases)
688 * - if enums are of different types, they
689 * all have to be integer types, and the
690 * base type is at least "int_ctype".
691 * - otherwise the base_type is "bad_ctype".
693 if (!base_type) {
694 base_type = ctype;
695 } else if (ctype == base_type) {
696 /* nothing */
697 } else if (is_int_type(base_type) && is_int_type(ctype)) {
698 base_type = bigger_enum_type(base_type, ctype);
699 } else
700 base_type = &bad_ctype;
701 parent->ctype.base_type = base_type;
703 if (is_int_type(base_type)) {
704 Num v = {.y = lastval};
705 if (ctype->ctype.modifiers & MOD_UNSIGNED)
706 v.x = 0;
707 else if ((long long)lastval >= 0)
708 v.x = 0;
709 else
710 v.x = -1;
711 upper_boundary(&upper, &v);
712 lower_boundary(&lower, &v);
714 token = next;
715 if (!match_op(token, ','))
716 break;
717 token = token->next;
719 if (!base_type) {
720 sparse_error(token->pos, "bad enum definition");
721 base_type = &bad_ctype;
723 else if (!is_int_type(base_type))
724 base_type = base_type;
725 else if (type_is_ok(base_type, &upper, &lower))
726 base_type = base_type;
727 else if (type_is_ok(&int_ctype, &upper, &lower))
728 base_type = &int_ctype;
729 else if (type_is_ok(&uint_ctype, &upper, &lower))
730 base_type = &uint_ctype;
731 else if (type_is_ok(&long_ctype, &upper, &lower))
732 base_type = &long_ctype;
733 else if (type_is_ok(&ulong_ctype, &upper, &lower))
734 base_type = &ulong_ctype;
735 else if (type_is_ok(&llong_ctype, &upper, &lower))
736 base_type = &llong_ctype;
737 else if (type_is_ok(&ullong_ctype, &upper, &lower))
738 base_type = &ullong_ctype;
739 else
740 base_type = &bad_ctype;
741 parent->ctype.base_type = base_type;
742 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
743 parent->examined = 0;
745 cast_enum_list(entries, base_type);
746 free_ptr_list(&entries);
748 return token;
751 static struct token *enum_specifier(struct token *token, struct ctype *ctype)
753 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctype, parse_enum_declaration);
755 ctype = &ctype->base_type->ctype;
756 if (!ctype->base_type)
757 ctype->base_type = &incomplete_ctype;
759 return ret;
762 static struct token *typeof_specifier(struct token *token, struct ctype *ctype)
764 struct symbol *sym;
766 if (!match_op(token, '(')) {
767 sparse_error(token->pos, "expected '(' after typeof");
768 return token;
770 if (lookup_type(token->next)) {
771 token = typename(token->next, &sym);
772 *ctype = sym->ctype;
773 } else {
774 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
775 token = parse_expression(token->next, &typeof_sym->initializer);
777 ctype->modifiers = 0;
778 ctype->base_type = typeof_sym;
780 return expect(token, ')', "after typeof");
783 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct ctype *ctype)
785 struct expression *expr = NULL;
786 if (match_op(token, '('))
787 token = parens_expression(token, &expr, "in attribute");
788 return token;
791 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct ctype *ctype)
793 ctype->alignment = 1;
794 return token;
797 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct ctype *ctype)
799 int alignment = max_alignment;
800 struct expression *expr = NULL;
802 if (match_op(token, '(')) {
803 token = parens_expression(token, &expr, "in attribute");
804 if (expr)
805 alignment = const_expression_value(expr);
807 ctype->alignment = alignment;
808 return token;
811 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct ctype *ctype)
813 ctype->modifiers |= attr->ctype.modifiers;
814 return token;
817 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct ctype *ctype)
819 struct expression *expr = NULL;
820 token = expect(token, '(', "after address_space attribute");
821 token = conditional_expression(token, &expr);
822 if (expr)
823 ctype->as = const_expression_value(expr);
824 token = expect(token, ')', "after address_space attribute");
825 return token;
828 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct ctype *ctype)
830 token = expect(token, '(', "after mode attribute");
831 if (token_type(token) == TOKEN_IDENT) {
832 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
833 if (mode && mode->op->type == KW_MODE)
834 ctype->modifiers |= mode->ctype.modifiers;
835 else
836 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
837 token = token->next;
838 } else
839 sparse_error(token->pos, "expect attribute mode symbol\n");
840 token = expect(token, ')', "after mode attribute");
841 return token;
844 static struct token *attribute_context(struct token *token, struct symbol *attr, struct ctype *ctype)
846 struct context *context = alloc_context();
847 struct expression *args[3];
848 int argc = 0;
850 token = expect(token, '(', "after context attribute");
851 while (!match_op(token, ')')) {
852 struct expression *expr = NULL;
853 token = conditional_expression(token, &expr);
854 if (!expr)
855 break;
856 if (argc < 3)
857 args[argc++] = expr;
858 if (!match_op(token, ','))
859 break;
860 token = token->next;
863 switch(argc) {
864 case 0:
865 sparse_error(token->pos, "expected context input/output values");
866 break;
867 case 1:
868 context->in = get_expression_value(args[0]);
869 break;
870 case 2:
871 context->in = get_expression_value(args[0]);
872 context->out = get_expression_value(args[1]);
873 break;
874 case 3:
875 context->context = args[0];
876 context->in = get_expression_value(args[1]);
877 context->out = get_expression_value(args[2]);
878 break;
881 if (argc)
882 add_ptr_list(&ctype->contexts, context);
884 token = expect(token, ')', "after context attribute");
885 return token;
888 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct ctype *ctype)
890 if (Wtransparent_union)
891 sparse_error(token->pos, "ignoring attribute __transparent_union__");
892 return token;
895 static struct token *recover_unknown_attribute(struct token *token)
897 struct expression *expr = NULL;
899 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
900 token = token->next;
901 if (match_op(token, '('))
902 token = parens_expression(token, &expr, "in attribute");
903 return token;
906 static struct token *attribute_specifier(struct token *token, struct ctype *ctype)
908 ctype->modifiers = 0;
909 token = expect(token, '(', "after attribute");
910 token = expect(token, '(', "after attribute");
912 for (;;) {
913 struct ident *attribute_name;
914 struct symbol *attr;
916 if (eof_token(token))
917 break;
918 if (match_op(token, ';'))
919 break;
920 if (token_type(token) != TOKEN_IDENT)
921 break;
922 attribute_name = token->ident;
923 attr = lookup_keyword(attribute_name, NS_KEYWORD);
924 if (attr && attr->op->attribute)
925 token = attr->op->attribute(token->next, attr, ctype);
926 else
927 token = recover_unknown_attribute(token);
929 if (!match_op(token, ','))
930 break;
931 token = token->next;
934 token = expect(token, ')', "after attribute");
935 token = expect(token, ')', "after attribute");
936 return token;
939 struct symbol * ctype_integer(unsigned long spec)
941 static struct symbol *const integer_ctypes[][3] = {
942 { &llong_ctype, &sllong_ctype, &ullong_ctype },
943 { &long_ctype, &slong_ctype, &ulong_ctype },
944 { &short_ctype, &sshort_ctype, &ushort_ctype },
945 { &char_ctype, &schar_ctype, &uchar_ctype },
946 { &int_ctype, &sint_ctype, &uint_ctype },
948 struct symbol *const (*ctype)[3];
949 int sub;
951 ctype = integer_ctypes;
952 if (!(spec & MOD_LONGLONG)) {
953 ctype++;
954 if (!(spec & MOD_LONG)) {
955 ctype++;
956 if (!(spec & MOD_SHORT)) {
957 ctype++;
958 if (!(spec & MOD_CHAR))
959 ctype++;
964 sub = ((spec & MOD_UNSIGNED)
966 : ((spec & MOD_EXPLICITLY_SIGNED)
968 : 0));
970 return ctype[0][sub];
973 struct symbol * ctype_fp(unsigned long spec)
975 if (spec & MOD_LONGLONG)
976 return &ldouble_ctype;
977 if (spec & MOD_LONG)
978 return &double_ctype;
979 return &float_ctype;
982 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
984 unsigned long mod = thistype->modifiers;
986 if (mod) {
987 unsigned long old = ctype->modifiers;
988 unsigned long extra = 0, dup, conflict;
990 if (mod & old & MOD_LONG) {
991 extra = MOD_LONGLONG | MOD_LONG;
992 mod &= ~MOD_LONG;
993 old &= ~MOD_LONG;
995 dup = (mod & old) | (extra & old) | (extra & mod);
996 if (dup)
997 sparse_error(pos, "Just how %sdo you want this type to be?",
998 modifier_string(dup));
1000 conflict = !(~mod & ~old & (MOD_LONG | MOD_SHORT));
1001 if (conflict)
1002 sparse_error(pos, "You cannot have both long and short modifiers.");
1004 conflict = !(~mod & ~old & (MOD_SIGNED | MOD_UNSIGNED));
1005 if (conflict)
1006 sparse_error(pos, "You cannot have both signed and unsigned modifiers.");
1008 // Only one storage modifier allowed, except that "inline" doesn't count.
1009 conflict = (mod | old) & (MOD_STORAGE & ~MOD_INLINE);
1010 conflict &= (conflict - 1);
1011 if (conflict)
1012 sparse_error(pos, "multiple storage classes");
1014 ctype->modifiers = old | mod | extra;
1017 /* Context */
1018 concat_ptr_list((struct ptr_list *)thistype->contexts,
1019 (struct ptr_list **)&ctype->contexts);
1021 /* Alignment */
1022 if (thistype->alignment & (thistype->alignment-1)) {
1023 warning(pos, "I don't like non-power-of-2 alignments");
1024 thistype->alignment = 0;
1026 if (thistype->alignment > ctype->alignment)
1027 ctype->alignment = thistype->alignment;
1029 /* Address space */
1030 if (thistype->as)
1031 ctype->as = thistype->as;
1034 static void check_modifiers(struct position *pos, struct symbol *s, unsigned long mod)
1036 unsigned long banned, wrong;
1037 const unsigned long BANNED_SIZE = MOD_LONG | MOD_LONGLONG | MOD_SHORT;
1038 const unsigned long BANNED_SIGN = MOD_SIGNED | MOD_UNSIGNED;
1040 if (s->type == SYM_KEYWORD)
1041 banned = s->op->type == KW_SPECIFIER ? (BANNED_SIZE | BANNED_SIGN) : 0;
1042 else if (s->ctype.base_type == &fp_type)
1043 banned = BANNED_SIGN;
1044 else if (s->ctype.base_type == &int_type || !s->ctype.base_type || is_int_type (s))
1045 banned = 0;
1046 else {
1047 // label_type
1048 // void_type
1049 // bad_type
1050 // vector_type <-- whatever that is
1051 banned = BANNED_SIZE | BANNED_SIGN;
1054 wrong = mod & banned;
1055 if (wrong)
1056 sparse_error(*pos, "modifier %sis invalid in this context",
1057 modifier_string (wrong));
1060 static struct token *declaration_specifiers(struct token *next, struct ctype *ctype, int qual)
1062 struct token *token;
1064 while ( (token = next) != NULL ) {
1065 struct ctype thistype;
1066 struct ident *ident;
1067 struct symbol *s, *type;
1068 unsigned long mod;
1070 next = token->next;
1071 if (token_type(token) != TOKEN_IDENT)
1072 break;
1073 ident = token->ident;
1075 s = lookup_symbol(ident, NS_TYPEDEF);
1076 if (!s)
1077 break;
1078 thistype = s->ctype;
1079 mod = thistype.modifiers;
1080 if (qual) {
1081 if (s->type != SYM_KEYWORD)
1082 break;
1083 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1084 break;
1086 if (s->type == SYM_KEYWORD && s->op->declarator) {
1087 next = s->op->declarator(next, &thistype);
1088 mod = thistype.modifiers;
1090 type = thistype.base_type;
1091 if (type) {
1092 if (qual)
1093 break;
1094 if (ctype->base_type)
1095 break;
1096 /* User types only mix with qualifiers */
1097 if (mod & MOD_USERTYPE) {
1098 if (ctype->modifiers & MOD_SPECIFIER)
1099 break;
1101 ctype->base_type = type;
1104 check_modifiers(&token->pos, s, ctype->modifiers);
1105 apply_ctype(token->pos, &thistype, ctype);
1108 if (!ctype->base_type) {
1109 struct symbol *base = &incomplete_ctype;
1112 * If we have modifiers, we'll default to an integer
1113 * type, and "ctype_integer()" will turn this into
1114 * a specific one.
1116 if (ctype->modifiers & MOD_SPECIFIER)
1117 base = &int_type;
1118 ctype->base_type = base;
1120 return token;
1123 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1125 struct expression *expr = NULL;
1127 token = parse_expression(token, &expr);
1128 sym->array_size = expr;
1129 return token;
1132 static struct token *parameter_type_list(struct token *, struct symbol *, struct ident **p);
1133 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p);
1135 static struct token *handle_attributes(struct token *token, struct ctype *ctype, unsigned int keywords)
1137 struct symbol *keyword;
1138 for (;;) {
1139 struct ctype thistype = { 0, };
1140 if (token_type(token) != TOKEN_IDENT)
1141 break;
1142 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1143 if (!keyword || keyword->type != SYM_KEYWORD)
1144 break;
1145 if (!(keyword->op->type & keywords))
1146 break;
1147 token = keyword->op->declarator(token->next, &thistype);
1148 apply_ctype(token->pos, &thistype, ctype);
1150 return token;
1153 static struct token *direct_declarator(struct token *token, struct symbol *decl, struct ident **p)
1155 struct ctype *ctype = &decl->ctype;
1157 if (p && token_type(token) == TOKEN_IDENT) {
1158 *p = token->ident;
1159 token = token->next;
1162 for (;;) {
1163 token = handle_attributes(token, ctype, KW_ATTRIBUTE | KW_ASM);
1165 if (token_type(token) != TOKEN_SPECIAL)
1166 return token;
1169 * This can be either a parameter list or a grouping.
1170 * For the direct (non-abstract) case, we know if must be
1171 * a parameter list if we already saw the identifier.
1172 * For the abstract case, we know if must be a parameter
1173 * list if it is empty or starts with a type.
1175 if (token->special == '(') {
1176 struct symbol *sym;
1177 struct token *next = token->next;
1178 int fn;
1180 next = handle_attributes(next, ctype, KW_ATTRIBUTE);
1181 fn = (p && *p) || match_op(next, ')') || lookup_type(next);
1183 if (!fn) {
1184 struct symbol *base_type = ctype->base_type;
1185 token = declarator(next, decl, p);
1186 token = expect(token, ')', "in nested declarator");
1187 while (ctype->base_type != base_type)
1188 ctype = &ctype->base_type->ctype;
1189 p = NULL;
1190 continue;
1193 sym = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1194 token = parameter_type_list(next, sym, p);
1195 token = expect(token, ')', "in function declarator");
1196 continue;
1198 if (token->special == '[') {
1199 struct symbol *array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1200 token = abstract_array_declarator(token->next, array);
1201 token = expect(token, ']', "in abstract_array_declarator");
1202 ctype = &array->ctype;
1203 continue;
1205 break;
1207 return token;
1210 static struct token *pointer(struct token *token, struct ctype *ctype)
1212 unsigned long modifiers;
1213 struct symbol *base_type;
1215 modifiers = ctype->modifiers & ~MOD_TYPEDEF;
1216 base_type = ctype->base_type;
1217 ctype->modifiers = modifiers;
1219 while (match_op(token,'*')) {
1220 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1221 ptr->ctype.modifiers = modifiers & ~MOD_STORAGE;
1222 ptr->ctype.as = ctype->as;
1223 concat_ptr_list((struct ptr_list *)ctype->contexts,
1224 (struct ptr_list **)&ptr->ctype.contexts);
1225 ptr->ctype.base_type = base_type;
1227 base_type = ptr;
1228 ctype->modifiers = modifiers & MOD_STORAGE;
1229 ctype->base_type = base_type;
1230 ctype->as = 0;
1231 free_ptr_list(&ctype->contexts);
1233 token = declaration_specifiers(token->next, ctype, 1);
1234 modifiers = ctype->modifiers;
1236 return token;
1239 static struct token *declarator(struct token *token, struct symbol *sym, struct ident **p)
1241 token = pointer(token, &sym->ctype);
1242 return direct_declarator(token, sym, p);
1245 static struct token *handle_bitfield(struct token *token, struct symbol *decl)
1247 struct ctype *ctype = &decl->ctype;
1248 struct expression *expr;
1249 struct symbol *bitfield;
1250 long long width;
1252 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1253 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1254 show_typename(ctype->base_type));
1255 // Parse this to recover gracefully.
1256 return conditional_expression(token->next, &expr);
1259 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1260 token = conditional_expression(token->next, &expr);
1261 width = const_expression_value(expr);
1262 bitfield->bit_size = width;
1264 if (width < 0 || width > INT_MAX) {
1265 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1266 width = -1;
1267 } else if (decl->ident && width == 0) {
1268 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1269 show_ident(decl->ident));
1270 width = -1;
1271 } else if (decl->ident) {
1272 struct symbol *base_type = bitfield->ctype.base_type;
1273 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1274 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1275 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1276 // Valid values are either {-1;0} or {0}, depending on integer
1277 // representation. The latter makes for very efficient code...
1278 sparse_error(token->pos, "dubious one-bit signed bitfield");
1280 if (Wdefault_bitfield_sign &&
1281 bitfield_type->type != SYM_ENUM &&
1282 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1283 is_signed) {
1284 // The sign of bitfields is unspecified by default.
1285 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1288 bitfield->bit_size = width;
1289 return token;
1292 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1294 struct ctype ctype = {0, };
1296 token = declaration_specifiers(token, &ctype, 0);
1297 for (;;) {
1298 struct ident *ident = NULL;
1299 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1300 decl->ctype = ctype;
1301 token = declarator(token, decl, &ident);
1302 decl->ident = ident;
1303 if (match_op(token, ':')) {
1304 token = handle_bitfield(token, decl);
1305 token = handle_attributes(token, &decl->ctype, KW_ATTRIBUTE | KW_ASM);
1307 apply_modifiers(token->pos, &decl->ctype);
1308 add_symbol(list, decl);
1309 if (!match_op(token, ','))
1310 break;
1311 token = token->next;
1313 return token;
1316 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1318 while (!match_op(token, '}')) {
1319 if (!match_op(token, ';'))
1320 token = declaration_list(token, list);
1321 if (!match_op(token, ';')) {
1322 sparse_error(token->pos, "expected ; at end of declaration");
1323 break;
1325 token = token->next;
1327 return token;
1330 static struct token *parameter_declaration(struct token *token, struct symbol **tree)
1332 struct ident *ident = NULL;
1333 struct symbol *sym;
1334 struct ctype ctype = { 0, };
1336 token = declaration_specifiers(token, &ctype, 0);
1337 sym = alloc_symbol(token->pos, SYM_NODE);
1338 sym->ctype = ctype;
1339 *tree = sym;
1340 token = declarator(token, sym, &ident);
1341 sym->ident = ident;
1342 apply_modifiers(token->pos, &sym->ctype);
1343 return token;
1346 struct token *typename(struct token *token, struct symbol **p)
1348 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1349 *p = sym;
1350 token = declaration_specifiers(token, &sym->ctype, 0);
1351 token = declarator(token, sym, NULL);
1352 apply_modifiers(token->pos, &sym->ctype);
1353 return token;
1356 static struct token *expression_statement(struct token *token, struct expression **tree)
1358 token = parse_expression(token, tree);
1359 return expect(token, ';', "at end of statement");
1362 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1363 struct expression_list **inout)
1365 struct expression *expr;
1367 /* Allow empty operands */
1368 if (match_op(token->next, ':') || match_op(token->next, ')'))
1369 return token->next;
1370 do {
1371 struct ident *ident = NULL;
1372 if (match_op(token->next, '[') &&
1373 token_type(token->next->next) == TOKEN_IDENT &&
1374 match_op(token->next->next->next, ']')) {
1375 ident = token->next->next->ident;
1376 token = token->next->next->next;
1378 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1379 token = primary_expression(token->next, &expr);
1380 add_expression(inout, expr);
1381 token = parens_expression(token, &expr, "in asm parameter");
1382 add_expression(inout, expr);
1383 } while (match_op(token, ','));
1384 return token;
1387 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1388 struct expression_list **clobbers)
1390 struct expression *expr;
1392 do {
1393 token = primary_expression(token->next, &expr);
1394 add_expression(clobbers, expr);
1395 } while (match_op(token, ','));
1396 return token;
1399 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1401 token = token->next;
1402 stmt->type = STMT_ASM;
1403 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1404 token = token->next;
1406 token = expect(token, '(', "after asm");
1407 token = parse_expression(token, &stmt->asm_string);
1408 if (match_op(token, ':'))
1409 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1410 if (match_op(token, ':'))
1411 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1412 if (match_op(token, ':'))
1413 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1414 token = expect(token, ')', "after asm");
1415 return expect(token, ';', "at end of asm-statement");
1418 static struct token *parse_asm_declarator(struct token *token, struct ctype *ctype)
1420 struct expression *expr;
1421 token = expect(token, '(', "after asm");
1422 token = parse_expression(token->next, &expr);
1423 token = expect(token, ')', "after asm");
1424 return token;
1427 /* Make a statement out of an expression */
1428 static struct statement *make_statement(struct expression *expr)
1430 struct statement *stmt;
1432 if (!expr)
1433 return NULL;
1434 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1435 stmt->expression = expr;
1436 return stmt;
1440 * All iterators have two symbols associated with them:
1441 * the "continue" and "break" symbols, which are targets
1442 * for continue and break statements respectively.
1444 * They are in a special name-space, but they follow
1445 * all the normal visibility rules, so nested iterators
1446 * automatically work right.
1448 static void start_iterator(struct statement *stmt)
1450 struct symbol *cont, *brk;
1452 start_symbol_scope();
1453 cont = alloc_symbol(stmt->pos, SYM_NODE);
1454 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1455 brk = alloc_symbol(stmt->pos, SYM_NODE);
1456 bind_symbol(brk, &break_ident, NS_ITERATOR);
1458 stmt->type = STMT_ITERATOR;
1459 stmt->iterator_break = brk;
1460 stmt->iterator_continue = cont;
1461 fn_local_symbol(brk);
1462 fn_local_symbol(cont);
1465 static void end_iterator(struct statement *stmt)
1467 end_symbol_scope();
1470 static struct statement *start_function(struct symbol *sym)
1472 struct symbol *ret;
1473 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1475 start_function_scope();
1476 ret = alloc_symbol(sym->pos, SYM_NODE);
1477 ret->ctype = sym->ctype.base_type->ctype;
1478 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1479 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1480 bind_symbol(ret, &return_ident, NS_ITERATOR);
1481 stmt->ret = ret;
1482 fn_local_symbol(ret);
1484 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1485 current_fn = sym;
1487 return stmt;
1490 static void end_function(struct symbol *sym)
1492 current_fn = NULL;
1493 end_function_scope();
1497 * A "switch()" statement, like an iterator, has a
1498 * the "break" symbol associated with it. It works
1499 * exactly like the iterator break - it's the target
1500 * for any break-statements in scope, and means that
1501 * "break" handling doesn't even need to know whether
1502 * it's breaking out of an iterator or a switch.
1504 * In addition, the "case" symbol is a marker for the
1505 * case/default statements to find the switch statement
1506 * that they are associated with.
1508 static void start_switch(struct statement *stmt)
1510 struct symbol *brk, *switch_case;
1512 start_symbol_scope();
1513 brk = alloc_symbol(stmt->pos, SYM_NODE);
1514 bind_symbol(brk, &break_ident, NS_ITERATOR);
1516 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
1517 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
1518 switch_case->stmt = stmt;
1520 stmt->type = STMT_SWITCH;
1521 stmt->switch_break = brk;
1522 stmt->switch_case = switch_case;
1524 fn_local_symbol(brk);
1525 fn_local_symbol(switch_case);
1528 static void end_switch(struct statement *stmt)
1530 if (!stmt->switch_case->symbol_list)
1531 warning(stmt->pos, "switch with no cases");
1532 end_symbol_scope();
1535 static void add_case_statement(struct statement *stmt)
1537 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
1538 struct symbol *sym;
1540 if (!target) {
1541 sparse_error(stmt->pos, "not in switch scope");
1542 stmt->type = STMT_NONE;
1543 return;
1545 sym = alloc_symbol(stmt->pos, SYM_NODE);
1546 add_symbol(&target->symbol_list, sym);
1547 sym->stmt = stmt;
1548 stmt->case_label = sym;
1549 fn_local_symbol(sym);
1552 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
1554 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
1556 if (!target)
1557 error_die(token->pos, "internal error: return without a function target");
1558 stmt->type = STMT_RETURN;
1559 stmt->ret_target = target;
1560 return expression_statement(token->next, &stmt->ret_value);
1563 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
1565 struct symbol_list *syms;
1566 struct expression *e1, *e2, *e3;
1567 struct statement *iterator;
1569 start_iterator(stmt);
1570 token = expect(token->next, '(', "after 'for'");
1572 syms = NULL;
1573 e1 = NULL;
1574 /* C99 variable declaration? */
1575 if (lookup_type(token)) {
1576 token = external_declaration(token, &syms);
1577 } else {
1578 token = parse_expression(token, &e1);
1579 token = expect(token, ';', "in 'for'");
1581 token = parse_expression(token, &e2);
1582 token = expect(token, ';', "in 'for'");
1583 token = parse_expression(token, &e3);
1584 token = expect(token, ')', "in 'for'");
1585 token = statement(token, &iterator);
1587 stmt->iterator_syms = syms;
1588 stmt->iterator_pre_statement = make_statement(e1);
1589 stmt->iterator_pre_condition = e2;
1590 stmt->iterator_post_statement = make_statement(e3);
1591 stmt->iterator_post_condition = NULL;
1592 stmt->iterator_statement = iterator;
1593 end_iterator(stmt);
1595 return token;
1598 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
1600 struct expression *expr;
1601 struct statement *iterator;
1603 start_iterator(stmt);
1604 token = parens_expression(token->next, &expr, "after 'while'");
1605 token = statement(token, &iterator);
1607 stmt->iterator_pre_condition = expr;
1608 stmt->iterator_post_condition = NULL;
1609 stmt->iterator_statement = iterator;
1610 end_iterator(stmt);
1612 return token;
1615 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
1617 struct expression *expr;
1618 struct statement *iterator;
1620 start_iterator(stmt);
1621 token = statement(token->next, &iterator);
1622 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
1623 token = token->next;
1624 else
1625 sparse_error(token->pos, "expected 'while' after 'do'");
1626 token = parens_expression(token, &expr, "after 'do-while'");
1628 stmt->iterator_post_condition = expr;
1629 stmt->iterator_statement = iterator;
1630 end_iterator(stmt);
1632 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
1633 warning(iterator->pos, "do-while statement is not a compound statement");
1635 return expect(token, ';', "after statement");
1638 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
1640 stmt->type = STMT_IF;
1641 token = parens_expression(token->next, &stmt->if_conditional, "after if");
1642 token = statement(token, &stmt->if_true);
1643 if (token_type(token) != TOKEN_IDENT)
1644 return token;
1645 if (token->ident != &else_ident)
1646 return token;
1647 return statement(token->next, &stmt->if_false);
1650 static inline struct token *case_statement(struct token *token, struct statement *stmt)
1652 stmt->type = STMT_CASE;
1653 token = expect(token, ':', "after default/case");
1654 add_case_statement(stmt);
1655 return statement(token, &stmt->case_statement);
1658 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
1660 token = parse_expression(token->next, &stmt->case_expression);
1661 if (match_op(token, SPECIAL_ELLIPSIS))
1662 token = parse_expression(token->next, &stmt->case_to);
1663 return case_statement(token, stmt);
1666 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
1668 return case_statement(token->next, stmt);
1671 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
1673 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
1674 stmt->type = STMT_GOTO;
1675 stmt->goto_label = target;
1676 if (!target)
1677 sparse_error(stmt->pos, "break/continue not in iterator scope");
1678 return expect(token->next, ';', "at end of statement");
1681 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
1683 stmt->type = STMT_SWITCH;
1684 start_switch(stmt);
1685 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
1686 token = statement(token, &stmt->switch_statement);
1687 end_switch(stmt);
1688 return token;
1691 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
1693 stmt->type = STMT_GOTO;
1694 token = token->next;
1695 if (match_op(token, '*')) {
1696 token = parse_expression(token->next, &stmt->goto_expression);
1697 add_statement(&function_computed_goto_list, stmt);
1698 } else if (token_type(token) == TOKEN_IDENT) {
1699 stmt->goto_label = label_symbol(token);
1700 token = token->next;
1701 } else {
1702 sparse_error(token->pos, "Expected identifier or goto expression");
1704 return expect(token, ';', "at end of statement");
1707 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
1709 stmt->type = STMT_CONTEXT;
1710 token = parse_expression(token->next, &stmt->expression);
1711 if(stmt->expression->type == EXPR_PREOP
1712 && stmt->expression->op == '('
1713 && stmt->expression->unop->type == EXPR_COMMA) {
1714 struct expression *expr;
1715 expr = stmt->expression->unop;
1716 stmt->context = expr->left;
1717 stmt->expression = expr->right;
1719 return expect(token, ';', "at end of statement");
1722 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
1724 stmt->type = STMT_RANGE;
1725 token = assignment_expression(token->next, &stmt->range_expression);
1726 token = expect(token, ',', "after range expression");
1727 token = assignment_expression(token, &stmt->range_low);
1728 token = expect(token, ',', "after low range");
1729 token = assignment_expression(token, &stmt->range_high);
1730 return expect(token, ';', "after range statement");
1733 static struct token *statement(struct token *token, struct statement **tree)
1735 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
1737 *tree = stmt;
1738 if (token_type(token) == TOKEN_IDENT) {
1739 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
1740 if (s && s->op->statement)
1741 return s->op->statement(token, stmt);
1743 if (match_op(token->next, ':')) {
1744 stmt->type = STMT_LABEL;
1745 stmt->label_identifier = label_symbol(token);
1746 token = handle_attributes(token->next->next, &stmt->label_identifier->ctype, KW_ATTRIBUTE);
1747 return statement(token, &stmt->label_statement);
1751 if (match_op(token, '{')) {
1752 stmt->type = STMT_COMPOUND;
1753 start_symbol_scope();
1754 token = compound_statement(token->next, stmt);
1755 end_symbol_scope();
1757 return expect(token, '}', "at end of compound statement");
1760 stmt->type = STMT_EXPRESSION;
1761 return expression_statement(token, &stmt->expression);
1764 static struct token * statement_list(struct token *token, struct statement_list **list)
1766 int seen_statement = 0;
1767 for (;;) {
1768 struct statement * stmt;
1769 if (eof_token(token))
1770 break;
1771 if (match_op(token, '}'))
1772 break;
1773 if (lookup_type(token)) {
1774 if (seen_statement) {
1775 warning(token->pos, "mixing declarations and code");
1776 seen_statement = 0;
1778 stmt = alloc_statement(token->pos, STMT_DECLARATION);
1779 token = external_declaration(token, &stmt->declaration);
1780 } else {
1781 seen_statement = warn_on_mixed;
1782 token = statement(token, &stmt);
1784 add_statement(list, stmt);
1786 return token;
1789 static struct token *parameter_type_list(struct token *token, struct symbol *fn, struct ident **p)
1791 struct symbol_list **list = &fn->arguments;
1793 if (match_op(token, ')')) {
1794 // No warning for "void oink ();"
1795 // Bug or feature: warns for "void oink () __attribute__ ((noreturn));"
1796 if (p && !match_op(token->next, ';'))
1797 warning(token->pos, "non-ANSI function declaration of function '%s'", show_ident(*p));
1798 return token;
1801 for (;;) {
1802 struct symbol *sym;
1804 if (match_op(token, SPECIAL_ELLIPSIS)) {
1805 if (!*list)
1806 warning(token->pos, "variadic functions must have one named argument");
1807 fn->variadic = 1;
1808 token = token->next;
1809 break;
1812 sym = alloc_symbol(token->pos, SYM_NODE);
1813 token = parameter_declaration(token, &sym);
1814 if (sym->ctype.base_type == &void_ctype) {
1815 /* Special case: (void) */
1816 if (!*list && !sym->ident)
1817 break;
1818 warning(token->pos, "void parameter");
1820 add_symbol(list, sym);
1821 if (!match_op(token, ','))
1822 break;
1823 token = token->next;
1826 return token;
1829 struct token *compound_statement(struct token *token, struct statement *stmt)
1831 token = statement_list(token, &stmt->stmts);
1832 return token;
1835 static struct expression *identifier_expression(struct token *token)
1837 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
1838 expr->expr_ident = token->ident;
1839 return expr;
1842 static struct expression *index_expression(struct expression *from, struct expression *to)
1844 int idx_from, idx_to;
1845 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
1847 idx_from = const_expression_value(from);
1848 idx_to = idx_from;
1849 if (to) {
1850 idx_to = const_expression_value(to);
1851 if (idx_to < idx_from || idx_from < 0)
1852 warning(from->pos, "nonsense array initializer index range");
1854 expr->idx_from = idx_from;
1855 expr->idx_to = idx_to;
1856 return expr;
1859 static struct token *single_initializer(struct expression **ep, struct token *token)
1861 int expect_equal = 0;
1862 struct token *next = token->next;
1863 struct expression **tail = ep;
1864 int nested;
1866 *ep = NULL;
1868 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
1869 struct expression *expr = identifier_expression(token);
1870 if (Wold_initializer)
1871 warning(token->pos, "obsolete struct initializer, use C99 syntax");
1872 token = initializer(&expr->ident_expression, next->next);
1873 if (expr->ident_expression)
1874 *ep = expr;
1875 return token;
1878 for (tail = ep, nested = 0; ; nested++, next = token->next) {
1879 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
1880 struct expression *expr = identifier_expression(next);
1881 *tail = expr;
1882 tail = &expr->ident_expression;
1883 expect_equal = 1;
1884 token = next->next;
1885 } else if (match_op(token, '[')) {
1886 struct expression *from = NULL, *to = NULL, *expr;
1887 token = constant_expression(token->next, &from);
1888 if (!from) {
1889 sparse_error(token->pos, "Expected constant expression");
1890 break;
1892 if (match_op(token, SPECIAL_ELLIPSIS))
1893 token = constant_expression(token->next, &to);
1894 expr = index_expression(from, to);
1895 *tail = expr;
1896 tail = &expr->idx_expression;
1897 token = expect(token, ']', "at end of initializer index");
1898 if (nested)
1899 expect_equal = 1;
1900 } else {
1901 break;
1904 if (nested && !expect_equal) {
1905 if (!match_op(token, '='))
1906 warning(token->pos, "obsolete array initializer, use C99 syntax");
1907 else
1908 expect_equal = 1;
1910 if (expect_equal)
1911 token = expect(token, '=', "at end of initializer index");
1913 token = initializer(tail, token);
1914 if (!*tail)
1915 *ep = NULL;
1916 return token;
1919 static struct token *initializer_list(struct expression_list **list, struct token *token)
1921 struct expression *expr;
1923 for (;;) {
1924 token = single_initializer(&expr, token);
1925 if (!expr)
1926 break;
1927 add_expression(list, expr);
1928 if (!match_op(token, ','))
1929 break;
1930 token = token->next;
1932 return token;
1935 struct token *initializer(struct expression **tree, struct token *token)
1937 if (match_op(token, '{')) {
1938 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
1939 *tree = expr;
1940 token = initializer_list(&expr->expr_list, token->next);
1941 return expect(token, '}', "at end of initializer");
1943 return assignment_expression(token, tree);
1946 static void declare_argument(struct symbol *sym, struct symbol *fn)
1948 if (!sym->ident) {
1949 sparse_error(sym->pos, "no identifier for function argument");
1950 return;
1952 bind_symbol(sym, sym->ident, NS_SYMBOL);
1955 static struct token *parse_function_body(struct token *token, struct symbol *decl,
1956 struct symbol_list **list)
1958 struct symbol_list **old_symbol_list;
1959 struct symbol *base_type = decl->ctype.base_type;
1960 struct statement *stmt, **p;
1961 struct symbol *arg;
1963 old_symbol_list = function_symbol_list;
1964 if (decl->ctype.modifiers & MOD_INLINE) {
1965 function_symbol_list = &decl->inline_symbol_list;
1966 p = &base_type->inline_stmt;
1967 } else {
1968 function_symbol_list = &decl->symbol_list;
1969 p = &base_type->stmt;
1971 function_computed_target_list = NULL;
1972 function_computed_goto_list = NULL;
1974 if (decl->ctype.modifiers & MOD_EXTERN) {
1975 if (!(decl->ctype.modifiers & MOD_INLINE))
1976 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
1978 if (!(decl->ctype.modifiers & MOD_STATIC))
1979 decl->ctype.modifiers |= MOD_EXTERN;
1981 stmt = start_function(decl);
1983 *p = stmt;
1984 FOR_EACH_PTR (base_type->arguments, arg) {
1985 declare_argument(arg, base_type);
1986 } END_FOR_EACH_PTR(arg);
1988 token = compound_statement(token->next, stmt);
1990 end_function(decl);
1991 if (!(decl->ctype.modifiers & MOD_INLINE))
1992 add_symbol(list, decl);
1993 check_declaration(decl);
1994 function_symbol_list = old_symbol_list;
1995 if (function_computed_goto_list) {
1996 if (!function_computed_target_list)
1997 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
1998 else {
1999 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2000 stmt->target_list = function_computed_target_list;
2001 } END_FOR_EACH_PTR(stmt);
2004 return expect(token, '}', "at end of function");
2007 static void promote_k_r_types(struct symbol *arg)
2009 struct symbol *base = arg->ctype.base_type;
2010 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2011 arg->ctype.base_type = &int_ctype;
2015 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2017 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2018 struct symbol *arg;
2020 FOR_EACH_PTR(real_args, arg) {
2021 struct symbol *type;
2023 /* This is quadratic in the number of arguments. We _really_ don't care */
2024 FOR_EACH_PTR(argtypes, type) {
2025 if (type->ident == arg->ident)
2026 goto match;
2027 } END_FOR_EACH_PTR(type);
2028 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2029 continue;
2030 match:
2031 type->used = 1;
2032 /* "char" and "short" promote to "int" */
2033 promote_k_r_types(type);
2035 arg->ctype = type->ctype;
2036 } END_FOR_EACH_PTR(arg);
2038 FOR_EACH_PTR(argtypes, arg) {
2039 if (!arg->used)
2040 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2041 } END_FOR_EACH_PTR(arg);
2045 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2046 struct symbol_list **list)
2048 struct symbol_list *args = NULL;
2050 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2051 do {
2052 token = declaration_list(token, &args);
2053 if (!match_op(token, ';')) {
2054 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2055 break;
2057 token = token->next;
2058 } while (lookup_type(token));
2060 apply_k_r_types(args, decl);
2062 if (!match_op(token, '{')) {
2063 sparse_error(token->pos, "expected function body");
2064 return token;
2066 return parse_function_body(token, decl, list);
2069 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2071 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2072 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2073 struct statement *stmt;
2075 anon->ctype.base_type = fn;
2076 stmt = alloc_statement(token->pos, STMT_NONE);
2077 fn->stmt = stmt;
2079 token = parse_asm_statement(token, stmt);
2081 add_symbol(list, anon);
2082 return token;
2085 struct token *external_declaration(struct token *token, struct symbol_list **list)
2087 struct ident *ident = NULL;
2088 struct symbol *decl;
2089 struct ctype ctype = { 0, };
2090 struct symbol *base_type;
2091 int is_typedef;
2093 /* Top-level inline asm? */
2094 if (token_type(token) == TOKEN_IDENT) {
2095 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2096 if (s && s->op->toplevel)
2097 return s->op->toplevel(token, list);
2100 /* Parse declaration-specifiers, if any */
2101 token = declaration_specifiers(token, &ctype, 0);
2102 decl = alloc_symbol(token->pos, SYM_NODE);
2103 decl->ctype = ctype;
2104 token = declarator(token, decl, &ident);
2105 apply_modifiers(token->pos, &decl->ctype);
2107 /* Just a type declaration? */
2108 if (!ident)
2109 return expect(token, ';', "end of type declaration");
2111 /* type define declaration? */
2112 is_typedef = (ctype.modifiers & MOD_TYPEDEF) != 0;
2114 /* Typedefs don't have meaningful storage */
2115 if (is_typedef) {
2116 ctype.modifiers &= ~MOD_STORAGE;
2117 decl->ctype.modifiers &= ~MOD_STORAGE;
2118 decl->ctype.modifiers |= MOD_USERTYPE;
2121 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2123 base_type = decl->ctype.base_type;
2125 if (is_typedef) {
2126 if (base_type && !base_type->ident)
2127 base_type->ident = ident;
2128 } else if (base_type && base_type->type == SYM_FN) {
2129 /* K&R argument declaration? */
2130 if (lookup_type(token))
2131 return parse_k_r_arguments(token, decl, list);
2132 if (match_op(token, '{'))
2133 return parse_function_body(token, decl, list);
2135 if (!(decl->ctype.modifiers & MOD_STATIC))
2136 decl->ctype.modifiers |= MOD_EXTERN;
2137 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2138 sparse_error(token->pos, "void declaration");
2141 for (;;) {
2142 if (!is_typedef && match_op(token, '=')) {
2143 if (decl->ctype.modifiers & MOD_EXTERN) {
2144 warning(decl->pos, "symbol with external linkage has initializer");
2145 decl->ctype.modifiers &= ~MOD_EXTERN;
2147 token = initializer(&decl->initializer, token->next);
2149 if (!is_typedef) {
2150 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2151 add_symbol(list, decl);
2152 fn_local_symbol(decl);
2155 check_declaration(decl);
2157 if (!match_op(token, ','))
2158 break;
2160 token = token->next;
2161 ident = NULL;
2162 decl = alloc_symbol(token->pos, SYM_NODE);
2163 decl->ctype = ctype;
2164 token = declaration_specifiers(token, &decl->ctype, 1);
2165 token = declarator(token, decl, &ident);
2166 apply_modifiers(token->pos, &decl->ctype);
2167 if (!ident) {
2168 sparse_error(token->pos, "expected identifier name in type definition");
2169 return token;
2172 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2174 /* Function declarations are automatically extern unless specifically static */
2175 base_type = decl->ctype.base_type;
2176 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2177 if (!(decl->ctype.modifiers & MOD_STATIC))
2178 decl->ctype.modifiers |= MOD_EXTERN;
2181 return expect(token, ';', "at end of declaration");