data: add snprintf to kernel.sizeof_param.remove
[smatch.git] / parse.c
blobbf5894d961199473a5edceba375bcdb03683f290
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 * Licensed under the Open Software License version 1.1
13 #include <stdarg.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <limits.h>
22 #include "lib.h"
23 #include "allocate.h"
24 #include "token.h"
25 #include "parse.h"
26 #include "symbol.h"
27 #include "scope.h"
28 #include "expression.h"
29 #include "target.h"
31 static struct symbol_list **function_symbol_list;
32 struct symbol_list *function_computed_target_list;
33 struct statement_list *function_computed_goto_list;
35 static struct token *statement(struct token *token, struct statement **tree);
36 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords);
38 typedef struct token *declarator_t(struct token *, struct decl_state *);
39 static declarator_t
40 struct_specifier, union_specifier, enum_specifier,
41 attribute_specifier, typeof_specifier, parse_asm_declarator,
42 typedef_specifier, inline_specifier, auto_specifier,
43 register_specifier, static_specifier, extern_specifier,
44 thread_specifier, const_qualifier, volatile_qualifier;
46 static struct token *parse_if_statement(struct token *token, struct statement *stmt);
47 static struct token *parse_return_statement(struct token *token, struct statement *stmt);
48 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt);
49 static struct token *parse_default_statement(struct token *token, struct statement *stmt);
50 static struct token *parse_case_statement(struct token *token, struct statement *stmt);
51 static struct token *parse_switch_statement(struct token *token, struct statement *stmt);
52 static struct token *parse_for_statement(struct token *token, struct statement *stmt);
53 static struct token *parse_while_statement(struct token *token, struct statement *stmt);
54 static struct token *parse_do_statement(struct token *token, struct statement *stmt);
55 static struct token *parse_goto_statement(struct token *token, struct statement *stmt);
56 static struct token *parse_context_statement(struct token *token, struct statement *stmt);
57 static struct token *parse_range_statement(struct token *token, struct statement *stmt);
58 static struct token *parse_asm_statement(struct token *token, struct statement *stmt);
59 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list);
61 typedef struct token *attr_t(struct token *, struct symbol *,
62 struct decl_state *);
64 static attr_t
65 attribute_packed, attribute_aligned, attribute_modifier,
66 attribute_address_space, attribute_context,
67 attribute_designated_init,
68 attribute_transparent_union, ignore_attribute,
69 attribute_mode, attribute_force;
71 typedef struct symbol *to_mode_t(struct symbol *);
73 static to_mode_t
74 to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode, to_word_mode;
76 enum {
77 Set_T = 1,
78 Set_S = 2,
79 Set_Char = 4,
80 Set_Int = 8,
81 Set_Double = 16,
82 Set_Float = 32,
83 Set_Signed = 64,
84 Set_Unsigned = 128,
85 Set_Short = 256,
86 Set_Long = 512,
87 Set_Vlong = 1024,
88 Set_Any = Set_T | Set_Short | Set_Long | Set_Signed | Set_Unsigned
91 enum {
92 CInt = 0, CSInt, CUInt, CReal, CChar, CSChar, CUChar
95 enum {
96 SNone = 0, STypedef, SAuto, SRegister, SExtern, SStatic, SForced
99 static struct symbol_op typedef_op = {
100 .type = KW_MODIFIER,
101 .declarator = typedef_specifier,
104 static struct symbol_op inline_op = {
105 .type = KW_MODIFIER,
106 .declarator = inline_specifier,
109 static struct symbol_op auto_op = {
110 .type = KW_MODIFIER,
111 .declarator = auto_specifier,
114 static struct symbol_op register_op = {
115 .type = KW_MODIFIER,
116 .declarator = register_specifier,
119 static struct symbol_op static_op = {
120 .type = KW_MODIFIER,
121 .declarator = static_specifier,
124 static struct symbol_op extern_op = {
125 .type = KW_MODIFIER,
126 .declarator = extern_specifier,
129 static struct symbol_op thread_op = {
130 .type = KW_MODIFIER,
131 .declarator = thread_specifier,
134 static struct symbol_op const_op = {
135 .type = KW_QUALIFIER,
136 .declarator = const_qualifier,
139 static struct symbol_op volatile_op = {
140 .type = KW_QUALIFIER,
141 .declarator = volatile_qualifier,
144 static struct symbol_op restrict_op = {
145 .type = KW_QUALIFIER,
148 static struct symbol_op typeof_op = {
149 .type = KW_SPECIFIER,
150 .declarator = typeof_specifier,
151 .test = Set_Any,
152 .set = Set_S|Set_T,
155 static struct symbol_op attribute_op = {
156 .type = KW_ATTRIBUTE,
157 .declarator = attribute_specifier,
160 static struct symbol_op struct_op = {
161 .type = KW_SPECIFIER,
162 .declarator = struct_specifier,
163 .test = Set_Any,
164 .set = Set_S|Set_T,
167 static struct symbol_op union_op = {
168 .type = KW_SPECIFIER,
169 .declarator = union_specifier,
170 .test = Set_Any,
171 .set = Set_S|Set_T,
174 static struct symbol_op enum_op = {
175 .type = KW_SPECIFIER,
176 .declarator = enum_specifier,
177 .test = Set_Any,
178 .set = Set_S|Set_T,
181 static struct symbol_op spec_op = {
182 .type = KW_SPECIFIER | KW_EXACT,
183 .test = Set_Any,
184 .set = Set_S|Set_T,
187 static struct symbol_op char_op = {
188 .type = KW_SPECIFIER,
189 .test = Set_T|Set_Long|Set_Short,
190 .set = Set_T|Set_Char,
191 .class = CChar,
194 static struct symbol_op int_op = {
195 .type = KW_SPECIFIER,
196 .test = Set_T,
197 .set = Set_T|Set_Int,
200 static struct symbol_op double_op = {
201 .type = KW_SPECIFIER,
202 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
203 .set = Set_T|Set_Double,
204 .class = CReal,
207 static struct symbol_op float_op = {
208 .type = KW_SPECIFIER | KW_SHORT,
209 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long,
210 .set = Set_T|Set_Float,
211 .class = CReal,
214 static struct symbol_op short_op = {
215 .type = KW_SPECIFIER | KW_SHORT,
216 .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short,
217 .set = Set_Short,
220 static struct symbol_op signed_op = {
221 .type = KW_SPECIFIER,
222 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
223 .set = Set_Signed,
224 .class = CSInt,
227 static struct symbol_op unsigned_op = {
228 .type = KW_SPECIFIER,
229 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned,
230 .set = Set_Unsigned,
231 .class = CUInt,
234 static struct symbol_op long_op = {
235 .type = KW_SPECIFIER | KW_LONG,
236 .test = Set_S|Set_Char|Set_Float|Set_Short|Set_Vlong,
237 .set = Set_Long,
240 static struct symbol_op if_op = {
241 .statement = parse_if_statement,
244 static struct symbol_op return_op = {
245 .statement = parse_return_statement,
248 static struct symbol_op loop_iter_op = {
249 .statement = parse_loop_iterator,
252 static struct symbol_op default_op = {
253 .statement = parse_default_statement,
256 static struct symbol_op case_op = {
257 .statement = parse_case_statement,
260 static struct symbol_op switch_op = {
261 .statement = parse_switch_statement,
264 static struct symbol_op for_op = {
265 .statement = parse_for_statement,
268 static struct symbol_op while_op = {
269 .statement = parse_while_statement,
272 static struct symbol_op do_op = {
273 .statement = parse_do_statement,
276 static struct symbol_op goto_op = {
277 .statement = parse_goto_statement,
280 static struct symbol_op __context___op = {
281 .statement = parse_context_statement,
284 static struct symbol_op range_op = {
285 .statement = parse_range_statement,
288 static struct symbol_op asm_op = {
289 .type = KW_ASM,
290 .declarator = parse_asm_declarator,
291 .statement = parse_asm_statement,
292 .toplevel = toplevel_asm_declaration,
295 static struct symbol_op packed_op = {
296 .attribute = attribute_packed,
299 static struct symbol_op aligned_op = {
300 .attribute = attribute_aligned,
303 static struct symbol_op attr_mod_op = {
304 .attribute = attribute_modifier,
307 static struct symbol_op attr_force_op = {
308 .attribute = attribute_force,
311 static struct symbol_op address_space_op = {
312 .attribute = attribute_address_space,
315 static struct symbol_op mode_op = {
316 .attribute = attribute_mode,
319 static struct symbol_op context_op = {
320 .attribute = attribute_context,
323 static struct symbol_op designated_init_op = {
324 .attribute = attribute_designated_init,
327 static struct symbol_op transparent_union_op = {
328 .attribute = attribute_transparent_union,
331 static struct symbol_op ignore_attr_op = {
332 .attribute = ignore_attribute,
335 static struct symbol_op mode_QI_op = {
336 .type = KW_MODE,
337 .to_mode = to_QI_mode
340 static struct symbol_op mode_HI_op = {
341 .type = KW_MODE,
342 .to_mode = to_HI_mode
345 static struct symbol_op mode_SI_op = {
346 .type = KW_MODE,
347 .to_mode = to_SI_mode
350 static struct symbol_op mode_DI_op = {
351 .type = KW_MODE,
352 .to_mode = to_DI_mode
355 static struct symbol_op mode_TI_op = {
356 .type = KW_MODE,
357 .to_mode = to_TI_mode
360 static struct symbol_op mode_word_op = {
361 .type = KW_MODE,
362 .to_mode = to_word_mode
365 static struct init_keyword {
366 const char *name;
367 enum namespace ns;
368 unsigned long modifiers;
369 struct symbol_op *op;
370 struct symbol *type;
371 } keyword_table[] = {
372 /* Type qualifiers */
373 { "const", NS_TYPEDEF, .op = &const_op },
374 { "__const", NS_TYPEDEF, .op = &const_op },
375 { "__const__", NS_TYPEDEF, .op = &const_op },
376 { "volatile", NS_TYPEDEF, .op = &volatile_op },
377 { "__volatile", NS_TYPEDEF, .op = &volatile_op },
378 { "__volatile__", NS_TYPEDEF, .op = &volatile_op },
380 /* Typedef.. */
381 { "typedef", NS_TYPEDEF, .op = &typedef_op },
383 /* Type specifiers */
384 { "void", NS_TYPEDEF, .type = &void_ctype, .op = &spec_op},
385 { "char", NS_TYPEDEF, .op = &char_op },
386 { "short", NS_TYPEDEF, .op = &short_op },
387 { "int", NS_TYPEDEF, .op = &int_op },
388 { "long", NS_TYPEDEF, .op = &long_op },
389 { "float", NS_TYPEDEF, .op = &float_op },
390 { "double", NS_TYPEDEF, .op = &double_op },
391 { "signed", NS_TYPEDEF, .op = &signed_op },
392 { "__signed", NS_TYPEDEF, .op = &signed_op },
393 { "__signed__", NS_TYPEDEF, .op = &signed_op },
394 { "unsigned", NS_TYPEDEF, .op = &unsigned_op },
395 { "_Bool", NS_TYPEDEF, .type = &bool_ctype, .op = &spec_op },
397 /* Predeclared types */
398 { "__builtin_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
399 { "__builtin_ms_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
400 { "__int128_t", NS_TYPEDEF, .type = &lllong_ctype, .op = &spec_op },
401 { "__uint128_t",NS_TYPEDEF, .type = &ulllong_ctype, .op = &spec_op },
403 /* Extended types */
404 { "typeof", NS_TYPEDEF, .op = &typeof_op },
405 { "__typeof", NS_TYPEDEF, .op = &typeof_op },
406 { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
408 { "__attribute", NS_TYPEDEF, .op = &attribute_op },
409 { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
411 { "struct", NS_TYPEDEF, .op = &struct_op },
412 { "union", NS_TYPEDEF, .op = &union_op },
413 { "enum", NS_TYPEDEF, .op = &enum_op },
415 { "inline", NS_TYPEDEF, .op = &inline_op },
416 { "__inline", NS_TYPEDEF, .op = &inline_op },
417 { "__inline__", NS_TYPEDEF, .op = &inline_op },
419 /* Ignored for now.. */
420 { "restrict", NS_TYPEDEF, .op = &restrict_op},
421 { "__restrict", NS_TYPEDEF, .op = &restrict_op},
422 { "__restrict__", NS_TYPEDEF, .op = &restrict_op},
424 /* Storage class */
425 { "auto", NS_TYPEDEF, .op = &auto_op },
426 { "register", NS_TYPEDEF, .op = &register_op },
427 { "static", NS_TYPEDEF, .op = &static_op },
428 { "extern", NS_TYPEDEF, .op = &extern_op },
429 { "__thread", NS_TYPEDEF, .op = &thread_op },
431 /* Statement */
432 { "if", NS_KEYWORD, .op = &if_op },
433 { "return", NS_KEYWORD, .op = &return_op },
434 { "break", NS_KEYWORD, .op = &loop_iter_op },
435 { "continue", NS_KEYWORD, .op = &loop_iter_op },
436 { "default", NS_KEYWORD, .op = &default_op },
437 { "case", NS_KEYWORD, .op = &case_op },
438 { "switch", NS_KEYWORD, .op = &switch_op },
439 { "for", NS_KEYWORD, .op = &for_op },
440 { "while", NS_KEYWORD, .op = &while_op },
441 { "do", NS_KEYWORD, .op = &do_op },
442 { "goto", NS_KEYWORD, .op = &goto_op },
443 { "__context__",NS_KEYWORD, .op = &__context___op },
444 { "__range__", NS_KEYWORD, .op = &range_op },
445 { "asm", NS_KEYWORD, .op = &asm_op },
446 { "__asm", NS_KEYWORD, .op = &asm_op },
447 { "__asm__", NS_KEYWORD, .op = &asm_op },
449 /* Attribute */
450 { "packed", NS_KEYWORD, .op = &packed_op },
451 { "__packed__", NS_KEYWORD, .op = &packed_op },
452 { "aligned", NS_KEYWORD, .op = &aligned_op },
453 { "__aligned__",NS_KEYWORD, .op = &aligned_op },
454 { "nocast", NS_KEYWORD, MOD_NOCAST, .op = &attr_mod_op },
455 { "noderef", NS_KEYWORD, MOD_NODEREF, .op = &attr_mod_op },
456 { "safe", NS_KEYWORD, MOD_SAFE, .op = &attr_mod_op },
457 { "force", NS_KEYWORD, .op = &attr_force_op },
458 { "bitwise", NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
459 { "__bitwise__",NS_KEYWORD, MOD_BITWISE, .op = &attr_mod_op },
460 { "address_space",NS_KEYWORD, .op = &address_space_op },
461 { "mode", NS_KEYWORD, .op = &mode_op },
462 { "context", NS_KEYWORD, .op = &context_op },
463 { "designated_init", NS_KEYWORD, .op = &designated_init_op },
464 { "__transparent_union__", NS_KEYWORD, .op = &transparent_union_op },
465 { "noreturn", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
466 { "__noreturn__", NS_KEYWORD, MOD_NORETURN, .op = &attr_mod_op },
467 { "pure", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
468 {"__pure__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
469 {"const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
470 {"__const", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
471 {"__const__", NS_KEYWORD, MOD_PURE, .op = &attr_mod_op },
473 { "__mode__", NS_KEYWORD, .op = &mode_op },
474 { "QI", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
475 { "__QI__", NS_KEYWORD, MOD_CHAR, .op = &mode_QI_op },
476 { "HI", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
477 { "__HI__", NS_KEYWORD, MOD_SHORT, .op = &mode_HI_op },
478 { "SI", NS_KEYWORD, .op = &mode_SI_op },
479 { "__SI__", NS_KEYWORD, .op = &mode_SI_op },
480 { "DI", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
481 { "__DI__", NS_KEYWORD, MOD_LONGLONG, .op = &mode_DI_op },
482 { "TI", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
483 { "__TI__", NS_KEYWORD, MOD_LONGLONGLONG, .op = &mode_TI_op },
484 { "word", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
485 { "__word__", NS_KEYWORD, MOD_LONG, .op = &mode_word_op },
488 const char *ignored_attributes[] = {
489 "alias",
490 "__alias__",
491 "alloc_size",
492 "__alloc_size__",
493 "always_inline",
494 "__always_inline__",
495 "artificial",
496 "__artificial__",
497 "bounded",
498 "__bounded__",
499 "cdecl",
500 "__cdecl__",
501 "cold",
502 "__cold__",
503 "constructor",
504 "__constructor__",
505 "deprecated",
506 "__deprecated__",
507 "destructor",
508 "__destructor__",
509 "dllexport",
510 "__dllexport__",
511 "dllimport",
512 "__dllimport__",
513 "error",
514 "__error__",
515 "externally_visible",
516 "__externally_visible__",
517 "fastcall",
518 "__fastcall__",
519 "format",
520 "__format__",
521 "format_arg",
522 "__format_arg__",
523 "hot",
524 "__hot__",
525 "leaf",
526 "__leaf__",
527 "l1_text",
528 "__l1_text__",
529 "l1_data",
530 "__l1_data__",
531 "l2",
532 "__l2__",
533 "may_alias",
534 "__may_alias__",
535 "malloc",
536 "__malloc__",
537 "may_alias",
538 "__may_alias__",
539 "model",
540 "__model__",
541 "ms_abi",
542 "__ms_abi__",
543 "ms_hook_prologue",
544 "__ms_hook_prologue__",
545 "naked",
546 "__naked__",
547 "no_instrument_function",
548 "__no_instrument_function__",
549 "noinline",
550 "__noinline__",
551 "nonnull",
552 "__nonnull",
553 "__nonnull__",
554 "nothrow",
555 "__nothrow",
556 "__nothrow__",
557 "regparm",
558 "__regparm__",
559 "section",
560 "__section__",
561 "sentinel",
562 "__sentinel__",
563 "signal",
564 "__signal__",
565 "stdcall",
566 "__stdcall__",
567 "syscall_linkage",
568 "__syscall_linkage__",
569 "sysv_abi",
570 "__sysv_abi__",
571 "unused",
572 "__unused__",
573 "used",
574 "__used__",
575 "vector_size",
576 "__vector_size__",
577 "visibility",
578 "__visibility__",
579 "warn_unused_result",
580 "__warn_unused_result__",
581 "warning",
582 "__warning__",
583 "weak",
584 "__weak__",
588 void init_parser(int stream)
590 int i;
591 for (i = 0; i < ARRAY_SIZE(keyword_table); i++) {
592 struct init_keyword *ptr = keyword_table + i;
593 struct symbol *sym = create_symbol(stream, ptr->name, SYM_KEYWORD, ptr->ns);
594 sym->ident->keyword = 1;
595 if (ptr->ns == NS_TYPEDEF)
596 sym->ident->reserved = 1;
597 sym->ctype.modifiers = ptr->modifiers;
598 sym->ctype.base_type = ptr->type;
599 sym->op = ptr->op;
602 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) {
603 const char * name = ignored_attributes[i];
604 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
605 NS_KEYWORD);
606 sym->ident->keyword = 1;
607 sym->op = &ignore_attr_op;
612 // Add a symbol to the list of function-local symbols
613 static void fn_local_symbol(struct symbol *sym)
615 if (function_symbol_list)
616 add_symbol(function_symbol_list, sym);
619 static int SENTINEL_ATTR match_idents(struct token *token, ...)
621 va_list args;
622 struct ident * next;
624 if (token_type(token) != TOKEN_IDENT)
625 return 0;
627 va_start(args, token);
628 do {
629 next = va_arg(args, struct ident *);
630 } while (next && token->ident != next);
631 va_end(args);
633 return next && token->ident == next;
637 struct statement *alloc_statement(struct position pos, int type)
639 struct statement *stmt = __alloc_statement(0);
640 stmt->type = type;
641 stmt->pos = pos;
642 return stmt;
645 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
647 static void apply_modifiers(struct position pos, struct decl_state *ctx)
649 struct symbol *ctype;
650 if (!ctx->mode)
651 return;
652 ctype = ctx->mode->to_mode(ctx->ctype.base_type);
653 if (!ctype)
654 sparse_error(pos, "don't know how to apply mode to %s",
655 show_typename(ctx->ctype.base_type));
656 else
657 ctx->ctype.base_type = ctype;
661 static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type)
663 struct symbol *sym = alloc_symbol(pos, type);
665 sym->ctype.base_type = ctype->base_type;
666 sym->ctype.modifiers = ctype->modifiers;
668 ctype->base_type = sym;
669 ctype->modifiers = 0;
670 return sym;
674 * NOTE! NS_LABEL is not just a different namespace,
675 * it also ends up using function scope instead of the
676 * regular symbol scope.
678 struct symbol *label_symbol(struct token *token)
680 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL);
681 if (!sym) {
682 sym = alloc_symbol(token->pos, SYM_LABEL);
683 bind_symbol(sym, token->ident, NS_LABEL);
684 fn_local_symbol(sym);
686 return sym;
689 static struct token *struct_union_enum_specifier(enum type type,
690 struct token *token, struct decl_state *ctx,
691 struct token *(*parse)(struct token *, struct symbol *))
693 struct symbol *sym;
694 struct position *repos;
696 token = handle_attributes(token, ctx, KW_ATTRIBUTE);
697 if (token_type(token) == TOKEN_IDENT) {
698 sym = lookup_symbol(token->ident, NS_STRUCT);
699 if (!sym ||
700 (is_outer_scope(sym->scope) &&
701 (match_op(token->next,';') || match_op(token->next,'{')))) {
702 // Either a new symbol, or else an out-of-scope
703 // symbol being redefined.
704 sym = alloc_symbol(token->pos, type);
705 bind_symbol(sym, token->ident, NS_STRUCT);
707 if (sym->type != type)
708 error_die(token->pos, "invalid tag applied to %s", show_typename (sym));
709 ctx->ctype.base_type = sym;
710 repos = &token->pos;
711 token = token->next;
712 if (match_op(token, '{')) {
713 // The following test is actually wrong for empty
714 // structs, but (1) they are not C99, (2) gcc does
715 // the same thing, and (3) it's easier.
716 if (sym->symbol_list)
717 error_die(token->pos, "redefinition of %s", show_typename (sym));
718 sym->pos = *repos;
719 token = parse(token->next, sym);
720 token = expect(token, '}', "at end of struct-union-enum-specifier");
722 // Mark the structure as needing re-examination
723 sym->examined = 0;
724 sym->endpos = token->pos;
726 return token;
729 // private struct/union/enum type
730 if (!match_op(token, '{')) {
731 sparse_error(token->pos, "expected declaration");
732 ctx->ctype.base_type = &bad_ctype;
733 return token;
736 sym = alloc_symbol(token->pos, type);
737 token = parse(token->next, sym);
738 ctx->ctype.base_type = sym;
739 token = expect(token, '}', "at end of specifier");
740 sym->endpos = token->pos;
742 return token;
745 static struct token *parse_struct_declaration(struct token *token, struct symbol *sym)
747 struct symbol *field, *last = NULL;
748 struct token *res;
749 res = struct_declaration_list(token, &sym->symbol_list);
750 FOR_EACH_PTR(sym->symbol_list, field) {
751 if (!field->ident) {
752 struct symbol *base = field->ctype.base_type;
753 if (base && base->type == SYM_BITFIELD)
754 continue;
756 if (last)
757 last->next_subobject = field;
758 last = field;
759 } END_FOR_EACH_PTR(field);
760 return res;
763 static struct token *parse_union_declaration(struct token *token, struct symbol *sym)
765 return struct_declaration_list(token, &sym->symbol_list);
768 static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
770 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
773 static struct token *union_specifier(struct token *token, struct decl_state *ctx)
775 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
779 typedef struct {
780 int x;
781 unsigned long long y;
782 } Num;
784 static void upper_boundary(Num *n, Num *v)
786 if (n->x > v->x)
787 return;
788 if (n->x < v->x) {
789 *n = *v;
790 return;
792 if (n->y < v->y)
793 n->y = v->y;
796 static void lower_boundary(Num *n, Num *v)
798 if (n->x < v->x)
799 return;
800 if (n->x > v->x) {
801 *n = *v;
802 return;
804 if (n->y > v->y)
805 n->y = v->y;
808 static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
810 int shift = type->bit_size;
811 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
813 if (!is_unsigned)
814 shift--;
815 if (upper->x == 0 && upper->y >> shift)
816 return 0;
817 if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
818 return 1;
819 return 0;
822 static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
824 if (s1->bit_size < s2->bit_size) {
825 s1 = s2;
826 } else if (s1->bit_size == s2->bit_size) {
827 if (s2->ctype.modifiers & MOD_UNSIGNED)
828 s1 = s2;
830 if (s1->bit_size < bits_in_int)
831 return &int_ctype;
832 return s1;
835 static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
837 struct symbol *sym;
839 FOR_EACH_PTR(list, sym) {
840 struct expression *expr = sym->initializer;
841 struct symbol *ctype;
842 if (expr->type != EXPR_VALUE)
843 continue;
844 ctype = expr->ctype;
845 if (ctype->bit_size == base_type->bit_size)
846 continue;
847 cast_value(expr, base_type, expr, ctype);
848 } END_FOR_EACH_PTR(sym);
851 static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
853 unsigned long long lastval = 0;
854 struct symbol *ctype = NULL, *base_type = NULL;
855 Num upper = {-1, 0}, lower = {1, 0};
857 parent->examined = 1;
858 parent->ctype.base_type = &int_ctype;
859 while (token_type(token) == TOKEN_IDENT) {
860 struct expression *expr = NULL;
861 struct token *next = token->next;
862 struct symbol *sym;
864 if (match_op(next, '=')) {
865 next = constant_expression(next->next, &expr);
866 lastval = get_expression_value(expr);
867 ctype = &void_ctype;
868 if (expr && expr->ctype)
869 ctype = expr->ctype;
870 } else if (!ctype) {
871 ctype = &int_ctype;
872 } else if (is_int_type(ctype)) {
873 lastval++;
874 } else {
875 error_die(token->pos, "can't increment the last enum member");
878 if (!expr) {
879 expr = alloc_expression(token->pos, EXPR_VALUE);
880 expr->value = lastval;
881 expr->ctype = ctype;
884 sym = alloc_symbol(token->pos, SYM_NODE);
885 bind_symbol(sym, token->ident, NS_SYMBOL);
886 sym->ctype.modifiers &= ~MOD_ADDRESSABLE;
887 sym->initializer = expr;
888 sym->enum_member = 1;
889 sym->ctype.base_type = parent;
890 add_ptr_list(&parent->symbol_list, sym);
892 if (base_type != &bad_ctype) {
893 if (ctype->type == SYM_NODE)
894 ctype = ctype->ctype.base_type;
895 if (ctype->type == SYM_ENUM) {
896 if (ctype == parent)
897 ctype = base_type;
898 else
899 ctype = ctype->ctype.base_type;
902 * base_type rules:
903 * - if all enums are of the same type, then
904 * the base_type is that type (two first
905 * cases)
906 * - if enums are of different types, they
907 * all have to be integer types, and the
908 * base type is at least "int_ctype".
909 * - otherwise the base_type is "bad_ctype".
911 if (!base_type) {
912 base_type = ctype;
913 } else if (ctype == base_type) {
914 /* nothing */
915 } else if (is_int_type(base_type) && is_int_type(ctype)) {
916 base_type = bigger_enum_type(base_type, ctype);
917 } else
918 base_type = &bad_ctype;
919 parent->ctype.base_type = base_type;
921 if (is_int_type(base_type)) {
922 Num v = {.y = lastval};
923 if (ctype->ctype.modifiers & MOD_UNSIGNED)
924 v.x = 0;
925 else if ((long long)lastval >= 0)
926 v.x = 0;
927 else
928 v.x = -1;
929 upper_boundary(&upper, &v);
930 lower_boundary(&lower, &v);
932 token = next;
934 sym->endpos = token->pos;
936 if (!match_op(token, ','))
937 break;
938 token = token->next;
940 if (!base_type) {
941 sparse_error(token->pos, "bad enum definition");
942 base_type = &bad_ctype;
944 else if (!is_int_type(base_type))
945 base_type = base_type;
946 else if (type_is_ok(base_type, &upper, &lower))
947 base_type = base_type;
948 else if (type_is_ok(&int_ctype, &upper, &lower))
949 base_type = &int_ctype;
950 else if (type_is_ok(&uint_ctype, &upper, &lower))
951 base_type = &uint_ctype;
952 else if (type_is_ok(&long_ctype, &upper, &lower))
953 base_type = &long_ctype;
954 else if (type_is_ok(&ulong_ctype, &upper, &lower))
955 base_type = &ulong_ctype;
956 else if (type_is_ok(&llong_ctype, &upper, &lower))
957 base_type = &llong_ctype;
958 else if (type_is_ok(&ullong_ctype, &upper, &lower))
959 base_type = &ullong_ctype;
960 else
961 base_type = &bad_ctype;
962 parent->ctype.base_type = base_type;
963 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
964 parent->examined = 0;
966 cast_enum_list(parent->symbol_list, base_type);
968 return token;
971 static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
973 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
974 struct ctype *ctype = &ctx->ctype.base_type->ctype;
976 if (!ctype->base_type)
977 ctype->base_type = &incomplete_ctype;
979 return ret;
982 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
984 static struct token *typeof_specifier(struct token *token, struct decl_state *ctx)
986 struct symbol *sym;
988 if (!match_op(token, '(')) {
989 sparse_error(token->pos, "expected '(' after typeof");
990 return token;
992 if (lookup_type(token->next)) {
993 token = typename(token->next, &sym, NULL);
994 ctx->ctype.base_type = sym->ctype.base_type;
995 apply_ctype(token->pos, &sym->ctype, &ctx->ctype);
996 } else {
997 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF);
998 token = parse_expression(token->next, &typeof_sym->initializer);
1000 typeof_sym->endpos = token->pos;
1001 if (!typeof_sym->initializer) {
1002 sparse_error(token->pos, "expected expression after the '(' token");
1003 typeof_sym = &bad_ctype;
1005 ctx->ctype.base_type = typeof_sym;
1007 return expect(token, ')', "after typeof");
1010 static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx)
1012 struct expression *expr = NULL;
1013 if (match_op(token, '('))
1014 token = parens_expression(token, &expr, "in attribute");
1015 return token;
1018 static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx)
1020 if (!ctx->ctype.alignment)
1021 ctx->ctype.alignment = 1;
1022 return token;
1025 static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx)
1027 int alignment = max_alignment;
1028 struct expression *expr = NULL;
1030 if (match_op(token, '(')) {
1031 token = parens_expression(token, &expr, "in attribute");
1032 if (expr)
1033 alignment = const_expression_value(expr);
1035 if (alignment & (alignment-1)) {
1036 warning(token->pos, "I don't like non-power-of-2 alignments");
1037 return token;
1038 } else if (alignment > ctx->ctype.alignment)
1039 ctx->ctype.alignment = alignment;
1040 return token;
1043 static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual)
1045 if (ctx->modifiers & qual)
1046 warning(*pos, "duplicate %s", modifier_string(qual));
1047 ctx->modifiers |= qual;
1050 static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1052 apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1053 return token;
1056 static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1058 struct expression *expr = NULL;
1059 int as;
1060 token = expect(token, '(', "after address_space attribute");
1061 token = conditional_expression(token, &expr);
1062 if (expr) {
1063 as = const_expression_value(expr);
1064 if (Waddress_space && as)
1065 ctx->ctype.as = as;
1067 token = expect(token, ')', "after address_space attribute");
1068 return token;
1071 static struct symbol *to_QI_mode(struct symbol *ctype)
1073 if (ctype->ctype.base_type != &int_type)
1074 return NULL;
1075 if (ctype == &char_ctype)
1076 return ctype;
1077 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
1078 : &schar_ctype;
1081 static struct symbol *to_HI_mode(struct symbol *ctype)
1083 if (ctype->ctype.base_type != &int_type)
1084 return NULL;
1085 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype
1086 : &sshort_ctype;
1089 static struct symbol *to_SI_mode(struct symbol *ctype)
1091 if (ctype->ctype.base_type != &int_type)
1092 return NULL;
1093 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype
1094 : &sint_ctype;
1097 static struct symbol *to_DI_mode(struct symbol *ctype)
1099 if (ctype->ctype.base_type != &int_type)
1100 return NULL;
1101 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype
1102 : &sllong_ctype;
1105 static struct symbol *to_TI_mode(struct symbol *ctype)
1107 if (ctype->ctype.base_type != &int_type)
1108 return NULL;
1109 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1110 : &slllong_ctype;
1113 static struct symbol *to_word_mode(struct symbol *ctype)
1115 if (ctype->ctype.base_type != &int_type)
1116 return NULL;
1117 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1118 : &slong_ctype;
1121 static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1123 token = expect(token, '(', "after mode attribute");
1124 if (token_type(token) == TOKEN_IDENT) {
1125 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1126 if (mode && mode->op->type == KW_MODE)
1127 ctx->mode = mode->op;
1128 else
1129 sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
1130 token = token->next;
1131 } else
1132 sparse_error(token->pos, "expect attribute mode symbol\n");
1133 token = expect(token, ')', "after mode attribute");
1134 return token;
1137 static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1139 struct context *context = alloc_context();
1140 struct expression *args[3];
1141 int argc = 0;
1143 token = expect(token, '(', "after context attribute");
1144 while (!match_op(token, ')')) {
1145 struct expression *expr = NULL;
1146 token = conditional_expression(token, &expr);
1147 if (!expr)
1148 break;
1149 if (argc < 3)
1150 args[argc++] = expr;
1151 if (!match_op(token, ','))
1152 break;
1153 token = token->next;
1156 switch(argc) {
1157 case 0:
1158 sparse_error(token->pos, "expected context input/output values");
1159 break;
1160 case 1:
1161 context->in = get_expression_value(args[0]);
1162 break;
1163 case 2:
1164 context->in = get_expression_value(args[0]);
1165 context->out = get_expression_value(args[1]);
1166 break;
1167 case 3:
1168 context->context = args[0];
1169 context->in = get_expression_value(args[1]);
1170 context->out = get_expression_value(args[2]);
1171 break;
1174 if (argc)
1175 add_ptr_list(&ctx->ctype.contexts, context);
1177 token = expect(token, ')', "after context attribute");
1178 return token;
1181 static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1183 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1184 ctx->ctype.base_type->designated_init = 1;
1185 else
1186 warning(token->pos, "attribute designated_init applied to non-structure type");
1187 return token;
1190 static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx)
1192 if (Wtransparent_union)
1193 warning(token->pos, "ignoring attribute __transparent_union__");
1194 return token;
1197 static struct token *recover_unknown_attribute(struct token *token)
1199 struct expression *expr = NULL;
1201 sparse_error(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
1202 token = token->next;
1203 if (match_op(token, '('))
1204 token = parens_expression(token, &expr, "in attribute");
1205 return token;
1208 static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1210 token = expect(token, '(', "after attribute");
1211 token = expect(token, '(', "after attribute");
1213 for (;;) {
1214 struct ident *attribute_name;
1215 struct symbol *attr;
1217 if (eof_token(token))
1218 break;
1219 if (match_op(token, ';'))
1220 break;
1221 if (token_type(token) != TOKEN_IDENT)
1222 break;
1223 attribute_name = token->ident;
1224 attr = lookup_keyword(attribute_name, NS_KEYWORD);
1225 if (attr && attr->op->attribute)
1226 token = attr->op->attribute(token->next, attr, ctx);
1227 else
1228 token = recover_unknown_attribute(token);
1230 if (!match_op(token, ','))
1231 break;
1232 token = token->next;
1235 token = expect(token, ')', "after attribute");
1236 token = expect(token, ')', "after attribute");
1237 return token;
1240 static const char *storage_class[] =
1242 [STypedef] = "typedef",
1243 [SAuto] = "auto",
1244 [SExtern] = "extern",
1245 [SStatic] = "static",
1246 [SRegister] = "register",
1247 [SForced] = "[force]"
1250 static unsigned long storage_modifiers(struct decl_state *ctx)
1252 static unsigned long mod[] =
1254 [SAuto] = MOD_AUTO,
1255 [SExtern] = MOD_EXTERN,
1256 [SStatic] = MOD_STATIC,
1257 [SRegister] = MOD_REGISTER
1259 return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1260 | (ctx->is_tls ? MOD_TLS : 0);
1263 static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1265 /* __thread can be used alone, or with extern or static */
1266 if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1267 sparse_error(*pos, "__thread can only be used alone, or with "
1268 "extern or static");
1269 return;
1272 if (!ctx->storage_class) {
1273 ctx->storage_class = class;
1274 return;
1276 if (ctx->storage_class == class)
1277 sparse_error(*pos, "duplicate %s", storage_class[class]);
1278 else
1279 sparse_error(*pos, "multiple storage classes");
1282 static struct token *typedef_specifier(struct token *next, struct decl_state *ctx)
1284 set_storage_class(&next->pos, ctx, STypedef);
1285 return next;
1288 static struct token *auto_specifier(struct token *next, struct decl_state *ctx)
1290 set_storage_class(&next->pos, ctx, SAuto);
1291 return next;
1294 static struct token *register_specifier(struct token *next, struct decl_state *ctx)
1296 set_storage_class(&next->pos, ctx, SRegister);
1297 return next;
1300 static struct token *static_specifier(struct token *next, struct decl_state *ctx)
1302 set_storage_class(&next->pos, ctx, SStatic);
1303 return next;
1306 static struct token *extern_specifier(struct token *next, struct decl_state *ctx)
1308 set_storage_class(&next->pos, ctx, SExtern);
1309 return next;
1312 static struct token *thread_specifier(struct token *next, struct decl_state *ctx)
1314 /* This GCC extension can be used alone, or with extern or static */
1315 if (!ctx->storage_class || ctx->storage_class == SStatic
1316 || ctx->storage_class == SExtern) {
1317 ctx->is_tls = 1;
1318 } else {
1319 sparse_error(next->pos, "__thread can only be used alone, or "
1320 "with extern or static");
1323 return next;
1326 static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx)
1328 set_storage_class(&token->pos, ctx, SForced);
1329 return token;
1332 static struct token *inline_specifier(struct token *next, struct decl_state *ctx)
1334 ctx->is_inline = 1;
1335 return next;
1338 static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
1340 apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1341 return next;
1344 static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1346 apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1347 return next;
1350 static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1352 unsigned long mod = thistype->modifiers;
1354 if (mod)
1355 apply_qualifier(&pos, ctype, mod);
1357 /* Context */
1358 concat_ptr_list((struct ptr_list *)thistype->contexts,
1359 (struct ptr_list **)&ctype->contexts);
1361 /* Alignment */
1362 if (thistype->alignment > ctype->alignment)
1363 ctype->alignment = thistype->alignment;
1365 /* Address space */
1366 if (thistype->as)
1367 ctype->as = thistype->as;
1370 static void specifier_conflict(struct position pos, int what, struct ident *new)
1372 const char *old;
1373 if (what & (Set_S | Set_T))
1374 goto Catch_all;
1375 if (what & Set_Char)
1376 old = "char";
1377 else if (what & Set_Double)
1378 old = "double";
1379 else if (what & Set_Float)
1380 old = "float";
1381 else if (what & Set_Signed)
1382 old = "signed";
1383 else if (what & Set_Unsigned)
1384 old = "unsigned";
1385 else if (what & Set_Short)
1386 old = "short";
1387 else if (what & Set_Long)
1388 old = "long";
1389 else
1390 old = "long long";
1391 sparse_error(pos, "impossible combination of type specifiers: %s %s",
1392 old, show_ident(new));
1393 return;
1395 Catch_all:
1396 sparse_error(pos, "two or more data types in declaration specifiers");
1399 static struct symbol * const int_types[] =
1400 {&short_ctype, &int_ctype, &long_ctype, &llong_ctype};
1401 static struct symbol * const signed_types[] =
1402 {&sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype,
1403 &slllong_ctype};
1404 static struct symbol * const unsigned_types[] =
1405 {&ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype,
1406 &ulllong_ctype};
1407 static struct symbol * const real_types[] =
1408 {&float_ctype, &double_ctype, &ldouble_ctype};
1409 static struct symbol * const char_types[] =
1410 {&char_ctype, &schar_ctype, &uchar_ctype};
1411 static struct symbol * const * const types[] = {
1412 int_types + 1, signed_types + 1, unsigned_types + 1,
1413 real_types + 1, char_types, char_types + 1, char_types + 2
1416 struct symbol *ctype_integer(int size, int want_unsigned)
1418 return types[want_unsigned ? CUInt : CInt][size];
1421 static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx)
1423 while (token_type(t) == TOKEN_IDENT) {
1424 struct symbol *s = lookup_symbol(t->ident, NS_TYPEDEF);
1425 if (!s)
1426 break;
1427 if (s->type != SYM_KEYWORD)
1428 break;
1429 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER)))
1430 break;
1431 t = t->next;
1432 if (s->op->declarator)
1433 t = s->op->declarator(t, ctx);
1435 return t;
1438 static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx)
1440 int seen = 0;
1441 int class = CInt;
1442 int size = 0;
1444 while (token_type(token) == TOKEN_IDENT) {
1445 struct symbol *s = lookup_symbol(token->ident,
1446 NS_TYPEDEF | NS_SYMBOL);
1447 if (!s || !(s->namespace & NS_TYPEDEF))
1448 break;
1449 if (s->type != SYM_KEYWORD) {
1450 if (seen & Set_Any)
1451 break;
1452 seen |= Set_S | Set_T;
1453 ctx->ctype.base_type = s->ctype.base_type;
1454 apply_ctype(token->pos, &s->ctype, &ctx->ctype);
1455 token = token->next;
1456 continue;
1458 if (s->op->type & KW_SPECIFIER) {
1459 if (seen & s->op->test) {
1460 specifier_conflict(token->pos,
1461 seen & s->op->test,
1462 token->ident);
1463 break;
1465 seen |= s->op->set;
1466 class += s->op->class;
1467 if (s->op->type & KW_SHORT) {
1468 size = -1;
1469 } else if (s->op->type & KW_LONG && size++) {
1470 if (class == CReal) {
1471 specifier_conflict(token->pos,
1472 Set_Vlong,
1473 &double_ident);
1474 break;
1476 seen |= Set_Vlong;
1479 token = token->next;
1480 if (s->op->declarator)
1481 token = s->op->declarator(token, ctx);
1482 if (s->op->type & KW_EXACT) {
1483 ctx->ctype.base_type = s->ctype.base_type;
1484 ctx->ctype.modifiers |= s->ctype.modifiers;
1488 if (!(seen & Set_S)) { /* not set explicitly? */
1489 struct symbol *base = &incomplete_ctype;
1490 if (seen & Set_Any)
1491 base = types[class][size];
1492 ctx->ctype.base_type = base;
1495 if (ctx->ctype.modifiers & MOD_BITWISE) {
1496 struct symbol *type;
1497 ctx->ctype.modifiers &= ~MOD_BITWISE;
1498 if (!is_int_type(ctx->ctype.base_type)) {
1499 sparse_error(token->pos, "invalid modifier");
1500 return token;
1502 type = alloc_symbol(token->pos, SYM_BASETYPE);
1503 *type = *ctx->ctype.base_type;
1504 type->ctype.modifiers &= ~MOD_SPECIFIER;
1505 type->ctype.base_type = ctx->ctype.base_type;
1506 type->type = SYM_RESTRICT;
1507 ctx->ctype.base_type = type;
1508 create_fouled(type);
1510 return token;
1513 static struct token *abstract_array_declarator(struct token *token, struct symbol *sym)
1515 struct expression *expr = NULL;
1517 if (match_idents(token, &restrict_ident, &__restrict_ident, NULL))
1518 token = token->next;
1519 token = parse_expression(token, &expr);
1520 sym->array_size = expr;
1521 return token;
1524 static struct token *parameter_type_list(struct token *, struct symbol *);
1525 static struct token *identifier_list(struct token *, struct symbol *);
1526 static struct token *declarator(struct token *token, struct decl_state *ctx);
1528 static struct token *skip_attribute(struct token *token)
1530 token = token->next;
1531 if (match_op(token, '(')) {
1532 int depth = 1;
1533 token = token->next;
1534 while (depth && !eof_token(token)) {
1535 if (token_type(token) == TOKEN_SPECIAL) {
1536 if (token->special == '(')
1537 depth++;
1538 else if (token->special == ')')
1539 depth--;
1541 token = token->next;
1544 return token;
1547 static struct token *skip_attributes(struct token *token)
1549 struct symbol *keyword;
1550 for (;;) {
1551 if (token_type(token) != TOKEN_IDENT)
1552 break;
1553 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1554 if (!keyword || keyword->type != SYM_KEYWORD)
1555 break;
1556 if (!(keyword->op->type & KW_ATTRIBUTE))
1557 break;
1558 token = expect(token->next, '(', "after attribute");
1559 token = expect(token, '(', "after attribute");
1560 for (;;) {
1561 if (eof_token(token))
1562 break;
1563 if (match_op(token, ';'))
1564 break;
1565 if (token_type(token) != TOKEN_IDENT)
1566 break;
1567 token = skip_attribute(token);
1568 if (!match_op(token, ','))
1569 break;
1570 token = token->next;
1572 token = expect(token, ')', "after attribute");
1573 token = expect(token, ')', "after attribute");
1575 return token;
1578 static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords)
1580 struct symbol *keyword;
1581 for (;;) {
1582 if (token_type(token) != TOKEN_IDENT)
1583 break;
1584 keyword = lookup_keyword(token->ident, NS_KEYWORD | NS_TYPEDEF);
1585 if (!keyword || keyword->type != SYM_KEYWORD)
1586 break;
1587 if (!(keyword->op->type & keywords))
1588 break;
1589 token = keyword->op->declarator(token->next, ctx);
1590 keywords &= KW_ATTRIBUTE;
1592 return token;
1595 static int is_nested(struct token *token, struct token **p,
1596 int prefer_abstract)
1599 * This can be either a parameter list or a grouping.
1600 * For the direct (non-abstract) case, we know if must be
1601 * a parameter list if we already saw the identifier.
1602 * For the abstract case, we know if must be a parameter
1603 * list if it is empty or starts with a type.
1605 struct token *next = token->next;
1607 *p = next = skip_attributes(next);
1609 if (token_type(next) == TOKEN_IDENT) {
1610 if (lookup_type(next))
1611 return !prefer_abstract;
1612 return 1;
1615 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS))
1616 return 0;
1618 return 1;
1621 enum kind {
1622 Empty, K_R, Proto, Bad_Func,
1625 static enum kind which_func(struct token *token,
1626 struct ident **n,
1627 int prefer_abstract)
1629 struct token *next = token->next;
1631 if (token_type(next) == TOKEN_IDENT) {
1632 if (lookup_type(next))
1633 return Proto;
1634 /* identifier list not in definition; complain */
1635 if (prefer_abstract)
1636 warning(token->pos,
1637 "identifier list not in definition");
1638 return K_R;
1641 if (token_type(next) != TOKEN_SPECIAL)
1642 return Bad_Func;
1644 if (next->special == ')') {
1645 /* don't complain about those */
1646 if (!n || match_op(next->next, ';'))
1647 return Empty;
1648 warning(next->pos,
1649 "non-ANSI function declaration of function '%s'",
1650 show_ident(*n));
1651 return Empty;
1654 if (next->special == SPECIAL_ELLIPSIS) {
1655 warning(next->pos,
1656 "variadic functions must have one named argument");
1657 return Proto;
1660 return Bad_Func;
1663 static struct token *direct_declarator(struct token *token, struct decl_state *ctx)
1665 struct ctype *ctype = &ctx->ctype;
1666 struct token *next;
1667 struct ident **p = ctx->ident;
1669 if (ctx->ident && token_type(token) == TOKEN_IDENT) {
1670 *ctx->ident = token->ident;
1671 token = token->next;
1672 } else if (match_op(token, '(') &&
1673 is_nested(token, &next, ctx->prefer_abstract)) {
1674 struct symbol *base_type = ctype->base_type;
1675 if (token->next != next)
1676 next = handle_attributes(token->next, ctx,
1677 KW_ATTRIBUTE);
1678 token = declarator(next, ctx);
1679 token = expect(token, ')', "in nested declarator");
1680 while (ctype->base_type != base_type)
1681 ctype = &ctype->base_type->ctype;
1682 p = NULL;
1685 if (match_op(token, '(')) {
1686 enum kind kind = which_func(token, p, ctx->prefer_abstract);
1687 struct symbol *fn;
1688 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN);
1689 token = token->next;
1690 if (kind == K_R)
1691 token = identifier_list(token, fn);
1692 else if (kind == Proto)
1693 token = parameter_type_list(token, fn);
1694 token = expect(token, ')', "in function declarator");
1695 fn->endpos = token->pos;
1696 return token;
1699 while (match_op(token, '[')) {
1700 struct symbol *array;
1701 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY);
1702 token = abstract_array_declarator(token->next, array);
1703 token = expect(token, ']', "in abstract_array_declarator");
1704 array->endpos = token->pos;
1705 ctype = &array->ctype;
1707 return token;
1710 static struct token *pointer(struct token *token, struct decl_state *ctx)
1712 while (match_op(token,'*')) {
1713 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1714 ptr->ctype.modifiers = ctx->ctype.modifiers;
1715 ptr->ctype.base_type = ctx->ctype.base_type;
1716 ptr->ctype.as = ctx->ctype.as;
1717 ptr->ctype.contexts = ctx->ctype.contexts;
1718 ctx->ctype.modifiers = 0;
1719 ctx->ctype.base_type = ptr;
1720 ctx->ctype.as = 0;
1721 ctx->ctype.contexts = NULL;
1722 ctx->ctype.alignment = 0;
1724 token = handle_qualifiers(token->next, ctx);
1725 ctx->ctype.base_type->endpos = token->pos;
1727 return token;
1730 static struct token *declarator(struct token *token, struct decl_state *ctx)
1732 token = pointer(token, ctx);
1733 return direct_declarator(token, ctx);
1736 static struct token *handle_bitfield(struct token *token, struct decl_state *ctx)
1738 struct ctype *ctype = &ctx->ctype;
1739 struct expression *expr;
1740 struct symbol *bitfield;
1741 long long width;
1743 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) {
1744 sparse_error(token->pos, "invalid bitfield specifier for type %s.",
1745 show_typename(ctype->base_type));
1746 // Parse this to recover gracefully.
1747 return conditional_expression(token->next, &expr);
1750 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD);
1751 token = conditional_expression(token->next, &expr);
1752 width = const_expression_value(expr);
1753 bitfield->bit_size = width;
1755 if (width < 0 || width > INT_MAX) {
1756 sparse_error(token->pos, "invalid bitfield width, %lld.", width);
1757 width = -1;
1758 } else if (*ctx->ident && width == 0) {
1759 sparse_error(token->pos, "invalid named zero-width bitfield `%s'",
1760 show_ident(*ctx->ident));
1761 width = -1;
1762 } else if (*ctx->ident) {
1763 struct symbol *base_type = bitfield->ctype.base_type;
1764 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type;
1765 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED);
1766 if (Wone_bit_signed_bitfield && width == 1 && is_signed) {
1767 // Valid values are either {-1;0} or {0}, depending on integer
1768 // representation. The latter makes for very efficient code...
1769 sparse_error(token->pos, "dubious one-bit signed bitfield");
1771 if (Wdefault_bitfield_sign &&
1772 bitfield_type->type != SYM_ENUM &&
1773 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) &&
1774 is_signed) {
1775 // The sign of bitfields is unspecified by default.
1776 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'");
1779 bitfield->bit_size = width;
1780 bitfield->endpos = token->pos;
1781 return token;
1784 static struct token *declaration_list(struct token *token, struct symbol_list **list)
1786 struct decl_state ctx = {.prefer_abstract = 0};
1787 struct ctype saved;
1788 unsigned long mod;
1790 token = declaration_specifiers(token, &ctx);
1791 mod = storage_modifiers(&ctx);
1792 saved = ctx.ctype;
1793 for (;;) {
1794 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
1795 ctx.ident = &decl->ident;
1797 token = declarator(token, &ctx);
1798 if (match_op(token, ':'))
1799 token = handle_bitfield(token, &ctx);
1801 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1802 apply_modifiers(token->pos, &ctx);
1804 decl->ctype = ctx.ctype;
1805 decl->ctype.modifiers |= mod;
1806 decl->endpos = token->pos;
1807 add_symbol(list, decl);
1808 if (!match_op(token, ','))
1809 break;
1810 token = token->next;
1811 ctx.ctype = saved;
1813 return token;
1816 static struct token *struct_declaration_list(struct token *token, struct symbol_list **list)
1818 while (!match_op(token, '}')) {
1819 if (!match_op(token, ';'))
1820 token = declaration_list(token, list);
1821 if (!match_op(token, ';')) {
1822 sparse_error(token->pos, "expected ; at end of declaration");
1823 break;
1825 token = token->next;
1827 return token;
1830 static struct token *parameter_declaration(struct token *token, struct symbol *sym)
1832 struct decl_state ctx = {.prefer_abstract = 1};
1834 token = declaration_specifiers(token, &ctx);
1835 ctx.ident = &sym->ident;
1836 token = declarator(token, &ctx);
1837 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
1838 apply_modifiers(token->pos, &ctx);
1839 sym->ctype = ctx.ctype;
1840 sym->ctype.modifiers |= storage_modifiers(&ctx);
1841 sym->endpos = token->pos;
1842 return token;
1845 struct token *typename(struct token *token, struct symbol **p, int *forced)
1847 struct decl_state ctx = {.prefer_abstract = 1};
1848 int class;
1849 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
1850 *p = sym;
1851 token = declaration_specifiers(token, &ctx);
1852 token = declarator(token, &ctx);
1853 apply_modifiers(token->pos, &ctx);
1854 sym->ctype = ctx.ctype;
1855 sym->endpos = token->pos;
1856 class = ctx.storage_class;
1857 if (forced) {
1858 *forced = 0;
1859 if (class == SForced) {
1860 *forced = 1;
1861 class = 0;
1864 if (class)
1865 warning(sym->pos, "storage class in typename (%s %s)",
1866 storage_class[class], show_typename(sym));
1867 return token;
1870 static struct token *expression_statement(struct token *token, struct expression **tree)
1872 token = parse_expression(token, tree);
1873 return expect(token, ';', "at end of statement");
1876 static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1877 struct expression_list **inout)
1879 struct expression *expr;
1881 /* Allow empty operands */
1882 if (match_op(token->next, ':') || match_op(token->next, ')'))
1883 return token->next;
1884 do {
1885 struct ident *ident = NULL;
1886 if (match_op(token->next, '[') &&
1887 token_type(token->next->next) == TOKEN_IDENT &&
1888 match_op(token->next->next->next, ']')) {
1889 ident = token->next->next->ident;
1890 token = token->next->next->next;
1892 add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1893 token = primary_expression(token->next, &expr);
1894 add_expression(inout, expr);
1895 token = parens_expression(token, &expr, "in asm parameter");
1896 add_expression(inout, expr);
1897 } while (match_op(token, ','));
1898 return token;
1901 static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1902 struct expression_list **clobbers)
1904 struct expression *expr;
1906 do {
1907 token = primary_expression(token->next, &expr);
1908 if (expr)
1909 add_expression(clobbers, expr);
1910 } while (match_op(token, ','));
1911 return token;
1914 static struct token *parse_asm_labels(struct token *token, struct statement *stmt,
1915 struct symbol_list **labels)
1917 struct symbol *label;
1919 do {
1920 token = token->next; /* skip ':' and ',' */
1921 if (token_type(token) != TOKEN_IDENT)
1922 return token;
1923 label = label_symbol(token);
1924 add_symbol(labels, label);
1925 token = token->next;
1926 } while (match_op(token, ','));
1927 return token;
1930 static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
1932 int is_goto = 0;
1934 token = token->next;
1935 stmt->type = STMT_ASM;
1936 if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
1937 token = token->next;
1939 if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
1940 is_goto = 1;
1941 token = token->next;
1943 token = expect(token, '(', "after asm");
1944 token = parse_expression(token, &stmt->asm_string);
1945 if (match_op(token, ':'))
1946 token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
1947 if (match_op(token, ':'))
1948 token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
1949 if (match_op(token, ':'))
1950 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
1951 if (is_goto && match_op(token, ':'))
1952 token = parse_asm_labels(token, stmt, &stmt->asm_labels);
1953 token = expect(token, ')', "after asm");
1954 return expect(token, ';', "at end of asm-statement");
1957 static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
1959 struct expression *expr;
1960 token = expect(token, '(', "after asm");
1961 token = parse_expression(token->next, &expr);
1962 token = expect(token, ')', "after asm");
1963 return token;
1966 /* Make a statement out of an expression */
1967 static struct statement *make_statement(struct expression *expr)
1969 struct statement *stmt;
1971 if (!expr)
1972 return NULL;
1973 stmt = alloc_statement(expr->pos, STMT_EXPRESSION);
1974 stmt->expression = expr;
1975 return stmt;
1979 * All iterators have two symbols associated with them:
1980 * the "continue" and "break" symbols, which are targets
1981 * for continue and break statements respectively.
1983 * They are in a special name-space, but they follow
1984 * all the normal visibility rules, so nested iterators
1985 * automatically work right.
1987 static void start_iterator(struct statement *stmt)
1989 struct symbol *cont, *brk;
1991 start_symbol_scope(stmt->pos);
1992 cont = alloc_symbol(stmt->pos, SYM_NODE);
1993 bind_symbol(cont, &continue_ident, NS_ITERATOR);
1994 brk = alloc_symbol(stmt->pos, SYM_NODE);
1995 bind_symbol(brk, &break_ident, NS_ITERATOR);
1997 stmt->type = STMT_ITERATOR;
1998 stmt->iterator_break = brk;
1999 stmt->iterator_continue = cont;
2000 fn_local_symbol(brk);
2001 fn_local_symbol(cont);
2004 static void end_iterator(struct statement *stmt)
2006 end_symbol_scope();
2009 static struct statement *start_function(struct symbol *sym)
2011 struct symbol *ret;
2012 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2014 start_function_scope(stmt->pos);
2015 ret = alloc_symbol(sym->pos, SYM_NODE);
2016 ret->ctype = sym->ctype.base_type->ctype;
2017 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
2018 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2019 bind_symbol(ret, &return_ident, NS_ITERATOR);
2020 stmt->ret = ret;
2021 fn_local_symbol(ret);
2023 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2024 current_fn = sym;
2026 return stmt;
2029 static void end_function(struct symbol *sym)
2031 current_fn = NULL;
2032 end_function_scope();
2036 * A "switch()" statement, like an iterator, has a
2037 * the "break" symbol associated with it. It works
2038 * exactly like the iterator break - it's the target
2039 * for any break-statements in scope, and means that
2040 * "break" handling doesn't even need to know whether
2041 * it's breaking out of an iterator or a switch.
2043 * In addition, the "case" symbol is a marker for the
2044 * case/default statements to find the switch statement
2045 * that they are associated with.
2047 static void start_switch(struct statement *stmt)
2049 struct symbol *brk, *switch_case;
2051 start_symbol_scope(stmt->pos);
2052 brk = alloc_symbol(stmt->pos, SYM_NODE);
2053 bind_symbol(brk, &break_ident, NS_ITERATOR);
2055 switch_case = alloc_symbol(stmt->pos, SYM_NODE);
2056 bind_symbol(switch_case, &case_ident, NS_ITERATOR);
2057 switch_case->stmt = stmt;
2059 stmt->type = STMT_SWITCH;
2060 stmt->switch_break = brk;
2061 stmt->switch_case = switch_case;
2063 fn_local_symbol(brk);
2064 fn_local_symbol(switch_case);
2067 static void end_switch(struct statement *stmt)
2069 if (!stmt->switch_case->symbol_list)
2070 warning(stmt->pos, "switch with no cases");
2071 end_symbol_scope();
2074 static void add_case_statement(struct statement *stmt)
2076 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR);
2077 struct symbol *sym;
2079 if (!target) {
2080 sparse_error(stmt->pos, "not in switch scope");
2081 stmt->type = STMT_NONE;
2082 return;
2084 sym = alloc_symbol(stmt->pos, SYM_NODE);
2085 add_symbol(&target->symbol_list, sym);
2086 sym->stmt = stmt;
2087 stmt->case_label = sym;
2088 fn_local_symbol(sym);
2091 static struct token *parse_return_statement(struct token *token, struct statement *stmt)
2093 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR);
2095 if (!target)
2096 error_die(token->pos, "internal error: return without a function target");
2097 stmt->type = STMT_RETURN;
2098 stmt->ret_target = target;
2099 return expression_statement(token->next, &stmt->ret_value);
2102 static struct token *parse_for_statement(struct token *token, struct statement *stmt)
2104 struct symbol_list *syms;
2105 struct expression *e1, *e2, *e3;
2106 struct statement *iterator;
2108 start_iterator(stmt);
2109 token = expect(token->next, '(', "after 'for'");
2111 syms = NULL;
2112 e1 = NULL;
2113 /* C99 variable declaration? */
2114 if (lookup_type(token)) {
2115 token = external_declaration(token, &syms);
2116 } else {
2117 token = parse_expression(token, &e1);
2118 token = expect(token, ';', "in 'for'");
2120 token = parse_expression(token, &e2);
2121 token = expect(token, ';', "in 'for'");
2122 token = parse_expression(token, &e3);
2123 token = expect(token, ')', "in 'for'");
2124 token = statement(token, &iterator);
2126 stmt->iterator_syms = syms;
2127 stmt->iterator_pre_statement = make_statement(e1);
2128 stmt->iterator_pre_condition = e2;
2129 stmt->iterator_post_statement = make_statement(e3);
2130 stmt->iterator_post_condition = NULL;
2131 stmt->iterator_statement = iterator;
2132 end_iterator(stmt);
2134 return token;
2137 static struct token *parse_while_statement(struct token *token, struct statement *stmt)
2139 struct expression *expr;
2140 struct statement *iterator;
2142 start_iterator(stmt);
2143 token = parens_expression(token->next, &expr, "after 'while'");
2144 token = statement(token, &iterator);
2146 stmt->iterator_pre_condition = expr;
2147 stmt->iterator_post_condition = NULL;
2148 stmt->iterator_statement = iterator;
2149 end_iterator(stmt);
2151 return token;
2154 static struct token *parse_do_statement(struct token *token, struct statement *stmt)
2156 struct expression *expr;
2157 struct statement *iterator;
2159 start_iterator(stmt);
2160 token = statement(token->next, &iterator);
2161 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident)
2162 token = token->next;
2163 else
2164 sparse_error(token->pos, "expected 'while' after 'do'");
2165 token = parens_expression(token, &expr, "after 'do-while'");
2167 stmt->iterator_post_condition = expr;
2168 stmt->iterator_statement = iterator;
2169 end_iterator(stmt);
2171 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while)
2172 warning(iterator->pos, "do-while statement is not a compound statement");
2174 return expect(token, ';', "after statement");
2177 static struct token *parse_if_statement(struct token *token, struct statement *stmt)
2179 stmt->type = STMT_IF;
2180 token = parens_expression(token->next, &stmt->if_conditional, "after if");
2181 token = statement(token, &stmt->if_true);
2182 if (token_type(token) != TOKEN_IDENT)
2183 return token;
2184 if (token->ident != &else_ident)
2185 return token;
2186 return statement(token->next, &stmt->if_false);
2189 static inline struct token *case_statement(struct token *token, struct statement *stmt)
2191 stmt->type = STMT_CASE;
2192 token = expect(token, ':', "after default/case");
2193 add_case_statement(stmt);
2194 return statement(token, &stmt->case_statement);
2197 static struct token *parse_case_statement(struct token *token, struct statement *stmt)
2199 token = parse_expression(token->next, &stmt->case_expression);
2200 if (match_op(token, SPECIAL_ELLIPSIS))
2201 token = parse_expression(token->next, &stmt->case_to);
2202 return case_statement(token, stmt);
2205 static struct token *parse_default_statement(struct token *token, struct statement *stmt)
2207 return case_statement(token->next, stmt);
2210 static struct token *parse_loop_iterator(struct token *token, struct statement *stmt)
2212 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR);
2213 stmt->type = STMT_GOTO;
2214 stmt->goto_label = target;
2215 if (!target)
2216 sparse_error(stmt->pos, "break/continue not in iterator scope");
2217 return expect(token->next, ';', "at end of statement");
2220 static struct token *parse_switch_statement(struct token *token, struct statement *stmt)
2222 stmt->type = STMT_SWITCH;
2223 start_switch(stmt);
2224 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'");
2225 token = statement(token, &stmt->switch_statement);
2226 end_switch(stmt);
2227 return token;
2230 static struct token *parse_goto_statement(struct token *token, struct statement *stmt)
2232 stmt->type = STMT_GOTO;
2233 token = token->next;
2234 if (match_op(token, '*')) {
2235 token = parse_expression(token->next, &stmt->goto_expression);
2236 add_statement(&function_computed_goto_list, stmt);
2237 } else if (token_type(token) == TOKEN_IDENT) {
2238 stmt->goto_label = label_symbol(token);
2239 token = token->next;
2240 } else {
2241 sparse_error(token->pos, "Expected identifier or goto expression");
2243 return expect(token, ';', "at end of statement");
2246 static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2248 stmt->type = STMT_CONTEXT;
2249 token = parse_expression(token->next, &stmt->expression);
2250 if (stmt->expression->type == EXPR_PREOP
2251 && stmt->expression->op == '('
2252 && stmt->expression->unop->type == EXPR_COMMA) {
2253 struct expression *expr;
2254 expr = stmt->expression->unop;
2255 stmt->context = expr->left;
2256 stmt->expression = expr->right;
2258 return expect(token, ';', "at end of statement");
2261 static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2263 stmt->type = STMT_RANGE;
2264 token = assignment_expression(token->next, &stmt->range_expression);
2265 token = expect(token, ',', "after range expression");
2266 token = assignment_expression(token, &stmt->range_low);
2267 token = expect(token, ',', "after low range");
2268 token = assignment_expression(token, &stmt->range_high);
2269 return expect(token, ';', "after range statement");
2272 static struct token *statement(struct token *token, struct statement **tree)
2274 struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2276 *tree = stmt;
2277 if (token_type(token) == TOKEN_IDENT) {
2278 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2279 if (s && s->op->statement)
2280 return s->op->statement(token, stmt);
2282 if (match_op(token->next, ':')) {
2283 stmt->type = STMT_LABEL;
2284 stmt->label_identifier = label_symbol(token);
2285 token = skip_attributes(token->next->next);
2286 return statement(token, &stmt->label_statement);
2290 if (match_op(token, '{')) {
2291 stmt->type = STMT_COMPOUND;
2292 start_symbol_scope(stmt->pos);
2293 token = compound_statement(token->next, stmt);
2294 end_symbol_scope();
2296 return expect(token, '}', "at end of compound statement");
2299 stmt->type = STMT_EXPRESSION;
2300 return expression_statement(token, &stmt->expression);
2303 /* gcc extension - __label__ ident-list; in the beginning of compound stmt */
2304 static struct token *label_statement(struct token *token)
2306 while (token_type(token) == TOKEN_IDENT) {
2307 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL);
2308 /* it's block-scope, but we want label namespace */
2309 bind_symbol(sym, token->ident, NS_SYMBOL);
2310 sym->namespace = NS_LABEL;
2311 fn_local_symbol(sym);
2312 token = token->next;
2313 if (!match_op(token, ','))
2314 break;
2315 token = token->next;
2317 return expect(token, ';', "at end of label declaration");
2320 static struct token * statement_list(struct token *token, struct statement_list **list)
2322 int seen_statement = 0;
2323 while (token_type(token) == TOKEN_IDENT &&
2324 token->ident == &__label___ident)
2325 token = label_statement(token->next);
2326 for (;;) {
2327 struct statement * stmt;
2328 if (eof_token(token))
2329 break;
2330 if (match_op(token, '}'))
2331 break;
2332 if (lookup_type(token)) {
2333 if (seen_statement) {
2334 warning(token->pos, "mixing declarations and code");
2335 seen_statement = 0;
2337 stmt = alloc_statement(token->pos, STMT_DECLARATION);
2338 token = external_declaration(token, &stmt->declaration);
2339 } else {
2340 seen_statement = Wdeclarationafterstatement;
2341 token = statement(token, &stmt);
2343 add_statement(list, stmt);
2345 return token;
2348 static struct token *identifier_list(struct token *token, struct symbol *fn)
2350 struct symbol_list **list = &fn->arguments;
2351 for (;;) {
2352 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE);
2353 sym->ident = token->ident;
2354 token = token->next;
2355 sym->endpos = token->pos;
2356 sym->ctype.base_type = &incomplete_ctype;
2357 add_symbol(list, sym);
2358 if (!match_op(token, ',') ||
2359 token_type(token->next) != TOKEN_IDENT ||
2360 lookup_type(token->next))
2361 break;
2362 token = token->next;
2364 return token;
2367 static struct token *parameter_type_list(struct token *token, struct symbol *fn)
2369 struct symbol_list **list = &fn->arguments;
2371 for (;;) {
2372 struct symbol *sym;
2374 if (match_op(token, SPECIAL_ELLIPSIS)) {
2375 fn->variadic = 1;
2376 token = token->next;
2377 break;
2380 sym = alloc_symbol(token->pos, SYM_NODE);
2381 token = parameter_declaration(token, sym);
2382 if (sym->ctype.base_type == &void_ctype) {
2383 /* Special case: (void) */
2384 if (!*list && !sym->ident)
2385 break;
2386 warning(token->pos, "void parameter");
2388 add_symbol(list, sym);
2389 if (!match_op(token, ','))
2390 break;
2391 token = token->next;
2393 return token;
2396 struct token *compound_statement(struct token *token, struct statement *stmt)
2398 token = statement_list(token, &stmt->stmts);
2399 return token;
2402 static struct expression *identifier_expression(struct token *token)
2404 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER);
2405 expr->expr_ident = token->ident;
2406 return expr;
2409 static struct expression *index_expression(struct expression *from, struct expression *to)
2411 int idx_from, idx_to;
2412 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX);
2414 idx_from = const_expression_value(from);
2415 idx_to = idx_from;
2416 if (to) {
2417 idx_to = const_expression_value(to);
2418 if (idx_to < idx_from || idx_from < 0)
2419 warning(from->pos, "nonsense array initializer index range");
2421 expr->idx_from = idx_from;
2422 expr->idx_to = idx_to;
2423 return expr;
2426 static struct token *single_initializer(struct expression **ep, struct token *token)
2428 int expect_equal = 0;
2429 struct token *next = token->next;
2430 struct expression **tail = ep;
2431 int nested;
2433 *ep = NULL;
2435 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) {
2436 struct expression *expr = identifier_expression(token);
2437 if (Wold_initializer)
2438 warning(token->pos, "obsolete struct initializer, use C99 syntax");
2439 token = initializer(&expr->ident_expression, next->next);
2440 if (expr->ident_expression)
2441 *ep = expr;
2442 return token;
2445 for (tail = ep, nested = 0; ; nested++, next = token->next) {
2446 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) {
2447 struct expression *expr = identifier_expression(next);
2448 *tail = expr;
2449 tail = &expr->ident_expression;
2450 expect_equal = 1;
2451 token = next->next;
2452 } else if (match_op(token, '[')) {
2453 struct expression *from = NULL, *to = NULL, *expr;
2454 token = constant_expression(token->next, &from);
2455 if (!from) {
2456 sparse_error(token->pos, "Expected constant expression");
2457 break;
2459 if (match_op(token, SPECIAL_ELLIPSIS))
2460 token = constant_expression(token->next, &to);
2461 expr = index_expression(from, to);
2462 *tail = expr;
2463 tail = &expr->idx_expression;
2464 token = expect(token, ']', "at end of initializer index");
2465 if (nested)
2466 expect_equal = 1;
2467 } else {
2468 break;
2471 if (nested && !expect_equal) {
2472 if (!match_op(token, '='))
2473 warning(token->pos, "obsolete array initializer, use C99 syntax");
2474 else
2475 expect_equal = 1;
2477 if (expect_equal)
2478 token = expect(token, '=', "at end of initializer index");
2480 token = initializer(tail, token);
2481 if (!*tail)
2482 *ep = NULL;
2483 return token;
2486 static struct token *initializer_list(struct expression_list **list, struct token *token)
2488 struct expression *expr;
2490 for (;;) {
2491 token = single_initializer(&expr, token);
2492 if (!expr)
2493 break;
2494 add_expression(list, expr);
2495 if (!match_op(token, ','))
2496 break;
2497 token = token->next;
2499 return token;
2502 struct token *initializer(struct expression **tree, struct token *token)
2504 if (match_op(token, '{')) {
2505 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
2506 *tree = expr;
2507 token = initializer_list(&expr->expr_list, token->next);
2508 return expect(token, '}', "at end of initializer");
2510 return assignment_expression(token, tree);
2513 static void declare_argument(struct symbol *sym, struct symbol *fn)
2515 if (!sym->ident) {
2516 sparse_error(sym->pos, "no identifier for function argument");
2517 return;
2519 bind_symbol(sym, sym->ident, NS_SYMBOL);
2522 static struct token *parse_function_body(struct token *token, struct symbol *decl,
2523 struct symbol_list **list)
2525 struct symbol_list **old_symbol_list;
2526 struct symbol *base_type = decl->ctype.base_type;
2527 struct statement *stmt, **p;
2528 struct symbol *prev;
2529 struct symbol *arg;
2531 old_symbol_list = function_symbol_list;
2532 if (decl->ctype.modifiers & MOD_INLINE) {
2533 function_symbol_list = &decl->inline_symbol_list;
2534 p = &base_type->inline_stmt;
2535 } else {
2536 function_symbol_list = &decl->symbol_list;
2537 p = &base_type->stmt;
2539 function_computed_target_list = NULL;
2540 function_computed_goto_list = NULL;
2542 if (decl->ctype.modifiers & MOD_EXTERN) {
2543 if (!(decl->ctype.modifiers & MOD_INLINE))
2544 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2546 if (!(decl->ctype.modifiers & MOD_STATIC))
2547 decl->ctype.modifiers |= MOD_EXTERN;
2549 stmt = start_function(decl);
2551 *p = stmt;
2552 FOR_EACH_PTR (base_type->arguments, arg) {
2553 declare_argument(arg, base_type);
2554 } END_FOR_EACH_PTR(arg);
2556 token = compound_statement(token->next, stmt);
2558 end_function(decl);
2559 if (!(decl->ctype.modifiers & MOD_INLINE))
2560 add_symbol(list, decl);
2561 check_declaration(decl);
2562 decl->definition = decl;
2563 prev = decl->same_symbol;
2564 if (prev && prev->definition) {
2565 warning(decl->pos, "multiple definitions for function '%s'",
2566 show_ident(decl->ident));
2567 info(prev->definition->pos, " the previous one is here");
2568 } else {
2569 while (prev) {
2570 prev->definition = decl;
2571 prev = prev->same_symbol;
2574 function_symbol_list = old_symbol_list;
2575 if (function_computed_goto_list) {
2576 if (!function_computed_target_list)
2577 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident));
2578 else {
2579 FOR_EACH_PTR(function_computed_goto_list, stmt) {
2580 stmt->target_list = function_computed_target_list;
2581 } END_FOR_EACH_PTR(stmt);
2584 return expect(token, '}', "at end of function");
2587 static void promote_k_r_types(struct symbol *arg)
2589 struct symbol *base = arg->ctype.base_type;
2590 if (base && base->ctype.base_type == &int_type && (base->ctype.modifiers & (MOD_CHAR | MOD_SHORT))) {
2591 arg->ctype.base_type = &int_ctype;
2595 static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn)
2597 struct symbol_list *real_args = fn->ctype.base_type->arguments;
2598 struct symbol *arg;
2600 FOR_EACH_PTR(real_args, arg) {
2601 struct symbol *type;
2603 /* This is quadratic in the number of arguments. We _really_ don't care */
2604 FOR_EACH_PTR(argtypes, type) {
2605 if (type->ident == arg->ident)
2606 goto match;
2607 } END_FOR_EACH_PTR(type);
2608 sparse_error(arg->pos, "missing type declaration for parameter '%s'", show_ident(arg->ident));
2609 continue;
2610 match:
2611 type->used = 1;
2612 /* "char" and "short" promote to "int" */
2613 promote_k_r_types(type);
2615 arg->ctype = type->ctype;
2616 } END_FOR_EACH_PTR(arg);
2618 FOR_EACH_PTR(argtypes, arg) {
2619 if (!arg->used)
2620 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident));
2621 } END_FOR_EACH_PTR(arg);
2625 static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl,
2626 struct symbol_list **list)
2628 struct symbol_list *args = NULL;
2630 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident));
2631 do {
2632 token = declaration_list(token, &args);
2633 if (!match_op(token, ';')) {
2634 sparse_error(token->pos, "expected ';' at end of parameter declaration");
2635 break;
2637 token = token->next;
2638 } while (lookup_type(token));
2640 apply_k_r_types(args, decl);
2642 if (!match_op(token, '{')) {
2643 sparse_error(token->pos, "expected function body");
2644 return token;
2646 return parse_function_body(token, decl, list);
2649 static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list)
2651 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE);
2652 struct symbol *fn = alloc_symbol(token->pos, SYM_FN);
2653 struct statement *stmt;
2655 anon->ctype.base_type = fn;
2656 stmt = alloc_statement(token->pos, STMT_NONE);
2657 fn->stmt = stmt;
2659 token = parse_asm_statement(token, stmt);
2661 add_symbol(list, anon);
2662 return token;
2665 struct token *external_declaration(struct token *token, struct symbol_list **list)
2667 struct ident *ident = NULL;
2668 struct symbol *decl;
2669 struct decl_state ctx = { .ident = &ident };
2670 struct ctype saved;
2671 struct symbol *base_type;
2672 unsigned long mod;
2673 int is_typedef;
2675 /* Top-level inline asm? */
2676 if (token_type(token) == TOKEN_IDENT) {
2677 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2678 if (s && s->op->toplevel)
2679 return s->op->toplevel(token, list);
2682 /* Parse declaration-specifiers, if any */
2683 token = declaration_specifiers(token, &ctx);
2684 mod = storage_modifiers(&ctx);
2685 decl = alloc_symbol(token->pos, SYM_NODE);
2686 /* Just a type declaration? */
2687 if (match_op(token, ';')) {
2688 apply_modifiers(token->pos, &ctx);
2689 return token->next;
2692 saved = ctx.ctype;
2693 token = declarator(token, &ctx);
2694 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2695 apply_modifiers(token->pos, &ctx);
2697 decl->ctype = ctx.ctype;
2698 decl->ctype.modifiers |= mod;
2699 decl->endpos = token->pos;
2701 /* Just a type declaration? */
2702 if (!ident) {
2703 warning(token->pos, "missing identifier in declaration");
2704 return expect(token, ';', "at the end of type declaration");
2707 /* type define declaration? */
2708 is_typedef = ctx.storage_class == STypedef;
2710 /* Typedefs don't have meaningful storage */
2711 if (is_typedef)
2712 decl->ctype.modifiers |= MOD_USERTYPE;
2714 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2716 base_type = decl->ctype.base_type;
2718 if (is_typedef) {
2719 if (base_type && !base_type->ident) {
2720 switch (base_type->type) {
2721 case SYM_STRUCT:
2722 case SYM_UNION:
2723 case SYM_ENUM:
2724 case SYM_RESTRICT:
2725 base_type->ident = ident;
2728 } else if (base_type && base_type->type == SYM_FN) {
2729 /* K&R argument declaration? */
2730 if (lookup_type(token))
2731 return parse_k_r_arguments(token, decl, list);
2732 if (match_op(token, '{'))
2733 return parse_function_body(token, decl, list);
2735 if (!(decl->ctype.modifiers & MOD_STATIC))
2736 decl->ctype.modifiers |= MOD_EXTERN;
2737 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) {
2738 sparse_error(token->pos, "void declaration");
2741 for (;;) {
2742 if (!is_typedef && match_op(token, '=')) {
2743 if (decl->ctype.modifiers & MOD_EXTERN) {
2744 warning(decl->pos, "symbol with external linkage has initializer");
2745 decl->ctype.modifiers &= ~MOD_EXTERN;
2747 token = initializer(&decl->initializer, token->next);
2749 if (!is_typedef) {
2750 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2751 add_symbol(list, decl);
2752 fn_local_symbol(decl);
2755 check_declaration(decl);
2756 if (decl->same_symbol)
2757 decl->definition = decl->same_symbol->definition;
2759 if (!match_op(token, ','))
2760 break;
2762 token = token->next;
2763 ident = NULL;
2764 decl = alloc_symbol(token->pos, SYM_NODE);
2765 ctx.ctype = saved;
2766 token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
2767 token = declarator(token, &ctx);
2768 token = handle_attributes(token, &ctx, KW_ATTRIBUTE | KW_ASM);
2769 apply_modifiers(token->pos, &ctx);
2770 decl->ctype = ctx.ctype;
2771 decl->ctype.modifiers |= mod;
2772 decl->endpos = token->pos;
2773 if (!ident) {
2774 sparse_error(token->pos, "expected identifier name in type definition");
2775 return token;
2778 if (is_typedef)
2779 decl->ctype.modifiers |= MOD_USERTYPE;
2781 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL);
2783 /* Function declarations are automatically extern unless specifically static */
2784 base_type = decl->ctype.base_type;
2785 if (!is_typedef && base_type && base_type->type == SYM_FN) {
2786 if (!(decl->ctype.modifiers & MOD_STATIC))
2787 decl->ctype.modifiers |= MOD_EXTERN;
2790 return expect(token, ';', "at end of declaration");