possible fix to cgcc issue in sparse 0.4.2:
[smatch.git] / parse.c
blob66b8112f3b213169849567af689527ac79f76b2a
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 static struct symbol_list **function_symbol_list;
32 struct symbol_list *function_computed_target_list;
33 struct statement_list *function_computed_goto_list;
35 static struct token *statement(struct token *token, struct statement **tree);
36 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords);
38 typedef struct token *declarator_t(struct token *, struct decl_state *);
39 static declarator_t
40 struct_specifier, union_specifier, enum_specifier,
41 attribute_specifier, typeof_specifier, parse_asm_declarator,
42 typedef_specifier, inline_specifier, auto_specifier,
43 register_specifier, static_specifier, extern_specifier,
44 thread_specifier, const_qualifier, volatile_qualifier;
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);
61 typedef struct token *attr_t(struct token *, struct symbol *,
62 struct decl_state *);
64 static attr_t
65 attribute_packed, attribute_aligned, attribute_modifier,
66 attribute_address_space, attribute_context,
67 attribute_designated_init,
68 attribute_transparent_union, ignore_attribute,
69 attribute_mode, attribute_force;
71 typedef struct symbol *to_mode_t(struct symbol *);
73 static to_mode_t
74 to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode, to_word_mode;
76 enum {
77 Set_T = 1,
78 Set_S = 2,
79 Set_Char = 4,
80 Set_Int = 8,
81 Set_Double = 16,
82 Set_Float = 32,
83 Set_Signed = 64,
84 Set_Unsigned = 128,
85 Set_Short = 256,
86 Set_Long = 512,
87 Set_Vlong = 1024,
88 Set_Any = Set_T | Set_Short | Set_Long | Set_Signed | Set_Unsigned
91 enum {
92 CInt = 0, CSInt, CUInt, CReal, CChar, CSChar, CUChar
95 enum {
96 SNone = 0, STypedef, SAuto, SRegister, SExtern, SStatic, SForced
99 static struct symbol_op typedef_op = {
100 .type = KW_MODIFIER,
101 .declarator = typedef_specifier,
104 static struct symbol_op inline_op = {
105 .type = KW_MODIFIER,
106 .declarator = inline_specifier,
109 static struct symbol_op auto_op = {
110 .type = KW_MODIFIER,
111 .declarator = auto_specifier,
114 static struct symbol_op register_op = {
115 .type = KW_MODIFIER,
116 .declarator = register_specifier,
119 static struct symbol_op static_op = {
120 .type = KW_MODIFIER,
121 .declarator = static_specifier,
124 static struct symbol_op extern_op = {
125 .type = KW_MODIFIER,
126 .declarator = extern_specifier,
129 static struct symbol_op thread_op = {
130 .type = KW_MODIFIER,
131 .declarator = thread_specifier,
134 static struct symbol_op const_op = {
135 .type = KW_QUALIFIER,
136 .declarator = const_qualifier,
139 static struct symbol_op volatile_op = {
140 .type = KW_QUALIFIER,
141 .declarator = volatile_qualifier,
144 static struct symbol_op restrict_op = {
145 .type = KW_QUALIFIER,
148 static struct symbol_op typeof_op = {
149 .type = KW_SPECIFIER,
150 .declarator = typeof_specifier,
151 .test = Set_Any,
152 .set = Set_S|Set_T,
155 static struct symbol_op attribute_op = {
156 .type = KW_ATTRIBUTE,
157 .declarator = attribute_specifier,
160 static struct symbol_op struct_op = {
161 .type = KW_SPECIFIER,
162 .declarator = struct_specifier,
163 .test = Set_Any,
164 .set = Set_S|Set_T,
167 static struct symbol_op union_op = {
168 .type = KW_SPECIFIER,
169 .declarator = union_specifier,
170 .test = Set_Any,
171 .set = Set_S|Set_T,
174 static struct symbol_op enum_op = {
175 .type = KW_SPECIFIER,
176 .declarator = enum_specifier,
177 .test = Set_Any,
178 .set = Set_S|Set_T,
181 static struct symbol_op spec_op = {
182 .type = KW_SPECIFIER | KW_EXACT,
183 .test = Set_Any,
184 .set = Set_S|Set_T,
187 static struct symbol_op char_op = {
188 .type = KW_SPECIFIER,
189 .test = Set_T|Set_Long|Set_Short,
190 .set = Set_T|Set_Char,
191 .class = CChar,
194 static struct symbol_op int_op = {
195 .type = KW_SPECIFIER,
196 .test = Set_T,
197 .set = Set_T|Set_Int,
200 static struct symbol_op double_op = {
201 .type = KW_SPECIFIER,
202 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
203 .set = Set_T|Set_Double,
204 .class = CReal,
207 static struct symbol_op float_op = {
208 .type = KW_SPECIFIER | KW_SHORT,
209 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long,
210 .set = Set_T|Set_Float,
211 .class = CReal,
214 static struct symbol_op short_op = {
215 .type = KW_SPECIFIER | KW_SHORT,
216 .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short,
217 .set = Set_Short,
220 static struct symbol_op signed_op = {
221 .type = KW_SPECIFIER,
222 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
223 .set = Set_Signed,
224 .class = CSInt,
227 static struct symbol_op unsigned_op = {
228 .type = KW_SPECIFIER,
229 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
230 .set = Set_Unsigned,
231 .class = CUInt,
234 static struct symbol_op long_op = {
235 .type = KW_SPECIFIER | KW_LONG,
236 .test = Set_S|Set_Char|Set_Float|Set_Short|Set_Vlong,
237 .set = Set_Long,
240 static struct symbol_op if_op = {
241 .statement = parse_if_statement,
244 static struct symbol_op return_op = {
245 .statement = parse_return_statement,
248 static struct symbol_op loop_iter_op = {
249 .statement = parse_loop_iterator,
252 static struct symbol_op default_op = {
253 .statement = parse_default_statement,
256 static struct symbol_op case_op = {
257 .statement = parse_case_statement,
260 static struct symbol_op switch_op = {
261 .statement = parse_switch_statement,
264 static struct symbol_op for_op = {
265 .statement = parse_for_statement,
268 static struct symbol_op while_op = {
269 .statement = parse_while_statement,
272 static struct symbol_op do_op = {
273 .statement = parse_do_statement,
276 static struct symbol_op goto_op = {
277 .statement = parse_goto_statement,
280 static struct symbol_op __context___op = {
281 .statement = parse_context_statement,
284 static struct symbol_op range_op = {
285 .statement = parse_range_statement,
288 static struct symbol_op asm_op = {
289 .type = KW_ASM,
290 .declarator = parse_asm_declarator,
291 .statement = parse_asm_statement,
292 .toplevel = toplevel_asm_declaration,
295 static struct symbol_op packed_op = {
296 .attribute = attribute_packed,
299 static struct symbol_op aligned_op = {
300 .attribute = attribute_aligned,
303 static struct symbol_op attr_mod_op = {
304 .attribute = attribute_modifier,
307 static struct symbol_op attr_force_op = {
308 .attribute = attribute_force,
311 static struct symbol_op address_space_op = {
312 .attribute = attribute_address_space,
315 static struct symbol_op mode_op = {
316 .attribute = attribute_mode,
319 static struct symbol_op context_op = {
320 .attribute = attribute_context,
323 static struct symbol_op designated_init_op = {
324 .attribute = attribute_designated_init,
327 static struct symbol_op transparent_union_op = {
328 .attribute = attribute_transparent_union,
331 static struct symbol_op ignore_attr_op = {
332 .attribute = ignore_attribute,
335 static struct symbol_op mode_QI_op = {
336 .type = KW_MODE,
337 .to_mode = to_QI_mode
340 static struct symbol_op mode_HI_op = {
341 .type = KW_MODE,
342 .to_mode = to_HI_mode
345 static struct symbol_op mode_SI_op = {
346 .type = KW_MODE,
347 .to_mode = to_SI_mode
350 static struct symbol_op mode_DI_op = {
351 .type = KW_MODE,
352 .to_mode = to_DI_mode
355 static struct symbol_op mode_TI_op = {
356 .type = KW_MODE,
357 .to_mode = to_TI_mode
360 static struct symbol_op mode_word_op = {
361 .type = KW_MODE,
362 .to_mode = to_word_mode
365 static struct init_keyword {
366 const char *name;
367 enum namespace ns;
368 unsigned long modifiers;
369 struct symbol_op *op;
370 struct symbol *type;
371 } keyword_table[] = {
372 /* Type qualifiers */
373 { "const", NS_TYPEDEF, .op = &const_op },
374 { "__const", NS_TYPEDEF, .op = &const_op },
375 { "__const__", NS_TYPEDEF, .op = &const_op },
376 { "volatile", NS_TYPEDEF, .op = &volatile_op },
377 { "__volatile", NS_TYPEDEF, .op = &volatile_op },
378 { "__volatile__", NS_TYPEDEF, .op = &volatile_op },
380 /* Typedef.. */
381 { "typedef", NS_TYPEDEF, .op = &typedef_op },
383 /* Type specifiers */
384 { "void", NS_TYPEDEF, .type = &void_ctype, .op = &spec_op},
385 { "char", NS_TYPEDEF, .op = &char_op },
386 { "short", NS_TYPEDEF, .op = &short_op },
387 { "int", NS_TYPEDEF, .op = &int_op },
388 { "long", NS_TYPEDEF, .op = &long_op },
389 { "float", NS_TYPEDEF, .op = &float_op },
390 { "double", NS_TYPEDEF, .op = &double_op },
391 { "signed", NS_TYPEDEF, .op = &signed_op },
392 { "__signed", NS_TYPEDEF, .op = &signed_op },
393 { "__signed__", NS_TYPEDEF, .op = &signed_op },
394 { "unsigned", NS_TYPEDEF, .op = &unsigned_op },
395 { "_Bool", NS_TYPEDEF, .type = &bool_ctype, .op = &spec_op },
397 /* Predeclared types */
398 { "__builtin_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
399 { "__builtin_ms_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
400 { "__int128_t", NS_TYPEDEF, .type = &lllong_ctype, .op = &spec_op },
401 { "__uint128_t",NS_TYPEDEF, .type = &ulllong_ctype, .op = &spec_op },
403 /* Extended types */
404 { "typeof", NS_TYPEDEF, .op = &typeof_op },
405 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
406 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
408 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
409 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
411 { "struct", NS_TYPEDEF, .op = &struct_op },
412 { "union", NS_TYPEDEF, .op = &union_op },
413 { "enum", NS_TYPEDEF, .op = &enum_op },
415 { "inline", NS_TYPEDEF, .op = &inline_op },
416 { "__inline", NS_TYPEDEF, .op = &inline_op },
417 { "__inline__", NS_TYPEDEF, .op = &inline_op },
419 /* Ignored for now.. */
420 { "restrict", NS_TYPEDEF, .op = &restrict_op},
421 { "__restrict", NS_TYPEDEF, .op = &restrict_op},
423 /* Storage class */
424 { "auto", NS_TYPEDEF, .op = &auto_op },
425 { "register", NS_TYPEDEF, .op = &register_op },
426 { "static", NS_TYPEDEF, .op = &static_op },
427 { "extern", NS_TYPEDEF, .op = &extern_op },
428 { "__thread", NS_TYPEDEF, .op = &thread_op },
430 /* Statement */
431 { "if", NS_KEYWORD, .op = &if_op },
432 { "return", NS_KEYWORD, .op = &return_op },
433 { "break", NS_KEYWORD, .op = &loop_iter_op },
434 { "continue", NS_KEYWORD, .op = &loop_iter_op },
435 { "default", NS_KEYWORD, .op = &default_op },
436 { "case", NS_KEYWORD, .op = &case_op },
437 { "switch", NS_KEYWORD, .op = &switch_op },
438 { "for", NS_KEYWORD, .op = &for_op },
439 { "while", NS_KEYWORD, .op = &while_op },
440 { "do", NS_KEYWORD, .op = &do_op },
441 { "goto", NS_KEYWORD, .op = &goto_op },
442 { "__context__",NS_KEYWORD, .op = &__context___op },
443 { "__range__", NS_KEYWORD, .op = &range_op },
444 { "asm", NS_KEYWORD, .op = &asm_op },
445 { "__asm", NS_KEYWORD, .op = &asm_op },
446 { "__asm__", NS_KEYWORD, .op = &asm_op },
448 /* Attribute */
449 { "packed", NS_KEYWORD, .op = &packed_op },
450 { "__packed__", NS_KEYWORD, .op = &packed_op },
451 { "aligned", NS_KEYWORD, .op = &aligned_op },
452 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
453 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
454 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
455 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
456 { "force", NS_KEYWORD, .op = &attr_force_op },
457 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
458 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
459 { "address_space",NS_KEYWORD, .op = &address_space_op },
460 { "mode", NS_KEYWORD, .op = &mode_op },
461 { "context", NS_KEYWORD, .op = &context_op },
462 { "designated_init", NS_KEYWORD, .op = &designated_init_op },
463 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
464 { "noreturn", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
465 { "__noreturn__", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
467 { "__mode__", NS_KEYWORD, .op = &mode_op },
468 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
469 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
470 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
471 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
472 { "SI", NS_KEYWORD, .op = &mode_SI_op },
473 { "__SI__", NS_KEYWORD, .op = &mode_SI_op },
474 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
475 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
476 { "TI", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
477 { "__TI__", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
478 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
479 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
482 const char *ignored_attributes[] = {
483 "alias",
484 "__alias__",
485 "alloc_size",
486 "__alloc_size__",
487 "always_inline",
488 "__always_inline__",
489 "bounded",
490 "__bounded__",
491 "cdecl",
492 "__cdecl__",
493 "cold",
494 "__cold__",
495 "const",
496 "__const",
497 "__const__",
498 "constructor",
499 "__constructor__",
500 "deprecated",
501 "__deprecated__",
502 "destructor",
503 "__destructor__",
504 "dllexport",
505 "__dllexport__",
506 "dllimport",
507 "__dllimport__",
508 "externally_visible",
509 "__externally_visible__",
510 "fastcall",
511 "__fastcall__",
512 "format",
513 "__format__",
514 "format_arg",
515 "__format_arg__",
516 "hot",
517 "__hot__",
518 "malloc",
519 "__malloc__",
520 "model",
521 "__model__",
522 "ms_abi",
523 "__ms_abi__",
524 "naked",
525 "__naked__",
526 "no_instrument_function",
527 "__no_instrument_function__",
528 "noinline",
529 "__noinline__",
530 "nonnull",
531 "__nonnull",
532 "__nonnull__",
533 "nothrow",
534 "__nothrow",
535 "__nothrow__",
536 "pure",
537 "__pure__",
538 "regparm",
539 "__regparm__",
540 "section",
541 "__section__",
542 "sentinel",
543 "__sentinel__",
544 "signal",
545 "__signal__",
546 "stdcall",
547 "__stdcall__",
548 "syscall_linkage",
549 "__syscall_linkage__",
550 "sysv_abi",
551 "__sysv_abi__",
552 "unused",
553 "__unused__",
554 "used",
555 "__used__",
556 "visibility",
557 "__visibility__",
558 "warn_unused_result",
559 "__warn_unused_result__",
560 "warning",
561 "__warning__",
562 "weak",
563 "__weak__",
567 void init_parser(int stream)
569 int i;
570 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
571 struct init_keyword *ptr = keyword_table + i;
572 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
573 sym->ident->keyword = 1;
574 if (ptr->ns == NS_TYPEDEF)
575 sym->ident->reserved = 1;
576 sym->ctype.modifiers = ptr->modifiers;
577 sym->ctype.base_type = ptr->type;
578 sym->op = ptr->op;
581 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
582 const char * name = ignored_attributes[i];
583 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
584 NS_KEYWORD);
585 sym->ident->keyword = 1;
586 sym->op = &ignore_attr_op;
591 // Add a symbol to the list of function-local symbols
592 static void fn_local_symbol(struct symbol *sym)
594 if (function_symbol_list)
595 add_symbol(function_symbol_list, sym);
598 static int SENTINEL_ATTR match_idents(struct token *token, ...)
600 va_list args;
601 struct ident * next;
603 if (token_type(token) != TOKEN_IDENT)
604 return 0;
606 va_start(args, token);
607 do {
608 next = va_arg(args, struct ident *);
609 } while (next && token->ident != next);
610 va_end(args);
612 return next && token->ident == next;
616 struct statement *alloc_statement(struct position pos, int type)
618 struct statement *stmt = __alloc_statement(0);
619 stmt->type = type;
620 stmt->pos = pos;
621 return stmt;
624 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
626 static void apply_modifiers(struct position pos, struct decl_state *ctx)
628 struct symbol *ctype;
629 if (!ctx->mode)
630 return;
631 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
632 if (!ctype)
633 sparse_error(pos, "don't know how to apply mode to %s",
634 show_typename(ctx->ctype.base_type));
635 else
636 ctx->ctype.base_type = ctype;
640 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
642 struct symbol *sym = alloc_symbol(pos, type);
644 sym->ctype.base_type = ctype->base_type;
645 sym->ctype.modifiers = ctype->modifiers;
647 ctype->base_type = sym;
648 ctype->modifiers = 0;
649 return sym;
653 * NOTE! NS_LABEL is not just a different namespace,
654 * it also ends up using function scope instead of the
655 * regular symbol scope.
657 struct symbol *label_symbol(struct token *token)
659 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
660 if (!sym) {
661 sym = alloc_symbol(token->pos, SYM_LABEL);
662 bind_symbol(sym, token->ident, NS_LABEL);
663 fn_local_symbol(sym);
665 return sym;
668 static struct token *struct_union_enum_specifier(enum type type,
669 struct token *token, struct decl_state *ctx,
670 struct token *(*parse)(struct token *, struct symbol *))
672 struct symbol *sym;
673 struct position *repos;
675 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
676 if (token_type(token) == TOKEN_IDENT) {
677 sym = lookup_symbol(token->ident, NS_STRUCT);
678 if (!sym ||
679 (is_outer_scope(sym->scope) &&
680 (match_op(token->next,';') || match_op(token->next,'{')))) {
681 // Either a new symbol, or else an out-of-scope
682 // symbol being redefined.
683 sym = alloc_symbol(token->pos, type);
684 bind_symbol(sym, token->ident, NS_STRUCT);
686 if (sym->type != type)
687 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
688 ctx->ctype.base_type = sym;
689 repos = &token->pos;
690 token = token->next;
691 if (match_op(token, '{')) {
692 // The following test is actually wrong for empty
693 // structs, but (1) they are not C99, (2) gcc does
694 // the same thing, and (3) it's easier.
695 if (sym->symbol_list)
696 error_die(token->pos, "redefinition of %s", show_typename (sym));
697 sym->pos = *repos;
698 token = parse(token->next, sym);
699 token = expect(token, '}', "at end of struct-union-enum-specifier");
701 // Mark the structure as needing re-examination
702 sym->examined = 0;
703 sym->endpos = token->pos;
705 return token;
708 // private struct/union/enum type
709 if (!match_op(token, '{')) {
710 sparse_error(token->pos, "expected declaration");
711 ctx->ctype.base_type = &bad_ctype;
712 return token;
715 sym = alloc_symbol(token->pos, type);
716 token = parse(token->next, sym);
717 ctx->ctype.base_type = sym;
718 token = expect(token, '}', "at end of specifier");
719 sym->endpos = token->pos;
721 return token;
724 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
726 struct symbol *field, *last = NULL;
727 struct token *res;
728 res = struct_declaration_list(token, &sym->symbol_list);
729 FOR_EACH_PTR(sym->symbol_list, field) {
730 if (!field->ident) {
731 struct symbol *base = field->ctype.base_type;
732 if (base && base->type == SYM_BITFIELD)
733 continue;
735 if (last)
736 last->next_subobject = field;
737 last = field;
738 } END_FOR_EACH_PTR(field);
739 return res;
742 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
744 return struct_declaration_list(token, &sym->symbol_list);
747 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
749 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
752 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
754 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
758 typedef struct {
759 int x;
760 unsigned long long y;
761 } Num;
763 static void upper_boundary(Num *n, Num *v)
765 if (n->x > v->x)
766 return;
767 if (n->x < v->x) {
768 *n = *v;
769 return;
771 if (n->y < v->y)
772 n->y = v->y;
775 static void lower_boundary(Num *n, Num *v)
777 if (n->x < v->x)
778 return;
779 if (n->x > v->x) {
780 *n = *v;
781 return;
783 if (n->y > v->y)
784 n->y = v->y;
787 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
789 int shift = type->bit_size;
790 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
792 if (!is_unsigned)
793 shift--;
794 if (upper->x == 0 && upper->y >> shift)
795 return 0;
796 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
797 return 1;
798 return 0;
801 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
803 if (s1->bit_size < s2->bit_size) {
804 s1 = s2;
805 } else if (s1->bit_size == s2->bit_size) {
806 if (s2->ctype.modifiers & MOD_UNSIGNED)
807 s1 = s2;
809 if (s1->bit_size < bits_in_int)
810 return &int_ctype;
811 return s1;
814 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
816 struct symbol *sym;
818 FOR_EACH_PTR(list, sym) {
819 struct expression *expr = sym->initializer;
820 struct symbol *ctype;
821 if (expr->type != EXPR_VALUE)
822 continue;
823 ctype = expr->ctype;
824 if (ctype->bit_size == base_type->bit_size)
825 continue;
826 cast_value(expr, base_type, expr, ctype);
827 } END_FOR_EACH_PTR(sym);
830 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
832 unsigned long long lastval = 0;
833 struct symbol *ctype = NULL, *base_type = NULL;
834 Num upper = {-1, 0}, lower = {1, 0};
836 parent->examined = 1;
837 parent->ctype.base_type = &int_ctype;
838 while (token_type(token) == TOKEN_IDENT) {
839 struct expression *expr = NULL;
840 struct token *next = token->next;
841 struct symbol *sym;
843 if (match_op(next, '=')) {
844 next = constant_expression(next->next, &expr);
845 lastval = get_expression_value(expr);
846 ctype = &void_ctype;
847 if (expr && expr->ctype)
848 ctype = expr->ctype;
849 } else if (!ctype) {
850 ctype = &int_ctype;
851 } else if (is_int_type(ctype)) {
852 lastval++;
853 } else {
854 error_die(token->pos, "can't increment the last enum member");
857 if (!expr) {
858 expr = alloc_expression(token->pos, EXPR_VALUE);
859 expr->value = lastval;
860 expr->ctype = ctype;
863 sym = alloc_symbol(token->pos, SYM_NODE);
864 bind_symbol(sym, token->ident, NS_SYMBOL);
865 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
866 sym->initializer = expr;
867 sym->enum_member = 1;
868 sym->ctype.base_type = parent;
869 add_ptr_list(&parent->symbol_list, sym);
871 if (base_type != &bad_ctype) {
872 if (ctype->type == SYM_NODE)
873 ctype = ctype->ctype.base_type;
874 if (ctype->type == SYM_ENUM) {
875 if (ctype == parent)
876 ctype = base_type;
877 else
878 ctype = ctype->ctype.base_type;
881 * base_type rules:
882 * - if all enums are of the same type, then
883 * the base_type is that type (two first
884 * cases)
885 * - if enums are of different types, they
886 * all have to be integer types, and the
887 * base type is at least "int_ctype".
888 * - otherwise the base_type is "bad_ctype".
890 if (!base_type) {
891 base_type = ctype;
892 } else if (ctype == base_type) {
893 /* nothing */
894 } else if (is_int_type(base_type) && is_int_type(ctype)) {
895 base_type = bigger_enum_type(base_type, ctype);
896 } else
897 base_type = &bad_ctype;
898 parent->ctype.base_type = base_type;
900 if (is_int_type(base_type)) {
901 Num v = {.y = lastval};
902 if (ctype->ctype.modifiers & MOD_UNSIGNED)
903 v.x = 0;
904 else if ((long long)lastval >= 0)
905 v.x = 0;
906 else
907 v.x = -1;
908 upper_boundary(&upper, &v);
909 lower_boundary(&lower, &v);
911 token = next;
913 sym->endpos = token->pos;
915 if (!match_op(token, ','))
916 break;
917 token = token->next;
919 if (!base_type) {
920 sparse_error(token->pos, "bad enum definition");
921 base_type = &bad_ctype;
923 else if (!is_int_type(base_type))
924 base_type = base_type;
925 else if (type_is_ok(base_type, &upper, &lower))
926 base_type = base_type;
927 else if (type_is_ok(&int_ctype, &upper, &lower))
928 base_type = &int_ctype;
929 else if (type_is_ok(&uint_ctype, &upper, &lower))
930 base_type = &uint_ctype;
931 else if (type_is_ok(&long_ctype, &upper, &lower))
932 base_type = &long_ctype;
933 else if (type_is_ok(&ulong_ctype, &upper, &lower))
934 base_type = &ulong_ctype;
935 else if (type_is_ok(&llong_ctype, &upper, &lower))
936 base_type = &llong_ctype;
937 else if (type_is_ok(&ullong_ctype, &upper, &lower))
938 base_type = &ullong_ctype;
939 else
940 base_type = &bad_ctype;
941 parent->ctype.base_type = base_type;
942 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
943 parent->examined = 0;
945 cast_enum_list(parent->symbol_list, base_type);
947 return token;
950 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
952 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
953 struct ctype *ctype = &ctx->ctype.base_type->ctype;
955 if (!ctype->base_type)
956 ctype->base_type = &incomplete_ctype;
958 return ret;
961 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
963 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
965 struct symbol *sym;
967 if (!match_op(token, '(')) {
968 sparse_error(token->pos, "expected '(' after typeof");
969 return token;
971 if (lookup_type(token->next)) {
972 token = typename(token->next, &sym, NULL);
973 ctx->ctype.base_type = sym->ctype.base_type;
974 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
975 } else {
976 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
977 token = parse_expression(token->next, &typeof_sym->initializer);
979 typeof_sym->endpos = token->pos;
980 if (!typeof_sym->initializer) {
981 sparse_error(token->pos, "expected expression after the '(' token");
982 typeof_sym = &bad_ctype;
984 ctx->ctype.base_type = typeof_sym;
986 return expect(token, ')', "after typeof");
989 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
991 struct expression *expr = NULL;
992 if (match_op(token, '('))
993 token = parens_expression(token, &expr, "in attribute");
994 return token;
997 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
999 if (!ctx->ctype.alignment)
1000 ctx->ctype.alignment = 1;
1001 return token;
1004 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1006 int alignment = max_alignment;
1007 struct expression *expr = NULL;
1009 if (match_op(token, '(')) {
1010 token = parens_expression(token, &expr, "in attribute");
1011 if (expr)
1012 alignment = const_expression_value(expr);
1014 if (alignment & (alignment-1)) {
1015 warning(token->pos, "I don't like non-power-of-2 alignments");
1016 return token;
1017 } else if (alignment > ctx->ctype.alignment)
1018 ctx->ctype.alignment = alignment;
1019 return token;
1022 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1024 if (ctx->modifiers & qual)
1025 warning(*pos, "duplicate %s", modifier_string(qual));
1026 ctx->modifiers |= qual;
1029 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1031 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1032 return token;
1035 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1037 struct expression *expr = NULL;
1038 int as;
1039 token = expect(token, '(', "after address_space attribute");
1040 token = conditional_expression(token, &expr);
1041 if (expr) {
1042 as = const_expression_value(expr);
1043 if (Waddress_space && as)
1044 ctx->ctype.as = as;
1046 token = expect(token, ')', "after address_space attribute");
1047 return token;
1050 static struct symbol *to_QI_mode(struct symbol *ctype)
1052 if (ctype->ctype.base_type != &int_type)
1053 return NULL;
1054 if (ctype == &char_ctype)
1055 return ctype;
1056 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1057 : &schar_ctype;
1060 static struct symbol *to_HI_mode(struct symbol *ctype)
1062 if (ctype->ctype.base_type != &int_type)
1063 return NULL;
1064 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1065 : &sshort_ctype;
1068 static struct symbol *to_SI_mode(struct symbol *ctype)
1070 if (ctype->ctype.base_type != &int_type)
1071 return NULL;
1072 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1073 : &sint_ctype;
1076 static struct symbol *to_DI_mode(struct symbol *ctype)
1078 if (ctype->ctype.base_type != &int_type)
1079 return NULL;
1080 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1081 : &sllong_ctype;
1084 static struct symbol *to_TI_mode(struct symbol *ctype)
1086 if (ctype->ctype.base_type != &int_type)
1087 return NULL;
1088 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1089 : &slllong_ctype;
1092 static struct symbol *to_word_mode(struct symbol *ctype)
1094 if (ctype->ctype.base_type != &int_type)
1095 return NULL;
1096 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1097 : &slong_ctype;
1100 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1102 token = expect(token, '(', "after mode attribute");
1103 if (token_type(token) == TOKEN_IDENT) {
1104 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1105 if (mode && mode->op->type == KW_MODE)
1106 ctx->mode = mode->op;
1107 else
1108 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1109 token = token->next;
1110 } else
1111 sparse_error(token->pos, "expect attribute mode symbol\n");
1112 token = expect(token, ')', "after mode attribute");
1113 return token;
1116 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1118 struct context *context = alloc_context();
1119 struct expression *args[3];
1120 int argc = 0;
1122 token = expect(token, '(', "after context attribute");
1123 while (!match_op(token, ')')) {
1124 struct expression *expr = NULL;
1125 token = conditional_expression(token, &expr);
1126 if (!expr)
1127 break;
1128 if (argc < 3)
1129 args[argc++] = expr;
1130 if (!match_op(token, ','))
1131 break;
1132 token = token->next;
1135 switch(argc) {
1136 case 0:
1137 sparse_error(token->pos, "expected context input/output values");
1138 break;
1139 case 1:
1140 context->in = get_expression_value(args[0]);
1141 break;
1142 case 2:
1143 context->in = get_expression_value(args[0]);
1144 context->out = get_expression_value(args[1]);
1145 break;
1146 case 3:
1147 context->context = args[0];
1148 context->in = get_expression_value(args[1]);
1149 context->out = get_expression_value(args[2]);
1150 break;
1153 if (argc)
1154 add_ptr_list(&ctx->ctype.contexts, context);
1156 token = expect(token, ')', "after context attribute");
1157 return token;
1160 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1162 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1163 ctx->ctype.base_type->designated_init = 1;
1164 else
1165 warning(token->pos, "attribute designated_init applied to non-structure type");
1166 return token;
1169 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1171 if (Wtransparent_union)
1172 warning(token->pos, "ignoring attribute __transparent_union__");
1173 return token;
1176 static struct token *recover_unknown_attribute(struct token *token)
1178 struct expression *expr = NULL;
1180 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1181 token = token->next;
1182 if (match_op(token, '('))
1183 token = parens_expression(token, &expr, "in attribute");
1184 return token;
1187 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1189 token = expect(token, '(', "after attribute");
1190 token = expect(token, '(', "after attribute");
1192 for (;;) {
1193 struct ident *attribute_name;
1194 struct symbol *attr;
1196 if (eof_token(token))
1197 break;
1198 if (match_op(token, ';'))
1199 break;
1200 if (token_type(token) != TOKEN_IDENT)
1201 break;
1202 attribute_name = token->ident;
1203 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1204 if (attr && attr->op->attribute)
1205 token = attr->op->attribute(token->next, attr, ctx);
1206 else
1207 token = recover_unknown_attribute(token);
1209 if (!match_op(token, ','))
1210 break;
1211 token = token->next;
1214 token = expect(token, ')', "after attribute");
1215 token = expect(token, ')', "after attribute");
1216 return token;
1219 static const char *storage_class[] =
1221 [STypedef] = "typedef",
1222 [SAuto] = "auto",
1223 [SExtern] = "extern",
1224 [SStatic] = "static",
1225 [SRegister] = "register",
1226 [SForced] = "[force]"
1229 static unsigned long storage_modifiers(struct decl_state *ctx)
1231 static unsigned long mod[] =
1233 [SAuto] = MOD_AUTO,
1234 [SExtern] = MOD_EXTERN,
1235 [SStatic] = MOD_STATIC,
1236 [SRegister] = MOD_REGISTER
1238 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1239 | (ctx->is_tls ? MOD_TLS : 0);
1242 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1244 /* __thread can be used alone, or with extern or static */
1245 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1246 sparse_error(*pos, "__thread can only be used alone, or with "
1247 "extern or static");
1248 return;
1251 if (!ctx->storage_class) {
1252 ctx->storage_class = class;
1253 return;
1255 if (ctx->storage_class == class)
1256 sparse_error(*pos, "duplicate %s", storage_class[class]);
1257 else
1258 sparse_error(*pos, "multiple storage classes");
1261 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1263 set_storage_class(&next->pos, ctx, STypedef);
1264 return next;
1267 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1269 set_storage_class(&next->pos, ctx, SAuto);
1270 return next;
1273 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1275 set_storage_class(&next->pos, ctx, SRegister);
1276 return next;
1279 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1281 set_storage_class(&next->pos, ctx, SStatic);
1282 return next;
1285 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1287 set_storage_class(&next->pos, ctx, SExtern);
1288 return next;
1291 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1293 /* This GCC extension can be used alone, or with extern or static */
1294 if (!ctx->storage_class || ctx->storage_class == SStatic
1295 || ctx->storage_class == SExtern) {
1296 ctx->is_tls = 1;
1297 } else {
1298 sparse_error(next->pos, "__thread can only be used alone, or "
1299 "with extern or static");
1302 return next;
1305 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1307 set_storage_class(&token->pos, ctx, SForced);
1308 return token;
1311 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1313 ctx->is_inline = 1;
1314 return next;
1317 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1319 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1320 return next;
1323 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1325 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1326 return next;
1329 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1331 unsigned long mod = thistype->modifiers;
1333 if (mod)
1334 apply_qualifier(&pos, ctype, mod);
1336 /* Context */
1337 concat_ptr_list((struct ptr_list *)thistype->contexts,
1338 (struct ptr_list **)&ctype->contexts);
1340 /* Alignment */
1341 if (thistype->alignment > ctype->alignment)
1342 ctype->alignment = thistype->alignment;
1344 /* Address space */
1345 if (thistype->as)
1346 ctype->as = thistype->as;
1349 static void specifier_conflict(struct position pos, int what, struct ident *new)
1351 const char *old;
1352 if (what & (Set_S | Set_T))
1353 goto Catch_all;
1354 if (what & Set_Char)
1355 old = "char";
1356 else if (what & Set_Double)
1357 old = "double";
1358 else if (what & Set_Float)
1359 old = "float";
1360 else if (what & Set_Signed)
1361 old = "signed";
1362 else if (what & Set_Unsigned)
1363 old = "unsigned";
1364 else if (what & Set_Short)
1365 old = "short";
1366 else if (what & Set_Long)
1367 old = "long";
1368 else
1369 old = "long long";
1370 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1371 old, show_ident(new));
1372 return;
1374 Catch_all:
1375 sparse_error(pos, "two or more data types in declaration specifiers");
1378 static struct symbol * const int_types[] =
1379 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype};
1380 static struct symbol * const signed_types[] =
1381 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1382 &slllong_ctype};
1383 static struct symbol * const unsigned_types[] =
1384 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1385 &ulllong_ctype};
1386 static struct symbol * const real_types[] =
1387 {&float_ctype, &double_ctype, &ldouble_ctype};
1388 static struct symbol * const char_types[] =
1389 {&char_ctype, &schar_ctype, &uchar_ctype};
1390 static struct symbol * const * const types[] = {
1391 int_types + 1, signed_types + 1, unsigned_types + 1,
1392 real_types + 1, char_types, char_types + 1, char_types + 2
1395 struct symbol *ctype_integer(int size, int want_unsigned)
1397 return types[want_unsigned ? CUInt : CInt][size];
1400 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1402 while (token_type(t) == TOKEN_IDENT) {
1403 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1404 if (!s)
1405 break;
1406 if (s->type != SYM_KEYWORD)
1407 break;
1408 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1409 break;
1410 t = t->next;
1411 if (s->op->declarator)
1412 t = s->op->declarator(t, ctx);
1414 return t;
1417 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1419 int seen = 0;
1420 int class = CInt;
1421 int size = 0;
1423 while (token_type(token) == TOKEN_IDENT) {
1424 struct symbol *s = lookup_symbol(token->ident,
1425 NS_TYPEDEF | NS_SYMBOL);
1426 if (!s || !(s->namespace & NS_TYPEDEF))
1427 break;
1428 if (s->type != SYM_KEYWORD) {
1429 if (seen & Set_Any)
1430 break;
1431 seen |= Set_S | Set_T;
1432 ctx->ctype.base_type = s->ctype.base_type;
1433 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1434 token = token->next;
1435 continue;
1437 if (s->op->type & KW_SPECIFIER) {
1438 if (seen & s->op->test) {
1439 specifier_conflict(token->pos,
1440 seen & s->op->test,
1441 token->ident);
1442 break;
1444 seen |= s->op->set;
1445 class += s->op->class;
1446 if (s->op->type & KW_SHORT) {
1447 size = -1;
1448 } else if (s->op->type & KW_LONG && size++) {
1449 if (class == CReal) {
1450 specifier_conflict(token->pos,
1451 Set_Vlong,
1452 &double_ident);
1453 break;
1455 seen |= Set_Vlong;
1458 token = token->next;
1459 if (s->op->declarator)
1460 token = s->op->declarator(token, ctx);
1461 if (s->op->type & KW_EXACT) {
1462 ctx->ctype.base_type = s->ctype.base_type;
1463 ctx->ctype.modifiers |= s->ctype.modifiers;
1467 if (!(seen & Set_S)) { /* not set explicitly? */
1468 struct symbol *base = &incomplete_ctype;
1469 if (seen & Set_Any)
1470 base = types[class][size];
1471 ctx->ctype.base_type = base;
1474 if (ctx->ctype.modifiers & MOD_BITWISE) {
1475 struct symbol *type;
1476 ctx->ctype.modifiers &= ~MOD_BITWISE;
1477 if (!is_int_type(ctx->ctype.base_type)) {
1478 sparse_error(token->pos, "invalid modifier");
1479 return token;
1481 type = alloc_symbol(token->pos, SYM_BASETYPE);
1482 *type = *ctx->ctype.base_type;
1483 type->ctype.modifiers &= ~MOD_SPECIFIER;
1484 type->ctype.base_type = ctx->ctype.base_type;
1485 type->type = SYM_RESTRICT;
1486 ctx->ctype.base_type = type;
1487 create_fouled(type);
1489 return token;
1492 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1494 struct expression *expr = NULL;
1496 if (match_idents(token, &restrict_ident, &__restrict_ident, NULL))
1497 token = token->next;
1498 token = parse_expression(token, &expr);
1499 sym->array_size = expr;
1500 return token;
1503 static struct token *parameter_type_list(struct token *, struct symbol *);
1504 static struct token *identifier_list(struct token *, struct symbol *);
1505 static struct token *declarator(struct token *token, struct decl_state *ctx);
1507 static struct token *skip_attribute(struct token *token)
1509 token = token->next;
1510 if (match_op(token, '(')) {
1511 int depth = 1;
1512 token = token->next;
1513 while (depth && !eof_token(token)) {
1514 if (token_type(token) == TOKEN_SPECIAL) {
1515 if (token->special == '(')
1516 depth++;
1517 else if (token->special == ')')
1518 depth--;
1520 token = token->next;
1523 return token;
1526 static struct token *skip_attributes(struct token *token)
1528 struct symbol *keyword;
1529 for (;;) {
1530 if (token_type(token) != TOKEN_IDENT)
1531 break;
1532 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1533 if (!keyword || keyword->type != SYM_KEYWORD)
1534 break;
1535 if (!(keyword->op->type & KW_ATTRIBUTE))
1536 break;
1537 token = expect(token->next, '(', "after attribute");
1538 token = expect(token, '(', "after attribute");
1539 for (;;) {
1540 if (eof_token(token))
1541 break;
1542 if (match_op(token, ';'))
1543 break;
1544 if (token_type(token) != TOKEN_IDENT)
1545 break;
1546 token = skip_attribute(token);
1547 if (!match_op(token, ','))
1548 break;
1549 token = token->next;
1551 token = expect(token, ')', "after attribute");
1552 token = expect(token, ')', "after attribute");
1554 return token;
1557 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1559 struct symbol *keyword;
1560 for (;;) {
1561 if (token_type(token) != TOKEN_IDENT)
1562 break;
1563 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1564 if (!keyword || keyword->type != SYM_KEYWORD)
1565 break;
1566 if (!(keyword->op->type & keywords))
1567 break;
1568 token = keyword->op->declarator(token->next, ctx);
1569 keywords &= KW_ATTRIBUTE;
1571 return token;
1574 static int is_nested(struct token *token, struct token **p,
1575 int prefer_abstract)
1578 * This can be either a parameter list or a grouping.
1579 * For the direct (non-abstract) case, we know if must be
1580 * a parameter list if we already saw the identifier.
1581 * For the abstract case, we know if must be a parameter
1582 * list if it is empty or starts with a type.
1584 struct token *next = token->next;
1586 *p = next = skip_attributes(next);
1588 if (token_type(next) == TOKEN_IDENT) {
1589 if (lookup_type(next))
1590 return !prefer_abstract;
1591 return 1;
1594 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1595 return 0;
1597 return 1;
1600 enum kind {
1601 Empty, K_R, Proto, Bad_Func,
1604 static enum kind which_func(struct token *token,
1605 struct ident **n,
1606 int prefer_abstract)
1608 struct token *next = token->next;
1610 if (token_type(next) == TOKEN_IDENT) {
1611 if (lookup_type(next))
1612 return Proto;
1613 /* identifier list not in definition; complain */
1614 if (prefer_abstract)
1615 warning(token->pos,
1616 "identifier list not in definition");
1617 return K_R;
1620 if (token_type(next) != TOKEN_SPECIAL)
1621 return Bad_Func;
1623 if (next->special == ')') {
1624 /* don't complain about those */
1625 if (!n || match_op(next->next, ';'))
1626 return Empty;
1627 warning(next->pos,
1628 "non-ANSI function declaration of function '%s'",
1629 show_ident(*n));
1630 return Empty;
1633 if (next->special == SPECIAL_ELLIPSIS) {
1634 warning(next->pos,
1635 "variadic functions must have one named argument");
1636 return Proto;
1639 return Bad_Func;
1642 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1644 struct ctype *ctype = &ctx->ctype;
1645 struct token *next;
1646 struct ident **p = ctx->ident;
1648 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1649 *ctx->ident = token->ident;
1650 token = token->next;
1651 } else if (match_op(token, '(') &&
1652 is_nested(token, &next, ctx->prefer_abstract)) {
1653 struct symbol *base_type = ctype->base_type;
1654 if (token->next != next)
1655 next = handle_attributes(token->next, ctx,
1656 KW_ATTRIBUTE);
1657 token = declarator(next, ctx);
1658 token = expect(token, ')', "in nested declarator");
1659 while (ctype->base_type != base_type)
1660 ctype = &ctype->base_type->ctype;
1661 p = NULL;
1664 if (match_op(token, '(')) {
1665 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1666 struct symbol *fn;
1667 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1668 token = token->next;
1669 if (kind == K_R)
1670 token = identifier_list(token, fn);
1671 else if (kind == Proto)
1672 token = parameter_type_list(token, fn);
1673 token = expect(token, ')', "in function declarator");
1674 fn->endpos = token->pos;
1675 return token;
1678 while (match_op(token, '[')) {
1679 struct symbol *array;
1680 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1681 token = abstract_array_declarator(token->next, array);
1682 token = expect(token, ']', "in abstract_array_declarator");
1683 array->endpos = token->pos;
1684 ctype = &array->ctype;
1686 return token;
1689 static struct token *pointer(struct token *token, struct decl_state *ctx)
1691 while (match_op(token,'*')) {
1692 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1693 ptr->ctype.modifiers = ctx->ctype.modifiers;
1694 ptr->ctype.base_type = ctx->ctype.base_type;
1695 ptr->ctype.as = ctx->ctype.as;
1696 ptr->ctype.contexts = ctx->ctype.contexts;
1697 ctx->ctype.modifiers = 0;
1698 ctx->ctype.base_type = ptr;
1699 ctx->ctype.as = 0;
1700 ctx->ctype.contexts = NULL;
1702 token = handle_qualifiers(token->next, ctx);
1703 ctx->ctype.base_type->endpos = token->pos;
1705 return token;
1708 static struct token *declarator(struct token *token, struct decl_state *ctx)
1710 token = pointer(token, ctx);
1711 return direct_declarator(token, ctx);
1714 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1716 struct ctype *ctype = &ctx->ctype;
1717 struct expression *expr;
1718 struct symbol *bitfield;
1719 long long width;
1721 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1722 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1723 show_typename(ctype->base_type));
1724 // Parse this to recover gracefully.
1725 return conditional_expression(token->next, &expr);
1728 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1729 token = conditional_expression(token->next, &expr);
1730 width = const_expression_value(expr);
1731 bitfield->bit_size = width;
1733 if (width < 0 || width > INT_MAX) {
1734 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1735 width = -1;
1736 } else if (*ctx->ident && width == 0) {
1737 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1738 show_ident(*ctx->ident));
1739 width = -1;
1740 } else if (*ctx->ident) {
1741 struct symbol *base_type = bitfield->ctype.base_type;
1742 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1743 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1744 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1745 // Valid values are either {-1;0} or {0}, depending on integer
1746 // representation. The latter makes for very efficient code...
1747 sparse_error(token->pos, "dubious one-bit signed bitfield");
1749 if (Wdefault_bitfield_sign &&
1750 bitfield_type->type != SYM_ENUM &&
1751 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1752 is_signed) {
1753 // The sign of bitfields is unspecified by default.
1754 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1757 bitfield->bit_size = width;
1758 bitfield->endpos = token->pos;
1759 return token;
1762 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1764 struct decl_state ctx = {.prefer_abstract = 0};
1765 struct ctype saved;
1766 unsigned long mod;
1768 token = declaration_specifiers(token, &ctx);
1769 mod = storage_modifiers(&ctx);
1770 saved = ctx.ctype;
1771 for (;;) {
1772 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1773 ctx.ident = &decl->ident;
1775 token = declarator(token, &ctx);
1776 if (match_op(token, ':'))
1777 token = handle_bitfield(token, &ctx);
1779 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1780 apply_modifiers(token->pos, &ctx);
1782 decl->ctype = ctx.ctype;
1783 decl->ctype.modifiers |= mod;
1784 decl->endpos = token->pos;
1785 add_symbol(list, decl);
1786 if (!match_op(token, ','))
1787 break;
1788 token = token->next;
1789 ctx.ctype = saved;
1791 return token;
1794 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1796 while (!match_op(token, '}')) {
1797 if (!match_op(token, ';'))
1798 token = declaration_list(token, list);
1799 if (!match_op(token, ';')) {
1800 sparse_error(token->pos, "expected ; at end of declaration");
1801 break;
1803 token = token->next;
1805 return token;
1808 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1810 struct decl_state ctx = {.prefer_abstract = 1};
1812 token = declaration_specifiers(token, &ctx);
1813 ctx.ident = &sym->ident;
1814 token = declarator(token, &ctx);
1815 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1816 apply_modifiers(token->pos, &ctx);
1817 sym->ctype = ctx.ctype;
1818 sym->ctype.modifiers |= storage_modifiers(&ctx);
1819 sym->endpos = token->pos;
1820 return token;
1823 struct token *typename(struct token *token, struct symbol **p, int *forced)
1825 struct decl_state ctx = {.prefer_abstract = 1};
1826 int class;
1827 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1828 *p = sym;
1829 token = declaration_specifiers(token, &ctx);
1830 token = declarator(token, &ctx);
1831 apply_modifiers(token->pos, &ctx);
1832 sym->ctype = ctx.ctype;
1833 sym->endpos = token->pos;
1834 class = ctx.storage_class;
1835 if (forced) {
1836 *forced = 0;
1837 if (class == SForced) {
1838 *forced = 1;
1839 class = 0;
1842 if (class)
1843 warning(sym->pos, "storage class in typename (%s %s)",
1844 storage_class[class], show_typename(sym));
1845 return token;
1848 static struct token *expression_statement(struct token *token, struct expression **tree)
1850 token = parse_expression(token, tree);
1851 return expect(token, ';', "at end of statement");
1854 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1855 struct expression_list **inout)
1857 struct expression *expr;
1859 /* Allow empty operands */
1860 if (match_op(token->next, ':') || match_op(token->next, ')'))
1861 return token->next;
1862 do {
1863 struct ident *ident = NULL;
1864 if (match_op(token->next, '[') &&
1865 token_type(token->next->next) == TOKEN_IDENT &&
1866 match_op(token->next->next->next, ']')) {
1867 ident = token->next->next->ident;
1868 token = token->next->next->next;
1870 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1871 token = primary_expression(token->next, &expr);
1872 add_expression(inout, expr);
1873 token = parens_expression(token, &expr, "in asm parameter");
1874 add_expression(inout, expr);
1875 } while (match_op(token, ','));
1876 return token;
1879 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1880 struct expression_list **clobbers)
1882 struct expression *expr;
1884 do {
1885 token = primary_expression(token->next, &expr);
1886 add_expression(clobbers, expr);
1887 } while (match_op(token, ','));
1888 return token;
1891 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1893 token = token->next;
1894 stmt->type = STMT_ASM;
1895 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1896 token = token->next;
1898 token = expect(token, '(', "after asm");
1899 token = parse_expression(token, &stmt->asm_string);
1900 if (match_op(token, ':'))
1901 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1902 if (match_op(token, ':'))
1903 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1904 if (match_op(token, ':'))
1905 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1906 token = expect(token, ')', "after asm");
1907 return expect(token, ';', "at end of asm-statement");
1910 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
1912 struct expression *expr;
1913 token = expect(token, '(', "after asm");
1914 token = parse_expression(token->next, &expr);
1915 token = expect(token, ')', "after asm");
1916 return token;
1919 /* Make a statement out of an expression */
1920 static struct statement *make_statement(struct expression *expr)
1922 struct statement *stmt;
1924 if (!expr)
1925 return NULL;
1926 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1927 stmt->expression = expr;
1928 return stmt;
1932 * All iterators have two symbols associated with them:
1933 * the "continue" and "break" symbols, which are targets
1934 * for continue and break statements respectively.
1936 * They are in a special name-space, but they follow
1937 * all the normal visibility rules, so nested iterators
1938 * automatically work right.
1940 static void start_iterator(struct statement *stmt)
1942 struct symbol *cont, *brk;
1944 start_symbol_scope();
1945 cont = alloc_symbol(stmt->pos, SYM_NODE);
1946 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1947 brk = alloc_symbol(stmt->pos, SYM_NODE);
1948 bind_symbol(brk, &break_ident, NS_ITERATOR);
1950 stmt->type = STMT_ITERATOR;
1951 stmt->iterator_break = brk;
1952 stmt->iterator_continue = cont;
1953 fn_local_symbol(brk);
1954 fn_local_symbol(cont);
1957 static void end_iterator(struct statement *stmt)
1959 end_symbol_scope();
1962 static struct statement *start_function(struct symbol *sym)
1964 struct symbol *ret;
1965 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1967 start_function_scope();
1968 ret = alloc_symbol(sym->pos, SYM_NODE);
1969 ret->ctype = sym->ctype.base_type->ctype;
1970 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1971 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1972 bind_symbol(ret, &return_ident, NS_ITERATOR);
1973 stmt->ret = ret;
1974 fn_local_symbol(ret);
1976 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1977 current_fn = sym;
1979 return stmt;
1982 static void end_function(struct symbol *sym)
1984 current_fn = NULL;
1985 end_function_scope();
1989 * A "switch()" statement, like an iterator, has a
1990 * the "break" symbol associated with it. It works
1991 * exactly like the iterator break - it's the target
1992 * for any break-statements in scope, and means that
1993 * "break" handling doesn't even need to know whether
1994 * it's breaking out of an iterator or a switch.
1996 * In addition, the "case" symbol is a marker for the
1997 * case/default statements to find the switch statement
1998 * that they are associated with.
2000 static void start_switch(struct statement *stmt)
2002 struct symbol *brk, *switch_case;
2004 start_symbol_scope();
2005 brk = alloc_symbol(stmt->pos, SYM_NODE);
2006 bind_symbol(brk, &break_ident, NS_ITERATOR);
2008 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2009 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2010 switch_case->stmt = stmt;
2012 stmt->type = STMT_SWITCH;
2013 stmt->switch_break = brk;
2014 stmt->switch_case = switch_case;
2016 fn_local_symbol(brk);
2017 fn_local_symbol(switch_case);
2020 static void end_switch(struct statement *stmt)
2022 if (!stmt->switch_case->symbol_list)
2023 warning(stmt->pos, "switch with no cases");
2024 end_symbol_scope();
2027 static void add_case_statement(struct statement *stmt)
2029 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2030 struct symbol *sym;
2032 if (!target) {
2033 sparse_error(stmt->pos, "not in switch scope");
2034 stmt->type = STMT_NONE;
2035 return;
2037 sym = alloc_symbol(stmt->pos, SYM_NODE);
2038 add_symbol(&target->symbol_list, sym);
2039 sym->stmt = stmt;
2040 stmt->case_label = sym;
2041 fn_local_symbol(sym);
2044 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2046 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2048 if (!target)
2049 error_die(token->pos, "internal error: return without a function target");
2050 stmt->type = STMT_RETURN;
2051 stmt->ret_target = target;
2052 return expression_statement(token->next, &stmt->ret_value);
2055 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2057 struct symbol_list *syms;
2058 struct expression *e1, *e2, *e3;
2059 struct statement *iterator;
2061 start_iterator(stmt);
2062 token = expect(token->next, '(', "after 'for'");
2064 syms = NULL;
2065 e1 = NULL;
2066 /* C99 variable declaration? */
2067 if (lookup_type(token)) {
2068 token = external_declaration(token, &syms);
2069 } else {
2070 token = parse_expression(token, &e1);
2071 token = expect(token, ';', "in 'for'");
2073 token = parse_expression(token, &e2);
2074 token = expect(token, ';', "in 'for'");
2075 token = parse_expression(token, &e3);
2076 token = expect(token, ')', "in 'for'");
2077 token = statement(token, &iterator);
2079 stmt->iterator_syms = syms;
2080 stmt->iterator_pre_statement = make_statement(e1);
2081 stmt->iterator_pre_condition = e2;
2082 stmt->iterator_post_statement = make_statement(e3);
2083 stmt->iterator_post_condition = NULL;
2084 stmt->iterator_statement = iterator;
2085 end_iterator(stmt);
2087 return token;
2090 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2092 struct expression *expr;
2093 struct statement *iterator;
2095 start_iterator(stmt);
2096 token = parens_expression(token->next, &expr, "after 'while'");
2097 token = statement(token, &iterator);
2099 stmt->iterator_pre_condition = expr;
2100 stmt->iterator_post_condition = NULL;
2101 stmt->iterator_statement = iterator;
2102 end_iterator(stmt);
2104 return token;
2107 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2109 struct expression *expr;
2110 struct statement *iterator;
2112 start_iterator(stmt);
2113 token = statement(token->next, &iterator);
2114 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2115 token = token->next;
2116 else
2117 sparse_error(token->pos, "expected 'while' after 'do'");
2118 token = parens_expression(token, &expr, "after 'do-while'");
2120 stmt->iterator_post_condition = expr;
2121 stmt->iterator_statement = iterator;
2122 end_iterator(stmt);
2124 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2125 warning(iterator->pos, "do-while statement is not a compound statement");
2127 return expect(token, ';', "after statement");
2130 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2132 stmt->type = STMT_IF;
2133 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2134 token = statement(token, &stmt->if_true);
2135 if (token_type(token) != TOKEN_IDENT)
2136 return token;
2137 if (token->ident != &else_ident)
2138 return token;
2139 return statement(token->next, &stmt->if_false);
2142 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2144 stmt->type = STMT_CASE;
2145 token = expect(token, ':', "after default/case");
2146 add_case_statement(stmt);
2147 return statement(token, &stmt->case_statement);
2150 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2152 token = parse_expression(token->next, &stmt->case_expression);
2153 if (match_op(token, SPECIAL_ELLIPSIS))
2154 token = parse_expression(token->next, &stmt->case_to);
2155 return case_statement(token, stmt);
2158 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2160 return case_statement(token->next, stmt);
2163 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2165 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2166 stmt->type = STMT_GOTO;
2167 stmt->goto_label = target;
2168 if (!target)
2169 sparse_error(stmt->pos, "break/continue not in iterator scope");
2170 return expect(token->next, ';', "at end of statement");
2173 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2175 stmt->type = STMT_SWITCH;
2176 start_switch(stmt);
2177 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2178 token = statement(token, &stmt->switch_statement);
2179 end_switch(stmt);
2180 return token;
2183 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2185 stmt->type = STMT_GOTO;
2186 token = token->next;
2187 if (match_op(token, '*')) {
2188 token = parse_expression(token->next, &stmt->goto_expression);
2189 add_statement(&function_computed_goto_list, stmt);
2190 } else if (token_type(token) == TOKEN_IDENT) {
2191 stmt->goto_label = label_symbol(token);
2192 token = token->next;
2193 } else {
2194 sparse_error(token->pos, "Expected identifier or goto expression");
2196 return expect(token, ';', "at end of statement");
2199 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2201 stmt->type = STMT_CONTEXT;
2202 token = parse_expression(token->next, &stmt->expression);
2203 if(stmt->expression->type == EXPR_PREOP
2204 && stmt->expression->op == '('
2205 && stmt->expression->unop->type == EXPR_COMMA) {
2206 struct expression *expr;
2207 expr = stmt->expression->unop;
2208 stmt->context = expr->left;
2209 stmt->expression = expr->right;
2211 return expect(token, ';', "at end of statement");
2214 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2216 stmt->type = STMT_RANGE;
2217 token = assignment_expression(token->next, &stmt->range_expression);
2218 token = expect(token, ',', "after range expression");
2219 token = assignment_expression(token, &stmt->range_low);
2220 token = expect(token, ',', "after low range");
2221 token = assignment_expression(token, &stmt->range_high);
2222 return expect(token, ';', "after range statement");
2225 static struct token *statement(struct token *token, struct statement **tree)
2227 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2229 *tree = stmt;
2230 if (token_type(token) == TOKEN_IDENT) {
2231 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2232 if (s && s->op->statement)
2233 return s->op->statement(token, stmt);
2235 if (match_op(token->next, ':')) {
2236 stmt->type = STMT_LABEL;
2237 stmt->label_identifier = label_symbol(token);
2238 token = skip_attributes(token->next->next);
2239 return statement(token, &stmt->label_statement);
2243 if (match_op(token, '{')) {
2244 stmt->type = STMT_COMPOUND;
2245 start_symbol_scope();
2246 token = compound_statement(token->next, stmt);
2247 end_symbol_scope();
2249 return expect(token, '}', "at end of compound statement");
2252 stmt->type = STMT_EXPRESSION;
2253 return expression_statement(token, &stmt->expression);
2256 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2257 static struct token *label_statement(struct token *token)
2259 while (token_type(token) == TOKEN_IDENT) {
2260 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2261 /* it's block-scope, but we want label namespace */
2262 bind_symbol(sym, token->ident, NS_SYMBOL);
2263 sym->namespace = NS_LABEL;
2264 fn_local_symbol(sym);
2265 token = token->next;
2266 if (!match_op(token, ','))
2267 break;
2268 token = token->next;
2270 return expect(token, ';', "at end of label declaration");
2273 static struct token * statement_list(struct token *token, struct statement_list **list)
2275 int seen_statement = 0;
2276 while (token_type(token) == TOKEN_IDENT &&
2277 token->ident == &__label___ident)
2278 token = label_statement(token->next);
2279 for (;;) {
2280 struct statement * stmt;
2281 if (eof_token(token))
2282 break;
2283 if (match_op(token, '}'))
2284 break;
2285 if (lookup_type(token)) {
2286 if (seen_statement) {
2287 warning(token->pos, "mixing declarations and code");
2288 seen_statement = 0;
2290 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2291 token = external_declaration(token, &stmt->declaration);
2292 } else {
2293 seen_statement = Wdeclarationafterstatement;
2294 token = statement(token, &stmt);
2296 add_statement(list, stmt);
2298 return token;
2301 static struct token *identifier_list(struct token *token, struct symbol *fn)
2303 struct symbol_list **list = &fn->arguments;
2304 for (;;) {
2305 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2306 sym->ident = token->ident;
2307 token = token->next;
2308 sym->endpos = token->pos;
2309 sym->ctype.base_type = &incomplete_ctype;
2310 add_symbol(list, sym);
2311 if (!match_op(token, ',') ||
2312 token_type(token->next) != TOKEN_IDENT ||
2313 lookup_type(token->next))
2314 break;
2315 token = token->next;
2317 return token;
2320 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2322 struct symbol_list **list = &fn->arguments;
2324 for (;;) {
2325 struct symbol *sym;
2327 if (match_op(token, SPECIAL_ELLIPSIS)) {
2328 fn->variadic = 1;
2329 token = token->next;
2330 break;
2333 sym = alloc_symbol(token->pos, SYM_NODE);
2334 token = parameter_declaration(token, sym);
2335 if (sym->ctype.base_type == &void_ctype) {
2336 /* Special case: (void) */
2337 if (!*list && !sym->ident)
2338 break;
2339 warning(token->pos, "void parameter");
2341 add_symbol(list, sym);
2342 if (!match_op(token, ','))
2343 break;
2344 token = token->next;
2346 return token;
2349 struct token *compound_statement(struct token *token, struct statement *stmt)
2351 token = statement_list(token, &stmt->stmts);
2352 return token;
2355 static struct expression *identifier_expression(struct token *token)
2357 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2358 expr->expr_ident = token->ident;
2359 return expr;
2362 static struct expression *index_expression(struct expression *from, struct expression *to)
2364 int idx_from, idx_to;
2365 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2367 idx_from = const_expression_value(from);
2368 idx_to = idx_from;
2369 if (to) {
2370 idx_to = const_expression_value(to);
2371 if (idx_to < idx_from || idx_from < 0)
2372 warning(from->pos, "nonsense array initializer index range");
2374 expr->idx_from = idx_from;
2375 expr->idx_to = idx_to;
2376 return expr;
2379 static struct token *single_initializer(struct expression **ep, struct token *token)
2381 int expect_equal = 0;
2382 struct token *next = token->next;
2383 struct expression **tail = ep;
2384 int nested;
2386 *ep = NULL;
2388 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2389 struct expression *expr = identifier_expression(token);
2390 if (Wold_initializer)
2391 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2392 token = initializer(&expr->ident_expression, next->next);
2393 if (expr->ident_expression)
2394 *ep = expr;
2395 return token;
2398 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2399 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2400 struct expression *expr = identifier_expression(next);
2401 *tail = expr;
2402 tail = &expr->ident_expression;
2403 expect_equal = 1;
2404 token = next->next;
2405 } else if (match_op(token, '[')) {
2406 struct expression *from = NULL, *to = NULL, *expr;
2407 token = constant_expression(token->next, &from);
2408 if (!from) {
2409 sparse_error(token->pos, "Expected constant expression");
2410 break;
2412 if (match_op(token, SPECIAL_ELLIPSIS))
2413 token = constant_expression(token->next, &to);
2414 expr = index_expression(from, to);
2415 *tail = expr;
2416 tail = &expr->idx_expression;
2417 token = expect(token, ']', "at end of initializer index");
2418 if (nested)
2419 expect_equal = 1;
2420 } else {
2421 break;
2424 if (nested && !expect_equal) {
2425 if (!match_op(token, '='))
2426 warning(token->pos, "obsolete array initializer, use C99 syntax");
2427 else
2428 expect_equal = 1;
2430 if (expect_equal)
2431 token = expect(token, '=', "at end of initializer index");
2433 token = initializer(tail, token);
2434 if (!*tail)
2435 *ep = NULL;
2436 return token;
2439 static struct token *initializer_list(struct expression_list **list, struct token *token)
2441 struct expression *expr;
2443 for (;;) {
2444 token = single_initializer(&expr, token);
2445 if (!expr)
2446 break;
2447 add_expression(list, expr);
2448 if (!match_op(token, ','))
2449 break;
2450 token = token->next;
2452 return token;
2455 struct token *initializer(struct expression **tree, struct token *token)
2457 if (match_op(token, '{')) {
2458 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2459 *tree = expr;
2460 token = initializer_list(&expr->expr_list, token->next);
2461 return expect(token, '}', "at end of initializer");
2463 return assignment_expression(token, tree);
2466 static void declare_argument(struct symbol *sym, struct symbol *fn)
2468 if (!sym->ident) {
2469 sparse_error(sym->pos, "no identifier for function argument");
2470 return;
2472 bind_symbol(sym, sym->ident, NS_SYMBOL);
2475 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2476 struct symbol_list **list)
2478 struct symbol_list **old_symbol_list;
2479 struct symbol *base_type = decl->ctype.base_type;
2480 struct statement *stmt, **p;
2481 struct symbol *prev;
2482 struct symbol *arg;
2484 old_symbol_list = function_symbol_list;
2485 if (decl->ctype.modifiers & MOD_INLINE) {
2486 function_symbol_list = &decl->inline_symbol_list;
2487 p = &base_type->inline_stmt;
2488 } else {
2489 function_symbol_list = &decl->symbol_list;
2490 p = &base_type->stmt;
2492 function_computed_target_list = NULL;
2493 function_computed_goto_list = NULL;
2495 if (decl->ctype.modifiers & MOD_EXTERN) {
2496 if (!(decl->ctype.modifiers & MOD_INLINE))
2497 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2499 if (!(decl->ctype.modifiers & MOD_STATIC))
2500 decl->ctype.modifiers |= MOD_EXTERN;
2502 stmt = start_function(decl);
2504 *p = stmt;
2505 FOR_EACH_PTR (base_type->arguments, arg) {
2506 declare_argument(arg, base_type);
2507 } END_FOR_EACH_PTR(arg);
2509 token = compound_statement(token->next, stmt);
2511 end_function(decl);
2512 if (!(decl->ctype.modifiers & MOD_INLINE))
2513 add_symbol(list, decl);
2514 check_declaration(decl);
2515 decl->definition = decl;
2516 prev = decl->same_symbol;
2517 if (prev && prev->definition) {
2518 warning(decl->pos, "multiple definitions for function '%s'",
2519 show_ident(decl->ident));
2520 info(prev->definition->pos, " the previous one is here");
2521 } else {
2522 while (prev) {
2523 prev->definition = decl;
2524 prev = prev->same_symbol;
2527 function_symbol_list = old_symbol_list;
2528 if (function_computed_goto_list) {
2529 if (!function_computed_target_list)
2530 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2531 else {
2532 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2533 stmt->target_list = function_computed_target_list;
2534 } END_FOR_EACH_PTR(stmt);
2537 return expect(token, '}', "at end of function");
2540 static void promote_k_r_types(struct symbol *arg)
2542 struct symbol *base = arg->ctype.base_type;
2543 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2544 arg->ctype.base_type = &int_ctype;
2548 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2550 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2551 struct symbol *arg;
2553 FOR_EACH_PTR(real_args, arg) {
2554 struct symbol *type;
2556 /* This is quadratic in the number of arguments. We _really_ don't care */
2557 FOR_EACH_PTR(argtypes, type) {
2558 if (type->ident == arg->ident)
2559 goto match;
2560 } END_FOR_EACH_PTR(type);
2561 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2562 continue;
2563 match:
2564 type->used = 1;
2565 /* "char" and "short" promote to "int" */
2566 promote_k_r_types(type);
2568 arg->ctype = type->ctype;
2569 } END_FOR_EACH_PTR(arg);
2571 FOR_EACH_PTR(argtypes, arg) {
2572 if (!arg->used)
2573 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2574 } END_FOR_EACH_PTR(arg);
2578 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2579 struct symbol_list **list)
2581 struct symbol_list *args = NULL;
2583 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2584 do {
2585 token = declaration_list(token, &args);
2586 if (!match_op(token, ';')) {
2587 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2588 break;
2590 token = token->next;
2591 } while (lookup_type(token));
2593 apply_k_r_types(args, decl);
2595 if (!match_op(token, '{')) {
2596 sparse_error(token->pos, "expected function body");
2597 return token;
2599 return parse_function_body(token, decl, list);
2602 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2604 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2605 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2606 struct statement *stmt;
2608 anon->ctype.base_type = fn;
2609 stmt = alloc_statement(token->pos, STMT_NONE);
2610 fn->stmt = stmt;
2612 token = parse_asm_statement(token, stmt);
2614 add_symbol(list, anon);
2615 return token;
2618 struct token *external_declaration(struct token *token, struct symbol_list **list)
2620 struct ident *ident = NULL;
2621 struct symbol *decl;
2622 struct decl_state ctx = { .ident = &ident };
2623 struct ctype saved;
2624 struct symbol *base_type;
2625 unsigned long mod;
2626 int is_typedef;
2628 /* Top-level inline asm? */
2629 if (token_type(token) == TOKEN_IDENT) {
2630 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2631 if (s && s->op->toplevel)
2632 return s->op->toplevel(token, list);
2635 /* Parse declaration-specifiers, if any */
2636 token = declaration_specifiers(token, &ctx);
2637 mod = storage_modifiers(&ctx);
2638 decl = alloc_symbol(token->pos, SYM_NODE);
2639 /* Just a type declaration? */
2640 if (match_op(token, ';')) {
2641 apply_modifiers(token->pos, &ctx);
2642 return token->next;
2645 saved = ctx.ctype;
2646 token = declarator(token, &ctx);
2647 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2648 apply_modifiers(token->pos, &ctx);
2650 decl->ctype = ctx.ctype;
2651 decl->ctype.modifiers |= mod;
2652 decl->endpos = token->pos;
2654 /* Just a type declaration? */
2655 if (!ident) {
2656 warning(token->pos, "missing identifier in declaration");
2657 return expect(token, ';', "at the end of type declaration");
2660 /* type define declaration? */
2661 is_typedef = ctx.storage_class == STypedef;
2663 /* Typedefs don't have meaningful storage */
2664 if (is_typedef)
2665 decl->ctype.modifiers |= MOD_USERTYPE;
2667 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2669 base_type = decl->ctype.base_type;
2671 if (is_typedef) {
2672 if (base_type && !base_type->ident) {
2673 switch (base_type->type) {
2674 case SYM_STRUCT:
2675 case SYM_UNION:
2676 case SYM_ENUM:
2677 case SYM_RESTRICT:
2678 base_type->ident = ident;
2681 } else if (base_type && base_type->type == SYM_FN) {
2682 /* K&R argument declaration? */
2683 if (lookup_type(token))
2684 return parse_k_r_arguments(token, decl, list);
2685 if (match_op(token, '{'))
2686 return parse_function_body(token, decl, list);
2688 if (!(decl->ctype.modifiers & MOD_STATIC))
2689 decl->ctype.modifiers |= MOD_EXTERN;
2690 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2691 sparse_error(token->pos, "void declaration");
2694 for (;;) {
2695 if (!is_typedef && match_op(token, '=')) {
2696 if (decl->ctype.modifiers & MOD_EXTERN) {
2697 warning(decl->pos, "symbol with external linkage has initializer");
2698 decl->ctype.modifiers &= ~MOD_EXTERN;
2700 token = initializer(&decl->initializer, token->next);
2702 if (!is_typedef) {
2703 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2704 add_symbol(list, decl);
2705 fn_local_symbol(decl);
2708 check_declaration(decl);
2709 if (decl->same_symbol)
2710 decl->definition = decl->same_symbol->definition;
2712 if (!match_op(token, ','))
2713 break;
2715 token = token->next;
2716 ident = NULL;
2717 decl = alloc_symbol(token->pos, SYM_NODE);
2718 ctx.ctype = saved;
2719 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2720 token = declarator(token, &ctx);
2721 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2722 apply_modifiers(token->pos, &ctx);
2723 decl->ctype = ctx.ctype;
2724 decl->ctype.modifiers |= mod;
2725 decl->endpos = token->pos;
2726 if (!ident) {
2727 sparse_error(token->pos, "expected identifier name in type definition");
2728 return token;
2731 if (is_typedef)
2732 decl->ctype.modifiers |= MOD_USERTYPE;
2734 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2736 /* Function declarations are automatically extern unless specifically static */
2737 base_type = decl->ctype.base_type;
2738 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2739 if (!(decl->ctype.modifiers & MOD_STATIC))
2740 decl->ctype.modifiers |= MOD_EXTERN;
2743 return expect(token, ';', "at end of declaration");