function_hooks: update comment explaining various function hooks
[smatch.git] / parse.c
blobc6fde98417fd95fee029a5c9fbc0edd8610ccb80
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 *dst, struct ctype *src);
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 decl_state attr = { };
723 struct symbol *sym;
724 struct position *repos;
726 token = handle_attributes(token, &attr);
727 if (token_type(token) == TOKEN_IDENT) {
728 sym = lookup_symbol(token->ident, NS_STRUCT);
729 if (!sym ||
730 (is_outer_scope(sym->scope) &&
731 (match_op(token->next,';') || match_op(token->next,'{')))) {
732 // Either a new symbol, or else an out-of-scope
733 // symbol being redefined.
734 sym = alloc_symbol(token->pos, type);
735 bind_symbol(sym, token->ident, NS_STRUCT);
737 if (sym->type != type)
738 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
739 ctx->ctype.base_type = sym;
740 repos = &token->pos;
741 token = token->next;
742 if (!match_op(token, '{'))
743 return token;
745 // The following test is actually wrong for empty
746 // structs, but (1) they are not C99, (2) gcc does
747 // the same thing, and (3) it's easier.
748 if (sym->symbol_list)
749 error_die(token->pos, "redefinition of %s", show_typename (sym));
750 sym->pos = *repos;
752 // Mark the structure as needing re-examination
753 sym->examined = 0;
754 } else if (match_op(token, '{')) {
755 // private struct/union/enum type
756 sym = alloc_symbol(token->pos, type);
757 set_current_scope(sym); // used by dissect
758 ctx->ctype.base_type = sym;
759 } else {
760 sparse_error(token->pos, "expected declaration");
761 ctx->ctype.base_type = &bad_ctype;
762 return token;
765 token = parse(token->next, sym);
766 token = expect(token, '}', "at end of specifier");
767 attr.ctype.base_type = sym;
768 token = handle_attributes(token, &attr);
769 apply_ctype(token->pos, &sym->ctype, &attr.ctype);
770 sym->packed = attr.packed;
772 sym->endpos = token->pos;
774 return token;
777 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
779 struct symbol *field, *last = NULL;
780 struct token *res;
781 res = struct_declaration_list(token, &sym->symbol_list);
782 FOR_EACH_PTR(sym->symbol_list, field) {
783 if (!field->ident) {
784 struct symbol *base = field->ctype.base_type;
785 if (base && base->type == SYM_BITFIELD)
786 continue;
788 if (last)
789 last->next_subobject = field;
790 last = field;
791 } END_FOR_EACH_PTR(field);
792 return res;
795 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
797 return struct_declaration_list(token, &sym->symbol_list);
800 static struct token *struct_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
802 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
805 static struct token *union_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
807 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
811 // safe right shift
813 // This allow to use a shift amount as big (or bigger)
814 // than the width of the value to be shifted, in which case
815 // the result is, of course, 0.
816 static unsigned long long rshift(unsigned long long val, unsigned int n)
818 if (n >= (sizeof(val) * 8))
819 return 0;
820 return val >> n;
823 struct range {
824 long long neg;
825 unsigned long long pos;
828 static void update_range(struct range *range, unsigned long long uval, struct symbol *vtype)
830 long long sval = uval;
832 if (is_signed_type(vtype) && (sval < 0)) {
833 if (sval < range->neg)
834 range->neg = sval;
835 } else {
836 if (uval > range->pos)
837 range->pos = uval;
841 static int type_is_ok(struct symbol *type, struct range range)
843 int shift = type->bit_size;
844 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
846 if (!is_unsigned)
847 shift--;
848 if (rshift(range.pos, shift))
849 return 0;
850 if (range.neg == 0)
851 return 1;
852 if (is_unsigned)
853 return 0;
854 if (rshift(~range.neg, shift))
855 return 0;
856 return 1;
859 static struct range type_range(struct symbol *type)
861 struct range range;
862 unsigned int size = type->bit_size;
863 unsigned long long max;
864 long long min;
866 if (is_signed_type(type)) {
867 min = sign_bit(size);
868 max = min - 1;
869 } else {
870 min = 0;
871 max = bits_mask(size);
874 range.pos = max;
875 range.neg = min;
876 return range;
879 static int val_in_range(struct range *range, long long sval, struct symbol *vtype)
881 unsigned long long uval = sval;
883 if (is_signed_type(vtype) && (sval < 0))
884 return range->neg <= sval;
885 else
886 return uval <= range->pos;
889 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
891 struct range irange = type_range(&int_ctype);
892 struct symbol *sym;
894 FOR_EACH_PTR(list, sym) {
895 struct expression *expr = sym->initializer;
896 struct symbol *ctype;
897 long long val;
898 if (expr->type != EXPR_VALUE)
899 continue;
900 ctype = expr->ctype;
901 val = get_expression_value(expr);
902 if (is_int_type(ctype) && val_in_range(&irange, val, ctype)) {
903 expr->ctype = &int_ctype;
904 continue;
906 cast_value(expr, base_type, expr, ctype);
907 expr->ctype = base_type;
908 } END_FOR_EACH_PTR(sym);
911 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
913 unsigned long long lastval = 0;
914 struct symbol *ctype = NULL, *base_type = NULL;
915 struct range range = { };
916 int mix_bitwise = 0;
918 parent->examined = 1;
919 parent->ctype.base_type = &int_ctype;
920 while (token_type(token) == TOKEN_IDENT) {
921 struct expression *expr = NULL;
922 struct token *next = token->next;
923 struct decl_state ctx = { };
924 struct symbol *sym;
926 // FIXME: only 'deprecated' should be accepted
927 next = handle_attributes(next, &ctx);
929 if (match_op(next, '=')) {
930 next = constant_expression(next->next, &expr);
931 lastval = get_expression_value(expr);
932 ctype = &void_ctype;
933 if (expr && expr->ctype)
934 ctype = expr->ctype;
935 } else if (!ctype) {
936 ctype = &int_ctype;
937 } else if (is_int_type(ctype)) {
938 lastval++;
939 } else {
940 error_die(token->pos, "can't increment the last enum member");
943 if (!expr) {
944 expr = alloc_expression(token->pos, EXPR_VALUE);
945 expr->value = lastval;
946 expr->ctype = ctype;
949 sym = alloc_symbol(token->pos, SYM_NODE);
950 bind_symbol(sym, token->ident, NS_SYMBOL);
951 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
952 sym->initializer = expr;
953 sym->enum_member = 1;
954 sym->ctype.base_type = parent;
955 add_ptr_list(&parent->symbol_list, sym);
957 if (base_type != &bad_ctype) {
958 if (ctype->type == SYM_NODE)
959 ctype = ctype->ctype.base_type;
960 if (ctype->type == SYM_ENUM) {
961 if (ctype == parent)
962 ctype = base_type;
963 else
964 ctype = ctype->ctype.base_type;
967 * base_type rules:
968 * - if all enums are of the same type, then
969 * the base_type is that type (two first
970 * cases)
971 * - if enums are of different types, they
972 * all have to be integer types, and the
973 * base type is at least "int_ctype".
974 * - otherwise the base_type is "bad_ctype".
976 if (!base_type || ctype == &bad_ctype) {
977 base_type = ctype;
978 } else if (ctype == base_type) {
979 /* nothing */
980 } else if (is_int_type(base_type) && is_int_type(ctype)) {
981 base_type = &int_ctype;
982 } else if (is_restricted_type(base_type) != is_restricted_type(ctype)) {
983 if (!mix_bitwise++) {
984 warning(expr->pos, "mixed bitwiseness");
986 } else if (is_restricted_type(base_type) && base_type != ctype) {
987 sparse_error(expr->pos, "incompatible restricted type");
988 info(expr->pos, " expected: %s", show_typename(base_type));
989 info(expr->pos, " got: %s", show_typename(ctype));
990 base_type = &bad_ctype;
991 } else if (base_type != &bad_ctype) {
992 sparse_error(token->pos, "bad enum definition");
993 base_type = &bad_ctype;
995 parent->ctype.base_type = base_type;
997 if (is_int_type(base_type)) {
998 update_range(&range, lastval, ctype);
1000 token = next;
1002 sym->endpos = token->pos;
1004 if (!match_op(token, ','))
1005 break;
1006 token = token->next;
1008 if (!base_type) {
1009 sparse_error(token->pos, "empty enum definition");
1010 base_type = &bad_ctype;
1012 else if (!is_int_type(base_type))
1014 else if (type_is_ok(&uint_ctype, range))
1015 base_type = &uint_ctype;
1016 else if (type_is_ok(&int_ctype, range))
1017 base_type = &int_ctype;
1018 else if (type_is_ok(&ulong_ctype, range))
1019 base_type = &ulong_ctype;
1020 else if (type_is_ok(&long_ctype, range))
1021 base_type = &long_ctype;
1022 else if (type_is_ok(&ullong_ctype, range))
1023 base_type = &ullong_ctype;
1024 else if (type_is_ok(&llong_ctype, range))
1025 base_type = &llong_ctype;
1026 else
1027 base_type = &bad_ctype;
1028 parent->ctype.base_type = base_type;
1029 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
1030 parent->examined = 0;
1032 if (mix_bitwise)
1033 return token;
1034 cast_enum_list(parent->symbol_list, base_type);
1036 return token;
1039 static struct token *enum_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
1041 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
1042 struct ctype *ctype = &ctx->ctype.base_type->ctype;
1044 if (!ctype->base_type)
1045 ctype->base_type = &incomplete_ctype;
1047 return ret;
1050 static struct token *typeof_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
1053 if (!match_op(token, '(')) {
1054 sparse_error(token->pos, "expected '(' after typeof");
1055 return token;
1057 if (lookup_type(token->next)) {
1058 struct symbol *sym;
1059 token = typename(token->next, &sym, NULL);
1060 ctx->ctype.base_type = sym->ctype.base_type;
1061 apply_ctype(token->pos, &ctx->ctype, &sym->ctype);
1062 } else {
1063 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
1064 token = parse_expression(token->next, &typeof_sym->initializer);
1066 typeof_sym->endpos = token->pos;
1067 if (!typeof_sym->initializer) {
1068 sparse_error(token->pos, "expected expression after the '(' token");
1069 typeof_sym = &bad_ctype;
1071 ctx->ctype.base_type = typeof_sym;
1073 return expect(token, ')', "after typeof");
1076 static struct token *autotype_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
1078 ctx->ctype.base_type = &autotype_ctype;
1079 ctx->autotype = 1;
1080 return token;
1083 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1085 struct expression *expr = NULL;
1086 if (match_op(token, '('))
1087 token = parens_expression(token, &expr, "in attribute");
1088 return token;
1091 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1093 if (!ctx->ctype.alignment) {
1094 ctx->ctype.alignment = 1;
1095 ctx->packed = 1;
1097 return token;
1100 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1102 int alignment = max_alignment;
1103 struct expression *expr = NULL;
1105 if (match_op(token, '(')) {
1106 token = parens_expression(token, &expr, "in attribute");
1107 if (expr)
1108 alignment = const_expression_value(expr);
1110 if (alignment & (alignment-1)) {
1111 warning(token->pos, "I don't like non-power-of-2 alignments");
1112 return token;
1113 } else if (alignment > ctx->ctype.alignment)
1114 ctx->ctype.alignment = alignment;
1115 return token;
1118 static void apply_mod(struct position *pos, unsigned long *mods, unsigned long mod)
1120 if (*mods & mod & ~MOD_DUP_OK)
1121 warning(*pos, "duplicate %s", modifier_name(mod));
1122 *mods |= mod;
1125 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1127 apply_mod(pos, &ctx->modifiers, qual);
1130 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1132 apply_mod(&token->pos, &ctx->ctype.modifiers, attr->ctype.modifiers);
1133 return token;
1136 static struct token *attribute_function(struct token *token, struct symbol *attr, struct decl_state *ctx)
1138 apply_mod(&token->pos, &ctx->f_modifiers, attr->ctype.modifiers);
1139 return token;
1142 static struct token *attribute_bitwise(struct token *token, struct symbol *attr, struct decl_state *ctx)
1144 if (Wbitwise)
1145 attribute_modifier(token, attr, ctx);
1146 return token;
1149 static struct ident *numerical_address_space(int asn)
1151 char buff[32];
1153 if (!asn)
1154 return NULL;
1155 sprintf(buff, "<asn:%d>", asn);
1156 return built_in_ident(buff);
1159 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1161 struct expression *expr = NULL;
1162 struct ident *as = NULL;
1163 struct token *next;
1165 token = expect(token, '(', "after address_space attribute");
1166 switch (token_type(token)) {
1167 case TOKEN_NUMBER:
1168 next = primary_expression(token, &expr);
1169 if (expr->type != EXPR_VALUE)
1170 goto invalid;
1171 as = numerical_address_space(expr->value);
1172 break;
1173 case TOKEN_IDENT:
1174 next = token->next;
1175 as = token->ident;
1176 break;
1177 default:
1178 next = token->next;
1179 invalid:
1180 as = NULL;
1181 warning(token->pos, "invalid address space name");
1184 if (Waddress_space && as) {
1185 if (ctx->ctype.as)
1186 sparse_error(token->pos,
1187 "multiple address spaces given: %s & %s",
1188 show_as(ctx->ctype.as), show_as(as));
1189 ctx->ctype.as = as;
1191 token = expect(next, ')', "after address_space attribute");
1192 return token;
1195 static struct symbol *to_QI_mode(struct symbol *ctype)
1197 if (ctype->ctype.base_type != &int_type)
1198 return NULL;
1199 if (ctype == &char_ctype)
1200 return ctype;
1201 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1202 : &schar_ctype;
1205 static struct symbol *to_HI_mode(struct symbol *ctype)
1207 if (ctype->ctype.base_type != &int_type)
1208 return NULL;
1209 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1210 : &sshort_ctype;
1213 static struct symbol *to_SI_mode(struct symbol *ctype)
1215 if (ctype->ctype.base_type != &int_type)
1216 return NULL;
1217 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1218 : &sint_ctype;
1221 static struct symbol *to_DI_mode(struct symbol *ctype)
1223 if (ctype->ctype.base_type != &int_type)
1224 return NULL;
1225 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1226 : &sllong_ctype;
1229 static struct symbol *to_TI_mode(struct symbol *ctype)
1231 if (ctype->ctype.base_type != &int_type)
1232 return NULL;
1233 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint128_ctype
1234 : &sint128_ctype;
1237 static struct symbol *to_pointer_mode(struct symbol *ctype)
1239 if (ctype->ctype.base_type != &int_type)
1240 return NULL;
1241 return ctype->ctype.modifiers & MOD_UNSIGNED ? uintptr_ctype
1242 : intptr_ctype;
1245 static struct symbol *to_word_mode(struct symbol *ctype)
1247 if (ctype->ctype.base_type != &int_type)
1248 return NULL;
1249 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1250 : &slong_ctype;
1253 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1255 token = expect(token, '(', "after mode attribute");
1256 if (token_type(token) == TOKEN_IDENT) {
1257 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1258 if (mode && mode->op->type & KW_MODE)
1259 ctx->mode = mode->op;
1260 else
1261 sparse_error(token->pos, "unknown mode attribute %s", show_ident(token->ident));
1262 token = token->next;
1263 } else
1264 sparse_error(token->pos, "expect attribute mode symbol\n");
1265 token = expect(token, ')', "after mode attribute");
1266 return token;
1269 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1271 struct context *context = alloc_context();
1272 struct expression *args[3];
1273 int idx = 0;
1275 token = expect(token, '(', "after context attribute");
1276 token = conditional_expression(token, &args[0]);
1277 token = expect(token, ',', "after context 1st argument");
1278 token = conditional_expression(token, &args[1]);
1279 if (match_op(token, ',')) {
1280 token = token->next;
1281 token = conditional_expression(token, &args[2]);
1282 token = expect(token, ')', "after context 3rd argument");
1283 context->context = args[0];
1284 idx++;
1285 } else {
1286 token = expect(token, ')', "after context 2nd argument");
1288 context->in = get_expression_value(args[idx++]);
1289 context->out = get_expression_value(args[idx++]);
1290 add_ptr_list(&ctx->ctype.contexts, context);
1291 return token;
1294 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1296 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1297 ctx->ctype.base_type->designated_init = 1;
1298 else
1299 warning(token->pos, "attribute designated_init applied to non-structure type");
1300 return token;
1303 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1305 if (Wtransparent_union)
1306 warning(token->pos, "attribute __transparent_union__");
1308 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_UNION)
1309 ctx->ctype.base_type->transparent_union = 1;
1310 else
1311 warning(token->pos, "attribute __transparent_union__ applied to non-union type");
1312 return token;
1315 static struct token *recover_unknown_attribute(struct token *token)
1317 struct expression *expr = NULL;
1319 if (Wunknown_attribute)
1320 warning(token->pos, "unknown attribute '%s'", show_ident(token->ident));
1321 token = token->next;
1322 if (match_op(token, '('))
1323 token = parens_expression(token, &expr, "in attribute");
1324 return token;
1327 static struct token *attribute_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
1329 token = expect(token, '(', "after attribute");
1330 token = expect(token, '(', "after attribute");
1332 while (token_type(token) == TOKEN_IDENT) {
1333 struct symbol *attr = lookup_keyword(token->ident, NS_KEYWORD);
1334 if (attr && attr->op->attribute)
1335 token = attr->op->attribute(token->next, attr, ctx);
1336 else
1337 token = recover_unknown_attribute(token);
1339 if (!match_op(token, ','))
1340 break;
1341 token = token->next;
1344 token = expect(token, ')', "after attribute");
1345 token = expect(token, ')', "after attribute");
1346 return token;
1349 static unsigned long decl_modifiers(struct decl_state *ctx)
1351 unsigned long mods = ctx->ctype.modifiers & MOD_DECLARE;
1352 ctx->ctype.modifiers &= ~MOD_DECLARE;
1353 return ctx->storage_class | mods;
1356 static struct token *storage_specifier(struct token *next, struct symbol *sym, struct decl_state *ctx)
1358 int is_tls = ctx->ctype.modifiers & MOD_TLS;
1359 unsigned long class = sym->ctype.modifiers;
1360 const char *storage = modifier_name(class);
1362 /* __thread can be used alone, or with extern or static */
1363 if (is_tls && (class & ~(MOD_STATIC|MOD_EXTERN)))
1364 sparse_error(next->pos, "__thread cannot be used with '%s'", storage);
1365 else if (!ctx->storage_class)
1366 ctx->storage_class = class;
1367 else if (ctx->storage_class == class)
1368 sparse_error(next->pos, "duplicate %s", storage);
1369 else
1370 sparse_error(next->pos, "multiple storage classes");
1371 return next;
1374 static struct token *thread_specifier(struct token *next, struct symbol *sym, struct decl_state *ctx)
1376 /* This GCC extension can be used alone, or with extern or static */
1377 if (!(ctx->storage_class & ~(MOD_STATIC|MOD_EXTERN))) {
1378 apply_qualifier(&next->pos, &ctx->ctype, MOD_TLS);
1379 } else {
1380 sparse_error(next->pos, "__thread cannot be used with '%s'",
1381 modifier_name(ctx->storage_class));
1384 return next;
1387 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1389 ctx->forced = 1;
1390 return token;
1393 static struct token *alignas_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx)
1395 int alignment = 0;
1397 if (!match_op(token, '(')) {
1398 sparse_error(token->pos, "expected '(' after _Alignas");
1399 return token;
1401 if (lookup_type(token->next)) {
1402 struct symbol *sym = NULL;
1403 token = typename(token->next, &sym, NULL);
1404 sym = examine_symbol_type(sym);
1405 alignment = sym->ctype.alignment;
1406 token = expect(token, ')', "after _Alignas(...");
1407 } else {
1408 struct expression *expr = NULL;
1409 token = parens_expression(token, &expr, "after _Alignas");
1410 if (!expr)
1411 return token;
1412 alignment = const_expression_value(expr);
1415 if (alignment < 0) {
1416 warning(token->pos, "non-positive alignment");
1417 return token;
1419 if (alignment & (alignment-1)) {
1420 warning(token->pos, "non-power-of-2 alignment");
1421 return token;
1423 if (alignment > ctx->ctype.alignment)
1424 ctx->ctype.alignment = alignment;
1425 return token;
1428 static struct token *generic_qualifier(struct token *next, struct symbol *sym, struct decl_state *ctx)
1430 apply_qualifier(&next->pos, &ctx->ctype, sym->ctype.modifiers);
1431 return next;
1434 static void apply_ctype(struct position pos, struct ctype *dst, struct ctype *src)
1436 unsigned long mod = src->modifiers;
1438 if (mod)
1439 apply_qualifier(&pos, dst, mod);
1441 /* Context */
1442 concat_ptr_list((struct ptr_list *)src->contexts,
1443 (struct ptr_list **)&dst->contexts);
1445 /* Alignment */
1446 if (src->alignment > dst->alignment)
1447 dst->alignment = src->alignment;
1449 /* Address space */
1450 if (src->as)
1451 dst->as = src->as;
1454 static void specifier_conflict(struct position pos, int what, struct ident *new)
1456 const char *old;
1457 if (what & (Set_S | Set_T))
1458 goto Catch_all;
1459 if (what & Set_Char)
1460 old = "char";
1461 else if (what & Set_Double)
1462 old = "double";
1463 else if (what & Set_Float)
1464 old = "float";
1465 else if (what & Set_Signed)
1466 old = "signed";
1467 else if (what & Set_Unsigned)
1468 old = "unsigned";
1469 else if (what & Set_Short)
1470 old = "short";
1471 else if (what & Set_Long)
1472 old = "long";
1473 else
1474 old = "long long";
1475 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1476 old, show_ident(new));
1477 return;
1479 Catch_all:
1480 sparse_error(pos, "two or more data types in declaration specifiers");
1483 static struct symbol * const int_types[] =
1484 {&char_ctype, &short_ctype, &int_ctype, &long_ctype, &llong_ctype, &int128_ctype};
1485 static struct symbol * const signed_types[] =
1486 {&schar_ctype, &sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1487 &sint128_ctype};
1488 static struct symbol * const unsigned_types[] =
1489 {&uchar_ctype, &ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1490 &uint128_ctype};
1491 static struct symbol * const real_types[] =
1492 {&float_ctype, &double_ctype, &ldouble_ctype};
1493 static struct symbol * const * const types[] = {
1494 [CInt] = int_types + 2,
1495 [CSInt] = signed_types + 2,
1496 [CUInt] = unsigned_types + 2,
1497 [CReal] = real_types + 1,
1500 struct symbol *ctype_integer(int size, int want_unsigned)
1502 return types[want_unsigned ? CUInt : CInt][size];
1505 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1507 while (token_type(t) == TOKEN_IDENT) {
1508 struct symbol *s = lookup_keyword(t->ident, NS_TYPEDEF);
1509 if (!s)
1510 break;
1511 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1512 break;
1513 t = t->next;
1514 if (s->op->declarator)
1515 t = s->op->declarator(t, s, ctx);
1517 return t;
1520 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1522 int seen = 0;
1523 int class = CInt;
1524 int rank = 0;
1526 while (token_type(token) == TOKEN_IDENT) {
1527 struct symbol *s = lookup_symbol(token->ident,
1528 NS_TYPEDEF | NS_SYMBOL);
1529 if (!s || !(s->namespace & NS_TYPEDEF))
1530 break;
1531 if (s->type != SYM_KEYWORD) {
1532 if (seen & Set_Any)
1533 break;
1534 seen |= Set_S | Set_T;
1535 ctx->ctype.base_type = s->ctype.base_type;
1536 apply_ctype(token->pos, &ctx->ctype, &s->ctype);
1537 token = token->next;
1538 continue;
1540 if (s->op->type & KW_SPECIFIER) {
1541 if (seen & s->op->test) {
1542 specifier_conflict(token->pos,
1543 seen & s->op->test,
1544 token->ident);
1545 break;
1547 seen |= s->op->set;
1548 class += s->op->class;
1549 if (s->op->set & Set_Int128)
1550 rank = 3;
1551 else if (s->op->set & Set_Char)
1552 rank = -2;
1553 if (s->op->set & (Set_Short|Set_Float)) {
1554 rank = -1;
1555 } else if (s->op->set & Set_Long && rank++) {
1556 if (class == CReal) {
1557 specifier_conflict(token->pos,
1558 Set_Vlong,
1559 &double_ident);
1560 break;
1562 seen |= Set_Vlong;
1565 token = token->next;
1566 if (s->op->declarator) // Note: this eats attributes
1567 token = s->op->declarator(token, s, ctx);
1568 if (s->op->type & KW_EXACT) {
1569 ctx->ctype.base_type = s->ctype.base_type;
1570 ctx->ctype.modifiers |= s->ctype.modifiers;
1574 if (!(seen & Set_S)) { /* not set explicitly? */
1575 struct symbol *base = &incomplete_ctype;
1576 if (seen & Set_Any)
1577 base = types[class][rank];
1578 ctx->ctype.base_type = base;
1581 if (ctx->ctype.modifiers & MOD_BITWISE) {
1582 struct symbol *type;
1583 ctx->ctype.modifiers &= ~MOD_BITWISE;
1584 if (!is_int_type(ctx->ctype.base_type)) {
1585 sparse_error(token->pos, "invalid modifier");
1586 return token;
1588 type = alloc_symbol(token->pos, SYM_BASETYPE);
1589 *type = *ctx->ctype.base_type;
1590 type->ctype.modifiers &= ~MOD_SPECIFIER;
1591 type->ctype.base_type = ctx->ctype.base_type;
1592 type->type = SYM_RESTRICT;
1593 ctx->ctype.base_type = type;
1594 create_fouled(type);
1596 return token;
1599 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1601 struct expression *expr = NULL;
1602 int has_static = 0;
1604 while (token_type(token) == TOKEN_IDENT) {
1605 struct symbol *sym = lookup_keyword(token->ident, NS_TYPEDEF);
1606 if (!sym || !(sym->op->type & (KW_STATIC|KW_QUALIFIER)))
1607 break;
1608 if (has_static && (sym->op->type & KW_STATIC))
1609 sparse_error(token->pos, "duplicate array static declarator");
1610 has_static |= (sym->op->type & KW_STATIC);
1611 token = token->next;
1613 if (match_op(token, '*') && match_op(token->next, ']')) {
1614 // FIXME: '[*]' is treated like '[]'
1615 token = token->next;
1616 } else {
1617 token = assignment_expression(token, &expr);
1619 sym->array_size = expr;
1620 return token;
1623 static struct token *parameter_type_list(struct token *, struct symbol *);
1624 static struct token *identifier_list(struct token *, struct symbol *);
1625 static struct token *declarator(struct token *token, struct decl_state *ctx);
1627 static struct token *handle_asm_name(struct token *token, struct decl_state *ctx)
1629 struct expression *expr;
1630 struct symbol *keyword;
1632 if (token_type(token) != TOKEN_IDENT)
1633 return token;
1634 keyword = lookup_keyword(token->ident, NS_KEYWORD);
1635 if (!keyword)
1636 return token;
1637 if (!(keyword->op->type & KW_ASM))
1638 return token;
1640 token = token->next;
1641 token = expect(token, '(', "after asm");
1642 token = string_expression(token, &expr, "asm name");
1643 token = expect(token, ')', "after asm");
1644 return token;
1648 // test if @token is '__attribute__' (or one of its variant)
1649 static bool match_attribute(struct token *token)
1651 struct symbol *sym;
1653 if (token_type(token) != TOKEN_IDENT)
1654 return false;
1655 sym = lookup_keyword(token->ident, NS_TYPEDEF);
1656 if (!sym)
1657 return false;
1658 return sym->op->type & KW_ATTRIBUTE;
1661 static struct token *skip_attribute(struct token *token)
1663 token = token->next;
1664 if (match_op(token, '(')) {
1665 int depth = 1;
1666 token = token->next;
1667 while (depth && !eof_token(token)) {
1668 if (token_type(token) == TOKEN_SPECIAL) {
1669 if (token->special == '(')
1670 depth++;
1671 else if (token->special == ')')
1672 depth--;
1674 token = token->next;
1677 return token;
1680 static struct token *skip_attributes(struct token *token)
1682 while (match_attribute(token)) {
1683 token = expect(token->next, '(', "after attribute");
1684 token = expect(token, '(', "after attribute");
1685 while (token_type(token) == TOKEN_IDENT) {
1686 token = skip_attribute(token);
1687 if (!match_op(token, ','))
1688 break;
1689 token = token->next;
1691 token = expect(token, ')', "after attribute");
1692 token = expect(token, ')', "after attribute");
1694 return token;
1697 static struct token *handle_attributes(struct token *token, struct decl_state *ctx)
1699 while (match_attribute(token))
1700 token = attribute_specifier(token->next, NULL, ctx);
1701 return token;
1704 static int is_nested(struct token *token, struct token **p,
1705 int prefer_abstract)
1708 * This can be either a parameter list or a grouping.
1709 * For the direct (non-abstract) case, we know if must be
1710 * a parameter list if we already saw the identifier.
1711 * For the abstract case, we know if must be a parameter
1712 * list if it is empty or starts with a type.
1714 struct token *next = token->next;
1716 *p = next = skip_attributes(next);
1718 if (token_type(next) == TOKEN_IDENT) {
1719 if (lookup_type(next))
1720 return !prefer_abstract;
1721 return 1;
1724 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1725 return 0;
1727 return 1;
1730 enum kind {
1731 Empty, K_R, Proto, Bad_Func,
1734 static enum kind which_func(struct token *token,
1735 struct ident **n,
1736 int prefer_abstract)
1738 struct token *next = token->next;
1740 if (token_type(next) == TOKEN_IDENT) {
1741 if (lookup_type(next))
1742 return Proto;
1743 /* identifier list not in definition; complain */
1744 if (prefer_abstract)
1745 warning(token->pos,
1746 "identifier list not in definition");
1747 return K_R;
1750 if (token_type(next) != TOKEN_SPECIAL)
1751 return Bad_Func;
1753 if (next->special == ')') {
1754 /* don't complain about those */
1755 if (!n || match_op(next->next, ';') || match_op(next->next, ','))
1756 return Empty;
1757 if (Wstrict_prototypes)
1758 warning(next->pos,
1759 "non-ANSI function declaration of function '%s'",
1760 show_ident(*n));
1761 return Empty;
1764 if (next->special == SPECIAL_ELLIPSIS) {
1765 warning(next->pos,
1766 "variadic functions must have one named argument");
1767 return Proto;
1770 return Bad_Func;
1773 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1775 struct ctype *ctype = &ctx->ctype;
1776 struct token *next;
1777 struct ident **p = ctx->ident;
1779 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1780 *ctx->ident = token->ident;
1781 token = token->next;
1782 } else if (match_op(token, '(') &&
1783 is_nested(token, &next, ctx->prefer_abstract)) {
1784 struct symbol *base_type = ctype->base_type;
1785 if (token->next != next)
1786 next = handle_attributes(token->next, ctx);
1787 token = declarator(next, ctx);
1788 token = expect(token, ')', "in nested declarator");
1789 while (ctype->base_type != base_type)
1790 ctype = &ctype->base_type->ctype;
1791 p = NULL;
1794 if (match_op(token, '(')) {
1795 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1796 struct symbol *fn;
1797 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1798 ctype->modifiers |= ctx->f_modifiers;
1799 token = token->next;
1800 if (kind == K_R)
1801 token = identifier_list(token, fn);
1802 else if (kind == Proto)
1803 token = parameter_type_list(token, fn);
1804 token = expect(token, ')', "in function declarator");
1805 fn->endpos = token->pos;
1806 return token;
1809 while (match_op(token, '[')) {
1810 struct symbol *array;
1811 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1812 token = abstract_array_declarator(token->next, array);
1813 token = expect(token, ']', "in abstract_array_declarator");
1814 array->endpos = token->pos;
1815 ctype = &array->ctype;
1817 return token;
1820 static struct token *pointer(struct token *token, struct decl_state *ctx)
1822 while (match_op(token,'*')) {
1823 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1824 ptr->ctype.modifiers = ctx->ctype.modifiers;
1825 ptr->ctype.base_type = ctx->ctype.base_type;
1826 ptr->ctype.as = ctx->ctype.as;
1827 ptr->ctype.contexts = ctx->ctype.contexts;
1828 ctx->ctype.modifiers = 0;
1829 ctx->ctype.base_type = ptr;
1830 ctx->ctype.as = NULL;
1831 ctx->ctype.contexts = NULL;
1832 ctx->ctype.alignment = 0;
1834 token = handle_qualifiers(token->next, ctx);
1835 ctx->ctype.base_type->endpos = token->pos;
1837 return token;
1840 static struct token *declarator(struct token *token, struct decl_state *ctx)
1842 token = pointer(token, ctx);
1843 return direct_declarator(token, ctx);
1846 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1848 struct ctype *ctype = &ctx->ctype;
1849 struct expression *expr;
1850 struct symbol *bitfield;
1851 long long width;
1853 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1854 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1855 show_typename(ctype->base_type));
1856 // Parse this to recover gracefully.
1857 return conditional_expression(token->next, &expr);
1860 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1861 token = conditional_expression(token->next, &expr);
1862 width = const_expression_value(expr);
1863 bitfield->bit_size = width;
1865 if (width < 0 || width > INT_MAX || (*ctx->ident && width == 0)) {
1866 sparse_error(token->pos, "bitfield '%s' has invalid width (%lld)",
1867 show_ident(*ctx->ident), width);
1868 width = -1;
1869 } else if (*ctx->ident) {
1870 struct symbol *base_type = bitfield->ctype.base_type;
1871 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1872 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1873 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1874 // Valid values are either {-1;0} or {0}, depending on integer
1875 // representation. The latter makes for very efficient code...
1876 sparse_error(token->pos, "dubious one-bit signed bitfield");
1878 if (Wdefault_bitfield_sign &&
1879 bitfield_type->type != SYM_ENUM &&
1880 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1881 is_signed) {
1882 // The sign of bitfields is unspecified by default.
1883 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1886 bitfield->bit_size = width;
1887 bitfield->endpos = token->pos;
1888 bitfield->ident = *ctx->ident;
1889 return token;
1892 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1894 struct decl_state ctx = {.prefer_abstract = 0};
1895 struct ctype saved;
1896 unsigned long mod;
1898 token = declaration_specifiers(token, &ctx);
1899 mod = decl_modifiers(&ctx);
1900 saved = ctx.ctype;
1901 for (;;) {
1902 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1903 ctx.ident = &decl->ident;
1905 token = declarator(token, &ctx);
1906 if (match_op(token, ':'))
1907 token = handle_bitfield(token, &ctx);
1909 token = handle_attributes(token, &ctx);
1910 apply_modifiers(token->pos, &ctx);
1912 decl->ctype = ctx.ctype;
1913 decl->ctype.modifiers |= mod;
1914 decl->endpos = token->pos;
1915 add_symbol(list, decl);
1916 if (!match_op(token, ','))
1917 break;
1918 token = token->next;
1919 ctx.ctype = saved;
1921 return token;
1924 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1926 while (!match_op(token, '}')) {
1927 if (match_ident(token, &_Static_assert_ident)) {
1928 token = parse_static_assert(token, NULL);
1929 continue;
1931 if (!match_op(token, ';'))
1932 token = declaration_list(token, list);
1933 if (!match_op(token, ';')) {
1934 sparse_error(token->pos, "expected ; at end of declaration");
1935 break;
1937 token = token->next;
1939 return token;
1942 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1944 struct decl_state ctx = {.prefer_abstract = 1};
1946 token = declaration_specifiers(token, &ctx);
1947 ctx.ident = &sym->ident;
1948 token = declarator(token, &ctx);
1949 token = handle_attributes(token, &ctx);
1950 apply_modifiers(token->pos, &ctx);
1951 sym->ctype = ctx.ctype;
1952 sym->ctype.modifiers |= decl_modifiers(&ctx);
1953 sym->endpos = token->pos;
1954 sym->forced_arg = ctx.forced;
1955 return token;
1958 struct token *typename(struct token *token, struct symbol **p, int *forced)
1960 struct decl_state ctx = {.prefer_abstract = 1};
1961 unsigned long class;
1962 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1963 *p = sym;
1964 token = declaration_specifiers(token, &ctx);
1965 token = declarator(token, &ctx);
1966 apply_modifiers(token->pos, &ctx);
1967 sym->ctype = ctx.ctype;
1968 sym->endpos = token->pos;
1969 class = ctx.storage_class;
1970 if (forced)
1971 *forced = ctx.forced;
1972 if (class)
1973 warning(sym->pos, "storage class in typename (%s%s)",
1974 modifier_string(class), show_typename(sym));
1975 return token;
1978 static struct token *parse_underscore_Pragma(struct token *token)
1980 struct token *next;
1982 next = token->next;
1983 if (!match_op(next, '('))
1984 return next;
1985 next = next->next;
1986 if (next->pos.type != TOKEN_STRING)
1987 return next;
1988 next = next->next;
1989 if (!match_op(next, ')'))
1990 return next;
1991 return next->next;
1994 static struct token *expression_statement(struct token *token, struct expression **tree)
1996 if (match_ident(token, &_Pragma_ident))
1997 return parse_underscore_Pragma(token);
1999 token = parse_expression(token, tree);
2000 return expect(token, ';', "at end of statement");
2003 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
2004 struct asm_operand_list **inout)
2006 /* Allow empty operands */
2007 if (match_op(token->next, ':') || match_op(token->next, ')'))
2008 return token->next;
2009 do {
2010 struct asm_operand *op = __alloc_asm_operand(0);
2011 if (match_op(token->next, '[') &&
2012 token_type(token->next->next) == TOKEN_IDENT &&
2013 match_op(token->next->next->next, ']')) {
2014 op->name = token->next->next->ident;
2015 token = token->next->next->next;
2017 token = token->next;
2018 token = string_expression(token, &op->constraint, "asm constraint");
2019 token = parens_expression(token, &op->expr, "in asm parameter");
2020 add_ptr_list(inout, op);
2021 } while (match_op(token, ','));
2022 return token;
2025 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
2026 struct expression_list **clobbers)
2028 struct expression *expr;
2030 do {
2031 token = primary_expression(token->next, &expr);
2032 if (expr)
2033 add_expression(clobbers, expr);
2034 } while (match_op(token, ','));
2035 return token;
2038 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
2039 struct symbol_list **labels)
2041 struct symbol *label;
2043 do {
2044 token = token->next; /* skip ':' and ',' */
2045 if (token_type(token) != TOKEN_IDENT)
2046 return token;
2047 label = label_symbol(token, 1);
2048 add_symbol(labels, label);
2049 token = token->next;
2050 } while (match_op(token, ','));
2051 return token;
2054 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
2056 unsigned long mods = 0;
2058 token = token->next;
2059 stmt->type = STMT_ASM;
2060 while (token_type(token) == TOKEN_IDENT) {
2061 struct symbol *s = lookup_keyword(token->ident, NS_TYPEDEF);
2062 if (s && s->op->asm_modifier)
2063 s->op->asm_modifier(token, &mods, s->ctype.modifiers);
2064 else if (token->ident == &goto_ident)
2065 asm_modifier(token, &mods, MOD_ASM_GOTO);
2066 token = token->next;
2068 token = expect(token, '(', "after asm");
2069 token = string_expression(token, &stmt->asm_string, "inline asm");
2070 if (match_op(token, ':'))
2071 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
2072 if (match_op(token, ':'))
2073 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
2074 if (match_op(token, ':'))
2075 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
2076 if (match_op(token, ':') && (mods & MOD_ASM_GOTO))
2077 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
2078 token = expect(token, ')', "after asm");
2079 return expect(token, ';', "at end of asm-statement");
2082 static struct token *parse_static_assert(struct token *token, struct symbol_list **unused)
2084 struct expression *cond = NULL, *message = NULL;
2086 token = expect(token->next, '(', "after _Static_assert");
2087 token = constant_expression(token, &cond);
2088 if (!cond)
2089 sparse_error(token->pos, "Expected constant expression");
2090 if (match_op(token, ',')) {
2091 token = token->next;
2092 token = string_expression(token, &message, "_Static_assert()");
2093 if (!message)
2094 cond = NULL;
2096 token = expect(token, ')', "after diagnostic message in _Static_assert");
2097 token = expect(token, ';', "after _Static_assert()");
2099 if (cond && !const_expression_value(cond) && cond->type == EXPR_VALUE) {
2100 const char *sep = "", *msg = "";
2102 if (message) {
2103 sep = ": ";
2104 msg = show_string(message->string);
2106 sparse_error(cond->pos, "static assertion failed%s%s", sep, msg);
2109 return token;
2112 /* Make a statement out of an expression */
2113 static struct statement *make_statement(struct expression *expr)
2115 struct statement *stmt;
2117 if (!expr)
2118 return NULL;
2119 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
2120 stmt->expression = expr;
2121 return stmt;
2125 * All iterators have two symbols associated with them:
2126 * the "continue" and "break" symbols, which are targets
2127 * for continue and break statements respectively.
2129 * They are in a special name-space, but they follow
2130 * all the normal visibility rules, so nested iterators
2131 * automatically work right.
2133 static void start_iterator(struct statement *stmt)
2135 struct symbol *cont, *brk;
2137 start_block_scope(stmt->pos);
2138 cont = alloc_symbol(stmt->pos, SYM_NODE);
2139 bind_symbol(cont, &continue_ident, NS_ITERATOR);
2140 brk = alloc_symbol(stmt->pos, SYM_NODE);
2141 bind_symbol(brk, &break_ident, NS_ITERATOR);
2143 stmt->type = STMT_ITERATOR;
2144 stmt->iterator_break = brk;
2145 stmt->iterator_continue = cont;
2146 fn_local_symbol(brk);
2147 fn_local_symbol(cont);
2150 static void end_iterator(struct statement *stmt)
2152 end_block_scope();
2155 static struct statement *start_function(struct symbol *sym)
2157 struct symbol *ret;
2158 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2160 start_function_scope(sym->pos);
2161 ret = alloc_symbol(sym->pos, SYM_NODE);
2162 ret->ctype = sym->ctype.base_type->ctype;
2163 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_QUALIFIER | MOD_TLS | MOD_ACCESS | MOD_NOCAST | MOD_NODEREF);
2164 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2165 bind_symbol(ret, &return_ident, NS_ITERATOR);
2166 stmt->ret = ret;
2167 fn_local_symbol(ret);
2169 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2170 current_fn = sym;
2172 return stmt;
2175 static void end_function(struct symbol *sym)
2177 current_fn = NULL;
2178 end_function_scope();
2182 * A "switch()" statement, like an iterator, has a
2183 * the "break" symbol associated with it. It works
2184 * exactly like the iterator break - it's the target
2185 * for any break-statements in scope, and means that
2186 * "break" handling doesn't even need to know whether
2187 * it's breaking out of an iterator or a switch.
2189 * In addition, the "case" symbol is a marker for the
2190 * case/default statements to find the switch statement
2191 * that they are associated with.
2193 static void start_switch(struct statement *stmt)
2195 struct symbol *brk, *switch_case;
2197 start_block_scope(stmt->pos);
2198 brk = alloc_symbol(stmt->pos, SYM_NODE);
2199 bind_symbol(brk, &break_ident, NS_ITERATOR);
2201 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2202 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2203 switch_case->stmt = stmt;
2205 stmt->type = STMT_SWITCH;
2206 stmt->switch_break = brk;
2207 stmt->switch_case = switch_case;
2209 fn_local_symbol(brk);
2210 fn_local_symbol(switch_case);
2213 static void end_switch(struct statement *stmt)
2215 if (!stmt->switch_case->symbol_list)
2216 warning(stmt->pos, "switch with no cases");
2217 end_block_scope();
2220 static void add_case_statement(struct statement *stmt)
2222 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2223 struct symbol *sym;
2225 if (!target) {
2226 sparse_error(stmt->pos, "not in switch scope");
2227 stmt->type = STMT_NONE;
2228 return;
2230 sym = alloc_symbol(stmt->pos, SYM_NODE);
2231 add_symbol(&target->symbol_list, sym);
2232 sym->stmt = stmt;
2233 stmt->case_label = sym;
2234 fn_local_symbol(sym);
2237 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2239 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2241 if (!target)
2242 error_die(token->pos, "internal error: return without a function target");
2243 stmt->type = STMT_RETURN;
2244 stmt->ret_target = target;
2245 return expression_statement(token->next, &stmt->ret_value);
2248 static void validate_for_loop_decl(struct symbol *sym)
2250 unsigned long storage = sym->ctype.modifiers & MOD_STORAGE;
2252 if (storage & ~(MOD_AUTO | MOD_REGISTER)) {
2253 const char *name = show_ident(sym->ident);
2254 sparse_error(sym->pos, "non-local var '%s' in for-loop initializer", name);
2255 sym->ctype.modifiers &= ~MOD_STORAGE;
2259 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2261 struct symbol_list *syms;
2262 struct expression *e1, *e2, *e3;
2263 struct statement *iterator;
2265 start_iterator(stmt);
2266 token = expect(token->next, '(', "after 'for'");
2268 syms = NULL;
2269 e1 = NULL;
2270 /* C99 variable declaration? */
2271 if (lookup_type(token)) {
2272 token = external_declaration(token, &syms, validate_for_loop_decl);
2273 } else {
2274 token = parse_expression(token, &e1);
2275 token = expect(token, ';', "in 'for'");
2277 token = parse_expression(token, &e2);
2278 token = expect(token, ';', "in 'for'");
2279 token = parse_expression(token, &e3);
2280 token = expect(token, ')', "in 'for'");
2281 token = statement(token, &iterator);
2283 stmt->iterator_syms = syms;
2284 stmt->iterator_pre_statement = make_statement(e1);
2285 stmt->iterator_pre_condition = e2;
2286 stmt->iterator_post_statement = make_statement(e3);
2287 stmt->iterator_post_condition = NULL;
2288 stmt->iterator_statement = iterator;
2289 end_iterator(stmt);
2291 return token;
2294 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2296 struct expression *expr;
2297 struct statement *iterator;
2299 start_iterator(stmt);
2300 token = parens_expression(token->next, &expr, "after 'while'");
2301 token = statement(token, &iterator);
2303 stmt->iterator_pre_condition = expr;
2304 stmt->iterator_post_condition = NULL;
2305 stmt->iterator_statement = iterator;
2306 end_iterator(stmt);
2308 return token;
2311 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2313 struct expression *expr;
2314 struct statement *iterator;
2316 start_iterator(stmt);
2317 token = statement(token->next, &iterator);
2318 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2319 token = token->next;
2320 else
2321 sparse_error(token->pos, "expected 'while' after 'do'");
2322 token = parens_expression(token, &expr, "after 'do-while'");
2324 stmt->iterator_post_condition = expr;
2325 stmt->iterator_statement = iterator;
2326 end_iterator(stmt);
2328 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2329 warning(iterator->pos, "do-while statement is not a compound statement");
2331 return expect(token, ';', "after statement");
2334 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2336 stmt->type = STMT_IF;
2337 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2338 token = statement(token, &stmt->if_true);
2339 if (token_type(token) != TOKEN_IDENT)
2340 return token;
2341 if (token->ident != &else_ident)
2342 return token;
2343 return statement(token->next, &stmt->if_false);
2346 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2348 stmt->type = STMT_CASE;
2349 token = expect(token, ':', "after default/case");
2350 add_case_statement(stmt);
2351 return statement(token, &stmt->case_statement);
2354 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2356 token = parse_expression(token->next, &stmt->case_expression);
2357 if (match_op(token, SPECIAL_ELLIPSIS))
2358 token = parse_expression(token->next, &stmt->case_to);
2359 return case_statement(token, stmt);
2362 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2364 return case_statement(token->next, stmt);
2367 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2369 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2370 stmt->type = STMT_GOTO;
2371 stmt->goto_label = target;
2372 if (!target)
2373 sparse_error(stmt->pos, "break/continue not in iterator scope");
2374 return expect(token->next, ';', "at end of statement");
2377 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2379 stmt->type = STMT_SWITCH;
2380 start_switch(stmt);
2381 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2382 token = statement(token, &stmt->switch_statement);
2383 end_switch(stmt);
2384 return token;
2387 static void warn_label_usage(struct position def, struct position use, struct ident *ident)
2389 const char *id = show_ident(ident);
2390 sparse_error(use, "label '%s' used outside statement expression", id);
2391 info(def, " label '%s' defined here", id);
2392 current_fn->bogus_linear = 1;
2395 void check_label_usage(struct symbol *label, struct position use_pos)
2397 struct statement *def = label->stmt;
2399 if (def) {
2400 if (!is_in_scope(def->label_scope, label_scope))
2401 warn_label_usage(def->pos, use_pos, label->ident);
2402 } else if (!label->label_scope) {
2403 label->label_scope = label_scope;
2404 label->label_pos = use_pos;
2408 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2410 stmt->type = STMT_GOTO;
2411 token = token->next;
2412 if (match_op(token, '*')) {
2413 token = parse_expression(token->next, &stmt->goto_expression);
2414 add_statement(&function_computed_goto_list, stmt);
2415 } else if (token_type(token) == TOKEN_IDENT) {
2416 struct symbol *label = label_symbol(token, 1);
2417 stmt->goto_label = label;
2418 check_label_usage(label, stmt->pos);
2419 token = token->next;
2420 } else {
2421 sparse_error(token->pos, "Expected identifier or goto expression");
2423 return expect(token, ';', "at end of statement");
2426 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2428 stmt->type = STMT_CONTEXT;
2429 token = token->next;
2430 token = expect(token, '(', "after __context__ statement");
2431 token = assignment_expression(token, &stmt->expression);
2432 if (!stmt->expression)
2433 unexpected(token, "expression expected after '('");
2434 if (match_op(token, ',')) {
2435 token = token->next;
2436 stmt->context = stmt->expression;
2437 token = assignment_expression(token, &stmt->expression);
2438 if (!stmt->expression)
2439 unexpected(token, "expression expected after ','");
2441 token = expect(token, ')', "at end of __context__ statement");
2442 return expect(token, ';', "at end of statement");
2445 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2447 stmt->type = STMT_RANGE;
2448 token = token->next;
2449 token = expect(token, '(', "after __range__ statement");
2450 token = assignment_expression(token, &stmt->range_expression);
2451 token = expect(token, ',', "after range expression");
2452 token = assignment_expression(token, &stmt->range_low);
2453 token = expect(token, ',', "after low range");
2454 token = assignment_expression(token, &stmt->range_high);
2455 token = expect(token, ')', "after range statement");
2456 return expect(token, ';', "after range statement");
2459 static struct token *handle_label_attributes(struct token *token, struct symbol *label)
2461 struct decl_state ctx = { };
2463 token = handle_attributes(token, &ctx);
2464 label->label_modifiers = ctx.ctype.modifiers;
2465 return token;
2468 static struct token *statement(struct token *token, struct statement **tree)
2470 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2472 *tree = stmt;
2473 if (token_type(token) == TOKEN_IDENT) {
2474 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2475 if (s && s->op->statement)
2476 return s->op->statement(token, stmt);
2478 if (match_op(token->next, ':')) {
2479 struct symbol *s = label_symbol(token, 0);
2480 token = handle_label_attributes(token->next->next, s);
2481 if (s->stmt) {
2482 sparse_error(stmt->pos, "label '%s' redefined", show_ident(s->ident));
2483 // skip the label to avoid multiple definitions
2484 return statement(token, tree);
2486 stmt->type = STMT_LABEL;
2487 stmt->label_identifier = s;
2488 stmt->label_scope = label_scope;
2489 if (s->label_scope) {
2490 if (!is_in_scope(label_scope, s->label_scope))
2491 warn_label_usage(stmt->pos, s->label_pos, s->ident);
2493 s->stmt = stmt;
2494 if (match_op(token, '}')) {
2495 warning(token->pos, "statement expected after label");
2496 stmt->label_statement = alloc_statement(token->pos, STMT_NONE);
2497 return token;
2499 return statement(token, &stmt->label_statement);
2503 if (match_op(token, '{')) {
2504 token = compound_statement(token->next, stmt);
2505 return expect(token, '}', "at end of compound statement");
2508 stmt->type = STMT_EXPRESSION;
2509 return expression_statement(token, &stmt->expression);
2512 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2513 static struct token *label_statement(struct token *token)
2515 while (token_type(token) == TOKEN_IDENT) {
2516 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2517 /* it's block-scope, but we want label namespace */
2518 bind_symbol_with_scope(sym, token->ident, NS_LABEL, block_scope);
2519 fn_local_symbol(sym);
2520 token = token->next;
2521 if (!match_op(token, ','))
2522 break;
2523 token = token->next;
2525 return expect(token, ';', "at end of label declaration");
2528 static struct token * statement_list(struct token *token, struct statement_list **list)
2530 int seen_statement = 0;
2531 while (token_type(token) == TOKEN_IDENT &&
2532 token->ident == &__label___ident)
2533 token = label_statement(token->next);
2534 for (;;) {
2535 struct statement * stmt;
2536 if (eof_token(token))
2537 break;
2538 if (match_op(token, '}'))
2539 break;
2540 if (match_ident(token, &_Static_assert_ident)) {
2541 token = parse_static_assert(token, NULL);
2542 continue;
2544 if (lookup_type(token)) {
2545 if (seen_statement) {
2546 warning(token->pos, "mixing declarations and code");
2547 seen_statement = 0;
2549 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2550 token = external_declaration(token, &stmt->declaration, NULL);
2551 } else {
2552 seen_statement = Wdeclarationafterstatement;
2553 token = statement(token, &stmt);
2555 add_statement(list, stmt);
2557 return token;
2560 static struct token *identifier_list(struct token *token, struct symbol *fn)
2562 struct symbol_list **list = &fn->arguments;
2563 for (;;) {
2564 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2565 sym->ident = token->ident;
2566 token = token->next;
2567 sym->endpos = token->pos;
2568 sym->ctype.base_type = &incomplete_ctype;
2569 add_symbol(list, sym);
2570 if (!match_op(token, ',') ||
2571 token_type(token->next) != TOKEN_IDENT ||
2572 lookup_type(token->next))
2573 break;
2574 token = token->next;
2576 return token;
2579 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2581 struct symbol_list **list = &fn->arguments;
2583 for (;;) {
2584 struct symbol *sym;
2586 if (match_op(token, SPECIAL_ELLIPSIS)) {
2587 fn->variadic = 1;
2588 token = token->next;
2589 break;
2592 sym = alloc_symbol(token->pos, SYM_NODE);
2593 token = parameter_declaration(token, sym);
2594 if (sym->ctype.base_type == &void_ctype) {
2595 /* Special case: (void) */
2596 if (!*list && !sym->ident)
2597 break;
2598 warning(token->pos, "void parameter");
2600 add_symbol(list, sym);
2601 if (!match_op(token, ','))
2602 break;
2603 token = token->next;
2605 return token;
2608 struct token *compound_statement(struct token *token, struct statement *stmt)
2610 stmt->type = STMT_COMPOUND;
2611 start_block_scope(token->pos);
2612 token = statement_list(token, &stmt->stmts);
2613 end_block_scope();
2614 return token;
2617 static struct expression *identifier_expression(struct token *token)
2619 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2620 expr->expr_ident = token->ident;
2621 return expr;
2624 static struct expression *index_expression(struct expression *from, struct expression *to)
2626 int idx_from, idx_to;
2627 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2629 idx_from = const_expression_value(from);
2630 idx_to = idx_from;
2631 if (to) {
2632 idx_to = const_expression_value(to);
2633 if (idx_to < idx_from || idx_from < 0)
2634 warning(from->pos, "nonsense array initializer index range");
2636 expr->idx_from = idx_from;
2637 expr->idx_to = idx_to;
2638 return expr;
2641 static struct token *single_initializer(struct expression **ep, struct token *token)
2643 int expect_equal = 0;
2644 struct token *next = token->next;
2645 struct expression **tail = ep;
2646 int nested;
2648 *ep = NULL;
2650 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2651 struct expression *expr = identifier_expression(token);
2652 if (Wold_initializer)
2653 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2654 token = initializer(&expr->ident_expression, next->next);
2655 if (expr->ident_expression)
2656 *ep = expr;
2657 return token;
2660 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2661 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2662 struct expression *expr = identifier_expression(next);
2663 *tail = expr;
2664 tail = &expr->ident_expression;
2665 expect_equal = 1;
2666 token = next->next;
2667 } else if (match_op(token, '[')) {
2668 struct expression *from = NULL, *to = NULL, *expr;
2669 token = constant_expression(token->next, &from);
2670 if (!from) {
2671 sparse_error(token->pos, "Expected constant expression");
2672 break;
2674 if (match_op(token, SPECIAL_ELLIPSIS))
2675 token = constant_expression(token->next, &to);
2676 expr = index_expression(from, to);
2677 *tail = expr;
2678 tail = &expr->idx_expression;
2679 token = expect(token, ']', "at end of initializer index");
2680 if (nested)
2681 expect_equal = 1;
2682 } else {
2683 break;
2686 if (nested && !expect_equal) {
2687 if (!match_op(token, '='))
2688 warning(token->pos, "obsolete array initializer, use C99 syntax");
2689 else
2690 expect_equal = 1;
2692 if (expect_equal)
2693 token = expect(token, '=', "at end of initializer index");
2695 token = initializer(tail, token);
2696 if (!*tail)
2697 *ep = NULL;
2698 return token;
2701 static struct token *initializer_list(struct expression_list **list, struct token *token)
2703 struct expression *expr;
2705 for (;;) {
2706 token = single_initializer(&expr, token);
2707 if (!expr)
2708 break;
2709 add_expression(list, expr);
2710 if (!match_op(token, ','))
2711 break;
2712 token = token->next;
2714 return token;
2717 struct token *initializer(struct expression **tree, struct token *token)
2719 if (match_op(token, '{')) {
2720 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2721 *tree = expr;
2722 if (!Wuniversal_initializer) {
2723 struct token *next = token->next;
2724 // '{ 0 }' is equivalent to '{ }' except for some
2725 // warnings, like using 0 to initialize a null-pointer.
2726 if (match_token_zero(next)) {
2727 if (match_op(next->next, '}'))
2728 expr->zero_init = 1;
2732 token = initializer_list(&expr->expr_list, token->next);
2733 return expect(token, '}', "at end of initializer");
2735 return assignment_expression(token, tree);
2738 static void declare_argument(struct symbol *sym, struct symbol *fn)
2740 if (!sym->ident) {
2741 sparse_error(sym->pos, "no identifier for function argument");
2742 return;
2744 if (sym->ctype.base_type == &incomplete_ctype) {
2745 sym->ctype.base_type = &int_ctype;
2747 if (Wimplicit_int) {
2748 sparse_error(sym->pos, "missing type declaration for parameter '%s'",
2749 show_ident(sym->ident));
2752 bind_symbol(sym, sym->ident, NS_SYMBOL);
2755 static int is_syscall(struct symbol *sym)
2757 char *macro;
2758 char *name;
2759 int is_syscall = 0;
2761 macro = get_macro_name(sym->pos);
2762 if (macro &&
2763 (strncmp("SYSCALL_DEFINE", macro, strlen("SYSCALL_DEFINE")) == 0 ||
2764 strncmp("COMPAT_SYSCALL_DEFINE", macro, strlen("COMPAT_SYSCALL_DEFINE")) == 0))
2765 is_syscall = 1;
2767 name = sym->ident->name;
2769 if (name && strncmp(name, "sys_", 4) ==0)
2770 is_syscall = 1;
2772 if (name && strncmp(name, "compat_sys_", 11) == 0)
2773 is_syscall = 1;
2775 return is_syscall;
2779 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2780 struct symbol_list **list)
2782 struct symbol_list **old_symbol_list;
2783 struct symbol *base_type = decl->ctype.base_type;
2784 struct statement *stmt, **p;
2785 struct symbol *prev;
2786 struct symbol *arg;
2788 old_symbol_list = function_symbol_list;
2789 if (decl->ctype.modifiers & MOD_INLINE) {
2790 function_symbol_list = &decl->inline_symbol_list;
2791 p = &base_type->inline_stmt;
2792 } else {
2793 function_symbol_list = &decl->symbol_list;
2794 p = &base_type->stmt;
2796 function_computed_target_list = NULL;
2797 function_computed_goto_list = NULL;
2799 if ((decl->ctype.modifiers & (MOD_EXTERN|MOD_INLINE)) == MOD_EXTERN) {
2800 if (Wexternal_function_has_definition)
2801 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2803 if (!(decl->ctype.modifiers & MOD_STATIC))
2804 decl->ctype.modifiers |= MOD_EXTERN;
2806 stmt = start_function(decl);
2807 *p = stmt;
2809 FOR_EACH_PTR (base_type->arguments, arg) {
2810 declare_argument(arg, base_type);
2811 } END_FOR_EACH_PTR(arg);
2813 token = statement_list(token->next, &stmt->stmts);
2814 end_function(decl);
2816 if (!(decl->ctype.modifiers & MOD_INLINE))
2817 add_symbol(list, decl);
2818 else if (is_syscall(decl)) {
2819 add_symbol(list, decl);
2821 printf("parse.c decl: %s\n", decl->ident->name);
2822 char *macro = get_macro_name(decl->pos);
2823 printf("decl macro: %s\n", macro);
2826 check_declaration(decl);
2827 decl->definition = decl;
2828 prev = decl->same_symbol;
2829 if (prev && prev->definition) {
2830 warning(decl->pos, "multiple definitions for function '%s'",
2831 show_ident(decl->ident));
2832 info(prev->definition->pos, " the previous one is here");
2833 } else {
2834 while (prev) {
2835 rebind_scope(prev, decl->scope);
2836 prev->definition = decl;
2837 prev = prev->same_symbol;
2840 function_symbol_list = old_symbol_list;
2841 if (function_computed_goto_list) {
2842 if (!function_computed_target_list)
2843 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2844 else {
2845 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2846 stmt->target_list = function_computed_target_list;
2847 } END_FOR_EACH_PTR(stmt);
2850 return expect(token, '}', "at end of function");
2853 static void promote_k_r_types(struct symbol *arg)
2855 struct symbol *base = arg->ctype.base_type;
2856 if (base && base->ctype.base_type == &int_type && base->rank < 0) {
2857 arg->ctype.base_type = &int_ctype;
2861 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2863 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2864 struct symbol *arg;
2866 FOR_EACH_PTR(real_args, arg) {
2867 struct symbol *type;
2869 /* This is quadratic in the number of arguments. We _really_ don't care */
2870 FOR_EACH_PTR(argtypes, type) {
2871 if (type->ident == arg->ident)
2872 goto match;
2873 } END_FOR_EACH_PTR(type);
2874 if (Wimplicit_int) {
2875 warning(arg->pos, "missing type declaration for parameter '%s'",
2876 show_ident(arg->ident));
2878 type = alloc_symbol(arg->pos, SYM_NODE);
2879 type->ident = arg->ident;
2880 type->ctype.base_type = &int_ctype;
2881 match:
2882 type->used = 1;
2883 /* "char" and "short" promote to "int" */
2884 promote_k_r_types(type);
2886 arg->ctype = type->ctype;
2887 } END_FOR_EACH_PTR(arg);
2889 FOR_EACH_PTR(argtypes, arg) {
2890 if (!arg->used)
2891 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2892 } END_FOR_EACH_PTR(arg);
2896 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2897 struct symbol_list **list)
2899 struct symbol_list *args = NULL;
2901 if (Wold_style_definition)
2902 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2904 do {
2905 token = declaration_list(token, &args);
2906 if (!match_op(token, ';')) {
2907 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2908 break;
2910 token = token->next;
2911 } while (lookup_type(token));
2913 apply_k_r_types(args, decl);
2915 if (!match_op(token, '{')) {
2916 sparse_error(token->pos, "expected function body");
2917 return token;
2919 return parse_function_body(token, decl, list);
2922 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2924 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2925 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2926 struct statement *stmt;
2928 anon->ctype.base_type = fn;
2929 stmt = alloc_statement(token->pos, STMT_NONE);
2930 fn->stmt = stmt;
2932 token = parse_asm_statement(token, stmt);
2934 // FIXME: add_symbol(list, anon);
2935 return token;
2938 struct token *external_declaration(struct token *token, struct symbol_list **list,
2939 validate_decl_t validate_decl)
2941 struct ident *ident = NULL;
2942 struct symbol *decl;
2943 struct decl_state ctx = { .ident = &ident };
2944 struct ctype saved;
2945 struct symbol *base_type;
2946 unsigned long mod;
2947 int is_typedef;
2949 if (match_ident(token, &_Pragma_ident))
2950 return parse_underscore_Pragma(token);
2952 /* Top-level inline asm or static assertion? */
2953 if (token_type(token) == TOKEN_IDENT) {
2954 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2955 if (s && s->op->toplevel)
2956 return s->op->toplevel(token, list);
2959 /* Parse declaration-specifiers, if any */
2960 token = declaration_specifiers(token, &ctx);
2961 mod = decl_modifiers(&ctx);
2962 decl = alloc_symbol(token->pos, SYM_NODE);
2963 /* Just a type declaration? */
2964 if (match_op(token, ';')) {
2965 apply_modifiers(token->pos, &ctx);
2966 return token->next;
2969 saved = ctx.ctype;
2970 token = declarator(token, &ctx);
2971 token = handle_asm_name(token, &ctx);
2972 token = handle_attributes(token, &ctx);
2973 apply_modifiers(token->pos, &ctx);
2975 decl->ctype = ctx.ctype;
2976 decl->ctype.modifiers |= mod;
2977 decl->endpos = token->pos;
2979 /* Just a type declaration? */
2980 if (!ident) {
2981 warning(token->pos, "missing identifier in declaration");
2982 return expect(token, ';', "at the end of type declaration");
2985 /* type define declaration? */
2986 is_typedef = ctx.storage_class == MOD_USERTYPE;
2988 /* Typedefs don't have meaningful storage */
2989 if (is_typedef)
2990 decl->ctype.modifiers |= MOD_USERTYPE;
2992 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2994 base_type = decl->ctype.base_type;
2996 if (is_typedef) {
2997 if (base_type && !base_type->ident) {
2998 switch (base_type->type) {
2999 case SYM_STRUCT:
3000 case SYM_UNION:
3001 case SYM_ENUM:
3002 case SYM_RESTRICT:
3003 base_type->ident = ident;
3004 break;
3005 default:
3006 break;
3009 } else if (base_type && base_type->type == SYM_FN) {
3010 if (base_type->ctype.base_type == &autotype_ctype) {
3011 sparse_error(decl->pos, "'%s()' has __auto_type return type",
3012 show_ident(decl->ident));
3013 base_type->ctype.base_type = &int_ctype;
3015 if (base_type->ctype.base_type == &incomplete_ctype) {
3016 warning(decl->pos, "'%s()' has implicit return type",
3017 show_ident(decl->ident));
3018 base_type->ctype.base_type = &int_ctype;
3020 /* apply attributes placed after the declarator */
3021 decl->ctype.modifiers |= ctx.f_modifiers;
3023 /* K&R argument declaration? */
3024 if (lookup_type(token))
3025 return parse_k_r_arguments(token, decl, list);
3026 if (match_op(token, '{'))
3027 return parse_function_body(token, decl, list);
3029 if (!(decl->ctype.modifiers & MOD_STATIC))
3030 decl->ctype.modifiers |= MOD_EXTERN;
3031 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
3032 sparse_error(token->pos, "void declaration");
3034 if (base_type == &incomplete_ctype) {
3035 warning(decl->pos, "'%s' has implicit type", show_ident(decl->ident));
3036 decl->ctype.base_type = &int_ctype;;
3039 for (;;) {
3040 if (!is_typedef && match_op(token, '=')) {
3041 struct token *next = token->next;
3042 token = initializer(&decl->initializer, next);
3043 if (token == next)
3044 sparse_error(token->pos, "expression expected before '%s'", show_token(token));
3046 if (!is_typedef) {
3047 if (validate_decl)
3048 validate_decl(decl);
3050 if (decl->initializer && decl->ctype.modifiers & MOD_EXTERN) {
3051 warning(decl->pos, "symbol with external linkage has initializer");
3052 decl->ctype.modifiers &= ~MOD_EXTERN;
3055 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
3056 add_symbol(list, decl);
3057 fn_local_symbol(decl);
3060 check_declaration(decl);
3061 if (decl->same_symbol) {
3062 decl->definition = decl->same_symbol->definition;
3063 decl->op = decl->same_symbol->op;
3064 if (is_typedef) {
3065 // TODO: handle -std=c89 --pedantic
3066 check_duplicates(decl);
3070 if (ctx.autotype) {
3071 const char *msg = NULL;
3072 if (decl->ctype.base_type != &autotype_ctype)
3073 msg = "on non-identifier";
3074 else if (match_op(token, ','))
3075 msg = "on declaration list";
3076 else if (!decl->initializer)
3077 msg = "without initializer";
3078 else if (decl->initializer->type == EXPR_SYMBOL &&
3079 decl->initializer->symbol == decl)
3080 msg = "on self-init var";
3081 if (msg) {
3082 sparse_error(decl->pos, "__auto_type %s", msg);
3083 decl->ctype.base_type = &bad_ctype;
3087 if (!match_op(token, ','))
3088 break;
3090 token = token->next;
3091 ident = NULL;
3092 decl = alloc_symbol(token->pos, SYM_NODE);
3093 ctx.ctype = saved;
3094 token = handle_attributes(token, &ctx);
3095 token = declarator(token, &ctx);
3096 token = handle_asm_name(token, &ctx);
3097 token = handle_attributes(token, &ctx);
3098 apply_modifiers(token->pos, &ctx);
3099 decl->ctype = ctx.ctype;
3100 decl->ctype.modifiers |= mod;
3101 decl->endpos = token->pos;
3102 if (!ident) {
3103 sparse_error(token->pos, "expected identifier name in type definition");
3104 return token;
3107 if (is_typedef)
3108 decl->ctype.modifiers |= MOD_USERTYPE;
3110 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
3112 /* Function declarations are automatically extern unless specifically static */
3113 base_type = decl->ctype.base_type;
3114 if (!is_typedef && base_type && base_type->type == SYM_FN) {
3115 if (!(decl->ctype.modifiers & MOD_STATIC))
3116 decl->ctype.modifiers |= MOD_EXTERN;
3119 return expect(token, ';', "at end of declaration");