constraints: escape SQL statements
[smatch.git] / parse.c
blobab8f4f755aaba57a4716c143587a34900382090d
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 static struct symbol_op float_op = {
239 .type = KW_SPECIFIER | KW_SHORT,
240 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long,
241 .set = Set_T|Set_Float,
242 .class = CReal,
245 static struct symbol_op short_op = {
246 .type = KW_SPECIFIER | KW_SHORT,
247 .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short,
248 .set = Set_Short,
251 static struct symbol_op signed_op = {
252 .type = KW_SPECIFIER,
253 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
254 .set = Set_Signed,
255 .class = CSInt,
258 static struct symbol_op unsigned_op = {
259 .type = KW_SPECIFIER,
260 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
261 .set = Set_Unsigned,
262 .class = CUInt,
265 static struct symbol_op long_op = {
266 .type = KW_SPECIFIER | KW_LONG,
267 .test = Set_S|Set_Char|Set_Float|Set_Short|Set_Vlong,
268 .set = Set_Long,
271 static struct symbol_op int128_op = {
272 .type = KW_SPECIFIER | KW_LONG,
273 .test = Set_S|Set_T|Set_Char|Set_Short|Set_Int|Set_Float|Set_Double|Set_Long|Set_Vlong|Set_Int128,
274 .set = Set_T|Set_Int128,
277 static struct symbol_op if_op = {
278 .statement = parse_if_statement,
281 static struct symbol_op return_op = {
282 .statement = parse_return_statement,
285 static struct symbol_op loop_iter_op = {
286 .statement = parse_loop_iterator,
289 static struct symbol_op default_op = {
290 .statement = parse_default_statement,
293 static struct symbol_op case_op = {
294 .statement = parse_case_statement,
297 static struct symbol_op switch_op = {
298 .statement = parse_switch_statement,
301 static struct symbol_op for_op = {
302 .statement = parse_for_statement,
305 static struct symbol_op while_op = {
306 .statement = parse_while_statement,
309 static struct symbol_op do_op = {
310 .statement = parse_do_statement,
313 static struct symbol_op goto_op = {
314 .statement = parse_goto_statement,
317 static struct symbol_op __context___op = {
318 .statement = parse_context_statement,
321 static struct symbol_op range_op = {
322 .statement = parse_range_statement,
325 static struct symbol_op asm_op = {
326 .type = KW_ASM,
327 .declarator = parse_asm_declarator,
328 .statement = parse_asm_statement,
329 .toplevel = toplevel_asm_declaration,
332 static struct symbol_op static_assert_op = {
333 .toplevel = parse_static_assert,
336 static struct symbol_op packed_op = {
337 .attribute = attribute_packed,
340 static struct symbol_op aligned_op = {
341 .attribute = attribute_aligned,
344 static struct symbol_op attr_mod_op = {
345 .attribute = attribute_modifier,
348 static struct symbol_op attr_bitwise_op = {
349 .attribute = attribute_bitwise,
352 static struct symbol_op attr_force_op = {
353 .attribute = attribute_force,
356 static struct symbol_op address_space_op = {
357 .attribute = attribute_address_space,
360 static struct symbol_op mode_op = {
361 .attribute = attribute_mode,
364 static struct symbol_op context_op = {
365 .attribute = attribute_context,
368 static struct symbol_op designated_init_op = {
369 .attribute = attribute_designated_init,
372 static struct symbol_op transparent_union_op = {
373 .attribute = attribute_transparent_union,
376 static struct symbol_op ignore_attr_op = {
377 .attribute = ignore_attribute,
380 static struct symbol_op mode_QI_op = {
381 .type = KW_MODE,
382 .to_mode = to_QI_mode
385 static struct symbol_op mode_HI_op = {
386 .type = KW_MODE,
387 .to_mode = to_HI_mode
390 static struct symbol_op mode_SI_op = {
391 .type = KW_MODE,
392 .to_mode = to_SI_mode
395 static struct symbol_op mode_DI_op = {
396 .type = KW_MODE,
397 .to_mode = to_DI_mode
400 static struct symbol_op mode_TI_op = {
401 .type = KW_MODE,
402 .to_mode = to_TI_mode
405 static struct symbol_op mode_word_op = {
406 .type = KW_MODE,
407 .to_mode = to_word_mode
410 /* Using NS_TYPEDEF will also make the keyword a reserved one */
411 static struct init_keyword {
412 const char *name;
413 enum namespace ns;
414 unsigned long modifiers;
415 struct symbol_op *op;
416 struct symbol *type;
417 } keyword_table[] = {
418 /* Type qualifiers */
419 { "const", NS_TYPEDEF, .op = &const_op },
420 { "__const", NS_TYPEDEF, .op = &const_op },
421 { "__const__", NS_TYPEDEF, .op = &const_op },
422 { "volatile", NS_TYPEDEF, .op = &volatile_op },
423 { "__volatile", NS_TYPEDEF, .op = &volatile_op },
424 { "__volatile__", NS_TYPEDEF, .op = &volatile_op },
426 /* Typedef.. */
427 { "typedef", NS_TYPEDEF, .op = &typedef_op },
429 /* Type specifiers */
430 { "void", NS_TYPEDEF, .type = &void_ctype, .op = &spec_op},
431 { "char", NS_TYPEDEF, .op = &char_op },
432 { "short", NS_TYPEDEF, .op = &short_op },
433 { "int", NS_TYPEDEF, .op = &int_op },
434 { "long", NS_TYPEDEF, .op = &long_op },
435 { "float", NS_TYPEDEF, .op = &float_op },
436 { "double", NS_TYPEDEF, .op = &double_op },
437 { "signed", NS_TYPEDEF, .op = &signed_op },
438 { "__signed", NS_TYPEDEF, .op = &signed_op },
439 { "__signed__", NS_TYPEDEF, .op = &signed_op },
440 { "unsigned", NS_TYPEDEF, .op = &unsigned_op },
441 { "__int128", NS_TYPEDEF, .op = &int128_op },
442 { "_Bool", NS_TYPEDEF, .type = &bool_ctype, .op = &spec_op },
444 /* Predeclared types */
445 { "__builtin_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
446 { "__builtin_ms_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
447 { "__int128_t", NS_TYPEDEF, .type = &lllong_ctype, .op = &spec_op },
448 { "__uint128_t",NS_TYPEDEF, .type = &ulllong_ctype, .op = &spec_op },
450 /* Extended types */
451 { "typeof", NS_TYPEDEF, .op = &typeof_op },
452 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
453 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
455 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
456 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
458 { "struct", NS_TYPEDEF, .op = &struct_op },
459 { "union", NS_TYPEDEF, .op = &union_op },
460 { "enum", NS_TYPEDEF, .op = &enum_op },
462 { "inline", NS_TYPEDEF, .op = &inline_op },
463 { "__inline", NS_TYPEDEF, .op = &inline_op },
464 { "__inline__", NS_TYPEDEF, .op = &inline_op },
466 { "_Noreturn", NS_TYPEDEF, .op = &noreturn_op },
468 { "_Alignas", NS_TYPEDEF, .op = &alignas_op },
470 /* Ignored for now.. */
471 { "restrict", NS_TYPEDEF, .op = &restrict_op},
472 { "__restrict", NS_TYPEDEF, .op = &restrict_op},
473 { "__restrict__", NS_TYPEDEF, .op = &restrict_op},
475 /* Static assertion */
476 { "_Static_assert", NS_KEYWORD, .op = &static_assert_op },
478 /* Storage class */
479 { "auto", NS_TYPEDEF, .op = &auto_op },
480 { "register", NS_TYPEDEF, .op = &register_op },
481 { "static", NS_TYPEDEF, .op = &static_op },
482 { "extern", NS_TYPEDEF, .op = &extern_op },
483 { "__thread", NS_TYPEDEF, .op = &thread_op },
484 { "_Thread_local", NS_TYPEDEF, .op = &thread_op },
486 /* Statement */
487 { "if", NS_KEYWORD, .op = &if_op },
488 { "return", NS_KEYWORD, .op = &return_op },
489 { "break", NS_KEYWORD, .op = &loop_iter_op },
490 { "continue", NS_KEYWORD, .op = &loop_iter_op },
491 { "default", NS_KEYWORD, .op = &default_op },
492 { "case", NS_KEYWORD, .op = &case_op },
493 { "switch", NS_KEYWORD, .op = &switch_op },
494 { "for", NS_KEYWORD, .op = &for_op },
495 { "while", NS_KEYWORD, .op = &while_op },
496 { "do", NS_KEYWORD, .op = &do_op },
497 { "goto", NS_KEYWORD, .op = &goto_op },
498 { "__context__",NS_KEYWORD, .op = &__context___op },
499 { "__range__", NS_KEYWORD, .op = &range_op },
500 { "asm", NS_KEYWORD, .op = &asm_op },
501 { "__asm", NS_KEYWORD, .op = &asm_op },
502 { "__asm__", NS_KEYWORD, .op = &asm_op },
504 /* Attribute */
505 { "packed", NS_KEYWORD, .op = &packed_op },
506 { "__packed__", NS_KEYWORD, .op = &packed_op },
507 { "aligned", NS_KEYWORD, .op = &aligned_op },
508 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
509 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
510 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
511 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
512 { "force", NS_KEYWORD, .op = &attr_force_op },
513 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_bitwise_op },
514 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_bitwise_op },
515 { "address_space",NS_KEYWORD, .op = &address_space_op },
516 { "mode", NS_KEYWORD, .op = &mode_op },
517 { "context", NS_KEYWORD, .op = &context_op },
518 { "designated_init", NS_KEYWORD, .op = &designated_init_op },
519 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
520 { "noreturn", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
521 { "__noreturn__", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
522 { "pure", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
523 {"__pure__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
524 {"const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
525 {"__const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
526 {"__const__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
528 { "__mode__", NS_KEYWORD, .op = &mode_op },
529 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
530 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
531 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
532 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
533 { "SI", NS_KEYWORD, .op = &mode_SI_op },
534 { "__SI__", NS_KEYWORD, .op = &mode_SI_op },
535 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
536 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
537 { "TI", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
538 { "__TI__", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
539 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
540 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
544 static const char *ignored_attributes[] = {
546 #define GCC_ATTR(x) \
547 STRINGIFY(x), \
548 STRINGIFY(__##x##__),
550 #include "gcc-attr-list.h"
552 #undef GCC_ATTR
554 "bounded",
555 "__bounded__",
556 "__noclone",
557 "__nonnull",
558 "__nothrow",
562 void init_parser(int stream)
564 int i;
565 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
566 struct init_keyword *ptr = keyword_table + i;
567 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
568 sym->ident->keyword = 1;
569 if (ptr->ns == NS_TYPEDEF)
570 sym->ident->reserved = 1;
571 sym->ctype.modifiers = ptr->modifiers;
572 sym->ctype.base_type = ptr->type;
573 sym->op = ptr->op;
576 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
577 const char * name = ignored_attributes[i];
578 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
579 NS_KEYWORD);
580 if (!sym->op) {
581 sym->ident->keyword = 1;
582 sym->op = &ignore_attr_op;
588 // Add a symbol to the list of function-local symbols
589 static void fn_local_symbol(struct symbol *sym)
591 if (function_symbol_list)
592 add_symbol(function_symbol_list, sym);
595 static int SENTINEL_ATTR match_idents(struct token *token, ...)
597 va_list args;
598 struct ident * next;
600 if (token_type(token) != TOKEN_IDENT)
601 return 0;
603 va_start(args, token);
604 do {
605 next = va_arg(args, struct ident *);
606 } while (next && token->ident != next);
607 va_end(args);
609 return next && token->ident == next;
613 struct statement *alloc_statement(struct position pos, int type)
615 struct statement *stmt = __alloc_statement(0);
616 stmt->type = type;
617 stmt->pos = pos;
618 return stmt;
621 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
623 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
625 static void apply_modifiers(struct position pos, struct decl_state *ctx)
627 struct symbol *ctype;
628 if (!ctx->mode)
629 return;
630 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
631 if (!ctype)
632 sparse_error(pos, "don't know how to apply mode to %s",
633 show_typename(ctx->ctype.base_type));
634 else
635 ctx->ctype.base_type = ctype;
639 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
641 struct symbol *sym = alloc_symbol(pos, type);
643 sym->ctype.base_type = ctype->base_type;
644 sym->ctype.modifiers = ctype->modifiers;
646 ctype->base_type = sym;
647 ctype->modifiers = 0;
648 return sym;
652 * NOTE! NS_LABEL is not just a different namespace,
653 * it also ends up using function scope instead of the
654 * regular symbol scope.
656 struct symbol *label_symbol(struct token *token)
658 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
659 if (!sym) {
660 sym = alloc_symbol(token->pos, SYM_LABEL);
661 bind_symbol(sym, token->ident, NS_LABEL);
662 fn_local_symbol(sym);
664 return sym;
667 static struct token *struct_union_enum_specifier(enum type type,
668 struct token *token, struct decl_state *ctx,
669 struct token *(*parse)(struct token *, struct symbol *))
671 struct symbol *sym;
672 struct position *repos;
674 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
675 if (token_type(token) == TOKEN_IDENT) {
676 sym = lookup_symbol(token->ident, NS_STRUCT);
677 if (!sym ||
678 (is_outer_scope(sym->scope) &&
679 (match_op(token->next,';') || match_op(token->next,'{')))) {
680 // Either a new symbol, or else an out-of-scope
681 // symbol being redefined.
682 sym = alloc_symbol(token->pos, type);
683 bind_symbol(sym, token->ident, NS_STRUCT);
685 if (sym->type != type)
686 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
687 ctx->ctype.base_type = sym;
688 repos = &token->pos;
689 token = token->next;
690 if (match_op(token, '{')) {
691 struct decl_state attr = { .ctype.base_type = sym, };
693 // The following test is actually wrong for empty
694 // structs, but (1) they are not C99, (2) gcc does
695 // the same thing, and (3) it's easier.
696 if (sym->symbol_list)
697 error_die(token->pos, "redefinition of %s", show_typename (sym));
698 sym->pos = *repos;
699 token = parse(token->next, sym);
700 token = expect(token, '}', "at end of struct-union-enum-specifier");
702 token = handle_attributes(token, &attr, KW_ATTRIBUTE);
703 apply_ctype(token->pos, &attr.ctype, &sym->ctype);
705 // Mark the structure as needing re-examination
706 sym->examined = 0;
707 sym->endpos = token->pos;
709 return token;
712 // private struct/union/enum type
713 if (!match_op(token, '{')) {
714 sparse_error(token->pos, "expected declaration");
715 ctx->ctype.base_type = &bad_ctype;
716 return token;
719 sym = alloc_symbol(token->pos, type);
720 token = parse(token->next, sym);
721 ctx->ctype.base_type = sym;
722 token = expect(token, '}', "at end of specifier");
723 sym->endpos = token->pos;
725 return token;
728 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
730 struct symbol *field, *last = NULL;
731 struct token *res;
732 res = struct_declaration_list(token, &sym->symbol_list);
733 FOR_EACH_PTR(sym->symbol_list, field) {
734 if (!field->ident) {
735 struct symbol *base = field->ctype.base_type;
736 if (base && base->type == SYM_BITFIELD)
737 continue;
739 if (last)
740 last->next_subobject = field;
741 last = field;
742 } END_FOR_EACH_PTR(field);
743 return res;
746 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
748 return struct_declaration_list(token, &sym->symbol_list);
751 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
753 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
756 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
758 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
762 typedef struct {
763 int x;
764 unsigned long long y;
765 } Num;
767 static void upper_boundary(Num *n, Num *v)
769 if (n->x > v->x)
770 return;
771 if (n->x < v->x) {
772 *n = *v;
773 return;
775 if (n->y < v->y)
776 n->y = v->y;
779 static void lower_boundary(Num *n, Num *v)
781 if (n->x < v->x)
782 return;
783 if (n->x > v->x) {
784 *n = *v;
785 return;
787 if (n->y > v->y)
788 n->y = v->y;
791 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
793 int shift = type->bit_size;
794 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
796 if (!is_unsigned)
797 shift--;
798 if (upper->x == 0 && upper->y >> shift)
799 return 0;
800 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
801 return 1;
802 return 0;
805 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
807 if (s1->bit_size < s2->bit_size) {
808 s1 = s2;
809 } else if (s1->bit_size == s2->bit_size) {
810 if (s2->ctype.modifiers & MOD_UNSIGNED)
811 s1 = s2;
813 if (s1->bit_size < bits_in_int)
814 return &int_ctype;
815 return s1;
818 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
820 struct symbol *sym;
822 FOR_EACH_PTR(list, sym) {
823 struct expression *expr = sym->initializer;
824 struct symbol *ctype;
825 if (expr->type != EXPR_VALUE)
826 continue;
827 ctype = expr->ctype;
828 if (ctype->bit_size == base_type->bit_size)
829 continue;
830 cast_value(expr, base_type, expr, ctype);
831 } END_FOR_EACH_PTR(sym);
834 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
836 unsigned long long lastval = 0;
837 struct symbol *ctype = NULL, *base_type = NULL;
838 Num upper = {-1, 0}, lower = {1, 0};
840 parent->examined = 1;
841 parent->ctype.base_type = &int_ctype;
842 while (token_type(token) == TOKEN_IDENT) {
843 struct expression *expr = NULL;
844 struct token *next = token->next;
845 struct symbol *sym;
847 if (match_op(next, '=')) {
848 next = constant_expression(next->next, &expr);
849 lastval = get_expression_value(expr);
850 ctype = &void_ctype;
851 if (expr && expr->ctype)
852 ctype = expr->ctype;
853 } else if (!ctype) {
854 ctype = &int_ctype;
855 } else if (is_int_type(ctype)) {
856 lastval++;
857 } else {
858 error_die(token->pos, "can't increment the last enum member");
861 if (!expr) {
862 expr = alloc_expression(token->pos, EXPR_VALUE);
863 expr->value = lastval;
864 expr->ctype = ctype;
867 sym = alloc_symbol(token->pos, SYM_NODE);
868 bind_symbol(sym, token->ident, NS_SYMBOL);
869 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
870 sym->initializer = expr;
871 sym->enum_member = 1;
872 sym->ctype.base_type = parent;
873 add_ptr_list(&parent->symbol_list, sym);
875 if (base_type != &bad_ctype) {
876 if (ctype->type == SYM_NODE)
877 ctype = ctype->ctype.base_type;
878 if (ctype->type == SYM_ENUM) {
879 if (ctype == parent)
880 ctype = base_type;
881 else
882 ctype = ctype->ctype.base_type;
885 * base_type rules:
886 * - if all enums are of the same type, then
887 * the base_type is that type (two first
888 * cases)
889 * - if enums are of different types, they
890 * all have to be integer types, and the
891 * base type is at least "int_ctype".
892 * - otherwise the base_type is "bad_ctype".
894 if (!base_type) {
895 base_type = ctype;
896 } else if (ctype == base_type) {
897 /* nothing */
898 } else if (is_int_type(base_type) && is_int_type(ctype)) {
899 base_type = bigger_enum_type(base_type, ctype);
900 } else
901 base_type = &bad_ctype;
902 parent->ctype.base_type = base_type;
904 if (is_int_type(base_type)) {
905 Num v = {.y = lastval};
906 if (ctype->ctype.modifiers & MOD_UNSIGNED)
907 v.x = 0;
908 else if ((long long)lastval >= 0)
909 v.x = 0;
910 else
911 v.x = -1;
912 upper_boundary(&upper, &v);
913 lower_boundary(&lower, &v);
915 token = next;
917 sym->endpos = token->pos;
919 if (!match_op(token, ','))
920 break;
921 token = token->next;
923 if (!base_type) {
924 sparse_error(token->pos, "bad enum definition");
925 base_type = &bad_ctype;
927 else if (!is_int_type(base_type))
928 base_type = base_type;
929 else if (type_is_ok(base_type, &upper, &lower))
930 base_type = base_type;
931 else if (type_is_ok(&int_ctype, &upper, &lower))
932 base_type = &int_ctype;
933 else if (type_is_ok(&uint_ctype, &upper, &lower))
934 base_type = &uint_ctype;
935 else if (type_is_ok(&long_ctype, &upper, &lower))
936 base_type = &long_ctype;
937 else if (type_is_ok(&ulong_ctype, &upper, &lower))
938 base_type = &ulong_ctype;
939 else if (type_is_ok(&llong_ctype, &upper, &lower))
940 base_type = &llong_ctype;
941 else if (type_is_ok(&ullong_ctype, &upper, &lower))
942 base_type = &ullong_ctype;
943 else
944 base_type = &bad_ctype;
945 parent->ctype.base_type = base_type;
946 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
947 parent->examined = 0;
949 cast_enum_list(parent->symbol_list, base_type);
951 return token;
954 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
956 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
957 struct ctype *ctype = &ctx->ctype.base_type->ctype;
959 if (!ctype->base_type)
960 ctype->base_type = &incomplete_ctype;
962 return ret;
965 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
967 struct symbol *sym;
969 if (!match_op(token, '(')) {
970 sparse_error(token->pos, "expected '(' after typeof");
971 return token;
973 if (lookup_type(token->next)) {
974 token = typename(token->next, &sym, NULL);
975 ctx->ctype.base_type = sym->ctype.base_type;
976 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
977 } else {
978 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
979 token = parse_expression(token->next, &typeof_sym->initializer);
981 typeof_sym->endpos = token->pos;
982 if (!typeof_sym->initializer) {
983 sparse_error(token->pos, "expected expression after the '(' token");
984 typeof_sym = &bad_ctype;
986 ctx->ctype.base_type = typeof_sym;
988 return expect(token, ')', "after typeof");
991 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
993 struct expression *expr = NULL;
994 if (match_op(token, '('))
995 token = parens_expression(token, &expr, "in attribute");
996 return token;
999 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1001 if (!ctx->ctype.alignment)
1002 ctx->ctype.alignment = 1;
1003 return token;
1006 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1008 int alignment = max_alignment;
1009 struct expression *expr = NULL;
1011 if (match_op(token, '(')) {
1012 token = parens_expression(token, &expr, "in attribute");
1013 if (expr)
1014 alignment = const_expression_value(expr);
1016 if (alignment & (alignment-1)) {
1017 warning(token->pos, "I don't like non-power-of-2 alignments");
1018 return token;
1019 } else if (alignment > ctx->ctype.alignment)
1020 ctx->ctype.alignment = alignment;
1021 return token;
1024 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1026 if (ctx->modifiers & qual)
1027 warning(*pos, "duplicate %s", modifier_string(qual));
1028 ctx->modifiers |= qual;
1031 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1033 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1034 return token;
1037 static struct token *attribute_bitwise(struct token *token, struct symbol *attr, struct decl_state *ctx)
1039 if (Wbitwise)
1040 attribute_modifier(token, attr, ctx);
1041 return token;
1044 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1046 struct expression *expr = NULL;
1047 int as;
1048 token = expect(token, '(', "after address_space attribute");
1049 token = conditional_expression(token, &expr);
1050 if (expr) {
1051 as = const_expression_value(expr);
1052 if (Waddress_space && as)
1053 ctx->ctype.as = as;
1055 token = expect(token, ')', "after address_space attribute");
1056 return token;
1059 static struct symbol *to_QI_mode(struct symbol *ctype)
1061 if (ctype->ctype.base_type != &int_type)
1062 return NULL;
1063 if (ctype == &char_ctype)
1064 return ctype;
1065 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1066 : &schar_ctype;
1069 static struct symbol *to_HI_mode(struct symbol *ctype)
1071 if (ctype->ctype.base_type != &int_type)
1072 return NULL;
1073 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1074 : &sshort_ctype;
1077 static struct symbol *to_SI_mode(struct symbol *ctype)
1079 if (ctype->ctype.base_type != &int_type)
1080 return NULL;
1081 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1082 : &sint_ctype;
1085 static struct symbol *to_DI_mode(struct symbol *ctype)
1087 if (ctype->ctype.base_type != &int_type)
1088 return NULL;
1089 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1090 : &sllong_ctype;
1093 static struct symbol *to_TI_mode(struct symbol *ctype)
1095 if (ctype->ctype.base_type != &int_type)
1096 return NULL;
1097 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1098 : &slllong_ctype;
1101 static struct symbol *to_word_mode(struct symbol *ctype)
1103 if (ctype->ctype.base_type != &int_type)
1104 return NULL;
1105 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1106 : &slong_ctype;
1109 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1111 token = expect(token, '(', "after mode attribute");
1112 if (token_type(token) == TOKEN_IDENT) {
1113 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1114 if (mode && mode->op->type == KW_MODE)
1115 ctx->mode = mode->op;
1116 else
1117 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1118 token = token->next;
1119 } else
1120 sparse_error(token->pos, "expect attribute mode symbol\n");
1121 token = expect(token, ')', "after mode attribute");
1122 return token;
1125 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1127 struct context *context = alloc_context();
1128 struct expression *args[3];
1129 int argc = 0;
1131 token = expect(token, '(', "after context attribute");
1132 while (!match_op(token, ')')) {
1133 struct expression *expr = NULL;
1134 token = conditional_expression(token, &expr);
1135 if (!expr)
1136 break;
1137 if (argc < 3)
1138 args[argc++] = expr;
1139 if (!match_op(token, ','))
1140 break;
1141 token = token->next;
1144 switch(argc) {
1145 case 0:
1146 sparse_error(token->pos, "expected context input/output values");
1147 break;
1148 case 1:
1149 context->in = get_expression_value(args[0]);
1150 break;
1151 case 2:
1152 context->in = get_expression_value(args[0]);
1153 context->out = get_expression_value(args[1]);
1154 break;
1155 case 3:
1156 context->context = args[0];
1157 context->in = get_expression_value(args[1]);
1158 context->out = get_expression_value(args[2]);
1159 break;
1162 if (argc)
1163 add_ptr_list(&ctx->ctype.contexts, context);
1165 token = expect(token, ')', "after context attribute");
1166 return token;
1169 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1171 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1172 ctx->ctype.base_type->designated_init = 1;
1173 else
1174 warning(token->pos, "attribute designated_init applied to non-structure type");
1175 return token;
1178 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1180 if (Wtransparent_union)
1181 warning(token->pos, "attribute __transparent_union__");
1183 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_UNION)
1184 ctx->ctype.base_type->transparent_union = 1;
1185 else
1186 warning(token->pos, "attribute __transparent_union__ applied to non-union type");
1187 return token;
1190 static struct token *recover_unknown_attribute(struct token *token)
1192 struct expression *expr = NULL;
1194 if (Wunknown_attribute)
1195 warning(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1196 token = token->next;
1197 if (match_op(token, '('))
1198 token = parens_expression(token, &expr, "in attribute");
1199 return token;
1202 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1204 token = expect(token, '(', "after attribute");
1205 token = expect(token, '(', "after attribute");
1207 for (;;) {
1208 struct ident *attribute_name;
1209 struct symbol *attr;
1211 if (eof_token(token))
1212 break;
1213 if (match_op(token, ';'))
1214 break;
1215 if (token_type(token) != TOKEN_IDENT)
1216 break;
1217 attribute_name = token->ident;
1218 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1219 if (attr && attr->op->attribute)
1220 token = attr->op->attribute(token->next, attr, ctx);
1221 else
1222 token = recover_unknown_attribute(token);
1224 if (!match_op(token, ','))
1225 break;
1226 token = token->next;
1229 token = expect(token, ')', "after attribute");
1230 token = expect(token, ')', "after attribute");
1231 return token;
1234 static const char *storage_class[] =
1236 [STypedef] = "typedef",
1237 [SAuto] = "auto",
1238 [SExtern] = "extern",
1239 [SStatic] = "static",
1240 [SRegister] = "register",
1241 [SForced] = "[force]"
1244 static unsigned long storage_modifiers(struct decl_state *ctx)
1246 static unsigned long mod[SMax] =
1248 [SAuto] = MOD_AUTO,
1249 [SExtern] = MOD_EXTERN,
1250 [SStatic] = MOD_STATIC,
1251 [SRegister] = MOD_REGISTER
1253 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1254 | (ctx->is_tls ? MOD_TLS : 0);
1257 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1259 /* __thread can be used alone, or with extern or static */
1260 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1261 sparse_error(*pos, "__thread can only be used alone, or with "
1262 "extern or static");
1263 return;
1266 if (!ctx->storage_class) {
1267 ctx->storage_class = class;
1268 return;
1270 if (ctx->storage_class == class)
1271 sparse_error(*pos, "duplicate %s", storage_class[class]);
1272 else
1273 sparse_error(*pos, "multiple storage classes");
1276 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1278 set_storage_class(&next->pos, ctx, STypedef);
1279 return next;
1282 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1284 set_storage_class(&next->pos, ctx, SAuto);
1285 return next;
1288 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1290 set_storage_class(&next->pos, ctx, SRegister);
1291 return next;
1294 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1296 set_storage_class(&next->pos, ctx, SStatic);
1297 return next;
1300 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1302 set_storage_class(&next->pos, ctx, SExtern);
1303 return next;
1306 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1308 /* This GCC extension can be used alone, or with extern or static */
1309 if (!ctx->storage_class || ctx->storage_class == SStatic
1310 || ctx->storage_class == SExtern) {
1311 ctx->is_tls = 1;
1312 } else {
1313 sparse_error(next->pos, "__thread can only be used alone, or "
1314 "with extern or static");
1317 return next;
1320 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1322 set_storage_class(&token->pos, ctx, SForced);
1323 return token;
1326 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1328 ctx->is_inline = 1;
1329 return next;
1332 static struct token *noreturn_specifier(struct token *next, struct decl_state *ctx)
1334 apply_qualifier(&next->pos, &ctx->ctype, MOD_NORETURN);
1335 return next;
1338 static struct token *alignas_specifier(struct token *token, struct decl_state *ctx)
1340 int alignment = 0;
1342 if (!match_op(token, '(')) {
1343 sparse_error(token->pos, "expected '(' after _Alignas");
1344 return token;
1346 if (lookup_type(token->next)) {
1347 struct symbol *sym = NULL;
1348 token = typename(token->next, &sym, NULL);
1349 sym = examine_symbol_type(sym);
1350 alignment = sym->ctype.alignment;
1351 token = expect(token, ')', "after _Alignas(...");
1352 } else {
1353 struct expression *expr = NULL;
1354 token = parens_expression(token, &expr, "after _Alignas");
1355 if (!expr)
1356 return token;
1357 alignment = const_expression_value(expr);
1360 if (alignment < 0) {
1361 warning(token->pos, "non-positive alignment");
1362 return token;
1364 if (alignment & (alignment-1)) {
1365 warning(token->pos, "non-power-of-2 alignment");
1366 return token;
1368 if (alignment > ctx->ctype.alignment)
1369 ctx->ctype.alignment = alignment;
1370 return token;
1373 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1375 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1376 return next;
1379 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1381 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1382 return next;
1385 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1387 unsigned long mod = thistype->modifiers;
1389 if (mod)
1390 apply_qualifier(&pos, ctype, mod);
1392 /* Context */
1393 concat_ptr_list((struct ptr_list *)thistype->contexts,
1394 (struct ptr_list **)&ctype->contexts);
1396 /* Alignment */
1397 if (thistype->alignment > ctype->alignment)
1398 ctype->alignment = thistype->alignment;
1400 /* Address space */
1401 if (thistype->as)
1402 ctype->as = thistype->as;
1405 static void specifier_conflict(struct position pos, int what, struct ident *new)
1407 const char *old;
1408 if (what & (Set_S | Set_T))
1409 goto Catch_all;
1410 if (what & Set_Char)
1411 old = "char";
1412 else if (what & Set_Double)
1413 old = "double";
1414 else if (what & Set_Float)
1415 old = "float";
1416 else if (what & Set_Signed)
1417 old = "signed";
1418 else if (what & Set_Unsigned)
1419 old = "unsigned";
1420 else if (what & Set_Short)
1421 old = "short";
1422 else if (what & Set_Long)
1423 old = "long";
1424 else
1425 old = "long long";
1426 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1427 old, show_ident(new));
1428 return;
1430 Catch_all:
1431 sparse_error(pos, "two or more data types in declaration specifiers");
1434 static struct symbol * const int_types[] =
1435 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype, &lllong_ctype};
1436 static struct symbol * const signed_types[] =
1437 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1438 &slllong_ctype};
1439 static struct symbol * const unsigned_types[] =
1440 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1441 &ulllong_ctype};
1442 static struct symbol * const real_types[] =
1443 {&float_ctype, &double_ctype, &ldouble_ctype};
1444 static struct symbol * const char_types[] =
1445 {&char_ctype, &schar_ctype, &uchar_ctype};
1446 static struct symbol * const * const types[] = {
1447 int_types + 1, signed_types + 1, unsigned_types + 1,
1448 real_types + 1, char_types, char_types + 1, char_types + 2
1451 struct symbol *ctype_integer(int size, int want_unsigned)
1453 return types[want_unsigned ? CUInt : CInt][size];
1456 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1458 while (token_type(t) == TOKEN_IDENT) {
1459 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1460 if (!s)
1461 break;
1462 if (s->type != SYM_KEYWORD)
1463 break;
1464 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1465 break;
1466 t = t->next;
1467 if (s->op->declarator)
1468 t = s->op->declarator(t, ctx);
1470 return t;
1473 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1475 int seen = 0;
1476 int class = CInt;
1477 int size = 0;
1479 while (token_type(token) == TOKEN_IDENT) {
1480 struct symbol *s = lookup_symbol(token->ident,
1481 NS_TYPEDEF | NS_SYMBOL);
1482 if (!s || !(s->namespace & NS_TYPEDEF))
1483 break;
1484 if (s->type != SYM_KEYWORD) {
1485 if (seen & Set_Any)
1486 break;
1487 seen |= Set_S | Set_T;
1488 ctx->ctype.base_type = s->ctype.base_type;
1489 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1490 token = token->next;
1491 continue;
1493 if (s->op->type & KW_SPECIFIER) {
1494 if (seen & s->op->test) {
1495 specifier_conflict(token->pos,
1496 seen & s->op->test,
1497 token->ident);
1498 break;
1500 seen |= s->op->set;
1501 class += s->op->class;
1502 if (s->op->set & Set_Int128)
1503 size = 2;
1504 if (s->op->type & KW_SHORT) {
1505 size = -1;
1506 } else if (s->op->type & KW_LONG && size++) {
1507 if (class == CReal) {
1508 specifier_conflict(token->pos,
1509 Set_Vlong,
1510 &double_ident);
1511 break;
1513 seen |= Set_Vlong;
1516 token = token->next;
1517 if (s->op->declarator)
1518 token = s->op->declarator(token, ctx);
1519 if (s->op->type & KW_EXACT) {
1520 ctx->ctype.base_type = s->ctype.base_type;
1521 ctx->ctype.modifiers |= s->ctype.modifiers;
1525 if (!(seen & Set_S)) { /* not set explicitly? */
1526 struct symbol *base = &incomplete_ctype;
1527 if (seen & Set_Any)
1528 base = types[class][size];
1529 ctx->ctype.base_type = base;
1532 if (ctx->ctype.modifiers & MOD_BITWISE) {
1533 struct symbol *type;
1534 ctx->ctype.modifiers &= ~MOD_BITWISE;
1535 if (!is_int_type(ctx->ctype.base_type)) {
1536 sparse_error(token->pos, "invalid modifier");
1537 return token;
1539 type = alloc_symbol(token->pos, SYM_BASETYPE);
1540 *type = *ctx->ctype.base_type;
1541 type->ctype.modifiers &= ~MOD_SPECIFIER;
1542 type->ctype.base_type = ctx->ctype.base_type;
1543 type->type = SYM_RESTRICT;
1544 ctx->ctype.base_type = type;
1545 create_fouled(type);
1547 return token;
1550 static struct token *abstract_array_static_declarator(struct token *token, int *has_static)
1552 while (token->ident == &static_ident) {
1553 if (*has_static)
1554 sparse_error(token->pos, "duplicate array static declarator");
1556 *has_static = 1;
1557 token = token->next;
1559 return token;
1563 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1565 struct expression *expr = NULL;
1566 int has_static = 0;
1568 token = abstract_array_static_declarator(token, &has_static);
1570 if (match_idents(token, &restrict_ident, &__restrict_ident, &__restrict___ident, NULL))
1571 token = abstract_array_static_declarator(token->next, &has_static);
1572 token = parse_expression(token, &expr);
1573 sym->array_size = expr;
1574 return token;
1577 static struct token *parameter_type_list(struct token *, struct symbol *);
1578 static struct token *identifier_list(struct token *, struct symbol *);
1579 static struct token *declarator(struct token *token, struct decl_state *ctx);
1581 static struct token *skip_attribute(struct token *token)
1583 token = token->next;
1584 if (match_op(token, '(')) {
1585 int depth = 1;
1586 token = token->next;
1587 while (depth && !eof_token(token)) {
1588 if (token_type(token) == TOKEN_SPECIAL) {
1589 if (token->special == '(')
1590 depth++;
1591 else if (token->special == ')')
1592 depth--;
1594 token = token->next;
1597 return token;
1600 static struct token *skip_attributes(struct token *token)
1602 struct symbol *keyword;
1603 for (;;) {
1604 if (token_type(token) != TOKEN_IDENT)
1605 break;
1606 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1607 if (!keyword || keyword->type != SYM_KEYWORD)
1608 break;
1609 if (!(keyword->op->type & KW_ATTRIBUTE))
1610 break;
1611 token = expect(token->next, '(', "after attribute");
1612 token = expect(token, '(', "after attribute");
1613 for (;;) {
1614 if (eof_token(token))
1615 break;
1616 if (match_op(token, ';'))
1617 break;
1618 if (token_type(token) != TOKEN_IDENT)
1619 break;
1620 token = skip_attribute(token);
1621 if (!match_op(token, ','))
1622 break;
1623 token = token->next;
1625 token = expect(token, ')', "after attribute");
1626 token = expect(token, ')', "after attribute");
1628 return token;
1631 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1633 struct symbol *keyword;
1634 for (;;) {
1635 if (token_type(token) != TOKEN_IDENT)
1636 break;
1637 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1638 if (!keyword || keyword->type != SYM_KEYWORD)
1639 break;
1640 if (!(keyword->op->type & keywords))
1641 break;
1642 token = keyword->op->declarator(token->next, ctx);
1643 keywords &= KW_ATTRIBUTE;
1645 return token;
1648 static int is_nested(struct token *token, struct token **p,
1649 int prefer_abstract)
1652 * This can be either a parameter list or a grouping.
1653 * For the direct (non-abstract) case, we know if must be
1654 * a parameter list if we already saw the identifier.
1655 * For the abstract case, we know if must be a parameter
1656 * list if it is empty or starts with a type.
1658 struct token *next = token->next;
1660 *p = next = skip_attributes(next);
1662 if (token_type(next) == TOKEN_IDENT) {
1663 if (lookup_type(next))
1664 return !prefer_abstract;
1665 return 1;
1668 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1669 return 0;
1671 return 1;
1674 enum kind {
1675 Empty, K_R, Proto, Bad_Func,
1678 static enum kind which_func(struct token *token,
1679 struct ident **n,
1680 int prefer_abstract)
1682 struct token *next = token->next;
1684 if (token_type(next) == TOKEN_IDENT) {
1685 if (lookup_type(next))
1686 return Proto;
1687 /* identifier list not in definition; complain */
1688 if (prefer_abstract)
1689 warning(token->pos,
1690 "identifier list not in definition");
1691 return K_R;
1694 if (token_type(next) != TOKEN_SPECIAL)
1695 return Bad_Func;
1697 if (next->special == ')') {
1698 /* don't complain about those */
1699 if (!n || match_op(next->next, ';'))
1700 return Empty;
1701 if (Wnon_ansi_function_declaration)
1702 warning(next->pos,
1703 "non-ANSI function declaration of function '%s'",
1704 show_ident(*n));
1705 return Empty;
1708 if (next->special == SPECIAL_ELLIPSIS) {
1709 warning(next->pos,
1710 "variadic functions must have one named argument");
1711 return Proto;
1714 return Bad_Func;
1717 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1719 struct ctype *ctype = &ctx->ctype;
1720 struct token *next;
1721 struct ident **p = ctx->ident;
1723 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1724 *ctx->ident = token->ident;
1725 token = token->next;
1726 } else if (match_op(token, '(') &&
1727 is_nested(token, &next, ctx->prefer_abstract)) {
1728 struct symbol *base_type = ctype->base_type;
1729 if (token->next != next)
1730 next = handle_attributes(token->next, ctx,
1731 KW_ATTRIBUTE);
1732 token = declarator(next, ctx);
1733 token = expect(token, ')', "in nested declarator");
1734 while (ctype->base_type != base_type)
1735 ctype = &ctype->base_type->ctype;
1736 p = NULL;
1739 if (match_op(token, '(')) {
1740 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1741 struct symbol *fn;
1742 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1743 token = token->next;
1744 if (kind == K_R)
1745 token = identifier_list(token, fn);
1746 else if (kind == Proto)
1747 token = parameter_type_list(token, fn);
1748 token = expect(token, ')', "in function declarator");
1749 fn->endpos = token->pos;
1750 return token;
1753 while (match_op(token, '[')) {
1754 struct symbol *array;
1755 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1756 token = abstract_array_declarator(token->next, array);
1757 token = expect(token, ']', "in abstract_array_declarator");
1758 array->endpos = token->pos;
1759 ctype = &array->ctype;
1761 return token;
1764 static struct token *pointer(struct token *token, struct decl_state *ctx)
1766 while (match_op(token,'*')) {
1767 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1768 ptr->ctype.modifiers = ctx->ctype.modifiers;
1769 ptr->ctype.base_type = ctx->ctype.base_type;
1770 ptr->ctype.as = ctx->ctype.as;
1771 ptr->ctype.contexts = ctx->ctype.contexts;
1772 ctx->ctype.modifiers = 0;
1773 ctx->ctype.base_type = ptr;
1774 ctx->ctype.as = 0;
1775 ctx->ctype.contexts = NULL;
1776 ctx->ctype.alignment = 0;
1778 token = handle_qualifiers(token->next, ctx);
1779 ctx->ctype.base_type->endpos = token->pos;
1781 return token;
1784 static struct token *declarator(struct token *token, struct decl_state *ctx)
1786 token = pointer(token, ctx);
1787 return direct_declarator(token, ctx);
1790 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1792 struct ctype *ctype = &ctx->ctype;
1793 struct expression *expr;
1794 struct symbol *bitfield;
1795 long long width;
1797 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1798 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1799 show_typename(ctype->base_type));
1800 // Parse this to recover gracefully.
1801 return conditional_expression(token->next, &expr);
1804 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1805 token = conditional_expression(token->next, &expr);
1806 width = const_expression_value(expr);
1807 bitfield->bit_size = width;
1809 if (width < 0 || width > INT_MAX) {
1810 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1811 width = -1;
1812 } else if (*ctx->ident && width == 0) {
1813 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1814 show_ident(*ctx->ident));
1815 width = -1;
1816 } else if (*ctx->ident) {
1817 struct symbol *base_type = bitfield->ctype.base_type;
1818 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1819 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1820 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1821 // Valid values are either {-1;0} or {0}, depending on integer
1822 // representation. The latter makes for very efficient code...
1823 sparse_error(token->pos, "dubious one-bit signed bitfield");
1825 if (Wdefault_bitfield_sign &&
1826 bitfield_type->type != SYM_ENUM &&
1827 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1828 is_signed) {
1829 // The sign of bitfields is unspecified by default.
1830 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1833 bitfield->bit_size = width;
1834 bitfield->endpos = token->pos;
1835 return token;
1838 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1840 struct decl_state ctx = {.prefer_abstract = 0};
1841 struct ctype saved;
1842 unsigned long mod;
1844 token = declaration_specifiers(token, &ctx);
1845 mod = storage_modifiers(&ctx);
1846 saved = ctx.ctype;
1847 for (;;) {
1848 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1849 ctx.ident = &decl->ident;
1851 token = declarator(token, &ctx);
1852 if (match_op(token, ':'))
1853 token = handle_bitfield(token, &ctx);
1855 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1856 apply_modifiers(token->pos, &ctx);
1858 decl->ctype = ctx.ctype;
1859 decl->ctype.modifiers |= mod;
1860 decl->endpos = token->pos;
1861 add_symbol(list, decl);
1862 if (!match_op(token, ','))
1863 break;
1864 token = token->next;
1865 ctx.ctype = saved;
1867 return token;
1870 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1872 while (!match_op(token, '}')) {
1873 if (match_ident(token, &_Static_assert_ident)) {
1874 token = parse_static_assert(token, NULL);
1875 continue;
1877 if (!match_op(token, ';'))
1878 token = declaration_list(token, list);
1879 if (!match_op(token, ';')) {
1880 sparse_error(token->pos, "expected ; at end of declaration");
1881 break;
1883 token = token->next;
1885 return token;
1888 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1890 struct decl_state ctx = {.prefer_abstract = 1};
1892 token = declaration_specifiers(token, &ctx);
1893 ctx.ident = &sym->ident;
1894 token = declarator(token, &ctx);
1895 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1896 apply_modifiers(token->pos, &ctx);
1897 sym->ctype = ctx.ctype;
1898 sym->ctype.modifiers |= storage_modifiers(&ctx);
1899 sym->endpos = token->pos;
1900 sym->forced_arg = ctx.storage_class == SForced;
1901 return token;
1904 struct token *typename(struct token *token, struct symbol **p, int *forced)
1906 struct decl_state ctx = {.prefer_abstract = 1};
1907 int class;
1908 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1909 *p = sym;
1910 token = declaration_specifiers(token, &ctx);
1911 token = declarator(token, &ctx);
1912 apply_modifiers(token->pos, &ctx);
1913 sym->ctype = ctx.ctype;
1914 sym->endpos = token->pos;
1915 class = ctx.storage_class;
1916 if (forced) {
1917 *forced = 0;
1918 if (class == SForced) {
1919 *forced = 1;
1920 class = 0;
1923 if (class)
1924 warning(sym->pos, "storage class in typename (%s %s)",
1925 storage_class[class], show_typename(sym));
1926 return token;
1929 static struct token *parse_underscore_Pragma(struct token *token)
1931 struct token *next;
1933 next = token->next;
1934 if (!match_op(next, '('))
1935 return next;
1936 next = next->next;
1937 if (next->pos.type != TOKEN_STRING)
1938 return next;
1939 next = next->next;
1940 if (!match_op(next, ')'))
1941 return next;
1942 return next->next;
1945 static struct token *expression_statement(struct token *token, struct expression **tree)
1947 if (match_ident(token, &_Pragma_ident))
1948 return parse_underscore_Pragma(token);
1950 token = parse_expression(token, tree);
1951 return expect(token, ';', "at end of statement");
1954 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1955 struct expression_list **inout)
1957 struct expression *expr;
1959 /* Allow empty operands */
1960 if (match_op(token->next, ':') || match_op(token->next, ')'))
1961 return token->next;
1962 do {
1963 struct ident *ident = NULL;
1964 if (match_op(token->next, '[') &&
1965 token_type(token->next->next) == TOKEN_IDENT &&
1966 match_op(token->next->next->next, ']')) {
1967 ident = token->next->next->ident;
1968 token = token->next->next->next;
1970 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1971 token = primary_expression(token->next, &expr);
1972 add_expression(inout, expr);
1973 token = parens_expression(token, &expr, "in asm parameter");
1974 add_expression(inout, expr);
1975 } while (match_op(token, ','));
1976 return token;
1979 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1980 struct expression_list **clobbers)
1982 struct expression *expr;
1984 do {
1985 token = primary_expression(token->next, &expr);
1986 if (expr)
1987 add_expression(clobbers, expr);
1988 } while (match_op(token, ','));
1989 return token;
1992 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
1993 struct symbol_list **labels)
1995 struct symbol *label;
1997 do {
1998 token = token->next; /* skip ':' and ',' */
1999 if (token_type(token) != TOKEN_IDENT)
2000 return token;
2001 label = label_symbol(token);
2002 add_symbol(labels, label);
2003 token = token->next;
2004 } while (match_op(token, ','));
2005 return token;
2008 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
2010 int is_goto = 0;
2012 token = token->next;
2013 stmt->type = STMT_ASM;
2014 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
2015 token = token->next;
2017 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
2018 is_goto = 1;
2019 token = token->next;
2021 token = expect(token, '(', "after asm");
2022 token = parse_expression(token, &stmt->asm_string);
2023 if (match_op(token, ':'))
2024 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
2025 if (match_op(token, ':'))
2026 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
2027 if (match_op(token, ':'))
2028 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
2029 if (is_goto && match_op(token, ':'))
2030 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
2031 token = expect(token, ')', "after asm");
2032 return expect(token, ';', "at end of asm-statement");
2035 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
2037 struct expression *expr;
2038 token = expect(token, '(', "after asm");
2039 token = parse_expression(token->next, &expr);
2040 token = expect(token, ')', "after asm");
2041 return token;
2044 static struct token *parse_static_assert(struct token *token, struct symbol_list **unused)
2046 struct expression *cond = NULL, *message = NULL;
2048 token = expect(token->next, '(', "after _Static_assert");
2049 token = constant_expression(token, &cond);
2050 if (!cond)
2051 sparse_error(token->pos, "Expected constant expression");
2052 token = expect(token, ',', "after conditional expression in _Static_assert");
2053 token = parse_expression(token, &message);
2054 if (!message || message->type != EXPR_STRING) {
2055 struct position pos;
2057 pos = message ? message->pos : token->pos;
2058 sparse_error(pos, "bad or missing string literal");
2059 cond = NULL;
2061 token = expect(token, ')', "after diagnostic message in _Static_assert");
2063 token = expect(token, ';', "after _Static_assert()");
2065 if (cond && !const_expression_value(cond) && cond->type == EXPR_VALUE)
2066 sparse_error(cond->pos, "static assertion failed: %s",
2067 show_string(message->string));
2068 return token;
2071 /* Make a statement out of an expression */
2072 static struct statement *make_statement(struct expression *expr)
2074 struct statement *stmt;
2076 if (!expr)
2077 return NULL;
2078 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
2079 stmt->expression = expr;
2080 return stmt;
2084 * All iterators have two symbols associated with them:
2085 * the "continue" and "break" symbols, which are targets
2086 * for continue and break statements respectively.
2088 * They are in a special name-space, but they follow
2089 * all the normal visibility rules, so nested iterators
2090 * automatically work right.
2092 static void start_iterator(struct statement *stmt)
2094 struct symbol *cont, *brk;
2096 start_symbol_scope(stmt->pos);
2097 cont = alloc_symbol(stmt->pos, SYM_NODE);
2098 bind_symbol(cont, &continue_ident, NS_ITERATOR);
2099 brk = alloc_symbol(stmt->pos, SYM_NODE);
2100 bind_symbol(brk, &break_ident, NS_ITERATOR);
2102 stmt->type = STMT_ITERATOR;
2103 stmt->iterator_break = brk;
2104 stmt->iterator_continue = cont;
2105 fn_local_symbol(brk);
2106 fn_local_symbol(cont);
2109 static void end_iterator(struct statement *stmt)
2111 end_symbol_scope();
2114 static struct statement *start_function(struct symbol *sym)
2116 struct symbol *ret;
2117 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2119 start_function_scope(sym->pos);
2120 ret = alloc_symbol(sym->pos, SYM_NODE);
2121 ret->ctype = sym->ctype.base_type->ctype;
2122 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2123 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2124 bind_symbol(ret, &return_ident, NS_ITERATOR);
2125 stmt->ret = ret;
2126 fn_local_symbol(ret);
2128 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2129 current_fn = sym;
2131 return stmt;
2134 static void end_function(struct symbol *sym)
2136 current_fn = NULL;
2137 end_function_scope();
2141 * A "switch()" statement, like an iterator, has a
2142 * the "break" symbol associated with it. It works
2143 * exactly like the iterator break - it's the target
2144 * for any break-statements in scope, and means that
2145 * "break" handling doesn't even need to know whether
2146 * it's breaking out of an iterator or a switch.
2148 * In addition, the "case" symbol is a marker for the
2149 * case/default statements to find the switch statement
2150 * that they are associated with.
2152 static void start_switch(struct statement *stmt)
2154 struct symbol *brk, *switch_case;
2156 start_symbol_scope(stmt->pos);
2157 brk = alloc_symbol(stmt->pos, SYM_NODE);
2158 bind_symbol(brk, &break_ident, NS_ITERATOR);
2160 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2161 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2162 switch_case->stmt = stmt;
2164 stmt->type = STMT_SWITCH;
2165 stmt->switch_break = brk;
2166 stmt->switch_case = switch_case;
2168 fn_local_symbol(brk);
2169 fn_local_symbol(switch_case);
2172 static void end_switch(struct statement *stmt)
2174 if (!stmt->switch_case->symbol_list)
2175 warning(stmt->pos, "switch with no cases");
2176 end_symbol_scope();
2179 static void add_case_statement(struct statement *stmt)
2181 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2182 struct symbol *sym;
2184 if (!target) {
2185 sparse_error(stmt->pos, "not in switch scope");
2186 stmt->type = STMT_NONE;
2187 return;
2189 sym = alloc_symbol(stmt->pos, SYM_NODE);
2190 add_symbol(&target->symbol_list, sym);
2191 sym->stmt = stmt;
2192 stmt->case_label = sym;
2193 fn_local_symbol(sym);
2196 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2198 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2200 if (!target)
2201 error_die(token->pos, "internal error: return without a function target");
2202 stmt->type = STMT_RETURN;
2203 stmt->ret_target = target;
2204 return expression_statement(token->next, &stmt->ret_value);
2207 static void validate_for_loop_decl(struct symbol *sym)
2209 unsigned long storage = sym->ctype.modifiers & MOD_STORAGE;
2211 if (storage & ~(MOD_AUTO | MOD_REGISTER)) {
2212 const char *name = show_ident(sym->ident);
2213 sparse_error(sym->pos, "non-local var '%s' in for-loop initializer", name);
2214 sym->ctype.modifiers &= ~MOD_STORAGE;
2218 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2220 struct symbol_list *syms;
2221 struct expression *e1, *e2, *e3;
2222 struct statement *iterator;
2224 start_iterator(stmt);
2225 token = expect(token->next, '(', "after 'for'");
2227 syms = NULL;
2228 e1 = NULL;
2229 /* C99 variable declaration? */
2230 if (lookup_type(token)) {
2231 token = external_declaration(token, &syms, validate_for_loop_decl);
2232 } else {
2233 token = parse_expression(token, &e1);
2234 token = expect(token, ';', "in 'for'");
2236 token = parse_expression(token, &e2);
2237 token = expect(token, ';', "in 'for'");
2238 token = parse_expression(token, &e3);
2239 token = expect(token, ')', "in 'for'");
2240 token = statement(token, &iterator);
2242 stmt->iterator_syms = syms;
2243 stmt->iterator_pre_statement = make_statement(e1);
2244 stmt->iterator_pre_condition = e2;
2245 stmt->iterator_post_statement = make_statement(e3);
2246 stmt->iterator_post_condition = NULL;
2247 stmt->iterator_statement = iterator;
2248 end_iterator(stmt);
2250 return token;
2253 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2255 struct expression *expr;
2256 struct statement *iterator;
2258 start_iterator(stmt);
2259 token = parens_expression(token->next, &expr, "after 'while'");
2260 token = statement(token, &iterator);
2262 stmt->iterator_pre_condition = expr;
2263 stmt->iterator_post_condition = NULL;
2264 stmt->iterator_statement = iterator;
2265 end_iterator(stmt);
2267 return token;
2270 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2272 struct expression *expr;
2273 struct statement *iterator;
2275 start_iterator(stmt);
2276 token = statement(token->next, &iterator);
2277 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2278 token = token->next;
2279 else
2280 sparse_error(token->pos, "expected 'while' after 'do'");
2281 token = parens_expression(token, &expr, "after 'do-while'");
2283 stmt->iterator_post_condition = expr;
2284 stmt->iterator_statement = iterator;
2285 end_iterator(stmt);
2287 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2288 warning(iterator->pos, "do-while statement is not a compound statement");
2290 return expect(token, ';', "after statement");
2293 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2295 stmt->type = STMT_IF;
2296 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2297 token = statement(token, &stmt->if_true);
2298 if (token_type(token) != TOKEN_IDENT)
2299 return token;
2300 if (token->ident != &else_ident)
2301 return token;
2302 return statement(token->next, &stmt->if_false);
2305 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2307 stmt->type = STMT_CASE;
2308 token = expect(token, ':', "after default/case");
2309 add_case_statement(stmt);
2310 return statement(token, &stmt->case_statement);
2313 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2315 token = parse_expression(token->next, &stmt->case_expression);
2316 if (match_op(token, SPECIAL_ELLIPSIS))
2317 token = parse_expression(token->next, &stmt->case_to);
2318 return case_statement(token, stmt);
2321 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2323 return case_statement(token->next, stmt);
2326 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2328 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2329 stmt->type = STMT_GOTO;
2330 stmt->goto_label = target;
2331 if (!target)
2332 sparse_error(stmt->pos, "break/continue not in iterator scope");
2333 return expect(token->next, ';', "at end of statement");
2336 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2338 stmt->type = STMT_SWITCH;
2339 start_switch(stmt);
2340 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2341 token = statement(token, &stmt->switch_statement);
2342 end_switch(stmt);
2343 return token;
2346 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2348 stmt->type = STMT_GOTO;
2349 token = token->next;
2350 if (match_op(token, '*')) {
2351 token = parse_expression(token->next, &stmt->goto_expression);
2352 add_statement(&function_computed_goto_list, stmt);
2353 } else if (token_type(token) == TOKEN_IDENT) {
2354 stmt->goto_label = label_symbol(token);
2355 token = token->next;
2356 } else {
2357 sparse_error(token->pos, "Expected identifier or goto expression");
2359 return expect(token, ';', "at end of statement");
2362 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2364 stmt->type = STMT_CONTEXT;
2365 token = parse_expression(token->next, &stmt->expression);
2366 if (stmt->expression->type == EXPR_PREOP
2367 && stmt->expression->op == '('
2368 && stmt->expression->unop->type == EXPR_COMMA) {
2369 struct expression *expr;
2370 expr = stmt->expression->unop;
2371 stmt->context = expr->left;
2372 stmt->expression = expr->right;
2374 return expect(token, ';', "at end of statement");
2377 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2379 stmt->type = STMT_RANGE;
2380 token = assignment_expression(token->next, &stmt->range_expression);
2381 token = expect(token, ',', "after range expression");
2382 token = assignment_expression(token, &stmt->range_low);
2383 token = expect(token, ',', "after low range");
2384 token = assignment_expression(token, &stmt->range_high);
2385 return expect(token, ';', "after range statement");
2388 static struct token *statement(struct token *token, struct statement **tree)
2390 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2392 *tree = stmt;
2393 if (token_type(token) == TOKEN_IDENT) {
2394 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2395 if (s && s->op->statement)
2396 return s->op->statement(token, stmt);
2398 if (match_op(token->next, ':')) {
2399 struct symbol *s = label_symbol(token);
2400 stmt->type = STMT_LABEL;
2401 stmt->label_identifier = s;
2402 if (s->stmt)
2403 sparse_error(stmt->pos, "label '%s' redefined", show_ident(token->ident));
2404 s->stmt = stmt;
2405 token = skip_attributes(token->next->next);
2406 return statement(token, &stmt->label_statement);
2410 if (match_op(token, '{')) {
2411 stmt->type = STMT_COMPOUND;
2412 start_symbol_scope(stmt->pos);
2413 token = compound_statement(token->next, stmt);
2414 end_symbol_scope();
2416 return expect(token, '}', "at end of compound statement");
2419 stmt->type = STMT_EXPRESSION;
2420 return expression_statement(token, &stmt->expression);
2423 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2424 static struct token *label_statement(struct token *token)
2426 while (token_type(token) == TOKEN_IDENT) {
2427 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2428 /* it's block-scope, but we want label namespace */
2429 bind_symbol(sym, token->ident, NS_SYMBOL);
2430 sym->namespace = NS_LABEL;
2431 fn_local_symbol(sym);
2432 token = token->next;
2433 if (!match_op(token, ','))
2434 break;
2435 token = token->next;
2437 return expect(token, ';', "at end of label declaration");
2440 static struct token * statement_list(struct token *token, struct statement_list **list)
2442 int seen_statement = 0;
2443 while (token_type(token) == TOKEN_IDENT &&
2444 token->ident == &__label___ident)
2445 token = label_statement(token->next);
2446 for (;;) {
2447 struct statement * stmt;
2448 if (eof_token(token))
2449 break;
2450 if (match_op(token, '}'))
2451 break;
2452 if (match_ident(token, &_Static_assert_ident)) {
2453 token = parse_static_assert(token, NULL);
2454 continue;
2456 if (lookup_type(token)) {
2457 if (seen_statement) {
2458 warning(token->pos, "mixing declarations and code");
2459 seen_statement = 0;
2461 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2462 token = external_declaration(token, &stmt->declaration, NULL);
2463 } else {
2464 seen_statement = Wdeclarationafterstatement;
2465 token = statement(token, &stmt);
2467 add_statement(list, stmt);
2469 return token;
2472 static struct token *identifier_list(struct token *token, struct symbol *fn)
2474 struct symbol_list **list = &fn->arguments;
2475 for (;;) {
2476 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2477 sym->ident = token->ident;
2478 token = token->next;
2479 sym->endpos = token->pos;
2480 sym->ctype.base_type = &incomplete_ctype;
2481 add_symbol(list, sym);
2482 if (!match_op(token, ',') ||
2483 token_type(token->next) != TOKEN_IDENT ||
2484 lookup_type(token->next))
2485 break;
2486 token = token->next;
2488 return token;
2491 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2493 struct symbol_list **list = &fn->arguments;
2495 for (;;) {
2496 struct symbol *sym;
2498 if (match_op(token, SPECIAL_ELLIPSIS)) {
2499 fn->variadic = 1;
2500 token = token->next;
2501 break;
2504 sym = alloc_symbol(token->pos, SYM_NODE);
2505 token = parameter_declaration(token, sym);
2506 if (sym->ctype.base_type == &void_ctype) {
2507 /* Special case: (void) */
2508 if (!*list && !sym->ident)
2509 break;
2510 warning(token->pos, "void parameter");
2512 add_symbol(list, sym);
2513 if (!match_op(token, ','))
2514 break;
2515 token = token->next;
2517 return token;
2520 struct token *compound_statement(struct token *token, struct statement *stmt)
2522 token = statement_list(token, &stmt->stmts);
2523 return token;
2526 static struct expression *identifier_expression(struct token *token)
2528 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2529 expr->expr_ident = token->ident;
2530 return expr;
2533 static struct expression *index_expression(struct expression *from, struct expression *to)
2535 int idx_from, idx_to;
2536 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2538 idx_from = const_expression_value(from);
2539 idx_to = idx_from;
2540 if (to) {
2541 idx_to = const_expression_value(to);
2542 if (idx_to < idx_from || idx_from < 0)
2543 warning(from->pos, "nonsense array initializer index range");
2545 expr->idx_from = idx_from;
2546 expr->idx_to = idx_to;
2547 return expr;
2550 static struct token *single_initializer(struct expression **ep, struct token *token)
2552 int expect_equal = 0;
2553 struct token *next = token->next;
2554 struct expression **tail = ep;
2555 int nested;
2557 *ep = NULL;
2559 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2560 struct expression *expr = identifier_expression(token);
2561 if (Wold_initializer)
2562 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2563 token = initializer(&expr->ident_expression, next->next);
2564 if (expr->ident_expression)
2565 *ep = expr;
2566 return token;
2569 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2570 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2571 struct expression *expr = identifier_expression(next);
2572 *tail = expr;
2573 tail = &expr->ident_expression;
2574 expect_equal = 1;
2575 token = next->next;
2576 } else if (match_op(token, '[')) {
2577 struct expression *from = NULL, *to = NULL, *expr;
2578 token = constant_expression(token->next, &from);
2579 if (!from) {
2580 sparse_error(token->pos, "Expected constant expression");
2581 break;
2583 if (match_op(token, SPECIAL_ELLIPSIS))
2584 token = constant_expression(token->next, &to);
2585 expr = index_expression(from, to);
2586 *tail = expr;
2587 tail = &expr->idx_expression;
2588 token = expect(token, ']', "at end of initializer index");
2589 if (nested)
2590 expect_equal = 1;
2591 } else {
2592 break;
2595 if (nested && !expect_equal) {
2596 if (!match_op(token, '='))
2597 warning(token->pos, "obsolete array initializer, use C99 syntax");
2598 else
2599 expect_equal = 1;
2601 if (expect_equal)
2602 token = expect(token, '=', "at end of initializer index");
2604 token = initializer(tail, token);
2605 if (!*tail)
2606 *ep = NULL;
2607 return token;
2610 static struct token *initializer_list(struct expression_list **list, struct token *token)
2612 struct expression *expr;
2614 for (;;) {
2615 token = single_initializer(&expr, token);
2616 if (!expr)
2617 break;
2618 add_expression(list, expr);
2619 if (!match_op(token, ','))
2620 break;
2621 token = token->next;
2623 return token;
2626 struct token *initializer(struct expression **tree, struct token *token)
2628 if (match_op(token, '{')) {
2629 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2630 *tree = expr;
2631 token = initializer_list(&expr->expr_list, token->next);
2632 return expect(token, '}', "at end of initializer");
2634 return assignment_expression(token, tree);
2637 static void declare_argument(struct symbol *sym, struct symbol *fn)
2639 if (!sym->ident) {
2640 sparse_error(sym->pos, "no identifier for function argument");
2641 return;
2643 bind_symbol(sym, sym->ident, NS_SYMBOL);
2646 static int is_syscall(struct symbol *sym)
2648 char *macro;
2649 char *name;
2650 int is_syscall = 0;
2652 macro = get_macro_name(sym->pos);
2653 if (macro &&
2654 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
2655 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
2656 is_syscall = 1;
2658 name = sym->ident->name;
2660 if (name && strncmp(name, "sys_", 4) ==0)
2661 is_syscall = 1;
2663 if (name && strncmp(name, "compat_sys_", 11) == 0)
2664 is_syscall = 1;
2666 return is_syscall;
2670 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2671 struct symbol_list **list)
2673 struct symbol_list **old_symbol_list;
2674 struct symbol *base_type = decl->ctype.base_type;
2675 struct statement *stmt, **p;
2676 struct symbol *prev;
2677 struct symbol *arg;
2679 old_symbol_list = function_symbol_list;
2680 if (decl->ctype.modifiers & MOD_INLINE) {
2681 function_symbol_list = &decl->inline_symbol_list;
2682 p = &base_type->inline_stmt;
2683 } else {
2684 function_symbol_list = &decl->symbol_list;
2685 p = &base_type->stmt;
2687 function_computed_target_list = NULL;
2688 function_computed_goto_list = NULL;
2690 if (decl->ctype.modifiers & MOD_EXTERN) {
2691 if (!(decl->ctype.modifiers & MOD_INLINE))
2692 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2694 if (!(decl->ctype.modifiers & MOD_STATIC))
2695 decl->ctype.modifiers |= MOD_EXTERN;
2697 stmt = start_function(decl);
2699 *p = stmt;
2700 FOR_EACH_PTR (base_type->arguments, arg) {
2701 declare_argument(arg, base_type);
2702 } END_FOR_EACH_PTR(arg);
2704 token = compound_statement(token->next, stmt);
2706 end_function(decl);
2707 if (!(decl->ctype.modifiers & MOD_INLINE))
2708 add_symbol(list, decl);
2709 else if (is_syscall(decl)) {
2710 add_symbol(list, decl);
2712 printf("parse.c decl: %s\n", decl->ident->name);
2713 char *macro = get_macro_name(decl->pos);
2714 printf("decl macro: %s\n", macro);
2717 check_declaration(decl);
2718 decl->definition = decl;
2719 prev = decl->same_symbol;
2720 if (prev && prev->definition) {
2721 warning(decl->pos, "multiple definitions for function '%s'",
2722 show_ident(decl->ident));
2723 info(prev->definition->pos, " the previous one is here");
2724 } else {
2725 while (prev) {
2726 rebind_scope(prev, decl->scope);
2727 prev->definition = decl;
2728 prev = prev->same_symbol;
2731 function_symbol_list = old_symbol_list;
2732 if (function_computed_goto_list) {
2733 if (!function_computed_target_list)
2734 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2735 else {
2736 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2737 stmt->target_list = function_computed_target_list;
2738 } END_FOR_EACH_PTR(stmt);
2741 return expect(token, '}', "at end of function");
2744 static void promote_k_r_types(struct symbol *arg)
2746 struct symbol *base = arg->ctype.base_type;
2747 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2748 arg->ctype.base_type = &int_ctype;
2752 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2754 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2755 struct symbol *arg;
2757 FOR_EACH_PTR(real_args, arg) {
2758 struct symbol *type;
2760 /* This is quadratic in the number of arguments. We _really_ don't care */
2761 FOR_EACH_PTR(argtypes, type) {
2762 if (type->ident == arg->ident)
2763 goto match;
2764 } END_FOR_EACH_PTR(type);
2765 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2766 continue;
2767 match:
2768 type->used = 1;
2769 /* "char" and "short" promote to "int" */
2770 promote_k_r_types(type);
2772 arg->ctype = type->ctype;
2773 } END_FOR_EACH_PTR(arg);
2775 FOR_EACH_PTR(argtypes, arg) {
2776 if (!arg->used)
2777 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2778 } END_FOR_EACH_PTR(arg);
2782 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2783 struct symbol_list **list)
2785 struct symbol_list *args = NULL;
2787 if (Wnon_ansi_function_declaration)
2788 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2790 do {
2791 token = declaration_list(token, &args);
2792 if (!match_op(token, ';')) {
2793 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2794 break;
2796 token = token->next;
2797 } while (lookup_type(token));
2799 apply_k_r_types(args, decl);
2801 if (!match_op(token, '{')) {
2802 sparse_error(token->pos, "expected function body");
2803 return token;
2805 return parse_function_body(token, decl, list);
2808 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2810 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2811 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2812 struct statement *stmt;
2814 anon->ctype.base_type = fn;
2815 stmt = alloc_statement(token->pos, STMT_NONE);
2816 fn->stmt = stmt;
2818 token = parse_asm_statement(token, stmt);
2820 add_symbol(list, anon);
2821 return token;
2824 struct token *external_declaration(struct token *token, struct symbol_list **list,
2825 validate_decl_t validate_decl)
2827 struct ident *ident = NULL;
2828 struct symbol *decl;
2829 struct decl_state ctx = { .ident = &ident };
2830 struct ctype saved;
2831 struct symbol *base_type;
2832 unsigned long mod;
2833 int is_typedef;
2835 if (match_ident(token, &_Pragma_ident))
2836 return parse_underscore_Pragma(token);
2838 /* Top-level inline asm or static assertion? */
2839 if (token_type(token) == TOKEN_IDENT) {
2840 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2841 if (s && s->op->toplevel)
2842 return s->op->toplevel(token, list);
2845 /* Parse declaration-specifiers, if any */
2846 token = declaration_specifiers(token, &ctx);
2847 mod = storage_modifiers(&ctx);
2848 decl = alloc_symbol(token->pos, SYM_NODE);
2849 /* Just a type declaration? */
2850 if (match_op(token, ';')) {
2851 apply_modifiers(token->pos, &ctx);
2852 return token->next;
2855 saved = ctx.ctype;
2856 token = declarator(token, &ctx);
2857 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2858 apply_modifiers(token->pos, &ctx);
2860 decl->ctype = ctx.ctype;
2861 decl->ctype.modifiers |= mod;
2862 decl->endpos = token->pos;
2864 /* Just a type declaration? */
2865 if (!ident) {
2866 warning(token->pos, "missing identifier in declaration");
2867 return expect(token, ';', "at the end of type declaration");
2870 /* type define declaration? */
2871 is_typedef = ctx.storage_class == STypedef;
2873 /* Typedefs don't have meaningful storage */
2874 if (is_typedef)
2875 decl->ctype.modifiers |= MOD_USERTYPE;
2877 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2879 base_type = decl->ctype.base_type;
2881 if (is_typedef) {
2882 if (base_type && !base_type->ident) {
2883 switch (base_type->type) {
2884 case SYM_STRUCT:
2885 case SYM_UNION:
2886 case SYM_ENUM:
2887 case SYM_RESTRICT:
2888 base_type->ident = ident;
2889 break;
2890 default:
2891 break;
2894 } else if (base_type && base_type->type == SYM_FN) {
2895 if (base_type->ctype.base_type == &incomplete_ctype) {
2896 warning(decl->pos, "'%s()' has implicit return type",
2897 show_ident(decl->ident));
2898 base_type->ctype.base_type = &int_ctype;
2900 /* K&R argument declaration? */
2901 if (lookup_type(token))
2902 return parse_k_r_arguments(token, decl, list);
2903 if (match_op(token, '{'))
2904 return parse_function_body(token, decl, list);
2906 if (!(decl->ctype.modifiers & MOD_STATIC))
2907 decl->ctype.modifiers |= MOD_EXTERN;
2908 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2909 sparse_error(token->pos, "void declaration");
2911 if (base_type == &incomplete_ctype) {
2912 warning(decl->pos, "'%s' has implicit type", show_ident(decl->ident));
2913 decl->ctype.base_type = &int_ctype;;
2916 for (;;) {
2917 if (!is_typedef && match_op(token, '=')) {
2918 token = initializer(&decl->initializer, token->next);
2920 if (!is_typedef) {
2921 if (validate_decl)
2922 validate_decl(decl);
2924 if (decl->initializer && decl->ctype.modifiers & MOD_EXTERN) {
2925 warning(decl->pos, "symbol with external linkage has initializer");
2926 decl->ctype.modifiers &= ~MOD_EXTERN;
2929 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2930 add_symbol(list, decl);
2931 fn_local_symbol(decl);
2934 check_declaration(decl);
2935 if (decl->same_symbol) {
2936 decl->definition = decl->same_symbol->definition;
2937 decl->op = decl->same_symbol->op;
2940 if (!match_op(token, ','))
2941 break;
2943 token = token->next;
2944 ident = NULL;
2945 decl = alloc_symbol(token->pos, SYM_NODE);
2946 ctx.ctype = saved;
2947 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2948 token = declarator(token, &ctx);
2949 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2950 apply_modifiers(token->pos, &ctx);
2951 decl->ctype = ctx.ctype;
2952 decl->ctype.modifiers |= mod;
2953 decl->endpos = token->pos;
2954 if (!ident) {
2955 sparse_error(token->pos, "expected identifier name in type definition");
2956 return token;
2959 if (is_typedef)
2960 decl->ctype.modifiers |= MOD_USERTYPE;
2962 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2964 /* Function declarations are automatically extern unless specifically static */
2965 base_type = decl->ctype.base_type;
2966 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2967 if (!(decl->ctype.modifiers & MOD_STATIC))
2968 decl->ctype.modifiers |= MOD_EXTERN;
2971 return expect(token, ';', "at end of declaration");