param_limit/filter: ignore whole ranges
[smatch.git] / parse.c
blob38b2f7856deaf37df9594874de2245f9feb350f6
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},
422 { "__restrict__", NS_TYPEDEF, .op = &restrict_op},
424 /* Storage class */
425 { "auto", NS_TYPEDEF, .op = &auto_op },
426 { "register", NS_TYPEDEF, .op = &register_op },
427 { "static", NS_TYPEDEF, .op = &static_op },
428 { "extern", NS_TYPEDEF, .op = &extern_op },
429 { "__thread", NS_TYPEDEF, .op = &thread_op },
431 /* Statement */
432 { "if", NS_KEYWORD, .op = &if_op },
433 { "return", NS_KEYWORD, .op = &return_op },
434 { "break", NS_KEYWORD, .op = &loop_iter_op },
435 { "continue", NS_KEYWORD, .op = &loop_iter_op },
436 { "default", NS_KEYWORD, .op = &default_op },
437 { "case", NS_KEYWORD, .op = &case_op },
438 { "switch", NS_KEYWORD, .op = &switch_op },
439 { "for", NS_KEYWORD, .op = &for_op },
440 { "while", NS_KEYWORD, .op = &while_op },
441 { "do", NS_KEYWORD, .op = &do_op },
442 { "goto", NS_KEYWORD, .op = &goto_op },
443 { "__context__",NS_KEYWORD, .op = &__context___op },
444 { "__range__", NS_KEYWORD, .op = &range_op },
445 { "asm", NS_KEYWORD, .op = &asm_op },
446 { "__asm", NS_KEYWORD, .op = &asm_op },
447 { "__asm__", NS_KEYWORD, .op = &asm_op },
449 /* Attribute */
450 { "packed", NS_KEYWORD, .op = &packed_op },
451 { "__packed__", NS_KEYWORD, .op = &packed_op },
452 { "aligned", NS_KEYWORD, .op = &aligned_op },
453 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
454 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
455 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
456 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
457 { "force", NS_KEYWORD, .op = &attr_force_op },
458 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
459 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
460 { "address_space",NS_KEYWORD, .op = &address_space_op },
461 { "mode", NS_KEYWORD, .op = &mode_op },
462 { "context", NS_KEYWORD, .op = &context_op },
463 { "designated_init", NS_KEYWORD, .op = &designated_init_op },
464 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
465 { "noreturn", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
466 { "__noreturn__", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
467 { "pure", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
468 {"__pure__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
469 {"const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
470 {"__const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
471 {"__const__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
473 { "__mode__", NS_KEYWORD, .op = &mode_op },
474 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
475 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
476 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
477 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
478 { "SI", NS_KEYWORD, .op = &mode_SI_op },
479 { "__SI__", NS_KEYWORD, .op = &mode_SI_op },
480 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
481 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
482 { "TI", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
483 { "__TI__", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
484 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
485 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
488 const char *ignored_attributes[] = {
489 "alias",
490 "__alias__",
491 "alloc_size",
492 "__alloc_size__",
493 "always_inline",
494 "__always_inline__",
495 "artificial",
496 "__artificial__",
497 "bounded",
498 "__bounded__",
499 "cdecl",
500 "__cdecl__",
501 "cold",
502 "__cold__",
503 "constructor",
504 "__constructor__",
505 "deprecated",
506 "__deprecated__",
507 "destructor",
508 "__destructor__",
509 "dllexport",
510 "__dllexport__",
511 "dllimport",
512 "__dllimport__",
513 "error",
514 "__error__",
515 "externally_visible",
516 "__externally_visible__",
517 "fastcall",
518 "__fastcall__",
519 "format",
520 "__format__",
521 "format_arg",
522 "__format_arg__",
523 "hot",
524 "__hot__",
525 "leaf",
526 "__leaf__",
527 "l1_text",
528 "__l1_text__",
529 "l1_data",
530 "__l1_data__",
531 "l2",
532 "__l2__",
533 "may_alias",
534 "__may_alias__",
535 "malloc",
536 "__malloc__",
537 "may_alias",
538 "__may_alias__",
539 "model",
540 "__model__",
541 "ms_abi",
542 "__ms_abi__",
543 "ms_hook_prologue",
544 "__ms_hook_prologue__",
545 "naked",
546 "__naked__",
547 "no_instrument_function",
548 "__no_instrument_function__",
549 "noclone",
550 "__noclone",
551 "__noclone__",
552 "noinline",
553 "__noinline__",
554 "nonnull",
555 "__nonnull",
556 "__nonnull__",
557 "nothrow",
558 "__nothrow",
559 "__nothrow__",
560 "regparm",
561 "__regparm__",
562 "section",
563 "__section__",
564 "sentinel",
565 "__sentinel__",
566 "signal",
567 "__signal__",
568 "stdcall",
569 "__stdcall__",
570 "syscall_linkage",
571 "__syscall_linkage__",
572 "sysv_abi",
573 "__sysv_abi__",
574 "unused",
575 "__unused__",
576 "used",
577 "__used__",
578 "vector_size",
579 "__vector_size__",
580 "visibility",
581 "__visibility__",
582 "warn_unused_result",
583 "__warn_unused_result__",
584 "warning",
585 "__warning__",
586 "weak",
587 "__weak__",
591 void init_parser(int stream)
593 int i;
594 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
595 struct init_keyword *ptr = keyword_table + i;
596 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
597 sym->ident->keyword = 1;
598 if (ptr->ns == NS_TYPEDEF)
599 sym->ident->reserved = 1;
600 sym->ctype.modifiers = ptr->modifiers;
601 sym->ctype.base_type = ptr->type;
602 sym->op = ptr->op;
605 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
606 const char * name = ignored_attributes[i];
607 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
608 NS_KEYWORD);
609 sym->ident->keyword = 1;
610 sym->op = &ignore_attr_op;
615 // Add a symbol to the list of function-local symbols
616 static void fn_local_symbol(struct symbol *sym)
618 if (function_symbol_list)
619 add_symbol(function_symbol_list, sym);
622 static int SENTINEL_ATTR match_idents(struct token *token, ...)
624 va_list args;
625 struct ident * next;
627 if (token_type(token) != TOKEN_IDENT)
628 return 0;
630 va_start(args, token);
631 do {
632 next = va_arg(args, struct ident *);
633 } while (next && token->ident != next);
634 va_end(args);
636 return next && token->ident == next;
640 struct statement *alloc_statement(struct position pos, int type)
642 struct statement *stmt = __alloc_statement(0);
643 stmt->type = type;
644 stmt->pos = pos;
645 return stmt;
648 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
650 static void apply_modifiers(struct position pos, struct decl_state *ctx)
652 struct symbol *ctype;
653 if (!ctx->mode)
654 return;
655 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
656 if (!ctype)
657 sparse_error(pos, "don't know how to apply mode to %s",
658 show_typename(ctx->ctype.base_type));
659 else
660 ctx->ctype.base_type = ctype;
664 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
666 struct symbol *sym = alloc_symbol(pos, type);
668 sym->ctype.base_type = ctype->base_type;
669 sym->ctype.modifiers = ctype->modifiers;
671 ctype->base_type = sym;
672 ctype->modifiers = 0;
673 return sym;
677 * NOTE! NS_LABEL is not just a different namespace,
678 * it also ends up using function scope instead of the
679 * regular symbol scope.
681 struct symbol *label_symbol(struct token *token)
683 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
684 if (!sym) {
685 sym = alloc_symbol(token->pos, SYM_LABEL);
686 bind_symbol(sym, token->ident, NS_LABEL);
687 fn_local_symbol(sym);
689 return sym;
692 static struct token *struct_union_enum_specifier(enum type type,
693 struct token *token, struct decl_state *ctx,
694 struct token *(*parse)(struct token *, struct symbol *))
696 struct symbol *sym;
697 struct position *repos;
699 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
700 if (token_type(token) == TOKEN_IDENT) {
701 sym = lookup_symbol(token->ident, NS_STRUCT);
702 if (!sym ||
703 (is_outer_scope(sym->scope) &&
704 (match_op(token->next,';') || match_op(token->next,'{')))) {
705 // Either a new symbol, or else an out-of-scope
706 // symbol being redefined.
707 sym = alloc_symbol(token->pos, type);
708 bind_symbol(sym, token->ident, NS_STRUCT);
710 if (sym->type != type)
711 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
712 ctx->ctype.base_type = sym;
713 repos = &token->pos;
714 token = token->next;
715 if (match_op(token, '{')) {
716 // The following test is actually wrong for empty
717 // structs, but (1) they are not C99, (2) gcc does
718 // the same thing, and (3) it's easier.
719 if (sym->symbol_list)
720 error_die(token->pos, "redefinition of %s", show_typename (sym));
721 sym->pos = *repos;
722 token = parse(token->next, sym);
723 token = expect(token, '}', "at end of struct-union-enum-specifier");
725 // Mark the structure as needing re-examination
726 sym->examined = 0;
727 sym->endpos = token->pos;
729 return token;
732 // private struct/union/enum type
733 if (!match_op(token, '{')) {
734 sparse_error(token->pos, "expected declaration");
735 ctx->ctype.base_type = &bad_ctype;
736 return token;
739 sym = alloc_symbol(token->pos, type);
740 token = parse(token->next, sym);
741 ctx->ctype.base_type = sym;
742 token = expect(token, '}', "at end of specifier");
743 sym->endpos = token->pos;
745 return token;
748 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
750 struct symbol *field, *last = NULL;
751 struct token *res;
752 res = struct_declaration_list(token, &sym->symbol_list);
753 FOR_EACH_PTR(sym->symbol_list, field) {
754 if (!field->ident) {
755 struct symbol *base = field->ctype.base_type;
756 if (base && base->type == SYM_BITFIELD)
757 continue;
759 if (last)
760 last->next_subobject = field;
761 last = field;
762 } END_FOR_EACH_PTR(field);
763 return res;
766 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
768 return struct_declaration_list(token, &sym->symbol_list);
771 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
773 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
776 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
778 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
782 typedef struct {
783 int x;
784 unsigned long long y;
785 } Num;
787 static void upper_boundary(Num *n, Num *v)
789 if (n->x > v->x)
790 return;
791 if (n->x < v->x) {
792 *n = *v;
793 return;
795 if (n->y < v->y)
796 n->y = v->y;
799 static void lower_boundary(Num *n, Num *v)
801 if (n->x < v->x)
802 return;
803 if (n->x > v->x) {
804 *n = *v;
805 return;
807 if (n->y > v->y)
808 n->y = v->y;
811 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
813 int shift = type->bit_size;
814 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
816 if (!is_unsigned)
817 shift--;
818 if (upper->x == 0 && upper->y >> shift)
819 return 0;
820 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
821 return 1;
822 return 0;
825 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
827 if (s1->bit_size < s2->bit_size) {
828 s1 = s2;
829 } else if (s1->bit_size == s2->bit_size) {
830 if (s2->ctype.modifiers & MOD_UNSIGNED)
831 s1 = s2;
833 if (s1->bit_size < bits_in_int)
834 return &int_ctype;
835 return s1;
838 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
840 struct symbol *sym;
842 FOR_EACH_PTR(list, sym) {
843 struct expression *expr = sym->initializer;
844 struct symbol *ctype;
845 if (expr->type != EXPR_VALUE)
846 continue;
847 ctype = expr->ctype;
848 if (ctype->bit_size == base_type->bit_size)
849 continue;
850 cast_value(expr, base_type, expr, ctype);
851 } END_FOR_EACH_PTR(sym);
854 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
856 unsigned long long lastval = 0;
857 struct symbol *ctype = NULL, *base_type = NULL;
858 Num upper = {-1, 0}, lower = {1, 0};
860 parent->examined = 1;
861 parent->ctype.base_type = &int_ctype;
862 while (token_type(token) == TOKEN_IDENT) {
863 struct expression *expr = NULL;
864 struct token *next = token->next;
865 struct symbol *sym;
867 if (match_op(next, '=')) {
868 next = constant_expression(next->next, &expr);
869 lastval = get_expression_value(expr);
870 ctype = &void_ctype;
871 if (expr && expr->ctype)
872 ctype = expr->ctype;
873 } else if (!ctype) {
874 ctype = &int_ctype;
875 } else if (is_int_type(ctype)) {
876 lastval++;
877 } else {
878 error_die(token->pos, "can't increment the last enum member");
881 if (!expr) {
882 expr = alloc_expression(token->pos, EXPR_VALUE);
883 expr->value = lastval;
884 expr->ctype = ctype;
887 sym = alloc_symbol(token->pos, SYM_NODE);
888 bind_symbol(sym, token->ident, NS_SYMBOL);
889 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
890 sym->initializer = expr;
891 sym->enum_member = 1;
892 sym->ctype.base_type = parent;
893 add_ptr_list(&parent->symbol_list, sym);
895 if (base_type != &bad_ctype) {
896 if (ctype->type == SYM_NODE)
897 ctype = ctype->ctype.base_type;
898 if (ctype->type == SYM_ENUM) {
899 if (ctype == parent)
900 ctype = base_type;
901 else
902 ctype = ctype->ctype.base_type;
905 * base_type rules:
906 * - if all enums are of the same type, then
907 * the base_type is that type (two first
908 * cases)
909 * - if enums are of different types, they
910 * all have to be integer types, and the
911 * base type is at least "int_ctype".
912 * - otherwise the base_type is "bad_ctype".
914 if (!base_type) {
915 base_type = ctype;
916 } else if (ctype == base_type) {
917 /* nothing */
918 } else if (is_int_type(base_type) && is_int_type(ctype)) {
919 base_type = bigger_enum_type(base_type, ctype);
920 } else
921 base_type = &bad_ctype;
922 parent->ctype.base_type = base_type;
924 if (is_int_type(base_type)) {
925 Num v = {.y = lastval};
926 if (ctype->ctype.modifiers & MOD_UNSIGNED)
927 v.x = 0;
928 else if ((long long)lastval >= 0)
929 v.x = 0;
930 else
931 v.x = -1;
932 upper_boundary(&upper, &v);
933 lower_boundary(&lower, &v);
935 token = next;
937 sym->endpos = token->pos;
939 if (!match_op(token, ','))
940 break;
941 token = token->next;
943 if (!base_type) {
944 sparse_error(token->pos, "bad enum definition");
945 base_type = &bad_ctype;
947 else if (!is_int_type(base_type))
948 base_type = base_type;
949 else if (type_is_ok(base_type, &upper, &lower))
950 base_type = base_type;
951 else if (type_is_ok(&int_ctype, &upper, &lower))
952 base_type = &int_ctype;
953 else if (type_is_ok(&uint_ctype, &upper, &lower))
954 base_type = &uint_ctype;
955 else if (type_is_ok(&long_ctype, &upper, &lower))
956 base_type = &long_ctype;
957 else if (type_is_ok(&ulong_ctype, &upper, &lower))
958 base_type = &ulong_ctype;
959 else if (type_is_ok(&llong_ctype, &upper, &lower))
960 base_type = &llong_ctype;
961 else if (type_is_ok(&ullong_ctype, &upper, &lower))
962 base_type = &ullong_ctype;
963 else
964 base_type = &bad_ctype;
965 parent->ctype.base_type = base_type;
966 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
967 parent->examined = 0;
969 cast_enum_list(parent->symbol_list, base_type);
971 return token;
974 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
976 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
977 struct ctype *ctype = &ctx->ctype.base_type->ctype;
979 if (!ctype->base_type)
980 ctype->base_type = &incomplete_ctype;
982 return ret;
985 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
987 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
989 struct symbol *sym;
991 if (!match_op(token, '(')) {
992 sparse_error(token->pos, "expected '(' after typeof");
993 return token;
995 if (lookup_type(token->next)) {
996 token = typename(token->next, &sym, NULL);
997 ctx->ctype.base_type = sym->ctype.base_type;
998 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
999 } else {
1000 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
1001 token = parse_expression(token->next, &typeof_sym->initializer);
1003 typeof_sym->endpos = token->pos;
1004 if (!typeof_sym->initializer) {
1005 sparse_error(token->pos, "expected expression after the '(' token");
1006 typeof_sym = &bad_ctype;
1008 ctx->ctype.base_type = typeof_sym;
1010 return expect(token, ')', "after typeof");
1013 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1015 struct expression *expr = NULL;
1016 if (match_op(token, '('))
1017 token = parens_expression(token, &expr, "in attribute");
1018 return token;
1021 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1023 if (!ctx->ctype.alignment)
1024 ctx->ctype.alignment = 1;
1025 return token;
1028 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1030 int alignment = max_alignment;
1031 struct expression *expr = NULL;
1033 if (match_op(token, '(')) {
1034 token = parens_expression(token, &expr, "in attribute");
1035 if (expr)
1036 alignment = const_expression_value(expr);
1038 if (alignment & (alignment-1)) {
1039 warning(token->pos, "I don't like non-power-of-2 alignments");
1040 return token;
1041 } else if (alignment > ctx->ctype.alignment)
1042 ctx->ctype.alignment = alignment;
1043 return token;
1046 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1048 if (ctx->modifiers & qual)
1049 warning(*pos, "duplicate %s", modifier_string(qual));
1050 ctx->modifiers |= qual;
1053 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1055 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1056 return token;
1059 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1061 struct expression *expr = NULL;
1062 int as;
1063 token = expect(token, '(', "after address_space attribute");
1064 token = conditional_expression(token, &expr);
1065 if (expr) {
1066 as = const_expression_value(expr);
1067 if (Waddress_space && as)
1068 attr_set_as(&ctx->ctype, as);
1070 token = expect(token, ')', "after address_space attribute");
1071 return token;
1074 static struct symbol *to_QI_mode(struct symbol *ctype)
1076 if (ctype->ctype.base_type != &int_type)
1077 return NULL;
1078 if (ctype == &char_ctype)
1079 return ctype;
1080 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1081 : &schar_ctype;
1084 static struct symbol *to_HI_mode(struct symbol *ctype)
1086 if (ctype->ctype.base_type != &int_type)
1087 return NULL;
1088 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1089 : &sshort_ctype;
1092 static struct symbol *to_SI_mode(struct symbol *ctype)
1094 if (ctype->ctype.base_type != &int_type)
1095 return NULL;
1096 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1097 : &sint_ctype;
1100 static struct symbol *to_DI_mode(struct symbol *ctype)
1102 if (ctype->ctype.base_type != &int_type)
1103 return NULL;
1104 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1105 : &sllong_ctype;
1108 static struct symbol *to_TI_mode(struct symbol *ctype)
1110 if (ctype->ctype.base_type != &int_type)
1111 return NULL;
1112 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1113 : &slllong_ctype;
1116 static struct symbol *to_word_mode(struct symbol *ctype)
1118 if (ctype->ctype.base_type != &int_type)
1119 return NULL;
1120 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1121 : &slong_ctype;
1124 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1126 token = expect(token, '(', "after mode attribute");
1127 if (token_type(token) == TOKEN_IDENT) {
1128 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1129 if (mode && mode->op->type == KW_MODE)
1130 ctx->mode = mode->op;
1131 else
1132 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1133 token = token->next;
1134 } else
1135 sparse_error(token->pos, "expect attribute mode symbol\n");
1136 token = expect(token, ')', "after mode attribute");
1137 return token;
1140 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1142 struct context *context = alloc_context();
1143 struct expression *args[3];
1144 int argc = 0;
1146 token = expect(token, '(', "after context attribute");
1147 while (!match_op(token, ')')) {
1148 struct expression *expr = NULL;
1149 token = conditional_expression(token, &expr);
1150 if (!expr)
1151 break;
1152 if (argc < 3)
1153 args[argc++] = expr;
1154 if (!match_op(token, ','))
1155 break;
1156 token = token->next;
1159 switch(argc) {
1160 case 0:
1161 sparse_error(token->pos, "expected context input/output values");
1162 break;
1163 case 1:
1164 context->in = get_expression_value(args[0]);
1165 break;
1166 case 2:
1167 context->in = get_expression_value(args[0]);
1168 context->out = get_expression_value(args[1]);
1169 break;
1170 case 3:
1171 context->context = args[0];
1172 context->in = get_expression_value(args[1]);
1173 context->out = get_expression_value(args[2]);
1174 break;
1177 if (argc)
1178 attr_add_context(&ctx->ctype, context);
1180 token = expect(token, ')', "after context attribute");
1181 return token;
1184 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1186 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1187 ctx->ctype.base_type->designated_init = 1;
1188 else
1189 warning(token->pos, "attribute designated_init applied to non-structure type");
1190 return token;
1193 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1195 if (Wtransparent_union)
1196 warning(token->pos, "ignoring attribute __transparent_union__");
1197 return token;
1200 static struct token *recover_unknown_attribute(struct token *token)
1202 struct expression *expr = NULL;
1204 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1205 token = token->next;
1206 if (match_op(token, '('))
1207 token = parens_expression(token, &expr, "in attribute");
1208 return token;
1211 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1213 token = expect(token, '(', "after attribute");
1214 token = expect(token, '(', "after attribute");
1216 for (;;) {
1217 struct ident *attribute_name;
1218 struct symbol *attr;
1220 if (eof_token(token))
1221 break;
1222 if (match_op(token, ';'))
1223 break;
1224 if (token_type(token) != TOKEN_IDENT)
1225 break;
1226 attribute_name = token->ident;
1227 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1228 if (attr && attr->op->attribute)
1229 token = attr->op->attribute(token->next, attr, ctx);
1230 else
1231 token = recover_unknown_attribute(token);
1233 if (!match_op(token, ','))
1234 break;
1235 token = token->next;
1238 token = expect(token, ')', "after attribute");
1239 token = expect(token, ')', "after attribute");
1240 return token;
1243 static const char *storage_class[] =
1245 [STypedef] = "typedef",
1246 [SAuto] = "auto",
1247 [SExtern] = "extern",
1248 [SStatic] = "static",
1249 [SRegister] = "register",
1250 [SForced] = "[force]"
1253 static unsigned long storage_modifiers(struct decl_state *ctx)
1255 static unsigned long mod[] =
1257 [SAuto] = MOD_AUTO,
1258 [SExtern] = MOD_EXTERN,
1259 [SStatic] = MOD_STATIC,
1260 [SRegister] = MOD_REGISTER
1262 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1263 | (ctx->is_tls ? MOD_TLS : 0);
1266 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1268 /* __thread can be used alone, or with extern or static */
1269 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1270 sparse_error(*pos, "__thread can only be used alone, or with "
1271 "extern or static");
1272 return;
1275 if (!ctx->storage_class) {
1276 ctx->storage_class = class;
1277 return;
1279 if (ctx->storage_class == class)
1280 sparse_error(*pos, "duplicate %s", storage_class[class]);
1281 else
1282 sparse_error(*pos, "multiple storage classes");
1285 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1287 set_storage_class(&next->pos, ctx, STypedef);
1288 return next;
1291 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1293 set_storage_class(&next->pos, ctx, SAuto);
1294 return next;
1297 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1299 set_storage_class(&next->pos, ctx, SRegister);
1300 return next;
1303 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1305 set_storage_class(&next->pos, ctx, SStatic);
1306 return next;
1309 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1311 set_storage_class(&next->pos, ctx, SExtern);
1312 return next;
1315 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1317 /* This GCC extension can be used alone, or with extern or static */
1318 if (!ctx->storage_class || ctx->storage_class == SStatic
1319 || ctx->storage_class == SExtern) {
1320 ctx->is_tls = 1;
1321 } else {
1322 sparse_error(next->pos, "__thread can only be used alone, or "
1323 "with extern or static");
1326 return next;
1329 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1331 set_storage_class(&token->pos, ctx, SForced);
1332 return token;
1335 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1337 ctx->is_inline = 1;
1338 return next;
1341 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1343 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1344 return next;
1347 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1349 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1350 return next;
1353 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1355 unsigned long mod = thistype->modifiers;
1357 if (mod)
1358 apply_qualifier(&pos, ctype, mod);
1360 /* Alignment */
1361 if (thistype->alignment > ctype->alignment)
1362 ctype->alignment = thistype->alignment;
1364 /* Attribute */
1365 merge_attr(ctype, thistype);
1368 static void specifier_conflict(struct position pos, int what, struct ident *new)
1370 const char *old;
1371 if (what & (Set_S | Set_T))
1372 goto Catch_all;
1373 if (what & Set_Char)
1374 old = "char";
1375 else if (what & Set_Double)
1376 old = "double";
1377 else if (what & Set_Float)
1378 old = "float";
1379 else if (what & Set_Signed)
1380 old = "signed";
1381 else if (what & Set_Unsigned)
1382 old = "unsigned";
1383 else if (what & Set_Short)
1384 old = "short";
1385 else if (what & Set_Long)
1386 old = "long";
1387 else
1388 old = "long long";
1389 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1390 old, show_ident(new));
1391 return;
1393 Catch_all:
1394 sparse_error(pos, "two or more data types in declaration specifiers");
1397 static struct symbol * const int_types[] =
1398 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype};
1399 static struct symbol * const signed_types[] =
1400 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1401 &slllong_ctype};
1402 static struct symbol * const unsigned_types[] =
1403 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1404 &ulllong_ctype};
1405 static struct symbol * const real_types[] =
1406 {&float_ctype, &double_ctype, &ldouble_ctype};
1407 static struct symbol * const char_types[] =
1408 {&char_ctype, &schar_ctype, &uchar_ctype};
1409 static struct symbol * const * const types[] = {
1410 int_types + 1, signed_types + 1, unsigned_types + 1,
1411 real_types + 1, char_types, char_types + 1, char_types + 2
1414 struct symbol *ctype_integer(int size, int want_unsigned)
1416 return types[want_unsigned ? CUInt : CInt][size];
1419 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1421 while (token_type(t) == TOKEN_IDENT) {
1422 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1423 if (!s)
1424 break;
1425 if (s->type != SYM_KEYWORD)
1426 break;
1427 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1428 break;
1429 t = t->next;
1430 if (s->op->declarator)
1431 t = s->op->declarator(t, ctx);
1433 return t;
1436 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1438 int seen = 0;
1439 int class = CInt;
1440 int size = 0;
1442 while (token_type(token) == TOKEN_IDENT) {
1443 struct symbol *s = lookup_symbol(token->ident,
1444 NS_TYPEDEF | NS_SYMBOL);
1445 if (!s || !(s->namespace & NS_TYPEDEF))
1446 break;
1447 if (s->type != SYM_KEYWORD) {
1448 if (seen & Set_Any)
1449 break;
1450 seen |= Set_S | Set_T;
1451 ctx->ctype.base_type = s->ctype.base_type;
1452 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1453 token = token->next;
1454 continue;
1456 if (s->op->type & KW_SPECIFIER) {
1457 if (seen & s->op->test) {
1458 specifier_conflict(token->pos,
1459 seen & s->op->test,
1460 token->ident);
1461 break;
1463 seen |= s->op->set;
1464 class += s->op->class;
1465 if (s->op->type & KW_SHORT) {
1466 size = -1;
1467 } else if (s->op->type & KW_LONG && size++) {
1468 if (class == CReal) {
1469 specifier_conflict(token->pos,
1470 Set_Vlong,
1471 &double_ident);
1472 break;
1474 seen |= Set_Vlong;
1477 token = token->next;
1478 if (s->op->declarator)
1479 token = s->op->declarator(token, ctx);
1480 if (s->op->type & KW_EXACT) {
1481 ctx->ctype.base_type = s->ctype.base_type;
1482 ctx->ctype.modifiers |= s->ctype.modifiers;
1486 if (!(seen & Set_S)) { /* not set explicitly? */
1487 struct symbol *base = &incomplete_ctype;
1488 if (seen & Set_Any)
1489 base = types[class][size];
1490 ctx->ctype.base_type = base;
1493 if (ctx->ctype.modifiers & MOD_BITWISE) {
1494 struct symbol *type;
1495 ctx->ctype.modifiers &= ~MOD_BITWISE;
1496 if (!is_int_type(ctx->ctype.base_type)) {
1497 sparse_error(token->pos, "invalid modifier");
1498 return token;
1500 type = alloc_symbol(token->pos, SYM_BASETYPE);
1501 *type = *ctx->ctype.base_type;
1502 type->ctype.modifiers &= ~MOD_SPECIFIER;
1503 type->ctype.base_type = ctx->ctype.base_type;
1504 type->type = SYM_RESTRICT;
1505 ctx->ctype.base_type = type;
1506 create_fouled(type);
1508 return token;
1511 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1513 struct expression *expr = NULL;
1515 if (match_idents(token, &restrict_ident, &__restrict_ident, NULL))
1516 token = token->next;
1517 token = parse_expression(token, &expr);
1518 sym->array_size = expr;
1519 return token;
1522 static struct token *parameter_type_list(struct token *, struct symbol *);
1523 static struct token *identifier_list(struct token *, struct symbol *);
1524 static struct token *declarator(struct token *token, struct decl_state *ctx);
1526 static struct token *skip_attribute(struct token *token)
1528 token = token->next;
1529 if (match_op(token, '(')) {
1530 int depth = 1;
1531 token = token->next;
1532 while (depth && !eof_token(token)) {
1533 if (token_type(token) == TOKEN_SPECIAL) {
1534 if (token->special == '(')
1535 depth++;
1536 else if (token->special == ')')
1537 depth--;
1539 token = token->next;
1542 return token;
1545 static struct token *skip_attributes(struct token *token)
1547 struct symbol *keyword;
1548 for (;;) {
1549 if (token_type(token) != TOKEN_IDENT)
1550 break;
1551 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1552 if (!keyword || keyword->type != SYM_KEYWORD)
1553 break;
1554 if (!(keyword->op->type & KW_ATTRIBUTE))
1555 break;
1556 token = expect(token->next, '(', "after attribute");
1557 token = expect(token, '(', "after attribute");
1558 for (;;) {
1559 if (eof_token(token))
1560 break;
1561 if (match_op(token, ';'))
1562 break;
1563 if (token_type(token) != TOKEN_IDENT)
1564 break;
1565 token = skip_attribute(token);
1566 if (!match_op(token, ','))
1567 break;
1568 token = token->next;
1570 token = expect(token, ')', "after attribute");
1571 token = expect(token, ')', "after attribute");
1573 return token;
1576 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1578 struct symbol *keyword;
1579 for (;;) {
1580 if (token_type(token) != TOKEN_IDENT)
1581 break;
1582 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1583 if (!keyword || keyword->type != SYM_KEYWORD)
1584 break;
1585 if (!(keyword->op->type & keywords))
1586 break;
1587 token = keyword->op->declarator(token->next, ctx);
1588 keywords &= KW_ATTRIBUTE;
1590 return token;
1593 static int is_nested(struct token *token, struct token **p,
1594 int prefer_abstract)
1597 * This can be either a parameter list or a grouping.
1598 * For the direct (non-abstract) case, we know if must be
1599 * a parameter list if we already saw the identifier.
1600 * For the abstract case, we know if must be a parameter
1601 * list if it is empty or starts with a type.
1603 struct token *next = token->next;
1605 *p = next = skip_attributes(next);
1607 if (token_type(next) == TOKEN_IDENT) {
1608 if (lookup_type(next))
1609 return !prefer_abstract;
1610 return 1;
1613 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1614 return 0;
1616 return 1;
1619 enum kind {
1620 Empty, K_R, Proto, Bad_Func,
1623 static enum kind which_func(struct token *token,
1624 struct ident **n,
1625 int prefer_abstract)
1627 struct token *next = token->next;
1629 if (token_type(next) == TOKEN_IDENT) {
1630 if (lookup_type(next))
1631 return Proto;
1632 /* identifier list not in definition; complain */
1633 if (prefer_abstract)
1634 warning(token->pos,
1635 "identifier list not in definition");
1636 return K_R;
1639 if (token_type(next) != TOKEN_SPECIAL)
1640 return Bad_Func;
1642 if (next->special == ')') {
1643 /* don't complain about those */
1644 if (!n || match_op(next->next, ';'))
1645 return Empty;
1646 warning(next->pos,
1647 "non-ANSI function declaration of function '%s'",
1648 show_ident(*n));
1649 return Empty;
1652 if (next->special == SPECIAL_ELLIPSIS) {
1653 warning(next->pos,
1654 "variadic functions must have one named argument");
1655 return Proto;
1658 return Bad_Func;
1661 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1663 struct ctype *ctype = &ctx->ctype;
1664 struct token *next;
1665 struct ident **p = ctx->ident;
1667 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1668 *ctx->ident = token->ident;
1669 token = token->next;
1670 } else if (match_op(token, '(') &&
1671 is_nested(token, &next, ctx->prefer_abstract)) {
1672 struct symbol *base_type = ctype->base_type;
1673 if (token->next != next)
1674 next = handle_attributes(token->next, ctx,
1675 KW_ATTRIBUTE);
1676 token = declarator(next, ctx);
1677 token = expect(token, ')', "in nested declarator");
1678 while (ctype->base_type != base_type)
1679 ctype = &ctype->base_type->ctype;
1680 p = NULL;
1683 if (match_op(token, '(')) {
1684 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1685 struct symbol *fn;
1686 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1687 token = token->next;
1688 if (kind == K_R)
1689 token = identifier_list(token, fn);
1690 else if (kind == Proto)
1691 token = parameter_type_list(token, fn);
1692 token = expect(token, ')', "in function declarator");
1693 fn->endpos = token->pos;
1694 return token;
1697 while (match_op(token, '[')) {
1698 struct symbol *array;
1699 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1700 token = abstract_array_declarator(token->next, array);
1701 token = expect(token, ']', "in abstract_array_declarator");
1702 array->endpos = token->pos;
1703 ctype = &array->ctype;
1705 return token;
1708 static struct token *pointer(struct token *token, struct decl_state *ctx)
1710 while (match_op(token,'*')) {
1711 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1712 ptr->ctype.modifiers = ctx->ctype.modifiers;
1713 ptr->ctype.base_type = ctx->ctype.base_type;
1714 merge_attr(&ptr->ctype, &ctx->ctype);
1715 ctx->ctype.modifiers = 0;
1716 ctx->ctype.base_type = ptr;
1717 ctx->ctype.attribute = &null_attr;
1718 ctx->ctype.alignment = 0;
1720 token = handle_qualifiers(token->next, ctx);
1721 ctx->ctype.base_type->endpos = token->pos;
1723 return token;
1726 static struct token *declarator(struct token *token, struct decl_state *ctx)
1728 token = pointer(token, ctx);
1729 return direct_declarator(token, ctx);
1732 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1734 struct ctype *ctype = &ctx->ctype;
1735 struct expression *expr;
1736 struct symbol *bitfield;
1737 long long width;
1739 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1740 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1741 show_typename(ctype->base_type));
1742 // Parse this to recover gracefully.
1743 return conditional_expression(token->next, &expr);
1746 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1747 token = conditional_expression(token->next, &expr);
1748 width = const_expression_value(expr);
1749 bitfield->bit_size = width;
1751 if (width < 0 || width > INT_MAX) {
1752 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1753 width = -1;
1754 } else if (*ctx->ident && width == 0) {
1755 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1756 show_ident(*ctx->ident));
1757 width = -1;
1758 } else if (*ctx->ident) {
1759 struct symbol *base_type = bitfield->ctype.base_type;
1760 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1761 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1762 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1763 // Valid values are either {-1;0} or {0}, depending on integer
1764 // representation. The latter makes for very efficient code...
1765 sparse_error(token->pos, "dubious one-bit signed bitfield");
1767 if (Wdefault_bitfield_sign &&
1768 bitfield_type->type != SYM_ENUM &&
1769 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1770 is_signed) {
1771 // The sign of bitfields is unspecified by default.
1772 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1775 bitfield->bit_size = width;
1776 bitfield->endpos = token->pos;
1777 return token;
1780 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1782 struct decl_state ctx = {.prefer_abstract = 0, .ctype.attribute = &null_attr};
1783 struct ctype saved;
1784 unsigned long mod;
1786 token = declaration_specifiers(token, &ctx);
1787 mod = storage_modifiers(&ctx);
1788 saved = ctx.ctype;
1789 for (;;) {
1790 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1791 ctx.ident = &decl->ident;
1793 token = declarator(token, &ctx);
1794 if (match_op(token, ':'))
1795 token = handle_bitfield(token, &ctx);
1797 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1798 apply_modifiers(token->pos, &ctx);
1800 decl->ctype = ctx.ctype;
1801 decl->ctype.modifiers |= mod;
1802 decl->endpos = token->pos;
1803 add_symbol(list, decl);
1804 if (!match_op(token, ','))
1805 break;
1806 token = token->next;
1807 ctx.ctype = saved;
1809 return token;
1812 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1814 while (!match_op(token, '}')) {
1815 if (!match_op(token, ';'))
1816 token = declaration_list(token, list);
1817 if (!match_op(token, ';')) {
1818 sparse_error(token->pos, "expected ; at end of declaration");
1819 break;
1821 token = token->next;
1823 return token;
1826 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1828 struct decl_state ctx = {.prefer_abstract = 1, .ctype.attribute = &null_attr};
1830 token = declaration_specifiers(token, &ctx);
1831 ctx.ident = &sym->ident;
1832 token = declarator(token, &ctx);
1833 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1834 apply_modifiers(token->pos, &ctx);
1835 sym->ctype = ctx.ctype;
1836 sym->ctype.modifiers |= storage_modifiers(&ctx);
1837 sym->endpos = token->pos;
1838 return token;
1841 struct token *typename(struct token *token, struct symbol **p, int *forced)
1843 struct decl_state ctx = {.prefer_abstract = 1, .ctype.attribute = &null_attr};
1844 int class;
1845 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1846 *p = sym;
1847 token = declaration_specifiers(token, &ctx);
1848 token = declarator(token, &ctx);
1849 apply_modifiers(token->pos, &ctx);
1850 sym->ctype = ctx.ctype;
1851 sym->endpos = token->pos;
1852 class = ctx.storage_class;
1853 if (forced) {
1854 *forced = 0;
1855 if (class == SForced) {
1856 *forced = 1;
1857 class = 0;
1860 if (class)
1861 warning(sym->pos, "storage class in typename (%s %s)",
1862 storage_class[class], show_typename(sym));
1863 return token;
1866 static struct token *expression_statement(struct token *token, struct expression **tree)
1868 token = parse_expression(token, tree);
1869 return expect(token, ';', "at end of statement");
1872 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1873 struct expression_list **inout)
1875 struct expression *expr;
1877 /* Allow empty operands */
1878 if (match_op(token->next, ':') || match_op(token->next, ')'))
1879 return token->next;
1880 do {
1881 struct ident *ident = NULL;
1882 if (match_op(token->next, '[') &&
1883 token_type(token->next->next) == TOKEN_IDENT &&
1884 match_op(token->next->next->next, ']')) {
1885 ident = token->next->next->ident;
1886 token = token->next->next->next;
1888 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1889 token = primary_expression(token->next, &expr);
1890 add_expression(inout, expr);
1891 token = parens_expression(token, &expr, "in asm parameter");
1892 add_expression(inout, expr);
1893 } while (match_op(token, ','));
1894 return token;
1897 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1898 struct expression_list **clobbers)
1900 struct expression *expr;
1902 do {
1903 token = primary_expression(token->next, &expr);
1904 if (expr)
1905 add_expression(clobbers, expr);
1906 } while (match_op(token, ','));
1907 return token;
1910 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
1911 struct symbol_list **labels)
1913 struct symbol *label;
1915 do {
1916 token = token->next; /* skip ':' and ',' */
1917 if (token_type(token) != TOKEN_IDENT)
1918 return token;
1919 label = label_symbol(token);
1920 add_symbol(labels, label);
1921 token = token->next;
1922 } while (match_op(token, ','));
1923 return token;
1926 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1928 int is_goto = 0;
1930 token = token->next;
1931 stmt->type = STMT_ASM;
1932 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1933 token = token->next;
1935 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
1936 is_goto = 1;
1937 token = token->next;
1939 token = expect(token, '(', "after asm");
1940 token = parse_expression(token, &stmt->asm_string);
1941 if (match_op(token, ':'))
1942 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1943 if (match_op(token, ':'))
1944 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1945 if (match_op(token, ':'))
1946 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1947 if (is_goto && match_op(token, ':'))
1948 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
1949 token = expect(token, ')', "after asm");
1950 return expect(token, ';', "at end of asm-statement");
1953 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
1955 struct expression *expr;
1956 token = expect(token, '(', "after asm");
1957 token = parse_expression(token->next, &expr);
1958 token = expect(token, ')', "after asm");
1959 return token;
1962 /* Make a statement out of an expression */
1963 static struct statement *make_statement(struct expression *expr)
1965 struct statement *stmt;
1967 if (!expr)
1968 return NULL;
1969 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1970 stmt->expression = expr;
1971 return stmt;
1975 * All iterators have two symbols associated with them:
1976 * the "continue" and "break" symbols, which are targets
1977 * for continue and break statements respectively.
1979 * They are in a special name-space, but they follow
1980 * all the normal visibility rules, so nested iterators
1981 * automatically work right.
1983 static void start_iterator(struct statement *stmt)
1985 struct symbol *cont, *brk;
1987 start_symbol_scope(stmt->pos);
1988 cont = alloc_symbol(stmt->pos, SYM_NODE);
1989 cont->stmt = stmt;
1990 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1991 brk = alloc_symbol(stmt->pos, SYM_NODE);
1992 brk->stmt = stmt;
1993 bind_symbol(brk, &break_ident, NS_ITERATOR);
1995 stmt->type = STMT_ITERATOR;
1996 stmt->iterator_break = brk;
1997 stmt->iterator_continue = cont;
1998 fn_local_symbol(brk);
1999 fn_local_symbol(cont);
2002 static void end_iterator(struct statement *stmt)
2004 end_symbol_scope();
2007 static struct statement *start_function(struct symbol *sym)
2009 struct symbol *ret;
2010 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2012 start_function_scope(stmt->pos);
2013 ret = alloc_symbol(sym->pos, SYM_NODE);
2014 ret->ctype = sym->ctype.base_type->ctype;
2015 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2016 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2017 bind_symbol(ret, &return_ident, NS_ITERATOR);
2018 stmt->ret = ret;
2019 fn_local_symbol(ret);
2021 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2022 current_fn = sym;
2024 return stmt;
2027 static void end_function(struct symbol *sym)
2029 current_fn = NULL;
2030 end_function_scope();
2034 * A "switch()" statement, like an iterator, has a
2035 * the "break" symbol associated with it. It works
2036 * exactly like the iterator break - it's the target
2037 * for any break-statements in scope, and means that
2038 * "break" handling doesn't even need to know whether
2039 * it's breaking out of an iterator or a switch.
2041 * In addition, the "case" symbol is a marker for the
2042 * case/default statements to find the switch statement
2043 * that they are associated with.
2045 static void start_switch(struct statement *stmt)
2047 struct symbol *brk, *switch_case;
2049 start_symbol_scope(stmt->pos);
2050 brk = alloc_symbol(stmt->pos, SYM_NODE);
2051 brk->stmt = stmt;
2052 bind_symbol(brk, &break_ident, NS_ITERATOR);
2054 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2055 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2056 switch_case->stmt = stmt;
2058 stmt->type = STMT_SWITCH;
2059 stmt->switch_break = brk;
2060 stmt->switch_case = switch_case;
2062 fn_local_symbol(brk);
2063 fn_local_symbol(switch_case);
2066 static void end_switch(struct statement *stmt)
2068 if (!stmt->switch_case->symbol_list)
2069 warning(stmt->pos, "switch with no cases");
2070 end_symbol_scope();
2073 static void add_case_statement(struct statement *stmt)
2075 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2076 struct symbol *sym;
2078 if (!target) {
2079 sparse_error(stmt->pos, "not in switch scope");
2080 stmt->type = STMT_NONE;
2081 return;
2083 sym = alloc_symbol(stmt->pos, SYM_NODE);
2084 add_symbol(&target->symbol_list, sym);
2085 sym->stmt = stmt;
2086 stmt->case_label = sym;
2087 fn_local_symbol(sym);
2090 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2092 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2094 if (!target)
2095 error_die(token->pos, "internal error: return without a function target");
2096 stmt->type = STMT_RETURN;
2097 stmt->ret_target = target;
2098 return expression_statement(token->next, &stmt->ret_value);
2101 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2103 struct symbol_list *syms;
2104 struct expression *e1, *e2, *e3;
2105 struct statement *iterator;
2107 start_iterator(stmt);
2108 token = expect(token->next, '(', "after 'for'");
2110 syms = NULL;
2111 e1 = NULL;
2112 /* C99 variable declaration? */
2113 if (lookup_type(token)) {
2114 token = external_declaration(token, &syms);
2115 } else {
2116 token = parse_expression(token, &e1);
2117 token = expect(token, ';', "in 'for'");
2119 token = parse_expression(token, &e2);
2120 token = expect(token, ';', "in 'for'");
2121 token = parse_expression(token, &e3);
2122 token = expect(token, ')', "in 'for'");
2123 token = statement(token, &iterator);
2125 stmt->iterator_syms = syms;
2126 stmt->iterator_pre_statement = make_statement(e1);
2127 stmt->iterator_pre_condition = e2;
2128 stmt->iterator_post_statement = make_statement(e3);
2129 stmt->iterator_post_condition = NULL;
2130 stmt->iterator_statement = iterator;
2131 end_iterator(stmt);
2133 return token;
2136 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2138 struct expression *expr;
2139 struct statement *iterator;
2141 start_iterator(stmt);
2142 token = parens_expression(token->next, &expr, "after 'while'");
2143 token = statement(token, &iterator);
2145 stmt->iterator_pre_condition = expr;
2146 stmt->iterator_post_condition = NULL;
2147 stmt->iterator_statement = iterator;
2148 end_iterator(stmt);
2150 return token;
2153 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2155 struct expression *expr;
2156 struct statement *iterator;
2158 start_iterator(stmt);
2159 token = statement(token->next, &iterator);
2160 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2161 token = token->next;
2162 else
2163 sparse_error(token->pos, "expected 'while' after 'do'");
2164 token = parens_expression(token, &expr, "after 'do-while'");
2166 stmt->iterator_post_condition = expr;
2167 stmt->iterator_statement = iterator;
2168 end_iterator(stmt);
2170 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2171 warning(iterator->pos, "do-while statement is not a compound statement");
2173 return expect(token, ';', "after statement");
2176 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2178 stmt->type = STMT_IF;
2179 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2180 token = statement(token, &stmt->if_true);
2181 if (token_type(token) != TOKEN_IDENT)
2182 return token;
2183 if (token->ident != &else_ident)
2184 return token;
2185 return statement(token->next, &stmt->if_false);
2188 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2190 stmt->type = STMT_CASE;
2191 token = expect(token, ':', "after default/case");
2192 add_case_statement(stmt);
2193 return statement(token, &stmt->case_statement);
2196 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2198 token = parse_expression(token->next, &stmt->case_expression);
2199 if (match_op(token, SPECIAL_ELLIPSIS))
2200 token = parse_expression(token->next, &stmt->case_to);
2201 return case_statement(token, stmt);
2204 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2206 return case_statement(token->next, stmt);
2209 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2211 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2212 stmt->type = STMT_GOTO;
2213 stmt->goto_label = target;
2214 if (!target)
2215 sparse_error(stmt->pos, "break/continue not in iterator scope");
2216 return expect(token->next, ';', "at end of statement");
2219 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2221 stmt->type = STMT_SWITCH;
2222 start_switch(stmt);
2223 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2224 token = statement(token, &stmt->switch_statement);
2225 end_switch(stmt);
2226 return token;
2229 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2231 stmt->type = STMT_GOTO;
2232 token = token->next;
2233 if (match_op(token, '*')) {
2234 token = parse_expression(token->next, &stmt->goto_expression);
2235 add_statement(&function_computed_goto_list, stmt);
2236 } else if (token_type(token) == TOKEN_IDENT) {
2237 stmt->goto_label = label_symbol(token);
2238 token = token->next;
2239 } else {
2240 sparse_error(token->pos, "Expected identifier or goto expression");
2242 return expect(token, ';', "at end of statement");
2245 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2247 stmt->type = STMT_CONTEXT;
2248 token = parse_expression(token->next, &stmt->expression);
2249 if (stmt->expression->type == EXPR_PREOP
2250 && stmt->expression->op == '('
2251 && stmt->expression->unop->type == EXPR_COMMA) {
2252 struct expression *expr;
2253 expr = stmt->expression->unop;
2254 stmt->context = expr->left;
2255 stmt->expression = expr->right;
2257 return expect(token, ';', "at end of statement");
2260 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2262 stmt->type = STMT_RANGE;
2263 token = assignment_expression(token->next, &stmt->range_expression);
2264 token = expect(token, ',', "after range expression");
2265 token = assignment_expression(token, &stmt->range_low);
2266 token = expect(token, ',', "after low range");
2267 token = assignment_expression(token, &stmt->range_high);
2268 return expect(token, ';', "after range statement");
2271 static struct token *statement(struct token *token, struct statement **tree)
2273 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2275 *tree = stmt;
2276 if (token_type(token) == TOKEN_IDENT) {
2277 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2278 if (s && s->op->statement)
2279 return s->op->statement(token, stmt);
2281 if (match_op(token->next, ':')) {
2282 struct symbol *s = label_symbol(token);
2283 stmt->type = STMT_LABEL;
2284 stmt->label_identifier = s;
2285 if (s->stmt)
2286 sparse_error(stmt->pos, "label '%s' redefined", show_ident(token->ident));
2287 s->stmt = stmt;
2288 token = skip_attributes(token->next->next);
2289 return statement(token, &stmt->label_statement);
2293 if (match_op(token, '{')) {
2294 stmt->type = STMT_COMPOUND;
2295 start_symbol_scope(stmt->pos);
2296 token = compound_statement(token->next, stmt);
2297 end_symbol_scope();
2299 return expect(token, '}', "at end of compound statement");
2302 stmt->type = STMT_EXPRESSION;
2303 return expression_statement(token, &stmt->expression);
2306 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2307 static struct token *label_statement(struct token *token)
2309 while (token_type(token) == TOKEN_IDENT) {
2310 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2311 /* it's block-scope, but we want label namespace */
2312 bind_symbol(sym, token->ident, NS_SYMBOL);
2313 sym->namespace = NS_LABEL;
2314 fn_local_symbol(sym);
2315 token = token->next;
2316 if (!match_op(token, ','))
2317 break;
2318 token = token->next;
2320 return expect(token, ';', "at end of label declaration");
2323 static struct token * statement_list(struct token *token, struct statement_list **list)
2325 int seen_statement = 0;
2326 while (token_type(token) == TOKEN_IDENT &&
2327 token->ident == &__label___ident)
2328 token = label_statement(token->next);
2329 for (;;) {
2330 struct statement * stmt;
2331 if (eof_token(token))
2332 break;
2333 if (match_op(token, '}'))
2334 break;
2335 if (lookup_type(token)) {
2336 if (seen_statement) {
2337 warning(token->pos, "mixing declarations and code");
2338 seen_statement = 0;
2340 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2341 token = external_declaration(token, &stmt->declaration);
2342 } else {
2343 seen_statement = Wdeclarationafterstatement;
2344 token = statement(token, &stmt);
2346 add_statement(list, stmt);
2348 return token;
2351 static struct token *identifier_list(struct token *token, struct symbol *fn)
2353 struct symbol_list **list = &fn->arguments;
2354 for (;;) {
2355 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2356 sym->ident = token->ident;
2357 token = token->next;
2358 sym->endpos = token->pos;
2359 sym->ctype.base_type = &incomplete_ctype;
2360 add_symbol(list, sym);
2361 if (!match_op(token, ',') ||
2362 token_type(token->next) != TOKEN_IDENT ||
2363 lookup_type(token->next))
2364 break;
2365 token = token->next;
2367 return token;
2370 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2372 struct symbol_list **list = &fn->arguments;
2374 for (;;) {
2375 struct symbol *sym;
2377 if (match_op(token, SPECIAL_ELLIPSIS)) {
2378 fn->variadic = 1;
2379 token = token->next;
2380 break;
2383 sym = alloc_symbol(token->pos, SYM_NODE);
2384 token = parameter_declaration(token, sym);
2385 if (sym->ctype.base_type == &void_ctype) {
2386 /* Special case: (void) */
2387 if (!*list && !sym->ident)
2388 break;
2389 warning(token->pos, "void parameter");
2391 add_symbol(list, sym);
2392 if (!match_op(token, ','))
2393 break;
2394 token = token->next;
2396 return token;
2399 struct token *compound_statement(struct token *token, struct statement *stmt)
2401 token = statement_list(token, &stmt->stmts);
2402 return token;
2405 static struct expression *identifier_expression(struct token *token)
2407 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2408 expr->expr_ident = token->ident;
2409 return expr;
2412 static struct expression *index_expression(struct expression *from, struct expression *to)
2414 int idx_from, idx_to;
2415 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2417 idx_from = const_expression_value(from);
2418 idx_to = idx_from;
2419 if (to) {
2420 idx_to = const_expression_value(to);
2421 if (idx_to < idx_from || idx_from < 0)
2422 warning(from->pos, "nonsense array initializer index range");
2424 expr->idx_from = idx_from;
2425 expr->idx_to = idx_to;
2426 return expr;
2429 static struct token *single_initializer(struct expression **ep, struct token *token)
2431 int expect_equal = 0;
2432 struct token *next = token->next;
2433 struct expression **tail = ep;
2434 int nested;
2436 *ep = NULL;
2438 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2439 struct expression *expr = identifier_expression(token);
2440 if (Wold_initializer)
2441 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2442 token = initializer(&expr->ident_expression, next->next);
2443 if (expr->ident_expression)
2444 *ep = expr;
2445 return token;
2448 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2449 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2450 struct expression *expr = identifier_expression(next);
2451 *tail = expr;
2452 tail = &expr->ident_expression;
2453 expect_equal = 1;
2454 token = next->next;
2455 } else if (match_op(token, '[')) {
2456 struct expression *from = NULL, *to = NULL, *expr;
2457 token = constant_expression(token->next, &from);
2458 if (!from) {
2459 sparse_error(token->pos, "Expected constant expression");
2460 break;
2462 if (match_op(token, SPECIAL_ELLIPSIS))
2463 token = constant_expression(token->next, &to);
2464 expr = index_expression(from, to);
2465 *tail = expr;
2466 tail = &expr->idx_expression;
2467 token = expect(token, ']', "at end of initializer index");
2468 if (nested)
2469 expect_equal = 1;
2470 } else {
2471 break;
2474 if (nested && !expect_equal) {
2475 if (!match_op(token, '='))
2476 warning(token->pos, "obsolete array initializer, use C99 syntax");
2477 else
2478 expect_equal = 1;
2480 if (expect_equal)
2481 token = expect(token, '=', "at end of initializer index");
2483 token = initializer(tail, token);
2484 if (!*tail)
2485 *ep = NULL;
2486 return token;
2489 static struct token *initializer_list(struct expression_list **list, struct token *token)
2491 struct expression *expr;
2493 for (;;) {
2494 token = single_initializer(&expr, token);
2495 if (!expr)
2496 break;
2497 add_expression(list, expr);
2498 if (!match_op(token, ','))
2499 break;
2500 token = token->next;
2502 return token;
2505 struct token *initializer(struct expression **tree, struct token *token)
2507 if (match_op(token, '{')) {
2508 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2509 *tree = expr;
2510 token = initializer_list(&expr->expr_list, token->next);
2511 return expect(token, '}', "at end of initializer");
2513 return assignment_expression(token, tree);
2516 static void declare_argument(struct symbol *sym, struct symbol *fn)
2518 if (!sym->ident) {
2519 sparse_error(sym->pos, "no identifier for function argument");
2520 return;
2522 bind_symbol(sym, sym->ident, NS_SYMBOL);
2525 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2526 struct symbol_list **list)
2528 struct symbol_list **old_symbol_list;
2529 struct symbol *base_type = decl->ctype.base_type;
2530 struct statement *stmt, **p;
2531 struct symbol *prev;
2532 struct symbol *arg;
2534 old_symbol_list = function_symbol_list;
2535 if (decl->ctype.modifiers & MOD_INLINE) {
2536 function_symbol_list = &decl->inline_symbol_list;
2537 p = &base_type->inline_stmt;
2538 } else {
2539 function_symbol_list = &decl->symbol_list;
2540 p = &base_type->stmt;
2542 function_computed_target_list = NULL;
2543 function_computed_goto_list = NULL;
2545 if (decl->ctype.modifiers & MOD_EXTERN) {
2546 if (!(decl->ctype.modifiers & MOD_INLINE))
2547 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2549 if (!(decl->ctype.modifiers & MOD_STATIC))
2550 decl->ctype.modifiers |= MOD_EXTERN;
2552 stmt = start_function(decl);
2554 *p = stmt;
2555 FOR_EACH_PTR (base_type->arguments, arg) {
2556 declare_argument(arg, base_type);
2557 } END_FOR_EACH_PTR(arg);
2559 token = compound_statement(token->next, stmt);
2561 end_function(decl);
2562 if (!(decl->ctype.modifiers & MOD_INLINE))
2563 add_symbol(list, decl);
2564 check_declaration(decl);
2565 decl->definition = decl;
2566 prev = decl->same_symbol;
2567 if (prev && prev->definition) {
2568 warning(decl->pos, "multiple definitions for function '%s'",
2569 show_ident(decl->ident));
2570 info(prev->definition->pos, " the previous one is here");
2571 } else {
2572 while (prev) {
2573 prev->definition = decl;
2574 prev = prev->same_symbol;
2577 function_symbol_list = old_symbol_list;
2578 if (function_computed_goto_list) {
2579 if (!function_computed_target_list)
2580 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2581 else {
2582 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2583 stmt->target_list = function_computed_target_list;
2584 } END_FOR_EACH_PTR(stmt);
2587 return expect(token, '}', "at end of function");
2590 static void promote_k_r_types(struct symbol *arg)
2592 struct symbol *base = arg->ctype.base_type;
2593 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2594 arg->ctype.base_type = &int_ctype;
2598 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2600 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2601 struct symbol *arg;
2603 FOR_EACH_PTR(real_args, arg) {
2604 struct symbol *type;
2606 /* This is quadratic in the number of arguments. We _really_ don't care */
2607 FOR_EACH_PTR(argtypes, type) {
2608 if (type->ident == arg->ident)
2609 goto match;
2610 } END_FOR_EACH_PTR(type);
2611 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2612 continue;
2613 match:
2614 type->used = 1;
2615 /* "char" and "short" promote to "int" */
2616 promote_k_r_types(type);
2618 arg->ctype = type->ctype;
2619 } END_FOR_EACH_PTR(arg);
2621 FOR_EACH_PTR(argtypes, arg) {
2622 if (!arg->used)
2623 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2624 } END_FOR_EACH_PTR(arg);
2628 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2629 struct symbol_list **list)
2631 struct symbol_list *args = NULL;
2633 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2634 do {
2635 token = declaration_list(token, &args);
2636 if (!match_op(token, ';')) {
2637 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2638 break;
2640 token = token->next;
2641 } while (lookup_type(token));
2643 apply_k_r_types(args, decl);
2645 if (!match_op(token, '{')) {
2646 sparse_error(token->pos, "expected function body");
2647 return token;
2649 return parse_function_body(token, decl, list);
2652 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2654 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2655 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2656 struct statement *stmt;
2658 anon->ctype.base_type = fn;
2659 stmt = alloc_statement(token->pos, STMT_NONE);
2660 fn->stmt = stmt;
2662 token = parse_asm_statement(token, stmt);
2664 add_symbol(list, anon);
2665 return token;
2668 struct token *external_declaration(struct token *token, struct symbol_list **list)
2670 struct ident *ident = NULL;
2671 struct symbol *decl;
2672 struct decl_state ctx = { .ident = &ident, .ctype.attribute = &null_attr};
2673 struct ctype saved;
2674 struct symbol *base_type;
2675 unsigned long mod;
2676 int is_typedef;
2678 /* Top-level inline asm? */
2679 if (token_type(token) == TOKEN_IDENT) {
2680 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2681 if (s && s->op->toplevel)
2682 return s->op->toplevel(token, list);
2685 /* Parse declaration-specifiers, if any */
2686 token = declaration_specifiers(token, &ctx);
2687 mod = storage_modifiers(&ctx);
2688 decl = alloc_symbol(token->pos, SYM_NODE);
2689 /* Just a type declaration? */
2690 if (match_op(token, ';')) {
2691 apply_modifiers(token->pos, &ctx);
2692 return token->next;
2695 saved = ctx.ctype;
2696 token = declarator(token, &ctx);
2697 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2698 apply_modifiers(token->pos, &ctx);
2700 decl->ctype = ctx.ctype;
2701 decl->ctype.modifiers |= mod;
2702 decl->endpos = token->pos;
2704 /* Just a type declaration? */
2705 if (!ident) {
2706 warning(token->pos, "missing identifier in declaration");
2707 return expect(token, ';', "at the end of type declaration");
2710 /* type define declaration? */
2711 is_typedef = ctx.storage_class == STypedef;
2713 /* Typedefs don't have meaningful storage */
2714 if (is_typedef)
2715 decl->ctype.modifiers |= MOD_USERTYPE;
2717 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2719 base_type = decl->ctype.base_type;
2721 if (is_typedef) {
2722 if (base_type && !base_type->ident) {
2723 switch (base_type->type) {
2724 case SYM_STRUCT:
2725 case SYM_UNION:
2726 case SYM_ENUM:
2727 case SYM_RESTRICT:
2728 base_type->ident = ident;
2731 } else if (base_type && base_type->type == SYM_FN) {
2732 /* K&R argument declaration? */
2733 if (lookup_type(token))
2734 return parse_k_r_arguments(token, decl, list);
2735 if (match_op(token, '{'))
2736 return parse_function_body(token, decl, list);
2738 if (!(decl->ctype.modifiers & MOD_STATIC))
2739 decl->ctype.modifiers |= MOD_EXTERN;
2740 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2741 sparse_error(token->pos, "void declaration");
2744 for (;;) {
2745 if (!is_typedef && match_op(token, '=')) {
2746 if (decl->ctype.modifiers & MOD_EXTERN) {
2747 warning(decl->pos, "symbol with external linkage has initializer");
2748 decl->ctype.modifiers &= ~MOD_EXTERN;
2750 token = initializer(&decl->initializer, token->next);
2752 if (!is_typedef) {
2753 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2754 add_symbol(list, decl);
2755 fn_local_symbol(decl);
2758 check_declaration(decl);
2759 if (decl->same_symbol)
2760 decl->definition = decl->same_symbol->definition;
2762 if (!match_op(token, ','))
2763 break;
2765 token = token->next;
2766 ident = NULL;
2767 decl = alloc_symbol(token->pos, SYM_NODE);
2768 ctx.ctype = saved;
2769 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2770 token = declarator(token, &ctx);
2771 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2772 apply_modifiers(token->pos, &ctx);
2773 decl->ctype = ctx.ctype;
2774 decl->ctype.modifiers |= mod;
2775 decl->endpos = token->pos;
2776 if (!ident) {
2777 sparse_error(token->pos, "expected identifier name in type definition");
2778 return token;
2781 if (is_typedef)
2782 decl->ctype.modifiers |= MOD_USERTYPE;
2784 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2786 /* Function declarations are automatically extern unless specifically static */
2787 base_type = decl->ctype.base_type;
2788 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2789 if (!(decl->ctype.modifiers & MOD_STATIC))
2790 decl->ctype.modifiers |= MOD_EXTERN;
2793 return expect(token, ';', "at end of declaration");