10063 basic support for smatch
[unleashed.git] / usr / src / tools / smatch / src / parse.c
blobca4726b80a41003046ece3294b50937b233bafae
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);
76 static struct token *parse_static_assert(struct token *token, struct symbol_list **unused);
78 typedef struct token *attr_t(struct token *, struct symbol *,
79 struct decl_state *);
81 static attr_t
82 attribute_packed, attribute_aligned, attribute_modifier,
83 attribute_bitwise,
84 attribute_address_space, attribute_context,
85 attribute_designated_init,
86 attribute_transparent_union, ignore_attribute,
87 attribute_mode, attribute_force;
89 typedef struct symbol *to_mode_t(struct symbol *);
91 static to_mode_t
92 to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode, to_word_mode;
94 enum {
95 Set_T = 1,
96 Set_S = 2,
97 Set_Char = 4,
98 Set_Int = 8,
99 Set_Double = 16,
100 Set_Float = 32,
101 Set_Signed = 64,
102 Set_Unsigned = 128,
103 Set_Short = 256,
104 Set_Long = 512,
105 Set_Vlong = 1024,
106 Set_Int128 = 2048,
107 Set_Any = Set_T | Set_Short | Set_Long | Set_Signed | Set_Unsigned
110 enum {
111 CInt = 0, CSInt, CUInt, CReal, CChar, CSChar, CUChar,
114 enum {
115 SNone = 0, STypedef, SAuto, SRegister, SExtern, SStatic, SForced, SMax,
118 static struct symbol_op typedef_op = {
119 .type = KW_MODIFIER,
120 .declarator = typedef_specifier,
123 static struct symbol_op inline_op = {
124 .type = KW_MODIFIER,
125 .declarator = inline_specifier,
128 static declarator_t noreturn_specifier;
129 static struct symbol_op noreturn_op = {
130 .type = KW_MODIFIER,
131 .declarator = noreturn_specifier,
134 static declarator_t alignas_specifier;
135 static struct symbol_op alignas_op = {
136 .type = KW_MODIFIER,
137 .declarator = alignas_specifier,
140 static struct symbol_op auto_op = {
141 .type = KW_MODIFIER,
142 .declarator = auto_specifier,
145 static struct symbol_op register_op = {
146 .type = KW_MODIFIER,
147 .declarator = register_specifier,
150 static struct symbol_op static_op = {
151 .type = KW_MODIFIER,
152 .declarator = static_specifier,
155 static struct symbol_op extern_op = {
156 .type = KW_MODIFIER,
157 .declarator = extern_specifier,
160 static struct symbol_op thread_op = {
161 .type = KW_MODIFIER,
162 .declarator = thread_specifier,
165 static struct symbol_op const_op = {
166 .type = KW_QUALIFIER,
167 .declarator = const_qualifier,
170 static struct symbol_op volatile_op = {
171 .type = KW_QUALIFIER,
172 .declarator = volatile_qualifier,
175 static struct symbol_op restrict_op = {
176 .type = KW_QUALIFIER,
179 static struct symbol_op typeof_op = {
180 .type = KW_SPECIFIER,
181 .declarator = typeof_specifier,
182 .test = Set_Any,
183 .set = Set_S|Set_T,
186 static struct symbol_op attribute_op = {
187 .type = KW_ATTRIBUTE,
188 .declarator = attribute_specifier,
191 static struct symbol_op struct_op = {
192 .type = KW_SPECIFIER,
193 .declarator = struct_specifier,
194 .test = Set_Any,
195 .set = Set_S|Set_T,
198 static struct symbol_op union_op = {
199 .type = KW_SPECIFIER,
200 .declarator = union_specifier,
201 .test = Set_Any,
202 .set = Set_S|Set_T,
205 static struct symbol_op enum_op = {
206 .type = KW_SPECIFIER,
207 .declarator = enum_specifier,
208 .test = Set_Any,
209 .set = Set_S|Set_T,
212 static struct symbol_op spec_op = {
213 .type = KW_SPECIFIER | KW_EXACT,
214 .test = Set_Any,
215 .set = Set_S|Set_T,
218 static struct symbol_op char_op = {
219 .type = KW_SPECIFIER,
220 .test = Set_T|Set_Long|Set_Short,
221 .set = Set_T|Set_Char,
222 .class = CChar,
225 static struct symbol_op int_op = {
226 .type = KW_SPECIFIER,
227 .test = Set_T,
228 .set = Set_T|Set_Int,
231 static struct symbol_op double_op = {
232 .type = KW_SPECIFIER,
233 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
234 .set = Set_T|Set_Double,
235 .class = CReal,
238 /* FIXME: this is not even slightly right. */
239 static struct symbol_op complex_op = {
240 .type = KW_SPECIFIER,
241 .test = 0, //Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
242 .set = 0, //Set_Double, //Set_T,Set_Double,
243 .class = CReal,
246 static struct symbol_op float_op = {
247 .type = KW_SPECIFIER | KW_SHORT,
248 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long,
249 .set = Set_T|Set_Float,
250 .class = CReal,
253 static struct symbol_op short_op = {
254 .type = KW_SPECIFIER | KW_SHORT,
255 .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short,
256 .set = Set_Short,
259 static struct symbol_op signed_op = {
260 .type = KW_SPECIFIER,
261 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
262 .set = Set_Signed,
263 .class = CSInt,
266 static struct symbol_op unsigned_op = {
267 .type = KW_SPECIFIER,
268 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
269 .set = Set_Unsigned,
270 .class = CUInt,
273 static struct symbol_op long_op = {
274 .type = KW_SPECIFIER | KW_LONG,
275 .test = Set_S|Set_Char|Set_Float|Set_Short|Set_Vlong,
276 .set = Set_Long,
279 static struct symbol_op int128_op = {
280 .type = KW_SPECIFIER | KW_LONG,
281 .test = Set_S|Set_T|Set_Char|Set_Short|Set_Int|Set_Float|Set_Double|Set_Long|Set_Vlong|Set_Int128,
282 .set = Set_T|Set_Int128,
285 static struct symbol_op if_op = {
286 .statement = parse_if_statement,
289 static struct symbol_op return_op = {
290 .statement = parse_return_statement,
293 static struct symbol_op loop_iter_op = {
294 .statement = parse_loop_iterator,
297 static struct symbol_op default_op = {
298 .statement = parse_default_statement,
301 static struct symbol_op case_op = {
302 .statement = parse_case_statement,
305 static struct symbol_op switch_op = {
306 .statement = parse_switch_statement,
309 static struct symbol_op for_op = {
310 .statement = parse_for_statement,
313 static struct symbol_op while_op = {
314 .statement = parse_while_statement,
317 static struct symbol_op do_op = {
318 .statement = parse_do_statement,
321 static struct symbol_op goto_op = {
322 .statement = parse_goto_statement,
325 static struct symbol_op __context___op = {
326 .statement = parse_context_statement,
329 static struct symbol_op range_op = {
330 .statement = parse_range_statement,
333 static struct symbol_op asm_op = {
334 .type = KW_ASM,
335 .declarator = parse_asm_declarator,
336 .statement = parse_asm_statement,
337 .toplevel = toplevel_asm_declaration,
340 static struct symbol_op static_assert_op = {
341 .toplevel = parse_static_assert,
344 static struct symbol_op packed_op = {
345 .attribute = attribute_packed,
348 static struct symbol_op aligned_op = {
349 .attribute = attribute_aligned,
352 static struct symbol_op attr_mod_op = {
353 .attribute = attribute_modifier,
356 static struct symbol_op attr_bitwise_op = {
357 .attribute = attribute_bitwise,
360 static struct symbol_op attr_force_op = {
361 .attribute = attribute_force,
364 static struct symbol_op address_space_op = {
365 .attribute = attribute_address_space,
368 static struct symbol_op mode_op = {
369 .attribute = attribute_mode,
372 static struct symbol_op context_op = {
373 .attribute = attribute_context,
376 static struct symbol_op designated_init_op = {
377 .attribute = attribute_designated_init,
380 static struct symbol_op transparent_union_op = {
381 .attribute = attribute_transparent_union,
384 static struct symbol_op ignore_attr_op = {
385 .attribute = ignore_attribute,
388 static struct symbol_op mode_QI_op = {
389 .type = KW_MODE,
390 .to_mode = to_QI_mode
393 static struct symbol_op mode_HI_op = {
394 .type = KW_MODE,
395 .to_mode = to_HI_mode
398 static struct symbol_op mode_SI_op = {
399 .type = KW_MODE,
400 .to_mode = to_SI_mode
403 static struct symbol_op mode_DI_op = {
404 .type = KW_MODE,
405 .to_mode = to_DI_mode
408 static struct symbol_op mode_TI_op = {
409 .type = KW_MODE,
410 .to_mode = to_TI_mode
413 static struct symbol_op mode_word_op = {
414 .type = KW_MODE,
415 .to_mode = to_word_mode
418 /* Using NS_TYPEDEF will also make the keyword a reserved one */
419 static struct init_keyword {
420 const char *name;
421 enum namespace ns;
422 unsigned long modifiers;
423 struct symbol_op *op;
424 struct symbol *type;
425 } keyword_table[] = {
426 /* Type qualifiers */
427 { "const", NS_TYPEDEF, .op = &const_op },
428 { "__const", NS_TYPEDEF, .op = &const_op },
429 { "__const__", NS_TYPEDEF, .op = &const_op },
430 { "volatile", NS_TYPEDEF, .op = &volatile_op },
431 { "__volatile", NS_TYPEDEF, .op = &volatile_op },
432 { "__volatile__", NS_TYPEDEF, .op = &volatile_op },
434 /* Typedef.. */
435 { "typedef", NS_TYPEDEF, .op = &typedef_op },
437 /* Type specifiers */
438 { "void", NS_TYPEDEF, .type = &void_ctype, .op = &spec_op},
439 { "char", NS_TYPEDEF, .op = &char_op },
440 { "short", NS_TYPEDEF, .op = &short_op },
441 { "int", NS_TYPEDEF, .op = &int_op },
442 { "long", NS_TYPEDEF, .op = &long_op },
443 { "float", NS_TYPEDEF, .op = &float_op },
444 { "double", NS_TYPEDEF, .op = &double_op },
445 { "signed", NS_TYPEDEF, .op = &signed_op },
446 { "__signed", NS_TYPEDEF, .op = &signed_op },
447 { "__signed__", NS_TYPEDEF, .op = &signed_op },
448 { "unsigned", NS_TYPEDEF, .op = &unsigned_op },
449 { "__int128", NS_TYPEDEF, .op = &int128_op },
450 { "_Bool", NS_TYPEDEF, .type = &bool_ctype, .op = &spec_op },
451 { "_Complex", NS_TYPEDEF, .op = &complex_op },
453 /* Predeclared types */
454 { "__builtin_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
455 { "__builtin_ms_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
456 { "__int128_t", NS_TYPEDEF, .type = &lllong_ctype, .op = &spec_op },
457 { "__uint128_t",NS_TYPEDEF, .type = &ulllong_ctype, .op = &spec_op },
459 /* Extended types */
460 { "typeof", NS_TYPEDEF, .op = &typeof_op },
461 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
462 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
464 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
465 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
467 { "struct", NS_TYPEDEF, .op = &struct_op },
468 { "union", NS_TYPEDEF, .op = &union_op },
469 { "enum", NS_TYPEDEF, .op = &enum_op },
471 { "inline", NS_TYPEDEF, .op = &inline_op },
472 { "__inline", NS_TYPEDEF, .op = &inline_op },
473 { "__inline__", NS_TYPEDEF, .op = &inline_op },
475 { "_Noreturn", NS_TYPEDEF, .op = &noreturn_op },
477 { "_Alignas", NS_TYPEDEF, .op = &alignas_op },
479 /* Ignored for now.. */
480 { "restrict", NS_TYPEDEF, .op = &restrict_op},
481 { "__restrict", NS_TYPEDEF, .op = &restrict_op},
482 { "__restrict__", NS_TYPEDEF, .op = &restrict_op},
484 /* Static assertion */
485 { "_Static_assert", NS_KEYWORD, .op = &static_assert_op },
487 /* Storage class */
488 { "auto", NS_TYPEDEF, .op = &auto_op },
489 { "register", NS_TYPEDEF, .op = &register_op },
490 { "static", NS_TYPEDEF, .op = &static_op },
491 { "extern", NS_TYPEDEF, .op = &extern_op },
492 { "__thread", NS_TYPEDEF, .op = &thread_op },
493 { "_Thread_local", NS_TYPEDEF, .op = &thread_op },
495 /* Statement */
496 { "if", NS_KEYWORD, .op = &if_op },
497 { "return", NS_KEYWORD, .op = &return_op },
498 { "break", NS_KEYWORD, .op = &loop_iter_op },
499 { "continue", NS_KEYWORD, .op = &loop_iter_op },
500 { "default", NS_KEYWORD, .op = &default_op },
501 { "case", NS_KEYWORD, .op = &case_op },
502 { "switch", NS_KEYWORD, .op = &switch_op },
503 { "for", NS_KEYWORD, .op = &for_op },
504 { "while", NS_KEYWORD, .op = &while_op },
505 { "do", NS_KEYWORD, .op = &do_op },
506 { "goto", NS_KEYWORD, .op = &goto_op },
507 { "__context__",NS_KEYWORD, .op = &__context___op },
508 { "__range__", NS_KEYWORD, .op = &range_op },
509 { "asm", NS_KEYWORD, .op = &asm_op },
510 { "__asm", NS_KEYWORD, .op = &asm_op },
511 { "__asm__", NS_KEYWORD, .op = &asm_op },
513 /* Attribute */
514 { "packed", NS_KEYWORD, .op = &packed_op },
515 { "__packed__", NS_KEYWORD, .op = &packed_op },
516 { "aligned", NS_KEYWORD, .op = &aligned_op },
517 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
518 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
519 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
520 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
521 { "force", NS_KEYWORD, .op = &attr_force_op },
522 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_bitwise_op },
523 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_bitwise_op },
524 { "address_space",NS_KEYWORD, .op = &address_space_op },
525 { "mode", NS_KEYWORD, .op = &mode_op },
526 { "context", NS_KEYWORD, .op = &context_op },
527 { "designated_init", NS_KEYWORD, .op = &designated_init_op },
528 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
529 { "noreturn", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
530 { "__noreturn__", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
531 { "pure", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
532 {"__pure__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
533 {"const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
534 {"__const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
535 {"__const__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
537 { "__mode__", NS_KEYWORD, .op = &mode_op },
538 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
539 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
540 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
541 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
542 { "SI", NS_KEYWORD, .op = &mode_SI_op },
543 { "__SI__", NS_KEYWORD, .op = &mode_SI_op },
544 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
545 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
546 { "TI", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
547 { "__TI__", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
548 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
549 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
553 static const char *ignored_attributes[] = {
555 #define GCC_ATTR(x) \
556 STRINGIFY(x), \
557 STRINGIFY(__##x##__),
559 #include "gcc-attr-list.h"
561 #undef GCC_ATTR
563 "bounded",
564 "__bounded__",
565 "__noclone",
566 "__nonnull",
567 "__nothrow",
571 void init_parser(int stream)
573 int i;
574 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
575 struct init_keyword *ptr = keyword_table + i;
576 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
577 sym->ident->keyword = 1;
578 if (ptr->ns == NS_TYPEDEF)
579 sym->ident->reserved = 1;
580 sym->ctype.modifiers = ptr->modifiers;
581 sym->ctype.base_type = ptr->type;
582 sym->op = ptr->op;
585 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
586 const char * name = ignored_attributes[i];
587 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
588 NS_KEYWORD);
589 if (!sym->op) {
590 sym->ident->keyword = 1;
591 sym->op = &ignore_attr_op;
597 // Add a symbol to the list of function-local symbols
598 static void fn_local_symbol(struct symbol *sym)
600 if (function_symbol_list)
601 add_symbol(function_symbol_list, sym);
604 static int SENTINEL_ATTR match_idents(struct token *token, ...)
606 va_list args;
607 struct ident * next;
609 if (token_type(token) != TOKEN_IDENT)
610 return 0;
612 va_start(args, token);
613 do {
614 next = va_arg(args, struct ident *);
615 } while (next && token->ident != next);
616 va_end(args);
618 return next && token->ident == next;
622 struct statement *alloc_statement(struct position pos, int type)
624 struct statement *stmt = __alloc_statement(0);
625 stmt->type = type;
626 stmt->pos = pos;
627 return stmt;
630 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
632 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
634 static void apply_modifiers(struct position pos, struct decl_state *ctx)
636 struct symbol *ctype;
637 if (!ctx->mode)
638 return;
639 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
640 if (!ctype)
641 sparse_error(pos, "don't know how to apply mode to %s",
642 show_typename(ctx->ctype.base_type));
643 else
644 ctx->ctype.base_type = ctype;
648 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
650 struct symbol *sym = alloc_symbol(pos, type);
652 sym->ctype.base_type = ctype->base_type;
653 sym->ctype.modifiers = ctype->modifiers;
655 ctype->base_type = sym;
656 ctype->modifiers = 0;
657 return sym;
661 * NOTE! NS_LABEL is not just a different namespace,
662 * it also ends up using function scope instead of the
663 * regular symbol scope.
665 struct symbol *label_symbol(struct token *token)
667 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
668 if (!sym) {
669 sym = alloc_symbol(token->pos, SYM_LABEL);
670 bind_symbol(sym, token->ident, NS_LABEL);
671 fn_local_symbol(sym);
673 return sym;
676 static struct token *struct_union_enum_specifier(enum type type,
677 struct token *token, struct decl_state *ctx,
678 struct token *(*parse)(struct token *, struct symbol *))
680 struct symbol *sym;
681 struct position *repos;
683 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
684 if (token_type(token) == TOKEN_IDENT) {
685 sym = lookup_symbol(token->ident, NS_STRUCT);
686 if (!sym ||
687 (is_outer_scope(sym->scope) &&
688 (match_op(token->next,';') || match_op(token->next,'{')))) {
689 // Either a new symbol, or else an out-of-scope
690 // symbol being redefined.
691 sym = alloc_symbol(token->pos, type);
692 bind_symbol(sym, token->ident, NS_STRUCT);
694 if (sym->type != type)
695 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
696 ctx->ctype.base_type = sym;
697 repos = &token->pos;
698 token = token->next;
699 if (match_op(token, '{')) {
700 struct decl_state attr = { .ctype.base_type = sym, };
702 // The following test is actually wrong for empty
703 // structs, but (1) they are not C99, (2) gcc does
704 // the same thing, and (3) it's easier.
705 if (sym->symbol_list)
706 error_die(token->pos, "redefinition of %s", show_typename (sym));
707 sym->pos = *repos;
708 token = parse(token->next, sym);
709 token = expect(token, '}', "at end of struct-union-enum-specifier");
711 token = handle_attributes(token, &attr, KW_ATTRIBUTE);
712 apply_ctype(token->pos, &attr.ctype, &sym->ctype);
714 // Mark the structure as needing re-examination
715 sym->examined = 0;
716 sym->endpos = token->pos;
718 return token;
721 // private struct/union/enum type
722 if (!match_op(token, '{')) {
723 sparse_error(token->pos, "expected declaration");
724 ctx->ctype.base_type = &bad_ctype;
725 return token;
728 sym = alloc_symbol(token->pos, type);
729 token = parse(token->next, sym);
730 ctx->ctype.base_type = sym;
731 token = expect(token, '}', "at end of specifier");
732 sym->endpos = token->pos;
734 return token;
737 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
739 struct symbol *field, *last = NULL;
740 struct token *res;
741 res = struct_declaration_list(token, &sym->symbol_list);
742 FOR_EACH_PTR(sym->symbol_list, field) {
743 if (!field->ident) {
744 struct symbol *base = field->ctype.base_type;
745 if (base && base->type == SYM_BITFIELD)
746 continue;
748 if (last)
749 last->next_subobject = field;
750 last = field;
751 } END_FOR_EACH_PTR(field);
752 return res;
755 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
757 return struct_declaration_list(token, &sym->symbol_list);
760 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
762 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
765 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
767 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
771 typedef struct {
772 int x;
773 unsigned long long y;
774 } Num;
776 static void upper_boundary(Num *n, Num *v)
778 if (n->x > v->x)
779 return;
780 if (n->x < v->x) {
781 *n = *v;
782 return;
784 if (n->y < v->y)
785 n->y = v->y;
788 static void lower_boundary(Num *n, Num *v)
790 if (n->x < v->x)
791 return;
792 if (n->x > v->x) {
793 *n = *v;
794 return;
796 if (n->y > v->y)
797 n->y = v->y;
800 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
802 int shift = type->bit_size;
803 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
805 if (!is_unsigned)
806 shift--;
807 if (upper->x == 0 && upper->y >> shift)
808 return 0;
809 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
810 return 1;
811 return 0;
814 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
816 if (s1->bit_size < s2->bit_size) {
817 s1 = s2;
818 } else if (s1->bit_size == s2->bit_size) {
819 if (s2->ctype.modifiers & MOD_UNSIGNED)
820 s1 = s2;
822 if (s1->bit_size < bits_in_int)
823 return &int_ctype;
824 return s1;
827 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
829 struct symbol *sym;
831 FOR_EACH_PTR(list, sym) {
832 struct expression *expr = sym->initializer;
833 struct symbol *ctype;
834 if (expr->type != EXPR_VALUE)
835 continue;
836 ctype = expr->ctype;
837 if (ctype->bit_size == base_type->bit_size)
838 continue;
839 cast_value(expr, base_type, expr, ctype);
840 } END_FOR_EACH_PTR(sym);
843 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
845 unsigned long long lastval = 0;
846 struct symbol *ctype = NULL, *base_type = NULL;
847 Num upper = {-1, 0}, lower = {1, 0};
849 parent->examined = 1;
850 parent->ctype.base_type = &int_ctype;
851 while (token_type(token) == TOKEN_IDENT) {
852 struct expression *expr = NULL;
853 struct token *next = token->next;
854 struct symbol *sym;
856 if (match_op(next, '=')) {
857 next = constant_expression(next->next, &expr);
858 lastval = get_expression_value(expr);
859 ctype = &void_ctype;
860 if (expr && expr->ctype)
861 ctype = expr->ctype;
862 } else if (!ctype) {
863 ctype = &int_ctype;
864 } else if (is_int_type(ctype)) {
865 lastval++;
866 } else {
867 error_die(token->pos, "can't increment the last enum member");
870 if (!expr) {
871 expr = alloc_expression(token->pos, EXPR_VALUE);
872 expr->value = lastval;
873 expr->ctype = ctype;
876 sym = alloc_symbol(token->pos, SYM_NODE);
877 bind_symbol(sym, token->ident, NS_SYMBOL);
878 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
879 sym->initializer = expr;
880 sym->enum_member = 1;
881 sym->ctype.base_type = parent;
882 add_ptr_list(&parent->symbol_list, sym);
884 if (base_type != &bad_ctype) {
885 if (ctype->type == SYM_NODE)
886 ctype = ctype->ctype.base_type;
887 if (ctype->type == SYM_ENUM) {
888 if (ctype == parent)
889 ctype = base_type;
890 else
891 ctype = ctype->ctype.base_type;
894 * base_type rules:
895 * - if all enums are of the same type, then
896 * the base_type is that type (two first
897 * cases)
898 * - if enums are of different types, they
899 * all have to be integer types, and the
900 * base type is at least "int_ctype".
901 * - otherwise the base_type is "bad_ctype".
903 if (!base_type) {
904 base_type = ctype;
905 } else if (ctype == base_type) {
906 /* nothing */
907 } else if (is_int_type(base_type) && is_int_type(ctype)) {
908 base_type = bigger_enum_type(base_type, ctype);
909 } else
910 base_type = &bad_ctype;
911 parent->ctype.base_type = base_type;
913 if (is_int_type(base_type)) {
914 Num v = {.y = lastval};
915 if (ctype->ctype.modifiers & MOD_UNSIGNED)
916 v.x = 0;
917 else if ((long long)lastval >= 0)
918 v.x = 0;
919 else
920 v.x = -1;
921 upper_boundary(&upper, &v);
922 lower_boundary(&lower, &v);
924 token = next;
926 sym->endpos = token->pos;
928 if (!match_op(token, ','))
929 break;
930 token = token->next;
932 if (!base_type) {
933 sparse_error(token->pos, "bad enum definition");
934 base_type = &bad_ctype;
936 else if (!is_int_type(base_type))
937 base_type = base_type;
938 else if (type_is_ok(base_type, &upper, &lower))
939 base_type = base_type;
940 else if (type_is_ok(&int_ctype, &upper, &lower))
941 base_type = &int_ctype;
942 else if (type_is_ok(&uint_ctype, &upper, &lower))
943 base_type = &uint_ctype;
944 else if (type_is_ok(&long_ctype, &upper, &lower))
945 base_type = &long_ctype;
946 else if (type_is_ok(&ulong_ctype, &upper, &lower))
947 base_type = &ulong_ctype;
948 else if (type_is_ok(&llong_ctype, &upper, &lower))
949 base_type = &llong_ctype;
950 else if (type_is_ok(&ullong_ctype, &upper, &lower))
951 base_type = &ullong_ctype;
952 else
953 base_type = &bad_ctype;
954 parent->ctype.base_type = base_type;
955 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
956 parent->examined = 0;
958 cast_enum_list(parent->symbol_list, base_type);
960 return token;
963 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
965 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
966 struct ctype *ctype = &ctx->ctype.base_type->ctype;
968 if (!ctype->base_type)
969 ctype->base_type = &incomplete_ctype;
971 return ret;
974 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
976 struct symbol *sym;
978 if (!match_op(token, '(')) {
979 sparse_error(token->pos, "expected '(' after typeof");
980 return token;
982 if (lookup_type(token->next)) {
983 token = typename(token->next, &sym, NULL);
984 ctx->ctype.base_type = sym->ctype.base_type;
985 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
986 } else {
987 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
988 token = parse_expression(token->next, &typeof_sym->initializer);
990 typeof_sym->endpos = token->pos;
991 if (!typeof_sym->initializer) {
992 sparse_error(token->pos, "expected expression after the '(' token");
993 typeof_sym = &bad_ctype;
995 ctx->ctype.base_type = typeof_sym;
997 return expect(token, ')', "after typeof");
1000 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1002 struct expression *expr = NULL;
1003 if (match_op(token, '('))
1004 token = parens_expression(token, &expr, "in attribute");
1005 return token;
1008 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1010 if (!ctx->ctype.alignment)
1011 ctx->ctype.alignment = 1;
1012 return token;
1015 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1017 int alignment = max_alignment;
1018 struct expression *expr = NULL;
1020 if (match_op(token, '(')) {
1021 token = parens_expression(token, &expr, "in attribute");
1022 if (expr)
1023 alignment = const_expression_value(expr);
1025 if (alignment & (alignment-1)) {
1026 warning(token->pos, "I don't like non-power-of-2 alignments");
1027 return token;
1028 } else if (alignment > ctx->ctype.alignment)
1029 ctx->ctype.alignment = alignment;
1030 return token;
1033 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1035 if (ctx->modifiers & qual)
1036 warning(*pos, "duplicate %s", modifier_string(qual));
1037 ctx->modifiers |= qual;
1040 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1042 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1043 return token;
1046 static struct token *attribute_bitwise(struct token *token, struct symbol *attr, struct decl_state *ctx)
1048 if (Wbitwise)
1049 attribute_modifier(token, attr, ctx);
1050 return token;
1053 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1055 struct expression *expr = NULL;
1056 int as;
1057 token = expect(token, '(', "after address_space attribute");
1058 token = conditional_expression(token, &expr);
1059 if (expr) {
1060 as = const_expression_value(expr);
1061 if (Waddress_space && as)
1062 ctx->ctype.as = as;
1064 token = expect(token, ')', "after address_space attribute");
1065 return token;
1068 static struct symbol *to_QI_mode(struct symbol *ctype)
1070 if (ctype->ctype.base_type != &int_type)
1071 return NULL;
1072 if (ctype == &char_ctype)
1073 return ctype;
1074 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1075 : &schar_ctype;
1078 static struct symbol *to_HI_mode(struct symbol *ctype)
1080 if (ctype->ctype.base_type != &int_type)
1081 return NULL;
1082 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1083 : &sshort_ctype;
1086 static struct symbol *to_SI_mode(struct symbol *ctype)
1088 if (ctype->ctype.base_type != &int_type)
1089 return NULL;
1090 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1091 : &sint_ctype;
1094 static struct symbol *to_DI_mode(struct symbol *ctype)
1096 if (ctype->ctype.base_type != &int_type)
1097 return NULL;
1098 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1099 : &sllong_ctype;
1102 static struct symbol *to_TI_mode(struct symbol *ctype)
1104 if (ctype->ctype.base_type != &int_type)
1105 return NULL;
1106 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1107 : &slllong_ctype;
1110 static struct symbol *to_word_mode(struct symbol *ctype)
1112 if (ctype->ctype.base_type != &int_type)
1113 return NULL;
1114 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1115 : &slong_ctype;
1118 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1120 token = expect(token, '(', "after mode attribute");
1121 if (token_type(token) == TOKEN_IDENT) {
1122 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1123 if (mode && mode->op->type == KW_MODE)
1124 ctx->mode = mode->op;
1125 else
1126 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1127 token = token->next;
1128 } else
1129 sparse_error(token->pos, "expect attribute mode symbol\n");
1130 token = expect(token, ')', "after mode attribute");
1131 return token;
1134 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1136 struct context *context = alloc_context();
1137 struct expression *args[3];
1138 int argc = 0;
1140 token = expect(token, '(', "after context attribute");
1141 while (!match_op(token, ')')) {
1142 struct expression *expr = NULL;
1143 token = conditional_expression(token, &expr);
1144 if (!expr)
1145 break;
1146 if (argc < 3)
1147 args[argc++] = expr;
1148 if (!match_op(token, ','))
1149 break;
1150 token = token->next;
1153 switch(argc) {
1154 case 0:
1155 sparse_error(token->pos, "expected context input/output values");
1156 break;
1157 case 1:
1158 context->in = get_expression_value(args[0]);
1159 break;
1160 case 2:
1161 context->in = get_expression_value(args[0]);
1162 context->out = get_expression_value(args[1]);
1163 break;
1164 case 3:
1165 context->context = args[0];
1166 context->in = get_expression_value(args[1]);
1167 context->out = get_expression_value(args[2]);
1168 break;
1171 if (argc)
1172 add_ptr_list(&ctx->ctype.contexts, context);
1174 token = expect(token, ')', "after context attribute");
1175 return token;
1178 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1180 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1181 ctx->ctype.base_type->designated_init = 1;
1182 else
1183 warning(token->pos, "attribute designated_init applied to non-structure type");
1184 return token;
1187 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1189 if (Wtransparent_union)
1190 warning(token->pos, "attribute __transparent_union__");
1192 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_UNION)
1193 ctx->ctype.base_type->transparent_union = 1;
1194 else
1195 warning(token->pos, "attribute __transparent_union__ applied to non-union type");
1196 return token;
1199 static struct token *recover_unknown_attribute(struct token *token)
1201 struct expression *expr = NULL;
1203 if (Wunknown_attribute)
1204 warning(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1205 token = token->next;
1206 if (match_op(token, '('))
1207 token = parens_expression(token, &expr, "in attribute");
1208 return token;
1211 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1213 token = expect(token, '(', "after attribute");
1214 token = expect(token, '(', "after attribute");
1216 for (;;) {
1217 struct ident *attribute_name;
1218 struct symbol *attr;
1220 if (eof_token(token))
1221 break;
1222 if (match_op(token, ';'))
1223 break;
1224 if (token_type(token) != TOKEN_IDENT)
1225 break;
1226 attribute_name = token->ident;
1227 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1228 if (attr && attr->op->attribute)
1229 token = attr->op->attribute(token->next, attr, ctx);
1230 else
1231 token = recover_unknown_attribute(token);
1233 if (!match_op(token, ','))
1234 break;
1235 token = token->next;
1238 token = expect(token, ')', "after attribute");
1239 token = expect(token, ')', "after attribute");
1240 return token;
1243 static const char *storage_class[] =
1245 [STypedef] = "typedef",
1246 [SAuto] = "auto",
1247 [SExtern] = "extern",
1248 [SStatic] = "static",
1249 [SRegister] = "register",
1250 [SForced] = "[force]"
1253 static unsigned long storage_modifiers(struct decl_state *ctx)
1255 static unsigned long mod[SMax] =
1257 [SAuto] = MOD_AUTO,
1258 [SExtern] = MOD_EXTERN,
1259 [SStatic] = MOD_STATIC,
1260 [SRegister] = MOD_REGISTER
1262 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1263 | (ctx->is_tls ? MOD_TLS : 0);
1266 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1268 /* __thread can be used alone, or with extern or static */
1269 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1270 sparse_error(*pos, "__thread can only be used alone, or with "
1271 "extern or static");
1272 return;
1275 if (!ctx->storage_class) {
1276 ctx->storage_class = class;
1277 return;
1279 if (ctx->storage_class == class)
1280 sparse_error(*pos, "duplicate %s", storage_class[class]);
1281 else
1282 sparse_error(*pos, "multiple storage classes");
1285 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1287 set_storage_class(&next->pos, ctx, STypedef);
1288 return next;
1291 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1293 set_storage_class(&next->pos, ctx, SAuto);
1294 return next;
1297 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1299 set_storage_class(&next->pos, ctx, SRegister);
1300 return next;
1303 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1305 set_storage_class(&next->pos, ctx, SStatic);
1306 return next;
1309 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1311 set_storage_class(&next->pos, ctx, SExtern);
1312 return next;
1315 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1317 /* This GCC extension can be used alone, or with extern or static */
1318 if (!ctx->storage_class || ctx->storage_class == SStatic
1319 || ctx->storage_class == SExtern) {
1320 ctx->is_tls = 1;
1321 } else {
1322 sparse_error(next->pos, "__thread can only be used alone, or "
1323 "with extern or static");
1326 return next;
1329 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1331 set_storage_class(&token->pos, ctx, SForced);
1332 return token;
1335 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1337 ctx->is_inline = 1;
1338 return next;
1341 static struct token *noreturn_specifier(struct token *next, struct decl_state *ctx)
1343 apply_qualifier(&next->pos, &ctx->ctype, MOD_NORETURN);
1344 return next;
1347 static struct token *alignas_specifier(struct token *token, struct decl_state *ctx)
1349 int alignment = 0;
1351 if (!match_op(token, '(')) {
1352 sparse_error(token->pos, "expected '(' after _Alignas");
1353 return token;
1355 if (lookup_type(token->next)) {
1356 struct symbol *sym = NULL;
1357 token = typename(token->next, &sym, NULL);
1358 sym = examine_symbol_type(sym);
1359 alignment = sym->ctype.alignment;
1360 token = expect(token, ')', "after _Alignas(...");
1361 } else {
1362 struct expression *expr = NULL;
1363 token = parens_expression(token, &expr, "after _Alignas");
1364 if (!expr)
1365 return token;
1366 alignment = const_expression_value(expr);
1369 if (alignment < 0) {
1370 warning(token->pos, "non-positive alignment");
1371 return token;
1373 if (alignment & (alignment-1)) {
1374 warning(token->pos, "non-power-of-2 alignment");
1375 return token;
1377 if (alignment > ctx->ctype.alignment)
1378 ctx->ctype.alignment = alignment;
1379 return token;
1382 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1384 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1385 return next;
1388 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1390 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1391 return next;
1394 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1396 unsigned long mod = thistype->modifiers;
1398 if (mod)
1399 apply_qualifier(&pos, ctype, mod);
1401 /* Context */
1402 concat_ptr_list((struct ptr_list *)thistype->contexts,
1403 (struct ptr_list **)&ctype->contexts);
1405 /* Alignment */
1406 if (thistype->alignment > ctype->alignment)
1407 ctype->alignment = thistype->alignment;
1409 /* Address space */
1410 if (thistype->as)
1411 ctype->as = thistype->as;
1414 static void specifier_conflict(struct position pos, int what, struct ident *new)
1416 const char *old;
1417 if (what & (Set_S | Set_T))
1418 goto Catch_all;
1419 if (what & Set_Char)
1420 old = "char";
1421 else if (what & Set_Double)
1422 old = "double";
1423 else if (what & Set_Float)
1424 old = "float";
1425 else if (what & Set_Signed)
1426 old = "signed";
1427 else if (what & Set_Unsigned)
1428 old = "unsigned";
1429 else if (what & Set_Short)
1430 old = "short";
1431 else if (what & Set_Long)
1432 old = "long";
1433 else
1434 old = "long long";
1435 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1436 old, show_ident(new));
1437 return;
1439 Catch_all:
1440 sparse_error(pos, "two or more data types in declaration specifiers");
1443 static struct symbol * const int_types[] =
1444 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype, &lllong_ctype};
1445 static struct symbol * const signed_types[] =
1446 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1447 &slllong_ctype};
1448 static struct symbol * const unsigned_types[] =
1449 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1450 &ulllong_ctype};
1451 static struct symbol * const real_types[] =
1452 {&float_ctype, &double_ctype, &ldouble_ctype};
1453 static struct symbol * const char_types[] =
1454 {&char_ctype, &schar_ctype, &uchar_ctype};
1455 static struct symbol * const * const types[] = {
1456 int_types + 1, signed_types + 1, unsigned_types + 1,
1457 real_types + 1, char_types, char_types + 1, char_types + 2
1460 struct symbol *ctype_integer(int size, int want_unsigned)
1462 return types[want_unsigned ? CUInt : CInt][size];
1465 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1467 while (token_type(t) == TOKEN_IDENT) {
1468 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1469 if (!s)
1470 break;
1471 if (s->type != SYM_KEYWORD)
1472 break;
1473 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1474 break;
1475 t = t->next;
1476 if (s->op->declarator)
1477 t = s->op->declarator(t, ctx);
1479 return t;
1482 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1484 int seen = 0;
1485 int class = CInt;
1486 int size = 0;
1488 while (token_type(token) == TOKEN_IDENT) {
1489 struct symbol *s = lookup_symbol(token->ident,
1490 NS_TYPEDEF | NS_SYMBOL);
1491 if (!s || !(s->namespace & NS_TYPEDEF))
1492 break;
1493 if (s->type != SYM_KEYWORD) {
1494 if (seen & Set_Any)
1495 break;
1496 seen |= Set_S | Set_T;
1497 ctx->ctype.base_type = s->ctype.base_type;
1498 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1499 token = token->next;
1500 continue;
1502 if (s->op->type & KW_SPECIFIER) {
1503 if (seen & s->op->test) {
1504 specifier_conflict(token->pos,
1505 seen & s->op->test,
1506 token->ident);
1507 break;
1509 seen |= s->op->set;
1510 class += s->op->class;
1511 if (s->op->set & Set_Int128)
1512 size = 2;
1513 if (s->op->type & KW_SHORT) {
1514 size = -1;
1515 } else if (s->op->type & KW_LONG && size++) {
1516 if (class == CReal) {
1517 specifier_conflict(token->pos,
1518 Set_Vlong,
1519 &double_ident);
1520 break;
1522 seen |= Set_Vlong;
1525 token = token->next;
1526 if (s->op->declarator)
1527 token = s->op->declarator(token, ctx);
1528 if (s->op->type & KW_EXACT) {
1529 ctx->ctype.base_type = s->ctype.base_type;
1530 ctx->ctype.modifiers |= s->ctype.modifiers;
1534 if (!(seen & Set_S)) { /* not set explicitly? */
1535 struct symbol *base = &incomplete_ctype;
1536 if (seen & Set_Any)
1537 base = types[class][size];
1538 ctx->ctype.base_type = base;
1541 if (ctx->ctype.modifiers & MOD_BITWISE) {
1542 struct symbol *type;
1543 ctx->ctype.modifiers &= ~MOD_BITWISE;
1544 if (!is_int_type(ctx->ctype.base_type)) {
1545 sparse_error(token->pos, "invalid modifier");
1546 return token;
1548 type = alloc_symbol(token->pos, SYM_BASETYPE);
1549 *type = *ctx->ctype.base_type;
1550 type->ctype.modifiers &= ~MOD_SPECIFIER;
1551 type->ctype.base_type = ctx->ctype.base_type;
1552 type->type = SYM_RESTRICT;
1553 ctx->ctype.base_type = type;
1554 create_fouled(type);
1556 return token;
1559 static struct token *abstract_array_static_declarator(struct token *token, int *has_static)
1561 while (token->ident == &static_ident) {
1562 if (*has_static)
1563 sparse_error(token->pos, "duplicate array static declarator");
1565 *has_static = 1;
1566 token = token->next;
1568 return token;
1572 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1574 struct expression *expr = NULL;
1575 int has_static = 0;
1577 token = abstract_array_static_declarator(token, &has_static);
1579 if (match_idents(token, &restrict_ident, &__restrict_ident, &__restrict___ident, NULL))
1580 token = abstract_array_static_declarator(token->next, &has_static);
1581 token = parse_expression(token, &expr);
1582 sym->array_size = expr;
1583 return token;
1586 static struct token *parameter_type_list(struct token *, struct symbol *);
1587 static struct token *identifier_list(struct token *, struct symbol *);
1588 static struct token *declarator(struct token *token, struct decl_state *ctx);
1590 static struct token *skip_attribute(struct token *token)
1592 token = token->next;
1593 if (match_op(token, '(')) {
1594 int depth = 1;
1595 token = token->next;
1596 while (depth && !eof_token(token)) {
1597 if (token_type(token) == TOKEN_SPECIAL) {
1598 if (token->special == '(')
1599 depth++;
1600 else if (token->special == ')')
1601 depth--;
1603 token = token->next;
1606 return token;
1609 static struct token *skip_attributes(struct token *token)
1611 struct symbol *keyword;
1612 for (;;) {
1613 if (token_type(token) != TOKEN_IDENT)
1614 break;
1615 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1616 if (!keyword || keyword->type != SYM_KEYWORD)
1617 break;
1618 if (!(keyword->op->type & KW_ATTRIBUTE))
1619 break;
1620 token = expect(token->next, '(', "after attribute");
1621 token = expect(token, '(', "after attribute");
1622 for (;;) {
1623 if (eof_token(token))
1624 break;
1625 if (match_op(token, ';'))
1626 break;
1627 if (token_type(token) != TOKEN_IDENT)
1628 break;
1629 token = skip_attribute(token);
1630 if (!match_op(token, ','))
1631 break;
1632 token = token->next;
1634 token = expect(token, ')', "after attribute");
1635 token = expect(token, ')', "after attribute");
1637 return token;
1640 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1642 struct symbol *keyword;
1643 for (;;) {
1644 if (token_type(token) != TOKEN_IDENT)
1645 break;
1646 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1647 if (!keyword || keyword->type != SYM_KEYWORD)
1648 break;
1649 if (!(keyword->op->type & keywords))
1650 break;
1651 token = keyword->op->declarator(token->next, ctx);
1652 keywords &= KW_ATTRIBUTE;
1654 return token;
1657 static int is_nested(struct token *token, struct token **p,
1658 int prefer_abstract)
1661 * This can be either a parameter list or a grouping.
1662 * For the direct (non-abstract) case, we know if must be
1663 * a parameter list if we already saw the identifier.
1664 * For the abstract case, we know if must be a parameter
1665 * list if it is empty or starts with a type.
1667 struct token *next = token->next;
1669 *p = next = skip_attributes(next);
1671 if (token_type(next) == TOKEN_IDENT) {
1672 if (lookup_type(next))
1673 return !prefer_abstract;
1674 return 1;
1677 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1678 return 0;
1680 return 1;
1683 enum kind {
1684 Empty, K_R, Proto, Bad_Func,
1687 static enum kind which_func(struct token *token,
1688 struct ident **n,
1689 int prefer_abstract)
1691 struct token *next = token->next;
1693 if (token_type(next) == TOKEN_IDENT) {
1694 if (lookup_type(next))
1695 return Proto;
1696 /* identifier list not in definition; complain */
1697 if (prefer_abstract)
1698 warning(token->pos,
1699 "identifier list not in definition");
1700 return K_R;
1703 if (token_type(next) != TOKEN_SPECIAL)
1704 return Bad_Func;
1706 if (next->special == ')') {
1707 /* don't complain about those */
1708 if (!n || match_op(next->next, ';'))
1709 if (!n || match_op(next->next, ';') || match_op(next->next, ','))
1710 return Empty;
1711 if (Wstrict_prototypes)
1712 warning(next->pos,
1713 "non-ANSI function declaration of function '%s'",
1714 show_ident(*n));
1715 return Empty;
1718 if (next->special == SPECIAL_ELLIPSIS) {
1719 warning(next->pos,
1720 "variadic functions must have one named argument");
1721 return Proto;
1724 return Bad_Func;
1727 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1729 struct ctype *ctype = &ctx->ctype;
1730 struct token *next;
1731 struct ident **p = ctx->ident;
1733 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1734 *ctx->ident = token->ident;
1735 token = token->next;
1736 } else if (match_op(token, '(') &&
1737 is_nested(token, &next, ctx->prefer_abstract)) {
1738 struct symbol *base_type = ctype->base_type;
1739 if (token->next != next)
1740 next = handle_attributes(token->next, ctx,
1741 KW_ATTRIBUTE);
1742 token = declarator(next, ctx);
1743 token = expect(token, ')', "in nested declarator");
1744 while (ctype->base_type != base_type)
1745 ctype = &ctype->base_type->ctype;
1746 p = NULL;
1749 if (match_op(token, '(')) {
1750 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1751 struct symbol *fn;
1752 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1753 token = token->next;
1754 if (kind == K_R)
1755 token = identifier_list(token, fn);
1756 else if (kind == Proto)
1757 token = parameter_type_list(token, fn);
1758 token = expect(token, ')', "in function declarator");
1759 fn->endpos = token->pos;
1760 return token;
1763 while (match_op(token, '[')) {
1764 struct symbol *array;
1765 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1766 token = abstract_array_declarator(token->next, array);
1767 token = expect(token, ']', "in abstract_array_declarator");
1768 array->endpos = token->pos;
1769 ctype = &array->ctype;
1771 return token;
1774 static struct token *pointer(struct token *token, struct decl_state *ctx)
1776 while (match_op(token,'*')) {
1777 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1778 ptr->ctype.modifiers = ctx->ctype.modifiers;
1779 ptr->ctype.base_type = ctx->ctype.base_type;
1780 ptr->ctype.as = ctx->ctype.as;
1781 ptr->ctype.contexts = ctx->ctype.contexts;
1782 ctx->ctype.modifiers = 0;
1783 ctx->ctype.base_type = ptr;
1784 ctx->ctype.as = 0;
1785 ctx->ctype.contexts = NULL;
1786 ctx->ctype.alignment = 0;
1788 token = handle_qualifiers(token->next, ctx);
1789 ctx->ctype.base_type->endpos = token->pos;
1791 return token;
1794 static struct token *declarator(struct token *token, struct decl_state *ctx)
1796 token = pointer(token, ctx);
1797 return direct_declarator(token, ctx);
1800 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1802 struct ctype *ctype = &ctx->ctype;
1803 struct expression *expr;
1804 struct symbol *bitfield;
1805 long long width;
1807 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1808 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1809 show_typename(ctype->base_type));
1810 // Parse this to recover gracefully.
1811 return conditional_expression(token->next, &expr);
1814 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1815 token = conditional_expression(token->next, &expr);
1816 width = const_expression_value(expr);
1817 bitfield->bit_size = width;
1819 if (width < 0 || width > INT_MAX) {
1820 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1821 width = -1;
1822 } else if (*ctx->ident && width == 0) {
1823 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1824 show_ident(*ctx->ident));
1825 width = -1;
1826 } else if (*ctx->ident) {
1827 struct symbol *base_type = bitfield->ctype.base_type;
1828 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1829 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1830 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1831 // Valid values are either {-1;0} or {0}, depending on integer
1832 // representation. The latter makes for very efficient code...
1833 sparse_error(token->pos, "dubious one-bit signed bitfield");
1835 if (Wdefault_bitfield_sign &&
1836 bitfield_type->type != SYM_ENUM &&
1837 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1838 is_signed) {
1839 // The sign of bitfields is unspecified by default.
1840 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1843 bitfield->bit_size = width;
1844 bitfield->endpos = token->pos;
1845 return token;
1848 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1850 struct decl_state ctx = {.prefer_abstract = 0};
1851 struct ctype saved;
1852 unsigned long mod;
1854 token = declaration_specifiers(token, &ctx);
1855 mod = storage_modifiers(&ctx);
1856 saved = ctx.ctype;
1857 for (;;) {
1858 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1859 ctx.ident = &decl->ident;
1861 token = declarator(token, &ctx);
1862 if (match_op(token, ':'))
1863 token = handle_bitfield(token, &ctx);
1865 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1866 apply_modifiers(token->pos, &ctx);
1868 decl->ctype = ctx.ctype;
1869 decl->ctype.modifiers |= mod;
1870 decl->endpos = token->pos;
1871 add_symbol(list, decl);
1872 if (!match_op(token, ','))
1873 break;
1874 token = token->next;
1875 ctx.ctype = saved;
1877 return token;
1880 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1882 while (!match_op(token, '}')) {
1883 if (match_ident(token, &_Static_assert_ident)) {
1884 token = parse_static_assert(token, NULL);
1885 continue;
1887 if (!match_op(token, ';'))
1888 token = declaration_list(token, list);
1889 if (!match_op(token, ';')) {
1890 sparse_error(token->pos, "expected ; at end of declaration");
1891 break;
1893 token = token->next;
1895 return token;
1898 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1900 struct decl_state ctx = {.prefer_abstract = 1};
1902 token = declaration_specifiers(token, &ctx);
1903 ctx.ident = &sym->ident;
1904 token = declarator(token, &ctx);
1905 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1906 apply_modifiers(token->pos, &ctx);
1907 sym->ctype = ctx.ctype;
1908 sym->ctype.modifiers |= storage_modifiers(&ctx);
1909 sym->endpos = token->pos;
1910 sym->forced_arg = ctx.storage_class == SForced;
1911 return token;
1914 struct token *typename(struct token *token, struct symbol **p, int *forced)
1916 struct decl_state ctx = {.prefer_abstract = 1};
1917 int class;
1918 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1919 *p = sym;
1920 token = declaration_specifiers(token, &ctx);
1921 token = declarator(token, &ctx);
1922 apply_modifiers(token->pos, &ctx);
1923 sym->ctype = ctx.ctype;
1924 sym->endpos = token->pos;
1925 class = ctx.storage_class;
1926 if (forced) {
1927 *forced = 0;
1928 if (class == SForced) {
1929 *forced = 1;
1930 class = 0;
1933 if (class)
1934 warning(sym->pos, "storage class in typename (%s %s)",
1935 storage_class[class], show_typename(sym));
1936 return token;
1939 static struct token *parse_underscore_Pragma(struct token *token)
1941 struct token *next;
1943 next = token->next;
1944 if (!match_op(next, '('))
1945 return next;
1946 next = next->next;
1947 if (next->pos.type != TOKEN_STRING)
1948 return next;
1949 next = next->next;
1950 if (!match_op(next, ')'))
1951 return next;
1952 return next->next;
1955 static struct token *expression_statement(struct token *token, struct expression **tree)
1957 if (match_ident(token, &_Pragma_ident))
1958 return parse_underscore_Pragma(token);
1960 token = parse_expression(token, tree);
1961 return expect(token, ';', "at end of statement");
1964 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1965 struct expression_list **inout)
1967 struct expression *expr;
1969 /* Allow empty operands */
1970 if (match_op(token->next, ':') || match_op(token->next, ')'))
1971 return token->next;
1972 do {
1973 struct ident *ident = NULL;
1974 if (match_op(token->next, '[') &&
1975 token_type(token->next->next) == TOKEN_IDENT &&
1976 match_op(token->next->next->next, ']')) {
1977 ident = token->next->next->ident;
1978 token = token->next->next->next;
1980 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1981 token = primary_expression(token->next, &expr);
1982 add_expression(inout, expr);
1983 token = parens_expression(token, &expr, "in asm parameter");
1984 add_expression(inout, expr);
1985 } while (match_op(token, ','));
1986 return token;
1989 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1990 struct expression_list **clobbers)
1992 struct expression *expr;
1994 do {
1995 token = primary_expression(token->next, &expr);
1996 if (expr)
1997 add_expression(clobbers, expr);
1998 } while (match_op(token, ','));
1999 return token;
2002 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
2003 struct symbol_list **labels)
2005 struct symbol *label;
2007 do {
2008 token = token->next; /* skip ':' and ',' */
2009 if (token_type(token) != TOKEN_IDENT)
2010 return token;
2011 label = label_symbol(token);
2012 add_symbol(labels, label);
2013 token = token->next;
2014 } while (match_op(token, ','));
2015 return token;
2018 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
2020 int is_goto = 0;
2022 token = token->next;
2023 stmt->type = STMT_ASM;
2024 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
2025 token = token->next;
2027 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
2028 is_goto = 1;
2029 token = token->next;
2031 token = expect(token, '(', "after asm");
2032 token = parse_expression(token, &stmt->asm_string);
2033 if (match_op(token, ':'))
2034 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
2035 if (match_op(token, ':'))
2036 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
2037 if (match_op(token, ':'))
2038 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
2039 if (is_goto && match_op(token, ':'))
2040 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
2041 token = expect(token, ')', "after asm");
2042 return expect(token, ';', "at end of asm-statement");
2045 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
2047 struct expression *expr;
2048 token = expect(token, '(', "after asm");
2049 token = parse_expression(token->next, &expr);
2050 token = expect(token, ')', "after asm");
2051 return token;
2054 static struct token *parse_static_assert(struct token *token, struct symbol_list **unused)
2056 struct expression *cond = NULL, *message = NULL;
2058 token = expect(token->next, '(', "after _Static_assert");
2059 token = constant_expression(token, &cond);
2060 if (!cond)
2061 sparse_error(token->pos, "Expected constant expression");
2062 token = expect(token, ',', "after conditional expression in _Static_assert");
2063 token = parse_expression(token, &message);
2064 if (!message || message->type != EXPR_STRING) {
2065 struct position pos;
2067 pos = message ? message->pos : token->pos;
2068 sparse_error(pos, "bad or missing string literal");
2069 cond = NULL;
2071 token = expect(token, ')', "after diagnostic message in _Static_assert");
2073 token = expect(token, ';', "after _Static_assert()");
2075 if (cond && !const_expression_value(cond) && cond->type == EXPR_VALUE)
2076 sparse_error(cond->pos, "static assertion failed: %s",
2077 show_string(message->string));
2078 return token;
2081 /* Make a statement out of an expression */
2082 static struct statement *make_statement(struct expression *expr)
2084 struct statement *stmt;
2086 if (!expr)
2087 return NULL;
2088 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
2089 stmt->expression = expr;
2090 return stmt;
2094 * All iterators have two symbols associated with them:
2095 * the "continue" and "break" symbols, which are targets
2096 * for continue and break statements respectively.
2098 * They are in a special name-space, but they follow
2099 * all the normal visibility rules, so nested iterators
2100 * automatically work right.
2102 static void start_iterator(struct statement *stmt)
2104 struct symbol *cont, *brk;
2106 start_symbol_scope(stmt->pos);
2107 cont = alloc_symbol(stmt->pos, SYM_NODE);
2108 bind_symbol(cont, &continue_ident, NS_ITERATOR);
2109 brk = alloc_symbol(stmt->pos, SYM_NODE);
2110 bind_symbol(brk, &break_ident, NS_ITERATOR);
2112 stmt->type = STMT_ITERATOR;
2113 stmt->iterator_break = brk;
2114 stmt->iterator_continue = cont;
2115 fn_local_symbol(brk);
2116 fn_local_symbol(cont);
2119 static void end_iterator(struct statement *stmt)
2121 end_symbol_scope();
2124 static struct statement *start_function(struct symbol *sym)
2126 struct symbol *ret;
2127 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2129 start_function_scope(sym->pos);
2130 ret = alloc_symbol(sym->pos, SYM_NODE);
2131 ret->ctype = sym->ctype.base_type->ctype;
2132 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2133 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2134 bind_symbol(ret, &return_ident, NS_ITERATOR);
2135 stmt->ret = ret;
2136 fn_local_symbol(ret);
2138 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2139 current_fn = sym;
2141 return stmt;
2144 static void end_function(struct symbol *sym)
2146 current_fn = NULL;
2147 end_function_scope();
2151 * A "switch()" statement, like an iterator, has a
2152 * the "break" symbol associated with it. It works
2153 * exactly like the iterator break - it's the target
2154 * for any break-statements in scope, and means that
2155 * "break" handling doesn't even need to know whether
2156 * it's breaking out of an iterator or a switch.
2158 * In addition, the "case" symbol is a marker for the
2159 * case/default statements to find the switch statement
2160 * that they are associated with.
2162 static void start_switch(struct statement *stmt)
2164 struct symbol *brk, *switch_case;
2166 start_symbol_scope(stmt->pos);
2167 brk = alloc_symbol(stmt->pos, SYM_NODE);
2168 bind_symbol(brk, &break_ident, NS_ITERATOR);
2170 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2171 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2172 switch_case->stmt = stmt;
2174 stmt->type = STMT_SWITCH;
2175 stmt->switch_break = brk;
2176 stmt->switch_case = switch_case;
2178 fn_local_symbol(brk);
2179 fn_local_symbol(switch_case);
2182 static void end_switch(struct statement *stmt)
2184 if (!stmt->switch_case->symbol_list)
2185 warning(stmt->pos, "switch with no cases");
2186 end_symbol_scope();
2189 static void add_case_statement(struct statement *stmt)
2191 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2192 struct symbol *sym;
2194 if (!target) {
2195 sparse_error(stmt->pos, "not in switch scope");
2196 stmt->type = STMT_NONE;
2197 return;
2199 sym = alloc_symbol(stmt->pos, SYM_NODE);
2200 add_symbol(&target->symbol_list, sym);
2201 sym->stmt = stmt;
2202 stmt->case_label = sym;
2203 fn_local_symbol(sym);
2206 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2208 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2210 if (!target)
2211 error_die(token->pos, "internal error: return without a function target");
2212 stmt->type = STMT_RETURN;
2213 stmt->ret_target = target;
2214 return expression_statement(token->next, &stmt->ret_value);
2217 static void validate_for_loop_decl(struct symbol *sym)
2219 unsigned long storage = sym->ctype.modifiers & MOD_STORAGE;
2221 if (storage & ~(MOD_AUTO | MOD_REGISTER)) {
2222 const char *name = show_ident(sym->ident);
2223 sparse_error(sym->pos, "non-local var '%s' in for-loop initializer", name);
2224 sym->ctype.modifiers &= ~MOD_STORAGE;
2228 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2230 struct symbol_list *syms;
2231 struct expression *e1, *e2, *e3;
2232 struct statement *iterator;
2234 start_iterator(stmt);
2235 token = expect(token->next, '(', "after 'for'");
2237 syms = NULL;
2238 e1 = NULL;
2239 /* C99 variable declaration? */
2240 if (lookup_type(token)) {
2241 token = external_declaration(token, &syms, validate_for_loop_decl);
2242 } else {
2243 token = parse_expression(token, &e1);
2244 token = expect(token, ';', "in 'for'");
2246 token = parse_expression(token, &e2);
2247 token = expect(token, ';', "in 'for'");
2248 token = parse_expression(token, &e3);
2249 token = expect(token, ')', "in 'for'");
2250 token = statement(token, &iterator);
2252 stmt->iterator_syms = syms;
2253 stmt->iterator_pre_statement = make_statement(e1);
2254 stmt->iterator_pre_condition = e2;
2255 stmt->iterator_post_statement = make_statement(e3);
2256 stmt->iterator_post_condition = NULL;
2257 stmt->iterator_statement = iterator;
2258 end_iterator(stmt);
2260 return token;
2263 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2265 struct expression *expr;
2266 struct statement *iterator;
2268 start_iterator(stmt);
2269 token = parens_expression(token->next, &expr, "after 'while'");
2270 token = statement(token, &iterator);
2272 stmt->iterator_pre_condition = expr;
2273 stmt->iterator_post_condition = NULL;
2274 stmt->iterator_statement = iterator;
2275 end_iterator(stmt);
2277 return token;
2280 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2282 struct expression *expr;
2283 struct statement *iterator;
2285 start_iterator(stmt);
2286 token = statement(token->next, &iterator);
2287 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2288 token = token->next;
2289 else
2290 sparse_error(token->pos, "expected 'while' after 'do'");
2291 token = parens_expression(token, &expr, "after 'do-while'");
2293 stmt->iterator_post_condition = expr;
2294 stmt->iterator_statement = iterator;
2295 end_iterator(stmt);
2297 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2298 warning(iterator->pos, "do-while statement is not a compound statement");
2300 return expect(token, ';', "after statement");
2303 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2305 stmt->type = STMT_IF;
2306 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2307 token = statement(token, &stmt->if_true);
2308 if (token_type(token) != TOKEN_IDENT)
2309 return token;
2310 if (token->ident != &else_ident)
2311 return token;
2312 return statement(token->next, &stmt->if_false);
2315 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2317 stmt->type = STMT_CASE;
2318 token = expect(token, ':', "after default/case");
2319 add_case_statement(stmt);
2320 return statement(token, &stmt->case_statement);
2323 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2325 token = parse_expression(token->next, &stmt->case_expression);
2326 if (match_op(token, SPECIAL_ELLIPSIS))
2327 token = parse_expression(token->next, &stmt->case_to);
2328 return case_statement(token, stmt);
2331 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2333 return case_statement(token->next, stmt);
2336 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2338 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2339 stmt->type = STMT_GOTO;
2340 stmt->goto_label = target;
2341 if (!target)
2342 sparse_error(stmt->pos, "break/continue not in iterator scope");
2343 return expect(token->next, ';', "at end of statement");
2346 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2348 stmt->type = STMT_SWITCH;
2349 start_switch(stmt);
2350 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2351 token = statement(token, &stmt->switch_statement);
2352 end_switch(stmt);
2353 return token;
2356 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2358 stmt->type = STMT_GOTO;
2359 token = token->next;
2360 if (match_op(token, '*')) {
2361 token = parse_expression(token->next, &stmt->goto_expression);
2362 add_statement(&function_computed_goto_list, stmt);
2363 } else if (token_type(token) == TOKEN_IDENT) {
2364 stmt->goto_label = label_symbol(token);
2365 token = token->next;
2366 } else {
2367 sparse_error(token->pos, "Expected identifier or goto expression");
2369 return expect(token, ';', "at end of statement");
2372 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2374 stmt->type = STMT_CONTEXT;
2375 token = parse_expression(token->next, &stmt->expression);
2376 if (stmt->expression->type == EXPR_PREOP
2377 && stmt->expression->op == '('
2378 && stmt->expression->unop->type == EXPR_COMMA) {
2379 struct expression *expr;
2380 expr = stmt->expression->unop;
2381 stmt->context = expr->left;
2382 stmt->expression = expr->right;
2384 return expect(token, ';', "at end of statement");
2387 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2389 stmt->type = STMT_RANGE;
2390 token = assignment_expression(token->next, &stmt->range_expression);
2391 token = expect(token, ',', "after range expression");
2392 token = assignment_expression(token, &stmt->range_low);
2393 token = expect(token, ',', "after low range");
2394 token = assignment_expression(token, &stmt->range_high);
2395 return expect(token, ';', "after range statement");
2398 static struct token *statement(struct token *token, struct statement **tree)
2400 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2402 *tree = stmt;
2403 if (token_type(token) == TOKEN_IDENT) {
2404 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2405 if (s && s->op->statement)
2406 return s->op->statement(token, stmt);
2408 if (match_op(token->next, ':')) {
2409 struct symbol *s = label_symbol(token);
2410 stmt->type = STMT_LABEL;
2411 stmt->label_identifier = s;
2412 if (s->stmt)
2413 sparse_error(stmt->pos, "label '%s' redefined", show_ident(token->ident));
2414 s->stmt = stmt;
2415 token = skip_attributes(token->next->next);
2416 return statement(token, &stmt->label_statement);
2420 if (match_op(token, '{')) {
2421 stmt->type = STMT_COMPOUND;
2422 start_symbol_scope(stmt->pos);
2423 token = compound_statement(token->next, stmt);
2424 end_symbol_scope();
2426 return expect(token, '}', "at end of compound statement");
2429 stmt->type = STMT_EXPRESSION;
2430 return expression_statement(token, &stmt->expression);
2433 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2434 static struct token *label_statement(struct token *token)
2436 while (token_type(token) == TOKEN_IDENT) {
2437 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2438 /* it's block-scope, but we want label namespace */
2439 bind_symbol(sym, token->ident, NS_SYMBOL);
2440 sym->namespace = NS_LABEL;
2441 fn_local_symbol(sym);
2442 token = token->next;
2443 if (!match_op(token, ','))
2444 break;
2445 token = token->next;
2447 return expect(token, ';', "at end of label declaration");
2450 static struct token * statement_list(struct token *token, struct statement_list **list)
2452 int seen_statement = 0;
2453 while (token_type(token) == TOKEN_IDENT &&
2454 token->ident == &__label___ident)
2455 token = label_statement(token->next);
2456 for (;;) {
2457 struct statement * stmt;
2458 if (eof_token(token))
2459 break;
2460 if (match_op(token, '}'))
2461 break;
2462 if (match_ident(token, &_Static_assert_ident)) {
2463 token = parse_static_assert(token, NULL);
2464 continue;
2466 if (lookup_type(token)) {
2467 if (seen_statement) {
2468 warning(token->pos, "mixing declarations and code");
2469 seen_statement = 0;
2471 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2472 token = external_declaration(token, &stmt->declaration, NULL);
2473 } else {
2474 seen_statement = Wdeclarationafterstatement;
2475 token = statement(token, &stmt);
2477 add_statement(list, stmt);
2479 return token;
2482 static struct token *identifier_list(struct token *token, struct symbol *fn)
2484 struct symbol_list **list = &fn->arguments;
2485 for (;;) {
2486 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2487 sym->ident = token->ident;
2488 token = token->next;
2489 sym->endpos = token->pos;
2490 sym->ctype.base_type = &incomplete_ctype;
2491 add_symbol(list, sym);
2492 if (!match_op(token, ',') ||
2493 token_type(token->next) != TOKEN_IDENT ||
2494 lookup_type(token->next))
2495 break;
2496 token = token->next;
2498 return token;
2501 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2503 struct symbol_list **list = &fn->arguments;
2505 for (;;) {
2506 struct symbol *sym;
2508 if (match_op(token, SPECIAL_ELLIPSIS)) {
2509 fn->variadic = 1;
2510 token = token->next;
2511 break;
2514 sym = alloc_symbol(token->pos, SYM_NODE);
2515 token = parameter_declaration(token, sym);
2516 if (sym->ctype.base_type == &void_ctype) {
2517 /* Special case: (void) */
2518 if (!*list && !sym->ident)
2519 break;
2520 warning(token->pos, "void parameter");
2522 add_symbol(list, sym);
2523 if (!match_op(token, ','))
2524 break;
2525 token = token->next;
2527 return token;
2530 struct token *compound_statement(struct token *token, struct statement *stmt)
2532 token = statement_list(token, &stmt->stmts);
2533 return token;
2536 static struct expression *identifier_expression(struct token *token)
2538 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2539 expr->expr_ident = token->ident;
2540 return expr;
2543 static struct expression *index_expression(struct expression *from, struct expression *to)
2545 int idx_from, idx_to;
2546 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2548 idx_from = const_expression_value(from);
2549 idx_to = idx_from;
2550 if (to) {
2551 idx_to = const_expression_value(to);
2552 if (idx_to < idx_from || idx_from < 0)
2553 warning(from->pos, "nonsense array initializer index range");
2555 expr->idx_from = idx_from;
2556 expr->idx_to = idx_to;
2557 return expr;
2560 static struct token *single_initializer(struct expression **ep, struct token *token)
2562 int expect_equal = 0;
2563 struct token *next = token->next;
2564 struct expression **tail = ep;
2565 int nested;
2567 *ep = NULL;
2569 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2570 struct expression *expr = identifier_expression(token);
2571 if (Wold_initializer)
2572 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2573 token = initializer(&expr->ident_expression, next->next);
2574 if (expr->ident_expression)
2575 *ep = expr;
2576 return token;
2579 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2580 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2581 struct expression *expr = identifier_expression(next);
2582 *tail = expr;
2583 tail = &expr->ident_expression;
2584 expect_equal = 1;
2585 token = next->next;
2586 } else if (match_op(token, '[')) {
2587 struct expression *from = NULL, *to = NULL, *expr;
2588 token = constant_expression(token->next, &from);
2589 if (!from) {
2590 sparse_error(token->pos, "Expected constant expression");
2591 break;
2593 if (match_op(token, SPECIAL_ELLIPSIS))
2594 token = constant_expression(token->next, &to);
2595 expr = index_expression(from, to);
2596 *tail = expr;
2597 tail = &expr->idx_expression;
2598 token = expect(token, ']', "at end of initializer index");
2599 if (nested)
2600 expect_equal = 1;
2601 } else {
2602 break;
2605 if (nested && !expect_equal) {
2606 if (!match_op(token, '='))
2607 warning(token->pos, "obsolete array initializer, use C99 syntax");
2608 else
2609 expect_equal = 1;
2611 if (expect_equal)
2612 token = expect(token, '=', "at end of initializer index");
2614 token = initializer(tail, token);
2615 if (!*tail)
2616 *ep = NULL;
2617 return token;
2620 static struct token *initializer_list(struct expression_list **list, struct token *token)
2622 struct expression *expr;
2624 for (;;) {
2625 token = single_initializer(&expr, token);
2626 if (!expr)
2627 break;
2628 add_expression(list, expr);
2629 if (!match_op(token, ','))
2630 break;
2631 token = token->next;
2633 return token;
2636 struct token *initializer(struct expression **tree, struct token *token)
2638 if (match_op(token, '{')) {
2639 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2640 *tree = expr;
2641 token = initializer_list(&expr->expr_list, token->next);
2642 return expect(token, '}', "at end of initializer");
2644 return assignment_expression(token, tree);
2647 static void declare_argument(struct symbol *sym, struct symbol *fn)
2649 if (!sym->ident) {
2650 sparse_error(sym->pos, "no identifier for function argument");
2651 return;
2653 bind_symbol(sym, sym->ident, NS_SYMBOL);
2656 static int is_syscall(struct symbol *sym)
2658 char *macro;
2659 char *name;
2660 int is_syscall = 0;
2662 macro = get_macro_name(sym->pos);
2663 if (macro &&
2664 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
2665 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
2666 is_syscall = 1;
2668 name = sym->ident->name;
2670 if (name && strncmp(name, "sys_", 4) ==0)
2671 is_syscall = 1;
2673 if (name && strncmp(name, "compat_sys_", 11) == 0)
2674 is_syscall = 1;
2676 return is_syscall;
2680 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2681 struct symbol_list **list)
2683 struct symbol_list **old_symbol_list;
2684 struct symbol *base_type = decl->ctype.base_type;
2685 struct statement *stmt, **p;
2686 struct symbol *prev;
2687 struct symbol *arg;
2689 old_symbol_list = function_symbol_list;
2690 if (decl->ctype.modifiers & MOD_INLINE) {
2691 function_symbol_list = &decl->inline_symbol_list;
2692 p = &base_type->inline_stmt;
2693 } else {
2694 function_symbol_list = &decl->symbol_list;
2695 p = &base_type->stmt;
2697 function_computed_target_list = NULL;
2698 function_computed_goto_list = NULL;
2700 if (decl->ctype.modifiers & MOD_EXTERN &&
2701 !(decl->ctype.modifiers & MOD_INLINE) &&
2702 Wexternal_function_has_definition)
2703 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2705 if (!(decl->ctype.modifiers & MOD_STATIC))
2706 decl->ctype.modifiers |= MOD_EXTERN;
2708 stmt = start_function(decl);
2710 *p = stmt;
2711 FOR_EACH_PTR (base_type->arguments, arg) {
2712 declare_argument(arg, base_type);
2713 } END_FOR_EACH_PTR(arg);
2715 token = compound_statement(token->next, stmt);
2717 end_function(decl);
2718 if (!(decl->ctype.modifiers & MOD_INLINE))
2719 add_symbol(list, decl);
2720 else if (is_syscall(decl)) {
2721 add_symbol(list, decl);
2723 printf("parse.c decl: %s\n", decl->ident->name);
2724 char *macro = get_macro_name(decl->pos);
2725 printf("decl macro: %s\n", macro);
2728 check_declaration(decl);
2729 decl->definition = decl;
2730 prev = decl->same_symbol;
2731 if (prev && prev->definition) {
2732 warning(decl->pos, "multiple definitions for function '%s'",
2733 show_ident(decl->ident));
2734 info(prev->definition->pos, " the previous one is here");
2735 } else {
2736 while (prev) {
2737 rebind_scope(prev, decl->scope);
2738 prev->definition = decl;
2739 prev = prev->same_symbol;
2742 function_symbol_list = old_symbol_list;
2743 if (function_computed_goto_list) {
2744 if (!function_computed_target_list)
2745 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2746 else {
2747 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2748 stmt->target_list = function_computed_target_list;
2749 } END_FOR_EACH_PTR(stmt);
2752 return expect(token, '}', "at end of function");
2755 static void promote_k_r_types(struct symbol *arg)
2757 struct symbol *base = arg->ctype.base_type;
2758 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2759 arg->ctype.base_type = &int_ctype;
2763 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2765 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2766 struct symbol *arg;
2768 FOR_EACH_PTR(real_args, arg) {
2769 struct symbol *type;
2771 /* This is quadratic in the number of arguments. We _really_ don't care */
2772 FOR_EACH_PTR(argtypes, type) {
2773 if (type->ident == arg->ident)
2774 goto match;
2775 } END_FOR_EACH_PTR(type);
2776 if (Wimplicit_int) {
2777 sparse_error(arg->pos, "missing type declaration for parameter '%s'",
2778 show_ident(arg->ident));
2780 continue;
2781 match:
2782 type->used = 1;
2783 /* "char" and "short" promote to "int" */
2784 promote_k_r_types(type);
2786 arg->ctype = type->ctype;
2787 } END_FOR_EACH_PTR(arg);
2789 FOR_EACH_PTR(argtypes, arg) {
2790 if (!arg->used)
2791 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2792 } END_FOR_EACH_PTR(arg);
2796 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2797 struct symbol_list **list)
2799 struct symbol_list *args = NULL;
2801 if (Wold_style_definition)
2802 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2804 do {
2805 token = declaration_list(token, &args);
2806 if (!match_op(token, ';')) {
2807 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2808 break;
2810 token = token->next;
2811 } while (lookup_type(token));
2813 apply_k_r_types(args, decl);
2815 if (!match_op(token, '{')) {
2816 sparse_error(token->pos, "expected function body");
2817 return token;
2819 return parse_function_body(token, decl, list);
2822 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2824 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2825 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2826 struct statement *stmt;
2828 anon->ctype.base_type = fn;
2829 stmt = alloc_statement(token->pos, STMT_NONE);
2830 fn->stmt = stmt;
2832 token = parse_asm_statement(token, stmt);
2834 add_symbol(list, anon);
2835 return token;
2838 struct token *external_declaration(struct token *token, struct symbol_list **list,
2839 validate_decl_t validate_decl)
2841 struct ident *ident = NULL;
2842 struct symbol *decl;
2843 struct decl_state ctx = { .ident = &ident };
2844 struct ctype saved;
2845 struct symbol *base_type;
2846 unsigned long mod;
2847 int is_typedef;
2849 if (match_ident(token, &_Pragma_ident))
2850 return parse_underscore_Pragma(token);
2852 /* Top-level inline asm or static assertion? */
2853 if (token_type(token) == TOKEN_IDENT) {
2854 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2855 if (s && s->op->toplevel)
2856 return s->op->toplevel(token, list);
2859 /* Parse declaration-specifiers, if any */
2860 token = declaration_specifiers(token, &ctx);
2861 mod = storage_modifiers(&ctx);
2862 decl = alloc_symbol(token->pos, SYM_NODE);
2863 /* Just a type declaration? */
2864 if (match_op(token, ';')) {
2865 apply_modifiers(token->pos, &ctx);
2866 return token->next;
2869 saved = ctx.ctype;
2870 token = declarator(token, &ctx);
2871 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2872 apply_modifiers(token->pos, &ctx);
2874 decl->ctype = ctx.ctype;
2875 decl->ctype.modifiers |= mod;
2876 decl->endpos = token->pos;
2878 /* Just a type declaration? */
2879 if (!ident) {
2880 warning(token->pos, "missing identifier in declaration");
2881 return expect(token, ';', "at the end of type declaration");
2884 /* type define declaration? */
2885 is_typedef = ctx.storage_class == STypedef;
2887 /* Typedefs don't have meaningful storage */
2888 if (is_typedef)
2889 decl->ctype.modifiers |= MOD_USERTYPE;
2891 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2893 base_type = decl->ctype.base_type;
2895 if (is_typedef) {
2896 if (base_type && !base_type->ident) {
2897 switch (base_type->type) {
2898 case SYM_STRUCT:
2899 case SYM_UNION:
2900 case SYM_ENUM:
2901 case SYM_RESTRICT:
2902 base_type->ident = ident;
2903 break;
2904 default:
2905 break;
2908 } else if (base_type && base_type->type == SYM_FN) {
2909 if (base_type->ctype.base_type == &incomplete_ctype) {
2910 warning(decl->pos, "'%s()' has implicit return type",
2911 show_ident(decl->ident));
2912 base_type->ctype.base_type = &int_ctype;
2914 /* K&R argument declaration? */
2915 if (lookup_type(token))
2916 return parse_k_r_arguments(token, decl, list);
2917 if (match_op(token, '{'))
2918 return parse_function_body(token, decl, list);
2920 if (!(decl->ctype.modifiers & MOD_STATIC))
2921 decl->ctype.modifiers |= MOD_EXTERN;
2922 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2923 sparse_error(token->pos, "void declaration");
2925 if (base_type == &incomplete_ctype) {
2926 warning(decl->pos, "'%s' has implicit type", show_ident(decl->ident));
2927 decl->ctype.base_type = &int_ctype;;
2930 for (;;) {
2931 if (!is_typedef && match_op(token, '=')) {
2932 token = initializer(&decl->initializer, token->next);
2934 if (!is_typedef) {
2935 if (validate_decl)
2936 validate_decl(decl);
2938 if (decl->initializer && decl->ctype.modifiers & MOD_EXTERN) {
2939 warning(decl->pos, "symbol with external linkage has initializer");
2940 decl->ctype.modifiers &= ~MOD_EXTERN;
2943 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2944 add_symbol(list, decl);
2945 fn_local_symbol(decl);
2948 check_declaration(decl);
2949 if (decl->same_symbol) {
2950 decl->definition = decl->same_symbol->definition;
2951 decl->op = decl->same_symbol->op;
2954 if (!match_op(token, ','))
2955 break;
2957 token = token->next;
2958 ident = NULL;
2959 decl = alloc_symbol(token->pos, SYM_NODE);
2960 ctx.ctype = saved;
2961 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2962 token = declarator(token, &ctx);
2963 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2964 apply_modifiers(token->pos, &ctx);
2965 decl->ctype = ctx.ctype;
2966 decl->ctype.modifiers |= mod;
2967 decl->endpos = token->pos;
2968 if (!ident) {
2969 sparse_error(token->pos, "expected identifier name in type definition");
2970 return token;
2973 if (is_typedef)
2974 decl->ctype.modifiers |= MOD_USERTYPE;
2976 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2978 /* Function declarations are automatically extern unless specifically static */
2979 base_type = decl->ctype.base_type;
2980 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2981 if (!(decl->ctype.modifiers & MOD_STATIC))
2982 decl->ctype.modifiers |= MOD_EXTERN;
2985 return expect(token, ';', "at end of declaration");