add get_<allocator>_stats()
[smatch.git] / parse.c
blob80f0337cca29234be634026d28de1bc53cda2c61
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_bitwise,
83 attribute_address_space, attribute_context,
84 attribute_designated_init,
85 attribute_transparent_union, ignore_attribute,
86 attribute_mode, attribute_force;
88 typedef struct symbol *to_mode_t(struct symbol *);
90 static to_mode_t
91 to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode, to_word_mode;
93 enum {
94 Set_T = 1,
95 Set_S = 2,
96 Set_Char = 4,
97 Set_Int = 8,
98 Set_Double = 16,
99 Set_Float = 32,
100 Set_Signed = 64,
101 Set_Unsigned = 128,
102 Set_Short = 256,
103 Set_Long = 512,
104 Set_Vlong = 1024,
105 Set_Int128 = 2048,
106 Set_Any = Set_T | Set_Short | Set_Long | Set_Signed | Set_Unsigned
109 enum {
110 CInt = 0, CSInt, CUInt, CReal, CChar, CSChar, CUChar,
113 enum {
114 SNone = 0, STypedef, SAuto, SRegister, SExtern, SStatic, SForced, SMax,
117 static struct symbol_op typedef_op = {
118 .type = KW_MODIFIER,
119 .declarator = typedef_specifier,
122 static struct symbol_op inline_op = {
123 .type = KW_MODIFIER,
124 .declarator = inline_specifier,
127 static declarator_t noreturn_specifier;
128 static struct symbol_op noreturn_op = {
129 .type = KW_MODIFIER,
130 .declarator = noreturn_specifier,
133 static declarator_t alignas_specifier;
134 static struct symbol_op alignas_op = {
135 .type = KW_MODIFIER,
136 .declarator = alignas_specifier,
139 static struct symbol_op auto_op = {
140 .type = KW_MODIFIER,
141 .declarator = auto_specifier,
144 static struct symbol_op register_op = {
145 .type = KW_MODIFIER,
146 .declarator = register_specifier,
149 static struct symbol_op static_op = {
150 .type = KW_MODIFIER,
151 .declarator = static_specifier,
154 static struct symbol_op extern_op = {
155 .type = KW_MODIFIER,
156 .declarator = extern_specifier,
159 static struct symbol_op thread_op = {
160 .type = KW_MODIFIER,
161 .declarator = thread_specifier,
164 static struct symbol_op const_op = {
165 .type = KW_QUALIFIER,
166 .declarator = const_qualifier,
169 static struct symbol_op volatile_op = {
170 .type = KW_QUALIFIER,
171 .declarator = volatile_qualifier,
174 static struct symbol_op restrict_op = {
175 .type = KW_QUALIFIER,
178 static struct symbol_op typeof_op = {
179 .type = KW_SPECIFIER,
180 .declarator = typeof_specifier,
181 .test = Set_Any,
182 .set = Set_S|Set_T,
185 static struct symbol_op attribute_op = {
186 .type = KW_ATTRIBUTE,
187 .declarator = attribute_specifier,
190 static struct symbol_op struct_op = {
191 .type = KW_SPECIFIER,
192 .declarator = struct_specifier,
193 .test = Set_Any,
194 .set = Set_S|Set_T,
197 static struct symbol_op union_op = {
198 .type = KW_SPECIFIER,
199 .declarator = union_specifier,
200 .test = Set_Any,
201 .set = Set_S|Set_T,
204 static struct symbol_op enum_op = {
205 .type = KW_SPECIFIER,
206 .declarator = enum_specifier,
207 .test = Set_Any,
208 .set = Set_S|Set_T,
211 static struct symbol_op spec_op = {
212 .type = KW_SPECIFIER | KW_EXACT,
213 .test = Set_Any,
214 .set = Set_S|Set_T,
217 static struct symbol_op char_op = {
218 .type = KW_SPECIFIER,
219 .test = Set_T|Set_Long|Set_Short,
220 .set = Set_T|Set_Char,
221 .class = CChar,
224 static struct symbol_op int_op = {
225 .type = KW_SPECIFIER,
226 .test = Set_T,
227 .set = Set_T|Set_Int,
230 static struct symbol_op double_op = {
231 .type = KW_SPECIFIER,
232 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
233 .set = Set_T|Set_Double,
234 .class = CReal,
237 static struct symbol_op float_op = {
238 .type = KW_SPECIFIER | KW_SHORT,
239 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long,
240 .set = Set_T|Set_Float,
241 .class = CReal,
244 static struct symbol_op short_op = {
245 .type = KW_SPECIFIER | KW_SHORT,
246 .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short,
247 .set = Set_Short,
250 static struct symbol_op signed_op = {
251 .type = KW_SPECIFIER,
252 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
253 .set = Set_Signed,
254 .class = CSInt,
257 static struct symbol_op unsigned_op = {
258 .type = KW_SPECIFIER,
259 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
260 .set = Set_Unsigned,
261 .class = CUInt,
264 static struct symbol_op long_op = {
265 .type = KW_SPECIFIER | KW_LONG,
266 .test = Set_S|Set_Char|Set_Float|Set_Short|Set_Vlong,
267 .set = Set_Long,
270 static struct symbol_op int128_op = {
271 .type = KW_SPECIFIER | KW_LONG,
272 .test = Set_S|Set_T|Set_Char|Set_Short|Set_Int|Set_Float|Set_Double|Set_Long|Set_Vlong|Set_Int128,
273 .set = Set_T|Set_Int128,
276 static struct symbol_op if_op = {
277 .statement = parse_if_statement,
280 static struct symbol_op return_op = {
281 .statement = parse_return_statement,
284 static struct symbol_op loop_iter_op = {
285 .statement = parse_loop_iterator,
288 static struct symbol_op default_op = {
289 .statement = parse_default_statement,
292 static struct symbol_op case_op = {
293 .statement = parse_case_statement,
296 static struct symbol_op switch_op = {
297 .statement = parse_switch_statement,
300 static struct symbol_op for_op = {
301 .statement = parse_for_statement,
304 static struct symbol_op while_op = {
305 .statement = parse_while_statement,
308 static struct symbol_op do_op = {
309 .statement = parse_do_statement,
312 static struct symbol_op goto_op = {
313 .statement = parse_goto_statement,
316 static struct symbol_op __context___op = {
317 .statement = parse_context_statement,
320 static struct symbol_op range_op = {
321 .statement = parse_range_statement,
324 static struct symbol_op asm_op = {
325 .type = KW_ASM,
326 .declarator = parse_asm_declarator,
327 .statement = parse_asm_statement,
328 .toplevel = toplevel_asm_declaration,
331 static struct symbol_op packed_op = {
332 .attribute = attribute_packed,
335 static struct symbol_op aligned_op = {
336 .attribute = attribute_aligned,
339 static struct symbol_op attr_mod_op = {
340 .attribute = attribute_modifier,
343 static struct symbol_op attr_bitwise_op = {
344 .attribute = attribute_bitwise,
347 static struct symbol_op attr_force_op = {
348 .attribute = attribute_force,
351 static struct symbol_op address_space_op = {
352 .attribute = attribute_address_space,
355 static struct symbol_op mode_op = {
356 .attribute = attribute_mode,
359 static struct symbol_op context_op = {
360 .attribute = attribute_context,
363 static struct symbol_op designated_init_op = {
364 .attribute = attribute_designated_init,
367 static struct symbol_op transparent_union_op = {
368 .attribute = attribute_transparent_union,
371 static struct symbol_op ignore_attr_op = {
372 .attribute = ignore_attribute,
375 static struct symbol_op mode_QI_op = {
376 .type = KW_MODE,
377 .to_mode = to_QI_mode
380 static struct symbol_op mode_HI_op = {
381 .type = KW_MODE,
382 .to_mode = to_HI_mode
385 static struct symbol_op mode_SI_op = {
386 .type = KW_MODE,
387 .to_mode = to_SI_mode
390 static struct symbol_op mode_DI_op = {
391 .type = KW_MODE,
392 .to_mode = to_DI_mode
395 static struct symbol_op mode_TI_op = {
396 .type = KW_MODE,
397 .to_mode = to_TI_mode
400 static struct symbol_op mode_word_op = {
401 .type = KW_MODE,
402 .to_mode = to_word_mode
405 static struct init_keyword {
406 const char *name;
407 enum namespace ns;
408 unsigned long modifiers;
409 struct symbol_op *op;
410 struct symbol *type;
411 } keyword_table[] = {
412 /* Type qualifiers */
413 { "const", NS_TYPEDEF, .op = &const_op },
414 { "__const", NS_TYPEDEF, .op = &const_op },
415 { "__const__", NS_TYPEDEF, .op = &const_op },
416 { "volatile", NS_TYPEDEF, .op = &volatile_op },
417 { "__volatile", NS_TYPEDEF, .op = &volatile_op },
418 { "__volatile__", NS_TYPEDEF, .op = &volatile_op },
420 /* Typedef.. */
421 { "typedef", NS_TYPEDEF, .op = &typedef_op },
423 /* Type specifiers */
424 { "void", NS_TYPEDEF, .type = &void_ctype, .op = &spec_op},
425 { "char", NS_TYPEDEF, .op = &char_op },
426 { "short", NS_TYPEDEF, .op = &short_op },
427 { "int", NS_TYPEDEF, .op = &int_op },
428 { "long", NS_TYPEDEF, .op = &long_op },
429 { "float", NS_TYPEDEF, .op = &float_op },
430 { "double", NS_TYPEDEF, .op = &double_op },
431 { "signed", NS_TYPEDEF, .op = &signed_op },
432 { "__signed", NS_TYPEDEF, .op = &signed_op },
433 { "__signed__", NS_TYPEDEF, .op = &signed_op },
434 { "unsigned", NS_TYPEDEF, .op = &unsigned_op },
435 { "__int128", NS_TYPEDEF, .op = &int128_op },
436 { "_Bool", NS_TYPEDEF, .type = &bool_ctype, .op = &spec_op },
438 /* Predeclared types */
439 { "__builtin_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
440 { "__builtin_ms_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
441 { "__int128_t", NS_TYPEDEF, .type = &lllong_ctype, .op = &spec_op },
442 { "__uint128_t",NS_TYPEDEF, .type = &ulllong_ctype, .op = &spec_op },
444 /* Extended types */
445 { "typeof", NS_TYPEDEF, .op = &typeof_op },
446 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
447 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
449 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
450 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
452 { "struct", NS_TYPEDEF, .op = &struct_op },
453 { "union", NS_TYPEDEF, .op = &union_op },
454 { "enum", NS_TYPEDEF, .op = &enum_op },
456 { "inline", NS_TYPEDEF, .op = &inline_op },
457 { "__inline", NS_TYPEDEF, .op = &inline_op },
458 { "__inline__", NS_TYPEDEF, .op = &inline_op },
460 { "_Noreturn", NS_TYPEDEF, .op = &noreturn_op },
462 { "_Alignas", NS_TYPEDEF, .op = &alignas_op },
464 /* Ignored for now.. */
465 { "restrict", NS_TYPEDEF, .op = &restrict_op},
466 { "__restrict", NS_TYPEDEF, .op = &restrict_op},
467 { "__restrict__", NS_TYPEDEF, .op = &restrict_op},
469 /* Storage class */
470 { "auto", NS_TYPEDEF, .op = &auto_op },
471 { "register", NS_TYPEDEF, .op = &register_op },
472 { "static", NS_TYPEDEF, .op = &static_op },
473 { "extern", NS_TYPEDEF, .op = &extern_op },
474 { "__thread", NS_TYPEDEF, .op = &thread_op },
475 { "_Thread_local", NS_TYPEDEF, .op = &thread_op },
477 /* Statement */
478 { "if", NS_KEYWORD, .op = &if_op },
479 { "return", NS_KEYWORD, .op = &return_op },
480 { "break", NS_KEYWORD, .op = &loop_iter_op },
481 { "continue", NS_KEYWORD, .op = &loop_iter_op },
482 { "default", NS_KEYWORD, .op = &default_op },
483 { "case", NS_KEYWORD, .op = &case_op },
484 { "switch", NS_KEYWORD, .op = &switch_op },
485 { "for", NS_KEYWORD, .op = &for_op },
486 { "while", NS_KEYWORD, .op = &while_op },
487 { "do", NS_KEYWORD, .op = &do_op },
488 { "goto", NS_KEYWORD, .op = &goto_op },
489 { "__context__",NS_KEYWORD, .op = &__context___op },
490 { "__range__", NS_KEYWORD, .op = &range_op },
491 { "asm", NS_KEYWORD, .op = &asm_op },
492 { "__asm", NS_KEYWORD, .op = &asm_op },
493 { "__asm__", NS_KEYWORD, .op = &asm_op },
495 /* Attribute */
496 { "packed", NS_KEYWORD, .op = &packed_op },
497 { "__packed__", NS_KEYWORD, .op = &packed_op },
498 { "aligned", NS_KEYWORD, .op = &aligned_op },
499 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
500 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
501 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
502 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
503 { "force", NS_KEYWORD, .op = &attr_force_op },
504 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_bitwise_op },
505 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_bitwise_op },
506 { "address_space",NS_KEYWORD, .op = &address_space_op },
507 { "mode", NS_KEYWORD, .op = &mode_op },
508 { "context", NS_KEYWORD, .op = &context_op },
509 { "designated_init", NS_KEYWORD, .op = &designated_init_op },
510 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
511 { "noreturn", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
512 { "__noreturn__", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
513 { "pure", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
514 {"__pure__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
515 {"const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
516 {"__const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
517 {"__const__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
519 { "__mode__", NS_KEYWORD, .op = &mode_op },
520 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
521 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
522 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
523 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
524 { "SI", NS_KEYWORD, .op = &mode_SI_op },
525 { "__SI__", NS_KEYWORD, .op = &mode_SI_op },
526 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
527 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
528 { "TI", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
529 { "__TI__", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
530 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
531 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
534 static const char *ignored_attributes[] = {
535 "alias",
536 "__alias__",
537 "alloc_align",
538 "__alloc_align__",
539 "alloc_size",
540 "__alloc_size__",
541 "always_inline",
542 "__always_inline__",
543 "artificial",
544 "__artificial__",
545 "assume_aligned",
546 "__assume_aligned__",
547 "bounded",
548 "__bounded__",
549 "cdecl",
550 "__cdecl__",
551 "cold",
552 "__cold__",
553 "constructor",
554 "__constructor__",
555 "deprecated",
556 "__deprecated__",
557 "destructor",
558 "__destructor__",
559 "dllexport",
560 "__dllexport__",
561 "dllimport",
562 "__dllimport__",
563 "error",
564 "__error__",
565 "externally_visible",
566 "__externally_visible__",
567 "fastcall",
568 "__fastcall__",
569 "format",
570 "__format__",
571 "format_arg",
572 "__format_arg__",
573 "gnu_inline",
574 "__gnu_inline__",
575 "hot",
576 "__hot__",
577 "hotpatch",
578 "__hotpatch__",
579 "leaf",
580 "__leaf__",
581 "l1_text",
582 "__l1_text__",
583 "l1_data",
584 "__l1_data__",
585 "l2",
586 "__l2__",
587 "malloc",
588 "__malloc__",
589 "may_alias",
590 "__may_alias__",
591 "model",
592 "__model__",
593 "ms_abi",
594 "__ms_abi__",
595 "ms_hook_prologue",
596 "__ms_hook_prologue__",
597 "naked",
598 "__naked__",
599 "no_instrument_function",
600 "__no_instrument_function__",
601 "noclone",
602 "__noclone",
603 "__noclone__",
604 "noinline",
605 "__noinline__",
606 "nonnull",
607 "__nonnull",
608 "__nonnull__",
609 "nothrow",
610 "__nothrow",
611 "__nothrow__",
612 "regparm",
613 "__regparm__",
614 "section",
615 "__section__",
616 "sentinel",
617 "__sentinel__",
618 "signal",
619 "__signal__",
620 "stdcall",
621 "__stdcall__",
622 "syscall_linkage",
623 "__syscall_linkage__",
624 "sysv_abi",
625 "__sysv_abi__",
626 "unused",
627 "__unused__",
628 "used",
629 "__used__",
630 "vector_size",
631 "__vector_size__",
632 "visibility",
633 "__visibility__",
634 "warn_unused_result",
635 "__warn_unused_result__",
636 "warning",
637 "__warning__",
638 "weak",
639 "__weak__",
640 "no_sanitize_address",
641 "__no_sanitize_address__",
645 void init_parser(int stream)
647 int i;
648 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
649 struct init_keyword *ptr = keyword_table + i;
650 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
651 sym->ident->keyword = 1;
652 if (ptr->ns == NS_TYPEDEF)
653 sym->ident->reserved = 1;
654 sym->ctype.modifiers = ptr->modifiers;
655 sym->ctype.base_type = ptr->type;
656 sym->op = ptr->op;
659 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
660 const char * name = ignored_attributes[i];
661 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
662 NS_KEYWORD);
663 sym->ident->keyword = 1;
664 sym->op = &ignore_attr_op;
669 // Add a symbol to the list of function-local symbols
670 static void fn_local_symbol(struct symbol *sym)
672 if (function_symbol_list)
673 add_symbol(function_symbol_list, sym);
676 static int SENTINEL_ATTR match_idents(struct token *token, ...)
678 va_list args;
679 struct ident * next;
681 if (token_type(token) != TOKEN_IDENT)
682 return 0;
684 va_start(args, token);
685 do {
686 next = va_arg(args, struct ident *);
687 } while (next && token->ident != next);
688 va_end(args);
690 return next && token->ident == next;
694 struct statement *alloc_statement(struct position pos, int type)
696 struct statement *stmt = __alloc_statement(0);
697 stmt->type = type;
698 stmt->pos = pos;
699 return stmt;
702 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
704 static void apply_modifiers(struct position pos, struct decl_state *ctx)
706 struct symbol *ctype;
707 if (!ctx->mode)
708 return;
709 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
710 if (!ctype)
711 sparse_error(pos, "don't know how to apply mode to %s",
712 show_typename(ctx->ctype.base_type));
713 else
714 ctx->ctype.base_type = ctype;
718 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
720 struct symbol *sym = alloc_symbol(pos, type);
722 sym->ctype.base_type = ctype->base_type;
723 sym->ctype.modifiers = ctype->modifiers;
725 ctype->base_type = sym;
726 ctype->modifiers = 0;
727 return sym;
731 * NOTE! NS_LABEL is not just a different namespace,
732 * it also ends up using function scope instead of the
733 * regular symbol scope.
735 struct symbol *label_symbol(struct token *token)
737 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
738 if (!sym) {
739 sym = alloc_symbol(token->pos, SYM_LABEL);
740 bind_symbol(sym, token->ident, NS_LABEL);
741 fn_local_symbol(sym);
743 return sym;
746 static struct token *struct_union_enum_specifier(enum type type,
747 struct token *token, struct decl_state *ctx,
748 struct token *(*parse)(struct token *, struct symbol *))
750 struct symbol *sym;
751 struct position *repos;
753 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
754 if (token_type(token) == TOKEN_IDENT) {
755 sym = lookup_symbol(token->ident, NS_STRUCT);
756 if (!sym ||
757 (is_outer_scope(sym->scope) &&
758 (match_op(token->next,';') || match_op(token->next,'{')))) {
759 // Either a new symbol, or else an out-of-scope
760 // symbol being redefined.
761 sym = alloc_symbol(token->pos, type);
762 bind_symbol(sym, token->ident, NS_STRUCT);
764 if (sym->type != type)
765 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
766 ctx->ctype.base_type = sym;
767 repos = &token->pos;
768 token = token->next;
769 if (match_op(token, '{')) {
770 // The following test is actually wrong for empty
771 // structs, but (1) they are not C99, (2) gcc does
772 // the same thing, and (3) it's easier.
773 if (sym->symbol_list)
774 error_die(token->pos, "redefinition of %s", show_typename (sym));
775 sym->pos = *repos;
776 token = parse(token->next, sym);
777 token = expect(token, '}', "at end of struct-union-enum-specifier");
779 // Mark the structure as needing re-examination
780 sym->examined = 0;
781 sym->endpos = token->pos;
783 return token;
786 // private struct/union/enum type
787 if (!match_op(token, '{')) {
788 sparse_error(token->pos, "expected declaration");
789 ctx->ctype.base_type = &bad_ctype;
790 return token;
793 sym = alloc_symbol(token->pos, type);
794 token = parse(token->next, sym);
795 ctx->ctype.base_type = sym;
796 token = expect(token, '}', "at end of specifier");
797 sym->endpos = token->pos;
799 return token;
802 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
804 struct symbol *field, *last = NULL;
805 struct token *res;
806 res = struct_declaration_list(token, &sym->symbol_list);
807 FOR_EACH_PTR(sym->symbol_list, field) {
808 if (!field->ident) {
809 struct symbol *base = field->ctype.base_type;
810 if (base && base->type == SYM_BITFIELD)
811 continue;
813 if (last)
814 last->next_subobject = field;
815 last = field;
816 } END_FOR_EACH_PTR(field);
817 return res;
820 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
822 return struct_declaration_list(token, &sym->symbol_list);
825 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
827 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
830 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
832 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
836 typedef struct {
837 int x;
838 unsigned long long y;
839 } Num;
841 static void upper_boundary(Num *n, Num *v)
843 if (n->x > v->x)
844 return;
845 if (n->x < v->x) {
846 *n = *v;
847 return;
849 if (n->y < v->y)
850 n->y = v->y;
853 static void lower_boundary(Num *n, Num *v)
855 if (n->x < v->x)
856 return;
857 if (n->x > v->x) {
858 *n = *v;
859 return;
861 if (n->y > v->y)
862 n->y = v->y;
865 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
867 int shift = type->bit_size;
868 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
870 if (!is_unsigned)
871 shift--;
872 if (upper->x == 0 && upper->y >> shift)
873 return 0;
874 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
875 return 1;
876 return 0;
879 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
881 if (s1->bit_size < s2->bit_size) {
882 s1 = s2;
883 } else if (s1->bit_size == s2->bit_size) {
884 if (s2->ctype.modifiers & MOD_UNSIGNED)
885 s1 = s2;
887 if (s1->bit_size < bits_in_int)
888 return &int_ctype;
889 return s1;
892 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
894 struct symbol *sym;
896 FOR_EACH_PTR(list, sym) {
897 struct expression *expr = sym->initializer;
898 struct symbol *ctype;
899 if (expr->type != EXPR_VALUE)
900 continue;
901 ctype = expr->ctype;
902 if (ctype->bit_size == base_type->bit_size)
903 continue;
904 cast_value(expr, base_type, expr, ctype);
905 } END_FOR_EACH_PTR(sym);
908 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
910 unsigned long long lastval = 0;
911 struct symbol *ctype = NULL, *base_type = NULL;
912 Num upper = {-1, 0}, lower = {1, 0};
914 parent->examined = 1;
915 parent->ctype.base_type = &int_ctype;
916 while (token_type(token) == TOKEN_IDENT) {
917 struct expression *expr = NULL;
918 struct token *next = token->next;
919 struct symbol *sym;
921 if (match_op(next, '=')) {
922 next = constant_expression(next->next, &expr);
923 lastval = get_expression_value(expr);
924 ctype = &void_ctype;
925 if (expr && expr->ctype)
926 ctype = expr->ctype;
927 } else if (!ctype) {
928 ctype = &int_ctype;
929 } else if (is_int_type(ctype)) {
930 lastval++;
931 } else {
932 error_die(token->pos, "can't increment the last enum member");
935 if (!expr) {
936 expr = alloc_expression(token->pos, EXPR_VALUE);
937 expr->value = lastval;
938 expr->ctype = ctype;
941 sym = alloc_symbol(token->pos, SYM_NODE);
942 bind_symbol(sym, token->ident, NS_SYMBOL);
943 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
944 sym->initializer = expr;
945 sym->enum_member = 1;
946 sym->ctype.base_type = parent;
947 add_ptr_list(&parent->symbol_list, sym);
949 if (base_type != &bad_ctype) {
950 if (ctype->type == SYM_NODE)
951 ctype = ctype->ctype.base_type;
952 if (ctype->type == SYM_ENUM) {
953 if (ctype == parent)
954 ctype = base_type;
955 else
956 ctype = ctype->ctype.base_type;
959 * base_type rules:
960 * - if all enums are of the same type, then
961 * the base_type is that type (two first
962 * cases)
963 * - if enums are of different types, they
964 * all have to be integer types, and the
965 * base type is at least "int_ctype".
966 * - otherwise the base_type is "bad_ctype".
968 if (!base_type) {
969 base_type = ctype;
970 } else if (ctype == base_type) {
971 /* nothing */
972 } else if (is_int_type(base_type) && is_int_type(ctype)) {
973 base_type = bigger_enum_type(base_type, ctype);
974 } else
975 base_type = &bad_ctype;
976 parent->ctype.base_type = base_type;
978 if (is_int_type(base_type)) {
979 Num v = {.y = lastval};
980 if (ctype->ctype.modifiers & MOD_UNSIGNED)
981 v.x = 0;
982 else if ((long long)lastval >= 0)
983 v.x = 0;
984 else
985 v.x = -1;
986 upper_boundary(&upper, &v);
987 lower_boundary(&lower, &v);
989 token = next;
991 sym->endpos = token->pos;
993 if (!match_op(token, ','))
994 break;
995 token = token->next;
997 if (!base_type) {
998 sparse_error(token->pos, "bad enum definition");
999 base_type = &bad_ctype;
1001 else if (!is_int_type(base_type))
1002 base_type = base_type;
1003 else if (type_is_ok(base_type, &upper, &lower))
1004 base_type = base_type;
1005 else if (type_is_ok(&int_ctype, &upper, &lower))
1006 base_type = &int_ctype;
1007 else if (type_is_ok(&uint_ctype, &upper, &lower))
1008 base_type = &uint_ctype;
1009 else if (type_is_ok(&long_ctype, &upper, &lower))
1010 base_type = &long_ctype;
1011 else if (type_is_ok(&ulong_ctype, &upper, &lower))
1012 base_type = &ulong_ctype;
1013 else if (type_is_ok(&llong_ctype, &upper, &lower))
1014 base_type = &llong_ctype;
1015 else if (type_is_ok(&ullong_ctype, &upper, &lower))
1016 base_type = &ullong_ctype;
1017 else
1018 base_type = &bad_ctype;
1019 parent->ctype.base_type = base_type;
1020 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
1021 parent->examined = 0;
1023 cast_enum_list(parent->symbol_list, base_type);
1025 return token;
1028 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
1030 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
1031 struct ctype *ctype = &ctx->ctype.base_type->ctype;
1033 if (!ctype->base_type)
1034 ctype->base_type = &incomplete_ctype;
1036 return ret;
1039 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
1041 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
1043 struct symbol *sym;
1045 if (!match_op(token, '(')) {
1046 sparse_error(token->pos, "expected '(' after typeof");
1047 return token;
1049 if (lookup_type(token->next)) {
1050 token = typename(token->next, &sym, NULL);
1051 ctx->ctype.base_type = sym->ctype.base_type;
1052 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
1053 } else {
1054 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
1055 token = parse_expression(token->next, &typeof_sym->initializer);
1057 typeof_sym->endpos = token->pos;
1058 if (!typeof_sym->initializer) {
1059 sparse_error(token->pos, "expected expression after the '(' token");
1060 typeof_sym = &bad_ctype;
1062 ctx->ctype.base_type = typeof_sym;
1064 return expect(token, ')', "after typeof");
1067 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1069 struct expression *expr = NULL;
1070 if (match_op(token, '('))
1071 token = parens_expression(token, &expr, "in attribute");
1072 return token;
1075 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1077 if (!ctx->ctype.alignment)
1078 ctx->ctype.alignment = 1;
1079 return token;
1082 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1084 int alignment = max_alignment;
1085 struct expression *expr = NULL;
1087 if (match_op(token, '(')) {
1088 token = parens_expression(token, &expr, "in attribute");
1089 if (expr)
1090 alignment = const_expression_value(expr);
1092 if (alignment & (alignment-1)) {
1093 warning(token->pos, "I don't like non-power-of-2 alignments");
1094 return token;
1095 } else if (alignment > ctx->ctype.alignment)
1096 ctx->ctype.alignment = alignment;
1097 return token;
1100 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1102 if (ctx->modifiers & qual)
1103 warning(*pos, "duplicate %s", modifier_string(qual));
1104 ctx->modifiers |= qual;
1107 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1109 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1110 return token;
1113 static struct token *attribute_bitwise(struct token *token, struct symbol *attr, struct decl_state *ctx)
1115 if (Wbitwise)
1116 attribute_modifier(token, attr, ctx);
1117 return token;
1120 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1122 struct expression *expr = NULL;
1123 int as;
1124 token = expect(token, '(', "after address_space attribute");
1125 token = conditional_expression(token, &expr);
1126 if (expr) {
1127 as = const_expression_value(expr);
1128 if (Waddress_space && as)
1129 ctx->ctype.as = as;
1131 token = expect(token, ')', "after address_space attribute");
1132 return token;
1135 static struct symbol *to_QI_mode(struct symbol *ctype)
1137 if (ctype->ctype.base_type != &int_type)
1138 return NULL;
1139 if (ctype == &char_ctype)
1140 return ctype;
1141 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1142 : &schar_ctype;
1145 static struct symbol *to_HI_mode(struct symbol *ctype)
1147 if (ctype->ctype.base_type != &int_type)
1148 return NULL;
1149 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1150 : &sshort_ctype;
1153 static struct symbol *to_SI_mode(struct symbol *ctype)
1155 if (ctype->ctype.base_type != &int_type)
1156 return NULL;
1157 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1158 : &sint_ctype;
1161 static struct symbol *to_DI_mode(struct symbol *ctype)
1163 if (ctype->ctype.base_type != &int_type)
1164 return NULL;
1165 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1166 : &sllong_ctype;
1169 static struct symbol *to_TI_mode(struct symbol *ctype)
1171 if (ctype->ctype.base_type != &int_type)
1172 return NULL;
1173 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1174 : &slllong_ctype;
1177 static struct symbol *to_word_mode(struct symbol *ctype)
1179 if (ctype->ctype.base_type != &int_type)
1180 return NULL;
1181 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1182 : &slong_ctype;
1185 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1187 token = expect(token, '(', "after mode attribute");
1188 if (token_type(token) == TOKEN_IDENT) {
1189 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1190 if (mode && mode->op->type == KW_MODE)
1191 ctx->mode = mode->op;
1192 else
1193 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1194 token = token->next;
1195 } else
1196 sparse_error(token->pos, "expect attribute mode symbol\n");
1197 token = expect(token, ')', "after mode attribute");
1198 return token;
1201 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1203 struct context *context = alloc_context();
1204 struct expression *args[3];
1205 int argc = 0;
1207 token = expect(token, '(', "after context attribute");
1208 while (!match_op(token, ')')) {
1209 struct expression *expr = NULL;
1210 token = conditional_expression(token, &expr);
1211 if (!expr)
1212 break;
1213 if (argc < 3)
1214 args[argc++] = expr;
1215 if (!match_op(token, ','))
1216 break;
1217 token = token->next;
1220 switch(argc) {
1221 case 0:
1222 sparse_error(token->pos, "expected context input/output values");
1223 break;
1224 case 1:
1225 context->in = get_expression_value(args[0]);
1226 break;
1227 case 2:
1228 context->in = get_expression_value(args[0]);
1229 context->out = get_expression_value(args[1]);
1230 break;
1231 case 3:
1232 context->context = args[0];
1233 context->in = get_expression_value(args[1]);
1234 context->out = get_expression_value(args[2]);
1235 break;
1238 if (argc)
1239 add_ptr_list(&ctx->ctype.contexts, context);
1241 token = expect(token, ')', "after context attribute");
1242 return token;
1245 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1247 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1248 ctx->ctype.base_type->designated_init = 1;
1249 else
1250 warning(token->pos, "attribute designated_init applied to non-structure type");
1251 return token;
1254 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1256 if (Wtransparent_union)
1257 warning(token->pos, "attribute __transparent_union__");
1259 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_UNION)
1260 ctx->ctype.base_type->transparent_union = 1;
1261 else
1262 warning(token->pos, "attribute __transparent_union__ applied to non-union type");
1263 return token;
1266 static struct token *recover_unknown_attribute(struct token *token)
1268 struct expression *expr = NULL;
1270 if (Wunknown_attribute)
1271 warning(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1272 token = token->next;
1273 if (match_op(token, '('))
1274 token = parens_expression(token, &expr, "in attribute");
1275 return token;
1278 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1280 token = expect(token, '(', "after attribute");
1281 token = expect(token, '(', "after attribute");
1283 for (;;) {
1284 struct ident *attribute_name;
1285 struct symbol *attr;
1287 if (eof_token(token))
1288 break;
1289 if (match_op(token, ';'))
1290 break;
1291 if (token_type(token) != TOKEN_IDENT)
1292 break;
1293 attribute_name = token->ident;
1294 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1295 if (attr && attr->op->attribute)
1296 token = attr->op->attribute(token->next, attr, ctx);
1297 else
1298 token = recover_unknown_attribute(token);
1300 if (!match_op(token, ','))
1301 break;
1302 token = token->next;
1305 token = expect(token, ')', "after attribute");
1306 token = expect(token, ')', "after attribute");
1307 return token;
1310 static const char *storage_class[] =
1312 [STypedef] = "typedef",
1313 [SAuto] = "auto",
1314 [SExtern] = "extern",
1315 [SStatic] = "static",
1316 [SRegister] = "register",
1317 [SForced] = "[force]"
1320 static unsigned long storage_modifiers(struct decl_state *ctx)
1322 static unsigned long mod[SMax] =
1324 [SAuto] = MOD_AUTO,
1325 [SExtern] = MOD_EXTERN,
1326 [SStatic] = MOD_STATIC,
1327 [SRegister] = MOD_REGISTER
1329 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1330 | (ctx->is_tls ? MOD_TLS : 0);
1333 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1335 /* __thread can be used alone, or with extern or static */
1336 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1337 sparse_error(*pos, "__thread can only be used alone, or with "
1338 "extern or static");
1339 return;
1342 if (!ctx->storage_class) {
1343 ctx->storage_class = class;
1344 return;
1346 if (ctx->storage_class == class)
1347 sparse_error(*pos, "duplicate %s", storage_class[class]);
1348 else
1349 sparse_error(*pos, "multiple storage classes");
1352 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1354 set_storage_class(&next->pos, ctx, STypedef);
1355 return next;
1358 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1360 set_storage_class(&next->pos, ctx, SAuto);
1361 return next;
1364 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1366 set_storage_class(&next->pos, ctx, SRegister);
1367 return next;
1370 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1372 set_storage_class(&next->pos, ctx, SStatic);
1373 return next;
1376 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1378 set_storage_class(&next->pos, ctx, SExtern);
1379 return next;
1382 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1384 /* This GCC extension can be used alone, or with extern or static */
1385 if (!ctx->storage_class || ctx->storage_class == SStatic
1386 || ctx->storage_class == SExtern) {
1387 ctx->is_tls = 1;
1388 } else {
1389 sparse_error(next->pos, "__thread can only be used alone, or "
1390 "with extern or static");
1393 return next;
1396 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1398 set_storage_class(&token->pos, ctx, SForced);
1399 return token;
1402 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1404 ctx->is_inline = 1;
1405 return next;
1408 static struct token *noreturn_specifier(struct token *next, struct decl_state *ctx)
1410 apply_qualifier(&next->pos, &ctx->ctype, MOD_NORETURN);
1411 return next;
1414 static struct token *alignas_specifier(struct token *token, struct decl_state *ctx)
1416 int alignment = 0;
1418 if (!match_op(token, '(')) {
1419 sparse_error(token->pos, "expected '(' after _Alignas");
1420 return token;
1422 if (lookup_type(token->next)) {
1423 struct symbol *sym = NULL;
1424 token = typename(token->next, &sym, NULL);
1425 sym = examine_symbol_type(sym);
1426 alignment = sym->ctype.alignment;
1427 token = expect(token, ')', "after _Alignas(...");
1428 } else {
1429 struct expression *expr = NULL;
1430 token = parens_expression(token, &expr, "after _Alignas");
1431 if (!expr)
1432 return token;
1433 alignment = const_expression_value(expr);
1436 if (alignment < 0) {
1437 warning(token->pos, "non-positive alignment");
1438 return token;
1440 if (alignment & (alignment-1)) {
1441 warning(token->pos, "non-power-of-2 alignment");
1442 return token;
1444 if (alignment > ctx->ctype.alignment)
1445 ctx->ctype.alignment = alignment;
1446 return token;
1449 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1451 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1452 return next;
1455 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1457 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1458 return next;
1461 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1463 unsigned long mod = thistype->modifiers;
1465 if (mod)
1466 apply_qualifier(&pos, ctype, mod);
1468 /* Context */
1469 concat_ptr_list((struct ptr_list *)thistype->contexts,
1470 (struct ptr_list **)&ctype->contexts);
1472 /* Alignment */
1473 if (thistype->alignment > ctype->alignment)
1474 ctype->alignment = thistype->alignment;
1476 /* Address space */
1477 if (thistype->as)
1478 ctype->as = thistype->as;
1481 static void specifier_conflict(struct position pos, int what, struct ident *new)
1483 const char *old;
1484 if (what & (Set_S | Set_T))
1485 goto Catch_all;
1486 if (what & Set_Char)
1487 old = "char";
1488 else if (what & Set_Double)
1489 old = "double";
1490 else if (what & Set_Float)
1491 old = "float";
1492 else if (what & Set_Signed)
1493 old = "signed";
1494 else if (what & Set_Unsigned)
1495 old = "unsigned";
1496 else if (what & Set_Short)
1497 old = "short";
1498 else if (what & Set_Long)
1499 old = "long";
1500 else
1501 old = "long long";
1502 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1503 old, show_ident(new));
1504 return;
1506 Catch_all:
1507 sparse_error(pos, "two or more data types in declaration specifiers");
1510 static struct symbol * const int_types[] =
1511 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype, &lllong_ctype};
1512 static struct symbol * const signed_types[] =
1513 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1514 &slllong_ctype};
1515 static struct symbol * const unsigned_types[] =
1516 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1517 &ulllong_ctype};
1518 static struct symbol * const real_types[] =
1519 {&float_ctype, &double_ctype, &ldouble_ctype};
1520 static struct symbol * const char_types[] =
1521 {&char_ctype, &schar_ctype, &uchar_ctype};
1522 static struct symbol * const * const types[] = {
1523 int_types + 1, signed_types + 1, unsigned_types + 1,
1524 real_types + 1, char_types, char_types + 1, char_types + 2
1527 struct symbol *ctype_integer(int size, int want_unsigned)
1529 return types[want_unsigned ? CUInt : CInt][size];
1532 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1534 while (token_type(t) == TOKEN_IDENT) {
1535 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1536 if (!s)
1537 break;
1538 if (s->type != SYM_KEYWORD)
1539 break;
1540 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1541 break;
1542 t = t->next;
1543 if (s->op->declarator)
1544 t = s->op->declarator(t, ctx);
1546 return t;
1549 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1551 int seen = 0;
1552 int class = CInt;
1553 int size = 0;
1555 while (token_type(token) == TOKEN_IDENT) {
1556 struct symbol *s = lookup_symbol(token->ident,
1557 NS_TYPEDEF | NS_SYMBOL);
1558 if (!s || !(s->namespace & NS_TYPEDEF))
1559 break;
1560 if (s->type != SYM_KEYWORD) {
1561 if (seen & Set_Any)
1562 break;
1563 seen |= Set_S | Set_T;
1564 ctx->ctype.base_type = s->ctype.base_type;
1565 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1566 token = token->next;
1567 continue;
1569 if (s->op->type & KW_SPECIFIER) {
1570 if (seen & s->op->test) {
1571 specifier_conflict(token->pos,
1572 seen & s->op->test,
1573 token->ident);
1574 break;
1576 seen |= s->op->set;
1577 class += s->op->class;
1578 if (s->op->set & Set_Int128)
1579 size = 2;
1580 if (s->op->type & KW_SHORT) {
1581 size = -1;
1582 } else if (s->op->type & KW_LONG && size++) {
1583 if (class == CReal) {
1584 specifier_conflict(token->pos,
1585 Set_Vlong,
1586 &double_ident);
1587 break;
1589 seen |= Set_Vlong;
1592 token = token->next;
1593 if (s->op->declarator)
1594 token = s->op->declarator(token, ctx);
1595 if (s->op->type & KW_EXACT) {
1596 ctx->ctype.base_type = s->ctype.base_type;
1597 ctx->ctype.modifiers |= s->ctype.modifiers;
1601 if (!(seen & Set_S)) { /* not set explicitly? */
1602 struct symbol *base = &incomplete_ctype;
1603 if (seen & Set_Any)
1604 base = types[class][size];
1605 ctx->ctype.base_type = base;
1608 if (ctx->ctype.modifiers & MOD_BITWISE) {
1609 struct symbol *type;
1610 ctx->ctype.modifiers &= ~MOD_BITWISE;
1611 if (!is_int_type(ctx->ctype.base_type)) {
1612 sparse_error(token->pos, "invalid modifier");
1613 return token;
1615 type = alloc_symbol(token->pos, SYM_BASETYPE);
1616 *type = *ctx->ctype.base_type;
1617 type->ctype.modifiers &= ~MOD_SPECIFIER;
1618 type->ctype.base_type = ctx->ctype.base_type;
1619 type->type = SYM_RESTRICT;
1620 ctx->ctype.base_type = type;
1621 create_fouled(type);
1623 return token;
1626 static struct token *abstract_array_static_declarator(struct token *token, int *has_static)
1628 while (token->ident == &static_ident) {
1629 if (*has_static)
1630 sparse_error(token->pos, "duplicate array static declarator");
1632 *has_static = 1;
1633 token = token->next;
1635 return token;
1639 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1641 struct expression *expr = NULL;
1642 int has_static = 0;
1644 token = abstract_array_static_declarator(token, &has_static);
1646 if (match_idents(token, &restrict_ident, &__restrict_ident, &__restrict___ident, NULL))
1647 token = abstract_array_static_declarator(token->next, &has_static);
1648 token = parse_expression(token, &expr);
1649 sym->array_size = expr;
1650 return token;
1653 static struct token *parameter_type_list(struct token *, struct symbol *);
1654 static struct token *identifier_list(struct token *, struct symbol *);
1655 static struct token *declarator(struct token *token, struct decl_state *ctx);
1657 static struct token *skip_attribute(struct token *token)
1659 token = token->next;
1660 if (match_op(token, '(')) {
1661 int depth = 1;
1662 token = token->next;
1663 while (depth && !eof_token(token)) {
1664 if (token_type(token) == TOKEN_SPECIAL) {
1665 if (token->special == '(')
1666 depth++;
1667 else if (token->special == ')')
1668 depth--;
1670 token = token->next;
1673 return token;
1676 static struct token *skip_attributes(struct token *token)
1678 struct symbol *keyword;
1679 for (;;) {
1680 if (token_type(token) != TOKEN_IDENT)
1681 break;
1682 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1683 if (!keyword || keyword->type != SYM_KEYWORD)
1684 break;
1685 if (!(keyword->op->type & KW_ATTRIBUTE))
1686 break;
1687 token = expect(token->next, '(', "after attribute");
1688 token = expect(token, '(', "after attribute");
1689 for (;;) {
1690 if (eof_token(token))
1691 break;
1692 if (match_op(token, ';'))
1693 break;
1694 if (token_type(token) != TOKEN_IDENT)
1695 break;
1696 token = skip_attribute(token);
1697 if (!match_op(token, ','))
1698 break;
1699 token = token->next;
1701 token = expect(token, ')', "after attribute");
1702 token = expect(token, ')', "after attribute");
1704 return token;
1707 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1709 struct symbol *keyword;
1710 for (;;) {
1711 if (token_type(token) != TOKEN_IDENT)
1712 break;
1713 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1714 if (!keyword || keyword->type != SYM_KEYWORD)
1715 break;
1716 if (!(keyword->op->type & keywords))
1717 break;
1718 token = keyword->op->declarator(token->next, ctx);
1719 keywords &= KW_ATTRIBUTE;
1721 return token;
1724 static int is_nested(struct token *token, struct token **p,
1725 int prefer_abstract)
1728 * This can be either a parameter list or a grouping.
1729 * For the direct (non-abstract) case, we know if must be
1730 * a parameter list if we already saw the identifier.
1731 * For the abstract case, we know if must be a parameter
1732 * list if it is empty or starts with a type.
1734 struct token *next = token->next;
1736 *p = next = skip_attributes(next);
1738 if (token_type(next) == TOKEN_IDENT) {
1739 if (lookup_type(next))
1740 return !prefer_abstract;
1741 return 1;
1744 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1745 return 0;
1747 return 1;
1750 enum kind {
1751 Empty, K_R, Proto, Bad_Func,
1754 static enum kind which_func(struct token *token,
1755 struct ident **n,
1756 int prefer_abstract)
1758 struct token *next = token->next;
1760 if (token_type(next) == TOKEN_IDENT) {
1761 if (lookup_type(next))
1762 return Proto;
1763 /* identifier list not in definition; complain */
1764 if (prefer_abstract)
1765 warning(token->pos,
1766 "identifier list not in definition");
1767 return K_R;
1770 if (token_type(next) != TOKEN_SPECIAL)
1771 return Bad_Func;
1773 if (next->special == ')') {
1774 /* don't complain about those */
1775 if (!n || match_op(next->next, ';'))
1776 return Empty;
1777 warning(next->pos,
1778 "non-ANSI function declaration of function '%s'",
1779 show_ident(*n));
1780 return Empty;
1783 if (next->special == SPECIAL_ELLIPSIS) {
1784 warning(next->pos,
1785 "variadic functions must have one named argument");
1786 return Proto;
1789 return Bad_Func;
1792 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1794 struct ctype *ctype = &ctx->ctype;
1795 struct token *next;
1796 struct ident **p = ctx->ident;
1798 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1799 *ctx->ident = token->ident;
1800 token = token->next;
1801 } else if (match_op(token, '(') &&
1802 is_nested(token, &next, ctx->prefer_abstract)) {
1803 struct symbol *base_type = ctype->base_type;
1804 if (token->next != next)
1805 next = handle_attributes(token->next, ctx,
1806 KW_ATTRIBUTE);
1807 token = declarator(next, ctx);
1808 token = expect(token, ')', "in nested declarator");
1809 while (ctype->base_type != base_type)
1810 ctype = &ctype->base_type->ctype;
1811 p = NULL;
1814 if (match_op(token, '(')) {
1815 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1816 struct symbol *fn;
1817 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1818 token = token->next;
1819 if (kind == K_R)
1820 token = identifier_list(token, fn);
1821 else if (kind == Proto)
1822 token = parameter_type_list(token, fn);
1823 token = expect(token, ')', "in function declarator");
1824 fn->endpos = token->pos;
1825 return token;
1828 while (match_op(token, '[')) {
1829 struct symbol *array;
1830 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1831 token = abstract_array_declarator(token->next, array);
1832 token = expect(token, ']', "in abstract_array_declarator");
1833 array->endpos = token->pos;
1834 ctype = &array->ctype;
1836 return token;
1839 static struct token *pointer(struct token *token, struct decl_state *ctx)
1841 while (match_op(token,'*')) {
1842 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1843 ptr->ctype.modifiers = ctx->ctype.modifiers;
1844 ptr->ctype.base_type = ctx->ctype.base_type;
1845 ptr->ctype.as = ctx->ctype.as;
1846 ptr->ctype.contexts = ctx->ctype.contexts;
1847 ctx->ctype.modifiers = 0;
1848 ctx->ctype.base_type = ptr;
1849 ctx->ctype.as = 0;
1850 ctx->ctype.contexts = NULL;
1851 ctx->ctype.alignment = 0;
1853 token = handle_qualifiers(token->next, ctx);
1854 ctx->ctype.base_type->endpos = token->pos;
1856 return token;
1859 static struct token *declarator(struct token *token, struct decl_state *ctx)
1861 token = pointer(token, ctx);
1862 return direct_declarator(token, ctx);
1865 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1867 struct ctype *ctype = &ctx->ctype;
1868 struct expression *expr;
1869 struct symbol *bitfield;
1870 long long width;
1872 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1873 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1874 show_typename(ctype->base_type));
1875 // Parse this to recover gracefully.
1876 return conditional_expression(token->next, &expr);
1879 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1880 token = conditional_expression(token->next, &expr);
1881 width = const_expression_value(expr);
1882 bitfield->bit_size = width;
1884 if (width < 0 || width > INT_MAX) {
1885 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1886 width = -1;
1887 } else if (*ctx->ident && width == 0) {
1888 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1889 show_ident(*ctx->ident));
1890 width = -1;
1891 } else if (*ctx->ident) {
1892 struct symbol *base_type = bitfield->ctype.base_type;
1893 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1894 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1895 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1896 // Valid values are either {-1;0} or {0}, depending on integer
1897 // representation. The latter makes for very efficient code...
1898 sparse_error(token->pos, "dubious one-bit signed bitfield");
1900 if (Wdefault_bitfield_sign &&
1901 bitfield_type->type != SYM_ENUM &&
1902 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1903 is_signed) {
1904 // The sign of bitfields is unspecified by default.
1905 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1908 bitfield->bit_size = width;
1909 bitfield->endpos = token->pos;
1910 return token;
1913 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1915 struct decl_state ctx = {.prefer_abstract = 0};
1916 struct ctype saved;
1917 unsigned long mod;
1919 token = declaration_specifiers(token, &ctx);
1920 mod = storage_modifiers(&ctx);
1921 saved = ctx.ctype;
1922 for (;;) {
1923 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1924 ctx.ident = &decl->ident;
1926 token = declarator(token, &ctx);
1927 if (match_op(token, ':'))
1928 token = handle_bitfield(token, &ctx);
1930 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1931 apply_modifiers(token->pos, &ctx);
1933 decl->ctype = ctx.ctype;
1934 decl->ctype.modifiers |= mod;
1935 decl->endpos = token->pos;
1936 add_symbol(list, decl);
1937 if (!match_op(token, ','))
1938 break;
1939 token = token->next;
1940 ctx.ctype = saved;
1942 return token;
1945 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1947 while (!match_op(token, '}')) {
1948 if (!match_op(token, ';'))
1949 token = declaration_list(token, list);
1950 if (!match_op(token, ';')) {
1951 sparse_error(token->pos, "expected ; at end of declaration");
1952 break;
1954 token = token->next;
1956 return token;
1959 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1961 struct decl_state ctx = {.prefer_abstract = 1};
1963 token = declaration_specifiers(token, &ctx);
1964 ctx.ident = &sym->ident;
1965 token = declarator(token, &ctx);
1966 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1967 apply_modifiers(token->pos, &ctx);
1968 sym->ctype = ctx.ctype;
1969 sym->ctype.modifiers |= storage_modifiers(&ctx);
1970 sym->endpos = token->pos;
1971 sym->forced_arg = ctx.storage_class == SForced;
1972 return token;
1975 struct token *typename(struct token *token, struct symbol **p, int *forced)
1977 struct decl_state ctx = {.prefer_abstract = 1};
1978 int class;
1979 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1980 *p = sym;
1981 token = declaration_specifiers(token, &ctx);
1982 token = declarator(token, &ctx);
1983 apply_modifiers(token->pos, &ctx);
1984 sym->ctype = ctx.ctype;
1985 sym->endpos = token->pos;
1986 class = ctx.storage_class;
1987 if (forced) {
1988 *forced = 0;
1989 if (class == SForced) {
1990 *forced = 1;
1991 class = 0;
1994 if (class)
1995 warning(sym->pos, "storage class in typename (%s %s)",
1996 storage_class[class], show_typename(sym));
1997 return token;
2000 static struct token *expression_statement(struct token *token, struct expression **tree)
2002 token = parse_expression(token, tree);
2003 return expect(token, ';', "at end of statement");
2006 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
2007 struct expression_list **inout)
2009 struct expression *expr;
2011 /* Allow empty operands */
2012 if (match_op(token->next, ':') || match_op(token->next, ')'))
2013 return token->next;
2014 do {
2015 struct ident *ident = NULL;
2016 if (match_op(token->next, '[') &&
2017 token_type(token->next->next) == TOKEN_IDENT &&
2018 match_op(token->next->next->next, ']')) {
2019 ident = token->next->next->ident;
2020 token = token->next->next->next;
2022 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
2023 token = primary_expression(token->next, &expr);
2024 add_expression(inout, expr);
2025 token = parens_expression(token, &expr, "in asm parameter");
2026 add_expression(inout, expr);
2027 } while (match_op(token, ','));
2028 return token;
2031 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
2032 struct expression_list **clobbers)
2034 struct expression *expr;
2036 do {
2037 token = primary_expression(token->next, &expr);
2038 if (expr)
2039 add_expression(clobbers, expr);
2040 } while (match_op(token, ','));
2041 return token;
2044 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
2045 struct symbol_list **labels)
2047 struct symbol *label;
2049 do {
2050 token = token->next; /* skip ':' and ',' */
2051 if (token_type(token) != TOKEN_IDENT)
2052 return token;
2053 label = label_symbol(token);
2054 add_symbol(labels, label);
2055 token = token->next;
2056 } while (match_op(token, ','));
2057 return token;
2060 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
2062 int is_goto = 0;
2064 token = token->next;
2065 stmt->type = STMT_ASM;
2066 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
2067 token = token->next;
2069 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
2070 is_goto = 1;
2071 token = token->next;
2073 token = expect(token, '(', "after asm");
2074 token = parse_expression(token, &stmt->asm_string);
2075 if (match_op(token, ':'))
2076 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
2077 if (match_op(token, ':'))
2078 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
2079 if (match_op(token, ':'))
2080 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
2081 if (is_goto && match_op(token, ':'))
2082 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
2083 token = expect(token, ')', "after asm");
2084 return expect(token, ';', "at end of asm-statement");
2087 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
2089 struct expression *expr;
2090 token = expect(token, '(', "after asm");
2091 token = parse_expression(token->next, &expr);
2092 token = expect(token, ')', "after asm");
2093 return token;
2096 /* Make a statement out of an expression */
2097 static struct statement *make_statement(struct expression *expr)
2099 struct statement *stmt;
2101 if (!expr)
2102 return NULL;
2103 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
2104 stmt->expression = expr;
2105 return stmt;
2109 * All iterators have two symbols associated with them:
2110 * the "continue" and "break" symbols, which are targets
2111 * for continue and break statements respectively.
2113 * They are in a special name-space, but they follow
2114 * all the normal visibility rules, so nested iterators
2115 * automatically work right.
2117 static void start_iterator(struct statement *stmt)
2119 struct symbol *cont, *brk;
2121 start_symbol_scope();
2122 cont = alloc_symbol(stmt->pos, SYM_NODE);
2123 bind_symbol(cont, &continue_ident, NS_ITERATOR);
2124 brk = alloc_symbol(stmt->pos, SYM_NODE);
2125 bind_symbol(brk, &break_ident, NS_ITERATOR);
2127 stmt->type = STMT_ITERATOR;
2128 stmt->iterator_break = brk;
2129 stmt->iterator_continue = cont;
2130 fn_local_symbol(brk);
2131 fn_local_symbol(cont);
2134 static void end_iterator(struct statement *stmt)
2136 end_symbol_scope();
2139 static struct statement *start_function(struct symbol *sym)
2141 struct symbol *ret;
2142 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2144 start_function_scope();
2145 ret = alloc_symbol(sym->pos, SYM_NODE);
2146 ret->ctype = sym->ctype.base_type->ctype;
2147 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2148 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2149 bind_symbol(ret, &return_ident, NS_ITERATOR);
2150 stmt->ret = ret;
2151 fn_local_symbol(ret);
2153 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2154 current_fn = sym;
2156 return stmt;
2159 static void end_function(struct symbol *sym)
2161 current_fn = NULL;
2162 end_function_scope();
2166 * A "switch()" statement, like an iterator, has a
2167 * the "break" symbol associated with it. It works
2168 * exactly like the iterator break - it's the target
2169 * for any break-statements in scope, and means that
2170 * "break" handling doesn't even need to know whether
2171 * it's breaking out of an iterator or a switch.
2173 * In addition, the "case" symbol is a marker for the
2174 * case/default statements to find the switch statement
2175 * that they are associated with.
2177 static void start_switch(struct statement *stmt)
2179 struct symbol *brk, *switch_case;
2181 start_symbol_scope();
2182 brk = alloc_symbol(stmt->pos, SYM_NODE);
2183 bind_symbol(brk, &break_ident, NS_ITERATOR);
2185 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2186 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2187 switch_case->stmt = stmt;
2189 stmt->type = STMT_SWITCH;
2190 stmt->switch_break = brk;
2191 stmt->switch_case = switch_case;
2193 fn_local_symbol(brk);
2194 fn_local_symbol(switch_case);
2197 static void end_switch(struct statement *stmt)
2199 if (!stmt->switch_case->symbol_list)
2200 warning(stmt->pos, "switch with no cases");
2201 end_symbol_scope();
2204 static void add_case_statement(struct statement *stmt)
2206 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2207 struct symbol *sym;
2209 if (!target) {
2210 sparse_error(stmt->pos, "not in switch scope");
2211 stmt->type = STMT_NONE;
2212 return;
2214 sym = alloc_symbol(stmt->pos, SYM_NODE);
2215 add_symbol(&target->symbol_list, sym);
2216 sym->stmt = stmt;
2217 stmt->case_label = sym;
2218 fn_local_symbol(sym);
2221 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2223 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2225 if (!target)
2226 error_die(token->pos, "internal error: return without a function target");
2227 stmt->type = STMT_RETURN;
2228 stmt->ret_target = target;
2229 return expression_statement(token->next, &stmt->ret_value);
2232 static void validate_for_loop_decl(struct symbol *sym)
2234 unsigned long storage = sym->ctype.modifiers & MOD_STORAGE;
2236 if (storage & ~(MOD_AUTO | MOD_REGISTER)) {
2237 const char *name = show_ident(sym->ident);
2238 sparse_error(sym->pos, "non-local var '%s' in for-loop initializer", name);
2239 sym->ctype.modifiers &= ~MOD_STORAGE;
2243 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2245 struct symbol_list *syms;
2246 struct expression *e1, *e2, *e3;
2247 struct statement *iterator;
2249 start_iterator(stmt);
2250 token = expect(token->next, '(', "after 'for'");
2252 syms = NULL;
2253 e1 = NULL;
2254 /* C99 variable declaration? */
2255 if (lookup_type(token)) {
2256 token = external_declaration(token, &syms, validate_for_loop_decl);
2257 } else {
2258 token = parse_expression(token, &e1);
2259 token = expect(token, ';', "in 'for'");
2261 token = parse_expression(token, &e2);
2262 token = expect(token, ';', "in 'for'");
2263 token = parse_expression(token, &e3);
2264 token = expect(token, ')', "in 'for'");
2265 token = statement(token, &iterator);
2267 stmt->iterator_syms = syms;
2268 stmt->iterator_pre_statement = make_statement(e1);
2269 stmt->iterator_pre_condition = e2;
2270 stmt->iterator_post_statement = make_statement(e3);
2271 stmt->iterator_post_condition = NULL;
2272 stmt->iterator_statement = iterator;
2273 end_iterator(stmt);
2275 return token;
2278 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2280 struct expression *expr;
2281 struct statement *iterator;
2283 start_iterator(stmt);
2284 token = parens_expression(token->next, &expr, "after 'while'");
2285 token = statement(token, &iterator);
2287 stmt->iterator_pre_condition = expr;
2288 stmt->iterator_post_condition = NULL;
2289 stmt->iterator_statement = iterator;
2290 end_iterator(stmt);
2292 return token;
2295 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2297 struct expression *expr;
2298 struct statement *iterator;
2300 start_iterator(stmt);
2301 token = statement(token->next, &iterator);
2302 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2303 token = token->next;
2304 else
2305 sparse_error(token->pos, "expected 'while' after 'do'");
2306 token = parens_expression(token, &expr, "after 'do-while'");
2308 stmt->iterator_post_condition = expr;
2309 stmt->iterator_statement = iterator;
2310 end_iterator(stmt);
2312 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2313 warning(iterator->pos, "do-while statement is not a compound statement");
2315 return expect(token, ';', "after statement");
2318 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2320 stmt->type = STMT_IF;
2321 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2322 token = statement(token, &stmt->if_true);
2323 if (token_type(token) != TOKEN_IDENT)
2324 return token;
2325 if (token->ident != &else_ident)
2326 return token;
2327 return statement(token->next, &stmt->if_false);
2330 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2332 stmt->type = STMT_CASE;
2333 token = expect(token, ':', "after default/case");
2334 add_case_statement(stmt);
2335 return statement(token, &stmt->case_statement);
2338 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2340 token = parse_expression(token->next, &stmt->case_expression);
2341 if (match_op(token, SPECIAL_ELLIPSIS))
2342 token = parse_expression(token->next, &stmt->case_to);
2343 return case_statement(token, stmt);
2346 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2348 return case_statement(token->next, stmt);
2351 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2353 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2354 stmt->type = STMT_GOTO;
2355 stmt->goto_label = target;
2356 if (!target)
2357 sparse_error(stmt->pos, "break/continue not in iterator scope");
2358 return expect(token->next, ';', "at end of statement");
2361 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2363 stmt->type = STMT_SWITCH;
2364 start_switch(stmt);
2365 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2366 token = statement(token, &stmt->switch_statement);
2367 end_switch(stmt);
2368 return token;
2371 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2373 stmt->type = STMT_GOTO;
2374 token = token->next;
2375 if (match_op(token, '*')) {
2376 token = parse_expression(token->next, &stmt->goto_expression);
2377 add_statement(&function_computed_goto_list, stmt);
2378 } else if (token_type(token) == TOKEN_IDENT) {
2379 stmt->goto_label = label_symbol(token);
2380 token = token->next;
2381 } else {
2382 sparse_error(token->pos, "Expected identifier or goto expression");
2384 return expect(token, ';', "at end of statement");
2387 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2389 stmt->type = STMT_CONTEXT;
2390 token = parse_expression(token->next, &stmt->expression);
2391 if (stmt->expression->type == EXPR_PREOP
2392 && stmt->expression->op == '('
2393 && stmt->expression->unop->type == EXPR_COMMA) {
2394 struct expression *expr;
2395 expr = stmt->expression->unop;
2396 stmt->context = expr->left;
2397 stmt->expression = expr->right;
2399 return expect(token, ';', "at end of statement");
2402 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2404 stmt->type = STMT_RANGE;
2405 token = assignment_expression(token->next, &stmt->range_expression);
2406 token = expect(token, ',', "after range expression");
2407 token = assignment_expression(token, &stmt->range_low);
2408 token = expect(token, ',', "after low range");
2409 token = assignment_expression(token, &stmt->range_high);
2410 return expect(token, ';', "after range statement");
2413 static struct token *statement(struct token *token, struct statement **tree)
2415 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2417 *tree = stmt;
2418 if (token_type(token) == TOKEN_IDENT) {
2419 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2420 if (s && s->op->statement)
2421 return s->op->statement(token, stmt);
2423 if (match_op(token->next, ':')) {
2424 struct symbol *s = label_symbol(token);
2425 stmt->type = STMT_LABEL;
2426 stmt->label_identifier = s;
2427 if (s->stmt)
2428 sparse_error(stmt->pos, "label '%s' redefined", show_ident(token->ident));
2429 s->stmt = stmt;
2430 token = skip_attributes(token->next->next);
2431 return statement(token, &stmt->label_statement);
2435 if (match_op(token, '{')) {
2436 stmt->type = STMT_COMPOUND;
2437 start_symbol_scope();
2438 token = compound_statement(token->next, stmt);
2439 end_symbol_scope();
2441 return expect(token, '}', "at end of compound statement");
2444 stmt->type = STMT_EXPRESSION;
2445 return expression_statement(token, &stmt->expression);
2448 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2449 static struct token *label_statement(struct token *token)
2451 while (token_type(token) == TOKEN_IDENT) {
2452 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2453 /* it's block-scope, but we want label namespace */
2454 bind_symbol(sym, token->ident, NS_SYMBOL);
2455 sym->namespace = NS_LABEL;
2456 fn_local_symbol(sym);
2457 token = token->next;
2458 if (!match_op(token, ','))
2459 break;
2460 token = token->next;
2462 return expect(token, ';', "at end of label declaration");
2465 static struct token * statement_list(struct token *token, struct statement_list **list)
2467 int seen_statement = 0;
2468 while (token_type(token) == TOKEN_IDENT &&
2469 token->ident == &__label___ident)
2470 token = label_statement(token->next);
2471 for (;;) {
2472 struct statement * stmt;
2473 if (eof_token(token))
2474 break;
2475 if (match_op(token, '}'))
2476 break;
2477 if (lookup_type(token)) {
2478 if (seen_statement) {
2479 warning(token->pos, "mixing declarations and code");
2480 seen_statement = 0;
2482 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2483 token = external_declaration(token, &stmt->declaration, NULL);
2484 } else {
2485 seen_statement = Wdeclarationafterstatement;
2486 token = statement(token, &stmt);
2488 add_statement(list, stmt);
2490 return token;
2493 static struct token *identifier_list(struct token *token, struct symbol *fn)
2495 struct symbol_list **list = &fn->arguments;
2496 for (;;) {
2497 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2498 sym->ident = token->ident;
2499 token = token->next;
2500 sym->endpos = token->pos;
2501 sym->ctype.base_type = &incomplete_ctype;
2502 add_symbol(list, sym);
2503 if (!match_op(token, ',') ||
2504 token_type(token->next) != TOKEN_IDENT ||
2505 lookup_type(token->next))
2506 break;
2507 token = token->next;
2509 return token;
2512 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2514 struct symbol_list **list = &fn->arguments;
2516 for (;;) {
2517 struct symbol *sym;
2519 if (match_op(token, SPECIAL_ELLIPSIS)) {
2520 fn->variadic = 1;
2521 token = token->next;
2522 break;
2525 sym = alloc_symbol(token->pos, SYM_NODE);
2526 token = parameter_declaration(token, sym);
2527 if (sym->ctype.base_type == &void_ctype) {
2528 /* Special case: (void) */
2529 if (!*list && !sym->ident)
2530 break;
2531 warning(token->pos, "void parameter");
2533 add_symbol(list, sym);
2534 if (!match_op(token, ','))
2535 break;
2536 token = token->next;
2538 return token;
2541 struct token *compound_statement(struct token *token, struct statement *stmt)
2543 token = statement_list(token, &stmt->stmts);
2544 return token;
2547 static struct expression *identifier_expression(struct token *token)
2549 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2550 expr->expr_ident = token->ident;
2551 return expr;
2554 static struct expression *index_expression(struct expression *from, struct expression *to)
2556 int idx_from, idx_to;
2557 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2559 idx_from = const_expression_value(from);
2560 idx_to = idx_from;
2561 if (to) {
2562 idx_to = const_expression_value(to);
2563 if (idx_to < idx_from || idx_from < 0)
2564 warning(from->pos, "nonsense array initializer index range");
2566 expr->idx_from = idx_from;
2567 expr->idx_to = idx_to;
2568 return expr;
2571 static struct token *single_initializer(struct expression **ep, struct token *token)
2573 int expect_equal = 0;
2574 struct token *next = token->next;
2575 struct expression **tail = ep;
2576 int nested;
2578 *ep = NULL;
2580 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2581 struct expression *expr = identifier_expression(token);
2582 if (Wold_initializer)
2583 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2584 token = initializer(&expr->ident_expression, next->next);
2585 if (expr->ident_expression)
2586 *ep = expr;
2587 return token;
2590 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2591 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2592 struct expression *expr = identifier_expression(next);
2593 *tail = expr;
2594 tail = &expr->ident_expression;
2595 expect_equal = 1;
2596 token = next->next;
2597 } else if (match_op(token, '[')) {
2598 struct expression *from = NULL, *to = NULL, *expr;
2599 token = constant_expression(token->next, &from);
2600 if (!from) {
2601 sparse_error(token->pos, "Expected constant expression");
2602 break;
2604 if (match_op(token, SPECIAL_ELLIPSIS))
2605 token = constant_expression(token->next, &to);
2606 expr = index_expression(from, to);
2607 *tail = expr;
2608 tail = &expr->idx_expression;
2609 token = expect(token, ']', "at end of initializer index");
2610 if (nested)
2611 expect_equal = 1;
2612 } else {
2613 break;
2616 if (nested && !expect_equal) {
2617 if (!match_op(token, '='))
2618 warning(token->pos, "obsolete array initializer, use C99 syntax");
2619 else
2620 expect_equal = 1;
2622 if (expect_equal)
2623 token = expect(token, '=', "at end of initializer index");
2625 token = initializer(tail, token);
2626 if (!*tail)
2627 *ep = NULL;
2628 return token;
2631 static struct token *initializer_list(struct expression_list **list, struct token *token)
2633 struct expression *expr;
2635 for (;;) {
2636 token = single_initializer(&expr, token);
2637 if (!expr)
2638 break;
2639 add_expression(list, expr);
2640 if (!match_op(token, ','))
2641 break;
2642 token = token->next;
2644 return token;
2647 struct token *initializer(struct expression **tree, struct token *token)
2649 if (match_op(token, '{')) {
2650 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2651 *tree = expr;
2652 token = initializer_list(&expr->expr_list, token->next);
2653 return expect(token, '}', "at end of initializer");
2655 return assignment_expression(token, tree);
2658 static void declare_argument(struct symbol *sym, struct symbol *fn)
2660 if (!sym->ident) {
2661 sparse_error(sym->pos, "no identifier for function argument");
2662 return;
2664 bind_symbol(sym, sym->ident, NS_SYMBOL);
2667 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2668 struct symbol_list **list)
2670 struct symbol_list **old_symbol_list;
2671 struct symbol *base_type = decl->ctype.base_type;
2672 struct statement *stmt, **p;
2673 struct symbol *prev;
2674 struct symbol *arg;
2676 old_symbol_list = function_symbol_list;
2677 if (decl->ctype.modifiers & MOD_INLINE) {
2678 function_symbol_list = &decl->inline_symbol_list;
2679 p = &base_type->inline_stmt;
2680 } else {
2681 function_symbol_list = &decl->symbol_list;
2682 p = &base_type->stmt;
2684 function_computed_target_list = NULL;
2685 function_computed_goto_list = NULL;
2687 if (decl->ctype.modifiers & MOD_EXTERN) {
2688 if (!(decl->ctype.modifiers & MOD_INLINE))
2689 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2691 if (!(decl->ctype.modifiers & MOD_STATIC))
2692 decl->ctype.modifiers |= MOD_EXTERN;
2694 stmt = start_function(decl);
2696 *p = stmt;
2697 FOR_EACH_PTR (base_type->arguments, arg) {
2698 declare_argument(arg, base_type);
2699 } END_FOR_EACH_PTR(arg);
2701 token = compound_statement(token->next, stmt);
2703 end_function(decl);
2704 if (!(decl->ctype.modifiers & MOD_INLINE))
2705 add_symbol(list, decl);
2706 check_declaration(decl);
2707 decl->definition = decl;
2708 prev = decl->same_symbol;
2709 if (prev && prev->definition) {
2710 warning(decl->pos, "multiple definitions for function '%s'",
2711 show_ident(decl->ident));
2712 info(prev->definition->pos, " the previous one is here");
2713 } else {
2714 while (prev) {
2715 rebind_scope(prev, decl->scope);
2716 prev->definition = decl;
2717 prev = prev->same_symbol;
2720 function_symbol_list = old_symbol_list;
2721 if (function_computed_goto_list) {
2722 if (!function_computed_target_list)
2723 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2724 else {
2725 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2726 stmt->target_list = function_computed_target_list;
2727 } END_FOR_EACH_PTR(stmt);
2730 return expect(token, '}', "at end of function");
2733 static void promote_k_r_types(struct symbol *arg)
2735 struct symbol *base = arg->ctype.base_type;
2736 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2737 arg->ctype.base_type = &int_ctype;
2741 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2743 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2744 struct symbol *arg;
2746 FOR_EACH_PTR(real_args, arg) {
2747 struct symbol *type;
2749 /* This is quadratic in the number of arguments. We _really_ don't care */
2750 FOR_EACH_PTR(argtypes, type) {
2751 if (type->ident == arg->ident)
2752 goto match;
2753 } END_FOR_EACH_PTR(type);
2754 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2755 continue;
2756 match:
2757 type->used = 1;
2758 /* "char" and "short" promote to "int" */
2759 promote_k_r_types(type);
2761 arg->ctype = type->ctype;
2762 } END_FOR_EACH_PTR(arg);
2764 FOR_EACH_PTR(argtypes, arg) {
2765 if (!arg->used)
2766 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2767 } END_FOR_EACH_PTR(arg);
2771 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2772 struct symbol_list **list)
2774 struct symbol_list *args = NULL;
2776 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2777 do {
2778 token = declaration_list(token, &args);
2779 if (!match_op(token, ';')) {
2780 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2781 break;
2783 token = token->next;
2784 } while (lookup_type(token));
2786 apply_k_r_types(args, decl);
2788 if (!match_op(token, '{')) {
2789 sparse_error(token->pos, "expected function body");
2790 return token;
2792 return parse_function_body(token, decl, list);
2795 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2797 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2798 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2799 struct statement *stmt;
2801 anon->ctype.base_type = fn;
2802 stmt = alloc_statement(token->pos, STMT_NONE);
2803 fn->stmt = stmt;
2805 token = parse_asm_statement(token, stmt);
2807 add_symbol(list, anon);
2808 return token;
2811 struct token *external_declaration(struct token *token, struct symbol_list **list,
2812 validate_decl_t validate_decl)
2814 struct ident *ident = NULL;
2815 struct symbol *decl;
2816 struct decl_state ctx = { .ident = &ident };
2817 struct ctype saved;
2818 struct symbol *base_type;
2819 unsigned long mod;
2820 int is_typedef;
2822 /* Top-level inline asm? */
2823 if (token_type(token) == TOKEN_IDENT) {
2824 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2825 if (s && s->op->toplevel)
2826 return s->op->toplevel(token, list);
2829 /* Parse declaration-specifiers, if any */
2830 token = declaration_specifiers(token, &ctx);
2831 mod = storage_modifiers(&ctx);
2832 decl = alloc_symbol(token->pos, SYM_NODE);
2833 /* Just a type declaration? */
2834 if (match_op(token, ';')) {
2835 apply_modifiers(token->pos, &ctx);
2836 return token->next;
2839 saved = ctx.ctype;
2840 token = declarator(token, &ctx);
2841 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2842 apply_modifiers(token->pos, &ctx);
2844 decl->ctype = ctx.ctype;
2845 decl->ctype.modifiers |= mod;
2846 decl->endpos = token->pos;
2848 /* Just a type declaration? */
2849 if (!ident) {
2850 warning(token->pos, "missing identifier in declaration");
2851 return expect(token, ';', "at the end of type declaration");
2854 /* type define declaration? */
2855 is_typedef = ctx.storage_class == STypedef;
2857 /* Typedefs don't have meaningful storage */
2858 if (is_typedef)
2859 decl->ctype.modifiers |= MOD_USERTYPE;
2861 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2863 base_type = decl->ctype.base_type;
2865 if (is_typedef) {
2866 if (base_type && !base_type->ident) {
2867 switch (base_type->type) {
2868 case SYM_STRUCT:
2869 case SYM_UNION:
2870 case SYM_ENUM:
2871 case SYM_RESTRICT:
2872 base_type->ident = ident;
2873 break;
2874 default:
2875 break;
2878 } else if (base_type && base_type->type == SYM_FN) {
2879 /* K&R argument declaration? */
2880 if (lookup_type(token))
2881 return parse_k_r_arguments(token, decl, list);
2882 if (match_op(token, '{'))
2883 return parse_function_body(token, decl, list);
2885 if (!(decl->ctype.modifiers & MOD_STATIC))
2886 decl->ctype.modifiers |= MOD_EXTERN;
2887 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2888 sparse_error(token->pos, "void declaration");
2891 for (;;) {
2892 if (!is_typedef && match_op(token, '=')) {
2893 token = initializer(&decl->initializer, token->next);
2895 if (!is_typedef) {
2896 if (validate_decl)
2897 validate_decl(decl);
2899 if (decl->initializer && decl->ctype.modifiers & MOD_EXTERN) {
2900 warning(decl->pos, "symbol with external linkage has initializer");
2901 decl->ctype.modifiers &= ~MOD_EXTERN;
2904 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2905 add_symbol(list, decl);
2906 fn_local_symbol(decl);
2909 check_declaration(decl);
2910 if (decl->same_symbol) {
2911 decl->definition = decl->same_symbol->definition;
2912 decl->op = decl->same_symbol->op;
2915 if (!match_op(token, ','))
2916 break;
2918 token = token->next;
2919 ident = NULL;
2920 decl = alloc_symbol(token->pos, SYM_NODE);
2921 ctx.ctype = saved;
2922 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2923 token = declarator(token, &ctx);
2924 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2925 apply_modifiers(token->pos, &ctx);
2926 decl->ctype = ctx.ctype;
2927 decl->ctype.modifiers |= mod;
2928 decl->endpos = token->pos;
2929 if (!ident) {
2930 sparse_error(token->pos, "expected identifier name in type definition");
2931 return token;
2934 if (is_typedef)
2935 decl->ctype.modifiers |= MOD_USERTYPE;
2937 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2939 /* Function declarations are automatically extern unless specifically static */
2940 base_type = decl->ctype.base_type;
2941 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2942 if (!(decl->ctype.modifiers & MOD_STATIC))
2943 decl->ctype.modifiers |= MOD_EXTERN;
2946 return expect(token, ';', "at end of declaration");