remove unused field 'multijmp' in struct statement
[smatch.git] / parse.c
bloba4a126720c68359eda15e5a6d8aa50701481a7c7
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);
77 typedef struct token *attr_t(struct token *, struct symbol *,
78 struct decl_state *);
80 static attr_t
81 attribute_packed, attribute_aligned, attribute_modifier,
82 attribute_address_space, attribute_context,
83 attribute_designated_init,
84 attribute_transparent_union, ignore_attribute,
85 attribute_mode, attribute_force;
87 typedef struct symbol *to_mode_t(struct symbol *);
89 static to_mode_t
90 to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode, to_word_mode;
92 enum {
93 Set_T = 1,
94 Set_S = 2,
95 Set_Char = 4,
96 Set_Int = 8,
97 Set_Double = 16,
98 Set_Float = 32,
99 Set_Signed = 64,
100 Set_Unsigned = 128,
101 Set_Short = 256,
102 Set_Long = 512,
103 Set_Vlong = 1024,
104 Set_Int128 = 2048,
105 Set_Any = Set_T | Set_Short | Set_Long | Set_Signed | Set_Unsigned
108 enum {
109 CInt = 0, CSInt, CUInt, CReal, CChar, CSChar, CUChar,
112 enum {
113 SNone = 0, STypedef, SAuto, SRegister, SExtern, SStatic, SForced, SMax,
116 static struct symbol_op typedef_op = {
117 .type = KW_MODIFIER,
118 .declarator = typedef_specifier,
121 static struct symbol_op inline_op = {
122 .type = KW_MODIFIER,
123 .declarator = inline_specifier,
126 static declarator_t noreturn_specifier;
127 static struct symbol_op noreturn_op = {
128 .type = KW_MODIFIER,
129 .declarator = noreturn_specifier,
132 static declarator_t alignas_specifier;
133 static struct symbol_op alignas_op = {
134 .type = KW_MODIFIER,
135 .declarator = alignas_specifier,
138 static struct symbol_op auto_op = {
139 .type = KW_MODIFIER,
140 .declarator = auto_specifier,
143 static struct symbol_op register_op = {
144 .type = KW_MODIFIER,
145 .declarator = register_specifier,
148 static struct symbol_op static_op = {
149 .type = KW_MODIFIER,
150 .declarator = static_specifier,
153 static struct symbol_op extern_op = {
154 .type = KW_MODIFIER,
155 .declarator = extern_specifier,
158 static struct symbol_op thread_op = {
159 .type = KW_MODIFIER,
160 .declarator = thread_specifier,
163 static struct symbol_op const_op = {
164 .type = KW_QUALIFIER,
165 .declarator = const_qualifier,
168 static struct symbol_op volatile_op = {
169 .type = KW_QUALIFIER,
170 .declarator = volatile_qualifier,
173 static struct symbol_op restrict_op = {
174 .type = KW_QUALIFIER,
177 static struct symbol_op typeof_op = {
178 .type = KW_SPECIFIER,
179 .declarator = typeof_specifier,
180 .test = Set_Any,
181 .set = Set_S|Set_T,
184 static struct symbol_op attribute_op = {
185 .type = KW_ATTRIBUTE,
186 .declarator = attribute_specifier,
189 static struct symbol_op struct_op = {
190 .type = KW_SPECIFIER,
191 .declarator = struct_specifier,
192 .test = Set_Any,
193 .set = Set_S|Set_T,
196 static struct symbol_op union_op = {
197 .type = KW_SPECIFIER,
198 .declarator = union_specifier,
199 .test = Set_Any,
200 .set = Set_S|Set_T,
203 static struct symbol_op enum_op = {
204 .type = KW_SPECIFIER,
205 .declarator = enum_specifier,
206 .test = Set_Any,
207 .set = Set_S|Set_T,
210 static struct symbol_op spec_op = {
211 .type = KW_SPECIFIER | KW_EXACT,
212 .test = Set_Any,
213 .set = Set_S|Set_T,
216 static struct symbol_op char_op = {
217 .type = KW_SPECIFIER,
218 .test = Set_T|Set_Long|Set_Short,
219 .set = Set_T|Set_Char,
220 .class = CChar,
223 static struct symbol_op int_op = {
224 .type = KW_SPECIFIER,
225 .test = Set_T,
226 .set = Set_T|Set_Int,
229 static struct symbol_op double_op = {
230 .type = KW_SPECIFIER,
231 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
232 .set = Set_T|Set_Double,
233 .class = CReal,
236 static struct symbol_op float_op = {
237 .type = KW_SPECIFIER | KW_SHORT,
238 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long,
239 .set = Set_T|Set_Float,
240 .class = CReal,
243 static struct symbol_op short_op = {
244 .type = KW_SPECIFIER | KW_SHORT,
245 .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short,
246 .set = Set_Short,
249 static struct symbol_op signed_op = {
250 .type = KW_SPECIFIER,
251 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
252 .set = Set_Signed,
253 .class = CSInt,
256 static struct symbol_op unsigned_op = {
257 .type = KW_SPECIFIER,
258 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
259 .set = Set_Unsigned,
260 .class = CUInt,
263 static struct symbol_op long_op = {
264 .type = KW_SPECIFIER | KW_LONG,
265 .test = Set_S|Set_Char|Set_Float|Set_Short|Set_Vlong,
266 .set = Set_Long,
269 static struct symbol_op int128_op = {
270 .type = KW_SPECIFIER | KW_LONG,
271 .test = Set_S|Set_T|Set_Char|Set_Short|Set_Int|Set_Float|Set_Double|Set_Long|Set_Vlong|Set_Int128,
272 .set = Set_T|Set_Int128,
275 static struct symbol_op if_op = {
276 .statement = parse_if_statement,
279 static struct symbol_op return_op = {
280 .statement = parse_return_statement,
283 static struct symbol_op loop_iter_op = {
284 .statement = parse_loop_iterator,
287 static struct symbol_op default_op = {
288 .statement = parse_default_statement,
291 static struct symbol_op case_op = {
292 .statement = parse_case_statement,
295 static struct symbol_op switch_op = {
296 .statement = parse_switch_statement,
299 static struct symbol_op for_op = {
300 .statement = parse_for_statement,
303 static struct symbol_op while_op = {
304 .statement = parse_while_statement,
307 static struct symbol_op do_op = {
308 .statement = parse_do_statement,
311 static struct symbol_op goto_op = {
312 .statement = parse_goto_statement,
315 static struct symbol_op __context___op = {
316 .statement = parse_context_statement,
319 static struct symbol_op range_op = {
320 .statement = parse_range_statement,
323 static struct symbol_op asm_op = {
324 .type = KW_ASM,
325 .declarator = parse_asm_declarator,
326 .statement = parse_asm_statement,
327 .toplevel = toplevel_asm_declaration,
330 static struct symbol_op packed_op = {
331 .attribute = attribute_packed,
334 static struct symbol_op aligned_op = {
335 .attribute = attribute_aligned,
338 static struct symbol_op attr_mod_op = {
339 .attribute = attribute_modifier,
342 static struct symbol_op attr_force_op = {
343 .attribute = attribute_force,
346 static struct symbol_op address_space_op = {
347 .attribute = attribute_address_space,
350 static struct symbol_op mode_op = {
351 .attribute = attribute_mode,
354 static struct symbol_op context_op = {
355 .attribute = attribute_context,
358 static struct symbol_op designated_init_op = {
359 .attribute = attribute_designated_init,
362 static struct symbol_op transparent_union_op = {
363 .attribute = attribute_transparent_union,
366 static struct symbol_op ignore_attr_op = {
367 .attribute = ignore_attribute,
370 static struct symbol_op mode_QI_op = {
371 .type = KW_MODE,
372 .to_mode = to_QI_mode
375 static struct symbol_op mode_HI_op = {
376 .type = KW_MODE,
377 .to_mode = to_HI_mode
380 static struct symbol_op mode_SI_op = {
381 .type = KW_MODE,
382 .to_mode = to_SI_mode
385 static struct symbol_op mode_DI_op = {
386 .type = KW_MODE,
387 .to_mode = to_DI_mode
390 static struct symbol_op mode_TI_op = {
391 .type = KW_MODE,
392 .to_mode = to_TI_mode
395 static struct symbol_op mode_word_op = {
396 .type = KW_MODE,
397 .to_mode = to_word_mode
400 static struct init_keyword {
401 const char *name;
402 enum namespace ns;
403 unsigned long modifiers;
404 struct symbol_op *op;
405 struct symbol *type;
406 } keyword_table[] = {
407 /* Type qualifiers */
408 { "const", NS_TYPEDEF, .op = &const_op },
409 { "__const", NS_TYPEDEF, .op = &const_op },
410 { "__const__", NS_TYPEDEF, .op = &const_op },
411 { "volatile", NS_TYPEDEF, .op = &volatile_op },
412 { "__volatile", NS_TYPEDEF, .op = &volatile_op },
413 { "__volatile__", NS_TYPEDEF, .op = &volatile_op },
415 /* Typedef.. */
416 { "typedef", NS_TYPEDEF, .op = &typedef_op },
418 /* Type specifiers */
419 { "void", NS_TYPEDEF, .type = &void_ctype, .op = &spec_op},
420 { "char", NS_TYPEDEF, .op = &char_op },
421 { "short", NS_TYPEDEF, .op = &short_op },
422 { "int", NS_TYPEDEF, .op = &int_op },
423 { "long", NS_TYPEDEF, .op = &long_op },
424 { "float", NS_TYPEDEF, .op = &float_op },
425 { "double", NS_TYPEDEF, .op = &double_op },
426 { "signed", NS_TYPEDEF, .op = &signed_op },
427 { "__signed", NS_TYPEDEF, .op = &signed_op },
428 { "__signed__", NS_TYPEDEF, .op = &signed_op },
429 { "unsigned", NS_TYPEDEF, .op = &unsigned_op },
430 { "__int128", NS_TYPEDEF, .op = &int128_op },
431 { "_Bool", NS_TYPEDEF, .type = &bool_ctype, .op = &spec_op },
433 /* Predeclared types */
434 { "__builtin_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
435 { "__builtin_ms_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
436 { "__int128_t", NS_TYPEDEF, .type = &lllong_ctype, .op = &spec_op },
437 { "__uint128_t",NS_TYPEDEF, .type = &ulllong_ctype, .op = &spec_op },
439 /* Extended types */
440 { "typeof", NS_TYPEDEF, .op = &typeof_op },
441 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
442 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
444 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
445 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
447 { "struct", NS_TYPEDEF, .op = &struct_op },
448 { "union", NS_TYPEDEF, .op = &union_op },
449 { "enum", NS_TYPEDEF, .op = &enum_op },
451 { "inline", NS_TYPEDEF, .op = &inline_op },
452 { "__inline", NS_TYPEDEF, .op = &inline_op },
453 { "__inline__", NS_TYPEDEF, .op = &inline_op },
455 { "_Noreturn", NS_TYPEDEF, .op = &noreturn_op },
457 { "_Alignas", NS_TYPEDEF, .op = &alignas_op },
459 /* Ignored for now.. */
460 { "restrict", NS_TYPEDEF, .op = &restrict_op},
461 { "__restrict", NS_TYPEDEF, .op = &restrict_op},
462 { "__restrict__", NS_TYPEDEF, .op = &restrict_op},
464 /* Storage class */
465 { "auto", NS_TYPEDEF, .op = &auto_op },
466 { "register", NS_TYPEDEF, .op = &register_op },
467 { "static", NS_TYPEDEF, .op = &static_op },
468 { "extern", NS_TYPEDEF, .op = &extern_op },
469 { "__thread", NS_TYPEDEF, .op = &thread_op },
470 { "_Thread_local", NS_TYPEDEF, .op = &thread_op },
472 /* Statement */
473 { "if", NS_KEYWORD, .op = &if_op },
474 { "return", NS_KEYWORD, .op = &return_op },
475 { "break", NS_KEYWORD, .op = &loop_iter_op },
476 { "continue", NS_KEYWORD, .op = &loop_iter_op },
477 { "default", NS_KEYWORD, .op = &default_op },
478 { "case", NS_KEYWORD, .op = &case_op },
479 { "switch", NS_KEYWORD, .op = &switch_op },
480 { "for", NS_KEYWORD, .op = &for_op },
481 { "while", NS_KEYWORD, .op = &while_op },
482 { "do", NS_KEYWORD, .op = &do_op },
483 { "goto", NS_KEYWORD, .op = &goto_op },
484 { "__context__",NS_KEYWORD, .op = &__context___op },
485 { "__range__", NS_KEYWORD, .op = &range_op },
486 { "asm", NS_KEYWORD, .op = &asm_op },
487 { "__asm", NS_KEYWORD, .op = &asm_op },
488 { "__asm__", NS_KEYWORD, .op = &asm_op },
490 /* Attribute */
491 { "packed", NS_KEYWORD, .op = &packed_op },
492 { "__packed__", NS_KEYWORD, .op = &packed_op },
493 { "aligned", NS_KEYWORD, .op = &aligned_op },
494 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
495 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
496 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
497 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
498 { "force", NS_KEYWORD, .op = &attr_force_op },
499 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
500 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
501 { "address_space",NS_KEYWORD, .op = &address_space_op },
502 { "mode", NS_KEYWORD, .op = &mode_op },
503 { "context", NS_KEYWORD, .op = &context_op },
504 { "designated_init", NS_KEYWORD, .op = &designated_init_op },
505 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
506 { "noreturn", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
507 { "__noreturn__", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
508 { "pure", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
509 {"__pure__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
510 {"const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
511 {"__const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
512 {"__const__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
514 { "__mode__", NS_KEYWORD, .op = &mode_op },
515 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
516 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
517 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
518 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
519 { "SI", NS_KEYWORD, .op = &mode_SI_op },
520 { "__SI__", NS_KEYWORD, .op = &mode_SI_op },
521 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
522 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
523 { "TI", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
524 { "__TI__", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
525 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
526 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
529 static const char *ignored_attributes[] = {
530 "alias",
531 "__alias__",
532 "alloc_align",
533 "__alloc_align__",
534 "alloc_size",
535 "__alloc_size__",
536 "always_inline",
537 "__always_inline__",
538 "artificial",
539 "__artificial__",
540 "assume_aligned",
541 "__assume_aligned__",
542 "bounded",
543 "__bounded__",
544 "cdecl",
545 "__cdecl__",
546 "cold",
547 "__cold__",
548 "constructor",
549 "__constructor__",
550 "deprecated",
551 "__deprecated__",
552 "destructor",
553 "__destructor__",
554 "dllexport",
555 "__dllexport__",
556 "dllimport",
557 "__dllimport__",
558 "error",
559 "__error__",
560 "externally_visible",
561 "__externally_visible__",
562 "fastcall",
563 "__fastcall__",
564 "format",
565 "__format__",
566 "format_arg",
567 "__format_arg__",
568 "gnu_inline",
569 "__gnu_inline__",
570 "hot",
571 "__hot__",
572 "hotpatch",
573 "__hotpatch__",
574 "leaf",
575 "__leaf__",
576 "l1_text",
577 "__l1_text__",
578 "l1_data",
579 "__l1_data__",
580 "l2",
581 "__l2__",
582 "malloc",
583 "__malloc__",
584 "may_alias",
585 "__may_alias__",
586 "model",
587 "__model__",
588 "ms_abi",
589 "__ms_abi__",
590 "ms_hook_prologue",
591 "__ms_hook_prologue__",
592 "naked",
593 "__naked__",
594 "no_instrument_function",
595 "__no_instrument_function__",
596 "noclone",
597 "__noclone",
598 "__noclone__",
599 "noinline",
600 "__noinline__",
601 "nonnull",
602 "__nonnull",
603 "__nonnull__",
604 "nothrow",
605 "__nothrow",
606 "__nothrow__",
607 "regparm",
608 "__regparm__",
609 "section",
610 "__section__",
611 "sentinel",
612 "__sentinel__",
613 "signal",
614 "__signal__",
615 "stdcall",
616 "__stdcall__",
617 "syscall_linkage",
618 "__syscall_linkage__",
619 "sysv_abi",
620 "__sysv_abi__",
621 "unused",
622 "__unused__",
623 "used",
624 "__used__",
625 "vector_size",
626 "__vector_size__",
627 "visibility",
628 "__visibility__",
629 "warn_unused_result",
630 "__warn_unused_result__",
631 "warning",
632 "__warning__",
633 "weak",
634 "__weak__",
635 "no_sanitize_address",
636 "__no_sanitize_address__",
640 void init_parser(int stream)
642 int i;
643 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
644 struct init_keyword *ptr = keyword_table + i;
645 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
646 sym->ident->keyword = 1;
647 if (ptr->ns == NS_TYPEDEF)
648 sym->ident->reserved = 1;
649 sym->ctype.modifiers = ptr->modifiers;
650 sym->ctype.base_type = ptr->type;
651 sym->op = ptr->op;
654 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
655 const char * name = ignored_attributes[i];
656 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
657 NS_KEYWORD);
658 sym->ident->keyword = 1;
659 sym->op = &ignore_attr_op;
664 // Add a symbol to the list of function-local symbols
665 static void fn_local_symbol(struct symbol *sym)
667 if (function_symbol_list)
668 add_symbol(function_symbol_list, sym);
671 static int SENTINEL_ATTR match_idents(struct token *token, ...)
673 va_list args;
674 struct ident * next;
676 if (token_type(token) != TOKEN_IDENT)
677 return 0;
679 va_start(args, token);
680 do {
681 next = va_arg(args, struct ident *);
682 } while (next && token->ident != next);
683 va_end(args);
685 return next && token->ident == next;
689 struct statement *alloc_statement(struct position pos, int type)
691 struct statement *stmt = __alloc_statement(0);
692 stmt->type = type;
693 stmt->pos = pos;
694 return stmt;
697 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
699 static void apply_modifiers(struct position pos, struct decl_state *ctx)
701 struct symbol *ctype;
702 if (!ctx->mode)
703 return;
704 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
705 if (!ctype)
706 sparse_error(pos, "don't know how to apply mode to %s",
707 show_typename(ctx->ctype.base_type));
708 else
709 ctx->ctype.base_type = ctype;
713 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
715 struct symbol *sym = alloc_symbol(pos, type);
717 sym->ctype.base_type = ctype->base_type;
718 sym->ctype.modifiers = ctype->modifiers;
720 ctype->base_type = sym;
721 ctype->modifiers = 0;
722 return sym;
726 * NOTE! NS_LABEL is not just a different namespace,
727 * it also ends up using function scope instead of the
728 * regular symbol scope.
730 struct symbol *label_symbol(struct token *token)
732 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
733 if (!sym) {
734 sym = alloc_symbol(token->pos, SYM_LABEL);
735 bind_symbol(sym, token->ident, NS_LABEL);
736 fn_local_symbol(sym);
738 return sym;
741 static struct token *struct_union_enum_specifier(enum type type,
742 struct token *token, struct decl_state *ctx,
743 struct token *(*parse)(struct token *, struct symbol *))
745 struct symbol *sym;
746 struct position *repos;
748 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
749 if (token_type(token) == TOKEN_IDENT) {
750 sym = lookup_symbol(token->ident, NS_STRUCT);
751 if (!sym ||
752 (is_outer_scope(sym->scope) &&
753 (match_op(token->next,';') || match_op(token->next,'{')))) {
754 // Either a new symbol, or else an out-of-scope
755 // symbol being redefined.
756 sym = alloc_symbol(token->pos, type);
757 bind_symbol(sym, token->ident, NS_STRUCT);
759 if (sym->type != type)
760 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
761 ctx->ctype.base_type = sym;
762 repos = &token->pos;
763 token = token->next;
764 if (match_op(token, '{')) {
765 // The following test is actually wrong for empty
766 // structs, but (1) they are not C99, (2) gcc does
767 // the same thing, and (3) it's easier.
768 if (sym->symbol_list)
769 error_die(token->pos, "redefinition of %s", show_typename (sym));
770 sym->pos = *repos;
771 token = parse(token->next, sym);
772 token = expect(token, '}', "at end of struct-union-enum-specifier");
774 // Mark the structure as needing re-examination
775 sym->examined = 0;
776 sym->endpos = token->pos;
778 return token;
781 // private struct/union/enum type
782 if (!match_op(token, '{')) {
783 sparse_error(token->pos, "expected declaration");
784 ctx->ctype.base_type = &bad_ctype;
785 return token;
788 sym = alloc_symbol(token->pos, type);
789 token = parse(token->next, sym);
790 ctx->ctype.base_type = sym;
791 token = expect(token, '}', "at end of specifier");
792 sym->endpos = token->pos;
794 return token;
797 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
799 struct symbol *field, *last = NULL;
800 struct token *res;
801 res = struct_declaration_list(token, &sym->symbol_list);
802 FOR_EACH_PTR(sym->symbol_list, field) {
803 if (!field->ident) {
804 struct symbol *base = field->ctype.base_type;
805 if (base && base->type == SYM_BITFIELD)
806 continue;
808 if (last)
809 last->next_subobject = field;
810 last = field;
811 } END_FOR_EACH_PTR(field);
812 return res;
815 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
817 return struct_declaration_list(token, &sym->symbol_list);
820 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
822 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
825 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
827 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
831 typedef struct {
832 int x;
833 unsigned long long y;
834 } Num;
836 static void upper_boundary(Num *n, Num *v)
838 if (n->x > v->x)
839 return;
840 if (n->x < v->x) {
841 *n = *v;
842 return;
844 if (n->y < v->y)
845 n->y = v->y;
848 static void lower_boundary(Num *n, Num *v)
850 if (n->x < v->x)
851 return;
852 if (n->x > v->x) {
853 *n = *v;
854 return;
856 if (n->y > v->y)
857 n->y = v->y;
860 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
862 int shift = type->bit_size;
863 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
865 if (!is_unsigned)
866 shift--;
867 if (upper->x == 0 && upper->y >> shift)
868 return 0;
869 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
870 return 1;
871 return 0;
874 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
876 if (s1->bit_size < s2->bit_size) {
877 s1 = s2;
878 } else if (s1->bit_size == s2->bit_size) {
879 if (s2->ctype.modifiers & MOD_UNSIGNED)
880 s1 = s2;
882 if (s1->bit_size < bits_in_int)
883 return &int_ctype;
884 return s1;
887 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
889 struct symbol *sym;
891 FOR_EACH_PTR(list, sym) {
892 struct expression *expr = sym->initializer;
893 struct symbol *ctype;
894 if (expr->type != EXPR_VALUE)
895 continue;
896 ctype = expr->ctype;
897 if (ctype->bit_size == base_type->bit_size)
898 continue;
899 cast_value(expr, base_type, expr, ctype);
900 } END_FOR_EACH_PTR(sym);
903 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
905 unsigned long long lastval = 0;
906 struct symbol *ctype = NULL, *base_type = NULL;
907 Num upper = {-1, 0}, lower = {1, 0};
909 parent->examined = 1;
910 parent->ctype.base_type = &int_ctype;
911 while (token_type(token) == TOKEN_IDENT) {
912 struct expression *expr = NULL;
913 struct token *next = token->next;
914 struct symbol *sym;
916 if (match_op(next, '=')) {
917 next = constant_expression(next->next, &expr);
918 lastval = get_expression_value(expr);
919 ctype = &void_ctype;
920 if (expr && expr->ctype)
921 ctype = expr->ctype;
922 } else if (!ctype) {
923 ctype = &int_ctype;
924 } else if (is_int_type(ctype)) {
925 lastval++;
926 } else {
927 error_die(token->pos, "can't increment the last enum member");
930 if (!expr) {
931 expr = alloc_expression(token->pos, EXPR_VALUE);
932 expr->value = lastval;
933 expr->ctype = ctype;
936 sym = alloc_symbol(token->pos, SYM_NODE);
937 bind_symbol(sym, token->ident, NS_SYMBOL);
938 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
939 sym->initializer = expr;
940 sym->enum_member = 1;
941 sym->ctype.base_type = parent;
942 add_ptr_list(&parent->symbol_list, sym);
944 if (base_type != &bad_ctype) {
945 if (ctype->type == SYM_NODE)
946 ctype = ctype->ctype.base_type;
947 if (ctype->type == SYM_ENUM) {
948 if (ctype == parent)
949 ctype = base_type;
950 else
951 ctype = ctype->ctype.base_type;
954 * base_type rules:
955 * - if all enums are of the same type, then
956 * the base_type is that type (two first
957 * cases)
958 * - if enums are of different types, they
959 * all have to be integer types, and the
960 * base type is at least "int_ctype".
961 * - otherwise the base_type is "bad_ctype".
963 if (!base_type) {
964 base_type = ctype;
965 } else if (ctype == base_type) {
966 /* nothing */
967 } else if (is_int_type(base_type) && is_int_type(ctype)) {
968 base_type = bigger_enum_type(base_type, ctype);
969 } else
970 base_type = &bad_ctype;
971 parent->ctype.base_type = base_type;
973 if (is_int_type(base_type)) {
974 Num v = {.y = lastval};
975 if (ctype->ctype.modifiers & MOD_UNSIGNED)
976 v.x = 0;
977 else if ((long long)lastval >= 0)
978 v.x = 0;
979 else
980 v.x = -1;
981 upper_boundary(&upper, &v);
982 lower_boundary(&lower, &v);
984 token = next;
986 sym->endpos = token->pos;
988 if (!match_op(token, ','))
989 break;
990 token = token->next;
992 if (!base_type) {
993 sparse_error(token->pos, "bad enum definition");
994 base_type = &bad_ctype;
996 else if (!is_int_type(base_type))
997 base_type = base_type;
998 else if (type_is_ok(base_type, &upper, &lower))
999 base_type = base_type;
1000 else if (type_is_ok(&int_ctype, &upper, &lower))
1001 base_type = &int_ctype;
1002 else if (type_is_ok(&uint_ctype, &upper, &lower))
1003 base_type = &uint_ctype;
1004 else if (type_is_ok(&long_ctype, &upper, &lower))
1005 base_type = &long_ctype;
1006 else if (type_is_ok(&ulong_ctype, &upper, &lower))
1007 base_type = &ulong_ctype;
1008 else if (type_is_ok(&llong_ctype, &upper, &lower))
1009 base_type = &llong_ctype;
1010 else if (type_is_ok(&ullong_ctype, &upper, &lower))
1011 base_type = &ullong_ctype;
1012 else
1013 base_type = &bad_ctype;
1014 parent->ctype.base_type = base_type;
1015 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
1016 parent->examined = 0;
1018 cast_enum_list(parent->symbol_list, base_type);
1020 return token;
1023 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
1025 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
1026 struct ctype *ctype = &ctx->ctype.base_type->ctype;
1028 if (!ctype->base_type)
1029 ctype->base_type = &incomplete_ctype;
1031 return ret;
1034 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
1036 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
1038 struct symbol *sym;
1040 if (!match_op(token, '(')) {
1041 sparse_error(token->pos, "expected '(' after typeof");
1042 return token;
1044 if (lookup_type(token->next)) {
1045 token = typename(token->next, &sym, NULL);
1046 ctx->ctype.base_type = sym->ctype.base_type;
1047 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
1048 } else {
1049 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
1050 token = parse_expression(token->next, &typeof_sym->initializer);
1052 typeof_sym->endpos = token->pos;
1053 if (!typeof_sym->initializer) {
1054 sparse_error(token->pos, "expected expression after the '(' token");
1055 typeof_sym = &bad_ctype;
1057 ctx->ctype.base_type = typeof_sym;
1059 return expect(token, ')', "after typeof");
1062 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1064 struct expression *expr = NULL;
1065 if (match_op(token, '('))
1066 token = parens_expression(token, &expr, "in attribute");
1067 return token;
1070 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1072 if (!ctx->ctype.alignment)
1073 ctx->ctype.alignment = 1;
1074 return token;
1077 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1079 int alignment = max_alignment;
1080 struct expression *expr = NULL;
1082 if (match_op(token, '(')) {
1083 token = parens_expression(token, &expr, "in attribute");
1084 if (expr)
1085 alignment = const_expression_value(expr);
1087 if (alignment & (alignment-1)) {
1088 warning(token->pos, "I don't like non-power-of-2 alignments");
1089 return token;
1090 } else if (alignment > ctx->ctype.alignment)
1091 ctx->ctype.alignment = alignment;
1092 return token;
1095 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1097 if (ctx->modifiers & qual)
1098 warning(*pos, "duplicate %s", modifier_string(qual));
1099 ctx->modifiers |= qual;
1102 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1104 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1105 return token;
1108 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1110 struct expression *expr = NULL;
1111 int as;
1112 token = expect(token, '(', "after address_space attribute");
1113 token = conditional_expression(token, &expr);
1114 if (expr) {
1115 as = const_expression_value(expr);
1116 if (Waddress_space && as)
1117 ctx->ctype.as = as;
1119 token = expect(token, ')', "after address_space attribute");
1120 return token;
1123 static struct symbol *to_QI_mode(struct symbol *ctype)
1125 if (ctype->ctype.base_type != &int_type)
1126 return NULL;
1127 if (ctype == &char_ctype)
1128 return ctype;
1129 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1130 : &schar_ctype;
1133 static struct symbol *to_HI_mode(struct symbol *ctype)
1135 if (ctype->ctype.base_type != &int_type)
1136 return NULL;
1137 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1138 : &sshort_ctype;
1141 static struct symbol *to_SI_mode(struct symbol *ctype)
1143 if (ctype->ctype.base_type != &int_type)
1144 return NULL;
1145 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1146 : &sint_ctype;
1149 static struct symbol *to_DI_mode(struct symbol *ctype)
1151 if (ctype->ctype.base_type != &int_type)
1152 return NULL;
1153 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1154 : &sllong_ctype;
1157 static struct symbol *to_TI_mode(struct symbol *ctype)
1159 if (ctype->ctype.base_type != &int_type)
1160 return NULL;
1161 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1162 : &slllong_ctype;
1165 static struct symbol *to_word_mode(struct symbol *ctype)
1167 if (ctype->ctype.base_type != &int_type)
1168 return NULL;
1169 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1170 : &slong_ctype;
1173 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1175 token = expect(token, '(', "after mode attribute");
1176 if (token_type(token) == TOKEN_IDENT) {
1177 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1178 if (mode && mode->op->type == KW_MODE)
1179 ctx->mode = mode->op;
1180 else
1181 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1182 token = token->next;
1183 } else
1184 sparse_error(token->pos, "expect attribute mode symbol\n");
1185 token = expect(token, ')', "after mode attribute");
1186 return token;
1189 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1191 struct context *context = alloc_context();
1192 struct expression *args[3];
1193 int argc = 0;
1195 token = expect(token, '(', "after context attribute");
1196 while (!match_op(token, ')')) {
1197 struct expression *expr = NULL;
1198 token = conditional_expression(token, &expr);
1199 if (!expr)
1200 break;
1201 if (argc < 3)
1202 args[argc++] = expr;
1203 if (!match_op(token, ','))
1204 break;
1205 token = token->next;
1208 switch(argc) {
1209 case 0:
1210 sparse_error(token->pos, "expected context input/output values");
1211 break;
1212 case 1:
1213 context->in = get_expression_value(args[0]);
1214 break;
1215 case 2:
1216 context->in = get_expression_value(args[0]);
1217 context->out = get_expression_value(args[1]);
1218 break;
1219 case 3:
1220 context->context = args[0];
1221 context->in = get_expression_value(args[1]);
1222 context->out = get_expression_value(args[2]);
1223 break;
1226 if (argc)
1227 add_ptr_list(&ctx->ctype.contexts, context);
1229 token = expect(token, ')', "after context attribute");
1230 return token;
1233 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1235 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1236 ctx->ctype.base_type->designated_init = 1;
1237 else
1238 warning(token->pos, "attribute designated_init applied to non-structure type");
1239 return token;
1242 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1244 if (Wtransparent_union)
1245 warning(token->pos, "attribute __transparent_union__");
1247 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_UNION)
1248 ctx->ctype.base_type->transparent_union = 1;
1249 else
1250 warning(token->pos, "attribute __transparent_union__ applied to non-union type");
1251 return token;
1254 static struct token *recover_unknown_attribute(struct token *token)
1256 struct expression *expr = NULL;
1258 if (Wunknown_attribute)
1259 warning(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1260 token = token->next;
1261 if (match_op(token, '('))
1262 token = parens_expression(token, &expr, "in attribute");
1263 return token;
1266 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1268 token = expect(token, '(', "after attribute");
1269 token = expect(token, '(', "after attribute");
1271 for (;;) {
1272 struct ident *attribute_name;
1273 struct symbol *attr;
1275 if (eof_token(token))
1276 break;
1277 if (match_op(token, ';'))
1278 break;
1279 if (token_type(token) != TOKEN_IDENT)
1280 break;
1281 attribute_name = token->ident;
1282 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1283 if (attr && attr->op->attribute)
1284 token = attr->op->attribute(token->next, attr, ctx);
1285 else
1286 token = recover_unknown_attribute(token);
1288 if (!match_op(token, ','))
1289 break;
1290 token = token->next;
1293 token = expect(token, ')', "after attribute");
1294 token = expect(token, ')', "after attribute");
1295 return token;
1298 static const char *storage_class[] =
1300 [STypedef] = "typedef",
1301 [SAuto] = "auto",
1302 [SExtern] = "extern",
1303 [SStatic] = "static",
1304 [SRegister] = "register",
1305 [SForced] = "[force]"
1308 static unsigned long storage_modifiers(struct decl_state *ctx)
1310 static unsigned long mod[SMax] =
1312 [SAuto] = MOD_AUTO,
1313 [SExtern] = MOD_EXTERN,
1314 [SStatic] = MOD_STATIC,
1315 [SRegister] = MOD_REGISTER
1317 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1318 | (ctx->is_tls ? MOD_TLS : 0);
1321 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1323 /* __thread can be used alone, or with extern or static */
1324 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1325 sparse_error(*pos, "__thread can only be used alone, or with "
1326 "extern or static");
1327 return;
1330 if (!ctx->storage_class) {
1331 ctx->storage_class = class;
1332 return;
1334 if (ctx->storage_class == class)
1335 sparse_error(*pos, "duplicate %s", storage_class[class]);
1336 else
1337 sparse_error(*pos, "multiple storage classes");
1340 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1342 set_storage_class(&next->pos, ctx, STypedef);
1343 return next;
1346 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1348 set_storage_class(&next->pos, ctx, SAuto);
1349 return next;
1352 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1354 set_storage_class(&next->pos, ctx, SRegister);
1355 return next;
1358 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1360 set_storage_class(&next->pos, ctx, SStatic);
1361 return next;
1364 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1366 set_storage_class(&next->pos, ctx, SExtern);
1367 return next;
1370 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1372 /* This GCC extension can be used alone, or with extern or static */
1373 if (!ctx->storage_class || ctx->storage_class == SStatic
1374 || ctx->storage_class == SExtern) {
1375 ctx->is_tls = 1;
1376 } else {
1377 sparse_error(next->pos, "__thread can only be used alone, or "
1378 "with extern or static");
1381 return next;
1384 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1386 set_storage_class(&token->pos, ctx, SForced);
1387 return token;
1390 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1392 ctx->is_inline = 1;
1393 return next;
1396 static struct token *noreturn_specifier(struct token *next, struct decl_state *ctx)
1398 apply_qualifier(&next->pos, &ctx->ctype, MOD_NORETURN);
1399 return next;
1402 static struct token *alignas_specifier(struct token *token, struct decl_state *ctx)
1404 int alignment = 0;
1406 if (!match_op(token, '(')) {
1407 sparse_error(token->pos, "expected '(' after _Alignas");
1408 return token;
1410 if (lookup_type(token->next)) {
1411 struct symbol *sym = NULL;
1412 token = typename(token->next, &sym, NULL);
1413 sym = examine_symbol_type(sym);
1414 alignment = sym->ctype.alignment;
1415 token = expect(token, ')', "after _Alignas(...");
1416 } else {
1417 struct expression *expr = NULL;
1418 token = parens_expression(token, &expr, "after _Alignas");
1419 if (!expr)
1420 return token;
1421 alignment = const_expression_value(expr);
1424 if (alignment < 0) {
1425 warning(token->pos, "non-positive alignment");
1426 return token;
1428 if (alignment & (alignment-1)) {
1429 warning(token->pos, "non-power-of-2 alignment");
1430 return token;
1432 if (alignment > ctx->ctype.alignment)
1433 ctx->ctype.alignment = alignment;
1434 return token;
1437 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1439 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1440 return next;
1443 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1445 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1446 return next;
1449 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1451 unsigned long mod = thistype->modifiers;
1453 if (mod)
1454 apply_qualifier(&pos, ctype, mod);
1456 /* Context */
1457 concat_ptr_list((struct ptr_list *)thistype->contexts,
1458 (struct ptr_list **)&ctype->contexts);
1460 /* Alignment */
1461 if (thistype->alignment > ctype->alignment)
1462 ctype->alignment = thistype->alignment;
1464 /* Address space */
1465 if (thistype->as)
1466 ctype->as = thistype->as;
1469 static void specifier_conflict(struct position pos, int what, struct ident *new)
1471 const char *old;
1472 if (what & (Set_S | Set_T))
1473 goto Catch_all;
1474 if (what & Set_Char)
1475 old = "char";
1476 else if (what & Set_Double)
1477 old = "double";
1478 else if (what & Set_Float)
1479 old = "float";
1480 else if (what & Set_Signed)
1481 old = "signed";
1482 else if (what & Set_Unsigned)
1483 old = "unsigned";
1484 else if (what & Set_Short)
1485 old = "short";
1486 else if (what & Set_Long)
1487 old = "long";
1488 else
1489 old = "long long";
1490 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1491 old, show_ident(new));
1492 return;
1494 Catch_all:
1495 sparse_error(pos, "two or more data types in declaration specifiers");
1498 static struct symbol * const int_types[] =
1499 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype, &lllong_ctype};
1500 static struct symbol * const signed_types[] =
1501 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1502 &slllong_ctype};
1503 static struct symbol * const unsigned_types[] =
1504 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1505 &ulllong_ctype};
1506 static struct symbol * const real_types[] =
1507 {&float_ctype, &double_ctype, &ldouble_ctype};
1508 static struct symbol * const char_types[] =
1509 {&char_ctype, &schar_ctype, &uchar_ctype};
1510 static struct symbol * const * const types[] = {
1511 int_types + 1, signed_types + 1, unsigned_types + 1,
1512 real_types + 1, char_types, char_types + 1, char_types + 2
1515 struct symbol *ctype_integer(int size, int want_unsigned)
1517 return types[want_unsigned ? CUInt : CInt][size];
1520 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1522 while (token_type(t) == TOKEN_IDENT) {
1523 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1524 if (!s)
1525 break;
1526 if (s->type != SYM_KEYWORD)
1527 break;
1528 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1529 break;
1530 t = t->next;
1531 if (s->op->declarator)
1532 t = s->op->declarator(t, ctx);
1534 return t;
1537 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1539 int seen = 0;
1540 int class = CInt;
1541 int size = 0;
1543 while (token_type(token) == TOKEN_IDENT) {
1544 struct symbol *s = lookup_symbol(token->ident,
1545 NS_TYPEDEF | NS_SYMBOL);
1546 if (!s || !(s->namespace & NS_TYPEDEF))
1547 break;
1548 if (s->type != SYM_KEYWORD) {
1549 if (seen & Set_Any)
1550 break;
1551 seen |= Set_S | Set_T;
1552 ctx->ctype.base_type = s->ctype.base_type;
1553 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1554 token = token->next;
1555 continue;
1557 if (s->op->type & KW_SPECIFIER) {
1558 if (seen & s->op->test) {
1559 specifier_conflict(token->pos,
1560 seen & s->op->test,
1561 token->ident);
1562 break;
1564 seen |= s->op->set;
1565 class += s->op->class;
1566 if (s->op->set & Set_Int128)
1567 size = 2;
1568 if (s->op->type & KW_SHORT) {
1569 size = -1;
1570 } else if (s->op->type & KW_LONG && size++) {
1571 if (class == CReal) {
1572 specifier_conflict(token->pos,
1573 Set_Vlong,
1574 &double_ident);
1575 break;
1577 seen |= Set_Vlong;
1580 token = token->next;
1581 if (s->op->declarator)
1582 token = s->op->declarator(token, ctx);
1583 if (s->op->type & KW_EXACT) {
1584 ctx->ctype.base_type = s->ctype.base_type;
1585 ctx->ctype.modifiers |= s->ctype.modifiers;
1589 if (!(seen & Set_S)) { /* not set explicitly? */
1590 struct symbol *base = &incomplete_ctype;
1591 if (seen & Set_Any)
1592 base = types[class][size];
1593 ctx->ctype.base_type = base;
1596 if (ctx->ctype.modifiers & MOD_BITWISE) {
1597 struct symbol *type;
1598 ctx->ctype.modifiers &= ~MOD_BITWISE;
1599 if (!is_int_type(ctx->ctype.base_type)) {
1600 sparse_error(token->pos, "invalid modifier");
1601 return token;
1603 type = alloc_symbol(token->pos, SYM_BASETYPE);
1604 *type = *ctx->ctype.base_type;
1605 type->ctype.modifiers &= ~MOD_SPECIFIER;
1606 type->ctype.base_type = ctx->ctype.base_type;
1607 type->type = SYM_RESTRICT;
1608 ctx->ctype.base_type = type;
1609 create_fouled(type);
1611 return token;
1614 static struct token *abstract_array_static_declarator(struct token *token, int *has_static)
1616 while (token->ident == &static_ident) {
1617 if (*has_static)
1618 sparse_error(token->pos, "duplicate array static declarator");
1620 *has_static = 1;
1621 token = token->next;
1623 return token;
1627 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1629 struct expression *expr = NULL;
1630 int has_static = 0;
1632 token = abstract_array_static_declarator(token, &has_static);
1634 if (match_idents(token, &restrict_ident, &__restrict_ident, &__restrict___ident, NULL))
1635 token = abstract_array_static_declarator(token->next, &has_static);
1636 token = parse_expression(token, &expr);
1637 sym->array_size = expr;
1638 return token;
1641 static struct token *parameter_type_list(struct token *, struct symbol *);
1642 static struct token *identifier_list(struct token *, struct symbol *);
1643 static struct token *declarator(struct token *token, struct decl_state *ctx);
1645 static struct token *skip_attribute(struct token *token)
1647 token = token->next;
1648 if (match_op(token, '(')) {
1649 int depth = 1;
1650 token = token->next;
1651 while (depth && !eof_token(token)) {
1652 if (token_type(token) == TOKEN_SPECIAL) {
1653 if (token->special == '(')
1654 depth++;
1655 else if (token->special == ')')
1656 depth--;
1658 token = token->next;
1661 return token;
1664 static struct token *skip_attributes(struct token *token)
1666 struct symbol *keyword;
1667 for (;;) {
1668 if (token_type(token) != TOKEN_IDENT)
1669 break;
1670 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1671 if (!keyword || keyword->type != SYM_KEYWORD)
1672 break;
1673 if (!(keyword->op->type & KW_ATTRIBUTE))
1674 break;
1675 token = expect(token->next, '(', "after attribute");
1676 token = expect(token, '(', "after attribute");
1677 for (;;) {
1678 if (eof_token(token))
1679 break;
1680 if (match_op(token, ';'))
1681 break;
1682 if (token_type(token) != TOKEN_IDENT)
1683 break;
1684 token = skip_attribute(token);
1685 if (!match_op(token, ','))
1686 break;
1687 token = token->next;
1689 token = expect(token, ')', "after attribute");
1690 token = expect(token, ')', "after attribute");
1692 return token;
1695 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1697 struct symbol *keyword;
1698 for (;;) {
1699 if (token_type(token) != TOKEN_IDENT)
1700 break;
1701 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1702 if (!keyword || keyword->type != SYM_KEYWORD)
1703 break;
1704 if (!(keyword->op->type & keywords))
1705 break;
1706 token = keyword->op->declarator(token->next, ctx);
1707 keywords &= KW_ATTRIBUTE;
1709 return token;
1712 static int is_nested(struct token *token, struct token **p,
1713 int prefer_abstract)
1716 * This can be either a parameter list or a grouping.
1717 * For the direct (non-abstract) case, we know if must be
1718 * a parameter list if we already saw the identifier.
1719 * For the abstract case, we know if must be a parameter
1720 * list if it is empty or starts with a type.
1722 struct token *next = token->next;
1724 *p = next = skip_attributes(next);
1726 if (token_type(next) == TOKEN_IDENT) {
1727 if (lookup_type(next))
1728 return !prefer_abstract;
1729 return 1;
1732 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1733 return 0;
1735 return 1;
1738 enum kind {
1739 Empty, K_R, Proto, Bad_Func,
1742 static enum kind which_func(struct token *token,
1743 struct ident **n,
1744 int prefer_abstract)
1746 struct token *next = token->next;
1748 if (token_type(next) == TOKEN_IDENT) {
1749 if (lookup_type(next))
1750 return Proto;
1751 /* identifier list not in definition; complain */
1752 if (prefer_abstract)
1753 warning(token->pos,
1754 "identifier list not in definition");
1755 return K_R;
1758 if (token_type(next) != TOKEN_SPECIAL)
1759 return Bad_Func;
1761 if (next->special == ')') {
1762 /* don't complain about those */
1763 if (!n || match_op(next->next, ';'))
1764 return Empty;
1765 warning(next->pos,
1766 "non-ANSI function declaration of function '%s'",
1767 show_ident(*n));
1768 return Empty;
1771 if (next->special == SPECIAL_ELLIPSIS) {
1772 warning(next->pos,
1773 "variadic functions must have one named argument");
1774 return Proto;
1777 return Bad_Func;
1780 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1782 struct ctype *ctype = &ctx->ctype;
1783 struct token *next;
1784 struct ident **p = ctx->ident;
1786 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1787 *ctx->ident = token->ident;
1788 token = token->next;
1789 } else if (match_op(token, '(') &&
1790 is_nested(token, &next, ctx->prefer_abstract)) {
1791 struct symbol *base_type = ctype->base_type;
1792 if (token->next != next)
1793 next = handle_attributes(token->next, ctx,
1794 KW_ATTRIBUTE);
1795 token = declarator(next, ctx);
1796 token = expect(token, ')', "in nested declarator");
1797 while (ctype->base_type != base_type)
1798 ctype = &ctype->base_type->ctype;
1799 p = NULL;
1802 if (match_op(token, '(')) {
1803 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1804 struct symbol *fn;
1805 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1806 token = token->next;
1807 if (kind == K_R)
1808 token = identifier_list(token, fn);
1809 else if (kind == Proto)
1810 token = parameter_type_list(token, fn);
1811 token = expect(token, ')', "in function declarator");
1812 fn->endpos = token->pos;
1813 return token;
1816 while (match_op(token, '[')) {
1817 struct symbol *array;
1818 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1819 token = abstract_array_declarator(token->next, array);
1820 token = expect(token, ']', "in abstract_array_declarator");
1821 array->endpos = token->pos;
1822 ctype = &array->ctype;
1824 return token;
1827 static struct token *pointer(struct token *token, struct decl_state *ctx)
1829 while (match_op(token,'*')) {
1830 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1831 ptr->ctype.modifiers = ctx->ctype.modifiers;
1832 ptr->ctype.base_type = ctx->ctype.base_type;
1833 ptr->ctype.as = ctx->ctype.as;
1834 ptr->ctype.contexts = ctx->ctype.contexts;
1835 ctx->ctype.modifiers = 0;
1836 ctx->ctype.base_type = ptr;
1837 ctx->ctype.as = 0;
1838 ctx->ctype.contexts = NULL;
1839 ctx->ctype.alignment = 0;
1841 token = handle_qualifiers(token->next, ctx);
1842 ctx->ctype.base_type->endpos = token->pos;
1844 return token;
1847 static struct token *declarator(struct token *token, struct decl_state *ctx)
1849 token = pointer(token, ctx);
1850 return direct_declarator(token, ctx);
1853 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1855 struct ctype *ctype = &ctx->ctype;
1856 struct expression *expr;
1857 struct symbol *bitfield;
1858 long long width;
1860 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1861 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1862 show_typename(ctype->base_type));
1863 // Parse this to recover gracefully.
1864 return conditional_expression(token->next, &expr);
1867 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1868 token = conditional_expression(token->next, &expr);
1869 width = const_expression_value(expr);
1870 bitfield->bit_size = width;
1872 if (width < 0 || width > INT_MAX) {
1873 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1874 width = -1;
1875 } else if (*ctx->ident && width == 0) {
1876 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1877 show_ident(*ctx->ident));
1878 width = -1;
1879 } else if (*ctx->ident) {
1880 struct symbol *base_type = bitfield->ctype.base_type;
1881 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1882 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1883 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1884 // Valid values are either {-1;0} or {0}, depending on integer
1885 // representation. The latter makes for very efficient code...
1886 sparse_error(token->pos, "dubious one-bit signed bitfield");
1888 if (Wdefault_bitfield_sign &&
1889 bitfield_type->type != SYM_ENUM &&
1890 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1891 is_signed) {
1892 // The sign of bitfields is unspecified by default.
1893 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1896 bitfield->bit_size = width;
1897 bitfield->endpos = token->pos;
1898 return token;
1901 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1903 struct decl_state ctx = {.prefer_abstract = 0};
1904 struct ctype saved;
1905 unsigned long mod;
1907 token = declaration_specifiers(token, &ctx);
1908 mod = storage_modifiers(&ctx);
1909 saved = ctx.ctype;
1910 for (;;) {
1911 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1912 ctx.ident = &decl->ident;
1914 token = declarator(token, &ctx);
1915 if (match_op(token, ':'))
1916 token = handle_bitfield(token, &ctx);
1918 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1919 apply_modifiers(token->pos, &ctx);
1921 decl->ctype = ctx.ctype;
1922 decl->ctype.modifiers |= mod;
1923 decl->endpos = token->pos;
1924 add_symbol(list, decl);
1925 if (!match_op(token, ','))
1926 break;
1927 token = token->next;
1928 ctx.ctype = saved;
1930 return token;
1933 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1935 while (!match_op(token, '}')) {
1936 if (!match_op(token, ';'))
1937 token = declaration_list(token, list);
1938 if (!match_op(token, ';')) {
1939 sparse_error(token->pos, "expected ; at end of declaration");
1940 break;
1942 token = token->next;
1944 return token;
1947 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1949 struct decl_state ctx = {.prefer_abstract = 1};
1951 token = declaration_specifiers(token, &ctx);
1952 ctx.ident = &sym->ident;
1953 token = declarator(token, &ctx);
1954 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1955 apply_modifiers(token->pos, &ctx);
1956 sym->ctype = ctx.ctype;
1957 sym->ctype.modifiers |= storage_modifiers(&ctx);
1958 sym->endpos = token->pos;
1959 sym->forced_arg = ctx.storage_class == SForced;
1960 return token;
1963 struct token *typename(struct token *token, struct symbol **p, int *forced)
1965 struct decl_state ctx = {.prefer_abstract = 1};
1966 int class;
1967 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1968 *p = sym;
1969 token = declaration_specifiers(token, &ctx);
1970 token = declarator(token, &ctx);
1971 apply_modifiers(token->pos, &ctx);
1972 sym->ctype = ctx.ctype;
1973 sym->endpos = token->pos;
1974 class = ctx.storage_class;
1975 if (forced) {
1976 *forced = 0;
1977 if (class == SForced) {
1978 *forced = 1;
1979 class = 0;
1982 if (class)
1983 warning(sym->pos, "storage class in typename (%s %s)",
1984 storage_class[class], show_typename(sym));
1985 return token;
1988 static struct token *expression_statement(struct token *token, struct expression **tree)
1990 token = parse_expression(token, tree);
1991 return expect(token, ';', "at end of statement");
1994 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1995 struct expression_list **inout)
1997 struct expression *expr;
1999 /* Allow empty operands */
2000 if (match_op(token->next, ':') || match_op(token->next, ')'))
2001 return token->next;
2002 do {
2003 struct ident *ident = NULL;
2004 if (match_op(token->next, '[') &&
2005 token_type(token->next->next) == TOKEN_IDENT &&
2006 match_op(token->next->next->next, ']')) {
2007 ident = token->next->next->ident;
2008 token = token->next->next->next;
2010 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
2011 token = primary_expression(token->next, &expr);
2012 add_expression(inout, expr);
2013 token = parens_expression(token, &expr, "in asm parameter");
2014 add_expression(inout, expr);
2015 } while (match_op(token, ','));
2016 return token;
2019 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
2020 struct expression_list **clobbers)
2022 struct expression *expr;
2024 do {
2025 token = primary_expression(token->next, &expr);
2026 if (expr)
2027 add_expression(clobbers, expr);
2028 } while (match_op(token, ','));
2029 return token;
2032 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
2033 struct symbol_list **labels)
2035 struct symbol *label;
2037 do {
2038 token = token->next; /* skip ':' and ',' */
2039 if (token_type(token) != TOKEN_IDENT)
2040 return token;
2041 label = label_symbol(token);
2042 add_symbol(labels, label);
2043 token = token->next;
2044 } while (match_op(token, ','));
2045 return token;
2048 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
2050 int is_goto = 0;
2052 token = token->next;
2053 stmt->type = STMT_ASM;
2054 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
2055 token = token->next;
2057 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
2058 is_goto = 1;
2059 token = token->next;
2061 token = expect(token, '(', "after asm");
2062 token = parse_expression(token, &stmt->asm_string);
2063 if (match_op(token, ':'))
2064 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
2065 if (match_op(token, ':'))
2066 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
2067 if (match_op(token, ':'))
2068 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
2069 if (is_goto && match_op(token, ':'))
2070 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
2071 token = expect(token, ')', "after asm");
2072 return expect(token, ';', "at end of asm-statement");
2075 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
2077 struct expression *expr;
2078 token = expect(token, '(', "after asm");
2079 token = parse_expression(token->next, &expr);
2080 token = expect(token, ')', "after asm");
2081 return token;
2084 /* Make a statement out of an expression */
2085 static struct statement *make_statement(struct expression *expr)
2087 struct statement *stmt;
2089 if (!expr)
2090 return NULL;
2091 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
2092 stmt->expression = expr;
2093 return stmt;
2097 * All iterators have two symbols associated with them:
2098 * the "continue" and "break" symbols, which are targets
2099 * for continue and break statements respectively.
2101 * They are in a special name-space, but they follow
2102 * all the normal visibility rules, so nested iterators
2103 * automatically work right.
2105 static void start_iterator(struct statement *stmt)
2107 struct symbol *cont, *brk;
2109 start_symbol_scope();
2110 cont = alloc_symbol(stmt->pos, SYM_NODE);
2111 bind_symbol(cont, &continue_ident, NS_ITERATOR);
2112 brk = alloc_symbol(stmt->pos, SYM_NODE);
2113 bind_symbol(brk, &break_ident, NS_ITERATOR);
2115 stmt->type = STMT_ITERATOR;
2116 stmt->iterator_break = brk;
2117 stmt->iterator_continue = cont;
2118 fn_local_symbol(brk);
2119 fn_local_symbol(cont);
2122 static void end_iterator(struct statement *stmt)
2124 end_symbol_scope();
2127 static struct statement *start_function(struct symbol *sym)
2129 struct symbol *ret;
2130 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2132 start_function_scope();
2133 ret = alloc_symbol(sym->pos, SYM_NODE);
2134 ret->ctype = sym->ctype.base_type->ctype;
2135 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2136 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2137 bind_symbol(ret, &return_ident, NS_ITERATOR);
2138 stmt->ret = ret;
2139 fn_local_symbol(ret);
2141 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2142 current_fn = sym;
2144 return stmt;
2147 static void end_function(struct symbol *sym)
2149 current_fn = NULL;
2150 end_function_scope();
2154 * A "switch()" statement, like an iterator, has a
2155 * the "break" symbol associated with it. It works
2156 * exactly like the iterator break - it's the target
2157 * for any break-statements in scope, and means that
2158 * "break" handling doesn't even need to know whether
2159 * it's breaking out of an iterator or a switch.
2161 * In addition, the "case" symbol is a marker for the
2162 * case/default statements to find the switch statement
2163 * that they are associated with.
2165 static void start_switch(struct statement *stmt)
2167 struct symbol *brk, *switch_case;
2169 start_symbol_scope();
2170 brk = alloc_symbol(stmt->pos, SYM_NODE);
2171 bind_symbol(brk, &break_ident, NS_ITERATOR);
2173 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2174 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2175 switch_case->stmt = stmt;
2177 stmt->type = STMT_SWITCH;
2178 stmt->switch_break = brk;
2179 stmt->switch_case = switch_case;
2181 fn_local_symbol(brk);
2182 fn_local_symbol(switch_case);
2185 static void end_switch(struct statement *stmt)
2187 if (!stmt->switch_case->symbol_list)
2188 warning(stmt->pos, "switch with no cases");
2189 end_symbol_scope();
2192 static void add_case_statement(struct statement *stmt)
2194 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2195 struct symbol *sym;
2197 if (!target) {
2198 sparse_error(stmt->pos, "not in switch scope");
2199 stmt->type = STMT_NONE;
2200 return;
2202 sym = alloc_symbol(stmt->pos, SYM_NODE);
2203 add_symbol(&target->symbol_list, sym);
2204 sym->stmt = stmt;
2205 stmt->case_label = sym;
2206 fn_local_symbol(sym);
2209 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2211 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2213 if (!target)
2214 error_die(token->pos, "internal error: return without a function target");
2215 stmt->type = STMT_RETURN;
2216 stmt->ret_target = target;
2217 return expression_statement(token->next, &stmt->ret_value);
2220 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2222 struct symbol_list *syms;
2223 struct expression *e1, *e2, *e3;
2224 struct statement *iterator;
2226 start_iterator(stmt);
2227 token = expect(token->next, '(', "after 'for'");
2229 syms = NULL;
2230 e1 = NULL;
2231 /* C99 variable declaration? */
2232 if (lookup_type(token)) {
2233 token = external_declaration(token, &syms);
2234 } else {
2235 token = parse_expression(token, &e1);
2236 token = expect(token, ';', "in 'for'");
2238 token = parse_expression(token, &e2);
2239 token = expect(token, ';', "in 'for'");
2240 token = parse_expression(token, &e3);
2241 token = expect(token, ')', "in 'for'");
2242 token = statement(token, &iterator);
2244 stmt->iterator_syms = syms;
2245 stmt->iterator_pre_statement = make_statement(e1);
2246 stmt->iterator_pre_condition = e2;
2247 stmt->iterator_post_statement = make_statement(e3);
2248 stmt->iterator_post_condition = NULL;
2249 stmt->iterator_statement = iterator;
2250 end_iterator(stmt);
2252 return token;
2255 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2257 struct expression *expr;
2258 struct statement *iterator;
2260 start_iterator(stmt);
2261 token = parens_expression(token->next, &expr, "after 'while'");
2262 token = statement(token, &iterator);
2264 stmt->iterator_pre_condition = expr;
2265 stmt->iterator_post_condition = NULL;
2266 stmt->iterator_statement = iterator;
2267 end_iterator(stmt);
2269 return token;
2272 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2274 struct expression *expr;
2275 struct statement *iterator;
2277 start_iterator(stmt);
2278 token = statement(token->next, &iterator);
2279 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2280 token = token->next;
2281 else
2282 sparse_error(token->pos, "expected 'while' after 'do'");
2283 token = parens_expression(token, &expr, "after 'do-while'");
2285 stmt->iterator_post_condition = expr;
2286 stmt->iterator_statement = iterator;
2287 end_iterator(stmt);
2289 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2290 warning(iterator->pos, "do-while statement is not a compound statement");
2292 return expect(token, ';', "after statement");
2295 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2297 stmt->type = STMT_IF;
2298 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2299 token = statement(token, &stmt->if_true);
2300 if (token_type(token) != TOKEN_IDENT)
2301 return token;
2302 if (token->ident != &else_ident)
2303 return token;
2304 return statement(token->next, &stmt->if_false);
2307 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2309 stmt->type = STMT_CASE;
2310 token = expect(token, ':', "after default/case");
2311 add_case_statement(stmt);
2312 return statement(token, &stmt->case_statement);
2315 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2317 token = parse_expression(token->next, &stmt->case_expression);
2318 if (match_op(token, SPECIAL_ELLIPSIS))
2319 token = parse_expression(token->next, &stmt->case_to);
2320 return case_statement(token, stmt);
2323 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2325 return case_statement(token->next, stmt);
2328 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2330 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2331 stmt->type = STMT_GOTO;
2332 stmt->goto_label = target;
2333 if (!target)
2334 sparse_error(stmt->pos, "break/continue not in iterator scope");
2335 return expect(token->next, ';', "at end of statement");
2338 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2340 stmt->type = STMT_SWITCH;
2341 start_switch(stmt);
2342 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2343 token = statement(token, &stmt->switch_statement);
2344 end_switch(stmt);
2345 return token;
2348 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2350 stmt->type = STMT_GOTO;
2351 token = token->next;
2352 if (match_op(token, '*')) {
2353 token = parse_expression(token->next, &stmt->goto_expression);
2354 add_statement(&function_computed_goto_list, stmt);
2355 } else if (token_type(token) == TOKEN_IDENT) {
2356 stmt->goto_label = label_symbol(token);
2357 token = token->next;
2358 } else {
2359 sparse_error(token->pos, "Expected identifier or goto expression");
2361 return expect(token, ';', "at end of statement");
2364 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2366 stmt->type = STMT_CONTEXT;
2367 token = parse_expression(token->next, &stmt->expression);
2368 if (stmt->expression->type == EXPR_PREOP
2369 && stmt->expression->op == '('
2370 && stmt->expression->unop->type == EXPR_COMMA) {
2371 struct expression *expr;
2372 expr = stmt->expression->unop;
2373 stmt->context = expr->left;
2374 stmt->expression = expr->right;
2376 return expect(token, ';', "at end of statement");
2379 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2381 stmt->type = STMT_RANGE;
2382 token = assignment_expression(token->next, &stmt->range_expression);
2383 token = expect(token, ',', "after range expression");
2384 token = assignment_expression(token, &stmt->range_low);
2385 token = expect(token, ',', "after low range");
2386 token = assignment_expression(token, &stmt->range_high);
2387 return expect(token, ';', "after range statement");
2390 static struct token *statement(struct token *token, struct statement **tree)
2392 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2394 *tree = stmt;
2395 if (token_type(token) == TOKEN_IDENT) {
2396 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2397 if (s && s->op->statement)
2398 return s->op->statement(token, stmt);
2400 if (match_op(token->next, ':')) {
2401 struct symbol *s = label_symbol(token);
2402 stmt->type = STMT_LABEL;
2403 stmt->label_identifier = s;
2404 if (s->stmt)
2405 sparse_error(stmt->pos, "label '%s' redefined", show_ident(token->ident));
2406 s->stmt = stmt;
2407 token = skip_attributes(token->next->next);
2408 return statement(token, &stmt->label_statement);
2412 if (match_op(token, '{')) {
2413 stmt->type = STMT_COMPOUND;
2414 start_symbol_scope();
2415 token = compound_statement(token->next, stmt);
2416 end_symbol_scope();
2418 return expect(token, '}', "at end of compound statement");
2421 stmt->type = STMT_EXPRESSION;
2422 return expression_statement(token, &stmt->expression);
2425 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2426 static struct token *label_statement(struct token *token)
2428 while (token_type(token) == TOKEN_IDENT) {
2429 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2430 /* it's block-scope, but we want label namespace */
2431 bind_symbol(sym, token->ident, NS_SYMBOL);
2432 sym->namespace = NS_LABEL;
2433 fn_local_symbol(sym);
2434 token = token->next;
2435 if (!match_op(token, ','))
2436 break;
2437 token = token->next;
2439 return expect(token, ';', "at end of label declaration");
2442 static struct token * statement_list(struct token *token, struct statement_list **list)
2444 int seen_statement = 0;
2445 while (token_type(token) == TOKEN_IDENT &&
2446 token->ident == &__label___ident)
2447 token = label_statement(token->next);
2448 for (;;) {
2449 struct statement * stmt;
2450 if (eof_token(token))
2451 break;
2452 if (match_op(token, '}'))
2453 break;
2454 if (lookup_type(token)) {
2455 if (seen_statement) {
2456 warning(token->pos, "mixing declarations and code");
2457 seen_statement = 0;
2459 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2460 token = external_declaration(token, &stmt->declaration);
2461 } else {
2462 seen_statement = Wdeclarationafterstatement;
2463 token = statement(token, &stmt);
2465 add_statement(list, stmt);
2467 return token;
2470 static struct token *identifier_list(struct token *token, struct symbol *fn)
2472 struct symbol_list **list = &fn->arguments;
2473 for (;;) {
2474 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2475 sym->ident = token->ident;
2476 token = token->next;
2477 sym->endpos = token->pos;
2478 sym->ctype.base_type = &incomplete_ctype;
2479 add_symbol(list, sym);
2480 if (!match_op(token, ',') ||
2481 token_type(token->next) != TOKEN_IDENT ||
2482 lookup_type(token->next))
2483 break;
2484 token = token->next;
2486 return token;
2489 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2491 struct symbol_list **list = &fn->arguments;
2493 for (;;) {
2494 struct symbol *sym;
2496 if (match_op(token, SPECIAL_ELLIPSIS)) {
2497 fn->variadic = 1;
2498 token = token->next;
2499 break;
2502 sym = alloc_symbol(token->pos, SYM_NODE);
2503 token = parameter_declaration(token, sym);
2504 if (sym->ctype.base_type == &void_ctype) {
2505 /* Special case: (void) */
2506 if (!*list && !sym->ident)
2507 break;
2508 warning(token->pos, "void parameter");
2510 add_symbol(list, sym);
2511 if (!match_op(token, ','))
2512 break;
2513 token = token->next;
2515 return token;
2518 struct token *compound_statement(struct token *token, struct statement *stmt)
2520 token = statement_list(token, &stmt->stmts);
2521 return token;
2524 static struct expression *identifier_expression(struct token *token)
2526 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2527 expr->expr_ident = token->ident;
2528 return expr;
2531 static struct expression *index_expression(struct expression *from, struct expression *to)
2533 int idx_from, idx_to;
2534 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2536 idx_from = const_expression_value(from);
2537 idx_to = idx_from;
2538 if (to) {
2539 idx_to = const_expression_value(to);
2540 if (idx_to < idx_from || idx_from < 0)
2541 warning(from->pos, "nonsense array initializer index range");
2543 expr->idx_from = idx_from;
2544 expr->idx_to = idx_to;
2545 return expr;
2548 static struct token *single_initializer(struct expression **ep, struct token *token)
2550 int expect_equal = 0;
2551 struct token *next = token->next;
2552 struct expression **tail = ep;
2553 int nested;
2555 *ep = NULL;
2557 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2558 struct expression *expr = identifier_expression(token);
2559 if (Wold_initializer)
2560 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2561 token = initializer(&expr->ident_expression, next->next);
2562 if (expr->ident_expression)
2563 *ep = expr;
2564 return token;
2567 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2568 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2569 struct expression *expr = identifier_expression(next);
2570 *tail = expr;
2571 tail = &expr->ident_expression;
2572 expect_equal = 1;
2573 token = next->next;
2574 } else if (match_op(token, '[')) {
2575 struct expression *from = NULL, *to = NULL, *expr;
2576 token = constant_expression(token->next, &from);
2577 if (!from) {
2578 sparse_error(token->pos, "Expected constant expression");
2579 break;
2581 if (match_op(token, SPECIAL_ELLIPSIS))
2582 token = constant_expression(token->next, &to);
2583 expr = index_expression(from, to);
2584 *tail = expr;
2585 tail = &expr->idx_expression;
2586 token = expect(token, ']', "at end of initializer index");
2587 if (nested)
2588 expect_equal = 1;
2589 } else {
2590 break;
2593 if (nested && !expect_equal) {
2594 if (!match_op(token, '='))
2595 warning(token->pos, "obsolete array initializer, use C99 syntax");
2596 else
2597 expect_equal = 1;
2599 if (expect_equal)
2600 token = expect(token, '=', "at end of initializer index");
2602 token = initializer(tail, token);
2603 if (!*tail)
2604 *ep = NULL;
2605 return token;
2608 static struct token *initializer_list(struct expression_list **list, struct token *token)
2610 struct expression *expr;
2612 for (;;) {
2613 token = single_initializer(&expr, token);
2614 if (!expr)
2615 break;
2616 add_expression(list, expr);
2617 if (!match_op(token, ','))
2618 break;
2619 token = token->next;
2621 return token;
2624 struct token *initializer(struct expression **tree, struct token *token)
2626 if (match_op(token, '{')) {
2627 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2628 *tree = expr;
2629 token = initializer_list(&expr->expr_list, token->next);
2630 return expect(token, '}', "at end of initializer");
2632 return assignment_expression(token, tree);
2635 static void declare_argument(struct symbol *sym, struct symbol *fn)
2637 if (!sym->ident) {
2638 sparse_error(sym->pos, "no identifier for function argument");
2639 return;
2641 bind_symbol(sym, sym->ident, NS_SYMBOL);
2644 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2645 struct symbol_list **list)
2647 struct symbol_list **old_symbol_list;
2648 struct symbol *base_type = decl->ctype.base_type;
2649 struct statement *stmt, **p;
2650 struct symbol *prev;
2651 struct symbol *arg;
2653 old_symbol_list = function_symbol_list;
2654 if (decl->ctype.modifiers & MOD_INLINE) {
2655 function_symbol_list = &decl->inline_symbol_list;
2656 p = &base_type->inline_stmt;
2657 } else {
2658 function_symbol_list = &decl->symbol_list;
2659 p = &base_type->stmt;
2661 function_computed_target_list = NULL;
2662 function_computed_goto_list = NULL;
2664 if (decl->ctype.modifiers & MOD_EXTERN) {
2665 if (!(decl->ctype.modifiers & MOD_INLINE))
2666 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2668 if (!(decl->ctype.modifiers & MOD_STATIC))
2669 decl->ctype.modifiers |= MOD_EXTERN;
2671 stmt = start_function(decl);
2673 *p = stmt;
2674 FOR_EACH_PTR (base_type->arguments, arg) {
2675 declare_argument(arg, base_type);
2676 } END_FOR_EACH_PTR(arg);
2678 token = compound_statement(token->next, stmt);
2680 end_function(decl);
2681 if (!(decl->ctype.modifiers & MOD_INLINE))
2682 add_symbol(list, decl);
2683 check_declaration(decl);
2684 decl->definition = decl;
2685 prev = decl->same_symbol;
2686 if (prev && prev->definition) {
2687 warning(decl->pos, "multiple definitions for function '%s'",
2688 show_ident(decl->ident));
2689 info(prev->definition->pos, " the previous one is here");
2690 } else {
2691 while (prev) {
2692 rebind_scope(prev, decl->scope);
2693 prev->definition = decl;
2694 prev = prev->same_symbol;
2697 function_symbol_list = old_symbol_list;
2698 if (function_computed_goto_list) {
2699 if (!function_computed_target_list)
2700 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2701 else {
2702 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2703 stmt->target_list = function_computed_target_list;
2704 } END_FOR_EACH_PTR(stmt);
2707 return expect(token, '}', "at end of function");
2710 static void promote_k_r_types(struct symbol *arg)
2712 struct symbol *base = arg->ctype.base_type;
2713 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2714 arg->ctype.base_type = &int_ctype;
2718 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2720 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2721 struct symbol *arg;
2723 FOR_EACH_PTR(real_args, arg) {
2724 struct symbol *type;
2726 /* This is quadratic in the number of arguments. We _really_ don't care */
2727 FOR_EACH_PTR(argtypes, type) {
2728 if (type->ident == arg->ident)
2729 goto match;
2730 } END_FOR_EACH_PTR(type);
2731 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2732 continue;
2733 match:
2734 type->used = 1;
2735 /* "char" and "short" promote to "int" */
2736 promote_k_r_types(type);
2738 arg->ctype = type->ctype;
2739 } END_FOR_EACH_PTR(arg);
2741 FOR_EACH_PTR(argtypes, arg) {
2742 if (!arg->used)
2743 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2744 } END_FOR_EACH_PTR(arg);
2748 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2749 struct symbol_list **list)
2751 struct symbol_list *args = NULL;
2753 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2754 do {
2755 token = declaration_list(token, &args);
2756 if (!match_op(token, ';')) {
2757 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2758 break;
2760 token = token->next;
2761 } while (lookup_type(token));
2763 apply_k_r_types(args, decl);
2765 if (!match_op(token, '{')) {
2766 sparse_error(token->pos, "expected function body");
2767 return token;
2769 return parse_function_body(token, decl, list);
2772 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2774 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2775 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2776 struct statement *stmt;
2778 anon->ctype.base_type = fn;
2779 stmt = alloc_statement(token->pos, STMT_NONE);
2780 fn->stmt = stmt;
2782 token = parse_asm_statement(token, stmt);
2784 add_symbol(list, anon);
2785 return token;
2788 struct token *external_declaration(struct token *token, struct symbol_list **list)
2790 struct ident *ident = NULL;
2791 struct symbol *decl;
2792 struct decl_state ctx = { .ident = &ident };
2793 struct ctype saved;
2794 struct symbol *base_type;
2795 unsigned long mod;
2796 int is_typedef;
2798 /* Top-level inline asm? */
2799 if (token_type(token) == TOKEN_IDENT) {
2800 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2801 if (s && s->op->toplevel)
2802 return s->op->toplevel(token, list);
2805 /* Parse declaration-specifiers, if any */
2806 token = declaration_specifiers(token, &ctx);
2807 mod = storage_modifiers(&ctx);
2808 decl = alloc_symbol(token->pos, SYM_NODE);
2809 /* Just a type declaration? */
2810 if (match_op(token, ';')) {
2811 apply_modifiers(token->pos, &ctx);
2812 return token->next;
2815 saved = ctx.ctype;
2816 token = declarator(token, &ctx);
2817 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2818 apply_modifiers(token->pos, &ctx);
2820 decl->ctype = ctx.ctype;
2821 decl->ctype.modifiers |= mod;
2822 decl->endpos = token->pos;
2824 /* Just a type declaration? */
2825 if (!ident) {
2826 warning(token->pos, "missing identifier in declaration");
2827 return expect(token, ';', "at the end of type declaration");
2830 /* type define declaration? */
2831 is_typedef = ctx.storage_class == STypedef;
2833 /* Typedefs don't have meaningful storage */
2834 if (is_typedef)
2835 decl->ctype.modifiers |= MOD_USERTYPE;
2837 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2839 base_type = decl->ctype.base_type;
2841 if (is_typedef) {
2842 if (base_type && !base_type->ident) {
2843 switch (base_type->type) {
2844 case SYM_STRUCT:
2845 case SYM_UNION:
2846 case SYM_ENUM:
2847 case SYM_RESTRICT:
2848 base_type->ident = ident;
2849 break;
2850 default:
2851 break;
2854 } else if (base_type && base_type->type == SYM_FN) {
2855 /* K&R argument declaration? */
2856 if (lookup_type(token))
2857 return parse_k_r_arguments(token, decl, list);
2858 if (match_op(token, '{'))
2859 return parse_function_body(token, decl, list);
2861 if (!(decl->ctype.modifiers & MOD_STATIC))
2862 decl->ctype.modifiers |= MOD_EXTERN;
2863 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2864 sparse_error(token->pos, "void declaration");
2867 for (;;) {
2868 if (!is_typedef && match_op(token, '=')) {
2869 if (decl->ctype.modifiers & MOD_EXTERN) {
2870 warning(decl->pos, "symbol with external linkage has initializer");
2871 decl->ctype.modifiers &= ~MOD_EXTERN;
2873 token = initializer(&decl->initializer, token->next);
2875 if (!is_typedef) {
2876 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2877 add_symbol(list, decl);
2878 fn_local_symbol(decl);
2881 check_declaration(decl);
2882 if (decl->same_symbol) {
2883 decl->definition = decl->same_symbol->definition;
2884 decl->op = decl->same_symbol->op;
2887 if (!match_op(token, ','))
2888 break;
2890 token = token->next;
2891 ident = NULL;
2892 decl = alloc_symbol(token->pos, SYM_NODE);
2893 ctx.ctype = saved;
2894 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2895 token = declarator(token, &ctx);
2896 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2897 apply_modifiers(token->pos, &ctx);
2898 decl->ctype = ctx.ctype;
2899 decl->ctype.modifiers |= mod;
2900 decl->endpos = token->pos;
2901 if (!ident) {
2902 sparse_error(token->pos, "expected identifier name in type definition");
2903 return token;
2906 if (is_typedef)
2907 decl->ctype.modifiers |= MOD_USERTYPE;
2909 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2911 /* Function declarations are automatically extern unless specifically static */
2912 base_type = decl->ctype.base_type;
2913 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2914 if (!(decl->ctype.modifiers & MOD_STATIC))
2915 decl->ctype.modifiers |= MOD_EXTERN;
2918 return expect(token, ';', "at end of declaration");