*new* check_macros: find macro precedence bugs
[smatch.git] / parse.c
blobf81b19f5fae3d2952317b5e1f0d3859cee384073
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;
1701 ctx->ctype.alignment = 0;
1703 token = handle_qualifiers(token->next, ctx);
1704 ctx->ctype.base_type->endpos = token->pos;
1706 return token;
1709 static struct token *declarator(struct token *token, struct decl_state *ctx)
1711 token = pointer(token, ctx);
1712 return direct_declarator(token, ctx);
1715 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1717 struct ctype *ctype = &ctx->ctype;
1718 struct expression *expr;
1719 struct symbol *bitfield;
1720 long long width;
1722 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1723 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1724 show_typename(ctype->base_type));
1725 // Parse this to recover gracefully.
1726 return conditional_expression(token->next, &expr);
1729 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1730 token = conditional_expression(token->next, &expr);
1731 width = const_expression_value(expr);
1732 bitfield->bit_size = width;
1734 if (width < 0 || width > INT_MAX) {
1735 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1736 width = -1;
1737 } else if (*ctx->ident && width == 0) {
1738 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1739 show_ident(*ctx->ident));
1740 width = -1;
1741 } else if (*ctx->ident) {
1742 struct symbol *base_type = bitfield->ctype.base_type;
1743 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1744 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1745 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1746 // Valid values are either {-1;0} or {0}, depending on integer
1747 // representation. The latter makes for very efficient code...
1748 sparse_error(token->pos, "dubious one-bit signed bitfield");
1750 if (Wdefault_bitfield_sign &&
1751 bitfield_type->type != SYM_ENUM &&
1752 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1753 is_signed) {
1754 // The sign of bitfields is unspecified by default.
1755 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1758 bitfield->bit_size = width;
1759 bitfield->endpos = token->pos;
1760 return token;
1763 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1765 struct decl_state ctx = {.prefer_abstract = 0};
1766 struct ctype saved;
1767 unsigned long mod;
1769 token = declaration_specifiers(token, &ctx);
1770 mod = storage_modifiers(&ctx);
1771 saved = ctx.ctype;
1772 for (;;) {
1773 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1774 ctx.ident = &decl->ident;
1776 token = declarator(token, &ctx);
1777 if (match_op(token, ':'))
1778 token = handle_bitfield(token, &ctx);
1780 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1781 apply_modifiers(token->pos, &ctx);
1783 decl->ctype = ctx.ctype;
1784 decl->ctype.modifiers |= mod;
1785 decl->endpos = token->pos;
1786 add_symbol(list, decl);
1787 if (!match_op(token, ','))
1788 break;
1789 token = token->next;
1790 ctx.ctype = saved;
1792 return token;
1795 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1797 while (!match_op(token, '}')) {
1798 if (!match_op(token, ';'))
1799 token = declaration_list(token, list);
1800 if (!match_op(token, ';')) {
1801 sparse_error(token->pos, "expected ; at end of declaration");
1802 break;
1804 token = token->next;
1806 return token;
1809 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1811 struct decl_state ctx = {.prefer_abstract = 1};
1813 token = declaration_specifiers(token, &ctx);
1814 ctx.ident = &sym->ident;
1815 token = declarator(token, &ctx);
1816 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1817 apply_modifiers(token->pos, &ctx);
1818 sym->ctype = ctx.ctype;
1819 sym->ctype.modifiers |= storage_modifiers(&ctx);
1820 sym->endpos = token->pos;
1821 return token;
1824 struct token *typename(struct token *token, struct symbol **p, int *forced)
1826 struct decl_state ctx = {.prefer_abstract = 1};
1827 int class;
1828 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1829 *p = sym;
1830 token = declaration_specifiers(token, &ctx);
1831 token = declarator(token, &ctx);
1832 apply_modifiers(token->pos, &ctx);
1833 sym->ctype = ctx.ctype;
1834 sym->endpos = token->pos;
1835 class = ctx.storage_class;
1836 if (forced) {
1837 *forced = 0;
1838 if (class == SForced) {
1839 *forced = 1;
1840 class = 0;
1843 if (class)
1844 warning(sym->pos, "storage class in typename (%s %s)",
1845 storage_class[class], show_typename(sym));
1846 return token;
1849 static struct token *expression_statement(struct token *token, struct expression **tree)
1851 token = parse_expression(token, tree);
1852 return expect(token, ';', "at end of statement");
1855 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1856 struct expression_list **inout)
1858 struct expression *expr;
1860 /* Allow empty operands */
1861 if (match_op(token->next, ':') || match_op(token->next, ')'))
1862 return token->next;
1863 do {
1864 struct ident *ident = NULL;
1865 if (match_op(token->next, '[') &&
1866 token_type(token->next->next) == TOKEN_IDENT &&
1867 match_op(token->next->next->next, ']')) {
1868 ident = token->next->next->ident;
1869 token = token->next->next->next;
1871 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1872 token = primary_expression(token->next, &expr);
1873 add_expression(inout, expr);
1874 token = parens_expression(token, &expr, "in asm parameter");
1875 add_expression(inout, expr);
1876 } while (match_op(token, ','));
1877 return token;
1880 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1881 struct expression_list **clobbers)
1883 struct expression *expr;
1885 do {
1886 token = primary_expression(token->next, &expr);
1887 add_expression(clobbers, expr);
1888 } while (match_op(token, ','));
1889 return token;
1892 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1894 token = token->next;
1895 stmt->type = STMT_ASM;
1896 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1897 token = token->next;
1899 token = expect(token, '(', "after asm");
1900 token = parse_expression(token, &stmt->asm_string);
1901 if (match_op(token, ':'))
1902 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1903 if (match_op(token, ':'))
1904 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1905 if (match_op(token, ':'))
1906 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1907 token = expect(token, ')', "after asm");
1908 return expect(token, ';', "at end of asm-statement");
1911 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
1913 struct expression *expr;
1914 token = expect(token, '(', "after asm");
1915 token = parse_expression(token->next, &expr);
1916 token = expect(token, ')', "after asm");
1917 return token;
1920 /* Make a statement out of an expression */
1921 static struct statement *make_statement(struct expression *expr)
1923 struct statement *stmt;
1925 if (!expr)
1926 return NULL;
1927 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1928 stmt->expression = expr;
1929 return stmt;
1933 * All iterators have two symbols associated with them:
1934 * the "continue" and "break" symbols, which are targets
1935 * for continue and break statements respectively.
1937 * They are in a special name-space, but they follow
1938 * all the normal visibility rules, so nested iterators
1939 * automatically work right.
1941 static void start_iterator(struct statement *stmt)
1943 struct symbol *cont, *brk;
1945 start_symbol_scope();
1946 cont = alloc_symbol(stmt->pos, SYM_NODE);
1947 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1948 brk = alloc_symbol(stmt->pos, SYM_NODE);
1949 bind_symbol(brk, &break_ident, NS_ITERATOR);
1951 stmt->type = STMT_ITERATOR;
1952 stmt->iterator_break = brk;
1953 stmt->iterator_continue = cont;
1954 fn_local_symbol(brk);
1955 fn_local_symbol(cont);
1958 static void end_iterator(struct statement *stmt)
1960 end_symbol_scope();
1963 static struct statement *start_function(struct symbol *sym)
1965 struct symbol *ret;
1966 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
1968 start_function_scope();
1969 ret = alloc_symbol(sym->pos, SYM_NODE);
1970 ret->ctype = sym->ctype.base_type->ctype;
1971 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
1972 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
1973 bind_symbol(ret, &return_ident, NS_ITERATOR);
1974 stmt->ret = ret;
1975 fn_local_symbol(ret);
1977 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
1978 current_fn = sym;
1980 return stmt;
1983 static void end_function(struct symbol *sym)
1985 current_fn = NULL;
1986 end_function_scope();
1990 * A "switch()" statement, like an iterator, has a
1991 * the "break" symbol associated with it. It works
1992 * exactly like the iterator break - it's the target
1993 * for any break-statements in scope, and means that
1994 * "break" handling doesn't even need to know whether
1995 * it's breaking out of an iterator or a switch.
1997 * In addition, the "case" symbol is a marker for the
1998 * case/default statements to find the switch statement
1999 * that they are associated with.
2001 static void start_switch(struct statement *stmt)
2003 struct symbol *brk, *switch_case;
2005 start_symbol_scope();
2006 brk = alloc_symbol(stmt->pos, SYM_NODE);
2007 bind_symbol(brk, &break_ident, NS_ITERATOR);
2009 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2010 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2011 switch_case->stmt = stmt;
2013 stmt->type = STMT_SWITCH;
2014 stmt->switch_break = brk;
2015 stmt->switch_case = switch_case;
2017 fn_local_symbol(brk);
2018 fn_local_symbol(switch_case);
2021 static void end_switch(struct statement *stmt)
2023 if (!stmt->switch_case->symbol_list)
2024 warning(stmt->pos, "switch with no cases");
2025 end_symbol_scope();
2028 static void add_case_statement(struct statement *stmt)
2030 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2031 struct symbol *sym;
2033 if (!target) {
2034 sparse_error(stmt->pos, "not in switch scope");
2035 stmt->type = STMT_NONE;
2036 return;
2038 sym = alloc_symbol(stmt->pos, SYM_NODE);
2039 add_symbol(&target->symbol_list, sym);
2040 sym->stmt = stmt;
2041 stmt->case_label = sym;
2042 fn_local_symbol(sym);
2045 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2047 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2049 if (!target)
2050 error_die(token->pos, "internal error: return without a function target");
2051 stmt->type = STMT_RETURN;
2052 stmt->ret_target = target;
2053 return expression_statement(token->next, &stmt->ret_value);
2056 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2058 struct symbol_list *syms;
2059 struct expression *e1, *e2, *e3;
2060 struct statement *iterator;
2062 start_iterator(stmt);
2063 token = expect(token->next, '(', "after 'for'");
2065 syms = NULL;
2066 e1 = NULL;
2067 /* C99 variable declaration? */
2068 if (lookup_type(token)) {
2069 token = external_declaration(token, &syms);
2070 } else {
2071 token = parse_expression(token, &e1);
2072 token = expect(token, ';', "in 'for'");
2074 token = parse_expression(token, &e2);
2075 token = expect(token, ';', "in 'for'");
2076 token = parse_expression(token, &e3);
2077 token = expect(token, ')', "in 'for'");
2078 token = statement(token, &iterator);
2080 stmt->iterator_syms = syms;
2081 stmt->iterator_pre_statement = make_statement(e1);
2082 stmt->iterator_pre_condition = e2;
2083 stmt->iterator_post_statement = make_statement(e3);
2084 stmt->iterator_post_condition = NULL;
2085 stmt->iterator_statement = iterator;
2086 end_iterator(stmt);
2088 return token;
2091 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2093 struct expression *expr;
2094 struct statement *iterator;
2096 start_iterator(stmt);
2097 token = parens_expression(token->next, &expr, "after 'while'");
2098 token = statement(token, &iterator);
2100 stmt->iterator_pre_condition = expr;
2101 stmt->iterator_post_condition = NULL;
2102 stmt->iterator_statement = iterator;
2103 end_iterator(stmt);
2105 return token;
2108 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2110 struct expression *expr;
2111 struct statement *iterator;
2113 start_iterator(stmt);
2114 token = statement(token->next, &iterator);
2115 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2116 token = token->next;
2117 else
2118 sparse_error(token->pos, "expected 'while' after 'do'");
2119 token = parens_expression(token, &expr, "after 'do-while'");
2121 stmt->iterator_post_condition = expr;
2122 stmt->iterator_statement = iterator;
2123 end_iterator(stmt);
2125 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2126 warning(iterator->pos, "do-while statement is not a compound statement");
2128 return expect(token, ';', "after statement");
2131 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2133 stmt->type = STMT_IF;
2134 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2135 token = statement(token, &stmt->if_true);
2136 if (token_type(token) != TOKEN_IDENT)
2137 return token;
2138 if (token->ident != &else_ident)
2139 return token;
2140 return statement(token->next, &stmt->if_false);
2143 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2145 stmt->type = STMT_CASE;
2146 token = expect(token, ':', "after default/case");
2147 add_case_statement(stmt);
2148 return statement(token, &stmt->case_statement);
2151 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2153 token = parse_expression(token->next, &stmt->case_expression);
2154 if (match_op(token, SPECIAL_ELLIPSIS))
2155 token = parse_expression(token->next, &stmt->case_to);
2156 return case_statement(token, stmt);
2159 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2161 return case_statement(token->next, stmt);
2164 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2166 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2167 stmt->type = STMT_GOTO;
2168 stmt->goto_label = target;
2169 if (!target)
2170 sparse_error(stmt->pos, "break/continue not in iterator scope");
2171 return expect(token->next, ';', "at end of statement");
2174 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2176 stmt->type = STMT_SWITCH;
2177 start_switch(stmt);
2178 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2179 token = statement(token, &stmt->switch_statement);
2180 end_switch(stmt);
2181 return token;
2184 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2186 stmt->type = STMT_GOTO;
2187 token = token->next;
2188 if (match_op(token, '*')) {
2189 token = parse_expression(token->next, &stmt->goto_expression);
2190 add_statement(&function_computed_goto_list, stmt);
2191 } else if (token_type(token) == TOKEN_IDENT) {
2192 stmt->goto_label = label_symbol(token);
2193 token = token->next;
2194 } else {
2195 sparse_error(token->pos, "Expected identifier or goto expression");
2197 return expect(token, ';', "at end of statement");
2200 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2202 stmt->type = STMT_CONTEXT;
2203 token = parse_expression(token->next, &stmt->expression);
2204 if(stmt->expression->type == EXPR_PREOP
2205 && stmt->expression->op == '('
2206 && stmt->expression->unop->type == EXPR_COMMA) {
2207 struct expression *expr;
2208 expr = stmt->expression->unop;
2209 stmt->context = expr->left;
2210 stmt->expression = expr->right;
2212 return expect(token, ';', "at end of statement");
2215 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2217 stmt->type = STMT_RANGE;
2218 token = assignment_expression(token->next, &stmt->range_expression);
2219 token = expect(token, ',', "after range expression");
2220 token = assignment_expression(token, &stmt->range_low);
2221 token = expect(token, ',', "after low range");
2222 token = assignment_expression(token, &stmt->range_high);
2223 return expect(token, ';', "after range statement");
2226 static struct token *statement(struct token *token, struct statement **tree)
2228 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2230 *tree = stmt;
2231 if (token_type(token) == TOKEN_IDENT) {
2232 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2233 if (s && s->op->statement)
2234 return s->op->statement(token, stmt);
2236 if (match_op(token->next, ':')) {
2237 stmt->type = STMT_LABEL;
2238 stmt->label_identifier = label_symbol(token);
2239 token = skip_attributes(token->next->next);
2240 return statement(token, &stmt->label_statement);
2244 if (match_op(token, '{')) {
2245 stmt->type = STMT_COMPOUND;
2246 start_symbol_scope();
2247 token = compound_statement(token->next, stmt);
2248 end_symbol_scope();
2250 return expect(token, '}', "at end of compound statement");
2253 stmt->type = STMT_EXPRESSION;
2254 return expression_statement(token, &stmt->expression);
2257 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2258 static struct token *label_statement(struct token *token)
2260 while (token_type(token) == TOKEN_IDENT) {
2261 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2262 /* it's block-scope, but we want label namespace */
2263 bind_symbol(sym, token->ident, NS_SYMBOL);
2264 sym->namespace = NS_LABEL;
2265 fn_local_symbol(sym);
2266 token = token->next;
2267 if (!match_op(token, ','))
2268 break;
2269 token = token->next;
2271 return expect(token, ';', "at end of label declaration");
2274 static struct token * statement_list(struct token *token, struct statement_list **list)
2276 int seen_statement = 0;
2277 while (token_type(token) == TOKEN_IDENT &&
2278 token->ident == &__label___ident)
2279 token = label_statement(token->next);
2280 for (;;) {
2281 struct statement * stmt;
2282 if (eof_token(token))
2283 break;
2284 if (match_op(token, '}'))
2285 break;
2286 if (lookup_type(token)) {
2287 if (seen_statement) {
2288 warning(token->pos, "mixing declarations and code");
2289 seen_statement = 0;
2291 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2292 token = external_declaration(token, &stmt->declaration);
2293 } else {
2294 seen_statement = Wdeclarationafterstatement;
2295 token = statement(token, &stmt);
2297 add_statement(list, stmt);
2299 return token;
2302 static struct token *identifier_list(struct token *token, struct symbol *fn)
2304 struct symbol_list **list = &fn->arguments;
2305 for (;;) {
2306 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2307 sym->ident = token->ident;
2308 token = token->next;
2309 sym->endpos = token->pos;
2310 sym->ctype.base_type = &incomplete_ctype;
2311 add_symbol(list, sym);
2312 if (!match_op(token, ',') ||
2313 token_type(token->next) != TOKEN_IDENT ||
2314 lookup_type(token->next))
2315 break;
2316 token = token->next;
2318 return token;
2321 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2323 struct symbol_list **list = &fn->arguments;
2325 for (;;) {
2326 struct symbol *sym;
2328 if (match_op(token, SPECIAL_ELLIPSIS)) {
2329 fn->variadic = 1;
2330 token = token->next;
2331 break;
2334 sym = alloc_symbol(token->pos, SYM_NODE);
2335 token = parameter_declaration(token, sym);
2336 if (sym->ctype.base_type == &void_ctype) {
2337 /* Special case: (void) */
2338 if (!*list && !sym->ident)
2339 break;
2340 warning(token->pos, "void parameter");
2342 add_symbol(list, sym);
2343 if (!match_op(token, ','))
2344 break;
2345 token = token->next;
2347 return token;
2350 struct token *compound_statement(struct token *token, struct statement *stmt)
2352 token = statement_list(token, &stmt->stmts);
2353 return token;
2356 static struct expression *identifier_expression(struct token *token)
2358 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2359 expr->expr_ident = token->ident;
2360 return expr;
2363 static struct expression *index_expression(struct expression *from, struct expression *to)
2365 int idx_from, idx_to;
2366 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2368 idx_from = const_expression_value(from);
2369 idx_to = idx_from;
2370 if (to) {
2371 idx_to = const_expression_value(to);
2372 if (idx_to < idx_from || idx_from < 0)
2373 warning(from->pos, "nonsense array initializer index range");
2375 expr->idx_from = idx_from;
2376 expr->idx_to = idx_to;
2377 return expr;
2380 static struct token *single_initializer(struct expression **ep, struct token *token)
2382 int expect_equal = 0;
2383 struct token *next = token->next;
2384 struct expression **tail = ep;
2385 int nested;
2387 *ep = NULL;
2389 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2390 struct expression *expr = identifier_expression(token);
2391 if (Wold_initializer)
2392 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2393 token = initializer(&expr->ident_expression, next->next);
2394 if (expr->ident_expression)
2395 *ep = expr;
2396 return token;
2399 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2400 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2401 struct expression *expr = identifier_expression(next);
2402 *tail = expr;
2403 tail = &expr->ident_expression;
2404 expect_equal = 1;
2405 token = next->next;
2406 } else if (match_op(token, '[')) {
2407 struct expression *from = NULL, *to = NULL, *expr;
2408 token = constant_expression(token->next, &from);
2409 if (!from) {
2410 sparse_error(token->pos, "Expected constant expression");
2411 break;
2413 if (match_op(token, SPECIAL_ELLIPSIS))
2414 token = constant_expression(token->next, &to);
2415 expr = index_expression(from, to);
2416 *tail = expr;
2417 tail = &expr->idx_expression;
2418 token = expect(token, ']', "at end of initializer index");
2419 if (nested)
2420 expect_equal = 1;
2421 } else {
2422 break;
2425 if (nested && !expect_equal) {
2426 if (!match_op(token, '='))
2427 warning(token->pos, "obsolete array initializer, use C99 syntax");
2428 else
2429 expect_equal = 1;
2431 if (expect_equal)
2432 token = expect(token, '=', "at end of initializer index");
2434 token = initializer(tail, token);
2435 if (!*tail)
2436 *ep = NULL;
2437 return token;
2440 static struct token *initializer_list(struct expression_list **list, struct token *token)
2442 struct expression *expr;
2444 for (;;) {
2445 token = single_initializer(&expr, token);
2446 if (!expr)
2447 break;
2448 add_expression(list, expr);
2449 if (!match_op(token, ','))
2450 break;
2451 token = token->next;
2453 return token;
2456 struct token *initializer(struct expression **tree, struct token *token)
2458 if (match_op(token, '{')) {
2459 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2460 *tree = expr;
2461 token = initializer_list(&expr->expr_list, token->next);
2462 return expect(token, '}', "at end of initializer");
2464 return assignment_expression(token, tree);
2467 static void declare_argument(struct symbol *sym, struct symbol *fn)
2469 if (!sym->ident) {
2470 sparse_error(sym->pos, "no identifier for function argument");
2471 return;
2473 bind_symbol(sym, sym->ident, NS_SYMBOL);
2476 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2477 struct symbol_list **list)
2479 struct symbol_list **old_symbol_list;
2480 struct symbol *base_type = decl->ctype.base_type;
2481 struct statement *stmt, **p;
2482 struct symbol *prev;
2483 struct symbol *arg;
2485 old_symbol_list = function_symbol_list;
2486 if (decl->ctype.modifiers & MOD_INLINE) {
2487 function_symbol_list = &decl->inline_symbol_list;
2488 p = &base_type->inline_stmt;
2489 } else {
2490 function_symbol_list = &decl->symbol_list;
2491 p = &base_type->stmt;
2493 function_computed_target_list = NULL;
2494 function_computed_goto_list = NULL;
2496 if (decl->ctype.modifiers & MOD_EXTERN) {
2497 if (!(decl->ctype.modifiers & MOD_INLINE))
2498 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2500 if (!(decl->ctype.modifiers & MOD_STATIC))
2501 decl->ctype.modifiers |= MOD_EXTERN;
2503 stmt = start_function(decl);
2505 *p = stmt;
2506 FOR_EACH_PTR (base_type->arguments, arg) {
2507 declare_argument(arg, base_type);
2508 } END_FOR_EACH_PTR(arg);
2510 token = compound_statement(token->next, stmt);
2512 end_function(decl);
2513 if (!(decl->ctype.modifiers & MOD_INLINE))
2514 add_symbol(list, decl);
2515 check_declaration(decl);
2516 decl->definition = decl;
2517 prev = decl->same_symbol;
2518 if (prev && prev->definition) {
2519 warning(decl->pos, "multiple definitions for function '%s'",
2520 show_ident(decl->ident));
2521 info(prev->definition->pos, " the previous one is here");
2522 } else {
2523 while (prev) {
2524 prev->definition = decl;
2525 prev = prev->same_symbol;
2528 function_symbol_list = old_symbol_list;
2529 if (function_computed_goto_list) {
2530 if (!function_computed_target_list)
2531 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2532 else {
2533 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2534 stmt->target_list = function_computed_target_list;
2535 } END_FOR_EACH_PTR(stmt);
2538 return expect(token, '}', "at end of function");
2541 static void promote_k_r_types(struct symbol *arg)
2543 struct symbol *base = arg->ctype.base_type;
2544 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2545 arg->ctype.base_type = &int_ctype;
2549 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2551 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2552 struct symbol *arg;
2554 FOR_EACH_PTR(real_args, arg) {
2555 struct symbol *type;
2557 /* This is quadratic in the number of arguments. We _really_ don't care */
2558 FOR_EACH_PTR(argtypes, type) {
2559 if (type->ident == arg->ident)
2560 goto match;
2561 } END_FOR_EACH_PTR(type);
2562 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2563 continue;
2564 match:
2565 type->used = 1;
2566 /* "char" and "short" promote to "int" */
2567 promote_k_r_types(type);
2569 arg->ctype = type->ctype;
2570 } END_FOR_EACH_PTR(arg);
2572 FOR_EACH_PTR(argtypes, arg) {
2573 if (!arg->used)
2574 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2575 } END_FOR_EACH_PTR(arg);
2579 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2580 struct symbol_list **list)
2582 struct symbol_list *args = NULL;
2584 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2585 do {
2586 token = declaration_list(token, &args);
2587 if (!match_op(token, ';')) {
2588 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2589 break;
2591 token = token->next;
2592 } while (lookup_type(token));
2594 apply_k_r_types(args, decl);
2596 if (!match_op(token, '{')) {
2597 sparse_error(token->pos, "expected function body");
2598 return token;
2600 return parse_function_body(token, decl, list);
2603 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2605 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2606 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2607 struct statement *stmt;
2609 anon->ctype.base_type = fn;
2610 stmt = alloc_statement(token->pos, STMT_NONE);
2611 fn->stmt = stmt;
2613 token = parse_asm_statement(token, stmt);
2615 add_symbol(list, anon);
2616 return token;
2619 struct token *external_declaration(struct token *token, struct symbol_list **list)
2621 struct ident *ident = NULL;
2622 struct symbol *decl;
2623 struct decl_state ctx = { .ident = &ident };
2624 struct ctype saved;
2625 struct symbol *base_type;
2626 unsigned long mod;
2627 int is_typedef;
2629 /* Top-level inline asm? */
2630 if (token_type(token) == TOKEN_IDENT) {
2631 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2632 if (s && s->op->toplevel)
2633 return s->op->toplevel(token, list);
2636 /* Parse declaration-specifiers, if any */
2637 token = declaration_specifiers(token, &ctx);
2638 mod = storage_modifiers(&ctx);
2639 decl = alloc_symbol(token->pos, SYM_NODE);
2640 /* Just a type declaration? */
2641 if (match_op(token, ';')) {
2642 apply_modifiers(token->pos, &ctx);
2643 return token->next;
2646 saved = ctx.ctype;
2647 token = declarator(token, &ctx);
2648 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2649 apply_modifiers(token->pos, &ctx);
2651 decl->ctype = ctx.ctype;
2652 decl->ctype.modifiers |= mod;
2653 decl->endpos = token->pos;
2655 /* Just a type declaration? */
2656 if (!ident) {
2657 warning(token->pos, "missing identifier in declaration");
2658 return expect(token, ';', "at the end of type declaration");
2661 /* type define declaration? */
2662 is_typedef = ctx.storage_class == STypedef;
2664 /* Typedefs don't have meaningful storage */
2665 if (is_typedef)
2666 decl->ctype.modifiers |= MOD_USERTYPE;
2668 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2670 base_type = decl->ctype.base_type;
2672 if (is_typedef) {
2673 if (base_type && !base_type->ident) {
2674 switch (base_type->type) {
2675 case SYM_STRUCT:
2676 case SYM_UNION:
2677 case SYM_ENUM:
2678 case SYM_RESTRICT:
2679 base_type->ident = ident;
2682 } else if (base_type && base_type->type == SYM_FN) {
2683 /* K&R argument declaration? */
2684 if (lookup_type(token))
2685 return parse_k_r_arguments(token, decl, list);
2686 if (match_op(token, '{'))
2687 return parse_function_body(token, decl, list);
2689 if (!(decl->ctype.modifiers & MOD_STATIC))
2690 decl->ctype.modifiers |= MOD_EXTERN;
2691 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2692 sparse_error(token->pos, "void declaration");
2695 for (;;) {
2696 if (!is_typedef && match_op(token, '=')) {
2697 if (decl->ctype.modifiers & MOD_EXTERN) {
2698 warning(decl->pos, "symbol with external linkage has initializer");
2699 decl->ctype.modifiers &= ~MOD_EXTERN;
2701 token = initializer(&decl->initializer, token->next);
2703 if (!is_typedef) {
2704 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2705 add_symbol(list, decl);
2706 fn_local_symbol(decl);
2709 check_declaration(decl);
2710 if (decl->same_symbol)
2711 decl->definition = decl->same_symbol->definition;
2713 if (!match_op(token, ','))
2714 break;
2716 token = token->next;
2717 ident = NULL;
2718 decl = alloc_symbol(token->pos, SYM_NODE);
2719 ctx.ctype = saved;
2720 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2721 token = declarator(token, &ctx);
2722 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2723 apply_modifiers(token->pos, &ctx);
2724 decl->ctype = ctx.ctype;
2725 decl->ctype.modifiers |= mod;
2726 decl->endpos = token->pos;
2727 if (!ident) {
2728 sparse_error(token->pos, "expected identifier name in type definition");
2729 return token;
2732 if (is_typedef)
2733 decl->ctype.modifiers |= MOD_USERTYPE;
2735 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2737 /* Function declarations are automatically extern unless specifically static */
2738 base_type = decl->ctype.base_type;
2739 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2740 if (!(decl->ctype.modifiers & MOD_STATIC))
2741 decl->ctype.modifiers |= MOD_EXTERN;
2744 return expect(token, ';', "at end of declaration");