teach sparse about -m{big,little}-endian
[smatch.git] / parse.c
blob21454790411a307b841c987a97dcc2bf7b0bb798
1 /*
2 * Stupid C parser, version 1e-6.
4 * Let's see how hard this is to do.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003-2004 Linus Torvalds
8 * Copyright (C) 2004 Christopher Li
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <limits.h>
38 #include "lib.h"
39 #include "allocate.h"
40 #include "token.h"
41 #include "parse.h"
42 #include "symbol.h"
43 #include "scope.h"
44 #include "expression.h"
45 #include "target.h"
47 static struct symbol_list **function_symbol_list;
48 struct symbol_list *function_computed_target_list;
49 struct statement_list *function_computed_goto_list;
51 static struct token *statement(struct token *token, struct statement **tree);
52 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords);
54 typedef struct token *declarator_t(struct token *, struct decl_state *);
55 static declarator_t
56 struct_specifier, union_specifier, enum_specifier,
57 attribute_specifier, typeof_specifier, parse_asm_declarator,
58 typedef_specifier, inline_specifier, auto_specifier,
59 register_specifier, static_specifier, extern_specifier,
60 thread_specifier, const_qualifier, volatile_qualifier;
62 static struct token *parse_if_statement(struct token *token, struct statement *stmt);
63 static struct token *parse_return_statement(struct token *token, struct statement *stmt);
64 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt);
65 static struct token *parse_default_statement(struct token *token, struct statement *stmt);
66 static struct token *parse_case_statement(struct token *token, struct statement *stmt);
67 static struct token *parse_switch_statement(struct token *token, struct statement *stmt);
68 static struct token *parse_for_statement(struct token *token, struct statement *stmt);
69 static struct token *parse_while_statement(struct token *token, struct statement *stmt);
70 static struct token *parse_do_statement(struct token *token, struct statement *stmt);
71 static struct token *parse_goto_statement(struct token *token, struct statement *stmt);
72 static struct token *parse_context_statement(struct token *token, struct statement *stmt);
73 static struct token *parse_range_statement(struct token *token, struct statement *stmt);
74 static struct token *parse_asm_statement(struct token *token, struct statement *stmt);
75 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list);
76 static struct token *parse_static_assert(struct token *token, struct symbol_list **unused);
78 typedef struct token *attr_t(struct token *, struct symbol *,
79 struct decl_state *);
81 static attr_t
82 attribute_packed, attribute_aligned, attribute_modifier,
83 attribute_bitwise,
84 attribute_address_space, attribute_context,
85 attribute_designated_init,
86 attribute_transparent_union, ignore_attribute,
87 attribute_mode, attribute_force;
89 typedef struct symbol *to_mode_t(struct symbol *);
91 static to_mode_t
92 to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode, to_word_mode;
94 enum {
95 Set_T = 1,
96 Set_S = 2,
97 Set_Char = 4,
98 Set_Int = 8,
99 Set_Double = 16,
100 Set_Float = 32,
101 Set_Signed = 64,
102 Set_Unsigned = 128,
103 Set_Short = 256,
104 Set_Long = 512,
105 Set_Vlong = 1024,
106 Set_Int128 = 2048,
107 Set_Any = Set_T | Set_Short | Set_Long | Set_Signed | Set_Unsigned
110 enum {
111 CInt = 0, CSInt, CUInt, CReal, CChar, CSChar, CUChar,
114 enum {
115 SNone = 0, STypedef, SAuto, SRegister, SExtern, SStatic, SForced, SMax,
118 static struct symbol_op typedef_op = {
119 .type = KW_MODIFIER,
120 .declarator = typedef_specifier,
123 static struct symbol_op inline_op = {
124 .type = KW_MODIFIER,
125 .declarator = inline_specifier,
128 static declarator_t noreturn_specifier;
129 static struct symbol_op noreturn_op = {
130 .type = KW_MODIFIER,
131 .declarator = noreturn_specifier,
134 static declarator_t alignas_specifier;
135 static struct symbol_op alignas_op = {
136 .type = KW_MODIFIER,
137 .declarator = alignas_specifier,
140 static struct symbol_op auto_op = {
141 .type = KW_MODIFIER,
142 .declarator = auto_specifier,
145 static struct symbol_op register_op = {
146 .type = KW_MODIFIER,
147 .declarator = register_specifier,
150 static struct symbol_op static_op = {
151 .type = KW_MODIFIER,
152 .declarator = static_specifier,
155 static struct symbol_op extern_op = {
156 .type = KW_MODIFIER,
157 .declarator = extern_specifier,
160 static struct symbol_op thread_op = {
161 .type = KW_MODIFIER,
162 .declarator = thread_specifier,
165 static struct symbol_op const_op = {
166 .type = KW_QUALIFIER,
167 .declarator = const_qualifier,
170 static struct symbol_op volatile_op = {
171 .type = KW_QUALIFIER,
172 .declarator = volatile_qualifier,
175 static struct symbol_op restrict_op = {
176 .type = KW_QUALIFIER,
179 static struct symbol_op typeof_op = {
180 .type = KW_SPECIFIER,
181 .declarator = typeof_specifier,
182 .test = Set_Any,
183 .set = Set_S|Set_T,
186 static struct symbol_op attribute_op = {
187 .type = KW_ATTRIBUTE,
188 .declarator = attribute_specifier,
191 static struct symbol_op struct_op = {
192 .type = KW_SPECIFIER,
193 .declarator = struct_specifier,
194 .test = Set_Any,
195 .set = Set_S|Set_T,
198 static struct symbol_op union_op = {
199 .type = KW_SPECIFIER,
200 .declarator = union_specifier,
201 .test = Set_Any,
202 .set = Set_S|Set_T,
205 static struct symbol_op enum_op = {
206 .type = KW_SPECIFIER,
207 .declarator = enum_specifier,
208 .test = Set_Any,
209 .set = Set_S|Set_T,
212 static struct symbol_op spec_op = {
213 .type = KW_SPECIFIER | KW_EXACT,
214 .test = Set_Any,
215 .set = Set_S|Set_T,
218 static struct symbol_op char_op = {
219 .type = KW_SPECIFIER,
220 .test = Set_T|Set_Long|Set_Short,
221 .set = Set_T|Set_Char,
222 .class = CChar,
225 static struct symbol_op int_op = {
226 .type = KW_SPECIFIER,
227 .test = Set_T,
228 .set = Set_T|Set_Int,
231 static struct symbol_op double_op = {
232 .type = KW_SPECIFIER,
233 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
234 .set = Set_T|Set_Double,
235 .class = CReal,
238 static struct symbol_op float_op = {
239 .type = KW_SPECIFIER | KW_SHORT,
240 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long,
241 .set = Set_T|Set_Float,
242 .class = CReal,
245 static struct symbol_op short_op = {
246 .type = KW_SPECIFIER | KW_SHORT,
247 .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short,
248 .set = Set_Short,
251 static struct symbol_op signed_op = {
252 .type = KW_SPECIFIER,
253 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
254 .set = Set_Signed,
255 .class = CSInt,
258 static struct symbol_op unsigned_op = {
259 .type = KW_SPECIFIER,
260 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
261 .set = Set_Unsigned,
262 .class = CUInt,
265 static struct symbol_op long_op = {
266 .type = KW_SPECIFIER | KW_LONG,
267 .test = Set_S|Set_Char|Set_Float|Set_Short|Set_Vlong,
268 .set = Set_Long,
271 static struct symbol_op int128_op = {
272 .type = KW_SPECIFIER | KW_LONG,
273 .test = Set_S|Set_T|Set_Char|Set_Short|Set_Int|Set_Float|Set_Double|Set_Long|Set_Vlong|Set_Int128,
274 .set = Set_T|Set_Int128,
277 static struct symbol_op if_op = {
278 .statement = parse_if_statement,
281 static struct symbol_op return_op = {
282 .statement = parse_return_statement,
285 static struct symbol_op loop_iter_op = {
286 .statement = parse_loop_iterator,
289 static struct symbol_op default_op = {
290 .statement = parse_default_statement,
293 static struct symbol_op case_op = {
294 .statement = parse_case_statement,
297 static struct symbol_op switch_op = {
298 .statement = parse_switch_statement,
301 static struct symbol_op for_op = {
302 .statement = parse_for_statement,
305 static struct symbol_op while_op = {
306 .statement = parse_while_statement,
309 static struct symbol_op do_op = {
310 .statement = parse_do_statement,
313 static struct symbol_op goto_op = {
314 .statement = parse_goto_statement,
317 static struct symbol_op __context___op = {
318 .statement = parse_context_statement,
321 static struct symbol_op range_op = {
322 .statement = parse_range_statement,
325 static struct symbol_op asm_op = {
326 .type = KW_ASM,
327 .declarator = parse_asm_declarator,
328 .statement = parse_asm_statement,
329 .toplevel = toplevel_asm_declaration,
332 static struct symbol_op static_assert_op = {
333 .toplevel = parse_static_assert,
336 static struct symbol_op packed_op = {
337 .attribute = attribute_packed,
340 static struct symbol_op aligned_op = {
341 .attribute = attribute_aligned,
344 static struct symbol_op attr_mod_op = {
345 .attribute = attribute_modifier,
348 static struct symbol_op attr_bitwise_op = {
349 .attribute = attribute_bitwise,
352 static struct symbol_op attr_force_op = {
353 .attribute = attribute_force,
356 static struct symbol_op address_space_op = {
357 .attribute = attribute_address_space,
360 static struct symbol_op mode_op = {
361 .attribute = attribute_mode,
364 static struct symbol_op context_op = {
365 .attribute = attribute_context,
368 static struct symbol_op designated_init_op = {
369 .attribute = attribute_designated_init,
372 static struct symbol_op transparent_union_op = {
373 .attribute = attribute_transparent_union,
376 static struct symbol_op ignore_attr_op = {
377 .attribute = ignore_attribute,
380 static struct symbol_op mode_QI_op = {
381 .type = KW_MODE,
382 .to_mode = to_QI_mode
385 static struct symbol_op mode_HI_op = {
386 .type = KW_MODE,
387 .to_mode = to_HI_mode
390 static struct symbol_op mode_SI_op = {
391 .type = KW_MODE,
392 .to_mode = to_SI_mode
395 static struct symbol_op mode_DI_op = {
396 .type = KW_MODE,
397 .to_mode = to_DI_mode
400 static struct symbol_op mode_TI_op = {
401 .type = KW_MODE,
402 .to_mode = to_TI_mode
405 static struct symbol_op mode_word_op = {
406 .type = KW_MODE,
407 .to_mode = to_word_mode
410 /* Using NS_TYPEDEF will also make the keyword a reserved one */
411 static struct init_keyword {
412 const char *name;
413 enum namespace ns;
414 unsigned long modifiers;
415 struct symbol_op *op;
416 struct symbol *type;
417 } keyword_table[] = {
418 /* Type qualifiers */
419 { "const", NS_TYPEDEF, .op = &const_op },
420 { "__const", NS_TYPEDEF, .op = &const_op },
421 { "__const__", NS_TYPEDEF, .op = &const_op },
422 { "volatile", NS_TYPEDEF, .op = &volatile_op },
423 { "__volatile", NS_TYPEDEF, .op = &volatile_op },
424 { "__volatile__", NS_TYPEDEF, .op = &volatile_op },
426 /* Typedef.. */
427 { "typedef", NS_TYPEDEF, .op = &typedef_op },
429 /* Type specifiers */
430 { "void", NS_TYPEDEF, .type = &void_ctype, .op = &spec_op},
431 { "char", NS_TYPEDEF, .op = &char_op },
432 { "short", NS_TYPEDEF, .op = &short_op },
433 { "int", NS_TYPEDEF, .op = &int_op },
434 { "long", NS_TYPEDEF, .op = &long_op },
435 { "float", NS_TYPEDEF, .op = &float_op },
436 { "double", NS_TYPEDEF, .op = &double_op },
437 { "signed", NS_TYPEDEF, .op = &signed_op },
438 { "__signed", NS_TYPEDEF, .op = &signed_op },
439 { "__signed__", NS_TYPEDEF, .op = &signed_op },
440 { "unsigned", NS_TYPEDEF, .op = &unsigned_op },
441 { "__int128", NS_TYPEDEF, .op = &int128_op },
442 { "_Bool", NS_TYPEDEF, .type = &bool_ctype, .op = &spec_op },
444 /* Predeclared types */
445 { "__builtin_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
446 { "__builtin_ms_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
447 { "__int128_t", NS_TYPEDEF, .type = &lllong_ctype, .op = &spec_op },
448 { "__uint128_t",NS_TYPEDEF, .type = &ulllong_ctype, .op = &spec_op },
450 /* Extended types */
451 { "typeof", NS_TYPEDEF, .op = &typeof_op },
452 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
453 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
455 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
456 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
458 { "struct", NS_TYPEDEF, .op = &struct_op },
459 { "union", NS_TYPEDEF, .op = &union_op },
460 { "enum", NS_TYPEDEF, .op = &enum_op },
462 { "inline", NS_TYPEDEF, .op = &inline_op },
463 { "__inline", NS_TYPEDEF, .op = &inline_op },
464 { "__inline__", NS_TYPEDEF, .op = &inline_op },
466 { "_Noreturn", NS_TYPEDEF, .op = &noreturn_op },
468 { "_Alignas", NS_TYPEDEF, .op = &alignas_op },
470 /* Ignored for now.. */
471 { "restrict", NS_TYPEDEF, .op = &restrict_op},
472 { "__restrict", NS_TYPEDEF, .op = &restrict_op},
473 { "__restrict__", NS_TYPEDEF, .op = &restrict_op},
475 /* Static assertion */
476 { "_Static_assert", NS_KEYWORD, .op = &static_assert_op },
478 /* Storage class */
479 { "auto", NS_TYPEDEF, .op = &auto_op },
480 { "register", NS_TYPEDEF, .op = &register_op },
481 { "static", NS_TYPEDEF, .op = &static_op },
482 { "extern", NS_TYPEDEF, .op = &extern_op },
483 { "__thread", NS_TYPEDEF, .op = &thread_op },
484 { "_Thread_local", NS_TYPEDEF, .op = &thread_op },
486 /* Statement */
487 { "if", NS_KEYWORD, .op = &if_op },
488 { "return", NS_KEYWORD, .op = &return_op },
489 { "break", NS_KEYWORD, .op = &loop_iter_op },
490 { "continue", NS_KEYWORD, .op = &loop_iter_op },
491 { "default", NS_KEYWORD, .op = &default_op },
492 { "case", NS_KEYWORD, .op = &case_op },
493 { "switch", NS_KEYWORD, .op = &switch_op },
494 { "for", NS_KEYWORD, .op = &for_op },
495 { "while", NS_KEYWORD, .op = &while_op },
496 { "do", NS_KEYWORD, .op = &do_op },
497 { "goto", NS_KEYWORD, .op = &goto_op },
498 { "__context__",NS_KEYWORD, .op = &__context___op },
499 { "__range__", NS_KEYWORD, .op = &range_op },
500 { "asm", NS_KEYWORD, .op = &asm_op },
501 { "__asm", NS_KEYWORD, .op = &asm_op },
502 { "__asm__", NS_KEYWORD, .op = &asm_op },
504 /* Attribute */
505 { "packed", NS_KEYWORD, .op = &packed_op },
506 { "__packed__", NS_KEYWORD, .op = &packed_op },
507 { "aligned", NS_KEYWORD, .op = &aligned_op },
508 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
509 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
510 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
511 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
512 { "force", NS_KEYWORD, .op = &attr_force_op },
513 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_bitwise_op },
514 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_bitwise_op },
515 { "address_space",NS_KEYWORD, .op = &address_space_op },
516 { "mode", NS_KEYWORD, .op = &mode_op },
517 { "context", NS_KEYWORD, .op = &context_op },
518 { "designated_init", NS_KEYWORD, .op = &designated_init_op },
519 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
520 { "noreturn", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
521 { "__noreturn__", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
522 { "pure", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
523 {"__pure__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
524 {"const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
525 {"__const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
526 {"__const__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
528 { "__mode__", NS_KEYWORD, .op = &mode_op },
529 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
530 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
531 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
532 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
533 { "SI", NS_KEYWORD, .op = &mode_SI_op },
534 { "__SI__", NS_KEYWORD, .op = &mode_SI_op },
535 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
536 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
537 { "TI", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
538 { "__TI__", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
539 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
540 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
543 static const char *ignored_attributes[] = {
544 "alias",
545 "__alias__",
546 "alloc_align",
547 "__alloc_align__",
548 "alloc_size",
549 "__alloc_size__",
550 "always_inline",
551 "__always_inline__",
552 "artificial",
553 "__artificial__",
554 "assume_aligned",
555 "__assume_aligned__",
556 "bounded",
557 "__bounded__",
558 "cdecl",
559 "__cdecl__",
560 "cold",
561 "__cold__",
562 "constructor",
563 "__constructor__",
564 "deprecated",
565 "__deprecated__",
566 "destructor",
567 "__destructor__",
568 "dllexport",
569 "__dllexport__",
570 "dllimport",
571 "__dllimport__",
572 "error",
573 "__error__",
574 "externally_visible",
575 "__externally_visible__",
576 "fastcall",
577 "__fastcall__",
578 "format",
579 "__format__",
580 "format_arg",
581 "__format_arg__",
582 "gnu_inline",
583 "__gnu_inline__",
584 "hot",
585 "__hot__",
586 "hotpatch",
587 "__hotpatch__",
588 "leaf",
589 "__leaf__",
590 "l1_text",
591 "__l1_text__",
592 "l1_data",
593 "__l1_data__",
594 "l2",
595 "__l2__",
596 "malloc",
597 "__malloc__",
598 "may_alias",
599 "__may_alias__",
600 "model",
601 "__model__",
602 "ms_abi",
603 "__ms_abi__",
604 "ms_hook_prologue",
605 "__ms_hook_prologue__",
606 "naked",
607 "__naked__",
608 "no_instrument_function",
609 "__no_instrument_function__",
610 "noclone",
611 "__noclone",
612 "__noclone__",
613 "noinline",
614 "__noinline__",
615 "nonnull",
616 "__nonnull",
617 "__nonnull__",
618 "nothrow",
619 "__nothrow",
620 "__nothrow__",
621 "regparm",
622 "__regparm__",
623 "section",
624 "__section__",
625 "sentinel",
626 "__sentinel__",
627 "signal",
628 "__signal__",
629 "stdcall",
630 "__stdcall__",
631 "syscall_linkage",
632 "__syscall_linkage__",
633 "sysv_abi",
634 "__sysv_abi__",
635 "unused",
636 "__unused__",
637 "used",
638 "__used__",
639 "vector_size",
640 "__vector_size__",
641 "visibility",
642 "__visibility__",
643 "warn_unused_result",
644 "__warn_unused_result__",
645 "warning",
646 "__warning__",
647 "weak",
648 "__weak__",
649 "no_sanitize_address",
650 "__no_sanitize_address__",
654 void init_parser(int stream)
656 int i;
657 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
658 struct init_keyword *ptr = keyword_table + i;
659 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
660 sym->ident->keyword = 1;
661 if (ptr->ns == NS_TYPEDEF)
662 sym->ident->reserved = 1;
663 sym->ctype.modifiers = ptr->modifiers;
664 sym->ctype.base_type = ptr->type;
665 sym->op = ptr->op;
668 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
669 const char * name = ignored_attributes[i];
670 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
671 NS_KEYWORD);
672 sym->ident->keyword = 1;
673 sym->op = &ignore_attr_op;
678 // Add a symbol to the list of function-local symbols
679 static void fn_local_symbol(struct symbol *sym)
681 if (function_symbol_list)
682 add_symbol(function_symbol_list, sym);
685 static int SENTINEL_ATTR match_idents(struct token *token, ...)
687 va_list args;
688 struct ident * next;
690 if (token_type(token) != TOKEN_IDENT)
691 return 0;
693 va_start(args, token);
694 do {
695 next = va_arg(args, struct ident *);
696 } while (next && token->ident != next);
697 va_end(args);
699 return next && token->ident == next;
703 struct statement *alloc_statement(struct position pos, int type)
705 struct statement *stmt = __alloc_statement(0);
706 stmt->type = type;
707 stmt->pos = pos;
708 return stmt;
711 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
713 static void apply_modifiers(struct position pos, struct decl_state *ctx)
715 struct symbol *ctype;
716 if (!ctx->mode)
717 return;
718 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
719 if (!ctype)
720 sparse_error(pos, "don't know how to apply mode to %s",
721 show_typename(ctx->ctype.base_type));
722 else
723 ctx->ctype.base_type = ctype;
727 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
729 struct symbol *sym = alloc_symbol(pos, type);
731 sym->ctype.base_type = ctype->base_type;
732 sym->ctype.modifiers = ctype->modifiers;
734 ctype->base_type = sym;
735 ctype->modifiers = 0;
736 return sym;
740 * NOTE! NS_LABEL is not just a different namespace,
741 * it also ends up using function scope instead of the
742 * regular symbol scope.
744 struct symbol *label_symbol(struct token *token)
746 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
747 if (!sym) {
748 sym = alloc_symbol(token->pos, SYM_LABEL);
749 bind_symbol(sym, token->ident, NS_LABEL);
750 fn_local_symbol(sym);
752 return sym;
755 static struct token *struct_union_enum_specifier(enum type type,
756 struct token *token, struct decl_state *ctx,
757 struct token *(*parse)(struct token *, struct symbol *))
759 struct symbol *sym;
760 struct position *repos;
762 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
763 if (token_type(token) == TOKEN_IDENT) {
764 sym = lookup_symbol(token->ident, NS_STRUCT);
765 if (!sym ||
766 (is_outer_scope(sym->scope) &&
767 (match_op(token->next,';') || match_op(token->next,'{')))) {
768 // Either a new symbol, or else an out-of-scope
769 // symbol being redefined.
770 sym = alloc_symbol(token->pos, type);
771 bind_symbol(sym, token->ident, NS_STRUCT);
773 if (sym->type != type)
774 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
775 ctx->ctype.base_type = sym;
776 repos = &token->pos;
777 token = token->next;
778 if (match_op(token, '{')) {
779 // The following test is actually wrong for empty
780 // structs, but (1) they are not C99, (2) gcc does
781 // the same thing, and (3) it's easier.
782 if (sym->symbol_list)
783 error_die(token->pos, "redefinition of %s", show_typename (sym));
784 sym->pos = *repos;
785 token = parse(token->next, sym);
786 token = expect(token, '}', "at end of struct-union-enum-specifier");
788 // Mark the structure as needing re-examination
789 sym->examined = 0;
790 sym->endpos = token->pos;
792 return token;
795 // private struct/union/enum type
796 if (!match_op(token, '{')) {
797 sparse_error(token->pos, "expected declaration");
798 ctx->ctype.base_type = &bad_ctype;
799 return token;
802 sym = alloc_symbol(token->pos, type);
803 token = parse(token->next, sym);
804 ctx->ctype.base_type = sym;
805 token = expect(token, '}', "at end of specifier");
806 sym->endpos = token->pos;
808 return token;
811 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
813 struct symbol *field, *last = NULL;
814 struct token *res;
815 res = struct_declaration_list(token, &sym->symbol_list);
816 FOR_EACH_PTR(sym->symbol_list, field) {
817 if (!field->ident) {
818 struct symbol *base = field->ctype.base_type;
819 if (base && base->type == SYM_BITFIELD)
820 continue;
822 if (last)
823 last->next_subobject = field;
824 last = field;
825 } END_FOR_EACH_PTR(field);
826 return res;
829 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
831 return struct_declaration_list(token, &sym->symbol_list);
834 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
836 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
839 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
841 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
845 typedef struct {
846 int x;
847 unsigned long long y;
848 } Num;
850 static void upper_boundary(Num *n, Num *v)
852 if (n->x > v->x)
853 return;
854 if (n->x < v->x) {
855 *n = *v;
856 return;
858 if (n->y < v->y)
859 n->y = v->y;
862 static void lower_boundary(Num *n, Num *v)
864 if (n->x < v->x)
865 return;
866 if (n->x > v->x) {
867 *n = *v;
868 return;
870 if (n->y > v->y)
871 n->y = v->y;
874 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
876 int shift = type->bit_size;
877 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
879 if (!is_unsigned)
880 shift--;
881 if (upper->x == 0 && upper->y >> shift)
882 return 0;
883 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
884 return 1;
885 return 0;
888 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
890 if (s1->bit_size < s2->bit_size) {
891 s1 = s2;
892 } else if (s1->bit_size == s2->bit_size) {
893 if (s2->ctype.modifiers & MOD_UNSIGNED)
894 s1 = s2;
896 if (s1->bit_size < bits_in_int)
897 return &int_ctype;
898 return s1;
901 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
903 struct symbol *sym;
905 FOR_EACH_PTR(list, sym) {
906 struct expression *expr = sym->initializer;
907 struct symbol *ctype;
908 if (expr->type != EXPR_VALUE)
909 continue;
910 ctype = expr->ctype;
911 if (ctype->bit_size == base_type->bit_size)
912 continue;
913 cast_value(expr, base_type, expr, ctype);
914 } END_FOR_EACH_PTR(sym);
917 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
919 unsigned long long lastval = 0;
920 struct symbol *ctype = NULL, *base_type = NULL;
921 Num upper = {-1, 0}, lower = {1, 0};
923 parent->examined = 1;
924 parent->ctype.base_type = &int_ctype;
925 while (token_type(token) == TOKEN_IDENT) {
926 struct expression *expr = NULL;
927 struct token *next = token->next;
928 struct symbol *sym;
930 if (match_op(next, '=')) {
931 next = constant_expression(next->next, &expr);
932 lastval = get_expression_value(expr);
933 ctype = &void_ctype;
934 if (expr && expr->ctype)
935 ctype = expr->ctype;
936 } else if (!ctype) {
937 ctype = &int_ctype;
938 } else if (is_int_type(ctype)) {
939 lastval++;
940 } else {
941 error_die(token->pos, "can't increment the last enum member");
944 if (!expr) {
945 expr = alloc_expression(token->pos, EXPR_VALUE);
946 expr->value = lastval;
947 expr->ctype = ctype;
950 sym = alloc_symbol(token->pos, SYM_NODE);
951 bind_symbol(sym, token->ident, NS_SYMBOL);
952 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
953 sym->initializer = expr;
954 sym->enum_member = 1;
955 sym->ctype.base_type = parent;
956 add_ptr_list(&parent->symbol_list, sym);
958 if (base_type != &bad_ctype) {
959 if (ctype->type == SYM_NODE)
960 ctype = ctype->ctype.base_type;
961 if (ctype->type == SYM_ENUM) {
962 if (ctype == parent)
963 ctype = base_type;
964 else
965 ctype = ctype->ctype.base_type;
968 * base_type rules:
969 * - if all enums are of the same type, then
970 * the base_type is that type (two first
971 * cases)
972 * - if enums are of different types, they
973 * all have to be integer types, and the
974 * base type is at least "int_ctype".
975 * - otherwise the base_type is "bad_ctype".
977 if (!base_type) {
978 base_type = ctype;
979 } else if (ctype == base_type) {
980 /* nothing */
981 } else if (is_int_type(base_type) && is_int_type(ctype)) {
982 base_type = bigger_enum_type(base_type, ctype);
983 } else
984 base_type = &bad_ctype;
985 parent->ctype.base_type = base_type;
987 if (is_int_type(base_type)) {
988 Num v = {.y = lastval};
989 if (ctype->ctype.modifiers & MOD_UNSIGNED)
990 v.x = 0;
991 else if ((long long)lastval >= 0)
992 v.x = 0;
993 else
994 v.x = -1;
995 upper_boundary(&upper, &v);
996 lower_boundary(&lower, &v);
998 token = next;
1000 sym->endpos = token->pos;
1002 if (!match_op(token, ','))
1003 break;
1004 token = token->next;
1006 if (!base_type) {
1007 sparse_error(token->pos, "bad enum definition");
1008 base_type = &bad_ctype;
1010 else if (!is_int_type(base_type))
1011 base_type = base_type;
1012 else if (type_is_ok(base_type, &upper, &lower))
1013 base_type = base_type;
1014 else if (type_is_ok(&int_ctype, &upper, &lower))
1015 base_type = &int_ctype;
1016 else if (type_is_ok(&uint_ctype, &upper, &lower))
1017 base_type = &uint_ctype;
1018 else if (type_is_ok(&long_ctype, &upper, &lower))
1019 base_type = &long_ctype;
1020 else if (type_is_ok(&ulong_ctype, &upper, &lower))
1021 base_type = &ulong_ctype;
1022 else if (type_is_ok(&llong_ctype, &upper, &lower))
1023 base_type = &llong_ctype;
1024 else if (type_is_ok(&ullong_ctype, &upper, &lower))
1025 base_type = &ullong_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 cast_enum_list(parent->symbol_list, base_type);
1034 return token;
1037 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
1039 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
1040 struct ctype *ctype = &ctx->ctype.base_type->ctype;
1042 if (!ctype->base_type)
1043 ctype->base_type = &incomplete_ctype;
1045 return ret;
1048 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
1050 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
1052 struct symbol *sym;
1054 if (!match_op(token, '(')) {
1055 sparse_error(token->pos, "expected '(' after typeof");
1056 return token;
1058 if (lookup_type(token->next)) {
1059 token = typename(token->next, &sym, NULL);
1060 ctx->ctype.base_type = sym->ctype.base_type;
1061 apply_ctype(token->pos, &sym->ctype, &ctx->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 *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1078 struct expression *expr = NULL;
1079 if (match_op(token, '('))
1080 token = parens_expression(token, &expr, "in attribute");
1081 return token;
1084 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1086 if (!ctx->ctype.alignment)
1087 ctx->ctype.alignment = 1;
1088 return token;
1091 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1093 int alignment = max_alignment;
1094 struct expression *expr = NULL;
1096 if (match_op(token, '(')) {
1097 token = parens_expression(token, &expr, "in attribute");
1098 if (expr)
1099 alignment = const_expression_value(expr);
1101 if (alignment & (alignment-1)) {
1102 warning(token->pos, "I don't like non-power-of-2 alignments");
1103 return token;
1104 } else if (alignment > ctx->ctype.alignment)
1105 ctx->ctype.alignment = alignment;
1106 return token;
1109 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1111 if (ctx->modifiers & qual)
1112 warning(*pos, "duplicate %s", modifier_string(qual));
1113 ctx->modifiers |= qual;
1116 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1118 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1119 return token;
1122 static struct token *attribute_bitwise(struct token *token, struct symbol *attr, struct decl_state *ctx)
1124 if (Wbitwise)
1125 attribute_modifier(token, attr, ctx);
1126 return token;
1129 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1131 struct expression *expr = NULL;
1132 int as;
1133 token = expect(token, '(', "after address_space attribute");
1134 token = conditional_expression(token, &expr);
1135 if (expr) {
1136 as = const_expression_value(expr);
1137 if (Waddress_space && as)
1138 ctx->ctype.as = as;
1140 token = expect(token, ')', "after address_space attribute");
1141 return token;
1144 static struct symbol *to_QI_mode(struct symbol *ctype)
1146 if (ctype->ctype.base_type != &int_type)
1147 return NULL;
1148 if (ctype == &char_ctype)
1149 return ctype;
1150 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1151 : &schar_ctype;
1154 static struct symbol *to_HI_mode(struct symbol *ctype)
1156 if (ctype->ctype.base_type != &int_type)
1157 return NULL;
1158 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1159 : &sshort_ctype;
1162 static struct symbol *to_SI_mode(struct symbol *ctype)
1164 if (ctype->ctype.base_type != &int_type)
1165 return NULL;
1166 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1167 : &sint_ctype;
1170 static struct symbol *to_DI_mode(struct symbol *ctype)
1172 if (ctype->ctype.base_type != &int_type)
1173 return NULL;
1174 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1175 : &sllong_ctype;
1178 static struct symbol *to_TI_mode(struct symbol *ctype)
1180 if (ctype->ctype.base_type != &int_type)
1181 return NULL;
1182 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1183 : &slllong_ctype;
1186 static struct symbol *to_word_mode(struct symbol *ctype)
1188 if (ctype->ctype.base_type != &int_type)
1189 return NULL;
1190 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1191 : &slong_ctype;
1194 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1196 token = expect(token, '(', "after mode attribute");
1197 if (token_type(token) == TOKEN_IDENT) {
1198 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1199 if (mode && mode->op->type == KW_MODE)
1200 ctx->mode = mode->op;
1201 else
1202 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1203 token = token->next;
1204 } else
1205 sparse_error(token->pos, "expect attribute mode symbol\n");
1206 token = expect(token, ')', "after mode attribute");
1207 return token;
1210 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1212 struct context *context = alloc_context();
1213 struct expression *args[3];
1214 int argc = 0;
1216 token = expect(token, '(', "after context attribute");
1217 while (!match_op(token, ')')) {
1218 struct expression *expr = NULL;
1219 token = conditional_expression(token, &expr);
1220 if (!expr)
1221 break;
1222 if (argc < 3)
1223 args[argc++] = expr;
1224 if (!match_op(token, ','))
1225 break;
1226 token = token->next;
1229 switch(argc) {
1230 case 0:
1231 sparse_error(token->pos, "expected context input/output values");
1232 break;
1233 case 1:
1234 context->in = get_expression_value(args[0]);
1235 break;
1236 case 2:
1237 context->in = get_expression_value(args[0]);
1238 context->out = get_expression_value(args[1]);
1239 break;
1240 case 3:
1241 context->context = args[0];
1242 context->in = get_expression_value(args[1]);
1243 context->out = get_expression_value(args[2]);
1244 break;
1247 if (argc)
1248 add_ptr_list(&ctx->ctype.contexts, context);
1250 token = expect(token, ')', "after context attribute");
1251 return token;
1254 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1256 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1257 ctx->ctype.base_type->designated_init = 1;
1258 else
1259 warning(token->pos, "attribute designated_init applied to non-structure type");
1260 return token;
1263 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1265 if (Wtransparent_union)
1266 warning(token->pos, "attribute __transparent_union__");
1268 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_UNION)
1269 ctx->ctype.base_type->transparent_union = 1;
1270 else
1271 warning(token->pos, "attribute __transparent_union__ applied to non-union type");
1272 return token;
1275 static struct token *recover_unknown_attribute(struct token *token)
1277 struct expression *expr = NULL;
1279 if (Wunknown_attribute)
1280 warning(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1281 token = token->next;
1282 if (match_op(token, '('))
1283 token = parens_expression(token, &expr, "in attribute");
1284 return token;
1287 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1289 token = expect(token, '(', "after attribute");
1290 token = expect(token, '(', "after attribute");
1292 for (;;) {
1293 struct ident *attribute_name;
1294 struct symbol *attr;
1296 if (eof_token(token))
1297 break;
1298 if (match_op(token, ';'))
1299 break;
1300 if (token_type(token) != TOKEN_IDENT)
1301 break;
1302 attribute_name = token->ident;
1303 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1304 if (attr && attr->op->attribute)
1305 token = attr->op->attribute(token->next, attr, ctx);
1306 else
1307 token = recover_unknown_attribute(token);
1309 if (!match_op(token, ','))
1310 break;
1311 token = token->next;
1314 token = expect(token, ')', "after attribute");
1315 token = expect(token, ')', "after attribute");
1316 return token;
1319 static const char *storage_class[] =
1321 [STypedef] = "typedef",
1322 [SAuto] = "auto",
1323 [SExtern] = "extern",
1324 [SStatic] = "static",
1325 [SRegister] = "register",
1326 [SForced] = "[force]"
1329 static unsigned long storage_modifiers(struct decl_state *ctx)
1331 static unsigned long mod[SMax] =
1333 [SAuto] = MOD_AUTO,
1334 [SExtern] = MOD_EXTERN,
1335 [SStatic] = MOD_STATIC,
1336 [SRegister] = MOD_REGISTER
1338 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1339 | (ctx->is_tls ? MOD_TLS : 0);
1342 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1344 /* __thread can be used alone, or with extern or static */
1345 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1346 sparse_error(*pos, "__thread can only be used alone, or with "
1347 "extern or static");
1348 return;
1351 if (!ctx->storage_class) {
1352 ctx->storage_class = class;
1353 return;
1355 if (ctx->storage_class == class)
1356 sparse_error(*pos, "duplicate %s", storage_class[class]);
1357 else
1358 sparse_error(*pos, "multiple storage classes");
1361 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1363 set_storage_class(&next->pos, ctx, STypedef);
1364 return next;
1367 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1369 set_storage_class(&next->pos, ctx, SAuto);
1370 return next;
1373 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1375 set_storage_class(&next->pos, ctx, SRegister);
1376 return next;
1379 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1381 set_storage_class(&next->pos, ctx, SStatic);
1382 return next;
1385 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1387 set_storage_class(&next->pos, ctx, SExtern);
1388 return next;
1391 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1393 /* This GCC extension can be used alone, or with extern or static */
1394 if (!ctx->storage_class || ctx->storage_class == SStatic
1395 || ctx->storage_class == SExtern) {
1396 ctx->is_tls = 1;
1397 } else {
1398 sparse_error(next->pos, "__thread can only be used alone, or "
1399 "with extern or static");
1402 return next;
1405 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1407 set_storage_class(&token->pos, ctx, SForced);
1408 return token;
1411 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1413 ctx->is_inline = 1;
1414 return next;
1417 static struct token *noreturn_specifier(struct token *next, struct decl_state *ctx)
1419 apply_qualifier(&next->pos, &ctx->ctype, MOD_NORETURN);
1420 return next;
1423 static struct token *alignas_specifier(struct token *token, struct decl_state *ctx)
1425 int alignment = 0;
1427 if (!match_op(token, '(')) {
1428 sparse_error(token->pos, "expected '(' after _Alignas");
1429 return token;
1431 if (lookup_type(token->next)) {
1432 struct symbol *sym = NULL;
1433 token = typename(token->next, &sym, NULL);
1434 sym = examine_symbol_type(sym);
1435 alignment = sym->ctype.alignment;
1436 token = expect(token, ')', "after _Alignas(...");
1437 } else {
1438 struct expression *expr = NULL;
1439 token = parens_expression(token, &expr, "after _Alignas");
1440 if (!expr)
1441 return token;
1442 alignment = const_expression_value(expr);
1445 if (alignment < 0) {
1446 warning(token->pos, "non-positive alignment");
1447 return token;
1449 if (alignment & (alignment-1)) {
1450 warning(token->pos, "non-power-of-2 alignment");
1451 return token;
1453 if (alignment > ctx->ctype.alignment)
1454 ctx->ctype.alignment = alignment;
1455 return token;
1458 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1460 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1461 return next;
1464 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1466 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1467 return next;
1470 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1472 unsigned long mod = thistype->modifiers;
1474 if (mod)
1475 apply_qualifier(&pos, ctype, mod);
1477 /* Context */
1478 concat_ptr_list((struct ptr_list *)thistype->contexts,
1479 (struct ptr_list **)&ctype->contexts);
1481 /* Alignment */
1482 if (thistype->alignment > ctype->alignment)
1483 ctype->alignment = thistype->alignment;
1485 /* Address space */
1486 if (thistype->as)
1487 ctype->as = thistype->as;
1490 static void specifier_conflict(struct position pos, int what, struct ident *new)
1492 const char *old;
1493 if (what & (Set_S | Set_T))
1494 goto Catch_all;
1495 if (what & Set_Char)
1496 old = "char";
1497 else if (what & Set_Double)
1498 old = "double";
1499 else if (what & Set_Float)
1500 old = "float";
1501 else if (what & Set_Signed)
1502 old = "signed";
1503 else if (what & Set_Unsigned)
1504 old = "unsigned";
1505 else if (what & Set_Short)
1506 old = "short";
1507 else if (what & Set_Long)
1508 old = "long";
1509 else
1510 old = "long long";
1511 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1512 old, show_ident(new));
1513 return;
1515 Catch_all:
1516 sparse_error(pos, "two or more data types in declaration specifiers");
1519 static struct symbol * const int_types[] =
1520 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype, &lllong_ctype};
1521 static struct symbol * const signed_types[] =
1522 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1523 &slllong_ctype};
1524 static struct symbol * const unsigned_types[] =
1525 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1526 &ulllong_ctype};
1527 static struct symbol * const real_types[] =
1528 {&float_ctype, &double_ctype, &ldouble_ctype};
1529 static struct symbol * const char_types[] =
1530 {&char_ctype, &schar_ctype, &uchar_ctype};
1531 static struct symbol * const * const types[] = {
1532 int_types + 1, signed_types + 1, unsigned_types + 1,
1533 real_types + 1, char_types, char_types + 1, char_types + 2
1536 struct symbol *ctype_integer(int size, int want_unsigned)
1538 return types[want_unsigned ? CUInt : CInt][size];
1541 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1543 while (token_type(t) == TOKEN_IDENT) {
1544 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1545 if (!s)
1546 break;
1547 if (s->type != SYM_KEYWORD)
1548 break;
1549 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1550 break;
1551 t = t->next;
1552 if (s->op->declarator)
1553 t = s->op->declarator(t, ctx);
1555 return t;
1558 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1560 int seen = 0;
1561 int class = CInt;
1562 int size = 0;
1564 while (token_type(token) == TOKEN_IDENT) {
1565 struct symbol *s = lookup_symbol(token->ident,
1566 NS_TYPEDEF | NS_SYMBOL);
1567 if (!s || !(s->namespace & NS_TYPEDEF))
1568 break;
1569 if (s->type != SYM_KEYWORD) {
1570 if (seen & Set_Any)
1571 break;
1572 seen |= Set_S | Set_T;
1573 ctx->ctype.base_type = s->ctype.base_type;
1574 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1575 token = token->next;
1576 continue;
1578 if (s->op->type & KW_SPECIFIER) {
1579 if (seen & s->op->test) {
1580 specifier_conflict(token->pos,
1581 seen & s->op->test,
1582 token->ident);
1583 break;
1585 seen |= s->op->set;
1586 class += s->op->class;
1587 if (s->op->set & Set_Int128)
1588 size = 2;
1589 if (s->op->type & KW_SHORT) {
1590 size = -1;
1591 } else if (s->op->type & KW_LONG && size++) {
1592 if (class == CReal) {
1593 specifier_conflict(token->pos,
1594 Set_Vlong,
1595 &double_ident);
1596 break;
1598 seen |= Set_Vlong;
1601 token = token->next;
1602 if (s->op->declarator)
1603 token = s->op->declarator(token, ctx);
1604 if (s->op->type & KW_EXACT) {
1605 ctx->ctype.base_type = s->ctype.base_type;
1606 ctx->ctype.modifiers |= s->ctype.modifiers;
1610 if (!(seen & Set_S)) { /* not set explicitly? */
1611 struct symbol *base = &incomplete_ctype;
1612 if (seen & Set_Any)
1613 base = types[class][size];
1614 ctx->ctype.base_type = base;
1617 if (ctx->ctype.modifiers & MOD_BITWISE) {
1618 struct symbol *type;
1619 ctx->ctype.modifiers &= ~MOD_BITWISE;
1620 if (!is_int_type(ctx->ctype.base_type)) {
1621 sparse_error(token->pos, "invalid modifier");
1622 return token;
1624 type = alloc_symbol(token->pos, SYM_BASETYPE);
1625 *type = *ctx->ctype.base_type;
1626 type->ctype.modifiers &= ~MOD_SPECIFIER;
1627 type->ctype.base_type = ctx->ctype.base_type;
1628 type->type = SYM_RESTRICT;
1629 ctx->ctype.base_type = type;
1630 create_fouled(type);
1632 return token;
1635 static struct token *abstract_array_static_declarator(struct token *token, int *has_static)
1637 while (token->ident == &static_ident) {
1638 if (*has_static)
1639 sparse_error(token->pos, "duplicate array static declarator");
1641 *has_static = 1;
1642 token = token->next;
1644 return token;
1648 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1650 struct expression *expr = NULL;
1651 int has_static = 0;
1653 token = abstract_array_static_declarator(token, &has_static);
1655 if (match_idents(token, &restrict_ident, &__restrict_ident, &__restrict___ident, NULL))
1656 token = abstract_array_static_declarator(token->next, &has_static);
1657 token = parse_expression(token, &expr);
1658 sym->array_size = expr;
1659 return token;
1662 static struct token *parameter_type_list(struct token *, struct symbol *);
1663 static struct token *identifier_list(struct token *, struct symbol *);
1664 static struct token *declarator(struct token *token, struct decl_state *ctx);
1666 static struct token *skip_attribute(struct token *token)
1668 token = token->next;
1669 if (match_op(token, '(')) {
1670 int depth = 1;
1671 token = token->next;
1672 while (depth && !eof_token(token)) {
1673 if (token_type(token) == TOKEN_SPECIAL) {
1674 if (token->special == '(')
1675 depth++;
1676 else if (token->special == ')')
1677 depth--;
1679 token = token->next;
1682 return token;
1685 static struct token *skip_attributes(struct token *token)
1687 struct symbol *keyword;
1688 for (;;) {
1689 if (token_type(token) != TOKEN_IDENT)
1690 break;
1691 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1692 if (!keyword || keyword->type != SYM_KEYWORD)
1693 break;
1694 if (!(keyword->op->type & KW_ATTRIBUTE))
1695 break;
1696 token = expect(token->next, '(', "after attribute");
1697 token = expect(token, '(', "after attribute");
1698 for (;;) {
1699 if (eof_token(token))
1700 break;
1701 if (match_op(token, ';'))
1702 break;
1703 if (token_type(token) != TOKEN_IDENT)
1704 break;
1705 token = skip_attribute(token);
1706 if (!match_op(token, ','))
1707 break;
1708 token = token->next;
1710 token = expect(token, ')', "after attribute");
1711 token = expect(token, ')', "after attribute");
1713 return token;
1716 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1718 struct symbol *keyword;
1719 for (;;) {
1720 if (token_type(token) != TOKEN_IDENT)
1721 break;
1722 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1723 if (!keyword || keyword->type != SYM_KEYWORD)
1724 break;
1725 if (!(keyword->op->type & keywords))
1726 break;
1727 token = keyword->op->declarator(token->next, ctx);
1728 keywords &= KW_ATTRIBUTE;
1730 return token;
1733 static int is_nested(struct token *token, struct token **p,
1734 int prefer_abstract)
1737 * This can be either a parameter list or a grouping.
1738 * For the direct (non-abstract) case, we know if must be
1739 * a parameter list if we already saw the identifier.
1740 * For the abstract case, we know if must be a parameter
1741 * list if it is empty or starts with a type.
1743 struct token *next = token->next;
1745 *p = next = skip_attributes(next);
1747 if (token_type(next) == TOKEN_IDENT) {
1748 if (lookup_type(next))
1749 return !prefer_abstract;
1750 return 1;
1753 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1754 return 0;
1756 return 1;
1759 enum kind {
1760 Empty, K_R, Proto, Bad_Func,
1763 static enum kind which_func(struct token *token,
1764 struct ident **n,
1765 int prefer_abstract)
1767 struct token *next = token->next;
1769 if (token_type(next) == TOKEN_IDENT) {
1770 if (lookup_type(next))
1771 return Proto;
1772 /* identifier list not in definition; complain */
1773 if (prefer_abstract)
1774 warning(token->pos,
1775 "identifier list not in definition");
1776 return K_R;
1779 if (token_type(next) != TOKEN_SPECIAL)
1780 return Bad_Func;
1782 if (next->special == ')') {
1783 /* don't complain about those */
1784 if (!n || match_op(next->next, ';'))
1785 return Empty;
1786 warning(next->pos,
1787 "non-ANSI function declaration of function '%s'",
1788 show_ident(*n));
1789 return Empty;
1792 if (next->special == SPECIAL_ELLIPSIS) {
1793 warning(next->pos,
1794 "variadic functions must have one named argument");
1795 return Proto;
1798 return Bad_Func;
1801 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1803 struct ctype *ctype = &ctx->ctype;
1804 struct token *next;
1805 struct ident **p = ctx->ident;
1807 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1808 *ctx->ident = token->ident;
1809 token = token->next;
1810 } else if (match_op(token, '(') &&
1811 is_nested(token, &next, ctx->prefer_abstract)) {
1812 struct symbol *base_type = ctype->base_type;
1813 if (token->next != next)
1814 next = handle_attributes(token->next, ctx,
1815 KW_ATTRIBUTE);
1816 token = declarator(next, ctx);
1817 token = expect(token, ')', "in nested declarator");
1818 while (ctype->base_type != base_type)
1819 ctype = &ctype->base_type->ctype;
1820 p = NULL;
1823 if (match_op(token, '(')) {
1824 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1825 struct symbol *fn;
1826 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1827 token = token->next;
1828 if (kind == K_R)
1829 token = identifier_list(token, fn);
1830 else if (kind == Proto)
1831 token = parameter_type_list(token, fn);
1832 token = expect(token, ')', "in function declarator");
1833 fn->endpos = token->pos;
1834 return token;
1837 while (match_op(token, '[')) {
1838 struct symbol *array;
1839 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1840 token = abstract_array_declarator(token->next, array);
1841 token = expect(token, ']', "in abstract_array_declarator");
1842 array->endpos = token->pos;
1843 ctype = &array->ctype;
1845 return token;
1848 static struct token *pointer(struct token *token, struct decl_state *ctx)
1850 while (match_op(token,'*')) {
1851 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1852 ptr->ctype.modifiers = ctx->ctype.modifiers;
1853 ptr->ctype.base_type = ctx->ctype.base_type;
1854 ptr->ctype.as = ctx->ctype.as;
1855 ptr->ctype.contexts = ctx->ctype.contexts;
1856 ctx->ctype.modifiers = 0;
1857 ctx->ctype.base_type = ptr;
1858 ctx->ctype.as = 0;
1859 ctx->ctype.contexts = NULL;
1860 ctx->ctype.alignment = 0;
1862 token = handle_qualifiers(token->next, ctx);
1863 ctx->ctype.base_type->endpos = token->pos;
1865 return token;
1868 static struct token *declarator(struct token *token, struct decl_state *ctx)
1870 token = pointer(token, ctx);
1871 return direct_declarator(token, ctx);
1874 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1876 struct ctype *ctype = &ctx->ctype;
1877 struct expression *expr;
1878 struct symbol *bitfield;
1879 long long width;
1881 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1882 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1883 show_typename(ctype->base_type));
1884 // Parse this to recover gracefully.
1885 return conditional_expression(token->next, &expr);
1888 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1889 token = conditional_expression(token->next, &expr);
1890 width = const_expression_value(expr);
1891 bitfield->bit_size = width;
1893 if (width < 0 || width > INT_MAX) {
1894 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1895 width = -1;
1896 } else if (*ctx->ident && width == 0) {
1897 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1898 show_ident(*ctx->ident));
1899 width = -1;
1900 } else if (*ctx->ident) {
1901 struct symbol *base_type = bitfield->ctype.base_type;
1902 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1903 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1904 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1905 // Valid values are either {-1;0} or {0}, depending on integer
1906 // representation. The latter makes for very efficient code...
1907 sparse_error(token->pos, "dubious one-bit signed bitfield");
1909 if (Wdefault_bitfield_sign &&
1910 bitfield_type->type != SYM_ENUM &&
1911 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1912 is_signed) {
1913 // The sign of bitfields is unspecified by default.
1914 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1917 bitfield->bit_size = width;
1918 bitfield->endpos = token->pos;
1919 return token;
1922 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1924 struct decl_state ctx = {.prefer_abstract = 0};
1925 struct ctype saved;
1926 unsigned long mod;
1928 token = declaration_specifiers(token, &ctx);
1929 mod = storage_modifiers(&ctx);
1930 saved = ctx.ctype;
1931 for (;;) {
1932 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1933 ctx.ident = &decl->ident;
1935 token = declarator(token, &ctx);
1936 if (match_op(token, ':'))
1937 token = handle_bitfield(token, &ctx);
1939 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1940 apply_modifiers(token->pos, &ctx);
1942 decl->ctype = ctx.ctype;
1943 decl->ctype.modifiers |= mod;
1944 decl->endpos = token->pos;
1945 add_symbol(list, decl);
1946 if (!match_op(token, ','))
1947 break;
1948 token = token->next;
1949 ctx.ctype = saved;
1951 return token;
1954 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1956 while (!match_op(token, '}')) {
1957 if (match_ident(token, &_Static_assert_ident)) {
1958 token = parse_static_assert(token, NULL);
1959 continue;
1961 if (!match_op(token, ';'))
1962 token = declaration_list(token, list);
1963 if (!match_op(token, ';')) {
1964 sparse_error(token->pos, "expected ; at end of declaration");
1965 break;
1967 token = token->next;
1969 return token;
1972 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1974 struct decl_state ctx = {.prefer_abstract = 1};
1976 token = declaration_specifiers(token, &ctx);
1977 ctx.ident = &sym->ident;
1978 token = declarator(token, &ctx);
1979 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1980 apply_modifiers(token->pos, &ctx);
1981 sym->ctype = ctx.ctype;
1982 sym->ctype.modifiers |= storage_modifiers(&ctx);
1983 sym->endpos = token->pos;
1984 sym->forced_arg = ctx.storage_class == SForced;
1985 return token;
1988 struct token *typename(struct token *token, struct symbol **p, int *forced)
1990 struct decl_state ctx = {.prefer_abstract = 1};
1991 int class;
1992 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1993 *p = sym;
1994 token = declaration_specifiers(token, &ctx);
1995 token = declarator(token, &ctx);
1996 apply_modifiers(token->pos, &ctx);
1997 sym->ctype = ctx.ctype;
1998 sym->endpos = token->pos;
1999 class = ctx.storage_class;
2000 if (forced) {
2001 *forced = 0;
2002 if (class == SForced) {
2003 *forced = 1;
2004 class = 0;
2007 if (class)
2008 warning(sym->pos, "storage class in typename (%s %s)",
2009 storage_class[class], show_typename(sym));
2010 return token;
2013 static struct token *expression_statement(struct token *token, struct expression **tree)
2015 token = parse_expression(token, tree);
2016 return expect(token, ';', "at end of statement");
2019 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
2020 struct expression_list **inout)
2022 struct expression *expr;
2024 /* Allow empty operands */
2025 if (match_op(token->next, ':') || match_op(token->next, ')'))
2026 return token->next;
2027 do {
2028 struct ident *ident = NULL;
2029 if (match_op(token->next, '[') &&
2030 token_type(token->next->next) == TOKEN_IDENT &&
2031 match_op(token->next->next->next, ']')) {
2032 ident = token->next->next->ident;
2033 token = token->next->next->next;
2035 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
2036 token = primary_expression(token->next, &expr);
2037 add_expression(inout, expr);
2038 token = parens_expression(token, &expr, "in asm parameter");
2039 add_expression(inout, expr);
2040 } while (match_op(token, ','));
2041 return token;
2044 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
2045 struct expression_list **clobbers)
2047 struct expression *expr;
2049 do {
2050 token = primary_expression(token->next, &expr);
2051 if (expr)
2052 add_expression(clobbers, expr);
2053 } while (match_op(token, ','));
2054 return token;
2057 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
2058 struct symbol_list **labels)
2060 struct symbol *label;
2062 do {
2063 token = token->next; /* skip ':' and ',' */
2064 if (token_type(token) != TOKEN_IDENT)
2065 return token;
2066 label = label_symbol(token);
2067 add_symbol(labels, label);
2068 token = token->next;
2069 } while (match_op(token, ','));
2070 return token;
2073 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
2075 int is_goto = 0;
2077 token = token->next;
2078 stmt->type = STMT_ASM;
2079 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
2080 token = token->next;
2082 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
2083 is_goto = 1;
2084 token = token->next;
2086 token = expect(token, '(', "after asm");
2087 token = parse_expression(token, &stmt->asm_string);
2088 if (match_op(token, ':'))
2089 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
2090 if (match_op(token, ':'))
2091 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
2092 if (match_op(token, ':'))
2093 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
2094 if (is_goto && match_op(token, ':'))
2095 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
2096 token = expect(token, ')', "after asm");
2097 return expect(token, ';', "at end of asm-statement");
2100 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
2102 struct expression *expr;
2103 token = expect(token, '(', "after asm");
2104 token = parse_expression(token->next, &expr);
2105 token = expect(token, ')', "after asm");
2106 return token;
2109 static struct token *parse_static_assert(struct token *token, struct symbol_list **unused)
2111 struct expression *cond = NULL, *message = NULL;
2113 token = expect(token->next, '(', "after _Static_assert");
2114 token = constant_expression(token, &cond);
2115 if (!cond)
2116 sparse_error(token->pos, "Expected constant expression");
2117 token = expect(token, ',', "after conditional expression in _Static_assert");
2118 token = parse_expression(token, &message);
2119 if (!message || message->type != EXPR_STRING) {
2120 struct position pos;
2122 pos = message ? message->pos : token->pos;
2123 sparse_error(pos, "bad or missing string literal");
2124 cond = NULL;
2126 token = expect(token, ')', "after diagnostic message in _Static_assert");
2128 token = expect(token, ';', "after _Static_assert()");
2130 if (cond && !const_expression_value(cond) && cond->type == EXPR_VALUE)
2131 sparse_error(cond->pos, "static assertion failed: %s",
2132 show_string(message->string));
2133 return token;
2136 /* Make a statement out of an expression */
2137 static struct statement *make_statement(struct expression *expr)
2139 struct statement *stmt;
2141 if (!expr)
2142 return NULL;
2143 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
2144 stmt->expression = expr;
2145 return stmt;
2149 * All iterators have two symbols associated with them:
2150 * the "continue" and "break" symbols, which are targets
2151 * for continue and break statements respectively.
2153 * They are in a special name-space, but they follow
2154 * all the normal visibility rules, so nested iterators
2155 * automatically work right.
2157 static void start_iterator(struct statement *stmt)
2159 struct symbol *cont, *brk;
2161 start_symbol_scope();
2162 cont = alloc_symbol(stmt->pos, SYM_NODE);
2163 bind_symbol(cont, &continue_ident, NS_ITERATOR);
2164 brk = alloc_symbol(stmt->pos, SYM_NODE);
2165 bind_symbol(brk, &break_ident, NS_ITERATOR);
2167 stmt->type = STMT_ITERATOR;
2168 stmt->iterator_break = brk;
2169 stmt->iterator_continue = cont;
2170 fn_local_symbol(brk);
2171 fn_local_symbol(cont);
2174 static void end_iterator(struct statement *stmt)
2176 end_symbol_scope();
2179 static struct statement *start_function(struct symbol *sym)
2181 struct symbol *ret;
2182 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2184 start_function_scope();
2185 ret = alloc_symbol(sym->pos, SYM_NODE);
2186 ret->ctype = sym->ctype.base_type->ctype;
2187 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2188 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2189 bind_symbol(ret, &return_ident, NS_ITERATOR);
2190 stmt->ret = ret;
2191 fn_local_symbol(ret);
2193 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2194 current_fn = sym;
2196 return stmt;
2199 static void end_function(struct symbol *sym)
2201 current_fn = NULL;
2202 end_function_scope();
2206 * A "switch()" statement, like an iterator, has a
2207 * the "break" symbol associated with it. It works
2208 * exactly like the iterator break - it's the target
2209 * for any break-statements in scope, and means that
2210 * "break" handling doesn't even need to know whether
2211 * it's breaking out of an iterator or a switch.
2213 * In addition, the "case" symbol is a marker for the
2214 * case/default statements to find the switch statement
2215 * that they are associated with.
2217 static void start_switch(struct statement *stmt)
2219 struct symbol *brk, *switch_case;
2221 start_symbol_scope();
2222 brk = alloc_symbol(stmt->pos, SYM_NODE);
2223 bind_symbol(brk, &break_ident, NS_ITERATOR);
2225 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2226 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2227 switch_case->stmt = stmt;
2229 stmt->type = STMT_SWITCH;
2230 stmt->switch_break = brk;
2231 stmt->switch_case = switch_case;
2233 fn_local_symbol(brk);
2234 fn_local_symbol(switch_case);
2237 static void end_switch(struct statement *stmt)
2239 if (!stmt->switch_case->symbol_list)
2240 warning(stmt->pos, "switch with no cases");
2241 end_symbol_scope();
2244 static void add_case_statement(struct statement *stmt)
2246 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2247 struct symbol *sym;
2249 if (!target) {
2250 sparse_error(stmt->pos, "not in switch scope");
2251 stmt->type = STMT_NONE;
2252 return;
2254 sym = alloc_symbol(stmt->pos, SYM_NODE);
2255 add_symbol(&target->symbol_list, sym);
2256 sym->stmt = stmt;
2257 stmt->case_label = sym;
2258 fn_local_symbol(sym);
2261 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2263 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2265 if (!target)
2266 error_die(token->pos, "internal error: return without a function target");
2267 stmt->type = STMT_RETURN;
2268 stmt->ret_target = target;
2269 return expression_statement(token->next, &stmt->ret_value);
2272 static void validate_for_loop_decl(struct symbol *sym)
2274 unsigned long storage = sym->ctype.modifiers & MOD_STORAGE;
2276 if (storage & ~(MOD_AUTO | MOD_REGISTER)) {
2277 const char *name = show_ident(sym->ident);
2278 sparse_error(sym->pos, "non-local var '%s' in for-loop initializer", name);
2279 sym->ctype.modifiers &= ~MOD_STORAGE;
2283 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2285 struct symbol_list *syms;
2286 struct expression *e1, *e2, *e3;
2287 struct statement *iterator;
2289 start_iterator(stmt);
2290 token = expect(token->next, '(', "after 'for'");
2292 syms = NULL;
2293 e1 = NULL;
2294 /* C99 variable declaration? */
2295 if (lookup_type(token)) {
2296 token = external_declaration(token, &syms, validate_for_loop_decl);
2297 } else {
2298 token = parse_expression(token, &e1);
2299 token = expect(token, ';', "in 'for'");
2301 token = parse_expression(token, &e2);
2302 token = expect(token, ';', "in 'for'");
2303 token = parse_expression(token, &e3);
2304 token = expect(token, ')', "in 'for'");
2305 token = statement(token, &iterator);
2307 stmt->iterator_syms = syms;
2308 stmt->iterator_pre_statement = make_statement(e1);
2309 stmt->iterator_pre_condition = e2;
2310 stmt->iterator_post_statement = make_statement(e3);
2311 stmt->iterator_post_condition = NULL;
2312 stmt->iterator_statement = iterator;
2313 end_iterator(stmt);
2315 return token;
2318 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2320 struct expression *expr;
2321 struct statement *iterator;
2323 start_iterator(stmt);
2324 token = parens_expression(token->next, &expr, "after 'while'");
2325 token = statement(token, &iterator);
2327 stmt->iterator_pre_condition = expr;
2328 stmt->iterator_post_condition = NULL;
2329 stmt->iterator_statement = iterator;
2330 end_iterator(stmt);
2332 return token;
2335 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2337 struct expression *expr;
2338 struct statement *iterator;
2340 start_iterator(stmt);
2341 token = statement(token->next, &iterator);
2342 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2343 token = token->next;
2344 else
2345 sparse_error(token->pos, "expected 'while' after 'do'");
2346 token = parens_expression(token, &expr, "after 'do-while'");
2348 stmt->iterator_post_condition = expr;
2349 stmt->iterator_statement = iterator;
2350 end_iterator(stmt);
2352 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2353 warning(iterator->pos, "do-while statement is not a compound statement");
2355 return expect(token, ';', "after statement");
2358 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2360 stmt->type = STMT_IF;
2361 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2362 token = statement(token, &stmt->if_true);
2363 if (token_type(token) != TOKEN_IDENT)
2364 return token;
2365 if (token->ident != &else_ident)
2366 return token;
2367 return statement(token->next, &stmt->if_false);
2370 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2372 stmt->type = STMT_CASE;
2373 token = expect(token, ':', "after default/case");
2374 add_case_statement(stmt);
2375 return statement(token, &stmt->case_statement);
2378 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2380 token = parse_expression(token->next, &stmt->case_expression);
2381 if (match_op(token, SPECIAL_ELLIPSIS))
2382 token = parse_expression(token->next, &stmt->case_to);
2383 return case_statement(token, stmt);
2386 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2388 return case_statement(token->next, stmt);
2391 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2393 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2394 stmt->type = STMT_GOTO;
2395 stmt->goto_label = target;
2396 if (!target)
2397 sparse_error(stmt->pos, "break/continue not in iterator scope");
2398 return expect(token->next, ';', "at end of statement");
2401 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2403 stmt->type = STMT_SWITCH;
2404 start_switch(stmt);
2405 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2406 token = statement(token, &stmt->switch_statement);
2407 end_switch(stmt);
2408 return token;
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 stmt->goto_label = label_symbol(token);
2420 token = token->next;
2421 } else {
2422 sparse_error(token->pos, "Expected identifier or goto expression");
2424 return expect(token, ';', "at end of statement");
2427 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2429 stmt->type = STMT_CONTEXT;
2430 token = parse_expression(token->next, &stmt->expression);
2431 if (stmt->expression->type == EXPR_PREOP
2432 && stmt->expression->op == '('
2433 && stmt->expression->unop->type == EXPR_COMMA) {
2434 struct expression *expr;
2435 expr = stmt->expression->unop;
2436 stmt->context = expr->left;
2437 stmt->expression = expr->right;
2439 return expect(token, ';', "at end of statement");
2442 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2444 stmt->type = STMT_RANGE;
2445 token = assignment_expression(token->next, &stmt->range_expression);
2446 token = expect(token, ',', "after range expression");
2447 token = assignment_expression(token, &stmt->range_low);
2448 token = expect(token, ',', "after low range");
2449 token = assignment_expression(token, &stmt->range_high);
2450 return expect(token, ';', "after range statement");
2453 static struct token *statement(struct token *token, struct statement **tree)
2455 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2457 *tree = stmt;
2458 if (token_type(token) == TOKEN_IDENT) {
2459 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2460 if (s && s->op->statement)
2461 return s->op->statement(token, stmt);
2463 if (match_op(token->next, ':')) {
2464 struct symbol *s = label_symbol(token);
2465 stmt->type = STMT_LABEL;
2466 stmt->label_identifier = s;
2467 if (s->stmt)
2468 sparse_error(stmt->pos, "label '%s' redefined", show_ident(token->ident));
2469 s->stmt = stmt;
2470 token = skip_attributes(token->next->next);
2471 return statement(token, &stmt->label_statement);
2475 if (match_op(token, '{')) {
2476 stmt->type = STMT_COMPOUND;
2477 start_symbol_scope();
2478 token = compound_statement(token->next, stmt);
2479 end_symbol_scope();
2481 return expect(token, '}', "at end of compound statement");
2484 stmt->type = STMT_EXPRESSION;
2485 return expression_statement(token, &stmt->expression);
2488 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2489 static struct token *label_statement(struct token *token)
2491 while (token_type(token) == TOKEN_IDENT) {
2492 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2493 /* it's block-scope, but we want label namespace */
2494 bind_symbol(sym, token->ident, NS_SYMBOL);
2495 sym->namespace = NS_LABEL;
2496 fn_local_symbol(sym);
2497 token = token->next;
2498 if (!match_op(token, ','))
2499 break;
2500 token = token->next;
2502 return expect(token, ';', "at end of label declaration");
2505 static struct token * statement_list(struct token *token, struct statement_list **list)
2507 int seen_statement = 0;
2508 while (token_type(token) == TOKEN_IDENT &&
2509 token->ident == &__label___ident)
2510 token = label_statement(token->next);
2511 for (;;) {
2512 struct statement * stmt;
2513 if (eof_token(token))
2514 break;
2515 if (match_op(token, '}'))
2516 break;
2517 if (match_ident(token, &_Static_assert_ident)) {
2518 token = parse_static_assert(token, NULL);
2519 continue;
2521 if (lookup_type(token)) {
2522 if (seen_statement) {
2523 warning(token->pos, "mixing declarations and code");
2524 seen_statement = 0;
2526 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2527 token = external_declaration(token, &stmt->declaration, NULL);
2528 } else {
2529 seen_statement = Wdeclarationafterstatement;
2530 token = statement(token, &stmt);
2532 add_statement(list, stmt);
2534 return token;
2537 static struct token *identifier_list(struct token *token, struct symbol *fn)
2539 struct symbol_list **list = &fn->arguments;
2540 for (;;) {
2541 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2542 sym->ident = token->ident;
2543 token = token->next;
2544 sym->endpos = token->pos;
2545 sym->ctype.base_type = &incomplete_ctype;
2546 add_symbol(list, sym);
2547 if (!match_op(token, ',') ||
2548 token_type(token->next) != TOKEN_IDENT ||
2549 lookup_type(token->next))
2550 break;
2551 token = token->next;
2553 return token;
2556 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2558 struct symbol_list **list = &fn->arguments;
2560 for (;;) {
2561 struct symbol *sym;
2563 if (match_op(token, SPECIAL_ELLIPSIS)) {
2564 fn->variadic = 1;
2565 token = token->next;
2566 break;
2569 sym = alloc_symbol(token->pos, SYM_NODE);
2570 token = parameter_declaration(token, sym);
2571 if (sym->ctype.base_type == &void_ctype) {
2572 /* Special case: (void) */
2573 if (!*list && !sym->ident)
2574 break;
2575 warning(token->pos, "void parameter");
2577 add_symbol(list, sym);
2578 if (!match_op(token, ','))
2579 break;
2580 token = token->next;
2582 return token;
2585 struct token *compound_statement(struct token *token, struct statement *stmt)
2587 token = statement_list(token, &stmt->stmts);
2588 return token;
2591 static struct expression *identifier_expression(struct token *token)
2593 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2594 expr->expr_ident = token->ident;
2595 return expr;
2598 static struct expression *index_expression(struct expression *from, struct expression *to)
2600 int idx_from, idx_to;
2601 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2603 idx_from = const_expression_value(from);
2604 idx_to = idx_from;
2605 if (to) {
2606 idx_to = const_expression_value(to);
2607 if (idx_to < idx_from || idx_from < 0)
2608 warning(from->pos, "nonsense array initializer index range");
2610 expr->idx_from = idx_from;
2611 expr->idx_to = idx_to;
2612 return expr;
2615 static struct token *single_initializer(struct expression **ep, struct token *token)
2617 int expect_equal = 0;
2618 struct token *next = token->next;
2619 struct expression **tail = ep;
2620 int nested;
2622 *ep = NULL;
2624 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2625 struct expression *expr = identifier_expression(token);
2626 if (Wold_initializer)
2627 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2628 token = initializer(&expr->ident_expression, next->next);
2629 if (expr->ident_expression)
2630 *ep = expr;
2631 return token;
2634 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2635 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2636 struct expression *expr = identifier_expression(next);
2637 *tail = expr;
2638 tail = &expr->ident_expression;
2639 expect_equal = 1;
2640 token = next->next;
2641 } else if (match_op(token, '[')) {
2642 struct expression *from = NULL, *to = NULL, *expr;
2643 token = constant_expression(token->next, &from);
2644 if (!from) {
2645 sparse_error(token->pos, "Expected constant expression");
2646 break;
2648 if (match_op(token, SPECIAL_ELLIPSIS))
2649 token = constant_expression(token->next, &to);
2650 expr = index_expression(from, to);
2651 *tail = expr;
2652 tail = &expr->idx_expression;
2653 token = expect(token, ']', "at end of initializer index");
2654 if (nested)
2655 expect_equal = 1;
2656 } else {
2657 break;
2660 if (nested && !expect_equal) {
2661 if (!match_op(token, '='))
2662 warning(token->pos, "obsolete array initializer, use C99 syntax");
2663 else
2664 expect_equal = 1;
2666 if (expect_equal)
2667 token = expect(token, '=', "at end of initializer index");
2669 token = initializer(tail, token);
2670 if (!*tail)
2671 *ep = NULL;
2672 return token;
2675 static struct token *initializer_list(struct expression_list **list, struct token *token)
2677 struct expression *expr;
2679 for (;;) {
2680 token = single_initializer(&expr, token);
2681 if (!expr)
2682 break;
2683 add_expression(list, expr);
2684 if (!match_op(token, ','))
2685 break;
2686 token = token->next;
2688 return token;
2691 struct token *initializer(struct expression **tree, struct token *token)
2693 if (match_op(token, '{')) {
2694 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2695 *tree = expr;
2696 token = initializer_list(&expr->expr_list, token->next);
2697 return expect(token, '}', "at end of initializer");
2699 return assignment_expression(token, tree);
2702 static void declare_argument(struct symbol *sym, struct symbol *fn)
2704 if (!sym->ident) {
2705 sparse_error(sym->pos, "no identifier for function argument");
2706 return;
2708 bind_symbol(sym, sym->ident, NS_SYMBOL);
2711 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2712 struct symbol_list **list)
2714 struct symbol_list **old_symbol_list;
2715 struct symbol *base_type = decl->ctype.base_type;
2716 struct statement *stmt, **p;
2717 struct symbol *prev;
2718 struct symbol *arg;
2720 old_symbol_list = function_symbol_list;
2721 if (decl->ctype.modifiers & MOD_INLINE) {
2722 function_symbol_list = &decl->inline_symbol_list;
2723 p = &base_type->inline_stmt;
2724 } else {
2725 function_symbol_list = &decl->symbol_list;
2726 p = &base_type->stmt;
2728 function_computed_target_list = NULL;
2729 function_computed_goto_list = NULL;
2731 if (decl->ctype.modifiers & MOD_EXTERN) {
2732 if (!(decl->ctype.modifiers & MOD_INLINE))
2733 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2735 if (!(decl->ctype.modifiers & MOD_STATIC))
2736 decl->ctype.modifiers |= MOD_EXTERN;
2738 stmt = start_function(decl);
2740 *p = stmt;
2741 FOR_EACH_PTR (base_type->arguments, arg) {
2742 declare_argument(arg, base_type);
2743 } END_FOR_EACH_PTR(arg);
2745 token = compound_statement(token->next, stmt);
2747 end_function(decl);
2748 if (!(decl->ctype.modifiers & MOD_INLINE))
2749 add_symbol(list, decl);
2750 check_declaration(decl);
2751 decl->definition = decl;
2752 prev = decl->same_symbol;
2753 if (prev && prev->definition) {
2754 warning(decl->pos, "multiple definitions for function '%s'",
2755 show_ident(decl->ident));
2756 info(prev->definition->pos, " the previous one is here");
2757 } else {
2758 while (prev) {
2759 rebind_scope(prev, decl->scope);
2760 prev->definition = decl;
2761 prev = prev->same_symbol;
2764 function_symbol_list = old_symbol_list;
2765 if (function_computed_goto_list) {
2766 if (!function_computed_target_list)
2767 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2768 else {
2769 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2770 stmt->target_list = function_computed_target_list;
2771 } END_FOR_EACH_PTR(stmt);
2774 return expect(token, '}', "at end of function");
2777 static void promote_k_r_types(struct symbol *arg)
2779 struct symbol *base = arg->ctype.base_type;
2780 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2781 arg->ctype.base_type = &int_ctype;
2785 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2787 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2788 struct symbol *arg;
2790 FOR_EACH_PTR(real_args, arg) {
2791 struct symbol *type;
2793 /* This is quadratic in the number of arguments. We _really_ don't care */
2794 FOR_EACH_PTR(argtypes, type) {
2795 if (type->ident == arg->ident)
2796 goto match;
2797 } END_FOR_EACH_PTR(type);
2798 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2799 continue;
2800 match:
2801 type->used = 1;
2802 /* "char" and "short" promote to "int" */
2803 promote_k_r_types(type);
2805 arg->ctype = type->ctype;
2806 } END_FOR_EACH_PTR(arg);
2808 FOR_EACH_PTR(argtypes, arg) {
2809 if (!arg->used)
2810 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2811 } END_FOR_EACH_PTR(arg);
2815 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2816 struct symbol_list **list)
2818 struct symbol_list *args = NULL;
2820 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2821 do {
2822 token = declaration_list(token, &args);
2823 if (!match_op(token, ';')) {
2824 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2825 break;
2827 token = token->next;
2828 } while (lookup_type(token));
2830 apply_k_r_types(args, decl);
2832 if (!match_op(token, '{')) {
2833 sparse_error(token->pos, "expected function body");
2834 return token;
2836 return parse_function_body(token, decl, list);
2839 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2841 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2842 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2843 struct statement *stmt;
2845 anon->ctype.base_type = fn;
2846 stmt = alloc_statement(token->pos, STMT_NONE);
2847 fn->stmt = stmt;
2849 token = parse_asm_statement(token, stmt);
2851 add_symbol(list, anon);
2852 return token;
2855 struct token *external_declaration(struct token *token, struct symbol_list **list,
2856 validate_decl_t validate_decl)
2858 struct ident *ident = NULL;
2859 struct symbol *decl;
2860 struct decl_state ctx = { .ident = &ident };
2861 struct ctype saved;
2862 struct symbol *base_type;
2863 unsigned long mod;
2864 int is_typedef;
2866 /* Top-level inline asm or static assertion? */
2867 if (token_type(token) == TOKEN_IDENT) {
2868 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2869 if (s && s->op->toplevel)
2870 return s->op->toplevel(token, list);
2873 /* Parse declaration-specifiers, if any */
2874 token = declaration_specifiers(token, &ctx);
2875 mod = storage_modifiers(&ctx);
2876 decl = alloc_symbol(token->pos, SYM_NODE);
2877 /* Just a type declaration? */
2878 if (match_op(token, ';')) {
2879 apply_modifiers(token->pos, &ctx);
2880 return token->next;
2883 saved = ctx.ctype;
2884 token = declarator(token, &ctx);
2885 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2886 apply_modifiers(token->pos, &ctx);
2888 decl->ctype = ctx.ctype;
2889 decl->ctype.modifiers |= mod;
2890 decl->endpos = token->pos;
2892 /* Just a type declaration? */
2893 if (!ident) {
2894 warning(token->pos, "missing identifier in declaration");
2895 return expect(token, ';', "at the end of type declaration");
2898 /* type define declaration? */
2899 is_typedef = ctx.storage_class == STypedef;
2901 /* Typedefs don't have meaningful storage */
2902 if (is_typedef)
2903 decl->ctype.modifiers |= MOD_USERTYPE;
2905 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2907 base_type = decl->ctype.base_type;
2909 if (is_typedef) {
2910 if (base_type && !base_type->ident) {
2911 switch (base_type->type) {
2912 case SYM_STRUCT:
2913 case SYM_UNION:
2914 case SYM_ENUM:
2915 case SYM_RESTRICT:
2916 base_type->ident = ident;
2917 break;
2918 default:
2919 break;
2922 } else if (base_type && base_type->type == SYM_FN) {
2923 if (base_type->ctype.base_type == &incomplete_ctype) {
2924 warning(decl->pos, "'%s()' has implicit return type",
2925 show_ident(decl->ident));
2926 base_type->ctype.base_type = &int_ctype;
2928 /* K&R argument declaration? */
2929 if (lookup_type(token))
2930 return parse_k_r_arguments(token, decl, list);
2931 if (match_op(token, '{'))
2932 return parse_function_body(token, decl, list);
2934 if (!(decl->ctype.modifiers & MOD_STATIC))
2935 decl->ctype.modifiers |= MOD_EXTERN;
2936 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2937 sparse_error(token->pos, "void declaration");
2939 if (base_type == &incomplete_ctype) {
2940 warning(decl->pos, "'%s' has implicit type", show_ident(decl->ident));
2941 decl->ctype.base_type = &int_ctype;;
2944 for (;;) {
2945 if (!is_typedef && match_op(token, '=')) {
2946 token = initializer(&decl->initializer, token->next);
2948 if (!is_typedef) {
2949 if (validate_decl)
2950 validate_decl(decl);
2952 if (decl->initializer && decl->ctype.modifiers & MOD_EXTERN) {
2953 warning(decl->pos, "symbol with external linkage has initializer");
2954 decl->ctype.modifiers &= ~MOD_EXTERN;
2957 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2958 add_symbol(list, decl);
2959 fn_local_symbol(decl);
2962 check_declaration(decl);
2963 if (decl->same_symbol) {
2964 decl->definition = decl->same_symbol->definition;
2965 decl->op = decl->same_symbol->op;
2968 if (!match_op(token, ','))
2969 break;
2971 token = token->next;
2972 ident = NULL;
2973 decl = alloc_symbol(token->pos, SYM_NODE);
2974 ctx.ctype = saved;
2975 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2976 token = declarator(token, &ctx);
2977 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2978 apply_modifiers(token->pos, &ctx);
2979 decl->ctype = ctx.ctype;
2980 decl->ctype.modifiers |= mod;
2981 decl->endpos = token->pos;
2982 if (!ident) {
2983 sparse_error(token->pos, "expected identifier name in type definition");
2984 return token;
2987 if (is_typedef)
2988 decl->ctype.modifiers |= MOD_USERTYPE;
2990 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2992 /* Function declarations are automatically extern unless specifically static */
2993 base_type = decl->ctype.base_type;
2994 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2995 if (!(decl->ctype.modifiers & MOD_STATIC))
2996 decl->ctype.modifiers |= MOD_EXTERN;
2999 return expect(token, ';', "at end of declaration");