indenting: allow "break; case XXX:"
[smatch.git] / parse.c
blob5b82c681349045dc863c6f1c9b8732edbd34a89e
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 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <limits.h>
38 #include "lib.h"
39 #include "allocate.h"
40 #include "token.h"
41 #include "parse.h"
42 #include "symbol.h"
43 #include "scope.h"
44 #include "expression.h"
45 #include "target.h"
47 static struct symbol_list **function_symbol_list;
48 struct symbol_list *function_computed_target_list;
49 struct statement_list *function_computed_goto_list;
51 static struct token *statement(struct token *token, struct statement **tree);
52 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords);
54 typedef struct token *declarator_t(struct token *, struct decl_state *);
55 static declarator_t
56 struct_specifier, union_specifier, enum_specifier,
57 attribute_specifier, typeof_specifier, parse_asm_declarator,
58 typedef_specifier, inline_specifier, auto_specifier,
59 register_specifier, static_specifier, extern_specifier,
60 thread_specifier, const_qualifier, volatile_qualifier;
62 static struct token *parse_if_statement(struct token *token, struct statement *stmt);
63 static struct token *parse_return_statement(struct token *token, struct statement *stmt);
64 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt);
65 static struct token *parse_default_statement(struct token *token, struct statement *stmt);
66 static struct token *parse_case_statement(struct token *token, struct statement *stmt);
67 static struct token *parse_switch_statement(struct token *token, struct statement *stmt);
68 static struct token *parse_for_statement(struct token *token, struct statement *stmt);
69 static struct token *parse_while_statement(struct token *token, struct statement *stmt);
70 static struct token *parse_do_statement(struct token *token, struct statement *stmt);
71 static struct token *parse_goto_statement(struct token *token, struct statement *stmt);
72 static struct token *parse_context_statement(struct token *token, struct statement *stmt);
73 static struct token *parse_range_statement(struct token *token, struct statement *stmt);
74 static struct token *parse_asm_statement(struct token *token, struct statement *stmt);
75 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list);
77 typedef struct token *attr_t(struct token *, struct symbol *,
78 struct decl_state *);
80 static attr_t
81 attribute_packed, attribute_aligned, attribute_modifier,
82 attribute_address_space, attribute_context,
83 attribute_designated_init,
84 attribute_transparent_union, ignore_attribute,
85 attribute_mode, attribute_force;
87 typedef struct symbol *to_mode_t(struct symbol *);
89 static to_mode_t
90 to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode, to_word_mode;
92 enum {
93 Set_T = 1,
94 Set_S = 2,
95 Set_Char = 4,
96 Set_Int = 8,
97 Set_Double = 16,
98 Set_Float = 32,
99 Set_Signed = 64,
100 Set_Unsigned = 128,
101 Set_Short = 256,
102 Set_Long = 512,
103 Set_Vlong = 1024,
104 Set_Any = Set_T | Set_Short | Set_Long | Set_Signed | Set_Unsigned
107 enum {
108 CInt = 0, CSInt, CUInt, CReal, CChar, CSChar, CUChar
111 enum {
112 SNone = 0, STypedef, SAuto, SRegister, SExtern, SStatic, SForced
115 static struct symbol_op typedef_op = {
116 .type = KW_MODIFIER,
117 .declarator = typedef_specifier,
120 static struct symbol_op inline_op = {
121 .type = KW_MODIFIER,
122 .declarator = inline_specifier,
125 static struct symbol_op auto_op = {
126 .type = KW_MODIFIER,
127 .declarator = auto_specifier,
130 static struct symbol_op register_op = {
131 .type = KW_MODIFIER,
132 .declarator = register_specifier,
135 static struct symbol_op static_op = {
136 .type = KW_MODIFIER,
137 .declarator = static_specifier,
140 static struct symbol_op extern_op = {
141 .type = KW_MODIFIER,
142 .declarator = extern_specifier,
145 static struct symbol_op thread_op = {
146 .type = KW_MODIFIER,
147 .declarator = thread_specifier,
150 static struct symbol_op const_op = {
151 .type = KW_QUALIFIER,
152 .declarator = const_qualifier,
155 static struct symbol_op volatile_op = {
156 .type = KW_QUALIFIER,
157 .declarator = volatile_qualifier,
160 static struct symbol_op restrict_op = {
161 .type = KW_QUALIFIER,
164 static struct symbol_op typeof_op = {
165 .type = KW_SPECIFIER,
166 .declarator = typeof_specifier,
167 .test = Set_Any,
168 .set = Set_S|Set_T,
171 static struct symbol_op attribute_op = {
172 .type = KW_ATTRIBUTE,
173 .declarator = attribute_specifier,
176 static struct symbol_op struct_op = {
177 .type = KW_SPECIFIER,
178 .declarator = struct_specifier,
179 .test = Set_Any,
180 .set = Set_S|Set_T,
183 static struct symbol_op union_op = {
184 .type = KW_SPECIFIER,
185 .declarator = union_specifier,
186 .test = Set_Any,
187 .set = Set_S|Set_T,
190 static struct symbol_op enum_op = {
191 .type = KW_SPECIFIER,
192 .declarator = enum_specifier,
193 .test = Set_Any,
194 .set = Set_S|Set_T,
197 static struct symbol_op spec_op = {
198 .type = KW_SPECIFIER | KW_EXACT,
199 .test = Set_Any,
200 .set = Set_S|Set_T,
203 static struct symbol_op char_op = {
204 .type = KW_SPECIFIER,
205 .test = Set_T|Set_Long|Set_Short,
206 .set = Set_T|Set_Char,
207 .class = CChar,
210 static struct symbol_op int_op = {
211 .type = KW_SPECIFIER,
212 .test = Set_T,
213 .set = Set_T|Set_Int,
216 static struct symbol_op double_op = {
217 .type = KW_SPECIFIER,
218 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
219 .set = Set_T|Set_Double,
220 .class = CReal,
223 static struct symbol_op float_op = {
224 .type = KW_SPECIFIER | KW_SHORT,
225 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long,
226 .set = Set_T|Set_Float,
227 .class = CReal,
230 static struct symbol_op short_op = {
231 .type = KW_SPECIFIER | KW_SHORT,
232 .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short,
233 .set = Set_Short,
236 static struct symbol_op signed_op = {
237 .type = KW_SPECIFIER,
238 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
239 .set = Set_Signed,
240 .class = CSInt,
243 static struct symbol_op unsigned_op = {
244 .type = KW_SPECIFIER,
245 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
246 .set = Set_Unsigned,
247 .class = CUInt,
250 static struct symbol_op long_op = {
251 .type = KW_SPECIFIER | KW_LONG,
252 .test = Set_S|Set_Char|Set_Float|Set_Short|Set_Vlong,
253 .set = Set_Long,
256 static struct symbol_op if_op = {
257 .statement = parse_if_statement,
260 static struct symbol_op return_op = {
261 .statement = parse_return_statement,
264 static struct symbol_op loop_iter_op = {
265 .statement = parse_loop_iterator,
268 static struct symbol_op default_op = {
269 .statement = parse_default_statement,
272 static struct symbol_op case_op = {
273 .statement = parse_case_statement,
276 static struct symbol_op switch_op = {
277 .statement = parse_switch_statement,
280 static struct symbol_op for_op = {
281 .statement = parse_for_statement,
284 static struct symbol_op while_op = {
285 .statement = parse_while_statement,
288 static struct symbol_op do_op = {
289 .statement = parse_do_statement,
292 static struct symbol_op goto_op = {
293 .statement = parse_goto_statement,
296 static struct symbol_op __context___op = {
297 .statement = parse_context_statement,
300 static struct symbol_op range_op = {
301 .statement = parse_range_statement,
304 static struct symbol_op asm_op = {
305 .type = KW_ASM,
306 .declarator = parse_asm_declarator,
307 .statement = parse_asm_statement,
308 .toplevel = toplevel_asm_declaration,
311 static struct symbol_op packed_op = {
312 .attribute = attribute_packed,
315 static struct symbol_op aligned_op = {
316 .attribute = attribute_aligned,
319 static struct symbol_op attr_mod_op = {
320 .attribute = attribute_modifier,
323 static struct symbol_op attr_force_op = {
324 .attribute = attribute_force,
327 static struct symbol_op address_space_op = {
328 .attribute = attribute_address_space,
331 static struct symbol_op mode_op = {
332 .attribute = attribute_mode,
335 static struct symbol_op context_op = {
336 .attribute = attribute_context,
339 static struct symbol_op designated_init_op = {
340 .attribute = attribute_designated_init,
343 static struct symbol_op transparent_union_op = {
344 .attribute = attribute_transparent_union,
347 static struct symbol_op ignore_attr_op = {
348 .attribute = ignore_attribute,
351 static struct symbol_op mode_QI_op = {
352 .type = KW_MODE,
353 .to_mode = to_QI_mode
356 static struct symbol_op mode_HI_op = {
357 .type = KW_MODE,
358 .to_mode = to_HI_mode
361 static struct symbol_op mode_SI_op = {
362 .type = KW_MODE,
363 .to_mode = to_SI_mode
366 static struct symbol_op mode_DI_op = {
367 .type = KW_MODE,
368 .to_mode = to_DI_mode
371 static struct symbol_op mode_TI_op = {
372 .type = KW_MODE,
373 .to_mode = to_TI_mode
376 static struct symbol_op mode_word_op = {
377 .type = KW_MODE,
378 .to_mode = to_word_mode
381 static struct init_keyword {
382 const char *name;
383 enum namespace ns;
384 unsigned long modifiers;
385 struct symbol_op *op;
386 struct symbol *type;
387 } keyword_table[] = {
388 /* Type qualifiers */
389 { "const", NS_TYPEDEF, .op = &const_op },
390 { "__const", NS_TYPEDEF, .op = &const_op },
391 { "__const__", NS_TYPEDEF, .op = &const_op },
392 { "volatile", NS_TYPEDEF, .op = &volatile_op },
393 { "__volatile", NS_TYPEDEF, .op = &volatile_op },
394 { "__volatile__", NS_TYPEDEF, .op = &volatile_op },
396 /* Typedef.. */
397 { "typedef", NS_TYPEDEF, .op = &typedef_op },
399 /* Type specifiers */
400 { "void", NS_TYPEDEF, .type = &void_ctype, .op = &spec_op},
401 { "char", NS_TYPEDEF, .op = &char_op },
402 { "short", NS_TYPEDEF, .op = &short_op },
403 { "int", NS_TYPEDEF, .op = &int_op },
404 { "long", NS_TYPEDEF, .op = &long_op },
405 { "float", NS_TYPEDEF, .op = &float_op },
406 { "double", NS_TYPEDEF, .op = &double_op },
407 { "signed", NS_TYPEDEF, .op = &signed_op },
408 { "__signed", NS_TYPEDEF, .op = &signed_op },
409 { "__signed__", NS_TYPEDEF, .op = &signed_op },
410 { "unsigned", NS_TYPEDEF, .op = &unsigned_op },
411 { "_Bool", NS_TYPEDEF, .type = &bool_ctype, .op = &spec_op },
413 /* Predeclared types */
414 { "__builtin_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
415 { "__builtin_ms_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
416 { "__int128_t", NS_TYPEDEF, .type = &lllong_ctype, .op = &spec_op },
417 { "__uint128_t",NS_TYPEDEF, .type = &ulllong_ctype, .op = &spec_op },
419 /* Extended types */
420 { "typeof", NS_TYPEDEF, .op = &typeof_op },
421 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
422 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
424 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
425 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
427 { "struct", NS_TYPEDEF, .op = &struct_op },
428 { "union", NS_TYPEDEF, .op = &union_op },
429 { "enum", NS_TYPEDEF, .op = &enum_op },
431 { "inline", NS_TYPEDEF, .op = &inline_op },
432 { "__inline", NS_TYPEDEF, .op = &inline_op },
433 { "__inline__", NS_TYPEDEF, .op = &inline_op },
435 /* Ignored for now.. */
436 { "restrict", NS_TYPEDEF, .op = &restrict_op},
437 { "__restrict", NS_TYPEDEF, .op = &restrict_op},
438 { "__restrict__", NS_TYPEDEF, .op = &restrict_op},
440 /* Storage class */
441 { "auto", NS_TYPEDEF, .op = &auto_op },
442 { "register", NS_TYPEDEF, .op = &register_op },
443 { "static", NS_TYPEDEF, .op = &static_op },
444 { "extern", NS_TYPEDEF, .op = &extern_op },
445 { "__thread", NS_TYPEDEF, .op = &thread_op },
447 /* Statement */
448 { "if", NS_KEYWORD, .op = &if_op },
449 { "return", NS_KEYWORD, .op = &return_op },
450 { "break", NS_KEYWORD, .op = &loop_iter_op },
451 { "continue", NS_KEYWORD, .op = &loop_iter_op },
452 { "default", NS_KEYWORD, .op = &default_op },
453 { "case", NS_KEYWORD, .op = &case_op },
454 { "switch", NS_KEYWORD, .op = &switch_op },
455 { "for", NS_KEYWORD, .op = &for_op },
456 { "while", NS_KEYWORD, .op = &while_op },
457 { "do", NS_KEYWORD, .op = &do_op },
458 { "goto", NS_KEYWORD, .op = &goto_op },
459 { "__context__",NS_KEYWORD, .op = &__context___op },
460 { "__range__", NS_KEYWORD, .op = &range_op },
461 { "asm", NS_KEYWORD, .op = &asm_op },
462 { "__asm", NS_KEYWORD, .op = &asm_op },
463 { "__asm__", NS_KEYWORD, .op = &asm_op },
465 /* Attribute */
466 { "packed", NS_KEYWORD, .op = &packed_op },
467 { "__packed__", NS_KEYWORD, .op = &packed_op },
468 { "aligned", NS_KEYWORD, .op = &aligned_op },
469 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
470 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
471 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
472 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
473 { "force", NS_KEYWORD, .op = &attr_force_op },
474 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
475 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
476 { "address_space",NS_KEYWORD, .op = &address_space_op },
477 { "mode", NS_KEYWORD, .op = &mode_op },
478 { "context", NS_KEYWORD, .op = &context_op },
479 { "designated_init", NS_KEYWORD, .op = &designated_init_op },
480 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
481 { "noreturn", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
482 { "__noreturn__", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
483 { "pure", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
484 {"__pure__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
485 {"const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
486 {"__const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
487 {"__const__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
489 { "__mode__", NS_KEYWORD, .op = &mode_op },
490 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
491 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
492 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
493 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
494 { "SI", NS_KEYWORD, .op = &mode_SI_op },
495 { "__SI__", NS_KEYWORD, .op = &mode_SI_op },
496 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
497 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
498 { "TI", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
499 { "__TI__", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
500 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
501 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
504 const char *ignored_attributes[] = {
505 "alias",
506 "__alias__",
507 "alloc_size",
508 "__alloc_size__",
509 "always_inline",
510 "__always_inline__",
511 "artificial",
512 "__artificial__",
513 "bounded",
514 "__bounded__",
515 "cdecl",
516 "__cdecl__",
517 "cold",
518 "__cold__",
519 "constructor",
520 "__constructor__",
521 "deprecated",
522 "__deprecated__",
523 "destructor",
524 "__destructor__",
525 "dllexport",
526 "__dllexport__",
527 "dllimport",
528 "__dllimport__",
529 "error",
530 "__error__",
531 "externally_visible",
532 "__externally_visible__",
533 "fastcall",
534 "__fastcall__",
535 "format",
536 "__format__",
537 "format_arg",
538 "__format_arg__",
539 "hot",
540 "__hot__",
541 "leaf",
542 "__leaf__",
543 "l1_text",
544 "__l1_text__",
545 "l1_data",
546 "__l1_data__",
547 "l2",
548 "__l2__",
549 "may_alias",
550 "__may_alias__",
551 "malloc",
552 "__malloc__",
553 "may_alias",
554 "__may_alias__",
555 "model",
556 "__model__",
557 "ms_abi",
558 "__ms_abi__",
559 "ms_hook_prologue",
560 "__ms_hook_prologue__",
561 "naked",
562 "__naked__",
563 "no_instrument_function",
564 "__no_instrument_function__",
565 "noclone",
566 "__noclone",
567 "__noclone__",
568 "noinline",
569 "__noinline__",
570 "nonnull",
571 "__nonnull",
572 "__nonnull__",
573 "nothrow",
574 "__nothrow",
575 "__nothrow__",
576 "regparm",
577 "__regparm__",
578 "section",
579 "__section__",
580 "sentinel",
581 "__sentinel__",
582 "signal",
583 "__signal__",
584 "stdcall",
585 "__stdcall__",
586 "syscall_linkage",
587 "__syscall_linkage__",
588 "sysv_abi",
589 "__sysv_abi__",
590 "unused",
591 "__unused__",
592 "used",
593 "__used__",
594 "vector_size",
595 "__vector_size__",
596 "visibility",
597 "__visibility__",
598 "warn_unused_result",
599 "__warn_unused_result__",
600 "warning",
601 "__warning__",
602 "weak",
603 "__weak__",
607 void init_parser(int stream)
609 int i;
610 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
611 struct init_keyword *ptr = keyword_table + i;
612 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
613 sym->ident->keyword = 1;
614 if (ptr->ns == NS_TYPEDEF)
615 sym->ident->reserved = 1;
616 sym->ctype.modifiers = ptr->modifiers;
617 sym->ctype.base_type = ptr->type;
618 sym->op = ptr->op;
621 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
622 const char * name = ignored_attributes[i];
623 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
624 NS_KEYWORD);
625 sym->ident->keyword = 1;
626 sym->op = &ignore_attr_op;
631 // Add a symbol to the list of function-local symbols
632 static void fn_local_symbol(struct symbol *sym)
634 if (function_symbol_list)
635 add_symbol(function_symbol_list, sym);
638 static int SENTINEL_ATTR match_idents(struct token *token, ...)
640 va_list args;
641 struct ident * next;
643 if (token_type(token) != TOKEN_IDENT)
644 return 0;
646 va_start(args, token);
647 do {
648 next = va_arg(args, struct ident *);
649 } while (next && token->ident != next);
650 va_end(args);
652 return next && token->ident == next;
656 struct statement *alloc_statement(struct position pos, int type)
658 struct statement *stmt = __alloc_statement(0);
659 stmt->type = type;
660 stmt->pos = pos;
661 return stmt;
664 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
666 static void apply_modifiers(struct position pos, struct decl_state *ctx)
668 struct symbol *ctype;
669 if (!ctx->mode)
670 return;
671 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
672 if (!ctype)
673 sparse_error(pos, "don't know how to apply mode to %s",
674 show_typename(ctx->ctype.base_type));
675 else
676 ctx->ctype.base_type = ctype;
680 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
682 struct symbol *sym = alloc_symbol(pos, type);
684 sym->ctype.base_type = ctype->base_type;
685 sym->ctype.modifiers = ctype->modifiers;
687 ctype->base_type = sym;
688 ctype->modifiers = 0;
689 return sym;
693 * NOTE! NS_LABEL is not just a different namespace,
694 * it also ends up using function scope instead of the
695 * regular symbol scope.
697 struct symbol *label_symbol(struct token *token)
699 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
700 if (!sym) {
701 sym = alloc_symbol(token->pos, SYM_LABEL);
702 bind_symbol(sym, token->ident, NS_LABEL);
703 fn_local_symbol(sym);
705 return sym;
708 static struct token *struct_union_enum_specifier(enum type type,
709 struct token *token, struct decl_state *ctx,
710 struct token *(*parse)(struct token *, struct symbol *))
712 struct symbol *sym;
713 struct position *repos;
715 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
716 if (token_type(token) == TOKEN_IDENT) {
717 sym = lookup_symbol(token->ident, NS_STRUCT);
718 if (!sym ||
719 (is_outer_scope(sym->scope) &&
720 (match_op(token->next,';') || match_op(token->next,'{')))) {
721 // Either a new symbol, or else an out-of-scope
722 // symbol being redefined.
723 sym = alloc_symbol(token->pos, type);
724 bind_symbol(sym, token->ident, NS_STRUCT);
726 if (sym->type != type)
727 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
728 ctx->ctype.base_type = sym;
729 repos = &token->pos;
730 token = token->next;
731 if (match_op(token, '{')) {
732 // The following test is actually wrong for empty
733 // structs, but (1) they are not C99, (2) gcc does
734 // the same thing, and (3) it's easier.
735 if (sym->symbol_list)
736 error_die(token->pos, "redefinition of %s", show_typename (sym));
737 sym->pos = *repos;
738 token = parse(token->next, sym);
739 token = expect(token, '}', "at end of struct-union-enum-specifier");
741 // Mark the structure as needing re-examination
742 sym->examined = 0;
743 sym->endpos = token->pos;
745 return token;
748 // private struct/union/enum type
749 if (!match_op(token, '{')) {
750 sparse_error(token->pos, "expected declaration");
751 ctx->ctype.base_type = &bad_ctype;
752 return token;
755 sym = alloc_symbol(token->pos, type);
756 token = parse(token->next, sym);
757 ctx->ctype.base_type = sym;
758 token = expect(token, '}', "at end of specifier");
759 sym->endpos = token->pos;
761 return token;
764 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
766 struct symbol *field, *last = NULL;
767 struct token *res;
768 res = struct_declaration_list(token, &sym->symbol_list);
769 FOR_EACH_PTR(sym->symbol_list, field) {
770 if (!field->ident) {
771 struct symbol *base = field->ctype.base_type;
772 if (base && base->type == SYM_BITFIELD)
773 continue;
775 if (last)
776 last->next_subobject = field;
777 last = field;
778 } END_FOR_EACH_PTR(field);
779 return res;
782 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
784 return struct_declaration_list(token, &sym->symbol_list);
787 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
789 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
792 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
794 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
798 typedef struct {
799 int x;
800 unsigned long long y;
801 } Num;
803 static void upper_boundary(Num *n, Num *v)
805 if (n->x > v->x)
806 return;
807 if (n->x < v->x) {
808 *n = *v;
809 return;
811 if (n->y < v->y)
812 n->y = v->y;
815 static void lower_boundary(Num *n, Num *v)
817 if (n->x < v->x)
818 return;
819 if (n->x > v->x) {
820 *n = *v;
821 return;
823 if (n->y > v->y)
824 n->y = v->y;
827 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
829 int shift = type->bit_size;
830 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
832 if (!is_unsigned)
833 shift--;
834 if (upper->x == 0 && upper->y >> shift)
835 return 0;
836 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
837 return 1;
838 return 0;
841 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
843 if (s1->bit_size < s2->bit_size) {
844 s1 = s2;
845 } else if (s1->bit_size == s2->bit_size) {
846 if (s2->ctype.modifiers & MOD_UNSIGNED)
847 s1 = s2;
849 if (s1->bit_size < bits_in_int)
850 return &int_ctype;
851 return s1;
854 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
856 struct symbol *sym;
858 FOR_EACH_PTR(list, sym) {
859 struct expression *expr = sym->initializer;
860 struct symbol *ctype;
861 if (expr->type != EXPR_VALUE)
862 continue;
863 ctype = expr->ctype;
864 if (ctype->bit_size == base_type->bit_size)
865 continue;
866 cast_value(expr, base_type, expr, ctype);
867 } END_FOR_EACH_PTR(sym);
870 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
872 unsigned long long lastval = 0;
873 struct symbol *ctype = NULL, *base_type = NULL;
874 Num upper = {-1, 0}, lower = {1, 0};
876 parent->examined = 1;
877 parent->ctype.base_type = &int_ctype;
878 while (token_type(token) == TOKEN_IDENT) {
879 struct expression *expr = NULL;
880 struct token *next = token->next;
881 struct symbol *sym;
883 if (match_op(next, '=')) {
884 next = constant_expression(next->next, &expr);
885 lastval = get_expression_value(expr);
886 ctype = &void_ctype;
887 if (expr && expr->ctype)
888 ctype = expr->ctype;
889 } else if (!ctype) {
890 ctype = &int_ctype;
891 } else if (is_int_type(ctype)) {
892 lastval++;
893 } else {
894 error_die(token->pos, "can't increment the last enum member");
897 if (!expr) {
898 expr = alloc_expression(token->pos, EXPR_VALUE);
899 expr->value = lastval;
900 expr->ctype = ctype;
903 sym = alloc_symbol(token->pos, SYM_NODE);
904 bind_symbol(sym, token->ident, NS_SYMBOL);
905 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
906 sym->initializer = expr;
907 sym->enum_member = 1;
908 sym->ctype.base_type = parent;
909 add_ptr_list(&parent->symbol_list, sym);
911 if (base_type != &bad_ctype) {
912 if (ctype->type == SYM_NODE)
913 ctype = ctype->ctype.base_type;
914 if (ctype->type == SYM_ENUM) {
915 if (ctype == parent)
916 ctype = base_type;
917 else
918 ctype = ctype->ctype.base_type;
921 * base_type rules:
922 * - if all enums are of the same type, then
923 * the base_type is that type (two first
924 * cases)
925 * - if enums are of different types, they
926 * all have to be integer types, and the
927 * base type is at least "int_ctype".
928 * - otherwise the base_type is "bad_ctype".
930 if (!base_type) {
931 base_type = ctype;
932 } else if (ctype == base_type) {
933 /* nothing */
934 } else if (is_int_type(base_type) && is_int_type(ctype)) {
935 base_type = bigger_enum_type(base_type, ctype);
936 } else
937 base_type = &bad_ctype;
938 parent->ctype.base_type = base_type;
940 if (is_int_type(base_type)) {
941 Num v = {.y = lastval};
942 if (ctype->ctype.modifiers & MOD_UNSIGNED)
943 v.x = 0;
944 else if ((long long)lastval >= 0)
945 v.x = 0;
946 else
947 v.x = -1;
948 upper_boundary(&upper, &v);
949 lower_boundary(&lower, &v);
951 token = next;
953 sym->endpos = token->pos;
955 if (!match_op(token, ','))
956 break;
957 token = token->next;
959 if (!base_type) {
960 sparse_error(token->pos, "bad enum definition");
961 base_type = &bad_ctype;
963 else if (!is_int_type(base_type))
964 base_type = base_type;
965 else if (type_is_ok(base_type, &upper, &lower))
966 base_type = base_type;
967 else if (type_is_ok(&int_ctype, &upper, &lower))
968 base_type = &int_ctype;
969 else if (type_is_ok(&uint_ctype, &upper, &lower))
970 base_type = &uint_ctype;
971 else if (type_is_ok(&long_ctype, &upper, &lower))
972 base_type = &long_ctype;
973 else if (type_is_ok(&ulong_ctype, &upper, &lower))
974 base_type = &ulong_ctype;
975 else if (type_is_ok(&llong_ctype, &upper, &lower))
976 base_type = &llong_ctype;
977 else if (type_is_ok(&ullong_ctype, &upper, &lower))
978 base_type = &ullong_ctype;
979 else
980 base_type = &bad_ctype;
981 parent->ctype.base_type = base_type;
982 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
983 parent->examined = 0;
985 cast_enum_list(parent->symbol_list, base_type);
987 return token;
990 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
992 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
993 struct ctype *ctype = &ctx->ctype.base_type->ctype;
995 if (!ctype->base_type)
996 ctype->base_type = &incomplete_ctype;
998 return ret;
1001 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
1003 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
1005 struct symbol *sym;
1007 if (!match_op(token, '(')) {
1008 sparse_error(token->pos, "expected '(' after typeof");
1009 return token;
1011 if (lookup_type(token->next)) {
1012 token = typename(token->next, &sym, NULL);
1013 ctx->ctype.base_type = sym->ctype.base_type;
1014 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
1015 } else {
1016 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
1017 token = parse_expression(token->next, &typeof_sym->initializer);
1019 typeof_sym->endpos = token->pos;
1020 if (!typeof_sym->initializer) {
1021 sparse_error(token->pos, "expected expression after the '(' token");
1022 typeof_sym = &bad_ctype;
1024 ctx->ctype.base_type = typeof_sym;
1026 return expect(token, ')', "after typeof");
1029 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1031 struct expression *expr = NULL;
1032 if (match_op(token, '('))
1033 token = parens_expression(token, &expr, "in attribute");
1034 return token;
1037 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1039 set_attr_is_packed(&ctx->ctype);
1040 if (!ctx->ctype.alignment)
1041 ctx->ctype.alignment = 1;
1042 return token;
1045 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1047 int alignment = max_alignment;
1048 struct expression *expr = NULL;
1050 if (match_op(token, '(')) {
1051 token = parens_expression(token, &expr, "in attribute");
1052 if (expr)
1053 alignment = const_expression_value(expr);
1055 if (alignment & (alignment-1)) {
1056 warning(token->pos, "I don't like non-power-of-2 alignments");
1057 return token;
1058 } else if (alignment > ctx->ctype.alignment)
1059 ctx->ctype.alignment = alignment;
1060 return token;
1063 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1065 if (ctx->modifiers & qual)
1066 warning(*pos, "duplicate %s", modifier_string(qual));
1067 ctx->modifiers |= qual;
1070 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1072 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1073 return token;
1076 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1078 struct expression *expr = NULL;
1079 int as;
1080 token = expect(token, '(', "after address_space attribute");
1081 token = conditional_expression(token, &expr);
1082 if (expr) {
1083 as = const_expression_value(expr);
1084 if (Waddress_space && as)
1085 attr_set_as(&ctx->ctype, as);
1087 token = expect(token, ')', "after address_space attribute");
1088 return token;
1091 static struct symbol *to_QI_mode(struct symbol *ctype)
1093 if (ctype->ctype.base_type != &int_type)
1094 return NULL;
1095 if (ctype == &char_ctype)
1096 return ctype;
1097 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1098 : &schar_ctype;
1101 static struct symbol *to_HI_mode(struct symbol *ctype)
1103 if (ctype->ctype.base_type != &int_type)
1104 return NULL;
1105 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1106 : &sshort_ctype;
1109 static struct symbol *to_SI_mode(struct symbol *ctype)
1111 if (ctype->ctype.base_type != &int_type)
1112 return NULL;
1113 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1114 : &sint_ctype;
1117 static struct symbol *to_DI_mode(struct symbol *ctype)
1119 if (ctype->ctype.base_type != &int_type)
1120 return NULL;
1121 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1122 : &sllong_ctype;
1125 static struct symbol *to_TI_mode(struct symbol *ctype)
1127 if (ctype->ctype.base_type != &int_type)
1128 return NULL;
1129 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1130 : &slllong_ctype;
1133 static struct symbol *to_word_mode(struct symbol *ctype)
1135 if (ctype->ctype.base_type != &int_type)
1136 return NULL;
1137 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1138 : &slong_ctype;
1141 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1143 token = expect(token, '(', "after mode attribute");
1144 if (token_type(token) == TOKEN_IDENT) {
1145 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1146 if (mode && mode->op->type == KW_MODE)
1147 ctx->mode = mode->op;
1148 else
1149 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1150 token = token->next;
1151 } else
1152 sparse_error(token->pos, "expect attribute mode symbol\n");
1153 token = expect(token, ')', "after mode attribute");
1154 return token;
1157 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1159 struct context *context = alloc_context();
1160 struct expression *args[3];
1161 int argc = 0;
1163 token = expect(token, '(', "after context attribute");
1164 while (!match_op(token, ')')) {
1165 struct expression *expr = NULL;
1166 token = conditional_expression(token, &expr);
1167 if (!expr)
1168 break;
1169 if (argc < 3)
1170 args[argc++] = expr;
1171 if (!match_op(token, ','))
1172 break;
1173 token = token->next;
1176 switch(argc) {
1177 case 0:
1178 sparse_error(token->pos, "expected context input/output values");
1179 break;
1180 case 1:
1181 context->in = get_expression_value(args[0]);
1182 break;
1183 case 2:
1184 context->in = get_expression_value(args[0]);
1185 context->out = get_expression_value(args[1]);
1186 break;
1187 case 3:
1188 context->context = args[0];
1189 context->in = get_expression_value(args[1]);
1190 context->out = get_expression_value(args[2]);
1191 break;
1194 if (argc)
1195 attr_add_context(&ctx->ctype, context);
1197 token = expect(token, ')', "after context attribute");
1198 return token;
1201 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1203 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1204 ctx->ctype.base_type->designated_init = 1;
1205 else
1206 warning(token->pos, "attribute designated_init applied to non-structure type");
1207 return token;
1210 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1212 if (Wtransparent_union)
1213 warning(token->pos, "attribute __transparent_union__");
1215 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_UNION)
1216 ctx->ctype.base_type->transparent_union = 1;
1217 else
1218 warning(token->pos, "attribute __transparent_union__ applied to non-union type");
1219 return token;
1222 static struct token *recover_unknown_attribute(struct token *token)
1224 struct expression *expr = NULL;
1226 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1227 token = token->next;
1228 if (match_op(token, '('))
1229 token = parens_expression(token, &expr, "in attribute");
1230 return token;
1233 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1235 token = expect(token, '(', "after attribute");
1236 token = expect(token, '(', "after attribute");
1238 for (;;) {
1239 struct ident *attribute_name;
1240 struct symbol *attr;
1242 if (eof_token(token))
1243 break;
1244 if (match_op(token, ';'))
1245 break;
1246 if (token_type(token) != TOKEN_IDENT)
1247 break;
1248 attribute_name = token->ident;
1249 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1250 if (attr && attr->op->attribute)
1251 token = attr->op->attribute(token->next, attr, ctx);
1252 else
1253 token = recover_unknown_attribute(token);
1255 if (!match_op(token, ','))
1256 break;
1257 token = token->next;
1260 token = expect(token, ')', "after attribute");
1261 token = expect(token, ')', "after attribute");
1263 if (ctx->ctype.attribute->is_packed && ctx->ctype.base_type)
1264 set_attr_is_packed(&ctx->ctype.base_type->ctype);
1266 return token;
1269 static const char *storage_class[] =
1271 [STypedef] = "typedef",
1272 [SAuto] = "auto",
1273 [SExtern] = "extern",
1274 [SStatic] = "static",
1275 [SRegister] = "register",
1276 [SForced] = "[force]"
1279 static unsigned long storage_modifiers(struct decl_state *ctx)
1281 static unsigned long mod[] =
1283 [SAuto] = MOD_AUTO,
1284 [SExtern] = MOD_EXTERN,
1285 [SStatic] = MOD_STATIC,
1286 [SRegister] = MOD_REGISTER
1288 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1289 | (ctx->is_tls ? MOD_TLS : 0);
1292 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1294 /* __thread can be used alone, or with extern or static */
1295 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1296 sparse_error(*pos, "__thread can only be used alone, or with "
1297 "extern or static");
1298 return;
1301 if (!ctx->storage_class) {
1302 ctx->storage_class = class;
1303 return;
1305 if (ctx->storage_class == class)
1306 sparse_error(*pos, "duplicate %s", storage_class[class]);
1307 else
1308 sparse_error(*pos, "multiple storage classes");
1311 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1313 set_storage_class(&next->pos, ctx, STypedef);
1314 return next;
1317 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1319 set_storage_class(&next->pos, ctx, SAuto);
1320 return next;
1323 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1325 set_storage_class(&next->pos, ctx, SRegister);
1326 return next;
1329 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1331 set_storage_class(&next->pos, ctx, SStatic);
1332 return next;
1335 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1337 set_storage_class(&next->pos, ctx, SExtern);
1338 return next;
1341 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1343 /* This GCC extension can be used alone, or with extern or static */
1344 if (!ctx->storage_class || ctx->storage_class == SStatic
1345 || ctx->storage_class == SExtern) {
1346 ctx->is_tls = 1;
1347 } else {
1348 sparse_error(next->pos, "__thread can only be used alone, or "
1349 "with extern or static");
1352 return next;
1355 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1357 set_storage_class(&token->pos, ctx, SForced);
1358 return token;
1361 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1363 ctx->is_inline = 1;
1364 return next;
1367 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1369 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1370 return next;
1373 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1375 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1376 return next;
1379 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1381 unsigned long mod = thistype->modifiers;
1383 if (mod)
1384 apply_qualifier(&pos, ctype, mod);
1386 /* Alignment */
1387 if (thistype->alignment > ctype->alignment)
1388 ctype->alignment = thistype->alignment;
1390 /* Attribute */
1391 merge_attr(ctype, thistype);
1394 static void specifier_conflict(struct position pos, int what, struct ident *new)
1396 const char *old;
1397 if (what & (Set_S | Set_T))
1398 goto Catch_all;
1399 if (what & Set_Char)
1400 old = "char";
1401 else if (what & Set_Double)
1402 old = "double";
1403 else if (what & Set_Float)
1404 old = "float";
1405 else if (what & Set_Signed)
1406 old = "signed";
1407 else if (what & Set_Unsigned)
1408 old = "unsigned";
1409 else if (what & Set_Short)
1410 old = "short";
1411 else if (what & Set_Long)
1412 old = "long";
1413 else
1414 old = "long long";
1415 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1416 old, show_ident(new));
1417 return;
1419 Catch_all:
1420 sparse_error(pos, "two or more data types in declaration specifiers");
1423 static struct symbol * const int_types[] =
1424 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype};
1425 static struct symbol * const signed_types[] =
1426 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1427 &slllong_ctype};
1428 static struct symbol * const unsigned_types[] =
1429 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1430 &ulllong_ctype};
1431 static struct symbol * const real_types[] =
1432 {&float_ctype, &double_ctype, &ldouble_ctype};
1433 static struct symbol * const char_types[] =
1434 {&char_ctype, &schar_ctype, &uchar_ctype};
1435 static struct symbol * const * const types[] = {
1436 int_types + 1, signed_types + 1, unsigned_types + 1,
1437 real_types + 1, char_types, char_types + 1, char_types + 2
1440 struct symbol *ctype_integer(int size, int want_unsigned)
1442 return types[want_unsigned ? CUInt : CInt][size];
1445 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1447 while (token_type(t) == TOKEN_IDENT) {
1448 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1449 if (!s)
1450 break;
1451 if (s->type != SYM_KEYWORD)
1452 break;
1453 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1454 break;
1455 t = t->next;
1456 if (s->op->declarator)
1457 t = s->op->declarator(t, ctx);
1459 return t;
1462 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1464 int seen = 0;
1465 int class = CInt;
1466 int size = 0;
1468 while (token_type(token) == TOKEN_IDENT) {
1469 struct symbol *s = lookup_symbol(token->ident,
1470 NS_TYPEDEF | NS_SYMBOL);
1471 if (!s || !(s->namespace & NS_TYPEDEF))
1472 break;
1473 if (s->type != SYM_KEYWORD) {
1474 if (seen & Set_Any)
1475 break;
1476 seen |= Set_S | Set_T;
1477 ctx->ctype.base_type = s->ctype.base_type;
1478 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1479 token = token->next;
1480 continue;
1482 if (s->op->type & KW_SPECIFIER) {
1483 if (seen & s->op->test) {
1484 specifier_conflict(token->pos,
1485 seen & s->op->test,
1486 token->ident);
1487 break;
1489 seen |= s->op->set;
1490 class += s->op->class;
1491 if (s->op->type & KW_SHORT) {
1492 size = -1;
1493 } else if (s->op->type & KW_LONG && size++) {
1494 if (class == CReal) {
1495 specifier_conflict(token->pos,
1496 Set_Vlong,
1497 &double_ident);
1498 break;
1500 seen |= Set_Vlong;
1503 token = token->next;
1504 if (s->op->declarator)
1505 token = s->op->declarator(token, ctx);
1506 if (s->op->type & KW_EXACT) {
1507 ctx->ctype.base_type = s->ctype.base_type;
1508 ctx->ctype.modifiers |= s->ctype.modifiers;
1512 if (!(seen & Set_S)) { /* not set explicitly? */
1513 struct symbol *base = &incomplete_ctype;
1514 if (seen & Set_Any)
1515 base = types[class][size];
1516 ctx->ctype.base_type = base;
1519 if (ctx->ctype.modifiers & MOD_BITWISE) {
1520 struct symbol *type;
1521 ctx->ctype.modifiers &= ~MOD_BITWISE;
1522 if (!is_int_type(ctx->ctype.base_type)) {
1523 sparse_error(token->pos, "invalid modifier");
1524 return token;
1526 type = alloc_symbol(token->pos, SYM_BASETYPE);
1527 *type = *ctx->ctype.base_type;
1528 type->ctype.modifiers &= ~MOD_SPECIFIER;
1529 type->ctype.base_type = ctx->ctype.base_type;
1530 type->type = SYM_RESTRICT;
1531 ctx->ctype.base_type = type;
1532 create_fouled(type);
1534 return token;
1537 static struct token *abstract_array_static_declarator(struct token *token, int *has_static)
1539 while (token->ident == &static_ident) {
1540 if (*has_static)
1541 sparse_error(token->pos, "duplicate array static declarator");
1543 *has_static = 1;
1544 token = token->next;
1546 return token;
1550 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1552 struct expression *expr = NULL;
1553 int has_static = 0;
1555 token = abstract_array_static_declarator(token, &has_static);
1557 if (match_idents(token, &restrict_ident, &__restrict_ident, NULL))
1558 token = abstract_array_static_declarator(token->next, &has_static);
1559 token = parse_expression(token, &expr);
1560 sym->array_size = expr;
1561 return token;
1564 static struct token *parameter_type_list(struct token *, struct symbol *);
1565 static struct token *identifier_list(struct token *, struct symbol *);
1566 static struct token *declarator(struct token *token, struct decl_state *ctx);
1568 static struct token *skip_attribute(struct token *token)
1570 token = token->next;
1571 if (match_op(token, '(')) {
1572 int depth = 1;
1573 token = token->next;
1574 while (depth && !eof_token(token)) {
1575 if (token_type(token) == TOKEN_SPECIAL) {
1576 if (token->special == '(')
1577 depth++;
1578 else if (token->special == ')')
1579 depth--;
1581 token = token->next;
1584 return token;
1587 static struct token *skip_attributes(struct token *token)
1589 struct symbol *keyword;
1590 for (;;) {
1591 if (token_type(token) != TOKEN_IDENT)
1592 break;
1593 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1594 if (!keyword || keyword->type != SYM_KEYWORD)
1595 break;
1596 if (!(keyword->op->type & KW_ATTRIBUTE))
1597 break;
1598 token = expect(token->next, '(', "after attribute");
1599 token = expect(token, '(', "after attribute");
1600 for (;;) {
1601 if (eof_token(token))
1602 break;
1603 if (match_op(token, ';'))
1604 break;
1605 if (token_type(token) != TOKEN_IDENT)
1606 break;
1607 token = skip_attribute(token);
1608 if (!match_op(token, ','))
1609 break;
1610 token = token->next;
1612 token = expect(token, ')', "after attribute");
1613 token = expect(token, ')', "after attribute");
1615 return token;
1618 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1620 struct symbol *keyword;
1621 for (;;) {
1622 if (token_type(token) != TOKEN_IDENT)
1623 break;
1624 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1625 if (!keyword || keyword->type != SYM_KEYWORD)
1626 break;
1627 if (!(keyword->op->type & keywords))
1628 break;
1629 token = keyword->op->declarator(token->next, ctx);
1630 keywords &= KW_ATTRIBUTE;
1632 return token;
1635 static int is_nested(struct token *token, struct token **p,
1636 int prefer_abstract)
1639 * This can be either a parameter list or a grouping.
1640 * For the direct (non-abstract) case, we know if must be
1641 * a parameter list if we already saw the identifier.
1642 * For the abstract case, we know if must be a parameter
1643 * list if it is empty or starts with a type.
1645 struct token *next = token->next;
1647 *p = next = skip_attributes(next);
1649 if (token_type(next) == TOKEN_IDENT) {
1650 if (lookup_type(next))
1651 return !prefer_abstract;
1652 return 1;
1655 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1656 return 0;
1658 return 1;
1661 enum kind {
1662 Empty, K_R, Proto, Bad_Func,
1665 static enum kind which_func(struct token *token,
1666 struct ident **n,
1667 int prefer_abstract)
1669 struct token *next = token->next;
1671 if (token_type(next) == TOKEN_IDENT) {
1672 if (lookup_type(next))
1673 return Proto;
1674 /* identifier list not in definition; complain */
1675 if (prefer_abstract)
1676 warning(token->pos,
1677 "identifier list not in definition");
1678 return K_R;
1681 if (token_type(next) != TOKEN_SPECIAL)
1682 return Bad_Func;
1684 if (next->special == ')') {
1685 /* don't complain about those */
1686 if (!n || match_op(next->next, ';'))
1687 return Empty;
1688 warning(next->pos,
1689 "non-ANSI function declaration of function '%s'",
1690 show_ident(*n));
1691 return Empty;
1694 if (next->special == SPECIAL_ELLIPSIS) {
1695 warning(next->pos,
1696 "variadic functions must have one named argument");
1697 return Proto;
1700 return Bad_Func;
1703 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1705 struct ctype *ctype = &ctx->ctype;
1706 struct token *next;
1707 struct ident **p = ctx->ident;
1709 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1710 *ctx->ident = token->ident;
1711 token = token->next;
1712 } else if (match_op(token, '(') &&
1713 is_nested(token, &next, ctx->prefer_abstract)) {
1714 struct symbol *base_type = ctype->base_type;
1715 if (token->next != next)
1716 next = handle_attributes(token->next, ctx,
1717 KW_ATTRIBUTE);
1718 token = declarator(next, ctx);
1719 token = expect(token, ')', "in nested declarator");
1720 while (ctype->base_type != base_type)
1721 ctype = &ctype->base_type->ctype;
1722 p = NULL;
1725 if (match_op(token, '(')) {
1726 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1727 struct symbol *fn;
1728 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1729 token = token->next;
1730 if (kind == K_R)
1731 token = identifier_list(token, fn);
1732 else if (kind == Proto)
1733 token = parameter_type_list(token, fn);
1734 token = expect(token, ')', "in function declarator");
1735 fn->endpos = token->pos;
1736 return token;
1739 while (match_op(token, '[')) {
1740 struct symbol *array;
1741 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1742 token = abstract_array_declarator(token->next, array);
1743 token = expect(token, ']', "in abstract_array_declarator");
1744 array->endpos = token->pos;
1745 ctype = &array->ctype;
1747 return token;
1750 static struct token *pointer(struct token *token, struct decl_state *ctx)
1752 while (match_op(token,'*')) {
1753 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1754 ptr->ctype.modifiers = ctx->ctype.modifiers;
1755 ptr->ctype.base_type = ctx->ctype.base_type;
1756 merge_attr(&ptr->ctype, &ctx->ctype);
1757 ctx->ctype.modifiers = 0;
1758 ctx->ctype.base_type = ptr;
1759 ctx->ctype.attribute = &null_attr;
1760 ctx->ctype.alignment = 0;
1762 token = handle_qualifiers(token->next, ctx);
1763 ctx->ctype.base_type->endpos = token->pos;
1765 return token;
1768 static struct token *declarator(struct token *token, struct decl_state *ctx)
1770 token = pointer(token, ctx);
1771 return direct_declarator(token, ctx);
1774 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1776 struct ctype *ctype = &ctx->ctype;
1777 struct expression *expr;
1778 struct symbol *bitfield;
1779 long long width;
1781 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1782 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1783 show_typename(ctype->base_type));
1784 // Parse this to recover gracefully.
1785 return conditional_expression(token->next, &expr);
1788 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1789 token = conditional_expression(token->next, &expr);
1790 width = const_expression_value(expr);
1791 bitfield->bit_size = width;
1793 if (width < 0 || width > INT_MAX) {
1794 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1795 width = -1;
1796 } else if (*ctx->ident && width == 0) {
1797 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1798 show_ident(*ctx->ident));
1799 width = -1;
1800 } else if (*ctx->ident) {
1801 struct symbol *base_type = bitfield->ctype.base_type;
1802 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1803 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1804 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1805 // Valid values are either {-1;0} or {0}, depending on integer
1806 // representation. The latter makes for very efficient code...
1807 sparse_error(token->pos, "dubious one-bit signed bitfield");
1809 if (Wdefault_bitfield_sign &&
1810 bitfield_type->type != SYM_ENUM &&
1811 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1812 is_signed) {
1813 // The sign of bitfields is unspecified by default.
1814 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1817 bitfield->bit_size = width;
1818 bitfield->endpos = token->pos;
1819 return token;
1822 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1824 struct decl_state ctx = {.prefer_abstract = 0, .ctype.attribute = &null_attr};
1825 struct ctype saved;
1826 unsigned long mod;
1828 token = declaration_specifiers(token, &ctx);
1829 mod = storage_modifiers(&ctx);
1830 saved = ctx.ctype;
1831 for (;;) {
1832 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1833 ctx.ident = &decl->ident;
1835 token = declarator(token, &ctx);
1836 if (match_op(token, ':'))
1837 token = handle_bitfield(token, &ctx);
1839 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1840 apply_modifiers(token->pos, &ctx);
1842 decl->ctype = ctx.ctype;
1843 decl->ctype.modifiers |= mod;
1844 decl->endpos = token->pos;
1845 add_symbol(list, decl);
1846 if (!match_op(token, ','))
1847 break;
1848 token = token->next;
1849 ctx.ctype = saved;
1851 return token;
1854 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1856 while (!match_op(token, '}')) {
1857 if (!match_op(token, ';'))
1858 token = declaration_list(token, list);
1859 if (!match_op(token, ';')) {
1860 sparse_error(token->pos, "expected ; at end of declaration");
1861 break;
1863 token = token->next;
1865 return token;
1868 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1870 struct decl_state ctx = {.prefer_abstract = 1, .ctype.attribute = &null_attr};
1872 token = declaration_specifiers(token, &ctx);
1873 ctx.ident = &sym->ident;
1874 token = declarator(token, &ctx);
1875 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1876 apply_modifiers(token->pos, &ctx);
1877 sym->ctype = ctx.ctype;
1878 sym->ctype.modifiers |= storage_modifiers(&ctx);
1879 sym->endpos = token->pos;
1880 sym->forced_arg = ctx.storage_class == SForced;
1881 return token;
1884 struct token *typename(struct token *token, struct symbol **p, int *forced)
1886 struct decl_state ctx = {.prefer_abstract = 1, .ctype.attribute = &null_attr};
1887 int class;
1888 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1889 *p = sym;
1890 token = declaration_specifiers(token, &ctx);
1891 token = declarator(token, &ctx);
1892 apply_modifiers(token->pos, &ctx);
1893 sym->ctype = ctx.ctype;
1894 sym->endpos = token->pos;
1895 class = ctx.storage_class;
1896 if (forced) {
1897 *forced = 0;
1898 if (class == SForced) {
1899 *forced = 1;
1900 class = 0;
1903 if (class)
1904 warning(sym->pos, "storage class in typename (%s %s)",
1905 storage_class[class], show_typename(sym));
1906 return token;
1909 static struct token *expression_statement(struct token *token, struct expression **tree)
1911 token = parse_expression(token, tree);
1912 return expect(token, ';', "at end of statement");
1915 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1916 struct expression_list **inout)
1918 struct expression *expr;
1920 /* Allow empty operands */
1921 if (match_op(token->next, ':') || match_op(token->next, ')'))
1922 return token->next;
1923 do {
1924 struct ident *ident = NULL;
1925 if (match_op(token->next, '[') &&
1926 token_type(token->next->next) == TOKEN_IDENT &&
1927 match_op(token->next->next->next, ']')) {
1928 ident = token->next->next->ident;
1929 token = token->next->next->next;
1931 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1932 token = primary_expression(token->next, &expr);
1933 add_expression(inout, expr);
1934 token = parens_expression(token, &expr, "in asm parameter");
1935 add_expression(inout, expr);
1936 } while (match_op(token, ','));
1937 return token;
1940 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1941 struct expression_list **clobbers)
1943 struct expression *expr;
1945 do {
1946 token = primary_expression(token->next, &expr);
1947 if (expr)
1948 add_expression(clobbers, expr);
1949 } while (match_op(token, ','));
1950 return token;
1953 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
1954 struct symbol_list **labels)
1956 struct symbol *label;
1958 do {
1959 token = token->next; /* skip ':' and ',' */
1960 if (token_type(token) != TOKEN_IDENT)
1961 return token;
1962 label = label_symbol(token);
1963 add_symbol(labels, label);
1964 token = token->next;
1965 } while (match_op(token, ','));
1966 return token;
1969 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1971 int is_goto = 0;
1973 token = token->next;
1974 stmt->type = STMT_ASM;
1975 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1976 token = token->next;
1978 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
1979 is_goto = 1;
1980 token = token->next;
1982 token = expect(token, '(', "after asm");
1983 token = parse_expression(token, &stmt->asm_string);
1984 if (match_op(token, ':'))
1985 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1986 if (match_op(token, ':'))
1987 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1988 if (match_op(token, ':'))
1989 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1990 if (is_goto && match_op(token, ':'))
1991 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
1992 token = expect(token, ')', "after asm");
1993 return expect(token, ';', "at end of asm-statement");
1996 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
1998 struct expression *expr;
1999 token = expect(token, '(', "after asm");
2000 token = parse_expression(token->next, &expr);
2001 token = expect(token, ')', "after asm");
2002 return token;
2005 /* Make a statement out of an expression */
2006 static struct statement *make_statement(struct expression *expr)
2008 struct statement *stmt;
2010 if (!expr)
2011 return NULL;
2012 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
2013 stmt->expression = expr;
2014 return stmt;
2018 * All iterators have two symbols associated with them:
2019 * the "continue" and "break" symbols, which are targets
2020 * for continue and break statements respectively.
2022 * They are in a special name-space, but they follow
2023 * all the normal visibility rules, so nested iterators
2024 * automatically work right.
2026 static void start_iterator(struct statement *stmt)
2028 struct symbol *cont, *brk;
2030 start_symbol_scope(stmt->pos);
2031 cont = alloc_symbol(stmt->pos, SYM_NODE);
2032 cont->stmt = stmt;
2033 bind_symbol(cont, &continue_ident, NS_ITERATOR);
2034 brk = alloc_symbol(stmt->pos, SYM_NODE);
2035 brk->stmt = stmt;
2036 bind_symbol(brk, &break_ident, NS_ITERATOR);
2038 stmt->type = STMT_ITERATOR;
2039 stmt->iterator_break = brk;
2040 stmt->iterator_continue = cont;
2041 fn_local_symbol(brk);
2042 fn_local_symbol(cont);
2045 static void end_iterator(struct statement *stmt)
2047 end_symbol_scope();
2050 static struct statement *start_function(struct symbol *sym)
2052 struct symbol *ret;
2053 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2055 start_function_scope(stmt->pos);
2056 ret = alloc_symbol(sym->pos, SYM_NODE);
2057 ret->ctype = sym->ctype.base_type->ctype;
2058 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2059 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2060 bind_symbol(ret, &return_ident, NS_ITERATOR);
2061 stmt->ret = ret;
2062 fn_local_symbol(ret);
2064 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2065 current_fn = sym;
2067 return stmt;
2070 static void end_function(struct symbol *sym)
2072 current_fn = NULL;
2073 end_function_scope();
2077 * A "switch()" statement, like an iterator, has a
2078 * the "break" symbol associated with it. It works
2079 * exactly like the iterator break - it's the target
2080 * for any break-statements in scope, and means that
2081 * "break" handling doesn't even need to know whether
2082 * it's breaking out of an iterator or a switch.
2084 * In addition, the "case" symbol is a marker for the
2085 * case/default statements to find the switch statement
2086 * that they are associated with.
2088 static void start_switch(struct statement *stmt)
2090 struct symbol *brk, *switch_case;
2092 start_symbol_scope(stmt->pos);
2093 brk = alloc_symbol(stmt->pos, SYM_NODE);
2094 brk->stmt = stmt;
2095 bind_symbol(brk, &break_ident, NS_ITERATOR);
2097 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2098 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2099 switch_case->stmt = stmt;
2101 stmt->type = STMT_SWITCH;
2102 stmt->switch_break = brk;
2103 stmt->switch_case = switch_case;
2105 fn_local_symbol(brk);
2106 fn_local_symbol(switch_case);
2109 static void end_switch(struct statement *stmt)
2111 if (!stmt->switch_case->symbol_list)
2112 warning(stmt->pos, "switch with no cases");
2113 end_symbol_scope();
2116 static void add_case_statement(struct statement *stmt)
2118 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2119 struct symbol *sym;
2121 if (!target) {
2122 sparse_error(stmt->pos, "not in switch scope");
2123 stmt->type = STMT_NONE;
2124 return;
2126 sym = alloc_symbol(stmt->pos, SYM_NODE);
2127 add_symbol(&target->symbol_list, sym);
2128 sym->stmt = stmt;
2129 stmt->case_label = sym;
2130 fn_local_symbol(sym);
2133 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2135 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2137 if (!target)
2138 error_die(token->pos, "internal error: return without a function target");
2139 stmt->type = STMT_RETURN;
2140 stmt->ret_target = target;
2141 return expression_statement(token->next, &stmt->ret_value);
2144 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2146 struct symbol_list *syms;
2147 struct expression *e1, *e2, *e3;
2148 struct statement *iterator;
2150 start_iterator(stmt);
2151 token = expect(token->next, '(', "after 'for'");
2153 syms = NULL;
2154 e1 = NULL;
2155 /* C99 variable declaration? */
2156 if (lookup_type(token)) {
2157 token = external_declaration(token, &syms);
2158 } else {
2159 token = parse_expression(token, &e1);
2160 token = expect(token, ';', "in 'for'");
2162 token = parse_expression(token, &e2);
2163 token = expect(token, ';', "in 'for'");
2164 token = parse_expression(token, &e3);
2165 token = expect(token, ')', "in 'for'");
2166 token = statement(token, &iterator);
2168 stmt->iterator_syms = syms;
2169 stmt->iterator_pre_statement = make_statement(e1);
2170 stmt->iterator_pre_condition = e2;
2171 stmt->iterator_post_statement = make_statement(e3);
2172 stmt->iterator_post_condition = NULL;
2173 stmt->iterator_statement = iterator;
2174 end_iterator(stmt);
2176 return token;
2179 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2181 struct expression *expr;
2182 struct statement *iterator;
2184 start_iterator(stmt);
2185 token = parens_expression(token->next, &expr, "after 'while'");
2186 token = statement(token, &iterator);
2188 stmt->iterator_pre_condition = expr;
2189 stmt->iterator_post_condition = NULL;
2190 stmt->iterator_statement = iterator;
2191 end_iterator(stmt);
2193 return token;
2196 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2198 struct expression *expr;
2199 struct statement *iterator;
2201 start_iterator(stmt);
2202 token = statement(token->next, &iterator);
2203 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2204 token = token->next;
2205 else
2206 sparse_error(token->pos, "expected 'while' after 'do'");
2207 token = parens_expression(token, &expr, "after 'do-while'");
2209 stmt->iterator_post_condition = expr;
2210 stmt->iterator_statement = iterator;
2211 end_iterator(stmt);
2213 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2214 warning(iterator->pos, "do-while statement is not a compound statement");
2216 return expect(token, ';', "after statement");
2219 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2221 stmt->type = STMT_IF;
2222 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2223 token = statement(token, &stmt->if_true);
2224 if (token_type(token) != TOKEN_IDENT)
2225 return token;
2226 if (token->ident != &else_ident)
2227 return token;
2228 return statement(token->next, &stmt->if_false);
2231 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2233 stmt->type = STMT_CASE;
2234 token = expect(token, ':', "after default/case");
2235 add_case_statement(stmt);
2236 return statement(token, &stmt->case_statement);
2239 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2241 token = parse_expression(token->next, &stmt->case_expression);
2242 if (match_op(token, SPECIAL_ELLIPSIS))
2243 token = parse_expression(token->next, &stmt->case_to);
2244 return case_statement(token, stmt);
2247 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2249 return case_statement(token->next, stmt);
2252 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2254 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2255 stmt->type = STMT_GOTO;
2256 stmt->goto_label = target;
2257 if (!target)
2258 sparse_error(stmt->pos, "break/continue not in iterator scope");
2259 return expect(token->next, ';', "at end of statement");
2262 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2264 stmt->type = STMT_SWITCH;
2265 start_switch(stmt);
2266 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2267 token = statement(token, &stmt->switch_statement);
2268 end_switch(stmt);
2269 return token;
2272 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2274 stmt->type = STMT_GOTO;
2275 token = token->next;
2276 if (match_op(token, '*')) {
2277 token = parse_expression(token->next, &stmt->goto_expression);
2278 add_statement(&function_computed_goto_list, stmt);
2279 } else if (token_type(token) == TOKEN_IDENT) {
2280 stmt->goto_label = label_symbol(token);
2281 token = token->next;
2282 } else {
2283 sparse_error(token->pos, "Expected identifier or goto expression");
2285 return expect(token, ';', "at end of statement");
2288 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2290 stmt->type = STMT_CONTEXT;
2291 token = parse_expression(token->next, &stmt->expression);
2292 if (stmt->expression->type == EXPR_PREOP
2293 && stmt->expression->op == '('
2294 && stmt->expression->unop->type == EXPR_COMMA) {
2295 struct expression *expr;
2296 expr = stmt->expression->unop;
2297 stmt->context = expr->left;
2298 stmt->expression = expr->right;
2300 return expect(token, ';', "at end of statement");
2303 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2305 stmt->type = STMT_RANGE;
2306 token = assignment_expression(token->next, &stmt->range_expression);
2307 token = expect(token, ',', "after range expression");
2308 token = assignment_expression(token, &stmt->range_low);
2309 token = expect(token, ',', "after low range");
2310 token = assignment_expression(token, &stmt->range_high);
2311 return expect(token, ';', "after range statement");
2314 static struct token *statement(struct token *token, struct statement **tree)
2316 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2318 *tree = stmt;
2319 if (token_type(token) == TOKEN_IDENT) {
2320 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2321 if (s && s->op->statement)
2322 return s->op->statement(token, stmt);
2324 if (match_op(token->next, ':')) {
2325 struct symbol *s = label_symbol(token);
2326 stmt->type = STMT_LABEL;
2327 stmt->label_identifier = s;
2328 if (s->stmt)
2329 sparse_error(stmt->pos, "label '%s' redefined", show_ident(token->ident));
2330 s->stmt = stmt;
2331 token = skip_attributes(token->next->next);
2332 return statement(token, &stmt->label_statement);
2336 if (match_op(token, '{')) {
2337 stmt->type = STMT_COMPOUND;
2338 start_symbol_scope(stmt->pos);
2339 token = compound_statement(token->next, stmt);
2340 end_symbol_scope();
2342 return expect(token, '}', "at end of compound statement");
2345 stmt->type = STMT_EXPRESSION;
2346 return expression_statement(token, &stmt->expression);
2349 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2350 static struct token *label_statement(struct token *token)
2352 while (token_type(token) == TOKEN_IDENT) {
2353 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2354 /* it's block-scope, but we want label namespace */
2355 bind_symbol(sym, token->ident, NS_SYMBOL);
2356 sym->namespace = NS_LABEL;
2357 fn_local_symbol(sym);
2358 token = token->next;
2359 if (!match_op(token, ','))
2360 break;
2361 token = token->next;
2363 return expect(token, ';', "at end of label declaration");
2366 static struct token * statement_list(struct token *token, struct statement_list **list)
2368 int seen_statement = 0;
2369 while (token_type(token) == TOKEN_IDENT &&
2370 token->ident == &__label___ident)
2371 token = label_statement(token->next);
2372 for (;;) {
2373 struct statement * stmt;
2374 if (eof_token(token))
2375 break;
2376 if (match_op(token, '}'))
2377 break;
2378 if (lookup_type(token)) {
2379 if (seen_statement) {
2380 warning(token->pos, "mixing declarations and code");
2381 seen_statement = 0;
2383 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2384 token = external_declaration(token, &stmt->declaration);
2385 } else {
2386 seen_statement = Wdeclarationafterstatement;
2387 token = statement(token, &stmt);
2389 add_statement(list, stmt);
2391 return token;
2394 static struct token *identifier_list(struct token *token, struct symbol *fn)
2396 struct symbol_list **list = &fn->arguments;
2397 for (;;) {
2398 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2399 sym->ident = token->ident;
2400 token = token->next;
2401 sym->endpos = token->pos;
2402 sym->ctype.base_type = &incomplete_ctype;
2403 add_symbol(list, sym);
2404 if (!match_op(token, ',') ||
2405 token_type(token->next) != TOKEN_IDENT ||
2406 lookup_type(token->next))
2407 break;
2408 token = token->next;
2410 return token;
2413 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2415 struct symbol_list **list = &fn->arguments;
2417 for (;;) {
2418 struct symbol *sym;
2420 if (match_op(token, SPECIAL_ELLIPSIS)) {
2421 fn->variadic = 1;
2422 token = token->next;
2423 break;
2426 sym = alloc_symbol(token->pos, SYM_NODE);
2427 token = parameter_declaration(token, sym);
2428 if (sym->ctype.base_type == &void_ctype) {
2429 /* Special case: (void) */
2430 if (!*list && !sym->ident)
2431 break;
2432 warning(token->pos, "void parameter");
2434 add_symbol(list, sym);
2435 if (!match_op(token, ','))
2436 break;
2437 token = token->next;
2439 return token;
2442 struct token *compound_statement(struct token *token, struct statement *stmt)
2444 token = statement_list(token, &stmt->stmts);
2445 return token;
2448 static struct expression *identifier_expression(struct token *token)
2450 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2451 expr->expr_ident = token->ident;
2452 return expr;
2455 static struct expression *index_expression(struct expression *from, struct expression *to)
2457 int idx_from, idx_to;
2458 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2460 idx_from = const_expression_value(from);
2461 idx_to = idx_from;
2462 if (to) {
2463 idx_to = const_expression_value(to);
2464 if (idx_to < idx_from || idx_from < 0)
2465 warning(from->pos, "nonsense array initializer index range");
2467 expr->idx_from = idx_from;
2468 expr->idx_to = idx_to;
2469 return expr;
2472 static struct token *single_initializer(struct expression **ep, struct token *token)
2474 int expect_equal = 0;
2475 struct token *next = token->next;
2476 struct expression **tail = ep;
2477 int nested;
2479 *ep = NULL;
2481 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2482 struct expression *expr = identifier_expression(token);
2483 if (Wold_initializer)
2484 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2485 token = initializer(&expr->ident_expression, next->next);
2486 if (expr->ident_expression)
2487 *ep = expr;
2488 return token;
2491 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2492 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2493 struct expression *expr = identifier_expression(next);
2494 *tail = expr;
2495 tail = &expr->ident_expression;
2496 expect_equal = 1;
2497 token = next->next;
2498 } else if (match_op(token, '[')) {
2499 struct expression *from = NULL, *to = NULL, *expr;
2500 token = constant_expression(token->next, &from);
2501 if (!from) {
2502 sparse_error(token->pos, "Expected constant expression");
2503 break;
2505 if (match_op(token, SPECIAL_ELLIPSIS))
2506 token = constant_expression(token->next, &to);
2507 expr = index_expression(from, to);
2508 *tail = expr;
2509 tail = &expr->idx_expression;
2510 token = expect(token, ']', "at end of initializer index");
2511 if (nested)
2512 expect_equal = 1;
2513 } else {
2514 break;
2517 if (nested && !expect_equal) {
2518 if (!match_op(token, '='))
2519 warning(token->pos, "obsolete array initializer, use C99 syntax");
2520 else
2521 expect_equal = 1;
2523 if (expect_equal)
2524 token = expect(token, '=', "at end of initializer index");
2526 token = initializer(tail, token);
2527 if (!*tail)
2528 *ep = NULL;
2529 return token;
2532 static struct token *initializer_list(struct expression_list **list, struct token *token)
2534 struct expression *expr;
2536 for (;;) {
2537 token = single_initializer(&expr, token);
2538 if (!expr)
2539 break;
2540 add_expression(list, expr);
2541 if (!match_op(token, ','))
2542 break;
2543 token = token->next;
2545 return token;
2548 struct token *initializer(struct expression **tree, struct token *token)
2550 if (match_op(token, '{')) {
2551 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2552 *tree = expr;
2553 token = initializer_list(&expr->expr_list, token->next);
2554 return expect(token, '}', "at end of initializer");
2556 return assignment_expression(token, tree);
2559 static void declare_argument(struct symbol *sym, struct symbol *fn)
2561 if (!sym->ident) {
2562 sparse_error(sym->pos, "no identifier for function argument");
2563 return;
2565 bind_symbol(sym, sym->ident, NS_SYMBOL);
2568 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2569 struct symbol_list **list)
2571 struct symbol_list **old_symbol_list;
2572 struct symbol *base_type = decl->ctype.base_type;
2573 struct statement *stmt, **p;
2574 struct symbol *prev;
2575 struct symbol *arg;
2577 old_symbol_list = function_symbol_list;
2578 if (decl->ctype.modifiers & MOD_INLINE) {
2579 function_symbol_list = &decl->inline_symbol_list;
2580 p = &base_type->inline_stmt;
2581 } else {
2582 function_symbol_list = &decl->symbol_list;
2583 p = &base_type->stmt;
2585 function_computed_target_list = NULL;
2586 function_computed_goto_list = NULL;
2588 if (decl->ctype.modifiers & MOD_EXTERN) {
2589 if (!(decl->ctype.modifiers & MOD_INLINE))
2590 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2592 if (!(decl->ctype.modifiers & MOD_STATIC))
2593 decl->ctype.modifiers |= MOD_EXTERN;
2595 stmt = start_function(decl);
2597 *p = stmt;
2598 FOR_EACH_PTR (base_type->arguments, arg) {
2599 declare_argument(arg, base_type);
2600 } END_FOR_EACH_PTR(arg);
2602 token = compound_statement(token->next, stmt);
2604 end_function(decl);
2605 if (!(decl->ctype.modifiers & MOD_INLINE))
2606 add_symbol(list, decl);
2607 check_declaration(decl);
2608 decl->definition = decl;
2609 prev = decl->same_symbol;
2610 if (prev && prev->definition) {
2611 warning(decl->pos, "multiple definitions for function '%s'",
2612 show_ident(decl->ident));
2613 info(prev->definition->pos, " the previous one is here");
2614 } else {
2615 while (prev) {
2616 rebind_scope(prev, decl->scope);
2617 prev->definition = decl;
2618 prev = prev->same_symbol;
2621 function_symbol_list = old_symbol_list;
2622 if (function_computed_goto_list) {
2623 if (!function_computed_target_list)
2624 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2625 else {
2626 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2627 stmt->target_list = function_computed_target_list;
2628 } END_FOR_EACH_PTR(stmt);
2631 return expect(token, '}', "at end of function");
2634 static void promote_k_r_types(struct symbol *arg)
2636 struct symbol *base = arg->ctype.base_type;
2637 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2638 arg->ctype.base_type = &int_ctype;
2642 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2644 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2645 struct symbol *arg;
2647 FOR_EACH_PTR(real_args, arg) {
2648 struct symbol *type;
2650 /* This is quadratic in the number of arguments. We _really_ don't care */
2651 FOR_EACH_PTR(argtypes, type) {
2652 if (type->ident == arg->ident)
2653 goto match;
2654 } END_FOR_EACH_PTR(type);
2655 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2656 continue;
2657 match:
2658 type->used = 1;
2659 /* "char" and "short" promote to "int" */
2660 promote_k_r_types(type);
2662 arg->ctype = type->ctype;
2663 } END_FOR_EACH_PTR(arg);
2665 FOR_EACH_PTR(argtypes, arg) {
2666 if (!arg->used)
2667 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2668 } END_FOR_EACH_PTR(arg);
2672 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2673 struct symbol_list **list)
2675 struct symbol_list *args = NULL;
2677 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2678 do {
2679 token = declaration_list(token, &args);
2680 if (!match_op(token, ';')) {
2681 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2682 break;
2684 token = token->next;
2685 } while (lookup_type(token));
2687 apply_k_r_types(args, decl);
2689 if (!match_op(token, '{')) {
2690 sparse_error(token->pos, "expected function body");
2691 return token;
2693 return parse_function_body(token, decl, list);
2696 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2698 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2699 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2700 struct statement *stmt;
2702 anon->ctype.base_type = fn;
2703 stmt = alloc_statement(token->pos, STMT_NONE);
2704 fn->stmt = stmt;
2706 token = parse_asm_statement(token, stmt);
2708 add_symbol(list, anon);
2709 return token;
2712 struct token *external_declaration(struct token *token, struct symbol_list **list)
2714 struct ident *ident = NULL;
2715 struct symbol *decl;
2716 struct decl_state ctx = { .ident = &ident, .ctype.attribute = &null_attr};
2717 struct ctype saved;
2718 struct symbol *base_type;
2719 unsigned long mod;
2720 int is_typedef;
2722 /* Top-level inline asm? */
2723 if (token_type(token) == TOKEN_IDENT) {
2724 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2725 if (s && s->op->toplevel)
2726 return s->op->toplevel(token, list);
2729 /* Parse declaration-specifiers, if any */
2730 token = declaration_specifiers(token, &ctx);
2731 mod = storage_modifiers(&ctx);
2732 decl = alloc_symbol(token->pos, SYM_NODE);
2733 /* Just a type declaration? */
2734 if (match_op(token, ';')) {
2735 apply_modifiers(token->pos, &ctx);
2736 return token->next;
2739 saved = ctx.ctype;
2740 token = declarator(token, &ctx);
2741 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2742 apply_modifiers(token->pos, &ctx);
2744 decl->ctype = ctx.ctype;
2745 decl->ctype.modifiers |= mod;
2746 decl->endpos = token->pos;
2748 /* Just a type declaration? */
2749 if (!ident) {
2750 warning(token->pos, "missing identifier in declaration");
2751 return expect(token, ';', "at the end of type declaration");
2754 /* type define declaration? */
2755 is_typedef = ctx.storage_class == STypedef;
2757 /* Typedefs don't have meaningful storage */
2758 if (is_typedef)
2759 decl->ctype.modifiers |= MOD_USERTYPE;
2761 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2763 base_type = decl->ctype.base_type;
2765 if (is_typedef) {
2766 if (base_type && !base_type->ident) {
2767 switch (base_type->type) {
2768 case SYM_STRUCT:
2769 case SYM_UNION:
2770 case SYM_ENUM:
2771 case SYM_RESTRICT:
2772 base_type->ident = ident;
2775 } else if (base_type && base_type->type == SYM_FN) {
2776 /* K&R argument declaration? */
2777 if (lookup_type(token))
2778 return parse_k_r_arguments(token, decl, list);
2779 if (match_op(token, '{'))
2780 return parse_function_body(token, decl, list);
2782 if (!(decl->ctype.modifiers & MOD_STATIC))
2783 decl->ctype.modifiers |= MOD_EXTERN;
2784 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2785 sparse_error(token->pos, "void declaration");
2788 for (;;) {
2789 if (!is_typedef && match_op(token, '=')) {
2790 if (decl->ctype.modifiers & MOD_EXTERN) {
2791 warning(decl->pos, "symbol with external linkage has initializer");
2792 decl->ctype.modifiers &= ~MOD_EXTERN;
2794 token = initializer(&decl->initializer, token->next);
2796 if (!is_typedef) {
2797 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2798 add_symbol(list, decl);
2799 fn_local_symbol(decl);
2802 check_declaration(decl);
2803 if (decl->same_symbol)
2804 decl->definition = decl->same_symbol->definition;
2806 if (!match_op(token, ','))
2807 break;
2809 token = token->next;
2810 ident = NULL;
2811 decl = alloc_symbol(token->pos, SYM_NODE);
2812 ctx.ctype = saved;
2813 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2814 token = declarator(token, &ctx);
2815 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2816 apply_modifiers(token->pos, &ctx);
2817 decl->ctype = ctx.ctype;
2818 decl->ctype.modifiers |= mod;
2819 decl->endpos = token->pos;
2820 if (!ident) {
2821 sparse_error(token->pos, "expected identifier name in type definition");
2822 return token;
2825 if (is_typedef)
2826 decl->ctype.modifiers |= MOD_USERTYPE;
2828 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2830 /* Function declarations are automatically extern unless specifically static */
2831 base_type = decl->ctype.base_type;
2832 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2833 if (!(decl->ctype.modifiers & MOD_STATIC))
2834 decl->ctype.modifiers |= MOD_EXTERN;
2837 return expect(token, ';', "at end of declaration");