math: INT_MAX is never a hard max
[smatch.git] / parse.c
blob1091844d249bf18cd86d400ef5eeeaadad082d1d
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);
54 typedef struct token *declarator_t(struct token *, struct symbol *, struct decl_state *);
55 static declarator_t
56 struct_specifier, union_specifier, enum_specifier,
57 attribute_specifier, typeof_specifier,
58 storage_specifier, thread_specifier;
59 static declarator_t generic_qualifier;
60 static declarator_t autotype_specifier;
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_function,
84 attribute_bitwise,
85 attribute_address_space, attribute_context,
86 attribute_designated_init,
87 attribute_transparent_union, ignore_attribute,
88 attribute_mode, attribute_force;
90 typedef struct symbol *to_mode_t(struct symbol *);
92 static to_mode_t
93 to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode;
94 static to_mode_t to_pointer_mode, to_word_mode;
96 enum {
97 Set_T = 1,
98 Set_S = 2,
99 Set_Char = 4,
100 Set_Int = 8,
101 Set_Double = 16,
102 Set_Float = 32,
103 Set_Signed = 64,
104 Set_Unsigned = 128,
105 Set_Short = 256,
106 Set_Long = 512,
107 Set_Vlong = 1024,
108 Set_Int128 = 2048,
109 Set_Any = Set_T | Set_Short | Set_Long | Set_Signed | Set_Unsigned
112 enum {
113 CInt = 0, CSInt, CUInt, CReal,
116 static void asm_modifier(struct token *token, unsigned long *mods, unsigned long mod)
118 if (*mods & mod)
119 warning(token->pos, "duplicated asm modifier");
120 *mods |= mod;
123 static struct symbol_op typedef_op = {
124 .type = KW_MODIFIER,
125 .declarator = storage_specifier,
128 static struct symbol_op inline_op = {
129 .type = KW_MODIFIER,
130 .declarator = generic_qualifier,
131 .asm_modifier = asm_modifier,
134 static struct symbol_op noreturn_op = {
135 .type = KW_MODIFIER,
136 .declarator = generic_qualifier,
139 static declarator_t alignas_specifier;
140 static struct symbol_op alignas_op = {
141 .type = KW_MODIFIER,
142 .declarator = alignas_specifier,
145 static struct symbol_op auto_op = {
146 .type = KW_MODIFIER,
147 .declarator = storage_specifier,
150 static struct symbol_op register_op = {
151 .type = KW_MODIFIER,
152 .declarator = storage_specifier,
155 static struct symbol_op static_op = {
156 .type = KW_MODIFIER|KW_STATIC,
157 .declarator = storage_specifier,
160 static struct symbol_op extern_op = {
161 .type = KW_MODIFIER,
162 .declarator = storage_specifier,
165 static struct symbol_op thread_op = {
166 .type = KW_MODIFIER,
167 .declarator = thread_specifier,
170 static struct symbol_op const_op = {
171 .type = KW_QUALIFIER,
172 .declarator = generic_qualifier,
175 static struct symbol_op volatile_op = {
176 .type = KW_QUALIFIER,
177 .declarator = generic_qualifier,
178 .asm_modifier = asm_modifier,
181 static struct symbol_op restrict_op = {
182 .type = KW_QUALIFIER,
183 .declarator = generic_qualifier,
186 static struct symbol_op atomic_op = {
187 .type = KW_QUALIFIER,
188 .declarator = generic_qualifier,
191 static struct symbol_op typeof_op = {
192 .type = KW_SPECIFIER,
193 .declarator = typeof_specifier,
194 .test = Set_Any,
195 .set = Set_S|Set_T,
198 static struct symbol_op autotype_op = {
199 .type = KW_SPECIFIER,
200 .declarator = autotype_specifier,
201 .test = Set_Any,
202 .set = Set_S|Set_T,
205 static struct symbol_op attribute_op = {
206 .type = KW_ATTRIBUTE,
207 .declarator = attribute_specifier,
210 static struct symbol_op struct_op = {
211 .type = KW_SPECIFIER,
212 .declarator = struct_specifier,
213 .test = Set_Any,
214 .set = Set_S|Set_T,
217 static struct symbol_op union_op = {
218 .type = KW_SPECIFIER,
219 .declarator = union_specifier,
220 .test = Set_Any,
221 .set = Set_S|Set_T,
224 static struct symbol_op enum_op = {
225 .type = KW_SPECIFIER,
226 .declarator = enum_specifier,
227 .test = Set_Any,
228 .set = Set_S|Set_T,
231 static struct symbol_op spec_op = {
232 .type = KW_SPECIFIER | KW_EXACT,
233 .test = Set_Any,
234 .set = Set_S|Set_T,
237 static struct symbol_op char_op = {
238 .type = KW_SPECIFIER,
239 .test = Set_T|Set_Long|Set_Short,
240 .set = Set_T|Set_Char,
241 .class = CInt,
244 static struct symbol_op int_op = {
245 .type = KW_SPECIFIER,
246 .test = Set_T,
247 .set = Set_T|Set_Int,
250 static struct symbol_op double_op = {
251 .type = KW_SPECIFIER,
252 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
253 .set = Set_T|Set_Double,
254 .class = CReal,
257 static struct symbol_op float_op = {
258 .type = KW_SPECIFIER,
259 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long,
260 .set = Set_T|Set_Float,
261 .class = CReal,
264 static struct symbol_op short_op = {
265 .type = KW_SPECIFIER,
266 .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short,
267 .set = Set_Short,
270 static struct symbol_op signed_op = {
271 .type = KW_SPECIFIER,
272 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
273 .set = Set_Signed,
274 .class = CSInt,
277 static struct symbol_op unsigned_op = {
278 .type = KW_SPECIFIER,
279 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
280 .set = Set_Unsigned,
281 .class = CUInt,
284 static struct symbol_op long_op = {
285 .type = KW_SPECIFIER,
286 .test = Set_S|Set_Char|Set_Float|Set_Short|Set_Vlong,
287 .set = Set_Long,
290 static struct symbol_op int128_op = {
291 .type = KW_SPECIFIER,
292 .test = Set_S|Set_T|Set_Char|Set_Short|Set_Int|Set_Float|Set_Double|Set_Long|Set_Vlong|Set_Int128,
293 .set = Set_T|Set_Int128|Set_Vlong,
294 .class = CInt,
297 static struct symbol_op if_op = {
298 .statement = parse_if_statement,
301 static struct symbol_op return_op = {
302 .statement = parse_return_statement,
305 static struct symbol_op loop_iter_op = {
306 .statement = parse_loop_iterator,
309 static struct symbol_op default_op = {
310 .statement = parse_default_statement,
313 static struct symbol_op case_op = {
314 .statement = parse_case_statement,
317 static struct symbol_op switch_op = {
318 .statement = parse_switch_statement,
321 static struct symbol_op for_op = {
322 .statement = parse_for_statement,
325 static struct symbol_op while_op = {
326 .statement = parse_while_statement,
329 static struct symbol_op do_op = {
330 .statement = parse_do_statement,
333 static struct symbol_op goto_op = {
334 .statement = parse_goto_statement,
337 static struct symbol_op __context___op = {
338 .statement = parse_context_statement,
339 .attribute = attribute_context,
342 static struct symbol_op range_op = {
343 .statement = parse_range_statement,
346 static struct symbol_op asm_op = {
347 .type = KW_ASM,
348 .statement = parse_asm_statement,
349 .toplevel = toplevel_asm_declaration,
352 static struct symbol_op static_assert_op = {
353 .toplevel = parse_static_assert,
356 static struct symbol_op packed_op = {
357 .attribute = attribute_packed,
360 static struct symbol_op aligned_op = {
361 .attribute = attribute_aligned,
364 static struct symbol_op attr_mod_op = {
365 .attribute = attribute_modifier,
368 static struct symbol_op attr_fun_op = {
369 .attribute = attribute_function,
372 static struct symbol_op attr_bitwise_op = {
373 .attribute = attribute_bitwise,
376 static struct symbol_op attr_force_op = {
377 .attribute = attribute_force,
380 static struct symbol_op address_space_op = {
381 .attribute = attribute_address_space,
384 static struct symbol_op mode_op = {
385 .attribute = attribute_mode,
388 static struct symbol_op context_op = {
389 .attribute = attribute_context,
392 static struct symbol_op designated_init_op = {
393 .attribute = attribute_designated_init,
396 static struct symbol_op transparent_union_op = {
397 .attribute = attribute_transparent_union,
400 static struct symbol_op ignore_attr_op = {
401 .attribute = ignore_attribute,
404 static struct symbol_op mode_QI_op = {
405 .type = KW_MODE,
406 .to_mode = to_QI_mode
409 static struct symbol_op mode_HI_op = {
410 .type = KW_MODE,
411 .to_mode = to_HI_mode
414 static struct symbol_op mode_SI_op = {
415 .type = KW_MODE,
416 .to_mode = to_SI_mode
419 static struct symbol_op mode_DI_op = {
420 .type = KW_MODE,
421 .to_mode = to_DI_mode
424 static struct symbol_op mode_TI_op = {
425 .type = KW_MODE,
426 .to_mode = to_TI_mode
429 static struct symbol_op mode_pointer_op = {
430 .type = KW_MODE,
431 .to_mode = to_pointer_mode
434 static struct symbol_op mode_word_op = {
435 .type = KW_MODE,
436 .to_mode = to_word_mode
440 * Define the keyword and their effects.
441 * The entries in the 'typedef' and put in NS_TYPEDEF and
442 * are automatically set as reserved keyword while the ones
443 * in the 'keyword' table are just put in NS_KEYWORD.
445 * The entries are added via the 3 macros:
446 * N() for entries with "name" only,
447 * D() for entries with "name" & "__name__",
448 * A() for entries with "name", "__name" & "__name__",
449 * U() for entries with "__name" & "__name__".
451 static struct init_keyword {
452 const char *name;
453 struct symbol_op *op;
454 struct symbol *type;
455 unsigned long mods;
456 } typedefs[] = {
457 #define N(I, O,...) { I, O,##__VA_ARGS__ }
458 #define D(I, O,...) N(I,O,##__VA_ARGS__ ), \
459 N("__" I "__",O,##__VA_ARGS__)
460 #define A(I, O,...) N(I,O,##__VA_ARGS__ ), \
461 N("__" I,O,##__VA_ARGS__), \
462 N("__" I "__",O,##__VA_ARGS__)
463 #define U(I, O,...) N("__" I,O,##__VA_ARGS__), \
464 N("__" I "__",O,##__VA_ARGS__)
465 /* Storage classes */
466 N("auto", &auto_op, .mods = MOD_AUTO),
467 N("register", &register_op, .mods = MOD_REGISTER),
468 N("static", &static_op, .mods = MOD_STATIC),
469 N("extern", &extern_op, .mods = MOD_EXTERN),
470 N("__thread", &thread_op),
471 N("_Thread_local", &thread_op),
473 A("inline", &inline_op, .mods = MOD_INLINE),
475 /* Typedef ... */
476 N("typedef", &typedef_op, .mods = MOD_USERTYPE),
477 A("typeof", &typeof_op),
478 N("__auto_type", &autotype_op),
480 /* Type qualifiers */
481 A("const", &const_op, .mods = MOD_CONST),
482 A("volatile", &volatile_op, .mods = MOD_VOLATILE),
483 A("restrict", &restrict_op, .mods = MOD_RESTRICT),
485 N("_Atomic", &atomic_op, .mods = MOD_ATOMIC),
486 N("_Noreturn", &noreturn_op, .mods = MOD_NORETURN),
487 N("_Alignas", &alignas_op),
489 U("attribute", &attribute_op),
491 /* Type specifiers */
492 N("struct", &struct_op),
493 N("union", &union_op),
494 N("enum", &enum_op),
496 N("void", &spec_op, .type = &void_ctype),
497 N("char", &char_op),
498 N("short", &short_op),
499 N("int", &int_op),
500 N("long", &long_op),
501 N("float", &float_op),
502 N("double", &double_op),
503 A("signed", &signed_op),
504 N("unsigned", &unsigned_op),
505 N("__int128", &int128_op),
506 N("_Bool", &spec_op, .type = &bool_ctype),
508 /* Predeclared types */
509 N("__builtin_va_list", &spec_op, .type = &ptr_ctype),
510 N("__builtin_ms_va_list",&spec_op, .type = &ptr_ctype),
511 N("__int128_t", &spec_op, .type = &sint128_ctype),
512 N("__uint128_t", &spec_op, .type = &uint128_ctype),
513 N("_Float32", &spec_op, .type = &float32_ctype),
514 N("_Float32x", &spec_op, .type = &float32x_ctype),
515 N("_Float64", &spec_op, .type = &float64_ctype),
516 N("_Float64x", &spec_op, .type = &float64x_ctype),
517 N("_Float128", &spec_op, .type = &float128_ctype),
518 }, keywords[] = {
519 /* Statements */
520 N("if", &if_op),
521 N("return", &return_op),
522 N("break", &loop_iter_op),
523 N("continue", &loop_iter_op),
524 N("default", &default_op),
525 N("case", &case_op),
526 N("switch", &switch_op),
527 N("for", &for_op),
528 N("while", &while_op),
529 N("do", &do_op),
530 N("goto", &goto_op),
531 A("asm", &asm_op),
532 N("context", &context_op),
533 N("__context__", &__context___op),
534 N("__range__", &range_op),
535 N("_Static_assert", &static_assert_op),
537 /* Attributes */
538 D("packed", &packed_op),
539 D("aligned", &aligned_op),
540 D("nocast", &attr_mod_op, .mods = MOD_NOCAST),
541 D("noderef", &attr_mod_op, .mods = MOD_NODEREF),
542 D("safe", &attr_mod_op, .mods = MOD_SAFE),
543 D("unused", &attr_mod_op, .mods = MOD_UNUSED),
544 D("externally_visible", &attr_mod_op, .mods = MOD_EXT_VISIBLE),
545 D("force", &attr_force_op),
546 D("bitwise", &attr_bitwise_op, .mods = MOD_BITWISE),
547 D("address_space", &address_space_op),
548 D("designated_init", &designated_init_op),
549 D("transparent_union", &transparent_union_op),
550 D("noreturn", &attr_fun_op, .mods = MOD_NORETURN),
551 D("pure", &attr_fun_op, .mods = MOD_PURE),
552 A("const", &attr_fun_op, .mods = MOD_PURE),
553 D("gnu_inline", &attr_fun_op, .mods = MOD_GNU_INLINE),
555 /* Modes */
556 D("mode", &mode_op),
557 D("QI", &mode_QI_op),
558 D("HI", &mode_HI_op),
559 D("SI", &mode_SI_op),
560 D("DI", &mode_DI_op),
561 D("TI", &mode_TI_op),
562 D("byte", &mode_QI_op),
563 D("pointer", &mode_pointer_op),
564 D("word", &mode_word_op),
568 static const char *ignored_attributes[] = {
570 #define GCC_ATTR(x) \
571 STRINGIFY(x), \
572 STRINGIFY(__##x##__),
574 #include "gcc-attr-list.h"
576 #undef GCC_ATTR
578 "bounded",
579 "__bounded__",
580 "__noclone",
581 "__nonnull",
582 "__nothrow",
586 static void init_keyword(int stream, struct init_keyword *kw, enum namespace ns)
588 struct symbol *sym = create_symbol(stream, kw->name, SYM_KEYWORD, ns);
589 sym->ident->keyword = 1;
590 sym->ident->reserved |= (ns == NS_TYPEDEF);
591 sym->ctype.modifiers = kw->mods;
592 sym->ctype.base_type = kw->type;
593 sym->op = kw->op;
596 void init_parser(int stream)
598 int i;
600 for (i = 0; i < ARRAY_SIZE(typedefs); i++)
601 init_keyword(stream, &typedefs[i], NS_TYPEDEF);
602 for (i = 0; i < ARRAY_SIZE(keywords); i++)
603 init_keyword(stream, &keywords[i], NS_KEYWORD);
605 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
606 const char * name = ignored_attributes[i];
607 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
608 NS_KEYWORD);
609 if (!sym->op) {
610 sym->ident->keyword = 1;
611 sym->op = &ignore_attr_op;
617 static struct token *skip_to(struct token *token, int op)
619 while (!match_op(token, op) && !eof_token(token))
620 token = token->next;
621 return token;
624 static struct token bad_token = { .pos.type = TOKEN_BAD };
625 struct token *expect(struct token *token, int op, const char *where)
627 if (!match_op(token, op)) {
628 if (token != &bad_token) {
629 bad_token.next = token;
630 sparse_error(token->pos, "Expected %s %s", show_special(op), where);
631 sparse_error(token->pos, "got %s", show_token(token));
633 if (op == ';')
634 return skip_to(token, op);
635 return &bad_token;
637 return token->next;
641 // issue an error message on new parsing errors
642 // @token: the current token
643 // @errmsg: the error message
644 // If the current token is from a previous error, an error message
645 // has already been issued, so nothing more is done.
646 // Otherwise, @errmsg is displayed followed by the current token.
647 static void unexpected(struct token *token, const char *errmsg)
649 if (token == &bad_token)
650 return;
651 sparse_error(token->pos, "%s", errmsg);
652 sparse_error(token->pos, "got %s", show_token(token));
655 // Add a symbol to the list of function-local symbols
656 static void fn_local_symbol(struct symbol *sym)
658 if (function_symbol_list)
659 add_symbol(function_symbol_list, sym);
662 struct statement *alloc_statement(struct position pos, int type)
664 struct statement *stmt = __alloc_statement(0);
665 stmt->type = type;
666 stmt->pos = pos;
667 return stmt;
670 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
672 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
674 static void apply_modifiers(struct position pos, struct decl_state *ctx)
676 struct symbol *ctype;
677 if (!ctx->mode)
678 return;
679 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
680 if (!ctype)
681 sparse_error(pos, "don't know how to apply mode to %s",
682 show_typename(ctx->ctype.base_type));
683 else
684 ctx->ctype.base_type = ctype;
688 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
690 struct symbol *sym = alloc_symbol(pos, type);
692 sym->ctype.base_type = ctype->base_type;
693 sym->ctype.modifiers = ctype->modifiers;
695 ctype->base_type = sym;
696 ctype->modifiers = 0;
697 return sym;
701 * NOTE! NS_LABEL is not just a different namespace,
702 * it also ends up using function scope instead of the
703 * regular symbol scope.
705 struct symbol *label_symbol(struct token *token, int used)
707 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
708 if (!sym) {
709 sym = alloc_symbol(token->pos, SYM_LABEL);
710 bind_symbol(sym, token->ident, NS_LABEL);
711 if (used)
712 sym->used = 1;
713 fn_local_symbol(sym);
715 return sym;
718 static struct token *struct_union_enum_specifier(enum type type,
719 struct token *token, struct decl_state *ctx,
720 struct token *(*parse)(struct token *, struct symbol *))
722 struct symbol *sym;
723 struct position *repos;
725 token = handle_attributes(token, ctx);
726 if (token_type(token) == TOKEN_IDENT) {
727 sym = lookup_symbol(token->ident, NS_STRUCT);
728 if (!sym ||
729 (is_outer_scope(sym->scope) &&
730 (match_op(token->next,';') || match_op(token->next,'{')))) {
731 // Either a new symbol, or else an out-of-scope
732 // symbol being redefined.
733 sym = alloc_symbol(token->pos, type);
734 bind_symbol(sym, token->ident, NS_STRUCT);
736 if (sym->type != type)
737 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
738 ctx->ctype.base_type = sym;
739 repos = &token->pos;
740 token = token->next;
741 if (match_op(token, '{')) {
742 struct decl_state attr = { .ctype.base_type = sym, };
744 // The following test is actually wrong for empty
745 // structs, but (1) they are not C99, (2) gcc does
746 // the same thing, and (3) it's easier.
747 if (sym->symbol_list)
748 error_die(token->pos, "redefinition of %s", show_typename (sym));
749 sym->pos = *repos;
750 token = parse(token->next, sym);
751 token = expect(token, '}', "at end of struct-union-enum-specifier");
753 token = handle_attributes(token, &attr);
754 apply_ctype(token->pos, &attr.ctype, &sym->ctype);
756 // Mark the structure as needing re-examination
757 sym->examined = 0;
758 sym->endpos = token->pos;
760 return token;
763 // private struct/union/enum type
764 if (!match_op(token, '{')) {
765 sparse_error(token->pos, "expected declaration");
766 ctx->ctype.base_type = &bad_ctype;
767 return token;
770 sym = alloc_symbol(token->pos, type);
771 set_current_scope(sym); // used by dissect
772 token = parse(token->next, sym);
773 ctx->ctype.base_type = sym;
774 token = expect(token, '}', "at end of specifier");
775 sym->endpos = token->pos;
777 return token;
780 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
782 struct symbol *field, *last = NULL;
783 struct token *res;
784 res = struct_declaration_list(token, &sym->symbol_list);
785 FOR_EACH_PTR(sym->symbol_list, field) {
786 if (!field->ident) {
787 struct symbol *base = field->ctype.base_type;
788 if (base && base->type == SYM_BITFIELD)
789 continue;
791 if (last)
792 last->next_subobject = field;
793 last = field;
794 } END_FOR_EACH_PTR(field);
795 return res;
798 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
800 return struct_declaration_list(token, &sym->symbol_list);
803 static struct token *struct_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
805 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
808 static struct token *union_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
810 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
814 // safe right shift
816 // This allow to use a shift amount as big (or bigger)
817 // than the width of the value to be shifted, in which case
818 // the result is, of course, 0.
819 static unsigned long long rshift(unsigned long long val, unsigned int n)
821 if (n >= (sizeof(val) * 8))
822 return 0;
823 return val >> n;
826 struct range {
827 long long neg;
828 unsigned long long pos;
831 static void update_range(struct range *range, unsigned long long uval, struct symbol *vtype)
833 long long sval = uval;
835 if (is_signed_type(vtype) && (sval < 0)) {
836 if (sval < range->neg)
837 range->neg = sval;
838 } else {
839 if (uval > range->pos)
840 range->pos = uval;
844 static int type_is_ok(struct symbol *type, struct range range)
846 int shift = type->bit_size;
847 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
849 if (!is_unsigned)
850 shift--;
851 if (rshift(range.pos, shift))
852 return 0;
853 if (range.neg == 0)
854 return 1;
855 if (is_unsigned)
856 return 0;
857 if (rshift(~range.neg, shift))
858 return 0;
859 return 1;
862 static struct range type_range(struct symbol *type)
864 struct range range;
865 unsigned int size = type->bit_size;
866 unsigned long long max;
867 long long min;
869 if (is_signed_type(type)) {
870 min = sign_bit(size);
871 max = min - 1;
872 } else {
873 min = 0;
874 max = bits_mask(size);
877 range.pos = max;
878 range.neg = min;
879 return range;
882 static int val_in_range(struct range *range, long long sval, struct symbol *vtype)
884 unsigned long long uval = sval;
886 if (is_signed_type(vtype) && (sval < 0))
887 return range->neg <= sval;
888 else
889 return uval <= range->pos;
892 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
894 struct range irange = type_range(&int_ctype);
895 struct symbol *sym;
897 FOR_EACH_PTR(list, sym) {
898 struct expression *expr = sym->initializer;
899 struct symbol *ctype;
900 long long val;
901 if (expr->type != EXPR_VALUE)
902 continue;
903 ctype = expr->ctype;
904 val = get_expression_value(expr);
905 if (is_int_type(ctype) && val_in_range(&irange, val, ctype)) {
906 expr->ctype = &int_ctype;
907 continue;
909 cast_value(expr, base_type, expr, ctype);
910 expr->ctype = base_type;
911 } END_FOR_EACH_PTR(sym);
914 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
916 unsigned long long lastval = 0;
917 struct symbol *ctype = NULL, *base_type = NULL;
918 struct range range = { };
919 int mix_bitwise = 0;
921 parent->examined = 1;
922 parent->ctype.base_type = &int_ctype;
923 while (token_type(token) == TOKEN_IDENT) {
924 struct expression *expr = NULL;
925 struct token *next = token->next;
926 struct decl_state ctx = { };
927 struct symbol *sym;
929 // FIXME: only 'deprecated' should be accepted
930 next = handle_attributes(next, &ctx);
932 if (match_op(next, '=')) {
933 next = constant_expression(next->next, &expr);
934 lastval = get_expression_value(expr);
935 ctype = &void_ctype;
936 if (expr && expr->ctype)
937 ctype = expr->ctype;
938 } else if (!ctype) {
939 ctype = &int_ctype;
940 } else if (is_int_type(ctype)) {
941 lastval++;
942 } else {
943 error_die(token->pos, "can't increment the last enum member");
946 if (!expr) {
947 expr = alloc_expression(token->pos, EXPR_VALUE);
948 expr->value = lastval;
949 expr->ctype = ctype;
952 sym = alloc_symbol(token->pos, SYM_NODE);
953 bind_symbol(sym, token->ident, NS_SYMBOL);
954 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
955 sym->initializer = expr;
956 sym->enum_member = 1;
957 sym->ctype.base_type = parent;
958 add_ptr_list(&parent->symbol_list, sym);
960 if (base_type != &bad_ctype) {
961 if (ctype->type == SYM_NODE)
962 ctype = ctype->ctype.base_type;
963 if (ctype->type == SYM_ENUM) {
964 if (ctype == parent)
965 ctype = base_type;
966 else
967 ctype = ctype->ctype.base_type;
970 * base_type rules:
971 * - if all enums are of the same type, then
972 * the base_type is that type (two first
973 * cases)
974 * - if enums are of different types, they
975 * all have to be integer types, and the
976 * base type is at least "int_ctype".
977 * - otherwise the base_type is "bad_ctype".
979 if (!base_type || ctype == &bad_ctype) {
980 base_type = ctype;
981 } else if (ctype == base_type) {
982 /* nothing */
983 } else if (is_int_type(base_type) && is_int_type(ctype)) {
984 base_type = &int_ctype;
985 } else if (is_restricted_type(base_type) != is_restricted_type(ctype)) {
986 if (!mix_bitwise++) {
987 warning(expr->pos, "mixed bitwiseness");
989 } else if (is_restricted_type(base_type) && base_type != ctype) {
990 sparse_error(expr->pos, "incompatible restricted type");
991 info(expr->pos, " expected: %s", show_typename(base_type));
992 info(expr->pos, " got: %s", show_typename(ctype));
993 base_type = &bad_ctype;
994 } else if (base_type != &bad_ctype) {
995 sparse_error(token->pos, "bad enum definition");
996 base_type = &bad_ctype;
998 parent->ctype.base_type = base_type;
1000 if (is_int_type(base_type)) {
1001 update_range(&range, lastval, ctype);
1003 token = next;
1005 sym->endpos = token->pos;
1007 if (!match_op(token, ','))
1008 break;
1009 token = token->next;
1011 if (!base_type) {
1012 sparse_error(token->pos, "empty enum definition");
1013 base_type = &bad_ctype;
1015 else if (!is_int_type(base_type))
1017 else if (type_is_ok(&uint_ctype, range))
1018 base_type = &uint_ctype;
1019 else if (type_is_ok(&int_ctype, range))
1020 base_type = &int_ctype;
1021 else if (type_is_ok(&ulong_ctype, range))
1022 base_type = &ulong_ctype;
1023 else if (type_is_ok(&long_ctype, range))
1024 base_type = &long_ctype;
1025 else if (type_is_ok(&ullong_ctype, range))
1026 base_type = &ullong_ctype;
1027 else if (type_is_ok(&llong_ctype, range))
1028 base_type = &llong_ctype;
1029 else
1030 base_type = &bad_ctype;
1031 parent->ctype.base_type = base_type;
1032 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
1033 parent->examined = 0;
1035 if (mix_bitwise)
1036 return token;
1037 cast_enum_list(parent->symbol_list, base_type);
1039 return token;
1042 static struct token *enum_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
1044 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
1045 struct ctype *ctype = &ctx->ctype.base_type->ctype;
1047 if (!ctype->base_type)
1048 ctype->base_type = &incomplete_ctype;
1050 return ret;
1053 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
1055 static struct token *typeof_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
1058 if (!match_op(token, '(')) {
1059 sparse_error(token->pos, "expected '(' after typeof");
1060 return token;
1062 if (lookup_type(token->next)) {
1063 struct symbol *sym;
1064 token = typename(token->next, &sym, NULL);
1065 ctx->ctype.base_type = sym->ctype.base_type;
1066 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
1067 } else {
1068 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
1069 token = parse_expression(token->next, &typeof_sym->initializer);
1071 typeof_sym->endpos = token->pos;
1072 if (!typeof_sym->initializer) {
1073 sparse_error(token->pos, "expected expression after the '(' token");
1074 typeof_sym = &bad_ctype;
1076 ctx->ctype.base_type = typeof_sym;
1078 return expect(token, ')', "after typeof");
1081 static struct token *autotype_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
1083 ctx->ctype.base_type = &autotype_ctype;
1084 ctx->autotype = 1;
1085 return token;
1088 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1090 struct expression *expr = NULL;
1091 if (match_op(token, '('))
1092 token = parens_expression(token, &expr, "in attribute");
1093 return token;
1096 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1098 if (!ctx->ctype.alignment)
1099 ctx->ctype.alignment = 1;
1100 return token;
1103 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1105 int alignment = max_alignment;
1106 struct expression *expr = NULL;
1108 if (match_op(token, '(')) {
1109 token = parens_expression(token, &expr, "in attribute");
1110 if (expr)
1111 alignment = const_expression_value(expr);
1113 if (alignment & (alignment-1)) {
1114 warning(token->pos, "I don't like non-power-of-2 alignments");
1115 return token;
1116 } else if (alignment > ctx->ctype.alignment)
1117 ctx->ctype.alignment = alignment;
1118 return token;
1121 static void apply_mod(struct position *pos, unsigned long *mods, unsigned long mod)
1123 if (*mods & mod & ~MOD_DUP_OK)
1124 warning(*pos, "duplicate %s", modifier_name(mod));
1125 *mods |= mod;
1128 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1130 apply_mod(pos, &ctx->modifiers, qual);
1133 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1135 apply_mod(&token->pos, &ctx->ctype.modifiers, attr->ctype.modifiers);
1136 return token;
1139 static struct token *attribute_function(struct token *token, struct symbol *attr, struct decl_state *ctx)
1141 apply_mod(&token->pos, &ctx->f_modifiers, attr->ctype.modifiers);
1142 return token;
1145 static struct token *attribute_bitwise(struct token *token, struct symbol *attr, struct decl_state *ctx)
1147 if (Wbitwise)
1148 attribute_modifier(token, attr, ctx);
1149 return token;
1152 static struct ident *numerical_address_space(int asn)
1154 char buff[32];
1156 if (!asn)
1157 return NULL;
1158 sprintf(buff, "<asn:%d>", asn);
1159 return built_in_ident(buff);
1162 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1164 struct expression *expr = NULL;
1165 struct ident *as = NULL;
1166 struct token *next;
1168 token = expect(token, '(', "after address_space attribute");
1169 switch (token_type(token)) {
1170 case TOKEN_NUMBER:
1171 next = primary_expression(token, &expr);
1172 if (expr->type != EXPR_VALUE)
1173 goto invalid;
1174 as = numerical_address_space(expr->value);
1175 break;
1176 case TOKEN_IDENT:
1177 next = token->next;
1178 as = token->ident;
1179 break;
1180 default:
1181 next = token->next;
1182 invalid:
1183 as = NULL;
1184 warning(token->pos, "invalid address space name");
1187 if (Waddress_space && as) {
1188 if (ctx->ctype.as)
1189 sparse_error(token->pos,
1190 "multiple address spaces given: %s & %s",
1191 show_as(ctx->ctype.as), show_as(as));
1192 ctx->ctype.as = as;
1194 token = expect(next, ')', "after address_space attribute");
1195 return token;
1198 static struct symbol *to_QI_mode(struct symbol *ctype)
1200 if (ctype->ctype.base_type != &int_type)
1201 return NULL;
1202 if (ctype == &char_ctype)
1203 return ctype;
1204 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1205 : &schar_ctype;
1208 static struct symbol *to_HI_mode(struct symbol *ctype)
1210 if (ctype->ctype.base_type != &int_type)
1211 return NULL;
1212 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1213 : &sshort_ctype;
1216 static struct symbol *to_SI_mode(struct symbol *ctype)
1218 if (ctype->ctype.base_type != &int_type)
1219 return NULL;
1220 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1221 : &sint_ctype;
1224 static struct symbol *to_DI_mode(struct symbol *ctype)
1226 if (ctype->ctype.base_type != &int_type)
1227 return NULL;
1228 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1229 : &sllong_ctype;
1232 static struct symbol *to_TI_mode(struct symbol *ctype)
1234 if (ctype->ctype.base_type != &int_type)
1235 return NULL;
1236 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint128_ctype
1237 : &sint128_ctype;
1240 static struct symbol *to_pointer_mode(struct symbol *ctype)
1242 if (ctype->ctype.base_type != &int_type)
1243 return NULL;
1244 return ctype->ctype.modifiers & MOD_UNSIGNED ? uintptr_ctype
1245 : intptr_ctype;
1248 static struct symbol *to_word_mode(struct symbol *ctype)
1250 if (ctype->ctype.base_type != &int_type)
1251 return NULL;
1252 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1253 : &slong_ctype;
1256 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1258 token = expect(token, '(', "after mode attribute");
1259 if (token_type(token) == TOKEN_IDENT) {
1260 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1261 if (mode && mode->op->type & KW_MODE)
1262 ctx->mode = mode->op;
1263 else
1264 sparse_error(token->pos, "unknown mode attribute %s", show_ident(token->ident));
1265 token = token->next;
1266 } else
1267 sparse_error(token->pos, "expect attribute mode symbol\n");
1268 token = expect(token, ')', "after mode attribute");
1269 return token;
1272 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1274 struct context *context = alloc_context();
1275 struct expression *args[3];
1276 int idx = 0;
1278 token = expect(token, '(', "after context attribute");
1279 token = conditional_expression(token, &args[0]);
1280 token = expect(token, ',', "after context 1st argument");
1281 token = conditional_expression(token, &args[1]);
1282 if (match_op(token, ',')) {
1283 token = token->next;
1284 token = conditional_expression(token, &args[2]);
1285 token = expect(token, ')', "after context 3rd argument");
1286 context->context = args[0];
1287 idx++;
1288 } else {
1289 token = expect(token, ')', "after context 2nd argument");
1291 context->in = get_expression_value(args[idx++]);
1292 context->out = get_expression_value(args[idx++]);
1293 add_ptr_list(&ctx->ctype.contexts, context);
1294 return token;
1297 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1299 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1300 ctx->ctype.base_type->designated_init = 1;
1301 else
1302 warning(token->pos, "attribute designated_init applied to non-structure type");
1303 return token;
1306 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1308 if (Wtransparent_union)
1309 warning(token->pos, "attribute __transparent_union__");
1311 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_UNION)
1312 ctx->ctype.base_type->transparent_union = 1;
1313 else
1314 warning(token->pos, "attribute __transparent_union__ applied to non-union type");
1315 return token;
1318 static struct token *recover_unknown_attribute(struct token *token)
1320 struct expression *expr = NULL;
1322 if (Wunknown_attribute)
1323 warning(token->pos, "unknown attribute '%s'", show_ident(token->ident));
1324 token = token->next;
1325 if (match_op(token, '('))
1326 token = parens_expression(token, &expr, "in attribute");
1327 return token;
1330 static struct token *attribute_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
1332 token = expect(token, '(', "after attribute");
1333 token = expect(token, '(', "after attribute");
1335 while (token_type(token) == TOKEN_IDENT) {
1336 struct symbol *attr = lookup_keyword(token->ident, NS_KEYWORD);
1337 if (attr && attr->op->attribute)
1338 token = attr->op->attribute(token->next, attr, ctx);
1339 else
1340 token = recover_unknown_attribute(token);
1342 if (!match_op(token, ','))
1343 break;
1344 token = token->next;
1347 token = expect(token, ')', "after attribute");
1348 token = expect(token, ')', "after attribute");
1349 return token;
1352 static unsigned long decl_modifiers(struct decl_state *ctx)
1354 unsigned long mods = ctx->ctype.modifiers & MOD_DECLARE;
1355 ctx->ctype.modifiers &= ~MOD_DECLARE;
1356 return ctx->storage_class | mods;
1359 static struct token *storage_specifier(struct token *next, struct symbol *sym, struct decl_state *ctx)
1361 int is_tls = ctx->ctype.modifiers & MOD_TLS;
1362 unsigned long class = sym->ctype.modifiers;
1363 const char *storage = modifier_name(class);
1365 /* __thread can be used alone, or with extern or static */
1366 if (is_tls && (class & ~(MOD_STATIC|MOD_EXTERN)))
1367 sparse_error(next->pos, "__thread cannot be used with '%s'", storage);
1368 else if (!ctx->storage_class)
1369 ctx->storage_class = class;
1370 else if (ctx->storage_class == class)
1371 sparse_error(next->pos, "duplicate %s", storage);
1372 else
1373 sparse_error(next->pos, "multiple storage classes");
1374 return next;
1377 static struct token *thread_specifier(struct token *next, struct symbol *sym, struct decl_state *ctx)
1379 /* This GCC extension can be used alone, or with extern or static */
1380 if (!(ctx->storage_class & ~(MOD_STATIC|MOD_EXTERN))) {
1381 apply_qualifier(&next->pos, &ctx->ctype, MOD_TLS);
1382 } else {
1383 sparse_error(next->pos, "__thread cannot be used with '%s'",
1384 modifier_name(ctx->storage_class));
1387 return next;
1390 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1392 ctx->forced = 1;
1393 return token;
1396 static struct token *alignas_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
1398 int alignment = 0;
1400 if (!match_op(token, '(')) {
1401 sparse_error(token->pos, "expected '(' after _Alignas");
1402 return token;
1404 if (lookup_type(token->next)) {
1405 struct symbol *sym = NULL;
1406 token = typename(token->next, &sym, NULL);
1407 sym = examine_symbol_type(sym);
1408 alignment = sym->ctype.alignment;
1409 token = expect(token, ')', "after _Alignas(...");
1410 } else {
1411 struct expression *expr = NULL;
1412 token = parens_expression(token, &expr, "after _Alignas");
1413 if (!expr)
1414 return token;
1415 alignment = const_expression_value(expr);
1418 if (alignment < 0) {
1419 warning(token->pos, "non-positive alignment");
1420 return token;
1422 if (alignment & (alignment-1)) {
1423 warning(token->pos, "non-power-of-2 alignment");
1424 return token;
1426 if (alignment > ctx->ctype.alignment)
1427 ctx->ctype.alignment = alignment;
1428 return token;
1431 static struct token *generic_qualifier(struct token *next, struct symbol *sym, struct decl_state *ctx)
1433 apply_qualifier(&next->pos, &ctx->ctype, sym->ctype.modifiers);
1434 return next;
1437 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1439 unsigned long mod = thistype->modifiers;
1441 if (mod)
1442 apply_qualifier(&pos, ctype, mod);
1444 /* Context */
1445 concat_ptr_list((struct ptr_list *)thistype->contexts,
1446 (struct ptr_list **)&ctype->contexts);
1448 /* Alignment */
1449 if (thistype->alignment > ctype->alignment)
1450 ctype->alignment = thistype->alignment;
1452 /* Address space */
1453 if (thistype->as)
1454 ctype->as = thistype->as;
1457 static void specifier_conflict(struct position pos, int what, struct ident *new)
1459 const char *old;
1460 if (what & (Set_S | Set_T))
1461 goto Catch_all;
1462 if (what & Set_Char)
1463 old = "char";
1464 else if (what & Set_Double)
1465 old = "double";
1466 else if (what & Set_Float)
1467 old = "float";
1468 else if (what & Set_Signed)
1469 old = "signed";
1470 else if (what & Set_Unsigned)
1471 old = "unsigned";
1472 else if (what & Set_Short)
1473 old = "short";
1474 else if (what & Set_Long)
1475 old = "long";
1476 else
1477 old = "long long";
1478 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1479 old, show_ident(new));
1480 return;
1482 Catch_all:
1483 sparse_error(pos, "two or more data types in declaration specifiers");
1486 static struct symbol * const int_types[] =
1487 {&char_ctype, &short_ctype, &int_ctype, &long_ctype, &llong_ctype, &int128_ctype};
1488 static struct symbol * const signed_types[] =
1489 {&schar_ctype, &sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1490 &sint128_ctype};
1491 static struct symbol * const unsigned_types[] =
1492 {&uchar_ctype, &ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1493 &uint128_ctype};
1494 static struct symbol * const real_types[] =
1495 {&float_ctype, &double_ctype, &ldouble_ctype};
1496 static struct symbol * const * const types[] = {
1497 [CInt] = int_types + 2,
1498 [CSInt] = signed_types + 2,
1499 [CUInt] = unsigned_types + 2,
1500 [CReal] = real_types + 1,
1503 struct symbol *ctype_integer(int size, int want_unsigned)
1505 return types[want_unsigned ? CUInt : CInt][size];
1508 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1510 while (token_type(t) == TOKEN_IDENT) {
1511 struct symbol *s = lookup_keyword(t->ident, NS_TYPEDEF);
1512 if (!s)
1513 break;
1514 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1515 break;
1516 t = t->next;
1517 if (s->op->declarator)
1518 t = s->op->declarator(t, s, ctx);
1520 return t;
1523 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1525 int seen = 0;
1526 int class = CInt;
1527 int rank = 0;
1529 while (token_type(token) == TOKEN_IDENT) {
1530 struct symbol *s = lookup_symbol(token->ident,
1531 NS_TYPEDEF | NS_SYMBOL);
1532 if (!s || !(s->namespace & NS_TYPEDEF))
1533 break;
1534 if (s->type != SYM_KEYWORD) {
1535 if (seen & Set_Any)
1536 break;
1537 seen |= Set_S | Set_T;
1538 ctx->ctype.base_type = s->ctype.base_type;
1539 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1540 token = token->next;
1541 continue;
1543 if (s->op->type & KW_SPECIFIER) {
1544 if (seen & s->op->test) {
1545 specifier_conflict(token->pos,
1546 seen & s->op->test,
1547 token->ident);
1548 break;
1550 seen |= s->op->set;
1551 class += s->op->class;
1552 if (s->op->set & Set_Int128)
1553 rank = 3;
1554 else if (s->op->set & Set_Char)
1555 rank = -2;
1556 if (s->op->set & (Set_Short|Set_Float)) {
1557 rank = -1;
1558 } else if (s->op->set & Set_Long && rank++) {
1559 if (class == CReal) {
1560 specifier_conflict(token->pos,
1561 Set_Vlong,
1562 &double_ident);
1563 break;
1565 seen |= Set_Vlong;
1568 token = token->next;
1569 if (s->op->declarator) // Note: this eats attributes
1570 token = s->op->declarator(token, s, ctx);
1571 if (s->op->type & KW_EXACT) {
1572 ctx->ctype.base_type = s->ctype.base_type;
1573 ctx->ctype.modifiers |= s->ctype.modifiers;
1577 if (!(seen & Set_S)) { /* not set explicitly? */
1578 struct symbol *base = &incomplete_ctype;
1579 if (seen & Set_Any)
1580 base = types[class][rank];
1581 ctx->ctype.base_type = base;
1584 if (ctx->ctype.modifiers & MOD_BITWISE) {
1585 struct symbol *type;
1586 ctx->ctype.modifiers &= ~MOD_BITWISE;
1587 if (!is_int_type(ctx->ctype.base_type)) {
1588 sparse_error(token->pos, "invalid modifier");
1589 return token;
1591 type = alloc_symbol(token->pos, SYM_BASETYPE);
1592 *type = *ctx->ctype.base_type;
1593 type->ctype.modifiers &= ~MOD_SPECIFIER;
1594 type->ctype.base_type = ctx->ctype.base_type;
1595 type->type = SYM_RESTRICT;
1596 ctx->ctype.base_type = type;
1597 create_fouled(type);
1599 return token;
1602 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1604 struct expression *expr = NULL;
1605 int has_static = 0;
1607 while (token_type(token) == TOKEN_IDENT) {
1608 struct symbol *sym = lookup_keyword(token->ident, NS_TYPEDEF);
1609 if (!sym || !(sym->op->type & (KW_STATIC|KW_QUALIFIER)))
1610 break;
1611 if (has_static && (sym->op->type & KW_STATIC))
1612 sparse_error(token->pos, "duplicate array static declarator");
1613 has_static |= (sym->op->type & KW_STATIC);
1614 token = token->next;
1616 if (match_op(token, '*') && match_op(token->next, ']')) {
1617 // FIXME: '[*]' is treated like '[]'
1618 token = token->next;
1619 } else {
1620 token = assignment_expression(token, &expr);
1622 sym->array_size = expr;
1623 return token;
1626 static struct token *parameter_type_list(struct token *, struct symbol *);
1627 static struct token *identifier_list(struct token *, struct symbol *);
1628 static struct token *declarator(struct token *token, struct decl_state *ctx);
1630 static struct token *handle_asm_name(struct token *token, struct decl_state *ctx)
1632 struct expression *expr;
1633 struct symbol *keyword;
1635 if (token_type(token) != TOKEN_IDENT)
1636 return token;
1637 keyword = lookup_keyword(token->ident, NS_KEYWORD);
1638 if (!keyword)
1639 return token;
1640 if (!(keyword->op->type & KW_ASM))
1641 return token;
1643 token = token->next;
1644 token = expect(token, '(', "after asm");
1645 token = string_expression(token, &expr, "asm name");
1646 token = expect(token, ')', "after asm");
1647 return token;
1651 // test if @token is '__attribute__' (or one of its variant)
1652 static bool match_attribute(struct token *token)
1654 struct symbol *sym;
1656 if (token_type(token) != TOKEN_IDENT)
1657 return false;
1658 sym = lookup_keyword(token->ident, NS_TYPEDEF);
1659 if (!sym)
1660 return false;
1661 return sym->op->type & KW_ATTRIBUTE;
1664 static struct token *skip_attribute(struct token *token)
1666 token = token->next;
1667 if (match_op(token, '(')) {
1668 int depth = 1;
1669 token = token->next;
1670 while (depth && !eof_token(token)) {
1671 if (token_type(token) == TOKEN_SPECIAL) {
1672 if (token->special == '(')
1673 depth++;
1674 else if (token->special == ')')
1675 depth--;
1677 token = token->next;
1680 return token;
1683 static struct token *skip_attributes(struct token *token)
1685 while (match_attribute(token)) {
1686 token = expect(token->next, '(', "after attribute");
1687 token = expect(token, '(', "after attribute");
1688 while (token_type(token) == TOKEN_IDENT) {
1689 token = skip_attribute(token);
1690 if (!match_op(token, ','))
1691 break;
1692 token = token->next;
1694 token = expect(token, ')', "after attribute");
1695 token = expect(token, ')', "after attribute");
1697 return token;
1700 static struct token *handle_attributes(struct token *token, struct decl_state *ctx)
1702 while (match_attribute(token))
1703 token = attribute_specifier(token->next, NULL, ctx);
1704 return token;
1707 static int is_nested(struct token *token, struct token **p,
1708 int prefer_abstract)
1711 * This can be either a parameter list or a grouping.
1712 * For the direct (non-abstract) case, we know if must be
1713 * a parameter list if we already saw the identifier.
1714 * For the abstract case, we know if must be a parameter
1715 * list if it is empty or starts with a type.
1717 struct token *next = token->next;
1719 *p = next = skip_attributes(next);
1721 if (token_type(next) == TOKEN_IDENT) {
1722 if (lookup_type(next))
1723 return !prefer_abstract;
1724 return 1;
1727 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1728 return 0;
1730 return 1;
1733 enum kind {
1734 Empty, K_R, Proto, Bad_Func,
1737 static enum kind which_func(struct token *token,
1738 struct ident **n,
1739 int prefer_abstract)
1741 struct token *next = token->next;
1743 if (token_type(next) == TOKEN_IDENT) {
1744 if (lookup_type(next))
1745 return Proto;
1746 /* identifier list not in definition; complain */
1747 if (prefer_abstract)
1748 warning(token->pos,
1749 "identifier list not in definition");
1750 return K_R;
1753 if (token_type(next) != TOKEN_SPECIAL)
1754 return Bad_Func;
1756 if (next->special == ')') {
1757 /* don't complain about those */
1758 if (!n || match_op(next->next, ';') || match_op(next->next, ','))
1759 return Empty;
1760 if (Wstrict_prototypes)
1761 warning(next->pos,
1762 "non-ANSI function declaration of function '%s'",
1763 show_ident(*n));
1764 return Empty;
1767 if (next->special == SPECIAL_ELLIPSIS) {
1768 warning(next->pos,
1769 "variadic functions must have one named argument");
1770 return Proto;
1773 return Bad_Func;
1776 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1778 struct ctype *ctype = &ctx->ctype;
1779 struct token *next;
1780 struct ident **p = ctx->ident;
1782 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1783 *ctx->ident = token->ident;
1784 token = token->next;
1785 } else if (match_op(token, '(') &&
1786 is_nested(token, &next, ctx->prefer_abstract)) {
1787 struct symbol *base_type = ctype->base_type;
1788 if (token->next != next)
1789 next = handle_attributes(token->next, ctx);
1790 token = declarator(next, ctx);
1791 token = expect(token, ')', "in nested declarator");
1792 while (ctype->base_type != base_type)
1793 ctype = &ctype->base_type->ctype;
1794 p = NULL;
1797 if (match_op(token, '(')) {
1798 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1799 struct symbol *fn;
1800 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1801 ctype->modifiers |= ctx->f_modifiers;
1802 token = token->next;
1803 if (kind == K_R)
1804 token = identifier_list(token, fn);
1805 else if (kind == Proto)
1806 token = parameter_type_list(token, fn);
1807 token = expect(token, ')', "in function declarator");
1808 fn->endpos = token->pos;
1809 return token;
1812 while (match_op(token, '[')) {
1813 struct symbol *array;
1814 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1815 token = abstract_array_declarator(token->next, array);
1816 token = expect(token, ']', "in abstract_array_declarator");
1817 array->endpos = token->pos;
1818 ctype = &array->ctype;
1820 return token;
1823 static struct token *pointer(struct token *token, struct decl_state *ctx)
1825 while (match_op(token,'*')) {
1826 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1827 ptr->ctype.modifiers = ctx->ctype.modifiers;
1828 ptr->ctype.base_type = ctx->ctype.base_type;
1829 ptr->ctype.as = ctx->ctype.as;
1830 ptr->ctype.contexts = ctx->ctype.contexts;
1831 ctx->ctype.modifiers = 0;
1832 ctx->ctype.base_type = ptr;
1833 ctx->ctype.as = NULL;
1834 ctx->ctype.contexts = NULL;
1835 ctx->ctype.alignment = 0;
1837 token = handle_qualifiers(token->next, ctx);
1838 ctx->ctype.base_type->endpos = token->pos;
1840 return token;
1843 static struct token *declarator(struct token *token, struct decl_state *ctx)
1845 token = pointer(token, ctx);
1846 return direct_declarator(token, ctx);
1849 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1851 struct ctype *ctype = &ctx->ctype;
1852 struct expression *expr;
1853 struct symbol *bitfield;
1854 long long width;
1856 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1857 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1858 show_typename(ctype->base_type));
1859 // Parse this to recover gracefully.
1860 return conditional_expression(token->next, &expr);
1863 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1864 token = conditional_expression(token->next, &expr);
1865 width = const_expression_value(expr);
1866 bitfield->bit_size = width;
1868 if (width < 0 || width > INT_MAX || (*ctx->ident && width == 0)) {
1869 sparse_error(token->pos, "bitfield '%s' has invalid width (%lld)",
1870 show_ident(*ctx->ident), width);
1871 width = -1;
1872 } else if (*ctx->ident) {
1873 struct symbol *base_type = bitfield->ctype.base_type;
1874 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1875 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1876 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1877 // Valid values are either {-1;0} or {0}, depending on integer
1878 // representation. The latter makes for very efficient code...
1879 sparse_error(token->pos, "dubious one-bit signed bitfield");
1881 if (Wdefault_bitfield_sign &&
1882 bitfield_type->type != SYM_ENUM &&
1883 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1884 is_signed) {
1885 // The sign of bitfields is unspecified by default.
1886 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1889 bitfield->bit_size = width;
1890 bitfield->endpos = token->pos;
1891 bitfield->ident = *ctx->ident;
1892 return token;
1895 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1897 struct decl_state ctx = {.prefer_abstract = 0};
1898 struct ctype saved;
1899 unsigned long mod;
1901 token = declaration_specifiers(token, &ctx);
1902 mod = decl_modifiers(&ctx);
1903 saved = ctx.ctype;
1904 for (;;) {
1905 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1906 ctx.ident = &decl->ident;
1908 token = declarator(token, &ctx);
1909 if (match_op(token, ':'))
1910 token = handle_bitfield(token, &ctx);
1912 token = handle_attributes(token, &ctx);
1913 apply_modifiers(token->pos, &ctx);
1915 decl->ctype = ctx.ctype;
1916 decl->ctype.modifiers |= mod;
1917 decl->endpos = token->pos;
1918 add_symbol(list, decl);
1919 if (!match_op(token, ','))
1920 break;
1921 token = token->next;
1922 ctx.ctype = saved;
1924 return token;
1927 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1929 while (!match_op(token, '}')) {
1930 if (match_ident(token, &_Static_assert_ident)) {
1931 token = parse_static_assert(token, NULL);
1932 continue;
1934 if (!match_op(token, ';'))
1935 token = declaration_list(token, list);
1936 if (!match_op(token, ';')) {
1937 sparse_error(token->pos, "expected ; at end of declaration");
1938 break;
1940 token = token->next;
1942 return token;
1945 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1947 struct decl_state ctx = {.prefer_abstract = 1};
1949 token = declaration_specifiers(token, &ctx);
1950 ctx.ident = &sym->ident;
1951 token = declarator(token, &ctx);
1952 token = handle_attributes(token, &ctx);
1953 apply_modifiers(token->pos, &ctx);
1954 sym->ctype = ctx.ctype;
1955 sym->ctype.modifiers |= decl_modifiers(&ctx);
1956 sym->endpos = token->pos;
1957 sym->forced_arg = ctx.forced;
1958 return token;
1961 struct token *typename(struct token *token, struct symbol **p, int *forced)
1963 struct decl_state ctx = {.prefer_abstract = 1};
1964 unsigned long class;
1965 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1966 *p = sym;
1967 token = declaration_specifiers(token, &ctx);
1968 token = declarator(token, &ctx);
1969 apply_modifiers(token->pos, &ctx);
1970 sym->ctype = ctx.ctype;
1971 sym->endpos = token->pos;
1972 class = ctx.storage_class;
1973 if (forced)
1974 *forced = ctx.forced;
1975 if (class)
1976 warning(sym->pos, "storage class in typename (%s%s)",
1977 modifier_string(class), show_typename(sym));
1978 return token;
1981 static struct token *parse_underscore_Pragma(struct token *token)
1983 struct token *next;
1985 next = token->next;
1986 if (!match_op(next, '('))
1987 return next;
1988 next = next->next;
1989 if (next->pos.type != TOKEN_STRING)
1990 return next;
1991 next = next->next;
1992 if (!match_op(next, ')'))
1993 return next;
1994 return next->next;
1997 static struct token *expression_statement(struct token *token, struct expression **tree)
1999 if (match_ident(token, &_Pragma_ident))
2000 return parse_underscore_Pragma(token);
2002 token = parse_expression(token, tree);
2003 return expect(token, ';', "at end of statement");
2006 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
2007 struct asm_operand_list **inout)
2009 /* Allow empty operands */
2010 if (match_op(token->next, ':') || match_op(token->next, ')'))
2011 return token->next;
2012 do {
2013 struct asm_operand *op = __alloc_asm_operand(0);
2014 if (match_op(token->next, '[') &&
2015 token_type(token->next->next) == TOKEN_IDENT &&
2016 match_op(token->next->next->next, ']')) {
2017 op->name = token->next->next->ident;
2018 token = token->next->next->next;
2020 token = token->next;
2021 token = string_expression(token, &op->constraint, "asm constraint");
2022 token = parens_expression(token, &op->expr, "in asm parameter");
2023 add_ptr_list(inout, op);
2024 } while (match_op(token, ','));
2025 return token;
2028 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
2029 struct expression_list **clobbers)
2031 struct expression *expr;
2033 do {
2034 token = primary_expression(token->next, &expr);
2035 if (expr)
2036 add_expression(clobbers, expr);
2037 } while (match_op(token, ','));
2038 return token;
2041 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
2042 struct symbol_list **labels)
2044 struct symbol *label;
2046 do {
2047 token = token->next; /* skip ':' and ',' */
2048 if (token_type(token) != TOKEN_IDENT)
2049 return token;
2050 label = label_symbol(token, 1);
2051 add_symbol(labels, label);
2052 token = token->next;
2053 } while (match_op(token, ','));
2054 return token;
2057 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
2059 unsigned long mods = 0;
2061 token = token->next;
2062 stmt->type = STMT_ASM;
2063 while (token_type(token) == TOKEN_IDENT) {
2064 struct symbol *s = lookup_keyword(token->ident, NS_TYPEDEF);
2065 if (s && s->op->asm_modifier)
2066 s->op->asm_modifier(token, &mods, s->ctype.modifiers);
2067 else if (token->ident == &goto_ident)
2068 asm_modifier(token, &mods, MOD_ASM_GOTO);
2069 token = token->next;
2071 token = expect(token, '(', "after asm");
2072 token = string_expression(token, &stmt->asm_string, "inline asm");
2073 if (match_op(token, ':'))
2074 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
2075 if (match_op(token, ':'))
2076 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
2077 if (match_op(token, ':'))
2078 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
2079 if (match_op(token, ':') && (mods & MOD_ASM_GOTO))
2080 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
2081 token = expect(token, ')', "after asm");
2082 return expect(token, ';', "at end of asm-statement");
2085 static struct token *parse_static_assert(struct token *token, struct symbol_list **unused)
2087 struct expression *cond = NULL, *message = NULL;
2089 token = expect(token->next, '(', "after _Static_assert");
2090 token = constant_expression(token, &cond);
2091 if (!cond)
2092 sparse_error(token->pos, "Expected constant expression");
2093 if (match_op(token, ',')) {
2094 token = token->next;
2095 token = string_expression(token, &message, "_Static_assert()");
2096 if (!message)
2097 cond = NULL;
2099 token = expect(token, ')', "after diagnostic message in _Static_assert");
2100 token = expect(token, ';', "after _Static_assert()");
2102 if (cond && !const_expression_value(cond) && cond->type == EXPR_VALUE) {
2103 const char *sep = "", *msg = "";
2105 if (message) {
2106 sep = ": ";
2107 msg = show_string(message->string);
2109 sparse_error(cond->pos, "static assertion failed%s%s", sep, msg);
2112 return token;
2115 /* Make a statement out of an expression */
2116 static struct statement *make_statement(struct expression *expr)
2118 struct statement *stmt;
2120 if (!expr)
2121 return NULL;
2122 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
2123 stmt->expression = expr;
2124 return stmt;
2128 * All iterators have two symbols associated with them:
2129 * the "continue" and "break" symbols, which are targets
2130 * for continue and break statements respectively.
2132 * They are in a special name-space, but they follow
2133 * all the normal visibility rules, so nested iterators
2134 * automatically work right.
2136 static void start_iterator(struct statement *stmt)
2138 struct symbol *cont, *brk;
2140 start_block_scope(stmt->pos);
2141 cont = alloc_symbol(stmt->pos, SYM_NODE);
2142 bind_symbol(cont, &continue_ident, NS_ITERATOR);
2143 brk = alloc_symbol(stmt->pos, SYM_NODE);
2144 bind_symbol(brk, &break_ident, NS_ITERATOR);
2146 stmt->type = STMT_ITERATOR;
2147 stmt->iterator_break = brk;
2148 stmt->iterator_continue = cont;
2149 fn_local_symbol(brk);
2150 fn_local_symbol(cont);
2153 static void end_iterator(struct statement *stmt)
2155 end_block_scope();
2158 static struct statement *start_function(struct symbol *sym)
2160 struct symbol *ret;
2161 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2163 start_function_scope(sym->pos);
2164 ret = alloc_symbol(sym->pos, SYM_NODE);
2165 ret->ctype = sym->ctype.base_type->ctype;
2166 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_QUALIFIER | MOD_TLS | MOD_ACCESS | MOD_NOCAST | MOD_NODEREF);
2167 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2168 bind_symbol(ret, &return_ident, NS_ITERATOR);
2169 stmt->ret = ret;
2170 fn_local_symbol(ret);
2172 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2173 current_fn = sym;
2175 return stmt;
2178 static void end_function(struct symbol *sym)
2180 current_fn = NULL;
2181 end_function_scope();
2185 * A "switch()" statement, like an iterator, has a
2186 * the "break" symbol associated with it. It works
2187 * exactly like the iterator break - it's the target
2188 * for any break-statements in scope, and means that
2189 * "break" handling doesn't even need to know whether
2190 * it's breaking out of an iterator or a switch.
2192 * In addition, the "case" symbol is a marker for the
2193 * case/default statements to find the switch statement
2194 * that they are associated with.
2196 static void start_switch(struct statement *stmt)
2198 struct symbol *brk, *switch_case;
2200 start_block_scope(stmt->pos);
2201 brk = alloc_symbol(stmt->pos, SYM_NODE);
2202 bind_symbol(brk, &break_ident, NS_ITERATOR);
2204 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2205 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2206 switch_case->stmt = stmt;
2208 stmt->type = STMT_SWITCH;
2209 stmt->switch_break = brk;
2210 stmt->switch_case = switch_case;
2212 fn_local_symbol(brk);
2213 fn_local_symbol(switch_case);
2216 static void end_switch(struct statement *stmt)
2218 if (!stmt->switch_case->symbol_list)
2219 warning(stmt->pos, "switch with no cases");
2220 end_block_scope();
2223 static void add_case_statement(struct statement *stmt)
2225 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2226 struct symbol *sym;
2228 if (!target) {
2229 sparse_error(stmt->pos, "not in switch scope");
2230 stmt->type = STMT_NONE;
2231 return;
2233 sym = alloc_symbol(stmt->pos, SYM_NODE);
2234 add_symbol(&target->symbol_list, sym);
2235 sym->stmt = stmt;
2236 stmt->case_label = sym;
2237 fn_local_symbol(sym);
2240 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2242 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2244 if (!target)
2245 error_die(token->pos, "internal error: return without a function target");
2246 stmt->type = STMT_RETURN;
2247 stmt->ret_target = target;
2248 return expression_statement(token->next, &stmt->ret_value);
2251 static void validate_for_loop_decl(struct symbol *sym)
2253 unsigned long storage = sym->ctype.modifiers & MOD_STORAGE;
2255 if (storage & ~(MOD_AUTO | MOD_REGISTER)) {
2256 const char *name = show_ident(sym->ident);
2257 sparse_error(sym->pos, "non-local var '%s' in for-loop initializer", name);
2258 sym->ctype.modifiers &= ~MOD_STORAGE;
2262 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2264 struct symbol_list *syms;
2265 struct expression *e1, *e2, *e3;
2266 struct statement *iterator;
2268 start_iterator(stmt);
2269 token = expect(token->next, '(', "after 'for'");
2271 syms = NULL;
2272 e1 = NULL;
2273 /* C99 variable declaration? */
2274 if (lookup_type(token)) {
2275 token = external_declaration(token, &syms, validate_for_loop_decl);
2276 } else {
2277 token = parse_expression(token, &e1);
2278 token = expect(token, ';', "in 'for'");
2280 token = parse_expression(token, &e2);
2281 token = expect(token, ';', "in 'for'");
2282 token = parse_expression(token, &e3);
2283 token = expect(token, ')', "in 'for'");
2284 token = statement(token, &iterator);
2286 stmt->iterator_syms = syms;
2287 stmt->iterator_pre_statement = make_statement(e1);
2288 stmt->iterator_pre_condition = e2;
2289 stmt->iterator_post_statement = make_statement(e3);
2290 stmt->iterator_post_condition = NULL;
2291 stmt->iterator_statement = iterator;
2292 end_iterator(stmt);
2294 return token;
2297 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2299 struct expression *expr;
2300 struct statement *iterator;
2302 start_iterator(stmt);
2303 token = parens_expression(token->next, &expr, "after 'while'");
2304 token = statement(token, &iterator);
2306 stmt->iterator_pre_condition = expr;
2307 stmt->iterator_post_condition = NULL;
2308 stmt->iterator_statement = iterator;
2309 end_iterator(stmt);
2311 return token;
2314 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2316 struct expression *expr;
2317 struct statement *iterator;
2319 start_iterator(stmt);
2320 token = statement(token->next, &iterator);
2321 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2322 token = token->next;
2323 else
2324 sparse_error(token->pos, "expected 'while' after 'do'");
2325 token = parens_expression(token, &expr, "after 'do-while'");
2327 stmt->iterator_post_condition = expr;
2328 stmt->iterator_statement = iterator;
2329 end_iterator(stmt);
2331 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2332 warning(iterator->pos, "do-while statement is not a compound statement");
2334 return expect(token, ';', "after statement");
2337 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2339 stmt->type = STMT_IF;
2340 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2341 token = statement(token, &stmt->if_true);
2342 if (token_type(token) != TOKEN_IDENT)
2343 return token;
2344 if (token->ident != &else_ident)
2345 return token;
2346 return statement(token->next, &stmt->if_false);
2349 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2351 stmt->type = STMT_CASE;
2352 token = expect(token, ':', "after default/case");
2353 add_case_statement(stmt);
2354 return statement(token, &stmt->case_statement);
2357 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2359 token = parse_expression(token->next, &stmt->case_expression);
2360 if (match_op(token, SPECIAL_ELLIPSIS))
2361 token = parse_expression(token->next, &stmt->case_to);
2362 return case_statement(token, stmt);
2365 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2367 return case_statement(token->next, stmt);
2370 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2372 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2373 stmt->type = STMT_GOTO;
2374 stmt->goto_label = target;
2375 if (!target)
2376 sparse_error(stmt->pos, "break/continue not in iterator scope");
2377 return expect(token->next, ';', "at end of statement");
2380 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2382 stmt->type = STMT_SWITCH;
2383 start_switch(stmt);
2384 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2385 token = statement(token, &stmt->switch_statement);
2386 end_switch(stmt);
2387 return token;
2390 static void warn_label_usage(struct position def, struct position use, struct ident *ident)
2392 const char *id = show_ident(ident);
2393 sparse_error(use, "label '%s' used outside statement expression", id);
2394 info(def, " label '%s' defined here", id);
2395 current_fn->bogus_linear = 1;
2398 void check_label_usage(struct symbol *label, struct position use_pos)
2400 struct statement *def = label->stmt;
2402 if (def) {
2403 if (!is_in_scope(def->label_scope, label_scope))
2404 warn_label_usage(def->pos, use_pos, label->ident);
2405 } else if (!label->label_scope) {
2406 label->label_scope = label_scope;
2407 label->label_pos = use_pos;
2411 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2413 stmt->type = STMT_GOTO;
2414 token = token->next;
2415 if (match_op(token, '*')) {
2416 token = parse_expression(token->next, &stmt->goto_expression);
2417 add_statement(&function_computed_goto_list, stmt);
2418 } else if (token_type(token) == TOKEN_IDENT) {
2419 struct symbol *label = label_symbol(token, 1);
2420 stmt->goto_label = label;
2421 check_label_usage(label, stmt->pos);
2422 token = token->next;
2423 } else {
2424 sparse_error(token->pos, "Expected identifier or goto expression");
2426 return expect(token, ';', "at end of statement");
2429 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2431 stmt->type = STMT_CONTEXT;
2432 token = token->next;
2433 token = expect(token, '(', "after __context__ statement");
2434 token = assignment_expression(token, &stmt->expression);
2435 if (!stmt->expression)
2436 unexpected(token, "expression expected after '('");
2437 if (match_op(token, ',')) {
2438 token = token->next;
2439 stmt->context = stmt->expression;
2440 token = assignment_expression(token, &stmt->expression);
2441 if (!stmt->expression)
2442 unexpected(token, "expression expected after ','");
2444 token = expect(token, ')', "at end of __context__ statement");
2445 return expect(token, ';', "at end of statement");
2448 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2450 stmt->type = STMT_RANGE;
2451 token = token->next;
2452 token = expect(token, '(', "after __range__ statement");
2453 token = assignment_expression(token, &stmt->range_expression);
2454 token = expect(token, ',', "after range expression");
2455 token = assignment_expression(token, &stmt->range_low);
2456 token = expect(token, ',', "after low range");
2457 token = assignment_expression(token, &stmt->range_high);
2458 token = expect(token, ')', "after range statement");
2459 return expect(token, ';', "after range statement");
2462 static struct token *handle_label_attributes(struct token *token, struct symbol *label)
2464 struct decl_state ctx = { };
2466 token = handle_attributes(token, &ctx);
2467 label->label_modifiers = ctx.ctype.modifiers;
2468 return token;
2471 static struct token *statement(struct token *token, struct statement **tree)
2473 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2475 *tree = stmt;
2476 if (token_type(token) == TOKEN_IDENT) {
2477 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2478 if (s && s->op->statement)
2479 return s->op->statement(token, stmt);
2481 if (match_op(token->next, ':')) {
2482 struct symbol *s = label_symbol(token, 0);
2483 token = handle_label_attributes(token->next->next, s);
2484 if (s->stmt) {
2485 sparse_error(stmt->pos, "label '%s' redefined", show_ident(s->ident));
2486 // skip the label to avoid multiple definitions
2487 return statement(token, tree);
2489 stmt->type = STMT_LABEL;
2490 stmt->label_identifier = s;
2491 stmt->label_scope = label_scope;
2492 if (s->label_scope) {
2493 if (!is_in_scope(label_scope, s->label_scope))
2494 warn_label_usage(stmt->pos, s->label_pos, s->ident);
2496 s->stmt = stmt;
2497 return statement(token, &stmt->label_statement);
2501 if (match_op(token, '{')) {
2502 token = compound_statement(token->next, stmt);
2503 return expect(token, '}', "at end of compound statement");
2506 stmt->type = STMT_EXPRESSION;
2507 return expression_statement(token, &stmt->expression);
2510 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2511 static struct token *label_statement(struct token *token)
2513 while (token_type(token) == TOKEN_IDENT) {
2514 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2515 /* it's block-scope, but we want label namespace */
2516 bind_symbol_with_scope(sym, token->ident, NS_LABEL, block_scope);
2517 fn_local_symbol(sym);
2518 token = token->next;
2519 if (!match_op(token, ','))
2520 break;
2521 token = token->next;
2523 return expect(token, ';', "at end of label declaration");
2526 static struct token * statement_list(struct token *token, struct statement_list **list)
2528 int seen_statement = 0;
2529 while (token_type(token) == TOKEN_IDENT &&
2530 token->ident == &__label___ident)
2531 token = label_statement(token->next);
2532 for (;;) {
2533 struct statement * stmt;
2534 if (eof_token(token))
2535 break;
2536 if (match_op(token, '}'))
2537 break;
2538 if (match_ident(token, &_Static_assert_ident)) {
2539 token = parse_static_assert(token, NULL);
2540 continue;
2542 if (lookup_type(token)) {
2543 if (seen_statement) {
2544 warning(token->pos, "mixing declarations and code");
2545 seen_statement = 0;
2547 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2548 token = external_declaration(token, &stmt->declaration, NULL);
2549 } else {
2550 seen_statement = Wdeclarationafterstatement;
2551 token = statement(token, &stmt);
2553 add_statement(list, stmt);
2555 return token;
2558 static struct token *identifier_list(struct token *token, struct symbol *fn)
2560 struct symbol_list **list = &fn->arguments;
2561 for (;;) {
2562 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2563 sym->ident = token->ident;
2564 token = token->next;
2565 sym->endpos = token->pos;
2566 sym->ctype.base_type = &incomplete_ctype;
2567 add_symbol(list, sym);
2568 if (!match_op(token, ',') ||
2569 token_type(token->next) != TOKEN_IDENT ||
2570 lookup_type(token->next))
2571 break;
2572 token = token->next;
2574 return token;
2577 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2579 struct symbol_list **list = &fn->arguments;
2581 for (;;) {
2582 struct symbol *sym;
2584 if (match_op(token, SPECIAL_ELLIPSIS)) {
2585 fn->variadic = 1;
2586 token = token->next;
2587 break;
2590 sym = alloc_symbol(token->pos, SYM_NODE);
2591 token = parameter_declaration(token, sym);
2592 if (sym->ctype.base_type == &void_ctype) {
2593 /* Special case: (void) */
2594 if (!*list && !sym->ident)
2595 break;
2596 warning(token->pos, "void parameter");
2598 add_symbol(list, sym);
2599 if (!match_op(token, ','))
2600 break;
2601 token = token->next;
2603 return token;
2606 struct token *compound_statement(struct token *token, struct statement *stmt)
2608 stmt->type = STMT_COMPOUND;
2609 start_block_scope(token->pos);
2610 token = statement_list(token, &stmt->stmts);
2611 end_block_scope();
2612 return token;
2615 static struct expression *identifier_expression(struct token *token)
2617 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2618 expr->expr_ident = token->ident;
2619 return expr;
2622 static struct expression *index_expression(struct expression *from, struct expression *to)
2624 int idx_from, idx_to;
2625 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2627 idx_from = const_expression_value(from);
2628 idx_to = idx_from;
2629 if (to) {
2630 idx_to = const_expression_value(to);
2631 if (idx_to < idx_from || idx_from < 0)
2632 warning(from->pos, "nonsense array initializer index range");
2634 expr->idx_from = idx_from;
2635 expr->idx_to = idx_to;
2636 return expr;
2639 static struct token *single_initializer(struct expression **ep, struct token *token)
2641 int expect_equal = 0;
2642 struct token *next = token->next;
2643 struct expression **tail = ep;
2644 int nested;
2646 *ep = NULL;
2648 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2649 struct expression *expr = identifier_expression(token);
2650 if (Wold_initializer)
2651 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2652 token = initializer(&expr->ident_expression, next->next);
2653 if (expr->ident_expression)
2654 *ep = expr;
2655 return token;
2658 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2659 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2660 struct expression *expr = identifier_expression(next);
2661 *tail = expr;
2662 tail = &expr->ident_expression;
2663 expect_equal = 1;
2664 token = next->next;
2665 } else if (match_op(token, '[')) {
2666 struct expression *from = NULL, *to = NULL, *expr;
2667 token = constant_expression(token->next, &from);
2668 if (!from) {
2669 sparse_error(token->pos, "Expected constant expression");
2670 break;
2672 if (match_op(token, SPECIAL_ELLIPSIS))
2673 token = constant_expression(token->next, &to);
2674 expr = index_expression(from, to);
2675 *tail = expr;
2676 tail = &expr->idx_expression;
2677 token = expect(token, ']', "at end of initializer index");
2678 if (nested)
2679 expect_equal = 1;
2680 } else {
2681 break;
2684 if (nested && !expect_equal) {
2685 if (!match_op(token, '='))
2686 warning(token->pos, "obsolete array initializer, use C99 syntax");
2687 else
2688 expect_equal = 1;
2690 if (expect_equal)
2691 token = expect(token, '=', "at end of initializer index");
2693 token = initializer(tail, token);
2694 if (!*tail)
2695 *ep = NULL;
2696 return token;
2699 static struct token *initializer_list(struct expression_list **list, struct token *token)
2701 struct expression *expr;
2703 for (;;) {
2704 token = single_initializer(&expr, token);
2705 if (!expr)
2706 break;
2707 add_expression(list, expr);
2708 if (!match_op(token, ','))
2709 break;
2710 token = token->next;
2712 return token;
2715 struct token *initializer(struct expression **tree, struct token *token)
2717 if (match_op(token, '{')) {
2718 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2719 *tree = expr;
2720 if (!Wuniversal_initializer) {
2721 struct token *next = token->next;
2722 // '{ 0 }' is equivalent to '{ }' except for some
2723 // warnings, like using 0 to initialize a null-pointer.
2724 if (match_token_zero(next)) {
2725 if (match_op(next->next, '}'))
2726 expr->zero_init = 1;
2730 token = initializer_list(&expr->expr_list, token->next);
2731 return expect(token, '}', "at end of initializer");
2733 return assignment_expression(token, tree);
2736 static void declare_argument(struct symbol *sym, struct symbol *fn)
2738 if (!sym->ident) {
2739 sparse_error(sym->pos, "no identifier for function argument");
2740 return;
2742 bind_symbol(sym, sym->ident, NS_SYMBOL);
2745 static int is_syscall(struct symbol *sym)
2747 char *macro;
2748 char *name;
2749 int is_syscall = 0;
2751 macro = get_macro_name(sym->pos);
2752 if (macro &&
2753 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
2754 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
2755 is_syscall = 1;
2757 name = sym->ident->name;
2759 if (name && strncmp(name, "sys_", 4) ==0)
2760 is_syscall = 1;
2762 if (name && strncmp(name, "compat_sys_", 11) == 0)
2763 is_syscall = 1;
2765 return is_syscall;
2769 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2770 struct symbol_list **list)
2772 struct symbol_list **old_symbol_list;
2773 struct symbol *base_type = decl->ctype.base_type;
2774 struct statement *stmt, **p;
2775 struct symbol *prev;
2776 struct symbol *arg;
2778 old_symbol_list = function_symbol_list;
2779 if (decl->ctype.modifiers & MOD_INLINE) {
2780 function_symbol_list = &decl->inline_symbol_list;
2781 p = &base_type->inline_stmt;
2782 } else {
2783 function_symbol_list = &decl->symbol_list;
2784 p = &base_type->stmt;
2786 function_computed_target_list = NULL;
2787 function_computed_goto_list = NULL;
2789 if ((decl->ctype.modifiers & (MOD_EXTERN|MOD_INLINE)) == MOD_EXTERN) {
2790 if (Wexternal_function_has_definition)
2791 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2793 if (!(decl->ctype.modifiers & MOD_STATIC))
2794 decl->ctype.modifiers |= MOD_EXTERN;
2796 stmt = start_function(decl);
2797 *p = stmt;
2799 FOR_EACH_PTR (base_type->arguments, arg) {
2800 declare_argument(arg, base_type);
2801 } END_FOR_EACH_PTR(arg);
2803 token = statement_list(token->next, &stmt->stmts);
2804 end_function(decl);
2806 if (!(decl->ctype.modifiers & MOD_INLINE))
2807 add_symbol(list, decl);
2808 else if (is_syscall(decl)) {
2809 add_symbol(list, decl);
2811 printf("parse.c decl: %s\n", decl->ident->name);
2812 char *macro = get_macro_name(decl->pos);
2813 printf("decl macro: %s\n", macro);
2816 check_declaration(decl);
2817 decl->definition = decl;
2818 prev = decl->same_symbol;
2819 if (prev && prev->definition) {
2820 warning(decl->pos, "multiple definitions for function '%s'",
2821 show_ident(decl->ident));
2822 info(prev->definition->pos, " the previous one is here");
2823 } else {
2824 while (prev) {
2825 rebind_scope(prev, decl->scope);
2826 prev->definition = decl;
2827 prev = prev->same_symbol;
2830 function_symbol_list = old_symbol_list;
2831 if (function_computed_goto_list) {
2832 if (!function_computed_target_list)
2833 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2834 else {
2835 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2836 stmt->target_list = function_computed_target_list;
2837 } END_FOR_EACH_PTR(stmt);
2840 return expect(token, '}', "at end of function");
2843 static void promote_k_r_types(struct symbol *arg)
2845 struct symbol *base = arg->ctype.base_type;
2846 if (base && base->ctype.base_type == &int_type && base->rank < 0) {
2847 arg->ctype.base_type = &int_ctype;
2851 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2853 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2854 struct symbol *arg;
2856 FOR_EACH_PTR(real_args, arg) {
2857 struct symbol *type;
2859 /* This is quadratic in the number of arguments. We _really_ don't care */
2860 FOR_EACH_PTR(argtypes, type) {
2861 if (type->ident == arg->ident)
2862 goto match;
2863 } END_FOR_EACH_PTR(type);
2864 if (Wimplicit_int) {
2865 sparse_error(arg->pos, "missing type declaration for parameter '%s'",
2866 show_ident(arg->ident));
2868 type = alloc_symbol(arg->pos, SYM_NODE);
2869 type->ident = arg->ident;
2870 type->ctype.base_type = &int_ctype;
2871 match:
2872 type->used = 1;
2873 /* "char" and "short" promote to "int" */
2874 promote_k_r_types(type);
2876 arg->ctype = type->ctype;
2877 } END_FOR_EACH_PTR(arg);
2879 FOR_EACH_PTR(argtypes, arg) {
2880 if (!arg->used)
2881 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2882 } END_FOR_EACH_PTR(arg);
2886 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2887 struct symbol_list **list)
2889 struct symbol_list *args = NULL;
2891 if (Wold_style_definition)
2892 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2894 do {
2895 token = declaration_list(token, &args);
2896 if (!match_op(token, ';')) {
2897 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2898 break;
2900 token = token->next;
2901 } while (lookup_type(token));
2903 apply_k_r_types(args, decl);
2905 if (!match_op(token, '{')) {
2906 sparse_error(token->pos, "expected function body");
2907 return token;
2909 return parse_function_body(token, decl, list);
2912 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2914 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2915 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2916 struct statement *stmt;
2918 anon->ctype.base_type = fn;
2919 stmt = alloc_statement(token->pos, STMT_NONE);
2920 fn->stmt = stmt;
2922 token = parse_asm_statement(token, stmt);
2924 // FIXME: add_symbol(list, anon);
2925 return token;
2928 struct token *external_declaration(struct token *token, struct symbol_list **list,
2929 validate_decl_t validate_decl)
2931 struct ident *ident = NULL;
2932 struct symbol *decl;
2933 struct decl_state ctx = { .ident = &ident };
2934 struct ctype saved;
2935 struct symbol *base_type;
2936 unsigned long mod;
2937 int is_typedef;
2939 if (match_ident(token, &_Pragma_ident))
2940 return parse_underscore_Pragma(token);
2942 /* Top-level inline asm or static assertion? */
2943 if (token_type(token) == TOKEN_IDENT) {
2944 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2945 if (s && s->op->toplevel)
2946 return s->op->toplevel(token, list);
2949 /* Parse declaration-specifiers, if any */
2950 token = declaration_specifiers(token, &ctx);
2951 mod = decl_modifiers(&ctx);
2952 decl = alloc_symbol(token->pos, SYM_NODE);
2953 /* Just a type declaration? */
2954 if (match_op(token, ';')) {
2955 apply_modifiers(token->pos, &ctx);
2956 return token->next;
2959 saved = ctx.ctype;
2960 token = declarator(token, &ctx);
2961 token = handle_asm_name(token, &ctx);
2962 token = handle_attributes(token, &ctx);
2963 apply_modifiers(token->pos, &ctx);
2965 decl->ctype = ctx.ctype;
2966 decl->ctype.modifiers |= mod;
2967 decl->endpos = token->pos;
2969 /* Just a type declaration? */
2970 if (!ident) {
2971 warning(token->pos, "missing identifier in declaration");
2972 return expect(token, ';', "at the end of type declaration");
2975 /* type define declaration? */
2976 is_typedef = ctx.storage_class == MOD_USERTYPE;
2978 /* Typedefs don't have meaningful storage */
2979 if (is_typedef)
2980 decl->ctype.modifiers |= MOD_USERTYPE;
2982 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2984 base_type = decl->ctype.base_type;
2986 if (is_typedef) {
2987 if (base_type && !base_type->ident) {
2988 switch (base_type->type) {
2989 case SYM_STRUCT:
2990 case SYM_UNION:
2991 case SYM_ENUM:
2992 case SYM_RESTRICT:
2993 base_type->ident = ident;
2994 break;
2995 default:
2996 break;
2999 } else if (base_type && base_type->type == SYM_FN) {
3000 if (base_type->ctype.base_type == &autotype_ctype) {
3001 sparse_error(decl->pos, "'%s()' has __auto_type return type",
3002 show_ident(decl->ident));
3003 base_type->ctype.base_type = &int_ctype;
3005 if (base_type->ctype.base_type == &incomplete_ctype) {
3006 warning(decl->pos, "'%s()' has implicit return type",
3007 show_ident(decl->ident));
3008 base_type->ctype.base_type = &int_ctype;
3010 /* apply attributes placed after the declarator */
3011 decl->ctype.modifiers |= ctx.f_modifiers;
3013 /* K&R argument declaration? */
3014 if (lookup_type(token))
3015 return parse_k_r_arguments(token, decl, list);
3016 if (match_op(token, '{'))
3017 return parse_function_body(token, decl, list);
3019 if (!(decl->ctype.modifiers & MOD_STATIC))
3020 decl->ctype.modifiers |= MOD_EXTERN;
3021 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
3022 sparse_error(token->pos, "void declaration");
3024 if (base_type == &incomplete_ctype) {
3025 warning(decl->pos, "'%s' has implicit type", show_ident(decl->ident));
3026 decl->ctype.base_type = &int_ctype;;
3029 for (;;) {
3030 if (!is_typedef && match_op(token, '=')) {
3031 struct token *next = token->next;
3032 token = initializer(&decl->initializer, next);
3033 if (token == next)
3034 sparse_error(token->pos, "expression expected before '%s'", show_token(token));
3036 if (!is_typedef) {
3037 if (validate_decl)
3038 validate_decl(decl);
3040 if (decl->initializer && decl->ctype.modifiers & MOD_EXTERN) {
3041 warning(decl->pos, "symbol with external linkage has initializer");
3042 decl->ctype.modifiers &= ~MOD_EXTERN;
3045 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
3046 add_symbol(list, decl);
3047 fn_local_symbol(decl);
3050 check_declaration(decl);
3051 if (decl->same_symbol) {
3052 decl->definition = decl->same_symbol->definition;
3053 decl->op = decl->same_symbol->op;
3054 if (is_typedef) {
3055 // TODO: handle -std=c89 --pedantic
3056 check_duplicates(decl);
3060 if (ctx.autotype) {
3061 const char *msg = NULL;
3062 if (decl->ctype.base_type != &autotype_ctype)
3063 msg = "on non-identifier";
3064 else if (match_op(token, ','))
3065 msg = "on declaration list";
3066 else if (!decl->initializer)
3067 msg = "without initializer";
3068 else if (decl->initializer->type == EXPR_SYMBOL &&
3069 decl->initializer->symbol == decl)
3070 msg = "on self-init var";
3071 if (msg) {
3072 sparse_error(decl->pos, "__auto_type %s", msg);
3073 decl->ctype.base_type = &bad_ctype;
3077 if (!match_op(token, ','))
3078 break;
3080 token = token->next;
3081 ident = NULL;
3082 decl = alloc_symbol(token->pos, SYM_NODE);
3083 ctx.ctype = saved;
3084 token = handle_attributes(token, &ctx);
3085 token = declarator(token, &ctx);
3086 token = handle_asm_name(token, &ctx);
3087 token = handle_attributes(token, &ctx);
3088 apply_modifiers(token->pos, &ctx);
3089 decl->ctype = ctx.ctype;
3090 decl->ctype.modifiers |= mod;
3091 decl->endpos = token->pos;
3092 if (!ident) {
3093 sparse_error(token->pos, "expected identifier name in type definition");
3094 return token;
3097 if (is_typedef)
3098 decl->ctype.modifiers |= MOD_USERTYPE;
3100 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
3102 /* Function declarations are automatically extern unless specifically static */
3103 base_type = decl->ctype.base_type;
3104 if (!is_typedef && base_type && base_type->type == SYM_FN) {
3105 if (!(decl->ctype.modifiers & MOD_STATIC))
3106 decl->ctype.modifiers |= MOD_EXTERN;
3109 return expect(token, ';', "at end of declaration");