sparse: ignore __assume_aligned__ attribute
[smatch.git] / parse.c
blob66f9353f824766cb305b0d696f5de1ea7905b97a
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_align",
508 "__alloc_align__",
509 "alloc_size",
510 "__alloc_size__",
511 "always_inline",
512 "__always_inline__",
513 "artificial",
514 "__artificial__",
515 "assume_aligned",
516 "__assume_aligned__",
517 "bounded",
518 "__bounded__",
519 "cdecl",
520 "__cdecl__",
521 "cold",
522 "__cold__",
523 "constructor",
524 "__constructor__",
525 "deprecated",
526 "__deprecated__",
527 "destructor",
528 "__destructor__",
529 "dllexport",
530 "__dllexport__",
531 "dllimport",
532 "__dllimport__",
533 "error",
534 "__error__",
535 "externally_visible",
536 "__externally_visible__",
537 "fastcall",
538 "__fastcall__",
539 "format",
540 "__format__",
541 "format_arg",
542 "__format_arg__",
543 "gnu_inline",
544 "__gnu_inline__",
545 "hot",
546 "__hot__",
547 "hotpatch",
548 "__hotpatch__",
549 "leaf",
550 "__leaf__",
551 "l1_text",
552 "__l1_text__",
553 "l1_data",
554 "__l1_data__",
555 "l2",
556 "__l2__",
557 "malloc",
558 "__malloc__",
559 "may_alias",
560 "__may_alias__",
561 "model",
562 "__model__",
563 "ms_abi",
564 "__ms_abi__",
565 "ms_hook_prologue",
566 "__ms_hook_prologue__",
567 "naked",
568 "__naked__",
569 "no_instrument_function",
570 "__no_instrument_function__",
571 "noclone",
572 "__noclone",
573 "__noclone__",
574 "noinline",
575 "__noinline__",
576 "nonnull",
577 "__nonnull",
578 "__nonnull__",
579 "nothrow",
580 "__nothrow",
581 "__nothrow__",
582 "regparm",
583 "__regparm__",
584 "section",
585 "__section__",
586 "sentinel",
587 "__sentinel__",
588 "signal",
589 "__signal__",
590 "stdcall",
591 "__stdcall__",
592 "syscall_linkage",
593 "__syscall_linkage__",
594 "sysv_abi",
595 "__sysv_abi__",
596 "unused",
597 "__unused__",
598 "used",
599 "__used__",
600 "vector_size",
601 "__vector_size__",
602 "visibility",
603 "__visibility__",
604 "warn_unused_result",
605 "__warn_unused_result__",
606 "warning",
607 "__warning__",
608 "weak",
609 "__weak__",
613 void init_parser(int stream)
615 int i;
616 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
617 struct init_keyword *ptr = keyword_table + i;
618 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
619 sym->ident->keyword = 1;
620 if (ptr->ns == NS_TYPEDEF)
621 sym->ident->reserved = 1;
622 sym->ctype.modifiers = ptr->modifiers;
623 sym->ctype.base_type = ptr->type;
624 sym->op = ptr->op;
627 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
628 const char * name = ignored_attributes[i];
629 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
630 NS_KEYWORD);
631 sym->ident->keyword = 1;
632 sym->op = &ignore_attr_op;
637 // Add a symbol to the list of function-local symbols
638 static void fn_local_symbol(struct symbol *sym)
640 if (function_symbol_list)
641 add_symbol(function_symbol_list, sym);
644 static int SENTINEL_ATTR match_idents(struct token *token, ...)
646 va_list args;
647 struct ident * next;
649 if (token_type(token) != TOKEN_IDENT)
650 return 0;
652 va_start(args, token);
653 do {
654 next = va_arg(args, struct ident *);
655 } while (next && token->ident != next);
656 va_end(args);
658 return next && token->ident == next;
662 struct statement *alloc_statement(struct position pos, int type)
664 struct statement *stmt = __alloc_statement(0);
665 stmt->type = type;
666 stmt->pos = pos;
667 return stmt;
670 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
672 static void apply_modifiers(struct position pos, struct decl_state *ctx)
674 struct symbol *ctype;
675 if (!ctx->mode)
676 return;
677 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
678 if (!ctype)
679 sparse_error(pos, "don't know how to apply mode to %s",
680 show_typename(ctx->ctype.base_type));
681 else
682 ctx->ctype.base_type = ctype;
686 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
688 struct symbol *sym = alloc_symbol(pos, type);
690 sym->ctype.base_type = ctype->base_type;
691 sym->ctype.modifiers = ctype->modifiers;
693 ctype->base_type = sym;
694 ctype->modifiers = 0;
695 return sym;
699 * NOTE! NS_LABEL is not just a different namespace,
700 * it also ends up using function scope instead of the
701 * regular symbol scope.
703 struct symbol *label_symbol(struct token *token)
705 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
706 if (!sym) {
707 sym = alloc_symbol(token->pos, SYM_LABEL);
708 bind_symbol(sym, token->ident, NS_LABEL);
709 fn_local_symbol(sym);
711 return sym;
714 static struct token *struct_union_enum_specifier(enum type type,
715 struct token *token, struct decl_state *ctx,
716 struct token *(*parse)(struct token *, struct symbol *))
718 struct symbol *sym;
719 struct position *repos;
721 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
722 if (token_type(token) == TOKEN_IDENT) {
723 sym = lookup_symbol(token->ident, NS_STRUCT);
724 if (!sym ||
725 (is_outer_scope(sym->scope) &&
726 (match_op(token->next,';') || match_op(token->next,'{')))) {
727 // Either a new symbol, or else an out-of-scope
728 // symbol being redefined.
729 sym = alloc_symbol(token->pos, type);
730 bind_symbol(sym, token->ident, NS_STRUCT);
732 if (sym->type != type)
733 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
734 ctx->ctype.base_type = sym;
735 repos = &token->pos;
736 token = token->next;
737 if (match_op(token, '{')) {
738 // The following test is actually wrong for empty
739 // structs, but (1) they are not C99, (2) gcc does
740 // the same thing, and (3) it's easier.
741 if (sym->symbol_list)
742 error_die(token->pos, "redefinition of %s", show_typename (sym));
743 sym->pos = *repos;
744 token = parse(token->next, sym);
745 token = expect(token, '}', "at end of struct-union-enum-specifier");
747 // Mark the structure as needing re-examination
748 sym->examined = 0;
749 sym->endpos = token->pos;
751 return token;
754 // private struct/union/enum type
755 if (!match_op(token, '{')) {
756 sparse_error(token->pos, "expected declaration");
757 ctx->ctype.base_type = &bad_ctype;
758 return token;
761 sym = alloc_symbol(token->pos, type);
762 token = parse(token->next, sym);
763 ctx->ctype.base_type = sym;
764 token = expect(token, '}', "at end of specifier");
765 sym->endpos = token->pos;
767 return token;
770 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
772 struct symbol *field, *last = NULL;
773 struct token *res;
774 res = struct_declaration_list(token, &sym->symbol_list);
775 FOR_EACH_PTR(sym->symbol_list, field) {
776 if (!field->ident) {
777 struct symbol *base = field->ctype.base_type;
778 if (base && base->type == SYM_BITFIELD)
779 continue;
781 if (last)
782 last->next_subobject = field;
783 last = field;
784 } END_FOR_EACH_PTR(field);
785 return res;
788 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
790 return struct_declaration_list(token, &sym->symbol_list);
793 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
795 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
798 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
800 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
804 typedef struct {
805 int x;
806 unsigned long long y;
807 } Num;
809 static void upper_boundary(Num *n, Num *v)
811 if (n->x > v->x)
812 return;
813 if (n->x < v->x) {
814 *n = *v;
815 return;
817 if (n->y < v->y)
818 n->y = v->y;
821 static void lower_boundary(Num *n, Num *v)
823 if (n->x < v->x)
824 return;
825 if (n->x > v->x) {
826 *n = *v;
827 return;
829 if (n->y > v->y)
830 n->y = v->y;
833 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
835 int shift = type->bit_size;
836 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
838 if (!is_unsigned)
839 shift--;
840 if (upper->x == 0 && upper->y >> shift)
841 return 0;
842 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
843 return 1;
844 return 0;
847 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
849 if (s1->bit_size < s2->bit_size) {
850 s1 = s2;
851 } else if (s1->bit_size == s2->bit_size) {
852 if (s2->ctype.modifiers & MOD_UNSIGNED)
853 s1 = s2;
855 if (s1->bit_size < bits_in_int)
856 return &int_ctype;
857 return s1;
860 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
862 struct symbol *sym;
864 FOR_EACH_PTR(list, sym) {
865 struct expression *expr = sym->initializer;
866 struct symbol *ctype;
867 if (expr->type != EXPR_VALUE)
868 continue;
869 ctype = expr->ctype;
870 if (ctype->bit_size == base_type->bit_size)
871 continue;
872 cast_value(expr, base_type, expr, ctype);
873 } END_FOR_EACH_PTR(sym);
876 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
878 unsigned long long lastval = 0;
879 struct symbol *ctype = NULL, *base_type = NULL;
880 Num upper = {-1, 0}, lower = {1, 0};
882 parent->examined = 1;
883 parent->ctype.base_type = &int_ctype;
884 while (token_type(token) == TOKEN_IDENT) {
885 struct expression *expr = NULL;
886 struct token *next = token->next;
887 struct symbol *sym;
889 if (match_op(next, '=')) {
890 next = constant_expression(next->next, &expr);
891 lastval = get_expression_value(expr);
892 ctype = &void_ctype;
893 if (expr && expr->ctype)
894 ctype = expr->ctype;
895 } else if (!ctype) {
896 ctype = &int_ctype;
897 } else if (is_int_type(ctype)) {
898 lastval++;
899 } else {
900 error_die(token->pos, "can't increment the last enum member");
903 if (!expr) {
904 expr = alloc_expression(token->pos, EXPR_VALUE);
905 expr->value = lastval;
906 expr->ctype = ctype;
909 sym = alloc_symbol(token->pos, SYM_NODE);
910 bind_symbol(sym, token->ident, NS_SYMBOL);
911 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
912 sym->initializer = expr;
913 sym->enum_member = 1;
914 sym->ctype.base_type = parent;
915 add_ptr_list(&parent->symbol_list, sym);
917 if (base_type != &bad_ctype) {
918 if (ctype->type == SYM_NODE)
919 ctype = ctype->ctype.base_type;
920 if (ctype->type == SYM_ENUM) {
921 if (ctype == parent)
922 ctype = base_type;
923 else
924 ctype = ctype->ctype.base_type;
927 * base_type rules:
928 * - if all enums are of the same type, then
929 * the base_type is that type (two first
930 * cases)
931 * - if enums are of different types, they
932 * all have to be integer types, and the
933 * base type is at least "int_ctype".
934 * - otherwise the base_type is "bad_ctype".
936 if (!base_type) {
937 base_type = ctype;
938 } else if (ctype == base_type) {
939 /* nothing */
940 } else if (is_int_type(base_type) && is_int_type(ctype)) {
941 base_type = bigger_enum_type(base_type, ctype);
942 } else
943 base_type = &bad_ctype;
944 parent->ctype.base_type = base_type;
946 if (is_int_type(base_type)) {
947 Num v = {.y = lastval};
948 if (ctype->ctype.modifiers & MOD_UNSIGNED)
949 v.x = 0;
950 else if ((long long)lastval >= 0)
951 v.x = 0;
952 else
953 v.x = -1;
954 upper_boundary(&upper, &v);
955 lower_boundary(&lower, &v);
957 token = next;
959 sym->endpos = token->pos;
961 if (!match_op(token, ','))
962 break;
963 token = token->next;
965 if (!base_type) {
966 sparse_error(token->pos, "bad enum definition");
967 base_type = &bad_ctype;
969 else if (!is_int_type(base_type))
970 base_type = base_type;
971 else if (type_is_ok(base_type, &upper, &lower))
972 base_type = base_type;
973 else if (type_is_ok(&int_ctype, &upper, &lower))
974 base_type = &int_ctype;
975 else if (type_is_ok(&uint_ctype, &upper, &lower))
976 base_type = &uint_ctype;
977 else if (type_is_ok(&long_ctype, &upper, &lower))
978 base_type = &long_ctype;
979 else if (type_is_ok(&ulong_ctype, &upper, &lower))
980 base_type = &ulong_ctype;
981 else if (type_is_ok(&llong_ctype, &upper, &lower))
982 base_type = &llong_ctype;
983 else if (type_is_ok(&ullong_ctype, &upper, &lower))
984 base_type = &ullong_ctype;
985 else
986 base_type = &bad_ctype;
987 parent->ctype.base_type = base_type;
988 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
989 parent->examined = 0;
991 cast_enum_list(parent->symbol_list, base_type);
993 return token;
996 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
998 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
999 struct ctype *ctype = &ctx->ctype.base_type->ctype;
1001 if (!ctype->base_type)
1002 ctype->base_type = &incomplete_ctype;
1004 return ret;
1007 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
1009 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
1011 struct symbol *sym;
1013 if (!match_op(token, '(')) {
1014 sparse_error(token->pos, "expected '(' after typeof");
1015 return token;
1017 if (lookup_type(token->next)) {
1018 token = typename(token->next, &sym, NULL);
1019 ctx->ctype.base_type = sym->ctype.base_type;
1020 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
1021 } else {
1022 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
1023 token = parse_expression(token->next, &typeof_sym->initializer);
1025 typeof_sym->endpos = token->pos;
1026 if (!typeof_sym->initializer) {
1027 sparse_error(token->pos, "expected expression after the '(' token");
1028 typeof_sym = &bad_ctype;
1030 ctx->ctype.base_type = typeof_sym;
1032 return expect(token, ')', "after typeof");
1035 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1037 struct expression *expr = NULL;
1038 if (match_op(token, '('))
1039 token = parens_expression(token, &expr, "in attribute");
1040 return token;
1043 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1045 if (!ctx->ctype.alignment)
1046 ctx->ctype.alignment = 1;
1047 return token;
1050 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1052 int alignment = max_alignment;
1053 struct expression *expr = NULL;
1055 if (match_op(token, '(')) {
1056 token = parens_expression(token, &expr, "in attribute");
1057 if (expr)
1058 alignment = const_expression_value(expr);
1060 if (alignment & (alignment-1)) {
1061 warning(token->pos, "I don't like non-power-of-2 alignments");
1062 return token;
1063 } else if (alignment > ctx->ctype.alignment)
1064 ctx->ctype.alignment = alignment;
1065 return token;
1068 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1070 if (ctx->modifiers & qual)
1071 warning(*pos, "duplicate %s", modifier_string(qual));
1072 ctx->modifiers |= qual;
1075 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1077 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1078 return token;
1081 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1083 struct expression *expr = NULL;
1084 int as;
1085 token = expect(token, '(', "after address_space attribute");
1086 token = conditional_expression(token, &expr);
1087 if (expr) {
1088 as = const_expression_value(expr);
1089 if (Waddress_space && as)
1090 ctx->ctype.as = as;
1092 token = expect(token, ')', "after address_space attribute");
1093 return token;
1096 static struct symbol *to_QI_mode(struct symbol *ctype)
1098 if (ctype->ctype.base_type != &int_type)
1099 return NULL;
1100 if (ctype == &char_ctype)
1101 return ctype;
1102 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1103 : &schar_ctype;
1106 static struct symbol *to_HI_mode(struct symbol *ctype)
1108 if (ctype->ctype.base_type != &int_type)
1109 return NULL;
1110 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1111 : &sshort_ctype;
1114 static struct symbol *to_SI_mode(struct symbol *ctype)
1116 if (ctype->ctype.base_type != &int_type)
1117 return NULL;
1118 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1119 : &sint_ctype;
1122 static struct symbol *to_DI_mode(struct symbol *ctype)
1124 if (ctype->ctype.base_type != &int_type)
1125 return NULL;
1126 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1127 : &sllong_ctype;
1130 static struct symbol *to_TI_mode(struct symbol *ctype)
1132 if (ctype->ctype.base_type != &int_type)
1133 return NULL;
1134 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1135 : &slllong_ctype;
1138 static struct symbol *to_word_mode(struct symbol *ctype)
1140 if (ctype->ctype.base_type != &int_type)
1141 return NULL;
1142 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1143 : &slong_ctype;
1146 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1148 token = expect(token, '(', "after mode attribute");
1149 if (token_type(token) == TOKEN_IDENT) {
1150 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1151 if (mode && mode->op->type == KW_MODE)
1152 ctx->mode = mode->op;
1153 else
1154 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1155 token = token->next;
1156 } else
1157 sparse_error(token->pos, "expect attribute mode symbol\n");
1158 token = expect(token, ')', "after mode attribute");
1159 return token;
1162 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1164 struct context *context = alloc_context();
1165 struct expression *args[3];
1166 int argc = 0;
1168 token = expect(token, '(', "after context attribute");
1169 while (!match_op(token, ')')) {
1170 struct expression *expr = NULL;
1171 token = conditional_expression(token, &expr);
1172 if (!expr)
1173 break;
1174 if (argc < 3)
1175 args[argc++] = expr;
1176 if (!match_op(token, ','))
1177 break;
1178 token = token->next;
1181 switch(argc) {
1182 case 0:
1183 sparse_error(token->pos, "expected context input/output values");
1184 break;
1185 case 1:
1186 context->in = get_expression_value(args[0]);
1187 break;
1188 case 2:
1189 context->in = get_expression_value(args[0]);
1190 context->out = get_expression_value(args[1]);
1191 break;
1192 case 3:
1193 context->context = args[0];
1194 context->in = get_expression_value(args[1]);
1195 context->out = get_expression_value(args[2]);
1196 break;
1199 if (argc)
1200 add_ptr_list(&ctx->ctype.contexts, context);
1202 token = expect(token, ')', "after context attribute");
1203 return token;
1206 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1208 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1209 ctx->ctype.base_type->designated_init = 1;
1210 else
1211 warning(token->pos, "attribute designated_init applied to non-structure type");
1212 return token;
1215 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1217 if (Wtransparent_union)
1218 warning(token->pos, "attribute __transparent_union__");
1220 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_UNION)
1221 ctx->ctype.base_type->transparent_union = 1;
1222 else
1223 warning(token->pos, "attribute __transparent_union__ applied to non-union type");
1224 return token;
1227 static struct token *recover_unknown_attribute(struct token *token)
1229 struct expression *expr = NULL;
1231 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1232 token = token->next;
1233 if (match_op(token, '('))
1234 token = parens_expression(token, &expr, "in attribute");
1235 return token;
1238 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1240 token = expect(token, '(', "after attribute");
1241 token = expect(token, '(', "after attribute");
1243 for (;;) {
1244 struct ident *attribute_name;
1245 struct symbol *attr;
1247 if (eof_token(token))
1248 break;
1249 if (match_op(token, ';'))
1250 break;
1251 if (token_type(token) != TOKEN_IDENT)
1252 break;
1253 attribute_name = token->ident;
1254 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1255 if (attr && attr->op->attribute)
1256 token = attr->op->attribute(token->next, attr, ctx);
1257 else
1258 token = recover_unknown_attribute(token);
1260 if (!match_op(token, ','))
1261 break;
1262 token = token->next;
1265 token = expect(token, ')', "after attribute");
1266 token = expect(token, ')', "after attribute");
1267 return token;
1270 static const char *storage_class[] =
1272 [STypedef] = "typedef",
1273 [SAuto] = "auto",
1274 [SExtern] = "extern",
1275 [SStatic] = "static",
1276 [SRegister] = "register",
1277 [SForced] = "[force]"
1280 static unsigned long storage_modifiers(struct decl_state *ctx)
1282 static unsigned long mod[] =
1284 [SAuto] = MOD_AUTO,
1285 [SExtern] = MOD_EXTERN,
1286 [SStatic] = MOD_STATIC,
1287 [SRegister] = MOD_REGISTER
1289 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1290 | (ctx->is_tls ? MOD_TLS : 0);
1293 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1295 /* __thread can be used alone, or with extern or static */
1296 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1297 sparse_error(*pos, "__thread can only be used alone, or with "
1298 "extern or static");
1299 return;
1302 if (!ctx->storage_class) {
1303 ctx->storage_class = class;
1304 return;
1306 if (ctx->storage_class == class)
1307 sparse_error(*pos, "duplicate %s", storage_class[class]);
1308 else
1309 sparse_error(*pos, "multiple storage classes");
1312 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1314 set_storage_class(&next->pos, ctx, STypedef);
1315 return next;
1318 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1320 set_storage_class(&next->pos, ctx, SAuto);
1321 return next;
1324 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1326 set_storage_class(&next->pos, ctx, SRegister);
1327 return next;
1330 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1332 set_storage_class(&next->pos, ctx, SStatic);
1333 return next;
1336 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1338 set_storage_class(&next->pos, ctx, SExtern);
1339 return next;
1342 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1344 /* This GCC extension can be used alone, or with extern or static */
1345 if (!ctx->storage_class || ctx->storage_class == SStatic
1346 || ctx->storage_class == SExtern) {
1347 ctx->is_tls = 1;
1348 } else {
1349 sparse_error(next->pos, "__thread can only be used alone, or "
1350 "with extern or static");
1353 return next;
1356 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1358 set_storage_class(&token->pos, ctx, SForced);
1359 return token;
1362 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1364 ctx->is_inline = 1;
1365 return next;
1368 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1370 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1371 return next;
1374 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1376 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1377 return next;
1380 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1382 unsigned long mod = thistype->modifiers;
1384 if (mod)
1385 apply_qualifier(&pos, ctype, mod);
1387 /* Context */
1388 concat_ptr_list((struct ptr_list *)thistype->contexts,
1389 (struct ptr_list **)&ctype->contexts);
1391 /* Alignment */
1392 if (thistype->alignment > ctype->alignment)
1393 ctype->alignment = thistype->alignment;
1395 /* Address space */
1396 if (thistype->as)
1397 ctype->as = thistype->as;
1400 static void specifier_conflict(struct position pos, int what, struct ident *new)
1402 const char *old;
1403 if (what & (Set_S | Set_T))
1404 goto Catch_all;
1405 if (what & Set_Char)
1406 old = "char";
1407 else if (what & Set_Double)
1408 old = "double";
1409 else if (what & Set_Float)
1410 old = "float";
1411 else if (what & Set_Signed)
1412 old = "signed";
1413 else if (what & Set_Unsigned)
1414 old = "unsigned";
1415 else if (what & Set_Short)
1416 old = "short";
1417 else if (what & Set_Long)
1418 old = "long";
1419 else
1420 old = "long long";
1421 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1422 old, show_ident(new));
1423 return;
1425 Catch_all:
1426 sparse_error(pos, "two or more data types in declaration specifiers");
1429 static struct symbol * const int_types[] =
1430 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype};
1431 static struct symbol * const signed_types[] =
1432 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1433 &slllong_ctype};
1434 static struct symbol * const unsigned_types[] =
1435 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1436 &ulllong_ctype};
1437 static struct symbol * const real_types[] =
1438 {&float_ctype, &double_ctype, &ldouble_ctype};
1439 static struct symbol * const char_types[] =
1440 {&char_ctype, &schar_ctype, &uchar_ctype};
1441 static struct symbol * const * const types[] = {
1442 int_types + 1, signed_types + 1, unsigned_types + 1,
1443 real_types + 1, char_types, char_types + 1, char_types + 2
1446 struct symbol *ctype_integer(int size, int want_unsigned)
1448 return types[want_unsigned ? CUInt : CInt][size];
1451 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1453 while (token_type(t) == TOKEN_IDENT) {
1454 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1455 if (!s)
1456 break;
1457 if (s->type != SYM_KEYWORD)
1458 break;
1459 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1460 break;
1461 t = t->next;
1462 if (s->op->declarator)
1463 t = s->op->declarator(t, ctx);
1465 return t;
1468 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1470 int seen = 0;
1471 int class = CInt;
1472 int size = 0;
1474 while (token_type(token) == TOKEN_IDENT) {
1475 struct symbol *s = lookup_symbol(token->ident,
1476 NS_TYPEDEF | NS_SYMBOL);
1477 if (!s || !(s->namespace & NS_TYPEDEF))
1478 break;
1479 if (s->type != SYM_KEYWORD) {
1480 if (seen & Set_Any)
1481 break;
1482 seen |= Set_S | Set_T;
1483 ctx->ctype.base_type = s->ctype.base_type;
1484 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1485 token = token->next;
1486 continue;
1488 if (s->op->type & KW_SPECIFIER) {
1489 if (seen & s->op->test) {
1490 specifier_conflict(token->pos,
1491 seen & s->op->test,
1492 token->ident);
1493 break;
1495 seen |= s->op->set;
1496 class += s->op->class;
1497 if (s->op->type & KW_SHORT) {
1498 size = -1;
1499 } else if (s->op->type & KW_LONG && size++) {
1500 if (class == CReal) {
1501 specifier_conflict(token->pos,
1502 Set_Vlong,
1503 &double_ident);
1504 break;
1506 seen |= Set_Vlong;
1509 token = token->next;
1510 if (s->op->declarator)
1511 token = s->op->declarator(token, ctx);
1512 if (s->op->type & KW_EXACT) {
1513 ctx->ctype.base_type = s->ctype.base_type;
1514 ctx->ctype.modifiers |= s->ctype.modifiers;
1518 if (!(seen & Set_S)) { /* not set explicitly? */
1519 struct symbol *base = &incomplete_ctype;
1520 if (seen & Set_Any)
1521 base = types[class][size];
1522 ctx->ctype.base_type = base;
1525 if (ctx->ctype.modifiers & MOD_BITWISE) {
1526 struct symbol *type;
1527 ctx->ctype.modifiers &= ~MOD_BITWISE;
1528 if (!is_int_type(ctx->ctype.base_type)) {
1529 sparse_error(token->pos, "invalid modifier");
1530 return token;
1532 type = alloc_symbol(token->pos, SYM_BASETYPE);
1533 *type = *ctx->ctype.base_type;
1534 type->ctype.modifiers &= ~MOD_SPECIFIER;
1535 type->ctype.base_type = ctx->ctype.base_type;
1536 type->type = SYM_RESTRICT;
1537 ctx->ctype.base_type = type;
1538 create_fouled(type);
1540 return token;
1543 static struct token *abstract_array_static_declarator(struct token *token, int *has_static)
1545 while (token->ident == &static_ident) {
1546 if (*has_static)
1547 sparse_error(token->pos, "duplicate array static declarator");
1549 *has_static = 1;
1550 token = token->next;
1552 return token;
1556 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1558 struct expression *expr = NULL;
1559 int has_static = 0;
1561 token = abstract_array_static_declarator(token, &has_static);
1563 if (match_idents(token, &restrict_ident, &__restrict_ident, &__restrict___ident, NULL))
1564 token = abstract_array_static_declarator(token->next, &has_static);
1565 token = parse_expression(token, &expr);
1566 sym->array_size = expr;
1567 return token;
1570 static struct token *parameter_type_list(struct token *, struct symbol *);
1571 static struct token *identifier_list(struct token *, struct symbol *);
1572 static struct token *declarator(struct token *token, struct decl_state *ctx);
1574 static struct token *skip_attribute(struct token *token)
1576 token = token->next;
1577 if (match_op(token, '(')) {
1578 int depth = 1;
1579 token = token->next;
1580 while (depth && !eof_token(token)) {
1581 if (token_type(token) == TOKEN_SPECIAL) {
1582 if (token->special == '(')
1583 depth++;
1584 else if (token->special == ')')
1585 depth--;
1587 token = token->next;
1590 return token;
1593 static struct token *skip_attributes(struct token *token)
1595 struct symbol *keyword;
1596 for (;;) {
1597 if (token_type(token) != TOKEN_IDENT)
1598 break;
1599 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1600 if (!keyword || keyword->type != SYM_KEYWORD)
1601 break;
1602 if (!(keyword->op->type & KW_ATTRIBUTE))
1603 break;
1604 token = expect(token->next, '(', "after attribute");
1605 token = expect(token, '(', "after attribute");
1606 for (;;) {
1607 if (eof_token(token))
1608 break;
1609 if (match_op(token, ';'))
1610 break;
1611 if (token_type(token) != TOKEN_IDENT)
1612 break;
1613 token = skip_attribute(token);
1614 if (!match_op(token, ','))
1615 break;
1616 token = token->next;
1618 token = expect(token, ')', "after attribute");
1619 token = expect(token, ')', "after attribute");
1621 return token;
1624 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1626 struct symbol *keyword;
1627 for (;;) {
1628 if (token_type(token) != TOKEN_IDENT)
1629 break;
1630 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1631 if (!keyword || keyword->type != SYM_KEYWORD)
1632 break;
1633 if (!(keyword->op->type & keywords))
1634 break;
1635 token = keyword->op->declarator(token->next, ctx);
1636 keywords &= KW_ATTRIBUTE;
1638 return token;
1641 static int is_nested(struct token *token, struct token **p,
1642 int prefer_abstract)
1645 * This can be either a parameter list or a grouping.
1646 * For the direct (non-abstract) case, we know if must be
1647 * a parameter list if we already saw the identifier.
1648 * For the abstract case, we know if must be a parameter
1649 * list if it is empty or starts with a type.
1651 struct token *next = token->next;
1653 *p = next = skip_attributes(next);
1655 if (token_type(next) == TOKEN_IDENT) {
1656 if (lookup_type(next))
1657 return !prefer_abstract;
1658 return 1;
1661 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1662 return 0;
1664 return 1;
1667 enum kind {
1668 Empty, K_R, Proto, Bad_Func,
1671 static enum kind which_func(struct token *token,
1672 struct ident **n,
1673 int prefer_abstract)
1675 struct token *next = token->next;
1677 if (token_type(next) == TOKEN_IDENT) {
1678 if (lookup_type(next))
1679 return Proto;
1680 /* identifier list not in definition; complain */
1681 if (prefer_abstract)
1682 warning(token->pos,
1683 "identifier list not in definition");
1684 return K_R;
1687 if (token_type(next) != TOKEN_SPECIAL)
1688 return Bad_Func;
1690 if (next->special == ')') {
1691 /* don't complain about those */
1692 if (!n || match_op(next->next, ';'))
1693 return Empty;
1694 warning(next->pos,
1695 "non-ANSI function declaration of function '%s'",
1696 show_ident(*n));
1697 return Empty;
1700 if (next->special == SPECIAL_ELLIPSIS) {
1701 warning(next->pos,
1702 "variadic functions must have one named argument");
1703 return Proto;
1706 return Bad_Func;
1709 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1711 struct ctype *ctype = &ctx->ctype;
1712 struct token *next;
1713 struct ident **p = ctx->ident;
1715 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1716 *ctx->ident = token->ident;
1717 token = token->next;
1718 } else if (match_op(token, '(') &&
1719 is_nested(token, &next, ctx->prefer_abstract)) {
1720 struct symbol *base_type = ctype->base_type;
1721 if (token->next != next)
1722 next = handle_attributes(token->next, ctx,
1723 KW_ATTRIBUTE);
1724 token = declarator(next, ctx);
1725 token = expect(token, ')', "in nested declarator");
1726 while (ctype->base_type != base_type)
1727 ctype = &ctype->base_type->ctype;
1728 p = NULL;
1731 if (match_op(token, '(')) {
1732 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1733 struct symbol *fn;
1734 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1735 token = token->next;
1736 if (kind == K_R)
1737 token = identifier_list(token, fn);
1738 else if (kind == Proto)
1739 token = parameter_type_list(token, fn);
1740 token = expect(token, ')', "in function declarator");
1741 fn->endpos = token->pos;
1742 return token;
1745 while (match_op(token, '[')) {
1746 struct symbol *array;
1747 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1748 token = abstract_array_declarator(token->next, array);
1749 token = expect(token, ']', "in abstract_array_declarator");
1750 array->endpos = token->pos;
1751 ctype = &array->ctype;
1753 return token;
1756 static struct token *pointer(struct token *token, struct decl_state *ctx)
1758 while (match_op(token,'*')) {
1759 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1760 ptr->ctype.modifiers = ctx->ctype.modifiers;
1761 ptr->ctype.base_type = ctx->ctype.base_type;
1762 ptr->ctype.as = ctx->ctype.as;
1763 ptr->ctype.contexts = ctx->ctype.contexts;
1764 ctx->ctype.modifiers = 0;
1765 ctx->ctype.base_type = ptr;
1766 ctx->ctype.as = 0;
1767 ctx->ctype.contexts = NULL;
1768 ctx->ctype.alignment = 0;
1770 token = handle_qualifiers(token->next, ctx);
1771 ctx->ctype.base_type->endpos = token->pos;
1773 return token;
1776 static struct token *declarator(struct token *token, struct decl_state *ctx)
1778 token = pointer(token, ctx);
1779 return direct_declarator(token, ctx);
1782 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1784 struct ctype *ctype = &ctx->ctype;
1785 struct expression *expr;
1786 struct symbol *bitfield;
1787 long long width;
1789 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1790 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1791 show_typename(ctype->base_type));
1792 // Parse this to recover gracefully.
1793 return conditional_expression(token->next, &expr);
1796 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1797 token = conditional_expression(token->next, &expr);
1798 width = const_expression_value(expr);
1799 bitfield->bit_size = width;
1801 if (width < 0 || width > INT_MAX) {
1802 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1803 width = -1;
1804 } else if (*ctx->ident && width == 0) {
1805 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1806 show_ident(*ctx->ident));
1807 width = -1;
1808 } else if (*ctx->ident) {
1809 struct symbol *base_type = bitfield->ctype.base_type;
1810 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1811 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1812 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1813 // Valid values are either {-1;0} or {0}, depending on integer
1814 // representation. The latter makes for very efficient code...
1815 sparse_error(token->pos, "dubious one-bit signed bitfield");
1817 if (Wdefault_bitfield_sign &&
1818 bitfield_type->type != SYM_ENUM &&
1819 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1820 is_signed) {
1821 // The sign of bitfields is unspecified by default.
1822 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1825 bitfield->bit_size = width;
1826 bitfield->endpos = token->pos;
1827 return token;
1830 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1832 struct decl_state ctx = {.prefer_abstract = 0};
1833 struct ctype saved;
1834 unsigned long mod;
1836 token = declaration_specifiers(token, &ctx);
1837 mod = storage_modifiers(&ctx);
1838 saved = ctx.ctype;
1839 for (;;) {
1840 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1841 ctx.ident = &decl->ident;
1843 token = declarator(token, &ctx);
1844 if (match_op(token, ':'))
1845 token = handle_bitfield(token, &ctx);
1847 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1848 apply_modifiers(token->pos, &ctx);
1850 decl->ctype = ctx.ctype;
1851 decl->ctype.modifiers |= mod;
1852 decl->endpos = token->pos;
1853 add_symbol(list, decl);
1854 if (!match_op(token, ','))
1855 break;
1856 token = token->next;
1857 ctx.ctype = saved;
1859 return token;
1862 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1864 while (!match_op(token, '}')) {
1865 if (!match_op(token, ';'))
1866 token = declaration_list(token, list);
1867 if (!match_op(token, ';')) {
1868 sparse_error(token->pos, "expected ; at end of declaration");
1869 break;
1871 token = token->next;
1873 return token;
1876 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1878 struct decl_state ctx = {.prefer_abstract = 1};
1880 token = declaration_specifiers(token, &ctx);
1881 ctx.ident = &sym->ident;
1882 token = declarator(token, &ctx);
1883 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1884 apply_modifiers(token->pos, &ctx);
1885 sym->ctype = ctx.ctype;
1886 sym->ctype.modifiers |= storage_modifiers(&ctx);
1887 sym->endpos = token->pos;
1888 sym->forced_arg = ctx.storage_class == SForced;
1889 return token;
1892 struct token *typename(struct token *token, struct symbol **p, int *forced)
1894 struct decl_state ctx = {.prefer_abstract = 1};
1895 int class;
1896 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1897 *p = sym;
1898 token = declaration_specifiers(token, &ctx);
1899 token = declarator(token, &ctx);
1900 apply_modifiers(token->pos, &ctx);
1901 sym->ctype = ctx.ctype;
1902 sym->endpos = token->pos;
1903 class = ctx.storage_class;
1904 if (forced) {
1905 *forced = 0;
1906 if (class == SForced) {
1907 *forced = 1;
1908 class = 0;
1911 if (class)
1912 warning(sym->pos, "storage class in typename (%s %s)",
1913 storage_class[class], show_typename(sym));
1914 return token;
1917 static struct token *expression_statement(struct token *token, struct expression **tree)
1919 token = parse_expression(token, tree);
1920 return expect(token, ';', "at end of statement");
1923 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1924 struct expression_list **inout)
1926 struct expression *expr;
1928 /* Allow empty operands */
1929 if (match_op(token->next, ':') || match_op(token->next, ')'))
1930 return token->next;
1931 do {
1932 struct ident *ident = NULL;
1933 if (match_op(token->next, '[') &&
1934 token_type(token->next->next) == TOKEN_IDENT &&
1935 match_op(token->next->next->next, ']')) {
1936 ident = token->next->next->ident;
1937 token = token->next->next->next;
1939 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1940 token = primary_expression(token->next, &expr);
1941 add_expression(inout, expr);
1942 token = parens_expression(token, &expr, "in asm parameter");
1943 add_expression(inout, expr);
1944 } while (match_op(token, ','));
1945 return token;
1948 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1949 struct expression_list **clobbers)
1951 struct expression *expr;
1953 do {
1954 token = primary_expression(token->next, &expr);
1955 if (expr)
1956 add_expression(clobbers, expr);
1957 } while (match_op(token, ','));
1958 return token;
1961 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
1962 struct symbol_list **labels)
1964 struct symbol *label;
1966 do {
1967 token = token->next; /* skip ':' and ',' */
1968 if (token_type(token) != TOKEN_IDENT)
1969 return token;
1970 label = label_symbol(token);
1971 add_symbol(labels, label);
1972 token = token->next;
1973 } while (match_op(token, ','));
1974 return token;
1977 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1979 int is_goto = 0;
1981 token = token->next;
1982 stmt->type = STMT_ASM;
1983 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1984 token = token->next;
1986 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
1987 is_goto = 1;
1988 token = token->next;
1990 token = expect(token, '(', "after asm");
1991 token = parse_expression(token, &stmt->asm_string);
1992 if (match_op(token, ':'))
1993 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1994 if (match_op(token, ':'))
1995 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1996 if (match_op(token, ':'))
1997 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1998 if (is_goto && match_op(token, ':'))
1999 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
2000 token = expect(token, ')', "after asm");
2001 return expect(token, ';', "at end of asm-statement");
2004 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
2006 struct expression *expr;
2007 token = expect(token, '(', "after asm");
2008 token = parse_expression(token->next, &expr);
2009 token = expect(token, ')', "after asm");
2010 return token;
2013 /* Make a statement out of an expression */
2014 static struct statement *make_statement(struct expression *expr)
2016 struct statement *stmt;
2018 if (!expr)
2019 return NULL;
2020 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
2021 stmt->expression = expr;
2022 return stmt;
2026 * All iterators have two symbols associated with them:
2027 * the "continue" and "break" symbols, which are targets
2028 * for continue and break statements respectively.
2030 * They are in a special name-space, but they follow
2031 * all the normal visibility rules, so nested iterators
2032 * automatically work right.
2034 static void start_iterator(struct statement *stmt)
2036 struct symbol *cont, *brk;
2038 start_symbol_scope();
2039 cont = alloc_symbol(stmt->pos, SYM_NODE);
2040 bind_symbol(cont, &continue_ident, NS_ITERATOR);
2041 brk = alloc_symbol(stmt->pos, SYM_NODE);
2042 bind_symbol(brk, &break_ident, NS_ITERATOR);
2044 stmt->type = STMT_ITERATOR;
2045 stmt->iterator_break = brk;
2046 stmt->iterator_continue = cont;
2047 fn_local_symbol(brk);
2048 fn_local_symbol(cont);
2051 static void end_iterator(struct statement *stmt)
2053 end_symbol_scope();
2056 static struct statement *start_function(struct symbol *sym)
2058 struct symbol *ret;
2059 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2061 start_function_scope();
2062 ret = alloc_symbol(sym->pos, SYM_NODE);
2063 ret->ctype = sym->ctype.base_type->ctype;
2064 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2065 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2066 bind_symbol(ret, &return_ident, NS_ITERATOR);
2067 stmt->ret = ret;
2068 fn_local_symbol(ret);
2070 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2071 current_fn = sym;
2073 return stmt;
2076 static void end_function(struct symbol *sym)
2078 current_fn = NULL;
2079 end_function_scope();
2083 * A "switch()" statement, like an iterator, has a
2084 * the "break" symbol associated with it. It works
2085 * exactly like the iterator break - it's the target
2086 * for any break-statements in scope, and means that
2087 * "break" handling doesn't even need to know whether
2088 * it's breaking out of an iterator or a switch.
2090 * In addition, the "case" symbol is a marker for the
2091 * case/default statements to find the switch statement
2092 * that they are associated with.
2094 static void start_switch(struct statement *stmt)
2096 struct symbol *brk, *switch_case;
2098 start_symbol_scope();
2099 brk = alloc_symbol(stmt->pos, SYM_NODE);
2100 bind_symbol(brk, &break_ident, NS_ITERATOR);
2102 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2103 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2104 switch_case->stmt = stmt;
2106 stmt->type = STMT_SWITCH;
2107 stmt->switch_break = brk;
2108 stmt->switch_case = switch_case;
2110 fn_local_symbol(brk);
2111 fn_local_symbol(switch_case);
2114 static void end_switch(struct statement *stmt)
2116 if (!stmt->switch_case->symbol_list)
2117 warning(stmt->pos, "switch with no cases");
2118 end_symbol_scope();
2121 static void add_case_statement(struct statement *stmt)
2123 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2124 struct symbol *sym;
2126 if (!target) {
2127 sparse_error(stmt->pos, "not in switch scope");
2128 stmt->type = STMT_NONE;
2129 return;
2131 sym = alloc_symbol(stmt->pos, SYM_NODE);
2132 add_symbol(&target->symbol_list, sym);
2133 sym->stmt = stmt;
2134 stmt->case_label = sym;
2135 fn_local_symbol(sym);
2138 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2140 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2142 if (!target)
2143 error_die(token->pos, "internal error: return without a function target");
2144 stmt->type = STMT_RETURN;
2145 stmt->ret_target = target;
2146 return expression_statement(token->next, &stmt->ret_value);
2149 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2151 struct symbol_list *syms;
2152 struct expression *e1, *e2, *e3;
2153 struct statement *iterator;
2155 start_iterator(stmt);
2156 token = expect(token->next, '(', "after 'for'");
2158 syms = NULL;
2159 e1 = NULL;
2160 /* C99 variable declaration? */
2161 if (lookup_type(token)) {
2162 token = external_declaration(token, &syms);
2163 } else {
2164 token = parse_expression(token, &e1);
2165 token = expect(token, ';', "in 'for'");
2167 token = parse_expression(token, &e2);
2168 token = expect(token, ';', "in 'for'");
2169 token = parse_expression(token, &e3);
2170 token = expect(token, ')', "in 'for'");
2171 token = statement(token, &iterator);
2173 stmt->iterator_syms = syms;
2174 stmt->iterator_pre_statement = make_statement(e1);
2175 stmt->iterator_pre_condition = e2;
2176 stmt->iterator_post_statement = make_statement(e3);
2177 stmt->iterator_post_condition = NULL;
2178 stmt->iterator_statement = iterator;
2179 end_iterator(stmt);
2181 return token;
2184 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2186 struct expression *expr;
2187 struct statement *iterator;
2189 start_iterator(stmt);
2190 token = parens_expression(token->next, &expr, "after 'while'");
2191 token = statement(token, &iterator);
2193 stmt->iterator_pre_condition = expr;
2194 stmt->iterator_post_condition = NULL;
2195 stmt->iterator_statement = iterator;
2196 end_iterator(stmt);
2198 return token;
2201 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2203 struct expression *expr;
2204 struct statement *iterator;
2206 start_iterator(stmt);
2207 token = statement(token->next, &iterator);
2208 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2209 token = token->next;
2210 else
2211 sparse_error(token->pos, "expected 'while' after 'do'");
2212 token = parens_expression(token, &expr, "after 'do-while'");
2214 stmt->iterator_post_condition = expr;
2215 stmt->iterator_statement = iterator;
2216 end_iterator(stmt);
2218 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2219 warning(iterator->pos, "do-while statement is not a compound statement");
2221 return expect(token, ';', "after statement");
2224 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2226 stmt->type = STMT_IF;
2227 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2228 token = statement(token, &stmt->if_true);
2229 if (token_type(token) != TOKEN_IDENT)
2230 return token;
2231 if (token->ident != &else_ident)
2232 return token;
2233 return statement(token->next, &stmt->if_false);
2236 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2238 stmt->type = STMT_CASE;
2239 token = expect(token, ':', "after default/case");
2240 add_case_statement(stmt);
2241 return statement(token, &stmt->case_statement);
2244 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2246 token = parse_expression(token->next, &stmt->case_expression);
2247 if (match_op(token, SPECIAL_ELLIPSIS))
2248 token = parse_expression(token->next, &stmt->case_to);
2249 return case_statement(token, stmt);
2252 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2254 return case_statement(token->next, stmt);
2257 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2259 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2260 stmt->type = STMT_GOTO;
2261 stmt->goto_label = target;
2262 if (!target)
2263 sparse_error(stmt->pos, "break/continue not in iterator scope");
2264 return expect(token->next, ';', "at end of statement");
2267 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2269 stmt->type = STMT_SWITCH;
2270 start_switch(stmt);
2271 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2272 token = statement(token, &stmt->switch_statement);
2273 end_switch(stmt);
2274 return token;
2277 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2279 stmt->type = STMT_GOTO;
2280 token = token->next;
2281 if (match_op(token, '*')) {
2282 token = parse_expression(token->next, &stmt->goto_expression);
2283 add_statement(&function_computed_goto_list, stmt);
2284 } else if (token_type(token) == TOKEN_IDENT) {
2285 stmt->goto_label = label_symbol(token);
2286 token = token->next;
2287 } else {
2288 sparse_error(token->pos, "Expected identifier or goto expression");
2290 return expect(token, ';', "at end of statement");
2293 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2295 stmt->type = STMT_CONTEXT;
2296 token = parse_expression(token->next, &stmt->expression);
2297 if (stmt->expression->type == EXPR_PREOP
2298 && stmt->expression->op == '('
2299 && stmt->expression->unop->type == EXPR_COMMA) {
2300 struct expression *expr;
2301 expr = stmt->expression->unop;
2302 stmt->context = expr->left;
2303 stmt->expression = expr->right;
2305 return expect(token, ';', "at end of statement");
2308 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2310 stmt->type = STMT_RANGE;
2311 token = assignment_expression(token->next, &stmt->range_expression);
2312 token = expect(token, ',', "after range expression");
2313 token = assignment_expression(token, &stmt->range_low);
2314 token = expect(token, ',', "after low range");
2315 token = assignment_expression(token, &stmt->range_high);
2316 return expect(token, ';', "after range statement");
2319 static struct token *statement(struct token *token, struct statement **tree)
2321 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2323 *tree = stmt;
2324 if (token_type(token) == TOKEN_IDENT) {
2325 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2326 if (s && s->op->statement)
2327 return s->op->statement(token, stmt);
2329 if (match_op(token->next, ':')) {
2330 struct symbol *s = label_symbol(token);
2331 stmt->type = STMT_LABEL;
2332 stmt->label_identifier = s;
2333 if (s->stmt)
2334 sparse_error(stmt->pos, "label '%s' redefined", show_ident(token->ident));
2335 s->stmt = stmt;
2336 token = skip_attributes(token->next->next);
2337 return statement(token, &stmt->label_statement);
2341 if (match_op(token, '{')) {
2342 stmt->type = STMT_COMPOUND;
2343 start_symbol_scope();
2344 token = compound_statement(token->next, stmt);
2345 end_symbol_scope();
2347 return expect(token, '}', "at end of compound statement");
2350 stmt->type = STMT_EXPRESSION;
2351 return expression_statement(token, &stmt->expression);
2354 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2355 static struct token *label_statement(struct token *token)
2357 while (token_type(token) == TOKEN_IDENT) {
2358 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2359 /* it's block-scope, but we want label namespace */
2360 bind_symbol(sym, token->ident, NS_SYMBOL);
2361 sym->namespace = NS_LABEL;
2362 fn_local_symbol(sym);
2363 token = token->next;
2364 if (!match_op(token, ','))
2365 break;
2366 token = token->next;
2368 return expect(token, ';', "at end of label declaration");
2371 static struct token * statement_list(struct token *token, struct statement_list **list)
2373 int seen_statement = 0;
2374 while (token_type(token) == TOKEN_IDENT &&
2375 token->ident == &__label___ident)
2376 token = label_statement(token->next);
2377 for (;;) {
2378 struct statement * stmt;
2379 if (eof_token(token))
2380 break;
2381 if (match_op(token, '}'))
2382 break;
2383 if (lookup_type(token)) {
2384 if (seen_statement) {
2385 warning(token->pos, "mixing declarations and code");
2386 seen_statement = 0;
2388 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2389 token = external_declaration(token, &stmt->declaration);
2390 } else {
2391 seen_statement = Wdeclarationafterstatement;
2392 token = statement(token, &stmt);
2394 add_statement(list, stmt);
2396 return token;
2399 static struct token *identifier_list(struct token *token, struct symbol *fn)
2401 struct symbol_list **list = &fn->arguments;
2402 for (;;) {
2403 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2404 sym->ident = token->ident;
2405 token = token->next;
2406 sym->endpos = token->pos;
2407 sym->ctype.base_type = &incomplete_ctype;
2408 add_symbol(list, sym);
2409 if (!match_op(token, ',') ||
2410 token_type(token->next) != TOKEN_IDENT ||
2411 lookup_type(token->next))
2412 break;
2413 token = token->next;
2415 return token;
2418 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2420 struct symbol_list **list = &fn->arguments;
2422 for (;;) {
2423 struct symbol *sym;
2425 if (match_op(token, SPECIAL_ELLIPSIS)) {
2426 fn->variadic = 1;
2427 token = token->next;
2428 break;
2431 sym = alloc_symbol(token->pos, SYM_NODE);
2432 token = parameter_declaration(token, sym);
2433 if (sym->ctype.base_type == &void_ctype) {
2434 /* Special case: (void) */
2435 if (!*list && !sym->ident)
2436 break;
2437 warning(token->pos, "void parameter");
2439 add_symbol(list, sym);
2440 if (!match_op(token, ','))
2441 break;
2442 token = token->next;
2444 return token;
2447 struct token *compound_statement(struct token *token, struct statement *stmt)
2449 token = statement_list(token, &stmt->stmts);
2450 return token;
2453 static struct expression *identifier_expression(struct token *token)
2455 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2456 expr->expr_ident = token->ident;
2457 return expr;
2460 static struct expression *index_expression(struct expression *from, struct expression *to)
2462 int idx_from, idx_to;
2463 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2465 idx_from = const_expression_value(from);
2466 idx_to = idx_from;
2467 if (to) {
2468 idx_to = const_expression_value(to);
2469 if (idx_to < idx_from || idx_from < 0)
2470 warning(from->pos, "nonsense array initializer index range");
2472 expr->idx_from = idx_from;
2473 expr->idx_to = idx_to;
2474 return expr;
2477 static struct token *single_initializer(struct expression **ep, struct token *token)
2479 int expect_equal = 0;
2480 struct token *next = token->next;
2481 struct expression **tail = ep;
2482 int nested;
2484 *ep = NULL;
2486 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2487 struct expression *expr = identifier_expression(token);
2488 if (Wold_initializer)
2489 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2490 token = initializer(&expr->ident_expression, next->next);
2491 if (expr->ident_expression)
2492 *ep = expr;
2493 return token;
2496 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2497 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2498 struct expression *expr = identifier_expression(next);
2499 *tail = expr;
2500 tail = &expr->ident_expression;
2501 expect_equal = 1;
2502 token = next->next;
2503 } else if (match_op(token, '[')) {
2504 struct expression *from = NULL, *to = NULL, *expr;
2505 token = constant_expression(token->next, &from);
2506 if (!from) {
2507 sparse_error(token->pos, "Expected constant expression");
2508 break;
2510 if (match_op(token, SPECIAL_ELLIPSIS))
2511 token = constant_expression(token->next, &to);
2512 expr = index_expression(from, to);
2513 *tail = expr;
2514 tail = &expr->idx_expression;
2515 token = expect(token, ']', "at end of initializer index");
2516 if (nested)
2517 expect_equal = 1;
2518 } else {
2519 break;
2522 if (nested && !expect_equal) {
2523 if (!match_op(token, '='))
2524 warning(token->pos, "obsolete array initializer, use C99 syntax");
2525 else
2526 expect_equal = 1;
2528 if (expect_equal)
2529 token = expect(token, '=', "at end of initializer index");
2531 token = initializer(tail, token);
2532 if (!*tail)
2533 *ep = NULL;
2534 return token;
2537 static struct token *initializer_list(struct expression_list **list, struct token *token)
2539 struct expression *expr;
2541 for (;;) {
2542 token = single_initializer(&expr, token);
2543 if (!expr)
2544 break;
2545 add_expression(list, expr);
2546 if (!match_op(token, ','))
2547 break;
2548 token = token->next;
2550 return token;
2553 struct token *initializer(struct expression **tree, struct token *token)
2555 if (match_op(token, '{')) {
2556 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2557 *tree = expr;
2558 token = initializer_list(&expr->expr_list, token->next);
2559 return expect(token, '}', "at end of initializer");
2561 return assignment_expression(token, tree);
2564 static void declare_argument(struct symbol *sym, struct symbol *fn)
2566 if (!sym->ident) {
2567 sparse_error(sym->pos, "no identifier for function argument");
2568 return;
2570 bind_symbol(sym, sym->ident, NS_SYMBOL);
2573 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2574 struct symbol_list **list)
2576 struct symbol_list **old_symbol_list;
2577 struct symbol *base_type = decl->ctype.base_type;
2578 struct statement *stmt, **p;
2579 struct symbol *prev;
2580 struct symbol *arg;
2582 old_symbol_list = function_symbol_list;
2583 if (decl->ctype.modifiers & MOD_INLINE) {
2584 function_symbol_list = &decl->inline_symbol_list;
2585 p = &base_type->inline_stmt;
2586 } else {
2587 function_symbol_list = &decl->symbol_list;
2588 p = &base_type->stmt;
2590 function_computed_target_list = NULL;
2591 function_computed_goto_list = NULL;
2593 if (decl->ctype.modifiers & MOD_EXTERN) {
2594 if (!(decl->ctype.modifiers & MOD_INLINE))
2595 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2597 if (!(decl->ctype.modifiers & MOD_STATIC))
2598 decl->ctype.modifiers |= MOD_EXTERN;
2600 stmt = start_function(decl);
2602 *p = stmt;
2603 FOR_EACH_PTR (base_type->arguments, arg) {
2604 declare_argument(arg, base_type);
2605 } END_FOR_EACH_PTR(arg);
2607 token = compound_statement(token->next, stmt);
2609 end_function(decl);
2610 if (!(decl->ctype.modifiers & MOD_INLINE))
2611 add_symbol(list, decl);
2612 check_declaration(decl);
2613 decl->definition = decl;
2614 prev = decl->same_symbol;
2615 if (prev && prev->definition) {
2616 warning(decl->pos, "multiple definitions for function '%s'",
2617 show_ident(decl->ident));
2618 info(prev->definition->pos, " the previous one is here");
2619 } else {
2620 while (prev) {
2621 rebind_scope(prev, decl->scope);
2622 prev->definition = decl;
2623 prev = prev->same_symbol;
2626 function_symbol_list = old_symbol_list;
2627 if (function_computed_goto_list) {
2628 if (!function_computed_target_list)
2629 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2630 else {
2631 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2632 stmt->target_list = function_computed_target_list;
2633 } END_FOR_EACH_PTR(stmt);
2636 return expect(token, '}', "at end of function");
2639 static void promote_k_r_types(struct symbol *arg)
2641 struct symbol *base = arg->ctype.base_type;
2642 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2643 arg->ctype.base_type = &int_ctype;
2647 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2649 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2650 struct symbol *arg;
2652 FOR_EACH_PTR(real_args, arg) {
2653 struct symbol *type;
2655 /* This is quadratic in the number of arguments. We _really_ don't care */
2656 FOR_EACH_PTR(argtypes, type) {
2657 if (type->ident == arg->ident)
2658 goto match;
2659 } END_FOR_EACH_PTR(type);
2660 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2661 continue;
2662 match:
2663 type->used = 1;
2664 /* "char" and "short" promote to "int" */
2665 promote_k_r_types(type);
2667 arg->ctype = type->ctype;
2668 } END_FOR_EACH_PTR(arg);
2670 FOR_EACH_PTR(argtypes, arg) {
2671 if (!arg->used)
2672 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2673 } END_FOR_EACH_PTR(arg);
2677 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2678 struct symbol_list **list)
2680 struct symbol_list *args = NULL;
2682 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2683 do {
2684 token = declaration_list(token, &args);
2685 if (!match_op(token, ';')) {
2686 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2687 break;
2689 token = token->next;
2690 } while (lookup_type(token));
2692 apply_k_r_types(args, decl);
2694 if (!match_op(token, '{')) {
2695 sparse_error(token->pos, "expected function body");
2696 return token;
2698 return parse_function_body(token, decl, list);
2701 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2703 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2704 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2705 struct statement *stmt;
2707 anon->ctype.base_type = fn;
2708 stmt = alloc_statement(token->pos, STMT_NONE);
2709 fn->stmt = stmt;
2711 token = parse_asm_statement(token, stmt);
2713 add_symbol(list, anon);
2714 return token;
2717 struct token *external_declaration(struct token *token, struct symbol_list **list)
2719 struct ident *ident = NULL;
2720 struct symbol *decl;
2721 struct decl_state ctx = { .ident = &ident };
2722 struct ctype saved;
2723 struct symbol *base_type;
2724 unsigned long mod;
2725 int is_typedef;
2727 /* Top-level inline asm? */
2728 if (token_type(token) == TOKEN_IDENT) {
2729 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2730 if (s && s->op->toplevel)
2731 return s->op->toplevel(token, list);
2734 /* Parse declaration-specifiers, if any */
2735 token = declaration_specifiers(token, &ctx);
2736 mod = storage_modifiers(&ctx);
2737 decl = alloc_symbol(token->pos, SYM_NODE);
2738 /* Just a type declaration? */
2739 if (match_op(token, ';')) {
2740 apply_modifiers(token->pos, &ctx);
2741 return token->next;
2744 saved = ctx.ctype;
2745 token = declarator(token, &ctx);
2746 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2747 apply_modifiers(token->pos, &ctx);
2749 decl->ctype = ctx.ctype;
2750 decl->ctype.modifiers |= mod;
2751 decl->endpos = token->pos;
2753 /* Just a type declaration? */
2754 if (!ident) {
2755 warning(token->pos, "missing identifier in declaration");
2756 return expect(token, ';', "at the end of type declaration");
2759 /* type define declaration? */
2760 is_typedef = ctx.storage_class == STypedef;
2762 /* Typedefs don't have meaningful storage */
2763 if (is_typedef)
2764 decl->ctype.modifiers |= MOD_USERTYPE;
2766 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2768 base_type = decl->ctype.base_type;
2770 if (is_typedef) {
2771 if (base_type && !base_type->ident) {
2772 switch (base_type->type) {
2773 case SYM_STRUCT:
2774 case SYM_UNION:
2775 case SYM_ENUM:
2776 case SYM_RESTRICT:
2777 base_type->ident = ident;
2778 break;
2779 default:
2780 break;
2783 } else if (base_type && base_type->type == SYM_FN) {
2784 /* K&R argument declaration? */
2785 if (lookup_type(token))
2786 return parse_k_r_arguments(token, decl, list);
2787 if (match_op(token, '{'))
2788 return parse_function_body(token, decl, list);
2790 if (!(decl->ctype.modifiers & MOD_STATIC))
2791 decl->ctype.modifiers |= MOD_EXTERN;
2792 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2793 sparse_error(token->pos, "void declaration");
2796 for (;;) {
2797 if (!is_typedef && match_op(token, '=')) {
2798 if (decl->ctype.modifiers & MOD_EXTERN) {
2799 warning(decl->pos, "symbol with external linkage has initializer");
2800 decl->ctype.modifiers &= ~MOD_EXTERN;
2802 token = initializer(&decl->initializer, token->next);
2804 if (!is_typedef) {
2805 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2806 add_symbol(list, decl);
2807 fn_local_symbol(decl);
2810 check_declaration(decl);
2811 if (decl->same_symbol)
2812 decl->definition = decl->same_symbol->definition;
2814 if (!match_op(token, ','))
2815 break;
2817 token = token->next;
2818 ident = NULL;
2819 decl = alloc_symbol(token->pos, SYM_NODE);
2820 ctx.ctype = saved;
2821 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2822 token = declarator(token, &ctx);
2823 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2824 apply_modifiers(token->pos, &ctx);
2825 decl->ctype = ctx.ctype;
2826 decl->ctype.modifiers |= mod;
2827 decl->endpos = token->pos;
2828 if (!ident) {
2829 sparse_error(token->pos, "expected identifier name in type definition");
2830 return token;
2833 if (is_typedef)
2834 decl->ctype.modifiers |= MOD_USERTYPE;
2836 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2838 /* Function declarations are automatically extern unless specifically static */
2839 base_type = decl->ctype.base_type;
2840 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2841 if (!(decl->ctype.modifiers & MOD_STATIC))
2842 decl->ctype.modifiers |= MOD_EXTERN;
2845 return expect(token, ';', "at end of declaration");